MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rpl_sync.inc
1 # ==== Purpose ====
2 #
3 # Sync all servers in an arbitrary replication topology. This works
4 # only if the servers have been configured with rpl_init.inc (and
5 # possibly rpl_change_topology.inc).
6 #
7 #
8 # ==== Usage ====
9 #
10 # [--let $rpl_only_running_threads= 1]
11 # [--let $use_gtids= 1]
12 # [--let $rpl_debug= 1]
13 # [--let $slave_timeout= NUMBER]
14 # --source include/rpl_sync.inc
15 #
16 # Parameters:
17 # $rpl_only_running_threads
18 # By default, this script assumes that both the IO thread and the
19 # SQL thread are running and fails if one of them is stopped. If
20 # $rpl_only_running_threads is set, this script first checks
21 # which slave threads are running:
22 # - If both threads are running, sync both threads with master.
23 # - If only IO thread is running, sync IO thread with master.
24 # - If only SQL thread is running, sync SQL thread with IO thread.
25 # - If no thread is running, don't sync.
26 #
27 # $use_gtids
28 # If set, uses GTIDs instead of filename and offset for positions.
29 #
30 # $slave_timeout
31 # Set the timeout when waiting for threads to sync. See
32 # include/wait_for_slave_param.inc
33 #
34 # $rpl_debug
35 # See include/rpl_init.inc
36 #
37 #
38 # ==== Side effects ====
39 #
40 # Does not change the current connection (note that this is different
41 # from mysqltest's built-in sync_slave_with_master command).
42 
43 
44 --let $include_filename= rpl_sync.inc
45 --source include/begin_include_file.inc
46 
47 
48 # Compute $rpl_sync_chain if needed. We could have done this in
49 # rpl_change_topology.inc, but instead we do it here because that
50 # means we only compute $rpl_sync_chain when it is needed.
51 if ($rpl_sync_chain_dirty)
52 {
53  --source include/rpl_generate_sync_chain.inc
54  --let $rpl_sync_chain_dirty= 0
55 }
56 
57 
58 if ($rpl_debug)
59 {
60  --echo \$rpl_sync_chain = '$rpl_sync_chain' \$rpl_only_running_threads= $rpl_only_running_threads
61 }
62 
63 if (!$rpl_server_count_length)
64 {
65  --die \$rpl_server_count_length is not set. Did you call rpl_init.inc?
66 }
67 
68 
69 --let $_rpl_i= 1
70 --let $_rpl_connect= 0
71 while ($_rpl_i) {
72  # $rpl_sync_chain consists of a sequence of sync chains. Each sync
73  # chain has the form:
74  #
75  # <space><server1_1><server1_2>...<server1_N>
76  #
77  # So the space character indicates that a new sync chain starts.
78  --let $_rpl_server= `SELECT TRIM(SUBSTR('$rpl_sync_chain', 1 + ($_rpl_i - 1) * $rpl_server_count_length, $rpl_server_count_length))`
79 
80  if ($_rpl_server)
81  {
82  if ($rpl_debug)
83  {
84  --echo [sync server_$_rpl_prev_server -> server_$_rpl_server]
85  }
86  if ($rpl_only_running_threads)
87  {
88  --connection server_$_rpl_server
89  --let $_rpl_slave_io_running= query_get_value(SHOW SLAVE STATUS, Slave_IO_Running, 1)
90  --let $_rpl_slave_sql_running= query_get_value(SHOW SLAVE STATUS, Slave_SQL_Running, 1)
91  if ($rpl_debug)
92  {
93  --echo Sync IO: $_rpl_slave_io_running; Sync SQL: $_rpl_slave_sql_running
94  }
95  if ($_rpl_slave_io_running != No)
96  {
97  --connection server_$_rpl_prev_server
98  --let $sync_slave_connection= server_$_rpl_server
99  if ($_rpl_slave_sql_running != No)
100  {
101  --source include/sync_slave_sql_with_master.inc
102  }
103  if ($_rpl_slave_sql_running == No)
104  {
105  --source include/sync_slave_io_with_master.inc
106  }
107  }
108  if ($_rpl_slave_io_running == No)
109  {
110  if ($_rpl_slave_sql_running != No)
111  {
112  --source include/sync_slave_sql_with_io.inc
113  }
114  }
115  }
116  if (!$rpl_only_running_threads)
117  {
118  --connection server_$_rpl_prev_server
119  --let $saved_value= $sync_slave_connection
120  --let $sync_slave_connection= server_$_rpl_server
121  --source include/sync_slave_sql_with_master.inc
122  --let $sync_slave_connection= $saved_value
123  }
124  }
125 
126  # This happens at the beginning of a new sync subchain and at the
127  # end of the full sync chain.
128  if (!$_rpl_server)
129  {
130  --inc $_rpl_i
131  --let $_rpl_server= `SELECT TRIM(SUBSTR('$rpl_sync_chain', 1 + ($_rpl_i - 1) * $rpl_server_count_length, $rpl_server_count_length))`
132 
133  if (!$_rpl_server)
134  {
135  # terminate loop
136  --let $_rpl_i= -1
137  }
138  }
139 
140  --let $_rpl_prev_server= $_rpl_server
141  --inc $_rpl_i
142 }
143 
144 
145 --let $include_filename= rpl_sync.inc
146 --source include/end_include_file.inc