2011-08-24 22:34:00 +00:00
|
|
|
module Mongo
|
|
|
|
class PoolManager
|
|
|
|
|
|
|
|
attr_reader :connection, :seeds, :arbiters, :primary, :secondaries,
|
2011-08-30 19:59:04 +00:00
|
|
|
:primary_pool, :read_pool, :secondary_pools, :hosts, :nodes, :max_bson_size,
|
2011-10-13 21:58:20 +00:00
|
|
|
:tags_to_pools, :tag_map, :members
|
2011-08-24 22:34:00 +00:00
|
|
|
|
|
|
|
def initialize(connection, seeds)
|
|
|
|
@connection = connection
|
|
|
|
@seeds = seeds
|
2011-09-15 19:44:12 +00:00
|
|
|
@previously_connected = false
|
2011-10-13 21:58:20 +00:00
|
|
|
@refresh_required = false
|
2011-08-24 22:34:00 +00:00
|
|
|
end
|
|
|
|
|
2011-09-01 15:42:56 +00:00
|
|
|
def inspect
|
|
|
|
"<Mongo::PoolManager:0x#{self.object_id.to_s(16)} @seeds=#{@seeds}>"
|
|
|
|
end
|
|
|
|
|
2011-08-24 22:34:00 +00:00
|
|
|
def connect
|
2011-09-15 19:44:12 +00:00
|
|
|
if @previously_connected
|
|
|
|
close
|
|
|
|
end
|
|
|
|
|
2011-08-24 22:34:00 +00:00
|
|
|
initialize_data
|
2011-08-25 18:57:24 +00:00
|
|
|
members = connect_to_members
|
|
|
|
initialize_pools(members)
|
|
|
|
update_seed_list(members)
|
2011-10-12 21:13:48 +00:00
|
|
|
set_read_pool
|
2011-10-13 21:58:20 +00:00
|
|
|
set_tag_mappings
|
2011-09-15 19:44:12 +00:00
|
|
|
|
2011-08-25 18:57:24 +00:00
|
|
|
@members = members
|
2011-09-15 19:44:12 +00:00
|
|
|
@previously_connected = true
|
2011-08-24 22:34:00 +00:00
|
|
|
end
|
|
|
|
|
2011-10-12 21:13:48 +00:00
|
|
|
# We're healthy if all members are pingable and if the view
|
|
|
|
# of the replica set returned by isMaster is equivalent
|
|
|
|
# to our view. If any of these isn't the case,
|
|
|
|
# set @refresh_require to true, and return.
|
|
|
|
def check_connection_health
|
|
|
|
begin
|
|
|
|
seed = get_valid_seed_node
|
|
|
|
rescue ConnectionFailure
|
|
|
|
@refresh_required = true
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
config = seed.set_config
|
|
|
|
if !config
|
|
|
|
@refresh_required = true
|
2011-10-14 13:52:11 +00:00
|
|
|
seed.close
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if config['hosts'].length != @members.length
|
|
|
|
@refresh_required = true
|
|
|
|
seed.close
|
2011-10-12 21:13:48 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
config['hosts'].each do |host|
|
|
|
|
member = @members.detect do |m|
|
|
|
|
m.address == host
|
|
|
|
end
|
|
|
|
|
|
|
|
if member && validate_existing_member(member)
|
|
|
|
next
|
|
|
|
else
|
|
|
|
@refresh_required = true
|
2011-10-14 13:52:11 +00:00
|
|
|
seed.close
|
2011-10-12 21:13:48 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2011-10-14 13:52:11 +00:00
|
|
|
|
|
|
|
seed.close
|
2011-10-12 21:13:48 +00:00
|
|
|
end
|
2011-09-13 21:50:01 +00:00
|
|
|
|
2011-10-12 21:13:48 +00:00
|
|
|
# The replica set connection should initiate a full refresh.
|
|
|
|
def refresh_required?
|
|
|
|
@refresh_required
|
2011-09-15 19:44:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
2011-09-13 21:50:01 +00:00
|
|
|
begin
|
|
|
|
if @primary_pool
|
|
|
|
@primary_pool.close
|
2011-08-24 22:34:00 +00:00
|
|
|
end
|
|
|
|
|
2011-09-13 21:50:01 +00:00
|
|
|
if @secondary_pools
|
|
|
|
@secondary_pools.each do |pool|
|
|
|
|
pool.close
|
|
|
|
end
|
|
|
|
end
|
2011-08-24 22:34:00 +00:00
|
|
|
|
2011-09-13 21:50:01 +00:00
|
|
|
if @members
|
|
|
|
@members.each do |member|
|
|
|
|
member.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue ConnectionFailure
|
|
|
|
end
|
2011-09-15 19:44:12 +00:00
|
|
|
end
|
2011-08-24 22:34:00 +00:00
|
|
|
|
2011-09-15 19:44:12 +00:00
|
|
|
private
|
|
|
|
|
2011-10-12 21:13:48 +00:00
|
|
|
def validate_existing_member(member)
|
|
|
|
config = member.set_config
|
|
|
|
if !config
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
if member.primary?
|
|
|
|
if member.last_state == :primary
|
|
|
|
return true
|
|
|
|
else # This node is now primary, but didn't used to be.
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
elsif member.last_state == :secondary &&
|
|
|
|
member.secondary?
|
|
|
|
return true
|
2011-10-13 21:58:20 +00:00
|
|
|
else # This node isn't what it used to be.
|
|
|
|
return false
|
2011-10-12 21:13:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-15 19:44:12 +00:00
|
|
|
def initialize_data
|
2011-08-24 22:34:00 +00:00
|
|
|
@primary = nil
|
|
|
|
@primary_pool = nil
|
|
|
|
@read_pool = nil
|
|
|
|
@arbiters = []
|
|
|
|
@secondaries = []
|
|
|
|
@secondary_pools = []
|
2011-08-31 20:05:21 +00:00
|
|
|
@hosts = Set.new
|
|
|
|
@members = Set.new
|
2011-08-30 19:59:04 +00:00
|
|
|
@tags_to_pools = {}
|
2011-10-13 21:58:20 +00:00
|
|
|
@tag_map = {}
|
2011-08-24 22:34:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Connect to each member of the replica set
|
|
|
|
# as reported by the given seed node, and return
|
|
|
|
# as a list of Mongo::Node objects.
|
|
|
|
def connect_to_members
|
2011-08-25 18:57:24 +00:00
|
|
|
members = []
|
2011-08-24 22:34:00 +00:00
|
|
|
|
|
|
|
seed = get_valid_seed_node
|
|
|
|
|
|
|
|
seed.node_list.each do |host|
|
|
|
|
node = Mongo::Node.new(self.connection, host)
|
|
|
|
if node.connect && node.set_config
|
2011-08-25 18:57:24 +00:00
|
|
|
members << node
|
2011-08-24 22:34:00 +00:00
|
|
|
end
|
|
|
|
end
|
2011-09-15 19:44:12 +00:00
|
|
|
seed.close
|
2011-08-24 22:34:00 +00:00
|
|
|
|
2011-08-25 18:57:24 +00:00
|
|
|
if members.empty?
|
2011-08-24 22:34:00 +00:00
|
|
|
raise ConnectionFailure, "Failed to connect to any given member."
|
|
|
|
end
|
|
|
|
|
2011-08-25 18:57:24 +00:00
|
|
|
members
|
2011-08-24 22:34:00 +00:00
|
|
|
end
|
|
|
|
|
2011-08-30 19:59:04 +00:00
|
|
|
def associate_tags_with_pool(tags, pool)
|
|
|
|
tags.each_key do |key|
|
|
|
|
@tags_to_pools[{key => tags[key]}] ||= []
|
|
|
|
@tags_to_pools[{key => tags[key]}] << pool
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-24 22:34:00 +00:00
|
|
|
# Initialize the connection pools for the primary and secondary nodes.
|
2011-08-25 18:57:24 +00:00
|
|
|
def initialize_pools(members)
|
|
|
|
members.each do |member|
|
2011-08-24 22:34:00 +00:00
|
|
|
@hosts << member.host_string
|
|
|
|
|
|
|
|
if member.primary?
|
2011-10-12 21:13:48 +00:00
|
|
|
assign_primary(member)
|
2011-08-24 22:34:00 +00:00
|
|
|
elsif member.secondary? && !@secondaries.include?(member.host_port)
|
2011-10-12 21:13:48 +00:00
|
|
|
assign_secondary(member)
|
2011-08-24 22:34:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-25 18:57:24 +00:00
|
|
|
@max_bson_size = members.first.config['maxBsonObjectSize'] ||
|
|
|
|
Mongo::DEFAULT_MAX_BSON_SIZE
|
|
|
|
@arbiters = members.first.arbiters
|
2011-10-12 21:13:48 +00:00
|
|
|
end
|
2011-08-31 21:34:06 +00:00
|
|
|
|
2011-10-12 21:13:48 +00:00
|
|
|
def assign_primary(member)
|
|
|
|
member.last_state = :primary
|
|
|
|
@primary = member.host_port
|
|
|
|
@primary_pool = Pool.new(self.connection, member.host, member.port,
|
|
|
|
:size => self.connection.pool_size,
|
|
|
|
:timeout => self.connection.connect_timeout,
|
|
|
|
:node => member)
|
|
|
|
associate_tags_with_pool(member.tags, @primary_pool)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_secondary(member)
|
|
|
|
member.last_state = :secondary
|
|
|
|
@secondaries << member.host_port
|
|
|
|
pool = Pool.new(self.connection, member.host, member.port,
|
|
|
|
:size => self.connection.pool_size,
|
|
|
|
:timeout => self.connection.connect_timeout,
|
|
|
|
:node => member)
|
|
|
|
@secondary_pools << pool
|
|
|
|
associate_tags_with_pool(member.tags, pool)
|
2011-08-31 21:34:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# If there's more than one pool associated with
|
|
|
|
# a given tag, choose a close one using the bucket method.
|
2011-10-13 21:58:20 +00:00
|
|
|
def set_tag_mappings
|
2011-10-12 21:13:48 +00:00
|
|
|
@tags_to_pools.each do |key, pool_list|
|
2011-08-31 21:34:06 +00:00
|
|
|
if pool_list.length == 1
|
2011-10-13 21:58:20 +00:00
|
|
|
@tag_map[key] = pool_list.first
|
2011-08-31 21:34:06 +00:00
|
|
|
else
|
2011-10-13 21:58:20 +00:00
|
|
|
@tag_map[key] = nearby_pool_from_set(pool_list)
|
2011-08-31 21:34:06 +00:00
|
|
|
end
|
|
|
|
end
|
2011-08-24 22:34:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Pick a node from the set of possible secondaries.
|
|
|
|
# If more than one node is available, use the ping
|
|
|
|
# time to figure out which nodes to choose from.
|
2011-08-31 21:34:06 +00:00
|
|
|
def set_read_pool
|
2011-08-24 22:34:00 +00:00
|
|
|
if @secondary_pools.empty?
|
2011-08-31 21:34:06 +00:00
|
|
|
@read_pool = @primary_pool
|
2011-08-24 22:34:00 +00:00
|
|
|
elsif @secondary_pools.size == 1
|
2011-08-31 21:34:06 +00:00
|
|
|
@read_pool = @secondary_pools[0]
|
2011-08-24 22:34:00 +00:00
|
|
|
else
|
2011-08-31 21:34:06 +00:00
|
|
|
@read_pool = nearby_pool_from_set(@secondary_pools)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def nearby_pool_from_set(pool_set)
|
|
|
|
ping_ranges = Array.new(3) { |i| Array.new }
|
|
|
|
pool_set.each do |pool|
|
2011-08-24 22:34:00 +00:00
|
|
|
case pool.ping_time
|
|
|
|
when 0..150
|
|
|
|
ping_ranges[0] << pool
|
|
|
|
when 150..1000
|
|
|
|
ping_ranges[1] << pool
|
|
|
|
else
|
|
|
|
ping_ranges[2] << pool
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for list in ping_ranges do
|
|
|
|
break if !list.empty?
|
|
|
|
end
|
|
|
|
|
2011-08-31 21:34:06 +00:00
|
|
|
list[rand(list.length)]
|
2011-08-24 22:34:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Iterate through the list of provided seed
|
|
|
|
# nodes until we've gotten a response from the
|
|
|
|
# replica set we're trying to connect to.
|
|
|
|
#
|
|
|
|
# If we don't get a response, raise an exception.
|
|
|
|
def get_valid_seed_node
|
|
|
|
@seeds.each do |seed|
|
|
|
|
node = Mongo::Node.new(self.connection, seed)
|
|
|
|
if node.connect && node.set_config
|
|
|
|
return node
|
2011-08-25 15:27:58 +00:00
|
|
|
else
|
2011-09-15 19:44:12 +00:00
|
|
|
node.close
|
2011-08-24 22:34:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
raise ConnectionFailure, "Cannot connect to a replica set using seeds " +
|
|
|
|
"#{@seeds.map {|s| "#{s[0]}:#{s[1]}" }.join(', ')}"
|
|
|
|
end
|
|
|
|
|
2011-08-25 18:57:24 +00:00
|
|
|
def update_seed_list(members)
|
|
|
|
@seeds = members.map { |n| n.host_port }
|
2011-08-24 22:34:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|