lib/mongo/retryable.rb
def retry_write(original_error, session, txn_num, &block)
      # We do not request a scan of the cluster here, because error handling
      # for the error which triggered the retry should have updated the
      # server description and/or topology as necessary (specifically,
      # a socket error or a not master error should have marked the respective
      # server unknown). Here we just need to wait for server selection.
      server = select_server(cluster, ServerSelector.primary, session)
      unless server.retry_writes?
        # Do not need to add "modern retry" here, it should already be on
        # the first exception.
        original_error.add_note('did not retry because server selected for retry does not supoprt retryable writes')
        raise original_error
      end
      log_retry(original_error, message: 'Write retry')
      yield(server, txn_num, true)
    rescue Error::SocketError, Error::SocketTimeoutError => e
      e.add_note('modern retry')
      e.add_note('attempt 2')
      raise e
    rescue Error::OperationFailure => e
      e.add_note('modern retry')
      if e.write_retryable?
        e.add_note('attempt 2')
        raise e
      else
        original_error.add_note("later retry failed: #{e.class}: #{e}")
        raise original_error
      end
    rescue => e
      # Do not need to add "modern retry" here, it should already be on
      # the first exception.
      original_error.add_note("later retry failed: #{e.class}: #{e}")
      raise original_error
    end
# Execute a read operation returning a cursor with retrying.
#
# This method performs server selection for the specified server selector
# and yields to the provided block, which should execute the initial
# query operation and return its result. The block will be passed the
# server selected for the operation. If the block raises an exception,
# and this exception corresponds to a read retryable error, and read
# retries are enabled for the client, this method will perform server
# selection again and yield to the block again (with potentially a
# different server). If the block returns successfully, the result
# of the block (which should be a Mongo::Operation::Result) is used to
# construct a Mongo::Cursor object for the result set. The cursor
# is then returned.
#
# If modern retry reads are on (which is the default), the initial read
# operation will be retried once. If legacy retry reads are on, the
# initial read operation will be retried zero or more times depending
# on the :max_read_retries client setting, the default for which is 1.
# To disable read retries, turn off modern read retries by setting
# retry_reads: false and set :max_read_retries to 0 on the client.
#
# @api private
#
# @example Execute a read returning a cursor.
#   cursor = read_with_retry_cursor(session, server_selector, view) do |server|
#     # return a Mongo::Operation::Result
#     ...
#   end
#
# @param [ Mongo::Session ] session The session that the operation is being
#   run on.
# @param [ Mongo::ServerSelector::Selectable ] server_selector Server
#   selector for the operation.
# @param [ CollectionView ] view The +CollectionView+ defining the query.
# @param [ Proc ] block The block to execute.
#
# @return [ Cursor ] The cursor for the result set.
Execute a read operation returning a cursor with retrying.
This method performs server selection for the specified server selector
and yields to the provided block, which should execute the initial
query operation and return its result. The block will be passed the
server selected for the operation. If the block raises an exception,
and this exception corresponds to a read retryable error, and read
retries are enabled for the client, this method will perform server
selection again and yield to the block again (with potentially a
different server).

If modern retry reads are on (which is the default), the initial read
operation will be retried once. If legacy retry reads are on, the
initial read operation will be retried zero or more times depending
on the :max_read_retries client setting, the default for which is 1.
To disable read retries, turn off modern read retries by setting
retry_reads: false and set :max_read_retries to 0 on the client.
Execute a read operation returning a cursor with retrying . This method performs server selection for the specified server selector and yields to the provided block which should execute the initial query operation and return its result . The block will be passed the server selected for the operation . If the block raises an exception and this exception corresponds to a read retryable error and read retries are enabled for the client this method will perform server selection again and yield to the block again ( with potentially a different server ) . If modern retry reads are on ( which is the default ) the initial read operation will be retried once . If legacy retry reads are on the initial read operation will be retried zero or more times depending on the : max_read_retries client setting the default for which is 1 . To disable read retries turn off modern read retries by setting retry_reads : false and set : max_read_retries to 0 on the client .
def retry_write(original_error, session, txn_num, &block)
      server = select_server(cluster, ServerSelector.primary, session)
      unless server.retry_writes?
        original_error.add_note('did not retry because server selected for retry does not supoprt retryable writes')
        raise original_error
      end
      log_retry(original_error, message: 'Write retry')
      yield(server, txn_num, true)
    rescue Error::SocketError, Error::SocketTimeoutError => e
      e.add_note('modern retry')
      e.add_note('attempt 2')
      raise e
    rescue Error::OperationFailure => e
      e.add_note('modern retry')
      if e.write_retryable?
        e.add_note('attempt 2')
        raise e
      else
        original_error.add_note("later retry failed: #{e.class}: #{e}")
        raise original_error
      end
    rescue => e
      original_error.add_note("later retry failed: #{e.class}: #{e}")
      raise original_error
    end
def retry_write ( original_error , session , txn_num , & block ) server = select_server ( cluster , ServerSelector . primary , session ) unless server . retry_writes? original_error . add_note ( 'did not retry because server selected for retry does not supoprt retryable writes' ) raise original_error end log_retry ( original_error , message : 'Write retry' ) yield ( server , txn_num , true ) rescue Error :: SocketError , Error :: SocketTimeoutError => e e . add_note ( 'modern retry' ) e . add_note ( 'attempt 2' ) raise e rescue Error :: OperationFailure => e e . add_note ( 'modern retry' ) if e . write_retryable? e . add_note ( 'attempt 2' ) raise e else original_error . add_note ( "later retry failed: #{e.class}: #{e}" ) raise original_error end rescue => e original_error . add_note ( "later retry failed: #{e.class}: #{e}" ) raise original_error end
Execute a read operation returning a cursor with retrying.
Execute a read operation returning a cursor with retrying .
# We do not request a scan of the cluster here, because error handling # for the error which triggered the retry should have updated the # server description and/or topology as necessary (specifically, # a socket error or a not master error should have marked the respective # server unknown). Here we just need to wait for server selection. # Do not need to add "modern retry" here, it should already be on # the first exception. # Do not need to add "modern retry" here, it should already be on # the first exception.
The cursor for the result set.
The cursor for the result set .
 Cursor 
original_error
session
txn_num
block
block The block to execute.
block The block to execute .
api
None
example
Execute a read returning a cursor. cursor = read_with_retry_cursor(session, server_selector, view) do |server| # return a Mongo::Operation::Result ... end
Execute a read returning a cursor . cursor = read_with_retry_cursor ( session server_selector view ) do |server| # return a Mongo :: Operation :: Result ... end