Class: OkComputer::MongoidReplicaSetCheck

Inherits:
Check
  • Object
show all
Defined in:
lib/ok_computer/built_in_checks/mongoid_replica_set_check.rb

Overview

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.

Constant Summary collapse

ConnectionFailed =
Class.new(StandardError)

Constants inherited from Check

Check::CheckNotDefined

Instance Attribute Summary collapse

Attributes inherited from Check

#failure_occurred, #message, #registrant_name, #time

Instance Method Summary collapse

Methods inherited from Check

#<=>, #clear, #mark_failure, #mark_message, #run, #success?, #to_json, #to_text, #with_benchmarking

Constructor Details

#initialize(session = :default) ⇒ MongoidReplicaSetCheck

Public: Initialize a check for a Mongoid replica set

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

default session.


26
27
28
29
30
# 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

Instance Attribute Details

#sessionObject

Returns the value of attribute session.



20
21
22
# File 'lib/ok_computer/built_in_checks/mongoid_replica_set_check.rb', line 20

def session
  @session
end

Instance Method Details

#checkObject

Public: Return the status of the mongodb replica set



33
34
35
36
37
38
39
40
41
# 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_statusObject

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

Returns a hash with the status of the primary



53
54
55
56
57
58
59
# 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

#refreshObject

Public: Refresh the cluster status



44
45
46
47
48
# 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_statusObject

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

Returns a hash with the status of the secondary



64
65
66
67
68
69
70
# 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