1# ==== Purpose ====
2#
3# Waits until SHOW SLAVE STATUS has returned a specified value, or
4# until a timeout is reached.
5#
6#
7# ==== Usage ====
8#
9# --let $slave_param= Slave_SQL_Running
10# --let $slave_param_value= No
11# [--let $slave_param_comparison= [ < | <= | >= | > | = | != ]]
12# [--let $slave_timeout= NUMBER]
13# [--let $slave_error_param= [Last_SQL_Errno | Last_IO_Errno]]
14# [--let $slave_error_param= [Slave_SQL_Errno | Slave_IO_Errno]]
15# [--let $slave_io_running_check_disable= 1]
16# [--let $rpl_channel_name= NAME]
17# [--let $rpl_debug= 1]
18# [--let $slave_io_errno= NUMBER [, NUMBER ...] [# comment]]
19# [--let $slave_sql_errno= NUMBER [, NUMBER ...] [# comment]]
20# --source include/wait_for_slave_param.inc
21#
22# Parameters:
23#
24# $slave_param, $slave_param_value
25#   This macro will wait until the column of the output of SHOW SLAVE
26#   STATUS named $slave_param gets the value $slave_param_value.  See
27#   the example above.
28#
29# $slave_param_comparison
30#   By default, this file waits until $slave_param becomes equal to
31#   $slave_param_value.  If you want to wait until $slave_param
32#   becomes *unequal* to $slave_param_value, set this parameter to the
33#   string '!=', like this:
34#     --let $slave_param_comparison= !=
35#
36# $rpl_channel_name
37#   If this is not '', adds a FOR CHANNEL  '$rpl_channel_name' clause
38#   to SHOW SLAVE STATUS.
39#
40# $slave_timeout
41#   The default timeout is 30 seconds. You can change the timeout by
42#   setting $slave_timeout. The unit is one second.
43#
44# $slave_error_param
45#   If set, this script will check for errors in the column of the
46#   output from SHOW SLAVE STATUS named $slave_error_param while
47#   waiting for the parameter. Once finding an error that is not
48#   expected (see $slave_io_errno and $slave_sql_errno parameters)
49#   this script will fail immediately. Typically, this should be set
50#   to Last_IO_Errno or Last_SQL_Errno.
51#
52# $slave_io_running_check_disable
53#   If set, this bypasses the check to see if the IO thread is started,
54#   so params can be checked and verified for a stopped IO thread as
55#   well. Otherwise check is made, and it is checked if the IO thread
56#   is connected successfully with master.
57#
58# $rpl_debug
59#   See include/rpl_init.inc
60#
61# $slave_io_errno
62#   See include/wait_for_slave_io_error.inc
63#
64# $slave_sql_errno
65#   See include/wait_for_slave_sql_error.inc
66#
67# ==== mysqltest variables configured by this file ====
68#
69# This file sets $slave_param_statement to the SQL statement used to
70# get the slave status: either SHOW SLAVE STATUS or SHOW SLAVE STATUS
71# FOR CHANNEL  '<channel>'.
72
73
74--let $_wait_for_slave_params= $slave_param
75if ($rpl_channel_name != '')
76{
77  --let $_wait_for_slave_params= $_wait_for_slave_params channel=$rpl_channel_name
78}
79--let $include_filename= wait_for_slave_param.inc [$_wait_for_slave_params]
80--source include/begin_include_file.inc
81
82--let $default_timeout= 30
83
84--let $sleep_freq= 10
85--let $sleep_time= `select 1.0 / $sleep_freq`
86
87--let $start_to_wait=`select current_timestamp()`
88
89let $_slave_timeout= $slave_timeout;
90if (!$_slave_timeout)
91{
92  let $_slave_timeout= `select $default_timeout * $sleep_freq`;
93}
94if ($VALGRIND_TEST) {
95  let $_slave_timeout= `select $_slave_timeout * 6`;
96}
97
98if ($slave_error_param)
99{
100  if ($slave_error_param != "Last_SQL_Errno")
101  {
102    if ($slave_error_param != "Last_IO_Errno")
103    {
104      --echo *** slave_error_param = $slave_error_param
105      --die slave_error_param must be null, Last_SQL_Errno or Last_IO_Errno
106    }
107  }
108}
109
110let $_slave_param_comparison= $slave_param_comparison;
111if (!$_slave_param_comparison)
112{
113  let $_slave_param_comparison= =;
114}
115
116if ($rpl_debug)
117{
118  --echo Waiting until '$slave_param' $_slave_param_comparison '$slave_param_value' [timeout='$_slave_timeout', \$slave_error_param='$slave_error_param']
119}
120
121--let $slave_param_statement= SHOW SLAVE STATUS
122if ($rpl_channel_name != '')
123{
124  --let $slave_param_statement= $slave_param_statement FOR CHANNEL  '$rpl_channel_name'
125}
126
127if (!$slave_io_running_check_disable)
128{
129  --let $_slave_check_configured= query_get_value($slave_param_statement, Slave_IO_Running, 1)
130
131  if ($_slave_check_configured == 'No such row')
132  {
133    --echo **** ERROR: $slave_param_statement returned empty result set. Slave not configured. ****
134    --source include/show_rpl_debug_info.inc
135    --die SHOW SLAVE STATUS returned empty result set. Slave not configured.
136  }
137}
138
139
140# Strip away comments on $slave_io_errno and $slave_sql_errno parameters
141--let $_slave_io_errno= `SELECT IF(LOCATE('#', '$slave_io_errno') != 0, SUBSTR('$slave_io_errno', 1, LOCATE('#', '$slave_io_errno') - 1), '$slave_io_errno')`
142--let $_slave_sql_errno= `SELECT IF(LOCATE('#', '$slave_sql_errno') != 0, SUBSTR('$slave_sql_errno', 1, LOCATE('#', '$slave_sql_errno') - 1), '$slave_sql_errno')`
143
144--let $_slave_timeout_counter= `select $_slave_timeout * $sleep_freq`
145--let $_slave_continue= 1
146while ($_slave_continue)
147{
148  --let $_show_slave_status_value= query_get_value($slave_param_statement, $slave_param, 1)
149
150  # Check if an error condition is reached.
151  if ($slave_error_param)
152  {
153    --let $_show_slave_status_error_value= query_get_value($slave_param_statement, $slave_error_param, 1)
154
155    # Check if the error condition was expected
156    if ($_show_slave_status_error_value)
157    {
158      --let $_expected_error=
159      if ($slave_error_param == "Last_IO_Errno")
160      {
161        --let $_expected_error=`SELECT FIND_IN_SET('$_show_slave_status_error_value','$_slave_io_errno')`
162      }
163      if ($slave_error_param == "Last_SQL_Errno")
164      {
165        --let $_expected_error=`SELECT FIND_IN_SET('$_show_slave_status_error_value','$_slave_sql_errno')`
166      }
167      # If the error is an expected error, just ignore it
168      if ($_expected_error)
169      {
170        --let $_show_slave_status_error_value=
171      }
172    }
173
174    if ($_show_slave_status_error_value)
175    {
176      --echo **** ERROR: $slave_error_param = '$_show_slave_status_error_value' while waiting for slave parameter $slave_param $_slave_param_comparison $slave_param_value ****
177      --source include/show_rpl_debug_info.inc
178      --die Error condition reached in include/wait_for_slave_param.inc
179    }
180  }
181
182  # Check if the termination condition is reached.
183  --let $_slave_continue= `SELECT NOT('$_show_slave_status_value' $_slave_param_comparison '$slave_param_value')`
184
185  # Decrease timer, and check if the timeout is reached.
186  if ($_slave_continue)
187  {
188    --dec $_slave_timeout_counter
189    if (!$_slave_timeout_counter)
190    {
191      --let $end_to_wait=`select current_timestamp()`
192
193      --echo **** ERROR: timeout after $_slave_timeout ($end_to_wait - $start_to_wait) seconds while waiting for slave parameter $slave_param $_slave_param_comparison $slave_param_value ****
194      --source include/show_rpl_debug_info.inc
195      --die Timeout in include/wait_for_slave_param.inc
196    }
197    --sleep  $sleep_time
198  }
199}
200
201
202--let $include_filename= wait_for_slave_param.inc [$slave_param]
203--source include/end_include_file.inc
204