Class: Mongo::MongoShardedClient
- Inherits:
-
MongoReplicaSetClient
- Object
- MongoClient
- MongoReplicaSetClient
- Mongo::MongoShardedClient
- Includes:
- ThreadLocalVariableManager
- Defined in:
- lib/mongo/mongo_sharded_client.rb
Overview
Instantiates and manages connections to a MongoDB sharded cluster for high availability.
Direct Known Subclasses
Constant Summary collapse
- SHARDED_CLUSTER_OPTS =
[:refresh_mode, :refresh_interval, :tag_sets, :read]
Constants inherited from MongoReplicaSetClient
Mongo::MongoReplicaSetClient::REPL_SET_OPTS
Constants included from ReadPreference
ReadPreference::MONGOS_MODES, ReadPreference::READ_PREFERENCES
Constants inherited from MongoClient
Mongo::MongoClient::CLIENT_ONLY_OPTS, Mongo::MongoClient::ConditionVariable, Mongo::MongoClient::DEFAULT_DB_NAME, Mongo::MongoClient::DEFAULT_HOST, Mongo::MongoClient::DEFAULT_PORT, Mongo::MongoClient::GENERIC_OPTS, Mongo::MongoClient::Mutex, Mongo::MongoClient::POOL_OPTS, Mongo::MongoClient::READ_PREFERENCE_OPTS, Mongo::MongoClient::SSL_OPTS, Mongo::MongoClient::TIMEOUT_OPTS, Mongo::MongoClient::WRITE_CONCERN_OPTS
Constants included from Networking
Networking::RESPONSE_HEADER_SIZE, Networking::STANDARD_HEADER_SIZE
Instance Attribute Summary collapse
-
#manager ⇒ Object
readonly
Returns the value of attribute manager.
-
#refresh_interval ⇒ Object
readonly
Returns the value of attribute refresh_interval.
-
#refresh_mode ⇒ Object
readonly
Returns the value of attribute refresh_mode.
-
#refresh_version ⇒ Object
readonly
Returns the value of attribute refresh_version.
-
#seeds ⇒ Object
readonly
Returns the value of attribute seeds.
Attributes inherited from MongoReplicaSetClient
Attributes inherited from MongoClient
#acceptable_latency, #auths, #connect_timeout, #host_to_try, #logger, #op_timeout, #pool_size, #pool_timeout, #primary, #primary_pool, #read, #size, #socket_class, #socket_opts, #tag_sets, #write_concern
Attributes included from WriteConcern
Class Method Summary collapse
-
.from_uri(uri, options = {}) ⇒ Mongo::MongoShardedClient
Initialize a connection to MongoDB using the MongoDB URI spec.
Instance Method Summary collapse
- #checkout(&block) ⇒ Object
-
#connect(force = !connected?)) ⇒ Object
Initiate a connection to the sharded cluster.
- #connected? ⇒ Boolean
-
#hard_refresh! ⇒ Boolean
Force a hard refresh of this connection’s view of the sharded cluster.
-
#initialize(*args) ⇒ MongoShardedClient
constructor
A new instance of MongoShardedClient.
- #inspect ⇒ Object
-
#slave_ok? ⇒ Boolean
Returns
trueif it’s okay to read from a secondary node. - #valid_opts ⇒ Object
Methods included from ThreadLocalVariableManager
Methods inherited from MongoReplicaSetClient
#arbiters, #authenticate_pools, #checkin, #checkout_reader, #checkout_writer, #close, #connecting?, #ensure_manager, #get_socket_from_pool, #host, #hosts, #local_manager, #logout_pools, #max_bson_size, #max_message_size, #nodes, #pin_pool, #pinned_pool, #pools, #port, #primary, #primary_pool, #read_primary?, #refresh, #reset_connection, #secondaries, #secondary_pool, #secondary_pools, #tag_map, #unpin_pool
Methods included from ReadPreference
mongos, #read_pool, #read_preference, #select_near_pool, #select_pool, #select_secondary_pool, validate
Methods inherited from MongoClient
#[], #active?, #add_auth, #apply_saved_authentication, #authenticate_pools, #check_is_master, #checkin, #checkout_reader, #checkout_writer, #clear_auths, #close, #copy_database, #database_info, #database_names, #db, #drop_database, #host, #host_port, #lock!, #locked?, #logout_pools, #max_bson_size, #max_message_size, #mongos?, multi, #parse_init, #pin_pool, #ping, #pinned_pool, #port, #read_pool, #read_primary?, #refresh, #remove_auth, #server_info, #server_version, #unlock!, #unpin_pool
Methods included from WriteConcern
#get_write_concern, gle?, #write_concern_from_legacy
Methods included from Networking
#receive_message, #send_message, #send_message_with_gle
Methods included from Logging
#instrument, instrumenter, instrumenter=, #log, #write_logging_startup_message
Constructor Details
#initialize(*args) ⇒ MongoShardedClient
Returns a new instance of MongoShardedClient.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mongo/mongo_sharded_client.rb', line 26 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'] opts = parser..merge! opts nodes = parser.node_strings end unless nodes.length > 0 raise MongoArgumentError, "A MongoShardedClient requires at least one seed node." end @seeds = nodes.map do |host_port| Support.normalize_seeds(host_port) 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 # Lock for request ids. @id_lock = Mutex.new @connected = false @connect_mutex = Mutex.new @mongos = true check_opts(opts) setup(opts) end |
Instance Attribute Details
#manager ⇒ Object (readonly)
Returns the value of attribute manager.
23 24 25 |
# File 'lib/mongo/mongo_sharded_client.rb', line 23 def manager @manager end |
#refresh_interval ⇒ Object (readonly)
Returns the value of attribute refresh_interval.
23 24 25 |
# File 'lib/mongo/mongo_sharded_client.rb', line 23 def refresh_interval @refresh_interval end |
#refresh_mode ⇒ Object (readonly)
Returns the value of attribute refresh_mode.
23 24 25 |
# File 'lib/mongo/mongo_sharded_client.rb', line 23 def refresh_mode @refresh_mode end |
#refresh_version ⇒ Object (readonly)
Returns the value of attribute refresh_version.
23 24 25 |
# File 'lib/mongo/mongo_sharded_client.rb', line 23 def refresh_version @refresh_version end |
#seeds ⇒ Object (readonly)
Returns the value of attribute seeds.
23 24 25 |
# File 'lib/mongo/mongo_sharded_client.rb', line 23 def seeds @seeds end |
Class Method Details
.from_uri(uri, options = {}) ⇒ Mongo::MongoShardedClient
Initialize a connection to MongoDB using the MongoDB URI spec.
153 154 155 156 |
# File 'lib/mongo/mongo_sharded_client.rb', line 153 def self.from_uri(uri, = {}) uri ||= ENV['MONGODB_URI'] URIParser.new(uri).connection(, false, true) end |
Instance Method Details
#checkout(&block) ⇒ Object
135 136 137 138 139 140 141 142 143 |
# File 'lib/mongo/mongo_sharded_client.rb', line 135 def checkout(&block) tries = 0 begin super(&block) rescue ConnectionFailure tries +=1 tries < 2 ? retry : raise end end |
#connect(force = !connected?)) ⇒ Object
Initiate a connection to the sharded cluster.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/mongo/mongo_sharded_client.rb', line 78 def connect(force = !connected?) return unless force log(:info, "Connecting...") # Prevent recursive connection attempts from the same thread. # This is done rather than using a Monitor to prevent potentially recursing # infinitely while attempting to connect and continually failing. Instead, fail fast. raise ConnectionFailure, "Failed to get node data." if thread_local[:locks][:connecting] @connect_mutex.synchronize do begin thread_local[:locks][:connecting] = true if @manager thread_local[:managers][self] = @manager @manager.refresh! @seeds else @manager = ShardingPoolManager.new(self, @seeds) ensure_manager @manager.connect end ensure thread_local[:locks][:connecting] = false end @refresh_version += 1 @last_refresh = Time.now @connected = true end end |
#connected? ⇒ Boolean
120 121 122 |
# File 'lib/mongo/mongo_sharded_client.rb', line 120 def connected? !!(@connected && @manager.primary_pool) end |
#hard_refresh! ⇒ Boolean
Force a hard refresh of this connection’s view of the sharded cluster.
114 115 116 117 118 |
# File 'lib/mongo/mongo_sharded_client.rb', line 114 def hard_refresh! log(:info, "Initiating hard refresh...") connect(true) return true end |
#inspect ⇒ Object
72 73 74 75 |
# File 'lib/mongo/mongo_sharded_client.rb', line 72 def inspect "<Mongo::MongoShardedClient:0x#{self.object_id.to_s(16)} @seeds=#{@seeds.inspect} " + "@connected=#{@connected}>" end |
#slave_ok? ⇒ Boolean
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.
131 132 133 |
# File 'lib/mongo/mongo_sharded_client.rb', line 131 def slave_ok? false end |
#valid_opts ⇒ Object
68 69 70 |
# File 'lib/mongo/mongo_sharded_client.rb', line 68 def valid_opts super + SHARDED_CLUSTER_OPTS end |