Skip to content

Commit

Permalink
fix(NODE-3380): perform retryable write checks against server (#2861)
Browse files Browse the repository at this point in the history
  • Loading branch information
emadum committed Jun 29, 2021
1 parent e4a9a57 commit 621677a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
4 changes: 4 additions & 0 deletions lib/core/sdam/server.js
Expand Up @@ -168,6 +168,10 @@ class Server extends EventEmitter {
return this.s.description;
}

get supportsRetryableWrites() {
return supportsRetryableWrites(this);
}

get name() {
return this.s.description.address;
}
Expand Down
38 changes: 24 additions & 14 deletions lib/core/sdam/topology.js
Expand Up @@ -9,7 +9,6 @@ const events = require('./events');
const Server = require('./server').Server;
const relayEvents = require('../utils').relayEvents;
const ReadPreference = require('../topologies/read_preference');
const isRetryableWritesSupported = require('../topologies/shared').isRetryableWritesSupported;
const CoreCursor = require('../cursor').CoreCursor;
const deprecate = require('util').deprecate;
const BSON = require('../connection/utils').retrieveBSON();
Expand Down Expand Up @@ -669,12 +668,17 @@ class Topology extends EventEmitter {
return;
}

const notAlreadyRetrying = !options.retrying;
const retryWrites = !!options.retryWrites;
const hasSession = !!options.session;
const supportsRetryableWrites = server.supportsRetryableWrites;
const notInTransaction = !hasSession || !options.session.inTransaction();
const willRetryWrite =
!options.retrying &&
!!options.retryWrites &&
options.session &&
isRetryableWritesSupported(this) &&
!options.session.inTransaction() &&
notAlreadyRetrying &&
retryWrites &&
hasSession &&
supportsRetryableWrites &&
notInTransaction &&
isWriteCommand(cmd);

const cb = (err, result) => {
Expand Down Expand Up @@ -925,20 +929,26 @@ function executeWriteOperation(args, options, callback) {
const ns = args.ns;
const ops = args.ops;

const willRetryWrite =
!args.retrying &&
!!options.retryWrites &&
options.session &&
isRetryableWritesSupported(topology) &&
!options.session.inTransaction() &&
options.explain === undefined;

topology.selectServer(writableServerSelector(), options, (err, server) => {
if (err) {
callback(err, null);
return;
}

const notAlreadyRetrying = !args.retrying;
const retryWrites = !!options.retryWrites;
const hasSession = !!options.session;
const supportsRetryableWrites = server.supportsRetryableWrites;
const notInTransaction = !hasSession || !options.session.inTransaction();
const notExplaining = options.explain === undefined;
const willRetryWrite =
notAlreadyRetrying &&
retryWrites &&
hasSession &&
supportsRetryableWrites &&
notInTransaction &&
notExplaining;

const handler = (err, result) => {
if (!err) return callback(null, result);
if (!shouldRetryOperation(err)) {
Expand Down

0 comments on commit 621677a

Please sign in to comment.