Debian Codegraph @

storage/innobase/clone/clone0snapshot.cc

raw
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/clone0snapshot.cc
29 Innodb physical Snapshot
30
31 *******************************************************/
32
33 #include "clone0snapshot.h"
34 #include "clone0clone.h"
35 #include "log0log.h" /* log_get_lsn */
36 #include "page0zip.h"
37 #include "sql/handler.h"
38
39 /** Snapshot heap initial size */
40 const uint SNAPSHOT_MEM_INITIAL_SIZE = 16 * 1024;
41
42 /** Number of clones that can attach to a snapshot. */
43 const uint MAX_CLONES_PER_SNAPSHOT = 1;
44
45 Clone_Snapshot::Clone_Snapshot(Clone_Handle_Type hdl_type,
46 Ha_clone_type clone_type, uint arr_idx,
47 uint64_t snap_id)
48 : m_snapshot_handle_type(hdl_type),
49 m_snapshot_type(clone_type),
50 m_snapshot_id(snap_id),
51 m_snapshot_arr_idx(arr_idx),
52 m_num_blockers(),
53 m_aborted(false),
54 m_num_clones(),
55 m_num_clones_transit(),
56 m_snapshot_state(CLONE_SNAPSHOT_INIT),
57 m_snapshot_next_state(CLONE_SNAPSHOT_NONE),
58 m_num_current_chunks(),
59 m_max_file_name_len(),
60 m_num_data_chunks(),
61 m_data_bytes_disk(),
62 m_page_ctx(false),
63 m_num_pages(),
64 m_num_duplicate_pages(),
65 m_redo_ctx(),
66 m_redo_start_offset(),
67 m_redo_header(),
68 m_redo_header_size(),
69 m_redo_trailer(),
70 m_redo_trailer_size(),
71 m_redo_trailer_offset(),
72 m_redo_file_size(),
73 m_num_redo_chunks(),
74 m_enable_pfs(false) {
75 mutex_create(LATCH_ID_CLONE_SNAPSHOT, &m_snapshot_mutex);
76
77 m_snapshot_heap =
78 mem_heap_create(SNAPSHOT_MEM_INITIAL_SIZE, UT_LOCATION_HERE);
79
80 m_chunk_size_pow2 = SNAPSHOT_DEF_CHUNK_SIZE_POW2;
81 m_block_size_pow2 = SNAPSHOT_DEF_BLOCK_SIZE_POW2;
82 }
83
84 Clone_Snapshot::~Clone_Snapshot() {
85 m_redo_ctx.release();
86
87 if (m_page_ctx.is_active()) {
88 m_page_ctx.stop(nullptr);
89 }
90 m_page_ctx.release();
91
92 mem_heap_free(m_snapshot_heap);
93
94 mutex_free(&m_snapshot_mutex);
95 }
96
97 void Clone_Snapshot::get_state_info(bool do_estimate,
98 Clone_Desc_State *state_desc) {
99 state_desc->m_state = m_snapshot_state;
100 state_desc->m_num_chunks = m_num_current_chunks;
101
102 state_desc->m_is_start = true;
103 state_desc->m_is_ack = false;
104
105 if (do_estimate) {
106 state_desc->m_estimate = m_monitor.get_estimate();
107 state_desc->m_estimate_disk = m_data_bytes_disk;
108 } else {
109 state_desc->m_estimate = 0;
110 state_desc->m_estimate_disk = 0;
111 }
112
113 switch (m_snapshot_state) {
114 case CLONE_SNAPSHOT_FILE_COPY:
115 state_desc->m_num_files = num_data_files();
116 break;
117
118 case CLONE_SNAPSHOT_PAGE_COPY:
119 state_desc->m_num_files = m_num_pages;
120 break;
121
122 case CLONE_SNAPSHOT_REDO_COPY:
123 state_desc->m_num_files = num_redo_files();
124 break;
125
126 case CLONE_SNAPSHOT_DONE:
127 case CLONE_SNAPSHOT_INIT:
128 state_desc->m_num_files = 0;
129 break;
130
131 default:
132 state_desc->m_num_files = 0;
133 ut_d(ut_error);
134 }
135 }
136
137 void Clone_Snapshot::set_state_info(Clone_Desc_State *state_desc) {
138 ut_ad(mutex_own(&m_snapshot_mutex));
139
140 m_snapshot_state = state_desc->m_state;
141 m_num_current_chunks = state_desc->m_num_chunks;
142
143 if (m_snapshot_state == CLONE_SNAPSHOT_FILE_COPY) {
144 m_num_data_chunks = state_desc->m_num_chunks;
145 m_data_bytes_disk = state_desc->m_estimate_disk;
146 m_data_file_vector.resize(state_desc->m_num_files, nullptr);
147
148 m_monitor.init_state(srv_stage_clone_file_copy.m_key, m_enable_pfs);
149 m_monitor.add_estimate(state_desc->m_estimate);
150 m_monitor.change_phase();
151
152 } else if (m_snapshot_state == CLONE_SNAPSHOT_PAGE_COPY) {
153 m_num_pages = state_desc->m_num_files;
154
155 m_monitor.init_state(srv_stage_clone_page_copy.m_key, m_enable_pfs);
156 m_monitor.add_estimate(state_desc->m_estimate);
157 m_monitor.change_phase();
158
159 } else if (m_snapshot_state == CLONE_SNAPSHOT_REDO_COPY) {
160 m_num_redo_chunks = state_desc->m_num_chunks;
161 m_redo_file_vector.resize(state_desc->m_num_files, nullptr);
162
163 m_monitor.init_state(srv_stage_clone_redo_copy.m_key, m_enable_pfs);
164 m_monitor.add_estimate(state_desc->m_estimate);
165 m_monitor.change_phase();
166
167 } else if (m_snapshot_state == CLONE_SNAPSHOT_DONE) {
168 ut_ad(m_num_current_chunks == 0);
169 m_monitor.init_state(PSI_NOT_INSTRUMENTED, m_enable_pfs);
170
171 } else {
172 ut_d(ut_error);
173 }
174 }
175
176 Snapshot_State Clone_Snapshot::get_next_state() {
177 Snapshot_State next_state;
178
179 ut_ad(m_snapshot_state != CLONE_SNAPSHOT_NONE);
180
181 if (m_snapshot_state == CLONE_SNAPSHOT_INIT) {
182 next_state = CLONE_SNAPSHOT_FILE_COPY;
183
184 } else if (m_snapshot_state == CLONE_SNAPSHOT_FILE_COPY) {
185 if (m_snapshot_type == HA_CLONE_HYBRID ||
186 m_snapshot_type == HA_CLONE_PAGE) {
187 next_state = CLONE_SNAPSHOT_PAGE_COPY;
188
189 } else if (m_snapshot_type == HA_CLONE_REDO) {
190 next_state = CLONE_SNAPSHOT_REDO_COPY;
191
192 } else {
193 ut_ad(m_snapshot_type == HA_CLONE_BLOCKING);
194 next_state = CLONE_SNAPSHOT_DONE;
195 }
196
197 } else if (m_snapshot_state == CLONE_SNAPSHOT_PAGE_COPY) {
198 next_state = CLONE_SNAPSHOT_REDO_COPY;
199
200 } else {
201 ut_ad(m_snapshot_state == CLONE_SNAPSHOT_REDO_COPY);
202 next_state = CLONE_SNAPSHOT_DONE;
203 }
204
205 return (next_state);
206 }
207
208 bool Clone_Snapshot::attach(Clone_Handle_Type hdl_type, bool pfs_monitor) {
209 bool ret = false;
210 mutex_enter(&m_snapshot_mutex);
211
212 if (hdl_type == m_snapshot_handle_type &&
213 m_num_clones < MAX_CLONES_PER_SNAPSHOT) {
214 ++m_num_clones;
215 m_enable_pfs = pfs_monitor;
216
217 ut_ad(!in_transit_state());
218 ret = true;
219 }
220
221 mutex_exit(&m_snapshot_mutex);
222 return ret;
223 }
224
225 void Clone_Snapshot::detach() {
226 mutex_enter(&m_snapshot_mutex);
227
228 ut_ad(m_num_clones > 0);
229 ut_ad(!in_transit_state());
230
231 --m_num_clones;
232 ut_ad(m_num_clones == 0);
233
234 mutex_exit(&m_snapshot_mutex);
235 }
236
237 bool Clone_Snapshot::is_aborted() const {
238 ut_ad(mutex_own(&m_snapshot_mutex));
239 return m_aborted;
240 }
241
242 void Clone_Snapshot::set_abort() {
243 IB_mutex_guard guard(&m_snapshot_mutex, UT_LOCATION_HERE);
244 m_aborted = true;
245 ib::info(ER_IB_CLONE_OPERATION) << "Clone Snapshot aborted";
246 }
247
248 Clone_Snapshot::State_transit::State_transit(Clone_Snapshot *snapshot,
249 Snapshot_State new_state)
250 : m_snapshot(snapshot) {
251 mutex_enter(&m_snapshot->m_snapshot_mutex);
252
253 ut_ad(!m_snapshot->in_transit_wait());
254 ut_ad(!m_snapshot->in_transit_state());
255
256 m_snapshot->begin_transit_ddl_wait();
257 ut_ad(m_snapshot->in_transit_wait());
258
259 /* Wait for DDLs blocking clone state transition. */
260 m_error = m_snapshot->wait(Wait_type::STATE_BLOCKER, nullptr, false, true);
261
262 if (m_error != 0) {
263 return; /* purecov: inspected */
264 }
265
266 m_snapshot->begin_transit(new_state);
267 ut_ad(m_snapshot->in_transit_state());
268 }
269
270 Clone_Snapshot::State_transit::~State_transit() {
271 if (m_error == 0) {
272 m_snapshot->end_transit();
273 }
274
275 ut_ad(!m_snapshot->in_transit_state());
276 ut_ad(!m_snapshot->in_transit_wait());
277
278 mutex_exit(&m_snapshot->m_snapshot_mutex);
279 }
280
281 Clone_File_Meta *Clone_Snapshot::get_file_by_index(uint index) {
282 auto file_ctx = get_file_ctx_by_index(index);
283
284 if (file_ctx == nullptr) {
285 return nullptr;
286 }
287 return file_ctx->get_file_meta();
288 }
289
290 Clone_file_ctx *Clone_Snapshot::get_file_ctx_by_index(uint index) {
291 Clone_file_ctx *file_ctx = nullptr;
292
293 if (m_snapshot_state == CLONE_SNAPSHOT_FILE_COPY ||
294 m_snapshot_state == CLONE_SNAPSHOT_PAGE_COPY) {
295 auto num_data_files = m_data_file_vector.size();
296
297 if (index < num_data_files) {
298 file_ctx = m_data_file_vector[index];
299 }
300
301 } else if (m_snapshot_state == CLONE_SNAPSHOT_REDO_COPY) {
302 auto num_redo_files = m_redo_file_vector.size();
303
304 if (index < num_redo_files) {
305 file_ctx = m_redo_file_vector[index];
306 }
307 }
308
309 return (file_ctx);
310 }
311
312 int Clone_Snapshot::iterate_files(File_Cbk_Func &&func) {
313 int err = 0;
314
315 switch (m_snapshot_state) {
316 case CLONE_SNAPSHOT_FILE_COPY:
317 err = iterate_data_files(std::forward<File_Cbk_Func>(func));
318 break;
319 case CLONE_SNAPSHOT_REDO_COPY:
320 err = iterate_redo_files(std::forward<File_Cbk_Func>(func));
321 break;
322 default:
323 err = 0;
324 }
325 return err;
326 }
327
328 int Clone_Snapshot::iterate_data_files(File_Cbk_Func &&func) {
329 IB_mutex_guard guard(&m_snapshot_mutex, UT_LOCATION_HERE);
330
331 for (auto file_ctx : m_data_file_vector) {
332 auto err = func(file_ctx);
333 if (err != 0) {
334 return err; /* purecov: inspected */
335 }
336 }
337 return 0;
338 }
339
340 int Clone_Snapshot::iterate_redo_files(File_Cbk_Func &&func) {
341 for (auto file_ctx : m_redo_file_vector) {
342 auto err = func(file_ctx);
343 if (err != 0) {
344 return err; /* purecov: inspected */
345 }
346 }
347 return 0;
348 }
349
350 int Clone_Snapshot::get_next_block(uint chunk_num, uint &block_num,
351 const Clone_file_ctx *&file_ctx,
352 uint64_t &data_offset, byte *&data_buf,
353 uint32_t &data_size, uint64_t &file_size) {
354 uint64_t start_offset = 0;
355 const auto file_meta = file_ctx->get_file_meta_read();
356 file_size = 0;
357
358 if (m_snapshot_state == CLONE_SNAPSHOT_PAGE_COPY) {
359 /* Copy the page from buffer pool. */
360 auto err = get_next_page(chunk_num, block_num, file_ctx, data_offset,
361 data_buf, data_size, file_size);
362 return (err);
363
364 } else if (m_snapshot_state == CLONE_SNAPSHOT_REDO_COPY) {
365 /* For redo copy header and trailer are returned in buffer. */
366
367 if (chunk_num == (m_num_current_chunks - 1)) {
368 /* Last but one chunk is the redo header. */
369
370 if (block_num != 0) {
371 block_num = 0;
372 return (0);
373 }
374
375 ++block_num;
376
377 data_offset = 0;
378
379 data_buf = m_redo_header;
380 ut_ad(data_buf != nullptr);
381
382 data_size = m_redo_header_size;
383
384 return (0);
385
386 } else if (chunk_num == m_num_current_chunks) {
387 /* Last chunk is the redo trailer. */
388
389 if (block_num != 0 || m_redo_trailer_size == 0) {
390 block_num = 0;
391 return (0);
392 }
393
394 ++block_num;
395
396 data_offset = m_redo_trailer_offset;
397
398 data_buf = m_redo_trailer;
399 ut_ad(data_buf != nullptr);
400
401 data_size = m_redo_trailer_size;
402
403 return (0);
404 }
405
406 /* This is not header or trailer chunk. Need to get redo
407 data from archived file. */
408 if (file_meta->m_begin_chunk == 1) {
409 /* Set start offset for the first file. */
410 start_offset = m_redo_start_offset;
411 }
412
413 /* Dummy redo file entry. Need to send metadata. */
414 if (file_meta->m_file_size == 0) {
415 if (block_num != 0) {
416 block_num = 0;
417 return (0);
418 }
419 ++block_num;
420
421 data_buf = nullptr;
422 data_size = 0;
423 data_offset = 0;
424
425 return (0);
426 }
427 }
428
429 /* We have identified the file to transfer data at this point.
430 Get the data offset for next block to transfer. */
431 uint num_blocks;
432
433 data_buf = nullptr;
434
435 uint64_t file_chnuk_num = chunk_num - file_meta->m_begin_chunk;
436
437 /* Offset in pages for current chunk. */
438 uint64_t chunk_offset = file_chnuk_num << m_chunk_size_pow2;
439
440 /* Find number of blocks in current chunk. */
441 if (chunk_num == file_meta->m_end_chunk) {
442 /* If it is last chunk, we need to adjust the size. */
443 uint64_t size_in_pages;
444 uint aligned_sz;
445
446 ut_ad(file_meta->m_file_size >= start_offset);
447 size_in_pages = ut_uint64_align_up(file_meta->m_file_size - start_offset,
448 UNIV_PAGE_SIZE);
449 size_in_pages /= UNIV_PAGE_SIZE;
450
451 ut_ad(size_in_pages >= chunk_offset);
452 size_in_pages -= chunk_offset;
453
454 aligned_sz = static_cast<uint>(size_in_pages);
455 ut_ad(aligned_sz == size_in_pages);
456
457 aligned_sz = ut_calc_align(aligned_sz, block_size());
458
459 num_blocks = aligned_sz >> m_block_size_pow2;
460 } else {
461 num_blocks = blocks_per_chunk();
462 }
463
464 /* Current block is the last one. No more blocks in current chunk. */
465 if (block_num == num_blocks) {
466 block_num = 0;
467 return (0);
468 }
469
470 ut_ad(block_num < num_blocks);
471
472 /* Calculate the offset of next block. */
473 uint64_t block_offset;
474
475 block_offset = static_cast<uint64_t>(block_num);
476 block_offset *= block_size();
477
478 data_offset = chunk_offset + block_offset;
479 data_size = block_size();
480
481 ++block_num;
482
483 /* Convert offset and length in bytes. */
484 data_size *= UNIV_PAGE_SIZE;
485 data_offset *= UNIV_PAGE_SIZE;
486 data_offset += start_offset;
487
488 ut_ad(data_offset < file_meta->m_file_size);
489
490 /* Adjust length for last block in last chunk. */
491 if (chunk_num == file_meta->m_end_chunk && block_num == num_blocks) {
492 ut_ad((data_offset + data_size) >= file_meta->m_file_size);
493 data_size = static_cast<uint>(file_meta->m_file_size - data_offset);
494 }
495
496 #ifdef UNIV_DEBUG
497 if (m_snapshot_state == CLONE_SNAPSHOT_REDO_COPY) {
498 /* Current file is the last redo file */
499 auto redo_file_ctx = m_redo_file_vector.back();
500 if (file_meta == redo_file_ctx->get_file_meta() &&
501 m_redo_trailer_size != 0) {
502 /* Should not exceed/overwrite the trailer */
503 ut_ad(data_offset + data_size <= m_redo_trailer_offset);
504 }
505 }
506 #endif /* UNIV_DEBUG */
507
508 return (0);
509 }
510
511 void Clone_Snapshot::update_block_size(uint buff_size) {
512 mutex_enter(&m_snapshot_mutex);
513
514 /* Transfer data block is used only for direct IO. */
515 if (m_snapshot_state != CLONE_SNAPSHOT_INIT || !srv_is_direct_io()) {
516 mutex_exit(&m_snapshot_mutex);
517 return;
518 }
519
520 /* Try to set block size bigger than the transfer buffer. */
521 while (buff_size > (block_size() * UNIV_PAGE_SIZE) &&
522 m_block_size_pow2 < SNAPSHOT_MAX_BLOCK_SIZE_POW2) {
523 ++m_block_size_pow2;
524 }
525
526 mutex_exit(&m_snapshot_mutex);
527 }
528
529 uint32_t Clone_Snapshot::get_blocks_per_chunk() const {
530 IB_mutex_guard guard(&m_snapshot_mutex, UT_LOCATION_HERE);
531 uint32_t num_blocks = 0;
532
533 switch (m_snapshot_state) {
534 case CLONE_SNAPSHOT_PAGE_COPY:
535 num_blocks = chunk_size();
536 break;
537
538 case CLONE_SNAPSHOT_FILE_COPY:
539 [[fallthrough]];
540
541 case CLONE_SNAPSHOT_REDO_COPY:
542 num_blocks = blocks_per_chunk();
543 break;
544
545 default:
546 /* purecov: begin deadcode */
547 num_blocks = 0;
548 break;
549 /* purecov: end */
550 }
551 return num_blocks;
552 }
553
554 int Clone_Snapshot::change_state(Clone_Desc_State *state_desc,
555 Snapshot_State new_state, byte *temp_buffer,
556 uint temp_buffer_len, Clone_Alert_Func cbk) {
557 ut_ad(m_snapshot_state != CLONE_SNAPSHOT_NONE);
558
559 int err = 0;
560 m_num_current_chunks = 0;
561
562 if (!is_copy()) {
563 err = init_apply_state(state_desc);
564 return (err);
565 }
566
567 switch (new_state) {
568 case CLONE_SNAPSHOT_NONE:
569 case CLONE_SNAPSHOT_INIT:
570 err = ER_INTERNAL_ERROR;
571 my_error(err, MYF(0), "Innodb Clone Snapshot Invalid state");
572 ut_d(ut_error);
573 ut_o(break);
574
575 case CLONE_SNAPSHOT_FILE_COPY:
576 ib::info(ER_IB_CLONE_OPERATION) << "Clone State BEGIN FILE COPY";
577
578 err = init_file_copy(new_state);
579
580 DEBUG_SYNC_C("clone_start_page_archiving");
581 DBUG_EXECUTE_IF("clone_crash_during_page_archiving", DBUG_SUICIDE(););
582 break;
583
584 case CLONE_SNAPSHOT_PAGE_COPY:
585 ib::info(ER_IB_CLONE_OPERATION) << "Clone State BEGIN PAGE COPY";
586
587 err = init_page_copy(new_state, temp_buffer, temp_buffer_len);
588
589 DEBUG_SYNC_C("clone_start_redo_archiving");
590 break;
591
592 case CLONE_SNAPSHOT_REDO_COPY:
593 ib::info(ER_IB_CLONE_OPERATION) << "Clone State BEGIN REDO COPY";
594
595 err = init_redo_copy(new_state, cbk);
596
597 break;
598
599 case CLONE_SNAPSHOT_DONE: {
600 ib::info(ER_IB_CLONE_OPERATION) << "Clone State DONE ";
601
602 State_transit transit_guard(this, new_state);
603 m_monitor.init_state(PSI_NOT_INSTRUMENTED, m_enable_pfs);
604
605 m_redo_ctx.release();
606
607 err = transit_guard.get_error();
608 break;
609 }
610 }
611 return err;
612 }
613
614 Clone_file_ctx *Clone_Snapshot::get_file(Clone_File_Vec &file_vector,
615 uint32_t chunk_num,
616 uint32_t start_index) {
617 Clone_file_ctx *current_file = nullptr;
618 uint idx;
619
620 auto num_files = file_vector.size();
621
622 /* Scan through the file vector matching chunk number. */
623 for (idx = start_index; idx < num_files; idx++) {
624 current_file = file_vector[idx];
625 auto file_meta = current_file->get_file_meta();
626
627 ut_ad(chunk_num >= file_meta->m_begin_chunk);
628
629 if (chunk_num <= file_meta->m_end_chunk) {
630 break;
631 }
632 }
633
634 return (current_file);
635 }
636
637 void Clone_Snapshot::skip_deleted_blocks(uint32_t chunk_num,
638 uint32_t &block_num) {
639 /* For file copy entire chunk can be ignored because chunk
640 doesn't span across files. */
641 if (m_snapshot_state != CLONE_SNAPSHOT_PAGE_COPY) {
642 ut_ad(m_snapshot_state == CLONE_SNAPSHOT_FILE_COPY);
643 block_num = 0;
644 return;
645 }
646
647 const auto *cur_file_ctx = get_page_file_ctx(chunk_num, block_num);
648 const auto *next_file_ctx = cur_file_ctx;
649
650 ut_ad(cur_file_ctx->deleted());
651
652 /* Skip over the deleted file pages of current file context. */
653 while (next_file_ctx == cur_file_ctx) {
654 ++block_num;
655 next_file_ctx = get_page_file_ctx(chunk_num, block_num);
656
657 /* End of current chunk. */
658 if (next_file_ctx == nullptr || block_num >= chunk_size()) {
659 block_num = 0;
660 break;
661 }
662 }
663 }
664
665 int Clone_Snapshot::get_next_page(uint chunk_num, uint &block_num,
666 const Clone_file_ctx *&file_ctx,
667 uint64_t &data_offset, byte *&data_buf,
668 uint32_t &data_size, uint64_t &file_size) {
669 ut_ad(data_size >= UNIV_PAGE_SIZE);
670 file_size = 0;
671
672 ut_ad(file_ctx->is_pinned());
673 ut_ad(block_num < chunk_size());
674
675 /* For "page copy", each block is a page. */
676 uint32_t page_index = chunk_size() * (chunk_num - 1);
677 page_index += block_num;
678
679 ut_a(page_index < m_page_vector.size());
680 auto clone_page = m_page_vector[page_index];
681
682 ++block_num;
683
684 /* Get the data file for current page. */
685 bool found;
686 const page_size_t &page_size =
687 fil_space_get_page_size(clone_page.m_space_id, &found);
688
689 auto file_meta = file_ctx->get_file_meta_read();
690
691 ut_ad(found);
692 ut_ad(file_meta->m_space_id == clone_page.m_space_id);
693
694 /* Data offset could be beyond 32 BIT integer. */
695 data_offset = static_cast<uint64_t>(clone_page.m_page_no);
696 data_offset *= page_size.physical();
697
698 auto file_index = file_meta->m_file_index;
699
700 /* Check if the page belongs to other nodes of the tablespace. */
701 while (num_data_files() > file_index + 1) {
702 const auto file_next = m_data_file_vector[file_index + 1];
703 const auto file_meta_next = file_next->get_file_meta();
704
705 /* Next node belongs to same tablespace and data offset
706 exceeds current node size */
707 if (file_meta_next->m_space_id == file_meta->m_space_id &&
708 data_offset >= file_meta->m_file_size) {
709 data_offset -= file_meta->m_file_size;
710 file_meta = file_meta_next;
711 file_index = file_meta->m_file_index;
712 file_ctx = file_next;
713 } else {
714 break;
715 }
716 }
717
718 /* Get page from buffer pool. */
719 page_id_t page_id(clone_page.m_space_id, clone_page.m_page_no);
720
721 auto err =
722 get_page_for_write(page_id, page_size, file_ctx, data_buf, data_size);
723
724 /* Update size from space header page. */
725 if (clone_page.m_page_no == 0) {
726 auto space_size = fsp_header_get_field(data_buf, FSP_SIZE);
727
728 auto size_bytes = static_cast<uint64_t>(space_size);
729
730 size_bytes *= page_size.physical();
731
732 if (file_meta->m_file_size < size_bytes) {
733 file_size = size_bytes;
734 }
735 }
736 return (err);
737 }
738
739 bool Clone_Snapshot::encrypt_key_in_log_header(byte *log_header,
740 uint32_t header_len) {
741 size_t offset = LOG_ENCRYPTION + LOG_HEADER_ENCRYPTION_INFO_OFFSET;
742 ut_a(offset + Encryption::INFO_SIZE <= header_len);
743
744 auto encryption_info = log_header + offset;
745
746 /* Get log Encryption Key and IV. */
747 Encryption_metadata encryption_metadata;
748 auto success = Encryption::decode_encryption_info(encryption_metadata,
749 encryption_info, false);
750
751 if (success) {
752 /* Encrypt with master key and fill encryption information. */
753 success = Encryption::fill_encryption_info(encryption_metadata, true,
754 encryption_info);
755 }
756 return (success);
757 }
758
759 bool Clone_Snapshot::encrypt_key_in_header(const page_size_t &page_size,
760 byte *page_data) {
761 auto offset = fsp_header_get_encryption_offset(page_size);
762 ut_ad(offset != 0 && offset + Encryption::INFO_SIZE <= UNIV_PAGE_SIZE);
763
764 auto encryption_info = page_data + offset;
765
766 /* Get tablespace Encryption Key and IV. */
767 Encryption_metadata encryption_metadata;
768 auto success = Encryption::decode_encryption_info(encryption_metadata,
769 encryption_info, false);
770 if (!success) {
771 return (false);
772 }
773
774 /* Encrypt with master key and fill encryption information. */
775 success = Encryption::fill_encryption_info(encryption_metadata, true,
776 encryption_info);
777 if (!success) {
778 return (false);
779 }
780
781 const auto frame_lsn =
782 static_cast<lsn_t>(mach_read_from_8(page_data + FIL_PAGE_LSN));
783
784 /* Update page checksum */
785 page_update_for_flush(page_size, frame_lsn, page_data);
786
787 return (true);
788 }
789
790 void Clone_Snapshot::decrypt_key_in_header(const Clone_File_Meta *file_meta,
791 const page_size_t &page_size,
792 byte *&page_data) {
793 byte encryption_info[Encryption::INFO_SIZE];
794
795 /* Get tablespace encryption information. */
796 Encryption::fill_encryption_info(file_meta->m_encryption_metadata, false,
797 encryption_info);
798
799 /* Set encryption information in page. */
800 auto offset = fsp_header_get_encryption_offset(page_size);
801 ut_ad(offset != 0 && offset < UNIV_PAGE_SIZE);
802 memcpy(page_data + offset, encryption_info, sizeof(encryption_info));
803 }
804
805 void Clone_Snapshot::page_update_for_flush(const page_size_t &page_size,
806 lsn_t page_lsn, byte *&page_data) {
807 /* For compressed table, must copy the compressed page. */
808 if (page_size.is_compressed()) {
809 page_zip_des_t page_zip;
810
811 auto data_size = page_size.physical();
812 page_zip_set_size(&page_zip, data_size);
813 page_zip.data = page_data;
814 ut_d(page_zip.m_start = 0);
815 page_zip.m_end = 0;
816 page_zip.n_blobs = 0;
817 page_zip.m_nonempty = false;
818
819 buf_flush_init_for_writing(nullptr, page_data, &page_zip, page_lsn, false,
820 false);
821 } else {
822 buf_flush_init_for_writing(nullptr, page_data, nullptr, page_lsn, false,
823 false);
824 }
825 }
826
827 /** Set Page encryption information for IORequest.
828 @param[in,out] request IO request
829 @param[in] page_id page id
830 @param[in] file_ctx clone file context */
831 static void set_page_encryption(IORequest &request, const page_id_t &page_id,
832 const Clone_file_ctx *file_ctx) {
833 auto file_meta = file_ctx->get_file_meta_read();
834
835 /* Page zero is never encrypted. Need to also check the FSP encryption
836 flag in case decryption is in progress. */
837 if (!file_meta->can_encrypt() ||
838 !FSP_FLAGS_GET_ENCRYPTION(file_meta->m_fsp_flags) ||
839 page_id.page_no() == 0) {
840 request.clear_encrypted();
841 return;
842 }
843 request.get_encryption_info().set(file_meta->m_encryption_metadata);
844 }
845
846 int Clone_Snapshot::get_page_for_write(const page_id_t &page_id,
847 const page_size_t &page_size,
848 const Clone_file_ctx *file_ctx,
849 byte *&page_data, uint &data_size) {
850 auto file_meta = file_ctx->get_file_meta_read();
851
852 mtr_t mtr;
853 mtr_start(&mtr);
854
855 ut_ad(data_size >= 2 * page_size.physical());
856
857 data_size = page_size.physical();
858
859 /* Space header page is modified with SX latch while extending. Also,
860 we would like to serialize with page flush to disk. */
861 auto block =
862 buf_page_get_gen(page_id, page_size, RW_SX_LATCH, nullptr,
863 Page_fetch::POSSIBLY_FREED, UT_LOCATION_HERE, &mtr);
864 auto bpage = &block->page;
865
866 buf_page_mutex_enter(block);
867 ut_ad(!fsp_is_checksum_disabled(bpage->id.space()));
868 /* Get oldest and newest page modification LSN for dirty page. */
869 auto oldest_lsn = bpage->get_oldest_lsn();
870 auto newest_lsn = bpage->get_newest_lsn();
871 buf_page_mutex_exit(block);
872
873 bool page_is_dirty = (oldest_lsn > 0);
874
875 byte *src_data;
876
877 if (bpage->zip.data != nullptr) {
878 ut_ad(bpage->size.is_compressed());
879 /* If the page is not dirty, then zip descriptor always has the latest
880 flushed page copy with LSN and checksum set properly. If the page is
881 dirty, the latest modified page is in uncompressed form for uncompressed
882 page types. The LSN in such case is to be taken from block newest LSN and
883 checksum needs to be recalculated. */
884 if (page_is_dirty && page_is_uncompressed_type(block->frame)) {
885 src_data = block->frame;
886 } else {
887 src_data = bpage->zip.data;
888 }
889 } else {
890 ut_ad(!bpage->size.is_compressed());
891 src_data = block->frame;
892 }
893
894 memcpy(page_data, src_data, data_size);
895
896 auto cur_lsn = log_get_lsn(*log_sys);
897 const auto frame_lsn =
898 static_cast<lsn_t>(mach_read_from_8(page_data + FIL_PAGE_LSN));
899
900 /* First page of a encrypted tablespace. */
901 if (file_meta->can_encrypt() && page_id.page_no() == 0) {
902 /* Update unencrypted tablespace key in page 0 to be send over
903 SSL connection. */
904 decrypt_key_in_header(file_meta, page_size, page_data);
905
906 /* Force to recalculate the checksum if the page is not dirty. */
907 if (!page_is_dirty) {
908 page_is_dirty = true;
909 newest_lsn = frame_lsn;
910 }
911 }
912
913 /* If the page is not dirty but frame LSN is zero, it could be half
914 initialized page left from incomplete operation. Assign valid LSN and checksum
915 before copy. */
916 if (frame_lsn == 0 && oldest_lsn == 0) {
917 page_is_dirty = true;
918 newest_lsn = cur_lsn;
919 }
920
921 /* If page is dirty, we need to set checksum and page LSN. */
922 if (page_is_dirty) {
923 ut_ad(newest_lsn > 0);
924 page_update_for_flush(page_size, newest_lsn, page_data);
925 }
926
927 BlockReporter reporter(false, page_data, page_size, false);
928
929 const auto page_lsn =
930 static_cast<lsn_t>(mach_read_from_8(page_data + FIL_PAGE_LSN));
931
932 const auto page_checksum = static_cast<uint32_t>(
933 mach_read_from_4(page_data + FIL_PAGE_SPACE_OR_CHKSUM));
934
935 int err = 0;
936
937 if (reporter.is_corrupted() || page_lsn > cur_lsn ||
938 (page_checksum != 0 && page_lsn == 0)) {
939 my_error(ER_INTERNAL_ERROR, MYF(0), "Innodb Clone Corrupt Page");
940 err = ER_INTERNAL_ERROR;
941 ut_d(ut_error);
942 }
943
944 auto encrypted_data = page_data + data_size;
945 /* Data length could be less for compressed page */
946 auto data_len = data_size;
947
948 /* Do transparent page compression if needed. */
949 if (page_id.page_no() != 0 && file_meta->m_punch_hole &&
950 file_meta->m_compress_type != Compression::NONE) {
951 auto compressed_data = page_data + data_size;
952 memset(compressed_data, 0, data_size);
953
954 IORequest request(IORequest::WRITE);
955 request.compression_algorithm(file_meta->m_compress_type);
956 ulint compressed_len = 0;
957
958 auto buf_ptr = os_file_compress_page(
959 request.compression_algorithm(), file_meta->m_fsblk_size, page_data,
960 data_size, compressed_data, &compressed_len);
961
962 if (buf_ptr != page_data) {
963 encrypted_data = page_data;
964 page_data = compressed_data;
965 data_len = static_cast<uint>(compressed_len);
966 }
967 }
968
969 IORequest request(IORequest::WRITE);
970 set_page_encryption(request, page_id, file_ctx);
971
972 /* Encrypt page if TDE is enabled. */
973 if (err == 0 && request.is_encrypted()) {
974 Encryption encryption(request.encryption_algorithm());
975 ulint encrypt_len = data_len;
976
977 memset(encrypted_data, 0, data_size);
978 auto ret_data = encryption.encrypt(request, page_data, data_len,
979 encrypted_data, &encrypt_len);
980 if (ret_data != page_data) {
981 page_data = encrypted_data;
982 data_len = static_cast<uint>(encrypt_len);
983 }
984 }
985
986 mtr_commit(&mtr);
987 return (err);
988 }
989
990 uint32_t Clone_Snapshot::get_max_blocks_pin() const {
991 return (m_snapshot_state == CLONE_SNAPSHOT_PAGE_COPY) ? S_MAX_PAGES_PIN
992 : S_MAX_BLOCKS_PIN;
993 }
994
995 Clone_file_ctx *Clone_Snapshot::get_file_ctx(uint32_t chunk_num,
996 uint32_t block_num,
997 uint32_t hint_index) {
998 Clone_file_ctx *file = nullptr;
999
1000 switch (m_snapshot_state) {
1001 case CLONE_SNAPSHOT_FILE_COPY:
1002 file = get_data_file_ctx(chunk_num, hint_index);
1003 break;
1004 case CLONE_SNAPSHOT_PAGE_COPY:
1005 file = get_page_file_ctx(chunk_num, block_num);
1006 break;
1007 case CLONE_SNAPSHOT_REDO_COPY:
1008 file = get_redo_file_ctx(chunk_num, hint_index);
1009 break;
1010 default:
1011 ut_d(ut_error); /* purecov: deadcode */
1012 }
1013 return file;
1014 }
1015
1016 Clone_file_ctx *Clone_Snapshot::get_data_file_ctx(uint32_t chunk_num,
1017 uint32_t hint_index) {
1018 return get_file(m_data_file_vector, chunk_num, hint_index);
1019 }
1020
1021 Clone_file_ctx *Clone_Snapshot::get_redo_file_ctx(uint32_t chunk_num,
1022 uint32_t hint_index) {
1023 /* Last but one chunk is redo header */
1024 if (chunk_num == (m_num_current_chunks - 1)) {
1025 return m_redo_file_vector.front();
1026 }
1027 /* Last chunk is the redo trailer. */
1028 if (chunk_num == m_num_current_chunks) {
1029 return m_redo_file_vector.back();
1030 }
1031 return get_file(m_redo_file_vector, chunk_num, hint_index);
1032 }
1033
1034 Clone_file_ctx *Clone_Snapshot::get_page_file_ctx(uint32_t chunk_num,
1035 uint32_t block_num) {
1036 /* Check if block is beyond the current chunk. */
1037 if (block_num >= chunk_size()) {
1038 ut_ad(block_num == chunk_size());
1039 return nullptr;
1040 }
1041
1042 auto page_index = chunk_size() * (chunk_num - 1);
1043 page_index += block_num;
1044
1045 /* Check if all blocks are over. For last chunk, actual number of blocks
1046 could be less than chunk_size. */
1047 if (page_index >= m_page_vector.size()) {
1048 ut_ad(page_index == m_page_vector.size());
1049 return nullptr;
1050 }
1051
1052 auto clone_page = m_page_vector[page_index];
1053 auto file_index = m_data_file_map[clone_page.m_space_id];
1054 if (file_index == 0) {
1055 /* purecov: begin deadcode */
1056 ut_d(ut_error);
1057 ut_o(return nullptr);
1058 /* purecov: end */
1059 }
1060 --file_index;
1061
1062 auto page_file = get_file_ctx_by_index(file_index);
1063
1064 #ifdef UNIV_DEBUG
1065 auto file_meta = page_file->get_file_meta();
1066 ut_ad(file_meta->m_space_id == clone_page.m_space_id);
1067 #endif // UNIV_DEBUG
1068
1069 return page_file;
1070 }
1071
1072 void Clone_file_ctx::get_file_name(std::string &name) const {
1073 name.assign(m_meta.m_file_name);
1074
1075 /* Add file name extension. */
1076 switch (m_extension) {
1077 case Extension::REPLACE:
1078 if (m_meta.m_space_id == dict_sys_t::s_log_space_id) {
1079 const auto [directory, file] = Fil_path::split(name);
1080 name = directory + CLONE_INNODB_REPLACED_FILE_EXTN + file;
1081 } else {
1082 name.append(CLONE_INNODB_REPLACED_FILE_EXTN);
1083 }
1084 break;
1085
1086 case Extension::DDL:
1087 name.append(CLONE_INNODB_DDL_FILE_EXTN);
1088 break;
1089
1090 case Extension::NONE:
1091 default:
1092 break;
1093 }
1094 }
1095
1096 bool Clone_Snapshot::begin_ddl_state(Clone_notify::Type type, space_id_t space,
1097 bool no_wait, bool check_intr,
1098 int &error) {
1099 IB_mutex_guard guard(&m_snapshot_mutex, UT_LOCATION_HERE);
1100 error = 0;
1101 bool blocked = false;
1102
1103 for (;;) {
1104 ut_ad(mutex_own(&m_snapshot_mutex));
1105 auto state = get_state();
1106
1107 switch (state) {
1108 case CLONE_SNAPSHOT_NONE:
1109 /* purecov: begin deadcode */
1110 /* Clone must have started at this point. */
1111 ut_d(ut_error);
1112 ut_o(break);
1113 /* purecov: end */
1114
1115 case CLONE_SNAPSHOT_INIT:
1116 /* Fall through. */
1117 case CLONE_SNAPSHOT_FILE_COPY:
1118 /* Allow clone to enter next stage only after the DDL file operation
1119 is complete. */
1120 blocked = block_state_change(type, space, no_wait, check_intr, error);
1121 ut_ad(mutex_own(&m_snapshot_mutex));
1122
1123 if (error != 0) {
1124 /* We should not have blocked in case of error but it is not fatal. */
1125 ut_ad(!blocked);
1126 break;
1127 }
1128
1129 if (state != get_state()) {
1130 /* purecov: begin inspected */
1131 /* State is modified. Start again and recheck. This is safe
1132 as clone has to eventually exit from the above two states. */
1133 ut_ad(!blocked);
1134 continue;
1135 /* purecov: end */
1136 }
1137
1138 ut_ad(blocked);
1139
1140 if (state == CLONE_SNAPSHOT_FILE_COPY) {
1141 error = begin_ddl_file(type, space, no_wait, check_intr);
1142 }
1143 break;
1144
1145 case CLONE_SNAPSHOT_PAGE_COPY:
1146 /* 1. Bulk operation currently need to wait if clone has entered page
1147 copy. This is because bulk changes don't generate any redo log.
1148 2. We don't let new encryption alter to begin during page copy state.
1149 We currently cannot handle encryption key in redo log which is
1150 encrypted by donor master key. */
1151 ut_ad(!blocked);
1152 if (type == Clone_notify::Type::SPACE_ALTER_INPLACE_BULK ||
1153 type == Clone_notify::Type::SPACE_ALTER_ENCRYPT_GENERAL ||
1154 type == Clone_notify::Type::SPACE_ALTER_ENCRYPT) {
1155 error =
1156 wait(Wait_type::STATE_END_PAGE_COPY, nullptr, false, check_intr);
1157 break;
1158 }
1159 /* Try to block state change. If state is already modified then nothing
1160 to do as the next states don't require blocking. */
1161 blocked = block_state_change(type, space, no_wait, check_intr, error);
1162 if (error != 0 || state != get_state()) {
1163 /* We should not have blocked in case of error but it is not fatal. */
1164 ut_ad(!blocked);
1165 break;
1166 }
1167 ut_ad(blocked);
1168
1169 error = begin_ddl_file(type, space, no_wait, check_intr);
1170 break;
1171 case CLONE_SNAPSHOT_REDO_COPY:
1172 /* Snapshot end point is already taken. This changes are not part of
1173 snapshot. */
1174 break;
1175 case CLONE_SNAPSHOT_DONE:
1176 /* Clone has already finished. */
1177 break;
1178 default:
1179 /* purecov: begin deadcode */
1180 ut_d(ut_error);
1181 ut_o(break);
1182 /* purecov: end */
1183 }
1184 break;
1185 } /* purecov: inspected */
1186
1187 /* Unblock clone, in case of error. */
1188 if (blocked && error != 0) {
1189 /* purecov: begin inspected */
1190 unblock_state_change();
1191 blocked = false;
1192 /* purecov: end */
1193 }
1194 return blocked;
1195 }
1196
1197 void Clone_Snapshot::end_ddl_state(Clone_notify::Type type, space_id_t space) {
1198 /* Caller is responsible to call if we have blocked state change. */
1199 IB_mutex_guard guard(&m_snapshot_mutex, UT_LOCATION_HERE);
1200 auto state = get_state();
1201
1202 if (state == CLONE_SNAPSHOT_FILE_COPY || state == CLONE_SNAPSHOT_PAGE_COPY) {
1203 end_ddl_file(type, space);
1204 }
1205 unblock_state_change();
1206 }
1207
1208 void Clone_Snapshot::get_wait_mesg(Wait_type wait_type, std::string &info,
1209 std::string &error) {
1210 switch (wait_type) {
1211 case Wait_type::STATE_TRANSIT_WAIT:
1212 break;
1213 case Wait_type::STATE_TRANSIT:
1214 info.assign("DDL waiting for clone state transition");
1215 error.assign("DDL wait for clone state transition timed out");
1216 break;
1217 case Wait_type::STATE_END_PAGE_COPY:
1218 info.assign("DDL waiting for Clone PAGE COPY to finish");
1219 error.assign("DDL wait for Clone PAGE COPY timed out");
1220 break;
1221 case Wait_type::STATE_BLOCKER:
1222 info.assign("Clone state transition waiting for DDL file operation");
1223 error.assign(
1224 "Clone state transition wait for DDL file operation timed out");
1225 break;
1226 case Wait_type::DATA_FILE_WAIT:
1227 info.assign("DDL waiting for clone threads to exit from previous wait");
1228 error.assign(
1229 "DDL wait for clone threads to exit from wait state timed out");
1230 break;
1231 case Wait_type::DATA_FILE_CLOSE:
1232 info.assign("DDL waiting for clone to close the open data file");
1233 error.assign("DDL wait for clone data file close timed out");
1234 break;
1235 case Wait_type::DDL_FILE_OPERATION:
1236 info.assign("Clone waiting for DDL file operation");
1237 error.assign("Clone wait for DDL file operation timed out");
1238 break;
1239 default:
1240 ut_d(ut_error); /* purecov: deadcode */
1241 }
1242 }
1243
1244 const char *Clone_Snapshot::wait_string(Wait_type wait_type) const {
1245 const char *wait_info = nullptr;
1246
1247 switch (wait_type) {
1248 /* DDL waiting for clone state transition */
1249 case Wait_type::STATE_TRANSIT_WAIT:
1250 [[fallthrough]];
1251 case Wait_type::STATE_TRANSIT:
1252 wait_info = "Waiting for clone state transition";
1253 break;
1254
1255 /* DDL waiting till Clone PAGE COPY state is over. */
1256 case Wait_type::STATE_END_PAGE_COPY:
1257 wait_info = "Waiting for clone PAGE_COPY state";
1258 break;
1259
1260 /*DDL waiting for clone file operation. */
1261 case Wait_type::DATA_FILE_WAIT:
1262 [[fallthrough]];
1263 case Wait_type::DATA_FILE_CLOSE:
1264 wait_info = "Waiting for clone to close files";
1265 break;
1266
1267 /* Clone waiting for DDL. */
1268 case Wait_type::DDL_FILE_OPERATION:
1269 wait_info = "Waiting for ddl file operation";
1270 break;
1271
1272 case Wait_type::STATE_BLOCKER:
1273 wait_info = "Waiting for ddl before state transition";
1274 [[fallthrough]];
1275
1276 default:
1277 break;
1278 }
1279
1280 return wait_info;
1281 }
1282
1283 int Clone_Snapshot::wait(Wait_type wait_type, const Clone_file_ctx *ctx,
1284 bool no_wait, bool check_intr) {
1285 ut_ad(mutex_own(&m_snapshot_mutex));
1286
1287 std::string info_mesg;
1288 std::string error_mesg;
1289
1290 get_wait_mesg(wait_type, info_mesg, error_mesg);
1291
1292 auto wait_cond = [&](bool alert, bool &wait) {
1293 ut_ad(mutex_own(&m_snapshot_mutex));
1294 bool early_exit = false;
1295
1296 switch (wait_type) {
1297 case Wait_type::STATE_TRANSIT_WAIT:
1298 wait = in_transit_wait();
1299 /* For state transition wait by DDL, exit on alert to avoid
1300 possible deadlock between DDLs. */
1301 early_exit = true;
1302 break;
1303 case Wait_type::STATE_TRANSIT:
1304 wait = in_transit_state();
1305 break;
1306 case Wait_type::STATE_END_PAGE_COPY:
1307 /* If clone has aborted, don't wait for state to end. */
1308 wait = !is_aborted() && (get_state() == CLONE_SNAPSHOT_PAGE_COPY);
1309 DBUG_EXECUTE_IF("clone_ddl_abort_wait_page_copy", {
1310 if (wait) {
1311 my_error(ER_INTERNAL_ERROR, MYF(0), "Simulated Clone DDL error");
1312 return ER_INTERNAL_ERROR;
1313 }
1314 });
1315 break;
1316 case Wait_type::STATE_BLOCKER:
1317 wait = (m_num_blockers > 0);
1318 break;
1319 case Wait_type::DATA_FILE_WAIT:
1320 wait = ctx->is_waiting();
1321 early_exit = true;
1322 break;
1323 case Wait_type::DATA_FILE_CLOSE:
1324 wait = ctx->is_pinned();
1325 break;
1326 case Wait_type::DDL_FILE_OPERATION:
1327 wait = blocks_clone(ctx);
1328 break;
1329 default:
1330 /* purecov: begin deadcode */
1331 wait = false;
1332 ut_d(ut_error);
1333 /* purecov: end */
1334 }
1335
1336 if (wait) {
1337 if (no_wait || (alert && early_exit)) {
1338 return ER_QUERY_TIMEOUT; /* purecov: inspected */
1339 }
1340
1341 if (alert) {
1342 ib::info(ER_IB_CLONE_TIMEOUT) << info_mesg; /* purecov: tested */
1343 }
1344
1345 if (check_intr && thd_killed(nullptr)) {
1346 /* For early exit the caller would ignore error. */
1347 if (!early_exit) {
1348 my_error(ER_QUERY_INTERRUPTED, MYF(0));
1349 }
1350 return ER_QUERY_INTERRUPTED;
1351 }
1352 }
1353 return 0;
1354 };
1355
1356 /* SET THD information string to display waiting state in PROCESS LIST. */
1357 Clone_Sys::Wait_stage wait_guard(wait_string(wait_type));
1358
1359 bool is_timeout = false;
1360 int err = 0;
1361
1362 /* Increase the defaults to wait more while waiting for page copy state. */
1363 if (wait_type == Wait_type::STATE_END_PAGE_COPY) {
1364 /* Generate alert message every 5 minutes. */
1365 Clone_Sec alert_interval(Clone_Min(5));
1366 /* Wait for 2 hours for clone to finish. */
1367 Clone_Sec time_out(Clone_Min(120));
1368
1369 err = Clone_Sys::wait(CLONE_DEF_SLEEP, time_out, alert_interval, wait_cond,
1370 &m_snapshot_mutex, is_timeout);
1371 } else {
1372 err = Clone_Sys::wait_default(wait_cond, &m_snapshot_mutex, is_timeout);
1373 }
1374
1375 if (!err && is_timeout) {
1376 /* purecov: begin deadcode */
1377 err = ER_INTERNAL_ERROR;
1378 my_error(err, MYF(0), error_mesg.c_str());
1379 ut_d(ut_error);
1380 /* purecov: end */
1381 }
1382 return err;
1383 }
1384
1385 bool Clone_Snapshot::block_state_change(Clone_notify::Type type,
1386 space_id_t space, bool no_wait,
1387 bool check_intr, int &error) {
1388 ut_ad(mutex_own(&m_snapshot_mutex));
1389
1390 bool undo_ddl_ntfn = (type == Clone_notify::Type::SPACE_UNDO_DDL);
1391 bool undo_space = fsp_is_undo_tablespace(space);
1392
1393 /* For undo DDL, there could be recursive notification for file create
1394 and drop which are !undo_ddl_ntfn. For such notifications we don't need
1395 to wait for clone as we must have already blocked it. */
1396 bool wait_clone = (!undo_space || undo_ddl_ntfn);
1397
1398 /* If no wait option is used, override any waiting clone. Used for undo
1399 truncate background currently. We don't want to block purge threads. */
1400 if (no_wait) {
1401 wait_clone = false;
1402 }
1403
1404 auto saved_state = get_state();
1405
1406 /* Wait for the waiting clone. That is if clone is blocked by other DDL and
1407 waiting. This is an attempt to prevent starvation of clone by DDLs. We wait
1408 here for limited time to prevent possible deadlock between DDLs.
1409 e.g. DDL-2 <- DDL-1 (Critical section) <- Clone <- DDL-2. */
1410 if (wait_clone) {
1411 static_cast<void>(
1412 wait(Wait_type::STATE_TRANSIT_WAIT, nullptr, false, false));
1413 ut_ad(mutex_own(&m_snapshot_mutex));
1414 if (saved_state != get_state()) {
1415 /* State is modified. Return for possible recheck. */
1416 return false; /* purecov: inspected */
1417 }
1418 }
1419
1420 /* Wait for state transition to get over. */
1421 error = wait(Wait_type::STATE_TRANSIT, nullptr, no_wait, check_intr);
1422
1423 if (error != 0) {
1424 return false;
1425 }
1426
1427 ut_ad(mutex_own(&m_snapshot_mutex));
1428 if (saved_state != get_state()) {
1429 /* State is modified. Return for possible recheck. */
1430 return false; /* purecov: inspected */
1431 }
1432
1433 ut_ad(mutex_own(&m_snapshot_mutex));
1434 ++m_num_blockers;
1435
1436 return true;
1437 }
1438
1439 inline void Clone_Snapshot::unblock_state_change() {
1440 ut_ad(mutex_own(&m_snapshot_mutex));
1441 --m_num_blockers;
1442 }
1443
1444 Clone_file_ctx::State Clone_Snapshot::get_target_file_state(
1445 Clone_notify::Type type, bool begin) {
1446 auto target_state = Clone_file_ctx::State::NONE;
1447
1448 switch (type) {
1449 case Clone_notify::Type::SPACE_DROP:
1450 target_state = begin ? Clone_file_ctx::State::DROPPING
1451 : Clone_file_ctx::State::DROPPED;
1452 break;
1453 case Clone_notify::Type::SPACE_RENAME:
1454 target_state = begin ? Clone_file_ctx::State::RENAMING
1455 : Clone_file_ctx::State::RENAMED;
1456 break;
1457 default:
1458 target_state = Clone_file_ctx::State::NONE;
1459 break;
1460 }
1461 return target_state;
1462 }
1463
1464 bool Clone_Snapshot::blocks_clone(const Clone_file_ctx *file_ctx) {
1465 bool block = false;
1466 auto clone_state = get_state();
1467
1468 switch (clone_state) {
1469 case CLONE_SNAPSHOT_FILE_COPY:
1470 /* Block clone operation for both rename and delete operation
1471 as we directly access the file. */
1472 block = file_ctx->modifying();
1473 break;
1474 case CLONE_SNAPSHOT_PAGE_COPY:
1475 /* Block clone operation only if deleting. In page copy state we don't
1476 bother about space/file rename. If the page is not found in buffer pool,
1477 it would need to be read from underlying file but this IO needs to be
1478 synchronized with file operation irrespective of clone. */
1479 block = file_ctx->deleting();
1480 break;
1481 default:
1482 block = false;
1483 break;
1484 }
1485 return block;
1486 }
1487
1488 int Clone_Snapshot::begin_ddl_file(Clone_notify::Type type, space_id_t space,
1489 bool no_wait, bool check_intr) {
1490 ut_ad(mutex_own(&m_snapshot_mutex));
1491 ut_ad(get_state() == CLONE_SNAPSHOT_FILE_COPY ||
1492 get_state() == CLONE_SNAPSHOT_PAGE_COPY);
1493
1494 auto target_state = get_target_file_state(type, true);
1495
1496 /* The type doesn't need any file operation. */
1497 if (target_state == Clone_file_ctx::State::NONE) {
1498 return 0;
1499 }
1500 auto count = m_data_file_map.count(space);
1501
1502 /* The space is added concurrently and then modified again. */
1503 if (count == 0) {
1504 return 0;
1505 }
1506 /* If the space is already added for clone, we would have that in the map
1507 with a valid file index (starts from 1). */
1508 auto file_index = m_data_file_map[space];
1509
1510 if (file_index == 0) {
1511 /* purecov: begin deadcode */
1512 ut_d(ut_error);
1513 ut_o(return 0);
1514 /* purecov: end */
1515 }
1516 --file_index;
1517
1518 auto file_ctx = get_file_ctx_by_index(file_index);
1519
1520 auto saved_state = file_ctx->m_state.load();
1521
1522 ut_ad(saved_state != Clone_file_ctx::State::DROPPING);
1523 ut_ad(saved_state != Clone_file_ctx::State::RENAMING);
1524 ut_ad(saved_state != Clone_file_ctx::State::DROPPED);
1525
1526 file_ctx->m_state.store(target_state);
1527
1528 /* Wait for all data files to be closed by clone threads. */
1529 if (blocks_clone(file_ctx)) {
1530 auto err = wait(Wait_type::DATA_FILE_CLOSE, file_ctx, no_wait, check_intr);
1531
1532 if (err != 0) {
1533 /* purecov: begin inspected */
1534 file_ctx->m_state.store(saved_state);
1535 return err;
1536 /* purecov: end */
1537 }
1538 }
1539 return 0;
1540 }
1541
1542 void Clone_Snapshot::end_ddl_file(Clone_notify::Type type, space_id_t space) {
1543 ut_ad(mutex_own(&m_snapshot_mutex));
1544 ut_ad(get_state() == CLONE_SNAPSHOT_FILE_COPY ||
1545 get_state() == CLONE_SNAPSHOT_PAGE_COPY);
1546
1547 auto target_state = get_target_file_state(type, false);
1548
1549 /* The type doesn't need any file operation. */
1550 if (target_state == Clone_file_ctx::State::NONE) {
1551 return;
1552 }
1553 auto count = m_data_file_map.count(space);
1554
1555 /* The space is added concurrently and then modified again. */
1556 if (count == 0) {
1557 return;
1558 }
1559 uint32_t file_index = m_data_file_map[space];
1560
1561 if (file_index == 0) {
1562 /* purecov: begin deadcode */
1563 ut_d(ut_error);
1564 ut_o(return);
1565 /* purecov: end */
1566 }
1567 --file_index;
1568
1569 auto file_ctx = get_file_ctx_by_index(file_index);
1570 auto file_meta = file_ctx->get_file_meta();
1571
1572 file_ctx->set_ddl(get_next_state());
1573
1574 if (type == Clone_notify::Type::SPACE_DROP) {
1575 file_meta->m_deleted = true;
1576 file_ctx->m_state.store(target_state);
1577 return;
1578 }
1579
1580 bool blocking_clone = blocks_clone(file_ctx);
1581
1582 /* We need file handling for drop and rename. */
1583 ut_ad(type == Clone_notify::Type::SPACE_RENAME);
1584 file_meta->m_renamed = true;
1585 file_ctx->m_state.store(target_state);
1586
1587 if (blocking_clone) {
1588 auto fil_space = fil_space_get(space);
1589
1590 ut_ad(fil_space->files.size() == 1);
1591
1592 auto &file = fil_space->files.front();
1593 build_file_name(file_meta, file.name);
1594
1595 /* Wait for any previously waiting clone threads to restart. This is to
1596 avoid starvation of clone by repeated renames. We ignore any error. Although
1597 not expected there is no functional impact of a timeout here. */
1598 static_cast<void>(wait(Wait_type::DATA_FILE_WAIT, file_ctx, false, false));
1599 }
1600 }
1601
1602 bool Clone_Snapshot::update_deleted_state(Clone_file_ctx *file_ctx) {
1603 ut_ad(mutex_own(&m_snapshot_mutex));
1604
1605 if (file_ctx->m_state == Clone_file_ctx::State::DROPPED_HANDLED) {
1606 return false;
1607 }
1608
1609 ut_ad(file_ctx->m_state == Clone_file_ctx::State::DROPPED);
1610 /* The deleted file to be handled by current task. Set the
1611 state here so that other tasks can ignore the deleted file. */
1612 file_ctx->m_state = Clone_file_ctx::State::DROPPED_HANDLED;
1613 return true;
1614 }
1615
1616 int Clone_Snapshot::pin_file(Clone_file_ctx *file_ctx, bool &handle_delete) {
1617 handle_delete = false;
1618 file_ctx->pin();
1619
1620 /* Quick return without acquiring mutex if no DDL. */
1621 if (!blocks_clone(file_ctx)) {
1622 /* Check and update deleted state. */
1623 if (file_ctx->deleted()) {
1624 IB_mutex_guard guard(&m_snapshot_mutex, UT_LOCATION_HERE);
1625 handle_delete = update_deleted_state(file_ctx);
1626 }
1627 return 0;
1628 }
1629 file_ctx->unpin();
1630
1631 IB_mutex_guard guard(&m_snapshot_mutex, UT_LOCATION_HERE);
1632
1633 if (!blocks_clone(file_ctx)) {
1634 /* purecov: begin inspected */
1635 file_ctx->pin();
1636 /* Check and update deleted state. */
1637 if (file_ctx->deleted()) {
1638 handle_delete = update_deleted_state(file_ctx);
1639 }
1640 return 0;
1641 /* purecov: end */
1642 }
1643
1644 file_ctx->begin_wait();
1645
1646 /* Wait for DDL file operation to complete. */
1647 auto err = wait(Wait_type::DDL_FILE_OPERATION, file_ctx, false, true);
1648
1649 if (err == 0) {
1650 file_ctx->pin();
1651 /* Check and update deleted state. */
1652 if (file_ctx->deleted()) {
1653 handle_delete = update_deleted_state(file_ctx);
1654 }
1655 }
1656
1657 file_ctx->end_wait();
1658 return err;
1659 }