-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(NODE-7122): exponential backoff between retries in convenient transaction API #4765
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
Open
baileympearson
wants to merge
11
commits into
main
Choose a base branch
from
NODE-7122
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.
+137
−23
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6dccb3c
misc cleanup to improve readability
baileympearson 09ec1ed
add exponential backoff to withTransaction retry loop
baileympearson e56e624
test
baileympearson 0d0b1a3
failures
baileympearson d933fea
increase test run count
baileympearson 9d2bb2c
Update for changes, test passing
baileympearson 3dad6c2
misc cleanup
baileympearson 46686b8
clarifying comment
baileympearson 112c2fa
Pavel's comments
baileympearson 7e27fba
Merge branch 'main' into NODE-7122
baileympearson 87ff29e
address Daria's prose test comments
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
87 changes: 87 additions & 0 deletions
87
test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts
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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { expect } from 'chai'; | ||
| import { test } from 'mocha'; | ||
| import * as sinon from 'sinon'; | ||
|
|
||
| import { type ClientSession, type Collection, type MongoClient } from '../../../src'; | ||
| import { configureFailPoint, type FailCommandFailPoint, measureDuration } from '../../tools/utils'; | ||
|
|
||
| const failCommand: FailCommandFailPoint = { | ||
| configureFailPoint: 'failCommand', | ||
| mode: { | ||
| times: 13 | ||
| }, | ||
| data: { | ||
| failCommands: ['commitTransaction'], | ||
| errorCode: 251 // no such transaction | ||
| } | ||
| }; | ||
|
|
||
| describe('Retry Backoff is Enforced', function () { | ||
| // 1. let client be a MongoClient | ||
| let client: MongoClient; | ||
|
|
||
| // 2. let coll be a collection | ||
| let collection: Collection; | ||
|
|
||
| beforeEach(async function () { | ||
| client = this.configuration.newClient(); | ||
| collection = client.db('foo').collection('bar'); | ||
| }); | ||
|
|
||
| afterEach(async function () { | ||
| sinon.restore(); | ||
| await client?.close(); | ||
| }); | ||
|
|
||
| test( | ||
| 'works', | ||
| { | ||
| requires: { | ||
| mongodb: '>=4.4', // failCommand | ||
| topology: '!single' // transactions can't run on standalone servers | ||
| } | ||
| }, | ||
| async function () { | ||
| const randomStub = sinon.stub(Math, 'random'); | ||
|
|
||
| // 3.i Configure the random number generator used for jitter to always return 0 | ||
| randomStub.returns(0); | ||
dariakp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // 3.ii Configure a fail point that forces 13 retries | ||
| await configureFailPoint(this.configuration, failCommand); | ||
|
|
||
| // 3.iii | ||
| const callback = async (s: ClientSession) => { | ||
| await collection.insertOne({}, { session: s }); | ||
| }; | ||
|
|
||
| // 3.iv Let no_backoff_time be the duration of the withTransaction API call | ||
| const { duration: noBackoffTime } = await measureDuration(() => { | ||
| return client.withSession(async s => { | ||
| await s.withTransaction(callback); | ||
| }); | ||
| }); | ||
|
|
||
| // 4.i Configure the random number generator used for jitter to always return 1. | ||
| randomStub.returns(1); | ||
|
|
||
| // 4.ii Configure a fail point that forces 13 retries like in step 3.2. | ||
| await configureFailPoint(this.configuration, failCommand); | ||
|
|
||
| // 4.iii Use the same callback defined in 3.3. | ||
| // 4.iv Let with_backoff_time be the duration of the withTransaction API call | ||
| const { duration: fullBackoffDuration } = await measureDuration(() => { | ||
| return client.withSession(async s => { | ||
| await s.withTransaction(callback); | ||
| }); | ||
| }); | ||
|
|
||
| // 5. Compare the two time between the two runs. | ||
|
Contributor
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. And this is how we catch spec typos 😅 time -> times |
||
| // The sum of 13 backoffs is roughly 2.2 seconds. There is a 1-second window to account for potential variance between the two runs. | ||
| expect(fullBackoffDuration).to.be.within( | ||
| noBackoffTime + 2200 - 1000, | ||
PavelSafronov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| noBackoffTime + 2200 + 1000 | ||
| ); | ||
| } | ||
| ); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.