MySQL 8.3.0
Source Code Documentation
binlog_event.h
Go to the documentation of this file.
1/* Copyright (c) 2011, 2023, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23/// @defgroup GroupLibsMysql MySQL Libraries
24
25/// @defgroup GroupLibsMysqlBinlog MySQL Libraries : Binlog Libraries
26/// @ingroup GroupLibsMysql
27
28/// @defgroup GroupLibsMysqlBinlogEvent MySQL Libraries : Binlog Event
29/// @ingroup GroupLibsMysqlBinlog
30
31/**
32 @file binlog_event.h
33
34 @brief Contains the classes representing events occurring in the replication
35 stream. Each event is represented as a byte sequence with logical divisions
36 as event header, event specific data and event footer. The header and footer
37 are common to all the events and are represented as two different subclasses.
38*/
39
40#ifndef MYSQL_BINLOG_EVENT_BINLOG_EVENT_H
41#define MYSQL_BINLOG_EVENT_BINLOG_EVENT_H
42
43#include <stdlib.h>
44#include <string.h>
45#include <sys/types.h>
46#include <zlib.h> //for checksum calculations
47#include <climits>
48#include <cstdio>
49#include <iostream>
50
51#include "my_checksum.h"
52#include "my_io.h"
56
57#if defined(_WIN32)
58#include <Winsock2.h>
59#else
60#include <sys/times.h>
61#endif
62
63/// @addtogroup GroupLibsMysqlBinlogEvent
64/// @{
65
66/*
67 The symbols below are a part of the common definitions between
68 the MySQL server and the client. Since they should not be a part of
69 this library but the server, these should be placed in a header file
70 common to the library and the MySQL server code, so that if they are
71 updated in the server code, it is reflected in the libbinlogevent also.
72
73 TODO(WL#7984): Moving the binlog constant in library libbinlogevents into a
74 separate file and make them const variables
75*/
76#ifndef SYSTEM_CHARSET_MBMAXLEN
77#define SYSTEM_CHARSET_MBMAXLEN 3
78#endif
79#ifndef NAME_CHAR_LEN
80#define NAME_CHAR_LEN 64 /* Field/table name length */
81#endif
82#ifndef NAME_LEN
83#define NAME_LEN (NAME_CHAR_LEN * SYSTEM_CHARSET_MBMAXLEN)
84#endif
85/* Length of the server_version_split array in FDE class */
86#ifndef ST_SERVER_VER_SPLIT_LEN
87#define ST_SERVER_VER_SPLIT_LEN 3
88#endif
89
90/*
91 Do not decrease the value of BIN_LOG_HEADER_SIZE.
92 Do not even increase it before checking code.
93*/
94#ifndef BIN_LOG_HEADER_SIZE
95#define BIN_LOG_HEADER_SIZE 4U
96#endif
97
98/**
99 binlog_version 3 is MySQL 4.x; 4 is MySQL 5.0.0.
100 Compared to version 3, version 4 has:
101 - a different Start_event, which includes info about the binary log
102 (sizes of headers); this info is included for better compatibility if the
103 master's MySQL version is different from the slave's.
104*/
105#define BINLOG_VERSION 4
106
107/*
108 Constants used by Query_event.
109*/
110
111/**
112 The maximum number of updated databases that a status of
113 Query-log-event can carry. It can be redefined within a range
114 [1.. OVER_MAX_DBS_IN_EVENT_MTS].
115*/
116#define MAX_DBS_IN_EVENT_MTS 16
117
118/**
119 When the actual number of databases exceeds MAX_DBS_IN_EVENT_MTS
120 the value of OVER_MAX_DBS_IN_EVENT_MTS is is put into the
121 mts_accessed_dbs status.
122*/
123#define OVER_MAX_DBS_IN_EVENT_MTS 254
124
125/**
126 Maximum length of time zone name that we support (Time zone name is
127 char(64) in db). mysqlbinlog needs it.
128*/
129#define MAX_TIME_ZONE_NAME_LENGTH (NAME_LEN + 1)
130
131/**
132 Max number of possible extra bytes in a replication event compared to a
133 packet (i.e. a query) sent from client to master;
134 First, an auxiliary log_event status vars estimation:
135*/
136#define MAX_SIZE_LOG_EVENT_STATUS \
137 (1U + 4 /* type, flags2 */ + 1U + 8 /* type, sql_mode */ + 1U + 1 + \
138 255 /* type, length, catalog */ + 1U + 4 /* type, auto_increment */ + 1U + \
139 6 /* type, charset */ + 1U + 1 + \
140 MAX_TIME_ZONE_NAME_LENGTH /* type, length, time_zone */ + 1U + \
141 2 /* type, lc_time_names_number */ + 1U + \
142 2 /* type, charset_database_number */ + 1U + \
143 8 /* type, table_map_for_update */ + 1U + 1 + \
144 32 * 3 /* type, user_len, user */ + 1 + 255 /* host_len, host */ \
145 + 1U + 1 + \
146 (MAX_DBS_IN_EVENT_MTS * (1 + NAME_LEN)) /* type, db_1, db_2, ... */ \
147 + 1U + 3 /* type, microseconds */ + 1U + 1 /* type, explicit_def..ts*/ + \
148 1U + 8 /* type, xid of DDL */ + 1U + \
149 2 /* type, default_collation_for_utf8mb4_number */ + 1U + \
150 1 /* sql_require_primary_key */ + 1U + \
151 1 /* type, default_table_encryption */)
152
153/**
154 Uninitialized timestamp value (for either last committed or sequence number).
155 Often carries meaning of the minimum value in the logical timestamp domain.
156*/
157const int64_t SEQ_UNINIT = 0;
158
159/** We use 7 bytes, 1 bit being used as a flag. */
160#define MAX_COMMIT_TIMESTAMP_VALUE (1ULL << 55)
161/**
162 Used to determine whether the original_commit_timestamp is already known or if
163 it still needs to be determined when computing it.
164*/
166
167const uint32_t UNDEFINED_SERVER_VERSION = 999999;
168const uint32_t UNKNOWN_SERVER_VERSION = 0;
169
170/** Setting this flag will mark an event as Ignorable */
171#define LOG_EVENT_IGNORABLE_F 0x80
172
173/**
174 In case the variable is updated,
175 make sure to update it in $MYSQL_SOURCE_DIR/my_io.h.
176*/
177#ifndef FN_REFLEN
178#define FN_REFLEN 512 /* Max length of full path-name */
179#endif
180
181/**
182 Splits server 'version' string into three numeric pieces stored
183 into 'split_versions':
184 X.Y.Zabc (X,Y,Z numbers, a not a digit) -> {X,Y,Z}
185 X.Yabc -> {X,Y,0}
186
187 @param version String representing server version
188 @param split_versions Array with each element containing one split of the
189 input version string
190*/
191inline void do_server_version_split(const char *version,
192 unsigned char split_versions[3]) {
193 const char *p = version;
194 char *r;
195 unsigned long number;
196 for (unsigned int i = 0; i <= 2; i++) {
197 number = strtoul(p, &r, 10);
198 /*
199 It is an invalid version if any version number greater than 255 or
200 first number is not followed by '.'.
201 */
202 if (number < 256 && (*r == '.' || i != 0))
203 split_versions[i] = static_cast<unsigned char>(number);
204 else {
205 split_versions[0] = 0;
206 split_versions[1] = 0;
207 split_versions[2] = 0;
208 break;
209 }
210
211 p = r;
212 if (*r == '.') p++; // skip the dot
213 }
214}
215
216/**
217 Transforms the server version from 'XX.YY.ZZ-suffix' into an integer in the
218 format XXYYZZ.
219
220 @param version String representing server version
221 @return The server version in the format XXYYZZ
222*/
223inline uint32_t do_server_version_int(const char *version) {
224 unsigned char version_split[3];
225 do_server_version_split(version, version_split);
226 const uint32_t ret = static_cast<uint32_t>(version_split[0]) * 10000 +
227 static_cast<uint32_t>(version_split[1]) * 100 +
228 static_cast<uint32_t>(version_split[2]);
229 return ret;
230}
231
232/**
233 Calculate the version product from the numeric pieces representing the server
234 version:
235 For a server version X.Y.Zabc (X,Y,Z numbers, a not a digit), the input is
236 {X,Y,Z}. This is converted to XYZ in bit representation.
237
238 @param version_split Array containing the version information of the server
239 @return The version product of the server
240*/
241inline unsigned long version_product(const unsigned char *version_split) {
242 return ((version_split[0] * 256 + version_split[1]) * 256 + version_split[2]);
243}
244
245/**
246 Replication event checksum is introduced in the following "checksum-home"
247 version. The checksum-aware servers extract FD's version to decide whether
248 the FD event carries checksum info.
249*/
250extern const unsigned char checksum_version_split[3];
251extern const unsigned long checksum_version_product;
252/**
253 @namespace mysql::binlog::event
254
255 The namespace contains classes representing events that can occur in a
256 replication stream.
257*/
258namespace mysql::binlog::event {
259/*
260 This flag only makes sense for Format_description_event. It is set
261 when the event is written, and *reset* when a binlog file is
262 closed (yes, it's the only case when MySQL modifies an already written
263 part of the binlog). Thus it is a reliable indicator that the binlog was
264 closed correctly. (Stop_event is not enough, there's always a
265 small chance that mysqld crashes in the middle of insert and end of
266 the binlog would look like a Stop_event).
267
268 This flag is used to detect a restart after a crash, and to provide
269 "unbreakable" binlog. The problem is that on a crash storage engines
270 rollback automatically, while binlog does not. To solve this we use this
271 flag and automatically append ROLLBACK to every non-closed binlog (append
272 virtually, on reading, file itself is not changed). If this flag is found,
273 mysqlbinlog simply prints "ROLLBACK". Replication master does not abort on
274 binlog corruption, but takes it as EOF, and replication slave forces a
275 rollback in this case.
276
277 Note, that old binlogs does not have this flag set, so we get a
278 a backward-compatible behaviour.
279*/
280#define LOG_EVENT_BINLOG_IN_USE_F 0x1
281
282/**
283 Enumeration type for the different types of log events.
284*/
286 /**
287 Every time you add a type, you have to
288 - Assign it a number explicitly. Otherwise it will cause trouble
289 if a event type before is deprecated and removed directly from
290 the enum.
291 - Fix Format_description_event::Format_description_event().
292 */
294 /*
295 Deprecated since mysql 8.0.2. It is just a placeholder,
296 should not be used anywhere else.
297 */
303
305
308
315
317
318 /**
319 The V1 event numbers are used from 5.1.16 until mysql-5.6.
320 */
324
325 /**
326 Something out of the ordinary happened on the master
327 */
329
330 /**
331 Heartbeat event to be send by master at its idle time
332 to ensure master's online status to slave
333 */
335
336 /**
337 In some situations, it is necessary to send over ignorable
338 data to the slave: data that a slave can handle in case there
339 is code for handling it, but which can be ignored if it is not
340 recognized.
341 */
344
345 /** Version 2 of the Row events */
349
352
354
356
358
359 /* Prepared XA transaction terminal event similar to Xid */
361
362 /**
363 Extension of UPDATE_ROWS_EVENT, allowing partial values according
364 to binlog_row_value_options.
365 */
367
369
371
373 /**
374 Add new events here - right above this comment!
375 Existing events (except ENUM_END_EVENT) should never change their numbers
376 */
377 ENUM_END_EVENT /* end marker */
379
380/**
381 @brief Get the event type as string object
382
383 @param type the event type for which to get a textual representation.
384 @return std::string a text representing the event name.
385*/
387
388/// @brief Event type helpers, enclosed in the structure
390 /// @brief Helps to identify known GTID event - returns true
391 /// for GTID_LOG_EVENT and GTID_TAGGED_LOG_EVENT
392 inline static bool is_assigned_gtid_event(const Log_event_type &type) {
394 }
395 /// @brief Helps to identify any GTID event - returns true
396 /// for GTID_LOG_EVENT, GTID_TAGGED_LOG_EVENT and ANONYMOUS_GTID_LOG_EVENT
397 inline static bool is_any_gtid_event(const Log_event_type &type) {
399 }
400};
401
402/**
403 Struct to pass basic information about a event: type, query, is it ignorable
404*/
407 const char *query{nullptr};
408 size_t query_length{0};
409 bool ignorable_event{false};
410};
411
412/**
413 The length of the array server_version, which is used to store the version
414 of MySQL server.
415 We could have used SERVER_VERSION_LENGTH, but this introduces an
416 obscure dependency - if somebody decided to change SERVER_VERSION_LENGTH
417 this would break the replication protocol
418 both of these are used to initialize the array server_version
419 SERVER_VERSION_LENGTH is used for global array server_version
420 and ST_SERVER_VER_LEN for the Start_event_v3 member server_version
421*/
422
423#define ST_SERVER_VER_LEN 50
424
425/*
426 Event header offsets;
427 these point to places inside the fixed header.
428*/
429#define EVENT_TYPE_OFFSET 4
430#define SERVER_ID_OFFSET 5
431#define EVENT_LEN_OFFSET 9
432#define LOG_POS_OFFSET 13
433#define FLAGS_OFFSET 17
434
435/** start event post-header (for v3 and v4) */
436#define ST_BINLOG_VER_OFFSET 0
437#define ST_SERVER_VER_OFFSET 2
438#define ST_CREATED_OFFSET (ST_SERVER_VER_OFFSET + ST_SERVER_VER_LEN)
439#define ST_COMMON_HEADER_LEN_OFFSET (ST_CREATED_OFFSET + 4)
440
441#define LOG_EVENT_HEADER_LEN 19U /* the fixed header length */
442
443/// The maximum value for MAX_ALLOWED_PACKET. This is also the
444/// maxmium size of binlog events, and dump threads always use this
445/// value for max_allowed_packet.
446constexpr size_t max_log_event_size = 1024 * 1024 * 1024;
447
448/**
449 Fixed header length, where 4.x and 5.0 agree. That is, 5.0 may have a longer
450 header (it will for sure when we have the unique event's ID), but at least
451 the first 19 bytes are the same in 4.x and 5.0. So when we have the unique
452 event's ID, LOG_EVENT_HEADER_LEN will be something like 26, but
453 LOG_EVENT_MINIMAL_HEADER_LEN will remain 19.
454*/
455#define LOG_EVENT_MINIMAL_HEADER_LEN 19U
456
457/**
458 Enumeration spcifying checksum algorithm used to encode a binary log event
459*/
461 /**
462 Events are without checksum though its generator is checksum-capable
463 New Master (NM).
464 */
466 /** CRC32 of zlib algorithm */
468 /** the cut line: valid alg range is [1, 0x7f] */
470 /**
471 Special value to tag undetermined yet checksum or events from
472 checksum-unaware servers
473 */
476
477#define CHECKSUM_CRC32_SIGNATURE_LEN 4
478
479/**
480 defined statically while there is just one alg implemented
481*/
482#define BINLOG_CHECKSUM_LEN CHECKSUM_CRC32_SIGNATURE_LEN
483#define BINLOG_CHECKSUM_ALG_DESC_LEN 1 /* 1 byte checksum alg descriptor */
484#define LOG_EVENT_HEADER_SIZE 20
485
486/**
487 Calculate a long checksum for a memoryblock.
488
489 @param crc start value for crc
490 @param pos pointer to memory block
491 @param length length of the block
492
493 @return checksum for a memory block
494*/
495inline uint32_t checksum_crc32(uint32_t crc, const unsigned char *pos,
496 size_t length) {
497 BAPI_ASSERT(length <= UINT_MAX);
498 return my_checksum(crc, pos, length);
499}
500
501/*
502 Forward declaration of Format_description_event class to be used in class
503 Log_event_header
504*/
506
507/**
508 @class Log_event_footer
509
510 The footer, in the current version of the MySQL server, only contains
511 the checksum algorithm descriptor. The descriptor is contained in the
512 FDE of the binary log. This is common for all the events contained in
513 that binary log, and defines the algorithm used to checksum
514 the events contained in the binary log.
515
516 @anchor Table_common_footer
517 The footer contains the following:
518 <table>
519 <caption>Common-Footer</caption>
520
521 <tr>
522 <th>Name</th>
523 <th>Format</th>
524 <th>Description</th>
525 </tr>
526
527 <tr>
528 <td>checkusm_alg</td>
529 <td>enum_checksum_alg</td>
530 <td>Algorithm used to checksum the events contained in the binary log</td>
531 </tr>
532
533 </table>
534
535 @note checksum *value* is not generated with the event. On master's side, it
536 is calculated right before writing the event into the binary log. The
537 data_written field of the event is adjusted (+BINLOG_CHECKSUM_LEN) to
538 consider the checksum value as part of the event. On reading the event,
539 if the Format Description Event (FDE) used when serializing the event
540 tells that the events have checksum information, the checksum value can
541 be retrieved from a particular offset of the serialized event buffer
542 (event length - BINLOG_CHECKSUM_LEN) and checked for corruption by
543 computing a new value over the event buffer. It is not required after
544 that. Therefore, the checksum value is not required to be stored in the
545 instance as a class member.
546*/
548 public:
549 /**
550 Wrapper to call get_checksum_alg(const char *, ulong) function based on the
551 event reader object (which knows both buffer and buffer length).
552
553 @param[in] reader The Event_reader object associated to the event buffer
554 of the FD event.
555
556 @retval BINLOG_CHECKSUM_ALG_UNDEF originator is checksum-unaware
557 (effectively no checksum).
558 @retval BINLOG_CHECKSUM_ALG_OFF no checksum.
559 @retval other the actual checksum algorithm descriptor.
560 */
562
563 /**
564 The method returns the checksum algorithm used to checksum the binary log
565 from a Format Description Event serialized buffer.
566
567 For MySQL server versions < 5.6, the algorithm is undefined.
568
569 @param buf buffer holding serialized FD event.
570 @param len length of the event buffer.
571
572 @retval BINLOG_CHECKSUM_ALG_UNDEF originator is checksum-unaware
573 (effectively no checksum).
574 @retval BINLOG_CHECKSUM_ALG_OFF no checksum.
575 @retval other the actual checksum algorithm descriptor.
576 */
578 unsigned long len);
579
580 static bool event_checksum_test(unsigned char *buf, unsigned long event_len,
582
583 /* Constructors */
585
586 /**
587 This ctor will create a new object of Log_event_footer, and will adjust
588 the event reader buffer size with respect to CRC usage.
589
590 @param reader the Event_reader containing the serialized event (including
591 header, event data and optional checksum information).
592 @param event_type the type of the event the footer belongs to.
593 @param fde An FDE event, used to get information about CRC.
594 */
595 Log_event_footer(Event_reader &reader, Log_event_type event_type,
596 const Format_description_event *fde);
597
599 : checksum_alg(checksum_alg_arg) {}
600
601 /**
602 @verbatim
603 Master side:
604 The value is set by caller of FD(Format Description) constructor
605 In the FD case it's propagated into the last byte
606 of post_header_len[].
607 Slave side:
608 On the slave side the value is assigned from post_header_len[last]
609 of the last seen FD event.
610 @endverbatim
611 TODO(WL#7546): Revisit this comment when encoder is moved in libbinlogevent
612 */
614};
615
616/**
617 @class Log_event_header
618
619 The Common-Header always has the same form and length within one
620 version of MySQL. Each event type specifies a format and length
621 of the Post-Header. The length of the Common-Header is the same
622 for all events of the same type.
623
624 @anchor Table_common_header
625 The binary format of Common-Header is as follows:
626 <table>
627 <caption>Common-Header</caption>
628
629 <tr>
630 <th>Name</th>
631 <th>Format</th>
632 <th>Description</th>
633 </tr>
634
635 <tr>
636 <td>when</td>
637 <td>4 byte unsigned integer, represented by type struct timeval</td>
638 <td>The time when the query started, in seconds since 1970.
639 </td>
640 </tr>
641
642 <tr>
643 <td>type_code</td>
644 <td>1 byte enumeration</td>
645 <td>See enum #Log_event_type.</td>
646 </tr>
647
648 <tr>
649 <td>unmasked_server_id</td>
650 <td>4 byte unsigned integer</td>
651 <td>Server ID of the server that created the event.</td>
652 </tr>
653
654 <tr>
655 <td>data_written</td>
656 <td>4 byte unsigned integer</td>
657 <td>The total size of this event, in bytes. In other words, this
658 is the sum of the sizes of Common-Header, Post-Header, and Body.
659 </td>
660 </tr>
661
662 <tr>
663 <td>log_pos</td>
664 <td>4 byte unsigned integer</td>
665 <td>The position of the next event in the master binary log, in
666 bytes from the beginning of the file. In a binlog that is not a
667 relay log, this is just the position of the next event, in bytes
668 from the beginning of the file. In a relay log, this is
669 the position of the next event in the master's binlog.
670 </td>
671 </tr>
672
673 <tr>
674 <td>flags</td>
675 <td>2 byte bitfield</td>
676 <td>16 or less flags depending on the version of the binary log.</td>
677 </tr>
678 </table>
679
680 Summing up the numbers above, we see that the total size of the
681 common header is 19 bytes.
682*/
684 public:
685 /*
686 Timestamp on the master(for debugging and replication of
687 NOW()/TIMESTAMP). It is important for queries and LOAD DATA
688 INFILE. This is set at the event's creation time, except for Query
689 and Load (and other events) events where this is set at the query's
690 execution time, which guarantees good replication (otherwise, we
691 could have a query and its event with different timestamps).
692 */
693 struct timeval when;
694
695 /**
696 Event type extracted from the header. In the server, it is decoded
697 by read_log_event(), but adding here for complete decoding.
698 */
700
701 /*
702 The server id read from the Binlog.
703 */
704 unsigned int unmasked_server_id;
705
706 /* Length of an event, which will be written by write() function */
708
709 /*
710 The offset in the log where this event originally appeared (it is
711 preserved in relay logs, making SHOW SLAVE STATUS able to print
712 coordinates of the event in the master's binlog).
713 */
714 unsigned long long log_pos;
715
716 /*
717 16 or less flags depending on the version of the binary log.
718 See the definitions above for LOG_EVENT_TIME_F,
719 LOG_EVENT_FORCED_ROTATE_F, LOG_EVENT_THREAD_SPECIFIC_F, and
720 LOG_EVENT_SUPPRESS_USE_F for notes.
721 */
722 uint16_t flags;
723
724 /**
725 The following type definition is to be used whenever data is placed
726 and manipulated in a common buffer. Use this typedef for buffers
727 that contain data containing binary and character data.
728 */
729 typedef unsigned char Byte;
730
732 : type_code(type_code_arg), data_written(0), log_pos(0), flags(0) {
733 when.tv_sec = 0;
734 when.tv_usec = 0;
735 }
736 /**
737 Log_event_header constructor.
738
739 @param reader the Event_reader containing the serialized event (including
740 header, event data and optional checksum information).
741 */
743
744 /**
745 The get_is_valid function is related to event specific sanity checks to
746 determine that the object was initialized without errors.
747
748 Note that a given event object may be valid at some point (ancestor
749 event type initialization was fine) but be turned invalid in a later
750 stage.
751
752 @return True if the event object is valid, false otherwise.
753 */
754
755 bool get_is_valid() { return m_is_valid; }
756
757 /**
758 Set if the event object shall be considered valid or not.
759
760 @param is_valid if the event object shall be considered valid.
761 */
762
764
765 /// @brief Accessor for decoding error
766 /// @return Error that occurred during decoding
768
769 /// @brief Sets specific decoding error
770 /// @param decoding_error Decoding error to be set
771 void set_decoding_error(const Event_decoding_error &decoding_error);
772
773 private:
774 /*
775 As errors might happen when de-serializing events, the m_is_valid variable
776 will hold information about the validity of the event.
777
778 An invalid event shall never be applied/dumped/displayed, as its
779 interpretation (accessing its contents) might lead to using invalid
780 memory pointers.
781 */
783
784 /// Holds information about specific decoding error
786};
787
788/**
789 This is the abstract base class for binary log events.
790
791 @section Binary_log_event_binary_format Binary Format
792
793 @anchor Binary_log_event_format
794 Any @c Binary_log_event saved on disk consists of the following four
795 components.
796
797 - Common-Header
798 - Post-Header
799 - Body
800 - Footer
801
802 Common header has the same format and length in a given MySQL version. It is
803 documented @ref Table_common_header "here".
804
805 The Body may be of different format and length even for different events of
806 the same type. The binary formats of Post-Header and Body are documented
807 separately in each subclass.
808
809 Footer is common to all the events in a given MySQL version. It is documented
810 @ref Table_common_footer "here".
811
812 @anchor packed_integer
813 - Some events, used for RBR use a special format for efficient representation
814 of unsigned integers, called Packed Integer. A Packed Integer has the
815 capacity of storing up to 8-byte integers, while small integers
816 still can use 1, 3, or 4 bytes. The value of the first byte
817 determines how to read the number, according to the following table:
818
819 <table>
820 <caption>Format of Packed Integer</caption>
821
822 <tr>
823 <th>First byte</th>
824 <th>Format</th>
825 </tr>
826
827 <tr>
828 <td>0-250</td>
829 <td>The first byte is the number (in the range 0-250), and no more
830 bytes are used.</td>
831 </tr>
832
833 <tr>
834 <td>252</td>
835 <td>Two more bytes are used. The number is in the range
836 251-0xffff.</td>
837 </tr>
838
839 <tr>
840 <td>253</td>
841 <td>Three more bytes are used. The number is in the range
842 0xffff-0xffffff.</td>
843 </tr>
844
845 <tr>
846 <td>254</td>
847 <td>Eight more bytes are used. The number is in the range
848 0xffffff-0xffffffffffffffff.</td>
849 </tr>
850
851 </table>
852
853 - Strings are stored in various formats. The format of each string
854 is documented separately.
855
856*/
858 public:
859 /*
860 The number of types we handle in Format_description_event (UNKNOWN_EVENT
861 is not to be handled, it does not exist in binlogs, it does not have a
862 format).
863 */
864 static const int LOG_EVENT_TYPES = (ENUM_END_EVENT - 1);
865
866 /**
867 The lengths for the fixed data part of each event.
868 This is an enum that provides post-header lengths for all events.
869 */
871 // where 3.23, 4.x and 5.0 agree
872 QUERY_HEADER_MINIMAL_LEN = (4 + 4 + 1 + 2),
873 // where 5.0 differs: 2 for length of N-bytes vars.
877 // this is FROZEN (the Rotate post-header is frozen)
900 }; // end enum_post_header_length
901 protected:
902 /**
903 This constructor is used to initialize the type_code of header object
904 m_header.
905 We set the type code to ENUM_END_EVENT so that the decoder
906 asserts if event type has not been modified by the sub classes
907 */
909 : m_reader(nullptr, 0), m_header(type_code) {}
910
911 /**
912 This constructor will create a new object of Log_event_header and initialize
913 the variable m_header, which in turn will be used to initialize Log_event's
914 member common_header.
915 It will also advance the Event_reader cursor after decoding the header (it
916 is done through the constructor of Log_event_header) and will be pointing to
917 the start of event data.
918
919 @param buf Contains the serialized event.
920 @param fde An FDE event used to get checksum information of non FDE events.
921 */
922 Binary_log_event(const char **buf, const Format_description_event *fde);
923
924 public:
925#ifndef HAVE_MYSYS
926 /*
927 The print_event_info functions are used in the free standing version of
928 the library only. Since MySQL server does not use them, and it does not
929 link to standard input/output library on Windows 32 bit system ,these
930 methods are commented out when the library(libbinlogevents) is built
931 with the server.
932 */
933 /**
934 Returns short information about the event
935 */
936 virtual void print_event_info(std::ostream &info) = 0;
937 /**
938 Returns detailed information about the event
939 */
940 virtual void print_long_info(std::ostream &info) = 0;
941#endif
942 virtual ~Binary_log_event() = 0;
943
948
949 /**
950 * Helper method
951 */
953
954 /**
955 Return a const pointer to the header of the log event
956 */
957 const Log_event_header *header() const { return &m_header; }
958 /**
959 Return a non-const pointer to the header of the log event
960 */
962 /**
963 Return a const pointer to the footer of the log event
964 */
965 const Log_event_footer *footer() const { return &m_footer; }
966 /**
967 Return a non-const pointer to the footer of the log event
968 */
970 /**
971 Returns a reference to the event Event_reader object.
972 */
974
975 private:
976 /*
977 All the accesses to the event buffer shall be performed by using m_reader
978 methods.
979 */
983};
984
985/**
986 @class Unknown_event
987
988 An unknown event should never occur. It is never written to a binary log.
989 If an event is read from a binary log that cannot be recognized as
990 something else, it is treated as UNKNOWN_EVENT.
991
992 The Post-Header and Body for this event type are empty; it only has
993 the Common-Header.
994*/
996 public:
997 /**
998 This is the minimal constructor, and set the type_code as
999 UNKNOWN_EVENT in the header object in Binary_log_event
1000 */
1002
1003 Unknown_event(const char *buf, const Format_description_event *fde);
1004#ifndef HAVE_MYSYS
1005 void print_event_info(std::ostream &info) override;
1006 void print_long_info(std::ostream &info) override;
1007#endif
1008};
1009} // end namespace mysql::binlog::event
1010
1011/// @}
1012
1013#endif // MYSQL_BINLOG_EVENT_BINLOG_EVENT_H
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:250
#define ST_SERVER_VER_LEN
The length of the array server_version, which is used to store the version of MySQL server.
Definition: binlog_event.h:423
This is the abstract base class for binary log events.
Definition: binlog_event.h:857
enum_post_header_length
The lengths for the fixed data part of each event.
Definition: binlog_event.h:870
@ QUERY_HEADER_LEN
Definition: binlog_event.h:874
@ APPEND_BLOCK_HEADER_LEN
Definition: binlog_event.h:880
@ IGNORABLE_HEADER_LEN
Definition: binlog_event.h:894
@ TRANSACTION_CONTEXT_HEADER_LEN
Definition: binlog_event.h:896
@ STOP_HEADER_LEN
Definition: binlog_event.h:875
@ FORMAT_DESCRIPTION_HEADER_LEN
Definition: binlog_event.h:884
@ XID_HEADER_LEN
Definition: binlog_event.h:885
@ ROWS_HEADER_LEN_V1
Definition: binlog_event.h:887
@ BEGIN_LOAD_QUERY_HEADER_LEN
Definition: binlog_event.h:886
@ RAND_HEADER_LEN
Definition: binlog_event.h:882
@ QUERY_HEADER_MINIMAL_LEN
Definition: binlog_event.h:872
@ START_V3_HEADER_LEN
Definition: binlog_event.h:876
@ XA_PREPARE_HEADER_LEN
Definition: binlog_event.h:898
@ EXECUTE_LOAD_QUERY_EXTRA_HEADER_LEN
Definition: binlog_event.h:889
@ INCIDENT_HEADER_LEN
Definition: binlog_event.h:892
@ USER_VAR_HEADER_LEN
Definition: binlog_event.h:883
@ DELETE_FILE_HEADER_LEN
Definition: binlog_event.h:881
@ ROTATE_HEADER_LEN
Definition: binlog_event.h:878
@ EXECUTE_LOAD_QUERY_HEADER_LEN
Definition: binlog_event.h:890
@ TABLE_MAP_HEADER_LEN
Definition: binlog_event.h:888
@ ROWS_HEADER_LEN_V2
Definition: binlog_event.h:895
@ VIEW_CHANGE_HEADER_LEN
Definition: binlog_event.h:897
@ TRANSACTION_PAYLOAD_HEADER_LEN
Definition: binlog_event.h:899
@ HEARTBEAT_HEADER_LEN
Definition: binlog_event.h:893
@ INTVAR_HEADER_LEN
Definition: binlog_event.h:879
const Log_event_header * header() const
Return a const pointer to the header of the log event.
Definition: binlog_event.h:957
Event_reader & reader()
Returns a reference to the event Event_reader object.
Definition: binlog_event.h:973
Binary_log_event(Log_event_type type_code)
This constructor is used to initialize the type_code of header object m_header.
Definition: binlog_event.h:908
Log_event_footer * footer()
Return a non-const pointer to the footer of the log event.
Definition: binlog_event.h:969
Log_event_header m_header
Definition: binlog_event.h:981
Log_event_footer m_footer
Definition: binlog_event.h:982
Binary_log_event & operator=(Binary_log_event &&)=default
Binary_log_event(Binary_log_event &&)=default
static const int LOG_EVENT_TYPES
Definition: binlog_event.h:864
Binary_log_event(const Binary_log_event &)=default
Binary_log_event & operator=(const Binary_log_event &)=default
Event_reader m_reader
Definition: binlog_event.h:980
Log_event_header * header()
Return a non-const pointer to the header of the log event.
Definition: binlog_event.h:961
enum Log_event_type get_event_type() const
Helper method.
Definition: binlog_event.h:952
const Log_event_footer * footer() const
Return a const pointer to the footer of the log event.
Definition: binlog_event.h:965
Event_reader class purpose is to avoid out-of-buffer reads when deserializing binary log events and i...
Definition: event_reader.h:73
For binlog version 4.
Definition: control_events.h:238
The Common-Header always has the same form and length within one version of MySQL.
Definition: binlog_event.h:683
unsigned int unmasked_server_id
Definition: binlog_event.h:704
unsigned long long log_pos
Definition: binlog_event.h:714
unsigned char Byte
The following type definition is to be used whenever data is placed and manipulated in a common buffe...
Definition: binlog_event.h:729
Event_decoding_error m_decoding_error
Holds information about specific decoding error.
Definition: binlog_event.h:785
struct timeval when
Definition: binlog_event.h:693
Log_event_type type_code
Event type extracted from the header.
Definition: binlog_event.h:699
const Event_decoding_error & get_decoding_error() const
Accessor for decoding error.
Definition: binlog_event.cpp:289
void set_is_valid(bool is_valid)
Set if the event object shall be considered valid or not.
Definition: binlog_event.h:763
size_t data_written
Definition: binlog_event.h:707
uint16_t flags
Definition: binlog_event.h:722
void set_decoding_error(const Event_decoding_error &decoding_error)
Sets specific decoding error.
Definition: binlog_event.cpp:293
bool m_is_valid
Definition: binlog_event.h:782
bool get_is_valid()
The get_is_valid function is related to event specific sanity checks to determine that the object was...
Definition: binlog_event.h:755
Log_event_header(Log_event_type type_code_arg=ENUM_END_EVENT)
Definition: binlog_event.h:731
An unknown event should never occur.
Definition: binlog_event.h:995
Unknown_event()
This is the minimal constructor, and set the type_code as UNKNOWN_EVENT in the header object in Binar...
Definition: binlog_event.h:1001
const char * p
Definition: ctype-mb.cc:1234
This header file contains the status of variables used by MySQL tests for debug operations.
Contains the class responsible for deserializing fields of an event previously stored in a buffer.
const unsigned char checksum_version_split[3]
Replication event checksum is introduced in the following "checksum-home" version.
Definition: binlog_event.cpp:30
void do_server_version_split(const char *version, unsigned char split_versions[3])
In case the variable is updated, make sure to update it in $MYSQL_SOURCE_DIR/my_io....
Definition: binlog_event.h:191
const int64_t UNDEFINED_COMMIT_TIMESTAMP
Used to determine whether the original_commit_timestamp is already known or if it still needs to be d...
Definition: binlog_event.h:165
const int64_t SEQ_UNINIT
Uninitialized timestamp value (for either last committed or sequence number).
Definition: binlog_event.h:157
unsigned long version_product(const unsigned char *version_split)
Calculate the version product from the numeric pieces representing the server version: For a server v...
Definition: binlog_event.h:241
const uint32_t UNKNOWN_SERVER_VERSION
Definition: binlog_event.h:168
uint32_t do_server_version_int(const char *version)
Transforms the server version from 'XX.YY.ZZ-suffix' into an integer in the format XXYYZZ.
Definition: binlog_event.h:223
const unsigned long checksum_version_product
Definition: binlog_event.cpp:31
#define MAX_COMMIT_TIMESTAMP_VALUE
We use 7 bytes, 1 bit being used as a flag.
Definition: binlog_event.h:160
const uint32_t UNDEFINED_SERVER_VERSION
Definition: binlog_event.h:167
Abstraction functions over zlib/intrinsics.
ha_checksum my_checksum(ha_checksum crc, const unsigned char *pos, size_t length)
Calculate a CRC32 checksum for a memoryblock.
Definition: my_checksum.h:116
Common #defines and includes for file and socket I/O.
Definition: buf0block_hint.cc:29
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:75
bool is_valid(const dd::Spatial_reference_system *srs, const Geometry *g, const char *func_name, bool *is_valid) noexcept
Decides if a geometry is valid.
Definition: is_valid.cc:94
The namespace contains classes representing events that can occur in a replication stream.
Definition: binlog_event.cpp:35
constexpr size_t max_log_event_size
The maximum value for MAX_ALLOWED_PACKET.
Definition: binlog_event.h:446
uint32_t checksum_crc32(uint32_t crc, const unsigned char *pos, size_t length)
Calculate a long checksum for a memoryblock.
Definition: binlog_event.h:495
Log_event_type
Enumeration type for the different types of log events.
Definition: binlog_event.h:285
@ HEARTBEAT_LOG_EVENT
Heartbeat event to be send by master at its idle time to ensure master's online status to slave.
Definition: binlog_event.h:334
@ HEARTBEAT_LOG_EVENT_V2
Definition: binlog_event.h:370
@ INCIDENT_EVENT
Something out of the ordinary happened on the master.
Definition: binlog_event.h:328
@ FORMAT_DESCRIPTION_EVENT
Definition: binlog_event.h:311
@ BEGIN_LOAD_QUERY_EVENT
Definition: binlog_event.h:313
@ UPDATE_ROWS_EVENT_V1
Definition: binlog_event.h:322
@ EXECUTE_LOAD_QUERY_EVENT
Definition: binlog_event.h:314
@ XID_EVENT
Definition: binlog_event.h:312
@ ROWS_QUERY_LOG_EVENT
Definition: binlog_event.h:343
@ WRITE_ROWS_EVENT
Version 2 of the Row events.
Definition: binlog_event.h:346
@ DELETE_ROWS_EVENT
Definition: binlog_event.h:348
@ IGNORABLE_LOG_EVENT
In some situations, it is necessary to send over ignorable data to the slave: data that a slave can h...
Definition: binlog_event.h:342
@ XA_PREPARE_LOG_EVENT
Definition: binlog_event.h:360
@ QUERY_EVENT
Definition: binlog_event.h:299
@ VIEW_CHANGE_EVENT
Definition: binlog_event.h:357
@ START_EVENT_V3
Definition: binlog_event.h:298
@ UNKNOWN_EVENT
Every time you add a type, you have to.
Definition: binlog_event.h:293
@ PREVIOUS_GTIDS_LOG_EVENT
Definition: binlog_event.h:353
@ UPDATE_ROWS_EVENT
Definition: binlog_event.h:347
@ DELETE_ROWS_EVENT_V1
Definition: binlog_event.h:323
@ RAND_EVENT
Definition: binlog_event.h:309
@ TABLE_MAP_EVENT
Definition: binlog_event.h:316
@ DELETE_FILE_EVENT
Definition: binlog_event.h:307
@ STOP_EVENT
Definition: binlog_event.h:300
@ PARTIAL_UPDATE_ROWS_EVENT
Extension of UPDATE_ROWS_EVENT, allowing partial values according to binlog_row_value_options.
Definition: binlog_event.h:366
@ GTID_LOG_EVENT
Definition: binlog_event.h:350
@ ANONYMOUS_GTID_LOG_EVENT
Definition: binlog_event.h:351
@ WRITE_ROWS_EVENT_V1
The V1 event numbers are used from 5.1.16 until mysql-5.6.
Definition: binlog_event.h:321
@ TRANSACTION_PAYLOAD_EVENT
Definition: binlog_event.h:368
@ APPEND_BLOCK_EVENT
Definition: binlog_event.h:306
@ INTVAR_EVENT
Definition: binlog_event.h:302
@ USER_VAR_EVENT
Definition: binlog_event.h:310
@ GTID_TAGGED_LOG_EVENT
Definition: binlog_event.h:372
@ ROTATE_EVENT
Definition: binlog_event.h:301
@ SLAVE_EVENT
Definition: binlog_event.h:304
@ ENUM_END_EVENT
Add new events here - right above this comment! Existing events (except ENUM_END_EVENT) should never ...
Definition: binlog_event.h:377
@ TRANSACTION_CONTEXT_EVENT
Definition: binlog_event.h:355
const std::string & get_event_type_as_string(Log_event_type type)
Get the event type as string object.
Definition: binlog_event.cpp:79
enum_binlog_checksum_alg
Enumeration spcifying checksum algorithm used to encode a binary log event.
Definition: binlog_event.h:460
@ BINLOG_CHECKSUM_ALG_OFF
Events are without checksum though its generator is checksum-capable New Master (NM).
Definition: binlog_event.h:465
@ BINLOG_CHECKSUM_ALG_ENUM_END
the cut line: valid alg range is [1, 0x7f]
Definition: binlog_event.h:469
@ BINLOG_CHECKSUM_ALG_UNDEF
Special value to tag undetermined yet checksum or events from checksum-unaware servers.
Definition: binlog_event.h:474
@ BINLOG_CHECKSUM_ALG_CRC32
CRC32 of zlib algorithm.
Definition: binlog_event.h:467
Event_decoding_error
Errors that we distinguish during event decoding, that are translated to specific error returned by t...
Definition: event_decoding_error.h:33
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:85
required uint64 version
Definition: replication_group_member_actions.proto:40
required string type
Definition: replication_group_member_actions.proto:33
Struct to pass basic information about a event: type, query, is it ignorable.
Definition: binlog_event.h:405
Log_event_type event_type
Definition: binlog_event.h:406
const char * query
Definition: binlog_event.h:407
size_t query_length
Definition: binlog_event.h:408
bool ignorable_event
Definition: binlog_event.h:409
Event type helpers, enclosed in the structure.
Definition: binlog_event.h:389
static bool is_any_gtid_event(const Log_event_type &type)
Helps to identify any GTID event - returns true for GTID_LOG_EVENT, GTID_TAGGED_LOG_EVENT and ANONYMO...
Definition: binlog_event.h:397
static bool is_assigned_gtid_event(const Log_event_type &type)
Helps to identify known GTID event - returns true for GTID_LOG_EVENT and GTID_TAGGED_LOG_EVENT.
Definition: binlog_event.h:392
#define BAPI_ASSERT(x)
Definition: wrapper_functions.h:60