-
Notifications
You must be signed in to change notification settings - Fork 331
Fixing the crawler framework to handle ddb outage scenario #6207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
san81
merged 3 commits into
opensearch-project:main
from
san81:crawler-source-coordinator-fix
Oct 30, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package org.opensearch.dataprepper.plugins.source.source_crawler.coordination.scheduler; | ||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
|
|
@@ -18,6 +19,7 @@ | |
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.BDDMockito.given; | ||
|
|
@@ -27,7 +29,6 @@ | |
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.verifyNoInteractions; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| public class LeaderSchedulerTest { | ||
|
|
@@ -169,13 +170,56 @@ void testWhileLoopRunnningAfterTheSleep() throws InterruptedException { | |
| void testSetLeaderProgressState_throwsExceptionOnStateMismatch() { | ||
| // Create a LeaderPartition with TokenPaginationCrawlerLeaderProgressState | ||
| LeaderPartition leaderPartition = new LeaderPartition(new TokenPaginationCrawlerLeaderProgressState("")); | ||
|
|
||
| // Try to set a different type of state (PaginationCrawlerLeaderProgressState) | ||
| PaginationCrawlerLeaderProgressState incompatibleState = new PaginationCrawlerLeaderProgressState(Instant.now()); | ||
|
|
||
| // Verify that RuntimeException is thrown due to state type mismatch | ||
| RuntimeException exception = assertThrows(RuntimeException.class, () -> { | ||
| leaderPartition.setLeaderProgressState(incompatibleState); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can move your comment to the test name to make it easier to read.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
| @DisplayName("Ensure that if DynamoDB becomes unreachable, the leader gives up the partition and retries acquisition") | ||
| void testLeaderPartitionGivenUpOnSaveFailure_andRetryAcquire() throws InterruptedException { | ||
| // This test verifies the fix for line 72: leaderPartition = null when saveProgressStateForPartition fails | ||
| // This ensures that if DynamoDB becomes unreachable, the leader gives up the partition and retries acquisition | ||
|
|
||
| LeaderScheduler leaderScheduler = new LeaderScheduler(coordinator, crawler); | ||
| leaderScheduler.setLeaseInterval(Duration.ofMillis(10)); | ||
|
|
||
| LeaderPartition leaderPartition = new LeaderPartition(new TokenPaginationCrawlerLeaderProgressState("")); | ||
| TokenPaginationCrawlerLeaderProgressState state = (TokenPaginationCrawlerLeaderProgressState) leaderPartition.getProgressState().get(); | ||
| state.setInitialized(true); | ||
| state.setLastToken(LAST_TOKEN); | ||
|
|
||
| // First acquisition succeeds, but subsequent saveProgressStateForPartition fails (simulating DynamoDB outage) | ||
| // Then second acquisition should be attempted after giving up the partition | ||
| when(coordinator.acquireAvailablePartition(LeaderPartition.PARTITION_TYPE)) | ||
| .thenReturn(Optional.of(leaderPartition)) // First acquisition succeeds | ||
| .thenReturn(Optional.of(leaderPartition)); // Second acquisition after giving up partition | ||
|
|
||
| // saveProgressStateForPartition throws exception to simulate DynamoDB outage | ||
| doThrow(new RuntimeException("DynamoDB unreachable")) | ||
| .doNothing() // Second time succeeds after recovery | ||
| .when(coordinator).saveProgressStateForPartition(any(LeaderPartition.class), any(Duration.class)); | ||
|
|
||
| ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||
| executorService.submit(leaderScheduler); | ||
|
|
||
| // Wait long enough for multiple iterations | ||
| Thread.sleep(100); | ||
| executorService.shutdownNow(); | ||
|
|
||
| // Verify that crawler was called multiple times (showing the scheduler continued to work) | ||
| verify(crawler, atLeast(2)).crawl(any(LeaderPartition.class), any(EnhancedSourceCoordinator.class)); | ||
|
|
||
| // Verify that acquireAvailablePartition was called at least twice: | ||
| // once for initial acquisition, and again after giving up due to save failure | ||
| verify(coordinator, atLeast(2)).acquireAvailablePartition(LeaderPartition.PARTITION_TYPE); | ||
|
|
||
| // Verify that saveProgressStateForPartition was attempted multiple times | ||
| verify(coordinator, atLeast(2)).saveProgressStateForPartition(any(LeaderPartition.class), any(Duration.class)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please include a unit test for this scenario and the expected outcome.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a unit test to validate reacquiring scenario.