RUBY-215 added Connection#read_primary? and ReplSetConnection#read_primary?

This commit is contained in:
Kyle Banker 2010-12-29 13:01:05 -05:00
parent b3b1cd091a
commit b77b3fe1e6
5 changed files with 30 additions and 11 deletions

View File

@ -430,7 +430,13 @@ module Mongo
reset_connection reset_connection
config = check_is_master(@host_to_try) 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) set_primary(@host_to_try)
end end
@ -450,6 +456,14 @@ module Mongo
@primary_pool && @primary_pool.host && @primary_pool.port @primary_pool && @primary_pool.host && @primary_pool.port
end 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. # Close the connection to the database.
def close def close
@primary_pool.close if @primary_pool @primary_pool.close if @primary_pool
@ -552,16 +566,6 @@ module Mongo
@primary = nil @primary = nil
end 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) def check_is_master(node)
begin begin
host, port = *node host, port = *node

View File

@ -124,6 +124,14 @@ module Mongo
@nodes_to_try.length > 0 @nodes_to_try.length > 0
end 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. # Close the connection to the database.
def close def close
super super

View File

@ -31,6 +31,7 @@ class ConnectTest < Test::Unit::TestCase
@conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]], @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
[RS.host, RS.ports[2]], :name => RS.name) [RS.host, RS.ports[2]], :name => RS.name)
assert @conn.connected? assert @conn.connected?
assert @conn.read_primary?
assert_equal RS.primary, @conn.primary assert_equal RS.primary, @conn.primary
assert_equal RS.secondaries.sort, @conn.secondaries.sort assert_equal RS.secondaries.sort, @conn.secondaries.sort

View File

@ -16,6 +16,10 @@ class ReplicaSetQuerySecondariesTest < Test::Unit::TestCase
RS.restart_killed_nodes RS.restart_killed_nodes
end end
def test_read_primary
assert !@conn.read_primary?
end
def test_con def test_con
assert @conn.primary_pool, "No primary pool!" assert @conn.primary_pool, "No primary pool!"
assert @conn.read_pool, "No read pool!" assert @conn.read_pool, "No read pool!"

View File

@ -13,6 +13,8 @@ class ReplicaSetAckTest < Test::Unit::TestCase
@slave1 = Connection.new(@conn.secondary_pools[0].host, @slave1 = Connection.new(@conn.secondary_pools[0].host,
@conn.secondary_pools[0].port, :slave_ok => true) @conn.secondary_pools[0].port, :slave_ok => true)
assert !@slave1.read_primary?
@db = @conn.db(MONGO_TEST_DB) @db = @conn.db(MONGO_TEST_DB)
@db.drop_collection("test-sets") @db.drop_collection("test-sets")
@col = @db.collection("test-sets") @col = @db.collection("test-sets")