lib/topologies/replset.js
function executeWriteOperation(args, options, callback) {
  if (typeof options === 'function') (callback = options), (options = {});
  options = options || {};

  // TODO: once we drop Node 4, use destructuring either here or in arguments.
  const self = args.self;
  const op = args.op;
  const ns = args.ns;
  const ops = args.ops;

  if (self.state === DESTROYED) {
    return callback(new MongoError(f('topology was destroyed')));
  }

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

  if (!self.s.replicaSetState.hasPrimary()) {
    if (self.s.disconnectHandler) {
      // Not connected but we have a disconnecthandler
      return self.s.disconnectHandler.add(op, ns, ops, options, callback);
    } else if (!willRetryWrite) {
      // No server returned we had an error
      return callback(new MongoError('no primary server found'));
    }
  }

  const handler = (err, result) => {
    if (!err) return callback(null, result);
    if (!isRetryableError(err)) {
      return callback(err);
    }

    if (willRetryWrite) {
      const newArgs = Object.assign({}, args, { retrying: true });
      return executeWriteOperation(newArgs, options, callback);
    }

    // Per SDAM, remove primary from replicaset
    if (self.s.replicaSetState.primary) {
      self.s.replicaSetState.remove(self.s.replicaSetState.primary, { force: true });
    }

    return callback(err);
  };

  if (callback.operationId) {
    handler.operationId = callback.operationId;
  }

  // increment and assign txnNumber
  if (willRetryWrite) {
    options.session.incrementTransactionNumber();
    options.willRetryWrite = willRetryWrite;
  }

  self.s.replicaSetState.primary[op](ns, ops, options, handler);
}
function executeWriteOperation(args, options, callback) {
  if (typeof options === 'function') (callback = options), (options = {});
  options = options || {};

  // TODO: once we drop Node 4, use destructuring either here or in arguments.
  const self = args.self;
  const op = args.op;
  const ns = args.ns;
  const ops = args.ops;

  if (self.state === DESTROYED) {
    return callback(new MongoError(f('topology was destroyed')));
  }

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

  if (!self.s.replicaSetState.hasPrimary()) {
    if (self.s.disconnectHandler) {
      // Not connected but we have a disconnecthandler
      return self.s.disconnectHandler.add(op, ns, ops, options, callback);
    } else if (!willRetryWrite) {
      // No server returned we had an error
      return callback(new MongoError('no primary server found'));
    }
  }

  const handler = (err, result) => {
    if (!err) return callback(null, result);
    if (!isRetryableError(err)) {
      return callback(err);
    }

    if (willRetryWrite) {
      const newArgs = Object.assign({}, args, { retrying: true });
      return executeWriteOperation(newArgs, options, callback);
    }

    // Per SDAM, remove primary from replicaset
    if (self.s.replicaSetState.primary) {
      self.s.replicaSetState.remove(self.s.replicaSetState.primary, { force: true });
    }

    return callback(err);
  };

  if (callback.operationId) {
    handler.operationId = callback.operationId;
  }

  // increment and assign txnNumber
  if (willRetryWrite) {
    options.session.incrementTransactionNumber();
    options.willRetryWrite = willRetryWrite;
  }

  self.s.replicaSetState.primary[op](ns, ops, options, handler);
}
function executeWriteOperation ( args , options , callback ) { if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ; options = options || { } ; // TODO: once we drop Node 4, use destructuring either here or in arguments. const self = args . self ; const op = args . op ; const ns = args . ns ; const ops = args . ops ; if ( self . state === DESTROYED ) { return callback ( new MongoError ( f ( 'topology was destroyed' ) ) ) ; } const willRetryWrite = ! args . retrying && ! ! options . retryWrites && options . session && isRetryableWritesSupported ( self ) && ! options . session . inTransaction ( ) ; if ( ! self . s . replicaSetState . hasPrimary ( ) ) { if ( self . s . disconnectHandler ) { // Not connected but we have a disconnecthandler return self . s . disconnectHandler . add ( op , ns , ops , options , callback ) ; } else if ( ! willRetryWrite ) { // No server returned we had an error return callback ( new MongoError ( 'no primary server found' ) ) ; } } const handler = ( err , result ) => { if ( ! err ) return callback ( null , result ) ; if ( ! isRetryableError ( err ) ) { return callback ( err ) ; } if ( willRetryWrite ) { const newArgs = Object . assign ( { } , args , { retrying : true } ) ; return executeWriteOperation ( newArgs , options , callback ) ; } // Per SDAM, remove primary from replicaset if ( self . s . replicaSetState . primary ) { self . s . replicaSetState . remove ( self . s . replicaSetState . primary , { force : true } ) ; } return callback ( err ) ; } ; if ( callback . operationId ) { handler . operationId = callback . operationId ; } // increment and assign txnNumber if ( willRetryWrite ) { options . session . incrementTransactionNumber ( ) ; options . willRetryWrite = willRetryWrite ; } self . s . replicaSetState . primary [ op ] ( ns , ops , options , handler ) ; }
Execute write operation
Execute write operation