-
Notifications
You must be signed in to change notification settings - Fork 5
/
pt-online-schema-change-plugin.pl
95 lines (71 loc) · 2.43 KB
/
pt-online-schema-change-plugin.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use strict ;
package pt_online_schema_change_plugin ;
sub new {
my ($class, %args) = @_;
my $self = { %args };
return bless $self, $class;
}
sub init {
my ($self, %args) = @_;
my $dbh = $self->{aux_cxn}->dbh;
my @dbname = $dbh->selectrow_array("SELECT DATABASE()") ;
if ( $self->{execute} ) {
`nodebot dba "pt-online-schema-change starting alter in $dbname[0]: $self->{alter}"` ;
}
}
sub before_exit {
my ($self, %args) = @_;
my $dbh = $self->{aux_cxn}->dbh;
my @dbname = $dbh->selectrow_array("SELECT DATABASE()") ;
if ( $self->{execute} ) {
`nodebot dba "pt-online-schema-change finishing alter in $dbname[0]: $self->{alter}"` ;
}
}
sub get_slave_lag {
my ($self, %args) = @_;
# oktorun is a reference, also update it using $$oktorun=0;
my $oktorun=$args{oktorun};
# This is a good place to check that you have everything you need
# to verify replication across all calls to get_slave_lag.
my $lag = sub {
# this subroutine will be called every time pt-online-schema-change
# calls get_slave_lag (which is done by default for each replica
# detected). In our case, we set recurse=0 and get_slave_lag calls a
# central service that gives us the lag amount across the entire
# replication hierarchy.
#
# You can do anything you want! For testing purposes here, we'll
# just set it to 3.
my $current_lag = 3 ;
if ($current_lag =~ /^\d+$/) {
$$oktorun = 1 ;
return $current_lag ;
} else {
print STDERR "ERROR: Bailling out, failed to get slave lag!\n" ;
$$oktorun = 0 ;
exit ;
}
} ;
return $lag ;
}
sub before_create_new_table {
my ($self, %args) = @_;
# connect to the database, and verify that read_only==0
my $dbh = $self->{aux_cxn}->dbh;
my @is_read_only = $dbh->selectrow_array("SELECT \@\@read_only") ;
if ( $is_read_only[0] != 0 ) {
print "\nERROR: MySQL is running in read-only mode, bailing out!\n\n" ;
exit 0 ;
} else {
return ;
}
}
sub before_swap_tables {
my ($self, %args) = @_;
# connect to the database, collect, and print binay log stats
my $dbh = $self->{aux_cxn}->dbh;
my @master_status = $dbh->selectrow_array("SHOW MASTER STATUS") ;
print "Captured binary log and position: @master_status\n" ;
return ;
}
1 ;