3
# Changes replication topology. This file is normally sourced from
4
# include/rpl_init.inc, but test cases can also source it if they
5
# need to change topology after they have sourced include/rpl_init.inc
7
# This file sets up variables needed by include/rpl_sync.inc and many
8
# other replication scripts in the include/ directory. It also issues
9
# CHANGE MASTER on all servers where the configuration changes from
10
# what it was before. It does not issue START SLAVE (use
11
# include/rpl_start_slaves.inc for that).
13
# Note: it is not currently possible to change the number of servers
14
# after the rpl_init.inc, without first calling rpl_end.inc. So the
15
# test has to set $rpl_server_count to the total number of servers
16
# that the test uses, before it sources include/rpl_init.inc. After
17
# that, $rpl_server_count must not change until after next time the
18
# test sources include/rpl_end.inc.
20
# Note: Since this script issues CHANGE MASTER, the test case must
21
# ensure that all slaves where the configuration changes have stopped
22
# both the IO thread and the SQL thread before this script is sourced.
27
# [--let $rpl_server_count= 7]
28
# --let $rpl_topology= 1->2->3->1->4, 2->5, 6->7
29
# [--let $rpl_skip_change_master= 1]
30
# [--let $rpl_master_log_file= 1:master-bin.000001,3:master-bin.000003]
31
# [--let $rpl_master_log_pos= 1:4711,3:107]
32
# [--let $rpl_debug= 1]
33
# --source include/rpl_change_topology.inc
36
# $rpl_master_log_file
37
# By default, CHANGE MASTER is executed with MASTER_LOG_FILE set
38
# to the name of the last binlog file on the master (retrieved by
39
# executing SHOW MASTER STATUS). This variable can be set to
40
# specify another filename. This variable should be a
41
# comma-separated list of the following form:
43
# SERVER_NUMBER_1:FILE_NAME_1,SERVER_NUMBER_2:FILE_NAME_2,...
45
# Before CHANGE MASTER is executed on server N, this script checks
46
# if $rpl_master_log_file contains the text N:FILE_NAME. If it
47
# does, then MASTER_LOG_FILE is set to FILE_NAME. Otherwise,
48
# MASTER_LOG_FILE is set to the last binlog on the master. For
49
# example, to specify that server_1 should start replicate from
50
# master-bin.000007 and server_5 should start replicate from
51
# master-bin.012345, do:
52
# --let $rpl_master_log_file= 1:master-bin.000007,5:master-bin.012345
55
# By default, CHANGE MASTER is executed without specifying the
56
# MASTER_LOG_POS parameter. This variable can be set to set a
57
# specific position. It has the same form as $rpl_master_log_file
58
# (see above). For example, to specify that server_3 should start
59
# replicate from position 4711 of its master, do:
60
# --let $rpl_master_log_pos= 3:4711
62
# $rpl_server_count, $rpl_topology, $rpl_debug, $rpl_skip_change_master
63
# See include/rpl_init.inc
66
# ==== Internal variables configured by this file ====
68
# This file sets up the following variables, which are used by other
69
# low-level replication files such as:
70
# include/rpl_sync.inc
71
# include/rpl_start_slaves.inc
72
# include/rpl_stop_slaves.inc
75
# $rpl_server_count_length:
76
# Set to LENGTH($rpl_server_count). So if $rpl_server_count < 10,
77
# then $rpl_server_count_length = 1; if 10 <= $rpl_server_count <
78
# 100, then $rpl_server_count_length = 2, etc.
81
# Set to a string consisting of $rpl_server_count numbers, each one
82
# whitespace-padded to $rpl_server_count_length characters. If
83
# server N is a slave, then the N'th number is the master of server
84
# N. If server N is not a slave, then the N'th number is just spaces
85
# (so in fact it is not a number). For example, if $rpl_topology is
86
# '1->2,2->3,3->1,2->4,5->6', then $rpl_master_list is '3122 6'.
88
# $rpl_sync_chain_dirty
89
# This variable is set to 1. This tells include/rpl_sync.inc to
90
# compute a new value for $rpl_sync_chain next time that
91
# include/rpl_sync.inc is sourced. See
92
# include/rpl_generate_sync_chain.inc and include/rpl_sync.inc for
96
# Remove whitespace from $rpl_topology
97
--let $rpl_topology= `SELECT REPLACE('$rpl_topology', ' ', '')`
99
--let $include_filename= rpl_change_topology.inc [new topology=$rpl_topology]
100
--source include/begin_include_file.inc
105
--echo ---- Check input ----
109
if (`SELECT '$rpl_topology' = '' OR '$rpl_server_count' = ''`)
111
--die You must set $rpl_topology and $rpl_server_count before you source rpl_change_topology.inc. If you really want to change to the empty topology, set $rpl_topology= none
113
--let $_rpl_topology= $rpl_topology
114
if (`SELECT '$_rpl_topology' = 'none'`)
116
--let $_rpl_topology=
118
if (`SELECT '!$rpl_master_list!' = '!!'`)
120
--die You must source include/rpl_init.inc before you source include/rpl_change_topology.inc
122
--let $_rpl_old_master_list= $rpl_master_list
126
--echo \$rpl_server_count='$rpl_server_count'
127
--echo \$rpl_server_count_length='$rpl_server_count_length'
128
--echo new \$rpl_topology='$_rpl_topology'
129
--echo old \$rpl_master_list='$rpl_master_list'
130
--echo old \$rpl_sync_chain='$rpl_sync_chain'
136
--echo ---- Generate \$rpl_server_count_length and \$rpl_master_list ----
139
--let $rpl_server_count_length= `SELECT LENGTH('$rpl_server_count')`
140
--let $rpl_master_list=
141
--let $_rpl_no_server= `SELECT REPEAT(' ', $rpl_server_count_length)`
142
--let $rpl_master_list= `SELECT REPEAT('$_rpl_no_server', $rpl_server_count)`
143
while ($_rpl_topology)
145
# Get 's1->s2' from 's1->s2->s3->...' or from 's1->s2,s3->s4,...'
146
--let $_rpl_master_slave= `SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('$_rpl_topology', ',', 1), '->', 2)`
147
# Modify $_rpl_topology as follows:
148
# - If it starts with 's1->s2,', remove 's1->s2,'
149
# - If it starts with 's1->s2->', remove 's1->'
150
# - If it is equal to 's1->s2', remove 's1->s2'
151
--let $_rpl_topology= `SELECT SUBSTR('$_rpl_topology', IF(SUBSTR('$_rpl_topology', LENGTH('$_rpl_master_slave') + 1, 2) != '->', LENGTH('$_rpl_master_slave'), LOCATE('->', '$_rpl_master_slave')) + 2)`
152
# Get 's1' from 's1->s2'
153
--let $_rpl_master= `SELECT SUBSTRING_INDEX('$_rpl_master_slave', '->', 1)`
154
# Get 's2' from 's1->s2'
155
--let $_rpl_slave= `SELECT SUBSTRING('$_rpl_master_slave', LENGTH('$_rpl_master') + 3)`
156
# Check that s2 does not have another master.
157
if (`SELECT SUBSTR('$rpl_master_list', 1 + ($_rpl_slave - 1) * $rpl_server_count_length, $rpl_server_count_length) != '$_rpl_no_server'`)
159
--echo ERROR IN TEST: Server '$_rpl_slave' has more than one master in topology '$rpl_topology'
160
--die ERROR IN TEST: found a server with more than one master in the $rpl_topology variable
162
# Save 's1' at position 's2' in $rpl_master_list
163
--let $rpl_master_list= `SELECT INSERT('$rpl_master_list', 1 + ($_rpl_slave - 1) * $rpl_server_count_length, $rpl_server_count_length, RPAD('$_rpl_master', $rpl_server_count_length, ' '))`
168
--echo new \$rpl_server_count_length = '$rpl_server_count_length'
169
--echo new \$rpl_master_list = '$rpl_master_list'
172
if (!$rpl_skip_change_master)
176
--echo ---- Execute CHANGE MASTER on all servers ----
184
--let $_rpl_server= $rpl_server_count
187
# The following statement evaluates to:
188
# 0, if server_$_rpl_server has the same master as before.
189
# The master's server, if server_$_rpl_server is a slave.
190
# The empty string, if server_$_rpl_server is not a slave.
191
--let $_rpl_master= `SELECT TRIM(IFNULL(NULLIF(SUBSTRING('$rpl_master_list', 1 + ($_rpl_server - 1) * $rpl_server_count_length, $rpl_server_count_length), SUBSTRING('$_rpl_old_master_list', 1 + ($_rpl_server - 1) * $rpl_server_count_length, $rpl_server_count_length)), 0))`
194
--echo \$_rpl_server='$_rpl_server' \$_rpl_master='$_rpl_master'
199
--let $_rpl_port= \$SERVER_MYPORT_$_rpl_master
200
# Get MASTER_LOG_FILE
201
--let $_rpl_master_log_file_index= `SELECT LOCATE('$_rpl_server:', '$rpl_master_log_file')`
202
if ($_rpl_master_log_file_index)
204
# Get text from after ':' and before ',', starting at
205
# $_rpl_master_log_file
206
--let $_rpl_master_log_file= `SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING('$rpl_master_log_file', $_rpl_master_log_file_index), ',', 1), ':', -1)`
208
if (!$_rpl_master_log_file_index)
210
--let $rpl_connection_name= server_$_rpl_master
211
--source include/rpl_connection.inc
212
--let $_rpl_master_log_file= query_get_value(SHOW MASTER STATUS, File, 1)
215
--let $rpl_connection_name= server_$_rpl_server
216
--source include/rpl_connection.inc
218
--let $_rpl_master_log_pos_index= `SELECT LOCATE('$_rpl_server:', '$rpl_master_log_pos')`
219
if ($_rpl_master_log_pos_index)
221
--let $_rpl_master_log_pos= `SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING('$rpl_master_log_pos', $_rpl_master_log_pos_index), ',', 1), ':', -1)`
222
--let $_rpl_master_log_pos= , MASTER_LOG_POS = $_rpl_master_log_pos
224
if (!$_rpl_master_log_pos_index)
226
--let $_rpl_master_log_pos=
228
eval CHANGE MASTER TO MASTER_HOST = '127.0.0.1', MASTER_PORT = $_rpl_port, MASTER_USER = 'root', MASTER_LOG_FILE = '$_rpl_master_log_file'$_rpl_master_log_pos, MASTER_CONNECT_RETRY = 1;
232
if (`SELECT '$_rpl_master' = ''`)
234
--let $rpl_connection_name= server_$_rpl_server
235
--source include/rpl_connection.inc
236
CHANGE MASTER TO MASTER_HOST = '';
244
--let $rpl_sync_chain_dirty= 1
247
--let $include_filename= rpl_change_topology.inc
248
--source include/end_include_file.inc