-
Notifications
You must be signed in to change notification settings - Fork 1.8k
WIP feat(DRIVERS-3239): add exponential backoff in operation retry loop #4806
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
Draft
baileympearson
wants to merge
7
commits into
main
Choose a base branch
from
DRIVERS-3239
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+12,649
−42
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0c99724
add token bucket
baileympearson c71fd41
token bucket comments
baileympearson 27fced8
hack
baileympearson e8af442
add retry context
baileympearson fed9604
sync latest tests
baileympearson 73a3587
fix CI
baileympearson 62f20fc
done
baileympearson 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
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,3 +1,5 @@ | ||
| import { setTimeout } from 'node:timers/promises'; | ||
|
|
||
| import { MIN_SUPPORTED_SNAPSHOT_READS_WIRE_VERSION } from '../cmap/wire_protocol/constants'; | ||
| import { | ||
| isRetryableReadError, | ||
|
|
@@ -10,6 +12,7 @@ import { | |
| MongoInvalidArgumentError, | ||
| MongoNetworkError, | ||
| MongoNotConnectedError, | ||
| MongoOperationTimeoutError, | ||
| MongoRuntimeError, | ||
| MongoServerError, | ||
| MongoTransactionError, | ||
|
|
@@ -26,9 +29,16 @@ import { | |
| import type { Topology } from '../sdam/topology'; | ||
| import type { ClientSession } from '../sessions'; | ||
| import { TimeoutContext } from '../timeout'; | ||
| import { abortable, maxWireVersion, supportsRetryableWrites } from '../utils'; | ||
| import { RETRY_COST, TOKEN_REFRESH_RATE } from '../token_bucket'; | ||
| import { | ||
| abortable, | ||
| ExponentialBackoffProvider, | ||
| maxWireVersion, | ||
| supportsRetryableWrites | ||
| } from '../utils'; | ||
| import { AggregateOperation } from './aggregate'; | ||
| import { AbstractOperation, Aspect } from './operation'; | ||
| import { AbstractOperation, Aspect, RetryContext } from './operation'; | ||
| import { RunCommandOperation } from './run_command'; | ||
|
|
||
| const MMAPv1_RETRY_WRITES_ERROR_CODE = MONGODB_ERROR_CODES.IllegalOperation; | ||
| const MMAPv1_RETRY_WRITES_ERROR_MESSAGE = | ||
|
|
@@ -50,7 +60,7 @@ type ResultTypeFromOperation<TOperation extends AbstractOperation> = ReturnType< | |
| * The expectation is that this function: | ||
| * - Connects the MongoClient if it has not already been connected, see {@link autoConnect} | ||
| * - Creates a session if none is provided and cleans up the session it creates | ||
| * - Tries an operation and retries under certain conditions, see {@link tryOperation} | ||
| * - Tries an operation and retries under certain conditions, see {@link executeOperationWithRetries} | ||
| * | ||
| * @typeParam T - The operation's type | ||
| * @typeParam TResult - The type of the operation's result, calculated from T | ||
|
|
@@ -120,7 +130,7 @@ export async function executeOperation< | |
| }); | ||
|
|
||
| try { | ||
| return await tryOperation(operation, { | ||
| return await executeOperationWithRetries(operation, { | ||
| topology, | ||
| timeoutContext, | ||
| session, | ||
|
|
@@ -184,7 +194,10 @@ type RetryOptions = { | |
| * | ||
| * @param operation - The operation to execute | ||
| * */ | ||
| async function tryOperation<T extends AbstractOperation, TResult = ResultTypeFromOperation<T>>( | ||
| async function executeOperationWithRetries< | ||
|
Contributor
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. better name imo. |
||
| T extends AbstractOperation, | ||
| TResult = ResultTypeFromOperation<T> | ||
| >( | ||
| operation: T, | ||
| { topology, timeoutContext, session, readPreference }: RetryOptions | ||
| ): Promise<TResult> { | ||
|
|
@@ -233,11 +246,21 @@ async function tryOperation<T extends AbstractOperation, TResult = ResultTypeFro | |
| session.incrementTransactionNumber(); | ||
| } | ||
|
|
||
| const maxTries = willRetry ? (timeoutContext.csotEnabled() ? Infinity : 2) : 1; | ||
| let previousOperationError: MongoError | undefined; | ||
| const deprioritizedServers = new DeprioritizedServers(); | ||
|
|
||
| for (let tries = 0; tries < maxTries; tries++) { | ||
| const backoffDelayProvider = new ExponentialBackoffProvider( | ||
| 10_000, // MAX_BACKOFF | ||
| 100, // base backoff | ||
| 2 // backoff rate | ||
| ); | ||
|
|
||
| const retryContext = new RetryContext( | ||
| willRetry, | ||
| willRetry ? (timeoutContext.csotEnabled() ? Infinity : 2) : 1 | ||
| ); | ||
|
|
||
| for (; retryContext.shouldRetry(); retryContext.recordFailure(previousOperationError)) { | ||
| if (previousOperationError) { | ||
| if (hasWriteAspect && previousOperationError.code === MMAPv1_RETRY_WRITES_ERROR_CODE) { | ||
| throw new MongoServerError({ | ||
|
|
@@ -247,15 +270,46 @@ async function tryOperation<T extends AbstractOperation, TResult = ResultTypeFro | |
| }); | ||
| } | ||
|
|
||
| if (operation.hasAspect(Aspect.COMMAND_BATCHING) && !operation.canRetryWrite) { | ||
| const canRetryBackpressureError = | ||
| (operation.hasAspect(Aspect.WRITE_OPERATION) && topology.s.options.retryWrites) || | ||
| (operation.hasAspect(Aspect.READ_OPERATION) && topology.s.options.retryReads) || | ||
| (operation instanceof RunCommandOperation && topology.s.options.retryReads); | ||
| // TODO: think about whether or not willRetry checks are necessary here. | ||
| const isRetryable = | ||
| // bulk write commands are retryable if all operations in the batch are retryable | ||
| (willRetryWrite && | ||
| operation.hasAspect(Aspect.COMMAND_BATCHING) && | ||
| operation.canRetryWrite) || | ||
| // if we have a retryable read or write operation, we can retry | ||
| (hasWriteAspect && willRetryWrite && isRetryableWriteError(previousOperationError)) || | ||
| (hasReadAspect && willRetryRead && isRetryableReadError(previousOperationError)) || | ||
| // if we have a retryable, system overloaded error, we can retry | ||
| (canRetryBackpressureError && | ||
| previousOperationError.hasErrorLabel(MongoErrorLabel.SystemOverloadedError) && | ||
| previousOperationError.hasErrorLabel(MongoErrorLabel.RetryableError)); | ||
|
|
||
| if (!isRetryable) { | ||
| throw previousOperationError; | ||
| } | ||
|
|
||
| if (hasWriteAspect && !isRetryableWriteError(previousOperationError)) | ||
| throw previousOperationError; | ||
| if (previousOperationError.hasErrorLabel(MongoErrorLabel.SystemOverloadedError)) { | ||
| const delayMS = backoffDelayProvider.getNextBackoffDuration(); | ||
|
|
||
| if (hasReadAspect && !isRetryableReadError(previousOperationError)) { | ||
| throw previousOperationError; | ||
| // if the delay would exhaust the CSOT timeout, short-circuit. | ||
| if (timeoutContext.csotEnabled() && delayMS > timeoutContext.remainingTimeMS) { | ||
| throw new MongoOperationTimeoutError( | ||
| `MongoDB SystemOverload exponential backoff would exceed timeoutMS deadline: remaining CSOT deadline=${timeoutContext.remainingTimeMS}, backoff delayMS=${delayMS}`, | ||
| { | ||
| cause: previousOperationError | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| if (!topology.tokenBucket.consume(RETRY_COST)) { | ||
| throw previousOperationError; | ||
| } | ||
|
|
||
| await setTimeout(delayMS); | ||
| } | ||
|
|
||
| if ( | ||
|
|
@@ -285,19 +339,32 @@ async function tryOperation<T extends AbstractOperation, TResult = ResultTypeFro | |
| operation.server = server; | ||
|
|
||
| try { | ||
| // If tries > 0 and we are command batching we need to reset the batch. | ||
| if (tries > 0 && operation.hasAspect(Aspect.COMMAND_BATCHING)) { | ||
| // If attempt > 0 and we are command batching we need to reset the batch. | ||
| if (retryContext.isRetry && operation.hasAspect(Aspect.COMMAND_BATCHING)) { | ||
| operation.resetBatch(); | ||
| } | ||
|
|
||
| try { | ||
| const result = await server.command(operation, timeoutContext); | ||
| topology.tokenBucket.deposit( | ||
| retryContext.isRetry | ||
| ? // on successful retry, deposit the retry cost + the refresh rate. | ||
| TOKEN_REFRESH_RATE + RETRY_COST | ||
| : // otherwise, just deposit the refresh rate. | ||
| TOKEN_REFRESH_RATE | ||
| ); | ||
| return operation.handleOk(result); | ||
| } catch (error) { | ||
| return operation.handleError(error); | ||
| } | ||
| } catch (operationError) { | ||
| if (!(operationError instanceof MongoError)) throw operationError; | ||
|
|
||
| if (!operationError.hasErrorLabel(MongoErrorLabel.SystemOverloadedError)) { | ||
| // if an operation fails with an error that does not contain the SystemOverloadError, deposit 1 token. | ||
| topology.tokenBucket.deposit(RETRY_COST); | ||
| } | ||
|
|
||
| if ( | ||
| previousOperationError != null && | ||
| operationError.hasErrorLabel(MongoErrorLabel.NoWritesPerformed) | ||
|
|
@@ -312,8 +379,5 @@ async function tryOperation<T extends AbstractOperation, TResult = ResultTypeFro | |
| } | ||
| } | ||
|
|
||
| throw ( | ||
| previousOperationError ?? | ||
| new MongoRuntimeError('Tried to propagate retryability error, but no error was found.') | ||
| ); | ||
| throw previousOperationError ?? new MongoRuntimeError('ahh'); | ||
| } | ||
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
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.
edge case: if we encounter a network error (such as a failCommand with closeConnection=true) we never get a server response to update a session with, but still need to update the session's transaction, if the session is in a transaction.