forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert_binlog_events.inc
329 lines (307 loc) · 10.2 KB
/
assert_binlog_events.inc
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# ==== Purpose ====
#
# Assert that the binary log contains a specific sequence of event types.
#
# ==== Usage ====
#
# --let $event_sequence= SEQUENCE_OF_EVENTS
# [--let $event_separator= CHAR]
# [--let $invert= 1]
# [--let $limit= [OFFSET,] ROW_COUNT]
# [--let $binlog_file= FILE]
# [--let $binlog_position= POSITION]
# [--let $relay_log= 1]
# [--let $include_header_events= 1]
# [--let $wait_for_binlog_events= 1]
# [--let $slave_timeout= 1]
# [--let $rpl_debug= 1]
# [--let $keep_temp_files= 1]
# [--let $dont_print_pattern= 1]
# --source include/assert_binlog_events.inc
#
# Parameters:
#
# $event_sequence
# This is a (perl) regular expression that will be matched with a
# sequence of event specifications. Here is an example:
#
# Query/BEGIN # Query/INSERT.* # Query # (Query/COMMIT|Xid)
#
# Match a sequence of a BEGIN event, followed by an
# Query_log_event where the query begins with INSERT, followed
# by any query, followed by either a Query_log_event with the
# query equal to COMMIT, or a Xid_log_event.
#
# A single event is specified using either just the 'Type' field
# of SHOW BINLOG EVENTS, or using both the 'Type' and the 'Info'
# fields:
#
# Query
# Match any query_log_event
# Query/BEGIN
# Match any query_log_event where the query is equal to BEGIN.
# Query/INSERT.*
# Match any query_log_event where the query begins with INSERT.
#
# A sequence of events is specified by separating multiple event
# specifications with a # character:
#
# Query/BEGIN # Query/INSERT.* # Query/COMMIT
#
# Regular expressions can span multiple events:
#
# Query/BEGIN # (Query/INSERT.*){1,2} # Query/COMMIT
# Allow either one or two INSERT statements
#
# The '.' wildcard does *not* match the special characters # or /.
# So the following is safe in the sense that the .* cannot
# 'swallow' multiple events:
#
# Query/INSERT.*
#
# The pattern must match from the beginning until the end. To
# allow arbitrary events after the pattern, append '(#.*)*' to the
# pattern.
#
# The following shortcuts are available as syntactic sugar:
#
# !Q(STATEMENT) -> Query/(use .*; )?STATEMENT
# !Begin -> !Q(BEGIN)
# !Commit -> !Q(COMMIT)
# !Insert -> (!Q(INSERT.*) | Table_map # Write_rows)
# !Update -> (!Q(UPDATE.*) | Table_map # Update_rows)
# !Delete -> (!Q(DELETE.*) | Table_map # Delete_rows)
# !Single_DML -> (!Insert | !Update | !Delete)
# !Multi_DML -> multiple DML statements, each possibly touching multiple tables
# !DML_transaction -> Begin # Multi_DML # Commit
# !DDL -> !Q(neither BEGIN or COMMIT)
# !Gtid_transaction -> Gtid # (DML_transaction | DDL)
# !Empty_gtid_transaction -> Gtid # Begin # Commit
#
# Whitespace around / and # is ignored.
#
# $event_separator
# Use $event_separator instead of # to delimit events. This must
# only be one character long and should not be used elsewhere in
# the pattern.
#
# $invert
# By default, this script asserts that SHOW BINLOG EVENTS matches
# the specification. If $invert is set, this scripts instead asserts
# that SHOW BINLOG EVENTS does *not* match the specification.
#
# $limit
# Pass this as the LIMIT clause of SHOW BINLOG EVENTS.
#
# $binlog_position
# Pass this as the FROM clause of SHOW BINLOG EVENTS.
# Note that you can use include/save_binlog_position.inc to read
# both $binlog_file and $binlog_position.
#
# $binlog_file
# Pass this as the IN clause of SHOW BINLOG EVENTS.
# Note that you can use include/save_binlog_position.inc to read
# both $binlog_file and $binlog_position.
#
# $relay_log
# Use SHOW RELAYLOG EVENTS instead of SHOW BINLOG EVENTS
#
# $include_header_events
# By default, Format_desc, Rotate, and Previous_gtids events are
# ignore. If this parameter is enabled, the events are included
# in the match.
#
# $wait_for_binlog_events
# If this is true, the script retries until the binlog contains
# the expected events. The timeout is given by $slave_timeout.
#
# $slave_timeout
# Default timeout used if $wait_for_binlog_events is enabled.
# The default timeout is 300 seconds. You can change the timeout by
# setting $slave_timeout. The unit for time is seconds.
#
# $rpl_debug
# Print lots of debug info.
#
# $keep_temp_files
# Keep the two temporary files that this script uses. This is for
# debugging only and should not be used in a checked-in version of
# a test.
#
# $dont_print_pattern
# By default, the pattern is printed to the result log. If this
# variable is set, the pattern is not printed.
if ($dont_print_pattern)
{
--let $include_filename= assert_binlog_events.inc
}
if (!$dont_print_pattern)
{
--let $include_filename= assert_binlog_events.inc [$event_sequence]
}
--source include/begin_include_file.inc
if (!$event_sequence)
{
--die ERROR IN TEST: specify $event_sequence before sourcing assert_binlog_events.inc. To assert that nothing was generated, set $event_sequence= ()
}
# Execute statement, write result to file.
if (!$relay_log)
{
--let $statement= SHOW BINLOG EVENTS
}
if ($relay_log)
{
--let $statement= SHOW RELAYLOG EVENTS
}
if ($binlog_file)
{
--let $statement= $statement IN '$binlog_file'
}
if ($binlog_position)
{
--let $statement= $statement FROM $binlog_position
}
if ($limit != "")
{
--let $statement= $statement LIMIT $limit
}
if ($wait_for_binlog_events)
{
--let $_abe_counter= 0
--let $_abe_timeout= $slave_timeout
# Wait 300 seconds for binlog events
if (!$_abe_timeout)
{
--let $_abe_timeout= 300
}
}
--let $_abe_verdict=
while ($_abe_verdict != 'ok')
{
--let $output_file= GENERATE
--source include/write_result_to_file.inc
if ($rpl_debug)
{
--echo Wrote output to $output_file
}
# Set environment variables used in perl.
--let _ABE_FILE= $output_file
--let _ABE_EVENT_SEQUENCE= $event_sequence
--let _ABE_INVERT= $invert
--let _ABE_DEBUG= $rpl_debug
--let _ABE_EVENT_SEPARATOR= $event_separator
--let _ABE_INCLUDE_HEADER_EVENTS= $include_header_events
############################################################################
perl;
my $event_sequence = $ENV{'_ABE_EVENT_SEQUENCE'};
my $file = $ENV{'_ABE_FILE'};
my $invert = $ENV{'_ABE_INVERT'};
my $debug = $ENV{'_ABE_DEBUG'};
my $include_header_events = $ENV{'_ABE_INCLUDE_HEADER_EVENTS'};
my $event_separator= $ENV{'_ABE_EVENT_SEPARATOR'};
if ($event_separator == '')
{
$event_separator = '#';
}
$event_sequence =~ s/$event_separator/\n/g;
# Ignore whitespace at beginning, end, and around separators.
$event_sequence =~ s{^\s*}{};
$event_sequence =~ s{\s*$}{};
$event_sequence =~ s{\s*\n\s*}{\n}g;
$event_sequence =~ s{\s*/\s*}{/}g;
# Expand syntactic sugar definitions.
$event_sequence =~ s{!Gtid_transaction}{Gtid\n(!DDL|!DML_transaction)}g;
$event_sequence =~ s{!Empty_gtid_transaction}{Gtid\n!Begin\n!Commit}g;
$event_sequence =~ s{!DML_transaction}{!Begin\n!Multi_DML\n!Commit}g;
$event_sequence =~ s{!DDL}{Query/(?!BEGIN|COMMIT).*}g;
$event_sequence =~ s{!Single_DML}{(?:Query|Table_map\n(?:Write|Update|Delete)_rows)}g;
$event_sequence =~ s{!Multi_DML}{(?:Query|(?:Table_map\n)+(?:(?:Write|Update|Delete)_rows))(?:\nTable_map|\n(?:Write|Update|Delete)_rows|\nQuery)*}g;
$event_sequence =~ s{!Begin}{Query/BEGIN}g;
$event_sequence =~ s{!Commit}{(?:Query/COMMIT|Xid/COMMIT.*)}g;
$event_sequence =~ s{!Insert}{(?:!Q(INSERT.*)|Table_map\nWrite_rows)}g;
$event_sequence =~ s{!Update}{(?:!Q(UPDATE.*)|Table_map\nUpdate_rows)}g;
$event_sequence =~ s{!Delete}{(?:!Q(DELETE.*)|Table_map\nDelete_rows)}g;
$event_sequence =~ s{!Q\(([^\n]+)\)}{Query/(?:use.*; )?$1}g;
# Allow matching 'Type' without 'Info'
# (e.g., 'Query' instead of 'Query/xyz').
$event_sequence =~ s{\n}{(?:/[^\n]*)?\n}g;
# Allow 'Type' without 'Info' at the end. Require match until the
# end. Allow missing \n at the end.
$event_sequence .= "(?:/[^\n]*)?\n?" . '$';
# Require match from the beginning.
$event_sequence = '^' . $event_sequence;
if ($debug)
{
print "Regex: $event_sequence\n";
}
# Read and filter file.
my $result= '';
open FILE, "< $file" or die "Error $? opening $file: $!";
my $line_number= 1;
while (<FILE>)
{
if ($line_number > 1)
{
# Six tab-separated fields; pick number 3 and number 6.
s{^[^\t]+\t[^\t]+\t([^\t]+)\t[^\t]+\t[^\t]+\t([^\t]*)$}{$1/$2}
or die "Unexpected line format in output line $line_number: $_";
if ($include_header_events or
($1 ne 'Format_desc' && $1 ne 'Rotate' && $1 ne 'Previous_gtids'))
{
chomp;
$result .= $_;
$result .= "\n";
}
}
$line_number++;
}
close FILE or die "Error $? closing $file: $!";
if ($debug)
{
print "Formatted output: $result\n";
}
# Check if there is a match
my $matches = eval("\$result =~ m{$event_sequence}");
my $is_ok = ($matches == !$invert);
# Write 'ok', or write some debug info.
open FILE, "> $file.verdict" or die "Error $? opening $file.verdict: $!";
print FILE ($is_ok ? 'ok' :
"Regex:\n$event_sequence\nFile contents:\n$result")
or die "Error $? writing to $file.verdict: $!";
close FILE or die "Error $? writing to $file.verdict: $!";
EOF
############################################################################
--let $_abe_verdict= `SELECT LOAD_FILE('$output_file.verdict')`
if ($_abe_verdict != 'ok')
{
--let $_abe_fail= 1
if ($wait_for_binlog_events)
{
--sleep 1
--inc $_abe_counter
if ($_abe_counter < $_abe_timeout)
{
--let $_abe_fail= 0
}
}
if ($_abe_fail)
{
--source include/show_rpl_debug_info.inc
--echo event_sequence=$event_sequence
--echo $_abe_verdict
--echo statement=$statement
--echo invert=$invert
--echo include_header_events=$include_header_events
--echo event_separator=$_ABE_EVENT_SEPARATOR
--die Binlog contents did not match expected pattern.
}
}
if (!$keep_temp_files)
{
--remove_file $output_file
--remove_file $output_file.verdict
}
--let $output_file=
}
--let $include_filename= assert_binlog_events.inc
--source include/end_include_file.inc