| 1 |
/* Copyright (c) 2014, 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 "control_events.h" |
| 25 |
#include <sstream> |
| 26 |
#include "codecs/factory.h" |
| 27 |
#include "compression/base.h" |
| 28 |
#include "event_reader_macros.h" |
| 29 |
|
| 30 |
namespace binary_log { |
| 31 |
|
| 32 |
Rotate_event::Rotate_event(const char *buf, const Format_description_event *fde) |
| 33 |
: Binary_log_event(&buf, fde), new_log_ident(nullptr), flags(DUP_NAME) { |
| 34 |
BAPI_ENTER("Rotate_event::Rotate_event(const char*, ...)"); |
| 35 |
READER_TRY_INITIALIZATION; |
| 36 |
#ifndef NDEBUG |
| 37 |
size_t header_size = fde->common_header_len; |
| 38 |
#endif |
| 39 |
READER_ASSERT_POSITION(header_size); |
| 40 |
uint8_t post_header_len = fde->post_header_len[ROTATE_EVENT - 1]; |
| 41 |
|
| 42 |
/* |
| 43 |
By default, an event start immediately after the magic bytes in the binary |
| 44 |
log, which is at offset 4. In case if the slave has to rotate to a |
| 45 |
different event instead of the first one, the binary log offset for that |
| 46 |
event is specified in the post header. Else, the position is set to 4. |
| 47 |
*/ |
| 48 |
if (post_header_len) { |
| 49 |
READER_ASSERT_POSITION(header_size + R_POS_OFFSET); |
| 50 |
READER_TRY_SET(pos, read<uint64_t>); |
| 51 |
READER_ASSERT_POSITION(header_size + post_header_len); |
| 52 |
} else |
| 53 |
pos = 4; |
| 54 |
|
| 55 |
ident_len = READER_CALL(available_to_read); |
| 56 |
if (ident_len == 0) READER_THROW("Event is smaller than expected"); |
| 57 |
|
| 58 |
if (ident_len > FN_REFLEN - 1) ident_len = FN_REFLEN - 1; |
| 59 |
|
| 60 |
READER_TRY_SET(new_log_ident, strndup<const char *>, ident_len); |
| 61 |
if (new_log_ident == nullptr) |
| 62 |
READER_THROW("Invalid binary log file name in Rotate event"); |
| 63 |
|
| 64 |
READER_CATCH_ERROR; |
| 65 |
BAPI_VOID_RETURN; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
Format_description_event 1st constructor. |
| 70 |
*/ |
| 71 |
Format_description_event::Format_description_event(uint8_t binlog_ver, |
| 72 |
const char *server_ver) |
| 73 |
: Binary_log_event(FORMAT_DESCRIPTION_EVENT), |
| 74 |
created(0), |
| 75 |
binlog_version(BINLOG_VERSION), |
| 76 |
dont_set_created(false) { |
| 77 |
binlog_version = binlog_ver; |
| 78 |
switch (binlog_ver) { |
| 79 |
case 4: /* MySQL 5.0 and above*/ |
| 80 |
{ |
| 81 |
/* |
| 82 |
As we are copying from a char * it might be the case at times that some |
| 83 |
part of the array server_version remains uninitialized so memset will |
| 84 |
help in getting rid of the valgrind errors. |
| 85 |
*/ |
| 86 |
memset(server_version, 0, ST_SERVER_VER_LEN); |
| 87 |
snprintf(server_version, ST_SERVER_VER_LEN, "%.*s", ST_SERVER_VER_LEN - 1, |
| 88 |
server_ver); |
| 89 |
|
| 90 |
common_header_len = LOG_EVENT_HEADER_LEN; |
| 91 |
number_of_event_types = LOG_EVENT_TYPES; |
| 92 |
/** |
| 93 |
This will be used to initialize the post_header_len, |
| 94 |
for binlog version 4. |
| 95 |
*/ |
| 96 |
static uint8_t server_event_header_length[] = { |
| 97 |
0, QUERY_HEADER_LEN, STOP_HEADER_LEN, ROTATE_HEADER_LEN, |
| 98 |
INTVAR_HEADER_LEN, 0, |
| 99 |
/* |
| 100 |
Unused because the code for Slave log event was removed. |
| 101 |
(15th Oct. 2010) |
| 102 |
*/ |
| 103 |
0, 0, APPEND_BLOCK_HEADER_LEN, 0, DELETE_FILE_HEADER_LEN, 0, |
| 104 |
RAND_HEADER_LEN, USER_VAR_HEADER_LEN, FORMAT_DESCRIPTION_HEADER_LEN, |
| 105 |
XID_HEADER_LEN, BEGIN_LOAD_QUERY_HEADER_LEN, |
| 106 |
EXECUTE_LOAD_QUERY_HEADER_LEN, TABLE_MAP_HEADER_LEN, 0, 0, 0, |
| 107 |
ROWS_HEADER_LEN_V1, /* WRITE_ROWS_EVENT_V1*/ |
| 108 |
ROWS_HEADER_LEN_V1, /* UPDATE_ROWS_EVENT_V1*/ |
| 109 |
ROWS_HEADER_LEN_V1, /* DELETE_ROWS_EVENT_V1*/ |
| 110 |
INCIDENT_HEADER_LEN, 0, /* HEARTBEAT_LOG_EVENT*/ |
| 111 |
IGNORABLE_HEADER_LEN, IGNORABLE_HEADER_LEN, ROWS_HEADER_LEN_V2, |
| 112 |
ROWS_HEADER_LEN_V2, ROWS_HEADER_LEN_V2, |
| 113 |
Gtid_event::POST_HEADER_LENGTH, /*GTID_EVENT*/ |
| 114 |
Gtid_event::POST_HEADER_LENGTH, /*ANONYMOUS_GTID_EVENT*/ |
| 115 |
IGNORABLE_HEADER_LEN, TRANSACTION_CONTEXT_HEADER_LEN, |
| 116 |
VIEW_CHANGE_HEADER_LEN, XA_PREPARE_HEADER_LEN, ROWS_HEADER_LEN_V2, |
| 117 |
TRANSACTION_PAYLOAD_EVENT, 0 /* HEARTBEAT_LOG_EVENT_V2*/ |
| 118 |
}; |
| 119 |
/* |
| 120 |
Allows us to sanity-check that all events initialized their |
| 121 |
events (see the end of this 'if' block). |
| 122 |
*/ |
| 123 |
post_header_len.insert( |
| 124 |
post_header_len.begin(), server_event_header_length, |
| 125 |
server_event_header_length + number_of_event_types); |
| 126 |
// Sanity-check that all post header lengths are initialized. |
| 127 |
#ifndef NDEBUG |
| 128 |
for (int i = 0; i < number_of_event_types; i++) |
| 129 |
BAPI_ASSERT(post_header_len[i] != 255); |
| 130 |
#endif |
| 131 |
break; |
| 132 |
} |
| 133 |
default: /* Includes binlog version < 4 */ |
| 134 |
/* |
| 135 |
Will make the mysql-server variable *is_valid* defined in class |
| 136 |
Log_event to be set to false. |
| 137 |
*/ |
| 138 |
break; |
| 139 |
} |
| 140 |
calc_server_version_split(); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
This method populates the array server_version_split which is then |
| 145 |
used for lookups to find if the server which |
| 146 |
created this event has some known bug. |
| 147 |
*/ |
| 148 |
void Format_description_event::calc_server_version_split() { |
| 149 |
do_server_version_split(server_version, server_version_split); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
This method is used to find out the version of server that originated |
| 154 |
the current FD instance. |
| 155 |
@return the version of server |
| 156 |
*/ |
| 157 |
unsigned long Format_description_event::get_product_version() const { |
| 158 |
return version_product(server_version_split); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
This method checks the MySQL version to determine whether checksums may be |
| 163 |
present in the events contained in the bainry log. |
| 164 |
|
| 165 |
@retval true if the event's version is earlier than one that introduced |
| 166 |
the replication event checksum. |
| 167 |
@retval false otherwise. |
| 168 |
*/ |
| 169 |
bool Format_description_event::is_version_before_checksum() const { |
| 170 |
return get_product_version() < checksum_version_product; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
The problem with this constructor is that the fixed header may have a |
| 175 |
length different from this version, but we don't know this length as we |
| 176 |
have not read the Format_description_log_event which says it, yet. This |
| 177 |
length is in the post-header of the event, but we don't know where the |
| 178 |
post-header starts. |
| 179 |
|
| 180 |
So this type of event HAS to: |
| 181 |
- either have the header's length at the beginning (in the header, at a |
| 182 |
fixed position which will never be changed), not in the post-header. That |
| 183 |
would make the header be "shifted" compared to other events. |
| 184 |
- or have a header of size LOG_EVENT_MINIMAL_HEADER_LEN (19), in all future |
| 185 |
versions, so that we know for sure. |
| 186 |
|
| 187 |
I (Guilhem) chose the 2nd solution. Rotate has the same constraint (because |
| 188 |
it is sent before Format_description_log_event). |
| 189 |
|
| 190 |
*/ |
| 191 |
Format_description_event::Format_description_event( |
| 192 |
const char *buf, const Format_description_event *fde) |
| 193 |
: Binary_log_event(&buf, fde), common_header_len(0) { |
| 194 |
BAPI_ENTER( |
| 195 |
"Format_description_event::" |
| 196 |
"Format_description_event(const char*, ...)"); |
| 197 |
READER_TRY_INITIALIZATION; |
| 198 |
|
| 199 |
unsigned long ver_calc; |
| 200 |
unsigned long available_bytes; |
| 201 |
number_of_event_types = 0; |
| 202 |
|
| 203 |
READER_ASSERT_POSITION(LOG_EVENT_MINIMAL_HEADER_LEN + ST_BINLOG_VER_OFFSET); |
| 204 |
READER_TRY_SET(binlog_version, read<uint16_t>); |
| 205 |
|
| 206 |
READER_ASSERT_POSITION(LOG_EVENT_MINIMAL_HEADER_LEN + ST_SERVER_VER_OFFSET); |
| 207 |
READER_TRY_CALL(memcpy<char *>, server_version, ST_SERVER_VER_LEN); |
| 208 |
|
| 209 |
// prevent overrun if log is corrupted on disk |
| 210 |
server_version[ST_SERVER_VER_LEN - 1] = 0; |
| 211 |
|
| 212 |
READER_ASSERT_POSITION(LOG_EVENT_MINIMAL_HEADER_LEN + ST_CREATED_OFFSET); |
| 213 |
READER_TRY_SET(created, read<uint64_t>, 4); |
| 214 |
dont_set_created = true; |
| 215 |
|
| 216 |
READER_ASSERT_POSITION(LOG_EVENT_MINIMAL_HEADER_LEN + |
| 217 |
ST_COMMON_HEADER_LEN_OFFSET); |
| 218 |
READER_TRY_SET(common_header_len, read<uint8_t>); |
| 219 |
|
| 220 |
if (common_header_len < LOG_EVENT_HEADER_LEN) |
| 221 |
READER_THROW("Invalid Format_description common header length"); |
| 222 |
|
| 223 |
available_bytes = READER_CALL(available_to_read); |
| 224 |
if (available_bytes == 0) |
| 225 |
READER_THROW("Invalid Format_description common header length"); |
| 226 |
|
| 227 |
calc_server_version_split(); |
| 228 |
if ((ver_calc = get_product_version()) >= checksum_version_product) { |
| 229 |
/* the last bytes are the checksum alg desc and value (or value's room) */ |
| 230 |
available_bytes -= BINLOG_CHECKSUM_ALG_DESC_LEN; |
| 231 |
} |
| 232 |
|
| 233 |
number_of_event_types = available_bytes; |
| 234 |
READER_TRY_CALL(assign, &post_header_len, number_of_event_types); |
| 235 |
|
| 236 |
if ((ver_calc = get_product_version()) >= checksum_version_product) { |
| 237 |
/* |
| 238 |
FD from the checksum-home version server (ver_calc == |
| 239 |
checksum_version_product) must have |
| 240 |
number_of_event_types == LOG_EVENT_TYPES. |
| 241 |
*/ |
| 242 |
BAPI_ASSERT(ver_calc != checksum_version_product || |
| 243 |
number_of_event_types == LOG_EVENT_TYPES); |
| 244 |
uint8_t alg; |
| 245 |
READER_TRY_SET(alg, read<uint8_t>); |
| 246 |
footer()->checksum_alg = static_cast<enum_binlog_checksum_alg>(alg); |
| 247 |
} else { |
| 248 |
footer()->checksum_alg = BINLOG_CHECKSUM_ALG_UNDEF; |
| 249 |
} |
| 250 |
|
| 251 |
if (!header_is_valid()) READER_THROW("Invalid Format_description header"); |
| 252 |
|
| 253 |
if (!version_is_valid()) |
| 254 |
READER_THROW("Invalid server version in Format_description event"); |
| 255 |
|
| 256 |
READER_CATCH_ERROR; |
| 257 |
BAPI_VOID_RETURN; |
| 258 |
} |
| 259 |
|
| 260 |
Format_description_event::~Format_description_event() = default; |
| 261 |
|
| 262 |
Stop_event::Stop_event(const char *buf, const Format_description_event *fde) |
| 263 |
: Binary_log_event(&buf, fde) { |
| 264 |
BAPI_ENTER("Stop_event::Stop_event (const char*, ...)"); |
| 265 |
BAPI_VOID_RETURN; |
| 266 |
} |
| 267 |
|
| 268 |
Incident_event::Incident_event(const char *buf, |
| 269 |
const Format_description_event *fde) |
| 270 |
: Binary_log_event(&buf, fde) { |
| 271 |
BAPI_ENTER("Incident_event::Incident_event(const char *, ...)"); |
| 272 |
READER_TRY_INITIALIZATION; |
| 273 |
READER_ASSERT_POSITION(fde->common_header_len); |
| 274 |
uint16_t incident_number; |
| 275 |
uint8_t len = 0; // Assignment to keep compiler happy |
| 276 |
const char *str = nullptr; // Assignment to keep compiler happy |
| 277 |
|
| 278 |
message = nullptr; |
| 279 |
message_length = 0; |
| 280 |
incident = INCIDENT_NONE; |
| 281 |
|
| 282 |
READER_TRY_SET(incident_number, read<uint16_t>); |
| 283 |
if (incident_number >= INCIDENT_COUNT || incident_number <= INCIDENT_NONE) |
| 284 |
/* |
| 285 |
If the incident is not recognized, this binlog event is |
| 286 |
invalid. |
| 287 |
*/ |
| 288 |
READER_THROW("Invalid incident number in INCIDENT"); |
| 289 |
|
| 290 |
incident = static_cast<enum_incident>(incident_number); |
| 291 |
|
| 292 |
READER_ASSERT_POSITION(fde->common_header_len + |
| 293 |
fde->post_header_len[INCIDENT_EVENT - 1]); |
| 294 |
READER_TRY_CALL(read_str_at_most_255_bytes, &str, &len); |
| 295 |
|
| 296 |
if (!(message = static_cast<char *>(bapi_malloc(len + 1, 16)))) |
| 297 |
READER_THROW("Out of memory"); |
| 298 |
|
| 299 |
strncpy(message, str, len); |
| 300 |
// Appending '\0' at the end. |
| 301 |
message[len] = '\0'; |
| 302 |
message_length = len; |
| 303 |
|
| 304 |
READER_CATCH_ERROR; |
| 305 |
BAPI_VOID_RETURN; |
| 306 |
} |
| 307 |
|
| 308 |
Ignorable_event::Ignorable_event(const char *buf, |
| 309 |
const Format_description_event *fde) |
| 310 |
: Binary_log_event(&buf, fde) { |
| 311 |
BAPI_ENTER("Ignorable_event::Ignorable_event(const char*, ...)"); |
| 312 |
BAPI_VOID_RETURN; |
| 313 |
} |
| 314 |
|
| 315 |
Xid_event::Xid_event(const char *buf, const Format_description_event *fde) |
| 316 |
: Binary_log_event(&buf, fde) { |
| 317 |
BAPI_ENTER("Xid_event::Xid_event(const char*, ...)"); |
| 318 |
READER_TRY_INITIALIZATION; |
| 319 |
READER_ASSERT_POSITION(fde->common_header_len); |
| 320 |
READER_TRY_CALL(forward, fde->post_header_len[XID_EVENT - 1]); |
| 321 |
READER_TRY_SET(xid, memcpy<int64_t>); |
| 322 |
READER_CATCH_ERROR; |
| 323 |
BAPI_VOID_RETURN; |
| 324 |
} |
| 325 |
|
| 326 |
XA_prepare_event::XA_prepare_event(const char *buf, |
| 327 |
const Format_description_event *fde) |
| 328 |
: Binary_log_event(&buf, fde) { |
| 329 |
BAPI_ENTER("XA_prepare_event::XA_prepare_event(const char*, ...)"); |
| 330 |
READER_TRY_INITIALIZATION; |
| 331 |
READER_ASSERT_POSITION(fde->common_header_len); |
| 332 |
READER_TRY_CALL(forward, fde->post_header_len[XA_PREPARE_LOG_EVENT - 1]); |
| 333 |
READER_TRY_SET(one_phase, read<bool>); |
| 334 |
READER_TRY_SET(my_xid.formatID, read<uint32_t>); |
| 335 |
READER_TRY_SET(my_xid.gtrid_length, read<uint32_t>); |
| 336 |
READER_TRY_SET(my_xid.bqual_length, read<uint32_t>); |
| 337 |
|
| 338 |
/* Sanity check */ |
| 339 |
if (MY_XIDDATASIZE >= my_xid.gtrid_length + my_xid.bqual_length && |
| 340 |
my_xid.gtrid_length >= 0 && my_xid.gtrid_length <= 64 && |
| 341 |
my_xid.bqual_length >= 0 && my_xid.bqual_length <= 64) { |
| 342 |
READER_TRY_CALL(memcpy<char *>, my_xid.data, |
| 343 |
my_xid.gtrid_length + my_xid.bqual_length); |
| 344 |
} else |
| 345 |
READER_THROW("Invalid XID information in XA Prepare"); |
| 346 |
|
| 347 |
READER_CATCH_ERROR; |
| 348 |
BAPI_VOID_RETURN; |
| 349 |
} |
| 350 |
|
| 351 |
bool XA_prepare_event::is_one_phase() const { return this->one_phase; } |
| 352 |
|
| 353 |
XA_prepare_event::MY_XID const &XA_prepare_event::get_xid() const { |
| 354 |
return this->my_xid; |
| 355 |
} |
| 356 |
|
| 357 |
Transaction_payload_event::Transaction_payload_event(const char *payload, |
| 358 |
uint64_t payload_size, |
| 359 |
uint16_t compression_type, |
| 360 |
uint64_t uncompressed_size) |
| 361 |
: Binary_log_event(TRANSACTION_PAYLOAD_EVENT), |
| 362 |
m_payload(payload), |
| 363 |
m_payload_size(payload_size), |
| 364 |
m_compression_type((transaction::compression::type)compression_type), |
| 365 |
m_uncompressed_size(uncompressed_size) {} |
| 366 |
|
| 367 |
Transaction_payload_event::~Transaction_payload_event() = default; |
| 368 |
|
| 369 |
Transaction_payload_event::Transaction_payload_event( |
| 370 |
const char *buf, const Format_description_event *fde) |
| 371 |
: Binary_log_event(&buf, fde) { |
| 372 |
if (header()->get_is_valid()) { |
| 373 |
auto codec = codecs::Factory::build_codec(header()->type_code); |
| 374 |
// decode the post LOG_EVENT header |
| 375 |
auto buffer = (const unsigned char *)reader().ptr(); |
| 376 |
size_t buffer_size = reader().available_to_read(); |
| 377 |
auto result = codec->decode(buffer, buffer_size, *this); |
| 378 |
|
| 379 |
header()->set_is_valid(result.second == false); |
| 380 |
if (result.second == false) { |
| 381 |
// move the reader position forward |
| 382 |
reader().forward(result.first); |
| 383 |
|
| 384 |
// set the payload to the rest of the input buffer |
| 385 |
set_payload(reader().ptr()); |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
std::string Transaction_payload_event::to_string() const { |
| 391 |
std::ostringstream oss; |
| 392 |
std::string comp_type = |
| 393 |
binary_log::transaction::compression::type_to_string(m_compression_type); |
| 394 |
|
| 395 |
oss << "\tpayload_size=" << m_payload_size; |
| 396 |
oss << "\tcompression_type=" << comp_type; |
| 397 |
if (m_compression_type != binary_log::transaction::compression::type::NONE) |
| 398 |
oss << "\tuncompressed_size=" << m_uncompressed_size; |
| 399 |
|
| 400 |
return oss.str(); |
| 401 |
} |
| 402 |
|
| 403 |
void Transaction_payload_event::set_payload( |
| 404 |
Buffer_sequence_view_t *buffer_sequence_view) { |
| 405 |
m_buffer_sequence_view = buffer_sequence_view; |
| 406 |
} |
| 407 |
|
| 408 |
#ifndef HAVE_MYSYS |
| 409 |
void Transaction_payload_event::print_event_info(std::ostream &os) { |
| 410 |
os << to_string(); |
| 411 |
} |
| 412 |
|
| 413 |
void Transaction_payload_event::print_long_info(std::ostream &os) { |
| 414 |
print_event_info(os); |
| 415 |
} |
| 416 |
#endif |
| 417 |
|
| 418 |
Gtid_event::Gtid_event(const char *buf, const Format_description_event *fde) |
| 419 |
: Binary_log_event(&buf, fde), |
| 420 |
last_committed(SEQ_UNINIT), |
| 421 |
sequence_number(SEQ_UNINIT), |
| 422 |
may_have_sbr_stmts(true), |
| 423 |
original_commit_timestamp(0), |
| 424 |
immediate_commit_timestamp(0), |
| 425 |
transaction_length(0), |
| 426 |
original_server_version(0), |
| 427 |
immediate_server_version(0) { |
| 428 |
/* |
| 429 |
The layout of the buffer is as follows: |
| 430 |
|
| 431 |
+------------+ |
| 432 |
| 1 byte | Flags |
| 433 |
+------------+ |
| 434 |
| 16 bytes| Encoded SID |
| 435 |
+------------+ |
| 436 |
| 8 bytes| Encoded GNO |
| 437 |
+------------+ |
| 438 |
| 1 byte | lt_type |
| 439 |
+------------+ |
| 440 |
| 8 bytes| last_committed |
| 441 |
+------------+ |
| 442 |
| 8 bytes| sequence_number |
| 443 |
+------------+ |
| 444 |
| 7/14 bytes| timestamps* |
| 445 |
+------------+ |
| 446 |
|1 to 9 bytes| transaction_length (see net_length_size()) |
| 447 |
+------------+ |
| 448 |
| 4/8 bytes| original/immediate_server_version (see timestamps*) |
| 449 |
+------------+ |
| 450 |
| 8 bytes| Commit group ticket |
| 451 |
+------------+ |
| 452 |
|
| 453 |
The 'Flags' field contains gtid flags. |
| 454 |
|
| 455 |
lt_type (for logical timestamp typecode) is always equal to the |
| 456 |
constant LOGICAL_TIMESTAMP_TYPECODE. |
| 457 |
|
| 458 |
5.6 did not have TS_TYPE and the following fields. 5.7.4 and |
| 459 |
earlier had a different value for TS_TYPE and a shorter length for |
| 460 |
the following TS fields. Both these cases are accepted and ignored. |
| 461 |
|
| 462 |
* The section titled "timestamps" contains commit timestamps on originating |
| 463 |
server and commit timestamp on the immediate master. |
| 464 |
|
| 465 |
This is how we write the timestamps section serialized to a memory buffer. |
| 466 |
|
| 467 |
if original_commit_timestamp != immediate_commit_timestamp: |
| 468 |
|
| 469 |
+-7 bytes, high bit (1<<55) set-----+-7 bytes----------+ |
| 470 |
| immediate_commit_timestamp |original_timestamp| |
| 471 |
+-----------------------------------+------------------+ |
| 472 |
|
| 473 |
else: |
| 474 |
|
| 475 |
+-7 bytes, high bit (1<<55) cleared-+ |
| 476 |
| immediate_commit_timestamp | |
| 477 |
+-----------------------------------+ |
| 478 |
*/ |
| 479 |
BAPI_ENTER("Gtid_event::Gtid_event(const char*, ...)"); |
| 480 |
READER_TRY_INITIALIZATION; |
| 481 |
READER_ASSERT_POSITION(fde->common_header_len); |
| 482 |
unsigned char gtid_flags; |
| 483 |
READER_TRY_SET(gtid_flags, read<unsigned char>); |
| 484 |
may_have_sbr_stmts = gtid_flags & FLAG_MAY_HAVE_SBR; |
| 485 |
|
| 486 |
READER_TRY_CALL(memcpy<unsigned char *>, Uuid_parent_struct.bytes, |
| 487 |
Uuid_parent_struct.BYTE_LENGTH); |
| 488 |
// SIDNO is only generated if needed, in get_sidno(). |
| 489 |
gtid_info_struct.rpl_gtid_sidno = -1; |
| 490 |
|
| 491 |
READER_TRY_SET(gtid_info_struct.rpl_gtid_gno, read<int64_t>); |
| 492 |
/* GNO sanity check */ |
| 493 |
if (header()->type_code == GTID_LOG_EVENT) { |
| 494 |
if (gtid_info_struct.rpl_gtid_gno < MIN_GNO || |
| 495 |
gtid_info_struct.rpl_gtid_gno >= GNO_END) |
| 496 |
READER_THROW("Invalid GNO"); |
| 497 |
} else { /* Assume this is an ANONYMOUS_GTID_LOG_EVENT */ |
| 498 |
BAPI_ASSERT(header()->type_code == ANONYMOUS_GTID_LOG_EVENT); |
| 499 |
if (gtid_info_struct.rpl_gtid_gno != 0) READER_THROW("Invalid GNO"); |
| 500 |
} |
| 501 |
|
| 502 |
/* |
| 503 |
Fetch the logical clocks. Check the length before reading, to |
| 504 |
avoid out of buffer reads. |
| 505 |
*/ |
| 506 |
if (READER_CALL(can_read, LOGICAL_TIMESTAMP_TYPECODE_LENGTH)) { |
| 507 |
uint8_t lc_typecode = 0; |
| 508 |
READER_TRY_SET(lc_typecode, read<uint8_t>); |
| 509 |
if (lc_typecode == LOGICAL_TIMESTAMP_TYPECODE) { |
| 510 |
READER_TRY_SET(last_committed, read<uint64_t>); |
| 511 |
READER_TRY_SET(sequence_number, read<uint64_t>); |
| 512 |
|
| 513 |
/* |
| 514 |
Fetch the timestamps used to monitor replication lags with respect to |
| 515 |
the immediate master and the server that originated this transaction. |
| 516 |
Check that the timestamps exist before reading. Note that a master |
| 517 |
older than MySQL-5.8 will NOT send these timestamps. We should be |
| 518 |
able to ignore these fields in this case. |
| 519 |
*/ |
| 520 |
has_commit_timestamps = |
| 521 |
READER_CALL(can_read, IMMEDIATE_COMMIT_TIMESTAMP_LENGTH); |
| 522 |
if (has_commit_timestamps) { |
| 523 |
READER_TRY_SET(immediate_commit_timestamp, read<uint64_t>, |
| 524 |
IMMEDIATE_COMMIT_TIMESTAMP_LENGTH); |
| 525 |
// Check the MSB to determine how to populate |
| 526 |
// original_commit_timestamps |
| 527 |
if ((immediate_commit_timestamp & |
| 528 |
(1ULL << ENCODED_COMMIT_TIMESTAMP_LENGTH)) != 0) { |
| 529 |
// Read the original_commit_timestamp |
| 530 |
immediate_commit_timestamp &= |
| 531 |
~(1ULL << ENCODED_COMMIT_TIMESTAMP_LENGTH); /* Clear MSB. */ |
| 532 |
READER_TRY_SET(original_commit_timestamp, read<uint64_t>, |
| 533 |
ORIGINAL_COMMIT_TIMESTAMP_LENGTH); |
| 534 |
} else { |
| 535 |
// The transaction originated in the previous server |
| 536 |
original_commit_timestamp = immediate_commit_timestamp; |
| 537 |
} |
| 538 |
|
| 539 |
/* Fetch the transaction length if possible */ |
| 540 |
if (READER_CALL(can_read, TRANSACTION_LENGTH_MIN_LENGTH)) { |
| 541 |
READER_TRY_SET(transaction_length, net_field_length_ll); |
| 542 |
} |
| 543 |
|
| 544 |
/** |
| 545 |
Fetch the original/immediate_server_version. Set it to |
| 546 |
UNDEFINED_SERVER_VERSION if no version can be fetched. |
| 547 |
*/ |
| 548 |
original_server_version = UNDEFINED_SERVER_VERSION; |
| 549 |
immediate_server_version = UNDEFINED_SERVER_VERSION; |
| 550 |
if (READER_CALL(can_read, IMMEDIATE_SERVER_VERSION_LENGTH)) { |
| 551 |
READER_TRY_SET(immediate_server_version, read<uint32_t>); |
| 552 |
// Check the MSB to determine how to populate original_server_version |
| 553 |
if ((immediate_server_version & |
| 554 |
(1ULL << ENCODED_SERVER_VERSION_LENGTH)) != 0) { |
| 555 |
// Read the original_server_version |
| 556 |
immediate_server_version &= |
| 557 |
~(1ULL << ENCODED_SERVER_VERSION_LENGTH); // Clear MSB |
| 558 |
READER_TRY_SET(original_server_version, read<uint32_t>, |
| 559 |
ORIGINAL_SERVER_VERSION_LENGTH); |
| 560 |
} else |
| 561 |
original_server_version = immediate_server_version; |
| 562 |
|
| 563 |
if (READER_CALL(can_read, COMMIT_GROUP_TICKET_LENGTH)) { |
| 564 |
READER_TRY_SET(this->commit_group_ticket, read<uint64_t>); |
| 565 |
} |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
READER_CATCH_ERROR; |
| 572 |
BAPI_VOID_RETURN; |
| 573 |
} |
| 574 |
|
| 575 |
int Gtid_event::get_commit_group_ticket_length() const { |
| 576 |
if (kGroupTicketUnset != commit_group_ticket) { |
| 577 |
return COMMIT_GROUP_TICKET_LENGTH; |
| 578 |
} |
| 579 |
return 0; |
| 580 |
} |
| 581 |
|
| 582 |
void Gtid_event::set_commit_group_ticket_and_update_transaction_length( |
| 583 |
std::uint64_t value) { |
| 584 |
/* |
| 585 |
Add the commit_group_ticket length to the transaction length if |
| 586 |
it was not yet considered. |
| 587 |
*/ |
| 588 |
assert(value > 0); |
| 589 |
set_trx_length(transaction_length + (kGroupTicketUnset == commit_group_ticket |
| 590 |
? COMMIT_GROUP_TICKET_LENGTH |
| 591 |
: 0)); |
| 592 |
commit_group_ticket = value; |
| 593 |
} |
| 594 |
|
| 595 |
Previous_gtids_event::Previous_gtids_event(const char *buffer, |
| 596 |
const Format_description_event *fde) |
| 597 |
: Binary_log_event(&buffer, fde) { |
| 598 |
BAPI_ENTER("Previous_gtids_event::Previous_gtids_event(const char*, ...)"); |
| 599 |
READER_TRY_INITIALIZATION; |
| 600 |
READER_ASSERT_POSITION(fde->common_header_len); |
| 601 |
READER_TRY_CALL(forward, fde->post_header_len[PREVIOUS_GTIDS_LOG_EVENT - 1]); |
| 602 |
|
| 603 |
buf = (const unsigned char *)READER_CALL(ptr); |
| 604 |
buf_size = READER_CALL(available_to_read); |
| 605 |
if (buf_size < 8) READER_THROW("Invalid Previous_gtids information"); |
| 606 |
READER_CATCH_ERROR; |
| 607 |
BAPI_VOID_RETURN; |
| 608 |
} |
| 609 |
|
| 610 |
Transaction_context_event::Transaction_context_event( |
| 611 |
const char *buffer, const Format_description_event *fde) |
| 612 |
: Binary_log_event(&buffer, fde) { |
| 613 |
BAPI_ENTER( |
| 614 |
"Transaction_context_event::" |
| 615 |
"Transaction_context_event(const char*, ...)"); |
| 616 |
READER_TRY_INITIALIZATION; |
| 617 |
READER_ASSERT_POSITION(fde->common_header_len); |
| 618 |
|
| 619 |
server_uuid = nullptr; |
| 620 |
encoded_snapshot_version = nullptr; |
| 621 |
|
| 622 |
uint8_t server_uuid_len; |
| 623 |
uint32_t write_set_len; |
| 624 |
uint32_t read_set_len; |
| 625 |
|
| 626 |
READER_TRY_SET(server_uuid_len, read<uint8_t>); |
| 627 |
READER_TRY_SET(thread_id, read<uint32_t>); |
| 628 |
READER_TRY_SET(gtid_specified, read<bool>); |
| 629 |
READER_TRY_SET(encoded_snapshot_version_length, read<uint32_t>); |
| 630 |
READER_TRY_SET(write_set_len, read<uint32_t>); |
| 631 |
READER_TRY_SET(read_set_len, read<uint32_t>); |
| 632 |
|
| 633 |
READER_TRY_SET(server_uuid, strndup<const char *>, server_uuid_len); |
| 634 |
READER_TRY_SET(encoded_snapshot_version, strndup<const unsigned char *>, |
| 635 |
encoded_snapshot_version_length); |
| 636 |
READER_TRY_CALL(read_data_set, write_set_len, &write_set); |
| 637 |
READER_TRY_CALL(read_data_set, read_set_len, &read_set); |
| 638 |
|
| 639 |
READER_CATCH_ERROR; |
| 640 |
BAPI_VOID_RETURN; |
| 641 |
} |
| 642 |
|
| 643 |
/** |
| 644 |
Function to clear the memory of the write_set and the read_set |
| 645 |
|
| 646 |
@param[in] set - pointer to write_set or read_set. |
| 647 |
*/ |
| 648 |
void Transaction_context_event::clear_set(std::list<const char *> *set) { |
| 649 |
for (std::list<const char *>::iterator it = set->begin(); it != set->end(); |
| 650 |
++it) |
| 651 |
bapi_free(const_cast<char *>(*it)); |
| 652 |
set->clear(); |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
Destructor of the Transaction_context_event class. |
| 657 |
*/ |
| 658 |
Transaction_context_event::~Transaction_context_event() { |
| 659 |
if (server_uuid) bapi_free(const_cast<char *>(server_uuid)); |
| 660 |
server_uuid = nullptr; |
| 661 |
if (encoded_snapshot_version) |
| 662 |
bapi_free(const_cast<unsigned char *>(encoded_snapshot_version)); |
| 663 |
encoded_snapshot_version = nullptr; |
| 664 |
clear_set(&write_set); |
| 665 |
clear_set(&read_set); |
| 666 |
} |
| 667 |
|
| 668 |
View_change_event::View_change_event(const char *raw_view_id) |
| 669 |
: Binary_log_event(VIEW_CHANGE_EVENT), |
| 670 |
view_id(), |
| 671 |
seq_number(0), |
| 672 |
certification_info() { |
| 673 |
strncpy(view_id, raw_view_id, sizeof(view_id) - 1); |
| 674 |
view_id[sizeof(view_id) - 1] = 0; |
| 675 |
} |
| 676 |
|
| 677 |
View_change_event::View_change_event(const char *buffer, |
| 678 |
const Format_description_event *fde) |
| 679 |
: Binary_log_event(&buffer, fde), |
| 680 |
view_id(), |
| 681 |
seq_number(0), |
| 682 |
certification_info() { |
| 683 |
BAPI_ENTER("View_change_event::View_change_event(const char*, ...)"); |
| 684 |
READER_TRY_INITIALIZATION; |
| 685 |
READER_ASSERT_POSITION(fde->common_header_len); |
| 686 |
uint32_t cert_info_len; |
| 687 |
|
| 688 |
READER_TRY_CALL(memcpy<char *>, view_id, ENCODED_VIEW_ID_MAX_LEN); |
| 689 |
if (strlen(view_id) == 0) READER_THROW("Invalid View_change information"); |
| 690 |
|
| 691 |
READER_TRY_SET(seq_number, read<uint64_t>); |
| 692 |
READER_TRY_SET(cert_info_len, read<uint32_t>); |
| 693 |
READER_TRY_CALL(read_data_map, cert_info_len, &certification_info); |
| 694 |
|
| 695 |
READER_CATCH_ERROR; |
| 696 |
BAPI_VOID_RETURN; |
| 697 |
} |
| 698 |
|
| 699 |
/** |
| 700 |
Destructor of the View_change_event class. |
| 701 |
*/ |
| 702 |
View_change_event::~View_change_event() { certification_info.clear(); } |
| 703 |
|
| 704 |
Heartbeat_event::Heartbeat_event(const char *buf, |
| 705 |
const Format_description_event *fde) |
| 706 |
: Binary_log_event(&buf, fde) { |
| 707 |
BAPI_ENTER("Heartbeat_event::Heartbeat_event(const char*, ...)"); |
| 708 |
READER_TRY_INITIALIZATION; |
| 709 |
READER_ASSERT_POSITION(fde->common_header_len); |
| 710 |
|
| 711 |
READER_TRY_SET(log_ident, ptr); |
| 712 |
if (log_ident == nullptr || header()->log_pos < BIN_LOG_HEADER_SIZE) |
| 713 |
READER_THROW("Invalid Heartbeat information"); |
| 714 |
|
| 715 |
ident_len = READER_CALL(available_to_read); |
| 716 |
if (ident_len > FN_REFLEN - 1) ident_len = FN_REFLEN - 1; |
| 717 |
|
| 718 |
READER_CATCH_ERROR; |
| 719 |
BAPI_VOID_RETURN; |
| 720 |
} |
| 721 |
|
| 722 |
Heartbeat_event_v2::Heartbeat_event_v2(const char *buf, |
| 723 |
const Format_description_event *fde) |
| 724 |
: Binary_log_event(&buf, fde) { |
| 725 |
BAPI_ENTER("Heartbeat_event_v2::Heartbeat_event_v2(const char*, ...)"); |
| 726 |
READER_TRY_INITIALIZATION; |
| 727 |
READER_ASSERT_POSITION(fde->common_header_len); |
| 728 |
if (header()->get_is_valid()) { |
| 729 |
auto codec = codecs::Factory::build_codec(header()->type_code); |
| 730 |
// decode the post LOG_EVENT header |
| 731 |
auto buffer = (const unsigned char *)reader().ptr(); |
| 732 |
size_t buffer_size = reader().available_to_read(); |
| 733 |
auto result = codec->decode(buffer, buffer_size, *this); |
| 734 |
header()->set_is_valid(result.second == false); |
| 735 |
} |
| 736 |
BAPI_VOID_RETURN; |
| 737 |
} |
| 738 |
|
| 739 |
Heartbeat_event_v2::Heartbeat_event_v2() |
| 740 |
: Binary_log_event(HEARTBEAT_LOG_EVENT_V2) {} |
| 741 |
|
| 742 |
void Heartbeat_event_v2::set_log_filename(const std::string name) { |
| 743 |
m_log_filename = name; |
| 744 |
} |
| 745 |
void Heartbeat_event_v2::set_log_position(uint64_t position) { |
| 746 |
m_log_position = position; |
| 747 |
} |
| 748 |
const std::string Heartbeat_event_v2::get_log_filename() const { |
| 749 |
return m_log_filename; |
| 750 |
} |
| 751 |
uint64_t Heartbeat_event_v2::get_log_position() const { return m_log_position; } |
| 752 |
|
| 753 |
/** |
| 754 |
This member function returns the len of the event |
| 755 |
|
| 756 |
@return the event len |
| 757 |
*/ |
| 758 |
uint64_t Heartbeat_event_v2::max_encoding_length() { |
| 759 |
auto max_log_filename_size_old = FN_REFLEN; |
| 760 |
auto string_terminator_size_old = 1; |
| 761 |
|
| 762 |
// add TYPE size + LEN size + VALUE size |
| 763 |
auto max_filename_len_size = 9UL + 9 + FN_REFLEN; |
| 764 |
auto max_log_position_size = 9UL + 9 + 9; |
| 765 |
|
| 766 |
// Add new field sizes here ------------------------ |
| 767 |
|
| 768 |
return max_log_filename_size_old + string_terminator_size_old + |
| 769 |
max_filename_len_size + max_log_position_size; |
| 770 |
} |
| 771 |
|
| 772 |
#ifndef HAVE_MYSYS |
| 773 |
void Rotate_event::print_event_info(std::ostream &info) { |
| 774 |
info << "Binlog Position: " << pos; |
| 775 |
info << ", Log name: " << new_log_ident; |
| 776 |
} |
| 777 |
|
| 778 |
void Rotate_event::print_long_info(std::ostream &info) { |
| 779 |
info << "Timestamp: " << header()->when.tv_sec; |
| 780 |
info << "\t"; |
| 781 |
this->print_event_info(info); |
| 782 |
} |
| 783 |
|
| 784 |
void Format_description_event::print_event_info(std::ostream &info) { |
| 785 |
info << "Server ver: " << server_version; |
| 786 |
info << ", Binlog ver: " << binlog_version; |
| 787 |
} |
| 788 |
|
| 789 |
void Format_description_event::print_long_info(std::ostream &info) { |
| 790 |
this->print_event_info(info); |
| 791 |
info << "\nCreated timestamp: " << created; |
| 792 |
info << "\tCommon Header Length: " << common_header_len; |
| 793 |
info << "\nPost header length for events: \n"; |
| 794 |
} |
| 795 |
|
| 796 |
void Incident_event::print_event_info(std::ostream &info) { |
| 797 |
info << get_message(); |
| 798 |
info << get_incident_type(); |
| 799 |
} |
| 800 |
|
| 801 |
void Incident_event::print_long_info(std::ostream &info) { |
| 802 |
this->print_event_info(info); |
| 803 |
} |
| 804 |
|
| 805 |
void Xid_event::print_event_info(std::ostream &info) { |
| 806 |
info << "Xid ID=" << xid; |
| 807 |
} |
| 808 |
|
| 809 |
void Xid_event::print_long_info(std::ostream &info) { |
| 810 |
info << "Timestamp: " << header()->when.tv_sec; |
| 811 |
info << "\t"; |
| 812 |
this->print_event_info(info); |
| 813 |
} |
| 814 |
|
| 815 |
void Heartbeat_event_v2::print_event_info(std::ostream &info) { |
| 816 |
info << "{ 'log filename' : '" << m_log_filename << "', " |
| 817 |
<< "'log_position' : " << m_log_position << " }"; |
| 818 |
} |
| 819 |
|
| 820 |
void Heartbeat_event_v2::print_long_info(std::ostream &info) { |
| 821 |
print_event_info(info); |
| 822 |
} |
| 823 |
|
| 824 |
#endif // end HAVE_MYSYS |
| 825 |
|
| 826 |
} // end namespace binary_log |