Class: CleanSweep::PurgeRunner::MysqlStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/clean_sweep/purge_runner/mysql_status.rb

Instance Method Summary (collapse)

Constructor Details

- (MysqlStatus) initialize(options = {})

Options: logger, model, max_history, max_repl_lag, check_period



7
8
9
10
11
12
13
14
# File 'lib/clean_sweep/purge_runner/mysql_status.rb', line 7

def initialize(options={})
  @logger = options[:logger] || ActiveRecord::Base.logger
  @model = options[:model]
  @max_history = options[:max_history]
  @max_replication_lag = options[:max_repl_lag]
  @check_period = options[:check_period] || 2.minutes
  @last_check = @check_period.ago
end

Instance Method Details

- (Object) all_clear!



59
60
61
62
# File 'lib/clean_sweep/purge_runner/mysql_status.rb', line 59

def all_clear!
  @last_check = Time.now
  @paused = nil
end

- (Object) check!



16
17
18
19
20
21
22
23
24
25
# File 'lib/clean_sweep/purge_runner/mysql_status.rb', line 16

def check!
  return if Time.now - @check_period < @last_check
  while (v = get_violations).any? do
    @logger.warn("pausing until threshold violations clear (#{v.to_a.map{ |key, value| "#{key} = #{value}"}.join(", ")})")
    @paused = true
    pause 5.minutes
  end
  @logger.info("violations clear") if paused?
  all_clear!
end

- (Object) get_history_length



74
75
76
77
78
79
80
# File 'lib/clean_sweep/purge_runner/mysql_status.rb', line 74

def get_history_length
  rows = @model.connection.select_rows <<-EOF
      show engine innodb status
  EOF
  status_string = rows.first[2]
  return /History list length ([0-9]+)/.match(status_string)[1].to_i
end

- (Object) get_replication_lag



64
65
66
67
68
69
70
71
# File 'lib/clean_sweep/purge_runner/mysql_status.rb', line 64

def get_replication_lag
  rows = @model.connection.select_rows 'SHOW SLAVE STATUS'
  if rows.nil? || rows.empty?
    return 0
  else
    return rows[0][32]
  end
end

- (Object) get_violations



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/clean_sweep/purge_runner/mysql_status.rb', line 27

def get_violations
  violations = {}
  if @max_history
    current = get_history_length
    violations["history length"] = "#{(current/1_000_000.0)} m" if threshold(@max_history) < current
  end
  if @max_replication_lag
    current = get_replication_lag
    violations["replication lag"] = current if threshold(@max_replication_lag) < current
  end
  return violations
end

- (Object) pause(time)



50
51
52
# File 'lib/clean_sweep/purge_runner/mysql_status.rb', line 50

def pause time
  Kernel.sleep time
end

- (Boolean) paused?

Returns:

  • (Boolean)


55
56
57
# File 'lib/clean_sweep/purge_runner/mysql_status.rb', line 55

def paused?
  @paused
end

- (Object) threshold(value)

Return the threshold to use in the check. If we are already failing, don't start up again until we've recovered at least 10%



42
43
44
45
46
47
48
# File 'lib/clean_sweep/purge_runner/mysql_status.rb', line 42

def threshold(value)
  if paused?
    value = 0.90 * value
  else
    value
  end
end