MongoReplicaSetClient
Instantiates and manages connections to a MongoDB sharded cluster for high availability.
# File lib/mongo/mongo_sharded_client.rb, line 29 def initialize(*args) opts = args.last.is_a?(Hash) ? args.pop : {} nodes = args.flatten if nodes.empty? and ENV.has_key?('MONGODB_URI') parser = URIParser.new ENV['MONGODB_URI'] if parser.direct? raise MongoArgumentError, "Mongo::MongoShardedClient.new called with no arguments, but ENV['MONGODB_URI'] implies a direct connection." end opts = parser.connection_options.merge! opts nodes = [parser.nodes] end unless nodes.length > 0 raise MongoArgumentError, "A MongoShardedClient requires at least one seed node." end @seeds = nodes.map do |host_port| host, port = host_port.split(":") [ host, port.to_i ] end # TODO: add a method for replacing this list of node. @seeds.freeze # Refresh @last_refresh = Time.now @refresh_version = 0 # No connection manager by default. @manager = nil @old_managers = [] # Lock for request ids. @id_lock = Mutex.new @pool_mutex = Mutex.new @connected = false @safe_mutex_lock = Mutex.new @safe_mutexes = Hash.new {|hash, key| hash[key] = Mutex.new} @connect_mutex = Mutex.new @refresh_mutex = Mutex.new @mongos = true check_opts(opts) setup(opts) end
# File lib/mongo/mongo_sharded_client.rb, line 135 def checkout(&block) 2.times do if connected? sync_refresh else connect end begin socket = block.call rescue => ex checkin(socket) if socket raise ex end if socket return socket else @connected = false #raise ConnectionFailure.new("Could not checkout a socket.") end end end
Initiate a connection to the sharded cluster.
# File lib/mongo/mongo_sharded_client.rb, line 91 def connect(force = !@connected) return unless force log(:info, "Connecting...") @connect_mutex.synchronize do discovered_seeds = @manager ? @manager.seeds : [] @old_managers << @manager if @manager @manager = ShardingPoolManager.new(self, discovered_seeds | @seeds) thread_local[:managers][self] = @manager @manager.connect @refresh_version += 1 @last_refresh = Time.now @connected = true end end
# File lib/mongo/mongo_sharded_client.rb, line 120 def connected? @connected && @manager.primary_pool end
Force a hard refresh of this connection's view of the sharded cluster.
@return [Boolean] true if hard refresh
occurred. +false+ is returned when unable to get the refresh lock.
# File lib/mongo/mongo_sharded_client.rb, line 114 def hard_refresh! log(:info, "Initiating hard refresh...") connect(true) return true end
# File lib/mongo/mongo_sharded_client.rb, line 85 def inspect "<Mongo::MongoShardedClient:0x#{self.object_id.to_s(16)} @seeds=#{@seeds.inspect} " + "@connected=#{@connected}>" end
Returns true if it's okay to read from a secondary node. Since this is a sharded cluster, this must always be false.
This method exist primarily so that Cursor objects will generate query messages with a slaveOkay value of true.
@return [Boolean] true
# File lib/mongo/mongo_sharded_client.rb, line 131 def slave_ok? false end
Generated with the Darkfish Rdoc Generator 2.