MasterSlave

MasterSlave handles common tasks related to master-slave setups.

Summary
MasterSlaveMasterSlave handles common tasks related to master-slave setups.
Functions
new
recurse_to_slavesDescend to slaves by examining SHOW SLAVE HOSTS.
find_slave_hosts
_find_slaves_by_processlist
_find_slaves_by_hosts
get_connected_slaves
is_master_of
get_master_dsn
get_slave_status
get_master_status
wait_for_masterExecute MASTER_POS_WAIT() to make slave wait for its master.
stop_slave
start_slave
catchup_to_master
catchup_to_same_pos
slave_is_running
has_slave_updates
repl_posn
get_slave_lag
pos_cmp
short_hostSimplify a hostname as much as possible.
is_replication_threadDetermine if a processlist item is a replication thread.
get_replication_filtersGet any replication filters set on the host.
pos_to_stringStringify a position in a way that’s string-comparable.
reset_known_replication_threads
_d

Functions

new

sub new

recurse_to_slaves

sub recurse_to_slaves

Descend to slaves by examining SHOW SLAVE HOSTS.  The callback gets the slave’s DSN, dbh, parent, and the recursion level as args.  The recursion is tail recursion.

Parameters

$argsHashref of arguments
$levelRecursion level

Required Arguments

dsnThe DSN to connect to; if no dbh arg, connect using this.
recurseHow many levels to recurse.  0 = none, undef = infinite.
callbackCode to execute after finding a new slave.
dsn_parserDSNParser object

Optional Arguments

dbhdbh
skip_callbackExecute with slaves that will be skipped.
methodWhether to prefer HOSTS over PROCESSLIST
parentThe DSN from which this call descended.

find_slave_hosts

sub find_slave_hosts

_find_slaves_by_processlist

sub _find_slaves_by_processlist

_find_slaves_by_hosts

sub _find_slaves_by_hosts

get_connected_slaves

sub get_connected_slaves

is_master_of

sub is_master_of

get_master_dsn

sub get_master_dsn

get_slave_status

sub get_slave_status

get_master_status

sub get_master_status

wait_for_master

sub wait_for_master

Execute MASTER_POS_WAIT() to make slave wait for its master.

Parameters

%argsArguments

Required Arguments

Optional Arguments

  • timeout - Wait time in seconds (default 60)

Returns

Hashref with result of waiting, like:

{
  result => the result returned by MASTER_POS_WAIT: -1, undef, 0+
  waited => the number of seconds waited, might be zero
}

stop_slave

sub stop_slave

start_slave

sub start_slave

catchup_to_master

sub catchup_to_master

catchup_to_same_pos

sub catchup_to_same_pos

slave_is_running

sub slave_is_running

has_slave_updates

sub has_slave_updates

repl_posn

sub repl_posn

get_slave_lag

sub get_slave_lag

pos_cmp

sub pos_cmp

short_host

sub short_host

Simplify a hostname as much as possible.  For purposes of replication, a hostname is really just the combination of hostname and port, since replication always uses TCP connections (it does not work via sockets).  If the port is the default 3306, it is omitted.  As a convenience, this sub accepts either SHOW SLAVE STATUS or a DSN.

Parameters

$dsnDSN hashref

Returns

Short hostname string

is_replication_thread

sub is_replication_thread

Determine if a processlist item is a replication thread.

Parameters

$queryHashref of a processlist item
%argsArguments

Arguments

typeWhich kind of repl thread to match: all, binlog_dump (master), slave_io, or slave_sql (default: all)
check_known_idsCheck known replication thread IDs (default: yes)

Returns

True if the proclist item is the given type of replication thread.

get_replication_filters

sub get_replication_filters

Get any replication filters set on the host.

Parameters

%argsArguments

Required Arguments

dbhdbh, master or slave

Returns

Hashref of any replication filters.  If none are set, an empty hashref is returned.

pos_to_string

sub pos_to_string

Stringify a position in a way that’s string-comparable.

Parameters

$posHashref with file and position

Returns

String like “file/posNNNNN”

reset_known_replication_threads

sub reset_known_replication_threads

_d

sub _d
sub new
sub recurse_to_slaves
Descend to slaves by examining SHOW SLAVE HOSTS.
sub find_slave_hosts
sub _find_slaves_by_processlist
sub _find_slaves_by_hosts
sub get_connected_slaves
sub is_master_of
sub get_master_dsn
sub get_slave_status
sub get_master_status
sub wait_for_master
Execute MASTER_POS_WAIT() to make slave wait for its master.
sub stop_slave
sub start_slave
sub catchup_to_master
sub catchup_to_same_pos
sub slave_is_running
sub has_slave_updates
sub repl_posn
sub get_slave_lag
sub pos_cmp
sub short_host
Simplify a hostname as much as possible.
sub is_replication_thread
Determine if a processlist item is a replication thread.
sub get_replication_filters
Get any replication filters set on the host.
sub pos_to_string
Stringify a position in a way that’s string-comparable.
sub reset_known_replication_threads
sub _d
DSNParser parses DSNs and creates connections to MySQL using DBI and DBD::mysql.
Close