Class: Bones::RPC::Cluster

Inherits:
Object
  • Object
show all
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.

Since:

  • 1.0.0

Constant Summary collapse

DOWN_INTERVAL =

The default interval that a node would be flagged as “down”.

Since:

  • 2.0.0

30
REFRESH_INTERVAL =

The default interval that a node should be refreshed in.

Since:

  • 2.0.0

300
RETRY_INTERVAL =

The default time to wait to retry an operation.

Since:

  • 2.0.0

0.25

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, hosts) ⇒ Cluster

Initialize the new cluster.

Examples:

Initialize the cluster.

Cluster.new([ "localhost:27017" ], down_interval: 20)

Parameters:

  • options (Hash)

    The cluster options.

Since:

  • 1.0.0



79
80
81
82
83
# File 'lib/bones/rpc/cluster.rb', line 79

def initialize(session, hosts)
  @session = session
  @seeds = hosts.map { |host| Node.new(self, Address.new(host)) }
  @peers = []
end

Instance Attribute Details

#optionsHash

Returns The refresh options.

Returns:

  • (Hash)

    The refresh options.

Since:

  • 1.0.0



34
# File 'lib/bones/rpc/cluster.rb', line 34

attr_reader :session, :peers, :seeds

#peersArray<Node>

Returns The node peers.

Returns:

  • (Array<Node>)

    The node peers.



34
# File 'lib/bones/rpc/cluster.rb', line 34

attr_reader :session, :peers, :seeds

#seedsObject

Since:

  • 1.0.0



34
# File 'lib/bones/rpc/cluster.rb', line 34

attr_reader :session, :peers, :seeds

#sessionObject (readonly)

Since:

  • 1.0.0



34
35
36
# File 'lib/bones/rpc/cluster.rb', line 34

def session
  @session
end

Instance Method Details

#disconnecttrue

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.

Returns:

  • (true)

    True if the disconnect succeeded.

Since:

  • 1.2.0



43
44
45
# File 'lib/bones/rpc/cluster.rb', line 43

def disconnect
  nodes.each { |node| node.disconnect } and true
end

#down_intervalInteger

Get the interval at which a node should be flagged as down before retrying.

Examples:

Get the down interval, in seconds.

cluster.down_interval

Returns:

  • (Integer)

    The down interval.

Since:

  • 1.2.7



56
57
58
# File 'lib/bones/rpc/cluster.rb', line 56

def down_interval
  @down_interval ||= options[:down_interval] || DOWN_INTERVAL
end

#handle_refresh(node) ⇒ Object

Since:

  • 1.0.0



60
61
62
# File 'lib/bones/rpc/cluster.rb', line 60

def handle_refresh(node)
  session.handle_refresh(node)
end

#inspectString

Provide a pretty string for cluster inspection.

Examples:

Inspect the cluster.

cluster.inspect

Returns:

  • (String)

    A nicely formatted string.

Since:

  • 1.0.0



93
94
95
# File 'lib/bones/rpc/cluster.rb', line 93

def inspect
  "#<#{self.class.name}:#{object_id} @seeds=#{seeds.inspect}>"
end

#max_retriesInteger

Get the number of times an operation should be retried before raising an error.

Examples:

Get the maximum retries.

cluster.max_retries

Returns:

  • (Integer)

    The max retries.

Since:

  • 1.2.7



106
107
108
# File 'lib/bones/rpc/cluster.rb', line 106

def max_retries
  @max_retries ||= options[:max_retries] || seeds.size
end

#nodesArray<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.

Examples:

Get the available nodes.

cluster.nodes

Returns:

  • (Array<Node>)

    the list of available nodes.

Since:

  • 1.0.0



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_sizeObject

Since:

  • 1.0.0



139
140
141
# File 'lib/bones/rpc/cluster.rb', line 139

def pool_size
  options[: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.

Examples:

Refresh the nodes.

cluster.refresh

Parameters:

  • nodes_to_refresh (Array<Node>) (defaults to: seeds)

    The nodes to refresh.

Returns:

  • (Array<Node>)

    the available nodes

Since:

  • 1.0.0



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_intervalInteger

Get the interval in which the node list should be refreshed.

Examples:

Get the refresh interval, in seconds.

cluster.refresh_interval

Returns:

  • (Integer)

    The refresh interval.

Since:

  • 1.2.7



188
189
190
# File 'lib/bones/rpc/cluster.rb', line 188

def refresh_interval
  @refresh_interval ||= options[:refresh_interval] || REFRESH_INTERVAL
end

#retry_intervalInteger

Get the operation retry interval - the time to wait before retrying a single operation.

Examples:

Get the retry interval, in seconds.

cluster.retry_interval

Returns:

  • (Integer)

    The retry interval.

Since:

  • 1.2.7



201
202
203
# File 'lib/bones/rpc/cluster.rb', line 201

def retry_interval
  @retry_interval ||= options[:retry_interval] || RETRY_INTERVAL
end