Debian Codegraph @

libbinlogevents/src/trx_boundary_parser.cpp

raw
1 /* Copyright (c) 2015, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include "libbinlogevents/include/trx_boundary_parser.h"
25
26 #include <string.h>
27
28 #include "libbinlogevents/include/binlog_event.h"
29 #include "m_string.h"
30 #include "my_byteorder.h"
31
32 #if !defined(NDEBUG) && defined(HAVE_MYSYS)
33 /* Event parser state names */
34 static const char *event_parser_state_names[] = {"None", "GTID", "DDL", "DML",
35 "Error"};
36 #endif
37
38 /*
39 -----------------------------------------
40 Transaction_boundary_parser class methods
41 -----------------------------------------
42 */
43
44 Transaction_boundary_parser::~Transaction_boundary_parser() = default;
45
46 /**
47 Reset the transaction boundary parser.
48
49 This method initialize the boundary parser state.
50 */
51 void Transaction_boundary_parser::reset() {
52 BAPI_PRINT("info", ("transaction boundary parser is changing state "
53 "from '%s' to '%s'",
54 event_parser_state_names[current_parser_state],
55 event_parser_state_names[EVENT_PARSER_NONE]));
56 current_parser_state = EVENT_PARSER_NONE;
57 last_parser_state = EVENT_PARSER_NONE;
58 m_current_boundary_state = EVENT_BOUNDARY_TYPE_ERROR;
59 }
60
61 bool Transaction_boundary_parser::feed_event(
62 binary_log::Log_event_basic_info log_event_info, bool throw_warnings) {
63 BAPI_TRACE;
64
65 m_current_boundary_state =
66 get_event_boundary_type(log_event_info, throw_warnings);
67 return update_state(m_current_boundary_state, throw_warnings);
68 }
69
70 bool Transaction_boundary_parser::check_row_logging_constraints(
71 binary_log::Log_event_basic_info log_event_info) {
72 BAPI_TRACE;
73
74 // If the boundary parser is in error state return true
75 if (EVENT_BOUNDARY_TYPE_ERROR == m_current_boundary_state) return true;
76
77 // INTVAR_EVENT, RAND_EVENT, USER_VAR_EVENT
78 if (EVENT_BOUNDARY_TYPE_PRE_STATEMENT == m_current_boundary_state) {
79 // An USER_VAR_EVENT can appear with RBR when associated to DDL events
80 return log_event_info.event_type != binary_log::USER_VAR_EVENT ||
81 current_parser_state != EVENT_PARSER_DDL;
82 }
83
84 // DDL or TRANSACTION_PAYLOAD_EVENT
85 if (EVENT_BOUNDARY_TYPE_STATEMENT == m_current_boundary_state ||
86 current_parser_state == EVENT_PARSER_NONE) {
87 if (log_event_info.query_length > 16 &&
88 (!native_strncasecmp(log_event_info.query,
89 STRING_WITH_LEN("CREATE TEMPORARY")) ||
90 !native_strncasecmp(log_event_info.query,
91 STRING_WITH_LEN("DROP TEMPORARY")))) {
92 return true;
93 }
94 }
95
96 // Defend against injected LOAD events
97 if (log_event_info.event_type == binary_log::BEGIN_LOAD_QUERY_EVENT ||
98 log_event_info.event_type == binary_log::EXECUTE_LOAD_QUERY_EVENT ||
99 log_event_info.event_type == binary_log::APPEND_BLOCK_EVENT ||
100 log_event_info.event_type == binary_log::DELETE_FILE_EVENT)
101 return true;
102
103 // DML using non row events
104 if (EVENT_BOUNDARY_TYPE_STATEMENT == m_current_boundary_state &&
105 current_parser_state == EVENT_PARSER_DML)
106 if (log_event_info.event_type != binary_log::TABLE_MAP_EVENT &&
107 log_event_info.event_type != binary_log::WRITE_ROWS_EVENT &&
108 log_event_info.event_type != binary_log::UPDATE_ROWS_EVENT &&
109 log_event_info.event_type != binary_log::DELETE_ROWS_EVENT &&
110 log_event_info.event_type != binary_log::WRITE_ROWS_EVENT_V1 &&
111 log_event_info.event_type != binary_log::UPDATE_ROWS_EVENT_V1 &&
112 log_event_info.event_type != binary_log::DELETE_ROWS_EVENT_V1 &&
113 log_event_info.event_type != binary_log::PARTIAL_UPDATE_ROWS_EVENT &&
114 log_event_info.event_type != binary_log::ROWS_QUERY_LOG_EVENT &&
115 log_event_info.event_type != binary_log::VIEW_CHANGE_EVENT) {
116 // We allow XA control statements
117 if (log_event_info.event_type == binary_log::QUERY_EVENT) {
118 if (!native_strncasecmp(log_event_info.query,
119 STRING_WITH_LEN("XA END")) ||
120 !native_strncasecmp(log_event_info.query,
121 STRING_WITH_LEN("XA PREPARE")) ||
122 !native_strncasecmp(log_event_info.query,
123 STRING_WITH_LEN("SAVEPOINT")) ||
124 !native_strncasecmp(log_event_info.query,
125 STRING_WITH_LEN("CREATE TABLE")))
126 return false;
127 }
128 return true;
129 }
130
131 return false;
132 }
133
134 Transaction_boundary_parser::enum_event_boundary_type
135 Transaction_boundary_parser::get_event_boundary_type(
136 binary_log::Log_event_basic_info event_info, bool throw_warnings) {
137 BAPI_TRACE;
138
139 enum_event_boundary_type boundary_type;
140
141 switch (event_info.event_type) {
142 case binary_log::GTID_LOG_EVENT:
143 case binary_log::ANONYMOUS_GTID_LOG_EVENT:
144 boundary_type = EVENT_BOUNDARY_TYPE_GTID;
145 break;
146
147 /*
148 There are four types of queries that we have to deal with: BEGIN, COMMIT,
149 ROLLBACK and the rest.
150 */
151 case binary_log::QUERY_EVENT: {
152 if (event_info.query_length == 0) {
153 /* purecov: begin inspected */
154 BAPI_ASSERT(event_info.query == nullptr);
155 boundary_type = EVENT_BOUNDARY_TYPE_ERROR;
156 break;
157 /* purecov: end */
158 }
159
160 /*
161 BEGIN is always the begin of a DML transaction.
162 */
163 if (!strncmp(event_info.query, "BEGIN", event_info.query_length) ||
164 !strncmp(event_info.query, STRING_WITH_LEN("XA START")))
165 boundary_type = EVENT_BOUNDARY_TYPE_BEGIN_TRX;
166 /*
167 COMMIT and ROLLBACK are always the end of a transaction.
168 */
169 else if (!strncmp(event_info.query, "COMMIT", event_info.query_length) ||
170 (!native_strncasecmp(event_info.query,
171 STRING_WITH_LEN("ROLLBACK")) &&
172 native_strncasecmp(event_info.query,
173 STRING_WITH_LEN("ROLLBACK TO "))))
174 boundary_type = EVENT_BOUNDARY_TYPE_END_TRX;
175 /*
176 XA ROLLBACK is always the end of a XA transaction.
177 */
178 else if (!native_strncasecmp(event_info.query,
179 STRING_WITH_LEN("XA ROLLBACK")))
180 boundary_type = EVENT_BOUNDARY_TYPE_END_XA_TRX;
181 /*
182 If the query is not (BEGIN | XA START | COMMIT | [XA] ROLLBACK), it can
183 be considered an ordinary statement.
184 */
185 else
186 boundary_type = EVENT_BOUNDARY_TYPE_STATEMENT;
187
188 break;
189 }
190
191 /*
192 XID events are always the end of a transaction.
193 */
194 case binary_log::XID_EVENT:
195 boundary_type = EVENT_BOUNDARY_TYPE_END_TRX;
196 break;
197 /*
198 XA_prepare event ends XA-prepared group of events (prepared XA
199 transaction).
200 */
201 case binary_log::XA_PREPARE_LOG_EVENT:
202 boundary_type = EVENT_BOUNDARY_TYPE_END_TRX;
203 break;
204
205 /*
206 Intvar, Rand and User_var events are always considered as pre-statements.
207 */
208 case binary_log::INTVAR_EVENT:
209 case binary_log::RAND_EVENT:
210 case binary_log::USER_VAR_EVENT:
211 boundary_type = EVENT_BOUNDARY_TYPE_PRE_STATEMENT;
212 break;
213
214 /*
215 The following event types are always considered as statements
216 because they will always be wrapped between BEGIN/COMMIT.
217 */
218 case binary_log::EXECUTE_LOAD_QUERY_EVENT:
219 case binary_log::TABLE_MAP_EVENT:
220 case binary_log::APPEND_BLOCK_EVENT:
221 case binary_log::BEGIN_LOAD_QUERY_EVENT:
222 case binary_log::ROWS_QUERY_LOG_EVENT:
223 case binary_log::WRITE_ROWS_EVENT:
224 case binary_log::UPDATE_ROWS_EVENT:
225 case binary_log::DELETE_ROWS_EVENT:
226 case binary_log::WRITE_ROWS_EVENT_V1:
227 case binary_log::UPDATE_ROWS_EVENT_V1:
228 case binary_log::DELETE_ROWS_EVENT_V1:
229 case binary_log::VIEW_CHANGE_EVENT:
230 case binary_log::PARTIAL_UPDATE_ROWS_EVENT:
231 boundary_type = EVENT_BOUNDARY_TYPE_STATEMENT;
232 break;
233
234 /*
235 Incident events have their own boundary type.
236 */
237 case binary_log::INCIDENT_EVENT:
238 boundary_type = EVENT_BOUNDARY_TYPE_INCIDENT;
239 break;
240
241 /*
242 Rotate, Format_description and Heartbeat should be ignored.
243 Also, any other kind of event not listed in the "cases" above
244 will be ignored.
245 */
246 case binary_log::ROTATE_EVENT:
247 case binary_log::FORMAT_DESCRIPTION_EVENT:
248 case binary_log::HEARTBEAT_LOG_EVENT:
249 case binary_log::HEARTBEAT_LOG_EVENT_V2:
250 case binary_log::PREVIOUS_GTIDS_LOG_EVENT:
251 case binary_log::STOP_EVENT:
252 case binary_log::SLAVE_EVENT:
253 case binary_log::DELETE_FILE_EVENT:
254 case binary_log::TRANSACTION_CONTEXT_EVENT:
255 boundary_type = EVENT_BOUNDARY_TYPE_IGNORE;
256 break;
257
258 case binary_log::TRANSACTION_PAYLOAD_EVENT:
259 boundary_type = EVENT_BOUNDARY_TYPE_TRANSACTION_PAYLOAD;
260 break;
261
262 /*
263 If the event is none of above supported event types, this is probably
264 an event type unsupported by this server version. So, we must check if
265 this event is ignorable or not.
266 */
267 default:
268 if (event_info.ignorable_event)
269 boundary_type = EVENT_BOUNDARY_TYPE_IGNORE;
270 else {
271 boundary_type = EVENT_BOUNDARY_TYPE_ERROR;
272 if (throw_warnings)
273 log_server_warning(
274 ER_TRX_BOUND_UNSUPPORTED_UNIGNORABLE_EVENT_IN_STREAM, nullptr);
275 }
276 } /* End of switch(event_type) */
277
278 return boundary_type;
279 }
280
281 /**
282 Update the boundary parser state based on a given boundary type.
283
284 @param event_boundary_type The event boundary type of the event used to
285 fed the boundary parser.
286 @param throw_warnings If the function should throw warnings while
287 updating the boundary parser state.
288 Please see comments on this at feed_event().
289
290 @return false State updated successfully.
291 true There was an error updating the state.
292 */
293 bool Transaction_boundary_parser::update_state(
294 enum_event_boundary_type event_boundary_type, bool throw_warnings) {
295 BAPI_TRACE;
296
297 enum_event_parser_state new_parser_state = EVENT_PARSER_NONE;
298
299 bool error = false;
300
301 switch (event_boundary_type) {
302 /*
303 GTIDs are always the start of a transaction stream.
304 */
305 case EVENT_BOUNDARY_TYPE_GTID:
306 /* In any case, we will update the state to GTID */
307 new_parser_state = EVENT_PARSER_GTID;
308 /* The following switch is mostly to differentiate the warning messages */
309 switch (current_parser_state) {
310 case EVENT_PARSER_GTID:
311 case EVENT_PARSER_DDL:
312 case EVENT_PARSER_DML:
313 /*
314 We don't consider an unexpected GTID appearing in the applier as
315 being an error. In this context it can mean several things:
316 - A transaction was logged partially in an old relay log
317 - A transaction is retrying after an error
318 */
319 if (m_trx_boundary_parser_context != TRX_BOUNDARY_PARSER_APPLIER) {
320 if (throw_warnings) {
321 if (current_parser_state == EVENT_PARSER_GTID)
322 log_server_warning(
323 ER_TRX_BOUND_GTID_LOG_EVENT_IN_STREAM,
324 "after a GTID_LOG_EVENT or an ANONYMOUS_GTID_LOG_EVENT");
325 else {
326 if (current_parser_state == EVENT_PARSER_DDL)
327 log_server_warning(ER_TRX_BOUND_GTID_LOG_EVENT_IN_STREAM,
328 "in the middle of a DDL");
329 else
330 log_server_warning(ER_TRX_BOUND_GTID_LOG_EVENT_IN_STREAM,
331 "in the middle of a DML");
332 }
333 }
334 error = true;
335 }
336 break;
337 case EVENT_PARSER_ERROR: /* we probably threw a warning before */
338 error = true;
339 /* FALL THROUGH */
340 case EVENT_PARSER_NONE:
341 break;
342 }
343 break;
344
345 /*
346 There are four types of queries that we have to deal with: BEGIN, COMMIT,
347 ROLLBACK and the rest.
348 */
349 case EVENT_BOUNDARY_TYPE_BEGIN_TRX:
350 /* In any case, we will update the state to DML */
351 new_parser_state = EVENT_PARSER_DML;
352 /* The following switch is mostly to differentiate the warning messages */
353 switch (current_parser_state) {
354 case EVENT_PARSER_DDL:
355 case EVENT_PARSER_DML:
356 if (throw_warnings) {
357 if (current_parser_state == EVENT_PARSER_DDL)
358 log_server_warning(ER_TRX_BOUND_UNEXPECTED_BEGIN_IN_STREAM,
359 "DDL");
360 else
361 log_server_warning(ER_TRX_BOUND_UNEXPECTED_BEGIN_IN_STREAM,
362 "DML");
363 }
364 error = true;
365 break;
366 case EVENT_PARSER_ERROR: /* we probably threw a warning before */
367 error = true;
368 /* FALL THROUGH */
369 case EVENT_PARSER_NONE:
370 case EVENT_PARSER_GTID:
371 break;
372 }
373 break;
374
375 case EVENT_BOUNDARY_TYPE_END_TRX:
376 /* In any case, we will update the state to NONE */
377 new_parser_state = EVENT_PARSER_NONE;
378 /* The following switch is mostly to differentiate the warning messages */
379 switch (current_parser_state) {
380 case EVENT_PARSER_NONE:
381 case EVENT_PARSER_GTID:
382 case EVENT_PARSER_DDL:
383 if (throw_warnings) {
384 if (current_parser_state == EVENT_PARSER_NONE)
385 log_server_warning(
386 ER_TRX_BOUND_UNEXPECTED_COMMIT_ROLLBACK_OR_XID_LOG_EVENT_IN_STREAM,
387 "outside a transaction");
388 else {
389 if (current_parser_state == EVENT_PARSER_GTID)
390 log_server_warning(
391 ER_TRX_BOUND_UNEXPECTED_COMMIT_ROLLBACK_OR_XID_LOG_EVENT_IN_STREAM,
392 "after a GTID_LOG_EVENT");
393 else
394 log_server_warning(
395 ER_TRX_BOUND_UNEXPECTED_COMMIT_ROLLBACK_OR_XID_LOG_EVENT_IN_STREAM,
396 "in the middle of a DDL");
397 }
398 } /* EVENT_PARSER_DDL */
399 error = true;
400 break;
401 case EVENT_PARSER_ERROR: /* we probably threw a warning before */
402 error = true;
403 /* FALL THROUGH */
404 case EVENT_PARSER_DML:
405 break;
406 }
407 break;
408
409 case EVENT_BOUNDARY_TYPE_END_XA_TRX:
410 /* In any case, we will update the state to NONE */
411 new_parser_state = EVENT_PARSER_NONE;
412 /* The following switch is mostly to differentiate the warning messages */
413 switch (current_parser_state) {
414 case EVENT_PARSER_NONE:
415 case EVENT_PARSER_DDL:
416 if (throw_warnings) {
417 if (current_parser_state == EVENT_PARSER_NONE) {
418 log_server_warning(ER_TRX_BOUND_UNEXPECTED_XA_ROLLBACK_IN_STREAM,
419 "outside a transaction");
420 } else {
421 log_server_warning(ER_TRX_BOUND_UNEXPECTED_XA_ROLLBACK_IN_STREAM,
422 "in the middle of a DDL");
423 }
424 } /* EVENT_PARSER_DDL */
425 error = true;
426 break;
427 case EVENT_PARSER_ERROR: /* we probably threw a warning before */
428 error = true;
429 /* FALL THROUGH */
430 case EVENT_PARSER_DML:
431 /* XA ROLLBACK can appear after a GTID event */
432 case EVENT_PARSER_GTID:
433 break;
434 }
435 break;
436
437 case EVENT_BOUNDARY_TYPE_STATEMENT:
438 switch (current_parser_state) {
439 case EVENT_PARSER_NONE:
440 new_parser_state = EVENT_PARSER_NONE;
441 break;
442 case EVENT_PARSER_GTID:
443 case EVENT_PARSER_DDL:
444 new_parser_state = EVENT_PARSER_NONE;
445 break;
446 case EVENT_PARSER_DML:
447 new_parser_state = current_parser_state;
448 break;
449 case EVENT_PARSER_ERROR: /* we probably threw a warning before */
450 error = true;
451 break;
452 }
453 break;
454
455 case EVENT_BOUNDARY_TYPE_TRANSACTION_PAYLOAD:
456 switch (current_parser_state) {
457 case EVENT_PARSER_GTID:
458 new_parser_state = EVENT_PARSER_NONE;
459 break;
460 case EVENT_PARSER_NONE:
461 case EVENT_PARSER_DML:
462 case EVENT_PARSER_DDL:
463 case EVENT_PARSER_ERROR: /* we probably threw a warning before */
464 error = true;
465 break;
466 }
467 break;
468
469 /*
470 Intvar, Rand and User_var events might be inside of a transaction stream
471 if any Intvar, Rand and User_var was fed before, if BEGIN was fed before
472 or if GTID was fed before. In the case of no GTID, no BEGIN and no
473 previous Intvar, Rand or User_var it will be considered the start of a
474 transaction stream.
475 */
476 case EVENT_BOUNDARY_TYPE_PRE_STATEMENT:
477 switch (current_parser_state) {
478 case EVENT_PARSER_NONE:
479 case EVENT_PARSER_GTID:
480 new_parser_state = EVENT_PARSER_DDL;
481 break;
482 case EVENT_PARSER_DDL:
483 case EVENT_PARSER_DML:
484 new_parser_state = current_parser_state;
485 break;
486 case EVENT_PARSER_ERROR: /* we probably threw a warning before */
487 error = true;
488 break;
489 }
490 break;
491
492 /*
493 Incident events can happen without a GTID (before BUG#19594845 fix) or
494 with its own GTID in order to be skipped. In any case, it should always
495 mark "the end" of a transaction.
496 */
497 case EVENT_BOUNDARY_TYPE_INCIDENT:
498 /* In any case, we will update the state to NONE */
499 new_parser_state = EVENT_PARSER_NONE;
500 break;
501
502 /*
503 Rotate, Format_description and Heartbeat should be ignored.
504 The rotate might be fake, like when the IO thread receives from dump
505 thread Previous_gtid and Heartbeat events due to reconnection/auto
506 positioning.
507 */
508 case EVENT_BOUNDARY_TYPE_IGNORE:
509 new_parser_state = current_parser_state;
510 break;
511
512 case EVENT_BOUNDARY_TYPE_ERROR:
513 error = true;
514 new_parser_state = EVENT_PARSER_ERROR;
515 break;
516 }
517
518 BAPI_PRINT("info", ("transaction boundary parser is changing state "
519 "from '%s' to '%s'",
520 event_parser_state_names[current_parser_state],
521 event_parser_state_names[new_parser_state]));
522
523 last_parser_state = current_parser_state;
524 current_parser_state = new_parser_state;
525
526 return error;
527 }
528
529 void Transaction_boundary_parser::log_server_warning(int, const char *) {}