| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2017, 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/clone0copy.cc |
| 29 |
Innodb copy snapshot data |
| 30 |
|
| 31 |
*******************************************************/ |
| 32 |
|
| 33 |
#include "buf0dump.h" |
| 34 |
#include "clone0clone.h" |
| 35 |
#include "dict0dict.h" |
| 36 |
#include "fsp0sysspace.h" |
| 37 |
#include "sql/binlog.h" |
| 38 |
#include "sql/clone_handler.h" |
| 39 |
#include "sql/handler.h" |
| 40 |
#include "sql/mysqld.h" |
| 41 |
#include "srv0start.h" |
| 42 |
|
| 43 |
/** Callback to add an archived redo file to current snapshot |
| 44 |
@param[in] file_name file name |
| 45 |
@param[in] file_size file size in bytes |
| 46 |
@param[in] file_offset start offset in bytes |
| 47 |
@param[in] context snapshot |
| 48 |
@return error code */ |
| 49 |
static int add_redo_file_callback(char *file_name, uint64_t file_size, |
| 50 |
uint64_t file_offset, void *context) { |
| 51 |
auto snapshot = static_cast<Clone_Snapshot *>(context); |
| 52 |
|
| 53 |
auto err = snapshot->add_redo_file(file_name, file_size, file_offset); |
| 54 |
|
| 55 |
return (err); |
| 56 |
} |
| 57 |
|
| 58 |
/** Callback to add tracked page IDs to current snapshot |
| 59 |
@param[in] context snapshot |
| 60 |
@param[in] buff buffer having page IDs |
| 61 |
@param[in] num_pages number of tracked pages |
| 62 |
@return error code */ |
| 63 |
static int add_page_callback(void *context, byte *buff, uint num_pages) { |
| 64 |
uint index; |
| 65 |
Clone_Snapshot *snapshot; |
| 66 |
|
| 67 |
space_id_t space_id; |
| 68 |
uint32_t page_num; |
| 69 |
|
| 70 |
snapshot = static_cast<Clone_Snapshot *>(context); |
| 71 |
|
| 72 |
/* Extract the page Ids from the buffer. */ |
| 73 |
for (index = 0; index < num_pages; index++) { |
| 74 |
space_id = mach_read_from_4(buff); |
| 75 |
buff += 4; |
| 76 |
|
| 77 |
page_num = mach_read_from_4(buff); |
| 78 |
buff += 4; |
| 79 |
|
| 80 |
auto err = snapshot->add_page(space_id, page_num); |
| 81 |
|
| 82 |
if (err != 0) { |
| 83 |
return (err); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return (0); |
| 88 |
} |
| 89 |
|
| 90 |
int Clone_Snapshot::add_buf_pool_file() { |
| 91 |
char path[OS_FILE_MAX_PATH]; |
| 92 |
/* Generate the file name. */ |
| 93 |
buf_dump_generate_path(path, sizeof(path)); |
| 94 |
|
| 95 |
/* Add if the file is found. */ |
| 96 |
int err = 0; |
| 97 |
|
| 98 |
if (os_file_exists(path)) { |
| 99 |
auto file_size = os_file_get_size(path); |
| 100 |
auto size_bytes = file_size.m_total_size; |
| 101 |
|
| 102 |
/* Check for error */ |
| 103 |
if (size_bytes == static_cast<os_offset_t>(~0)) { |
| 104 |
char errbuf[MYSYS_STRERROR_SIZE]; |
| 105 |
my_error(ER_CANT_OPEN_FILE, MYF(0), path, errno, |
| 106 |
my_strerror(errbuf, sizeof(errbuf), errno)); |
| 107 |
return (ER_CANT_OPEN_FILE); |
| 108 |
} |
| 109 |
|
| 110 |
/* Always the first file in list */ |
| 111 |
ut_ad(num_data_files() == 0); |
| 112 |
|
| 113 |
m_data_bytes_disk += size_bytes; |
| 114 |
m_monitor.add_estimate(size_bytes); |
| 115 |
|
| 116 |
err = add_file(path, size_bytes, size_bytes, nullptr, false); |
| 117 |
} |
| 118 |
|
| 119 |
return (err); |
| 120 |
} |
| 121 |
|
| 122 |
int Clone_Snapshot::init_redo_archiving() { |
| 123 |
ut_ad(m_snapshot_type != HA_CLONE_BLOCKING); |
| 124 |
|
| 125 |
if (m_snapshot_type == HA_CLONE_BLOCKING) { |
| 126 |
/* We are not supposed to start redo archiving in this mode. */ |
| 127 |
m_redo_file_size = m_redo_header_size = m_redo_trailer_size = 0; |
| 128 |
return ER_INTERNAL_ERROR; /* purecov: inspected */ |
| 129 |
} |
| 130 |
|
| 131 |
/* If not blocking clone, allocate redo header and trailer buffer. */ |
| 132 |
|
| 133 |
m_redo_ctx.get_header_size(m_redo_header_size, m_redo_trailer_size); |
| 134 |
|
| 135 |
m_redo_header = static_cast<byte *>(mem_heap_zalloc( |
| 136 |
m_snapshot_heap, |
| 137 |
m_redo_header_size + m_redo_trailer_size + UNIV_SECTOR_SIZE)); |
| 138 |
|
| 139 |
if (m_redo_header == nullptr) { |
| 140 |
/* purecov: begin inspected */ |
| 141 |
my_error(ER_OUTOFMEMORY, MYF(0), m_redo_header_size + m_redo_trailer_size); |
| 142 |
|
| 143 |
return ER_OUTOFMEMORY; |
| 144 |
/* purecov: end */ |
| 145 |
} |
| 146 |
|
| 147 |
m_redo_header = |
| 148 |
static_cast<byte *>(ut_align(m_redo_header, UNIV_SECTOR_SIZE)); |
| 149 |
|
| 150 |
m_redo_trailer = m_redo_header + m_redo_header_size; |
| 151 |
|
| 152 |
/* Start Redo Archiving */ |
| 153 |
const int err = m_redo_ctx.start(m_redo_header, m_redo_header_size); |
| 154 |
|
| 155 |
if (err != 0) { |
| 156 |
m_redo_file_size = 0; |
| 157 |
return err; /* purecov: inspected */ |
| 158 |
} |
| 159 |
|
| 160 |
m_redo_file_size = uint64_t{m_redo_ctx.get_archived_file_size()}; |
| 161 |
ut_ad(m_redo_file_size >= LOG_FILE_MIN_SIZE); |
| 162 |
|
| 163 |
if (m_redo_file_size < LOG_FILE_MIN_SIZE) { |
| 164 |
my_error(ER_INTERNAL_ERROR, MYF(0)); |
| 165 |
return ERR_R_INTERNAL_ERROR; /* purecov: inspected */ |
| 166 |
} |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
#ifdef UNIV_DEBUG |
| 172 |
void Clone_Snapshot::debug_wait_state_transit() { |
| 173 |
mutex_own(&m_snapshot_mutex); |
| 174 |
|
| 175 |
/* Allow DDL to enter and check. */ |
| 176 |
mutex_exit(&m_snapshot_mutex); |
| 177 |
|
| 178 |
DEBUG_SYNC_C("clone_state_transit_file_copy"); |
| 179 |
|
| 180 |
mutex_enter(&m_snapshot_mutex); |
| 181 |
} |
| 182 |
#endif /* UNIV_DEBUG */ |
| 183 |
|
| 184 |
int Clone_Snapshot::init_file_copy(Snapshot_State new_state) { |
| 185 |
ut_ad(m_snapshot_handle_type == CLONE_HDL_COPY); |
| 186 |
|
| 187 |
State_transit transit_guard(this, new_state); |
| 188 |
|
| 189 |
int err = transit_guard.get_error(); |
| 190 |
|
| 191 |
if (err != 0) { |
| 192 |
return err; /* purecov: inspected */ |
| 193 |
} |
| 194 |
|
| 195 |
ut_d(debug_wait_state_transit()); |
| 196 |
|
| 197 |
m_monitor.init_state(srv_stage_clone_file_copy.m_key, m_enable_pfs); |
| 198 |
|
| 199 |
if (m_snapshot_type == HA_CLONE_BLOCKING) { |
| 200 |
/* In HA_CLONE_BLOCKING mode we treat redo files as usual files. |
| 201 |
We need to clear these special treatment not to count them twice. */ |
| 202 |
m_redo_file_size = m_redo_header_size = m_redo_trailer_size = 0; |
| 203 |
|
| 204 |
} else if (m_snapshot_type == HA_CLONE_REDO) { |
| 205 |
err = init_redo_archiving(); |
| 206 |
|
| 207 |
} else if (m_snapshot_type == HA_CLONE_HYBRID || |
| 208 |
m_snapshot_type == HA_CLONE_PAGE) { |
| 209 |
/* Start modified Page ID Archiving */ |
| 210 |
err = m_page_ctx.start(false, nullptr); |
| 211 |
} else { |
| 212 |
ut_ad(m_snapshot_type == HA_CLONE_BLOCKING); |
| 213 |
err = ER_INTERNAL_ERROR; |
| 214 |
} |
| 215 |
|
| 216 |
if (err != 0) { |
| 217 |
return err; /* purecov: inspected */ |
| 218 |
} |
| 219 |
|
| 220 |
/* Initialize estimation about on disk bytes. */ |
| 221 |
init_disk_estimate(); |
| 222 |
|
| 223 |
/* Add buffer pool dump file. Always the first one in the list. */ |
| 224 |
err = add_buf_pool_file(); |
| 225 |
|
| 226 |
if (err != 0) { |
| 227 |
return err; /* purecov: inspected */ |
| 228 |
} |
| 229 |
|
| 230 |
/* Iterate all tablespace files and add persistent data files. */ |
| 231 |
auto error = Fil_iterator::for_each_file( |
| 232 |
[&](fil_node_t *file) { return (add_node(file, false)); }); |
| 233 |
|
| 234 |
if (error != DB_SUCCESS) { |
| 235 |
return ER_INTERNAL_ERROR; /* purecov: inspected */ |
| 236 |
} |
| 237 |
|
| 238 |
ib::info(ER_IB_CLONE_OPERATION) |
| 239 |
<< "Clone State FILE COPY : " << m_num_current_chunks << " chunks, " |
| 240 |
<< " chunk size : " << (chunk_size() * UNIV_PAGE_SIZE) / (1024 * 1024) |
| 241 |
<< " M"; |
| 242 |
|
| 243 |
m_monitor.change_phase(); |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
int Clone_Snapshot::init_page_copy(Snapshot_State new_state, byte *page_buffer, |
| 248 |
uint page_buffer_len) { |
| 249 |
ut_ad(m_snapshot_handle_type == CLONE_HDL_COPY); |
| 250 |
|
| 251 |
State_transit transit_guard(this, new_state); |
| 252 |
|
| 253 |
int err = transit_guard.get_error(); |
| 254 |
|
| 255 |
if (err != 0) { |
| 256 |
return err; /* purecov: inspected */ |
| 257 |
} |
| 258 |
|
| 259 |
m_monitor.init_state(srv_stage_clone_page_copy.m_key, m_enable_pfs); |
| 260 |
|
| 261 |
if (m_snapshot_type == HA_CLONE_HYBRID) { |
| 262 |
/* Start Redo Archiving */ |
| 263 |
err = init_redo_archiving(); |
| 264 |
|
| 265 |
} else if (m_snapshot_type == HA_CLONE_PAGE) { |
| 266 |
/* Start COW for all modified pages - Not implemented. */ |
| 267 |
ut_d(ut_error); |
| 268 |
} else { |
| 269 |
ut_d(ut_error); |
| 270 |
} |
| 271 |
|
| 272 |
if (err != 0) { |
| 273 |
/* purecov: begin inspected */ |
| 274 |
m_page_ctx.release(); |
| 275 |
return err; |
| 276 |
/* purecov: end */ |
| 277 |
} |
| 278 |
|
| 279 |
/* Stop modified page archiving. */ |
| 280 |
err = m_page_ctx.stop(nullptr); |
| 281 |
|
| 282 |
DEBUG_SYNC_C("clone_stop_page_archiving_without_releasing"); |
| 283 |
|
| 284 |
if (err != 0) { |
| 285 |
/* purecov: begin inspected */ |
| 286 |
m_page_ctx.release(); |
| 287 |
return err; |
| 288 |
/* purecov: end */ |
| 289 |
} |
| 290 |
|
| 291 |
/* Iterate all tablespace files and add new data files created. */ |
| 292 |
auto error = Fil_iterator::for_each_file( |
| 293 |
[&](fil_node_t *file) { return add_node(file, true); }); |
| 294 |
|
| 295 |
if (error != DB_SUCCESS) { |
| 296 |
return ER_INTERNAL_ERROR; /* purecov: inspected */ |
| 297 |
} |
| 298 |
|
| 299 |
/* Collect modified page Ids from Page Archiver. */ |
| 300 |
void *context; |
| 301 |
uint aligned_size; |
| 302 |
|
| 303 |
context = static_cast<void *>(this); |
| 304 |
|
| 305 |
/* Check pages added for encryption. */ |
| 306 |
auto num_pages_encryption = m_page_set.size(); |
| 307 |
|
| 308 |
if (num_pages_encryption > 0) { |
| 309 |
m_monitor.add_estimate(num_pages_encryption * UNIV_PAGE_SIZE); |
| 310 |
} |
| 311 |
|
| 312 |
err = m_page_ctx.get_pages(add_page_callback, context, page_buffer, |
| 313 |
page_buffer_len); |
| 314 |
|
| 315 |
m_page_vector.assign(m_page_set.begin(), m_page_set.end()); |
| 316 |
|
| 317 |
aligned_size = ut_calc_align(m_num_pages, chunk_size()); |
| 318 |
m_num_current_chunks = aligned_size >> m_chunk_size_pow2; |
| 319 |
|
| 320 |
ib::info(ER_IB_CLONE_OPERATION) |
| 321 |
<< "Clone State PAGE COPY : " << m_num_pages << " pages, " |
| 322 |
<< m_num_duplicate_pages << " duplicate pages, " << m_num_current_chunks |
| 323 |
<< " chunks, " |
| 324 |
<< " chunk size : " << (chunk_size() * UNIV_PAGE_SIZE) / (1024 * 1024) |
| 325 |
<< " M"; |
| 326 |
m_page_ctx.release(); |
| 327 |
|
| 328 |
m_monitor.change_phase(); |
| 329 |
return err; |
| 330 |
} |
| 331 |
|
| 332 |
int Clone_Snapshot::synchronize_binlog_gtid(Clone_Alert_Func cbk) { |
| 333 |
/* Get a list of binlog prepared transactions and wait for them to commit |
| 334 |
or rollback. This is to ensure that any possible unordered transactions |
| 335 |
are completed. */ |
| 336 |
auto error = wait_for_binlog_prepared_trx(); |
| 337 |
|
| 338 |
if (error != 0) { |
| 339 |
return (error); |
| 340 |
} |
| 341 |
|
| 342 |
/* Persist non-innodb GTIDs */ |
| 343 |
auto >id_persistor = clone_sys->get_gtid_persistor(); |
| 344 |
gtid_persistor.wait_flush(true, false, cbk); |
| 345 |
|
| 346 |
error = update_binlog_position(); |
| 347 |
return (error); |
| 348 |
} |
| 349 |
|
| 350 |
int Clone_Snapshot::update_binlog_position() { |
| 351 |
/* Since the caller ensures all future commits are in order of binary log and |
| 352 |
innodb updates trx sys page for all transactions by default, any single |
| 353 |
transaction commit here would ensure that the binary log position is |
| 354 |
synchronized. However, currently we don't create any special table for clone |
| 355 |
and we cannot execute transaction/dml here. A possible simplification for |
| 356 |
future. |
| 357 |
|
| 358 |
Ideally the only case we need to update innodb trx sys page is when no |
| 359 |
transaction commit has happened yet after forced ordering is imposed. We |
| 360 |
end up updating the page in more cases but is harmless. We follow the steps |
| 361 |
below. |
| 362 |
|
| 363 |
1. Note the last updated Innodb binary log position - P1 |
| 364 |
|
| 365 |
2. Note the current log position from binary log - P2 |
| 366 |
All transactions up to this point are already prepared and may or may not |
| 367 |
be committed. |
| 368 |
|
| 369 |
3. Note the Innodb binary log position again - P3 |
| 370 |
if P1 != P3 then exit as there is already some new transaction committed. |
| 371 |
if P1 == P3 then update the trx sys log position with P2 |
| 372 |
*Check and update in [3] are atomic for trx sys page. |
| 373 |
|
| 374 |
4. Wait for all binary log prepared transaction to complete. We have |
| 375 |
updated the trx sys page out of order but it is sufficient to ensure that |
| 376 |
all transaction up to the updated binary log position are committed. */ |
| 377 |
|
| 378 |
/* 1. Read binary log position from innodb. */ |
| 379 |
LOG_INFO log_info1; |
| 380 |
char file_name[TRX_SYS_MYSQL_LOG_NAME_LEN + 1]; |
| 381 |
uint64_t file_pos; |
| 382 |
trx_sys_read_binlog_position(&file_name[0], file_pos); |
| 383 |
|
| 384 |
/* 2. Get current binary log position. */ |
| 385 |
LOG_INFO log_info; |
| 386 |
mysql_bin_log.get_current_log(&log_info); |
| 387 |
|
| 388 |
/* 3. Check and write binary log position in Innodb. */ |
| 389 |
bool written = trx_sys_write_binlog_position( |
| 390 |
&file_name[0], file_pos, &log_info.log_file_name[0], log_info.pos); |
| 391 |
|
| 392 |
/* 4. If we had to write current binary log position, should wait for all |
| 393 |
prepared transactions to finish to make sure that all transactions up to |
| 394 |
the binary log position is committed. */ |
| 395 |
if (written) { |
| 396 |
auto err = wait_for_binlog_prepared_trx(); |
| 397 |
return (err); |
| 398 |
} |
| 399 |
return (0); |
| 400 |
} |
| 401 |
|
| 402 |
int Clone_Snapshot::wait_trx_end(THD *thd, trx_id_t trx_id) { |
| 403 |
auto trx = trx_rw_is_active(trx_id, false); |
| 404 |
if (trx == nullptr) { |
| 405 |
return (0); |
| 406 |
} |
| 407 |
|
| 408 |
auto wait_cond = [&](bool alert, bool &result) { |
| 409 |
/* Check if transaction is still active. */ |
| 410 |
auto trx = trx_rw_is_active(trx_id, false); |
| 411 |
if (trx == nullptr) { |
| 412 |
result = false; |
| 413 |
return (0); |
| 414 |
} |
| 415 |
|
| 416 |
result = true; |
| 417 |
if (thd_killed(thd)) { |
| 418 |
my_error(ER_QUERY_INTERRUPTED, MYF(0)); |
| 419 |
return (ER_QUERY_INTERRUPTED); |
| 420 |
} |
| 421 |
|
| 422 |
if (alert) { |
| 423 |
ib::warn(ER_IB_CLONE_TIMEOUT) |
| 424 |
<< "Waiting for prepared transaction to exit"; |
| 425 |
} |
| 426 |
return (0); |
| 427 |
}; |
| 428 |
|
| 429 |
bool is_timeout = false; |
| 430 |
|
| 431 |
/* Sleep for 10 millisecond */ |
| 432 |
Clone_Msec sleep_time(10); |
| 433 |
/* Generate alert message every 5 second. */ |
| 434 |
Clone_Sec alert_interval(5); |
| 435 |
/* Wait for 5 minutes. */ |
| 436 |
Clone_Sec time_out(Clone_Min(5)); |
| 437 |
|
| 438 |
auto err = Clone_Sys::wait(sleep_time, time_out, alert_interval, wait_cond, |
| 439 |
nullptr, is_timeout); |
| 440 |
|
| 441 |
if (err == 0 && is_timeout) { |
| 442 |
ib::info(ER_IB_CLONE_TIMEOUT) |
| 443 |
<< "Clone wait for prepared transaction timed out"; |
| 444 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 445 |
"Innodb Clone wait for prepared transaction timed out."); |
| 446 |
err = ER_INTERNAL_ERROR; |
| 447 |
} |
| 448 |
return (err); |
| 449 |
} |
| 450 |
|
| 451 |
/* To get current session thread default THD */ |
| 452 |
THD *thd_get_current_thd(); |
| 453 |
|
| 454 |
int Clone_Snapshot::wait_for_binlog_prepared_trx() { |
| 455 |
/* Return if binary log is not enabled. */ |
| 456 |
if (!opt_bin_log) { |
| 457 |
return (0); |
| 458 |
} |
| 459 |
auto thd = thd_get_current_thd(); |
| 460 |
/* Get all binlog prepared transactions. */ |
| 461 |
std::vector<trx_id_t> trx_ids; |
| 462 |
trx_sys_get_binlog_prepared(trx_ids); |
| 463 |
|
| 464 |
/* Now wait for the transactions to finish. */ |
| 465 |
for (auto trx_id : trx_ids) { |
| 466 |
auto err = wait_trx_end(thd, trx_id); |
| 467 |
if (err != 0) { |
| 468 |
return (err); |
| 469 |
} |
| 470 |
} |
| 471 |
return (0); |
| 472 |
} |
| 473 |
|
| 474 |
int Clone_Snapshot::init_redo_copy(Snapshot_State new_state, |
| 475 |
Clone_Alert_Func cbk) { |
| 476 |
ut_ad(m_snapshot_handle_type == CLONE_HDL_COPY); |
| 477 |
ut_ad(m_snapshot_type != HA_CLONE_BLOCKING); |
| 478 |
|
| 479 |
/* Block external XA operations. XA prepare commit and rollback operations |
| 480 |
are first logged to binlog and added to global gtid_executed before doing |
| 481 |
operation in SE. Without blocking, we might persist such GTIDs from global |
| 482 |
gtid_executed before the operations are persisted in Innodb. */ |
| 483 |
int binlog_error = 0; |
| 484 |
auto thd = thd_get_current_thd(); |
| 485 |
Clone_handler::XA_Block xa_block_guard(thd); |
| 486 |
|
| 487 |
if (xa_block_guard.failed()) { |
| 488 |
if (thd_killed(thd)) { |
| 489 |
my_error(ER_QUERY_INTERRUPTED, MYF(0)); |
| 490 |
binlog_error = ER_QUERY_INTERRUPTED; |
| 491 |
} else { |
| 492 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 493 |
"Clone wait for XA operation timed out."); |
| 494 |
binlog_error = ER_INTERNAL_ERROR; |
| 495 |
ut_d(ut_error); |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
/* Before stopping redo log archiving synchronize with binlog and GTID. At |
| 500 |
this point a transaction can commit only in the order they are written to |
| 501 |
binary log. We have ensure this by forcing ordered commit and waiting for |
| 502 |
all unordered transactions to finish. */ |
| 503 |
if (binlog_error == 0) { |
| 504 |
binlog_error = synchronize_binlog_gtid(cbk); |
| 505 |
} |
| 506 |
|
| 507 |
/* Save all dynamic metadata to DD buffer table and let all future operations |
| 508 |
to save data immediately after generation. This makes clone recovery |
| 509 |
independent of dynamic metadata stored in redo log and avoids generating |
| 510 |
redo log during recovery. */ |
| 511 |
dict_persist_t::Enable_immediate dyn_metadata_guard(dict_persist); |
| 512 |
|
| 513 |
/* Use it only for local clone. For remote clone, donor session is different |
| 514 |
from the sessions created within mtr test case. */ |
| 515 |
DEBUG_SYNC_C("clone_donor_after_saving_dynamic_metadata"); |
| 516 |
|
| 517 |
/* Start transition to next state. */ |
| 518 |
State_transit transit_guard(this, new_state); |
| 519 |
|
| 520 |
/* Stop redo archiving even on error. */ |
| 521 |
auto redo_error = m_redo_ctx.stop(m_redo_trailer, m_redo_trailer_size, |
| 522 |
m_redo_trailer_offset); |
| 523 |
|
| 524 |
if (binlog_error != 0) { |
| 525 |
return binlog_error; /* purecov: inspected */ |
| 526 |
} |
| 527 |
|
| 528 |
if (redo_error != 0) { |
| 529 |
return redo_error; /* purecov: inspected */ |
| 530 |
} |
| 531 |
|
| 532 |
int transit_error = transit_guard.get_error(); |
| 533 |
|
| 534 |
if (transit_error != 0) { |
| 535 |
return transit_error; /* purecov: inspected */ |
| 536 |
} |
| 537 |
|
| 538 |
m_monitor.init_state(srv_stage_clone_redo_copy.m_key, m_enable_pfs); |
| 539 |
|
| 540 |
/* Iterate all tablespace files and add new data files created. */ |
| 541 |
auto error = Fil_iterator::for_each_file( |
| 542 |
[&](fil_node_t *file) { return add_node(file, true); }); |
| 543 |
|
| 544 |
if (error != DB_SUCCESS) { |
| 545 |
return ER_INTERNAL_ERROR; /* purecov: inspected */ |
| 546 |
} |
| 547 |
|
| 548 |
/* Collect archived redo log files from Log Archiver. */ |
| 549 |
auto context = static_cast<void *>(this); |
| 550 |
|
| 551 |
redo_error = m_redo_ctx.get_files(add_redo_file_callback, context); |
| 552 |
|
| 553 |
/* Add another chunk for the redo log header. */ |
| 554 |
++m_num_redo_chunks; |
| 555 |
|
| 556 |
m_monitor.add_estimate(m_redo_header_size); |
| 557 |
|
| 558 |
/* Add another chunk for the redo log trailer. */ |
| 559 |
++m_num_redo_chunks; |
| 560 |
|
| 561 |
if (m_redo_trailer_size != 0) { |
| 562 |
m_monitor.add_estimate(m_redo_trailer_size); |
| 563 |
} |
| 564 |
|
| 565 |
m_num_current_chunks = m_num_redo_chunks; |
| 566 |
|
| 567 |
ib::info(ER_IB_CLONE_OPERATION) |
| 568 |
<< "Clone State REDO COPY : " << m_num_current_chunks << " chunks, " |
| 569 |
<< " chunk size : " << (chunk_size() * UNIV_PAGE_SIZE) / (1024 * 1024) |
| 570 |
<< " M"; |
| 571 |
|
| 572 |
m_monitor.change_phase(); |
| 573 |
return redo_error; |
| 574 |
} |
| 575 |
|
| 576 |
bool Clone_Snapshot::build_file_name(Clone_File_Meta *file_meta, |
| 577 |
const char *file_name) { |
| 578 |
size_t new_len = strlen(file_name) + 1; |
| 579 |
auto new_name = const_cast<char *>(file_meta->m_file_name); |
| 580 |
|
| 581 |
/* Check if allocation required. */ |
| 582 |
if (new_len <= file_meta->m_file_name_alloc_len) { |
| 583 |
strcpy(new_name, file_name); |
| 584 |
file_meta->m_file_name_len = new_len; |
| 585 |
return true; |
| 586 |
} |
| 587 |
|
| 588 |
if (new_len > FN_REFLEN_SE) { |
| 589 |
/* purecov: begin deadcode */ |
| 590 |
my_error(ER_PATH_LENGTH, MYF(0), "CLONE FILE NAME"); |
| 591 |
ut_d(ut_error); |
| 592 |
ut_o(return false); |
| 593 |
/* purecov: end */ |
| 594 |
} |
| 595 |
|
| 596 |
size_t alloc_len = new_len; |
| 597 |
|
| 598 |
/* For reallocation, allocate in multiple of base size to avoid frequent |
| 599 |
allocation by rename DDL. */ |
| 600 |
if (file_meta->m_file_name_alloc_len > 0) { |
| 601 |
alloc_len = ut_calc_align(new_len, S_FILE_NAME_BASE_LEN); |
| 602 |
} |
| 603 |
|
| 604 |
new_name = static_cast<char *>(mem_heap_zalloc(m_snapshot_heap, alloc_len)); |
| 605 |
|
| 606 |
if (new_name == nullptr) { |
| 607 |
/* purecov: begin inspected */ |
| 608 |
my_error(ER_OUTOFMEMORY, MYF(0), static_cast<int>(alloc_len)); |
| 609 |
return false; |
| 610 |
/* purecov: end */ |
| 611 |
} |
| 612 |
|
| 613 |
strcpy(new_name, file_name); |
| 614 |
|
| 615 |
file_meta->m_file_name = new_name; |
| 616 |
file_meta->m_file_name_len = new_len; |
| 617 |
file_meta->m_file_name_alloc_len = alloc_len; |
| 618 |
|
| 619 |
return true; |
| 620 |
} |
| 621 |
|
| 622 |
Clone_file_ctx *Clone_Snapshot::build_file(const char *file_name, |
| 623 |
uint64_t file_size, |
| 624 |
uint64_t file_offset, |
| 625 |
uint &num_chunks) { |
| 626 |
/* Allocate for file metadata from snapshot heap. */ |
| 627 |
uint64_t aligned_size = sizeof(Clone_file_ctx); |
| 628 |
|
| 629 |
auto file_ctx = static_cast<Clone_file_ctx *>( |
| 630 |
mem_heap_alloc(m_snapshot_heap, aligned_size)); |
| 631 |
|
| 632 |
if (file_ctx == nullptr) { |
| 633 |
/* purecov: begin inspected */ |
| 634 |
my_error(ER_OUTOFMEMORY, MYF(0), static_cast<int>(aligned_size)); |
| 635 |
return nullptr; |
| 636 |
/* purecov: end */ |
| 637 |
} |
| 638 |
|
| 639 |
file_ctx->init(Clone_file_ctx::Extension::NONE); |
| 640 |
|
| 641 |
auto file_meta = file_ctx->get_file_meta(); |
| 642 |
|
| 643 |
/* For redo file with no data, add dummy entry. */ |
| 644 |
if (file_name == nullptr) { |
| 645 |
num_chunks = 1; |
| 646 |
|
| 647 |
file_meta->m_begin_chunk = 1; |
| 648 |
file_meta->m_end_chunk = 1; |
| 649 |
|
| 650 |
return file_ctx; |
| 651 |
} |
| 652 |
|
| 653 |
file_meta->m_file_size = file_size; |
| 654 |
|
| 655 |
/* reduce offset amount from total size */ |
| 656 |
ut_ad(file_size >= file_offset); |
| 657 |
file_size -= file_offset; |
| 658 |
|
| 659 |
/* Calculate and set chunk parameters. */ |
| 660 |
uint64_t size_in_pages = ut_uint64_align_up(file_size, UNIV_PAGE_SIZE); |
| 661 |
size_in_pages /= UNIV_PAGE_SIZE; |
| 662 |
|
| 663 |
aligned_size = ut_uint64_align_up(size_in_pages, chunk_size()); |
| 664 |
|
| 665 |
num_chunks = static_cast<uint>(aligned_size >> m_chunk_size_pow2); |
| 666 |
|
| 667 |
file_meta->m_begin_chunk = m_num_current_chunks + 1; |
| 668 |
file_meta->m_end_chunk = m_num_current_chunks + num_chunks; |
| 669 |
|
| 670 |
bool success = build_file_name(file_meta, file_name); |
| 671 |
|
| 672 |
return success ? file_ctx : nullptr; |
| 673 |
} |
| 674 |
|
| 675 |
bool Clone_Snapshot::file_ctx_changed(const fil_node_t *node, |
| 676 |
Clone_file_ctx *&file_ctx) { |
| 677 |
file_ctx = nullptr; |
| 678 |
|
| 679 |
auto space = node->space; |
| 680 |
auto count = m_data_file_map.count(space->id); |
| 681 |
|
| 682 |
/* This is a new file after clone has started. */ |
| 683 |
if (count == 0) { |
| 684 |
return true; |
| 685 |
} |
| 686 |
auto file_index = m_data_file_map[space->id]; |
| 687 |
|
| 688 |
if (file_index == 0) { |
| 689 |
/* purecov: begin deadcode */ |
| 690 |
ut_d(ut_error); |
| 691 |
ut_o(return true); |
| 692 |
/* purecov: end */ |
| 693 |
} |
| 694 |
|
| 695 |
/* File descriptor already exists. */ |
| 696 |
--file_index; |
| 697 |
auto num_data_files = m_data_file_vector.size(); |
| 698 |
|
| 699 |
ut_ad(file_index < num_data_files); |
| 700 |
|
| 701 |
if (file_index < num_data_files) { |
| 702 |
file_ctx = m_data_file_vector[file_index]; |
| 703 |
} |
| 704 |
|
| 705 |
if (file_ctx == nullptr) { |
| 706 |
/* purecov: begin deadcode */ |
| 707 |
ut_d(ut_error); |
| 708 |
ut_o(return false); |
| 709 |
/* purecov: end */ |
| 710 |
} |
| 711 |
|
| 712 |
ut_ad(!file_ctx->modifying()); |
| 713 |
|
| 714 |
bool file_changed = |
| 715 |
file_ctx->m_state.load() != Clone_file_ctx::State::CREATED; |
| 716 |
|
| 717 |
/* Consider file modification only from previous state. */ |
| 718 |
if (file_changed && file_ctx->by_ddl(get_state())) { |
| 719 |
return true; |
| 720 |
} |
| 721 |
|
| 722 |
/* The file is not modified in previous state. Next we consider |
| 723 |
encryption or compression type changes. The information is not |
| 724 |
useful for "redo copy" state. Such changes would be applied |
| 725 |
during redo recovery. */ |
| 726 |
if (get_state() == CLONE_SNAPSHOT_REDO_COPY) { |
| 727 |
return false; |
| 728 |
} |
| 729 |
|
| 730 |
const auto file_meta = file_ctx->get_file_meta_read(); |
| 731 |
|
| 732 |
/* Check if encryption property has changed. */ |
| 733 |
if (file_meta->m_encryption_metadata.m_type != |
| 734 |
space->m_encryption_metadata.m_type || |
| 735 |
space->encryption_op_in_progress != Encryption::Progress::NONE) { |
| 736 |
return true; |
| 737 |
} |
| 738 |
|
| 739 |
/* Check if compression property has changed. */ |
| 740 |
if (file_meta->m_compress_type != space->compression_type) { |
| 741 |
return true; |
| 742 |
} |
| 743 |
|
| 744 |
return false; |
| 745 |
} |
| 746 |
|
| 747 |
int Clone_Snapshot::add_file(const char *name, uint64_t size_bytes, |
| 748 |
uint64_t alloc_bytes, fil_node_t *node, |
| 749 |
bool by_ddl) { |
| 750 |
ut_ad(m_snapshot_handle_type == CLONE_HDL_COPY); |
| 751 |
|
| 752 |
Clone_file_ctx *file_ctx = nullptr; |
| 753 |
|
| 754 |
/* Check if ddl has modified this space. */ |
| 755 |
if (by_ddl && !file_ctx_changed(node, file_ctx)) { |
| 756 |
return 0; |
| 757 |
} |
| 758 |
|
| 759 |
if (file_ctx == nullptr) { |
| 760 |
uint32_t num_chunks = 0; |
| 761 |
/* Build file metadata entry and add to data file vector. */ |
| 762 |
file_ctx = build_file(name, size_bytes, 0, num_chunks); |
| 763 |
|
| 764 |
if (file_ctx == nullptr) { |
| 765 |
return (ER_OUTOFMEMORY); /* purecov: inspected */ |
| 766 |
} |
| 767 |
auto file_meta = file_ctx->get_file_meta(); |
| 768 |
|
| 769 |
file_meta->m_alloc_size = alloc_bytes; |
| 770 |
file_meta->m_file_index = num_data_files(); |
| 771 |
m_data_file_vector.push_back(file_ctx); |
| 772 |
|
| 773 |
if (!by_ddl) { |
| 774 |
/* Update total number of chunks. */ |
| 775 |
m_num_data_chunks += num_chunks; |
| 776 |
m_num_current_chunks = m_num_data_chunks; |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
/* All done if not a space file node like buffer pool dump file. */ |
| 781 |
if (node == nullptr) { |
| 782 |
return 0; |
| 783 |
} |
| 784 |
|
| 785 |
auto file_meta = file_ctx->get_file_meta(); |
| 786 |
|
| 787 |
/* Update maximum file name length in snapshot. */ |
| 788 |
if (file_meta->m_file_name_len > m_max_file_name_len) { |
| 789 |
m_max_file_name_len = static_cast<uint32_t>(file_meta->m_file_name_len); |
| 790 |
} |
| 791 |
|
| 792 |
ut_ad(file_meta->m_deleted == file_ctx->deleted()); |
| 793 |
ut_ad(file_meta->m_renamed == file_ctx->renamed()); |
| 794 |
|
| 795 |
if (by_ddl) { |
| 796 |
file_ctx->set_ddl(get_state()); |
| 797 |
|
| 798 |
/* Rebuild file name for renamed descriptor. */ |
| 799 |
if (file_ctx->renamed()) { |
| 800 |
build_file_name(file_meta, name); |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
/* Set space ID, compression and encryption attribute */ |
| 805 |
auto space = node->space; |
| 806 |
file_meta->m_space_id = space->id; |
| 807 |
file_meta->m_compress_type = space->compression_type; |
| 808 |
file_meta->m_encryption_metadata = space->m_encryption_metadata; |
| 809 |
file_meta->m_fsp_flags = static_cast<uint32_t>(space->flags); |
| 810 |
file_meta->m_punch_hole = node->punch_hole; |
| 811 |
file_meta->m_fsblk_size = node->block_size; |
| 812 |
|
| 813 |
/* Modify file meta encryption flag if space encryption or decryption |
| 814 |
already started. This would allow clone to send pages accordingly |
| 815 |
during page copy and persist the flag. */ |
| 816 |
if (space->encryption_op_in_progress == Encryption::Progress::DECRYPTION) { |
| 817 |
fsp_flags_unset_encryption(file_meta->m_fsp_flags); |
| 818 |
|
| 819 |
} else if (space->encryption_op_in_progress == |
| 820 |
Encryption::Progress::ENCRYPTION) { |
| 821 |
fsp_flags_set_encryption(file_meta->m_fsp_flags); |
| 822 |
} |
| 823 |
|
| 824 |
/* If file node supports punch hole then check if we need it. */ |
| 825 |
if (file_meta->m_punch_hole) { |
| 826 |
page_size_t page_size(space->flags); |
| 827 |
/* Transparent compression is skipped if table compression is enabled. */ |
| 828 |
if (page_size.is_compressed() || |
| 829 |
space->compression_type == Compression::NONE) { |
| 830 |
file_meta->m_punch_hole = false; |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
bool is_redo_copy = (get_state() == CLONE_SNAPSHOT_REDO_COPY); |
| 835 |
|
| 836 |
if (file_meta->can_encrypt()) { |
| 837 |
/* All encrypted files created during PAGE_COPY state have their keys redo |
| 838 |
logged. The recovered keys from redo log are encrypted by donor master key |
| 839 |
and cannot be used is recipient. We send the keys explicitly in this case |
| 840 |
to be encrypted by recipient with its own master key. |
| 841 |
|
| 842 |
NOTE: encrypted files created during FILE_COPY state don't have this issue |
| 843 |
as page-0 is always sent during PAGE_COPY with unencrypted data file key. |
| 844 |
|
| 845 |
We always check for SSL connection before sending keys for encrypted tables |
| 846 |
and error out for security. */ |
| 847 |
if (is_redo_copy && by_ddl) { |
| 848 |
file_meta->m_transfer_encryption_key = true; |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
/* Add to hash map only for first node of the tablespace. */ |
| 853 |
auto space_id = file_meta->m_space_id; |
| 854 |
|
| 855 |
if (m_data_file_map[space_id] == 0) { |
| 856 |
m_data_file_map[space_id] = file_meta->m_file_index + 1; |
| 857 |
} |
| 858 |
|
| 859 |
return 0; |
| 860 |
} |
| 861 |
|
| 862 |
dberr_t Clone_Snapshot::add_node(fil_node_t *node, bool by_ddl) { |
| 863 |
ut_ad(m_snapshot_handle_type == CLONE_HDL_COPY); |
| 864 |
|
| 865 |
auto space = node->space; |
| 866 |
|
| 867 |
/* Skip deleted tablespaces. Their pages may still be in |
| 868 |
the buffer pool. */ |
| 869 |
if (space->is_deleted()) { |
| 870 |
return (DB_SUCCESS); |
| 871 |
} |
| 872 |
|
| 873 |
bool is_page_copy = (get_state() == CLONE_SNAPSHOT_PAGE_COPY); |
| 874 |
|
| 875 |
if (by_ddl && is_page_copy && space->can_encrypt()) { |
| 876 |
/* Add page 0 always for encrypted tablespace. */ |
| 877 |
Clone_Page page_zero; |
| 878 |
page_zero.m_space_id = space->id; |
| 879 |
page_zero.m_page_no = 0; |
| 880 |
m_page_set.insert(page_zero); |
| 881 |
++m_num_pages; |
| 882 |
} |
| 883 |
|
| 884 |
/* Find out the file size from node. */ |
| 885 |
page_size_t page_sz(space->flags); |
| 886 |
|
| 887 |
/* For compressed pages the file size doesn't match |
| 888 |
physical page size multiplied by number of pages. It is |
| 889 |
because we use UNIV_PAGE_SIZE while creating the node |
| 890 |
and tablespace. */ |
| 891 |
auto file_size = os_file_get_size(node->name); |
| 892 |
auto size_bytes = file_size.m_total_size; |
| 893 |
auto alloc_size = file_size.m_alloc_size; |
| 894 |
|
| 895 |
/* Check for error */ |
| 896 |
if (size_bytes == static_cast<os_offset_t>(~0)) { |
| 897 |
char errbuf[MYSYS_STRERROR_SIZE]; |
| 898 |
my_error(ER_CANT_OPEN_FILE, MYF(0), node->name, errno, |
| 899 |
my_strerror(errbuf, sizeof(errbuf), errno)); |
| 900 |
return (DB_ERROR); |
| 901 |
} |
| 902 |
|
| 903 |
/* Update estimation */ |
| 904 |
if (!by_ddl) { |
| 905 |
m_data_bytes_disk += alloc_size; |
| 906 |
m_monitor.add_estimate(size_bytes); |
| 907 |
} |
| 908 |
|
| 909 |
/* Add file to snapshot. */ |
| 910 |
auto err = add_file(node->name, size_bytes, alloc_size, node, by_ddl); |
| 911 |
|
| 912 |
return (err != 0 ? DB_ERROR : DB_SUCCESS); |
| 913 |
} |
| 914 |
|
| 915 |
int Clone_Snapshot::add_page(space_id_t space_id, uint32_t page_num) { |
| 916 |
/* Skip pages belonging to tablespace not included for clone. This could |
| 917 |
be some left over pages from drop or truncate in buffer pool which |
| 918 |
would eventually get removed. Or it may be a page for an undo tablespace |
| 919 |
that was deleted with BUF_REMOVE_NONE. */ |
| 920 |
auto count = m_data_file_map.count(space_id); |
| 921 |
if (count == 0) { |
| 922 |
return (0); |
| 923 |
} |
| 924 |
|
| 925 |
Clone_Page cur_page; |
| 926 |
cur_page.m_space_id = space_id; |
| 927 |
cur_page.m_page_no = page_num; |
| 928 |
|
| 929 |
auto result = m_page_set.insert(cur_page); |
| 930 |
|
| 931 |
if (result.second) { |
| 932 |
m_num_pages++; |
| 933 |
m_monitor.add_estimate(UNIV_PAGE_SIZE); |
| 934 |
} else { |
| 935 |
m_num_duplicate_pages++; |
| 936 |
} |
| 937 |
|
| 938 |
return (0); |
| 939 |
} |
| 940 |
|
| 941 |
int Clone_Snapshot::add_redo_file(char *file_name, uint64_t file_size, |
| 942 |
uint64_t file_offset) { |
| 943 |
ut_ad(m_snapshot_handle_type == CLONE_HDL_COPY); |
| 944 |
|
| 945 |
uint num_chunks; |
| 946 |
|
| 947 |
/* Build redo file metadata and add to redo vector. */ |
| 948 |
auto file_ctx = build_file(file_name, file_size, file_offset, num_chunks); |
| 949 |
if (file_ctx == nullptr) { |
| 950 |
return (ER_OUTOFMEMORY); |
| 951 |
} |
| 952 |
|
| 953 |
auto file_meta = file_ctx->get_file_meta(); |
| 954 |
m_monitor.add_estimate(file_meta->m_file_size - file_offset); |
| 955 |
|
| 956 |
/* Set the start offset for first redo file. This could happen |
| 957 |
if redo archiving was already in progress, possibly by another |
| 958 |
concurrent snapshot. */ |
| 959 |
if (num_redo_files() == 0) { |
| 960 |
m_redo_start_offset = file_offset; |
| 961 |
} else { |
| 962 |
ut_ad(file_offset == 0); |
| 963 |
} |
| 964 |
|
| 965 |
file_meta->m_alloc_size = 0; |
| 966 |
|
| 967 |
file_meta->m_space_id = dict_sys_t::s_log_space_id; |
| 968 |
file_meta->m_compress_type = Compression::NONE; |
| 969 |
file_meta->m_encryption_metadata = log_sys->m_encryption_metadata; |
| 970 |
file_meta->m_fsp_flags = UINT32_UNDEFINED; |
| 971 |
file_meta->m_punch_hole = false; |
| 972 |
file_meta->m_fsblk_size = 0; |
| 973 |
|
| 974 |
file_meta->m_file_index = num_redo_files(); |
| 975 |
|
| 976 |
m_redo_file_vector.push_back(file_ctx); |
| 977 |
|
| 978 |
m_num_redo_chunks += num_chunks; |
| 979 |
m_num_current_chunks = m_num_redo_chunks; |
| 980 |
|
| 981 |
/* In rare case of small redo file, large concurrent DMLs and |
| 982 |
slow data transfer. Currently we support maximum 1k redo files. */ |
| 983 |
if (num_redo_files() > SRV_N_LOG_FILES_CLONE_MAX) { |
| 984 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 985 |
"More than %zu archived redo files. Please retry clone.", |
| 986 |
SRV_N_LOG_FILES_CLONE_MAX); |
| 987 |
return (ER_INTERNAL_ERROR); |
| 988 |
} |
| 989 |
|
| 990 |
return (0); |
| 991 |
} |
| 992 |
|
| 993 |
int Clone_Handle::send_task_metadata(Clone_Task *task, Ha_clone_cbk *callback) { |
| 994 |
Clone_Desc_Task_Meta task_desc; |
| 995 |
|
| 996 |
ut_ad(m_clone_handle_type == CLONE_HDL_COPY); |
| 997 |
|
| 998 |
/* Build task descriptor with metadata */ |
| 999 |
task_desc.init_header(get_version()); |
| 1000 |
task_desc.m_task_meta = task->m_task_meta; |
| 1001 |
|
| 1002 |
auto desc_len = task->m_alloc_len; |
| 1003 |
task_desc.serialize(task->m_serial_desc, desc_len, nullptr); |
| 1004 |
|
| 1005 |
callback->set_data_desc(task->m_serial_desc, desc_len); |
| 1006 |
callback->clear_flags(); |
| 1007 |
callback->set_ack(); |
| 1008 |
|
| 1009 |
auto err = callback->buffer_cbk(nullptr, 0); |
| 1010 |
|
| 1011 |
return (err); |
| 1012 |
} |
| 1013 |
|
| 1014 |
int Clone_Handle::send_keep_alive(Clone_Task *task, Ha_clone_cbk *callback) { |
| 1015 |
ut_ad(m_clone_handle_type == CLONE_HDL_COPY); |
| 1016 |
|
| 1017 |
Clone_Desc_State state_desc; |
| 1018 |
state_desc.init_header(get_version()); |
| 1019 |
|
| 1020 |
/* Build state descriptor from snapshot and task */ |
| 1021 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 1022 |
snapshot->get_state_info(false, &state_desc); |
| 1023 |
|
| 1024 |
state_desc.m_is_ack = true; |
| 1025 |
|
| 1026 |
auto task_meta = &task->m_task_meta; |
| 1027 |
state_desc.m_task_index = task_meta->m_task_index; |
| 1028 |
|
| 1029 |
auto desc_len = task->m_alloc_len; |
| 1030 |
state_desc.serialize(task->m_serial_desc, desc_len, nullptr); |
| 1031 |
|
| 1032 |
callback->set_data_desc(task->m_serial_desc, desc_len); |
| 1033 |
callback->clear_flags(); |
| 1034 |
|
| 1035 |
auto err = callback->buffer_cbk(nullptr, 0); |
| 1036 |
|
| 1037 |
return (err); |
| 1038 |
} |
| 1039 |
|
| 1040 |
int Clone_Handle::send_state_metadata(Clone_Task *task, Ha_clone_cbk *callback, |
| 1041 |
bool is_start) { |
| 1042 |
ut_ad(m_clone_handle_type == CLONE_HDL_COPY); |
| 1043 |
|
| 1044 |
/* Before starting state, check and send any new metadata added by DDL. */ |
| 1045 |
if (is_start) { |
| 1046 |
auto err = send_all_ddl_metadata(task, callback); |
| 1047 |
if (err != 0) { |
| 1048 |
return err; /* purecov: inspected */ |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
Clone_Desc_State state_desc; |
| 1053 |
state_desc.init_header(get_version()); |
| 1054 |
|
| 1055 |
/* Build state descriptor from snapshot and task */ |
| 1056 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 1057 |
|
| 1058 |
/* Master needs to send estimation while beginning state */ |
| 1059 |
auto get_estimate = (task->m_is_master && is_start); |
| 1060 |
|
| 1061 |
snapshot->get_state_info(get_estimate, &state_desc); |
| 1062 |
|
| 1063 |
/* Indicate if it is the end of state */ |
| 1064 |
state_desc.m_is_start = is_start; |
| 1065 |
|
| 1066 |
/* Check if remote has already acknowledged state transfer */ |
| 1067 |
if (!is_start && task->m_is_master && |
| 1068 |
!m_clone_task_manager.check_ack(&state_desc)) { |
| 1069 |
ut_ad(task->m_is_master); |
| 1070 |
ut_ad(m_clone_task_manager.is_restarted()); |
| 1071 |
|
| 1072 |
ib::info(ER_IB_CLONE_RESTART) |
| 1073 |
<< "CLONE COPY: Skip ACK after restart for state " |
| 1074 |
<< state_desc.m_state; |
| 1075 |
return (0); |
| 1076 |
} |
| 1077 |
|
| 1078 |
auto task_meta = &task->m_task_meta; |
| 1079 |
state_desc.m_task_index = task_meta->m_task_index; |
| 1080 |
|
| 1081 |
auto desc_len = task->m_alloc_len; |
| 1082 |
state_desc.serialize(task->m_serial_desc, desc_len, nullptr); |
| 1083 |
|
| 1084 |
callback->set_data_desc(task->m_serial_desc, desc_len); |
| 1085 |
callback->clear_flags(); |
| 1086 |
callback->set_ack(); |
| 1087 |
|
| 1088 |
auto err = callback->buffer_cbk(nullptr, 0); |
| 1089 |
|
| 1090 |
if (err != 0) { |
| 1091 |
return (err); |
| 1092 |
} |
| 1093 |
|
| 1094 |
if (is_start) { |
| 1095 |
/* Send all file metadata while starting state */ |
| 1096 |
err = send_all_file_metadata(task, callback); |
| 1097 |
|
| 1098 |
} else { |
| 1099 |
/* Wait for ACK while finishing state */ |
| 1100 |
err = m_clone_task_manager.wait_ack(this, task, callback); |
| 1101 |
} |
| 1102 |
|
| 1103 |
return (err); |
| 1104 |
} |
| 1105 |
|
| 1106 |
int Clone_Handle::send_all_file_metadata(Clone_Task *task, |
| 1107 |
Ha_clone_cbk *callback) { |
| 1108 |
ut_ad(m_clone_handle_type == CLONE_HDL_COPY); |
| 1109 |
|
| 1110 |
if (!task->m_is_master) { |
| 1111 |
return 0; |
| 1112 |
} |
| 1113 |
|
| 1114 |
DEBUG_SYNC_C("clone_before_init_meta"); |
| 1115 |
|
| 1116 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 1117 |
bool is_redo = (snapshot->get_state() == CLONE_SNAPSHOT_REDO_COPY); |
| 1118 |
|
| 1119 |
/* Send all file metadata for data/redo files */ |
| 1120 |
auto err = snapshot->iterate_files([&](Clone_file_ctx *file_ctx) { |
| 1121 |
auto file_meta = file_ctx->get_file_meta(); |
| 1122 |
/* While sending initial metadata, reset the DDL state so that |
| 1123 |
recipient could create the files as new file. */ |
| 1124 |
Clone_File_Meta local_meta = *file_meta; |
| 1125 |
local_meta.reset_ddl(); |
| 1126 |
|
| 1127 |
auto err_file = send_file_metadata(task, &local_meta, is_redo, callback); |
| 1128 |
return err_file; |
| 1129 |
}); |
| 1130 |
|
| 1131 |
return err; |
| 1132 |
} |
| 1133 |
|
| 1134 |
int Clone_Handle::send_file_metadata(Clone_Task *task, |
| 1135 |
const Clone_File_Meta *file_meta, |
| 1136 |
bool is_redo, Ha_clone_cbk *callback) { |
| 1137 |
ut_ad(m_clone_handle_type == CLONE_HDL_COPY); |
| 1138 |
|
| 1139 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 1140 |
|
| 1141 |
Clone_Desc_File_MetaData file_desc; |
| 1142 |
|
| 1143 |
file_desc.m_file_meta = *file_meta; |
| 1144 |
file_desc.m_state = snapshot->get_state(); |
| 1145 |
|
| 1146 |
if (is_redo) { |
| 1147 |
/* For Redo log always send the fixed redo file size. */ |
| 1148 |
file_desc.m_file_meta.m_file_size = snapshot->get_redo_file_size(); |
| 1149 |
|
| 1150 |
file_desc.m_file_meta.m_file_name = nullptr; |
| 1151 |
file_desc.m_file_meta.m_file_name_len = 0; |
| 1152 |
file_desc.m_file_meta.m_file_name_alloc_len = 0; |
| 1153 |
|
| 1154 |
} else if (file_meta->m_space_id == dict_sys_t::s_invalid_space_id) { |
| 1155 |
/* Server buffer dump file ib_buffer_pool. */ |
| 1156 |
ut_ad(file_desc.m_state == CLONE_SNAPSHOT_FILE_COPY); |
| 1157 |
ut_ad(file_meta->m_file_index == 0); |
| 1158 |
|
| 1159 |
file_desc.m_file_meta.m_file_name = SRV_BUF_DUMP_FILENAME_DEFAULT; |
| 1160 |
|
| 1161 |
file_desc.m_file_meta.m_file_name_len = |
| 1162 |
static_cast<uint32_t>(strlen(SRV_BUF_DUMP_FILENAME_DEFAULT)) + 1; |
| 1163 |
|
| 1164 |
file_desc.m_file_meta.m_file_name_alloc_len = 0; |
| 1165 |
|
| 1166 |
} else if (!fsp_is_ibd_tablespace( |
| 1167 |
static_cast<space_id_t>(file_meta->m_space_id))) { |
| 1168 |
/* For system tablespace, remove path. */ |
| 1169 |
auto name_ptr = strrchr(file_meta->m_file_name, OS_PATH_SEPARATOR); |
| 1170 |
|
| 1171 |
if (name_ptr != nullptr) { |
| 1172 |
name_ptr++; |
| 1173 |
|
| 1174 |
file_desc.m_file_meta.m_file_name = name_ptr; |
| 1175 |
file_desc.m_file_meta.m_file_name_len = |
| 1176 |
static_cast<uint32_t>(strlen(name_ptr)) + 1; |
| 1177 |
} |
| 1178 |
file_desc.m_file_meta.m_file_name_alloc_len = 0; |
| 1179 |
} |
| 1180 |
|
| 1181 |
file_desc.init_header(get_version()); |
| 1182 |
|
| 1183 |
auto desc_len = task->m_alloc_len; |
| 1184 |
file_desc.serialize(task->m_serial_desc, desc_len, nullptr); |
| 1185 |
|
| 1186 |
callback->set_data_desc(task->m_serial_desc, desc_len); |
| 1187 |
callback->clear_flags(); |
| 1188 |
|
| 1189 |
/* Check for secure transfer for encrypted table. */ |
| 1190 |
if (file_meta->can_encrypt() || srv_undo_log_encrypt || |
| 1191 |
srv_redo_log_encrypt) { |
| 1192 |
callback->set_secure(); |
| 1193 |
} |
| 1194 |
|
| 1195 |
auto err = callback->buffer_cbk(nullptr, 0); |
| 1196 |
|
| 1197 |
return (err); |
| 1198 |
} |
| 1199 |
|
| 1200 |
int Clone_Handle::send_data(Clone_Task *task, const Clone_file_ctx *file_ctx, |
| 1201 |
uint64_t offset, byte *buffer, uint32_t size, |
| 1202 |
uint64_t new_file_size, Ha_clone_cbk *callback) { |
| 1203 |
ut_ad(m_clone_handle_type == CLONE_HDL_COPY); |
| 1204 |
|
| 1205 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 1206 |
auto file_meta = file_ctx->get_file_meta_read(); |
| 1207 |
|
| 1208 |
/* Build data descriptor */ |
| 1209 |
Clone_Desc_Data data_desc; |
| 1210 |
data_desc.init_header(get_version()); |
| 1211 |
data_desc.m_state = snapshot->get_state(); |
| 1212 |
|
| 1213 |
data_desc.m_task_meta = task->m_task_meta; |
| 1214 |
|
| 1215 |
data_desc.m_file_index = file_meta->m_file_index; |
| 1216 |
data_desc.m_data_len = size; |
| 1217 |
data_desc.m_file_offset = offset; |
| 1218 |
data_desc.m_file_size = file_meta->m_file_size; |
| 1219 |
|
| 1220 |
/* Adjust file size to extend automatically while copying page 0. */ |
| 1221 |
if (new_file_size > data_desc.m_file_size) { |
| 1222 |
ut_ad(snapshot->get_state() == CLONE_SNAPSHOT_PAGE_COPY); |
| 1223 |
ut_ad(offset == 0); |
| 1224 |
data_desc.m_file_size = new_file_size; |
| 1225 |
} |
| 1226 |
|
| 1227 |
/* Serialize data descriptor and set in callback */ |
| 1228 |
auto desc_len = task->m_alloc_len; |
| 1229 |
data_desc.serialize(task->m_serial_desc, desc_len, nullptr); |
| 1230 |
|
| 1231 |
callback->set_data_desc(task->m_serial_desc, desc_len); |
| 1232 |
callback->clear_flags(); |
| 1233 |
|
| 1234 |
auto file_type = OS_CLONE_DATA_FILE; |
| 1235 |
bool is_log_file = (data_desc.m_state == CLONE_SNAPSHOT_REDO_COPY); |
| 1236 |
|
| 1237 |
if (is_log_file || file_meta->m_space_id == dict_sys_t::s_invalid_space_id) { |
| 1238 |
file_type = OS_CLONE_LOG_FILE; |
| 1239 |
} |
| 1240 |
|
| 1241 |
int err = 0; |
| 1242 |
|
| 1243 |
if (buffer != nullptr) { |
| 1244 |
/* Send data from buffer. */ |
| 1245 |
err = callback->buffer_cbk(buffer, size); |
| 1246 |
|
| 1247 |
} else { |
| 1248 |
/* Send data from file. */ |
| 1249 |
if (task->m_current_file_des.m_file == OS_FILE_CLOSED) { |
| 1250 |
File_init_cbk empty_cbk; |
| 1251 |
err = open_file(task, file_ctx, file_type, false, empty_cbk); |
| 1252 |
|
| 1253 |
if (err != 0) { |
| 1254 |
return (err); |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
ut_ad(task->m_current_file_index == file_meta->m_file_index); |
| 1259 |
|
| 1260 |
os_file_t file_hdl; |
| 1261 |
char errbuf[MYSYS_STRERROR_SIZE]; |
| 1262 |
|
| 1263 |
file_hdl = task->m_current_file_des.m_file; |
| 1264 |
auto success = os_file_seek(nullptr, file_hdl, offset); |
| 1265 |
|
| 1266 |
if (!success) { |
| 1267 |
my_error(ER_ERROR_ON_READ, MYF(0), file_meta->m_file_name, errno, |
| 1268 |
my_strerror(errbuf, sizeof(errbuf), errno)); |
| 1269 |
return (ER_ERROR_ON_READ); |
| 1270 |
} |
| 1271 |
|
| 1272 |
if (task->m_file_cache) { |
| 1273 |
callback->set_os_buffer_cache(); |
| 1274 |
/* For data file recommend zero copy for cached IO. */ |
| 1275 |
if (!is_log_file) { |
| 1276 |
callback->set_zero_copy(); |
| 1277 |
} |
| 1278 |
} |
| 1279 |
|
| 1280 |
callback->set_source_name(file_meta->m_file_name); |
| 1281 |
|
| 1282 |
err = file_callback(callback, task, size, false, offset |
| 1283 |
#ifdef UNIV_PFS_IO |
| 1284 |
, |
| 1285 |
UT_LOCATION_HERE |
| 1286 |
#endif /* UNIV_PFS_IO */ |
| 1287 |
); |
| 1288 |
} |
| 1289 |
|
| 1290 |
task->m_data_size += size; |
| 1291 |
|
| 1292 |
return (err); |
| 1293 |
} |
| 1294 |
|
| 1295 |
void Clone_Handle::display_progress( |
| 1296 |
uint32_t cur_chunk, uint32_t max_chunk, uint32_t &percent_done, |
| 1297 |
std::chrono::steady_clock::time_point &disp_time) { |
| 1298 |
auto current_time = std::chrono::steady_clock::now(); |
| 1299 |
auto current_percent = (cur_chunk * 100) / max_chunk; |
| 1300 |
|
| 1301 |
if (current_percent >= percent_done + 20 || |
| 1302 |
(current_time - disp_time > std::chrono::seconds{5} && |
| 1303 |
current_percent > percent_done)) { |
| 1304 |
percent_done = current_percent; |
| 1305 |
disp_time = current_time; |
| 1306 |
|
| 1307 |
ib::info(ER_IB_CLONE_OPERATION) |
| 1308 |
<< "Stage progress: " << percent_done << "% completed."; |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
int Clone_Handle::copy(uint task_id, Ha_clone_cbk *callback) { |
| 1313 |
ut_ad(m_clone_handle_type == CLONE_HDL_COPY); |
| 1314 |
|
| 1315 |
/* Get task from task manager. */ |
| 1316 |
auto task = m_clone_task_manager.get_task_by_index(task_id); |
| 1317 |
|
| 1318 |
auto err = m_clone_task_manager.alloc_buffer(task); |
| 1319 |
if (err != 0) { |
| 1320 |
return (err); |
| 1321 |
} |
| 1322 |
|
| 1323 |
/* Allow restart only after copy is started. */ |
| 1324 |
m_allow_restart = true; |
| 1325 |
|
| 1326 |
/* Send the task metadata. */ |
| 1327 |
err = send_task_metadata(task, callback); |
| 1328 |
|
| 1329 |
if (err != 0) { |
| 1330 |
return (err); |
| 1331 |
} |
| 1332 |
|
| 1333 |
auto send_matadata = m_clone_task_manager.is_restart_metadata(task); |
| 1334 |
|
| 1335 |
/* Send state metadata to remote during restart */ |
| 1336 |
if (send_matadata) { |
| 1337 |
ut_ad(task->m_is_master); |
| 1338 |
ut_ad(m_clone_task_manager.is_restarted()); |
| 1339 |
|
| 1340 |
err = send_state_metadata(task, callback, true); |
| 1341 |
|
| 1342 |
/* Send all file metadata during restart */ |
| 1343 |
} else if (task->m_is_master && |
| 1344 |
m_clone_task_manager.get_state() == CLONE_SNAPSHOT_FILE_COPY && |
| 1345 |
!m_clone_task_manager.is_file_metadata_transferred()) { |
| 1346 |
ut_ad(m_clone_task_manager.is_restarted()); |
| 1347 |
err = send_all_file_metadata(task, callback); |
| 1348 |
} |
| 1349 |
|
| 1350 |
if (err != 0) { |
| 1351 |
return (err); |
| 1352 |
} |
| 1353 |
/* Adjust block size based on client buffer size. */ |
| 1354 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 1355 |
snapshot->update_block_size(callback->get_client_buffer_size()); |
| 1356 |
|
| 1357 |
auto max_chunks = snapshot->get_num_chunks(); |
| 1358 |
|
| 1359 |
/* Set time values for tracking stage progress. */ |
| 1360 |
|
| 1361 |
auto disp_time = std::chrono::steady_clock::now(); |
| 1362 |
|
| 1363 |
/* Loop and process data until snapshot is moved to DONE state. */ |
| 1364 |
uint32_t percent_done = 0; |
| 1365 |
|
| 1366 |
while (m_clone_task_manager.get_state() != CLONE_SNAPSHOT_DONE) { |
| 1367 |
/* Reserve next chunk for current state from snapshot. */ |
| 1368 |
uint32_t current_chunk = 0; |
| 1369 |
uint32_t current_block = 0; |
| 1370 |
|
| 1371 |
err = m_clone_task_manager.reserve_next_chunk(task, current_chunk, |
| 1372 |
current_block); |
| 1373 |
|
| 1374 |
if (err != 0) { |
| 1375 |
break; |
| 1376 |
} |
| 1377 |
|
| 1378 |
if (current_chunk != 0) { |
| 1379 |
/* Send blocks from the reserved chunk. */ |
| 1380 |
err = process_chunk(task, current_chunk, current_block, callback); |
| 1381 |
|
| 1382 |
/* Display stage progress based on % completion. */ |
| 1383 |
if (task->m_is_master) { |
| 1384 |
display_progress(current_chunk, max_chunks, percent_done, disp_time); |
| 1385 |
} |
| 1386 |
|
| 1387 |
} else { |
| 1388 |
/* No more chunks in current state. Transit to next state. */ |
| 1389 |
|
| 1390 |
/* Close the last open file before proceeding to next state */ |
| 1391 |
err = close_and_unpin_file(task); |
| 1392 |
|
| 1393 |
if (err != 0) { |
| 1394 |
break; |
| 1395 |
} |
| 1396 |
|
| 1397 |
/* Inform that the data transfer for current state |
| 1398 |
is over before moving to next state. The remote |
| 1399 |
needs to send back state transfer ACK for the state |
| 1400 |
transfer to complete. */ |
| 1401 |
err = send_state_metadata(task, callback, false); |
| 1402 |
|
| 1403 |
if (err != 0) { |
| 1404 |
break; |
| 1405 |
} |
| 1406 |
|
| 1407 |
/* Next state is decided by snapshot for Copy. */ |
| 1408 |
err = move_to_next_state(task, callback, nullptr); |
| 1409 |
|
| 1410 |
ut_d(task->m_ignore_sync = false); |
| 1411 |
|
| 1412 |
if (err != 0) { |
| 1413 |
break; |
| 1414 |
} |
| 1415 |
|
| 1416 |
max_chunks = snapshot->get_num_chunks(); |
| 1417 |
percent_done = 0; |
| 1418 |
disp_time = std::chrono::steady_clock::now(); |
| 1419 |
|
| 1420 |
/* Send state metadata before processing chunks. */ |
| 1421 |
err = send_state_metadata(task, callback, true); |
| 1422 |
} |
| 1423 |
|
| 1424 |
if (err != 0) { |
| 1425 |
break; |
| 1426 |
} |
| 1427 |
} |
| 1428 |
|
| 1429 |
/* Close the last open file. */ |
| 1430 |
auto err2 = close_and_unpin_file(task); |
| 1431 |
|
| 1432 |
if (err == 0) { |
| 1433 |
err = err2; |
| 1434 |
} |
| 1435 |
|
| 1436 |
return (err); |
| 1437 |
} |
| 1438 |
|
| 1439 |
int Clone_Handle::process_chunk(Clone_Task *task, uint32_t chunk_num, |
| 1440 |
uint32_t block_num, Ha_clone_cbk *callback) { |
| 1441 |
ut_ad(m_clone_handle_type == CLONE_HDL_COPY); |
| 1442 |
|
| 1443 |
auto &task_meta = task->m_task_meta; |
| 1444 |
|
| 1445 |
/* If chunks are in increasing order, optimize file |
| 1446 |
search by index */ |
| 1447 |
uint32_t file_index_hint = 0; |
| 1448 |
|
| 1449 |
if (task_meta.m_chunk_num <= chunk_num) { |
| 1450 |
file_index_hint = task->m_current_file_index; |
| 1451 |
} |
| 1452 |
|
| 1453 |
auto state = m_clone_task_manager.get_state(); |
| 1454 |
bool is_page_copy = (state == CLONE_SNAPSHOT_PAGE_COPY); |
| 1455 |
bool is_redo_copy = (state == CLONE_SNAPSHOT_REDO_COPY); |
| 1456 |
|
| 1457 |
/* Except for page copy, file remains same for all blocks of a chunk. */ |
| 1458 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 1459 |
|
| 1460 |
const auto *file_ctx = |
| 1461 |
snapshot->get_file_ctx(chunk_num, block_num, file_index_hint); |
| 1462 |
|
| 1463 |
const Clone_File_Meta *file_meta = nullptr; |
| 1464 |
|
| 1465 |
/* For page copy, file context is null if current chunk is over. */ |
| 1466 |
ut_ad(is_page_copy || file_ctx != nullptr); |
| 1467 |
|
| 1468 |
if (file_ctx != nullptr) { |
| 1469 |
file_meta = file_ctx->get_file_meta_read(); |
| 1470 |
} |
| 1471 |
|
| 1472 |
/* Loop over all the blocks of current chunk and send data. */ |
| 1473 |
int err = 0; |
| 1474 |
|
| 1475 |
uint32_t pin_loop_count = 0; |
| 1476 |
uint32_t unpin_count = snapshot->get_max_blocks_pin(); |
| 1477 |
|
| 1478 |
while (err == 0) { |
| 1479 |
/* For page copy same chunk might have pages from different files. */ |
| 1480 |
if (is_page_copy) { |
| 1481 |
file_ctx = snapshot->get_file_ctx(chunk_num, block_num, 0); |
| 1482 |
if (file_ctx == nullptr) { |
| 1483 |
/* Already handled all pages. */ |
| 1484 |
break; |
| 1485 |
} |
| 1486 |
file_meta = file_ctx->get_file_meta_read(); |
| 1487 |
} |
| 1488 |
|
| 1489 |
bool pins_other; |
| 1490 |
bool pins_current; |
| 1491 |
|
| 1492 |
std::tie(pins_current, pins_other) = pins_file(task, file_ctx); |
| 1493 |
|
| 1494 |
/* Let any waiting DDL file operation to proceed after handling a |
| 1495 |
set of data blocks. */ |
| 1496 |
bool allow_ddl = |
| 1497 |
snapshot->blocks_clone(file_ctx) && (pin_loop_count >= unpin_count); |
| 1498 |
|
| 1499 |
if (pins_other || (pins_current && allow_ddl)) { |
| 1500 |
err = close_and_unpin_file(task); |
| 1501 |
if (err != 0) { |
| 1502 |
break; /* purecov: inspected */ |
| 1503 |
} |
| 1504 |
pin_loop_count = 0; |
| 1505 |
} |
| 1506 |
|
| 1507 |
bool delete_action = false; |
| 1508 |
|
| 1509 |
auto mutable_ctx = const_cast<Clone_file_ctx *>(file_ctx); |
| 1510 |
|
| 1511 |
err = check_and_pin_file(task, mutable_ctx, delete_action); |
| 1512 |
|
| 1513 |
if (err != 0) { |
| 1514 |
break; /* purecov: inspected */ |
| 1515 |
} |
| 1516 |
|
| 1517 |
++pin_loop_count; |
| 1518 |
|
| 1519 |
/* Check if file is already deleted. */ |
| 1520 |
if (file_ctx->deleted()) { |
| 1521 |
/* One task get to handle the deleted file chunks. Other tasks can ignore |
| 1522 |
the chunks of a deleted file */ |
| 1523 |
if (delete_action) { |
| 1524 |
auto delete_file_meta = *file_meta; |
| 1525 |
delete_file_meta.set_deleted_chunk(chunk_num); |
| 1526 |
err = send_file_metadata(task, &delete_file_meta, false, callback); |
| 1527 |
} |
| 1528 |
|
| 1529 |
auto err2 = close_and_unpin_file(task); |
| 1530 |
if (err == 0) { |
| 1531 |
err = err2; |
| 1532 |
} |
| 1533 |
|
| 1534 |
if (err != 0) { |
| 1535 |
break; /* purecov: inspected */ |
| 1536 |
} |
| 1537 |
|
| 1538 |
/* Send deleted block, to check recipient can handle it. It otherwise is |
| 1539 |
hit only in rare concurrency case. */ |
| 1540 |
DBUG_EXECUTE_IF("clone_send_deleted_block", { |
| 1541 |
if (delete_action && is_page_copy) { |
| 1542 |
auto data_buf = task->m_current_buffer; |
| 1543 |
send_data(task, file_ctx, 0, data_buf, UNIV_PAGE_SIZE, 0, callback); |
| 1544 |
} |
| 1545 |
}); |
| 1546 |
|
| 1547 |
snapshot->skip_deleted_blocks(chunk_num, block_num); |
| 1548 |
|
| 1549 |
/* No more blocks in current chunk. */ |
| 1550 |
if (block_num == 0) { |
| 1551 |
break; |
| 1552 |
} |
| 1553 |
|
| 1554 |
/* Only in page copy state we can have more blocks belonging to |
| 1555 |
another tablespace. */ |
| 1556 |
ut_ad(is_page_copy); |
| 1557 |
continue; |
| 1558 |
} |
| 1559 |
|
| 1560 |
/* Get next block from snapshot */ |
| 1561 |
auto data_buf = task->m_current_buffer; |
| 1562 |
auto data_size = task->m_buffer_alloc_len; |
| 1563 |
uint64_t data_offset = 0; |
| 1564 |
uint64_t file_size = 0; |
| 1565 |
|
| 1566 |
err = snapshot->get_next_block(chunk_num, block_num, file_ctx, data_offset, |
| 1567 |
data_buf, data_size, file_size); |
| 1568 |
|
| 1569 |
file_meta = file_ctx->get_file_meta_read(); |
| 1570 |
|
| 1571 |
/* '0' block number indicates no more blocks. */ |
| 1572 |
if (err != 0 || block_num == 0) { |
| 1573 |
break; |
| 1574 |
} |
| 1575 |
|
| 1576 |
/* Check for error from other tasks and DDL */ |
| 1577 |
err = m_clone_task_manager.handle_error_other_task(task->m_has_thd); |
| 1578 |
|
| 1579 |
if (err != 0) { |
| 1580 |
break; |
| 1581 |
} |
| 1582 |
|
| 1583 |
task->m_task_meta.m_block_num = block_num; |
| 1584 |
task->m_task_meta.m_chunk_num = chunk_num; |
| 1585 |
|
| 1586 |
/* During redo copy, worker could be ahead of master and needs to |
| 1587 |
send the metadata */ |
| 1588 |
if (is_redo_copy && !pins_current) { |
| 1589 |
err = send_file_metadata(task, file_meta, true, callback); |
| 1590 |
if (err != 0) { |
| 1591 |
break; |
| 1592 |
} |
| 1593 |
} |
| 1594 |
|
| 1595 |
/* For remote clone, donor clone threads cannot be controlled |
| 1596 |
by debug sync and hence need to release PIN after every page so as |
| 1597 |
to fire concurrent DDLs after blocking clone in apply. In file copy |
| 1598 |
state we cannot release the PIN as we could be reading the file |
| 1599 |
while sending data after this point. */ |
| 1600 |
DBUG_EXECUTE_IF("remote_release_clone_file_pin", { |
| 1601 |
if (is_page_copy) { |
| 1602 |
close_and_unpin_file(task); |
| 1603 |
} |
| 1604 |
};); |
| 1605 |
|
| 1606 |
if (data_size != 0) { |
| 1607 |
err = send_data(task, file_ctx, data_offset, data_buf, data_size, |
| 1608 |
file_size, callback); |
| 1609 |
} |
| 1610 |
} |
| 1611 |
|
| 1612 |
/* Save current error and file name. */ |
| 1613 |
if (err != 0) { |
| 1614 |
m_clone_task_manager.set_error(err, file_meta->m_file_name); |
| 1615 |
} |
| 1616 |
|
| 1617 |
return (err); |
| 1618 |
} |
| 1619 |
|
| 1620 |
int Clone_Handle::restart_copy(THD *thd, const byte *loc, uint loc_len) { |
| 1621 |
ut_ad(mutex_own(clone_sys->get_mutex())); |
| 1622 |
|
| 1623 |
if (is_abort()) { |
| 1624 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 1625 |
"Innodb Clone Restart failed, existing clone aborted"); |
| 1626 |
return (ER_INTERNAL_ERROR); |
| 1627 |
} |
| 1628 |
|
| 1629 |
/* Wait for the Idle state */ |
| 1630 |
if (!is_idle()) { |
| 1631 |
/* Sleep for 1 second */ |
| 1632 |
Clone_Msec sleep_time(Clone_Sec(1)); |
| 1633 |
/* Generate alert message every 5 seconds. */ |
| 1634 |
Clone_Sec alert_time(5); |
| 1635 |
/* Wait for 30 seconds for server to reach idle state. */ |
| 1636 |
Clone_Sec time_out(30); |
| 1637 |
|
| 1638 |
bool is_timeout = false; |
| 1639 |
auto err = Clone_Sys::wait( |
| 1640 |
sleep_time, time_out, alert_time, |
| 1641 |
[&](bool alert, bool &result) { |
| 1642 |
ut_ad(mutex_own(clone_sys->get_mutex())); |
| 1643 |
result = !is_idle(); |
| 1644 |
|
| 1645 |
if (thd_killed(thd)) { |
| 1646 |
my_error(ER_QUERY_INTERRUPTED, MYF(0)); |
| 1647 |
return (ER_QUERY_INTERRUPTED); |
| 1648 |
|
| 1649 |
} else if (is_abort()) { |
| 1650 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 1651 |
"Innodb Clone Restart failed, existing clone aborted"); |
| 1652 |
return (ER_INTERNAL_ERROR); |
| 1653 |
|
| 1654 |
} else if (Clone_Sys::s_clone_sys_state == CLONE_SYS_ABORT) { |
| 1655 |
my_error(ER_CLONE_DDL_IN_PROGRESS, MYF(0)); |
| 1656 |
return (ER_CLONE_DDL_IN_PROGRESS); |
| 1657 |
} |
| 1658 |
|
| 1659 |
if (result && alert) { |
| 1660 |
ib::info(ER_IB_CLONE_TIMEOUT) << "Clone Master Restart " |
| 1661 |
"wait for idle state"; |
| 1662 |
} |
| 1663 |
return (0); |
| 1664 |
}, |
| 1665 |
clone_sys->get_mutex(), is_timeout); |
| 1666 |
|
| 1667 |
if (err != 0) { |
| 1668 |
return (err); |
| 1669 |
|
| 1670 |
} else if (is_timeout) { |
| 1671 |
ib::info(ER_IB_CLONE_TIMEOUT) |
| 1672 |
<< "Clone Master restart wait for idle timed out"; |
| 1673 |
|
| 1674 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 1675 |
"Clone restart wait for idle state timed out"); |
| 1676 |
return (ER_INTERNAL_ERROR); |
| 1677 |
} |
| 1678 |
} |
| 1679 |
|
| 1680 |
ut_ad(is_idle()); |
| 1681 |
m_clone_task_manager.reinit_copy_state(loc, loc_len); |
| 1682 |
|
| 1683 |
set_state(CLONE_STATE_ACTIVE); |
| 1684 |
|
| 1685 |
return (0); |
| 1686 |
} |
| 1687 |
|
| 1688 |
int Clone_Handle::check_and_pin_file(Clone_Task *task, Clone_file_ctx *file_ctx, |
| 1689 |
bool &handle_deleted) { |
| 1690 |
bool pin_current; |
| 1691 |
bool pin_other; |
| 1692 |
std::tie(pin_current, pin_other) = pins_file(task, file_ctx); |
| 1693 |
|
| 1694 |
handle_deleted = false; |
| 1695 |
|
| 1696 |
/* Nothing to do, the file is already pinned. */ |
| 1697 |
if (pin_current) { |
| 1698 |
ut_ad(task->m_pinned_file); |
| 1699 |
return 0; |
| 1700 |
} |
| 1701 |
|
| 1702 |
/* If pinning any other file, release it. */ |
| 1703 |
if (pin_other) { |
| 1704 |
/* purecov: begin inspected */ |
| 1705 |
auto err2 = close_and_unpin_file(task); |
| 1706 |
if (err2 != 0) { |
| 1707 |
return err2; |
| 1708 |
} |
| 1709 |
/* purecov: end */ |
| 1710 |
} |
| 1711 |
|
| 1712 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 1713 |
auto err = snapshot->pin_file(file_ctx, handle_deleted); |
| 1714 |
|
| 1715 |
if (err == 0) { |
| 1716 |
task->m_pinned_file = true; |
| 1717 |
const auto file_meta = file_ctx->get_file_meta_read(); |
| 1718 |
task->m_current_file_index = file_meta->m_file_index; |
| 1719 |
} |
| 1720 |
|
| 1721 |
return err; |
| 1722 |
} |
| 1723 |
|
| 1724 |
int Clone_Handle::close_and_unpin_file(Clone_Task *task) { |
| 1725 |
/* Close open file if there. */ |
| 1726 |
auto err = close_file(task); |
| 1727 |
|
| 1728 |
/* Task doesn't hold any pin. */ |
| 1729 |
if (!task->m_pinned_file) { |
| 1730 |
return err; |
| 1731 |
} |
| 1732 |
|
| 1733 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 1734 |
auto file_ctx = snapshot->get_file_ctx_by_index(task->m_current_file_index); |
| 1735 |
|
| 1736 |
if (file_ctx == nullptr) { |
| 1737 |
/* purecov: begin deadcode */ |
| 1738 |
err = ER_INTERNAL_ERROR; |
| 1739 |
my_error(ER_INTERNAL_ERROR, MYF(0), "Clone file missing before unpin"); |
| 1740 |
ut_d(ut_error); |
| 1741 |
ut_o(return err); |
| 1742 |
/* purecov: end */ |
| 1743 |
} |
| 1744 |
|
| 1745 |
snapshot->unpin_file(file_ctx); |
| 1746 |
|
| 1747 |
task->m_pinned_file = false; |
| 1748 |
task->m_current_file_index = 0; |
| 1749 |
return err; |
| 1750 |
} |
| 1751 |
|
| 1752 |
std::tuple<bool, bool> Clone_Handle::pins_file(const Clone_Task *task, |
| 1753 |
const Clone_file_ctx *file_ctx) { |
| 1754 |
/* Task doesn't hold any pin. */ |
| 1755 |
if (!task->m_pinned_file) { |
| 1756 |
return std::make_tuple(false, false); |
| 1757 |
} |
| 1758 |
|
| 1759 |
const auto file_meta = file_ctx->get_file_meta_read(); |
| 1760 |
/* Task pins input file. */ |
| 1761 |
if (task->m_current_file_index == file_meta->m_file_index) { |
| 1762 |
return std::make_tuple(true, false); |
| 1763 |
} |
| 1764 |
|
| 1765 |
/* Task pins other file. */ |
| 1766 |
return std::make_tuple(false, true); |
| 1767 |
} |
| 1768 |
|
| 1769 |
int Clone_Handle::send_all_ddl_metadata(Clone_Task *task, |
| 1770 |
Ha_clone_cbk *callback) { |
| 1771 |
ut_ad(m_clone_handle_type == CLONE_HDL_COPY); |
| 1772 |
|
| 1773 |
if (!task->m_is_master) { |
| 1774 |
return 0; |
| 1775 |
} |
| 1776 |
auto state = m_clone_task_manager.get_state(); |
| 1777 |
|
| 1778 |
/* Send DDL metadata added during 'file copy' and 'page copy' in the |
| 1779 |
beginning of next stage. */ |
| 1780 |
if (state != CLONE_SNAPSHOT_PAGE_COPY && state != CLONE_SNAPSHOT_REDO_COPY) { |
| 1781 |
return 0; |
| 1782 |
} |
| 1783 |
|
| 1784 |
ut_d(m_clone_task_manager.debug_wait_ddl_meta()); |
| 1785 |
|
| 1786 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 1787 |
|
| 1788 |
/* Send all file metadata for data/redo files */ |
| 1789 |
auto err = snapshot->iterate_data_files([&](Clone_file_ctx *file_ctx) { |
| 1790 |
/* Skip entries if not modified by ddl in previous state. */ |
| 1791 |
if (!file_ctx->by_ddl(state)) { |
| 1792 |
return 0; |
| 1793 |
} |
| 1794 |
|
| 1795 |
auto file_meta = file_ctx->get_file_meta(); |
| 1796 |
auto err_file = send_file_metadata(task, file_meta, false, callback); |
| 1797 |
|
| 1798 |
return err_file; |
| 1799 |
}); |
| 1800 |
|
| 1801 |
return err; |
| 1802 |
} |