class OkComputer::MongoidReplicaSetCheck

This class provides a check for a mongodb replica set via the Mongoid ORM.

The check first refreshes the cluster status, which communicates with all the nodes, discovers any new ones, and figures out which node is the primary and which ones are the secondaries. Nodes that are recovering or unavailable are automatically removed from rotation. It's okay to do this fairly frequently.

The second part of the check attempts to contact the primary node (to ensure writes are accepted) and a secondary node (to ensure reads can be distributed).

This calls the replSetGetStatus command on the admin database of each node. This provides further information as well as the replica set's name. This could potentially be parsed for more actionable information.

Constants

ConnectionFailed

Attributes

session[RW]

Public Class Methods

new(session = :default) click to toggle source

Public: Initialize a check for a Mongoid replica set

session - The name of the Mongoid session to use. Defaults to the

default session.
# File lib/ok_computer/built_in_checks/mongoid_replica_set_check.rb, line 26
def initialize(session = :default)
  self.session = Mongoid::Sessions.with_name(session)
rescue => e
  # client/session not configured
end

Public Instance Methods

check() click to toggle source

Public: Return the status of the mongodb replica set

# File lib/ok_computer/built_in_checks/mongoid_replica_set_check.rb, line 33
def check
  refresh
  primary_status = self.primary_status
  secondary_status = self.secondary_status
  mark_message "Connected to #{session.cluster.nodes.count} nodes in mongodb replica set '#{primary_status['set']}'"
rescue ConnectionFailed => e
  mark_failure
  mark_message "Error: '#{e}'"
end
primary_status() click to toggle source

Public: The status for the session's mongodb replica set primary

Returns a hash with the status of the primary

# File lib/ok_computer/built_in_checks/mongoid_replica_set_check.rb, line 53
def primary_status
  session.cluster.with_primary do |primary|
    primary.command(:admin, replSetGetStatus: 1)
  end
rescue => e
  raise ConnectionFailed, e
end
refresh() click to toggle source

Public: Refresh the cluster status

# File lib/ok_computer/built_in_checks/mongoid_replica_set_check.rb, line 44
def refresh
  session.cluster.refresh
rescue => e
  raise ConnectionFailed, e
end
secondary_status() click to toggle source

Public: The status for the session's mongodb replica set secondary

Returns a hash with the status of the secondary

# File lib/ok_computer/built_in_checks/mongoid_replica_set_check.rb, line 64
def secondary_status
  session.cluster.with_secondary do |secondary|
    secondary.command(:admin, replSetGetStatus: 1)
  end
rescue => e
  raise ConnectionFailed, e
end