Debian Codegraph @

storage/innobase/include/clone0clone.h

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 include/clone0clone.h
29 Innodb Clone System
30
31 *******************************************************/
32
33 #ifndef CLONE_CLONE_INCLUDE
34 #define CLONE_CLONE_INCLUDE
35
36 #include <chrono>
37 #include "db0err.h"
38 #include "mysql/plugin.h" // thd_killed()
39 #include "sql/handler.h"
40 #include "univ.i"
41 #include "ut0mutex.h"
42
43 #include "clone0api.h"
44 #include "clone0desc.h"
45 #include "clone0repl.h"
46 #include "clone0snapshot.h"
47
48 #include <tuple>
49
50 /** Directory under data directory for all clone status files. */
51 #define CLONE_FILES_DIR OS_FILE_PREFIX "clone" OS_PATH_SEPARATOR_STR
52
53 /** Clone in progress file name length. */
54 const size_t CLONE_INNODB_FILE_LEN = 64;
55
56 #ifdef UNIV_DEBUG
57 /** Clone simulate recovery error file name. */
58 const char CLONE_INNODB_RECOVERY_CRASH_POINT[] =
59 CLONE_FILES_DIR OS_FILE_PREFIX "status_crash_point";
60 #endif
61
62 /** Clone in progress file name. */
63 const char CLONE_INNODB_IN_PROGRESS_FILE[] =
64 CLONE_FILES_DIR OS_FILE_PREFIX "status_in_progress";
65
66 /** Clone error file name. */
67 const char CLONE_INNODB_ERROR_FILE[] =
68 CLONE_FILES_DIR OS_FILE_PREFIX "status_error";
69
70 /** Clone fix up file name. Present when clone needs table fix up. */
71 const char CLONE_INNODB_FIXUP_FILE[] =
72 CLONE_FILES_DIR OS_FILE_PREFIX "status_fix";
73
74 /** Clone recovery status. */
75 const char CLONE_INNODB_RECOVERY_FILE[] =
76 CLONE_FILES_DIR OS_FILE_PREFIX "status_recovery";
77
78 /** Clone file name for list of files cloned in place. */
79 const char CLONE_INNODB_NEW_FILES[] =
80 CLONE_FILES_DIR OS_FILE_PREFIX "new_files";
81
82 /** Clone file name for list of files to be replaced. */
83 const char CLONE_INNODB_REPLACED_FILES[] =
84 CLONE_FILES_DIR OS_FILE_PREFIX "replace_files";
85
86 /** Clone file name for list of old files to be removed. */
87 const char CLONE_INNODB_OLD_FILES[] =
88 CLONE_FILES_DIR OS_FILE_PREFIX "old_files";
89
90 /** Clone file name for list of temp files renamed by ddl. */
91 const char CLONE_INNODB_DDL_FILES[] =
92 CLONE_FILES_DIR OS_FILE_PREFIX "ddl_files";
93
94 /** Clone file extension for files to be replaced. */
95 const char CLONE_INNODB_REPLACED_FILE_EXTN[] = "." OS_FILE_PREFIX "clone";
96
97 /** Clone file extension for saved old files. */
98 const char CLONE_INNODB_SAVED_FILE_EXTN[] = "." OS_FILE_PREFIX "clone_save";
99
100 /** Clone file extension for temporary renamed file. */
101 const char CLONE_INNODB_DDL_FILE_EXTN[] = "." OS_FILE_PREFIX "clone_ddl";
102
103 using Clone_Msec = std::chrono::milliseconds;
104 using Clone_Sec = std::chrono::seconds;
105 using Clone_Min = std::chrono::minutes;
106
107 /** Default sleep time while waiting: 100 ms */
108 const Clone_Msec CLONE_DEF_SLEEP{100};
109
110 /** Default alert interval in multiple of sleep time: 5 seconds */
111 const Clone_Sec CLONE_DEF_ALERT_INTERVAL{5};
112
113 /** Default timeout in multiple of sleep time: 30 minutes */
114 const Clone_Min CLONE_DEF_TIMEOUT{30};
115
116 /** Clone system state */
117 enum Clone_System_State {
118 CLONE_SYS_INACTIVE,
119 CLONE_SYS_ACTIVE,
120 CLONE_SYS_ABORT
121 };
122
123 using Clone_Sys_State = std::atomic<Clone_System_State>;
124
125 /** Clone Handle State */
126 enum Clone_Handle_State {
127 CLONE_STATE_INIT = 1,
128 CLONE_STATE_ACTIVE,
129 CLONE_STATE_IDLE,
130 CLONE_STATE_ABORT
131 };
132
133 /** Clone task state */
134 enum Clone_Task_State { CLONE_TASK_INACTIVE = 1, CLONE_TASK_ACTIVE };
135
136 /** Maximum number of concurrent snapshots */
137 const int MAX_SNAPSHOTS = 1;
138
139 /** Maximum number of concurrent clones */
140 const int MAX_CLONES = 1;
141
142 /** Clone system array size */
143 const int CLONE_ARR_SIZE = 2 * MAX_CLONES;
144
145 /** Snapshot system array size */
146 const int SNAPSHOT_ARR_SIZE = 2 * MAX_SNAPSHOTS;
147
148 /** Task for clone operation. Multiple task can concurrently work
149 on a clone operation. */
150 struct Clone_Task {
151 /** Task Meta data */
152 Clone_Task_Meta m_task_meta;
153
154 /** Task state */
155 Clone_Task_State m_task_state;
156
157 /** Serial descriptor byte string */
158 byte *m_serial_desc;
159
160 /** Serial descriptor allocated length */
161 uint m_alloc_len;
162
163 /** If task is currently pinning file. Before opening
164 the file we must have a pin on file metadata. */
165 bool m_pinned_file;
166
167 /** Current file descriptor */
168 pfs_os_file_t m_current_file_des;
169
170 /** Current file index */
171 uint m_current_file_index;
172
173 /** Data files are read using OS buffer cache */
174 bool m_file_cache;
175
176 /** If master task */
177 bool m_is_master;
178
179 /** If task has associated session */
180 bool m_has_thd;
181
182 #ifdef UNIV_DEBUG
183 /** Ignore debug sync point */
184 bool m_ignore_sync;
185
186 /** Counter to restart in different state */
187 int m_debug_counter;
188 #endif /* UNIV_DEBUG */
189
190 /** Allocated buffer */
191 byte *m_current_buffer;
192
193 /** Allocated buffer length */
194 uint m_buffer_alloc_len;
195
196 /** Data transferred for current chunk in bytes */
197 uint32_t m_data_size;
198 };
199
200 class Clone_Handle;
201
202 /** Task manager for manging the tasks for a clone operation */
203 class Clone_Task_Manager {
204 public:
205 /** Initialize task manager for clone handle
206 @param[in] snapshot snapshot */
207 void init(Clone_Snapshot *snapshot);
208
209 /** Get task state mutex
210 @return state mutex */
211 ib_mutex_t *get_mutex() { return (&m_state_mutex); }
212
213 /** Handle any error raised by concurrent tasks.
214 @param[in] raise_error raise error if true
215 @return error code */
216 int handle_error_other_task(bool raise_error);
217
218 /** Set error number
219 @param[in] err error number
220 @param[in] file_name associated file name if any */
221 void set_error(int err, const char *file_name) {
222 mutex_enter(&m_state_mutex);
223
224 ib::info(ER_IB_CLONE_OPERATION) << "Clone Set Error code: " << err
225 << " Saved Error code: " << m_saved_error;
226
227 /* Override any network error as we should not be waiting for restart
228 if other errors have occurred. */
229 if (m_saved_error == 0 || is_network_error(m_saved_error)) {
230 m_saved_error = err;
231
232 if (file_name != nullptr) {
233 m_err_file_name.assign(file_name);
234 }
235 }
236
237 mutex_exit(&m_state_mutex);
238 }
239
240 /** Add a task to task manager
241 @param[in] thd server THD object
242 @param[in] ref_loc reference locator from remote
243 @param[in] loc_len locator length in bytes
244 @param[out] task_id task identifier
245 @return error code */
246 int add_task(THD *thd, const byte *ref_loc, uint loc_len, uint &task_id);
247
248 /** Drop task from task manager
249 @param[in] thd server THD object
250 @param[in] task_id current task ID
251 @param[out] is_master true, if master task
252 @return true if needs to wait for re-start */
253 bool drop_task(THD *thd, uint task_id, bool &is_master);
254
255 /** Check if chunk is already reserved.
256 @param[in] chunk_num chunk number
257 @return true, iff chunk is reserved. */
258 bool is_chunk_reserved(uint32_t chunk_num) {
259 return m_chunk_info.m_reserved_chunks[chunk_num];
260 }
261
262 /** Reset chunk information for task
263 @param[in] task current task */
264 void reset_chunk(Clone_Task *task) {
265 ut_ad(mutex_own(&m_state_mutex));
266
267 /* Reset current processing chunk */
268 task->m_task_meta.m_chunk_num = 0;
269 task->m_task_meta.m_block_num = 0;
270
271 if (task->m_data_size > 0) {
272 ut_ad(get_state() != CLONE_SNAPSHOT_NONE);
273 ut_ad(get_state() != CLONE_SNAPSHOT_INIT);
274 ut_ad(get_state() != CLONE_SNAPSHOT_DONE);
275
276 auto &monitor = m_clone_snapshot->get_clone_monitor();
277
278 monitor.update_work(task->m_data_size);
279 }
280
281 task->m_data_size = 0;
282 }
283
284 /** Get task by index
285 @param[in] index task index
286 @return task */
287 Clone_Task *get_task_by_index(uint index) {
288 auto task = (m_clone_tasks + index);
289 ut_ad(task->m_task_state == CLONE_TASK_ACTIVE);
290
291 return (task);
292 }
293
294 /** Reserve next chunk from task manager. Called by individual tasks.
295 @param[in] task requesting task
296 @param[out] ret_chunk reserved chunk number
297 @param[out] ret_block start block number
298 '0' if no more chunk.
299 @return error code */
300 int reserve_next_chunk(Clone_Task *task, uint32_t &ret_chunk,
301 uint32_t &ret_block);
302
303 /** Set current chunk and block information
304 @param[in,out] task requesting task
305 @param[in] new_meta updated task metadata
306 @return error code */
307 int set_chunk(Clone_Task *task, Clone_Task_Meta *new_meta);
308
309 /** Track any incomplete chunks handled by the task
310 @param[in,out] task current task */
311 void add_incomplete_chunk(Clone_Task *task);
312
313 /** Initialize task manager for current state */
314 void init_state();
315
316 /** Reinitialize state using locator
317 @param[in] loc locator from remote client
318 @param[in] loc_len locator length in bytes */
319 void reinit_copy_state(const byte *loc, uint loc_len);
320
321 /** Reinitialize state using locator
322 @param[in] ref_loc current locator
323 @param[in] ref_len current locator length
324 @param[out] new_loc new locator to be sent to remote server
325 @param[out] new_len length of new locator
326 @param[in,out] alloc_len allocated length for locator buffer */
327 void reinit_apply_state(const byte *ref_loc, uint ref_len, byte *&new_loc,
328 uint &new_len, uint &alloc_len);
329
330 /** Reset state transition information */
331 void reset_transition() {
332 m_num_tasks_transit = 0;
333 m_num_tasks_finished = 0;
334 m_next_state = CLONE_SNAPSHOT_NONE;
335 }
336
337 /** Reset error information */
338 void reset_error() {
339 m_saved_error = 0;
340 m_err_file_name.assign("Clone File");
341 }
342
343 /** Get current clone state
344 @return clone state */
345 Snapshot_State get_state() { return (m_current_state); }
346
347 /** Check if in state transition
348 @return true if state transition is in progress */
349 bool in_transit_state() { return (m_next_state != CLONE_SNAPSHOT_NONE); }
350
351 /** Get attached snapshot
352 @return snapshot */
353 Clone_Snapshot *get_snapshot() { return (m_clone_snapshot); }
354
355 /** Move to next snapshot state. Each task must call this after
356 no more chunk is left in current state. The state can be changed
357 only after all tasks have finished transferring the reserved chunks.
358 @param[in] task clone task
359 @param[in] state_desc descriptor for next state
360 @param[in] new_state next state to move to
361 @param[in] cbk alert callback for long wait
362 @param[out] num_wait unfinished tasks in current state
363 @return error code */
364 int change_state(Clone_Task *task, Clone_Desc_State *state_desc,
365 Snapshot_State new_state, Clone_Alert_Func cbk,
366 uint &num_wait);
367
368 /** Check if state transition is over and all tasks moved to next state
369 @param[in] task requesting task
370 @param[in] new_state next state to move to
371 @param[in] exit_on_wait exit from transition if needs to wait
372 @param[in] in_err input error if already occurred
373 @param[out] num_wait number of tasks to move to next state
374 @return error code */
375 int check_state(Clone_Task *task, Snapshot_State new_state, bool exit_on_wait,
376 int in_err, uint32_t &num_wait);
377
378 /** Check if needs to send state metadata once
379 @param[in] task current task
380 @return true if needs to send state metadata */
381 bool is_restart_metadata(Clone_Task *task) {
382 if (task->m_is_master && m_send_state_meta) {
383 m_send_state_meta = false;
384 return (true);
385 }
386
387 return (false);
388 }
389
390 /** @return true if file metadata is transferred */
391 bool is_file_metadata_transferred() const {
392 return (m_transferred_file_meta);
393 }
394
395 /** Set sub-state: all file metadata is transferred */
396 void set_file_meta_transferred() { m_transferred_file_meta = true; }
397
398 /** Mark state finished for current task
399 @param[in] task current task
400 @return error code */
401 int finish_state(Clone_Task *task);
402
403 /** Set acknowledged state
404 @param[in] state_desc State descriptor */
405 void ack_state(const Clone_Desc_State *state_desc);
406
407 /** Wait for acknowledgement
408 @param[in] clone parent clone handle
409 @param[in] task current task
410 @param[in] callback user callback interface
411 @return error code */
412 int wait_ack(Clone_Handle *clone, Clone_Task *task, Ha_clone_cbk *callback);
413
414 /** Check if state ACK is needed
415 @param[in] state_desc State descriptor
416 @return true if need to wait for ACK from remote */
417 bool check_ack(const Clone_Desc_State *state_desc) {
418 bool ret = true;
419
420 mutex_enter(&m_state_mutex);
421
422 /* Check if state is already acknowledged */
423 if (m_ack_state == state_desc->m_state) {
424 ut_ad(m_restart_count > 0);
425 ret = false;
426 ++m_num_tasks_finished;
427 }
428
429 mutex_exit(&m_state_mutex);
430
431 return (ret);
432 }
433
434 /** Check if clone is restarted after failure
435 @return true if restarted */
436 bool is_restarted() { return (m_restart_count > 0); }
437
438 /** Allocate buffers for current task
439 @param[in,out] task current task
440 @return error code */
441 int alloc_buffer(Clone_Task *task);
442
443 #ifdef UNIV_DEBUG
444 /** Check if needs to wait for debug sync point
445 @param[in] chunk_num chunk number to process
446 @param[in] task current task
447 @return true, if clone needs to check and wait */
448 bool debug_sync_check(uint32_t chunk_num, Clone_Task *task);
449
450 /** Wait during clone operation
451 @param[in] chunk_num chunk number to process
452 @param[in] task current task */
453 void debug_wait(uint chunk_num, Clone_Task *task);
454
455 /** Wait before sending DDL metadata. */
456 void debug_wait_ddl_meta();
457
458 /** Force restart clone operation by raising network error
459 @param[in] task current task
460 @param[in] in_err any err that has occurred
461 @param[in] restart_count restart counter
462 @return error code */
463 int debug_restart(Clone_Task *task, int in_err, int restart_count);
464
465 /** @return clone master task. */
466 Clone_Task *find_master_task();
467 #endif /* UNIV_DEBUG */
468
469 private:
470 /** Check if we need to wait before adding current task
471 @param[in] ref_loc reference locator from remote
472 @param[in] loc_len reference locator length
473 @return true, if needs to wait */
474 bool wait_before_add(const byte *ref_loc, uint loc_len);
475
476 private:
477 /** Check if network error
478 @param[in] err error code
479 @return true if network error */
480 bool is_network_error(int err) {
481 if (err == ER_NET_ERROR_ON_WRITE || err == ER_NET_READ_ERROR ||
482 err == ER_NET_WRITE_INTERRUPTED || err == ER_NET_READ_INTERRUPTED ||
483 err == ER_NET_WAIT_ERROR) {
484 return (true);
485 }
486 return (false);
487 }
488
489 /** Reserve free task from task manager and initialize
490 @param[in] thd server THD object
491 @param[out] task_id initialized task ID */
492 void reserve_task(THD *thd, uint &task_id);
493
494 /** Check if we should process incomplete chunk next. Incomplete
495 chunks could be there after a re-start from network failure. We always
496 process the chunks in order and need to choose accordingly.
497 @return if need to process incomplete chunk next. */
498 inline bool process_inclomplete_chunk() {
499 /* 1. Check if there is any incomplete chunk. */
500 auto &chunks = m_chunk_info.m_incomplete_chunks;
501 if (chunks.empty()) {
502 return (false);
503 }
504
505 /* 2. Check if all complete chunks are processed. */
506 auto min_complete_chunk = m_chunk_info.m_min_unres_chunk;
507 if (min_complete_chunk > m_chunk_info.m_total_chunks) {
508 return (true);
509 }
510
511 /* 3. Compare the minimum chunk number for complete and incomplete chunk */
512 auto it = chunks.begin();
513 auto min_incomplete_chunk = it->first;
514
515 ut_ad(min_complete_chunk != min_incomplete_chunk);
516 return (min_incomplete_chunk < min_complete_chunk);
517 }
518
519 /** Get next in complete chunk if any
520 @param[out] block_num first block number in chunk
521 @return incomplete chunk number */
522 uint32_t get_next_incomplete_chunk(uint32_t &block_num);
523
524 /** Get next unreserved chunk
525 @return chunk number */
526 uint32_t get_next_chunk();
527
528 private:
529 /** Mutex synchronizing access by concurrent tasks */
530 ib_mutex_t m_state_mutex;
531
532 /** Finished and incomplete chunk information */
533 Chunk_Info m_chunk_info;
534
535 /** Clone task array */
536 Clone_Task m_clone_tasks[CLONE_MAX_TASKS];
537
538 /** Current number of tasks */
539 uint m_num_tasks;
540
541 /** Number of tasks finished current state */
542 uint m_num_tasks_finished;
543
544 /** Number of tasks in transit state */
545 uint m_num_tasks_transit;
546
547 /** Number of times clone is restarted */
548 uint m_restart_count;
549
550 /** Acknowledged state from client */
551 Snapshot_State m_ack_state;
552
553 /** Current state for clone */
554 Snapshot_State m_current_state;
555
556 /** Next state: used during state transfer */
557 Snapshot_State m_next_state;
558
559 /* Sub state: File metadata is transferred */
560 bool m_transferred_file_meta;
561
562 /** Send state metadata before starting: Used for restart */
563 bool m_send_state_meta;
564
565 /** Save any error raised by a task */
566 int m_saved_error;
567
568 /** File name related to the saved error */
569 std::string m_err_file_name;
570
571 /** Attached snapshot handle */
572 Clone_Snapshot *m_clone_snapshot;
573 };
574
575 /** Clone Handle for copying or applying data */
576 class Clone_Handle {
577 public:
578 /** Construct clone handle
579 @param[in] handle_type clone handle type
580 @param[in] clone_version clone version
581 @param[in] clone_index index in clone array */
582 Clone_Handle(Clone_Handle_Type handle_type, uint clone_version,
583 uint clone_index);
584
585 /** Destructor: Detach from snapshot */
586 ~Clone_Handle();
587
588 /** Initialize clone handle
589 @param[in] ref_loc reference locator
590 @param[in] ref_len reference locator length
591 @param[in] type clone type
592 @param[in] data_dir data directory for apply
593 @return error code */
594 int init(const byte *ref_loc, uint ref_len, Ha_clone_type type,
595 const char *data_dir);
596
597 /** Attach to the clone handle */
598 void attach() { ++m_ref_count; }
599
600 /** Detach from the clone handle
601 @return reference count */
602 uint detach() {
603 ut_a(m_ref_count > 0);
604 --m_ref_count;
605
606 return (m_ref_count);
607 }
608
609 /** Get locator for the clone handle.
610 @param[out] loc_len serialized locator length
611 @return serialized clone locator */
612 byte *get_locator(uint &loc_len);
613
614 /** @return clone data directory */
615 const char *get_datadir() const { return (m_clone_dir); }
616
617 /** @return true, if clone is replacing current data directory. */
618 bool replace_datadir() const {
619 return (!is_copy_clone() && m_clone_dir == nullptr);
620 }
621
622 /** Build locator descriptor for the clone handle
623 @param[out] loc_desc locator descriptor */
624 void build_descriptor(Clone_Desc_Locator *loc_desc);
625
626 /** Add a task to clone handle
627 @param[in] thd server THD object
628 @param[in] ref_loc reference locator from remote
629 @param[in] ref_len reference locator length
630 @param[out] task_id task identifier
631 @return error code */
632 int add_task(THD *thd, const byte *ref_loc, uint ref_len, uint &task_id) {
633 return (m_clone_task_manager.add_task(thd, ref_loc, ref_len, task_id));
634 }
635
636 /** Drop task from clone handle
637 @param[in] thd server THD object
638 @param[in] task_id current task ID
639 @param[out] is_master true, if master task
640 @return true if needs to wait for re-start */
641 bool drop_task(THD *thd, uint task_id, bool &is_master);
642
643 /** Save current error number
644 @param[in] err error number */
645 void save_error(int err) {
646 if (err != 0) {
647 m_clone_task_manager.set_error(err, nullptr);
648 }
649 }
650
651 /** Check for error from other tasks and DDL
652 @param[in,out] thd session THD
653 @return error code */
654 int check_error(THD *thd) {
655 bool has_thd = (thd != nullptr);
656 auto err = m_clone_task_manager.handle_error_other_task(has_thd);
657 /* Save any error reported */
658 save_error(err);
659 return (err);
660 }
661
662 /** @return true if any task is interrupted */
663 bool is_interrupted() {
664 auto err = m_clone_task_manager.handle_error_other_task(false);
665 return (err == ER_QUERY_INTERRUPTED);
666 }
667
668 /** Get clone handle index in clone array
669 @return array index */
670 uint get_index() { return (m_clone_arr_index); }
671
672 /** Get clone data descriptor version
673 @return version */
674 uint get_version() { return (m_clone_desc_version); }
675
676 /** @return active snapshot */
677 Clone_Snapshot *get_snapshot() { return m_clone_task_manager.get_snapshot(); }
678
679 /** Check if it is copy clone
680 @return true if copy clone handle */
681 bool is_copy_clone() const { return (m_clone_handle_type == CLONE_HDL_COPY); }
682
683 /** Check if clone type matches
684 @param[in] other_handle_type type to match with
685 @return true if type matches with clone handle type */
686 bool match_hdl_type(Clone_Handle_Type other_handle_type) {
687 return (m_clone_handle_type == other_handle_type);
688 }
689
690 /** Set current clone state
691 @param[in] state clone handle state */
692 void set_state(Clone_Handle_State state) { m_clone_handle_state = state; }
693
694 /** Set clone to ABORT state end any attached snapshot. */
695 void set_abort();
696
697 /** Check if clone state is active
698 @return true if in active state */
699 bool is_active() { return (m_clone_handle_state == CLONE_STATE_ACTIVE); }
700
701 /** Check if clone is initialized
702 @return true if in initial state */
703 bool is_init() { return (m_clone_handle_state == CLONE_STATE_INIT); }
704
705 /** Check if clone is idle waiting for restart
706 @return true if clone is in idle state */
707 bool is_idle() { return (m_clone_handle_state == CLONE_STATE_IDLE); }
708
709 /** Check if clone is aborted
710 @return true if clone is aborted */
711 bool is_abort() { return (m_clone_handle_state == CLONE_STATE_ABORT); }
712
713 /** Restart copy after a network failure
714 @param[in] thd server THD object
715 @param[in] loc locator with copy state from remote client
716 @param[in] loc_len locator length in bytes
717 @return error code */
718 int restart_copy(THD *thd, const byte *loc, uint loc_len);
719
720 /** Build locator with current state and restart apply
721 @param[in] thd server THD object
722 @param[in,out] loc loctor with current state information
723 @param[in,out] loc_len locator length in bytes
724 @return error code */
725 int restart_apply(THD *thd, const byte *&loc, uint &loc_len);
726
727 /** Transfer snapshot data via callback
728 @param[in] task_id current task ID
729 @param[in] callback user callback interface
730 @return error code */
731 int copy(uint task_id, Ha_clone_cbk *callback);
732
733 /** Apply snapshot data received via callback
734 @param[in] thd server THD
735 @param[in] task_id current task ID
736 @param[in] callback user callback interface
737 @return error code */
738 int apply(THD *thd, uint task_id, Ha_clone_cbk *callback);
739
740 /** Send keep alive while during long wait
741 @param[in] task task that is sending the information
742 @param[in] callback callback interface
743 @return error code */
744 int send_keep_alive(Clone_Task *task, Ha_clone_cbk *callback);
745
746 /** @return true iff DDL should abort running clone. */
747 bool abort_by_ddl() const { return m_abort_ddl; }
748
749 /** Allow concurrent DDL to abort clone. */
750 void set_ddl_abort() { m_abort_ddl = true; }
751
752 #ifdef UNIV_DEBUG
753 /** Close master task file if open and unpin. */
754 void close_master_file();
755 #endif /* UNIV_DEBUG */
756
757 private:
758 /** Check if enough space is there to clone.
759 @param[in] task current task
760 @return error if not enough space */
761 int check_space(const Clone_Task *task);
762
763 /** Create clone data directory.
764 @return error code */
765 int create_clone_directory();
766
767 /** Display clone progress
768 @param[in] cur_chunk current chunk number
769 @param[in] max_chunk total number of chunks
770 @param[in,out] percent_done percentage completed
771 @param[in,out] disp_time last displayed time */
772 void display_progress(uint32_t cur_chunk, uint32_t max_chunk,
773 uint32_t &percent_done,
774 std::chrono::steady_clock::time_point &disp_time);
775
776 /** Create a tablespace file and initialize.
777 @param[in] file_ctx file information
778 @param[in] file_type file type (data, log etc.)
779 @param[in] init true, if needs to write initial pages.
780 @return error code */
781 int file_create_init(const Clone_file_ctx *file_ctx, ulint file_type,
782 bool init);
783
784 using File_init_cbk = std::function<dberr_t(pfs_os_file_t)>;
785
786 /** Open file for the task
787 @param[in] task clone task
788 @param[in] file_ctx file information
789 @param[in] file_type file type (data, log etc.)
790 @param[in] create_file create if not present
791 @param[in] init_cbk callback to fill initial data
792 @return error code */
793 int open_file(Clone_Task *task, const Clone_file_ctx *file_ctx,
794 ulint file_type, bool create_file, File_init_cbk &init_cbk);
795
796 /** Close file for the task
797 @param[in] task clone task
798 @return error code */
799 int close_file(Clone_Task *task);
800
801 /** Check and pin a file context if not already pinned.
802 @param[in,out] task clone task
803 @param[in,out] file_ctx snapshot file context
804 @param[out] handle_deleted true, iff caller needs to handle
805 deleted file state
806 @return error code */
807 int check_and_pin_file(Clone_Task *task, Clone_file_ctx *file_ctx,
808 bool &handle_deleted);
809
810 /** Unpin and close currently pinned file.
811 @param[in,out] task clone task
812 @return error code */
813 int close_and_unpin_file(Clone_Task *task);
814
815 /** Check if the task pins a file context.
816 @param[in] task clone task
817 @param[in] file_ctx snapshot file context
818 @return true, if task pins the file, other file. */
819 std::tuple<bool, bool> pins_file(const Clone_Task *task,
820 const Clone_file_ctx *file_ctx);
821
822 /** Callback providing the file reference and data length to copy
823 @param[in] cbk callback interface
824 @param[in] task clone task
825 @param[in] len data length
826 @param[in] buf_cbk invoke buffer callback
827 @param[in] offset file offset
828 @param[in] location location where func invoked
829 @return error code */
830 int file_callback(Ha_clone_cbk *cbk, Clone_Task *task, uint len, bool buf_cbk,
831 uint64_t offset
832 #ifdef UNIV_PFS_IO
833 ,
834 ut::Location location
835 #endif /* UNIV_PFS_IO */
836 );
837
838 /** Move to next state
839 @param[in] task clone task
840 @param[in] callback callback interface
841 @param[in] state_desc descriptor for next state to move to
842 @return error code */
843 int move_to_next_state(Clone_Task *task, Ha_clone_cbk *callback,
844 Clone_Desc_State *state_desc);
845
846 /** Send current state information via callback
847 @param[in] task task that is sending the information
848 @param[in] callback callback interface
849 @param[in] is_start if it is the start of current state
850 @return error code */
851 int send_state_metadata(Clone_Task *task, Ha_clone_cbk *callback,
852 bool is_start);
853
854 /** Send current task information via callback
855 @param[in] task task that is sending the information
856 @param[in] callback callback interface
857 @return error code */
858 int send_task_metadata(Clone_Task *task, Ha_clone_cbk *callback);
859
860 /** Send all DDL metadata generated.
861 @param[in] task task that is sending the information
862 @param[in] callback callback interface
863 @return error code */
864 int send_all_ddl_metadata(Clone_Task *task, Ha_clone_cbk *callback);
865
866 /** Send all file information via callback
867 @param[in] task task that is sending the information
868 @param[in] callback callback interface
869 @return error code */
870 int send_all_file_metadata(Clone_Task *task, Ha_clone_cbk *callback);
871
872 /** Send current file information via callback
873 @param[in] task task that is sending the information
874 @param[in] file_meta file meta information
875 @param[in] is_redo true if redo file
876 @param[in] callback callback interface
877 @return error code */
878 int send_file_metadata(Clone_Task *task, const Clone_File_Meta *file_meta,
879 bool is_redo, Ha_clone_cbk *callback);
880
881 /** Send cloned data via callback
882 @param[in] task task that is sending the information
883 @param[in] file_ctx file information
884 @param[in] offset file offset
885 @param[in] buffer data buffer or NULL if send from file
886 @param[in] size data buffer size
887 @param[in] new_file_size updated file size from page 0
888 @param[in] callback callback interface
889 @return error code */
890 int send_data(Clone_Task *task, const Clone_file_ctx *file_ctx,
891 uint64_t offset, byte *buffer, uint32_t size,
892 uint64_t new_file_size, Ha_clone_cbk *callback);
893
894 /** Process a data chunk and send data blocks via callback
895 @param[in] task task that is sending the information
896 @param[in] chunk_num chunk number to process
897 @param[in] block_num start block number
898 @param[in] callback callback interface
899 @return error code */
900 int process_chunk(Clone_Task *task, uint32_t chunk_num, uint32_t block_num,
901 Ha_clone_cbk *callback);
902
903 /** Create apply task based on task metadata in callback
904 @param[in] task current task
905 @param[in] callback callback interface
906 @return error code */
907 int apply_task_metadata(Clone_Task *task, Ha_clone_cbk *callback);
908
909 /** Move to next state based on state metadata and set
910 state information
911 @param[in] task current task
912 @param[in,out] callback callback interface
913 @param[in,out] state_desc clone state descriptor
914 @return error code */
915 int ack_state_metadata(Clone_Task *task, Ha_clone_cbk *callback,
916 Clone_Desc_State *state_desc);
917
918 /** Notify state change via callback.
919 @param[in] task current task
920 @param[in,out] callback callback interface
921 @param[in,out] state_desc clone state descriptor */
922 void notify_state_change(Clone_Task *task, Ha_clone_cbk *callback,
923 Clone_Desc_State *state_desc);
924
925 /** Move to next state based on state metadata and set
926 state information
927 @param[in] task current task
928 @param[in] callback callback interface
929 @return error code */
930 int apply_state_metadata(Clone_Task *task, Ha_clone_cbk *callback);
931
932 /** Create file metadata based on callback
933 @param[in] task current task
934 @param[in] callback callback interface
935 @return error code */
936 int apply_file_metadata(Clone_Task *task, Ha_clone_cbk *callback);
937
938 /** Apply DDL delete to existing file to update chunk and block information.
939 @param[in,out] task task performing the operation
940 @param[in,out] file_ctx current file context
941 @param[in] new_meta new file metadata
942 @return error code */
943 int apply_file_delete(Clone_Task *task, Clone_file_ctx *file_ctx,
944 const Clone_File_Meta *new_meta);
945
946 /** Apply DDL changes to file at the end of FILE_COPY stage.
947 @param[in] new_meta new file metadata
948 @param[in,out] file_ctx current file context
949 @return error code. */
950 int apply_ddl(const Clone_File_Meta *new_meta, Clone_file_ctx *file_ctx);
951
952 /** Set compression type based on local capability.
953 @param[in,out] file_ctx file context
954 @return error code. */
955 int set_compression(Clone_file_ctx *file_ctx);
956
957 /** Fix the file name and meta information for all files that are renamed
958 with DDL extension.
959 @param[in] task current task
960 @return error code. */
961 int fix_all_renamed(const Clone_Task *task);
962
963 /** Apply data received via callback
964 @param[in] task current task
965 @param[in] callback callback interface
966 @return error code */
967 int apply_data(Clone_Task *task, Ha_clone_cbk *callback);
968
969 /** Receive data from callback and apply
970 @param[in] task task that is receiving the information
971 @param[in] offset file offset for applying data
972 @param[in] file_size updated file size
973 @param[in] size data length in bytes
974 @param[in] callback callback interface
975 @return error code */
976 int receive_data(Clone_Task *task, uint64_t offset, uint64_t file_size,
977 uint32_t size, Ha_clone_cbk *callback);
978
979 /** Read compressed length from the page
980 @param[in] buffer data buffer
981 @param[in] len buffer length
982 @param[in] block_size block size
983 @param[out] compressed_len compressed length
984 @return true for compressed page false otherwise. */
985 bool read_compressed_len(unsigned char *buffer, uint32_t len,
986 uint32_t block_size, uint32_t &compressed_len);
987
988 /** Write pages to file and punch holes
989 @param[in] file_meta clone file metadata
990 @param[in] buffer data buffer
991 @param[in] len buffer length
992 @param[in] file file descriptor
993 @param[in] start_off starting offset in file
994 @return error code */
995 int sparse_file_write(Clone_File_Meta *file_meta, unsigned char *buffer,
996 uint32_t len, pfs_os_file_t file, uint64_t start_off);
997
998 /** Modify page encryption attribute and/or punch hole.
999 @param[in] task task that is applying data
1000 @param[in] offset file offset for applying data
1001 @param[in,out] buffer data to apply
1002 @param[in] buf_len data buffer length
1003 @return error code */
1004 int modify_and_write(const Clone_Task *task, uint64_t offset,
1005 unsigned char *buffer, uint32_t buf_len);
1006
1007 private:
1008 /** Clone handle type: Copy, Apply */
1009 Clone_Handle_Type m_clone_handle_type;
1010
1011 /** Clone handle state */
1012 Clone_Handle_State m_clone_handle_state;
1013
1014 /** Fixed locator for version negotiation. */
1015 byte m_version_locator[CLONE_DESC_MAX_BASE_LEN];
1016
1017 /** Serialized locator */
1018 byte *m_clone_locator;
1019
1020 /** Locator length in bytes */
1021 uint m_locator_length;
1022
1023 /** Serialized Restart locator */
1024 byte *m_restart_loc;
1025
1026 /** Restart locator length in bytes */
1027 uint m_restart_loc_len;
1028
1029 /** Clone descriptor version in use */
1030 uint m_clone_desc_version;
1031
1032 /** Index in global array */
1033 uint m_clone_arr_index;
1034
1035 /** Unique clone identifier */
1036 uint64_t m_clone_id;
1037
1038 /** Reference count */
1039 uint m_ref_count;
1040
1041 /** Allow restart of clone operation after network failure */
1042 bool m_allow_restart;
1043
1044 /** If concurrent DDL should abort clone. */
1045 bool m_abort_ddl;
1046
1047 /** Clone data directory */
1048 const char *m_clone_dir;
1049
1050 /** Clone task manager */
1051 Clone_Task_Manager m_clone_task_manager;
1052 };
1053
1054 /** Clone System */
1055 class Clone_Sys {
1056 public:
1057 /** RAII style wrapper to enter and exit wait stage. */
1058 class Wait_stage : private ut::Non_copyable {
1059 public:
1060 /** Constructor to change the THD information string.
1061 @param[in] new_info new information string */
1062 explicit Wait_stage(const char *new_info);
1063
1064 /** Destructor to revert back the old information string. */
1065 ~Wait_stage();
1066
1067 private:
1068 /** Saved old THD information string. */
1069 const char *m_saved_info;
1070 };
1071
1072 class Acquire_clone : private ut::Non_copyable {
1073 public:
1074 /** Constructor to get and pin clone handle. */
1075 explicit Acquire_clone();
1076
1077 /** Destructor to release and free clone handle if necessary. */
1078 ~Acquire_clone();
1079
1080 /** Get current clone snapshot. */
1081 Clone_Snapshot *get_snapshot();
1082
1083 private:
1084 /** Acquired clone handle */
1085 Clone_Handle *m_clone{};
1086 };
1087
1088 /** Construct clone system */
1089 Clone_Sys();
1090
1091 /** Destructor: Call during system shutdown */
1092 ~Clone_Sys();
1093
1094 /** Create and add a new clone handle to clone system
1095 @param[in] loc locator
1096 @param[in] hdl_type handle type
1097 @param[out] clone_hdl clone handle
1098 @return error code */
1099 int add_clone(const byte *loc, Clone_Handle_Type hdl_type,
1100 Clone_Handle *&clone_hdl);
1101
1102 /** drop a clone handle from clone system
1103 @param[in] clone_handle Clone handle */
1104 void drop_clone(Clone_Handle *clone_handle);
1105
1106 /** Find if a clone is already running for the reference locator
1107 @param[in] ref_loc reference locator
1108 @param[in] loc_len reference locator length
1109 @param[in] hdl_type clone type
1110 @return clone handle if found, NULL otherwise */
1111 Clone_Handle *find_clone(const byte *ref_loc, uint loc_len,
1112 Clone_Handle_Type hdl_type);
1113
1114 /** Get the clone handle from locator by index
1115 @param[in] loc locator
1116 @param[in] loc_len locator length in bytes
1117 @return clone handle */
1118 Clone_Handle *get_clone_by_index(const byte *loc, uint loc_len);
1119
1120 /** Get or create a snapshot for clone and attach
1121 @param[in] hdl_type handle type
1122 @param[in] clone_type clone type
1123 @param[in] snapshot_id snapshot identifier
1124 @param[in] is_pfs_monitor true, if needs PFS monitoring
1125 @param[out] snapshot clone snapshot
1126 @return error code */
1127 int attach_snapshot(Clone_Handle_Type hdl_type, Ha_clone_type clone_type,
1128 uint64_t snapshot_id, bool is_pfs_monitor,
1129 Clone_Snapshot *&snapshot);
1130
1131 /** Detach clone handle from snapshot
1132 @param[in] snapshot snapshot
1133 @param[in] hdl_type handle type */
1134 void detach_snapshot(Clone_Snapshot *snapshot, Clone_Handle_Type hdl_type);
1135
1136 /** Mark clone state to abort if no active clone. If force is set,
1137 abort all active clones and set state to abort.
1138 @param[in] force force active clones to abort
1139 @return true if global state is set to abort successfully */
1140 bool mark_abort(bool force);
1141
1142 /** Mark clone state to active if no other abort request */
1143 void mark_active();
1144
1145 /** Mark to indicate that new clone operations should wait. */
1146 void mark_wait();
1147
1148 /** Free the wait marker. */
1149 void mark_free();
1150
1151 #ifdef UNIV_DEBUG
1152 /** Debug wait while starting clone and waiting for free marker. */
1153 void debug_wait_clone_begin();
1154
1155 /** Close donor master task file if open and unpin. */
1156 void close_donor_master_file();
1157 #endif /* UNIV_DEBUG */
1158
1159 /** Wait for marker to get freed.
1160 @param[in,out] thd user session
1161 @return, error if timeout */
1162 int wait_for_free(THD *thd);
1163
1164 /** Begin restricted state during some critical ddl phase.
1165 @param[in] type ddl notification type
1166 @param[in] space tablespace ID for which notification is sent
1167 @param[in] no_wait return with error if needs to wait
1168 @param[in] check_intr check for interrupt during wait
1169 @param[out] blocked_state blocked state when state change is blocked
1170 @param[out] error mysql error code
1171 @return true iff clone needs to wait for state change. */
1172 bool begin_ddl_state(Clone_notify::Type type, space_id_t space, bool no_wait,
1173 bool check_intr, uint32_t &blocked_state, int &error);
1174
1175 /** End restricted state during some critical ddl phase.
1176 @param[in] type ddl notification type
1177 @param[in] space tablespace ID for which notification is sent
1178 @param[in] blocked_state blocked state when state change is blocked */
1179 void end_ddl_state(Clone_notify::Type type, space_id_t space,
1180 uint32_t blocked_state);
1181
1182 /** Get next unique ID
1183 @return unique ID */
1184 uint64_t get_next_id();
1185
1186 /** Get clone sys mutex
1187 @return clone system mutex */
1188 ib_mutex_t *get_mutex() { return (&m_clone_sys_mutex); }
1189
1190 /** Clone System state */
1191 static Clone_Sys_State s_clone_sys_state;
1192
1193 /** Number of active abort requests */
1194 static uint s_clone_abort_count;
1195
1196 /** Number of active wait requests */
1197 static uint s_clone_wait_count;
1198
1199 /** Function to check wait condition
1200 @param[in] is_alert print alert message
1201 @param[out] result true, if condition is satisfied
1202 @return error code */
1203 using Wait_Cond_Cbk_Func = std::function<int(bool is_alert, bool &result)>;
1204
1205 /** Wait till the condition is satisfied or timeout.
1206 @param[in] sleep_time sleep time in milliseconds
1207 @param[in] timeout total time to wait in seconds
1208 @param[in] alert_interval alert interval in seconds
1209 @param[in] func callback function for condition check
1210 @param[in] mutex release during sleep and re-acquire
1211 @param[out] is_timeout true if timeout
1212 @return error code returned by callback function. */
1213 static int wait(Clone_Msec sleep_time, Clone_Sec timeout,
1214 Clone_Sec alert_interval, Wait_Cond_Cbk_Func &&func,
1215 ib_mutex_t *mutex, bool &is_timeout) {
1216 int err = 0;
1217 bool wait = true;
1218 is_timeout = false;
1219
1220 int loop_count = 0;
1221 auto alert_count = static_cast<int>(alert_interval / sleep_time);
1222 auto total_count = static_cast<int>(timeout / sleep_time);
1223
1224 /* Call function once before waiting. */
1225 err = func(false, wait);
1226
1227 /* Start with 1 ms sleep and increase up to target sleep time. */
1228 Clone_Msec cur_sleep_time{1};
1229
1230 while (!is_timeout && wait && err == 0) {
1231 /* Release input mutex */
1232 if (mutex != nullptr) {
1233 ut_ad(mutex_own(mutex));
1234 mutex_exit(mutex);
1235 }
1236
1237 /* Limit sleep time to what is passed by caller. */
1238 if (cur_sleep_time > sleep_time) {
1239 cur_sleep_time = sleep_time;
1240 }
1241
1242 std::this_thread::sleep_for(cur_sleep_time);
1243
1244 if (cur_sleep_time < sleep_time) {
1245 /* Double sleep time in each iteration till we reach target. */
1246 cur_sleep_time *= 2;
1247 } else {
1248 /* Increment count once we have reached target sleep time. */
1249 ++loop_count;
1250 }
1251
1252 /* Acquire input mutex back */
1253 if (mutex != nullptr) {
1254 mutex_enter(mutex);
1255 }
1256
1257 /* We have not yet reached the target sleep time. */
1258 if (loop_count == 0) {
1259 err = func(false, wait);
1260 continue;
1261 }
1262
1263 auto alert = (alert_count > 0) ? (loop_count % alert_count == 0) : true;
1264
1265 err = func(alert, wait);
1266
1267 is_timeout = (loop_count > total_count);
1268 }
1269 return (err);
1270 }
1271
1272 /** Wait till the condition is satisfied or default timeout.
1273 @param[in] func callback function for condition check
1274 @param[in] mutex release during sleep and re-acquire
1275 @param[out] is_timeout true if timeout
1276 @return error code returned by callback function. */
1277 static int wait_default(Wait_Cond_Cbk_Func &&func, ib_mutex_t *mutex,
1278 bool &is_timeout) {
1279 return (wait(CLONE_DEF_SLEEP, Clone_Sec(CLONE_DEF_TIMEOUT),
1280 CLONE_DEF_ALERT_INTERVAL,
1281 std::forward<Wait_Cond_Cbk_Func>(func), mutex, is_timeout));
1282 }
1283
1284 /** Check if any active clone is running.
1285 @param[in] print_alert print alert message
1286 @return true, if concurrent clone in progress */
1287 bool check_active_clone(bool print_alert);
1288
1289 /** Check if any active clone is running.
1290 @return (true, handle) if concurrent clone in progress */
1291 std::tuple<bool, Clone_Handle *> check_active_clone();
1292
1293 /** @return GTID persistor */
1294 Clone_persist_gtid &get_gtid_persistor() { return (m_gtid_persister); }
1295
1296 /** Remember that all innodb spaces are initialized after last startup. */
1297 void set_space_initialized() { m_space_initialized.store(true); }
1298
1299 /** @return true if all innodb spaces are initialized. */
1300 bool is_space_initialized() const { return m_space_initialized.load(); }
1301
1302 private:
1303 /** Find free index to allocate new clone handle.
1304 @param[in] hdl_type clone handle type
1305 @param[out] free_index free index in array
1306 @return error code */
1307 int find_free_index(Clone_Handle_Type hdl_type, uint &free_index);
1308
1309 /** Handle restricted state during critical ddl phase.
1310 @param[in] type ddl notification type
1311 @param[in] space tablespace ID for which notification is sent
1312 @param[in] begin true, if beginning state
1313 false, if ending
1314 @return true iff clone needs to wait for state change. */
1315 bool handle_ddl_state(Clone_notify::Type type, space_id_t space, bool begin);
1316
1317 private:
1318 /** Array of clone handles */
1319 Clone_Handle *m_clone_arr[CLONE_ARR_SIZE];
1320
1321 /** Number of copy clones */
1322 uint m_num_clones;
1323
1324 /** Number of apply clones */
1325 uint m_num_apply_clones;
1326
1327 /** Array of clone snapshots */
1328 Clone_Snapshot *m_snapshot_arr[SNAPSHOT_ARR_SIZE];
1329
1330 /** Number of copy snapshots */
1331 uint m_num_snapshots;
1332
1333 /** Number of apply snapshots */
1334 uint m_num_apply_snapshots;
1335
1336 /** Clone system mutex */
1337 ib_mutex_t m_clone_sys_mutex;
1338
1339 /** Clone unique ID generator */
1340 uint64_t m_clone_id_generator;
1341
1342 /** If all innodb tablespaces are initialized. */
1343 std::atomic<bool> m_space_initialized;
1344
1345 /** GTID persister */
1346 Clone_persist_gtid m_gtid_persister;
1347 };
1348
1349 /** Clone system global */
1350 extern Clone_Sys *clone_sys;
1351
1352 #endif /* CLONE_CLONE_INCLUDE */