RUBY-215 added Connection#read_primary? and ReplSetConnection#read_primary?
This commit is contained in:
parent
b3b1cd091a
commit
b77b3fe1e6
|
@ -430,7 +430,13 @@ module Mongo
|
|||
reset_connection
|
||||
|
||||
config = check_is_master(@host_to_try)
|
||||
if is_primary?(config)
|
||||
if config
|
||||
if config['ismaster'] == 1 || config['ismaster'] == true
|
||||
@read_primary = true
|
||||
elsif @slave_ok
|
||||
@read_primary = false
|
||||
end
|
||||
|
||||
set_primary(@host_to_try)
|
||||
end
|
||||
|
||||
|
@ -450,6 +456,14 @@ module Mongo
|
|||
@primary_pool && @primary_pool.host && @primary_pool.port
|
||||
end
|
||||
|
||||
# Determine whether we're reading from a primary node. If false,
|
||||
# this connection connects to a secondary node and @slave_ok is true.
|
||||
#
|
||||
# @return [Boolean]
|
||||
def read_primary?
|
||||
@read_primary
|
||||
end
|
||||
|
||||
# Close the connection to the database.
|
||||
def close
|
||||
@primary_pool.close if @primary_pool
|
||||
|
@ -552,16 +566,6 @@ module Mongo
|
|||
@primary = nil
|
||||
end
|
||||
|
||||
# Primary is defined as either a master node or a slave if
|
||||
# :slave_ok has been set to +true+.
|
||||
#
|
||||
# If a primary node is discovered, we set the the @host and @port and
|
||||
# apply any saved authentication.
|
||||
# TODO: simplify
|
||||
def is_primary?(config)
|
||||
config && (config['ismaster'] == 1 || config['ismaster'] == true) || @slave_ok
|
||||
end
|
||||
|
||||
def check_is_master(node)
|
||||
begin
|
||||
host, port = *node
|
||||
|
|
|
@ -124,6 +124,14 @@ module Mongo
|
|||
@nodes_to_try.length > 0
|
||||
end
|
||||
|
||||
# Determine whether we're reading from a primary node. If false,
|
||||
# this connection connects to a secondary node and @read_secondaries is true.
|
||||
#
|
||||
# @return [Boolean]
|
||||
def read_primary?
|
||||
!@read_pool || @read_pool.length.zero?
|
||||
end
|
||||
|
||||
# Close the connection to the database.
|
||||
def close
|
||||
super
|
||||
|
|
|
@ -31,6 +31,7 @@ class ConnectTest < Test::Unit::TestCase
|
|||
@conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
|
||||
[RS.host, RS.ports[2]], :name => RS.name)
|
||||
assert @conn.connected?
|
||||
assert @conn.read_primary?
|
||||
|
||||
assert_equal RS.primary, @conn.primary
|
||||
assert_equal RS.secondaries.sort, @conn.secondaries.sort
|
||||
|
|
|
@ -16,6 +16,10 @@ class ReplicaSetQuerySecondariesTest < Test::Unit::TestCase
|
|||
RS.restart_killed_nodes
|
||||
end
|
||||
|
||||
def test_read_primary
|
||||
assert !@conn.read_primary?
|
||||
end
|
||||
|
||||
def test_con
|
||||
assert @conn.primary_pool, "No primary pool!"
|
||||
assert @conn.read_pool, "No read pool!"
|
||||
|
|
|
@ -13,6 +13,8 @@ class ReplicaSetAckTest < Test::Unit::TestCase
|
|||
@slave1 = Connection.new(@conn.secondary_pools[0].host,
|
||||
@conn.secondary_pools[0].port, :slave_ok => true)
|
||||
|
||||
assert !@slave1.read_primary?
|
||||
|
||||
@db = @conn.db(MONGO_TEST_DB)
|
||||
@db.drop_collection("test-sets")
|
||||
@col = @db.collection("test-sets")
|
||||
|
|
Loading…
Reference in New Issue