diff --git a/lib/core/sdam/server.js b/lib/core/sdam/server.js index 7adb0dfb95..182ca9f234 100644 --- a/lib/core/sdam/server.js +++ b/lib/core/sdam/server.js @@ -167,6 +167,10 @@ class Server extends EventEmitter { return this.s.description; } + get supportsRetryableWrites() { + return supportsRetryableWrites(this); + } + get name() { return this.s.description.address; } diff --git a/lib/core/sdam/topology.js b/lib/core/sdam/topology.js index 1ca73ee48a..4b62bb3c01 100644 --- a/lib/core/sdam/topology.js +++ b/lib/core/sdam/topology.js @@ -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(); @@ -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) => { @@ -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)) {