Class: Lhm::Throttler::Replica

Inherits:
Object
  • Object
show all
Defined in:
lib/lhm/throttler/replica_lag.rb

Constant Summary collapse

SQL_SELECT_REPLICA_HOSTS =
"SELECT host FROM information_schema.processlist WHERE command LIKE 'Binlog Dump%'"
SQL_SELECT_MAX_REPLICA_LAG =
'SHOW SLAVE STATUS'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, connection_config = nil) ⇒ Replica

Returns a new instance of Replica.



102
103
104
105
106
# File 'lib/lhm/throttler/replica_lag.rb', line 102

def initialize(host, connection_config = nil)
  @host = host
  @connection_config = prepare_connection_config(connection_config)
  @connection = client(@connection_config)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



92
93
94
# File 'lib/lhm/throttler/replica_lag.rb', line 92

def connection
  @connection
end

#hostObject (readonly)

Returns the value of attribute host.



92
93
94
# File 'lib/lhm/throttler/replica_lag.rb', line 92

def host
  @host
end

Class Method Details

.clientObject



94
95
96
# File 'lib/lhm/throttler/replica_lag.rb', line 94

def self.client
  defined?(Mysql2::Client) ? Mysql2::Client : Trilogy
end

.client_errorObject



98
99
100
# File 'lib/lhm/throttler/replica_lag.rb', line 98

def self.client_error
  defined?(Mysql2::Error) ? Mysql2::Error : Trilogy::Error
end

Instance Method Details

#lagObject



112
113
114
# File 'lib/lhm/throttler/replica_lag.rb', line 112

def lag
  query_connection(SQL_SELECT_MAX_REPLICA_LAG, 'Seconds_Behind_Master').first.to_i
end

#replica_hostsObject



108
109
110
# File 'lib/lhm/throttler/replica_lag.rb', line 108

def replica_hosts
  Throttler.format_hosts(query_connection(SQL_SELECT_REPLICA_HOSTS, 'host'))
end