| 1 |
#!/usr/bin/perl |
| 2 |
# make_replication_sandbox |
| 3 |
# The MySQL Sandbox |
| 4 |
# Copyright (C) 2006-2018 Giuseppe Maxia |
| 5 |
# |
| 6 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
# you may not use this file except in compliance with the License. |
| 8 |
# You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, software |
| 13 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
# See the License for the specific language governing permissions and |
| 16 |
# limitations under the License. |
| 17 |
|
| 18 |
use strict; |
| 19 |
use warnings; |
| 20 |
use English qw( -no_match_vars ); |
| 21 |
use Data::Dumper; |
| 22 |
use Getopt::Long qw(:config no_ignore_case ); |
| 23 |
use MySQL::Sandbox qw(get_ranges runs_as_root use_env sbinstr); |
| 24 |
use MySQL::Sandbox::Scripts; |
| 25 |
|
| 26 |
my $DEBUG = $MySQL::Sandbox::DEBUG; |
| 27 |
|
| 28 |
runs_as_root(); |
| 29 |
|
| 30 |
my %defaults = ( |
| 31 |
replication_base_port => $MySQL::Sandbox::default_base_port{replication}, |
| 32 |
circular_base_port => $MySQL::Sandbox::default_base_port{circular}, |
| 33 |
replication_directory => 'rsandbox', |
| 34 |
); |
| 35 |
|
| 36 |
unless ( $ENV{SANDBOX_HOME} ) { |
| 37 |
$ENV{SANDBOX_HOME} = "$ENV{HOME}/sandboxes"; |
| 38 |
} |
| 39 |
# |
| 40 |
# $SANDBOX_HOME must be set BEFORE loading MyScripts |
| 41 |
# |
| 42 |
#if ( -d "$ENV{HOME}/sandboxes" ) { |
| 43 |
# $ENV{SANDBOX_HOME} = $ENV{SANDBOX_HOME} || "$ENV{HOME}/sandboxes"; |
| 44 |
#} |
| 45 |
# |
| 46 |
unless ( -d $ENV{'SANDBOX_HOME'} ) { |
| 47 |
mkdir $ENV{'SANDBOX_HOME'} |
| 48 |
or die "can't create $ENV{'SANDBOX_HOME'} ($CHILD_ERROR) \n"; |
| 49 |
} |
| 50 |
|
| 51 |
#my $install_dir; |
| 52 |
#$install_dir = $PROGRAM_NAME; |
| 53 |
#$install_dir =~ s{/\w+(\.pl)?$}{}; |
| 54 |
# eval "use lib q($install_dir)"; |
| 55 |
|
| 56 |
my $msb = MySQL::Sandbox->new(); |
| 57 |
|
| 58 |
$msb->parse_options( MySQL::Sandbox::Scripts::parse_options_replication() ); |
| 59 |
|
| 60 |
GetOptions ( |
| 61 |
map { $msb->{parse_options}{$_}{parse}, \$msb->{options}{$_} } |
| 62 |
grep { $msb->{parse_options}{$_}{parse}} keys %{$msb->{parse_options}} |
| 63 |
) or $msb->get_help(); |
| 64 |
|
| 65 |
$msb->get_help() if $msb->{options}{'help'}; |
| 66 |
|
| 67 |
unless ($msb->{options}{server_version}) { |
| 68 |
$msb->{options}{server_version} = shift |
| 69 |
or $msb->get_help('server version required'); |
| 70 |
} |
| 71 |
|
| 72 |
if ($msb->{options}{master_options}) |
| 73 |
{ |
| 74 |
$ENV{MASTER_OPTIONS} = $msb->{options}{master_options}; |
| 75 |
} |
| 76 |
if ($msb->{options}{node_options}) |
| 77 |
{ |
| 78 |
$ENV{NODE_OPTIONS} = $msb->{options}{node_options}; |
| 79 |
} |
| 80 |
if ($msb->{options}{slave_options}) |
| 81 |
{ |
| 82 |
$ENV{SLAVE_OPTIONS} = $msb->{options}{slave_options}; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
if ($msb->{options}{one_node_options}) |
| 87 |
{ |
| 88 |
for my $node_opt (@{ $msb->{options}{one_node_options}} ) |
| 89 |
{ |
| 90 |
if ($node_opt =~ /^(\d+):\s*(\S.+)/) |
| 91 |
{ |
| 92 |
$ENV{"NODE${1}_OPTIONS"} = $2; |
| 93 |
} |
| 94 |
else |
| 95 |
{ |
| 96 |
get_help("invalid format for --one_node_option ($node_opt)"); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
if ($msb->{options}{one_slave_options}) |
| 101 |
{ |
| 102 |
for my $node_opt (@{ $msb->{options}{one_slave_options}} ) |
| 103 |
{ |
| 104 |
if ($node_opt =~ /^(\d+):\s*(\S.+)/) |
| 105 |
{ |
| 106 |
$ENV{"SLAVE${1}_OPTIONS"} = $2; |
| 107 |
} |
| 108 |
else |
| 109 |
{ |
| 110 |
get_help("invalid format for --one_slave_option ($node_opt)"); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
for my $opt (qw(repl_user repl_password remote_access)) { |
| 117 |
if (defined $msb->{options}{$opt}) { |
| 118 |
$MySQL::Sandbox::default_users{$opt} = $msb->{options}{$opt}; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
my $base_version ; |
| 123 |
my $release_no; |
| 124 |
my $create_gtid_script=0; |
| 125 |
my $master_use_gtid=0; |
| 126 |
my $dashed_version ; |
| 127 |
my $replication_port = 0; |
| 128 |
my $prefix=''; |
| 129 |
my $additional_master_options = $ENV{MASTER_OPTIONS} || ''; |
| 130 |
my $temp_path = $msb->{options}{server_version}; |
| 131 |
$temp_path =~ s{.*/}{}; |
| 132 |
if ( $temp_path =~ /^(\D+)\d+/ ) { |
| 133 |
$prefix = $1; |
| 134 |
} |
| 135 |
if ( $temp_path =~ /(\d+)\.(\d+)\.(\d+)/ ) { |
| 136 |
my ($major, $minor, $rev) = ($1, $2, $3); |
| 137 |
$base_version = "${major}${minor}${rev}"; |
| 138 |
$release_no = $rev; |
| 139 |
$dashed_version = "${prefix}${major}_${minor}_${rev}"; |
| 140 |
$base_version =~ s/^0+//; |
| 141 |
$base_version += $major + ($minor * 10) + ($rev * 100); |
| 142 |
if ( ($major > 5) # MySQL 6 |
| 143 |
or (($major == 5) && ($minor == 6) && ($rev >= 9 )) #MySQL 5.6.9 and newer |
| 144 |
or (( $major == 5) && ($minor > 6)) # MySQL 5.7 and newer |
| 145 |
) |
| 146 |
{ |
| 147 |
$create_gtid_script=1; |
| 148 |
} |
| 149 |
if ($create_gtid_script && ($major == 10)) |
| 150 |
{ |
| 151 |
$create_gtid_script =0; |
| 152 |
$master_use_gtid=1; |
| 153 |
} |
| 154 |
} |
| 155 |
else { |
| 156 |
$msb->get_help("No recognizable version pattern in $msb->{options}{server_version}\n"); |
| 157 |
} |
| 158 |
|
| 159 |
my @supported_topologies = qw(standard circular); |
| 160 |
# |
| 161 |
# deals with circular replication |
| 162 |
# |
| 163 |
|
| 164 |
my $node_list_text = 'master'; |
| 165 |
my @node_list = ('master'); |
| 166 |
|
| 167 |
if ($msb->{options}{master_master}) { |
| 168 |
$msb->{options}{topology} = 'circular'; |
| 169 |
$msb->{options}{how_many_slaves} = 2; |
| 170 |
} |
| 171 |
|
| 172 |
if ($msb->{options}{circular} && ($msb->{options}{circular} =~ /^\d+$/)) { |
| 173 |
$msb->{options}{topology} = 'circular'; |
| 174 |
$msb->{options}{how_many_slaves} = $msb->{options}{circular}; |
| 175 |
} |
| 176 |
|
| 177 |
if ($msb->{options}{topology} eq 'circular') |
| 178 |
{ |
| 179 |
$node_list_text = ''; |
| 180 |
@node_list = (); |
| 181 |
} |
| 182 |
|
| 183 |
for my $n (1 .. $msb->{options}{how_many_slaves}) |
| 184 |
{ |
| 185 |
$node_list_text .= " node$n"; |
| 186 |
push @node_list, "node$n"; |
| 187 |
} |
| 188 |
|
| 189 |
if ($msb->{options}{'topology'} ) { |
| 190 |
if ($msb->{options}{'topology'} eq 'standard') { |
| 191 |
# do nothing. default behavior |
| 192 |
} |
| 193 |
elsif ($msb->{options}{'topology'} eq 'circular') { |
| 194 |
make_circular_replication(); |
| 195 |
} |
| 196 |
else { |
| 197 |
die qq(unsupported topology "$msb->{options}{topology}". Allowed options: {@supported_topologies}\n); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
# print Dumper(\%{$msb->{options}}); |
| 202 |
|
| 203 |
my $replication_directory ; |
| 204 |
|
| 205 |
if ( $msb->{options}{replication_directory}) { |
| 206 |
$replication_directory = "$msb->{options}{upper_directory}/$msb->{options}{replication_directory}"; |
| 207 |
} |
| 208 |
else { |
| 209 |
$replication_directory = "$msb->{options}{upper_directory}/$defaults{replication_directory}_$dashed_version"; |
| 210 |
} |
| 211 |
|
| 212 |
if ( -d $replication_directory ) { |
| 213 |
if ( -x "$replication_directory/clear_all") { |
| 214 |
system "$replication_directory/clear_all"; |
| 215 |
} |
| 216 |
system "rm -rf $replication_directory/*"; |
| 217 |
} |
| 218 |
else { |
| 219 |
print "creating replication directory $replication_directory\n" if $msb->{options}{verbose}; |
| 220 |
mkdir $replication_directory; |
| 221 |
unless ( -d $replication_directory ) { |
| 222 |
die "error creating replication directory n"; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
for my $dir ( 1 .. $msb->{options}{how_many_slaves}) { |
| 227 |
if ( -d "$replication_directory/node$dir" ) { |
| 228 |
system "rm -rf $replication_directory/node$dir" |
| 229 |
and die "unable to clean $replication_directory/node$dir\n"; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
if ( -d "$replication_directory/master" ) { |
| 234 |
system "rm -rf $replication_directory/master" |
| 235 |
and die "unable to clean $replication_directory/master\n"; |
| 236 |
} |
| 237 |
|
| 238 |
if ( $msb->{options}{sandbox_base_port}) { |
| 239 |
$replication_port = $msb->{options}{sandbox_base_port}; |
| 240 |
} |
| 241 |
else { |
| 242 |
$replication_port = $defaults{replication_base_port} + $base_version + ($release_no * 100); |
| 243 |
} |
| 244 |
|
| 245 |
if ( $msb->{options}{check_base_port}) { |
| 246 |
$replication_port = get_ranges({ |
| 247 |
min_range => $replication_port, |
| 248 |
range_size => $msb->{options}{how_many_slaves}, |
| 249 |
max_range => 64000, |
| 250 |
search_path => $ENV{SANDBOX_HOME}, |
| 251 |
}, 1); |
| 252 |
} |
| 253 |
|
| 254 |
print "installing and starting master\n"; |
| 255 |
my $additional_slave_options = $ENV{SLAVE_OPTIONS} || ''; |
| 256 |
if ($ENV{NODE_OPTIONS}) { |
| 257 |
$additional_master_options .= q{ } . $ENV{NODE_OPTIONS}; |
| 258 |
$additional_slave_options .= q{ } . $ENV{NODE_OPTIONS}; |
| 259 |
} |
| 260 |
$additional_master_options .= q{ }; |
| 261 |
$additional_slave_options .= q{ }; |
| 262 |
if ( $msb->{options}{gtid}) { |
| 263 |
$additional_master_options .= ' --gtid '; |
| 264 |
$additional_slave_options .= ' --gtid '; |
| 265 |
} |
| 266 |
|
| 267 |
my $install_master=''; |
| 268 |
my $install_master_command = qq( |
| 269 |
make_sandbox $msb->{options}{server_version} -- \\ |
| 270 |
--datadir_from=script \\ |
| 271 |
--no_confirm \\ |
| 272 |
--no_check_port \\ |
| 273 |
--upper_directory=$replication_directory \\ |
| 274 |
--sandbox_directory=master \\ |
| 275 |
--sandbox_port=$replication_port \\ |
| 276 |
--repl_user=$msb->{options}{repl_user} \\ |
| 277 |
--repl_password=$msb->{options}{repl_password} \\ |
| 278 |
--remote_access=$msb->{options}{remote_access} \\ |
| 279 |
--load_grants \\ |
| 280 |
--prompt_prefix=master \\ |
| 281 |
--history_dir=$replication_directory \\ |
| 282 |
-c relay-log-index=mysql-relay \\ |
| 283 |
-c relay-log=mysql-relay \\ |
| 284 |
-c log-bin=mysql-bin \\ |
| 285 |
-c server-id=1 $additional_master_options |
| 286 |
); |
| 287 |
if ($msb->{options}{interactive} |
| 288 |
or ($additional_master_options =~ /\binteractive\b/)) { |
| 289 |
my $result = system($install_master_command); |
| 290 |
} |
| 291 |
else { |
| 292 |
my $install_master = qx($install_master_command); |
| 293 |
print $install_master if $msb->{options}{verbose}; |
| 294 |
} |
| 295 |
if ($CHILD_ERROR) { |
| 296 |
print "error installing the master\n"; |
| 297 |
print "$install_master\n($CHILD_ERROR $OS_ERROR)\n"; |
| 298 |
exit(1) |
| 299 |
} |
| 300 |
|
| 301 |
# sleep 1 ; |
| 302 |
|
| 303 |
for my $slave (1 .. $msb->{options}{how_many_slaves}) { |
| 304 |
my $slave_port = $replication_port+ $slave; |
| 305 |
my $slave_id = 100 + $slave; |
| 306 |
print "installing slave $slave\n"; |
| 307 |
my $this_slave_options = $ENV{"SLAVE${slave}_OPTIONS"} || ''; |
| 308 |
my $install_slave_command = qq( |
| 309 |
make_sandbox $msb->{options}{server_version} -- \\ |
| 310 |
--datadir_from=script \\ |
| 311 |
--upper_directory=$replication_directory \\ |
| 312 |
--sandbox_directory=node$slave \\ |
| 313 |
--no_confirm \\ |
| 314 |
--no_load_grants \\ |
| 315 |
--prompt_prefix=slave$slave \\ |
| 316 |
--sandbox_port=$slave_port \\ |
| 317 |
--repl_user=$msb->{options}{repl_user} \\ |
| 318 |
--repl_password=$msb->{options}{repl_password} \\ |
| 319 |
--remote_access=$msb->{options}{remote_access} \\ |
| 320 |
--history_dir=$replication_directory \\ |
| 321 |
-c server-id=$slave_id \\ |
| 322 |
-c report-host=SBslave$slave \\ |
| 323 |
-c report-port=$slave_port \\ |
| 324 |
-c relay-log-index=mysql-relay \\ |
| 325 |
-c relay-log=mysql-relay \\ |
| 326 |
-c log-bin=mysql-bin $additional_slave_options $this_slave_options |
| 327 |
); |
| 328 |
my $install_slave =''; |
| 329 |
if ($msb->{options}{interactive} |
| 330 |
or ($additional_slave_options =~ /\binteractive\b/)) { |
| 331 |
system($install_slave_command); |
| 332 |
} |
| 333 |
else { |
| 334 |
$install_slave = qx($install_slave_command); |
| 335 |
print $install_slave if $msb->{options}{verbose}; |
| 336 |
} |
| 337 |
if ($CHILD_ERROR) { |
| 338 |
print "error installing slave $slave\n"; |
| 339 |
print "$install_slave\n ($CHILD_ERROR)\n"; |
| 340 |
exit(1) |
| 341 |
} |
| 342 |
# sleep 1; |
| 343 |
} |
| 344 |
|
| 345 |
my $current_dir = $ENV{PWD}; |
| 346 |
|
| 347 |
chdir $replication_directory; |
| 348 |
|
| 349 |
for my $cmd ( qw(start stop clear send_kill ) ) { |
| 350 |
my $cmd_fname = $cmd . '_all'; |
| 351 |
$msb->write_to($cmd_fname, '>', '#!/bin/sh'); |
| 352 |
$msb->write_to($cmd_fname, '>>', qq(echo '# executing "$cmd"' on $replication_directory)); |
| 353 |
if ($cmd eq 'start') { |
| 354 |
$msb->write_to ($cmd_fname, '>>', qq(echo 'executing "$cmd" on master') ); |
| 355 |
$msb->write_to ($cmd_fname, '>>', qq($replication_directory/master/$cmd "\$@") ); |
| 356 |
} |
| 357 |
for my $slave (1 .. $msb->{options}{how_many_slaves} ) { |
| 358 |
$msb->write_to ($cmd_fname, '>>', qq(echo 'executing "$cmd" on slave $slave')); |
| 359 |
$msb->write_to ($cmd_fname, '>>', qq($replication_directory/node$slave/$cmd "\$@")); |
| 360 |
} |
| 361 |
if ($cmd ne 'start') { |
| 362 |
$msb->write_to ($cmd_fname, '>>', qq(echo 'executing "$cmd" on master') ); |
| 363 |
$msb->write_to ($cmd_fname, '>>', qq($replication_directory/master/$cmd "\$@")); |
| 364 |
} |
| 365 |
chmod 0755, $cmd_fname; |
| 366 |
} |
| 367 |
|
| 368 |
my $rw_splitting = '/usr/local/share/mysql-proxy/rw-splitting.lua'; |
| 369 |
my $mysql_proxy = '/usr/local/sbin/mysql-proxy'; |
| 370 |
if ( ( -x $mysql_proxy ) && ( -f $rw_splitting) ) { |
| 371 |
my $port = $replication_port; |
| 372 |
$msb->write_to('run_proxy', '>', '#!/bin/sh'); |
| 373 |
$msb->write_to('run_proxy', '>>', "$mysql_proxy \\" ); |
| 374 |
$msb->write_to('run_proxy', '>>', " --proxy-backend-addresses=127.0.0.1:$port \\" ); |
| 375 |
for my $slave ( 1 .. $msb->{options}{how_many_slaves}) { |
| 376 |
$port++; |
| 377 |
$msb->write_to('run_proxy', '>>', " --proxy-read-only-backend-addresses=127.0.0.1:$port \\" ); |
| 378 |
} |
| 379 |
$msb->write_to('run_proxy', '>>', " --proxy-lua-script=$rw_splitting" ); |
| 380 |
chmod 0755, 'run_proxy'; |
| 381 |
} |
| 382 |
$msb->write_to('clear_all', '>>', "date > $replication_directory/needs_initialization"); |
| 383 |
|
| 384 |
$msb->write_to('start_all', '>>', "if [ -f $replication_directory/needs_initialization ] "); |
| 385 |
$msb->write_to('start_all', '>>', "then"); |
| 386 |
$msb->write_to('start_all', '>>', " $replication_directory/initialize_slaves"); |
| 387 |
$msb->write_to('start_all', '>>', " rm -f $replication_directory/needs_initialization"); |
| 388 |
$msb->write_to('start_all', '>>', "fi"); |
| 389 |
|
| 390 |
|
| 391 |
$msb->write_to('m', '>', "#!/bin/sh"); |
| 392 |
$msb->write_to('m', '>>', qq($replication_directory/master/use "\$@") ); |
| 393 |
chmod 0755, 'm'; |
| 394 |
|
| 395 |
$msb->write_to('restart_all', '>', '#!/bin/sh'); |
| 396 |
$msb->write_to('restart_all', '>>', qq($replication_directory/stop_all)); |
| 397 |
$msb->write_to('restart_all', '>>', qq($replication_directory/start_all "\$@")); |
| 398 |
|
| 399 |
chmod 0755, 'restart_all'; |
| 400 |
$msb->write_to('use_all', '>', '#!/bin/sh'); |
| 401 |
my $slave_list = join(' ', (1 .. $msb->{options}{how_many_slaves}) ); |
| 402 |
|
| 403 |
$msb->write_to('use_all', '>>', 'if [ "$1" = "" ]'); |
| 404 |
$msb->write_to('use_all', '>>', 'then'); |
| 405 |
$msb->write_to('use_all', '>>', ' echo "syntax: $0 command"'); |
| 406 |
$msb->write_to('use_all', '>>', ' exit 1'); |
| 407 |
$msb->write_to('use_all', '>>', 'fi'); |
| 408 |
$msb->write_to('use_all', '>>', ''); |
| 409 |
$msb->write_to('use_all', '>>', 'echo "# master " ' ); |
| 410 |
$msb->write_to('use_all', '>>', 'echo "$@" | ' . $replication_directory . '/master/use '); |
| 411 |
$msb->write_to('use_all', '>>', ''); |
| 412 |
$msb->write_to('use_all', '>>', 'for SNUM in ' . $slave_list); |
| 413 |
$msb->write_to('use_all', '>>', 'do ' ); |
| 414 |
$msb->write_to('use_all', '>>', ' echo "# server: $SNUM: " ' ); |
| 415 |
$msb->write_to('use_all', '>>', ' echo "$@" | ' . $replication_directory . '/node$SNUM/use $MYCLIENT_OPTIONS '); |
| 416 |
$msb->write_to('use_all', '>>', 'done ' ); |
| 417 |
chmod 0755, 'use_all'; |
| 418 |
|
| 419 |
$msb->write_to('status_all', '>', '#!/bin/sh'); |
| 420 |
my $rdir = $replication_directory; |
| 421 |
$rdir =~ s{.*/}{}; |
| 422 |
$msb->write_to('status_all', '>>', "echo REPLICATION $rdir"); |
| 423 |
$msb->write_to('status_all', '>>', $replication_directory . '/master/status'); |
| 424 |
$msb->write_to('status_all', '>>', $replication_directory . '/master/use -BN -e' |
| 425 |
. qq( "select CONCAT('port: ', \@\@port) AS port" )); |
| 426 |
$msb->write_to('status_all', '>>', ''); |
| 427 |
$msb->write_to('status_all', '>>', 'for SNUM in ' . $slave_list); |
| 428 |
$msb->write_to('status_all', '>>', 'do ' ); |
| 429 |
$msb->write_to('status_all', '>>', ' ' . $replication_directory . '/node$SNUM/status'); |
| 430 |
$msb->write_to('status_all', '>>', ' ' . $replication_directory . '/node$SNUM/use -BN -e' |
| 431 |
. qq( "select CONCAT('port: ', \@\@port) AS port" )); |
| 432 |
$msb->write_to('status_all', '>>', 'done ' ); |
| 433 |
chmod 0755, 'status_all'; |
| 434 |
|
| 435 |
|
| 436 |
$msb->write_to('check_slaves', '>', '#!/bin/sh' ); |
| 437 |
$msb->write_to('check_slaves', '>>', 'echo "master"'); |
| 438 |
$msb->write_to('check_slaves', '>>', $replication_directory . '/master/use -BN -e' |
| 439 |
. qq( "select CONCAT('port: ', \@\@port) AS port" )); |
| 440 |
$msb->write_to('check_slaves', '>>', $replication_directory . '/master/use -e' |
| 441 |
. qq( 'show master status\\G' | grep "File\\|Position\\|Executed" )); |
| 442 |
$msb->write_to('check_slaves', '>>', 'for NNUM in ' . $slave_list ); |
| 443 |
$msb->write_to('check_slaves', '>>', 'do' ); |
| 444 |
$msb->write_to('check_slaves', '>>', ' echo "slave # $NNUM"'); |
| 445 |
$msb->write_to('check_slaves', '>>', ' ' . $replication_directory . '/node$NNUM/use -BN -e' |
| 446 |
. qq( "select CONCAT('port: ', \@\@port) AS port" )); |
| 447 |
$msb->write_to('check_slaves', '>>', ' ' . $replication_directory . '/node$NNUM/use -e' |
| 448 |
. qq( 'show slave status\\G' | grep "\\(Running:\\|Master_Log_Pos\\|\\<Master_Log_File\\|Retrieved\\|Executed\\)") ); |
| 449 |
$msb->write_to('check_slaves', '>>', 'done ' ); |
| 450 |
chmod 0755, 'check_slaves'; |
| 451 |
|
| 452 |
system "cp m n1"; |
| 453 |
chmod 0755, "n1"; |
| 454 |
|
| 455 |
for my $slave (1 .. $msb->{options}{how_many_slaves} ) { |
| 456 |
$msb->write_to("s$slave", '>', "#!/bin/sh"); |
| 457 |
$msb->write_to("s$slave", '>', qq($replication_directory/node$slave/use "\$@")); |
| 458 |
chmod 0755, "s$slave"; |
| 459 |
my $N=$slave+1; |
| 460 |
system "cp s$slave n$N"; |
| 461 |
chmod 0755, "s$slave"; |
| 462 |
chmod 0755, "n$N"; |
| 463 |
} |
| 464 |
|
| 465 |
$msb->write_to('initialize_slaves', '>', "#!/bin/sh"); |
| 466 |
$msb->write_to('initialize_slaves', '>>', ""); |
| 467 |
$msb->write_to('initialize_slaves', '>>', "# Don't use directly."); |
| 468 |
$msb->write_to('initialize_slaves', '>>', "# This script is called by 'start_all' when needed"); |
| 469 |
$msb->write_to('initialize_slaves', '>>', ""); |
| 470 |
for my $slave (1 .. $msb->{options}{how_many_slaves} ) { |
| 471 |
print "starting slave $slave\n"; |
| 472 |
system "./node$slave/start" |
| 473 |
and die "can't execute ./node$slave/start ($CHILD_ERROR $OS_ERROR)"; |
| 474 |
|
| 475 |
$msb->write_to('initialize_slaves', '>>', qq(echo "initializing slave $slave")); |
| 476 |
|
| 477 |
$msb->write_to('initialize_slaves', '>>', |
| 478 |
qq(echo 'CHANGE MASTER TO ) |
| 479 |
. qq( master_host="$msb->{options}{master_ip}", ) |
| 480 |
. qq( master_port=$replication_port, ) |
| 481 |
. qq( master_user="$MySQL::Sandbox::default_users{'repl_user'}", ) |
| 482 |
. qq( master_password="$MySQL::Sandbox::default_users{'repl_password'}" )); |
| 483 |
if ($master_use_gtid) |
| 484 |
{ |
| 485 |
$msb->write_to('initialize_slaves', '>>', |
| 486 |
qq(, master_use_gtid=current_pos) ); |
| 487 |
} |
| 488 |
if ($msb->{options}{gtid}) |
| 489 |
{ |
| 490 |
$msb->write_to('initialize_slaves', '>>', ', MASTER_AUTO_POSITION=1'); |
| 491 |
} |
| 492 |
$msb->write_to('initialize_slaves', '>>', |
| 493 |
qq(' | $replication_directory/node$slave/use -u root ) ); |
| 494 |
|
| 495 |
$msb->write_to('initialize_slaves', '>>', |
| 496 |
qq( echo 'START SLAVE' ) |
| 497 |
. qq( | $replication_directory/node$slave/use -u root ) ); |
| 498 |
system "perl -i -pe 's/^password/#password/' ./node$slave/my.sandbox.cnf" |
| 499 |
} |
| 500 |
|
| 501 |
# |
| 502 |
# Creates the replication user |
| 503 |
# |
| 504 |
##system qq(./m -u root -e 'grant replication slave on *.* ) |
| 505 |
## . qq( to "$MySQL::Sandbox::default_users{'repl_user'}"\@"127.%" ) |
| 506 |
## . qq( identified by "$MySQL::Sandbox::default_users{'repl_password'}"') |
| 507 |
## and die "can't create slave user"; |
| 508 |
|
| 509 |
chmod 0755, "initialize_slaves"; |
| 510 |
|
| 511 |
system "./initialize_slaves" |
| 512 |
and die "can't initialize slave"; |
| 513 |
|
| 514 |
# |
| 515 |
# For the first run, there is root without password. |
| 516 |
# After the first run, a password is assigned |
| 517 |
# |
| 518 |
for my $slave (1 .. $msb->{options}{how_many_slaves} ) { |
| 519 |
system "perl -i -pe 's/^#password/password/' ./node$slave/my.sandbox.cnf" |
| 520 |
} |
| 521 |
# system q( perl -i -pe 's/(-u root) (--password=)/$1/g' initialize_slaves ); |
| 522 |
|
| 523 |
post_installation($replication_directory); |
| 524 |
$msb->write_to('README', '>', MySQL::Sandbox::Scripts::get_readme_master_slave()); |
| 525 |
$msb->write_to('README', '>>', MySQL::Sandbox::Scripts::get_readme_common_replication()); |
| 526 |
$msb->write_to('README', '>>', MySQL::Sandbox::Scripts::get_readme_common()); |
| 527 |
|
| 528 |
for my $json (qw(connection.json default_connection.json)) |
| 529 |
{ |
| 530 |
my $json_text = MySQL::Sandbox::get_json_from_dirs(\@node_list, $json); |
| 531 |
$msb->write_to($json, '>', $json_text); |
| 532 |
} |
| 533 |
chdir $current_dir; |
| 534 |
print "replication directory installed in ", use_env($replication_directory), "\n"; |
| 535 |
|
| 536 |
# |
| 537 |
# instrumentation |
| 538 |
# |
| 539 |
sbinstr( "replication_directory installed in <" |
| 540 |
. use_env($replication_directory) . ">"); |
| 541 |
|
| 542 |
sub make_circular_replication { |
| 543 |
my $cmd = "make_multiple_sandbox --circular"; |
| 544 |
my $group_dir = 'rcsandbox_' . $dashed_version; |
| 545 |
if ($msb->{options}{replication_directory} ) { |
| 546 |
$group_dir = $msb->{options}{replication_directory}; |
| 547 |
} |
| 548 |
|
| 549 |
if ($msb->{options}{how_many_slaves}) { |
| 550 |
$cmd .= ' --how_many_nodes=' . $msb->{options}{how_many_slaves}; |
| 551 |
} |
| 552 |
$cmd .= ' --group_directory=' . $group_dir; |
| 553 |
if ($msb->{options}{sandbox_base_port} ) { |
| 554 |
$cmd .= ' --sandbox_base_port=' . $msb->{options}{sandbox_base_port} |
| 555 |
} |
| 556 |
else { |
| 557 |
$cmd .= ' --sandbox_base_port=' . ($defaults{circular_base_port} + ($release_no * 100)); |
| 558 |
} |
| 559 |
my $result = system "$cmd $msb->{options}{server_version}" ; |
| 560 |
|
| 561 |
post_installation("$msb->{options}{upper_directory}/$group_dir") unless $result; |
| 562 |
exit $result; |
| 563 |
} |
| 564 |
|
| 565 |
sub post_installation |
| 566 |
{ |
| 567 |
my ($replication_directory) = @_; |
| 568 |
# print "++ post_installation: <$replication_directory> <$node_list_text> <$create_gtid_script>\n"; |
| 569 |
unless (-d $replication_directory) |
| 570 |
{ |
| 571 |
die "Error in post_installation procedure. $replication_directory not found\n"; |
| 572 |
} |
| 573 |
chdir $replication_directory; |
| 574 |
|
| 575 |
my $gtid_script_template=<<'END_TEMPLATE'; |
| 576 |
#!/bin/bash |
| 577 |
cd _REPLICATION_DIR_ |
| 578 |
OPTIONS="master-info-repository=table " |
| 579 |
OPTIONS="$OPTIONS relay-log-info-repository=table" |
| 580 |
OPTIONS="$OPTIONS gtid_mode=ON" |
| 581 |
OPTIONS="$OPTIONS log-slave-updates enforce-gtid-consistency" |
| 582 |
CHANGED="" |
| 583 |
for NODE in _NODE_LIST_ |
| 584 |
do |
| 585 |
for OPTION in $OPTIONS |
| 586 |
do |
| 587 |
option_exists=$(grep $OPTION $NODE/my.sandbox.cnf) |
| 588 |
if [ -z "$option_exists" ] |
| 589 |
then |
| 590 |
echo "$OPTION" >> $NODE/my.sandbox.cnf |
| 591 |
echo "# option '$OPTION' added to $NODE configuration file" |
| 592 |
CHANGED=1 |
| 593 |
else |
| 594 |
echo "# option '$OPTION' already exists in $NODE configuration file" |
| 595 |
fi |
| 596 |
done |
| 597 |
done |
| 598 |
if [ -n "$CHANGED" ] |
| 599 |
then |
| 600 |
./restart_all |
| 601 |
for NODE in _NODE_LIST_ |
| 602 |
do |
| 603 |
if [ "$NODE" != "master" ] |
| 604 |
then |
| 605 |
$NODE/use -e 'stop slave' |
| 606 |
$NODE/use -e 'CHANGE MASTER TO MASTER_AUTO_POSITION=1' |
| 607 |
$NODE/use -e 'start slave' |
| 608 |
fi |
| 609 |
done |
| 610 |
fi |
| 611 |
END_TEMPLATE |
| 612 |
|
| 613 |
$msb->write_to('test_replication', '>', '#!/bin/bash' ); |
| 614 |
my $test_replication="$ENV{SANDBOX_HOME}/test_replication"; |
| 615 |
$msb->write_to('test_replication', '>>', "cd $replication_directory" ); |
| 616 |
$msb->write_to('test_replication', '>>', "TEST_REPLICATION=$test_replication" ); |
| 617 |
$msb->write_to('test_replication', '>>', 'if [ -x $TEST_REPLICATION ]' ); |
| 618 |
$msb->write_to('test_replication', '>>', 'then' ); |
| 619 |
$msb->write_to('test_replication', '>>', ' $TEST_REPLICATION' ); |
| 620 |
$msb->write_to('test_replication', '>>', 'else' ); |
| 621 |
$msb->write_to('test_replication', '>>', ' echo "$TEST_REPLICATION not found"' ); |
| 622 |
$msb->write_to('test_replication', '>>', ' exit 1' ); |
| 623 |
$msb->write_to('test_replication', '>>', 'fi' ); |
| 624 |
chmod 0755, 'test_replication'; |
| 625 |
if ($create_gtid_script) |
| 626 |
{ |
| 627 |
$gtid_script_template =~ s/_REPLICATION_DIR_/$replication_directory/; |
| 628 |
$gtid_script_template =~ s/_NODE_LIST_/$node_list_text/g; |
| 629 |
$msb->write_to('enable_gtid', '>', $gtid_script_template); |
| 630 |
chmod 0755, 'enable_gtid'; |
| 631 |
} |
| 632 |
|
| 633 |
} |
| 634 |
|
| 635 |
__END__ |