RUBY-406 added ReplSetConnection read preference :secondary_only
This commit is contained in:
parent
78fc1debbe
commit
542b8f46da
@ -224,7 +224,6 @@ module Mongo
|
|||||||
@manager = background_manager
|
@manager = background_manager
|
||||||
old_manager.close(:soft => true)
|
old_manager.close(:soft => true)
|
||||||
@refresh_version += 1
|
@refresh_version += 1
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -324,11 +323,9 @@ module Mongo
|
|||||||
else
|
else
|
||||||
connect
|
connect
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
socket = get_socket_from_pool(self.read_pool)
|
socket = get_socket_from_pool(self.read_pool)
|
||||||
|
if !socket && @read != :secondary_only
|
||||||
if !socket
|
|
||||||
connect
|
connect
|
||||||
socket = get_socket_from_pool(self.primary_pool)
|
socket = get_socket_from_pool(self.primary_pool)
|
||||||
end
|
end
|
||||||
|
@ -232,7 +232,7 @@ module Mongo
|
|||||||
# If more than one node is available, use the ping
|
# If more than one node is available, use the ping
|
||||||
# time to figure out which nodes to choose from.
|
# time to figure out which nodes to choose from.
|
||||||
def set_read_pool
|
def set_read_pool
|
||||||
if @secondary_pools.empty?
|
if @secondary_pools.empty? && @connection.read_preference != :secondary_only
|
||||||
@read_pool = @primary_pool
|
@read_pool = @primary_pool
|
||||||
elsif @secondary_pools.size == 1
|
elsif @secondary_pools.size == 1
|
||||||
@read_pool = @secondary_pools[0]
|
@read_pool = @secondary_pools[0]
|
||||||
|
@ -59,11 +59,11 @@ module Mongo
|
|||||||
end
|
end
|
||||||
|
|
||||||
def validate_read_preference(value)
|
def validate_read_preference(value)
|
||||||
if [:primary, :secondary, nil].include?(value)
|
if [:primary, :secondary, :secondary_only, nil].include?(value)
|
||||||
return true
|
return true
|
||||||
else
|
else
|
||||||
raise MongoArgumentError, "#{value} is not a valid read preference. " +
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -13,7 +13,6 @@ class ReadPreferenceTest < Test::Unit::TestCase
|
|||||||
:refresh_mode => false, :refresh_interval => 5, :logger => log)
|
:refresh_mode => false, :refresh_interval => 5, :logger => log)
|
||||||
@db = @conn.db(MONGO_TEST_DB)
|
@db = @conn.db(MONGO_TEST_DB)
|
||||||
@db.drop_collection("test-sets")
|
@db.drop_collection("test-sets")
|
||||||
col = @db['mongo-test']
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
@ -34,6 +33,20 @@ class ReadPreferenceTest < Test::Unit::TestCase
|
|||||||
"Primary port and read port at the same!"
|
"Primary port and read port at the same!"
|
||||||
end
|
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
|
def test_query_secondaries
|
||||||
@secondary = Connection.new(@rs.host, @conn.read_pool.port, :slave_ok => true)
|
@secondary = Connection.new(@rs.host, @conn.read_pool.port, :slave_ok => true)
|
||||||
@coll = @db.collection("test-sets", :safe => {:w => 3, :wtimeout => 20000})
|
@coll = @db.collection("test-sets", :safe => {:w => 3, :wtimeout => 20000})
|
||||||
|
Loading…
Reference in New Issue
Block a user