pt_table_sync

Summary
pt_table_sync
Variables
%dsn_for
Functions
main
lock_and_renameLock and rename a table.
sync_one_tableSync one table between one source host and multiple destination hosts.
sync_via_replicationSync multiple destination hosts to one source host via replication.
sync_allSync every table between one source host and multiple destination hosts.
lock_serverLock a host with FLUSH TABLES WITH READ LOCK.
unlock_serverUnlock a host with UNLOCK TABLES.
sync_a_tableSync the destination host table to the source host table.
get_change_dbhReturn the dbh to write to for syncing changes.
make_action_subsMake callbacks for ChangeHandler::new() actions argument.
print_errTry to extract the MySQL error message and print it.
get_cxnConnect to host specified by DSN.
ok_to_syncCheck that the destination host table can be synced to the source host table.
filter_diffsFilter different slave tables according to the various schema object filters.
disconnectDisconnect host dbhs created by get_cxn().
print_sqlCallback for TableSyncer::sync_table() if --print --verbose --verbose is specified.
cmd_conflict_colCompare --conflict-column values for --bidirectional.
set_bidirectional_callbacksSet syncer plugin callbacks for --bidirectional.
get_pluginsGet internal TableSync* plugins.
get_server_timeReturn HH:MM:SS of SELECT NOW() from the server.

Variables

%dsn_for

my %dsn_for

Functions

main

sub main

lock_and_rename

sub lock_and_rename

Lock and rename a table.

Parameters

%argsArguments

Required Arguments

dsnsArrayref of DSNs
pluginsArrayref of TableSync* objects
OptionParserOptionParser object
DSNParserDSNParser object
QuoterQuoter object

Returns

Exit status

sync_one_table

sub sync_one_table

Sync one table between one source host and multiple destination hosts.  The first DSN in $args{dsns} specifies the source host, database (D), and table (t).  The other DSNs are the destination hosts.  If a destination DSN does not specify a database or table, the source database or table are used as defaults.  Else, the destination-specific database or table are used.  This allows you to sync tables with different names.

Parameters

%argsArguments

Required Arguments

dsnsArrayref of DSNs
pluginsArrayref of TableSync* objects
OptionParserOptionParser object
DSNParserDSNParser object
QuoterQuoter object
VersionParserVersionParser object

Returns

Exit status

sync_via_replication

sub sync_via_replication

Sync multiple destination hosts to one source host via replication.  The first DSN in $args{dsns} specifies the source host.  If --sync-to-master is specified, then the source host is a master and there is only one destination host which is its slave.  Else, destination hosts are auto-discovered with MasterSlave::recurse_to_slaves().

Parameters

%argsArguments

Required Arguments

dsnsArrayref of DSNs
pluginsArrayref of TableSync* objects
OptionParserOptionParser object
DSNParserDSNParser object
QuoterQuoter object
VersionParserVersionParser object
TableChecksumTableChecksum object
MasterSlaveMasterSlave object

Returns

Exit status

See Also

filter_diffs()

sync_all

sub sync_all

Sync every table between one source host and multiple destination hosts.  The first DSN in $args{dsns} specifies the source host.  The other DSNs are the destination hosts.  Unlike sync_one_table, the database and table names must be the same on the source and destination hosts.

Parameters

%argsArguments

Required Arguments

dsnsArrayref of DSNs
pluginsArrayref of TableSync* objects
OptionParserOptionParser object
DSNParserDSNParser object
QuoterQuoter object
VersionParserVersionParser object
TableParserTableParser object
MySQLDumpMySQLDump object

Returns

Exit status

lock_server

sub lock_server

Lock a host with FLUSH TABLES WITH READ LOCK.  This implements --lock 3 by calling TableSyncer::lock_and_wait().

Parameters

%argsArguments

Required Arguments

srcHashref with source host information
dstHashref with destination host information
OptionParserOptionParser object
DSNParserDSNParser object
TableSyncerTableSyncer object

unlock_server

sub unlock_server

Unlock a host with UNLOCK TABLES.  This implements --lock 3 by calling TableSyncer::unlock().

Parameters

%argsArguments

Required Arguments

srcHashref with source host information
dstHashref with destination host information
OptionParserOptionParser object
DSNParserDSNParser object
TableSyncerTableSyncer object

sync_a_table

sub sync_a_table

Sync the destination host table to the source host table.  This sub is not called directly but indirectly via the other sync_* subs.  In turn, this sub calls TableSyncer::sync_table() which actually does the sync work.  Calling sync_table() requires a fair amount of prep work that this sub does/simplifies.  New RowDiff and ChangeHandler objects are created, so those packages need to be available.

Parameters

$argsArguments

Required Arguments

srcHashref with source host information
dstHashref with destination host information
pluginsArrayref of TableSync* objects
OptionParserOptionParser object
QuoterQuoter object
TableParserTableParser object
MySQLDumpMySQLDump object
TableSyncerTableSyncer object

Returns

Exit status

get_change_dbh

sub get_change_dbh

Return the dbh to write to for syncing changes.  Write statements are executed on the “change dbh”.  If --sync-to-master or --replicate is specified, the source (master) dbh is the “change dbh”.  This means changes replicate to all slaves.  Else, the destination dbh is the change dbh.  This is the case when two independent servers (or perhaps one table on the same server) are synced.  This sub implements --[no]check-slave because writing to a slave is generally a bad thing.

Parameters

%argsArguments

Required Arguments

srcHashref with source host information
dstHashref with destination host information
tbl_structHashref returned by TableParser::parse()
OptionParserOptionParser object
DSNParserDSNParser object
MasterSlaveMasterSlave object

Returns

Either $args{src}->{dbh} or $args{dst}->{dbh} if no checks fail.

See Also

make_action_subs()

make_action_subs

sub make_action_subs

Make callbacks for ChangeHandler::new() actions argument.  This sub implements --print and --execute.

Parameters

%argsArguments

Required Arguments

change_dbhdbh returned by get_change_dbh
OptionParserOptionParser object

Returns

Arrayref of callbacks (coderefs)

print_err

Try to extract the MySQL error message and print it.

Parameters

$msgError message
$databaseDatabase name being synced when error occurred
$tableTable name being synced when error occurred
$hostHost name error occurred on

get_cxn

Connect to host specified by DSN.

Parameters

$dsnHost DSN
%argsArguments

Required Arguments

OptionaParserOptionParser object
DSNParserDSNParser object

Returns

dbh

ok_to_sync

Check that the destination host table can be synced to the source host table.  All sorts of sanity checks are performed to help ensure that syncing the table won’t cause problems in sync_a_table() or TableSyncer::sync_table().

Parameters

%argsArguments

Required Arguments

srcHashref with source host information
dstHashref with destination host information
DSNParserDSNParser object
QuoterQuoter object
VersionParserVersionParser object
TableParserTableParser object
MySQLDumpMySQLDump object
TableSyncerTableSyncer object
OptionParserOptionParser object

Returns

Table structure (from TableParser::parse()) if ok to sync, else it dies.

filter_diffs

Filter different slave tables according to the various schema object filters.  This sub is called in sync_via_replication() to implement schema object filters like --databases and --tables.

Parameters

$skip_tableHashref of databases and tables to skip
$databasesHashref of databases to skip
$tablesHashref of tables to skip
@diffsArray of hashrefs, one for each different slave table

Returns

Array of different slave tables that pass the filters

disconnect

Disconnect host dbhs created by get_cxn().  To make sure all dbh are closed, pt-table-sync keeps track of the dbh it opens and this sub helps keep track of the dbh that are closed.

Parameters

@hostsArray of hashrefs with host information, one for each host

print_sql

Callback for TableSyncer::sync_table() if --print --verbose --verbose is specified.  The callback simply prints the SQL statements passed to it by sync_table().  They’re usually (always?) identical statements.

Parameters

$src_sqlSQL statement to be executed on the sourch host
$dst_sqlSQL statement to be executed on the destination host

cmd_conflict_col

Compare --conflict-column values for --bidirectional.  This sub is used as a callback in set_bidirectional_callbacks().

Parameters

$left_valColumn value from left (usually the source host)
$right_valColumn value from right (usually the destination host)
$cmpType of conflict comparison, --conflict-comparison
$valValue for certain types of comparisons, --conflict-value
$thrThreshold for certain types of comparisons, --conflict-threshold

Returns

One of the constants above, UPDATE_* or FAILED_THRESHOLD

set_bidirectional_callbacks

Set syncer plugin callbacks for --bidirectional.

Parameters

%argsArguments

Required Arguments

pluginTableSync* object
OptionParserOptionParser object

get_plugins

Get internal TableSync* plugins.

Returns

Hash of available algoritms and the plugin/module names that implement them, like “chunk => TableSyncChunk”.

get_server_time

Return HH:MM:SS of SELECT NOW() from the server.

Parameters

$dbhdbh
my %dsn_for
sub main
sub lock_and_rename
Lock and rename a table.
sub sync_one_table
Sync one table between one source host and multiple destination hosts.
sub sync_via_replication
Sync multiple destination hosts to one source host via replication.
sub sync_all
Sync every table between one source host and multiple destination hosts.
sub lock_server
Lock a host with FLUSH TABLES WITH READ LOCK.
sub unlock_server
Unlock a host with UNLOCK TABLES.
sub sync_a_table
Sync the destination host table to the source host table.
sub get_change_dbh
Return the dbh to write to for syncing changes.
sub make_action_subs
Make callbacks for ChangeHandler::new() actions argument.
sub new
Connect to host specified by DSN.
sub sync_table
OptionParser parses command line options from a tool’s POD.
DSNParser parses DSNs and creates connections to MySQL using DBI and DBD::mysql.
Quoter handles value quoting, unquoting, escaping, etc.
VersionParser parses a MySQL version string.
sub recurse_to_slaves
Descend to slaves by examining SHOW SLAVE HOSTS.
TableChecksum checksums tables.
MasterSlave handles common tasks related to master-slave setups.
Filter different slave tables according to the various schema object filters.
TableParser parses SHOW CREATE TABLE.
MySQLDump gets CREATE TABLE defs from MySQL.
sub lock_and_wait
TableSyncer helps sync tables with various table sync algo modules.
sub unlock
RowDiff compares two sets of rows to find ones that are different.
ChangeHandler creates SQL statements for changing rows in a table.
sub parse
Parse SHOW CREATE TABLE.
Set syncer plugin callbacks for --bidirectional.
Close