Skip to content

Commit

Permalink
fix(NODE-3711): retry txn end on retryable write (#3045)
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Dec 1, 2021
1 parent c2a10b4 commit 7b00d0f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/error.ts
Expand Up @@ -697,6 +697,10 @@ const RETRYABLE_WRITE_ERROR_CODES = new Set<number>([
MONGODB_ERROR_CODES.ExceededTimeLimit
]);

export function isRetryableEndTransactionError(error: MongoError): boolean {
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
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
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
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

0 comments on commit 7b00d0f

Please sign in to comment.