From 542b8f46daef16397d563247dc4719819c6bc61e Mon Sep 17 00:00:00 2001 From: Tyler Brock Date: Thu, 16 Feb 2012 12:59:52 -0500 Subject: [PATCH] RUBY-406 added ReplSetConnection read preference :secondary_only --- lib/mongo/repl_set_connection.rb | 5 +---- lib/mongo/util/pool_manager.rb | 2 +- lib/mongo/util/support.rb | 4 ++-- test/replica_sets/read_preference_test.rb | 15 ++++++++++++++- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/mongo/repl_set_connection.rb b/lib/mongo/repl_set_connection.rb index 414caa7..5a69436 100644 --- a/lib/mongo/repl_set_connection.rb +++ b/lib/mongo/repl_set_connection.rb @@ -224,7 +224,6 @@ module Mongo @manager = background_manager old_manager.close(:soft => true) @refresh_version += 1 - return true end @@ -324,11 +323,9 @@ module Mongo else connect end - begin socket = get_socket_from_pool(self.read_pool) - - if !socket + if !socket && @read != :secondary_only connect socket = get_socket_from_pool(self.primary_pool) end diff --git a/lib/mongo/util/pool_manager.rb b/lib/mongo/util/pool_manager.rb index 2241421..cb26172 100644 --- a/lib/mongo/util/pool_manager.rb +++ b/lib/mongo/util/pool_manager.rb @@ -232,7 +232,7 @@ module Mongo # If more than one node is available, use the ping # time to figure out which nodes to choose from. def set_read_pool - if @secondary_pools.empty? + if @secondary_pools.empty? && @connection.read_preference != :secondary_only @read_pool = @primary_pool elsif @secondary_pools.size == 1 @read_pool = @secondary_pools[0] diff --git a/lib/mongo/util/support.rb b/lib/mongo/util/support.rb index 7a63643..c1eb8a7 100644 --- a/lib/mongo/util/support.rb +++ b/lib/mongo/util/support.rb @@ -59,11 +59,11 @@ module Mongo end def validate_read_preference(value) - if [:primary, :secondary, nil].include?(value) + if [:primary, :secondary, :secondary_only, nil].include?(value) return true else raise MongoArgumentError, "#{value} is not a valid read preference. " + - "Please specify either :primary or :secondary." + "Please specify either :primary or :secondary or :secondary_only." end end diff --git a/test/replica_sets/read_preference_test.rb b/test/replica_sets/read_preference_test.rb index 74fddc7..1981ceb 100644 --- a/test/replica_sets/read_preference_test.rb +++ b/test/replica_sets/read_preference_test.rb @@ -13,7 +13,6 @@ class ReadPreferenceTest < Test::Unit::TestCase :refresh_mode => false, :refresh_interval => 5, :logger => log) @db = @conn.db(MONGO_TEST_DB) @db.drop_collection("test-sets") - col = @db['mongo-test'] end def teardown @@ -34,6 +33,20 @@ class ReadPreferenceTest < Test::Unit::TestCase "Primary port and read port at the same!" end + def test_read_secondary_only + @conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]], :read => :secondary_only) + assert_equal @conn.read_preference, :secondary_only + + @db = @conn.db(MONGO_TEST_DB) + @coll = @db.collection("test-sets") + @coll.save({:a => 20}) + @rs.kill_all_secondaries + + assert_raise ConnectionFailure do + @coll.find_one + end + end + def test_query_secondaries @secondary = Connection.new(@rs.host, @conn.read_pool.port, :slave_ok => true) @coll = @db.collection("test-sets", :safe => {:w => 3, :wtimeout => 20000})