RUBY-314 replica set connection and test cleanup

This commit is contained in:
Kyle Banker 2011-08-25 11:27:58 -04:00
parent 1d064e107d
commit f3fbb98fa8
17 changed files with 103 additions and 54 deletions

View File

@ -605,6 +605,23 @@ module Mongo
end end
end end
# Log a message with the given level.
def log(level, message)
return unless @logger
case level
when :debug then
@logger.debug "MONGODB [DEBUG] #{msg}"
when :warn then
@logger.warn "MONGODB [WARNING] #{msg}"
when :error then
@logger.error "MONGODB [ERROR] #{msg}"
when :fatal then
@logger.fatal "MONGODB [FATAL] #{msg}"
else
@logger.info "MONGODB [INFO] #{msg}"
end
end
# Execute the block and log the operation described by name # Execute the block and log the operation described by name
# and payload. # and payload.
# TODO: Not sure if this should take a block. # TODO: Not sure if this should take a block.

View File

@ -27,6 +27,7 @@ module Mongo
# return nil. # return nil.
def connect def connect
begin begin
socket = nil
if self.connection.connect_timeout if self.connection.connect_timeout
Mongo::TimeoutHandler.timeout(self.connection.connect_timeout, OperationTimeout) do Mongo::TimeoutHandler.timeout(self.connection.connect_timeout, OperationTimeout) do
socket = TCPSocket.new(self.host, self.port) socket = TCPSocket.new(self.host, self.port)
@ -40,8 +41,10 @@ module Mongo
else else
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
end end
rescue OperationFailure, SocketError, SystemCallError, IOError => ex rescue OperationTimeout, OperationFailure, SocketError, SystemCallError, IOError => ex
return nil self.connection.log(:debug, "Failed connection to #{host_string} with #{ex.class}, #{ex.message}.")
socket.close if socket
return nil
end end
self.socket = socket self.socket = socket

View File

@ -21,7 +21,7 @@ module Mongo
# Instantiates and manages connections to a MongoDB replica set. # Instantiates and manages connections to a MongoDB replica set.
class ReplSetConnection < Connection class ReplSetConnection < Connection
attr_reader :nodes, :secondaries, :arbiters, :secondary_pools, attr_reader :nodes, :secondaries, :arbiters, :secondary_pools,
:replica_set_name, :read_pool :replica_set_name, :read_pool, :seeds
# Create a connection to a MongoDB replica set. # Create a connection to a MongoDB replica set.
# #
@ -163,6 +163,7 @@ module Mongo
@seeds = manager.seeds @seeds = manager.seeds
@manager = manager @manager = manager
@hosts = manager.hosts @hosts = manager.hosts
@nodes = manager.nodes
end end
# If ismaster doesn't match our current view # If ismaster doesn't match our current view
@ -171,8 +172,13 @@ module Mongo
# Then take out the connection lock and replace # Then take out the connection lock and replace
# our current values. # our current values.
def refresh def refresh
background_manager = PoolManager.new(self, @seeds) return if !connected?
if !Thread.current[:background]
Thread.current[:background] = PoolManager.new(self, @seeds)
end
background_manager = Thread.current[:background]
if update_struct = background_manager.update_required?(@hosts) if update_struct = background_manager.update_required?(@hosts)
@connection_lock.synchronize do @connection_lock.synchronize do
background_manager.update(@manager, update_struct) background_manager.update(@manager, update_struct)
@ -223,15 +229,19 @@ module Mongo
@refresh_thread = nil @refresh_thread = nil
end end
@nodes.each do |member| if @nodes
member.disconnect @nodes.each do |member|
member.disconnect
end
end end
@nodes = [] @nodes = []
@read_pool = nil @read_pool = nil
@secondary_pools.each do |pool| if @secondary_pools
pool.close @secondary_pools.each do |pool|
pool.close
end
end end
@secondaries = [] @secondaries = []
@ -272,6 +282,7 @@ module Mongo
private private
def initiate_auto_refresh def initiate_auto_refresh
return unless @auto_refresh
return if @refresh_thread && @refresh_thread.alive? return if @refresh_thread && @refresh_thread.alive?
@refresh_thread = Thread.new do @refresh_thread = Thread.new do
sleep(@refresh_interval) sleep(@refresh_interval)

View File

@ -106,9 +106,10 @@ module Mongo
# therefore, it runs within a mutex. # therefore, it runs within a mutex.
def checkout_new_socket def checkout_new_socket
begin begin
socket = TCPSocket.new(@host, @port) socket = TCPSocket.new(@host, @port)
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
rescue => ex rescue => ex
socket.close if socket
raise ConnectionFailure, "Failed to connect to host #{@host} and port #{@port}: #{ex}" raise ConnectionFailure, "Failed to connect to host #{@host} and port #{@port}: #{ex}"
end end

View File

@ -2,11 +2,12 @@ module Mongo
class PoolManager class PoolManager
attr_reader :connection, :seeds, :arbiters, :primary, :secondaries, attr_reader :connection, :seeds, :arbiters, :primary, :secondaries,
:primary_pool, :read_pool, :secondary_pools, :hosts :primary_pool, :read_pool, :secondary_pools, :hosts, :nodes
def initialize(connection, seeds) def initialize(connection, seeds)
@connection = connection @connection = connection
@seeds = seeds @seeds = seeds
@refresh_node = nil
end end
def connect def connect
@ -14,6 +15,7 @@ module Mongo
nodes = connect_to_members nodes = connect_to_members
initialize_pools(nodes) initialize_pools(nodes)
update_seed_list(nodes) update_seed_list(nodes)
@nodes = nodes
end end
# Ensure that the view of the replica set is current by # Ensure that the view of the replica set is current by
@ -26,15 +28,15 @@ module Mongo
# If we're connected to nodes that are no longer part of the set, # If we're connected to nodes that are no longer part of the set,
# remove these from our set of secondary pools. # remove these from our set of secondary pools.
def update_required?(hosts) def update_required?(hosts)
node = Thread.current[:refresher_node] if !@refresh_node || !@refresh_node.active?
if !node || !node.active?
begin begin
node = get_valid_seed_node @refresh_node = get_valid_seed_node
Thread.current[:refresher_node] = node
rescue ConnectionFailure rescue ConnectionFailure
warn "Could not refresh config because no valid seed node was unavailable." warn "Could not refresh config because no valid seed node was available."
return
end end
end end
node = @refresh_node
node_list = node.node_list node_list = node.node_list
@ -104,6 +106,7 @@ module Mongo
@secondaries = [] @secondaries = []
@secondary_pools = [] @secondary_pools = []
@hosts = [] @hosts = []
@nodes = []
end end
def connected_nodes def connected_nodes
@ -204,6 +207,8 @@ module Mongo
node = Mongo::Node.new(self.connection, seed) node = Mongo::Node.new(self.connection, seed)
if node.connect && node.set_config if node.connect && node.set_config
return node return node
else
node.disconnect
end end
end end

View File

@ -6,12 +6,9 @@ require './test/replica_sets/rs_test_helper'
class ConnectTest < Test::Unit::TestCase class ConnectTest < Test::Unit::TestCase
include Mongo include Mongo
def setup
RS.restart_killed_nodes
end
def teardown def teardown
RS.restart_killed_nodes RS.restart_killed_nodes
@conn.close if defined?(@conn) && @conn
end end
def test_connect_with_deprecated_multi def test_connect_with_deprecated_multi
@ -22,25 +19,25 @@ class ConnectTest < Test::Unit::TestCase
def test_connect_bad_name def test_connect_bad_name
assert_raise_error(ReplicaSetConnectionError, "-wrong") do assert_raise_error(ReplicaSetConnectionError, "-wrong") do
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]], :rs_name => RS.name + "-wrong") [RS.host, RS.ports[2]], :rs_name => RS.name + "-wrong")
end end
end end
def test_connect_timeout # def test_connect_timeout
passed = false # passed = false
timeout = 3 # timeout = 3
begin # begin
t0 = Time.now # t0 = Time.now
ReplSetConnection.new(['192.169.169.1', 27017], :connect_timeout => timeout) # @conn = ReplSetConnection.new(['192.169.169.1', 27017], :connect_timeout => timeout)
rescue OperationTimeout # rescue OperationTimeout
passed = true # passed = true
t1 = Time.now # t1 = Time.now
end # end
assert passed # assert passed
assert t1 - t0 < timeout + 1 # assert t1 - t0 < timeout + 1
end # end
def test_connect def test_connect
@conn = ReplSetConnection.new([RS.host, RS.ports[1]], [RS.host, RS.ports[0]], @conn = ReplSetConnection.new([RS.host, RS.ports[1]], [RS.host, RS.ports[0]],
@ -84,16 +81,20 @@ class ConnectTest < Test::Unit::TestCase
def test_connect_with_secondary_node_killed def test_connect_with_secondary_node_killed
node = RS.kill_secondary node = RS.kill_secondary
@conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]], rescue_connection_failure do
[RS.host, RS.ports[2]]) @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
[RS.host, RS.ports[2]])
end
assert @conn.connected? assert @conn.connected?
end end
def test_connect_with_third_node_killed def test_connect_with_third_node_killed
RS.kill(RS.get_node_from_port(RS.ports[2])) RS.kill(RS.get_node_from_port(RS.ports[2]))
@conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]], rescue_connection_failure do
[RS.host, RS.ports[2]]) @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
[RS.host, RS.ports[2]])
end
assert @conn.connected? assert @conn.connected?
end end

View File

@ -6,12 +6,9 @@ require './test/replica_sets/rs_test_helper'
class ConnectionStringTest < Test::Unit::TestCase class ConnectionStringTest < Test::Unit::TestCase
include Mongo include Mongo
def setup
RS.restart_killed_nodes
end
def teardown def teardown
RS.restart_killed_nodes RS.restart_killed_nodes
@conn.close if @conn
end end
def test_connect_with_connection_string def test_connect_with_connection_string

View File

@ -15,6 +15,7 @@ class ReplicaSetCountTest < Test::Unit::TestCase
def teardown def teardown
RS.restart_killed_nodes RS.restart_killed_nodes
@conn.close if @conn
end end
def test_correct_count_after_insertion_reconnect def test_correct_count_after_insertion_reconnect

View File

@ -15,6 +15,7 @@ class ReplicaSetInsertTest < Test::Unit::TestCase
def teardown def teardown
RS.restart_killed_nodes RS.restart_killed_nodes
@conn.close if @conn
end end
def test_insert def test_insert

View File

@ -8,7 +8,7 @@ class ReplicaSetPooledInsertTest < Test::Unit::TestCase
def setup def setup
@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]], :pool_size => 10, :timeout => 5) [RS.host, RS.ports[2]], :pool_size => 5, :timeout => 5)
@db = @conn.db(MONGO_TEST_DB) @db = @conn.db(MONGO_TEST_DB)
@db.drop_collection("test-sets") @db.drop_collection("test-sets")
@coll = @db.collection("test-sets") @coll = @db.collection("test-sets")
@ -16,6 +16,7 @@ class ReplicaSetPooledInsertTest < Test::Unit::TestCase
def teardown def teardown
RS.restart_killed_nodes RS.restart_killed_nodes
@conn.close if @conn
end end
def test_insert def test_insert

View File

@ -14,6 +14,7 @@ class ReplicaSetQuerySecondariesTest < Test::Unit::TestCase
def teardown def teardown
RS.restart_killed_nodes RS.restart_killed_nodes
@conn.close if @conn
end end
def test_read_primary def test_read_primary

View File

@ -15,6 +15,7 @@ class ReplicaSetQueryTest < Test::Unit::TestCase
def teardown def teardown
RS.restart_killed_nodes RS.restart_killed_nodes
@conn.close if @conn
end end
def test_query def test_query

View File

@ -13,6 +13,7 @@ class ReplicaSetReconfigureTest < Test::Unit::TestCase
def teardown def teardown
RS.restart_killed_nodes RS.restart_killed_nodes
@conn.close if @conn
end end
def test_query def test_query

View File

@ -7,12 +7,9 @@ require 'benchmark'
class ReplicaSetRefreshTest < Test::Unit::TestCase class ReplicaSetRefreshTest < Test::Unit::TestCase
include Mongo include Mongo
def setup
#RS.restart_killed_nodes
end
def teardown def teardown
RS.restart_killed_nodes RS.restart_killed_nodes
@conn.close if @conn
end end
def test_connect_and_manual_refresh_with_secondaries_down def test_connect_and_manual_refresh_with_secondaries_down

View File

@ -20,6 +20,11 @@ class ReplicaSetAckTest < Test::Unit::TestCase
@col = @db.collection("test-sets") @col = @db.collection("test-sets")
end end
def teardown
RS.restart_killed_nodes
@conn.close if @conn
end
def test_safe_mode_with_w_failure def test_safe_mode_with_w_failure
assert_raise_error OperationFailure, "timeout" do assert_raise_error OperationFailure, "timeout" do
@col.insert({:foo => 1}, :safe => {:w => 4, :wtimeout => 1, :fsync => true}) @col.insert({:foo => 1}, :safe => {:w => 4, :wtimeout => 1, :fsync => true})

View File

@ -11,7 +11,7 @@ class Test::Unit::TestCase
# Generic code for rescuing connection failures and retrying operations. # Generic code for rescuing connection failures and retrying operations.
# This could be combined with some timeout functionality. # This could be combined with some timeout functionality.
def rescue_connection_failure(max_retries=60) def rescue_connection_failure(max_retries=30)
retries = 0 retries = 0
begin begin
yield yield
@ -19,7 +19,7 @@ class Test::Unit::TestCase
puts "Rescue attempt #{retries}: from #{ex}" puts "Rescue attempt #{retries}: from #{ex}"
retries += 1 retries += 1
raise ex if retries > max_retries raise ex if retries > max_retries
sleep(1) sleep(2)
retry retry
end end
end end

View File

@ -17,7 +17,7 @@ class ReplSetManager
@ports = [] @ports = []
@name = opts[:name] || 'replica-set-foo' @name = opts[:name] || 'replica-set-foo'
@host = opts[:host] || 'localhost' @host = opts[:host] || 'localhost'
@retries = opts[:retries] || 60 @retries = opts[:retries] || 30
@config = {"_id" => @name, "members" => []} @config = {"_id" => @name, "members" => []}
@durable = opts.fetch(:durable, false) @durable = opts.fetch(:durable, false)
@path = File.join(File.expand_path(File.dirname(__FILE__)), "data") @path = File.join(File.expand_path(File.dirname(__FILE__)), "data")
@ -110,8 +110,6 @@ class ReplSetManager
config = con['local']['system.replset'].find_one config = con['local']['system.replset'].find_one
@config['version'] = config['version'] + 1 @config['version'] = config['version'] + 1
p "Old config: #{config}"
p "New config: #{@config}"
# We expect a connection failure on reconfigure here. # We expect a connection failure on reconfigure here.
begin begin
@ -119,6 +117,7 @@ class ReplSetManager
rescue Mongo::ConnectionFailure rescue Mongo::ConnectionFailure
end end
con.close
ensure_up ensure_up
end end
@ -146,6 +145,7 @@ class ReplSetManager
con['admin'].command({'replSetStepDown' => 90}) con['admin'].command({'replSetStepDown' => 90})
rescue Mongo::ConnectionFailure rescue Mongo::ConnectionFailure
end end
con.close
end end
def kill_secondary def kill_secondary
@ -194,11 +194,14 @@ class ReplSetManager
con = get_connection con = get_connection
status = con['admin'].command({'replSetGetStatus' => 1}) status = con['admin'].command({'replSetGetStatus' => 1})
print "." print "."
if status['members'].all? { |m| m['health'] == 1 && [1, 2, 7].include?(m['state']) } && if status['members'].all? { |m| m['health'] == 1 &&
[1, 2, 7].include?(m['state']) } &&
status['members'].any? { |m| m['state'] == 1 } status['members'].any? { |m| m['state'] == 1 }
print "all members up!\n\n" print "all members up!\n\n"
con.close
return status return status
else else
con.close
raise Mongo::OperationFailure raise Mongo::OperationFailure
end end
end end
@ -235,6 +238,8 @@ class ReplSetManager
attempt do attempt do
con['admin'].command({'replSetInitiate' => @config}) con['admin'].command({'replSetInitiate' => @config})
end end
con.close
end end
def get_all_nodes_with_state(state) def get_all_nodes_with_state(state)
@ -295,11 +300,12 @@ class ReplSetManager
begin begin
return yield return yield
rescue Mongo::OperationFailure, Mongo::ConnectionFailure => ex rescue Mongo::OperationFailure, Mongo::ConnectionFailure => ex
sleep(1) sleep(2)
count += 1 count += 1
end end
end end
puts "NO MORE ATTEMPTS"
raise ex raise ex
end end