RUBY-379 RUBY-381 reconnect on 'not master' errors (fix close connection logic)

This commit is contained in:
Kyle Banker 2011-12-05 15:25:37 -05:00
parent c25b1f1585
commit b6ff77fcbc
5 changed files with 34 additions and 25 deletions

View File

@ -458,12 +458,13 @@ module Mongo
@primary_pool @primary_pool
end end
# The value of the read preference. Because # The value of the read preference.
# this is a single-node connection, the value
# is +:primary+, and the connection will read
# from whichever type of node it's connected to.
def read_preference def read_preference
:primary if slave_ok?
:secondary
else
:primary
end
end end
# Close the connection to the database. # Close the connection to the database.

View File

@ -86,7 +86,7 @@ module Mongo
if(!@timeout) if(!@timeout)
add_option(OP_QUERY_NO_CURSOR_TIMEOUT) add_option(OP_QUERY_NO_CURSOR_TIMEOUT)
end end
if(@connection.slave_ok?) if(@read_preference != :primary)
add_option(OP_QUERY_SLAVE_OK) add_option(OP_QUERY_SLAVE_OK)
end end
if(@tailable) if(@tailable)
@ -136,7 +136,7 @@ module Mongo
# If the server has stopped being the master (e.g., it's one of a # If the server has stopped being the master (e.g., it's one of a
# pair but it has died or something like that) then we close that # pair but it has died or something like that) then we close that
# connection. The next request will re-open on master server. # connection. The next request will re-open on master server.
if err == "not master" if err.include?("not master")
@connection.close @connection.close
raise ConnectionFailure.new(err, doc['code'], doc) raise ConnectionFailure.new(err, doc['code'], doc)
end end

View File

@ -205,7 +205,7 @@ module Mongo
def hard_refresh! def hard_refresh!
log(:info, "Initiating hard refresh...") log(:info, "Initiating hard refresh...")
discovered_seeds = @manager ? @manager.seeds : [] discovered_seeds = @manager ? @manager.seeds : []
background_manager = PoolManager.new(self, discovered_seeds | @original_seeds) background_manager = PoolManager.new(self, discovered_seeds | @seeds)
background_manager.connect background_manager.connect
# TODO: make sure that connect has succeeded # TODO: make sure that connect has succeeded
@ -218,7 +218,7 @@ module Mongo
end end
def connected? def connected?
self.primary_pool || self.read_pool @connected && (self.primary_pool || self.read_pool)
end end
# @deprecated # @deprecated
@ -261,9 +261,13 @@ module Mongo
end end
# Close the connection to the database. # Close the connection to the database.
def close def close(opts={})
if opts[:soft]
@manager.close(:soft => true) if @manager
else
@manager.close if @manager
end
@connected = false @connected = false
@manager.close(:soft => true) if @manager
end end
# If a ConnectionFailure is raised, this method will be called # If a ConnectionFailure is raised, this method will be called
@ -348,8 +352,8 @@ module Mongo
# Checkin a socket used for reading. # Checkin a socket used for reading.
def checkin_reader(socket) def checkin_reader(socket)
if !self.read_pool.checkin(socket) && if !((self.read_pool && self.read_pool.checkin(socket)) ||
!self.primary_pool.checkin(socket) (self.primary_pool && self.primary_pool.checkin(socket)))
close_socket(socket) close_socket(socket)
end end
sync_refresh sync_refresh
@ -365,7 +369,7 @@ module Mongo
def close_socket(socket) def close_socket(socket)
begin begin
socket.close socket.close if socket
rescue IOError rescue IOError
log(:info, "Tried to close socket #{socket} but already closed.") log(:info, "Tried to close socket #{socket} but already closed.")
end end
@ -388,36 +392,40 @@ module Mongo
end end
def primary def primary
@manager.primary @manager ? @manager.primary : nil
end end
# Note: might want to freeze these after connecting. # Note: might want to freeze these after connecting.
def secondaries def secondaries
@manager.secondaries @manager ? @manager.secondaries : []
end end
def hosts def hosts
@manager.hosts @manager ? @manager.hosts : []
end end
def primary_pool def primary_pool
@manager.primary_pool @manager ? @manager.primary_pool : nil
end end
def read_pool def read_pool
@manager.read_pool @manager ? @manager.read_pool : nil
end end
def secondary_pools def secondary_pools
@manager.secondary_pools @manager ? @manager.secondary_pools : []
end end
def tag_map def tag_map
@manager.tag_map @manager ? @manager.tag_map : {}
end end
def max_bson_size def max_bson_size
@manager.max_bson_size if @manager && @manager.max_bson_size
@manager.max_bson_size
else
Mongo::DEFAULT_MAX_BSON_SIZE
end
end end
private private

View File

@ -109,7 +109,7 @@ module Mongo
# The set of nodes that this class has discovered and # The set of nodes that this class has discovered and
# successfully connected to. # successfully connected to.
def seeds def seeds
@seeds @seeds || []
end end
private private
@ -280,7 +280,7 @@ module Mongo
end end
raise ConnectionFailure, "Cannot connect to a replica set using seeds " + raise ConnectionFailure, "Cannot connect to a replica set using seeds " +
"#{@seeds.map {|s| "#{s[0]}:#{s[1]}" }.join(', ')}" "#{seed_list.map {|s| "#{s[0]}:#{s[1]}" }.join(', ')}"
end end
def seed_list def seed_list

View File

@ -5,7 +5,7 @@ class ReplicaSetQueryTest < Test::Unit::TestCase
include ReplicaSetTest include ReplicaSetTest
def setup def setup
@conn = ReplSetConnection.new([self.rs.host, self.rs.ports[0], self.rs.ports[1]]) @conn = ReplSetConnection.new([self.rs.host, self.rs.ports[0]])
@db = @conn.db(MONGO_TEST_DB) @db = @conn.db(MONGO_TEST_DB)
@db.drop_collection("test-sets") @db.drop_collection("test-sets")
@coll = @db.collection("test-sets") @coll = @db.collection("test-sets")