Class: Moped::Cluster
Overview
The cluster represents a cluster of MongoDB server nodes, either a single node, a replica set, or a mongos server.
Instance Attribute Summary collapse
-
#seeds ⇒ Object
readonly
Returns the value of attribute seeds.
- #seeds The seeds the cluster was initialized with.(Theseedstheclusterwasinitializedwith.) ⇒ Object readonly
Instance Method Summary collapse
-
#auth ⇒ Hash
Get the authentication details for the cluster.
-
#initialize(hosts, options) ⇒ Cluster
constructor
Initialize the new cluster.
- #inspect ⇒ Object
-
#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 = @nodes) ⇒ Array<Node>
Refreshes information for each of the nodes provided.
-
#with_primary(retry_on_failure = true, &block) ⇒ Object
Yields the replica set’s primary node to the provided block.
-
#with_secondary(retry_on_failure = true, &block) ⇒ Object
Yields a secondary node if available, otherwise the primary node.
Constructor Details
Instance Attribute Details
#seeds ⇒ Object (readonly)
Returns the value of attribute seeds.
8 9 10 |
# File 'lib/moped/cluster.rb', line 8 def seeds @seeds end |
#seeds The seeds the cluster was initialized with.(Theseedstheclusterwasinitializedwith.) ⇒ Object (readonly)
8 |
# File 'lib/moped/cluster.rb', line 8 attr_reader :seeds |
Instance Method Details
#auth ⇒ Hash
Get the authentication details for the cluster.
18 19 20 |
# File 'lib/moped/cluster.rb', line 18 def auth @auth ||= {} end |
#inspect ⇒ Object
200 201 202 |
# File 'lib/moped/cluster.rb', line 200 def inspect "<#{self.class.name} nodes=#{@nodes.inspect}>" 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.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/moped/cluster.rb', line 55 def nodes current_time = Time.new down_boundary = current_time - @options[:down_interval] refresh_boundary = current_time - @options[:refresh_interval] # Find the nodes that were down but are ready to be refreshed, or those # with stale connection information. needs_refresh, available = @nodes.partition do |node| (node.down? && node.down_at < down_boundary) || node.needs_refresh?(refresh_boundary) 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? || node.arbiter? } end |
#refresh(nodes_to_refresh = @nodes) ⇒ 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.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/moped/cluster.rb', line 88 def refresh(nodes_to_refresh = @nodes) 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. @nodes << node unless @nodes.include?(node) begin node.refresh # This node is good, so add it to the list of nodes to return. refreshed_nodes << node unless refreshed_nodes.include?(node) # Now refresh any newly discovered peer nodes. (node.peers - @nodes).each(&refresh_node) rescue Errors::ConnectionFailure # We couldn't connect to the node, so don't do anything with it. end end end nodes_to_refresh.each(&refresh_node) refreshed_nodes.to_a end |
#with_primary(retry_on_failure = true, &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.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/moped/cluster.rb', line 135 def with_primary(retry_on_failure = true, &block) if node = nodes.find(&:primary?) begin node.ensure_primary do return yield node.apply_auth(auth) end rescue Errors::ConnectionFailure, Errors::ReplicaSetReconfigured # Fall through to the code below if our connection was dropped or the # node is no longer the primary. end end if retry_on_failure # We couldn't find a primary node, so refresh the list and try again. refresh with_primary(false, &block) else raise( Errors::ConnectionFailure, "Could not connect to a primary node for replica set #{inspect}" ) end end |
#with_secondary(retry_on_failure = true, &block) ⇒ Object
Yields a secondary node if available, otherwise the primary node. This method will retry the block in case of connection errors.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/moped/cluster.rb', line 175 def with_secondary(retry_on_failure = true, &block) available_nodes = nodes.shuffle!.partition(&:secondary?).flatten while node = available_nodes.shift begin return yield node.apply_auth(auth) rescue Errors::ConnectionFailure # That node's no good, so let's try the next one. next end end if retry_on_failure # We couldn't find a secondary or primary node, so refresh the list and # try again. refresh with_secondary(false, &block) else raise( Errors::ConnectionFailure, "Could not connect to any secondary or primary nodes for replica set #{inspect}" ) end end |