From b63250e6e44e80d22dad4905f0f11c27a1b838ed Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Fri, 10 Dec 2010 11:12:18 -0500 Subject: [PATCH] Fix for connections to replica sets with 1 secondary and 1 arbiter --- lib/mongo/connection.rb | 8 +++----- test/replica_sets/connect_test.rb | 19 ++++++++++--------- test/test_helper.rb | 15 +++++++++++---- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index 68e7851..e71ce86 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -629,9 +629,9 @@ module Mongo 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 - if (size = @secondary_pools.size) > 1 + if (size = @secondary_pools.size) > 0 @read_pool = @secondary_pools[rand(size)] end end @@ -715,9 +715,7 @@ module Mongo if config['secondary'] host, port = *node @secondaries << node unless @secondaries.include?(node) - if @read_secondary - @secondary_pools << Pool.new(self, host, port, :size => @pool_size, :timeout => @timeout) - end + @secondary_pools << Pool.new(self, host, port, :size => @pool_size, :timeout => @timeout) elsif config['arbiterOnly'] @arbiters << node unless @arbiters.include?(node) end diff --git a/test/replica_sets/connect_test.rb b/test/replica_sets/connect_test.rb index e6f9703..df229d9 100644 --- a/test/replica_sets/connect_test.rb +++ b/test/replica_sets/connect_test.rb @@ -3,44 +3,45 @@ require 'mongo' require 'test/unit' 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 include Mongo def test_connect_bad_name 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") end end 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") assert @conn.connected? end def test_connect_with_first_node_down - puts "Please kill the node at 27017." + puts "Please kill the node at #{TEST_PORT}." 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? end def test_connect_with_second_node_down - puts "Please kill the node at 27018." + puts "Please kill the node at #{TEST_PORT + 1}." 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? end def test_connect_with_third_node_down - puts "Please kill the node at 27019." + puts "Please kill the node at #{TEST_PORT + 2}." 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? end end diff --git a/test/test_helper.rb b/test/test_helper.rb index f60872f..0e13370 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -25,13 +25,20 @@ unless defined? MONGO_TEST_DB MONGO_TEST_DB = 'ruby-test-' + BSON::ObjectId.new.to_s 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 include Mongo include BSON def self.standard_connection(options={}) - Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', - ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT, options) + Connection.new(TEST_HOST, TEST_PORT, options) end def standard_connection(options={}) @@ -43,11 +50,11 @@ class Test::Unit::TestCase end def self.mongo_host - ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' + TEST_HOST end def self.mongo_port - ENV['MONGO_RUBY_DRIVER_PORT'] ? ENV['MONGO_RUBY_DRIVER_PORT'].to_i : 27017 + TEST_PORT end def host_port