class MysqlReplicaFinder::Finder

Public Class Methods

new(connection, recurse=false) click to toggle source
# File lib/mysql_replica_finder.rb, line 7
def initialize(connection, recurse=false)
  @replicas = []
  @primary_connection = connection
  @recurse = recurse
end

Public Instance Methods

fetch() click to toggle source
# File lib/mysql_replica_finder.rb, line 13
def fetch
  unless @recurse
    @replicas << replica_hosts_from_connection(@primary_connection)
  else
    replica_hosts_from_connection_rec(@primary_connection)
  end
end
replica_hosts_from_connection(connection) click to toggle source

Returns an array or replicas that are connected

# File lib/mysql_replica_finder.rb, line 27
def replica_hosts_from_connection(connection)
  replicas = []
  rows = select_all(connection, "SHOW PROCESSLIST")
  rows.each do |row|
    if row["Command"] == "Binlog Dump"
      replicas.push(row['Host'].split(":")[0])
    end
  end
  replicas
end
replica_hosts_from_connection_rec(connection) click to toggle source
# File lib/mysql_replica_finder.rb, line 46
def replica_hosts_from_connection_rec(connection)
  replica_hosts_from_connection(connection).map do |replica|
    @replicas << replica
    replica_hosts_from_connection_rec(transform_replica_to_connection(replica))
  end
end
replicas() click to toggle source
# File lib/mysql_replica_finder.rb, line 21
def replicas
  fetch
  @replicas
end
select_all(connection, query) click to toggle source
# File lib/mysql_replica_finder.rb, line 38
def select_all(connection, query)
  connection.query(query)
end
transform_replica_to_connection(host) click to toggle source
# File lib/mysql_replica_finder.rb, line 42
def transform_replica_to_connection(host)
  Mysql2::Client.new(@primary_connection.query_options.merge({ :host => host}))
end