File: SlaveUtil.pm

package info (click to toggle)
mha4mysql-node 0.58-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 232 kB
  • sloc: perl: 2,648; makefile: 8
file content (279 lines) | stat: -rw-r--r-- 8,030 bytes parent folder | download | duplicates (3)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

package MHA::SlaveUtil;

use strict;
use warnings FATAL => 'all';

use Carp;
use English qw(-no_match_vars);
use File::Basename;
use MHA::NodeUtil;
use DBI;

use constant Get_Version_SQL => "SELECT VERSION() AS Value";
use constant Get_Datadir_SQL => "SELECT \@\@global.datadir AS Value";
use constant Get_Relay_Log_Info_Type_SQL =>
  "SELECT \@\@global.relay_log_info_repository AS Value";
use constant Get_Relay_Log_File_SQL =>
  "SELECT Relay_log_name FROM mysql.slave_relay_log_info";
use constant Get_Relay_Log_Info_SQL =>
  "SELECT \@\@global.relay_log_info_file AS Value";
use constant Is_Relay_Purge_SQL => "SELECT \@\@global.relay_log_purge As Value";
use constant Show_Log_Error_File_SQL => "SHOW VARIABLES LIKE 'log_error'";
use constant Show_Slave_Status_SQL   => "SHOW SLAVE STATUS";
use constant Disable_Relay_Purge_SQL => "SET GLOBAL relay_log_purge=0";
use constant Enable_Relay_Purge_SQL  => "SET GLOBAL relay_log_purge=1";
use constant Flush_Relay_Logs_SQL =>
  "FLUSH NO_WRITE_TO_BINLOG /*!50501 RELAY */ LOGS";
use constant Get_Failover_Lock_SQL =>
  "SELECT GET_LOCK('MHA_Master_High_Availability_Failover', ?) AS Value";
use constant Release_Failover_Lock_SQL =>
  "SELECT RELEASE_LOCK('MHA_Master_High_Availability_Failover') As Value";
use constant Get_Monitor_Lock_SQL =>
  "SELECT GET_LOCK('MHA_Master_High_Availability_Monitor', ?) AS Value";
use constant Release_Monitor_Lock_SQL =>
  "SELECT RELEASE_LOCK('MHA_Master_High_Availability_Monitor') As Value";

sub get_variable($$) {
  my $dbh   = shift;
  my $query = shift;
  my $sth   = $dbh->prepare($query);
  $sth->execute();
  my $href = $sth->fetchrow_hashref;
  return $href->{Value};
}

sub get_version($) {
  my $dbh = shift;
  return get_variable( $dbh, Get_Version_SQL );
}

sub get_log_error_file($) {
  my $dbh = shift;
  return get_variable( $dbh, Show_Log_Error_File_SQL );
}

sub get_relay_log_info_type {
  my $dbh           = shift;
  my $mysql_version = shift;
  my $type;
  $mysql_version = get_version($dbh) unless ($mysql_version);
  if ( !is_mariadb($mysql_version) && MHA::NodeUtil::mysql_version_ge( $mysql_version, "5.6.2" ) ) {
    $type = get_variable( $dbh, Get_Relay_Log_Info_Type_SQL );
  }
  unless ( defined($type) ) {
    $type = "FILE";
  }
  return $type;
}

sub get_relay_dir_file_from_table($) {
  my $dbh  = shift;
  my $sth  = $dbh->prepare(Get_Relay_Log_File_SQL);
  my $ret  = $sth->execute();
  my $href = $sth->fetchrow_hashref;
  if ( !defined($href) || !defined( $href->{Relay_log_name} ) ) {
    return;
  }
  my $current_relay_log_file = $href->{Relay_log_name};
  my $datadir = get_variable( $dbh, Get_Datadir_SQL );
  my $relay_dir;
  unless ( $current_relay_log_file =~ m/^\// ) {
    $current_relay_log_file =~ s/^\.\///;
    $current_relay_log_file = $datadir . "/" . $current_relay_log_file;
    $relay_dir              = $datadir;
    $relay_dir =~ s/\/$//;
  }
  else {
    $relay_dir = dirname($current_relay_log_file);
  }
  my $relay_log_basename = basename($current_relay_log_file);
  return ( $relay_dir, $relay_log_basename );
}

sub get_relay_log_info_path {
  my $dbh           = shift;
  my $mysql_version = shift;
  my $relay_log_info_path;
  my $filename;
  my $datadir = get_variable( $dbh, Get_Datadir_SQL );
  $mysql_version = get_version($dbh) unless ($mysql_version);
  if ( MHA::NodeUtil::mysql_version_ge( $mysql_version, "5.1.0" ) ) {
    $filename = get_variable( $dbh, Get_Relay_Log_Info_SQL );
  }

#relay_log_info_file was introduced in 5.1. In 5.0, it's fixed to "relay_log.info"
  if ( !defined($filename) ) {
    $filename = "relay-log.info";
  }
  unless ( $filename =~ m/^\// ) {
    $filename =~ s/^\.\///;
    $datadir =~ s/\/$//;
    $relay_log_info_path = $datadir . "/" . $filename;
  }
  else {
    $relay_log_info_path = $filename;
  }
  return $relay_log_info_path;
}

sub is_relay_log_purge($) {
  my $dbh = shift;
  return get_variable( $dbh, Is_Relay_Purge_SQL );
}

sub disable_relay_log_purge($) {
  my $dbh = shift;
  $dbh->do(Disable_Relay_Purge_SQL);
}

sub purge_relay_logs($) {
  my $dbh = shift;
  $dbh->do(Enable_Relay_Purge_SQL);
  $dbh->do(Flush_Relay_Logs_SQL);

  # To (almost) make sure that relay log is switched(purged) before setting
  # relay_log_purge=0;
  sleep 3;
  $dbh->do(Disable_Relay_Purge_SQL);
  return 0;
}

sub is_slave($) {
  my $dbh   = shift;
  my $query = Show_Slave_Status_SQL;
  my $sth   = $dbh->prepare($query);
  my $ret   = $sth->execute();
  if ( !defined($ret) || $ret != 1 ) {
    return 0;
  }
  return 1;
}

sub is_mariadb($) {
  my $version_str   = shift;
  if ($version_str =~ m/.*MariaDB.*/i)
  {
    return 1;
  }
  return 0;
}

sub get_advisory_lock_internal($$$) {
  my $dbh     = shift;
  my $timeout = shift;
  my $query   = shift;
  my $sth     = $dbh->prepare($query);
  my $ret     = $sth->execute($timeout);
  my $href    = $sth->fetchrow_hashref;
  if ( !defined($href) || !defined( $href->{Value} ) ) {
    return 2;
  }
  elsif ( $href->{Value} != 1 ) {
    return 1;
  }
  return 0;
}

sub release_advisory_lock_internal($$) {
  my $dbh   = shift;
  my $query = shift;
  my $sth   = $dbh->prepare($query);
  my $ret   = $sth->execute();
  my $href  = $sth->fetchrow_hashref;
  if ( !defined($href) || !defined( $href->{Value} ) || $href->{Value} != 1 ) {
    return 1;
  }
  return 0;
}

sub get_failover_advisory_lock {
  my $dbh     = shift;
  my $timeout = shift;
  return get_advisory_lock_internal( $dbh, $timeout, Get_Failover_Lock_SQL );
}

sub release_failover_advisory_lock($) {
  my $dbh = shift;
  return release_advisory_lock_internal( $dbh, Release_Failover_Lock_SQL );
}

sub get_monitor_advisory_lock {
  my $dbh     = shift;
  my $timeout = shift;
  return get_advisory_lock_internal( $dbh, $timeout, Get_Monitor_Lock_SQL );
}

sub release_monitor_advisory_lock($) {
  my $dbh = shift;
  return release_advisory_lock_internal( $dbh, Release_Monitor_Lock_SQL );
}

sub check_if_super_read_only {
  my $host = shift;
  my $ip = shift;
  my $port = shift;
  my $user = shift;
  my $pass = shift;
  # create databasehandle to check super_read_only setting
  my $dsn_host = $ip =~ m{:} ? '[' . $ip . ']' : $ip;
  my $dsn = "DBI:mysql:;host=$dsn_host;port=$port";
  my $dbh =
    DBI->connect( $dsn, $user, MHA::NodeUtil::unescape_for_shell($pass),
    { PrintError => 0, RaiseError => 1 } );
  croak "Failed to get DB Handle to check super_read_only on $host:$port!\n" unless ($dbh);

  my $sth = $dbh->prepare("SELECT \@\@global.super_read_only as Value");
  $sth->execute();
  my $href = $sth->fetchrow_hashref;
  if ( $href->{Value} == '1' ) {
    return (1, $dbh);
  }
  return (0, 0);
}

sub disable_super_read_only {
  my $dbh = shift;
  my $log = shift;
  my $message = "Disabling super_read_only, enabling read_only, so that the applying can be done on the slave\n";
  if ( $log ) {
    $log->info($message);
  } else {
    print $message;
  }
  $dbh->do("SET GLOBAL super_read_only=off;");
  $dbh->do("SET GLOBAL read_only=on;");
}

sub enable_super_read_only {
  my $dbh = shift;
  my $log = shift;
  my $message = "Enabling super_read_only again after applying\n";
  if ( $log ) {
    $log->info($message);
  } else {
    print $message;
  }
  $dbh->do("SET GLOBAL super_read_only=on;");
}

1;