Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds more resiliency to retrying read operations during failures. #685

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/mongo/retryable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ module Retryable
# @since 2.1.0
NOT_MASTER = 'not master'.freeze

# Error codes received around reconfiguration
CONNECTION_ERRORS_RECONFIGURATION_CODES = [ 15988, 10276, 11600, 9001, 13639, 10009, 11002, 7 ].map(&:to_s).freeze
CONNECTION_ERRORS_MAGIC_STRINGS = [ 'could not get last error', 'connection attempt failed' ].freeze

# Execute a read operation with a retry.
#
# @example Execute the read.
Expand All @@ -43,6 +47,12 @@ def read_with_retry(&block)
block.call
rescue Error::SocketError, Error::SocketTimeoutError
retry_operation(&block)
rescue Error::OperationFailure => e
if CONNECTION_ERRORS_RECONFIGURATION_CODES.any? {|code| e.message.include?(code) } || CONNECTION_ERRORS_MAGIC_STRINGS.any? {|str| e.message.include?(str)}
retry_operation(&block)
else
raise e
end
end
end

Expand Down
8 changes: 7 additions & 1 deletion lib/mongo/server/connectable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class Server
# @since 2.0.0
module Connectable

def self.included(base)
base.__send__(:include, Mongo::Retryable)
end

# The ssl option prefix.
#
# @since 2.1.0
Expand Down Expand Up @@ -103,7 +107,9 @@ def ensure_same_process!
end

def read
ensure_connected{ |socket| Protocol::Reply.deserialize(socket) }
read_with_retry do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work here as the connection does not know anything about the cluster and the retry code calls cluster.scan!.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Thoughts on how to handle a retry if the read that ultimately gets called from here gets a socket error, then?

Stack trace again:

from /var/www/bundle/ruby/2.2.0/bundler/gems/mongo-ruby-driver-a2a7e0f949a3/lib/mongo/socket.rb:123:in `block in read'
  from /var/www/bundle/ruby/2.2.0/bundler/gems/mongo-ruby-driver-a2a7e0f949a3/lib/mongo/socket.rb:191:in `handle_errors'
  from /var/www/bundle/ruby/2.2.0/bundler/gems/mongo-ruby-driver-a2a7e0f949a3/lib/mongo/socket.rb:122:in `read'
  from /var/www/bundle/ruby/2.2.0/bundler/gems/mongo-ruby-driver-a2a7e0f949a3/lib/mongo/protocol/serializers.rb:62:in `deserialize'
  from /var/www/bundle/ruby/2.2.0/bundler/gems/mongo-ruby-driver-a2a7e0f949a3/lib/mongo/protocol/message.rb:193:in `deserialize_header'
  from /var/www/bundle/ruby/2.2.0/bundler/gems/mongo-ruby-driver-a2a7e0f949a3/lib/mongo/protocol/message.rb:83:in `deserialize'
  from /var/www/bundle/ruby/2.2.0/bundler/gems/mongo-ruby-driver-a2a7e0f949a3/lib/mongo/server/connectable.rb:106:in `block in read'
  from /var/www/bundle/ruby/2.2.0/bundler/gems/mongo-ruby-driver-a2a7e0f949a3/lib/mongo/server/connectable.rb:91:in `ensure_connected'
  from /var/www/bundle/ruby/2.2.0/bundler/gems/mongo-ruby-driver-a2a7e0f949a3/lib/mongo/server/connectable.rb:106:in `read'
  from /var/www/bundle/ruby/2.2.0/bundler/gems/mongo-ruby-driver-a2a7e0f949a3/lib/mongo/server/connection.rb:180:in `deliver'
  from /var/www/bundle/ruby/2.2.0/bundler/gems/mongo-ruby-driver-a2a7e0f949a3/lib/mongo/server/connection.rb:125:in `block in dispatch'
  from /var/www/bundle/ruby/2.2.0/bundler/gems/mongo-ruby-driver-a2a7e0f949a3/lib/mongo/monitoring/publishable.rb:47:in `publish_command'
  from /var/www/bundle/ruby/2.2.0/bundler/gems/mongo-ruby-driver-a2a7e0f949a3/lib/mongo/server/connection.rb:124:in `dispatch'
  from /var/www/bundle/ruby/2.2.0/bundler/gems/mongo-ruby-driver-a2a7e0f949a3/lib/mongo/operation/executable.rb:36:in `block in execute'

ensure_connected{ |socket| Protocol::Reply.deserialize(socket) }
end
end
end
end
Expand Down
52 changes: 52 additions & 0 deletions spec/mongo/retryable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,58 @@ def write
end
end

context 'when an operation error occurs that could not get last error' do

before do
expect(operation).to receive(:execute).and_raise(Mongo::Error::OperationFailure.new("could not get last error")).ordered
expect(cluster).to receive(:scan!).and_return(true).ordered
expect(operation).to receive(:execute).and_return(true).ordered
end

it 'executes the operation twice' do
expect(retryable.read).to be true
end
end

context 'when an operation error occurs that had a connection attempt failed' do

before do
expect(operation).to receive(:execute).and_raise(Mongo::Error::OperationFailure.new("connection attempt failed")).ordered
expect(cluster).to receive(:scan!).and_return(true).ordered
expect(operation).to receive(:execute).and_return(true).ordered
end

it 'executes the operation twice' do
expect(retryable.read).to be true
end
end

context 'when an operation error occurs with code 15988' do

before do
expect(operation).to receive(:execute).and_raise(Mongo::Error::OperationFailure.new("error querying server (15988)")).ordered
expect(cluster).to receive(:scan!).and_return(true).ordered
expect(operation).to receive(:execute).and_return(true).ordered
end

it 'executes the operation twice' do
expect(retryable.read).to be true
end
end

context 'when an operation error occurs with code 13639' do

before do
expect(operation).to receive(:execute).and_raise(Mongo::Error::OperationFailure.new("connection attempt failed (13639)")).ordered
expect(cluster).to receive(:scan!).and_return(true).ordered
expect(operation).to receive(:execute).and_return(true).ordered
end

it 'executes the operation twice' do
expect(retryable.read).to be true
end
end

context 'when a socket timeout error occurs' do

before do
Expand Down