| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2018, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file clone/clone0repl.cc |
| 29 |
Innodb Clone Replication Coordinates |
| 30 |
|
| 31 |
*******************************************************/ |
| 32 |
#include "clone0repl.h" |
| 33 |
#include "clone0api.h" |
| 34 |
#include "clone0clone.h" |
| 35 |
#include "sql/field.h" |
| 36 |
#include "sql/mysqld.h" |
| 37 |
#include "sql/rpl_gtid_persist.h" |
| 38 |
#include "sql/sql_class.h" |
| 39 |
#include "sql/sql_thd_internal_api.h" |
| 40 |
|
| 41 |
/* To get current session thread default THD */ |
| 42 |
THD *thd_get_current_thd(); |
| 43 |
|
| 44 |
void Clone_persist_gtid::add(const Gtid_desc >id_desc) { |
| 45 |
/* Check if valid descriptor. */ |
| 46 |
if (!gtid_desc.m_is_set) { |
| 47 |
return; |
| 48 |
} |
| 49 |
/* Check if GTID persistence is active. */ |
| 50 |
if (!is_active() || gtid_table_persistor == nullptr) { |
| 51 |
return; |
| 52 |
} |
| 53 |
ut_ad(trx_sys_serialisation_mutex_own()); |
| 54 |
|
| 55 |
/* If too many GTIDs are accumulated, wait for all to get flushed. Ignore |
| 56 |
timeout and loop to avoid possible hang. The insert should already be |
| 57 |
slowed down by the wait here. */ |
| 58 |
if (check_max_gtid_threshold() && is_thread_active()) { |
| 59 |
trx_sys_serialisation_mutex_exit(); |
| 60 |
wait_flush(false, false, nullptr); |
| 61 |
trx_sys_serialisation_mutex_enter(); |
| 62 |
} |
| 63 |
|
| 64 |
ut_ad(trx_sys_serialisation_mutex_own()); |
| 65 |
/* Get active GTID list */ |
| 66 |
auto ¤t_gtids = get_active_list(); |
| 67 |
|
| 68 |
/* Add input GTID to the set */ |
| 69 |
current_gtids.push_back(gtid_desc.m_info); |
| 70 |
/* Atomic increment. */ |
| 71 |
int current_value = ++m_num_gtid_mem; |
| 72 |
|
| 73 |
/* Wake up background if GTIDs crossed threshold. */ |
| 74 |
if (current_value == s_gtid_threshold) { |
| 75 |
os_event_set(m_event); |
| 76 |
} |
| 77 |
|
| 78 |
DBUG_EXECUTE_IF("dont_compress_gtid_table", { |
| 79 |
/* For predictable outcome of mtr test we flush the GTID immediately. */ |
| 80 |
trx_sys_serialisation_mutex_exit(); |
| 81 |
wait_flush(false, false, nullptr); |
| 82 |
trx_sys_serialisation_mutex_enter(); |
| 83 |
}); |
| 84 |
} |
| 85 |
|
| 86 |
trx_undo_t::Gtid_storage Clone_persist_gtid::persists_gtid(const trx_t *trx) { |
| 87 |
auto thd = trx->mysql_thd; |
| 88 |
|
| 89 |
if (thd == nullptr) { |
| 90 |
thd = thd_get_current_thd(); |
| 91 |
} |
| 92 |
|
| 93 |
if (thd == nullptr || !thd->se_persists_gtid()) { |
| 94 |
/* No need to persist GTID. */ |
| 95 |
return trx_undo_t::Gtid_storage::NONE; |
| 96 |
} |
| 97 |
|
| 98 |
if (thd->is_extrenal_xa()) { |
| 99 |
/* Need to persist both XA prepare and commit GTID. */ |
| 100 |
return trx_undo_t::Gtid_storage::PREPARE_AND_COMMIT; |
| 101 |
} |
| 102 |
|
| 103 |
/* Need to persist only commit GTID. */ |
| 104 |
return trx_undo_t::Gtid_storage::COMMIT; |
| 105 |
} |
| 106 |
|
| 107 |
void Clone_persist_gtid::set_persist_gtid(trx_t *trx, bool set) { |
| 108 |
bool thd_check = false; |
| 109 |
auto thd = trx->mysql_thd; |
| 110 |
|
| 111 |
/* Check conditions if the session is good for persisting GTID. */ |
| 112 |
static_cast<void>(has_gtid(trx, thd, thd_check)); |
| 113 |
|
| 114 |
/* For attachable transaction, skip both set and reset. */ |
| 115 |
if (thd == nullptr || thd->is_attachable_transaction_active() || |
| 116 |
trx->internal) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
/* First do the reset. */ |
| 121 |
if (!set) { |
| 122 |
thd->reset_gtid_persisted_by_se(); |
| 123 |
/* Reset transaction flag also. */ |
| 124 |
trx->persists_gtid = false; |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
ut_ad(set); |
| 129 |
/* Don't set if thread checks have failed. */ |
| 130 |
if (!thd_check) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
/* This is an optimization to skip GTID allocation, if transaction |
| 135 |
is guaranteed to not have GTID. */ |
| 136 |
if (!thd->se_persists_gtid()) { |
| 137 |
auto gtid_next = thd->variables.gtid_next.type; |
| 138 |
|
| 139 |
if (opt_bin_log) { |
| 140 |
/* This transaction would not have GTID. */ |
| 141 |
if (gtid_next == ANONYMOUS_GTID) { |
| 142 |
return; |
| 143 |
} |
| 144 |
} else { |
| 145 |
/* If binary log is disabled, GTID must be directly assigned. */ |
| 146 |
if (gtid_next != ASSIGNED_GTID) { |
| 147 |
return; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/* Test case to validate direct write to gtid_executed table. */ |
| 153 |
DBUG_EXECUTE_IF("simulate_err_on_write_gtid_into_table", { return; }); |
| 154 |
DBUG_EXECUTE_IF("disable_se_persists_gtid", { return; }); |
| 155 |
|
| 156 |
/* Set or Reset GTID persist flag in THD session. The transaction flag |
| 157 |
is set later during prepare/commit/rollback. */ |
| 158 |
thd->set_gtid_persisted_by_se(); |
| 159 |
} |
| 160 |
|
| 161 |
bool Clone_persist_gtid::trx_check_set(trx_t *trx, bool prepare, bool rollback, |
| 162 |
bool &set_explicit) { |
| 163 |
auto thd = trx->mysql_thd; |
| 164 |
bool alloc_check = false; |
| 165 |
|
| 166 |
bool gtid_exists = has_gtid(trx, thd, alloc_check); |
| 167 |
|
| 168 |
set_explicit = false; |
| 169 |
|
| 170 |
if (prepare) { |
| 171 |
/* Check for XA prepare. */ |
| 172 |
gtid_exists = check_gtid_prepare(thd, trx, gtid_exists, alloc_check); |
| 173 |
} else if (rollback) { |
| 174 |
/* Check for Rollback. */ |
| 175 |
gtid_exists = check_gtid_rollback(thd, trx, gtid_exists); |
| 176 |
alloc_check = gtid_exists; |
| 177 |
} else { |
| 178 |
/* Check for Commit. */ |
| 179 |
gtid_exists = check_gtid_commit(thd, gtid_exists, set_explicit); |
| 180 |
alloc_check = gtid_exists; |
| 181 |
} |
| 182 |
/* Set transaction to persist GTID. This is one single point of decision |
| 183 |
in prepare/commit and rollback. Once set, the GTID would be persisted |
| 184 |
in undo and added to in memory GTID list to be written to gtid_executed |
| 185 |
table later. */ |
| 186 |
trx->persists_gtid = gtid_exists; |
| 187 |
return (alloc_check); |
| 188 |
} |
| 189 |
|
| 190 |
bool Clone_persist_gtid::check_gtid_prepare(THD *thd, trx_t *trx, |
| 191 |
bool found_gtid, bool &alloc) { |
| 192 |
/* Skip GTID if alloc checks have failed. */ |
| 193 |
if (!alloc) { |
| 194 |
return (false); |
| 195 |
} |
| 196 |
alloc = false; |
| 197 |
/* Skip GTID if mysql internal XA Prepare created by binlog. */ |
| 198 |
if (trx_is_mysql_xa(trx) || thd->is_one_phase_commit()) { |
| 199 |
return (false); |
| 200 |
} |
| 201 |
auto thd_trx = thd->get_transaction(); |
| 202 |
auto xid_state = thd_trx->xid_state(); |
| 203 |
/* In permissive mode GTID could be assigned during XA commit/rollback. */ |
| 204 |
if (xid_state->has_state(XID_STATE::XA_IDLE)) { |
| 205 |
auto gtid_mode = global_gtid_mode.get(); |
| 206 |
if (gtid_mode == Gtid_mode::ON_PERMISSIVE || |
| 207 |
gtid_mode == Gtid_mode::OFF_PERMISSIVE) { |
| 208 |
alloc = true; |
| 209 |
} |
| 210 |
} |
| 211 |
/* Skip GTID if not set */ |
| 212 |
if (!found_gtid) { |
| 213 |
return (false); |
| 214 |
} |
| 215 |
/* Skip GTID if External XA transaction is not in IDLE state. */ |
| 216 |
if (!xid_state->has_state(XID_STATE::XA_IDLE)) { |
| 217 |
ut_d(ut_error); |
| 218 |
ut_o(return (false)); |
| 219 |
} |
| 220 |
/* Skip if SE is not set to persist GTID.*/ |
| 221 |
if (!thd->se_persists_gtid()) { |
| 222 |
return (false); |
| 223 |
} |
| 224 |
alloc = true; |
| 225 |
return (true); |
| 226 |
} |
| 227 |
|
| 228 |
bool Clone_persist_gtid::check_gtid_commit(THD *thd, bool found_gtid, |
| 229 |
bool &set_explicit) { |
| 230 |
set_explicit = (thd == nullptr) ? false : thd->se_persists_gtid_explicit(); |
| 231 |
|
| 232 |
if (!found_gtid) { |
| 233 |
ut_ad(!set_explicit || thd->is_attachable_transaction_active()); |
| 234 |
return (false); |
| 235 |
} |
| 236 |
|
| 237 |
/* Persist if SE is set to persist GTID.*/ |
| 238 |
return (thd->se_persists_gtid()); |
| 239 |
} |
| 240 |
|
| 241 |
bool Clone_persist_gtid::check_gtid_rollback(THD *thd, trx_t *trx, |
| 242 |
bool found_gtid) { |
| 243 |
if (!found_gtid) { |
| 244 |
return (false); |
| 245 |
} |
| 246 |
|
| 247 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 248 |
|
| 249 |
/* Skip GTID if XA transaction not in prepared state. */ |
| 250 |
if (trx->state.load(std::memory_order_relaxed) != TRX_STATE_PREPARED) { |
| 251 |
return (false); |
| 252 |
} |
| 253 |
|
| 254 |
/* We don't need to persist GTID for binlog internal XA transaction. |
| 255 |
One issue here is xid could be NULL when |
| 256 |
1. External XA transaction is rolled back by XID |
| 257 |
2. binlog internal XA transaction is rolled back during recovery |
| 258 |
This is a side effect of trx_get_trx_by_xid() resetting the xid. We |
| 259 |
cannot use trx_is_mysql_xa() to differentiate external XA transactions |
| 260 |
in that case. However, it is safe to assume it is case (1) here as GTID |
| 261 |
is never set for case (2) and input found_gtid should be false. */ |
| 262 |
if (!trx->xid->is_null() && trx_is_mysql_xa(trx)) { |
| 263 |
return (false); |
| 264 |
} |
| 265 |
|
| 266 |
/* Skip GTID if it is rollback in error case. Ideally we should |
| 267 |
not allow prepared transaction to be rolled back on error but |
| 268 |
currently server/replication does rollback and has test for it. */ |
| 269 |
auto thd_trx = thd->get_transaction(); |
| 270 |
auto xid_state = thd_trx->xid_state(); |
| 271 |
if (xid_state->has_state(XID_STATE::XA_ROLLBACK_ONLY)) { |
| 272 |
return (false); |
| 273 |
} |
| 274 |
|
| 275 |
/* Persist if SE is set to persist GTID.*/ |
| 276 |
return (thd->se_persists_gtid()); |
| 277 |
} |
| 278 |
|
| 279 |
bool Clone_persist_gtid::has_gtid(trx_t *trx, THD *&thd, bool &passed_check) { |
| 280 |
passed_check = false; |
| 281 |
|
| 282 |
/* Note, that the assertion does nothing useful if thd == nullptr. */ |
| 283 |
ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx)); |
| 284 |
|
| 285 |
/* Transaction is not associated with mysql foreground session. */ |
| 286 |
if (trx->state.load(std::memory_order_relaxed) == TRX_STATE_PREPARED && |
| 287 |
thd == nullptr) { |
| 288 |
/* For XA transaction, the current transaction THD could be NULL. Also |
| 289 |
check the default THD of current thread. */ |
| 290 |
thd = thd_get_current_thd(); |
| 291 |
} |
| 292 |
/* Transaction should be associated with a THD session object. */ |
| 293 |
if (thd == nullptr) { |
| 294 |
return (false); |
| 295 |
} |
| 296 |
/* Transaction is internal innodb transaction. */ |
| 297 |
if (trx->internal) { |
| 298 |
return (false); |
| 299 |
} |
| 300 |
|
| 301 |
/* Attachable transactions can be started and committed while |
| 302 |
the main transaction is in progress. We don't want to consider |
| 303 |
GTID persistence for such transactions. */ |
| 304 |
if (thd->is_attachable_transaction_active()) { |
| 305 |
return (false); |
| 306 |
} |
| 307 |
|
| 308 |
/* Explicit request is made while slave threads wants |
| 309 |
to persist GTID for non innodb table. */ |
| 310 |
bool explicit_request = thd->se_persists_gtid_explicit(); |
| 311 |
|
| 312 |
if (!explicit_request) { |
| 313 |
/* Transaction is updating GTID table implicitly. */ |
| 314 |
if (thd->is_operating_gtid_table_implicitly || |
| 315 |
thd->is_operating_substatement_implicitly) { |
| 316 |
/* On slave, explicit request can be set after making some |
| 317 |
modification and thus allocating undo. Allow space for GTID in |
| 318 |
undo log for sub-statements always. */ |
| 319 |
passed_check = thd->is_operating_substatement_implicitly; |
| 320 |
return (false); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
/* Transaction passed checks other than GTID. */ |
| 325 |
passed_check = true; |
| 326 |
|
| 327 |
auto &trx_gtid = thd->owned_gtid; |
| 328 |
/* Transaction is not assigned any GTID */ |
| 329 |
if (trx_gtid.is_empty() || trx_gtid.sidno == THD::OWNED_SIDNO_ANONYMOUS) { |
| 330 |
return (false); |
| 331 |
} |
| 332 |
return (true); |
| 333 |
} |
| 334 |
|
| 335 |
void Clone_persist_gtid::get_gtid_info(trx_t *trx, Gtid_desc >id_desc) { |
| 336 |
gtid_desc.m_is_set = false; |
| 337 |
/* Check if transaction has GTID */ |
| 338 |
if (!trx->persists_gtid) { |
| 339 |
return; |
| 340 |
} |
| 341 |
bool thd_check; |
| 342 |
auto thd = trx->mysql_thd; |
| 343 |
|
| 344 |
if (!has_gtid(trx, thd, thd_check)) { |
| 345 |
ut_d(ut_error); |
| 346 |
ut_o(return); |
| 347 |
} |
| 348 |
|
| 349 |
gtid_desc.m_version = GTID_VERSION; |
| 350 |
auto &trx_gtid = thd->owned_gtid; |
| 351 |
auto &trx_sid = thd->owned_sid; |
| 352 |
|
| 353 |
ut_ad(trx_gtid.sidno > 0); |
| 354 |
ut_ad(trx_gtid.gno > 0); |
| 355 |
ut_ad(trx_gtid.gno < GNO_END); |
| 356 |
|
| 357 |
/* Build GTID string. */ |
| 358 |
gtid_desc.m_info.fill(0); |
| 359 |
auto char_buf = reinterpret_cast<char *>(>id_desc.m_info[0]); |
| 360 |
auto len = trx_gtid.to_string(trx_sid, char_buf); |
| 361 |
ut_a((size_t)len <= GTID_INFO_SIZE); |
| 362 |
|
| 363 |
gtid_desc.m_is_set = true; |
| 364 |
} |
| 365 |
|
| 366 |
int Clone_persist_gtid::write_other_gtids() { |
| 367 |
int err = 0; |
| 368 |
if (opt_bin_log) { |
| 369 |
err = gtid_state->save_gtids_of_last_binlog_into_table(); |
| 370 |
} |
| 371 |
return (err); |
| 372 |
} |
| 373 |
|
| 374 |
bool Clone_persist_gtid::check_compress() { |
| 375 |
/* Check for explicit flush request. */ |
| 376 |
if (m_explicit_request.load()) { |
| 377 |
return (true); |
| 378 |
} |
| 379 |
|
| 380 |
/* Wait for explicit request when debug compress request is set. |
| 381 |
This is to make the debug test outcome predictable. */ |
| 382 |
DBUG_EXECUTE_IF("compress_gtid_table", { return false; }); |
| 383 |
|
| 384 |
/* Check replication global threshold on number of GTIDs. */ |
| 385 |
if (!opt_bin_log && gtid_executed_compression_period != 0 && |
| 386 |
m_compression_gtid_counter > gtid_executed_compression_period) { |
| 387 |
return (true); |
| 388 |
} |
| 389 |
|
| 390 |
/* Check local threshold on number of flush. */ |
| 391 |
if (m_compression_counter >= s_compression_threshold) { |
| 392 |
return (true); |
| 393 |
} |
| 394 |
|
| 395 |
return (false); |
| 396 |
} |
| 397 |
|
| 398 |
bool Clone_persist_gtid::debug_skip_write(bool compression) { |
| 399 |
bool skip = false; |
| 400 |
DBUG_EXECUTE_IF("simulate_flush_commit_error", { skip = true; }); |
| 401 |
DBUG_EXECUTE_IF("simulate_err_on_write_gtid_into_table", { skip = true; }); |
| 402 |
DBUG_EXECUTE_IF("disable_gtid_background_persister", { skip = true; }); |
| 403 |
if (compression) { |
| 404 |
DBUG_EXECUTE_IF("dont_compress_gtid_table", { skip = true; }); |
| 405 |
} |
| 406 |
return (skip); |
| 407 |
} |
| 408 |
|
| 409 |
int Clone_persist_gtid::write_to_table(uint64_t flush_list_number, |
| 410 |
Gtid_set &table_gtid_set, |
| 411 |
Sid_map &sid_map) { |
| 412 |
int err = 0; |
| 413 |
Gtid_set write_gtid_set(&sid_map, nullptr); |
| 414 |
|
| 415 |
/* Allocate some intervals from stack */ |
| 416 |
static const int PREALLOCATED_INTERVAL_COUNT = 64; |
| 417 |
Gtid_set::Interval iv[PREALLOCATED_INTERVAL_COUNT]; |
| 418 |
write_gtid_set.add_interval_memory(PREALLOCATED_INTERVAL_COUNT, iv); |
| 419 |
|
| 420 |
auto &flush_list = get_list(flush_list_number); |
| 421 |
/* Extract GTIDs from flush list. */ |
| 422 |
for (auto >id_info : flush_list) { |
| 423 |
auto gtid_str = reinterpret_cast<const char *>(>id_info[0]); |
| 424 |
auto status = write_gtid_set.add_gtid_text(gtid_str); |
| 425 |
if (status != RETURN_STATUS_OK) { |
| 426 |
err = ER_INTERNAL_ERROR; |
| 427 |
return (err); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
/* Skip call if error test. We don't want to catch this error here. */ |
| 432 |
if (debug_skip_write(false)) { |
| 433 |
flush_list.clear(); |
| 434 |
ut_ad((m_flush_number + 1) == flush_list_number); |
| 435 |
m_flush_number.store(flush_list_number); |
| 436 |
return (0); |
| 437 |
} |
| 438 |
|
| 439 |
bool is_recovery = !m_thread_active.load(); |
| 440 |
if (is_recovery) { |
| 441 |
/* During recovery, eliminate GTIDs already in gtid_executed table. */ |
| 442 |
write_gtid_set.remove_gtid_set(&table_gtid_set); |
| 443 |
table_gtid_set.add_gtid_set(&write_gtid_set); |
| 444 |
} else { |
| 445 |
/* Handle concurrent write by other threads when binlog is enabled. */ |
| 446 |
gtid_state->update_prev_gtids(&write_gtid_set); |
| 447 |
} |
| 448 |
|
| 449 |
/* Write GTIDs to table. */ |
| 450 |
if (!write_gtid_set.is_empty()) { |
| 451 |
++m_compression_counter; |
| 452 |
err = gtid_table_persistor->save(&write_gtid_set, false); |
| 453 |
} |
| 454 |
|
| 455 |
/* Clear flush list and return */ |
| 456 |
flush_list.clear(); |
| 457 |
ut_ad((m_flush_number + 1) == flush_list_number); |
| 458 |
m_flush_number.store(flush_list_number); |
| 459 |
return (err); |
| 460 |
} |
| 461 |
|
| 462 |
void Clone_persist_gtid::update_gtid_trx_no(trx_id_t new_gtid_trx_no) { |
| 463 |
auto trx_no = m_gtid_trx_no.load(); |
| 464 |
/* Noting to do if number hasn't increased. */ |
| 465 |
if (trx_no != TRX_ID_MAX && trx_no >= new_gtid_trx_no) { |
| 466 |
ut_ad(trx_no == new_gtid_trx_no); |
| 467 |
return; |
| 468 |
} |
| 469 |
/* Update in memory variable. */ |
| 470 |
m_gtid_trx_no.store(new_gtid_trx_no); |
| 471 |
|
| 472 |
/* Persist to disk. This would be useful during recovery. */ |
| 473 |
trx_sys_persist_gtid_num(new_gtid_trx_no); |
| 474 |
|
| 475 |
/* Wake up purge thread. */ |
| 476 |
srv_purge_wakeup(); |
| 477 |
} |
| 478 |
|
| 479 |
void Clone_persist_gtid::flush_gtids(THD *thd) { |
| 480 |
int err = 0; |
| 481 |
Sid_map sid_map(nullptr); |
| 482 |
Gtid_set table_gtid_set(&sid_map, nullptr); |
| 483 |
|
| 484 |
DBUG_EXECUTE_IF("gtid_persist_flush_disable", return;); |
| 485 |
|
| 486 |
/* During recovery, fetch existing GTIDs from gtid_executed table. */ |
| 487 |
bool is_recovery = !m_thread_active.load(); |
| 488 |
if (is_recovery && !opt_initialize) { |
| 489 |
gtid_table_persistor->fetch_gtids(&table_gtid_set); |
| 490 |
} |
| 491 |
|
| 492 |
bool explicit_request = m_explicit_request.load(); |
| 493 |
|
| 494 |
trx_sys_serialisation_mutex_enter(); |
| 495 |
/* Get oldest transaction number that is yet to be committed. Any transaction |
| 496 |
with lower transaction number is committed and is added to GTID list. */ |
| 497 |
auto oldest_trx_no = trx_sys_oldest_trx_no(); |
| 498 |
bool compress_recovery = false; |
| 499 |
/* Check and write if any GTID is accumulated. */ |
| 500 |
if (m_num_gtid_mem.load() != 0) { |
| 501 |
m_flush_in_progress.store(true); |
| 502 |
/* Switch active list and get the previous list to write to disk table. */ |
| 503 |
auto flush_list_number = switch_active_list(); |
| 504 |
/* Exit trx mutex during write to table. */ |
| 505 |
trx_sys_serialisation_mutex_exit(); |
| 506 |
err = write_to_table(flush_list_number, table_gtid_set, sid_map); |
| 507 |
m_flush_in_progress.store(false); |
| 508 |
/* Compress always after recovery, if GTIDs are added. */ |
| 509 |
if (!m_thread_active.load()) { |
| 510 |
compress_recovery = true; |
| 511 |
ib::info(ER_IB_CLONE_GTID_PERSIST) << "GTID compression after recovery. "; |
| 512 |
} |
| 513 |
} else { |
| 514 |
trx_sys_serialisation_mutex_exit(); |
| 515 |
} |
| 516 |
|
| 517 |
if (is_recovery) { |
| 518 |
/* Allocate buffer and fill GTIDs */ |
| 519 |
char *gtid_buffer = nullptr; |
| 520 |
auto gtid_buffer_size = table_gtid_set.to_string(>id_buffer); |
| 521 |
/* Update GTID set to status for clone recovery. */ |
| 522 |
std::string all_gtids; |
| 523 |
if (gtid_buffer_size > 0) { |
| 524 |
all_gtids.assign(gtid_buffer); |
| 525 |
} |
| 526 |
/* Must update GITD status even if no GTID. This call completes |
| 527 |
clone operation. */ |
| 528 |
clone_update_gtid_status(all_gtids); |
| 529 |
my_free(gtid_buffer); |
| 530 |
} |
| 531 |
|
| 532 |
/* Update trx number up to which GTID is written to table. */ |
| 533 |
update_gtid_trx_no(oldest_trx_no); |
| 534 |
|
| 535 |
/* Request Compression once the counter reaches threshold. */ |
| 536 |
bool debug_skip = debug_skip_write(true); |
| 537 |
if (err == 0 && !debug_skip && (compress_recovery || check_compress())) { |
| 538 |
m_compression_counter = 0; |
| 539 |
m_compression_gtid_counter = 0; |
| 540 |
/* Write non-innodb GTIDs before compression. */ |
| 541 |
write_other_gtids(); |
| 542 |
err = gtid_table_persistor->compress(thd); |
| 543 |
} |
| 544 |
if (err != 0) { |
| 545 |
ib::error(ER_IB_CLONE_GTID_PERSIST) << "Error persisting GTIDs to table"; |
| 546 |
ut_ad(debug_skip || srv_force_recovery > 0); |
| 547 |
} |
| 548 |
|
| 549 |
/* Reset the explicit compression request, if our previous check |
| 550 |
for explicit returned true. If the request is made after previous |
| 551 |
check then we do the compression next time. */ |
| 552 |
if (explicit_request) { |
| 553 |
m_explicit_request.store(false); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
bool Clone_persist_gtid::check_max_gtid_threshold() { |
| 558 |
ut_ad(trx_sys_serialisation_mutex_own()); |
| 559 |
/* Allow only one GTID to flush at a time. */ |
| 560 |
DBUG_EXECUTE_IF("dont_compress_gtid_table", |
| 561 |
{ return m_num_gtid_mem.load() > 0; }); |
| 562 |
return m_num_gtid_mem.load() >= s_max_gtid_threshold; |
| 563 |
} |
| 564 |
|
| 565 |
void Clone_persist_gtid::periodic_write() { |
| 566 |
auto thd = create_internal_thd(); |
| 567 |
|
| 568 |
/* Allow GTID to be persisted on read only server. */ |
| 569 |
thd->set_skip_readonly_check(); |
| 570 |
|
| 571 |
/* Write all accumulated GTIDs while starting server. These GTIDs |
| 572 |
are found in undo log during recovery. We must make sure all these |
| 573 |
GTIDs are flushed and on disk before server is open for new operation |
| 574 |
and new GTIDs are generated. |
| 575 |
|
| 576 |
Why is it needed ? |
| 577 |
|
| 578 |
1. mysql.gtid_executed table must be up to date at this point as global |
| 579 |
variable gtid_executed is updated from it when binary log is disabled. |
| 580 |
|
| 581 |
2. In older versions we used to have only one GTID storage in undo log |
| 582 |
and PREAPARE GTID was stored in same place as COMMIT GTID. We used to |
| 583 |
wait for PREPARE GTID to flush before writing commit GTID. Now this |
| 584 |
limitation is removed and we no longer wait for PREPARE GTID to get |
| 585 |
flushed before COMMIT as we store the PREPARE GTID in separate location. |
| 586 |
However, while upgrading from previous version, there could be XA |
| 587 |
transaction in PREPARED state with GTID stored in place of commit GTID. |
| 588 |
Those GTIDs are also flushed here so that they are not overwritten later |
| 589 |
at COMMIT. |
| 590 |
*/ |
| 591 |
flush_gtids(thd); |
| 592 |
|
| 593 |
/* Let the caller wait till first set of GTIDs are persisted to table |
| 594 |
after recovery. */ |
| 595 |
m_thread_active.store(true); |
| 596 |
|
| 597 |
for (;;) { |
| 598 |
/* Exit if last phase of shutdown */ |
| 599 |
auto is_shutdown = (srv_shutdown_state.load() >= SRV_SHUTDOWN_CLEANUP); |
| 600 |
|
| 601 |
if (is_shutdown || m_close_thread.load()) { |
| 602 |
/* Stop accepting any more GTID */ |
| 603 |
m_active.store(false); |
| 604 |
break; |
| 605 |
} |
| 606 |
|
| 607 |
if (!flush_immediate()) { |
| 608 |
os_event_wait_time(m_event, s_time_threshold); |
| 609 |
} |
| 610 |
os_event_reset(m_event); |
| 611 |
/* Write accumulated GTIDs to disk table */ |
| 612 |
flush_gtids(thd); |
| 613 |
/* Memory is allocated from thd->memroot to store the information about the |
| 614 |
table being locked for flushing gtids. In this case, being a background |
| 615 |
thread which is flushing the gtids every 100 millisecond, we should release |
| 616 |
the memory after every cycle of flushing gtids to avoid memory growth in |
| 617 |
Clone_persist_gtid thread .*/ |
| 618 |
thd->mem_root->ClearForReuse(); |
| 619 |
} |
| 620 |
|
| 621 |
/* For slow shutdown, consume remaining GTIDs so that undo can be purged. */ |
| 622 |
if (m_num_gtid_mem.load() > 0 && srv_fast_shutdown < 2) { |
| 623 |
flush_gtids(thd); |
| 624 |
/* All GTIDs should have been flushed at this point. */ |
| 625 |
if (m_num_gtid_mem.load() > 0) { |
| 626 |
ib::warn(ER_IB_MSG_GTID_FLUSH_AT_SHUTDOWN); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
m_active.store(false); |
| 631 |
destroy_internal_thd(thd); |
| 632 |
m_thread_active.store(false); |
| 633 |
} |
| 634 |
|
| 635 |
bool Clone_persist_gtid::wait_thread(bool start, bool wait_flush, |
| 636 |
uint64_t flush_number, bool compress, |
| 637 |
bool early_timeout, Clone_Alert_Func cbk) { |
| 638 |
size_t count = 0; |
| 639 |
|
| 640 |
auto wait_cond = [&](bool alert, bool &result) { |
| 641 |
if (wait_flush) { |
| 642 |
/* If the thread is not active, return. */ |
| 643 |
if (!is_thread_active()) { |
| 644 |
result = false; |
| 645 |
return (0); |
| 646 |
} |
| 647 |
/* If it is flushed up to the point requested. */ |
| 648 |
if (check_flushed(flush_number)) { |
| 649 |
/* Check if compression is done if requested. */ |
| 650 |
if (!compress || !m_explicit_request.load()) { |
| 651 |
result = false; |
| 652 |
return (0); |
| 653 |
} |
| 654 |
} |
| 655 |
} else if (is_thread_active() == start) { |
| 656 |
result = false; |
| 657 |
return (0); |
| 658 |
} |
| 659 |
if (is_thread_active()) { |
| 660 |
os_event_set(m_event); |
| 661 |
} |
| 662 |
result = true; |
| 663 |
if (alert) { |
| 664 |
ib::info(ER_IB_CLONE_TIMEOUT) << "Waiting for Clone GTID thread"; |
| 665 |
if (cbk) { |
| 666 |
auto err = cbk(); |
| 667 |
if (err != 0) { |
| 668 |
return (err); |
| 669 |
} |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
++count; |
| 674 |
/* Force early exit from wait loop. We wait for about 1 sec |
| 675 |
for early timeout: 10 x 100ms + 5 iteration for ramp up |
| 676 |
from 1ms to 100ms. */ |
| 677 |
if (early_timeout && count > 15) { |
| 678 |
return (ER_QUERY_TIMEOUT); |
| 679 |
} |
| 680 |
return (0); |
| 681 |
}; |
| 682 |
|
| 683 |
bool is_timeout = false; |
| 684 |
|
| 685 |
/* Sleep starts with 1ms and backs off to 100 ms. */ |
| 686 |
Clone_Msec sleep_time(100); |
| 687 |
/* Generate alert message every 5 seconds. */ |
| 688 |
Clone_Sec alert_interval(5); |
| 689 |
/* Wait for 5 minutes. */ |
| 690 |
Clone_Sec time_out(Clone_Min(5)); |
| 691 |
|
| 692 |
static_cast<void>(Clone_Sys::wait(sleep_time, time_out, alert_interval, |
| 693 |
wait_cond, nullptr, is_timeout)); |
| 694 |
return (!is_timeout); |
| 695 |
} |
| 696 |
|
| 697 |
/** Persist GTID to on disk table from time to time. |
| 698 |
@param[in,out] persist_gtid GTID persister */ |
| 699 |
static void clone_gtid_thread(Clone_persist_gtid *persist_gtid) { |
| 700 |
persist_gtid->periodic_write(); |
| 701 |
} |
| 702 |
|
| 703 |
bool Clone_persist_gtid::start() { |
| 704 |
if (m_thread_active.load()) { |
| 705 |
m_active.store(true); |
| 706 |
return (true); |
| 707 |
} |
| 708 |
|
| 709 |
srv_threads.m_gtid_persister = |
| 710 |
os_thread_create(clone_gtid_thread_key, 0, clone_gtid_thread, this); |
| 711 |
srv_threads.m_gtid_persister.start(); |
| 712 |
|
| 713 |
if (!wait_thread(true, false, 0, false, false, nullptr)) { |
| 714 |
ib::error(ER_IB_CLONE_TIMEOUT) << "Wait for GTID thread to start timed out"; |
| 715 |
ut_d(ut_error); |
| 716 |
ut_o(return (false)); |
| 717 |
} |
| 718 |
m_active.store(true); |
| 719 |
return (true); |
| 720 |
} |
| 721 |
|
| 722 |
void Clone_persist_gtid::stop() { |
| 723 |
m_close_thread.store(true); |
| 724 |
if (m_thread_active.load() && |
| 725 |
!wait_thread(false, false, 0, false, false, nullptr)) { |
| 726 |
ib::error(ER_IB_CLONE_TIMEOUT) << "Wait for GTID thread to stop timed out"; |
| 727 |
ut_d(ut_error); |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
void Clone_persist_gtid::wait_flush(bool compress_gtid, bool early_timeout, |
| 732 |
Clone_Alert_Func cbk) { |
| 733 |
/* During recovery, avoid wait if called before persister is active. */ |
| 734 |
if (!is_thread_active()) { |
| 735 |
return; |
| 736 |
} |
| 737 |
auto request_number = request_immediate_flush(compress_gtid); |
| 738 |
os_event_set(m_event); |
| 739 |
|
| 740 |
/* For RESET MASTER we must wait for the flush. */ |
| 741 |
auto thd = thd_get_current_thd(); |
| 742 |
if (thd != nullptr && thd->is_log_reset()) { |
| 743 |
early_timeout = false; |
| 744 |
} |
| 745 |
|
| 746 |
/* Wait for flush if test is asking for it. */ |
| 747 |
DBUG_EXECUTE_IF("wait_for_flush_gtid_persister", { early_timeout = false; }); |
| 748 |
|
| 749 |
auto success = wait_thread(false, true, request_number, compress_gtid, |
| 750 |
early_timeout, cbk); |
| 751 |
/* No error for early timeout. */ |
| 752 |
if (!success && !early_timeout) { |
| 753 |
ib::error(ER_IB_CLONE_TIMEOUT) << "Wait for GTID thread to flush timed out"; |
| 754 |
ut_d(ut_error); |
| 755 |
} |
| 756 |
} |