-
Notifications
You must be signed in to change notification settings - Fork 3
/
pt-plugin-tungsten_replicator.pl
169 lines (130 loc) · 4.75 KB
/
pt-plugin-tungsten_replicator.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
## CONFIGURATION
# trepctl command to run
my $trepctl="/opt/tungsten/installs/cookbook/tungsten/tungsten-replicator/bin/trepctl";
# what tungsten replicator service to check
my $service="bravo";
# what user does tungsten replicator use to perform the writes?
# See Binlog Format for more information
my $tungstenusername = 'tungsten';
# ###############################################################
# The actual get_slave_lag which is invoked by the plugin classes
# ###############################################################
{
package plugin_tungsten_replicator;
use Data::Dumper;
local $Data::Dumper::Indent = 1;
local $Data::Dumper::Sortkeys = 1;
local $Data::Dumper::Quotekeys = 0;
use JSON::XS;
sub get_slave_lag {
my ($self, %args) = @_;
# oktorun is a reference, also update it using $$oktorun=0;
my $oktorun=$args{oktorun};
print "PLUGIN get_slave_lag: Using Tungsten Replicator to check replication lag\n";
my $get_lag = sub {
my ($cxn) = @_;
my $hostname = $cxn->{hostname};
my $lag;
my $json = `$trepctl -host $hostname -service $service status -json`;
# if trepctl doesn't return 0, something went wrong and we should abort
# the complete process
my $return = $? >> 8;
if ( $return != 0 )
{
$$oktorun=0;
die "\nCould not run trepctl successfully for $hostname in order to get replication lag:\n"
. $json;
}
my $status = decode_json $json;
if ( $status->{state} ne "ONLINE" ) {
print "Tungsten Replicator status of host $hostname is " . $status->{state} . ", waiting\n";
return;
}
$lag = sprintf("%.0f", $status->{appliedLatency});
# we return oktorun and the lag
return $lag;
};
return $get_lag;
}
}
1;
# #############################################################################
# pt_online_schema_change_plugin
# #############################################################################
{
package pt_online_schema_change_plugin;
use Data::Dumper;
local $Data::Dumper::Indent = 1;
local $Data::Dumper::Sortkeys = 1;
local $Data::Dumper::Quotekeys = 0;
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
sub new {
my ($class, %args) = @_;
my $self = { %args };
my $dbh = $self->{cxn}->dbh();
my ($binlog_format) = $dbh->selectrow_array('SELECT @@GLOBAL.binlog_format as binlog_format');
if ( $binlog_format eq "STATEMENT" ) {
$self->{custom_triggers} = 0;
} elsif ( $binlog_format eq "ROW" ) {
$self->{custom_triggers} = 1;
} elsif ( $binlog_format eq "MIXED" ) {
die ("The master it's binlog_format=MIXED,"
. " pt-online-schema change does not work well with "
. "Tungsten Replicator and binlog_format=MIXED.\n");
} else {
die ("Invalid binlog_format: " . $binlog_format . "\n");
}
return bless $self, $class;
}
sub get_slave_lag {
return plugin_tungsten_replicator::get_slave_lag(@_);
}
sub after_create_triggers {
my ($self, %args) = @_;
if ( $self->{custom_triggers} == 1 ) {
my $dbh = $self->{cxn}->dbh();
my $schema = $self->{cxn}->{dsn}->{D};
my $table = $self->{cxn}->{dsn}->{t};
my $dbhtriggers = $dbh->prepare("SHOW TRIGGERS IN " . $schema . " LIKE '" . $table . "'");
$dbhtriggers->execute();
my $trigger;
while ( $trigger = $dbhtriggers->fetchrow_hashref() ) {
$dbh->do("DROP TRIGGER " . $schema . "." . $trigger->{trigger})
or die ("PLUGIN was unable to drop the existing trigger in order to replace it"
. " with a tungsten replicator compatible one\n.");
$dbh->do("CREATE TRIGGER " . $trigger->{trigger} . " "
. $trigger->{timing} . " " . $trigger->{event}
. " ON $schema.$table "
. " FOR EACH ROW "
. " IF if(substring_index(user(),'\@',1) != '$tungstenusername',true, false) THEN "
. " " . $trigger->{statement} . "; "
. " END IF"
)
or die("PLUGIN could not create tungsten replicator compatible trigger\n");
}
}
}
}
1;
# #############################################################################
# pt_table_checksum_plugin
# #############################################################################
{
package pt_table_checksum_plugin;
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
sub new {
my ($class, %args) = @_;
my $self = { %args };
return bless $self, $class;
}
sub get_slave_lag {
return plugin_tungsten_replicator::get_slave_lag(@_);
}
}
1;