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