-
Notifications
You must be signed in to change notification settings - Fork 31
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
Bugfixes in RFS work coordination update queries to claim leases #769
Merged
gregschohn
merged 7 commits into
opensearch-project:main
from
gregschohn:FixFalseUpdatesForRFSCoordination
Jun 26, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6bcf1c2
2 details oriented bugfixes in RFS work coordination
gregschohn a7ba17f
Merge branch 'main' into FixFalseUpdatesForRFSCoordination
gregschohn 9fb8abc
When clocks are out of sync, some client-generated queries match, but…
gregschohn 612d6e7
Merge branch 'FixFalseUpdatesForRFSCoordination' of github.com:gregsc…
gregschohn cbac6ef
Tighten when the issue was likely drift and when the response was som…
gregschohn 6c23514
Merge branch 'main' into FixFalseUpdatesForRFSCoordination
gregschohn 4bd3273
Merge branch 'main' into FixFalseUpdatesForRFSCoordination
gregschohn 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 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 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 |
---|---|---|
|
@@ -20,7 +20,9 @@ | |
@Slf4j | ||
public class OpenSearchWorkCoordinator implements IWorkCoordinator { | ||
public static final String INDEX_NAME = ".migrations_working_state"; | ||
public static final int MAX_RETRIES = 6; // at 100ms, the total delay will be 105s | ||
public static final int MAX_REFRESH_RETRIES = 6; | ||
public static final int MAX_SETUP_RETRIES = 6; | ||
public static final int MAX_JITTER_RETRIES = 6; | ||
|
||
public static final String PUT_METHOD = "PUT"; | ||
public static final String POST_METHOD = "POST"; | ||
|
@@ -104,7 +106,7 @@ | |
"}\n"; | ||
|
||
try { | ||
doUntil("setup-" + INDEX_NAME, 100, MAX_RETRIES, | ||
doUntil("setup-" + INDEX_NAME, 100, MAX_SETUP_RETRIES, | ||
() -> { | ||
try { | ||
return httpClient.makeJsonRequest(PUT_METHOD, INDEX_NAME, null, body); | ||
|
@@ -384,6 +386,8 @@ | |
" ctx._source." + EXPIRATION_FIELD_NAME + " = newExpiration;" + | ||
" ctx._source." + LEASE_HOLDER_ID_FIELD_NAME + " = params.workerId;" + | ||
" ctx._source.numAttempts += 1;" + | ||
" } else {" + | ||
" ctx.op = \\\"noop\\\";" + | ||
" }" + | ||
"\" " + // end of source script contents | ||
"}" + // end of script block | ||
|
@@ -400,15 +404,22 @@ | |
|
||
var response = httpClient.makeJsonRequest(POST_METHOD, INDEX_NAME + "/_update_by_query?refresh=true&max_docs=1", | ||
null, body); | ||
if (response.getStatusCode() == 409) { | ||
return UpdateResult.VERSION_CONFLICT; | ||
} | ||
var resultTree = objectMapper.readTree(response.getPayloadStream()); | ||
final var numUpdated = resultTree.path(UPDATED_COUNT_FIELD_NAME).longValue(); | ||
final var noops = resultTree.path("noops").longValue(); | ||
assert numUpdated <= 1; | ||
if (numUpdated > 0) { | ||
return UpdateResult.SUCCESSFUL_ACQUISITION; | ||
} else if (resultTree.path(VERSION_CONFLICTS_FIELD_NAME).longValue() > 0) { | ||
return UpdateResult.VERSION_CONFLICT; | ||
} else if (resultTree.path("total").longValue() == 0) { | ||
return UpdateResult.NOTHING_TO_ACQUIRE; | ||
} else if (noops > 0) { | ||
throw new PotentialClockDriftDetectedException("Found " + noops + | ||
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. VERSION_CONFLICT isn't being handled with an exception, what do you think about adding This cleans up the logic around |
||
" noop values in response with no successful updates"); | ||
} else { | ||
throw new IllegalStateException("Unexpected response for update: " + resultTree); | ||
} | ||
|
@@ -460,6 +471,12 @@ | |
Object transformedValue; | ||
} | ||
|
||
private static class PotentialClockDriftDetectedException extends IllegalStateException { | ||
public PotentialClockDriftDetectedException(String s) { | ||
super(s); | ||
} | ||
} | ||
|
||
public static <T,U> U doUntil(String labelThatShouldBeAContext, long initialRetryDelayMs, int maxTries, | ||
Supplier<T> supplier, Function<T,U> transformer, BiPredicate<T,U> test) | ||
throws InterruptedException, MaxTriesExceededException | ||
|
@@ -484,7 +501,7 @@ | |
|
||
private void refresh() throws IOException, InterruptedException { | ||
try { | ||
doUntil("refresh", 100, MAX_RETRIES, () -> { | ||
doUntil("refresh", 100, MAX_REFRESH_RETRIES, () -> { | ||
try { | ||
return httpClient.makeJsonRequest(GET_METHOD, INDEX_NAME + "/_refresh",null,null); | ||
} catch (IOException e) { | ||
|
@@ -499,17 +516,32 @@ | |
|
||
public WorkAcquisitionOutcome acquireNextWorkItem(Duration leaseDuration) throws IOException, InterruptedException { | ||
refresh(); | ||
int jitterRecoveryTimeMs = 10; | ||
int tries = 0; | ||
while (true) { | ||
final var obtainResult = assignOneWorkItem(leaseDuration.toSeconds()); | ||
switch (obtainResult) { | ||
case SUCCESSFUL_ACQUISITION: | ||
return getAssignedWorkItem(); | ||
case NOTHING_TO_ACQUIRE: | ||
return new NoAvailableWorkToBeDone(); | ||
case VERSION_CONFLICT: | ||
continue; | ||
default: | ||
throw new IllegalStateException("unknown result from the assignOneWorkItem: " + obtainResult); | ||
try { | ||
final var obtainResult = assignOneWorkItem(leaseDuration.toSeconds()); | ||
switch (obtainResult) { | ||
case SUCCESSFUL_ACQUISITION: | ||
return getAssignedWorkItem(); | ||
case NOTHING_TO_ACQUIRE: | ||
return new NoAvailableWorkToBeDone(); | ||
case VERSION_CONFLICT: | ||
continue; | ||
default: | ||
throw new IllegalStateException("unknown result from the assignOneWorkItem: " + obtainResult); | ||
} | ||
} catch (PotentialClockDriftDetectedException e) { | ||
if (++tries > MAX_JITTER_RETRIES) { | ||
throw e; | ||
} | ||
int finalJitterRecoveryTimeMs = jitterRecoveryTimeMs; | ||
log.atWarn().setMessage(()->"Couldn't complete work assignment. " + | ||
"Presuming that the issue was due to clock synchronization. " + | ||
"Backing off " + finalJitterRecoveryTimeMs +"ms and trying again.") | ||
.setCause(e).log(); | ||
Thread.sleep(jitterRecoveryTimeMs); | ||
jitterRecoveryTimeMs *= 2; | ||
} | ||
} | ||
} | ||
|
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.
I think we need to check for resultTree['noop'] > 0 on the below elseIf [line 413]
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.
Thanks - addressed in the latest commit (if I understood the concern)