Class: Mongo::ReplSetConnection
- Inherits:
-
Connection
- Object
- Connection
- Mongo::ReplSetConnection
- Defined in:
- lib/mongo/repl_set_connection.rb
Overview
Instantiates and manages connections to a MongoDB replica set.
Constant Summary
Constants inherited from Connection
Connection::ConditionVariable, Connection::DEFAULT_PORT, Connection::Mutex, Connection::RESPONSE_HEADER_SIZE, Connection::STANDARD_HEADER_SIZE, Connection::TCPSocket, Connection::UNIXSocket
Instance Attribute Summary collapse
-
#arbiters ⇒ Object
readonly
Returns the value of attribute arbiters.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#read_pool ⇒ Object
readonly
Returns the value of attribute read_pool.
-
#secondaries ⇒ Object
readonly
Returns the value of attribute secondaries.
-
#secondary_pools ⇒ Object
readonly
Returns the value of attribute secondary_pools.
Attributes inherited from Connection
#auths, #host_to_try, #logger, #pool_size, #primary, #primary_pool, #safe, #size, #unix_socket_path
Instance Method Summary collapse
- #authenticate_pools ⇒ Object
-
#close ⇒ Object
Close the connection to the database.
-
#connect ⇒ Object
(also: #reconnect)
Create a new socket and attempt to connect to master.
- #connecting? ⇒ Boolean
-
#initialize(*args) ⇒ ReplSetConnection
constructor
Create a connection to a MongoDB replica set.
- #logout_pools(db) ⇒ Object
-
#read_primary? ⇒ Boolean
(also: #primary?)
Determine whether we’re reading from a primary node.
-
#reset_connection ⇒ Object
If a ConnectionFailure is raised, this method will be called to close the connection and reset connection values.
-
#slave_ok? ⇒ Boolean
Is it okay to connect to a slave?.
Methods inherited from Connection
#[], #active?, #add_auth, #apply_saved_authentication, #clear_auths, #connected?, #copy_database, #database_info, #database_names, #db, #drop_database, from_uri, #instrument, #lock!, #locked?, #max_bson_size, multi, #ping, #receive_message, #remove_auth, #send_message, #send_message_with_safe_check, #server_info, #server_version, #to_s, #unlock!
Constructor Details
#initialize(*args) ⇒ ReplSetConnection
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.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/mongo/repl_set_connection.rb', line 65 def initialize(*args) if args.last.is_a?(Hash) opts = args.pop else opts = {} end unless args.length > 0 raise MongoArgumentError, "A ReplSetConnection requires at least one node." end # Get seed nodes @nodes = args # Replica set name @replica_set = opts[:rs_name] # Cache the various node types when connecting to a replica set. @secondaries = [] @arbiters = [] # Connection pools for each secondary node @secondary_pools = [] @read_pool = nil # Are we allowing reads from secondaries? @read_secondary = opts.fetch(:read_secondary, false) setup(opts) end |
Instance Attribute Details
#arbiters ⇒ Object (readonly)
Returns the value of attribute arbiters.
23 24 25 |
# File 'lib/mongo/repl_set_connection.rb', line 23 def arbiters @arbiters end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
23 24 25 |
# File 'lib/mongo/repl_set_connection.rb', line 23 def nodes @nodes end |
#read_pool ⇒ Object (readonly)
Returns the value of attribute read_pool.
23 24 25 |
# File 'lib/mongo/repl_set_connection.rb', line 23 def read_pool @read_pool end |
#secondaries ⇒ Object (readonly)
Returns the value of attribute secondaries.
23 24 25 |
# File 'lib/mongo/repl_set_connection.rb', line 23 def secondaries @secondaries end |
#secondary_pools ⇒ Object (readonly)
Returns the value of attribute secondary_pools.
23 24 25 |
# File 'lib/mongo/repl_set_connection.rb', line 23 def secondary_pools @secondary_pools end |
Instance Method Details
#authenticate_pools ⇒ Object
175 176 177 178 179 180 |
# File 'lib/mongo/repl_set_connection.rb', line 175 def authenticate_pools super @secondary_pools.each do |pool| pool.authenticate_existing end end |
#close ⇒ Object
Close the connection to the database.
148 149 150 151 152 153 154 |
# File 'lib/mongo/repl_set_connection.rb', line 148 def close super @read_pool = nil @secondary_pools.each do |pool| pool.close end 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.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/mongo/repl_set_connection.rb', line 103 def connect reset_connection @nodes_to_try = @nodes.clone while connecting? node = @nodes_to_try.shift config = check_is_master(node) if is_primary?(config) set_primary(node) else set_auxillary(node, config) end end pick_secondary_for_read if @read_secondary if connected? BSON::BSON_CODER.update_max_bson_size(self) else if @secondary_pools.empty? close # close any existing pools and sockets raise ConnectionFailure, "Failed to connect any given host:port" else close # close any existing pools and sockets raise ConnectionFailure, "Failed to connect to primary node." end end end |
#connecting? ⇒ Boolean
134 135 136 |
# File 'lib/mongo/repl_set_connection.rb', line 134 def connecting? @nodes_to_try.length > 0 end |
#logout_pools(db) ⇒ Object
182 183 184 185 186 187 |
# File 'lib/mongo/repl_set_connection.rb', line 182 def logout_pools(db) super @secondary_pools.each do |pool| pool.logout_existing(db) end 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 @read_secondaries is true.
142 143 144 |
# File 'lib/mongo/repl_set_connection.rb', line 142 def read_primary? !@read_pool end |
#reset_connection ⇒ Object
If a ConnectionFailure is raised, this method will be called to close the connection and reset connection values. TODO: what’s the point of this method?
159 160 161 162 163 164 165 166 |
# File 'lib/mongo/repl_set_connection.rb', line 159 def reset_connection super @secondaries = [] @secondary_pools = [] @arbiters = [] @nodes_tried = [] @nodes_to_try = [] end |
#slave_ok? ⇒ Boolean
Is it okay to connect to a slave?
171 172 173 |
# File 'lib/mongo/repl_set_connection.rb', line 171 def slave_ok? @read_secondary || @slave_ok end |