Fix for connections to replica sets with 1 secondary and 1 arbiter

This commit is contained in:
Kyle Banker 2010-12-10 11:12:18 -05:00
parent 313ff2e738
commit b63250e6e4
3 changed files with 24 additions and 18 deletions

View File

@ -629,9 +629,9 @@ module Mongo
private private
# Pick a node randomly from the set of possibly secondaries. # Pick a node randomly from the set of possible secondaries.
def pick_secondary_for_read def pick_secondary_for_read
if (size = @secondary_pools.size) > 1 if (size = @secondary_pools.size) > 0
@read_pool = @secondary_pools[rand(size)] @read_pool = @secondary_pools[rand(size)]
end end
end end
@ -715,9 +715,7 @@ module Mongo
if config['secondary'] if config['secondary']
host, port = *node host, port = *node
@secondaries << node unless @secondaries.include?(node) @secondaries << node unless @secondaries.include?(node)
if @read_secondary
@secondary_pools << Pool.new(self, host, port, :size => @pool_size, :timeout => @timeout) @secondary_pools << Pool.new(self, host, port, :size => @pool_size, :timeout => @timeout)
end
elsif config['arbiterOnly'] elsif config['arbiterOnly']
@arbiters << node unless @arbiters.include?(node) @arbiters << node unless @arbiters.include?(node)
end end

View File

@ -3,44 +3,45 @@ require 'mongo'
require 'test/unit' require 'test/unit'
require './test/test_helper' require './test/test_helper'
# NOTE: This test expects a replica set of three nodes to be running on local host. # NOTE: This test expects a replica set of three nodes to be running on TEST_HOST,
# on ports TEST_PORT, TEST_PORT + 1, and TEST + 2.
class ConnectTest < Test::Unit::TestCase class ConnectTest < Test::Unit::TestCase
include Mongo include Mongo
def test_connect_bad_name def test_connect_bad_name
assert_raise_error(ReplicaSetConnectionError, "expected 'wrong-repl-set-name'") do assert_raise_error(ReplicaSetConnectionError, "expected 'wrong-repl-set-name'") do
Mongo::Connection.multi([['localhost', 27017], ['localhost', 27018], ['localhost', 27019]], Mongo::Connection.multi([[TEST_HOST, TEST_PORT], [TEST_HOST, TEST_PORT + 1], [TEST_HOST, TEST_PORT + 2]],
:rs_name => "wrong-repl-set-name") :rs_name => "wrong-repl-set-name")
end end
end end
def test_connect def test_connect
@conn = Mongo::Connection.multi([['localhost', 27017], ['localhost', 27018], ['localhost', 27019]], @conn = Mongo::Connection.multi([[TEST_HOST, TEST_PORT], [TEST_HOST, TEST_PORT + 1], [TEST_HOST, TEST_PORT + 2]],
:name => "foo") :name => "foo")
assert @conn.connected? assert @conn.connected?
end end
def test_connect_with_first_node_down def test_connect_with_first_node_down
puts "Please kill the node at 27017." puts "Please kill the node at #{TEST_PORT}."
gets gets
@conn = Mongo::Connection.multi([['localhost', 27017], ['localhost', 27018], ['localhost', 27019]]) @conn = Mongo::Connection.multi([[TEST_HOST, TEST_PORT], [TEST_HOST, TEST_PORT + 1], [TEST_HOST, TEST_PORT + 2]])
assert @conn.connected? assert @conn.connected?
end end
def test_connect_with_second_node_down def test_connect_with_second_node_down
puts "Please kill the node at 27018." puts "Please kill the node at #{TEST_PORT + 1}."
gets gets
@conn = Mongo::Connection.multi([['localhost', 27017], ['localhost', 27018], ['localhost', 27019]]) @conn = Mongo::Connection.multi([[TEST_HOST, TEST_PORT], [TEST_HOST, TEST_PORT + 1], [TEST_HOST, TEST_PORT + 2]])
assert @conn.connected? assert @conn.connected?
end end
def test_connect_with_third_node_down def test_connect_with_third_node_down
puts "Please kill the node at 27019." puts "Please kill the node at #{TEST_PORT + 2}."
gets gets
@conn = Mongo::Connection.multi([['localhost', 27017], ['localhost', 27018], ['localhost', 27019]]) @conn = Mongo::Connection.multi([[TEST_HOST, TEST_PORT], [TEST_HOST, TEST_PORT + 1], [TEST_HOST, TEST_PORT + 2]])
assert @conn.connected? assert @conn.connected?
end end
end end

View File

@ -25,13 +25,20 @@ unless defined? MONGO_TEST_DB
MONGO_TEST_DB = 'ruby-test-' + BSON::ObjectId.new.to_s MONGO_TEST_DB = 'ruby-test-' + BSON::ObjectId.new.to_s
end end
unless defined? TEST_PORT
TEST_PORT = ENV['MONGO_RUBY_DRIVER_PORT'].to_i || Connection::DEFAULT_PORT
end
unless defined? TEST_HOST
TEST_HOST = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
end
class Test::Unit::TestCase class Test::Unit::TestCase
include Mongo include Mongo
include BSON include BSON
def self.standard_connection(options={}) def self.standard_connection(options={})
Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', Connection.new(TEST_HOST, TEST_PORT, options)
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT, options)
end end
def standard_connection(options={}) def standard_connection(options={})
@ -43,11 +50,11 @@ class Test::Unit::TestCase
end end
def self.mongo_host def self.mongo_host
ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' TEST_HOST
end end
def self.mongo_port def self.mongo_port
ENV['MONGO_RUBY_DRIVER_PORT'] ? ENV['MONGO_RUBY_DRIVER_PORT'].to_i : 27017 TEST_PORT
end end
def host_port def host_port