2010-12-10 21:00:35 +00:00
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
|
|
# --
|
2011-01-17 17:26:32 +00:00
|
|
|
# Copyright (C) 2008-2011 10gen Inc.
|
2010-12-10 21:00:35 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
# ++
|
|
|
|
|
|
|
|
module Mongo
|
|
|
|
|
2010-12-15 19:14:06 +00:00
|
|
|
# Instantiates and manages connections to a MongoDB replica set.
|
2010-12-13 19:07:32 +00:00
|
|
|
class ReplSetConnection < Connection
|
2011-11-04 18:26:12 +00:00
|
|
|
CLEANUP_INTERVAL = 300
|
|
|
|
|
2011-11-03 22:37:23 +00:00
|
|
|
attr_reader :replica_set_name, :seeds, :refresh_interval, :refresh_mode,
|
|
|
|
:refresh_version
|
2010-12-10 21:00:35 +00:00
|
|
|
|
2010-12-15 19:14:06 +00:00
|
|
|
# Create a connection to a MongoDB replica set.
|
|
|
|
#
|
|
|
|
# Once connected to a replica set, you can find out which nodes are primary, secondary, and
|
|
|
|
# arbiters with the corresponding accessors: Connection#primary, Connection#secondaries, and
|
|
|
|
# Connection#arbiters. This is useful if your application needs to connect manually to nodes other
|
|
|
|
# than the primary.
|
|
|
|
#
|
2011-03-12 13:40:29 +00:00
|
|
|
# @param [Array] args A list of host-port pairs to be used as seed nodes followed by a
|
|
|
|
# hash containing any options. See the examples below for exactly how to use the constructor.
|
2010-12-15 19:14:06 +00:00
|
|
|
#
|
2010-12-30 20:40:50 +00:00
|
|
|
# @option options [String] :rs_name (nil) The name of the replica set to connect to. You
|
|
|
|
# can use this option to verify that you're connecting to the right replica set.
|
2010-12-15 19:14:06 +00:00
|
|
|
# @option options [Boolean, Hash] :safe (false) Set the default safe-mode options
|
|
|
|
# propogated to DB objects instantiated off of this Connection. This
|
|
|
|
# default can be overridden upon instantiation of any DB by explicity setting a :safe value
|
|
|
|
# on initialization.
|
2011-09-06 18:58:03 +00:00
|
|
|
# @option options [:primary, :secondary] :read (:primary) The default read preference for Mongo::DB
|
|
|
|
# objects created from this connection object. If +:secondary+ is chosen, reads will be sent
|
|
|
|
# to one of the closest available secondary nodes. If a secondary node cannot be located, the
|
|
|
|
# read will be sent to the primary.
|
2011-09-15 22:44:02 +00:00
|
|
|
# @option options [Logger] :logger (nil) Logger instance to receive driver operation log.
|
2010-12-15 19:14:06 +00:00
|
|
|
# @option options [Integer] :pool_size (1) The maximum number of socket connections allowed per
|
|
|
|
# connection pool. Note: this setting is relevant only for multi-threaded applications.
|
2011-06-15 20:17:42 +00:00
|
|
|
# @option options [Float] :pool_timeout (5.0) When all of the connections a pool are checked out,
|
2010-12-15 19:14:06 +00:00
|
|
|
# this is the number of seconds to wait for a new connection to be released before throwing an exception.
|
|
|
|
# Note: this setting is relevant only for multi-threaded applications.
|
2012-01-21 00:12:24 +00:00
|
|
|
# @option opts [Float] :op_timeout (nil) The number of seconds to wait for a read operation to time out.
|
2011-12-13 19:51:39 +00:00
|
|
|
# @option opts [Float] :connect_timeout (30) The number of seconds to wait before timing out a
|
2011-06-15 18:20:11 +00:00
|
|
|
# connection attempt.
|
2011-09-06 15:40:25 +00:00
|
|
|
# @option opts [Boolean] :ssl (false) If true, create the connection to the server using SSL.
|
2011-11-18 21:13:19 +00:00
|
|
|
# @option opts [Boolean] :refresh_mode (false) Set this to :sync to periodically update the
|
|
|
|
# state of the connection every :refresh_interval seconds. Replica set connection failures
|
|
|
|
# will always trigger a complete refresh. This option is useful when you want to add new nodes
|
|
|
|
# or remove replica set nodes not currently in use by the driver.
|
2011-09-15 22:44:02 +00:00
|
|
|
# @option opts [Integer] :refresh_interval (90) If :refresh_mode is enabled, this is the number of seconds
|
|
|
|
# between calls to check the replica set's state.
|
2011-09-13 21:50:01 +00:00
|
|
|
# @option opts [Boolean] :require_primary (true) If true, require a primary node for the connection
|
2011-09-15 22:44:02 +00:00
|
|
|
# to succeed. Otherwise, connection will succeed as long as there's at least one secondary node.
|
2010-12-15 19:14:06 +00:00
|
|
|
#
|
2011-03-12 13:40:29 +00:00
|
|
|
# @example Connect to a replica set and provide two seed nodes. Note that the number of seed nodes does
|
|
|
|
# not have to be equal to the number of replica set members. The purpose of seed nodes is to permit
|
|
|
|
# the driver to find at least one replica set member even if a member is down.
|
2010-12-15 19:14:06 +00:00
|
|
|
# ReplSetConnection.new(['localhost', 30000], ['localhost', 30001])
|
|
|
|
#
|
2011-01-05 16:30:20 +00:00
|
|
|
# @example Connect to a replica set providing two seed nodes and ensuring a connection to the replica set named 'prod':
|
|
|
|
# ReplSetConnection.new(['localhost', 30000], ['localhost', 30001], :rs_name => 'prod')
|
|
|
|
#
|
|
|
|
# @example Connect to a replica set providing two seed nodes and allowing reads from a secondary node:
|
2010-12-15 19:14:06 +00:00
|
|
|
# ReplSetConnection.new(['localhost', 30000], ['localhost', 30001], :read_secondary => true)
|
|
|
|
#
|
|
|
|
# @see http://api.mongodb.org/ruby/current/file.REPLICA_SETS.html Replica sets in Ruby
|
|
|
|
#
|
|
|
|
# @raise [ReplicaSetConnectionError] This is raised if a replica set name is specified and the
|
|
|
|
# driver fails to connect to a replica set with that name.
|
2010-12-10 21:00:35 +00:00
|
|
|
def initialize(*args)
|
|
|
|
if args.last.is_a?(Hash)
|
2010-12-13 19:07:32 +00:00
|
|
|
opts = args.pop
|
|
|
|
else
|
|
|
|
opts = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
unless args.length > 0
|
2011-08-24 22:34:00 +00:00
|
|
|
raise MongoArgumentError, "A ReplSetConnection requires at least one seed node."
|
2010-12-10 21:00:35 +00:00
|
|
|
end
|
|
|
|
|
2011-12-05 17:27:19 +00:00
|
|
|
# The original, immutable list of seed node.
|
|
|
|
# TODO: add a method for replacing this list of node.
|
2011-08-16 20:47:07 +00:00
|
|
|
@seeds = args
|
2011-12-05 17:27:19 +00:00
|
|
|
@seeds.freeze
|
2010-12-10 21:00:35 +00:00
|
|
|
|
2011-08-31 20:05:21 +00:00
|
|
|
# TODO: get rid of this
|
2011-08-25 18:57:24 +00:00
|
|
|
@nodes = @seeds.dup
|
|
|
|
|
2011-08-24 22:34:00 +00:00
|
|
|
# Refresh
|
2011-10-26 14:26:50 +00:00
|
|
|
@refresh_mode = opts.fetch(:refresh_mode, false)
|
2011-08-24 22:34:00 +00:00
|
|
|
@refresh_interval = opts[:refresh_interval] || 90
|
2011-10-17 18:41:09 +00:00
|
|
|
@last_refresh = Time.now
|
2011-08-22 15:52:11 +00:00
|
|
|
|
2011-11-02 19:41:59 +00:00
|
|
|
# No connection manager by default.
|
2011-11-02 19:16:28 +00:00
|
|
|
@manager = nil
|
2011-11-18 20:47:06 +00:00
|
|
|
@pool_mutex = Mutex.new
|
2011-11-02 19:16:28 +00:00
|
|
|
|
2011-11-18 21:13:19 +00:00
|
|
|
if @refresh_mode == :async
|
|
|
|
warn ":async refresh mode has been deprecated. Refresh
|
|
|
|
mode will be disabled."
|
|
|
|
elsif ![:sync, false].include?(@refresh_mode)
|
2011-09-15 22:44:02 +00:00
|
|
|
raise MongoArgumentError,
|
2011-11-18 21:13:19 +00:00
|
|
|
"Refresh mode must be either :sync or false."
|
2011-09-15 22:44:02 +00:00
|
|
|
end
|
|
|
|
|
2010-12-13 19:07:32 +00:00
|
|
|
# Are we allowing reads from secondaries?
|
2011-08-29 21:49:58 +00:00
|
|
|
if opts[:read_secondary]
|
|
|
|
warn ":read_secondary options has now been deprecated and will " +
|
|
|
|
"be removed in driver v2.0. Use the :read option instead."
|
|
|
|
@read_secondary = opts.fetch(:read_secondary, false)
|
|
|
|
@read = :secondary
|
|
|
|
else
|
|
|
|
@read = opts.fetch(:read, :primary)
|
2011-09-06 18:58:03 +00:00
|
|
|
Mongo::Support.validate_read_preference(@read)
|
2011-08-29 21:49:58 +00:00
|
|
|
end
|
2010-12-13 19:07:32 +00:00
|
|
|
|
2011-08-24 22:34:00 +00:00
|
|
|
@connected = false
|
2011-11-03 22:37:23 +00:00
|
|
|
@refresh_version = 0
|
2011-08-24 22:34:00 +00:00
|
|
|
|
2011-08-16 20:47:07 +00:00
|
|
|
# Replica set name
|
|
|
|
if opts[:rs_name]
|
2011-09-06 18:30:00 +00:00
|
|
|
warn ":rs_name option has been deprecated and will be removed in v2.0. " +
|
2011-08-16 20:47:07 +00:00
|
|
|
"Please use :name instead."
|
|
|
|
@replica_set_name = opts[:rs_name]
|
|
|
|
else
|
|
|
|
@replica_set_name = opts[:name]
|
|
|
|
end
|
|
|
|
|
2011-09-13 21:50:01 +00:00
|
|
|
# Require a primary node to connect?
|
2011-09-15 21:46:59 +00:00
|
|
|
@require_primary = opts.fetch(:require_primary, true)
|
2011-09-13 21:50:01 +00:00
|
|
|
|
2010-12-13 19:07:32 +00:00
|
|
|
setup(opts)
|
2010-12-10 21:00:35 +00:00
|
|
|
end
|
|
|
|
|
2011-09-01 15:42:56 +00:00
|
|
|
def inspect
|
2011-09-26 21:48:17 +00:00
|
|
|
"<Mongo::ReplSetConnection:0x#{self.object_id.to_s(16)} @seeds=#{@seeds.inspect} " +
|
2011-09-01 15:42:56 +00:00
|
|
|
"@connected=#{@connected}>"
|
|
|
|
end
|
|
|
|
|
2011-08-24 22:34:00 +00:00
|
|
|
# Initiate a connection to the replica set.
|
2010-12-10 21:00:35 +00:00
|
|
|
def connect
|
2011-09-15 22:44:02 +00:00
|
|
|
log(:info, "Connecting...")
|
2011-11-02 21:33:34 +00:00
|
|
|
return if @connected
|
|
|
|
|
2011-12-05 17:27:19 +00:00
|
|
|
discovered_seeds = @manager ? @manager.seeds : []
|
|
|
|
@manager = PoolManager.new(self, discovered_seeds)
|
|
|
|
|
|
|
|
@manager.connect
|
|
|
|
@refresh_version += 1
|
2011-11-02 21:33:34 +00:00
|
|
|
|
|
|
|
if @require_primary && self.primary.nil? #TODO: in v2.0, we'll let this be optional and do a lazy connect.
|
|
|
|
close
|
|
|
|
raise ConnectionFailure, "Failed to connect to primary node."
|
|
|
|
elsif self.read_pool.nil?
|
|
|
|
close
|
|
|
|
raise ConnectionFailure, "Failed to connect to any node."
|
|
|
|
else
|
|
|
|
@connected = true
|
2011-08-24 22:34:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-14 13:52:11 +00:00
|
|
|
# Determine whether a replica set refresh is
|
|
|
|
# required. If so, run a hard refresh. You can
|
|
|
|
# force a hard refresh by running
|
|
|
|
# ReplSetConnection#hard_refresh!
|
2011-10-12 21:13:48 +00:00
|
|
|
#
|
2011-10-14 13:52:11 +00:00
|
|
|
# @return [Boolean] +true+ unless a hard refresh
|
|
|
|
# is run and the refresh lock can't be acquired.
|
2011-10-12 21:13:48 +00:00
|
|
|
def refresh(opts={})
|
|
|
|
if !connected?
|
2011-10-14 13:52:11 +00:00
|
|
|
log(:info, "Trying to check replica set health but not " +
|
|
|
|
"connected...")
|
|
|
|
return hard_refresh!
|
2011-10-12 21:13:48 +00:00
|
|
|
end
|
2011-08-24 22:34:00 +00:00
|
|
|
|
2011-11-15 21:26:41 +00:00
|
|
|
log(:debug, "Checking replica set connection health...")
|
2011-10-12 21:13:48 +00:00
|
|
|
@manager.check_connection_health
|
|
|
|
|
|
|
|
if @manager.refresh_required?
|
2011-10-14 13:52:11 +00:00
|
|
|
return hard_refresh!
|
2011-10-12 21:13:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
2011-08-24 22:34:00 +00:00
|
|
|
end
|
|
|
|
|
2011-10-12 21:13:48 +00:00
|
|
|
# Force a hard refresh of this connection's view
|
|
|
|
# of the replica set.
|
|
|
|
#
|
|
|
|
# @return [Boolean] +true+ if hard refresh
|
|
|
|
# occurred. +false+ is returned when unable
|
|
|
|
# to get the refresh lock.
|
|
|
|
def hard_refresh!
|
|
|
|
log(:info, "Initiating hard refresh...")
|
2011-12-05 17:27:19 +00:00
|
|
|
discovered_seeds = @manager ? @manager.seeds : []
|
2011-12-05 20:25:37 +00:00
|
|
|
background_manager = PoolManager.new(self, discovered_seeds | @seeds)
|
2011-11-02 19:16:28 +00:00
|
|
|
background_manager.connect
|
2011-09-15 19:44:12 +00:00
|
|
|
|
2011-11-02 21:33:34 +00:00
|
|
|
# TODO: make sure that connect has succeeded
|
2011-11-04 13:23:41 +00:00
|
|
|
old_manager = @manager
|
2011-12-05 17:27:19 +00:00
|
|
|
@manager = background_manager
|
2011-11-04 13:23:41 +00:00
|
|
|
old_manager.close(:soft => true)
|
2011-12-05 17:27:19 +00:00
|
|
|
@refresh_version += 1
|
2011-10-14 13:52:11 +00:00
|
|
|
|
2011-09-15 19:44:12 +00:00
|
|
|
return true
|
2010-12-10 21:00:35 +00:00
|
|
|
end
|
|
|
|
|
2011-08-16 20:47:07 +00:00
|
|
|
def connected?
|
2011-12-05 20:25:37 +00:00
|
|
|
@connected && (self.primary_pool || self.read_pool)
|
2011-08-16 20:47:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# @deprecated
|
2010-12-10 21:00:35 +00:00
|
|
|
def connecting?
|
2011-09-15 22:44:02 +00:00
|
|
|
warn "ReplSetConnection#connecting? is deprecated and will be removed in v2.0."
|
2011-08-16 20:47:07 +00:00
|
|
|
false
|
2010-12-10 21:00:35 +00:00
|
|
|
end
|
|
|
|
|
2011-03-23 20:34:42 +00:00
|
|
|
# The replica set primary's host name.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def host
|
2011-11-02 21:33:34 +00:00
|
|
|
self.primary_pool.host
|
2011-03-23 20:34:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# The replica set primary's port.
|
|
|
|
#
|
|
|
|
# @return [Integer]
|
|
|
|
def port
|
2011-11-02 21:33:34 +00:00
|
|
|
self.primary_pool.port
|
2011-03-23 20:34:42 +00:00
|
|
|
end
|
|
|
|
|
2011-08-25 18:57:24 +00:00
|
|
|
def nodes
|
2011-09-15 21:46:59 +00:00
|
|
|
warn "ReplSetConnection#nodes is DEPRECATED and will be removed in v2.0. " +
|
|
|
|
"Please use ReplSetConnection#seeds instead."
|
2011-08-25 18:57:24 +00:00
|
|
|
@seeds
|
|
|
|
end
|
|
|
|
|
2010-12-29 18:01:05 +00:00
|
|
|
# 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?
|
2011-11-02 19:41:59 +00:00
|
|
|
self.read_pool == self.primary_pool
|
2010-12-29 18:01:05 +00:00
|
|
|
end
|
2011-01-17 18:37:41 +00:00
|
|
|
alias :primary? :read_primary?
|
2010-12-29 18:01:05 +00:00
|
|
|
|
2011-08-29 21:49:58 +00:00
|
|
|
def read_preference
|
|
|
|
@read
|
|
|
|
end
|
|
|
|
|
2010-12-10 21:00:35 +00:00
|
|
|
# Close the connection to the database.
|
2011-12-05 20:25:37 +00:00
|
|
|
def close(opts={})
|
|
|
|
if opts[:soft]
|
|
|
|
@manager.close(:soft => true) if @manager
|
|
|
|
else
|
|
|
|
@manager.close if @manager
|
|
|
|
end
|
2011-11-02 21:33:34 +00:00
|
|
|
@connected = false
|
2010-12-10 21:00:35 +00:00
|
|
|
end
|
|
|
|
|
2011-03-29 16:18:58 +00:00
|
|
|
# If a ConnectionFailure is raised, this method will be called
|
|
|
|
# to close the connection and reset connection values.
|
|
|
|
# @deprecated
|
|
|
|
def reset_connection
|
|
|
|
close
|
2011-09-06 18:30:00 +00:00
|
|
|
warn "ReplSetConnection#reset_connection is now deprecated and will be removed in v2.0. " +
|
2011-03-29 16:18:58 +00:00
|
|
|
"Use ReplSetConnection#close instead."
|
|
|
|
end
|
|
|
|
|
2011-08-29 21:49:58 +00:00
|
|
|
# Returns +true+ if it's okay to read from a secondary node.
|
|
|
|
# Since this is a replica set, this must always be true.
|
2010-12-15 17:36:43 +00:00
|
|
|
#
|
2011-08-29 21:49:58 +00:00
|
|
|
# This method exist primarily so that Cursor objects will
|
|
|
|
# generate query messages with a slaveOkay value of +true+.
|
|
|
|
#
|
|
|
|
# @return [Boolean] +true+
|
2010-12-15 17:36:43 +00:00
|
|
|
def slave_ok?
|
2011-08-29 21:49:58 +00:00
|
|
|
true
|
2010-12-15 17:36:43 +00:00
|
|
|
end
|
|
|
|
|
2011-01-31 19:47:05 +00:00
|
|
|
def authenticate_pools
|
2011-11-02 21:33:34 +00:00
|
|
|
self.primary_pool.authenticate_existing
|
2011-11-02 19:41:59 +00:00
|
|
|
self.secondary_pools.each do |pool|
|
2011-01-31 19:47:05 +00:00
|
|
|
pool.authenticate_existing
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def logout_pools(db)
|
2011-11-02 21:33:34 +00:00
|
|
|
self.primary_pool.logout_existing(db)
|
2011-11-02 19:41:59 +00:00
|
|
|
self.secondary_pools.each do |pool|
|
2011-01-31 19:47:05 +00:00
|
|
|
pool.logout_existing(db)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-17 18:41:09 +00:00
|
|
|
# Checkout a socket for reading (i.e., a secondary node).
|
|
|
|
# Note that @read_pool might point to the primary pool
|
|
|
|
# if no read pool has been defined.
|
|
|
|
def checkout_reader
|
2011-12-06 19:38:56 +00:00
|
|
|
if connected?
|
|
|
|
sync_refresh
|
|
|
|
else
|
|
|
|
connect
|
|
|
|
end
|
|
|
|
|
2011-12-02 20:37:05 +00:00
|
|
|
begin
|
|
|
|
socket = get_socket_from_pool(self.read_pool)
|
|
|
|
|
|
|
|
if !socket
|
|
|
|
connect
|
|
|
|
socket = get_socket_from_pool(self.primary_pool)
|
|
|
|
end
|
|
|
|
rescue => ex
|
|
|
|
checkin(socket) if socket
|
|
|
|
raise ex
|
2011-10-17 18:41:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if socket
|
|
|
|
socket
|
|
|
|
else
|
|
|
|
raise ConnectionFailure.new("Could not connect to a node for reading.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Checkout a socket for writing (i.e., a primary node).
|
|
|
|
def checkout_writer
|
2011-12-06 19:38:56 +00:00
|
|
|
if connected?
|
|
|
|
sync_refresh
|
|
|
|
else
|
|
|
|
connect
|
|
|
|
end
|
2011-12-02 20:37:05 +00:00
|
|
|
begin
|
2011-11-02 19:41:59 +00:00
|
|
|
socket = get_socket_from_pool(self.primary_pool)
|
2011-12-02 20:37:05 +00:00
|
|
|
|
|
|
|
if !socket
|
|
|
|
connect
|
|
|
|
socket = get_socket_from_pool(self.primary_pool)
|
|
|
|
end
|
|
|
|
rescue => ex
|
|
|
|
checkin(socket)
|
|
|
|
raise ex
|
2011-10-17 18:41:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if socket
|
|
|
|
socket
|
|
|
|
else
|
|
|
|
raise ConnectionFailure.new("Could not connect to primary node.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-18 20:47:06 +00:00
|
|
|
# Checkin a socket used for reading.
|
|
|
|
def checkin_reader(socket)
|
2011-12-05 20:25:37 +00:00
|
|
|
if !((self.read_pool && self.read_pool.checkin(socket)) ||
|
|
|
|
(self.primary_pool && self.primary_pool.checkin(socket)))
|
2011-11-02 21:33:34 +00:00
|
|
|
close_socket(socket)
|
2011-10-17 18:41:09 +00:00
|
|
|
end
|
2011-11-18 20:47:06 +00:00
|
|
|
sync_refresh
|
|
|
|
end
|
2011-10-17 18:41:09 +00:00
|
|
|
|
2011-11-18 20:47:06 +00:00
|
|
|
# Checkin a socket used for writing.
|
|
|
|
def checkin_writer(socket)
|
2011-12-01 17:01:13 +00:00
|
|
|
if !self.primary_pool || !self.primary_pool.checkin(socket)
|
2011-11-18 20:47:06 +00:00
|
|
|
close_socket(socket)
|
2011-10-17 18:41:09 +00:00
|
|
|
end
|
2011-11-18 20:47:06 +00:00
|
|
|
sync_refresh
|
2011-10-17 18:41:09 +00:00
|
|
|
end
|
|
|
|
|
2011-11-02 21:33:34 +00:00
|
|
|
def close_socket(socket)
|
|
|
|
begin
|
2011-12-05 20:25:37 +00:00
|
|
|
socket.close if socket
|
2011-11-02 21:33:34 +00:00
|
|
|
rescue IOError
|
|
|
|
log(:info, "Tried to close socket #{socket} but already closed.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-17 18:41:09 +00:00
|
|
|
def get_socket_from_pool(pool)
|
|
|
|
begin
|
2011-11-02 21:33:34 +00:00
|
|
|
if pool
|
|
|
|
socket = pool.checkout
|
|
|
|
socket
|
2011-10-17 18:41:09 +00:00
|
|
|
end
|
|
|
|
rescue ConnectionFailure => ex
|
|
|
|
log(:info, "Failed to checkout from #{pool} with #{ex.class}; #{ex.message}")
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-02 19:16:28 +00:00
|
|
|
def arbiters
|
2011-11-02 19:41:59 +00:00
|
|
|
@manager.arbiters.nil? ? [] : @manager.arbiters
|
2011-11-02 19:16:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def primary
|
2011-12-05 20:25:37 +00:00
|
|
|
@manager ? @manager.primary : nil
|
2011-11-02 19:16:28 +00:00
|
|
|
end
|
|
|
|
|
2011-11-02 19:41:59 +00:00
|
|
|
# Note: might want to freeze these after connecting.
|
2011-11-02 19:16:28 +00:00
|
|
|
def secondaries
|
2011-12-05 20:25:37 +00:00
|
|
|
@manager ? @manager.secondaries : []
|
2011-11-02 19:16:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def hosts
|
2011-12-05 20:25:37 +00:00
|
|
|
@manager ? @manager.hosts : []
|
2011-11-02 19:16:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def primary_pool
|
2011-12-05 20:25:37 +00:00
|
|
|
@manager ? @manager.primary_pool : nil
|
2011-11-02 19:16:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def read_pool
|
2011-12-05 20:25:37 +00:00
|
|
|
@manager ? @manager.read_pool : nil
|
2011-11-02 19:16:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def secondary_pools
|
2011-12-05 20:25:37 +00:00
|
|
|
@manager ? @manager.secondary_pools : []
|
2011-11-02 19:16:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def tag_map
|
2011-12-05 20:25:37 +00:00
|
|
|
@manager ? @manager.tag_map : {}
|
2011-11-02 19:16:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def max_bson_size
|
2011-12-05 20:25:37 +00:00
|
|
|
if @manager && @manager.max_bson_size
|
|
|
|
@manager.max_bson_size
|
|
|
|
else
|
|
|
|
Mongo::DEFAULT_MAX_BSON_SIZE
|
|
|
|
end
|
2011-11-02 19:16:28 +00:00
|
|
|
end
|
|
|
|
|
2010-12-10 21:00:35 +00:00
|
|
|
private
|
|
|
|
|
2011-11-02 22:00:52 +00:00
|
|
|
# Generic initialization code.
|
|
|
|
def setup(opts)
|
|
|
|
# Default maximum BSON object size
|
|
|
|
@max_bson_size = Mongo::DEFAULT_MAX_BSON_SIZE
|
|
|
|
|
|
|
|
@safe_mutex_lock = Mutex.new
|
|
|
|
@safe_mutexes = Hash.new {|hash, key| hash[key] = Mutex.new}
|
|
|
|
|
|
|
|
# Determine whether to use SSL.
|
|
|
|
@ssl = opts.fetch(:ssl, false)
|
|
|
|
if @ssl
|
|
|
|
@socket_class = Mongo::SSLSocket
|
|
|
|
else
|
|
|
|
@socket_class = ::TCPSocket
|
|
|
|
end
|
|
|
|
|
|
|
|
# Authentication objects
|
|
|
|
@auths = opts.fetch(:auths, [])
|
|
|
|
|
|
|
|
# Lock for request ids.
|
|
|
|
@id_lock = Mutex.new
|
|
|
|
|
|
|
|
# Pool size and timeout.
|
|
|
|
@pool_size = opts[:pool_size] || 1
|
|
|
|
if opts[:timeout]
|
|
|
|
warn "The :timeout option has been deprecated " +
|
|
|
|
"and will be removed in the 2.0 release. Use :pool_timeout instead."
|
|
|
|
end
|
|
|
|
@pool_timeout = opts[:pool_timeout] || opts[:timeout] || 5.0
|
|
|
|
|
|
|
|
# Timeout on socket read operation.
|
2012-01-21 00:12:24 +00:00
|
|
|
@op_timeout = opts[:op_timeout] || nil
|
2011-11-02 22:00:52 +00:00
|
|
|
|
|
|
|
# Timeout on socket connect.
|
2011-12-13 19:51:39 +00:00
|
|
|
@connect_timeout = opts[:connect_timeout] || 30
|
2011-11-02 22:00:52 +00:00
|
|
|
|
|
|
|
# Mutex for synchronizing pool access
|
|
|
|
# TODO: remove this.
|
|
|
|
@connection_mutex = Mutex.new
|
|
|
|
|
|
|
|
# Global safe option. This is false by default.
|
|
|
|
@safe = opts[:safe] || false
|
|
|
|
|
|
|
|
# Condition variable for signal and wait
|
|
|
|
@queue = ConditionVariable.new
|
|
|
|
|
|
|
|
@logger = opts[:logger] || nil
|
|
|
|
|
2011-11-04 18:26:12 +00:00
|
|
|
# Clean up connections to dead threads.
|
|
|
|
@last_cleanup = Time.now
|
|
|
|
@cleanup_lock = Mutex.new
|
|
|
|
|
2011-11-02 22:00:52 +00:00
|
|
|
if @logger
|
2011-11-15 20:59:42 +00:00
|
|
|
write_logging_startup_message
|
2011-11-02 22:00:52 +00:00
|
|
|
end
|
|
|
|
|
2011-11-18 21:13:19 +00:00
|
|
|
@last_refresh = Time.now
|
|
|
|
|
2011-11-02 22:00:52 +00:00
|
|
|
should_connect = opts.fetch(:connect, true)
|
|
|
|
connect if should_connect
|
|
|
|
end
|
|
|
|
|
2011-08-29 21:49:58 +00:00
|
|
|
# Checkout a socket connected to a node with one of
|
|
|
|
# the provided tags. If no such node exists, raise
|
|
|
|
# an exception.
|
2011-09-13 21:50:01 +00:00
|
|
|
#
|
|
|
|
# NOTE: will be available in driver release v2.0.
|
2011-08-29 21:49:58 +00:00
|
|
|
def checkout_tagged(tags)
|
2011-11-02 21:33:34 +00:00
|
|
|
tags.each do |k, v|
|
|
|
|
pool = self.tag_map[{k.to_s => v}]
|
|
|
|
if pool
|
|
|
|
socket = pool.checkout
|
|
|
|
return socket
|
2011-08-24 22:34:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-29 21:49:58 +00:00
|
|
|
raise NodeWithTagsNotFound,
|
|
|
|
"Could not find a connection tagged with #{tags}."
|
2010-12-15 17:36:43 +00:00
|
|
|
end
|
|
|
|
|
2011-11-18 20:47:06 +00:00
|
|
|
def sync_refresh
|
|
|
|
if @refresh_mode == :sync &&
|
|
|
|
((Time.now - @last_refresh) > @refresh_interval)
|
|
|
|
@last_refresh = Time.now
|
|
|
|
refresh
|
|
|
|
end
|
2011-08-29 21:49:58 +00:00
|
|
|
end
|
2010-12-10 21:00:35 +00:00
|
|
|
end
|
|
|
|
end
|