1 /* Copyright (c) 2005, 2017, 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 #ifndef RPL_RLI_H 18 #define RPL_RLI_H 19 20 #include "rpl_tblmap.h" 21 #include "rpl_reporting.h" 22 #include "rpl_utility.h" 23 #include "log.h" /* LOG_INFO, MYSQL_BIN_LOG */ 24 #include "sql_class.h" /* THD */ 25 #include "log_event.h" 26 #include "rpl_parallel.h" 27 28 struct RPL_TABLE_LIST; 29 class Master_info; 30 class Rpl_filter; 31 32 33 /**************************************************************************** 34 35 Replication SQL Thread 36 37 Relay_log_info contains: 38 - the current relay log 39 - the current relay log offset 40 - master log name 41 - master log sequence corresponding to the last update 42 - misc information specific to the SQL thread 43 44 Relay_log_info is initialized from the slave.info file if such 45 exists. Otherwise, data members are intialized with defaults. The 46 initialization is done with Relay_log_info::init() call. 47 48 The format of slave.info file: 49 50 relay_log_name 51 relay_log_pos 52 master_log_name 53 master_log_pos 54 55 To clean up, call end_relay_log_info() 56 57 *****************************************************************************/ 58 59 struct rpl_group_info; 60 struct inuse_relaylog; 61 62 class Relay_log_info : public Slave_reporting_capability 63 { 64 public: 65 /** 66 Flags for the state of reading the relay log. Note that these are 67 bit masks. 68 */ 69 enum enum_state_flag { 70 /** We are inside a group of events forming a statement */ 71 IN_STMT=1, 72 /** We have inside a transaction */ 73 IN_TRANSACTION=2 74 }; 75 76 /* 77 The SQL thread owns one Relay_log_info, and each client that has 78 executed a BINLOG statement owns one Relay_log_info. This function 79 returns zero for the Relay_log_info object that belongs to the SQL 80 thread and nonzero for Relay_log_info objects that belong to 81 clients. 82 */ belongs_to_client()83 inline bool belongs_to_client() 84 { 85 DBUG_ASSERT(sql_driver_thd); 86 return !sql_driver_thd->slave_thread; 87 } 88 89 /* 90 If true, events with the same server id should be replicated. This 91 field is set on creation of a relay log info structure by copying 92 the value of ::replicate_same_server_id and can be overridden if 93 necessary. For example of when this is done, check sql_binlog.cc, 94 where the BINLOG statement can be used to execute "raw" events. 95 */ 96 bool replicate_same_server_id; 97 98 /*** The following variables can only be read when protect by data lock ****/ 99 100 /* 101 info_fd - file descriptor of the info file. set only during 102 initialization or clean up - safe to read anytime 103 cur_log_fd - file descriptor of the current read relay log 104 */ 105 File info_fd,cur_log_fd; 106 107 /* 108 Protected with internal locks. 109 Must get data_lock when resetting the logs. 110 */ 111 MYSQL_BIN_LOG relay_log; 112 LOG_INFO linfo; 113 114 /* 115 cur_log 116 Pointer that either points at relay_log.get_log_file() or 117 &rli->cache_buf, depending on whether the log is hot or there was 118 the need to open a cold relay_log. 119 120 cache_buf 121 IO_CACHE used when opening cold relay logs. 122 */ 123 IO_CACHE cache_buf,*cur_log; 124 125 /* 126 Keeps track of the number of transactions that commits 127 before fsyncing. The option --sync-relay-log-info determines 128 how many transactions should commit before fsyncing. 129 */ 130 uint sync_counter; 131 132 /* 133 Identifies when the recovery process is going on. 134 See sql/slave.cc:init_recovery for further details. 135 */ 136 bool is_relay_log_recovery; 137 138 /* The following variables are safe to read any time */ 139 140 /* IO_CACHE of the info file - set only during init or end */ 141 IO_CACHE info_file; 142 143 /* 144 List of temporary tables used by this connection. 145 This is updated when a temporary table is created or dropped by 146 a replication thread. 147 148 Not reset when replication ends, to allow one to access the tables 149 when replication restarts. 150 151 Protected by data_lock. 152 */ 153 All_tmp_tables_list *save_temporary_tables; 154 155 /* 156 standard lock acquisition order to avoid deadlocks: 157 run_lock, data_lock, relay_log.LOCK_log, relay_log.LOCK_index 158 */ 159 mysql_mutex_t data_lock, run_lock; 160 /* 161 start_cond is broadcast when SQL thread is started 162 stop_cond - when stopped 163 data_cond - when data protected by data_lock changes 164 */ 165 mysql_cond_t start_cond, stop_cond, data_cond; 166 /* parent Master_info structure */ 167 Master_info *mi; 168 169 /* 170 List of active relay log files. 171 (This can be more than one in case of parallel replication). 172 */ 173 inuse_relaylog *inuse_relaylog_list; 174 inuse_relaylog *last_inuse_relaylog; 175 176 /* 177 Needed to deal properly with cur_log getting closed and re-opened with 178 a different log under our feet 179 */ 180 uint32 cur_log_old_open_count; 181 182 /* 183 If on init_info() call error_on_rli_init_info is true that means 184 that previous call to init_info() terminated with an error, RESET 185 SLAVE must be executed and the problem fixed manually. 186 */ 187 bool error_on_rli_init_info; 188 189 /* 190 Let's call a group (of events) : 191 - a transaction 192 or 193 - an autocommiting query + its associated events (INSERT_ID, 194 TIMESTAMP...) 195 We need these rli coordinates : 196 - relay log name and position of the beginning of the group we currently 197 are executing. Needed to know where we have to restart when replication has 198 stopped in the middle of a group (which has been rolled back by the slave). 199 - relay log name and position just after the event we have just 200 executed. This event is part of the current group. 201 Formerly we only had the immediately above coordinates, plus a 'pending' 202 variable, but this dealt wrong with the case of a transaction starting on a 203 relay log and finishing (commiting) on another relay log. Case which can 204 happen when, for example, the relay log gets rotated because of 205 max_binlog_size. 206 207 Note: group_relay_log_name, group_relay_log_pos must only be 208 written from the thread owning the Relay_log_info (SQL thread if 209 !belongs_to_client(); client thread executing BINLOG statement if 210 belongs_to_client()). 211 */ 212 char group_relay_log_name[FN_REFLEN]; 213 ulonglong group_relay_log_pos; 214 char event_relay_log_name[FN_REFLEN]; 215 ulonglong event_relay_log_pos; 216 ulonglong future_event_relay_log_pos; 217 /* 218 The master log name for current event. Only used in parallel replication. 219 */ 220 char future_event_master_log_name[FN_REFLEN]; 221 222 /* 223 Original log name and position of the group we're currently executing 224 (whose coordinates are group_relay_log_name/pos in the relay log) 225 in the master's binlog. These concern the *group*, because in the master's 226 binlog the log_pos that comes with each event is the position of the 227 beginning of the group. 228 229 Note: group_master_log_name, group_master_log_pos must only be 230 written from the thread owning the Relay_log_info (SQL thread if 231 !belongs_to_client(); client thread executing BINLOG statement if 232 belongs_to_client()). 233 */ 234 char group_master_log_name[FN_REFLEN]; 235 volatile my_off_t group_master_log_pos; 236 237 /* 238 Handling of the relay_log_space_limit optional constraint. 239 ignore_log_space_limit is used to resolve a deadlock between I/O and SQL 240 threads, the SQL thread sets it to unblock the I/O thread and make it 241 temporarily forget about the constraint. 242 */ 243 ulonglong log_space_limit; 244 Atomic_counter<uint64> log_space_total; 245 bool ignore_log_space_limit; 246 247 /* 248 Used by the SQL thread to instructs the IO thread to rotate 249 the logs when the SQL thread needs to purge to release some 250 disk space. 251 */ 252 bool sql_force_rotate_relay; 253 254 time_t last_master_timestamp; 255 /* 256 The SQL driver thread sets this true while it is waiting at the end of the 257 relay log for more events to arrive. SHOW SLAVE STATUS uses this to report 258 Seconds_Behind_Master as zero while the SQL thread is so waiting. 259 */ 260 bool sql_thread_caught_up; 261 262 void clear_until_condition(); 263 /** 264 Reset the delay. 265 This is used by RESET SLAVE to clear the delay. 266 */ clear_sql_delay()267 void clear_sql_delay() 268 { 269 sql_delay= 0; 270 } 271 272 273 /* 274 Needed for problems when slave stops and we want to restart it 275 skipping one or more events in the master log that have caused 276 errors, and have been manually applied by DBA already. 277 Must be ulong as it's referred to from set_var.cc 278 */ 279 volatile ulonglong slave_skip_counter; 280 ulonglong max_relay_log_size; 281 282 volatile ulong abort_pos_wait; /* Incremented on change master */ 283 volatile ulong slave_run_id; /* Incremented on slave start */ 284 mysql_mutex_t log_space_lock; 285 mysql_cond_t log_space_cond; 286 /* 287 THD for the main sql thread, the one that starts threads to process 288 slave requests. If there is only one thread, then this THD is also 289 used for SQL processing. 290 A kill sent to this THD will kill the replication. 291 */ 292 THD *sql_driver_thd; 293 #ifndef DBUG_OFF 294 int events_till_abort; 295 #endif 296 297 enum_gtid_skip_type gtid_skip_flag; 298 299 /* 300 inited changes its value within LOCK_active_mi-guarded critical 301 sections at times of start_slave_threads() (0->1) and end_slave() (1->0). 302 Readers may not acquire the mutex while they realize potential concurrency 303 issue. 304 If not set, the value of other members of the structure are undefined. 305 */ 306 volatile bool inited; 307 volatile bool abort_slave; 308 volatile bool stop_for_until; 309 volatile uint slave_running; 310 311 /* 312 Condition and its parameters from START SLAVE UNTIL clause. 313 314 UNTIL condition is tested with is_until_satisfied() method that is 315 called by exec_relay_log_event(). is_until_satisfied() caches the result 316 of the comparison of log names because log names don't change very often; 317 this cache is invalidated by parts of code which change log names with 318 notify_*_log_name_updated() methods. (They need to be called only if SQL 319 thread is running). 320 */ 321 322 enum { 323 UNTIL_NONE= 0, UNTIL_MASTER_POS, UNTIL_RELAY_POS, UNTIL_GTID 324 } until_condition; 325 char until_log_name[FN_REFLEN]; 326 ulonglong until_log_pos; 327 /* extension extracted from log_name and converted to int */ 328 ulong until_log_name_extension; 329 /* 330 Cached result of comparison of until_log_name and current log name 331 -2 means unitialised, -1,0,1 are comarison results 332 */ 333 enum 334 { 335 UNTIL_LOG_NAMES_CMP_UNKNOWN= -2, UNTIL_LOG_NAMES_CMP_LESS= -1, 336 UNTIL_LOG_NAMES_CMP_EQUAL= 0, UNTIL_LOG_NAMES_CMP_GREATER= 1 337 } until_log_names_cmp_result; 338 /* Condition for UNTIL master_gtid_pos. */ 339 slave_connection_state until_gtid_pos; 340 341 /* 342 retried_trans is a cumulative counter: how many times the slave 343 has retried a transaction (any) since slave started. 344 Protected by data_lock. 345 */ 346 ulong retried_trans; 347 /* 348 Number of executed events for SLAVE STATUS. 349 Protected by slave_executed_entries_lock 350 */ 351 Atomic_counter<uint32_t> executed_entries; 352 353 /* 354 If the end of the hot relay log is made of master's events ignored by the 355 slave I/O thread, these two keep track of the coords (in the master's 356 binlog) of the last of these events seen by the slave I/O thread. If not, 357 ign_master_log_name_end[0] == 0. 358 As they are like a Rotate event read/written from/to the relay log, they 359 are both protected by rli->relay_log.LOCK_log. 360 */ 361 char ign_master_log_name_end[FN_REFLEN]; 362 ulonglong ign_master_log_pos_end; 363 /* Similar for ignored GTID events. */ 364 slave_connection_state ign_gtids; 365 366 /* 367 Indentifies where the SQL Thread should create temporary files for the 368 LOAD DATA INFILE. This is used for security reasons. 369 */ 370 char slave_patternload_file[FN_REFLEN]; 371 size_t slave_patternload_file_size; 372 373 rpl_parallel parallel; 374 /* 375 The relay_log_state keeps track of the current binlog state of the 376 execution of the relay log. This is used to know where to resume 377 current GTID position if the slave thread is stopped and 378 restarted. It is only accessed from the SQL thread, so it does 379 not need any locking. 380 */ 381 rpl_binlog_state relay_log_state; 382 /* 383 The restart_gtid_state is used when the SQL thread restarts on a relay log 384 in GTID mode. In multi-domain parallel replication, each domain may have a 385 separat position, so some events in more progressed domains may need to be 386 skipped. This keeps track of the domains that have not yet reached their 387 starting event. 388 */ 389 slave_connection_state restart_gtid_pos; 390 391 Relay_log_info(bool is_slave_recovery, const char* thread_name= "SQL"); 392 ~Relay_log_info(); 393 394 /* 395 Invalidate cached until_log_name and group_relay_log_name comparison 396 result. Should be called after any update of group_realy_log_name if 397 there chances that sql_thread is running. 398 */ notify_group_relay_log_name_update()399 inline void notify_group_relay_log_name_update() 400 { 401 if (until_condition==UNTIL_RELAY_POS) 402 until_log_names_cmp_result= UNTIL_LOG_NAMES_CMP_UNKNOWN; 403 } 404 405 /* 406 The same as previous but for group_master_log_name. 407 */ notify_group_master_log_name_update()408 inline void notify_group_master_log_name_update() 409 { 410 if (until_condition==UNTIL_MASTER_POS) 411 until_log_names_cmp_result= UNTIL_LOG_NAMES_CMP_UNKNOWN; 412 } 413 414 void inc_group_relay_log_pos(ulonglong log_pos, 415 rpl_group_info *rgi, 416 bool skip_lock=0); 417 418 int wait_for_pos(THD* thd, String* log_name, longlong log_pos, 419 longlong timeout); 420 void close_temporary_tables(); 421 422 /* Check if UNTIL condition is satisfied. See slave.cc for more. */ 423 bool is_until_satisfied(Log_event *ev); until_pos()424 inline ulonglong until_pos() 425 { 426 DBUG_ASSERT(until_condition == UNTIL_MASTER_POS || 427 until_condition == UNTIL_RELAY_POS); 428 return ((until_condition == UNTIL_MASTER_POS) ? group_master_log_pos : 429 group_relay_log_pos); 430 } until_name()431 inline char *until_name() 432 { 433 DBUG_ASSERT(until_condition == UNTIL_MASTER_POS || 434 until_condition == UNTIL_RELAY_POS); 435 return ((until_condition == UNTIL_MASTER_POS) ? group_master_log_name : 436 group_relay_log_name); 437 } 438 /** 439 Helper function to do after statement completion. 440 441 This function is called from an event to complete the group by 442 either stepping the group position, if the "statement" is not 443 inside a transaction; or increase the event position, if the 444 "statement" is inside a transaction. 445 446 @param event_log_pos 447 Master log position of the event. The position is recorded in the 448 relay log info and used to produce information for <code>SHOW 449 SLAVE STATUS</code>. 450 */ 451 bool stmt_done(my_off_t event_log_pos, THD *thd, rpl_group_info *rgi); 452 int alloc_inuse_relaylog(const char *name); 453 void free_inuse_relaylog(inuse_relaylog *ir); 454 void reset_inuse_relaylog(); 455 int update_relay_log_state(rpl_gtid *gtid_list, uint32 count); 456 457 /** 458 Is the replication inside a group? 459 460 The reader of the relay log is inside a group if either: 461 - The IN_TRANSACTION flag is set, meaning we're inside a transaction 462 - The IN_STMT flag is set, meaning we have read at least one row from 463 a multi-event entry. 464 465 This flag reflects the state of the log 'just now', ie after the last 466 read event would be executed. 467 This allow us to test if we can stop replication before reading 468 the next entry. 469 470 @retval true Replication thread is currently inside a group 471 @retval false Replication thread is currently not inside a group 472 */ is_in_group()473 bool is_in_group() const { 474 return (m_flags & (IN_STMT | IN_TRANSACTION)); 475 } 476 477 /** 478 Set the value of a replication state flag. 479 480 @param flag Flag to set 481 */ set_flag(enum_state_flag flag)482 void set_flag(enum_state_flag flag) 483 { 484 m_flags|= flag; 485 } 486 487 /** 488 Get the value of a replication state flag. 489 490 @param flag Flag to get value of 491 492 @return @c true if the flag was set, @c false otherwise. 493 */ get_flag(enum_state_flag flag)494 bool get_flag(enum_state_flag flag) 495 { 496 return m_flags & flag; 497 } 498 499 /** 500 Clear the value of a replication state flag. 501 502 @param flag Flag to clear 503 */ clear_flag(enum_state_flag flag)504 void clear_flag(enum_state_flag flag) 505 { 506 m_flags&= ~flag; 507 } 508 509 /** 510 Text used in THD::proc_info when the slave SQL thread is delaying. 511 */ 512 static const char *const state_delaying_string; 513 514 bool flush(); 515 516 /** 517 Reads the relay_log.info file. 518 */ 519 int init(const char* info_filename); 520 521 /** 522 Indicate that a delay starts. 523 524 This does not actually sleep; it only sets the state of this 525 Relay_log_info object to delaying so that the correct state can be 526 reported by SHOW SLAVE STATUS and SHOW PROCESSLIST. 527 528 Requires rli->data_lock. 529 530 @param delay_end The time when the delay shall end. 531 */ start_sql_delay(time_t delay_end)532 void start_sql_delay(time_t delay_end) 533 { 534 mysql_mutex_assert_owner(&data_lock); 535 sql_delay_end= delay_end; 536 thd_proc_info(sql_driver_thd, state_delaying_string); 537 } 538 get_sql_delay()539 int32 get_sql_delay() { return sql_delay; } set_sql_delay(int32 _sql_delay)540 void set_sql_delay(int32 _sql_delay) { sql_delay= _sql_delay; } get_sql_delay_end()541 time_t get_sql_delay_end() { return sql_delay_end; } 542 543 private: 544 545 546 /** 547 Delay slave SQL thread by this amount, compared to master (in 548 seconds). This is set with CHANGE MASTER TO MASTER_DELAY=X. 549 550 Guarded by data_lock. Initialized by the client thread executing 551 START SLAVE. Written by client threads executing CHANGE MASTER TO 552 MASTER_DELAY=X. Read by SQL thread and by client threads 553 executing SHOW SLAVE STATUS. Note: must not be written while the 554 slave SQL thread is running, since the SQL thread reads it without 555 a lock when executing Relay_log_info::flush(). 556 */ 557 int sql_delay; 558 559 /** 560 During a delay, specifies the point in time when the delay ends. 561 562 This is used for the SQL_Remaining_Delay column in SHOW SLAVE STATUS. 563 564 Guarded by data_lock. Written by the sql thread. Read by client 565 threads executing SHOW SLAVE STATUS. 566 */ 567 time_t sql_delay_end; 568 569 /* 570 Before the MASTER_DELAY parameter was added (WL#344), 571 relay_log.info had 4 lines. Now it has 5 lines. 572 */ 573 static const int LINES_IN_RELAY_LOG_INFO_WITH_DELAY= 5; 574 /* 575 Hint for when to stop event distribution by sql driver thread. 576 The flag is set ON by a non-group event when this event is in the middle 577 of a group (e.g a transaction group) so it's too early 578 to refresh the current-relay-log vs until-log cached comparison result. 579 And it is checked and to decide whether it's a right time to do so 580 when the being processed group has been fully scheduled. 581 */ 582 bool until_relay_log_names_defer; 583 584 /* 585 Holds the state of the data in the relay log. 586 We need this to ensure that we are not in the middle of a 587 statement or inside BEGIN ... COMMIT when should rotate the 588 relay log. 589 */ 590 uint32 m_flags; 591 }; 592 593 594 /* 595 In parallel replication, if we need to re-try a transaction due to a 596 deadlock or other temporary error, we may need to go back and re-read events 597 out of an earlier relay log. 598 599 This structure keeps track of the relaylogs that are potentially in use. 600 Each rpl_group_info has a pointer to one of those, corresponding to the 601 first GTID event. 602 603 A pair of reference count keeps track of how long a relay log is potentially 604 in use. When the `completed' flag is set, all events have been read out of 605 the relay log, but the log might still be needed for retry in worker 606 threads. As worker threads complete an event group, they increment 607 atomically the `dequeued_count' with number of events queued. Thus, when 608 completed is set and dequeued_count equals queued_count, the relay log file 609 is finally done with and can be purged. 610 611 By separating the queued and dequeued count, only the dequeued_count needs 612 multi-thread synchronisation; the completed flag and queued_count fields 613 are only accessed by the SQL driver thread and need no synchronisation. 614 */ 615 struct inuse_relaylog { 616 inuse_relaylog *next; 617 Relay_log_info *rli; 618 /* 619 relay_log_state holds the binlog state corresponding to the start of this 620 relay log file. It is an array with relay_log_state_count elements. 621 */ 622 rpl_gtid *relay_log_state; 623 uint32 relay_log_state_count; 624 /* Number of events in this relay log queued for worker threads. */ 625 int64 queued_count; 626 /* Number of events completed by worker threads. */ 627 Atomic_counter<int64> dequeued_count; 628 /* Set when all events have been read from a relaylog. */ 629 bool completed; 630 char name[FN_REFLEN]; 631 inuse_relayloginuse_relaylog632 inuse_relaylog(Relay_log_info *rli_arg, rpl_gtid *relay_log_state_arg, 633 uint32 relay_log_state_count_arg, 634 const char *name_arg): 635 next(0), rli(rli_arg), relay_log_state(relay_log_state_arg), 636 relay_log_state_count(relay_log_state_count_arg), queued_count(0), 637 dequeued_count(0), completed(false) 638 { 639 strmake_buf(name, name_arg); 640 } 641 }; 642 643 644 /* 645 This is data for various state needed to be kept for the processing of 646 one event group (transaction) during replication. 647 648 In single-threaded replication, there will be one global rpl_group_info and 649 one global Relay_log_info per master connection. They will be linked 650 together. 651 652 In parallel replication, there will be one rpl_group_info object for 653 each running sql thread, each having their own thd. 654 655 All rpl_group_info will share the same Relay_log_info. 656 */ 657 658 struct rpl_group_info 659 { 660 rpl_group_info *next; /* For free list in rpl_parallel_thread */ 661 Relay_log_info *rli; 662 THD *thd; 663 /* 664 Current GTID being processed. 665 The sub_id gives the binlog order within one domain_id. A zero sub_id 666 means that there is no active GTID. 667 */ 668 uint64 gtid_sub_id; 669 rpl_gtid current_gtid; 670 uint64 commit_id; 671 /* 672 This is used to keep transaction commit order. 673 We will signal this when we commit, and can register it to wait for the 674 commit_orderer of the previous commit to signal us. 675 */ 676 wait_for_commit commit_orderer; 677 /* 678 If non-zero, the sub_id of a prior event group whose commit we have to wait 679 for before committing ourselves. Then wait_commit_group_info points to the 680 event group to wait for. 681 682 Before using this, rpl_parallel_entry::last_committed_sub_id should be 683 compared against wait_commit_sub_id. Only if last_committed_sub_id is 684 smaller than wait_commit_sub_id must the wait be done (otherwise the 685 waited-for transaction is already committed, so we would otherwise wait 686 for the wrong commit). 687 */ 688 uint64 wait_commit_sub_id; 689 rpl_group_info *wait_commit_group_info; 690 /* 691 This holds a pointer to a struct that keeps track of the need to wait 692 for the previous batch of event groups to reach the commit stage, before 693 this batch can start to execute. 694 695 (When we execute in parallel the transactions that group committed 696 together on the master, we still need to wait for any prior transactions 697 to have reached the commit stage). 698 699 The pointed-to gco is only valid for as long as 700 gtid_sub_id < parallel_entry->last_committed_sub_id. After that, it can 701 be freed by another thread. 702 */ 703 group_commit_orderer *gco; 704 705 struct rpl_parallel_entry *parallel_entry; 706 707 /* 708 A container to hold on Intvar-, Rand-, Uservar- log-events in case 709 the slave is configured with table filtering rules. 710 The withhold events are executed when their parent Query destiny is 711 determined for execution as well. 712 */ 713 Deferred_log_events *deferred_events; 714 715 /* 716 State of the container: true stands for IRU events gathering, 717 false does for execution, either deferred or direct. 718 */ 719 bool deferred_events_collecting; 720 721 Annotate_rows_log_event *m_annotate_event; 722 723 RPL_TABLE_LIST *tables_to_lock; /* RBR: Tables to lock */ 724 uint tables_to_lock_count; /* RBR: Count of tables to lock */ 725 table_mapping m_table_map; /* RBR: Mapping table-id to table */ 726 mysql_mutex_t sleep_lock; 727 mysql_cond_t sleep_cond; 728 729 /* 730 trans_retries varies between 0 to slave_transaction_retries and counts how 731 many times the slave has retried the present transaction; gets reset to 0 732 when the transaction finally succeeds. 733 */ 734 ulong trans_retries; 735 736 /* 737 Used to defer stopping the SQL thread to give it a chance 738 to finish up the current group of events. 739 The timestamp is set and reset in @c sql_slave_killed(). 740 */ 741 time_t last_event_start_time; 742 743 char *event_relay_log_name; 744 char event_relay_log_name_buf[FN_REFLEN]; 745 ulonglong event_relay_log_pos; 746 ulonglong future_event_relay_log_pos; 747 /* 748 The master log name for current event. Only used in parallel replication. 749 */ 750 char future_event_master_log_name[FN_REFLEN]; 751 bool is_parallel_exec; 752 /* When gtid_pending is true, we have not yet done record_gtid(). */ 753 bool gtid_pending; 754 int worker_error; 755 /* 756 Set true when we signalled that we reach the commit phase. Used to avoid 757 counting one event group twice. 758 */ 759 bool did_mark_start_commit; 760 /* Copy of flags2 from GTID event. */ 761 uchar gtid_ev_flags2; 762 enum { 763 GTID_DUPLICATE_NULL=0, 764 GTID_DUPLICATE_IGNORE=1, 765 GTID_DUPLICATE_OWNER=2 766 }; 767 /* 768 When --gtid-ignore-duplicates, this is set to one of the above three 769 values: 770 GTID_DUPLICATE_NULL - Not using --gtid-ignore-duplicates. 771 GTID_DUPLICATE_IGNORE - This gtid already applied, skip the event group. 772 GTID_DUPLICATE_OWNER - We are the current owner of the domain, and must 773 apply the event group and then release the domain. 774 */ 775 uint8 gtid_ignore_duplicate_state; 776 777 /* 778 Runtime state for printing a note when slave is taking 779 too long while processing a row event. 780 */ 781 longlong row_stmt_start_timestamp; 782 bool long_find_row_note_printed; 783 /* Needs room for "Gtid D-S-N\x00". */ 784 char gtid_info_buf[5+10+1+10+1+20+1]; 785 786 /* 787 The timestamp, from the master, of the commit event. 788 Used to do delayed update of rli->last_master_timestamp, for getting 789 reasonable values out of Seconds_Behind_Master in SHOW SLAVE STATUS. 790 */ 791 time_t last_master_timestamp; 792 793 /* 794 Information to be able to re-try an event group in case of a deadlock or 795 other temporary error. 796 */ 797 inuse_relaylog *relay_log; 798 uint64 retry_start_offset; 799 uint64 retry_event_count; 800 /* 801 If `speculation' is != SPECULATE_NO, then we are optimistically running 802 this transaction in parallel, even though it might not be safe (there may 803 be a conflict with a prior event group). 804 805 In this case, a conflict can cause other errors than deadlocks (like 806 duplicate key for example). So in case of _any_ error, we need to roll 807 back and retry the event group. 808 */ 809 enum enum_speculation { 810 /* 811 This transaction was group-committed together on the master with the 812 other transactions with which it is replicated in parallel. 813 */ 814 SPECULATE_NO, 815 /* 816 We will optimistically try to run this transaction in parallel with 817 other transactions, even though it is not known to be conflict free. 818 If we get a conflict, we will detect it as a deadlock, roll back and 819 retry. 820 */ 821 SPECULATE_OPTIMISTIC, 822 /* 823 This transaction got a conflict during speculative parallel apply, or 824 it was marked on the master as likely to cause a conflict or unsafe to 825 speculate. So it will wait for the prior transaction to commit before 826 starting to replicate. 827 */ 828 SPECULATE_WAIT 829 } speculation; 830 enum enum_retry_killed { 831 RETRY_KILL_NONE = 0, 832 RETRY_KILL_PENDING, 833 RETRY_KILL_KILLED 834 }; 835 uchar killed_for_retry; 836 837 rpl_group_info(Relay_log_info *rli_); 838 ~rpl_group_info(); 839 void reinit(Relay_log_info *rli); 840 841 /* 842 Returns true if the argument event resides in the containter; 843 more specifically, the checking is done against the last added event. 844 */ is_deferred_eventrpl_group_info845 bool is_deferred_event(Log_event * ev) 846 { 847 return deferred_events_collecting ? deferred_events->is_last(ev) : false; 848 }; 849 /* The general cleanup that slave applier may need at the end of query. */ cleanup_after_queryrpl_group_info850 inline void cleanup_after_query() 851 { 852 if (deferred_events) 853 deferred_events->rewind(); 854 }; 855 /* The general cleanup that slave applier may need at the end of session. */ cleanup_after_sessionrpl_group_info856 void cleanup_after_session() 857 { 858 if (deferred_events) 859 { 860 delete deferred_events; 861 deferred_events= NULL; 862 } 863 }; 864 865 /** 866 Save pointer to Annotate_rows event and switch on the 867 binlog_annotate_row_events for this sql thread. 868 To be called when sql thread receives an Annotate_rows event. 869 */ set_annotate_eventrpl_group_info870 inline void set_annotate_event(Annotate_rows_log_event *event) 871 { 872 DBUG_ASSERT(m_annotate_event == NULL); 873 m_annotate_event= event; 874 this->thd->variables.binlog_annotate_row_events= 1; 875 } 876 877 /** 878 Returns pointer to the saved Annotate_rows event or NULL if there is 879 no saved event. 880 */ get_annotate_eventrpl_group_info881 inline Annotate_rows_log_event* get_annotate_event() 882 { 883 return m_annotate_event; 884 } 885 886 /** 887 Delete saved Annotate_rows event (if any) and switch off the 888 binlog_annotate_row_events for this sql thread. 889 To be called when sql thread has applied the last (i.e. with 890 STMT_END_F flag) rbr event. 891 */ free_annotate_eventrpl_group_info892 inline void free_annotate_event() 893 { 894 if (m_annotate_event) 895 { 896 this->thd->variables.binlog_annotate_row_events= 0; 897 delete m_annotate_event; 898 m_annotate_event= 0; 899 } 900 } 901 get_table_datarpl_group_info902 bool get_table_data(TABLE *table_arg, table_def **tabledef_var, TABLE **conv_table_var) const 903 { 904 DBUG_ASSERT(tabledef_var && conv_table_var); 905 for (TABLE_LIST *ptr= tables_to_lock ; ptr != NULL ; ptr= ptr->next_global) 906 if (ptr->table == table_arg) 907 { 908 *tabledef_var= &static_cast<RPL_TABLE_LIST*>(ptr)->m_tabledef; 909 *conv_table_var= static_cast<RPL_TABLE_LIST*>(ptr)->m_conv_table; 910 DBUG_PRINT("debug", ("Fetching table data for table %s.%s:" 911 " tabledef: %p, conv_table: %p", 912 table_arg->s->db.str, table_arg->s->table_name.str, 913 *tabledef_var, *conv_table_var)); 914 return true; 915 } 916 return false; 917 } 918 919 void clear_tables_to_lock(); 920 void cleanup_context(THD *, bool); 921 void slave_close_thread_tables(THD *); 922 void mark_start_commit_no_lock(); 923 void mark_start_commit(); 924 char *gtid_info(); 925 void unmark_start_commit(); 926 get_row_stmt_start_timestamprpl_group_info927 longlong get_row_stmt_start_timestamp() 928 { 929 return row_stmt_start_timestamp; 930 } 931 set_row_stmt_start_timestamprpl_group_info932 void set_row_stmt_start_timestamp() 933 { 934 if (row_stmt_start_timestamp == 0) 935 row_stmt_start_timestamp= microsecond_interval_timer(); 936 } 937 reset_row_stmt_start_timestamprpl_group_info938 void reset_row_stmt_start_timestamp() 939 { 940 row_stmt_start_timestamp= 0; 941 } 942 set_long_find_row_note_printedrpl_group_info943 void set_long_find_row_note_printed() 944 { 945 long_find_row_note_printed= true; 946 } 947 unset_long_find_row_note_printedrpl_group_info948 void unset_long_find_row_note_printed() 949 { 950 long_find_row_note_printed= false; 951 } 952 is_long_find_row_note_printedrpl_group_info953 bool is_long_find_row_note_printed() 954 { 955 return long_find_row_note_printed; 956 } 957 inc_event_relay_log_posrpl_group_info958 inline void inc_event_relay_log_pos() 959 { 960 if (!is_parallel_exec) 961 rli->event_relay_log_pos= future_event_relay_log_pos; 962 } 963 }; 964 965 966 /* 967 The class rpl_sql_thread_info is the THD::system_thread_info for an SQL 968 thread; this is either the driver SQL thread or a worker thread for parallel 969 replication. 970 */ 971 class rpl_sql_thread_info 972 { 973 public: 974 char cached_charset[6]; 975 Rpl_filter* rpl_filter; 976 977 rpl_sql_thread_info(Rpl_filter *filter); 978 979 /* 980 Last charset (6 bytes) seen by slave SQL thread is cached here; it helps 981 the thread save 3 get_charset() per Query_log_event if the charset is not 982 changing from event to event (common situation). 983 When the 6 bytes are equal to 0 is used to mean "cache is invalidated". 984 */ 985 void cached_charset_invalidate(); 986 bool cached_charset_compare(char *charset) const; 987 }; 988 989 990 extern struct rpl_slave_state *rpl_global_gtid_slave_state; 991 extern gtid_waiting rpl_global_gtid_waiting; 992 993 int rpl_load_gtid_slave_state(THD *thd); 994 int find_gtid_slave_pos_tables(THD *thd); 995 int event_group_new_gtid(rpl_group_info *rgi, Gtid_log_event *gev); 996 void delete_or_keep_event_post_apply(rpl_group_info *rgi, 997 Log_event_type typ, Log_event *ev); 998 999 #endif /* RPL_RLI_H */ 1000