lib/mongo/retryable.rb in mongo-2.7.0 vs lib/mongo/retryable.rb in mongo-2.7.1
- old
+ new
@@ -67,18 +67,23 @@
# ...
# end
#
# @note This only retries read operations on socket errors.
#
+ # @param [ Hash ] options Options.
# @param [ Proc ] block The block to execute.
#
+ # @option options [ String ] :retry_message Message to log when retrying.
+ #
# @return [ Result ] The result of the operation.
#
# @since 2.2.6
- def read_with_one_retry
+ def read_with_one_retry(options = nil)
yield
- rescue Error::SocketError, Error::SocketTimeoutError
+ rescue Error::SocketError, Error::SocketTimeoutError => e
+ retry_message = options && options[:retry_message]
+ log_retry(e, message: retry_message)
yield
end
# Implements write retrying functionality by yielding to the passed
# block one or more times.
@@ -100,10 +105,11 @@
# end
#
# @note This only retries operations on not master failures, since it is
# the only case we can be sure a partial write did not already occur.
#
+ # @param [ nil | Session ] session Optional session to use with the operation.
# @param [ nil | Hash | WriteConcern::Base ] write_concern The write concern.
# @param [ true | false ] ending_transaction True if the write operation is abortTransaction or
# commitTransaction, false otherwise.
# @param [ Proc ] block The block to execute.
#
@@ -120,19 +126,22 @@
unless ending_transaction || retry_write_allowed?(session, write_concern)
return legacy_write_with_retry(nil, session, &block)
end
+ # If we are here, session is not nil. A session being nil would have
+ # failed retry_write_allowed? check.
+
server = cluster.next_primary
unless ending_transaction || server.retry_writes?
return legacy_write_with_retry(server, session, &block)
end
begin
txn_num = session.in_transaction? ? session.txn_num : session.next_txn_num
- yield(server, txn_num)
+ yield(server, txn_num, false)
rescue Error::SocketError, Error::SocketTimeoutError => e
raise e if session.in_transaction? && !ending_transaction
retry_write(e, txn_num, &block)
rescue Error::OperationFailure => e
raise e if (session.in_transaction? && !ending_transaction) || !e.write_retryable?
@@ -160,11 +169,11 @@
def retry_write(original_error, txn_num, &block)
cluster.scan!(false)
server = cluster.next_primary
raise original_error unless (server.retry_writes? && txn_num)
log_retry(original_error)
- yield(server, txn_num)
+ yield(server, txn_num, true)
rescue Error::SocketError, Error::SocketTimeoutError => e
cluster.scan!(false)
raise e
rescue Error::OperationFailure => e
raise original_error unless e.write_retryable?
@@ -194,10 +203,15 @@
end
end
end
# Log a warning so that any application slow down is immediately obvious.
- def log_retry(e)
- Logger.logger.warn "Retry due to: #{e.class.name} #{e.message}"
+ def log_retry(e, options = nil)
+ message = if options && options[:message]
+ options[:message]
+ else
+ "Retry"
+ end
+ Logger.logger.warn "#{message} due to: #{e.class.name} #{e.message}"
end
end
end