From b77b3fe1e68d411906ef25273f35d6fb9abdf2c7 Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Wed, 29 Dec 2010 13:01:05 -0500 Subject: [PATCH] RUBY-215 added Connection#read_primary? and ReplSetConnection#read_primary? --- lib/mongo/connection.rb | 26 +++++++++++++---------- lib/mongo/repl_set_connection.rb | 8 +++++++ test/replica_sets/connect_test.rb | 1 + test/replica_sets/query_secondaries.rb | 4 ++++ test/replica_sets/replication_ack_test.rb | 2 ++ 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index 094f996..a7aa60e 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -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 diff --git a/lib/mongo/repl_set_connection.rb b/lib/mongo/repl_set_connection.rb index f90d593..ecc5292 100644 --- a/lib/mongo/repl_set_connection.rb +++ b/lib/mongo/repl_set_connection.rb @@ -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 diff --git a/test/replica_sets/connect_test.rb b/test/replica_sets/connect_test.rb index 03bd0ba..3b018f7 100644 --- a/test/replica_sets/connect_test.rb +++ b/test/replica_sets/connect_test.rb @@ -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 diff --git a/test/replica_sets/query_secondaries.rb b/test/replica_sets/query_secondaries.rb index c4a28f6..fe1770e 100644 --- a/test/replica_sets/query_secondaries.rb +++ b/test/replica_sets/query_secondaries.rb @@ -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!" diff --git a/test/replica_sets/replication_ack_test.rb b/test/replica_sets/replication_ack_test.rb index 53441fd..33f0bb8 100644 --- a/test/replica_sets/replication_ack_test.rb +++ b/test/replica_sets/replication_ack_test.rb @@ -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")