Skip to content
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

fix(NODE-3711): retry txn end on retryable write #3045

Merged
merged 1 commit into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,10 @@ const RETRYABLE_WRITE_ERROR_CODES = new Set<number>([
MONGODB_ERROR_CODES.ExceededTimeLimit
]);

export function isRetryableEndTransactionError(error: MongoError): boolean {
dariakp marked this conversation as resolved.
Show resolved Hide resolved
return error.hasErrorLabel('RetryableWriteError');
}

export function isRetryableWriteError(error: MongoError): boolean {
if (error instanceof MongoWriteConcernError) {
return RETRYABLE_WRITE_ERROR_CODES.has(error.result?.code ?? error.code ?? 0);
Expand Down
3 changes: 2 additions & 1 deletion src/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
MongoError,
MongoInvalidArgumentError,
isRetryableError,
isRetryableEndTransactionError,
MongoCompatibilityError,
MongoNetworkError,
MongoWriteConcernError,
Expand Down Expand Up @@ -767,7 +768,7 @@ function endTransaction(session: ClientSession, commandName: string, callback: C
session.unpin();
}

if (err && isRetryableError(err as MongoError)) {
if (err && isRetryableEndTransactionError(err as MongoError)) {
// SPEC-1185: apply majority write concern when retrying commitTransaction
if (command.commitTransaction) {
// per txns spec, must unpin session in this case
Expand Down
8 changes: 1 addition & 7 deletions test/functional/transactions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,7 @@ const SKIP_TESTS = [

// Will be implemented as part of NODE-2034
'Client side error in command starting transaction',
'Client side error when transaction is in progress',

// Will be implemented as part of NODE-2538
'abortTransaction only retries once with RetryableWriteError from server',
'abortTransaction does not retry without RetryableWriteError label',
'commitTransaction does not retry error without RetryableWriteError label',
'commitTransaction retries once with RetryableWriteError from server'
'Client side error when transaction is in progress'
];

describe('Transactions Spec Legacy Tests', function () {
Expand Down
29 changes: 29 additions & 0 deletions test/unit/error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { ReplSetFixture } = require('../tools/common');
const { ns } = require('../../src/utils');
const { Topology } = require('../../src/sdam/topology');
const { MongoNetworkError, MongoWriteConcernError } = require('../../src/index');
const { isRetryableEndTransactionError } = require('../../src/error');
const {
PoolClosedError: MongoPoolClosedError,
WaitQueueTimeoutError: MongoWaitQueueTimeoutError
Expand Down Expand Up @@ -36,6 +37,34 @@ describe('MongoErrors', () => {
});
}

describe('#isRetryableEndTransactionError', function () {
context('when the error has a RetryableWriteError label', function () {
const error = new MongoNetworkError('');
error.addErrorLabel('RetryableWriteError');

it('returns true', function () {
expect(isRetryableEndTransactionError(error)).to.be.true;
});
});

context('when the error does not have a RetryableWriteError label', function () {
const error = new MongoNetworkError('');
error.addErrorLabel('InvalidLabel');

it('returns false', function () {
expect(isRetryableEndTransactionError(error)).to.be.false;
});
});

context('when the error does not have any label', function () {
const error = new MongoNetworkError('');

it('returns false', function () {
expect(isRetryableEndTransactionError(error)).to.be.false;
});
});
});

describe('when MongoNetworkError is constructed', () => {
it('should only define beforeHandshake symbol if boolean option passed in', function () {
const errorWithOptionTrue = new MongoNetworkError('', { beforeHandshake: true });
Expand Down