Class: Bones::RPC::Cluster
- Inherits:
-
Object
- Object
- Bones::RPC::Cluster
- Defined in:
- lib/bones/rpc/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
- #session ⇒ Object readonly
Instance Method Summary collapse
-
#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.
- #handle_refresh(node) ⇒ Object
-
#initialize(session, hosts) ⇒ 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.
- #pool_size ⇒ Object
-
#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.
Constructor Details
Instance Attribute Details
#options ⇒ Hash
Returns The refresh options.
34 |
# File 'lib/bones/rpc/cluster.rb', line 34 attr_reader :session, :peers, :seeds |
#peers ⇒ Array<Node>
Returns The node peers.
34 |
# File 'lib/bones/rpc/cluster.rb', line 34 attr_reader :session, :peers, :seeds |
#seeds ⇒ Object
34 |
# File 'lib/bones/rpc/cluster.rb', line 34 attr_reader :session, :peers, :seeds |
#session ⇒ Object (readonly)
34 35 36 |
# File 'lib/bones/rpc/cluster.rb', line 34 def session @session end |
Instance Method Details
#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.
43 44 45 |
# File 'lib/bones/rpc/cluster.rb', line 43 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.
56 57 58 |
# File 'lib/bones/rpc/cluster.rb', line 56 def down_interval @down_interval ||= [:down_interval] || DOWN_INTERVAL end |
#handle_refresh(node) ⇒ Object
60 61 62 |
# File 'lib/bones/rpc/cluster.rb', line 60 def handle_refresh(node) session.handle_refresh(node) end |
#inspect ⇒ String
Provide a pretty string for cluster inspection.
93 94 95 |
# File 'lib/bones/rpc/cluster.rb', line 93 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.
106 107 108 |
# File 'lib/bones/rpc/cluster.rb', line 106 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.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/bones/rpc/cluster.rb', line 120 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 |
#pool_size ⇒ Object
139 140 141 |
# File 'lib/bones/rpc/cluster.rb', line 139 def pool_size [:pool_size] || 5 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.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/bones/rpc/cluster.rb', line 157 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 seen[node] 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) 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.
188 189 190 |
# File 'lib/bones/rpc/cluster.rb', line 188 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.
201 202 203 |
# File 'lib/bones/rpc/cluster.rb', line 201 def retry_interval @retry_interval ||= [:retry_interval] || RETRY_INTERVAL end |