class Lhm::Throttler::SlaveLag

Constants

DEFAULT_MAX_ALLOWED_LAG
DEFAULT_STRIDE
INITIAL_TIMEOUT
MAX_TIMEOUT

Attributes

allowed_lag[RW]
connection[RW]
stride[RW]
timeout_seconds[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/lhm/throttler/slave_lag.rb, line 25
def initialize(options = {})
  @timeout_seconds = INITIAL_TIMEOUT
  @stride = options[:stride] || DEFAULT_STRIDE
  @allowed_lag = options[:allowed_lag] || DEFAULT_MAX_ALLOWED_LAG
  @slaves = {}
  @get_config = options[:current_config]
  @check_only = options[:check_only]
end

Public Instance Methods

execute() click to toggle source
# File lib/lhm/throttler/slave_lag.rb, line 34
def execute
  sleep(throttle_seconds)
end

Private Instance Methods

get_slaves() click to toggle source
# File lib/lhm/throttler/slave_lag.rb, line 58
def get_slaves
  slaves = []
  if @check_only.nil? or !@check_only.respond_to?(:call)
    slave_hosts = master_slave_hosts
    while slave_hosts.any? do
      host = slave_hosts.pop
      slave = Slave.new(host, @get_config)
      if !slaves.map(&:host).include?(host) && slave.connection
        slaves << slave
        slave_hosts.concat(slave.slave_hosts)
      end
    end
  else
    slave_config = @check_only.call
    slaves << Slave.new(slave_config['host'], @get_config)
  end
  slaves
end
master_slave_hosts() click to toggle source
# File lib/lhm/throttler/slave_lag.rb, line 77
def master_slave_hosts
  Throttler.format_hosts(@connection.select_values(Slave::SQL_SELECT_SLAVE_HOSTS))
end
max_current_slave_lag() click to toggle source
# File lib/lhm/throttler/slave_lag.rb, line 81
def max_current_slave_lag
  max = slaves.map { |slave| slave.lag }.push(0).max
  Lhm.logger.info "Max current slave lag: #{max}"
  max
end
slaves() click to toggle source
# File lib/lhm/throttler/slave_lag.rb, line 54
def slaves
  @slaves[@connection] ||= get_slaves
end
throttle_seconds() click to toggle source
# File lib/lhm/throttler/slave_lag.rb, line 40
def throttle_seconds
  lag = max_current_slave_lag

  if lag > @allowed_lag && @timeout_seconds < MAX_TIMEOUT
    Lhm.logger.info("Increasing timeout between strides from #{@timeout_seconds} to #{@timeout_seconds * 2} because #{lag} seconds of slave lag detected is greater than the maximum of #{@allowed_lag} seconds allowed.")
    @timeout_seconds = @timeout_seconds * 2
  elsif lag <= @allowed_lag && @timeout_seconds > INITIAL_TIMEOUT
    Lhm.logger.info("Decreasing timeout between strides from #{@timeout_seconds} to #{@timeout_seconds / 2} because #{lag} seconds of slave lag detected is less than or equal to the #{@allowed_lag} seconds allowed.")
    @timeout_seconds = @timeout_seconds / 2
  else
    @timeout_seconds
  end
end