1 /* Copyright (c) 2000, 2014, Oracle and/or its affiliates.
2 Copyright (c) 2009, 2017, MariaDB Corporation.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
16
17 /**
18 @addtogroup Replication
19 @{
20
21 @file
22
23 @brief Binary log event definitions. This includes generic code
24 common to all types of log events, as well as specific code for each
25 type of log event.
26 */
27
28
29 #ifndef _log_event_h
30 #define _log_event_h
31
32 #if defined(USE_PRAGMA_INTERFACE) && defined(MYSQL_SERVER)
33 #pragma interface /* gcc class implementation */
34 #endif
35
36 #include <my_bitmap.h>
37 #include "rpl_constants.h"
38
39 #ifdef MYSQL_CLIENT
40 #include "sql_const.h"
41 #include "rpl_utility.h"
42 #include "hash.h"
43 #include "rpl_tblmap.h"
44 #include "sql_string.h"
45 #endif
46
47 #ifdef MYSQL_SERVER
48 #include "rpl_record.h"
49 #include "rpl_reporting.h"
50 #include "sql_class.h" /* THD */
51 #endif
52
53 #include "rpl_gtid.h"
54
55 /* Forward declarations */
56 #ifndef MYSQL_CLIENT
57 class String;
58 #endif
59
60 #define PREFIX_SQL_LOAD "SQL_LOAD-"
61 #define LONG_FIND_ROW_THRESHOLD 60 /* seconds */
62
63 /**
64 Either assert or return an error.
65
66 In debug build, the condition will be checked, but in non-debug
67 builds, the error code given will be returned instead.
68
69 @param COND Condition to check
70 @param ERRNO Error number to return in non-debug builds
71 */
72 #ifdef DBUG_OFF
73 #define ASSERT_OR_RETURN_ERROR(COND, ERRNO) \
74 do { if (!(COND)) return ERRNO; } while (0)
75 #else
76 #define ASSERT_OR_RETURN_ERROR(COND, ERRNO) \
77 DBUG_ASSERT(COND)
78 #endif
79
80 #define LOG_READ_EOF -1
81 #define LOG_READ_BOGUS -2
82 #define LOG_READ_IO -3
83 #define LOG_READ_MEM -5
84 #define LOG_READ_TRUNC -6
85 #define LOG_READ_TOO_LARGE -7
86 #define LOG_READ_CHECKSUM_FAILURE -8
87 #define LOG_READ_DECRYPT -9
88
89 #define LOG_EVENT_OFFSET 4
90
91 /*
92 3 is MySQL 4.x; 4 is MySQL 5.0.0.
93 Compared to version 3, version 4 has:
94 - a different Start_log_event, which includes info about the binary log
95 (sizes of headers); this info is included for better compatibility if the
96 master's MySQL version is different from the slave's.
97 - all events have a unique ID (the triplet (server_id, timestamp at server
98 start, other) to be sure an event is not executed more than once in a
99 multimaster setup, example:
100 M1
101 / \
102 v v
103 M2 M3
104 \ /
105 v v
106 S
107 if a query is run on M1, it will arrive twice on S, so we need that S
108 remembers the last unique ID it has processed, to compare and know if the
109 event should be skipped or not. Example of ID: we already have the server id
110 (4 bytes), plus:
111 timestamp_when_the_master_started (4 bytes), a counter (a sequence number
112 which increments every time we write an event to the binlog) (3 bytes).
113 Q: how do we handle when the counter is overflowed and restarts from 0 ?
114
115 - Query and Load (Create or Execute) events may have a more precise
116 timestamp (with microseconds), number of matched/affected/warnings rows
117 and fields of session variables: SQL_MODE,
118 FOREIGN_KEY_CHECKS, UNIQUE_CHECKS, SQL_AUTO_IS_NULL, the collations and
119 charsets, the PASSWORD() version (old/new/...).
120 */
121 #define BINLOG_VERSION 4
122
123 /*
124 We could have used SERVER_VERSION_LENGTH, but this introduces an
125 obscure dependency - if somebody decided to change SERVER_VERSION_LENGTH
126 this would break the replication protocol
127 */
128 #define ST_SERVER_VER_LEN 50
129
130 /*
131 These are flags and structs to handle all the LOAD DATA INFILE options (LINES
132 TERMINATED etc).
133 */
134
135 /*
136 These are flags and structs to handle all the LOAD DATA INFILE options (LINES
137 TERMINATED etc).
138 DUMPFILE_FLAG is probably useless (DUMPFILE is a clause of SELECT, not of LOAD
139 DATA).
140 */
141 #define DUMPFILE_FLAG 0x1
142 #define OPT_ENCLOSED_FLAG 0x2
143 #define REPLACE_FLAG 0x4
144 #define IGNORE_FLAG 0x8
145
146 #define FIELD_TERM_EMPTY 0x1
147 #define ENCLOSED_EMPTY 0x2
148 #define LINE_TERM_EMPTY 0x4
149 #define LINE_START_EMPTY 0x8
150 #define ESCAPED_EMPTY 0x10
151
152 #define NUM_LOAD_DELIM_STRS 5
153
154 /*****************************************************************************
155
156 MySQL Binary Log
157
158 This log consists of events. Each event has a fixed-length header,
159 possibly followed by a variable length data body.
160
161 The data body consists of an optional fixed length segment (post-header)
162 and an optional variable length segment.
163
164 See the #defines below for the format specifics.
165
166 The events which really update data are Query_log_event,
167 Execute_load_query_log_event and old Load_log_event and
168 Execute_load_log_event events (Execute_load_query is used together with
169 Begin_load_query and Append_block events to replicate LOAD DATA INFILE.
170 Create_file/Append_block/Execute_load (which includes Load_log_event)
171 were used to replicate LOAD DATA before the 5.0.3).
172
173 ****************************************************************************/
174
175 #define LOG_EVENT_HEADER_LEN 19 /* the fixed header length */
176 #define OLD_HEADER_LEN 13 /* the fixed header length in 3.23 */
177 /*
178 Fixed header length, where 4.x and 5.0 agree. That is, 5.0 may have a longer
179 header (it will for sure when we have the unique event's ID), but at least
180 the first 19 bytes are the same in 4.x and 5.0. So when we have the unique
181 event's ID, LOG_EVENT_HEADER_LEN will be something like 26, but
182 LOG_EVENT_MINIMAL_HEADER_LEN will remain 19.
183 */
184 #define LOG_EVENT_MINIMAL_HEADER_LEN 19
185
186 /* event-specific post-header sizes */
187 // where 3.23, 4.x and 5.0 agree
188 #define QUERY_HEADER_MINIMAL_LEN (4 + 4 + 1 + 2)
189 // where 5.0 differs: 2 for len of N-bytes vars.
190 #define QUERY_HEADER_LEN (QUERY_HEADER_MINIMAL_LEN + 2)
191 #define STOP_HEADER_LEN 0
192 #define LOAD_HEADER_LEN (4 + 4 + 4 + 1 +1 + 4)
193 #define SLAVE_HEADER_LEN 0
194 #define START_V3_HEADER_LEN (2 + ST_SERVER_VER_LEN + 4)
195 #define ROTATE_HEADER_LEN 8 // this is FROZEN (the Rotate post-header is frozen)
196 #define INTVAR_HEADER_LEN 0
197 #define CREATE_FILE_HEADER_LEN 4
198 #define APPEND_BLOCK_HEADER_LEN 4
199 #define EXEC_LOAD_HEADER_LEN 4
200 #define DELETE_FILE_HEADER_LEN 4
201 #define NEW_LOAD_HEADER_LEN LOAD_HEADER_LEN
202 #define RAND_HEADER_LEN 0
203 #define USER_VAR_HEADER_LEN 0
204 #define FORMAT_DESCRIPTION_HEADER_LEN (START_V3_HEADER_LEN+1+LOG_EVENT_TYPES)
205 #define XID_HEADER_LEN 0
206 #define BEGIN_LOAD_QUERY_HEADER_LEN APPEND_BLOCK_HEADER_LEN
207 #define ROWS_HEADER_LEN_V1 8
208 #define TABLE_MAP_HEADER_LEN 8
209 #define EXECUTE_LOAD_QUERY_EXTRA_HEADER_LEN (4 + 4 + 4 + 1)
210 #define EXECUTE_LOAD_QUERY_HEADER_LEN (QUERY_HEADER_LEN + EXECUTE_LOAD_QUERY_EXTRA_HEADER_LEN)
211 #define INCIDENT_HEADER_LEN 2
212 #define HEARTBEAT_HEADER_LEN 0
213 #define IGNORABLE_HEADER_LEN 0
214 #define ROWS_HEADER_LEN_V2 10
215 #define ANNOTATE_ROWS_HEADER_LEN 0
216 #define BINLOG_CHECKPOINT_HEADER_LEN 4
217 #define GTID_HEADER_LEN 19
218 #define GTID_LIST_HEADER_LEN 4
219 #define START_ENCRYPTION_HEADER_LEN 0
220
221 /*
222 Max number of possible extra bytes in a replication event compared to a
223 packet (i.e. a query) sent from client to master;
224 First, an auxiliary log_event status vars estimation:
225 */
226 #define MAX_SIZE_LOG_EVENT_STATUS (1 + 4 /* type, flags2 */ + \
227 1 + 8 /* type, sql_mode */ + \
228 1 + 1 + 255 /* type, length, catalog */ + \
229 1 + 4 /* type, auto_increment */ + \
230 1 + 6 /* type, charset */ + \
231 1 + 1 + 255 /* type, length, time_zone */ + \
232 1 + 2 /* type, lc_time_names_number */ + \
233 1 + 2 /* type, charset_database_number */ + \
234 1 + 8 /* type, table_map_for_update */ + \
235 1 + 4 /* type, master_data_written */ + \
236 1 + 3 /* type, sec_part of NOW() */ + \
237 1 + 16 + 1 + 60/* type, user_len, user, host_len, host */)
238 #define MAX_LOG_EVENT_HEADER ( /* in order of Query_log_event::write */ \
239 LOG_EVENT_HEADER_LEN + /* write_header */ \
240 QUERY_HEADER_LEN + /* write_data */ \
241 EXECUTE_LOAD_QUERY_EXTRA_HEADER_LEN + /*write_post_header_for_derived */ \
242 MAX_SIZE_LOG_EVENT_STATUS + /* status */ \
243 NAME_LEN + 1)
244
245 /*
246 The new option is added to handle large packets that are sent from the master
247 to the slave. It is used to increase the thd(max_allowed) for both the
248 DUMP thread on the master and the SQL/IO thread on the slave.
249 */
250 #define MAX_MAX_ALLOWED_PACKET (1024*1024*1024)
251
252 /*
253 Event header offsets;
254 these point to places inside the fixed header.
255 */
256
257 #define EVENT_TYPE_OFFSET 4
258 #define SERVER_ID_OFFSET 5
259 #define EVENT_LEN_OFFSET 9
260 #define LOG_POS_OFFSET 13
261 #define FLAGS_OFFSET 17
262
263 /* start event post-header (for v3 and v4) */
264
265 #define ST_BINLOG_VER_OFFSET 0
266 #define ST_SERVER_VER_OFFSET 2
267 #define ST_CREATED_OFFSET (ST_SERVER_VER_OFFSET + ST_SERVER_VER_LEN)
268 #define ST_COMMON_HEADER_LEN_OFFSET (ST_CREATED_OFFSET + 4)
269
270 /* slave event post-header (this event is never written) */
271
272 #define SL_MASTER_PORT_OFFSET 8
273 #define SL_MASTER_POS_OFFSET 0
274 #define SL_MASTER_HOST_OFFSET 10
275
276 /* query event post-header */
277
278 #define Q_THREAD_ID_OFFSET 0
279 #define Q_EXEC_TIME_OFFSET 4
280 #define Q_DB_LEN_OFFSET 8
281 #define Q_ERR_CODE_OFFSET 9
282 #define Q_STATUS_VARS_LEN_OFFSET 11
283 #define Q_DATA_OFFSET QUERY_HEADER_LEN
284 /* these are codes, not offsets; not more than 256 values (1 byte). */
285 #define Q_FLAGS2_CODE 0
286 #define Q_SQL_MODE_CODE 1
287 /*
288 Q_CATALOG_CODE is catalog with end zero stored; it is used only by MySQL
289 5.0.x where 0<=x<=3. We have to keep it to be able to replicate these
290 old masters.
291 */
292 #define Q_CATALOG_CODE 2
293 #define Q_AUTO_INCREMENT 3
294 #define Q_CHARSET_CODE 4
295 #define Q_TIME_ZONE_CODE 5
296 /*
297 Q_CATALOG_NZ_CODE is catalog withOUT end zero stored; it is used by MySQL
298 5.0.x where x>=4. Saves one byte in every Query_log_event in binlog,
299 compared to Q_CATALOG_CODE. The reason we didn't simply re-use
300 Q_CATALOG_CODE is that then a 5.0.3 slave of this 5.0.x (x>=4) master would
301 crash (segfault etc) because it would expect a 0 when there is none.
302 */
303 #define Q_CATALOG_NZ_CODE 6
304
305 #define Q_LC_TIME_NAMES_CODE 7
306
307 #define Q_CHARSET_DATABASE_CODE 8
308
309 #define Q_TABLE_MAP_FOR_UPDATE_CODE 9
310
311 #define Q_MASTER_DATA_WRITTEN_CODE 10
312
313 #define Q_INVOKER 11
314
315 #define Q_HRNOW 128
316
317 /* Intvar event post-header */
318
319 /* Intvar event data */
320 #define I_TYPE_OFFSET 0
321 #define I_VAL_OFFSET 1
322
323 /* Rand event data */
324 #define RAND_SEED1_OFFSET 0
325 #define RAND_SEED2_OFFSET 8
326
327 /* User_var event data */
328 #define UV_VAL_LEN_SIZE 4
329 #define UV_VAL_IS_NULL 1
330 #define UV_VAL_TYPE_SIZE 1
331 #define UV_NAME_LEN_SIZE 4
332 #define UV_CHARSET_NUMBER_SIZE 4
333
334 /* Load event post-header */
335 #define L_THREAD_ID_OFFSET 0
336 #define L_EXEC_TIME_OFFSET 4
337 #define L_SKIP_LINES_OFFSET 8
338 #define L_TBL_LEN_OFFSET 12
339 #define L_DB_LEN_OFFSET 13
340 #define L_NUM_FIELDS_OFFSET 14
341 #define L_SQL_EX_OFFSET 18
342 #define L_DATA_OFFSET LOAD_HEADER_LEN
343
344 /* Rotate event post-header */
345 #define R_POS_OFFSET 0
346 #define R_IDENT_OFFSET 8
347
348 /* CF to DF handle LOAD DATA INFILE */
349
350 /* CF = "Create File" */
351 #define CF_FILE_ID_OFFSET 0
352 #define CF_DATA_OFFSET CREATE_FILE_HEADER_LEN
353
354 /* AB = "Append Block" */
355 #define AB_FILE_ID_OFFSET 0
356 #define AB_DATA_OFFSET APPEND_BLOCK_HEADER_LEN
357
358 /* EL = "Execute Load" */
359 #define EL_FILE_ID_OFFSET 0
360
361 /* DF = "Delete File" */
362 #define DF_FILE_ID_OFFSET 0
363
364 /* TM = "Table Map" */
365 #define TM_MAPID_OFFSET 0
366 #define TM_FLAGS_OFFSET 6
367
368 /* RW = "RoWs" */
369 #define RW_MAPID_OFFSET 0
370 #define RW_FLAGS_OFFSET 6
371 #define RW_VHLEN_OFFSET 8
372 #define RW_V_TAG_LEN 1
373 #define RW_V_EXTRAINFO_TAG 0
374
375 /* ELQ = "Execute Load Query" */
376 #define ELQ_FILE_ID_OFFSET QUERY_HEADER_LEN
377 #define ELQ_FN_POS_START_OFFSET ELQ_FILE_ID_OFFSET + 4
378 #define ELQ_FN_POS_END_OFFSET ELQ_FILE_ID_OFFSET + 8
379 #define ELQ_DUP_HANDLING_OFFSET ELQ_FILE_ID_OFFSET + 12
380
381 /* 4 bytes which all binlogs should begin with */
382 #define BINLOG_MAGIC (const uchar*) "\xfe\x62\x69\x6e"
383
384 /*
385 The 2 flags below were useless :
386 - the first one was never set
387 - the second one was set in all Rotate events on the master, but not used for
388 anything useful.
389 So they are now removed and their place may later be reused for other
390 flags. Then one must remember that Rotate events in 4.x have
391 LOG_EVENT_FORCED_ROTATE_F set, so one should not rely on the value of the
392 replacing flag when reading a Rotate event.
393 I keep the defines here just to remember what they were.
394 */
395 #ifdef TO_BE_REMOVED
396 #define LOG_EVENT_TIME_F 0x1
397 #define LOG_EVENT_FORCED_ROTATE_F 0x2
398 #endif
399
400 /*
401 This flag only makes sense for Format_description_log_event. It is set
402 when the event is written, and *reset* when a binlog file is
403 closed (yes, it's the only case when MySQL modifies already written
404 part of binlog). Thus it is a reliable indicator that binlog was
405 closed correctly. (Stop_log_event is not enough, there's always a
406 small chance that mysqld crashes in the middle of insert and end of
407 the binlog would look like a Stop_log_event).
408
409 This flag is used to detect a restart after a crash, and to provide
410 "unbreakable" binlog. The problem is that on a crash storage engines
411 rollback automatically, while binlog does not. To solve this we use this
412 flag and automatically append ROLLBACK to every non-closed binlog (append
413 virtually, on reading, file itself is not changed). If this flag is found,
414 mysqlbinlog simply prints "ROLLBACK" Replication master does not abort on
415 binlog corruption, but takes it as EOF, and replication slave forces a
416 rollback in this case.
417
418 Note, that old binlogs does not have this flag set, so we get a
419 a backward-compatible behaviour.
420 */
421
422 #define LOG_EVENT_BINLOG_IN_USE_F 0x1
423
424 /**
425 @def LOG_EVENT_THREAD_SPECIFIC_F
426
427 If the query depends on the thread (for example: TEMPORARY TABLE).
428 Currently this is used by mysqlbinlog to know it must print
429 SET @@PSEUDO_THREAD_ID=xx; before the query (it would not hurt to print it
430 for every query but this would be slow).
431 */
432 #define LOG_EVENT_THREAD_SPECIFIC_F 0x4
433
434 /**
435 @def LOG_EVENT_SUPPRESS_USE_F
436
437 Suppress the generation of 'USE' statements before the actual
438 statement. This flag should be set for any events that does not need
439 the current database set to function correctly. Most notable cases
440 are 'CREATE DATABASE' and 'DROP DATABASE'.
441
442 This flags should only be used in exceptional circumstances, since
443 it introduce a significant change in behaviour regarding the
444 replication logic together with the flags --binlog-do-db and
445 --replicated-do-db.
446 */
447 #define LOG_EVENT_SUPPRESS_USE_F 0x8
448
449 /*
450 Note: this is a place holder for the flag
451 LOG_EVENT_UPDATE_TABLE_MAP_VERSION_F (0x10), which is not used any
452 more, please do not reused this value for other flags.
453 */
454
455 /**
456 @def LOG_EVENT_ARTIFICIAL_F
457
458 Artificial events are created arbitrarily and not written to binary
459 log
460
461 These events should not update the master log position when slave
462 SQL thread executes them.
463 */
464 #define LOG_EVENT_ARTIFICIAL_F 0x20
465
466 /**
467 @def LOG_EVENT_RELAY_LOG_F
468
469 Events with this flag set are created by slave IO thread and written
470 to relay log
471 */
472 #define LOG_EVENT_RELAY_LOG_F 0x40
473
474 /**
475 @def LOG_EVENT_IGNORABLE_F
476
477 For an event, 'e', carrying a type code, that a slave,
478 's', does not recognize, 's' will check 'e' for
479 LOG_EVENT_IGNORABLE_F, and if the flag is set, then 'e'
480 is ignored. Otherwise, 's' acknowledges that it has
481 found an unknown event in the relay log.
482 */
483 #define LOG_EVENT_IGNORABLE_F 0x80
484
485 /**
486 @def LOG_EVENT_SKIP_REPLICATION_F
487
488 Flag set by application creating the event (with @@skip_replication); the
489 slave will skip replication of such events if
490 --replicate-events-marked-for-skip is not set to REPLICATE.
491
492 This is a MariaDB flag; we allocate it from the end of the available
493 values to reduce risk of conflict with new MySQL flags.
494 */
495 #define LOG_EVENT_SKIP_REPLICATION_F 0x8000
496
497
498 /**
499 @def OPTIONS_WRITTEN_TO_BIN_LOG
500
501 OPTIONS_WRITTEN_TO_BIN_LOG are the bits of thd->options which must
502 be written to the binlog. OPTIONS_WRITTEN_TO_BIN_LOG could be
503 written into the Format_description_log_event, so that if later we
504 don't want to replicate a variable we did replicate, or the
505 contrary, it's doable. But it should not be too hard to decide once
506 for all of what we replicate and what we don't, among the fixed 32
507 bits of thd->options.
508
509 I (Guilhem) have read through every option's usage, and it looks
510 like OPTION_AUTO_IS_NULL and OPTION_NO_FOREIGN_KEYS are the only
511 ones which alter how the query modifies the table. It's good to
512 replicate OPTION_RELAXED_UNIQUE_CHECKS too because otherwise, the
513 slave may insert data slower than the master, in InnoDB.
514 OPTION_BIG_SELECTS is not needed (the slave thread runs with
515 max_join_size=HA_POS_ERROR) and OPTION_BIG_TABLES is not needed
516 either, as the manual says (because a too big in-memory temp table
517 is automatically written to disk).
518 */
519 #define OPTIONS_WRITTEN_TO_BIN_LOG \
520 (OPTION_AUTO_IS_NULL | OPTION_NO_FOREIGN_KEY_CHECKS | \
521 OPTION_RELAXED_UNIQUE_CHECKS | OPTION_NOT_AUTOCOMMIT)
522
523 /* Shouldn't be defined before */
524 #define EXPECTED_OPTIONS \
525 ((1ULL << 14) | (1ULL << 26) | (1ULL << 27) | (1ULL << 19))
526
527 #if OPTIONS_WRITTEN_TO_BIN_LOG != EXPECTED_OPTIONS
528 #error OPTIONS_WRITTEN_TO_BIN_LOG must NOT change their values!
529 #endif
530 #undef EXPECTED_OPTIONS /* You shouldn't use this one */
531
532 #define CHECKSUM_CRC32_SIGNATURE_LEN 4
533 /**
534 defined statically while there is just one alg implemented
535 */
536 #define BINLOG_CHECKSUM_LEN CHECKSUM_CRC32_SIGNATURE_LEN
537 #define BINLOG_CHECKSUM_ALG_DESC_LEN 1 /* 1 byte checksum alg descriptor */
538
539 /*
540 These are capability numbers for MariaDB slave servers.
541
542 Newer MariaDB slaves set this to inform the master about their capabilities.
543 This allows the master to decide which events it can send to the slave
544 without breaking replication on old slaves that maybe do not understand
545 all events from newer masters.
546
547 As new releases are backwards compatible, a given capability implies also
548 all capabilities with smaller number.
549
550 Older MariaDB slaves and other MySQL slave servers do not set this, so they
551 are recorded with capability 0.
552 */
553
554 /* MySQL or old MariaDB slave with no announced capability. */
555 #define MARIA_SLAVE_CAPABILITY_UNKNOWN 0
556 /* MariaDB >= 5.3, which understands ANNOTATE_ROWS_EVENT. */
557 #define MARIA_SLAVE_CAPABILITY_ANNOTATE 1
558 /*
559 MariaDB >= 5.5. This version has the capability to tolerate events omitted
560 from the binlog stream without breaking replication (MySQL slaves fail
561 because they mis-compute the offsets into the master's binlog).
562 */
563 #define MARIA_SLAVE_CAPABILITY_TOLERATE_HOLES 2
564 /* MariaDB >= 10.0, which knows about binlog_checkpoint_log_event. */
565 #define MARIA_SLAVE_CAPABILITY_BINLOG_CHECKPOINT 3
566 /* MariaDB >= 10.0.1, which knows about global transaction id events. */
567 #define MARIA_SLAVE_CAPABILITY_GTID 4
568
569 /* Our capability. */
570 #define MARIA_SLAVE_CAPABILITY_MINE MARIA_SLAVE_CAPABILITY_GTID
571
572
573 /*
574 When the size of 'log_pos' within Heartbeat_log_event exceeds UINT32_MAX it
575 cannot be accommodated in common_header, as 'log_pos' is of 4 bytes size. In
576 such cases, sub_header, of size 8 bytes will hold larger 'log_pos' value.
577 */
578 #define HB_SUB_HEADER_LEN 8
579
580
581 /**
582 @enum Log_event_type
583
584 Enumeration type for the different types of log events.
585 */
586 enum Log_event_type
587 {
588 /*
589 Every time you update this enum (when you add a type), you have to
590 fix Format_description_log_event::Format_description_log_event().
591 */
592 UNKNOWN_EVENT= 0,
593 START_EVENT_V3= 1,
594 QUERY_EVENT= 2,
595 STOP_EVENT= 3,
596 ROTATE_EVENT= 4,
597 INTVAR_EVENT= 5,
598 LOAD_EVENT= 6,
599 SLAVE_EVENT= 7,
600 CREATE_FILE_EVENT= 8,
601 APPEND_BLOCK_EVENT= 9,
602 EXEC_LOAD_EVENT= 10,
603 DELETE_FILE_EVENT= 11,
604 /*
605 NEW_LOAD_EVENT is like LOAD_EVENT except that it has a longer
606 sql_ex, allowing multibyte TERMINATED BY etc; both types share the
607 same class (Load_log_event)
608 */
609 NEW_LOAD_EVENT= 12,
610 RAND_EVENT= 13,
611 USER_VAR_EVENT= 14,
612 FORMAT_DESCRIPTION_EVENT= 15,
613 XID_EVENT= 16,
614 BEGIN_LOAD_QUERY_EVENT= 17,
615 EXECUTE_LOAD_QUERY_EVENT= 18,
616
617 TABLE_MAP_EVENT = 19,
618
619 /*
620 These event numbers were used for 5.1.0 to 5.1.15 and are
621 therefore obsolete.
622 */
623 PRE_GA_WRITE_ROWS_EVENT = 20,
624 PRE_GA_UPDATE_ROWS_EVENT = 21,
625 PRE_GA_DELETE_ROWS_EVENT = 22,
626
627 /*
628 These event numbers are used from 5.1.16 until mysql-5.6.6,
629 and in MariaDB
630 */
631 WRITE_ROWS_EVENT_V1 = 23,
632 UPDATE_ROWS_EVENT_V1 = 24,
633 DELETE_ROWS_EVENT_V1 = 25,
634
635 /*
636 Something out of the ordinary happened on the master
637 */
638 INCIDENT_EVENT= 26,
639
640 /*
641 Heartbeat event to be send by master at its idle time
642 to ensure master's online status to slave
643 */
644 HEARTBEAT_LOG_EVENT= 27,
645
646 /*
647 In some situations, it is necessary to send over ignorable
648 data to the slave: data that a slave can handle in case there
649 is code for handling it, but which can be ignored if it is not
650 recognized.
651
652 These mysql-5.6 events are not recognized (and ignored) by MariaDB
653 */
654 IGNORABLE_LOG_EVENT= 28,
655 ROWS_QUERY_LOG_EVENT= 29,
656
657 /* Version 2 of the Row events, generated only by mysql-5.6.6+ */
658 WRITE_ROWS_EVENT = 30,
659 UPDATE_ROWS_EVENT = 31,
660 DELETE_ROWS_EVENT = 32,
661
662 /* MySQL 5.6 GTID events, ignored by MariaDB */
663 GTID_LOG_EVENT= 33,
664 ANONYMOUS_GTID_LOG_EVENT= 34,
665 PREVIOUS_GTIDS_LOG_EVENT= 35,
666
667 /* MySQL 5.7 events, ignored by MariaDB */
668 TRANSACTION_CONTEXT_EVENT= 36,
669 VIEW_CHANGE_EVENT= 37,
670 XA_PREPARE_LOG_EVENT= 38,
671
672 /*
673 Add new events here - right above this comment!
674 Existing events (except ENUM_END_EVENT) should never change their numbers
675 */
676
677 /* New MySQL/Sun events are to be added right above this comment */
678 MYSQL_EVENTS_END,
679
680 MARIA_EVENTS_BEGIN= 160,
681 /* New Maria event numbers start from here */
682 ANNOTATE_ROWS_EVENT= 160,
683 /*
684 Binlog checkpoint event. Used for XA crash recovery on the master, not used
685 in replication.
686 A binlog checkpoint event specifies a binlog file such that XA crash
687 recovery can start from that file - and it is guaranteed to find all XIDs
688 that are prepared in storage engines but not yet committed.
689 */
690 BINLOG_CHECKPOINT_EVENT= 161,
691 /*
692 Gtid event. For global transaction ID, used to start a new event group,
693 instead of the old BEGIN query event, and also to mark stand-alone
694 events.
695 */
696 GTID_EVENT= 162,
697 /*
698 Gtid list event. Logged at the start of every binlog, to record the
699 current replication state. This consists of the last GTID seen for
700 each replication domain.
701 */
702 GTID_LIST_EVENT= 163,
703
704 START_ENCRYPTION_EVENT= 164,
705
706 /*
707 Compressed binlog event.
708
709 Note that the order between WRITE/UPDATE/DELETE events is significant;
710 this is so that we can convert from the compressed to the uncompressed
711 event type with (type-WRITE_ROWS_COMPRESSED_EVENT + WRITE_ROWS_EVENT)
712 and similar for _V1.
713 */
714 QUERY_COMPRESSED_EVENT = 165,
715 WRITE_ROWS_COMPRESSED_EVENT_V1 = 166,
716 UPDATE_ROWS_COMPRESSED_EVENT_V1 = 167,
717 DELETE_ROWS_COMPRESSED_EVENT_V1 = 168,
718 WRITE_ROWS_COMPRESSED_EVENT = 169,
719 UPDATE_ROWS_COMPRESSED_EVENT = 170,
720 DELETE_ROWS_COMPRESSED_EVENT = 171,
721
722 /* Add new MariaDB events here - right above this comment! */
723
724 ENUM_END_EVENT /* end marker */
725 };
726
727
728 /*
729 Bit flags for what has been writting to cache. Used to
730 discard logs with table map events but not row events and
731 nothing else important. This is stored by cache.
732 */
733
734 enum enum_logged_status
735 {
736 LOGGED_TABLE_MAP= 1,
737 LOGGED_ROW_EVENT= 2,
738 LOGGED_NO_DATA= 4,
739 LOGGED_CRITICAL= 8
740 };
741
LOG_EVENT_IS_QUERY(enum Log_event_type type)742 static inline bool LOG_EVENT_IS_QUERY(enum Log_event_type type)
743 {
744 return type == QUERY_EVENT || type == QUERY_COMPRESSED_EVENT;
745 }
746
747
LOG_EVENT_IS_WRITE_ROW(enum Log_event_type type)748 static inline bool LOG_EVENT_IS_WRITE_ROW(enum Log_event_type type)
749 {
750 return type == WRITE_ROWS_EVENT || type == WRITE_ROWS_EVENT_V1 ||
751 type == WRITE_ROWS_COMPRESSED_EVENT ||
752 type == WRITE_ROWS_COMPRESSED_EVENT_V1;
753 }
754
755
LOG_EVENT_IS_UPDATE_ROW(enum Log_event_type type)756 static inline bool LOG_EVENT_IS_UPDATE_ROW(enum Log_event_type type)
757 {
758 return type == UPDATE_ROWS_EVENT || type == UPDATE_ROWS_EVENT_V1 ||
759 type == UPDATE_ROWS_COMPRESSED_EVENT ||
760 type == UPDATE_ROWS_COMPRESSED_EVENT_V1;
761 }
762
763
LOG_EVENT_IS_DELETE_ROW(enum Log_event_type type)764 static inline bool LOG_EVENT_IS_DELETE_ROW(enum Log_event_type type)
765 {
766 return type == DELETE_ROWS_EVENT || type == DELETE_ROWS_EVENT_V1 ||
767 type == DELETE_ROWS_COMPRESSED_EVENT ||
768 type == DELETE_ROWS_COMPRESSED_EVENT_V1;
769 }
770
771
LOG_EVENT_IS_ROW_COMPRESSED(enum Log_event_type type)772 static inline bool LOG_EVENT_IS_ROW_COMPRESSED(enum Log_event_type type)
773 {
774 return type == WRITE_ROWS_COMPRESSED_EVENT ||
775 type == WRITE_ROWS_COMPRESSED_EVENT_V1 ||
776 type == UPDATE_ROWS_COMPRESSED_EVENT ||
777 type == UPDATE_ROWS_COMPRESSED_EVENT_V1 ||
778 type == DELETE_ROWS_COMPRESSED_EVENT ||
779 type == DELETE_ROWS_COMPRESSED_EVENT_V1;
780 }
781
782
LOG_EVENT_IS_ROW_V2(enum Log_event_type type)783 static inline bool LOG_EVENT_IS_ROW_V2(enum Log_event_type type)
784 {
785 return (type >= WRITE_ROWS_EVENT && type <= DELETE_ROWS_EVENT) ||
786 (type >= WRITE_ROWS_COMPRESSED_EVENT && type <= DELETE_ROWS_COMPRESSED_EVENT);
787 }
788
789
790 /*
791 The number of types we handle in Format_description_log_event (UNKNOWN_EVENT
792 is not to be handled, it does not exist in binlogs, it does not have a
793 format).
794 */
795 #define LOG_EVENT_TYPES (ENUM_END_EVENT-1)
796
797 enum Int_event_type
798 {
799 INVALID_INT_EVENT = 0, LAST_INSERT_ID_EVENT = 1, INSERT_ID_EVENT = 2
800 };
801
802
803 #ifdef MYSQL_SERVER
804 class String;
805 class MYSQL_BIN_LOG;
806 class THD;
807 #endif
808
809 class Format_description_log_event;
810 class Relay_log_info;
811 class binlog_cache_data;
812
813 bool copy_event_cache_to_file_and_reinit(IO_CACHE *cache, FILE *file);
814
815 #ifdef MYSQL_CLIENT
816 enum enum_base64_output_mode {
817 BASE64_OUTPUT_NEVER= 0,
818 BASE64_OUTPUT_AUTO= 1,
819 BASE64_OUTPUT_ALWAYS= 2,
820 BASE64_OUTPUT_UNSPEC= 3,
821 BASE64_OUTPUT_DECODE_ROWS= 4,
822 /* insert new output modes here */
823 BASE64_OUTPUT_MODE_COUNT
824 };
825
826 bool copy_event_cache_to_string_and_reinit(IO_CACHE *cache, LEX_STRING *to);
827
828 /*
829 A structure for mysqlbinlog to know how to print events
830
831 This structure is passed to the event's print() methods,
832
833 There are two types of settings stored here:
834 1. Last db, flags2, sql_mode etc comes from the last printed event.
835 They are stored so that only the necessary USE and SET commands
836 are printed.
837 2. Other information on how to print the events, e.g. short_form,
838 hexdump_from. These are not dependent on the last event.
839 */
840 typedef struct st_print_event_info
841 {
842 /*
843 Settings for database, sql_mode etc that comes from the last event
844 that was printed. We cache these so that we don't have to print
845 them if they are unchanged.
846 */
847 char db[FN_REFLEN+1]; // TODO: make this a LEX_STRING when thd->db is
848 char charset[6]; // 3 variables, each of them storable in 2 bytes
849 char time_zone_str[MAX_TIME_ZONE_NAME_LENGTH];
850 char delimiter[16];
851 sql_mode_t sql_mode; /* must be same as THD.variables.sql_mode */
852 my_thread_id thread_id;
853 ulonglong row_events;
854 ulong auto_increment_increment, auto_increment_offset;
855 uint lc_time_names_number;
856 uint charset_database_number;
857 uint verbose;
858 uint32 flags2;
859 uint32 server_id;
860 uint32 domain_id;
861 uint8 common_header_len;
862 enum_base64_output_mode base64_output_mode;
863 my_off_t hexdump_from;
864
865 table_mapping m_table_map;
866 table_mapping m_table_map_ignored;
867 bool flags2_inited;
868 bool sql_mode_inited;
869 bool charset_inited;
870 bool thread_id_printed;
871 bool server_id_printed;
872 bool domain_id_printed;
873 bool allow_parallel;
874 bool allow_parallel_printed;
875 bool found_row_event;
876 bool print_row_count;
877 static const uint max_delimiter_size= 16;
878 /* Settings on how to print the events */
879 bool short_form;
880 /*
881 This is set whenever a Format_description_event is printed.
882 Later, when an event is printed in base64, this flag is tested: if
883 no Format_description_event has been seen, it is unsafe to print
884 the base64 event, so an error message is generated.
885 */
886 bool printed_fd_event;
887 /*
888 Track when @@skip_replication changes so we need to output a SET
889 statement for it.
890 */
891 bool skip_replication;
892
893 /*
894 These two caches are used by the row-based replication events to
895 collect the header information and the main body of the events
896 making up a statement.
897 */
898 IO_CACHE head_cache;
899 IO_CACHE body_cache;
900 IO_CACHE tail_cache;
901 #ifdef WHEN_FLASHBACK_REVIEW_READY
902 /* Storing the SQL for reviewing */
903 IO_CACHE review_sql_cache;
904 #endif
905 FILE *file;
906 st_print_event_info();
907
~st_print_event_infost_print_event_info908 ~st_print_event_info() {
909 close_cached_file(&head_cache);
910 close_cached_file(&body_cache);
911 close_cached_file(&tail_cache);
912 #ifdef WHEN_FLASHBACK_REVIEW_READY
913 close_cached_file(&review_sql_cache);
914 #endif
915 }
init_okst_print_event_info916 bool init_ok() /* tells if construction was successful */
917 { return my_b_inited(&head_cache) && my_b_inited(&body_cache)
918 #ifdef WHEN_FLASHBACK_REVIEW_READY
919 && my_b_inited(&review_sql_cache)
920 #endif
921 ; }
flush_for_errorst_print_event_info922 void flush_for_error()
923 {
924 if (!copy_event_cache_to_file_and_reinit(&head_cache, file))
925 copy_event_cache_to_file_and_reinit(&body_cache, file);
926 fflush(file);
927 }
928 } PRINT_EVENT_INFO;
929 #endif
930
931 /**
932 This class encapsulates writing of Log_event objects to IO_CACHE.
933 Automatically calculates the checksum and encrypts the data, if necessary.
934 */
935
936 class Log_event_writer
937 {
938 public:
939 ulonglong bytes_written;
940 void *ctx; ///< Encryption context or 0 if no encryption is needed
941 uint checksum_len;
942 int write(Log_event *ev);
943 int write_header(uchar *pos, size_t len);
944 int write_data(const uchar *pos, size_t len);
945 int write_footer();
pos()946 my_off_t pos() { return my_b_safe_tell(file); }
947 void add_status(enum_logged_status status);
948
949 Log_event_writer(IO_CACHE *file_arg, binlog_cache_data *cache_data_arg,
950 Binlog_crypt_data *cr= 0)
951 : bytes_written(0), ctx(0),
952 file(file_arg), cache_data(cache_data_arg), crypto(cr) { }
953
954 private:
955 IO_CACHE *file;
956 binlog_cache_data *cache_data;
957 /**
958 Placeholder for event checksum while writing to binlog.
959 */
960 ha_checksum crc;
961 /**
962 Encryption data (key, nonce). Only used if ctx != 0.
963 */
964 Binlog_crypt_data *crypto;
965 /**
966 Event length to be written into the next encrypted block
967 */
968 uint event_len;
969 int write_internal(const uchar *pos, size_t len);
970 int encrypt_and_write(const uchar *pos, size_t len);
971 int maybe_write_event_len(uchar *pos, size_t len);
972 };
973
974 /**
975 the struct aggregates two parameters that identify an event
976 uniquely in scope of communication of a particular master and slave couple.
977 I.e there can not be 2 events from the same staying connected master which
978 have the same coordinates.
979 @note
980 Such identifier is not yet unique generally as the event originating master
981 is resettable. Also the crashed master can be replaced with some other.
982 */
983 typedef struct event_coordinates
984 {
985 char * file_name; // binlog file name (directories stripped)
986 my_off_t pos; // event's position in the binlog file
987 } LOG_POS_COORD;
988
989 /**
990 @class Log_event
991
992 This is the abstract base class for binary log events.
993
994 @section Log_event_binary_format Binary Format
995
996 Any @c Log_event saved on disk consists of the following three
997 components.
998
999 - Common-Header
1000 - Post-Header
1001 - Body
1002
1003 The Common-Header, documented in the table @ref Table_common_header
1004 "below", always has the same form and length within one version of
1005 MySQL. Each event type specifies a format and length of the
1006 Post-Header. The length of the Common-Header is the same for all
1007 events of the same type. The Body may be of different format and
1008 length even for different events of the same type. The binary
1009 formats of Post-Header and Body are documented separately in each
1010 subclass. The binary format of Common-Header is as follows.
1011
1012 <table>
1013 <caption>Common-Header</caption>
1014
1015 <tr>
1016 <th>Name</th>
1017 <th>Format</th>
1018 <th>Description</th>
1019 </tr>
1020
1021 <tr>
1022 <td>timestamp</td>
1023 <td>4 byte unsigned integer</td>
1024 <td>The time when the query started, in seconds since 1970.
1025 </td>
1026 </tr>
1027
1028 <tr>
1029 <td>type</td>
1030 <td>1 byte enumeration</td>
1031 <td>See enum #Log_event_type.</td>
1032 </tr>
1033
1034 <tr>
1035 <td>server_id</td>
1036 <td>4 byte unsigned integer</td>
1037 <td>Server ID of the server that created the event.</td>
1038 </tr>
1039
1040 <tr>
1041 <td>total_size</td>
1042 <td>4 byte unsigned integer</td>
1043 <td>The total size of this event, in bytes. In other words, this
1044 is the sum of the sizes of Common-Header, Post-Header, and Body.
1045 </td>
1046 </tr>
1047
1048 <tr>
1049 <td>master_position</td>
1050 <td>4 byte unsigned integer</td>
1051 <td>The position of the next event in the master binary log, in
1052 bytes from the beginning of the file. In a binlog that is not a
1053 relay log, this is just the position of the next event, in bytes
1054 from the beginning of the file. In a relay log, this is
1055 the position of the next event in the master's binlog.
1056 </td>
1057 </tr>
1058
1059 <tr>
1060 <td>flags</td>
1061 <td>2 byte bitfield</td>
1062 <td>See Log_event::flags.</td>
1063 </tr>
1064 </table>
1065
1066 Summing up the numbers above, we see that the total size of the
1067 common header is 19 bytes.
1068
1069 @subsection Log_event_format_of_atomic_primitives Format of Atomic Primitives
1070
1071 - All numbers, whether they are 16-, 24-, 32-, or 64-bit numbers,
1072 are stored in little endian, i.e., the least significant byte first,
1073 unless otherwise specified.
1074
1075 @anchor packed_integer
1076 - Some events use a special format for efficient representation of
1077 unsigned integers, called Packed Integer. A Packed Integer has the
1078 capacity of storing up to 8-byte integers, while small integers
1079 still can use 1, 3, or 4 bytes. The value of the first byte
1080 determines how to read the number, according to the following table:
1081
1082 <table>
1083 <caption>Format of Packed Integer</caption>
1084
1085 <tr>
1086 <th>First byte</th>
1087 <th>Format</th>
1088 </tr>
1089
1090 <tr>
1091 <td>0-250</td>
1092 <td>The first byte is the number (in the range 0-250), and no more
1093 bytes are used.</td>
1094 </tr>
1095
1096 <tr>
1097 <td>252</td>
1098 <td>Two more bytes are used. The number is in the range
1099 251-0xffff.</td>
1100 </tr>
1101
1102 <tr>
1103 <td>253</td>
1104 <td>Three more bytes are used. The number is in the range
1105 0xffff-0xffffff.</td>
1106 </tr>
1107
1108 <tr>
1109 <td>254</td>
1110 <td>Eight more bytes are used. The number is in the range
1111 0xffffff-0xffffffffffffffff.</td>
1112 </tr>
1113
1114 </table>
1115
1116 - Strings are stored in various formats. The format of each string
1117 is documented separately.
1118 */
1119 class Log_event
1120 {
1121 public:
1122 /**
1123 Enumeration of what kinds of skipping (and non-skipping) that can
1124 occur when the slave executes an event.
1125
1126 @see shall_skip
1127 @see do_shall_skip
1128 */
1129 enum enum_skip_reason {
1130 /**
1131 Don't skip event.
1132 */
1133 EVENT_SKIP_NOT,
1134
1135 /**
1136 Skip event by ignoring it.
1137
1138 This means that the slave skip counter will not be changed.
1139 */
1140 EVENT_SKIP_IGNORE,
1141
1142 /**
1143 Skip event and decrease skip counter.
1144 */
1145 EVENT_SKIP_COUNT
1146 };
1147
1148 enum enum_event_cache_type
1149 {
1150 EVENT_INVALID_CACHE,
1151 /*
1152 If possible the event should use a non-transactional cache before
1153 being flushed to the binary log. This means that it must be flushed
1154 right after its correspondent statement is completed.
1155 */
1156 EVENT_STMT_CACHE,
1157 /*
1158 The event should use a transactional cache before being flushed to
1159 the binary log. This means that it must be flushed upon commit or
1160 rollback.
1161 */
1162 EVENT_TRANSACTIONAL_CACHE,
1163 /*
1164 The event must be written directly to the binary log without going
1165 through a cache.
1166 */
1167 EVENT_NO_CACHE,
1168 /**
1169 If there is a need for different types, introduce them before this.
1170 */
1171 EVENT_CACHE_COUNT
1172 };
1173
1174 /*
1175 The following type definition is to be used whenever data is placed
1176 and manipulated in a common buffer. Use this typedef for buffers
1177 that contain data containing binary and character data.
1178 */
1179 typedef unsigned char Byte;
1180
1181 /*
1182 The offset in the log where this event originally appeared (it is
1183 preserved in relay logs, making SHOW SLAVE STATUS able to print
1184 coordinates of the event in the master's binlog). Note: when a
1185 transaction is written by the master to its binlog (wrapped in
1186 BEGIN/COMMIT) the log_pos of all the queries it contains is the
1187 one of the BEGIN (this way, when one does SHOW SLAVE STATUS it
1188 sees the offset of the BEGIN, which is logical as rollback may
1189 occur), except the COMMIT query which has its real offset.
1190 */
1191 my_off_t log_pos;
1192 /*
1193 A temp buffer for read_log_event; it is later analysed according to the
1194 event's type, and its content is distributed in the event-specific fields.
1195 */
1196 char *temp_buf;
1197
1198 /*
1199 TRUE <=> this event 'owns' temp_buf and should call my_free() when done
1200 with it
1201 */
1202 bool event_owns_temp_buf;
1203
1204 /*
1205 Timestamp on the master(for debugging and replication of
1206 NOW()/TIMESTAMP). It is important for queries and LOAD DATA
1207 INFILE. This is set at the event's creation time, except for Query
1208 and Load (et al.) events where this is set at the query's
1209 execution time, which guarantees good replication (otherwise, we
1210 could have a query and its event with different timestamps).
1211 */
1212 my_time_t when;
1213 ulong when_sec_part;
1214 /* The number of seconds the query took to run on the master. */
1215 ulong exec_time;
1216 /* Number of bytes written by write() function */
1217 size_t data_written;
1218
1219 /*
1220 The master's server id (is preserved in the relay log; used to
1221 prevent from infinite loops in circular replication).
1222 */
1223 uint32 server_id;
1224
1225 /**
1226 Some 16 flags. See the definitions above for LOG_EVENT_TIME_F,
1227 LOG_EVENT_FORCED_ROTATE_F, LOG_EVENT_THREAD_SPECIFIC_F,
1228 LOG_EVENT_SUPPRESS_USE_F, and LOG_EVENT_SKIP_REPLICATION_F for notes.
1229 */
1230 uint16 flags;
1231
1232 uint16 cache_type;
1233
1234 /**
1235 A storage to cache the global system variable's value.
1236 Handling of a separate event will be governed its member.
1237 */
1238 ulong slave_exec_mode;
1239
1240 Log_event_writer *writer;
1241
1242 #ifdef MYSQL_SERVER
1243 THD* thd;
1244
1245 Log_event();
1246 Log_event(THD* thd_arg, uint16 flags_arg, bool is_transactional);
1247
1248 /*
1249 init_show_field_list() prepares the column names and types for the
1250 output of SHOW BINLOG EVENTS; it is used only by SHOW BINLOG
1251 EVENTS.
1252 */
1253 static void init_show_field_list(THD *thd, List<Item>* field_list);
1254 #ifdef HAVE_REPLICATION
1255 int net_send(Protocol *protocol, const char* log_name, my_off_t pos);
1256
1257 /*
1258 pack_info() is used by SHOW BINLOG EVENTS; as print() it prepares and sends
1259 a string to display to the user, so it resembles print().
1260 */
1261
1262 virtual void pack_info(Protocol *protocol);
1263
1264 #endif /* HAVE_REPLICATION */
get_db()1265 virtual const char* get_db()
1266 {
1267 return thd ? thd->db.str : 0;
1268 }
1269 #else
Log_event()1270 Log_event() : temp_buf(0), when(0), flags(0) {}
1271 ha_checksum crc;
1272 /* print*() functions are used by mysqlbinlog */
1273 virtual bool print(FILE* file, PRINT_EVENT_INFO* print_event_info) = 0;
1274 bool print_timestamp(IO_CACHE* file, time_t *ts = 0);
1275 bool print_header(IO_CACHE* file, PRINT_EVENT_INFO* print_event_info,
1276 bool is_more);
1277 bool print_base64(IO_CACHE* file, PRINT_EVENT_INFO* print_event_info,
1278 bool do_print_encoded);
1279 #endif /* MYSQL_SERVER */
1280
1281 /* The following code used for Flashback */
1282 #ifdef MYSQL_CLIENT
1283 my_bool is_flashback;
1284 my_bool need_flashback_review;
1285 String output_buf; // Storing the event output
1286 #ifdef WHEN_FLASHBACK_REVIEW_READY
1287 String m_review_dbname;
1288 String m_review_tablename;
1289
set_review_dbname(const char * name)1290 void set_review_dbname(const char *name)
1291 {
1292 if (name)
1293 {
1294 m_review_dbname.free();
1295 m_review_dbname.append(name);
1296 }
1297 }
set_review_tablename(const char * name)1298 void set_review_tablename(const char *name)
1299 {
1300 if (name)
1301 {
1302 m_review_tablename.free();
1303 m_review_tablename.append(name);
1304 }
1305 }
get_review_dbname()1306 const char *get_review_dbname() const { return m_review_dbname.ptr(); }
get_review_tablename()1307 const char *get_review_tablename() const { return m_review_tablename.ptr(); }
1308 #endif
1309 #endif
1310
1311 /*
1312 read_log_event() functions read an event from a binlog or relay
1313 log; used by SHOW BINLOG EVENTS, the binlog_dump thread on the
1314 master (reads master's binlog), the slave IO thread (reads the
1315 event sent by binlog_dump), the slave SQL thread (reads the event
1316 from the relay log). If mutex is 0, the read will proceed without
1317 mutex. We need the description_event to be able to parse the
1318 event (to know the post-header's size); in fact in read_log_event
1319 we detect the event's type, then call the specific event's
1320 constructor and pass description_event as an argument.
1321 */
1322 static Log_event* read_log_event(IO_CACHE* file,
1323 const Format_description_log_event
1324 *description_event,
1325 my_bool crc_check);
1326
1327 /**
1328 Reads an event from a binlog or relay log. Used by the dump thread
1329 this method reads the event into a raw buffer without parsing it.
1330
1331 @Note If mutex is 0, the read will proceed without mutex.
1332
1333 @Note If a log name is given than the method will check if the
1334 given binlog is still active.
1335
1336 @param[in] file log file to be read
1337 @param[out] packet packet to hold the event
1338 @param[in] checksum_alg_arg verify the event checksum using this
1339 algorithm (or don't if it's
1340 use BINLOG_CHECKSUM_ALG_OFF)
1341
1342 @retval 0 success
1343 @retval LOG_READ_EOF end of file, nothing was read
1344 @retval LOG_READ_BOGUS malformed event
1345 @retval LOG_READ_IO io error while reading
1346 @retval LOG_READ_MEM packet memory allocation failed
1347 @retval LOG_READ_TRUNC only a partial event could be read
1348 @retval LOG_READ_TOO_LARGE event too large
1349 */
1350 static int read_log_event(IO_CACHE* file, String* packet,
1351 const Format_description_log_event *fdle,
1352 enum enum_binlog_checksum_alg checksum_alg_arg);
1353 /*
1354 The value is set by caller of FD constructor and
1355 Log_event::write_header() for the rest.
1356 In the FD case it's propagated into the last byte
1357 of post_header_len[] at FD::write().
1358 On the slave side the value is assigned from post_header_len[last]
1359 of the last seen FD event.
1360 */
1361 enum enum_binlog_checksum_alg checksum_alg;
1362
new(size_t size)1363 static void *operator new(size_t size)
1364 {
1365 return (void*) my_malloc((uint)size, MYF(MY_WME|MY_FAE));
1366 }
1367
delete(void * ptr,size_t)1368 static void operator delete(void *ptr, size_t)
1369 {
1370 my_free(ptr);
1371 }
1372
1373 /* Placement version of the above operators */
new(size_t,void * ptr)1374 static void *operator new(size_t, void* ptr) { return ptr; }
delete(void *,void *)1375 static void operator delete(void*, void*) { }
1376
1377 #ifdef MYSQL_SERVER
1378 bool write_header(size_t event_data_length);
write_data(const uchar * buf,size_t data_length)1379 bool write_data(const uchar *buf, size_t data_length)
1380 { return writer->write_data(buf, data_length); }
write_data(const char * buf,size_t data_length)1381 bool write_data(const char *buf, size_t data_length)
1382 { return write_data((uchar*)buf, data_length); }
write_footer()1383 bool write_footer()
1384 { return writer->write_footer(); }
1385
1386 my_bool need_checksum();
1387
write()1388 virtual bool write()
1389 {
1390 return write_header(get_data_size()) || write_data_header() ||
1391 write_data_body() || write_footer();
1392 }
write_data_header()1393 virtual bool write_data_header()
1394 { return 0; }
write_data_body()1395 virtual bool write_data_body()
1396 { return 0; }
1397
1398 /* Return start of query time or current time */
get_time()1399 inline my_time_t get_time()
1400 {
1401 THD *tmp_thd;
1402 if (when)
1403 return when;
1404 if (thd)
1405 {
1406 when= thd->start_time;
1407 when_sec_part= thd->start_time_sec_part;
1408 return when;
1409 }
1410 /* thd will only be 0 here at time of log creation */
1411 if ((tmp_thd= current_thd))
1412 {
1413 when= tmp_thd->start_time;
1414 when_sec_part= tmp_thd->start_time_sec_part;
1415 return when;
1416 }
1417 my_hrtime_t hrtime= my_hrtime();
1418 when= hrtime_to_my_time(hrtime);
1419 when_sec_part= hrtime_sec_part(hrtime);
1420 return when;
1421 }
1422 #endif
1423 virtual Log_event_type get_type_code() = 0;
logged_status()1424 virtual enum_logged_status logged_status() { return LOGGED_CRITICAL; }
1425 virtual bool is_valid() const = 0;
get_header_len(my_off_t len)1426 virtual my_off_t get_header_len(my_off_t len) { return len; }
set_artificial_event()1427 void set_artificial_event() { flags |= LOG_EVENT_ARTIFICIAL_F; }
set_relay_log_event()1428 void set_relay_log_event() { flags |= LOG_EVENT_RELAY_LOG_F; }
is_artificial_event()1429 bool is_artificial_event() const { return flags & LOG_EVENT_ARTIFICIAL_F; }
is_relay_log_event()1430 bool is_relay_log_event() const { return flags & LOG_EVENT_RELAY_LOG_F; }
use_trans_cache()1431 inline bool use_trans_cache() const
1432 {
1433 return (cache_type == Log_event::EVENT_TRANSACTIONAL_CACHE);
1434 }
set_direct_logging()1435 inline void set_direct_logging()
1436 {
1437 cache_type = Log_event::EVENT_NO_CACHE;
1438 }
use_direct_logging()1439 inline bool use_direct_logging()
1440 {
1441 return (cache_type == Log_event::EVENT_NO_CACHE);
1442 }
1443 Log_event(const char* buf, const Format_description_log_event
1444 *description_event);
~Log_event()1445 virtual ~Log_event() { free_temp_buf();}
register_temp_buf(char * buf,bool must_free)1446 void register_temp_buf(char* buf, bool must_free)
1447 {
1448 temp_buf= buf;
1449 event_owns_temp_buf= must_free;
1450 }
free_temp_buf()1451 void free_temp_buf()
1452 {
1453 if (temp_buf)
1454 {
1455 if (event_owns_temp_buf)
1456 my_free(temp_buf);
1457 temp_buf = 0;
1458 }
1459 }
1460 /*
1461 Get event length for simple events. For complicated events the length
1462 is calculated during write()
1463 */
get_data_size()1464 virtual int get_data_size() { return 0;}
1465 static Log_event* read_log_event(const char* buf, uint event_len,
1466 const char **error,
1467 const Format_description_log_event
1468 *description_event, my_bool crc_check);
1469 /**
1470 Returns the human readable name of the given event type.
1471 */
1472 static const char* get_type_str(Log_event_type type);
1473 /**
1474 Returns the human readable name of this event's type.
1475 */
1476 const char* get_type_str();
1477
1478 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
1479
1480 /**
1481 Apply the event to the database.
1482
1483 This function represents the public interface for applying an
1484 event.
1485
1486 @see do_apply_event
1487 */
apply_event(rpl_group_info * rgi)1488 int apply_event(rpl_group_info *rgi)
1489 {
1490 int res;
1491 THD_STAGE_INFO(thd, stage_apply_event);
1492 res= do_apply_event(rgi);
1493 THD_STAGE_INFO(thd, stage_after_apply_event);
1494 return res;
1495 }
1496
1497
1498 /**
1499 Update the relay log position.
1500
1501 This function represents the public interface for "stepping over"
1502 the event and will update the relay log information.
1503
1504 @see do_update_pos
1505 */
update_pos(rpl_group_info * rgi)1506 int update_pos(rpl_group_info *rgi)
1507 {
1508 return do_update_pos(rgi);
1509 }
1510
1511 /**
1512 Decide if the event shall be skipped, and the reason for skipping
1513 it.
1514
1515 @see do_shall_skip
1516 */
shall_skip(rpl_group_info * rgi)1517 enum_skip_reason shall_skip(rpl_group_info *rgi)
1518 {
1519 return do_shall_skip(rgi);
1520 }
1521
1522
1523 /*
1524 Check if an event is non-final part of a stand-alone event group,
1525 such as Intvar_log_event (such events should be processed as part
1526 of the following event group, not individually).
1527 See also is_part_of_group()
1528 */
is_part_of_group(enum Log_event_type ev_type)1529 static bool is_part_of_group(enum Log_event_type ev_type)
1530 {
1531 switch (ev_type)
1532 {
1533 case GTID_EVENT:
1534 case INTVAR_EVENT:
1535 case RAND_EVENT:
1536 case USER_VAR_EVENT:
1537 case TABLE_MAP_EVENT:
1538 case ANNOTATE_ROWS_EVENT:
1539 return true;
1540 case DELETE_ROWS_EVENT:
1541 case UPDATE_ROWS_EVENT:
1542 case WRITE_ROWS_EVENT:
1543 /*
1544 ToDo: also check for non-final Rows_log_event (though such events
1545 are usually in a BEGIN-COMMIT group).
1546 */
1547 default:
1548 return false;
1549 }
1550 }
1551 /*
1552 Same as above, but works on the object. In addition this is true for all
1553 rows event except the last one.
1554 */
is_part_of_group()1555 virtual bool is_part_of_group() { return 0; }
1556
is_group_event(enum Log_event_type ev_type)1557 static bool is_group_event(enum Log_event_type ev_type)
1558 {
1559 switch (ev_type)
1560 {
1561 case START_EVENT_V3:
1562 case STOP_EVENT:
1563 case ROTATE_EVENT:
1564 case SLAVE_EVENT:
1565 case FORMAT_DESCRIPTION_EVENT:
1566 case INCIDENT_EVENT:
1567 case HEARTBEAT_LOG_EVENT:
1568 case BINLOG_CHECKPOINT_EVENT:
1569 case GTID_LIST_EVENT:
1570 case START_ENCRYPTION_EVENT:
1571 return false;
1572
1573 default:
1574 return true;
1575 }
1576 }
1577
1578 protected:
1579
1580 /**
1581 Helper function to ignore an event w.r.t. the slave skip counter.
1582
1583 This function can be used inside do_shall_skip() for functions
1584 that cannot end a group. If the slave skip counter is 1 when
1585 seeing such an event, the event shall be ignored, the counter
1586 left intact, and processing continue with the next event.
1587
1588 A typical usage is:
1589 @code
1590 enum_skip_reason do_shall_skip(rpl_group_info *rgi) {
1591 return continue_group(rgi);
1592 }
1593 @endcode
1594
1595 @return Skip reason
1596 */
1597 enum_skip_reason continue_group(rpl_group_info *rgi);
1598
1599 /**
1600 Primitive to apply an event to the database.
1601
1602 This is where the change to the database is made.
1603
1604 @note The primitive is protected instead of private, since there
1605 is a hierarchy of actions to be performed in some cases.
1606
1607 @see Format_description_log_event::do_apply_event()
1608
1609 @param rli Pointer to relay log info structure
1610
1611 @retval 0 Event applied successfully
1612 @retval errno Error code if event application failed
1613 */
do_apply_event(rpl_group_info * rgi)1614 virtual int do_apply_event(rpl_group_info *rgi)
1615 {
1616 return 0; /* Default implementation does nothing */
1617 }
1618
1619
1620 /**
1621 Advance relay log coordinates.
1622
1623 This function is called to advance the relay log coordinates to
1624 just after the event. It is essential that both the relay log
1625 coordinate and the group log position is updated correctly, since
1626 this function is used also for skipping events.
1627
1628 Normally, each implementation of do_update_pos() shall:
1629
1630 - Update the event position to refer to the position just after
1631 the event.
1632
1633 - Update the group log position to refer to the position just
1634 after the event <em>if the event is last in a group</em>
1635
1636 @param rli Pointer to relay log info structure
1637
1638 @retval 0 Coordinates changed successfully
1639 @retval errno Error code if advancing failed (usually just
1640 1). Observe that handler errors are returned by the
1641 do_apply_event() function, and not by this one.
1642 */
1643 virtual int do_update_pos(rpl_group_info *rgi);
1644
1645
1646 /**
1647 Decide if this event shall be skipped or not and the reason for
1648 skipping it.
1649
1650 The default implementation decide that the event shall be skipped
1651 if either:
1652
1653 - the server id of the event is the same as the server id of the
1654 server and <code>rli->replicate_same_server_id</code> is true,
1655 or
1656
1657 - if <code>rli->slave_skip_counter</code> is greater than zero.
1658
1659 @see do_apply_event
1660 @see do_update_pos
1661
1662 @retval Log_event::EVENT_SKIP_NOT
1663 The event shall not be skipped and should be applied.
1664
1665 @retval Log_event::EVENT_SKIP_IGNORE
1666 The event shall be skipped by just ignoring it, i.e., the slave
1667 skip counter shall not be changed. This happends if, for example,
1668 the originating server id of the event is the same as the server
1669 id of the slave.
1670
1671 @retval Log_event::EVENT_SKIP_COUNT
1672 The event shall be skipped because the slave skip counter was
1673 non-zero. The caller shall decrease the counter by one.
1674 */
1675 virtual enum_skip_reason do_shall_skip(rpl_group_info *rgi);
1676 #endif
1677 };
1678
1679
1680 /*
1681 One class for each type of event.
1682 Two constructors for each class:
1683 - one to create the event for logging (when the server acts as a master),
1684 called after an update to the database is done,
1685 which accepts parameters like the query, the database, the options for LOAD
1686 DATA INFILE...
1687 - one to create the event from a packet (when the server acts as a slave),
1688 called before reproducing the update, which accepts parameters (like a
1689 buffer). Used to read from the master, from the relay log, and in
1690 mysqlbinlog. This constructor must be format-tolerant.
1691 */
1692
1693 /**
1694 @class Query_log_event
1695
1696 A @c Query_log_event is created for each query that modifies the
1697 database, unless the query is logged row-based.
1698
1699 @section Query_log_event_binary_format Binary format
1700
1701 See @ref Log_event_binary_format "Binary format for log events" for
1702 a general discussion and introduction to the binary format of binlog
1703 events.
1704
1705 The Post-Header has five components:
1706
1707 <table>
1708 <caption>Post-Header for Query_log_event</caption>
1709
1710 <tr>
1711 <th>Name</th>
1712 <th>Format</th>
1713 <th>Description</th>
1714 </tr>
1715
1716 <tr>
1717 <td>slave_proxy_id</td>
1718 <td>4 byte unsigned integer</td>
1719 <td>An integer identifying the client thread that issued the
1720 query. The id is unique per server. (Note, however, that two
1721 threads on different servers may have the same slave_proxy_id.)
1722 This is used when a client thread creates a temporary table local
1723 to the client. The slave_proxy_id is used to distinguish
1724 temporary tables that belong to different clients.
1725 </td>
1726 </tr>
1727
1728 <tr>
1729 <td>exec_time</td>
1730 <td>4 byte unsigned integer</td>
1731 <td>The time from when the query started to when it was logged in
1732 the binlog, in seconds.</td>
1733 </tr>
1734
1735 <tr>
1736 <td>db_len</td>
1737 <td>1 byte integer</td>
1738 <td>The length of the name of the currently selected database.</td>
1739 </tr>
1740
1741 <tr>
1742 <td>error_code</td>
1743 <td>2 byte unsigned integer</td>
1744 <td>Error code generated by the master. If the master fails, the
1745 slave will fail with the same error code, except for the error
1746 codes ER_DB_CREATE_EXISTS == 1007 and ER_DB_DROP_EXISTS == 1008.
1747 </td>
1748 </tr>
1749
1750 <tr>
1751 <td>status_vars_len</td>
1752 <td>2 byte unsigned integer</td>
1753 <td>The length of the status_vars block of the Body, in bytes. See
1754 @ref query_log_event_status_vars "below".
1755 </td>
1756 </tr>
1757 </table>
1758
1759 The Body has the following components:
1760
1761 <table>
1762 <caption>Body for Query_log_event</caption>
1763
1764 <tr>
1765 <th>Name</th>
1766 <th>Format</th>
1767 <th>Description</th>
1768 </tr>
1769
1770 <tr>
1771 <td>@anchor query_log_event_status_vars status_vars</td>
1772 <td>status_vars_len bytes</td>
1773 <td>Zero or more status variables. Each status variable consists
1774 of one byte identifying the variable stored, followed by the value
1775 of the variable. The possible variables are listed separately in
1776 the table @ref Table_query_log_event_status_vars "below". MySQL
1777 always writes events in the order defined below; however, it is
1778 capable of reading them in any order. </td>
1779 </tr>
1780
1781 <tr>
1782 <td>db</td>
1783 <td>db_len+1</td>
1784 <td>The currently selected database, as a null-terminated string.
1785
1786 (The trailing zero is redundant since the length is already known;
1787 it is db_len from Post-Header.)
1788 </td>
1789 </tr>
1790
1791 <tr>
1792 <td>query</td>
1793 <td>variable length string without trailing zero, extending to the
1794 end of the event (determined by the length field of the
1795 Common-Header)
1796 </td>
1797 <td>The SQL query.</td>
1798 </tr>
1799 </table>
1800
1801 The following table lists the status variables that may appear in
1802 the status_vars field.
1803
1804 @anchor Table_query_log_event_status_vars
1805 <table>
1806 <caption>Status variables for Query_log_event</caption>
1807
1808 <tr>
1809 <th>Status variable</th>
1810 <th>1 byte identifier</th>
1811 <th>Format</th>
1812 <th>Description</th>
1813 </tr>
1814
1815 <tr>
1816 <td>flags2</td>
1817 <td>Q_FLAGS2_CODE == 0</td>
1818 <td>4 byte bitfield</td>
1819 <td>The flags in @c thd->options, binary AND-ed with @c
1820 OPTIONS_WRITTEN_TO_BIN_LOG. The @c thd->options bitfield contains
1821 options for "SELECT". @c OPTIONS_WRITTEN identifies those options
1822 that need to be written to the binlog (not all do). Specifically,
1823 @c OPTIONS_WRITTEN_TO_BIN_LOG equals (@c OPTION_AUTO_IS_NULL | @c
1824 OPTION_NO_FOREIGN_KEY_CHECKS | @c OPTION_RELAXED_UNIQUE_CHECKS |
1825 @c OPTION_NOT_AUTOCOMMIT), or 0x0c084000 in hex.
1826
1827 These flags correspond to the SQL variables SQL_AUTO_IS_NULL,
1828 FOREIGN_KEY_CHECKS, UNIQUE_CHECKS, and AUTOCOMMIT, documented in
1829 the "SET Syntax" section of the MySQL Manual.
1830
1831 This field is always written to the binlog in version >= 5.0, and
1832 never written in version < 5.0.
1833 </td>
1834 </tr>
1835
1836 <tr>
1837 <td>sql_mode</td>
1838 <td>Q_SQL_MODE_CODE == 1</td>
1839 <td>8 byte bitfield</td>
1840 <td>The @c sql_mode variable. See the section "SQL Modes" in the
1841 MySQL manual, and see sql_priv.h for a list of the possible
1842 flags. Currently (2007-10-04), the following flags are available:
1843 <pre>
1844 MODE_REAL_AS_FLOAT==0x1
1845 MODE_PIPES_AS_CONCAT==0x2
1846 MODE_ANSI_QUOTES==0x4
1847 MODE_IGNORE_SPACE==0x8
1848 MODE_IGNORE_BAD_TABLE_OPTIONS==0x10
1849 MODE_ONLY_FULL_GROUP_BY==0x20
1850 MODE_NO_UNSIGNED_SUBTRACTION==0x40
1851 MODE_NO_DIR_IN_CREATE==0x80
1852 MODE_POSTGRESQL==0x100
1853 MODE_ORACLE==0x200
1854 MODE_MSSQL==0x400
1855 MODE_DB2==0x800
1856 MODE_MAXDB==0x1000
1857 MODE_NO_KEY_OPTIONS==0x2000
1858 MODE_NO_TABLE_OPTIONS==0x4000
1859 MODE_NO_FIELD_OPTIONS==0x8000
1860 MODE_MYSQL323==0x10000
1861 MODE_MYSQL323==0x20000
1862 MODE_MYSQL40==0x40000
1863 MODE_ANSI==0x80000
1864 MODE_NO_AUTO_VALUE_ON_ZERO==0x100000
1865 MODE_NO_BACKSLASH_ESCAPES==0x200000
1866 MODE_STRICT_TRANS_TABLES==0x400000
1867 MODE_STRICT_ALL_TABLES==0x800000
1868 MODE_NO_ZERO_IN_DATE==0x1000000
1869 MODE_NO_ZERO_DATE==0x2000000
1870 MODE_INVALID_DATES==0x4000000
1871 MODE_ERROR_FOR_DIVISION_BY_ZERO==0x8000000
1872 MODE_TRADITIONAL==0x10000000
1873 MODE_NO_AUTO_CREATE_USER==0x20000000
1874 MODE_HIGH_NOT_PRECEDENCE==0x40000000
1875 MODE_PAD_CHAR_TO_FULL_LENGTH==0x80000000
1876 </pre>
1877 All these flags are replicated from the server. However, all
1878 flags except @c MODE_NO_DIR_IN_CREATE are honored by the slave;
1879 the slave always preserves its old value of @c
1880 MODE_NO_DIR_IN_CREATE. For a rationale, see comment in
1881 @c Query_log_event::do_apply_event in @c log_event.cc.
1882
1883 This field is always written to the binlog.
1884 </td>
1885 </tr>
1886
1887 <tr>
1888 <td>catalog</td>
1889 <td>Q_CATALOG_NZ_CODE == 6</td>
1890 <td>Variable-length string: the length in bytes (1 byte) followed
1891 by the characters (at most 255 bytes)
1892 </td>
1893 <td>Stores the client's current catalog. Every database belongs
1894 to a catalog, the same way that every table belongs to a
1895 database. Currently, there is only one catalog, "std".
1896
1897 This field is written if the length of the catalog is > 0;
1898 otherwise it is not written.
1899 </td>
1900 </tr>
1901
1902 <tr>
1903 <td>auto_increment</td>
1904 <td>Q_AUTO_INCREMENT == 3</td>
1905 <td>two 2 byte unsigned integers, totally 2+2=4 bytes</td>
1906
1907 <td>The two variables auto_increment_increment and
1908 auto_increment_offset, in that order. For more information, see
1909 "System variables" in the MySQL manual.
1910
1911 This field is written if auto_increment > 1. Otherwise, it is not
1912 written.
1913 </td>
1914 </tr>
1915
1916 <tr>
1917 <td>charset</td>
1918 <td>Q_CHARSET_CODE == 4</td>
1919 <td>three 2 byte unsigned integers, totally 2+2+2=6 bytes</td>
1920 <td>The three variables character_set_client,
1921 collation_connection, and collation_server, in that order.
1922 character_set_client is a code identifying the character set and
1923 collation used by the client to encode the query.
1924 collation_connection identifies the character set and collation
1925 that the master converts the query to when it receives it; this is
1926 useful when comparing literal strings. collation_server is the
1927 default character set and collation used when a new database is
1928 created.
1929
1930 See also "Connection Character Sets and Collations" in the MySQL
1931 5.1 manual.
1932
1933 All three variables are codes identifying a (character set,
1934 collation) pair. To see which codes map to which pairs, run the
1935 query "SELECT id, character_set_name, collation_name FROM
1936 COLLATIONS".
1937
1938 Cf. Q_CHARSET_DATABASE_CODE below.
1939
1940 This field is always written.
1941 </td>
1942 </tr>
1943
1944 <tr>
1945 <td>time_zone</td>
1946 <td>Q_TIME_ZONE_CODE == 5</td>
1947 <td>Variable-length string: the length in bytes (1 byte) followed
1948 by the characters (at most 255 bytes).
1949 <td>The time_zone of the master.
1950
1951 See also "System Variables" and "MySQL Server Time Zone Support"
1952 in the MySQL manual.
1953
1954 This field is written if the length of the time zone string is >
1955 0; otherwise, it is not written.
1956 </td>
1957 </tr>
1958
1959 <tr>
1960 <td>lc_time_names_number</td>
1961 <td>Q_LC_TIME_NAMES_CODE == 7</td>
1962 <td>2 byte integer</td>
1963 <td>A code identifying a table of month and day names. The
1964 mapping from codes to languages is defined in @c sql_locale.cc.
1965
1966 This field is written if it is not 0, i.e., if the locale is not
1967 en_US.
1968 </td>
1969 </tr>
1970
1971 <tr>
1972 <td>charset_database_number</td>
1973 <td>Q_CHARSET_DATABASE_CODE == 8</td>
1974 <td>2 byte integer</td>
1975
1976 <td>The value of the collation_database system variable (in the
1977 source code stored in @c thd->variables.collation_database), which
1978 holds the code for a (character set, collation) pair as described
1979 above (see Q_CHARSET_CODE).
1980
1981 collation_database was used in old versions (???WHEN). Its value
1982 was loaded when issuing a "use db" query and could be changed by
1983 issuing a "SET collation_database=xxx" query. It used to affect
1984 the "LOAD DATA INFILE" and "CREATE TABLE" commands.
1985
1986 In newer versions, "CREATE TABLE" has been changed to take the
1987 character set from the database of the created table, rather than
1988 the character set of the current database. This makes a
1989 difference when creating a table in another database than the
1990 current one. "LOAD DATA INFILE" has not yet changed to do this,
1991 but there are plans to eventually do it, and to make
1992 collation_database read-only.
1993
1994 This field is written if it is not 0.
1995 </td>
1996 </tr>
1997 <tr>
1998 <td>table_map_for_update</td>
1999 <td>Q_TABLE_MAP_FOR_UPDATE_CODE == 9</td>
2000 <td>8 byte integer</td>
2001
2002 <td>The value of the table map that is to be updated by the
2003 multi-table update query statement. Every bit of this variable
2004 represents a table, and is set to 1 if the corresponding table is
2005 to be updated by this statement.
2006
2007 The value of this variable is set when executing a multi-table update
2008 statement and used by slave to apply filter rules without opening
2009 all the tables on slave. This is required because some tables may
2010 not exist on slave because of the filter rules.
2011 </td>
2012 </tr>
2013 </table>
2014
2015 @subsection Query_log_event_notes_on_previous_versions Notes on Previous Versions
2016
2017 * Status vars were introduced in version 5.0. To read earlier
2018 versions correctly, check the length of the Post-Header.
2019
2020 * The status variable Q_CATALOG_CODE == 2 existed in MySQL 5.0.x,
2021 where 0<=x<=3. It was identical to Q_CATALOG_CODE, except that the
2022 string had a trailing '\0'. The '\0' was removed in 5.0.4 since it
2023 was redundant (the string length is stored before the string). The
2024 Q_CATALOG_CODE will never be written by a new master, but can still
2025 be understood by a new slave.
2026
2027 * See Q_CHARSET_DATABASE_CODE in the table above.
2028
2029 * When adding new status vars, please don't forget to update the
2030 MAX_SIZE_LOG_EVENT_STATUS, and update function code_name
2031
2032 */
2033 class Query_log_event: public Log_event
2034 {
2035 LEX_CSTRING user;
2036 LEX_CSTRING host;
2037 protected:
2038 Log_event::Byte* data_buf;
2039 public:
2040 const char* query;
2041 const char* catalog;
2042 const char* db;
2043 /*
2044 If we already know the length of the query string
2045 we pass it with q_len, so we would not have to call strlen()
2046 otherwise, set it to 0, in which case, we compute it with strlen()
2047 */
2048 uint32 q_len;
2049 uint32 db_len;
2050 uint16 error_code;
2051 my_thread_id thread_id;
2052 /*
2053 For events created by Query_log_event::do_apply_event (and
2054 Load_log_event::do_apply_event()) we need the *original* thread
2055 id, to be able to log the event with the original (=master's)
2056 thread id (fix for BUG#1686).
2057 */
2058 ulong slave_proxy_id;
2059
2060 /*
2061 Binlog format 3 and 4 start to differ (as far as class members are
2062 concerned) from here.
2063 */
2064
2065 uint catalog_len; // <= 255 char; 0 means uninited
2066
2067 /*
2068 We want to be able to store a variable number of N-bit status vars:
2069 (generally N=32; but N=64 for SQL_MODE) a user may want to log the number
2070 of affected rows (for debugging) while another does not want to lose 4
2071 bytes in this.
2072 The storage on disk is the following:
2073 status_vars_len is part of the post-header,
2074 status_vars are in the variable-length part, after the post-header, before
2075 the db & query.
2076 status_vars on disk is a sequence of pairs (code, value) where 'code' means
2077 'sql_mode', 'affected' etc. Sometimes 'value' must be a short string, so
2078 its first byte is its length. For now the order of status vars is:
2079 flags2 - sql_mode - catalog - autoinc - charset
2080 We should add the same thing to Load_log_event, but in fact
2081 LOAD DATA INFILE is going to be logged with a new type of event (logging of
2082 the plain text query), so Load_log_event would be frozen, so no need. The
2083 new way of logging LOAD DATA INFILE would use a derived class of
2084 Query_log_event, so automatically benefit from the work already done for
2085 status variables in Query_log_event.
2086 */
2087 uint16 status_vars_len;
2088
2089 /*
2090 'flags2' is a second set of flags (on top of those in Log_event), for
2091 session variables. These are thd->options which is & against a mask
2092 (OPTIONS_WRITTEN_TO_BIN_LOG).
2093 flags2_inited helps make a difference between flags2==0 (3.23 or 4.x
2094 master, we don't know flags2, so use the slave server's global options) and
2095 flags2==0 (5.0 master, we know this has a meaning of flags all down which
2096 must influence the query).
2097 */
2098 bool flags2_inited;
2099 bool sql_mode_inited;
2100 bool charset_inited;
2101
2102 uint32 flags2;
2103 sql_mode_t sql_mode;
2104 ulong auto_increment_increment, auto_increment_offset;
2105 char charset[6];
2106 uint time_zone_len; /* 0 means uninited */
2107 const char *time_zone_str;
2108 uint lc_time_names_number; /* 0 means en_US */
2109 uint charset_database_number;
2110 /*
2111 map for tables that will be updated for a multi-table update query
2112 statement, for other query statements, this will be zero.
2113 */
2114 ulonglong table_map_for_update;
2115 /*
2116 Holds the original length of a Query_log_event that comes from a
2117 master of version < 5.0 (i.e., binlog_version < 4). When the IO
2118 thread writes the relay log, it augments the Query_log_event with a
2119 Q_MASTER_DATA_WRITTEN_CODE status_var that holds the original event
2120 length. This field is initialized to non-zero in the SQL thread when
2121 it reads this augmented event. SQL thread does not write
2122 Q_MASTER_DATA_WRITTEN_CODE to the slave's server binlog.
2123 */
2124 uint32 master_data_written;
2125
2126 #ifdef MYSQL_SERVER
2127
2128 Query_log_event(THD* thd_arg, const char* query_arg, size_t query_length,
2129 bool using_trans, bool direct, bool suppress_use, int error);
get_db()2130 const char* get_db() { return db; }
2131 #ifdef HAVE_REPLICATION
2132 void pack_info(Protocol* protocol);
2133 #endif /* HAVE_REPLICATION */
2134 #else
2135 bool print_query_header(IO_CACHE* file, PRINT_EVENT_INFO* print_event_info);
2136 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2137 #endif
2138
2139 Query_log_event();
2140 Query_log_event(const char* buf, uint event_len,
2141 const Format_description_log_event *description_event,
2142 Log_event_type event_type);
~Query_log_event()2143 ~Query_log_event()
2144 {
2145 if (data_buf)
2146 my_free(data_buf);
2147 }
get_type_code()2148 Log_event_type get_type_code() { return QUERY_EVENT; }
2149 static int dummy_event(String *packet, ulong ev_offset, enum enum_binlog_checksum_alg checksum_alg);
2150 static int begin_event(String *packet, ulong ev_offset, enum enum_binlog_checksum_alg checksum_alg);
2151 #ifdef MYSQL_SERVER
2152 bool write();
write_post_header_for_derived()2153 virtual bool write_post_header_for_derived() { return FALSE; }
2154 #endif
is_valid()2155 bool is_valid() const { return query != 0; }
2156
2157 /*
2158 Returns number of bytes additionally written to post header by derived
2159 events (so far it is only Execute_load_query event).
2160 */
get_post_header_size_for_derived()2161 virtual ulong get_post_header_size_for_derived() { return 0; }
2162 /* Writes derived event-specific part of post header. */
2163
2164 public: /* !!! Public in this patch to allow old usage */
2165 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2166 virtual enum_skip_reason do_shall_skip(rpl_group_info *rgi);
2167 virtual int do_apply_event(rpl_group_info *rgi);
2168
2169 int do_apply_event(rpl_group_info *rgi,
2170 const char *query_arg,
2171 uint32 q_len_arg);
2172 static bool peek_is_commit_rollback(const char *event_start,
2173 size_t event_len, enum enum_binlog_checksum_alg checksum_alg);
2174 #endif /* HAVE_REPLICATION */
2175 /*
2176 If true, the event always be applied by slave SQL thread or be printed by
2177 mysqlbinlog
2178 */
is_trans_keyword()2179 bool is_trans_keyword()
2180 {
2181 /*
2182 Before the patch for bug#50407, The 'SAVEPOINT and ROLLBACK TO'
2183 queries input by user was written into log events directly.
2184 So the keywords can be written in both upper case and lower case
2185 together, strncasecmp is used to check both cases. they also could be
2186 binlogged with comments in the front of these keywords. for examples:
2187 / * bla bla * / SAVEPOINT a;
2188 / * bla bla * / ROLLBACK TO a;
2189 but we don't handle these cases and after the patch, both quiries are
2190 binlogged in upper case with no comments.
2191 */
2192 return !strncmp(query, "BEGIN", q_len) ||
2193 !strncmp(query, "COMMIT", q_len) ||
2194 !strncasecmp(query, "SAVEPOINT", 9) ||
2195 !strncasecmp(query, "ROLLBACK", 8);
2196 }
is_begin()2197 virtual bool is_begin() { return !strcmp(query, "BEGIN"); }
is_commit()2198 virtual bool is_commit() { return !strcmp(query, "COMMIT"); }
is_rollback()2199 virtual bool is_rollback() { return !strcmp(query, "ROLLBACK"); }
2200 };
2201
2202 class Query_compressed_log_event:public Query_log_event{
2203 protected:
2204 Log_event::Byte* query_buf; // point to the uncompressed query
2205 public:
2206 Query_compressed_log_event(const char* buf, uint event_len,
2207 const Format_description_log_event *description_event,
2208 Log_event_type event_type);
~Query_compressed_log_event()2209 ~Query_compressed_log_event()
2210 {
2211 if (query_buf)
2212 my_free(query_buf);
2213 }
get_type_code()2214 Log_event_type get_type_code() { return QUERY_COMPRESSED_EVENT; }
2215
2216 /*
2217 the min length of log_bin_compress_min_len is 10,
2218 means that Begin/Commit/Rollback would never be compressed!
2219 */
is_begin()2220 virtual bool is_begin() { return false; }
is_commit()2221 virtual bool is_commit() { return false; }
is_rollback()2222 virtual bool is_rollback() { return false; }
2223 #ifdef MYSQL_SERVER
2224 Query_compressed_log_event(THD* thd_arg, const char* query_arg, ulong query_length,
2225 bool using_trans, bool direct, bool suppress_use, int error);
2226 virtual bool write();
2227 #endif
2228 };
2229
2230
2231 /*****************************************************************************
2232 sql_ex_info struct
2233 ****************************************************************************/
2234 struct sql_ex_info
2235 {
sql_ex_infosql_ex_info2236 sql_ex_info():
2237 cached_new_format(-1),
2238 field_term_len(0),
2239 enclosed_len(0),
2240 line_term_len(0),
2241 line_start_len(0),
2242 escaped_len(0),
2243 empty_flags(0)
2244 {} /* Remove gcc warning */
2245 const char* field_term;
2246 const char* enclosed;
2247 const char* line_term;
2248 const char* line_start;
2249 const char* escaped;
2250 int cached_new_format;
2251 uint8 field_term_len,enclosed_len,line_term_len,line_start_len, escaped_len;
2252 char opt_flags;
2253 char empty_flags;
2254
2255 // store in new format even if old is possible
force_new_formatsql_ex_info2256 void force_new_format() { cached_new_format = 1;}
data_sizesql_ex_info2257 int data_size()
2258 {
2259 return (new_format() ?
2260 field_term_len + enclosed_len + line_term_len +
2261 line_start_len + escaped_len + 6 : 7);
2262 }
2263 bool write_data(Log_event_writer *writer);
2264 const char* init(const char* buf, const char* buf_end, bool use_new_format);
new_formatsql_ex_info2265 bool new_format()
2266 {
2267 return ((cached_new_format != -1) ? cached_new_format :
2268 (cached_new_format=(field_term_len > 1 ||
2269 enclosed_len > 1 ||
2270 line_term_len > 1 || line_start_len > 1 ||
2271 escaped_len > 1)));
2272 }
2273 };
2274
2275 /**
2276 @class Load_log_event
2277
2278 This log event corresponds to a "LOAD DATA INFILE" SQL query on the
2279 following form:
2280
2281 @verbatim
2282 (1) USE db;
2283 (2) LOAD DATA [CONCURRENT] [LOCAL] INFILE 'file_name'
2284 (3) [REPLACE | IGNORE]
2285 (4) INTO TABLE 'table_name'
2286 (5) [FIELDS
2287 (6) [TERMINATED BY 'field_term']
2288 (7) [[OPTIONALLY] ENCLOSED BY 'enclosed']
2289 (8) [ESCAPED BY 'escaped']
2290 (9) ]
2291 (10) [LINES
2292 (11) [TERMINATED BY 'line_term']
2293 (12) [LINES STARTING BY 'line_start']
2294 (13) ]
2295 (14) [IGNORE skip_lines LINES]
2296 (15) (field_1, field_2, ..., field_n)@endverbatim
2297
2298 @section Load_log_event_binary_format Binary Format
2299
2300 The Post-Header consists of the following six components.
2301
2302 <table>
2303 <caption>Post-Header for Load_log_event</caption>
2304
2305 <tr>
2306 <th>Name</th>
2307 <th>Format</th>
2308 <th>Description</th>
2309 </tr>
2310
2311 <tr>
2312 <td>slave_proxy_id</td>
2313 <td>4 byte unsigned integer</td>
2314 <td>An integer identifying the client thread that issued the
2315 query. The id is unique per server. (Note, however, that two
2316 threads on different servers may have the same slave_proxy_id.)
2317 This is used when a client thread creates a temporary table local
2318 to the client. The slave_proxy_id is used to distinguish
2319 temporary tables that belong to different clients.
2320 </td>
2321 </tr>
2322
2323 <tr>
2324 <td>exec_time</td>
2325 <td>4 byte unsigned integer</td>
2326 <td>The time from when the query started to when it was logged in
2327 the binlog, in seconds.</td>
2328 </tr>
2329
2330 <tr>
2331 <td>skip_lines</td>
2332 <td>4 byte unsigned integer</td>
2333 <td>The number on line (14) above, if present, or 0 if line (14)
2334 is left out.
2335 </td>
2336 </tr>
2337
2338 <tr>
2339 <td>table_name_len</td>
2340 <td>1 byte unsigned integer</td>
2341 <td>The length of 'table_name' on line (4) above.</td>
2342 </tr>
2343
2344 <tr>
2345 <td>db_len</td>
2346 <td>1 byte unsigned integer</td>
2347 <td>The length of 'db' on line (1) above.</td>
2348 </tr>
2349
2350 <tr>
2351 <td>num_fields</td>
2352 <td>4 byte unsigned integer</td>
2353 <td>The number n of fields on line (15) above.</td>
2354 </tr>
2355 </table>
2356
2357 The Body contains the following components.
2358
2359 <table>
2360 <caption>Body of Load_log_event</caption>
2361
2362 <tr>
2363 <th>Name</th>
2364 <th>Format</th>
2365 <th>Description</th>
2366 </tr>
2367
2368 <tr>
2369 <td>sql_ex</td>
2370 <td>variable length</td>
2371
2372 <td>Describes the part of the query on lines (3) and
2373 (5)–(13) above. More precisely, it stores the five strings
2374 (on lines) field_term (6), enclosed (7), escaped (8), line_term
2375 (11), and line_start (12); as well as a bitfield indicating the
2376 presence of the keywords REPLACE (3), IGNORE (3), and OPTIONALLY
2377 (7).
2378
2379 The data is stored in one of two formats, called "old" and "new".
2380 The type field of Common-Header determines which of these two
2381 formats is used: type LOAD_EVENT means that the old format is
2382 used, and type NEW_LOAD_EVENT means that the new format is used.
2383 When MySQL writes a Load_log_event, it uses the new format if at
2384 least one of the five strings is two or more bytes long.
2385 Otherwise (i.e., if all strings are 0 or 1 bytes long), the old
2386 format is used.
2387
2388 The new and old format differ in the way the five strings are
2389 stored.
2390
2391 <ul>
2392 <li> In the new format, the strings are stored in the order
2393 field_term, enclosed, escaped, line_term, line_start. Each string
2394 consists of a length (1 byte), followed by a sequence of
2395 characters (0-255 bytes). Finally, a boolean combination of the
2396 following flags is stored in 1 byte: REPLACE_FLAG==0x4,
2397 IGNORE_FLAG==0x8, and OPT_ENCLOSED_FLAG==0x2. If a flag is set,
2398 it indicates the presence of the corresponding keyword in the SQL
2399 query.
2400
2401 <li> In the old format, we know that each string has length 0 or
2402 1. Therefore, only the first byte of each string is stored. The
2403 order of the strings is the same as in the new format. These five
2404 bytes are followed by the same 1 byte bitfield as in the new
2405 format. Finally, a 1 byte bitfield called empty_flags is stored.
2406 The low 5 bits of empty_flags indicate which of the five strings
2407 have length 0. For each of the following flags that is set, the
2408 corresponding string has length 0; for the flags that are not set,
2409 the string has length 1: FIELD_TERM_EMPTY==0x1,
2410 ENCLOSED_EMPTY==0x2, LINE_TERM_EMPTY==0x4, LINE_START_EMPTY==0x8,
2411 ESCAPED_EMPTY==0x10.
2412 </ul>
2413
2414 Thus, the size of the new format is 6 bytes + the sum of the sizes
2415 of the five strings. The size of the old format is always 7
2416 bytes.
2417 </td>
2418 </tr>
2419
2420 <tr>
2421 <td>field_lens</td>
2422 <td>num_fields 1 byte unsigned integers</td>
2423 <td>An array of num_fields integers representing the length of
2424 each field in the query. (num_fields is from the Post-Header).
2425 </td>
2426 </tr>
2427
2428 <tr>
2429 <td>fields</td>
2430 <td>num_fields null-terminated strings</td>
2431 <td>An array of num_fields null-terminated strings, each
2432 representing a field in the query. (The trailing zero is
2433 redundant, since the length are stored in the num_fields array.)
2434 The total length of all strings equals to the sum of all
2435 field_lens, plus num_fields bytes for all the trailing zeros.
2436 </td>
2437 </tr>
2438
2439 <tr>
2440 <td>table_name</td>
2441 <td>null-terminated string of length table_len+1 bytes</td>
2442 <td>The 'table_name' from the query, as a null-terminated string.
2443 (The trailing zero is actually redundant since the table_len is
2444 known from Post-Header.)
2445 </td>
2446 </tr>
2447
2448 <tr>
2449 <td>db</td>
2450 <td>null-terminated string of length db_len+1 bytes</td>
2451 <td>The 'db' from the query, as a null-terminated string.
2452 (The trailing zero is actually redundant since the db_len is known
2453 from Post-Header.)
2454 </td>
2455 </tr>
2456
2457 <tr>
2458 <td>file_name</td>
2459 <td>variable length string without trailing zero, extending to the
2460 end of the event (determined by the length field of the
2461 Common-Header)
2462 </td>
2463 <td>The 'file_name' from the query.
2464 </td>
2465 </tr>
2466
2467 </table>
2468
2469 @subsection Load_log_event_notes_on_previous_versions Notes on Previous Versions
2470
2471 This event type is understood by current versions, but only
2472 generated by MySQL 3.23 and earlier.
2473 */
2474 class Load_log_event: public Log_event
2475 {
2476 private:
2477 protected:
2478 int copy_log_event(const char *buf, ulong event_len,
2479 int body_offset,
2480 const Format_description_log_event* description_event);
2481
2482 public:
2483 bool print_query(THD *thd, bool need_db, const char *cs, String *buf,
2484 my_off_t *fn_start, my_off_t *fn_end,
2485 const char *qualify_db);
2486 my_thread_id thread_id;
2487 ulong slave_proxy_id;
2488 uint32 table_name_len;
2489 /*
2490 No need to have a catalog, as these events can only come from 4.x.
2491 TODO: this may become false if Dmitri pushes his new LOAD DATA INFILE in
2492 5.0 only (not in 4.x).
2493 */
2494 uint32 db_len;
2495 uint32 fname_len;
2496 uint32 num_fields;
2497 const char* fields;
2498 const uchar* field_lens;
2499 uint32 field_block_len;
2500
2501 const char* table_name;
2502 const char* db;
2503 const char* fname;
2504 uint32 skip_lines;
2505 sql_ex_info sql_ex;
2506 bool local_fname;
2507 /**
2508 Indicates that this event corresponds to LOAD DATA CONCURRENT,
2509
2510 @note Since Load_log_event event coming from the binary log
2511 lacks information whether LOAD DATA on master was concurrent
2512 or not, this flag is only set to TRUE for an auxiliary
2513 Load_log_event object which is used in mysql_load() to
2514 re-construct LOAD DATA statement from function parameters,
2515 for logging.
2516 */
2517 bool is_concurrent;
2518
2519 /* fname doesn't point to memory inside Log_event::temp_buf */
set_fname_outside_temp_buf(const char * afname,size_t alen)2520 void set_fname_outside_temp_buf(const char *afname, size_t alen)
2521 {
2522 fname= afname;
2523 fname_len= (uint)alen;
2524 local_fname= TRUE;
2525 }
2526 /* fname doesn't point to memory inside Log_event::temp_buf */
check_fname_outside_temp_buf()2527 int check_fname_outside_temp_buf()
2528 {
2529 return local_fname;
2530 }
2531
2532 #ifdef MYSQL_SERVER
2533 String field_lens_buf;
2534 String fields_buf;
2535
2536 Load_log_event(THD* thd, const sql_exchange* ex, const char* db_arg,
2537 const char* table_name_arg,
2538 List<Item>& fields_arg,
2539 bool is_concurrent_arg,
2540 enum enum_duplicates handle_dup, bool ignore,
2541 bool using_trans);
2542 void set_fields(const char* db, List<Item> &fields_arg,
2543 Name_resolution_context *context);
get_db()2544 const char* get_db() { return db; }
2545 #ifdef HAVE_REPLICATION
2546 void pack_info(Protocol* protocol);
2547 #endif /* HAVE_REPLICATION */
2548 #else
2549 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2550 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info, bool commented);
2551 #endif
2552
2553 /*
2554 Note that for all the events related to LOAD DATA (Load_log_event,
2555 Create_file/Append/Exec/Delete, we pass description_event; however as
2556 logging of LOAD DATA is going to be changed in 4.1 or 5.0, this is only used
2557 for the common_header_len (post_header_len will not be changed).
2558 */
2559 Load_log_event(const char* buf, uint event_len,
2560 const Format_description_log_event* description_event);
~Load_log_event()2561 ~Load_log_event()
2562 {}
get_type_code()2563 Log_event_type get_type_code()
2564 {
2565 return sql_ex.new_format() ? NEW_LOAD_EVENT: LOAD_EVENT;
2566 }
2567 #ifdef MYSQL_SERVER
2568 bool write_data_header();
2569 bool write_data_body();
2570 #endif
is_valid()2571 bool is_valid() const { return table_name != 0; }
get_data_size()2572 int get_data_size()
2573 {
2574 return (table_name_len + db_len + 2 + fname_len
2575 + LOAD_HEADER_LEN
2576 + sql_ex.data_size() + field_block_len + num_fields);
2577 }
2578
2579 public: /* !!! Public in this patch to allow old usage */
2580 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
do_apply_event(rpl_group_info * rgi)2581 virtual int do_apply_event(rpl_group_info *rgi)
2582 {
2583 return do_apply_event(thd->slave_net,rgi,0);
2584 }
2585
2586 int do_apply_event(NET *net, rpl_group_info *rgi,
2587 bool use_rli_only_for_errors);
2588 #endif
2589 };
2590
2591 /**
2592 @class Start_log_event_v3
2593
2594 Start_log_event_v3 is the Start_log_event of binlog format 3 (MySQL 3.23 and
2595 4.x).
2596
2597 Format_description_log_event derives from Start_log_event_v3; it is
2598 the Start_log_event of binlog format 4 (MySQL 5.0), that is, the
2599 event that describes the other events' Common-Header/Post-Header
2600 lengths. This event is sent by MySQL 5.0 whenever it starts sending
2601 a new binlog if the requested position is >4 (otherwise if ==4 the
2602 event will be sent naturally).
2603
2604 @section Start_log_event_v3_binary_format Binary Format
2605 */
2606 class Start_log_event_v3: public Log_event
2607 {
2608 public:
2609 /*
2610 If this event is at the start of the first binary log since server
2611 startup 'created' should be the timestamp when the event (and the
2612 binary log) was created. In the other case (i.e. this event is at
2613 the start of a binary log created by FLUSH LOGS or automatic
2614 rotation), 'created' should be 0. This "trick" is used by MySQL
2615 >=4.0.14 slaves to know whether they must drop stale temporary
2616 tables and whether they should abort unfinished transaction.
2617
2618 Note that when 'created'!=0, it is always equal to the event's
2619 timestamp; indeed Start_log_event is written only in log.cc where
2620 the first constructor below is called, in which 'created' is set
2621 to 'when'. So in fact 'created' is a useless variable. When it is
2622 0 we can read the actual value from timestamp ('when') and when it
2623 is non-zero we can read the same value from timestamp
2624 ('when'). Conclusion:
2625 - we use timestamp to print when the binlog was created.
2626 - we use 'created' only to know if this is a first binlog or not.
2627 In 3.23.57 we did not pay attention to this identity, so mysqlbinlog in
2628 3.23.57 does not print 'created the_date' if created was zero. This is now
2629 fixed.
2630 */
2631 time_t created;
2632 uint16 binlog_version;
2633 char server_version[ST_SERVER_VER_LEN];
2634 /*
2635 We set this to 1 if we don't want to have the created time in the log,
2636 which is the case when we rollover to a new log.
2637 */
2638 bool dont_set_created;
2639
2640 #ifdef MYSQL_SERVER
2641 Start_log_event_v3();
2642 #ifdef HAVE_REPLICATION
2643 void pack_info(Protocol* protocol);
2644 #endif /* HAVE_REPLICATION */
2645 #else
Start_log_event_v3()2646 Start_log_event_v3() {}
2647 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2648 #endif
2649
2650 Start_log_event_v3(const char* buf, uint event_len,
2651 const Format_description_log_event* description_event);
~Start_log_event_v3()2652 ~Start_log_event_v3() {}
get_type_code()2653 Log_event_type get_type_code() { return START_EVENT_V3;}
get_header_len(my_off_t l)2654 my_off_t get_header_len(my_off_t l __attribute__((unused)))
2655 { return LOG_EVENT_MINIMAL_HEADER_LEN; }
2656 #ifdef MYSQL_SERVER
2657 bool write();
2658 #endif
is_valid()2659 bool is_valid() const { return server_version[0] != 0; }
get_data_size()2660 int get_data_size()
2661 {
2662 return START_V3_HEADER_LEN; //no variable-sized part
2663 }
2664
2665 protected:
2666 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2667 virtual int do_apply_event(rpl_group_info *rgi);
do_shall_skip(rpl_group_info *)2668 virtual enum_skip_reason do_shall_skip(rpl_group_info*)
2669 {
2670 /*
2671 Events from ourself should be skipped, but they should not
2672 decrease the slave skip counter.
2673 */
2674 if (this->server_id == global_system_variables.server_id)
2675 return Log_event::EVENT_SKIP_IGNORE;
2676 else
2677 return Log_event::EVENT_SKIP_NOT;
2678 }
2679 #endif
2680 };
2681
2682 /**
2683 @class Start_encryption_log_event
2684
2685 Start_encryption_log_event marks the beginning of encrypted data (all events
2686 after this event are encrypted).
2687
2688 It contains the cryptographic scheme used for the encryption as well as any
2689 data required to decrypt (except the actual key).
2690
2691 For binlog cryptoscheme 1: key version, and nonce for iv generation.
2692 */
2693 class Start_encryption_log_event : public Log_event
2694 {
2695 public:
2696 #ifdef MYSQL_SERVER
Start_encryption_log_event(uint crypto_scheme_arg,uint key_version_arg,const uchar * nonce_arg)2697 Start_encryption_log_event(uint crypto_scheme_arg, uint key_version_arg,
2698 const uchar* nonce_arg)
2699 : crypto_scheme(crypto_scheme_arg), key_version(key_version_arg)
2700 {
2701 cache_type = EVENT_NO_CACHE;
2702 DBUG_ASSERT(crypto_scheme == 1);
2703 memcpy(nonce, nonce_arg, BINLOG_NONCE_LENGTH);
2704 }
2705
write_data_body()2706 bool write_data_body()
2707 {
2708 uchar scheme_buf= crypto_scheme;
2709 uchar key_version_buf[BINLOG_KEY_VERSION_LENGTH];
2710 int4store(key_version_buf, key_version);
2711 return write_data(&scheme_buf, sizeof(scheme_buf)) ||
2712 write_data(key_version_buf, sizeof(key_version_buf)) ||
2713 write_data(nonce, BINLOG_NONCE_LENGTH);
2714 }
2715 #else
2716 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2717 #endif
2718
2719 Start_encryption_log_event(
2720 const char* buf, uint event_len,
2721 const Format_description_log_event* description_event);
2722
is_valid()2723 bool is_valid() const { return crypto_scheme == 1; }
2724
get_type_code()2725 Log_event_type get_type_code() { return START_ENCRYPTION_EVENT; }
2726
get_data_size()2727 int get_data_size()
2728 {
2729 return BINLOG_CRYPTO_SCHEME_LENGTH + BINLOG_KEY_VERSION_LENGTH +
2730 BINLOG_NONCE_LENGTH;
2731 }
2732
2733 uint crypto_scheme;
2734 uint key_version;
2735 uchar nonce[BINLOG_NONCE_LENGTH];
2736
2737 protected:
2738 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2739 virtual int do_apply_event(rpl_group_info* rgi);
2740 virtual int do_update_pos(rpl_group_info *rgi);
do_shall_skip(rpl_group_info * rgi)2741 virtual enum_skip_reason do_shall_skip(rpl_group_info* rgi)
2742 {
2743 return Log_event::EVENT_SKIP_NOT;
2744 }
2745 #endif
2746
2747 };
2748
2749
2750 class Version
2751 {
2752 protected:
2753 uchar m_ver[3];
cmp(const Version & other)2754 int cmp(const Version &other) const
2755 {
2756 return memcmp(m_ver, other.m_ver, 3);
2757 }
2758 public:
Version()2759 Version()
2760 {
2761 m_ver[0]= m_ver[1]= m_ver[2]= '\0';
2762 }
Version(uchar v0,uchar v1,uchar v2)2763 Version(uchar v0, uchar v1, uchar v2)
2764 {
2765 m_ver[0]= v0;
2766 m_ver[1]= v1;
2767 m_ver[2]= v2;
2768 }
2769 Version(const char *version, const char **endptr);
2770 const uchar& operator [] (size_t i) const
2771 {
2772 DBUG_ASSERT(i < 3);
2773 return m_ver[i];
2774 }
2775 bool operator<(const Version &other) const { return cmp(other) < 0; }
2776 bool operator>(const Version &other) const { return cmp(other) > 0; }
2777 bool operator<=(const Version &other) const { return cmp(other) <= 0; }
2778 bool operator>=(const Version &other) const { return cmp(other) >= 0; }
2779 };
2780
2781
2782 /**
2783 @class Format_description_log_event
2784
2785 For binlog version 4.
2786 This event is saved by threads which read it, as they need it for future
2787 use (to decode the ordinary events).
2788
2789 @section Format_description_log_event_binary_format Binary Format
2790 */
2791
2792 class Format_description_log_event: public Start_log_event_v3
2793 {
2794 public:
2795 /*
2796 The size of the fixed header which _all_ events have
2797 (for binlogs written by this version, this is equal to
2798 LOG_EVENT_HEADER_LEN), except FORMAT_DESCRIPTION_EVENT and ROTATE_EVENT
2799 (those have a header of size LOG_EVENT_MINIMAL_HEADER_LEN).
2800 */
2801 uint8 common_header_len;
2802 uint8 number_of_event_types;
2803 /*
2804 The list of post-headers' lengths followed
2805 by the checksum alg description byte
2806 */
2807 uint8 *post_header_len;
2808 class master_version_split: public Version {
2809 public:
2810 enum {KIND_MYSQL, KIND_MARIADB};
2811 int kind;
master_version_split()2812 master_version_split() :kind(KIND_MARIADB) { }
2813 master_version_split(const char *version);
version_is_valid()2814 bool version_is_valid() const
2815 {
2816 /* It is invalid only when all version numbers are 0 */
2817 return !(m_ver[0] == 0 && m_ver[1] == 0 && m_ver[2] == 0);
2818 }
2819 };
2820 master_version_split server_version_split;
2821 const uint8 *event_type_permutation;
2822
2823 Format_description_log_event(uint8 binlog_ver, const char* server_ver=0);
2824 Format_description_log_event(const char* buf, uint event_len,
2825 const Format_description_log_event
2826 *description_event);
~Format_description_log_event()2827 ~Format_description_log_event()
2828 {
2829 my_free(post_header_len);
2830 }
get_type_code()2831 Log_event_type get_type_code() { return FORMAT_DESCRIPTION_EVENT;}
2832 #ifdef MYSQL_SERVER
2833 bool write();
2834 #endif
header_is_valid()2835 bool header_is_valid() const
2836 {
2837 return ((common_header_len >= ((binlog_version==1) ? OLD_HEADER_LEN :
2838 LOG_EVENT_MINIMAL_HEADER_LEN)) &&
2839 (post_header_len != NULL));
2840 }
2841
is_valid()2842 bool is_valid() const
2843 {
2844 return header_is_valid() && server_version_split.version_is_valid();
2845 }
2846
get_data_size()2847 int get_data_size()
2848 {
2849 /*
2850 The vector of post-header lengths is considered as part of the
2851 post-header, because in a given version it never changes (contrary to the
2852 query in a Query_log_event).
2853 */
2854 return FORMAT_DESCRIPTION_HEADER_LEN;
2855 }
2856
2857 Binlog_crypt_data crypto_data;
2858 bool start_decryption(Start_encryption_log_event* sele);
copy_crypto_data(const Format_description_log_event * o)2859 void copy_crypto_data(const Format_description_log_event* o)
2860 {
2861 crypto_data= o->crypto_data;
2862 }
reset_crypto()2863 void reset_crypto()
2864 {
2865 crypto_data.scheme= 0;
2866 }
2867
2868 void calc_server_version_split();
2869 static bool is_version_before_checksum(const master_version_split *version_split);
2870 protected:
2871 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2872 virtual int do_apply_event(rpl_group_info *rgi);
2873 virtual int do_update_pos(rpl_group_info *rgi);
2874 virtual enum_skip_reason do_shall_skip(rpl_group_info *rgi);
2875 #endif
2876 };
2877
2878
2879 /**
2880 @class Intvar_log_event
2881
2882 An Intvar_log_event will be created just before a Query_log_event,
2883 if the query uses one of the variables LAST_INSERT_ID or INSERT_ID.
2884 Each Intvar_log_event holds the value of one of these variables.
2885
2886 @section Intvar_log_event_binary_format Binary Format
2887
2888 The Post-Header for this event type is empty. The Body has two
2889 components:
2890
2891 <table>
2892 <caption>Body for Intvar_log_event</caption>
2893
2894 <tr>
2895 <th>Name</th>
2896 <th>Format</th>
2897 <th>Description</th>
2898 </tr>
2899
2900 <tr>
2901 <td>type</td>
2902 <td>1 byte enumeration</td>
2903 <td>One byte identifying the type of variable stored. Currently,
2904 two identifiers are supported: LAST_INSERT_ID_EVENT==1 and
2905 INSERT_ID_EVENT==2.
2906 </td>
2907 </tr>
2908
2909 <tr>
2910 <td>value</td>
2911 <td>8 byte unsigned integer</td>
2912 <td>The value of the variable.</td>
2913 </tr>
2914
2915 </table>
2916 */
2917 class Intvar_log_event: public Log_event
2918 {
2919 public:
2920 ulonglong val;
2921 uchar type;
2922
2923 #ifdef MYSQL_SERVER
Intvar_log_event(THD * thd_arg,uchar type_arg,ulonglong val_arg,bool using_trans,bool direct)2924 Intvar_log_event(THD* thd_arg,uchar type_arg, ulonglong val_arg,
2925 bool using_trans, bool direct)
2926 :Log_event(thd_arg,0,using_trans),val(val_arg),type(type_arg)
2927 {
2928 if (direct)
2929 cache_type= Log_event::EVENT_NO_CACHE;
2930 }
2931 #ifdef HAVE_REPLICATION
2932 void pack_info(Protocol* protocol);
2933 #endif /* HAVE_REPLICATION */
2934 #else
2935 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2936 #endif
2937
2938 Intvar_log_event(const char* buf,
2939 const Format_description_log_event *description_event);
~Intvar_log_event()2940 ~Intvar_log_event() {}
get_type_code()2941 Log_event_type get_type_code() { return INTVAR_EVENT;}
2942 const char* get_var_type_name();
get_data_size()2943 int get_data_size() { return 9; /* sizeof(type) + sizeof(val) */;}
2944 #ifdef MYSQL_SERVER
2945 bool write();
2946 #endif
is_valid()2947 bool is_valid() const { return 1; }
is_part_of_group()2948 bool is_part_of_group() { return 1; }
2949
2950 private:
2951 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2952 virtual int do_apply_event(rpl_group_info *rgi);
2953 virtual int do_update_pos(rpl_group_info *rgi);
2954 virtual enum_skip_reason do_shall_skip(rpl_group_info *rgi);
2955 #endif
2956 };
2957
2958
2959 /**
2960 @class Rand_log_event
2961
2962 Logs random seed used by the next RAND(), and by PASSWORD() in 4.1.0.
2963 4.1.1 does not need it (it's repeatable again) so this event needn't be
2964 written in 4.1.1 for PASSWORD() (but the fact that it is written is just a
2965 waste, it does not cause bugs).
2966
2967 The state of the random number generation consists of 128 bits,
2968 which are stored internally as two 64-bit numbers.
2969
2970 @section Rand_log_event_binary_format Binary Format
2971
2972 The Post-Header for this event type is empty. The Body has two
2973 components:
2974
2975 <table>
2976 <caption>Body for Rand_log_event</caption>
2977
2978 <tr>
2979 <th>Name</th>
2980 <th>Format</th>
2981 <th>Description</th>
2982 </tr>
2983
2984 <tr>
2985 <td>seed1</td>
2986 <td>8 byte unsigned integer</td>
2987 <td>64 bit random seed1.</td>
2988 </tr>
2989
2990 <tr>
2991 <td>seed2</td>
2992 <td>8 byte unsigned integer</td>
2993 <td>64 bit random seed2.</td>
2994 </tr>
2995 </table>
2996 */
2997
2998 class Rand_log_event: public Log_event
2999 {
3000 public:
3001 ulonglong seed1;
3002 ulonglong seed2;
3003
3004 #ifdef MYSQL_SERVER
Rand_log_event(THD * thd_arg,ulonglong seed1_arg,ulonglong seed2_arg,bool using_trans,bool direct)3005 Rand_log_event(THD* thd_arg, ulonglong seed1_arg, ulonglong seed2_arg,
3006 bool using_trans, bool direct)
3007 :Log_event(thd_arg,0,using_trans),seed1(seed1_arg),seed2(seed2_arg)
3008 {
3009 if (direct)
3010 cache_type= Log_event::EVENT_NO_CACHE;
3011 }
3012 #ifdef HAVE_REPLICATION
3013 void pack_info(Protocol* protocol);
3014 #endif /* HAVE_REPLICATION */
3015 #else
3016 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
3017 #endif
3018
3019 Rand_log_event(const char* buf,
3020 const Format_description_log_event *description_event);
~Rand_log_event()3021 ~Rand_log_event() {}
get_type_code()3022 Log_event_type get_type_code() { return RAND_EVENT;}
get_data_size()3023 int get_data_size() { return 16; /* sizeof(ulonglong) * 2*/ }
3024 #ifdef MYSQL_SERVER
3025 bool write();
3026 #endif
is_valid()3027 bool is_valid() const { return 1; }
is_part_of_group()3028 bool is_part_of_group() { return 1; }
3029
3030 private:
3031 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3032 virtual int do_apply_event(rpl_group_info *rgi);
3033 virtual int do_update_pos(rpl_group_info *rgi);
3034 virtual enum_skip_reason do_shall_skip(rpl_group_info *rgi);
3035 #endif
3036 };
3037
3038 /**
3039 @class Xid_log_event
3040
3041 Logs xid of the transaction-to-be-committed in the 2pc protocol.
3042 Has no meaning in replication, slaves ignore it.
3043
3044 @section Xid_log_event_binary_format Binary Format
3045 */
3046 #ifdef MYSQL_CLIENT
3047 typedef ulonglong my_xid; // this line is the same as in handler.h
3048 #endif
3049
3050 class Xid_log_event: public Log_event
3051 {
3052 public:
3053 my_xid xid;
3054
3055 #ifdef MYSQL_SERVER
Xid_log_event(THD * thd_arg,my_xid x,bool direct)3056 Xid_log_event(THD* thd_arg, my_xid x, bool direct):
3057 Log_event(thd_arg, 0, TRUE), xid(x)
3058 {
3059 if (direct)
3060 cache_type= Log_event::EVENT_NO_CACHE;
3061 }
3062 #ifdef HAVE_REPLICATION
3063 void pack_info(Protocol* protocol);
3064 #endif /* HAVE_REPLICATION */
3065 #else
3066 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
3067 #endif
3068
3069 Xid_log_event(const char* buf,
3070 const Format_description_log_event *description_event);
~Xid_log_event()3071 ~Xid_log_event() {}
get_type_code()3072 Log_event_type get_type_code() { return XID_EVENT;}
get_data_size()3073 int get_data_size() { return sizeof(xid); }
3074 #ifdef MYSQL_SERVER
3075 bool write();
3076 #endif
is_valid()3077 bool is_valid() const { return 1; }
3078
3079 private:
3080 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3081 virtual int do_apply_event(rpl_group_info *rgi);
3082 enum_skip_reason do_shall_skip(rpl_group_info *rgi);
3083 #endif
3084 };
3085
3086 /**
3087 @class User_var_log_event
3088
3089 Every time a query uses the value of a user variable, a User_var_log_event is
3090 written before the Query_log_event, to set the user variable.
3091
3092 @section User_var_log_event_binary_format Binary Format
3093 */
3094
3095 class User_var_log_event: public Log_event
3096 {
3097 public:
3098 enum {
3099 UNDEF_F= 0,
3100 UNSIGNED_F= 1
3101 };
3102 const char *name;
3103 size_t name_len;
3104 const char *val;
3105 size_t val_len;
3106 Item_result type;
3107 uint charset_number;
3108 bool is_null;
3109 uchar flags;
3110 #ifdef MYSQL_SERVER
3111 bool deferred;
3112 query_id_t query_id;
User_var_log_event(THD * thd_arg,const char * name_arg,size_t name_len_arg,const char * val_arg,size_t val_len_arg,Item_result type_arg,uint charset_number_arg,uchar flags_arg,bool using_trans,bool direct)3113 User_var_log_event(THD* thd_arg, const char *name_arg, size_t name_len_arg,
3114 const char *val_arg, size_t val_len_arg, Item_result type_arg,
3115 uint charset_number_arg, uchar flags_arg,
3116 bool using_trans, bool direct)
3117 :Log_event(thd_arg, 0, using_trans),
3118 name(name_arg), name_len(name_len_arg), val(val_arg),
3119 val_len(val_len_arg), type(type_arg), charset_number(charset_number_arg),
3120 flags(flags_arg), deferred(false)
3121 {
3122 is_null= !val;
3123 if (direct)
3124 cache_type= Log_event::EVENT_NO_CACHE;
3125 }
3126 void pack_info(Protocol* protocol);
3127 #else
3128 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
3129 #endif
3130
3131 User_var_log_event(const char* buf, uint event_len,
3132 const Format_description_log_event *description_event);
~User_var_log_event()3133 ~User_var_log_event() {}
get_type_code()3134 Log_event_type get_type_code() { return USER_VAR_EVENT;}
3135 #ifdef MYSQL_SERVER
3136 bool write();
3137 /*
3138 Getter and setter for deferred User-event.
3139 Returns true if the event is not applied directly
3140 and which case the applier adjusts execution path.
3141 */
is_deferred()3142 bool is_deferred() { return deferred; }
3143 /*
3144 In case of the deferred applying the variable instance is flagged
3145 and the parsing time query id is stored to be used at applying time.
3146 */
set_deferred(query_id_t qid)3147 void set_deferred(query_id_t qid) { deferred= true; query_id= qid; }
3148 #endif
is_valid()3149 bool is_valid() const { return name != 0; }
is_part_of_group()3150 bool is_part_of_group() { return 1; }
3151
3152 private:
3153 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3154 virtual int do_apply_event(rpl_group_info *rgi);
3155 virtual int do_update_pos(rpl_group_info *rgi);
3156 virtual enum_skip_reason do_shall_skip(rpl_group_info *rgi);
3157 #endif
3158 };
3159
3160
3161 /**
3162 @class Stop_log_event
3163
3164 @section Stop_log_event_binary_format Binary Format
3165
3166 The Post-Header and Body for this event type are empty; it only has
3167 the Common-Header.
3168 */
3169 class Stop_log_event: public Log_event
3170 {
3171 public:
3172 #ifdef MYSQL_SERVER
Stop_log_event()3173 Stop_log_event() :Log_event()
3174 {}
3175 #else
3176 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
3177 #endif
3178
Stop_log_event(const char * buf,const Format_description_log_event * description_event)3179 Stop_log_event(const char* buf,
3180 const Format_description_log_event *description_event):
3181 Log_event(buf, description_event)
3182 {}
~Stop_log_event()3183 ~Stop_log_event() {}
get_type_code()3184 Log_event_type get_type_code() { return STOP_EVENT;}
is_valid()3185 bool is_valid() const { return 1; }
3186
3187 private:
3188 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3189 virtual int do_update_pos(rpl_group_info *rgi);
do_shall_skip(rpl_group_info * rgi)3190 virtual enum_skip_reason do_shall_skip(rpl_group_info *rgi)
3191 {
3192 /*
3193 Events from ourself should be skipped, but they should not
3194 decrease the slave skip counter.
3195 */
3196 if (this->server_id == global_system_variables.server_id)
3197 return Log_event::EVENT_SKIP_IGNORE;
3198 else
3199 return Log_event::EVENT_SKIP_NOT;
3200 }
3201 #endif
3202 };
3203
3204 /**
3205 @class Rotate_log_event
3206
3207 This will be deprecated when we move to using sequence ids.
3208
3209 @section Rotate_log_event_binary_format Binary Format
3210
3211 The Post-Header has one component:
3212
3213 <table>
3214 <caption>Post-Header for Rotate_log_event</caption>
3215
3216 <tr>
3217 <th>Name</th>
3218 <th>Format</th>
3219 <th>Description</th>
3220 </tr>
3221
3222 <tr>
3223 <td>position</td>
3224 <td>8 byte integer</td>
3225 <td>The position within the binlog to rotate to.</td>
3226 </tr>
3227
3228 </table>
3229
3230 The Body has one component:
3231
3232 <table>
3233 <caption>Body for Rotate_log_event</caption>
3234
3235 <tr>
3236 <th>Name</th>
3237 <th>Format</th>
3238 <th>Description</th>
3239 </tr>
3240
3241 <tr>
3242 <td>new_log</td>
3243 <td>variable length string without trailing zero, extending to the
3244 end of the event (determined by the length field of the
3245 Common-Header)
3246 </td>
3247 <td>Name of the binlog to rotate to.</td>
3248 </tr>
3249
3250 </table>
3251 */
3252
3253 class Rotate_log_event: public Log_event
3254 {
3255 public:
3256 enum {
3257 DUP_NAME= 2, // if constructor should dup the string argument
3258 RELAY_LOG=4 // rotate event for relay log
3259 };
3260 const char* new_log_ident;
3261 ulonglong pos;
3262 uint ident_len;
3263 uint flags;
3264 #ifdef MYSQL_SERVER
3265 Rotate_log_event(const char* new_log_ident_arg,
3266 uint ident_len_arg,
3267 ulonglong pos_arg, uint flags);
3268 #ifdef HAVE_REPLICATION
3269 void pack_info(Protocol* protocol);
3270 #endif /* HAVE_REPLICATION */
3271 #else
3272 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
3273 #endif
3274
3275 Rotate_log_event(const char* buf, uint event_len,
3276 const Format_description_log_event* description_event);
~Rotate_log_event()3277 ~Rotate_log_event()
3278 {
3279 if (flags & DUP_NAME)
3280 my_free((void*) new_log_ident);
3281 }
get_type_code()3282 Log_event_type get_type_code() { return ROTATE_EVENT;}
get_header_len(my_off_t l)3283 my_off_t get_header_len(my_off_t l __attribute__((unused)))
3284 { return LOG_EVENT_MINIMAL_HEADER_LEN; }
get_data_size()3285 int get_data_size() { return ident_len + ROTATE_HEADER_LEN;}
is_valid()3286 bool is_valid() const { return new_log_ident != 0; }
3287 #ifdef MYSQL_SERVER
3288 bool write();
3289 #endif
3290
3291 private:
3292 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3293 virtual int do_update_pos(rpl_group_info *rgi);
3294 virtual enum_skip_reason do_shall_skip(rpl_group_info *rgi);
3295 #endif
3296 };
3297
3298
3299 class Binlog_checkpoint_log_event: public Log_event
3300 {
3301 public:
3302 char *binlog_file_name;
3303 uint binlog_file_len;
3304
3305 #ifdef MYSQL_SERVER
3306 Binlog_checkpoint_log_event(const char *binlog_file_name_arg,
3307 uint binlog_file_len_arg);
3308 #ifdef HAVE_REPLICATION
3309 void pack_info(Protocol *protocol);
3310 #endif
3311 #else
3312 bool print(FILE *file, PRINT_EVENT_INFO *print_event_info);
3313 #endif
3314 Binlog_checkpoint_log_event(const char *buf, uint event_len,
3315 const Format_description_log_event *description_event);
~Binlog_checkpoint_log_event()3316 ~Binlog_checkpoint_log_event() { my_free(binlog_file_name); }
get_type_code()3317 Log_event_type get_type_code() { return BINLOG_CHECKPOINT_EVENT;}
get_data_size()3318 int get_data_size() { return binlog_file_len + BINLOG_CHECKPOINT_HEADER_LEN;}
is_valid()3319 bool is_valid() const { return binlog_file_name != 0; }
3320 #ifdef MYSQL_SERVER
3321 bool write();
3322 enum_skip_reason do_shall_skip(rpl_group_info *rgi);
3323 #endif
3324 };
3325
3326
3327 /**
3328 @class Gtid_log_event
3329
3330 This event is logged as part of every event group to give the global
3331 transaction id (GTID) of that group.
3332
3333 It replaces the BEGIN query event used in earlier versions to begin most
3334 event groups, but is also used for events that used to be stand-alone.
3335
3336 @section Gtid_log_event_binary_format Binary Format
3337
3338 The binary format for Gtid_log_event has 6 extra reserved bytes to make the
3339 length a total of 19 byte (+ 19 bytes of header in common with all events).
3340 This is just the minimal size for a BEGIN query event, which makes it easy
3341 to replace this event with such BEGIN event to remain compatible with old
3342 slave servers.
3343
3344 <table>
3345 <caption>Post-Header</caption>
3346
3347 <tr>
3348 <th>Name</th>
3349 <th>Format</th>
3350 <th>Description</th>
3351 </tr>
3352
3353 <tr>
3354 <td>seq_no</td>
3355 <td>8 byte unsigned integer</td>
3356 <td>increasing id within one server_id. Starts at 1, holes in the sequence
3357 may occur</td>
3358 </tr>
3359
3360 <tr>
3361 <td>domain_id</td>
3362 <td>4 byte unsigned integer</td>
3363 <td>Replication domain id, identifying independent replication streams></td>
3364 </tr>
3365
3366 <tr>
3367 <td>flags</td>
3368 <td>1 byte bitfield</td>
3369 <td>Bit 0 set indicates stand-alone event (no terminating COMMIT)</td>
3370 <td>Bit 1 set indicates group commit, and that commit id exists</td>
3371 <td>Bit 2 set indicates a transactional event group (can be safely rolled
3372 back).</td>
3373 <td>Bit 3 set indicates that user allowed optimistic parallel apply (the
3374 @@SESSION.replicate_allow_parallel value was true at commit).</td>
3375 <td>Bit 4 set indicates that this transaction encountered a row (or other)
3376 lock wait during execution.</td>
3377 </tr>
3378
3379 <tr>
3380 <td>Reserved (no group commit) / commit id (group commit) (see flags bit 1)</td>
3381 <td>6 bytes / 8 bytes</td>
3382 <td>Reserved bytes, set to 0. Maybe be used for future expansion (no
3383 group commit). OR commit id, same for all GTIDs in the same group
3384 commit (see flags bit 1).</td>
3385 </tr>
3386 </table>
3387
3388 The Body of Gtid_log_event is empty. The total event size is 19 bytes +
3389 the normal 19 bytes common-header.
3390 */
3391
3392 class Gtid_log_event: public Log_event
3393 {
3394 public:
3395 uint64 seq_no;
3396 uint64 commit_id;
3397 uint32 domain_id;
3398 uchar flags2;
3399
3400 /* Flags2. */
3401
3402 /* FL_STANDALONE is set when there is no terminating COMMIT event. */
3403 static const uchar FL_STANDALONE= 1;
3404 /*
3405 FL_GROUP_COMMIT_ID is set when event group is part of a group commit on the
3406 master. Groups with same commit_id are part of the same group commit.
3407 */
3408 static const uchar FL_GROUP_COMMIT_ID= 2;
3409 /*
3410 FL_TRANSACTIONAL is set for an event group that can be safely rolled back
3411 (no MyISAM, eg.).
3412 */
3413 static const uchar FL_TRANSACTIONAL= 4;
3414 /*
3415 FL_ALLOW_PARALLEL reflects the (negation of the) value of
3416 @@SESSION.skip_parallel_replication at the time of commit.
3417 */
3418 static const uchar FL_ALLOW_PARALLEL= 8;
3419 /*
3420 FL_WAITED is set if a row lock wait (or other wait) is detected during the
3421 execution of the transaction.
3422 */
3423 static const uchar FL_WAITED= 16;
3424 /* FL_DDL is set for event group containing DDL. */
3425 static const uchar FL_DDL= 32;
3426
3427 #ifdef MYSQL_SERVER
3428 Gtid_log_event(THD *thd_arg, uint64 seq_no, uint32 domain_id, bool standalone,
3429 uint16 flags, bool is_transactional, uint64 commit_id);
3430 #ifdef HAVE_REPLICATION
3431 void pack_info(Protocol *protocol);
3432 virtual int do_apply_event(rpl_group_info *rgi);
3433 virtual int do_update_pos(rpl_group_info *rgi);
3434 virtual enum_skip_reason do_shall_skip(rpl_group_info *rgi);
3435 #endif
3436 #else
3437 bool print(FILE *file, PRINT_EVENT_INFO *print_event_info);
3438 #endif
3439 Gtid_log_event(const char *buf, uint event_len,
3440 const Format_description_log_event *description_event);
~Gtid_log_event()3441 ~Gtid_log_event() { }
get_type_code()3442 Log_event_type get_type_code() { return GTID_EVENT; }
logged_status()3443 enum_logged_status logged_status() { return LOGGED_NO_DATA; }
get_data_size()3444 int get_data_size()
3445 {
3446 return GTID_HEADER_LEN + ((flags2 & FL_GROUP_COMMIT_ID) ? 2 : 0);
3447 }
is_valid()3448 bool is_valid() const { return seq_no != 0; }
3449 #ifdef MYSQL_SERVER
3450 bool write();
3451 static int make_compatible_event(String *packet, bool *need_dummy_event,
3452 ulong ev_offset, enum enum_binlog_checksum_alg checksum_alg);
3453 static bool peek(const char *event_start, size_t event_len,
3454 enum enum_binlog_checksum_alg checksum_alg,
3455 uint32 *domain_id, uint32 *server_id, uint64 *seq_no,
3456 uchar *flags2, const Format_description_log_event *fdev);
3457 #endif
3458 };
3459
3460
3461 /**
3462 @class Gtid_list_log_event
3463
3464 This event is logged at the start of every binlog file to record the
3465 current replication state: the last global transaction id (GTID) applied
3466 on the server within each replication domain.
3467
3468 It consists of a list of GTIDs, one for each replication domain ever seen
3469 on the server.
3470
3471 @section Gtid_list_log_event_binary_format Binary Format
3472
3473 <table>
3474 <caption>Post-Header</caption>
3475
3476 <tr>
3477 <th>Name</th>
3478 <th>Format</th>
3479 <th>Description</th>
3480 </tr>
3481
3482 <tr>
3483 <td>count</td>
3484 <td>4 byte unsigned integer</td>
3485 <td>The lower 28 bits are the number of GTIDs. The upper 4 bits are
3486 flags bits.</td>
3487 </tr>
3488 </table>
3489
3490 <table>
3491 <caption>Body</caption>
3492
3493 <tr>
3494 <th>Name</th>
3495 <th>Format</th>
3496 <th>Description</th>
3497 </tr>
3498
3499 <tr>
3500 <td>domain_id</td>
3501 <td>4 byte unsigned integer</td>
3502 <td>Replication domain id of one GTID</td>
3503 </tr>
3504
3505 <tr>
3506 <td>server_id</td>
3507 <td>4 byte unsigned integer</td>
3508 <td>Server id of one GTID</td>
3509 </tr>
3510
3511 <tr>
3512 <td>seq_no</td>
3513 <td>8 byte unsigned integer</td>
3514 <td>sequence number of one GTID</td>
3515 </tr>
3516 </table>
3517
3518 The three elements in the body repeat COUNT times to form the GTID list.
3519
3520 At the time of writing, only two flag bit are in use.
3521
3522 Bit 28 of `count' is used for flag FLAG_UNTIL_REACHED, which is sent in a
3523 Gtid_list event from the master to the slave to indicate that the START
3524 SLAVE UNTIL master_gtid_pos=xxx condition has been reached. (This flag is
3525 only sent in "fake" events generated on the fly, it is not written into
3526 the binlog).
3527 */
3528
3529 class Gtid_list_log_event: public Log_event
3530 {
3531 public:
3532 uint32 count;
3533 uint32 gl_flags;
3534 struct rpl_gtid *list;
3535 uint64 *sub_id_list;
3536
3537 static const uint element_size= 4+4+8;
3538 /* Upper bits stored in 'count'. See comment above */
3539 enum gtid_flags
3540 {
3541 FLAG_UNTIL_REACHED= (1<<28),
3542 FLAG_IGN_GTIDS= (1<<29),
3543 };
3544 #ifdef MYSQL_SERVER
3545 Gtid_list_log_event(rpl_binlog_state *gtid_set, uint32 gl_flags);
3546 Gtid_list_log_event(slave_connection_state *gtid_set, uint32 gl_flags);
3547 #ifdef HAVE_REPLICATION
3548 void pack_info(Protocol *protocol);
3549 #endif
3550 #else
3551 bool print(FILE *file, PRINT_EVENT_INFO *print_event_info);
3552 #endif
3553 Gtid_list_log_event(const char *buf, uint event_len,
3554 const Format_description_log_event *description_event);
~Gtid_list_log_event()3555 ~Gtid_list_log_event() { my_free(list); my_free(sub_id_list); }
get_type_code()3556 Log_event_type get_type_code() { return GTID_LIST_EVENT; }
get_data_size()3557 int get_data_size() {
3558 /*
3559 Replacing with dummy event, needed for older slaves, requires a minimum
3560 of 6 bytes in the body.
3561 */
3562 return (count==0 ?
3563 GTID_LIST_HEADER_LEN+2 : GTID_LIST_HEADER_LEN+count*element_size);
3564 }
is_valid()3565 bool is_valid() const { return list != NULL; }
3566 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3567 bool to_packet(String *packet);
3568 bool write();
3569 virtual int do_apply_event(rpl_group_info *rgi);
3570 enum_skip_reason do_shall_skip(rpl_group_info *rgi);
3571 #endif
3572 static bool peek(const char *event_start, size_t event_len,
3573 enum enum_binlog_checksum_alg checksum_alg,
3574 rpl_gtid **out_gtid_list, uint32 *out_list_len,
3575 const Format_description_log_event *fdev);
3576 };
3577
3578
3579 /* the classes below are for the new LOAD DATA INFILE logging */
3580
3581 /**
3582 @class Create_file_log_event
3583
3584 @section Create_file_log_event_binary_format Binary Format
3585 */
3586
3587 class Create_file_log_event: public Load_log_event
3588 {
3589 protected:
3590 /*
3591 Pretend we are Load event, so we can write out just
3592 our Load part - used on the slave when writing event out to
3593 SQL_LOAD-*.info file
3594 */
3595 bool fake_base;
3596 public:
3597 uchar* block;
3598 const char *event_buf;
3599 uint block_len;
3600 uint file_id;
3601 bool inited_from_old;
3602
3603 #ifdef MYSQL_SERVER
3604 Create_file_log_event(THD* thd, sql_exchange* ex, const char* db_arg,
3605 const char* table_name_arg,
3606 List<Item>& fields_arg,
3607 bool is_concurrent_arg,
3608 enum enum_duplicates handle_dup, bool ignore,
3609 uchar* block_arg, uint block_len_arg,
3610 bool using_trans);
3611 #ifdef HAVE_REPLICATION
3612 void pack_info(Protocol* protocol);
3613 #endif /* HAVE_REPLICATION */
3614 #else
3615 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
3616 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info,
3617 bool enable_local);
3618 #endif
3619
3620 Create_file_log_event(const char* buf, uint event_len,
3621 const Format_description_log_event* description_event);
~Create_file_log_event()3622 ~Create_file_log_event()
3623 {
3624 my_free((void*) event_buf);
3625 }
3626
get_type_code()3627 Log_event_type get_type_code()
3628 {
3629 return fake_base ? Load_log_event::get_type_code() : CREATE_FILE_EVENT;
3630 }
get_data_size()3631 int get_data_size()
3632 {
3633 return (fake_base ? Load_log_event::get_data_size() :
3634 Load_log_event::get_data_size() +
3635 4 + 1 + block_len);
3636 }
is_valid()3637 bool is_valid() const { return inited_from_old || block != 0; }
3638 #ifdef MYSQL_SERVER
3639 bool write_data_header();
3640 bool write_data_body();
3641 /*
3642 Cut out Create_file extensions and
3643 write it as Load event - used on the slave
3644 */
3645 bool write_base();
3646 #endif
3647
3648 private:
3649 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3650 virtual int do_apply_event(rpl_group_info *rgi);
3651 #endif
3652 };
3653
3654
3655 /**
3656 @class Append_block_log_event
3657
3658 @section Append_block_log_event_binary_format Binary Format
3659 */
3660
3661 class Append_block_log_event: public Log_event
3662 {
3663 public:
3664 uchar* block;
3665 uint block_len;
3666 uint file_id;
3667 /*
3668 'db' is filled when the event is created in mysql_load() (the
3669 event needs to have a 'db' member to be well filtered by
3670 binlog-*-db rules). 'db' is not written to the binlog (it's not
3671 used by Append_block_log_event::write()), so it can't be read in
3672 the Append_block_log_event(const char* buf, int event_len)
3673 constructor. In other words, 'db' is used only for filtering by
3674 binlog-*-db rules. Create_file_log_event is different: it's 'db'
3675 (which is inherited from Load_log_event) is written to the binlog
3676 and can be re-read.
3677 */
3678 const char* db;
3679
3680 #ifdef MYSQL_SERVER
3681 Append_block_log_event(THD* thd, const char* db_arg, uchar* block_arg,
3682 uint block_len_arg, bool using_trans);
3683 #ifdef HAVE_REPLICATION
3684 void pack_info(Protocol* protocol);
3685 virtual int get_create_or_append() const;
3686 #endif /* HAVE_REPLICATION */
3687 #else
3688 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
3689 #endif
3690
3691 Append_block_log_event(const char* buf, uint event_len,
3692 const Format_description_log_event
3693 *description_event);
~Append_block_log_event()3694 ~Append_block_log_event() {}
get_type_code()3695 Log_event_type get_type_code() { return APPEND_BLOCK_EVENT;}
get_data_size()3696 int get_data_size() { return block_len + APPEND_BLOCK_HEADER_LEN ;}
is_valid()3697 bool is_valid() const { return block != 0; }
3698 #ifdef MYSQL_SERVER
3699 bool write();
get_db()3700 const char* get_db() { return db; }
3701 #endif
3702
3703 private:
3704 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3705 virtual int do_apply_event(rpl_group_info *rgi);
3706 #endif
3707 };
3708
3709
3710 /**
3711 @class Delete_file_log_event
3712
3713 @section Delete_file_log_event_binary_format Binary Format
3714 */
3715
3716 class Delete_file_log_event: public Log_event
3717 {
3718 public:
3719 uint file_id;
3720 const char* db; /* see comment in Append_block_log_event */
3721
3722 #ifdef MYSQL_SERVER
3723 Delete_file_log_event(THD* thd, const char* db_arg, bool using_trans);
3724 #ifdef HAVE_REPLICATION
3725 void pack_info(Protocol* protocol);
3726 #endif /* HAVE_REPLICATION */
3727 #else
3728 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
3729 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info,
3730 bool enable_local);
3731 #endif
3732
3733 Delete_file_log_event(const char* buf, uint event_len,
3734 const Format_description_log_event* description_event);
~Delete_file_log_event()3735 ~Delete_file_log_event() {}
get_type_code()3736 Log_event_type get_type_code() { return DELETE_FILE_EVENT;}
get_data_size()3737 int get_data_size() { return DELETE_FILE_HEADER_LEN ;}
is_valid()3738 bool is_valid() const { return file_id != 0; }
3739 #ifdef MYSQL_SERVER
3740 bool write();
get_db()3741 const char* get_db() { return db; }
3742 #endif
3743
3744 private:
3745 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3746 virtual int do_apply_event(rpl_group_info *rgi);
3747 #endif
3748 };
3749
3750
3751 /**
3752 @class Execute_load_log_event
3753
3754 @section Delete_file_log_event_binary_format Binary Format
3755 */
3756
3757 class Execute_load_log_event: public Log_event
3758 {
3759 public:
3760 uint file_id;
3761 const char* db; /* see comment in Append_block_log_event */
3762
3763 #ifdef MYSQL_SERVER
3764 Execute_load_log_event(THD* thd, const char* db_arg, bool using_trans);
3765 #ifdef HAVE_REPLICATION
3766 void pack_info(Protocol* protocol);
3767 #endif /* HAVE_REPLICATION */
3768 #else
3769 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
3770 #endif
3771
3772 Execute_load_log_event(const char* buf, uint event_len,
3773 const Format_description_log_event
3774 *description_event);
~Execute_load_log_event()3775 ~Execute_load_log_event() {}
get_type_code()3776 Log_event_type get_type_code() { return EXEC_LOAD_EVENT;}
get_data_size()3777 int get_data_size() { return EXEC_LOAD_HEADER_LEN ;}
is_valid()3778 bool is_valid() const { return file_id != 0; }
3779 #ifdef MYSQL_SERVER
3780 bool write();
get_db()3781 const char* get_db() { return db; }
3782 #endif
3783
3784 private:
3785 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3786 virtual int do_apply_event(rpl_group_info *rgi);
3787 #endif
3788 };
3789
3790
3791 /**
3792 @class Begin_load_query_log_event
3793
3794 Event for the first block of file to be loaded, its only difference from
3795 Append_block event is that this event creates or truncates existing file
3796 before writing data.
3797
3798 @section Begin_load_query_log_event_binary_format Binary Format
3799 */
3800 class Begin_load_query_log_event: public Append_block_log_event
3801 {
3802 public:
3803 #ifdef MYSQL_SERVER
3804 Begin_load_query_log_event(THD* thd_arg, const char *db_arg,
3805 uchar* block_arg, uint block_len_arg,
3806 bool using_trans);
3807 #ifdef HAVE_REPLICATION
3808 Begin_load_query_log_event(THD* thd);
3809 int get_create_or_append() const;
3810 #endif /* HAVE_REPLICATION */
3811 #endif
3812 Begin_load_query_log_event(const char* buf, uint event_len,
3813 const Format_description_log_event
3814 *description_event);
~Begin_load_query_log_event()3815 ~Begin_load_query_log_event() {}
get_type_code()3816 Log_event_type get_type_code() { return BEGIN_LOAD_QUERY_EVENT; }
3817 private:
3818 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3819 virtual enum_skip_reason do_shall_skip(rpl_group_info *rgi);
3820 #endif
3821 };
3822
3823
3824 /*
3825 Elements of this enum describe how LOAD DATA handles duplicates.
3826 */
3827 enum enum_load_dup_handling { LOAD_DUP_ERROR= 0, LOAD_DUP_IGNORE,
3828 LOAD_DUP_REPLACE };
3829
3830 /**
3831 @class Execute_load_query_log_event
3832
3833 Event responsible for LOAD DATA execution, it similar to Query_log_event
3834 but before executing the query it substitutes original filename in LOAD DATA
3835 query with name of temporary file.
3836
3837 @section Execute_load_query_log_event_binary_format Binary Format
3838 */
3839 class Execute_load_query_log_event: public Query_log_event
3840 {
3841 public:
3842 uint file_id; // file_id of temporary file
3843 uint fn_pos_start; // pointer to the part of the query that should
3844 // be substituted
3845 uint fn_pos_end; // pointer to the end of this part of query
3846 /*
3847 We have to store type of duplicate handling explicitly, because
3848 for LOAD DATA it also depends on LOCAL option. And this part
3849 of query will be rewritten during replication so this information
3850 may be lost...
3851 */
3852 enum_load_dup_handling dup_handling;
3853
3854 #ifdef MYSQL_SERVER
3855 Execute_load_query_log_event(THD* thd, const char* query_arg,
3856 ulong query_length, uint fn_pos_start_arg,
3857 uint fn_pos_end_arg,
3858 enum_load_dup_handling dup_handling_arg,
3859 bool using_trans, bool direct,
3860 bool suppress_use, int errcode);
3861 #ifdef HAVE_REPLICATION
3862 void pack_info(Protocol* protocol);
3863 #endif /* HAVE_REPLICATION */
3864 #else
3865 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
3866 /* Prints the query as LOAD DATA LOCAL and with rewritten filename */
3867 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info,
3868 const char *local_fname);
3869 #endif
3870 Execute_load_query_log_event(const char* buf, uint event_len,
3871 const Format_description_log_event
3872 *description_event);
~Execute_load_query_log_event()3873 ~Execute_load_query_log_event() {}
3874
get_type_code()3875 Log_event_type get_type_code() { return EXECUTE_LOAD_QUERY_EVENT; }
is_valid()3876 bool is_valid() const { return Query_log_event::is_valid() && file_id != 0; }
3877
3878 ulong get_post_header_size_for_derived();
3879 #ifdef MYSQL_SERVER
3880 bool write_post_header_for_derived();
3881 #endif
3882
3883 private:
3884 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3885 virtual int do_apply_event(rpl_group_info *rgi);
3886 #endif
3887 };
3888
3889
3890 #ifdef MYSQL_CLIENT
3891 /**
3892 @class Unknown_log_event
3893
3894 @section Unknown_log_event_binary_format Binary Format
3895 */
3896 class Unknown_log_event: public Log_event
3897 {
3898 public:
3899 enum { UNKNOWN, ENCRYPTED } what;
3900 /*
3901 Even if this is an unknown event, we still pass description_event to
3902 Log_event's ctor, this way we can extract maximum information from the
3903 event's header (the unique ID for example).
3904 */
Unknown_log_event(const char * buf,const Format_description_log_event * description_event)3905 Unknown_log_event(const char* buf,
3906 const Format_description_log_event *description_event):
3907 Log_event(buf, description_event), what(UNKNOWN)
3908 {}
3909 /* constructor for hopelessly corrupted events */
Unknown_log_event()3910 Unknown_log_event(): Log_event(), what(ENCRYPTED) {}
~Unknown_log_event()3911 ~Unknown_log_event() {}
3912 bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
get_type_code()3913 Log_event_type get_type_code() { return UNKNOWN_EVENT;}
is_valid()3914 bool is_valid() const { return 1; }
3915 };
3916 #endif
3917 char *str_to_hex(char *to, const char *from, size_t len);
3918
3919 /**
3920 @class Annotate_rows_log_event
3921
3922 In row-based mode, if binlog_annotate_row_events = ON, each group of
3923 Table_map_log_events is preceded by an Annotate_rows_log_event which
3924 contains the query which caused the subsequent rows operations.
3925
3926 The Annotate_rows_log_event has no post-header and its body contains
3927 the corresponding query (without trailing zero). Note. The query length
3928 is to be calculated as a difference between the whole event length and
3929 the common header length.
3930 */
3931 class Annotate_rows_log_event: public Log_event
3932 {
3933 public:
3934 #ifndef MYSQL_CLIENT
3935 Annotate_rows_log_event(THD*, bool using_trans, bool direct);
3936 #endif
3937 Annotate_rows_log_event(const char *buf, uint event_len,
3938 const Format_description_log_event*);
3939 ~Annotate_rows_log_event();
3940
3941 virtual int get_data_size();
3942 virtual Log_event_type get_type_code();
logged_status()3943 enum_logged_status logged_status() { return LOGGED_NO_DATA; }
3944 virtual bool is_valid() const;
is_part_of_group()3945 virtual bool is_part_of_group() { return 1; }
3946
3947 #ifndef MYSQL_CLIENT
3948 virtual bool write_data_header();
3949 virtual bool write_data_body();
3950 #endif
3951
3952 #if !defined(MYSQL_CLIENT) && defined(HAVE_REPLICATION)
3953 virtual void pack_info(Protocol*);
3954 #endif
3955
3956 #ifdef MYSQL_CLIENT
3957 virtual bool print(FILE*, PRINT_EVENT_INFO*);
3958 #endif
3959
3960 #if !defined(MYSQL_CLIENT) && defined(HAVE_REPLICATION)
3961 private:
3962 virtual int do_apply_event(rpl_group_info *rgi);
3963 virtual int do_update_pos(rpl_group_info *rgi);
3964 virtual enum_skip_reason do_shall_skip(rpl_group_info*);
3965 #endif
3966
3967 private:
3968 char *m_query_txt;
3969 uint m_query_len;
3970 char *m_save_thd_query_txt;
3971 uint m_save_thd_query_len;
3972 bool m_saved_thd_query;
3973 bool m_used_query_txt;
3974 };
3975
3976 /**
3977 @class Table_map_log_event
3978
3979 In row-based mode, every row operation event is preceded by a
3980 Table_map_log_event which maps a table definition to a number. The
3981 table definition consists of database name, table name, and column
3982 definitions.
3983
3984 @section Table_map_log_event_binary_format Binary Format
3985
3986 The Post-Header has the following components:
3987
3988 <table>
3989 <caption>Post-Header for Table_map_log_event</caption>
3990
3991 <tr>
3992 <th>Name</th>
3993 <th>Format</th>
3994 <th>Description</th>
3995 </tr>
3996
3997 <tr>
3998 <td>table_id</td>
3999 <td>6 bytes unsigned integer</td>
4000 <td>The number that identifies the table.</td>
4001 </tr>
4002
4003 <tr>
4004 <td>flags</td>
4005 <td>2 byte bitfield</td>
4006 <td>Reserved for future use; currently always 0.</td>
4007 </tr>
4008
4009 </table>
4010
4011 The Body has the following components:
4012
4013 <table>
4014 <caption>Body for Table_map_log_event</caption>
4015
4016 <tr>
4017 <th>Name</th>
4018 <th>Format</th>
4019 <th>Description</th>
4020 </tr>
4021
4022 <tr>
4023 <td>database_name</td>
4024 <td>one byte string length, followed by null-terminated string</td>
4025 <td>The name of the database in which the table resides. The name
4026 is represented as a one byte unsigned integer representing the
4027 number of bytes in the name, followed by length bytes containing
4028 the database name, followed by a terminating 0 byte. (Note the
4029 redundancy in the representation of the length.) </td>
4030 </tr>
4031
4032 <tr>
4033 <td>table_name</td>
4034 <td>one byte string length, followed by null-terminated string</td>
4035 <td>The name of the table, encoded the same way as the database
4036 name above.</td>
4037 </tr>
4038
4039 <tr>
4040 <td>column_count</td>
4041 <td>@ref packed_integer "Packed Integer"</td>
4042 <td>The number of columns in the table, represented as a packed
4043 variable-length integer.</td>
4044 </tr>
4045
4046 <tr>
4047 <td>column_type</td>
4048 <td>List of column_count 1 byte enumeration values</td>
4049 <td>The type of each column in the table, listed from left to
4050 right. Each byte is mapped to a column type according to the
4051 enumeration type enum_field_types defined in mysql_com.h. The
4052 mapping of types to numbers is listed in the table @ref
4053 Table_table_map_log_event_column_types "below" (along with
4054 description of the associated metadata field). </td>
4055 </tr>
4056
4057 <tr>
4058 <td>metadata_length</td>
4059 <td>@ref packed_integer "Packed Integer"</td>
4060 <td>The length of the following metadata block</td>
4061 </tr>
4062
4063 <tr>
4064 <td>metadata</td>
4065 <td>list of metadata for each column</td>
4066 <td>For each column from left to right, a chunk of data who's
4067 length and semantics depends on the type of the column. The
4068 length and semantics for the metadata for each column are listed
4069 in the table @ref Table_table_map_log_event_column_types
4070 "below".</td>
4071 </tr>
4072
4073 <tr>
4074 <td>null_bits</td>
4075 <td>column_count bits, rounded up to nearest byte</td>
4076 <td>For each column, a bit indicating whether data in the column
4077 can be NULL or not. The number of bytes needed for this is
4078 int((column_count+7)/8). The flag for the first column from the
4079 left is in the least-significant bit of the first byte, the second
4080 is in the second least significant bit of the first byte, the
4081 ninth is in the least significant bit of the second byte, and so
4082 on. </td>
4083 </tr>
4084
4085 </table>
4086
4087 The table below lists all column types, along with the numerical
4088 identifier for it and the size and interpretation of meta-data used
4089 to describe the type.
4090
4091 @anchor Table_table_map_log_event_column_types
4092 <table>
4093 <caption>Table_map_log_event column types: numerical identifier and
4094 metadata</caption>
4095 <tr>
4096 <th>Name</th>
4097 <th>Identifier</th>
4098 <th>Size of metadata in bytes</th>
4099 <th>Description of metadata</th>
4100 </tr>
4101
4102 <tr>
4103 <td>MYSQL_TYPE_DECIMAL</td><td>0</td>
4104 <td>0</td>
4105 <td>No column metadata.</td>
4106 </tr>
4107
4108 <tr>
4109 <td>MYSQL_TYPE_TINY</td><td>1</td>
4110 <td>0</td>
4111 <td>No column metadata.</td>
4112 </tr>
4113
4114 <tr>
4115 <td>MYSQL_TYPE_SHORT</td><td>2</td>
4116 <td>0</td>
4117 <td>No column metadata.</td>
4118 </tr>
4119
4120 <tr>
4121 <td>MYSQL_TYPE_LONG</td><td>3</td>
4122 <td>0</td>
4123 <td>No column metadata.</td>
4124 </tr>
4125
4126 <tr>
4127 <td>MYSQL_TYPE_FLOAT</td><td>4</td>
4128 <td>1 byte</td>
4129 <td>1 byte unsigned integer, representing the "pack_length", which
4130 is equal to sizeof(float) on the server from which the event
4131 originates.</td>
4132 </tr>
4133
4134 <tr>
4135 <td>MYSQL_TYPE_DOUBLE</td><td>5</td>
4136 <td>1 byte</td>
4137 <td>1 byte unsigned integer, representing the "pack_length", which
4138 is equal to sizeof(double) on the server from which the event
4139 originates.</td>
4140 </tr>
4141
4142 <tr>
4143 <td>MYSQL_TYPE_NULL</td><td>6</td>
4144 <td>0</td>
4145 <td>No column metadata.</td>
4146 </tr>
4147
4148 <tr>
4149 <td>MYSQL_TYPE_TIMESTAMP</td><td>7</td>
4150 <td>0</td>
4151 <td>No column metadata.</td>
4152 </tr>
4153
4154 <tr>
4155 <td>MYSQL_TYPE_LONGLONG</td><td>8</td>
4156 <td>0</td>
4157 <td>No column metadata.</td>
4158 </tr>
4159
4160 <tr>
4161 <td>MYSQL_TYPE_INT24</td><td>9</td>
4162 <td>0</td>
4163 <td>No column metadata.</td>
4164 </tr>
4165
4166 <tr>
4167 <td>MYSQL_TYPE_DATE</td><td>10</td>
4168 <td>0</td>
4169 <td>No column metadata.</td>
4170 </tr>
4171
4172 <tr>
4173 <td>MYSQL_TYPE_TIME</td><td>11</td>
4174 <td>0</td>
4175 <td>No column metadata.</td>
4176 </tr>
4177
4178 <tr>
4179 <td>MYSQL_TYPE_DATETIME</td><td>12</td>
4180 <td>0</td>
4181 <td>No column metadata.</td>
4182 </tr>
4183
4184 <tr>
4185 <td>MYSQL_TYPE_YEAR</td><td>13</td>
4186 <td>0</td>
4187 <td>No column metadata.</td>
4188 </tr>
4189
4190 <tr>
4191 <td><i>MYSQL_TYPE_NEWDATE</i></td><td><i>14</i></td>
4192 <td>–</td>
4193 <td><i>This enumeration value is only used internally and cannot
4194 exist in a binlog.</i></td>
4195 </tr>
4196
4197 <tr>
4198 <td>MYSQL_TYPE_VARCHAR</td><td>15</td>
4199 <td>2 bytes</td>
4200 <td>2 byte unsigned integer representing the maximum length of
4201 the string.</td>
4202 </tr>
4203
4204 <tr>
4205 <td>MYSQL_TYPE_BIT</td><td>16</td>
4206 <td>2 bytes</td>
4207 <td>A 1 byte unsigned int representing the length in bits of the
4208 bitfield (0 to 64), followed by a 1 byte unsigned int
4209 representing the number of bytes occupied by the bitfield. The
4210 number of bytes is either int((length+7)/8) or int(length/8).</td>
4211 </tr>
4212
4213 <tr>
4214 <td>MYSQL_TYPE_NEWDECIMAL</td><td>246</td>
4215 <td>2 bytes</td>
4216 <td>A 1 byte unsigned int representing the precision, followed
4217 by a 1 byte unsigned int representing the number of decimals.</td>
4218 </tr>
4219
4220 <tr>
4221 <td><i>MYSQL_TYPE_ENUM</i></td><td><i>247</i></td>
4222 <td>–</td>
4223 <td><i>This enumeration value is only used internally and cannot
4224 exist in a binlog.</i></td>
4225 </tr>
4226
4227 <tr>
4228 <td><i>MYSQL_TYPE_SET</i></td><td><i>248</i></td>
4229 <td>–</td>
4230 <td><i>This enumeration value is only used internally and cannot
4231 exist in a binlog.</i></td>
4232 </tr>
4233
4234 <tr>
4235 <td>MYSQL_TYPE_TINY_BLOB</td><td>249</td>
4236 <td>–</td>
4237 <td><i>This enumeration value is only used internally and cannot
4238 exist in a binlog.</i></td>
4239 </tr>
4240
4241 <tr>
4242 <td><i>MYSQL_TYPE_MEDIUM_BLOB</i></td><td><i>250</i></td>
4243 <td>–</td>
4244 <td><i>This enumeration value is only used internally and cannot
4245 exist in a binlog.</i></td>
4246 </tr>
4247
4248 <tr>
4249 <td><i>MYSQL_TYPE_LONG_BLOB</i></td><td><i>251</i></td>
4250 <td>–</td>
4251 <td><i>This enumeration value is only used internally and cannot
4252 exist in a binlog.</i></td>
4253 </tr>
4254
4255 <tr>
4256 <td>MYSQL_TYPE_BLOB</td><td>252</td>
4257 <td>1 byte</td>
4258 <td>The pack length, i.e., the number of bytes needed to represent
4259 the length of the blob: 1, 2, 3, or 4.</td>
4260 </tr>
4261
4262 <tr>
4263 <td>MYSQL_TYPE_VAR_STRING</td><td>253</td>
4264 <td>2 bytes</td>
4265 <td>This is used to store both strings and enumeration values.
4266 The first byte is a enumeration value storing the <i>real
4267 type</i>, which may be either MYSQL_TYPE_VAR_STRING or
4268 MYSQL_TYPE_ENUM. The second byte is a 1 byte unsigned integer
4269 representing the field size, i.e., the number of bytes needed to
4270 store the length of the string.</td>
4271 </tr>
4272
4273 <tr>
4274 <td>MYSQL_TYPE_STRING</td><td>254</td>
4275 <td>2 bytes</td>
4276 <td>The first byte is always MYSQL_TYPE_VAR_STRING (i.e., 253).
4277 The second byte is the field size, i.e., the number of bytes in
4278 the representation of size of the string: 3 or 4.</td>
4279 </tr>
4280
4281 <tr>
4282 <td>MYSQL_TYPE_GEOMETRY</td><td>255</td>
4283 <td>1 byte</td>
4284 <td>The pack length, i.e., the number of bytes needed to represent
4285 the length of the geometry: 1, 2, 3, or 4.</td>
4286 </tr>
4287
4288 </table>
4289 */
4290 class Table_map_log_event : public Log_event
4291 {
4292 public:
4293 /* Constants */
4294 enum
4295 {
4296 TYPE_CODE = TABLE_MAP_EVENT
4297 };
4298
4299 /**
4300 Enumeration of the errors that can be returned.
4301 */
4302 enum enum_error
4303 {
4304 ERR_OPEN_FAILURE = -1, /**< Failure to open table */
4305 ERR_OK = 0, /**< No error */
4306 ERR_TABLE_LIMIT_EXCEEDED = 1, /**< No more room for tables */
4307 ERR_OUT_OF_MEM = 2, /**< Out of memory */
4308 ERR_BAD_TABLE_DEF = 3, /**< Table definition does not match */
4309 ERR_RBR_TO_SBR = 4 /**< daisy-chanining RBR to SBR not allowed */
4310 };
4311
4312 enum enum_flag
4313 {
4314 /*
4315 Nothing here right now, but the flags support is there in
4316 preparation for changes that are coming. Need to add a
4317 constant to make it compile under HP-UX: aCC does not like
4318 empty enumerations.
4319 */
4320 ENUM_FLAG_COUNT
4321 };
4322
4323 typedef uint16 flag_set;
4324
4325 /* Special constants representing sets of flags */
4326 enum
4327 {
4328 TM_NO_FLAGS = 0U,
4329 TM_BIT_LEN_EXACT_F = (1U << 0),
4330 // MariaDB flags (we starts from the other end)
4331 TM_BIT_HAS_TRIGGERS_F= (1U << 14)
4332 };
4333
get_flags(flag_set flag)4334 flag_set get_flags(flag_set flag) const { return m_flags & flag; }
4335
4336 #ifdef MYSQL_SERVER
4337 Table_map_log_event(THD *thd, TABLE *tbl, ulong tid, bool is_transactional);
4338 #endif
4339 #ifdef HAVE_REPLICATION
4340 Table_map_log_event(const char *buf, uint event_len,
4341 const Format_description_log_event *description_event);
4342 #endif
4343
4344 ~Table_map_log_event();
4345
4346 #ifdef MYSQL_CLIENT
create_table_def()4347 table_def *create_table_def()
4348 {
4349 return new table_def(m_coltype, m_colcnt, m_field_metadata,
4350 m_field_metadata_size, m_null_bits, m_flags);
4351 }
4352 int rewrite_db(const char* new_name, size_t new_name_len,
4353 const Format_description_log_event*);
4354 #endif
get_table_id()4355 ulonglong get_table_id() const { return m_table_id; }
get_table_name()4356 const char *get_table_name() const { return m_tblnam; }
get_db_name()4357 const char *get_db_name() const { return m_dbnam; }
4358
get_type_code()4359 virtual Log_event_type get_type_code() { return TABLE_MAP_EVENT; }
logged_status()4360 virtual enum_logged_status logged_status() { return LOGGED_TABLE_MAP; }
is_valid()4361 virtual bool is_valid() const { return m_memory != NULL; /* we check malloc */ }
is_part_of_group()4362 virtual bool is_part_of_group() { return 1; }
4363
get_data_size()4364 virtual int get_data_size() { return (uint) m_data_size; }
4365 #ifdef MYSQL_SERVER
4366 virtual int save_field_metadata();
4367 virtual bool write_data_header();
4368 virtual bool write_data_body();
get_db()4369 virtual const char *get_db() { return m_dbnam; }
4370 #endif
4371
4372 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
4373 virtual void pack_info(Protocol *protocol);
4374 #endif
4375
4376 #ifdef MYSQL_CLIENT
4377 virtual bool print(FILE *file, PRINT_EVENT_INFO *print_event_info);
4378 #endif
4379
4380
4381 private:
4382 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
4383 virtual int do_apply_event(rpl_group_info *rgi);
4384 virtual int do_update_pos(rpl_group_info *rgi);
4385 virtual enum_skip_reason do_shall_skip(rpl_group_info *rgi);
4386 #endif
4387
4388 #ifdef MYSQL_SERVER
4389 TABLE *m_table;
4390 #endif
4391 char const *m_dbnam;
4392 size_t m_dblen;
4393 char const *m_tblnam;
4394 size_t m_tbllen;
4395 ulong m_colcnt;
4396 uchar *m_coltype;
4397
4398 uchar *m_memory;
4399 ulonglong m_table_id;
4400 flag_set m_flags;
4401
4402 size_t m_data_size;
4403
4404 uchar *m_field_metadata; // buffer for field metadata
4405 /*
4406 The size of field metadata buffer set by calling save_field_metadata()
4407 */
4408 ulong m_field_metadata_size;
4409 uchar *m_null_bits;
4410 uchar *m_meta_memory;
4411 };
4412
4413
4414 /**
4415 @class Rows_log_event
4416
4417 Common base class for all row-containing log events.
4418
4419 RESPONSIBILITIES
4420
4421 Encode the common parts of all events containing rows, which are:
4422 - Write data header and data body to an IO_CACHE.
4423 - Provide an interface for adding an individual row to the event.
4424
4425 @section Rows_log_event_binary_format Binary Format
4426 */
4427
4428
4429 class Rows_log_event : public Log_event
4430 {
4431 public:
4432 /**
4433 Enumeration of the errors that can be returned.
4434 */
4435 enum enum_error
4436 {
4437 ERR_OPEN_FAILURE = -1, /**< Failure to open table */
4438 ERR_OK = 0, /**< No error */
4439 ERR_TABLE_LIMIT_EXCEEDED = 1, /**< No more room for tables */
4440 ERR_OUT_OF_MEM = 2, /**< Out of memory */
4441 ERR_BAD_TABLE_DEF = 3, /**< Table definition does not match */
4442 ERR_RBR_TO_SBR = 4 /**< daisy-chanining RBR to SBR not allowed */
4443 };
4444
4445 /*
4446 These definitions allow you to combine the flags into an
4447 appropriate flag set using the normal bitwise operators. The
4448 implicit conversion from an enum-constant to an integer is
4449 accepted by the compiler, which is then used to set the real set
4450 of flags.
4451 */
4452 enum enum_flag
4453 {
4454 /* Last event of a statement */
4455 STMT_END_F = (1U << 0),
4456
4457 /* Value of the OPTION_NO_FOREIGN_KEY_CHECKS flag in thd->options */
4458 NO_FOREIGN_KEY_CHECKS_F = (1U << 1),
4459
4460 /* Value of the OPTION_RELAXED_UNIQUE_CHECKS flag in thd->options */
4461 RELAXED_UNIQUE_CHECKS_F = (1U << 2),
4462
4463 /**
4464 Indicates that rows in this event are complete, that is contain
4465 values for all columns of the table.
4466 */
4467 COMPLETE_ROWS_F = (1U << 3),
4468
4469 /* Value of the OPTION_NO_CHECK_CONSTRAINT_CHECKS flag in thd->options */
4470 NO_CHECK_CONSTRAINT_CHECKS_F = (1U << 7)
4471 };
4472
4473 typedef uint16 flag_set;
4474
4475 /* Special constants representing sets of flags */
4476 enum
4477 {
4478 RLE_NO_FLAGS = 0U
4479 };
4480
4481 virtual ~Rows_log_event();
4482
set_flags(flag_set flags_arg)4483 void set_flags(flag_set flags_arg) { m_flags |= flags_arg; }
clear_flags(flag_set flags_arg)4484 void clear_flags(flag_set flags_arg) { m_flags &= ~flags_arg; }
get_flags(flag_set flags_arg)4485 flag_set get_flags(flag_set flags_arg) const { return m_flags & flags_arg; }
update_flags()4486 void update_flags() { int2store(temp_buf + m_flags_pos, m_flags); }
4487
get_type_code()4488 Log_event_type get_type_code() { return m_type; } /* Specific type (_V1 etc) */
logged_status()4489 enum_logged_status logged_status() { return LOGGED_ROW_EVENT; }
4490 virtual Log_event_type get_general_type_code() = 0; /* General rows op type, no version */
4491
4492 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
4493 virtual void pack_info(Protocol *protocol);
4494 #endif
4495
4496 #ifdef MYSQL_CLIENT
4497 /* not for direct call, each derived has its own ::print() */
4498 virtual bool print(FILE *file, PRINT_EVENT_INFO *print_event_info)= 0;
4499 void change_to_flashback_event(PRINT_EVENT_INFO *print_event_info, uchar *rows_buff, Log_event_type ev_type);
4500 bool print_verbose(IO_CACHE *file,
4501 PRINT_EVENT_INFO *print_event_info);
4502 size_t print_verbose_one_row(IO_CACHE *file, table_def *td,
4503 PRINT_EVENT_INFO *print_event_info,
4504 MY_BITMAP *cols_bitmap,
4505 const uchar *ptr, const uchar *prefix,
4506 const my_bool no_fill_output= 0); // if no_fill_output=1, then print result is unnecessary
4507 size_t calc_row_event_length(table_def *td,
4508 PRINT_EVENT_INFO *print_event_info,
4509 MY_BITMAP *cols_bitmap,
4510 const uchar *value);
4511 void count_row_events(PRINT_EVENT_INFO *print_event_info);
4512
4513 #endif
4514
4515 #ifdef MYSQL_SERVER
add_row_data(uchar * data,size_t length)4516 int add_row_data(uchar *data, size_t length)
4517 {
4518 return do_add_row_data(data,length);
4519 }
4520 #endif
4521
4522 /* Member functions to implement superclass interface */
4523 virtual int get_data_size();
4524
get_cols()4525 MY_BITMAP const *get_cols() const { return &m_cols; }
get_cols_ai()4526 MY_BITMAP const *get_cols_ai() const { return &m_cols_ai; }
get_width()4527 size_t get_width() const { return m_width; }
get_table_id()4528 ulonglong get_table_id() const { return m_table_id; }
4529
4530 #if defined(MYSQL_SERVER)
4531 /*
4532 This member function compares the table's read/write_set
4533 with this event's m_cols and m_cols_ai. Comparison takes
4534 into account what type of rows event is this: Delete, Write or
4535 Update, therefore it uses the correct m_cols[_ai] according
4536 to the event type code.
4537
4538 Note that this member function should only be called for the
4539 following events:
4540 - Delete_rows_log_event
4541 - Write_rows_log_event
4542 - Update_rows_log_event
4543
4544 @param[IN] table The table to compare this events bitmaps
4545 against.
4546
4547 @return TRUE if sets match, FALSE otherwise. (following
4548 bitmap_cmp return logic).
4549
4550 */
read_write_bitmaps_cmp(TABLE * table)4551 bool read_write_bitmaps_cmp(TABLE *table)
4552 {
4553 bool res= FALSE;
4554
4555 switch (get_general_type_code())
4556 {
4557 case DELETE_ROWS_EVENT:
4558 res= bitmap_cmp(get_cols(), table->read_set);
4559 break;
4560 case UPDATE_ROWS_EVENT:
4561 res= (bitmap_cmp(get_cols(), table->read_set) &&
4562 bitmap_cmp(get_cols_ai(), table->rpl_write_set));
4563 break;
4564 case WRITE_ROWS_EVENT:
4565 res= bitmap_cmp(get_cols(), table->rpl_write_set);
4566 break;
4567 default:
4568 /*
4569 We should just compare bitmaps for Delete, Write
4570 or Update rows events.
4571 */
4572 DBUG_ASSERT(0);
4573 }
4574 return res;
4575 }
4576 #endif
4577
4578 #ifdef MYSQL_SERVER
4579 virtual bool write_data_header();
4580 virtual bool write_data_body();
4581 virtual bool write_compressed();
get_db()4582 virtual const char *get_db() { return m_table->s->db.str; }
4583 #endif
4584 /*
4585 Check that malloc() succeeded in allocating memory for the rows
4586 buffer and the COLS vector. Checking that an Update_rows_log_event
4587 is valid is done in the Update_rows_log_event::is_valid()
4588 function.
4589 */
is_valid()4590 virtual bool is_valid() const
4591 {
4592 return m_rows_buf && m_cols.bitmap;
4593 }
is_part_of_group()4594 bool is_part_of_group() { return get_flags(STMT_END_F) != 0; }
4595
4596 uint m_row_count; /* The number of rows added to the event */
4597
get_extra_row_data()4598 const uchar* get_extra_row_data() const { return m_extra_row_data; }
4599
4600 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
4601 virtual uint8 get_trg_event_map()= 0;
4602 #endif
4603
4604 protected:
4605 /*
4606 The constructors are protected since you're supposed to inherit
4607 this class, not create instances of this class.
4608 */
4609 #ifdef MYSQL_SERVER
4610 Rows_log_event(THD*, TABLE*, ulong table_id,
4611 MY_BITMAP const *cols, bool is_transactional,
4612 Log_event_type event_type);
4613 #endif
4614 Rows_log_event(const char *row_data, uint event_len,
4615 const Format_description_log_event *description_event);
4616 void uncompress_buf();
4617
4618 #ifdef MYSQL_CLIENT
4619 bool print_helper(FILE *, PRINT_EVENT_INFO *, char const *const name);
4620 #endif
4621
4622 #ifdef MYSQL_SERVER
4623 virtual int do_add_row_data(uchar *data, size_t length);
4624 #endif
4625
4626 #ifdef MYSQL_SERVER
4627 TABLE *m_table; /* The table the rows belong to */
4628 #endif
4629 ulonglong m_table_id; /* Table ID */
4630 MY_BITMAP m_cols; /* Bitmap denoting columns available */
4631 ulong m_width; /* The width of the columns bitmap */
4632 /*
4633 Bitmap for columns available in the after image, if present. These
4634 fields are only available for Update_rows events. Observe that the
4635 width of both the before image COLS vector and the after image
4636 COLS vector is the same: the number of columns of the table on the
4637 master.
4638 */
4639 MY_BITMAP m_cols_ai;
4640
4641 ulong m_master_reclength; /* Length of record on master side */
4642
4643 /* Bit buffers in the same memory as the class */
4644 uint32 m_bitbuf[128/(sizeof(uint32)*8)];
4645 uint32 m_bitbuf_ai[128/(sizeof(uint32)*8)];
4646
4647 uchar *m_rows_buf; /* The rows in packed format */
4648 uchar *m_rows_cur; /* One-after the end of the data */
4649 uchar *m_rows_end; /* One-after the end of the allocated space */
4650
4651 size_t m_rows_before_size; /* The length before m_rows_buf */
4652 size_t m_flags_pos; /* The position of the m_flags */
4653
4654 flag_set m_flags; /* Flags for row-level events */
4655
4656 Log_event_type m_type; /* Actual event type */
4657
4658 uchar *m_extra_row_data; /* Pointer to extra row data if any */
4659 /* If non null, first byte is length */
4660
4661 bool m_vers_from_plain;
4662
4663
4664 /* helper functions */
4665
4666 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
4667 const uchar *m_curr_row; /* Start of the row being processed */
4668 const uchar *m_curr_row_end; /* One-after the end of the current row */
4669 uchar *m_key; /* Buffer to keep key value during searches */
4670 KEY *m_key_info; /* Pointer to KEY info for m_key_nr */
4671 uint m_key_nr; /* Key number */
4672 bool master_had_triggers; /* set after tables opening */
4673
4674 int find_key(); // Find a best key to use in find_row()
4675 int find_row(rpl_group_info *);
4676 int write_row(rpl_group_info *, const bool);
4677 int update_sequence();
4678
4679 // Unpack the current row into m_table->record[0], but with
4680 // a different columns bitmap.
unpack_current_row(rpl_group_info * rgi,MY_BITMAP const * cols)4681 int unpack_current_row(rpl_group_info *rgi, MY_BITMAP const *cols)
4682 {
4683 DBUG_ASSERT(m_table);
4684
4685 ASSERT_OR_RETURN_ERROR(m_curr_row <= m_rows_end, HA_ERR_CORRUPT_EVENT);
4686 return ::unpack_row(rgi, m_table, m_width, m_curr_row, cols,
4687 &m_curr_row_end, &m_master_reclength, m_rows_end);
4688 }
4689
4690 // Unpack the current row into m_table->record[0]
unpack_current_row(rpl_group_info * rgi)4691 int unpack_current_row(rpl_group_info *rgi)
4692 {
4693 DBUG_ASSERT(m_table);
4694
4695 ASSERT_OR_RETURN_ERROR(m_curr_row <= m_rows_end, HA_ERR_CORRUPT_EVENT);
4696 return ::unpack_row(rgi, m_table, m_width, m_curr_row, &m_cols,
4697 &m_curr_row_end, &m_master_reclength, m_rows_end);
4698 }
4699 bool process_triggers(trg_event_type event,
4700 trg_action_time_type time_type,
4701 bool old_row_is_record1);
4702
4703 /**
4704 Helper function to check whether there is an auto increment
4705 column on the table where the event is to be applied.
4706
4707 @return true if there is an autoincrement field on the extra
4708 columns, false otherwise.
4709 */
is_auto_inc_in_extra_columns()4710 inline bool is_auto_inc_in_extra_columns()
4711 {
4712 DBUG_ASSERT(m_table);
4713 return (m_table->next_number_field &&
4714 m_table->next_number_field->field_index >= m_width);
4715 }
4716 #endif
4717
4718 private:
4719
4720 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
4721 virtual int do_apply_event(rpl_group_info *rgi);
4722 virtual int do_update_pos(rpl_group_info *rgi);
4723 virtual enum_skip_reason do_shall_skip(rpl_group_info *rgi);
4724
4725 /*
4726 Primitive to prepare for a sequence of row executions.
4727
4728 DESCRIPTION
4729
4730 Before doing a sequence of do_prepare_row() and do_exec_row()
4731 calls, this member function should be called to prepare for the
4732 entire sequence. Typically, this member function will allocate
4733 space for any buffers that are needed for the two member
4734 functions mentioned above.
4735
4736 RETURN VALUE
4737
4738 The member function will return 0 if all went OK, or a non-zero
4739 error code otherwise.
4740 */
4741 virtual
4742 int do_before_row_operations(const Slave_reporting_capability *const log) = 0;
4743
4744 /*
4745 Primitive to clean up after a sequence of row executions.
4746
4747 DESCRIPTION
4748
4749 After doing a sequence of do_prepare_row() and do_exec_row(),
4750 this member function should be called to clean up and release
4751 any allocated buffers.
4752
4753 The error argument, if non-zero, indicates an error which happened during
4754 row processing before this function was called. In this case, even if
4755 function is successful, it should return the error code given in the argument.
4756 */
4757 virtual
4758 int do_after_row_operations(const Slave_reporting_capability *const log,
4759 int error) = 0;
4760
4761 /*
4762 Primitive to do the actual execution necessary for a row.
4763
4764 DESCRIPTION
4765 The member function will do the actual execution needed to handle a row.
4766 The row is located at m_curr_row. When the function returns,
4767 m_curr_row_end should point at the next row (one byte after the end
4768 of the current row).
4769
4770 RETURN VALUE
4771 0 if execution succeeded, 1 if execution failed.
4772
4773 */
4774 virtual int do_exec_row(rpl_group_info *rli) = 0;
4775 #endif /* defined(MYSQL_SERVER) && defined(HAVE_REPLICATION) */
4776
4777 friend class Old_rows_log_event;
4778 };
4779
4780 /**
4781 @class Write_rows_log_event
4782
4783 Log row insertions and updates. The event contain several
4784 insert/update rows for a table. Note that each event contains only
4785 rows for one table.
4786
4787 @section Write_rows_log_event_binary_format Binary Format
4788 */
4789 class Write_rows_log_event : public Rows_log_event
4790 {
4791 public:
4792 enum
4793 {
4794 /* Support interface to THD::binlog_prepare_pending_rows_event */
4795 TYPE_CODE = WRITE_ROWS_EVENT
4796 };
4797
4798 #if defined(MYSQL_SERVER)
4799 Write_rows_log_event(THD*, TABLE*, ulong table_id,
4800 bool is_transactional);
4801 #endif
4802 #ifdef HAVE_REPLICATION
4803 Write_rows_log_event(const char *buf, uint event_len,
4804 const Format_description_log_event *description_event);
4805 #endif
4806 #if defined(MYSQL_SERVER)
binlog_row_logging_function(THD * thd,TABLE * table,bool is_transactional,const uchar * before_record,const uchar * after_record)4807 static bool binlog_row_logging_function(THD *thd, TABLE *table,
4808 bool is_transactional,
4809 const uchar *before_record
4810 __attribute__((unused)),
4811 const uchar *after_record)
4812 {
4813 DBUG_ASSERT(!table->versioned(VERS_TRX_ID));
4814 return thd->binlog_write_row(table, is_transactional, after_record);
4815 }
4816 #endif
4817
4818 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
4819 uint8 get_trg_event_map();
4820 #endif
4821
4822 private:
get_general_type_code()4823 virtual Log_event_type get_general_type_code() { return (Log_event_type)TYPE_CODE; }
4824
4825 #ifdef MYSQL_CLIENT
4826 bool print(FILE *file, PRINT_EVENT_INFO *print_event_info);
4827 #endif
4828
4829 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
4830 virtual int do_before_row_operations(const Slave_reporting_capability *const);
4831 virtual int do_after_row_operations(const Slave_reporting_capability *const,int);
4832 virtual int do_exec_row(rpl_group_info *);
4833 #endif
4834 };
4835
4836 class Write_rows_compressed_log_event : public Write_rows_log_event
4837 {
4838 public:
4839 #if defined(MYSQL_SERVER)
4840 Write_rows_compressed_log_event(THD*, TABLE*, ulong table_id,
4841 bool is_transactional);
4842 virtual bool write();
4843 #endif
4844 #ifdef HAVE_REPLICATION
4845 Write_rows_compressed_log_event(const char *buf, uint event_len,
4846 const Format_description_log_event *description_event);
4847 #endif
4848 private:
4849 #if defined(MYSQL_CLIENT)
4850 bool print(FILE *file, PRINT_EVENT_INFO *print_event_info);
4851 #endif
4852 };
4853
4854 /**
4855 @class Update_rows_log_event
4856
4857 Log row updates with a before image. The event contain several
4858 update rows for a table. Note that each event contains only rows for
4859 one table.
4860
4861 Also note that the row data consists of pairs of row data: one row
4862 for the old data and one row for the new data.
4863
4864 @section Update_rows_log_event_binary_format Binary Format
4865 */
4866 class Update_rows_log_event : public Rows_log_event
4867 {
4868 public:
4869 enum
4870 {
4871 /* Support interface to THD::binlog_prepare_pending_rows_event */
4872 TYPE_CODE = UPDATE_ROWS_EVENT
4873 };
4874
4875 #ifdef MYSQL_SERVER
4876 Update_rows_log_event(THD*, TABLE*, ulong table_id,
4877 bool is_transactional);
4878
4879 void init(MY_BITMAP const *cols);
4880 #endif
4881
4882 virtual ~Update_rows_log_event();
4883
4884 #ifdef HAVE_REPLICATION
4885 Update_rows_log_event(const char *buf, uint event_len,
4886 const Format_description_log_event *description_event);
4887 #endif
4888
4889 #ifdef MYSQL_SERVER
binlog_row_logging_function(THD * thd,TABLE * table,bool is_transactional,const uchar * before_record,const uchar * after_record)4890 static bool binlog_row_logging_function(THD *thd, TABLE *table,
4891 bool is_transactional,
4892 const uchar *before_record,
4893 const uchar *after_record)
4894 {
4895 DBUG_ASSERT(!table->versioned(VERS_TRX_ID));
4896 return thd->binlog_update_row(table, is_transactional,
4897 before_record, after_record);
4898 }
4899 #endif
4900
is_valid()4901 virtual bool is_valid() const
4902 {
4903 return Rows_log_event::is_valid() && m_cols_ai.bitmap;
4904 }
4905
4906 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
4907 uint8 get_trg_event_map();
4908 #endif
4909
4910 protected:
get_general_type_code()4911 virtual Log_event_type get_general_type_code() { return (Log_event_type)TYPE_CODE; }
4912
4913 #ifdef MYSQL_CLIENT
4914 bool print(FILE *file, PRINT_EVENT_INFO *print_event_info);
4915 #endif
4916
4917 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
4918 virtual int do_before_row_operations(const Slave_reporting_capability *const);
4919 virtual int do_after_row_operations(const Slave_reporting_capability *const,int);
4920 virtual int do_exec_row(rpl_group_info *);
4921 #endif /* defined(MYSQL_SERVER) && defined(HAVE_REPLICATION) */
4922 };
4923
4924 class Update_rows_compressed_log_event : public Update_rows_log_event
4925 {
4926 public:
4927 #if defined(MYSQL_SERVER)
4928 Update_rows_compressed_log_event(THD*, TABLE*, ulong table_id,
4929 bool is_transactional);
4930 virtual bool write();
4931 #endif
4932 #ifdef HAVE_REPLICATION
4933 Update_rows_compressed_log_event(const char *buf, uint event_len,
4934 const Format_description_log_event *description_event);
4935 #endif
4936 private:
4937 #if defined(MYSQL_CLIENT)
4938 bool print(FILE *file, PRINT_EVENT_INFO *print_event_info);
4939 #endif
4940 };
4941
4942 /**
4943 @class Delete_rows_log_event
4944
4945 Log row deletions. The event contain several delete rows for a
4946 table. Note that each event contains only rows for one table.
4947
4948 RESPONSIBILITIES
4949
4950 - Act as a container for rows that has been deleted on the master
4951 and should be deleted on the slave.
4952
4953 COLLABORATION
4954
4955 Row_writer
4956 Create the event and add rows to the event.
4957 Row_reader
4958 Extract the rows from the event.
4959
4960 @section Delete_rows_log_event_binary_format Binary Format
4961 */
4962 class Delete_rows_log_event : public Rows_log_event
4963 {
4964 public:
4965 enum
4966 {
4967 /* Support interface to THD::binlog_prepare_pending_rows_event */
4968 TYPE_CODE = DELETE_ROWS_EVENT
4969 };
4970
4971 #ifdef MYSQL_SERVER
4972 Delete_rows_log_event(THD*, TABLE*, ulong, bool is_transactional);
4973 #endif
4974 #ifdef HAVE_REPLICATION
4975 Delete_rows_log_event(const char *buf, uint event_len,
4976 const Format_description_log_event *description_event);
4977 #endif
4978 #ifdef MYSQL_SERVER
binlog_row_logging_function(THD * thd,TABLE * table,bool is_transactional,const uchar * before_record,const uchar * after_record)4979 static bool binlog_row_logging_function(THD *thd, TABLE *table,
4980 bool is_transactional,
4981 const uchar *before_record,
4982 const uchar *after_record
4983 __attribute__((unused)))
4984 {
4985 DBUG_ASSERT(!table->versioned(VERS_TRX_ID));
4986 return thd->binlog_delete_row(table, is_transactional,
4987 before_record);
4988 }
4989 #endif
4990
4991 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
4992 uint8 get_trg_event_map();
4993 #endif
4994
4995 protected:
get_general_type_code()4996 virtual Log_event_type get_general_type_code() { return (Log_event_type)TYPE_CODE; }
4997
4998 #ifdef MYSQL_CLIENT
4999 bool print(FILE *file, PRINT_EVENT_INFO *print_event_info);
5000 #endif
5001
5002 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
5003 virtual int do_before_row_operations(const Slave_reporting_capability *const);
5004 virtual int do_after_row_operations(const Slave_reporting_capability *const,int);
5005 virtual int do_exec_row(rpl_group_info *);
5006 #endif
5007 };
5008
5009 class Delete_rows_compressed_log_event : public Delete_rows_log_event
5010 {
5011 public:
5012 #if defined(MYSQL_SERVER)
5013 Delete_rows_compressed_log_event(THD*, TABLE*, ulong, bool is_transactional);
5014 virtual bool write();
5015 #endif
5016 #ifdef HAVE_REPLICATION
5017 Delete_rows_compressed_log_event(const char *buf, uint event_len,
5018 const Format_description_log_event *description_event);
5019 #endif
5020 private:
5021 #if defined(MYSQL_CLIENT)
5022 bool print(FILE *file, PRINT_EVENT_INFO *print_event_info);
5023 #endif
5024 };
5025
5026
5027 #include "log_event_old.h"
5028
5029 /**
5030 @class Incident_log_event
5031
5032 Class representing an incident, an occurence out of the ordinary,
5033 that happened on the master.
5034
5035 The event is used to inform the slave that something out of the
5036 ordinary happened on the master that might cause the database to be
5037 in an inconsistent state.
5038
5039 <table id="IncidentFormat">
5040 <caption>Incident event format</caption>
5041 <tr>
5042 <th>Symbol</th>
5043 <th>Format</th>
5044 <th>Description</th>
5045 </tr>
5046 <tr>
5047 <td>INCIDENT</td>
5048 <td align="right">2</td>
5049 <td>Incident number as an unsigned integer</td>
5050 </tr>
5051 <tr>
5052 <td>MSGLEN</td>
5053 <td align="right">1</td>
5054 <td>Message length as an unsigned integer</td>
5055 </tr>
5056 <tr>
5057 <td>MESSAGE</td>
5058 <td align="right">MSGLEN</td>
5059 <td>The message, if present. Not null terminated.</td>
5060 </tr>
5061 </table>
5062
5063 @section Delete_rows_log_event_binary_format Binary Format
5064 */
5065 class Incident_log_event : public Log_event {
5066 public:
5067 #ifdef MYSQL_SERVER
Incident_log_event(THD * thd_arg,Incident incident)5068 Incident_log_event(THD *thd_arg, Incident incident)
5069 : Log_event(thd_arg, 0, FALSE), m_incident(incident)
5070 {
5071 DBUG_ENTER("Incident_log_event::Incident_log_event");
5072 DBUG_PRINT("enter", ("m_incident: %d", m_incident));
5073 m_message.str= NULL; /* Just as a precaution */
5074 m_message.length= 0;
5075 set_direct_logging();
5076 /* Replicate the incident regardless of @@skip_replication. */
5077 flags&= ~LOG_EVENT_SKIP_REPLICATION_F;
5078 DBUG_VOID_RETURN;
5079 }
5080
Incident_log_event(THD * thd_arg,Incident incident,const LEX_CSTRING * msg)5081 Incident_log_event(THD *thd_arg, Incident incident, const LEX_CSTRING *msg)
5082 : Log_event(thd_arg, 0, FALSE), m_incident(incident)
5083 {
5084 DBUG_ENTER("Incident_log_event::Incident_log_event");
5085 DBUG_PRINT("enter", ("m_incident: %d", m_incident));
5086 m_message.length= 0;
5087 if (unlikely(!(m_message.str= (char*) my_malloc(msg->length+1,
5088 MYF(MY_WME)))))
5089 {
5090 /* Mark this event invalid */
5091 m_incident= INCIDENT_NONE;
5092 DBUG_VOID_RETURN;
5093 }
5094 strmake(m_message.str, msg->str, msg->length);
5095 m_message.length= msg->length;
5096 set_direct_logging();
5097 /* Replicate the incident regardless of @@skip_replication. */
5098 flags&= ~LOG_EVENT_SKIP_REPLICATION_F;
5099 DBUG_VOID_RETURN;
5100 }
5101 #endif
5102
5103 #ifdef MYSQL_SERVER
5104 void pack_info(Protocol*);
5105
5106 virtual bool write_data_header();
5107 virtual bool write_data_body();
5108 #endif
5109
5110 Incident_log_event(const char *buf, uint event_len,
5111 const Format_description_log_event *descr_event);
5112
5113 virtual ~Incident_log_event();
5114
5115 #ifdef MYSQL_CLIENT
5116 virtual bool print(FILE *file, PRINT_EVENT_INFO *print_event_info);
5117 #endif
5118
5119 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
5120 virtual int do_apply_event(rpl_group_info *rgi);
5121 #endif
5122
get_type_code()5123 virtual Log_event_type get_type_code() { return INCIDENT_EVENT; }
5124
is_valid()5125 virtual bool is_valid() const
5126 {
5127 return m_incident > INCIDENT_NONE && m_incident < INCIDENT_COUNT;
5128 }
get_data_size()5129 virtual int get_data_size() {
5130 return INCIDENT_HEADER_LEN + 1 + (uint) m_message.length;
5131 }
5132
5133 private:
5134 const char *description() const;
5135
5136 Incident m_incident;
5137 LEX_STRING m_message;
5138 };
5139
5140 /**
5141 @class Ignorable_log_event
5142
5143 Base class for ignorable log events. Events deriving from
5144 this class can be safely ignored by slaves that cannot
5145 recognize them. Newer slaves, will be able to read and
5146 handle them. This has been designed to be an open-ended
5147 architecture, so adding new derived events shall not harm
5148 the old slaves that support ignorable log event mechanism
5149 (they will just ignore unrecognized ignorable events).
5150
5151 @note The only thing that makes an event ignorable is that it has
5152 the LOG_EVENT_IGNORABLE_F flag set. It is not strictly necessary
5153 that ignorable event types derive from Ignorable_log_event; they may
5154 just as well derive from Log_event and pass LOG_EVENT_IGNORABLE_F as
5155 argument to the Log_event constructor.
5156 **/
5157
5158 class Ignorable_log_event : public Log_event {
5159 public:
5160 int number;
5161 const char *description;
5162
5163 #ifndef MYSQL_CLIENT
Ignorable_log_event(THD * thd_arg)5164 Ignorable_log_event(THD *thd_arg)
5165 :Log_event(thd_arg, LOG_EVENT_IGNORABLE_F, FALSE),
5166 number(0), description("internal")
5167 {
5168 DBUG_ENTER("Ignorable_log_event::Ignorable_log_event");
5169 DBUG_VOID_RETURN;
5170 }
5171 #endif
5172
5173 Ignorable_log_event(const char *buf,
5174 const Format_description_log_event *descr_event,
5175 const char *event_name);
5176 virtual ~Ignorable_log_event();
5177
5178 #ifndef MYSQL_CLIENT
5179 void pack_info(Protocol*);
5180 #endif
5181
5182 #ifdef MYSQL_CLIENT
5183 virtual bool print(FILE *file, PRINT_EVENT_INFO *print_event_info);
5184 #endif
5185
get_type_code()5186 virtual Log_event_type get_type_code() { return IGNORABLE_LOG_EVENT; }
5187
is_valid()5188 virtual bool is_valid() const { return 1; }
5189
get_data_size()5190 virtual int get_data_size() { return IGNORABLE_HEADER_LEN; }
5191 };
5192
5193 #ifdef MYSQL_CLIENT
5194 bool copy_cache_to_string_wrapped(IO_CACHE *body,
5195 LEX_STRING *to,
5196 bool do_wrap,
5197 const char *delimiter,
5198 bool is_verbose);
5199 bool copy_cache_to_file_wrapped(IO_CACHE *body,
5200 FILE *file,
5201 bool do_wrap,
5202 const char *delimiter,
5203 bool is_verbose);
5204 #endif
5205
5206 #ifdef MYSQL_SERVER
5207 /*****************************************************************************
5208
5209 Heartbeat Log Event class
5210
5211 Replication event to ensure to slave that master is alive.
5212 The event is originated by master's dump thread and sent straight to
5213 slave without being logged. Slave itself does not store it in relay log
5214 but rather uses a data for immediate checks and throws away the event.
5215
5216 Two members of the class log_ident and Log_event::log_pos comprise
5217 @see the event_coordinates instance. The coordinates that a heartbeat
5218 instance carries correspond to the last event master has sent from
5219 its binlog.
5220
5221 ****************************************************************************/
5222 class Heartbeat_log_event: public Log_event
5223 {
5224 public:
5225 uint8 hb_flags;
5226 Heartbeat_log_event(const char* buf, ulong event_len,
5227 const Format_description_log_event* description_event);
get_type_code()5228 Log_event_type get_type_code() { return HEARTBEAT_LOG_EVENT; }
is_valid()5229 bool is_valid() const
5230 {
5231 return (log_ident != NULL && ident_len <= FN_REFLEN-1 &&
5232 log_pos >= BIN_LOG_HEADER_SIZE);
5233 }
get_log_ident()5234 const char * get_log_ident() { return log_ident; }
get_ident_len()5235 uint get_ident_len() { return ident_len; }
5236
5237 private:
5238 const char* log_ident;
5239 uint ident_len;
5240 };
5241
write(Log_event * ev)5242 inline int Log_event_writer::write(Log_event *ev)
5243 {
5244 ev->writer= this;
5245 int res= ev->write();
5246 IF_DBUG(ev->writer= 0,); // writer must be set before every Log_event::write
5247 add_status(ev->logged_status());
5248 return res;
5249 }
5250
5251 /**
5252 The function is called by slave applier in case there are
5253 active table filtering rules to force gathering events associated
5254 with Query-log-event into an array to execute
5255 them once the fate of the Query is determined for execution.
5256 */
5257 bool slave_execute_deferred_events(THD *thd);
5258 #endif
5259
5260 bool event_that_should_be_ignored(const char *buf);
5261 bool event_checksum_test(uchar *buf, ulong event_len, enum_binlog_checksum_alg alg);
5262 enum enum_binlog_checksum_alg get_checksum_alg(const char* buf, ulong len);
5263 extern TYPELIB binlog_checksum_typelib;
5264 #ifdef WITH_WSREP
5265 enum Log_event_type wsrep_peak_event(rpl_group_info *rgi, ulonglong* event_size);
5266 #endif /* WITH_WSREP */
5267
5268 /**
5269 @} (end of group Replication)
5270 */
5271
5272
5273 int binlog_buf_compress(const char *src, char *dst, uint32 len, uint32 *comlen);
5274 int binlog_buf_uncompress(const char *src, char *dst, uint32 len, uint32 *newlen);
5275 uint32 binlog_get_compress_len(uint32 len);
5276 uint32 binlog_get_uncompress_len(const char *buf);
5277
5278 int query_event_uncompress(const Format_description_log_event *description_event, bool contain_checksum,
5279 const char *src, ulong src_len, char* buf, ulong buf_size, bool* is_malloc,
5280 char **dst, ulong *newlen);
5281
5282 int row_log_event_uncompress(const Format_description_log_event *description_event, bool contain_checksum,
5283 const char *src, ulong src_len, char* buf, ulong buf_size, bool* is_malloc,
5284 char **dst, ulong *newlen);
5285
5286
5287 #endif /* _log_event_h */
5288