Class: Mongo::Connection
Overview
Instantiates and manages connections to MongoDB.
Direct Known Subclasses
Constant Summary collapse
- TCPSocket =
::TCPSocket
- UNIXSocket =
::UNIXSocket
- Mutex =
::Mutex
- ConditionVariable =
::ConditionVariable
- DEFAULT_PORT =
27017
- STANDARD_HEADER_SIZE =
16
- RESPONSE_HEADER_SIZE =
20
- @@current_request_id =
Counter for generating unique request ids.
0
Instance Attribute Summary collapse
-
#auths ⇒ Object
readonly
Returns the value of attribute auths.
-
#host_to_try ⇒ Object
readonly
Returns the value of attribute host_to_try.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#pool_size ⇒ Object
readonly
Returns the value of attribute pool_size.
-
#primary ⇒ Object
readonly
Returns the value of attribute primary.
-
#primary_pool ⇒ Object
readonly
Returns the value of attribute primary_pool.
-
#safe ⇒ Object
readonly
Returns the value of attribute safe.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#unix_socket_path ⇒ Object
readonly
Returns the value of attribute unix_socket_path.
Class Method Summary collapse
-
.from_uri(string, extra_opts = {}) ⇒ Mongo::Connection, Mongo::ReplSetConnection
Initialize a connection to MongoDB using the MongoDB URI spec:.
- .multi(nodes, opts = {}) ⇒ Mongo::Connection deprecated Deprecated.
Instance Method Summary collapse
-
#[](db_name) ⇒ Mongo::DB
Shortcut for returning a database.
-
#active? ⇒ Boolean
Determine if the connection is active.
-
#add_auth(db_name, username, password) ⇒ Hash
Save an authentication to this connection.
-
#apply_saved_authentication(opts = {}) ⇒ Boolean
Apply each of the saved database authentications.
- #authenticate_pools ⇒ Object
-
#checkin_reader(socket) ⇒ Object
Checkin a socket used for reading.
-
#checkin_writer(socket) ⇒ Object
Checkin a socket used for writing.
-
#checkout_reader ⇒ Object
Checkout a socket for reading (i.e., a secondary node).
-
#checkout_writer ⇒ Object
Checkout a socket for writing (i.e., a primary node).
-
#clear_auths ⇒ true
Remove all authenication information stored in this connection.
-
#close ⇒ Object
Close the connection to the database.
-
#connect ⇒ Object
(also: #reconnect)
Create a new socket and attempt to connect to master.
-
#connected? ⇒ Boolean
It’s possible that we defined connected as all nodes being connected??? NOTE: Do check if this needs to be more stringent.
- #connecting? ⇒ Boolean
-
#copy_database(from, to, from_host = "localhost", username = nil, password = nil) ⇒ Object
Copy the database
from
toto
on localhost. -
#database_info ⇒ Hash
Return a hash with all database names and their respective sizes on disk.
-
#database_names ⇒ Array
Return an array of database names.
-
#db(db_name, opts = {}) ⇒ Mongo::DB
Return a database with the given name.
-
#drop_database(name) ⇒ Object
Drop a database.
-
#initialize(host = nil, port = nil, opts = {}) ⇒ Connection
constructor
Create a connection to single MongoDB instance.
-
#instrument(name, payload = {}, &blk) ⇒ Object
Execute the block and log the operation described by name and payload.
-
#lock! ⇒ BSON::OrderedHash
Fsync, then lock the mongod process against writes.
-
#locked? ⇒ Boolean
Is this database locked against writes?.
- #logout_pools(db) ⇒ Object
-
#max_bson_size ⇒ Integer
Returns the maximum BSON object size as returned by the core server.
-
#ping ⇒ Hash
Checks if a server is alive.
-
#read_primary? ⇒ Boolean
(also: #primary?)
Determine whether we’re reading from a primary node.
-
#receive_message(operation, message, log_message = nil, socket = nil, command = false) ⇒ Array
Sends a message to the database and waits for the response.
-
#remove_auth(db_name) ⇒ Boolean
Remove a saved authentication for this connection.
-
#send_message(operation, message, log_message = nil) ⇒ Integer
Send a message to MongoDB, adding the necessary headers.
-
#send_message_with_safe_check(operation, message, db_name, log_message = nil, last_error_params = false) ⇒ Hash
Sends a message to the database, waits for a response, and raises an exception if the operation has failed.
-
#server_info ⇒ Hash
Get the build information for the current connection.
-
#server_version ⇒ Mongo::ServerVersion
Get the build version of the current server.
-
#slave_ok? ⇒ Boolean
Is it okay to connect to a slave?.
- #to_s ⇒ Object
-
#unlock! ⇒ BSON::OrderedHash
Unlock a previously fsync-locked mongod process.
Constructor Details
#initialize(host = nil, port = nil, opts = {}) ⇒ Connection
Create a connection to single MongoDB instance.
You may specify whether connection to slave is permitted. In all cases, the default host is “localhost” and the default port is 27017.
If you’re connecting to a replica set, you’ll need to use ReplSetConnection.new instead.
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.
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mongo/connection.rb', line 91 def initialize(host=nil, port=nil, opts={}) @host_to_try = format_pair(host, port) # Host and port of current master. @host = @port = nil # slave_ok can be true only if one node is specified @slave_ok = opts[:slave_ok] opts[:socket] = host[:socket] if host.kind_of?(Hash) && !opts[:socket] setup(opts) end |
Instance Attribute Details
#auths ⇒ Object (readonly)
Returns the value of attribute auths.
39 40 41 |
# File 'lib/mongo/connection.rb', line 39 def auths @auths end |
#host_to_try ⇒ Object (readonly)
Returns the value of attribute host_to_try.
39 40 41 |
# File 'lib/mongo/connection.rb', line 39 def host_to_try @host_to_try end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
39 40 41 |
# File 'lib/mongo/connection.rb', line 39 def logger @logger end |
#pool_size ⇒ Object (readonly)
Returns the value of attribute pool_size.
39 40 41 |
# File 'lib/mongo/connection.rb', line 39 def pool_size @pool_size end |
#primary ⇒ Object (readonly)
Returns the value of attribute primary.
39 40 41 |
# File 'lib/mongo/connection.rb', line 39 def primary @primary end |
#primary_pool ⇒ Object (readonly)
Returns the value of attribute primary_pool.
39 40 41 |
# File 'lib/mongo/connection.rb', line 39 def primary_pool @primary_pool end |
#safe ⇒ Object (readonly)
Returns the value of attribute safe.
39 40 41 |
# File 'lib/mongo/connection.rb', line 39 def safe @safe end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
39 40 41 |
# File 'lib/mongo/connection.rb', line 39 def size @size end |
#unix_socket_path ⇒ Object (readonly)
Returns the value of attribute unix_socket_path.
39 40 41 |
# File 'lib/mongo/connection.rb', line 39 def unix_socket_path @unix_socket_path end |
Class Method Details
.from_uri(string, extra_opts = {}) ⇒ Mongo::Connection, Mongo::ReplSetConnection
Initialize a connection to MongoDB using the MongoDB URI spec:
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/mongo/connection.rb', line 146 def self.from_uri(string, extra_opts={}) uri = URIParser.new(string) opts = uri. opts.merge!(extra_opts) if uri.nodes.length == 1 opts.merge!({:auths => uri.auths}) Connection.new(uri.nodes[0][0], uri.nodes[0][1], opts) elsif uri.nodes.length > 1 nodes = uri.nodes.clone nodes_with_opts = nodes << opts ReplSetConnection.new(*nodes_with_opts) else raise MongoArgumentError, "No nodes specified. Please ensure that you've provided at least one node." end end |
.multi(nodes, opts = {}) ⇒ Mongo::Connection
DEPRECATED
Initialize a connection to a MongoDB replica set using an array of seed nodes.
The seed nodes specified will be used on the initial connection to the replica set, but note that this list of nodes will be replced by the list of canonical nodes returned by running the is_master command on the replica set.
131 132 133 134 135 136 |
# File 'lib/mongo/connection.rb', line 131 def self.multi(nodes, opts={}) warn "Connection.multi is now deprecated. Please use ReplSetConnection.new instead." nodes << opts ReplSetConnection.new(*nodes) end |
Instance Method Details
#[](db_name) ⇒ Mongo::DB
Shortcut for returning a database. Use DB#db to accept options.
295 296 297 |
# File 'lib/mongo/connection.rb', line 295 def [](db_name) DB.new(db_name, self, :safe => @safe) end |
#active? ⇒ Boolean
Determine if the connection is active. In a normal case the server_info operation will be performed without issues, but if the connection was dropped by the server or for some reason the sockets are unsynchronized, a ConnectionFailure will be raised and the return will be false.
503 504 505 506 507 508 509 510 511 |
# File 'lib/mongo/connection.rb', line 503 def active? return false unless connected? ping true rescue ConnectionFailure false end |
#add_auth(db_name, username, password) ⇒ Hash
Save an authentication to this connection. When connecting, the connection will attempt to re-authenticate on every db specificed in the list of auths. This method is called automatically by DB#authenticate.
Note: this method will not actually issue an authentication command. To do that, either run Connection#apply_saved_authentication or DB#authenticate.
217 218 219 220 221 222 223 224 225 |
# File 'lib/mongo/connection.rb', line 217 def add_auth(db_name, username, password) remove_auth(db_name) auth = {} auth['db_name'] = db_name auth['username'] = username auth['password'] = password @auths << auth auth end |
#apply_saved_authentication(opts = {}) ⇒ Boolean
Apply each of the saved database authentications.
195 196 197 198 199 200 201 202 |
# File 'lib/mongo/connection.rb', line 195 def apply_saved_authentication(opts={}) return false if @auths.empty? @auths.each do |auth| self[auth['db_name']].issue_authentication(auth['username'], auth['password'], false, :socket => opts[:socket]) end true end |
#authenticate_pools ⇒ Object
249 250 251 |
# File 'lib/mongo/connection.rb', line 249 def authenticate_pools @primary_pool.authenticate_existing end |
#checkin_reader(socket) ⇒ Object
Checkin a socket used for reading. Note: this is overridden in ReplSetConnection.
553 554 555 556 557 |
# File 'lib/mongo/connection.rb', line 553 def checkin_reader(socket) if @primary_pool @primary_pool.checkin(socket) end end |
#checkin_writer(socket) ⇒ Object
Checkin a socket used for writing. Note: this is overridden in ReplSetConnection.
561 562 563 564 565 |
# File 'lib/mongo/connection.rb', line 561 def checkin_writer(socket) if @primary_pool @primary_pool.checkin(socket) end end |
#checkout_reader ⇒ Object
Checkout a socket for reading (i.e., a secondary node). Note: this is overridden in ReplSetConnection.
539 540 541 542 |
# File 'lib/mongo/connection.rb', line 539 def checkout_reader connect unless connected? @primary_pool.checkout end |
#checkout_writer ⇒ Object
Checkout a socket for writing (i.e., a primary node). Note: this is overridden in ReplSetConnection.
546 547 548 549 |
# File 'lib/mongo/connection.rb', line 546 def checkout_writer connect unless connected? @primary_pool.checkout end |
#clear_auths ⇒ true
Remove all authenication information stored in this connection.
244 245 246 247 |
# File 'lib/mongo/connection.rb', line 244 def clear_auths @auths = [] true end |
#close ⇒ Object
Close the connection to the database.
523 524 525 526 |
# File 'lib/mongo/connection.rb', line 523 def close @primary_pool.close if @primary_pool @primary_pool = nil end |
#connect ⇒ Object Also known as: reconnect
Create a new socket and attempt to connect to master. If successful, sets host and port to master and returns the socket.
If connecting to a replica set, this method will replace the initially-provided seed list with any nodes known to the set.
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/mongo/connection.rb', line 464 def connect reset_connection config = check_is_master(@host_to_try) if config if config['ismaster'] == 1 || config['ismaster'] == true @read_primary = true elsif @slave_ok @read_primary = false end set_primary(@host_to_try) end if connected? BSON::BSON_CODER.update_max_bson_size(self) else raise ConnectionFailure, "Failed to connect to a master node at #{self.to_s}" end end |
#connected? ⇒ Boolean
It’s possible that we defined connected as all nodes being connected??? NOTE: Do check if this needs to be more stringent. Probably not since if any node raises a connection failure, all nodes will be closed.
493 494 495 |
# File 'lib/mongo/connection.rb', line 493 def connected? @primary_pool && ((@primary_pool.host && @primary_pool.port) || @primary_pool.unix_socket_path ) end |
#connecting? ⇒ Boolean
486 487 488 |
# File 'lib/mongo/connection.rb', line 486 def connecting? @nodes_to_try.length > 0 end |
#copy_database(from, to, from_host = "localhost", username = nil, password = nil) ⇒ Object
Copy the database from
to to
on localhost. The from
database is assumed to be on localhost, but an alternate host can be specified.
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/mongo/connection.rb', line 314 def copy_database(from, to, from_host="localhost", username=nil, password=nil) oh = BSON::OrderedHash.new oh[:copydb] = 1 oh[:fromhost] = from_host oh[:fromdb] = from oh[:todb] = to if username || password unless username && password raise MongoArgumentError, "Both username and password must be supplied for authentication." end nonce_cmd = BSON::OrderedHash.new nonce_cmd[:copydbgetnonce] = 1 nonce_cmd[:fromhost] = from_host result = self["admin"].command(nonce_cmd) oh[:nonce] = result["nonce"] oh[:username] = username oh[:key] = Mongo::Support.auth_key(username, password, oh[:nonce]) end self["admin"].command(oh) end |
#database_info ⇒ Hash
Return a hash with all database names and their respective sizes on disk.
261 262 263 264 265 266 |
# File 'lib/mongo/connection.rb', line 261 def database_info doc = self['admin'].command({:listDatabases => 1}) doc['databases'].each_with_object({}) do |db, info| info[db['name']] = db['sizeOnDisk'].to_i end end |
#database_names ⇒ Array
Return an array of database names.
271 272 273 |
# File 'lib/mongo/connection.rb', line 271 def database_names database_info.keys end |
#db(db_name, opts = {}) ⇒ Mongo::DB
Return a database with the given name. See DB#new for valid options hash parameters.
284 285 286 |
# File 'lib/mongo/connection.rb', line 284 def db(db_name, opts={}) DB.new(db_name, self, opts) end |
#drop_database(name) ⇒ Object
Drop a database.
302 303 304 |
# File 'lib/mongo/connection.rb', line 302 def drop_database(name) self[name].command(:dropDatabase => 1) end |
#instrument(name, payload = {}, &blk) ⇒ Object
Execute the block and log the operation described by name and payload. TODO: Not sure if this should take a block.
570 571 572 573 574 |
# File 'lib/mongo/connection.rb', line 570 def instrument(name, payload = {}, &blk) res = yield log_operation(name, payload) res end |
#lock! ⇒ BSON::OrderedHash
Fsync, then lock the mongod process against writes. Use this to get the datafiles in a state safe for snapshotting, backing up, etc.
167 168 169 170 171 172 |
# File 'lib/mongo/connection.rb', line 167 def lock! cmd = BSON::OrderedHash.new cmd[:fsync] = 1 cmd[:lock] = true self['admin'].command(cmd) end |
#locked? ⇒ Boolean
Is this database locked against writes?
177 178 179 |
# File 'lib/mongo/connection.rb', line 177 def locked? self['admin']['$cmd.sys.inprog'].find_one['fsyncLock'] == 1 end |
#logout_pools(db) ⇒ Object
253 254 255 |
# File 'lib/mongo/connection.rb', line 253 def logout_pools(db) @primary_pool.logout_existing(db) end |
#max_bson_size ⇒ Integer
Returns the maximum BSON object size as returned by the core server. Use the 4MB default when the server doesn’t report this.
532 533 534 535 |
# File 'lib/mongo/connection.rb', line 532 def max_bson_size config = self['admin'].command({:ismaster => 1}) config['maxBsonObjectSize'] || Mongo::DEFAULT_MAX_BSON_SIZE end |
#ping ⇒ Hash
Checks if a server is alive. This command will return immediately even if the server is in a lock.
339 340 341 |
# File 'lib/mongo/connection.rb', line 339 def ping self["admin"].command({:ping => 1}) end |
#read_primary? ⇒ Boolean Also known as: primary?
Determine whether we’re reading from a primary node. If false, this connection connects to a secondary node and @slave_ok is true.
517 518 519 |
# File 'lib/mongo/connection.rb', line 517 def read_primary? @read_primary end |
#receive_message(operation, message, log_message = nil, socket = nil, command = false) ⇒ Array
Sends a message to the database and waits for the response.
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
# File 'lib/mongo/connection.rb', line 432 def (operation, , =nil, socket=nil, command=false) request_id = (, operation) = .to_s begin if socket sock = socket checkin = false else sock = (command ? checkout_writer : checkout_reader) checkin = true end result = '' @safe_mutexes[sock].synchronize do (, sock) result = receive(sock, request_id) end ensure if checkin command ? checkin_writer(sock) : checkin_reader(sock) end end result end |
#remove_auth(db_name) ⇒ Boolean
Remove a saved authentication for this connection.
232 233 234 235 236 237 238 239 |
# File 'lib/mongo/connection.rb', line 232 def remove_auth(db_name) return unless @auths if @auths.reject! { |a| a['db_name'] == db_name } true else false end end |
#send_message(operation, message, log_message = nil) ⇒ Integer
Send a message to MongoDB, adding the necessary headers.
372 373 374 375 376 377 378 379 380 381 |
# File 'lib/mongo/connection.rb', line 372 def (operation, , =nil) begin (, operation) = .to_s socket = checkout_writer (, socket) ensure checkin_writer(socket) end end |
#send_message_with_safe_check(operation, message, db_name, log_message = nil, last_error_params = false) ⇒ Hash
Sends a message to the database, waits for a response, and raises an exception if the operation has failed.
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/mongo/connection.rb', line 395 def (operation, , db_name, =nil, last_error_params=false) docs = num_received = cursor_id = '' (, operation) = BSON::ByteBuffer.new (, db_name, last_error_params) last_error_id = (, Mongo::Constants::OP_QUERY) = .append!().to_s begin sock = checkout_writer @safe_mutexes[sock].synchronize do (, sock) docs, num_received, cursor_id = receive(sock, last_error_id) end ensure checkin_writer(sock) end if num_received == 1 && (error = docs[0]['err'] || docs[0]['errmsg']) close if error == "not master" error = "wtimeout" if error == "timeout" raise Mongo::OperationFailure, docs[0]['code'].to_s + ': ' + error end docs[0] end |
#server_info ⇒ Hash
Get the build information for the current connection.
346 347 348 |
# File 'lib/mongo/connection.rb', line 346 def server_info self["admin"].command({:buildinfo => 1}) end |
#server_version ⇒ Mongo::ServerVersion
Get the build version of the current server.
355 356 357 |
# File 'lib/mongo/connection.rb', line 355 def server_version ServerVersion.new(server_info["version"]) end |
#slave_ok? ⇒ Boolean
Is it okay to connect to a slave?
362 363 364 |
# File 'lib/mongo/connection.rb', line 362 def slave_ok? @slave_ok end |
#to_s ⇒ Object
576 577 578 579 580 581 582 |
# File 'lib/mongo/connection.rb', line 576 def to_s if @host_to_try[1] "#{@host_to_try[0]}:#{@host_to_try[1]}" else "#{@host_to_try[0]}" end end |
#unlock! ⇒ BSON::OrderedHash
Unlock a previously fsync-locked mongod process.
184 185 186 |
# File 'lib/mongo/connection.rb', line 184 def unlock! self['admin']['$cmd.sys.unlock'].find_one end |