Module: Galera

Defined in:
lib/apprentice/checks/galera.rb

Overview

Contains Galera specific methods for checking cluster member consistency

Constant Summary collapse

STATES =

Galera knows a couple of different states. This constant describes their respective meaning for user feedback and, possibly, logging purposes.

{1 => 'Joining', 2 => 'Donor/Desynced', 3 => 'Joined', 4 => 'Synced'}

Instance Method Summary collapse

Instance Method Details

#check_cluster_sizeObject

Checks whether the cluster size as reported by the member is above 1. Any value below 2 is considered bad, as a cluster, by definition, should consist of at least 2 members connected to each other.

A cluster size of 1 might also indicate a split-brain situation.

Return values

  • true or false - depending on the value of @status['wsrep_cluster_size']

Examples

@status = Hash.new

@status['wsrep_cluster_size'] = 3
r = check_cluster_size
r.inspect # => true

@status['wsrep_cluster_size'] = 1
r = check_cluster_size
r.inspect # => false


100
101
102
103
# File 'lib/apprentice/checks/galera.rb', line 100

def check_cluster_size
  return true if Integer(@status['wsrep_cluster_size']) > 1
  false
end

#check_local_stateObject

Checks how the cluster member sees itself in terms of status

Valid states, read from the 'wsrep_local_state' variable and depending on the configuration, are 4, meaning Synced, or 2, meaning Donor/Desynced, if the option --accept-donor was passed at runtime.

Return values

  • true or false - depending on the value of @status['wsrep_local_state']

Examples

@status = Hash.new
@donor_allowed = false

@status['wsrep_local_state'] = 4
r = check_local_state
r.inspect # => true

@status['wsrep_local_state'] = 2
r = check_local_state
r.inspect # => false

@donor_allowed = true
@status['wsrep_local_state'] = 2
r = check_local_state
r.inspect # => true


155
156
157
158
159
# File 'lib/apprentice/checks/galera.rb', line 155

def check_local_state
  s = Integer(@status['wsrep_local_state'])
  return true if s == 4 || (s == 2 && @donor_allowed)
  false
end

#check_ready_stateObject

Checks whether the cluster replication is running and active. If this returns false the 'wsrep_ready' status variable is set to 'OFF' and thus the server is not an active member of a running cluster.

Return values

  • true or false - depending on the value of @status['wsrep_ready']

Examples

@status = Hash.new

@status['wsrep_ready'] = 'ON'
r = check_ready_state
r.inspect # => true

@status['wsrep_ready'] = 'OFF'
r = check_ready_state
r.inspect # => false


124
125
126
127
# File 'lib/apprentice/checks/galera.rb', line 124

def check_ready_state
  return true if @status['wsrep_ready'] == 'ON'
  false
end

#get_galera_statusObject

Gets the actual status from the Galera cluster member using the Mysql2 gem. Notice that we’re using the EventMachine-enabled Mysql2::Client.

Right now it only returns the relevant error output and continues working afterwards.

Nothing is mentioned about explicitly closing a client connection in the Mysql2 docs, however, we need to be careful with the amount of connections we’re using since we might find ourselves in an environment where the number of connections is constraint for a very few.

Return values

  • @status - Contains a hash of all the relevant wsrep_* variables to be examined by #run_checks



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/apprentice/checks/galera.rb', line 20

def get_galera_status
  begin
    client = Mysql2::Client.new(
        host: @server,
        port: @sql_port,
        username: @user,
        password: @password,
        as: :array
    )
    result = client.query "SHOW STATUS LIKE 'wsrep_%';"
    if result.count > 0

      # We need to do some conversion here in order to get a usable hash
      result.each do |r|
        @status.merge!(Hash[*r])
      end
    end
    client.close
  rescue Exception => message
    # FIXME Properly handle exception
    puts message
  end
end

#run_checksObject

Returns the relevant status HTTP code accompanied by a useful user feedback text

Attributes

  • @status - Should contain a hash with the relevant information to determine the the cluster member status. Also see #get_galera_status.

Return values

  • response - A hash containing a HTTP :code and a :text to return to the user

Example

@status = {'wsrep_cluster_size' => 4 }
response = self.run_checks # => {:code => 503, :text => 'Some text'}


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/apprentice/checks/galera.rb', line 59

def run_checks
  get_galera_status
  unless @status.empty?
    response = {code: 200, text: []}
    if !check_cluster_size
      response[:text] << "Cluster size is #{@status['wsrep_cluster_size']}. Split-brain situation is likely."
    end
    if !check_ready_state
      response[:text] << 'Cluster replication is not running.'
    end
    if !check_local_state
      response[:text] << "Local state is '#{STATES[@status['wsrep_local_state']]}'."
    end
    response[:code] = 503 unless response[:text].empty?
    return response
  else
    return {code: 503, text: ['Unable to determine cluster status']}
  end
end