From 5559f91da2e754eff4ab3ddd1034843c80055f0d Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Thu, 25 Aug 2011 18:52:20 -0400 Subject: [PATCH] RUBY-314 test for update when a node is removed --- lib/mongo/util/pool.rb | 4 ++++ lib/mongo/util/pool_manager.rb | 7 +++---- test/replica_sets/refresh_test.rb | 13 +++++++++++++ test/tools/repl_set_manager.rb | 21 +++++++++++++++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/mongo/util/pool.rb b/lib/mongo/util/pool.rb index 73e19c3..2d1ce13 100644 --- a/lib/mongo/util/pool.rb +++ b/lib/mongo/util/pool.rb @@ -67,6 +67,10 @@ module Mongo "#{@host}:#{@port}" end + def host_port + [@host, @port] + end + # Return the time it takes on average # to do a round-trip against this node. def ping_time diff --git a/lib/mongo/util/pool_manager.rb b/lib/mongo/util/pool_manager.rb index 94ea367..70dc253 100644 --- a/lib/mongo/util/pool_manager.rb +++ b/lib/mongo/util/pool_manager.rb @@ -61,10 +61,9 @@ module Mongo @primary = nil @primary_pool.close @primary_pool = nil - elsif rejected_pool = @secondary_pools.reject! {|pool| pool.host_string == node} - @secondaries.reject! do |secondary| - secondary.port == rejected_pool.port && secondary.host == rejected_pool.host - end + elsif rejected_pool = @secondary_pools.detect {|pool| pool.host_string == node} + @secondary_pools.delete(rejected_pool) + @secondaries.delete(rejected_pool.host_port) end end end diff --git a/test/replica_sets/refresh_test.rb b/test/replica_sets/refresh_test.rb index 1bf3b37..c028319 100644 --- a/test/replica_sets/refresh_test.rb +++ b/test/replica_sets/refresh_test.rb @@ -62,6 +62,19 @@ class ReplicaSetRefreshTest < Test::Unit::TestCase end def test_automated_refresh_with_removed_node + @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]], + [RS.host, RS.ports[2]], :refresh_interval => 2, :auto_refresh => true) + assert @conn.secondaries.length == 2 + p @conn.secondaries + p @conn.secondary_pools.length + + RS.remove_secondary_node + sleep(6) + + p @conn.secondaries + p @conn.secondary_pools.length + assert @conn.secondaries.length == 1 end + end diff --git a/test/tools/repl_set_manager.rb b/test/tools/repl_set_manager.rb index 9d316d6..cfe0b6c 100644 --- a/test/tools/repl_set_manager.rb +++ b/test/tools/repl_set_manager.rb @@ -103,6 +103,27 @@ class ReplSetManager @mongods[n]['start'] end + def remove_secondary_node + primary = get_node_with_state(1) + con = get_connection(primary) + config = con['local']['system.replset'].find_one + secondary = get_node_with_state(2) + host_port = "#{@host}:#{@mongods[secondary]['port']}" + @config['members'].reject! {|m| m['host'] == host_port} + + @config['version'] = config['version'] + 1 + + primary = get_node_with_state(1) + con = get_connection(primary) + + begin + con['admin'].command({'replSetReconfig' => @config}) + rescue Mongo::ConnectionFailure + end + + con.close + end + def add_node primary = get_node_with_state(1) con = get_connection(primary)