Class: Moped::Cluster
- Inherits:
-
Object
- Object
- Moped::Cluster
- Defined in:
- lib/moped/cluster.rb
Overview
The cluster represents a cluster of MongoDB server nodes, either a single node, a replica set, or a mongos server.
Constant Summary collapse
- DOWN_INTERVAL =
The default interval that a node would be flagged as “down”.
30
- REFRESH_INTERVAL =
The default interval that a node should be refreshed in.
300
- RETRY_INTERVAL =
The default time to wait to retry an operation.
0.25
Instance Attribute Summary collapse
-
#options ⇒ Hash
The refresh options.
-
#peers ⇒ Array<Node>
The node peers.
- #seeds ⇒ Object
Instance Method Summary collapse
-
#add_credential(db, username, password) ⇒ Boolean
Add a credential to the cluster.
-
#delete_credential(db) ⇒ Boolean
Remove a credential from the cluster.
-
#disconnect ⇒ true
Disconnects all nodes in the cluster.
-
#down_interval ⇒ Integer
Get the interval at which a node should be flagged as down before retrying.
-
#initialize(hosts, options) ⇒ Cluster
constructor
Initialize the new cluster.
-
#inspect ⇒ String
Provide a pretty string for cluster inspection.
-
#max_retries ⇒ Integer
Get the number of times an operation should be retried before raising an error.
-
#nodes ⇒ Array<Node>
Returns the list of available nodes, refreshing 1) any nodes which were down and ready to be checked again and 2) any nodes whose information is out of date.
-
#refresh(nodes_to_refresh = seeds) ⇒ Array<Node>
Refreshes information for each of the nodes provided.
-
#refresh_interval ⇒ Integer
Get the interval in which the node list should be refreshed.
-
#retry_interval ⇒ Integer
Get the operation retry interval - the time to wait before retrying a single operation.
-
#with_primary(&block) ⇒ Object
Yields the replica set’s primary node to the provided block.
-
#with_secondary(&block) ⇒ Object
Yields a secondary node if available, otherwise the primary node.
Constructor Details
Instance Attribute Details
#options ⇒ Hash
Returns The refresh options.
33 34 35 |
# File 'lib/moped/cluster.rb', line 33 def @options end |
#peers ⇒ Array<Node>
Returns The node peers.
33 |
# File 'lib/moped/cluster.rb', line 33 attr_reader :options, :peers, :seeds |
#seeds ⇒ Object
33 |
# File 'lib/moped/cluster.rb', line 33 attr_reader :options, :peers, :seeds |
Instance Method Details
#add_credential(db, username, password) ⇒ Boolean
Add a credential to the cluster
43 44 45 46 47 |
# File 'lib/moped/cluster.rb', line 43 def add_credential(db, username, password) @credentials ||= {} @credentials[db] = [ username, password ] apply_credentials end |
#delete_credential(db) ⇒ Boolean
Remove a credential from the cluster
57 58 59 60 61 |
# File 'lib/moped/cluster.rb', line 57 def delete_credential(db) return true unless @credentials @credentials.delete(db) apply_credentials end |
#disconnect ⇒ true
Disconnects all nodes in the cluster. This should only be used in cases where you know you’re not going to use the cluster on the thread anymore and need to force the connections to close.
70 71 72 |
# File 'lib/moped/cluster.rb', line 70 def disconnect nodes.each { |node| node.disconnect } and true end |
#down_interval ⇒ Integer
Get the interval at which a node should be flagged as down before retrying.
83 84 85 |
# File 'lib/moped/cluster.rb', line 83 def down_interval @down_interval ||= [:down_interval] || DOWN_INTERVAL end |
#inspect ⇒ String
Provide a pretty string for cluster inspection.
116 117 118 |
# File 'lib/moped/cluster.rb', line 116 def inspect "#<#{self.class.name}:#{object_id} @seeds=#{seeds.inspect}>" end |
#max_retries ⇒ Integer
Get the number of times an operation should be retried before raising an error.
129 130 131 |
# File 'lib/moped/cluster.rb', line 129 def max_retries @max_retries ||= [:max_retries] || seeds.size end |
#nodes ⇒ Array<Node>
Returns the list of available nodes, refreshing 1) any nodes which were down and ready to be checked again and 2) any nodes whose information is out of date. Arbiter nodes are not returned.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/moped/cluster.rb', line 143 def nodes # Find the nodes that were down but are ready to be refreshed, or those # with stale connection information. needs_refresh, available = seeds.partition do |node| refreshable?(node) end # Refresh those nodes. available.concat(refresh(needs_refresh)) # Now return all the nodes that are available and participating in the # replica set. available.reject{ |node| node.down? } end |
#refresh(nodes_to_refresh = seeds) ⇒ Array<Node>
Refreshes information for each of the nodes provided. The node list defaults to the list of all known nodes.
If a node is successfully refreshed, any newly discovered peers will also be refreshed.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/moped/cluster.rb', line 172 def refresh(nodes_to_refresh = seeds) refreshed_nodes = [] seen = {} # Set up a recursive lambda function for refreshing a node and it's peers. refresh_node = ->(node) do unless node.address.resolved begin node.refresh rescue Errors::ConnectionFailure end end unless seen[node] || !node.address.resolved seen[node] = true # Add the node to the global list of known nodes. seeds.push(node) unless seeds.include?(node) begin node.refresh # This node is good, so add it to the list of nodes to return. refreshed_nodes.push(node) unless refreshed_nodes.include?(node) # Now refresh any newly discovered peer nodes - this will also # remove nodes that are not included in the peer list. refresh_peers(node, &refresh_node) rescue Errors::ConnectionFailure # We couldn't connect to the node. end end end nodes_to_refresh.each(&refresh_node) refreshed_nodes end |
#refresh_interval ⇒ Integer
Get the interval in which the node list should be refreshed.
212 213 214 |
# File 'lib/moped/cluster.rb', line 212 def refresh_interval @refresh_interval ||= [:refresh_interval] || REFRESH_INTERVAL end |
#retry_interval ⇒ Integer
Get the operation retry interval - the time to wait before retrying a single operation.
225 226 227 |
# File 'lib/moped/cluster.rb', line 225 def retry_interval @retry_interval ||= [:retry_interval] || RETRY_INTERVAL end |
#with_primary(&block) ⇒ Object
Yields the replica set’s primary node to the provided block. This method will retry the block in case of connection errors or replica set reconfiguration.
245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/moped/cluster.rb', line 245 def with_primary(&block) if node = nodes.find(&:primary?) begin node.ensure_primary do return yield(node) end rescue Errors::ConnectionFailure, Errors::ReplicaSetReconfigured end end raise Errors::ConnectionFailure, "Could not connect to a primary node for replica set #{inspect}" end |
#with_secondary(&block) ⇒ Object
Yields a secondary node if available, otherwise the primary node. This method will retry the block in case of connection errors.
272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/moped/cluster.rb', line 272 def with_secondary(&block) available_nodes = available_secondary_nodes while node = available_nodes.shift begin return yield(node) rescue Errors::ConnectionFailure, Errors::ReplicaSetReconfigured => e next end end raise Errors::ConnectionFailure, "Could not connect to a secondary node for replica set #{inspect}" end |