Skip to content

Commit

Permalink
fix(NODE-3711): retry txn end on retryable write (#3047)
Browse files Browse the repository at this point in the history
  • Loading branch information
durran authored Dec 1, 2021
1 parent 9b980c4 commit 1595140
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
7 changes: 6 additions & 1 deletion lib/core/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ const RETRYABLE_WRITE_ERROR_CODES = new Set([
MONGODB_ERROR_CODES.ExceededTimeLimit
]);

function isRetryableEndTransactionError(error) {
return error.hasErrorLabel('RetryableWriteError');
}

function isRetryableWriteError(error) {
if (error instanceof MongoWriteConcernError) {
return (
Expand Down Expand Up @@ -347,5 +351,6 @@ module.exports = {
isSDAMUnrecoverableError,
isNodeShuttingDownError,
isRetryableWriteError,
isNetworkErrorBeforeHandshake
isNetworkErrorBeforeHandshake,
isRetryableEndTransactionError
};
3 changes: 2 additions & 1 deletion lib/core/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Binary = BSON.Binary;
const uuidV4 = require('./utils').uuidV4;
const MongoError = require('./error').MongoError;
const isRetryableError = require('././error').isRetryableError;
const isRetryableEndTransactionError = require('././error').isRetryableEndTransactionError;
const MongoNetworkError = require('./error').MongoNetworkError;
const MongoWriteConcernError = require('./error').MongoWriteConcernError;
const Transaction = require('./transactions').Transaction;
Expand Down Expand Up @@ -511,7 +512,7 @@ function endTransaction(session, commandName, callback) {

// send the command
session.topology.command('admin.$cmd', command, { session }, (err, reply) => {
if (err && isRetryableError(err)) {
if (err && isRetryableEndTransactionError(err)) {
// 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 @@ -131,13 +131,7 @@ describe('Transactions', function() {

// 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'
];

return SKIP_TESTS.indexOf(spec.description) === -1;
Expand Down
30 changes: 30 additions & 0 deletions test/unit/error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const expect = require('chai').expect;
const MongoNetworkError = require('../../lib/core/error').MongoNetworkError;
const isRetryableEndTransactionError = require('../../lib/core/error')
.isRetryableEndTransactionError;

describe('MongoErrors', function() {
describe('MongoNetworkError', function() {
Expand All @@ -19,4 +21,32 @@ describe('MongoErrors', function() {
expect(Object.getOwnPropertySymbols(errorWithoutOption).length).to.equal(0);
});
});

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;
});
});
});
});

0 comments on commit 1595140

Please sign in to comment.