Class Moped::Cluster
In: lib/moped/cluster.rb
Parent: Object

The cluster represents a cluster of MongoDB server nodes, either a single node, a replica set, or a mongos server.

@since 1.0.0

Methods

Constants

DOWN_INTERVAL = 30   The default interval that a node would be flagged as "down".

@since 2.0.0

REFRESH_INTERVAL = 300   The default interval that a node should be refreshed in.

@since 2.0.0

RETRY_INTERVAL = 0.25   The default time to wait to retry an operation.

@since 2.0.0

Attributes

options  [R]  @!attribute options
  @return [ Hash ] The refresh options.

@!attribute peers

  @return [ Array<Node> ] The node peers.

@!attribute seeds

  @return [ Array<Node> ] The seed nodes.
peers  [R]  @!attribute options
  @return [ Hash ] The refresh options.

@!attribute peers

  @return [ Array<Node> ] The node peers.

@!attribute seeds

  @return [ Array<Node> ] The seed nodes.
seeds  [R]  @!attribute options
  @return [ Hash ] The refresh options.

@!attribute peers

  @return [ Array<Node> ] The node peers.

@!attribute seeds

  @return [ Array<Node> ] The seed nodes.

Public Class methods

Initialize the new cluster.

@example Initialize the cluster.

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

@param [ Hash ] options The cluster options.

@option options :down_interval number of seconds to wait before attempting

  to reconnect to a down node. (30)

@option options :refresh_interval number of seconds to cache information

  about a node. (300)

@option options [ Integer ] :timeout The time in seconds to wait for an

  operation to timeout. (5)

@since 1.0.0

Public Instance methods

Add a credential to the cluster

@example Get the applied credentials.

  node.credentials

@return [ Boolean ] true

@since 2.0.0

Remove a credential from the cluster

@example Get the applied credentials.

  node.delete_credential(database_name)

@return [ Boolean ] true

@since 2.0.0

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.

@return [ true ] True if the disconnect succeeded.

@since 1.2.0

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

@example Get the down interval, in seconds.

  cluster.down_interval

@return [ Integer ] The down interval.

@since 1.2.7

Provide a pretty string for cluster inspection.

@example Inspect the cluster.

  cluster.inspect

@return [ String ] A nicely formatted string.

@since 1.0.0

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

@example Get the maximum retries.

  cluster.max_retries

@return [ Integer ] The max retries.

@since 1.2.7

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.

@example Get the available nodes.

  cluster.nodes

@return [ Array<Node> ] the list of available nodes.

@since 1.0.0

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.

@example Refresh the nodes.

  cluster.refresh

@param [ Array<Node> ] nodes_to_refresh The nodes to refresh.

@return [ Array<Node> ] the available nodes

@since 1.0.0

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

@example Get the refresh interval, in seconds.

  cluster.refresh_interval

@return [ Integer ] The refresh interval.

@since 1.2.7

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

@example Get the retry interval, in seconds.

  cluster.retry_interval

@return [ Integer ] The retry interval.

@since 1.2.7

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.

@example Yield the primary to the block.

  cluster.with_primary do |node|
    # ...
  end

@param [ Integer ] retries The number of times to retry.

@raises [ ConnectionFailure ] When no primary node can be found

@return [ Object ] The result of the yield.

@since 1.0.0

Yields a secondary node if available, otherwise the primary node. This method will retry the block in case of connection errors.

@example Yield the secondary to the block.

  cluster.with_secondary do |node|
    # ...
  end

@param [ Integer ] retries The number of times to retry.

@raises [ ConnectionFailure ] When no primary node can be found

@return [ Object ] The result of the yield.

@since 1.0.0

[Validate]