Debian Codegraph @

storage/innobase/include/clone0snapshot.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/clone0snapshot.h
29 Database Physical Snapshot
30
31 *******************************************************/
32
33 #ifndef CLONE_SNAPSHOT_INCLUDE
34 #define CLONE_SNAPSHOT_INCLUDE
35
36 #include "univ.i"
37
38 #include "arch0log.h"
39 #include "arch0page.h"
40 #include "clone0api.h"
41 #include "clone0desc.h"
42 #include "clone0monitor.h"
43 #include "fil0fil.h"
44 #include "sql/handler.h"
45
46 #include <map>
47 #include <vector>
48
49 struct Clone_file_ctx {
50 /** File state:
51 [CREATED] -------------> [DROPPING] --> [DROPPED] --> [DROPPED_HANDLED]
52 | ^
53 | |
54 ----> [RENAMING] -> [RENAMED]
55 | |
56 <------------
57 */
58 enum class State {
59 /* Invalid state. */
60 NONE,
61 /* File is being dropped. */
62 DROPPING,
63 /* File is being renamed. */
64 RENAMING,
65 /* Newly created file or pre-existing before clone. */
66 CREATED,
67 /* File is renamed during clone. */
68 RENAMED,
69 /* File is deleted during clone. */
70 DROPPED,
71 /* File is deleted and chunk information is handled. */
72 DROPPED_HANDLED
73 };
74
75 /** File extension to use with name. */
76 enum class Extension {
77 /* No extension. */
78 NONE,
79 /* Replace extension - clone file to be replaced during recovery. */
80 REPLACE,
81 /* DDL extension - temporary extension used during rename. */
82 DDL
83 };
84
85 /** Initialize file state.
86 @param[in] extn file name extension */
87 void init(Extension extn) {
88 m_state.store(State::CREATED);
89 m_extension = extn;
90
91 m_pin.store(0);
92 m_modified_ddl = false;
93 m_waiting = 0;
94
95 m_next_state = CLONE_SNAPSHOT_NONE;
96
97 m_meta.init();
98 }
99
100 /** Get file name with extension.
101 @param[out] name file name. */
102 void get_file_name(std::string &name) const;
103
104 /** Mark file added by DDL.
105 @param[in] next_state next snapshot state */
106 void set_ddl(Snapshot_State next_state) {
107 m_modified_ddl = true;
108 m_next_state = next_state;
109 }
110
111 /** @return true iff added or modified by ddl in previous state.
112 @param[in] state current snapshot state */
113 bool by_ddl(Snapshot_State state) const {
114 return m_modified_ddl && (state <= m_next_state);
115 }
116
117 /** Start waiting for DDL */
118 void begin_wait() { ++m_waiting; }
119
120 /** Finish waiting for DDL */
121 void end_wait() {
122 ut_a(m_waiting > 0);
123 --m_waiting;
124 }
125
126 /** @return true, iff there are waiting clone tasks. */
127 bool is_waiting() const { return (m_waiting > 0); }
128
129 /** Pin the file. */
130 void pin() { ++m_pin; }
131
132 /** Unpin the file. */
133 void unpin() {
134 ut_a(m_pin > 0);
135 --m_pin;
136 }
137
138 /** @return true, iff clone tasks are using the file. */
139 bool is_pinned() const { return (m_pin.load() > 0); }
140
141 /** @return true, iff DDL is modifying file. */
142 bool modifying() const {
143 State state = m_state.load();
144 return (state == State::RENAMING || state == State::DROPPING);
145 }
146
147 /** @return true, iff DDL is deleting file. */
148 bool deleting() const {
149 State state = m_state.load();
150 return (state == State::DROPPING);
151 }
152
153 /** @return true, iff file is already deleted. */
154 bool deleted() const {
155 State state = m_state.load();
156 return (state == State::DROPPED || state == State::DROPPED_HANDLED);
157 }
158
159 /** @return true, iff file is already renamed. */
160 bool renamed() const {
161 State state = m_state.load();
162 return (state == State::RENAMED);
163 }
164
165 /** @return file metadata. */
166 Clone_File_Meta *get_file_meta() { return &m_meta; }
167
168 /** @return file metadata for read. */
169 const Clone_File_Meta *get_file_meta_read() const { return &m_meta; }
170
171 /** File metadata state. Modified by DDL commands. Protected by snapshot
172 mutex. Atomic operation helps clone to skip mutex when no ddl. */
173 std::atomic<State> m_state;
174
175 /** File name extension. */
176 Extension m_extension;
177
178 private:
179 /** Pin count incremented and decremented by clone tasks to synchronize with
180 concurrent DDL. Protected by snapshot mutex. */
181 std::atomic<uint32_t> m_pin;
182
183 /** Waiting count incremented and decremented by clone tasks while waiting
184 DDL file operation in progress. Protected by snapshot mutex. */
185 uint32_t m_waiting;
186
187 /** true, if file created or modified after clone is started. */
188 bool m_modified_ddl{false};
189
190 /** Next state when ddl last modified file. */
191 Snapshot_State m_next_state{CLONE_SNAPSHOT_DONE};
192
193 /** File metadata. */
194 Clone_File_Meta m_meta;
195 };
196
197 /** Vector type for storing clone files */
198 using Clone_File_Vec = std::vector<Clone_file_ctx *>;
199
200 /** Map type for mapping space ID to clone file index */
201 using Clone_File_Map = std::map<space_id_t, uint>;
202
203 /** Page identified by space and page number */
204 struct Clone_Page {
205 /** Tablespace ID */
206 uint32_t m_space_id;
207
208 /** Page number within tablespace */
209 uint32_t m_page_no;
210 };
211
212 /** Comparator for storing sorted page ID. */
213 struct Less_Clone_Page {
214 /** Less than operator for page ID.
215 @param[in] page1 first page
216 @param[in] page2 second page
217 @return true, if page1 is less than page2 */
218 inline bool operator()(const Clone_Page &page1,
219 const Clone_Page &page2) const {
220 if (page1.m_space_id < page2.m_space_id) {
221 return (true);
222 }
223
224 if (page1.m_space_id == page2.m_space_id &&
225 page1.m_page_no < page2.m_page_no) {
226 return (true);
227 }
228 return (false);
229 }
230 };
231
232 /** Vector type for storing clone page IDs */
233 using Clone_Page_Vec = std::vector<Clone_Page>;
234
235 /** Set for storing unique page IDs. */
236 using Clone_Page_Set = std::set<Clone_Page, Less_Clone_Page>;
237
238 /** Clone handle type */
239 enum Clone_Handle_Type {
240 /** Clone Handle for COPY */
241 CLONE_HDL_COPY = 1,
242
243 /** Clone Handle for APPLY */
244 CLONE_HDL_APPLY
245 };
246
247 /** Default chunk size in power of 2 in unit of pages.
248 Chunks are reserved by each thread for multi-threaded clone. For 16k page
249 size, chunk size is 64M. */
250 const uint SNAPSHOT_DEF_CHUNK_SIZE_POW2 = 12;
251
252 /** Default block size in power of 2 in unit of pages.
253 Data transfer callback is invoked once for each block. This is also
254 the maximum size of data that would be re-send if clone is stopped
255 and resumed. For 16k page size, block size is 1M. */
256 const uint SNAPSHOT_DEF_BLOCK_SIZE_POW2 = 6;
257
258 /** Maximum block size in power of 2 in unit of pages.
259 For 16k page size, maximum block size is 64M. */
260 const uint SNAPSHOT_MAX_BLOCK_SIZE_POW2 = 12;
261
262 /** Dynamic database snapshot: Holds metadata and handle to data */
263 class Clone_Snapshot {
264 public:
265 /** RAII style guard for begin & end of snapshot state transition. */
266 class State_transit {
267 public:
268 /** Constructor to begin state transition.
269 @param[in,out] snapshot Clone Snapshot
270 @param[in] new_state State to transit */
271 explicit State_transit(Clone_Snapshot *snapshot, Snapshot_State new_state);
272
273 /** Destructor to end state transition. */
274 ~State_transit();
275
276 /** @return error code */
277 int get_error() const { return m_error; }
278
279 /** Disable copy construction */
280 State_transit(State_transit const &) = delete;
281
282 /** Disable assignment */
283 State_transit &operator=(State_transit const &) = delete;
284
285 private:
286 /** Clone Snapshot */
287 Clone_Snapshot *m_snapshot;
288
289 /** Saved error while beginning transition. */
290 int m_error;
291 };
292
293 /** Construct snapshot
294 @param[in] hdl_type copy, apply
295 @param[in] clone_type clone type
296 @param[in] arr_idx index in global array
297 @param[in] snap_id unique snapshot ID */
298 Clone_Snapshot(Clone_Handle_Type hdl_type, Ha_clone_type clone_type,
299 uint arr_idx, uint64_t snap_id);
300
301 /** Release contexts and free heap */
302 ~Clone_Snapshot();
303
304 /** DDL notification before the operation.
305 @param[in] type type of DDL notification
306 @param[in] space space ID for the ddl operation
307 @param[in] no_wait return with error if needs to wait
308 @param[in] check_intr check for interrupt during wait
309 @param[out] error mysql error code
310 @return true iff clone state change is blocked. */
311 bool begin_ddl_state(Clone_notify::Type type, space_id_t space, bool no_wait,
312 bool check_intr, int &error);
313
314 /** DDL notification after the operation.
315 @param[in] type type of DDL notification
316 @param[in] space space ID for the ddl operation */
317 void end_ddl_state(Clone_notify::Type type, space_id_t space);
318
319 /** Wait for concurrent DDL file operation and pin file.
320 @param[in,out] file_ctx file context
321 @param[out] handle_delete if caller needs to handle deleted state
322 @return mysql error code. */
323 int pin_file(Clone_file_ctx *file_ctx, bool &handle_delete);
324
325 /** Unpin a file.
326 @param[in,out] file_ctx file context */
327 void unpin_file(Clone_file_ctx *file_ctx) { file_ctx->unpin(); }
328
329 /** Check if DDL needs to block clone operation.
330 @param[in] file_ctx file context
331 @return true iff clone operation needs to be blocked. */
332 bool blocks_clone(const Clone_file_ctx *file_ctx);
333
334 /** @return estimated bytes on disk */
335 uint64_t get_disk_estimate() const { return (m_data_bytes_disk); }
336
337 /** Get unique snapshot identifier
338 @return snapshot ID */
339 uint64_t get_id() { return (m_snapshot_id); }
340
341 /** Get snapshot index in global array
342 @return array index */
343 uint get_index() { return (m_snapshot_arr_idx); }
344
345 /** Get performance schema accounting object used to monitor stage
346 progress.
347 @return PFS stage object */
348 Clone_Monitor &get_clone_monitor() { return (m_monitor); }
349
350 /** Get snapshot heap used for allocation during clone.
351 @return heap */
352 mem_heap_t *lock_heap() {
353 mutex_enter(&m_snapshot_mutex);
354 return (m_snapshot_heap);
355 }
356
357 /* Release snapshot heap */
358 void release_heap(mem_heap_t *&heap) {
359 heap = nullptr;
360 mutex_exit(&m_snapshot_mutex);
361 }
362
363 /** Get snapshot state
364 @return state */
365 Snapshot_State get_state() { return (m_snapshot_state); }
366
367 /** Get the redo file size for the snapshot
368 @return redo file size */
369 uint64_t get_redo_file_size() { return (m_redo_file_size); }
370
371 /** Get total number of chunks for current state
372 @return number of data chunks */
373 uint get_num_chunks() { return (m_num_current_chunks); }
374
375 /** Get maximum file length seen till now
376 @return file name length */
377 size_t get_max_file_name_length() { return (m_max_file_name_len); }
378
379 /** Get maximum buffer size required for clone
380 @return maximum dynamic buffer */
381 uint get_dyn_buffer_length() {
382 uint ret_len = 0;
383
384 if (is_copy() && m_snapshot_type != HA_CLONE_BLOCKING) {
385 ret_len = static_cast<uint>(2 * UNIV_PAGE_SIZE);
386 }
387
388 return (ret_len);
389 }
390
391 using File_Cbk_Func = std::function<int(Clone_file_ctx *)>;
392
393 /** Iterate through all files in current state
394 @param[in] func callback function
395 @return error code */
396 int iterate_files(File_Cbk_Func &&func);
397
398 /** Iterate through all data files
399 @param[in] func callback function
400 @return error code */
401 int iterate_data_files(File_Cbk_Func &&func);
402
403 /** Iterate through all redo files
404 @param[in] func callback function
405 @return error code */
406 int iterate_redo_files(File_Cbk_Func &&func);
407
408 /** Fill state descriptor from snapshot
409 @param[in] do_estimate estimate data bytes to transfer
410 @param[out] state_desc snapshot state descriptor */
411 void get_state_info(bool do_estimate, Clone_Desc_State *state_desc);
412
413 /** Set state information during apply
414 @param[in] state_desc snapshot state descriptor */
415 void set_state_info(Clone_Desc_State *state_desc);
416
417 /** Get next state based on snapshot type
418 @return next state */
419 Snapshot_State get_next_state();
420
421 /** Try to attach to snapshot
422 @param[in] hdl_type copy, apply
423 @param[in] pfs_monitor enable PFS monitoring
424 @return true if successfully attached */
425 bool attach(Clone_Handle_Type hdl_type, bool pfs_monitor);
426
427 /** Detach from snapshot. */
428 void detach();
429
430 /** Set current snapshot aborted state. Used in error cases before exiting
431 clone to make sure any DDL notifier exits waiting. */
432 void set_abort();
433
434 /** @return true, iff clone has aborted. */
435 bool is_aborted() const;
436
437 /** Start transition to new state
438 @param[in] state_desc descriptor for next state
439 @param[in] new_state state to move for apply
440 @param[in] temp_buffer buffer used for collecting page IDs
441 @param[in] temp_buffer_len buffer length
442 @param[in] cbk alter callback for long wait
443 @return error code */
444 int change_state(Clone_Desc_State *state_desc, Snapshot_State new_state,
445 byte *temp_buffer, uint temp_buffer_len,
446 Clone_Alert_Func cbk);
447
448 /** Add file metadata entry at destination
449 @param[in] file_meta file metadata from donor
450 @param[in] data_dir destination data directory
451 @param[in] desc_create create if doesn't exist
452 @param[out] desc_exists descriptor already exists
453 @param[out] file_ctx if there, set to current file context
454 @return error code */
455 int get_file_from_desc(const Clone_File_Meta *file_meta, const char *data_dir,
456 bool desc_create, bool &desc_exists,
457 Clone_file_ctx *&file_ctx);
458
459 /** Rename an existing file descriptor.
460 @param[in] file_meta renamed file metadata from donor
461 @param[in] data_dir destination data directory
462 @param[out] file_ctx if there, set to current file context
463 @return error code */
464 int rename_desc(const Clone_File_Meta *file_meta, const char *data_dir,
465 Clone_file_ctx *&file_ctx);
466
467 /** Fix files renamed with ddl extension. The file name is checked against
468 existing file and added to appropriate status file.
469 @param[in] data_dir destination data directory
470 @param[in,out] file_ctx Set to correct extension
471 @return error code */
472 int fix_ddl_extension(const char *data_dir, Clone_file_ctx *file_ctx);
473
474 /** Add file descriptor to file list
475 @param[in,out] file_ctx current file context
476 @param[in] ddl_create added by DDL concurrently
477 @return true, if it is the last file. */
478 bool add_file_from_desc(Clone_file_ctx *&file_ctx, bool ddl_create);
479
480 /** Extract file information from node and add to snapshot
481 @param[in] node file node
482 @param[in] by_ddl node is added concurrently by DDL
483 @return error code */
484 dberr_t add_node(fil_node_t *node, bool by_ddl);
485
486 /** Add page ID to to the set of pages in snapshot
487 @param[in] space_id page tablespace
488 @param[in] page_num page number within tablespace
489 @return error code */
490 int add_page(uint32_t space_id, uint32_t page_num);
491
492 /** Add redo file to snapshot
493 @param[in] file_name file name
494 @param[in] file_size file size in bytes
495 @param[in] file_offset start offset
496 @return error code. */
497 int add_redo_file(char *file_name, uint64_t file_size, uint64_t file_offset);
498
499 /** Get file metadata by index for current state
500 @param[in] index file index
501 @return file metadata entry */
502 Clone_File_Meta *get_file_by_index(uint index);
503
504 /** Get clone file context by index for current state
505 @param[in] index file index
506 @return file context */
507 Clone_file_ctx *get_file_ctx_by_index(uint index);
508
509 /** Get clone file context by chunk and block number.
510 @param[in] chunk_num chunk number
511 @param[in] block_num block number
512 @param[in] hint_index hint file index number to start search.
513 @return file context */
514 Clone_file_ctx *get_file_ctx(uint32_t chunk_num, uint32_t block_num,
515 uint32_t hint_index);
516
517 /** Get next block of data to transfer
518 @param[in] chunk_num current chunk
519 @param[in,out] block_num current/next block
520 @param[in,out] file_ctx current/next block file context
521 @param[out] data_offset block offset in file
522 @param[out] data_buf data buffer or NULL if transfer from file
523 @param[out] data_size size of data in bytes
524 @param[out] file_size updated file size if extended
525 @return error code */
526 int get_next_block(uint chunk_num, uint &block_num,
527 const Clone_file_ctx *&file_ctx, uint64_t &data_offset,
528 byte *&data_buf, uint32_t &data_size, uint64_t &file_size);
529
530 /** Update snapshot block size based on caller's buffer size
531 @param[in] buff_size buffer size for clone transfer */
532 void update_block_size(uint buff_size);
533
534 /** @return chunk size in bytes. */
535 inline uint32_t get_chunk_size() const {
536 return chunk_size() * UNIV_PAGE_SIZE;
537 }
538
539 /** @return number of blocks per chunk for different states. */
540 uint32_t get_blocks_per_chunk() const;
541
542 /** Check if copy snapshot
543 @return true if snapshot is for copy */
544 bool is_copy() const { return (m_snapshot_handle_type == CLONE_HDL_COPY); }
545
546 /** Update file size when file is extended during page copy
547 @param[in] file_index current file index
548 @param[in] file_size new file size */
549 void update_file_size(uint32_t file_index, uint64_t file_size);
550
551 /** Encrypt tablespace key in header page with master key.
552 @param[in] page_size page size descriptor
553 @param[in,out] page_data page data to update
554 @return true, if successful. */
555 bool encrypt_key_in_header(const page_size_t &page_size, byte *page_data);
556
557 /** Encrypt tablespace key in header page with master key.
558 @param[in,out] log_header page data to update
559 @param[in] header_len length of log header
560 @return true, if successful. */
561 bool encrypt_key_in_log_header(byte *log_header, uint32_t header_len);
562
563 /** Decrypt tablespace key in header page with master key.
564 @param[in] file_meta clone file metadata
565 @param[in] page_size page size descriptor
566 @param[in,out] page_data page data to update */
567 void decrypt_key_in_header(const Clone_File_Meta *file_meta,
568 const page_size_t &page_size, byte *&page_data);
569
570 /** @return maximum blocks to transfer with file pinned. */
571 uint32_t get_max_blocks_pin() const;
572
573 /** Skip all blocks belonging to currently deleted file context.
574 @param[in] chunk_num current chunk
575 @param[in,out] block_num current, next block */
576 void skip_deleted_blocks(uint32_t chunk_num, uint32_t &block_num);
577
578 private:
579 /** Allow DDL file operation after 64 pages. */
580 const static uint32_t S_MAX_PAGES_PIN = 64;
581
582 /** Allow DDL file operation after every block (1M data by default) */
583 const static uint32_t S_MAX_BLOCKS_PIN = 1;
584
585 /** File name allocation size base. */
586 const static size_t S_FILE_NAME_BASE_LEN = 256;
587
588 /** Various wait types related to snapshot state. */
589 enum class Wait_type {
590 /* DDL- limited wait if clone is waiting for another DDL. */
591 STATE_TRANSIT_WAIT,
592 /* DDL- Wait till snapshot state transition is over. */
593 STATE_TRANSIT,
594 /* DDL- Wait till PAGE COPY state is over. */
595 STATE_END_PAGE_COPY,
596 /* Clone - Wait till there are no blockers for state transition. */
597 STATE_BLOCKER,
598 /*DDL - Wait till the waiting clone threads are active. This are
599 clone threads from last DDL and useful to prevent starvation. */
600 DATA_FILE_WAIT,
601 /* DDL - Wait till all threads have closed active data files. */
602 DATA_FILE_CLOSE,
603 /* Clone - Wait till DDL file operation is complete. */
604 DDL_FILE_OPERATION
605 };
606
607 #ifdef UNIV_DEBUG
608 /** Debug sync Wait during state transition. */
609 void debug_wait_state_transit();
610 #endif /* UNIV_DEBUG */
611
612 /** Update deleted state of a file if not yet done.
613 @param[in,out] file_ctx file context
614 @return true, if updated state */
615 bool update_deleted_state(Clone_file_ctx *file_ctx);
616
617 /** Get clone data file context by chunk number.
618 @param[in] chunk_num chunk number
619 @param[in] hint_index hint file index number to start search.
620 @return file context */
621 Clone_file_ctx *get_data_file_ctx(uint32_t chunk_num, uint32_t hint_index);
622
623 /** Get clone page file context by chunk number and block number.
624 @param[in] chunk_num chunk number
625 @param[in] block_num block number
626 @return file context */
627 Clone_file_ctx *get_page_file_ctx(uint32_t chunk_num, uint32_t block_num);
628
629 /** Get clone redo file context by chunk number.
630 @param[in] chunk_num chunk number
631 @param[in] hint_index hint file index number to start search.
632 @return file context */
633 Clone_file_ctx *get_redo_file_ctx(uint32_t chunk_num, uint32_t hint_index);
634
635 /** Get wait information string based on wait type.
636 @param[in] wait_type wait type
637 @return wait information string. */
638 const char *wait_string(Wait_type wait_type) const;
639
640 /** Wait for various operations based on type.
641 @param[in] type wait type
642 @param[in] ctx file context when relevant
643 @param[in] no_wait return with error if needs to wait
644 @param[in] check_intr check for interrupt during wait
645 @return mysql error code. */
646 int wait(Wait_type type, const Clone_file_ctx *ctx, bool no_wait,
647 bool check_intr);
648
649 /** During wait get relevant message string for logging.
650 @param[in] wait_type wait type
651 @param[out] info notification to log while waiting
652 @param[out] error error message to log on timeout */
653 void get_wait_mesg(Wait_type wait_type, std::string &info,
654 std::string &error);
655
656 /** Block clone state transition. Clone must wait.
657 @param[in] type type of DDL notification
658 @param[in] space space ID for the ddl operation
659 @param[in] no_wait return with error if needs to wait
660 @param[in] check_intr check for interrupt during wait`
661 @param[out] error mysql error code
662 @return true iff clone state change is blocked. */
663 bool block_state_change(Clone_notify::Type type, space_id_t space,
664 bool no_wait, bool check_intr, int &error);
665
666 /** Unblock clone state transition. */
667 void unblock_state_change();
668
669 /** Get next file state while being modified by ddl.
670 @param[in] type ddl notification type
671 @param[in] begin true, if DDL begin notification
672 false, if DDL end notification
673 @return target file state. */
674 Clone_file_ctx::State get_target_file_state(Clone_notify::Type type,
675 bool begin);
676
677 /** Handle files for DDL begin notification.
678 @param[in] type type of DDL notification
679 @param[in] space space ID for the ddl operation
680 @param[in] no_wait return with error if needs to wait
681 @param[in] check_intr check for interrupt during wait
682 @return mysql error code */
683 int begin_ddl_file(Clone_notify::Type type, space_id_t space, bool no_wait,
684 bool check_intr);
685
686 /** Handle files for DDL end notification.
687 @param[in] type type of DDL notification
688 @param[in] space space ID for the ddl operation */
689 void end_ddl_file(Clone_notify::Type type, space_id_t space);
690
691 /** Synchronize snapshot with binary log and GTID.
692 @param[in] cbk alert callback for long wait
693 @return error code. */
694 int synchronize_binlog_gtid(Clone_Alert_Func cbk);
695
696 /** Make sure that the trx sys page binary log position correctly reflects
697 all transactions committed to innodb. It updates binary log position
698 in transaction sys page, if required. The caller must ensure that any new
699 transaction is committed in order of binary log.
700 @return error code. */
701 int update_binlog_position();
702
703 /** Wait for already prepared binlog transactions to end.
704 @return error code. */
705 int wait_for_binlog_prepared_trx();
706
707 /** Wait for a transaction to end.
708 @param[in] thd current THD
709 @param[in] trx_id transaction to wait for
710 @return error code. */
711 int wait_trx_end(THD *thd, trx_id_t trx_id);
712
713 /** Begin state transition before waiting for DDL. */
714 void begin_transit_ddl_wait() {
715 mutex_own(&m_snapshot_mutex);
716 /* Update number of clones to transit to new state. Set this prior to
717 waiting for DDLs blocking state transfer. This would help a new DDL to
718 find if clone is blocked by other DDL before state transition. */
719 m_num_clones_transit = m_num_clones;
720 }
721
722 /** Begin state transition.
723 @param[in] new_state state to transit to */
724 void begin_transit(Snapshot_State new_state) {
725 mutex_own(&m_snapshot_mutex);
726 m_snapshot_next_state = new_state;
727 /* Move to next state. This is ok as the snapshot
728 mutex is not released till transition is ended, This
729 could change later when we ideally should release
730 the snapshot mutex during transition. */
731 m_snapshot_state = m_snapshot_next_state;
732 }
733
734 /** End state transition. */
735 void end_transit() {
736 mutex_own(&m_snapshot_mutex);
737 m_num_clones_transit = 0;
738 m_snapshot_next_state = CLONE_SNAPSHOT_NONE;
739 }
740
741 /** Check if state transition is in progress
742 @return true during state transition */
743 bool in_transit_state() const {
744 mutex_own(&m_snapshot_mutex);
745 return (m_snapshot_next_state != CLONE_SNAPSHOT_NONE);
746 }
747
748 /** @return true, if waiting before starting transition. Generally the
749 case when some DDL blocks state transition. */
750 bool in_transit_wait() const {
751 mutex_own(&m_snapshot_mutex);
752 return (!in_transit_state() && m_num_clones_transit != 0);
753 }
754
755 /** Start redo archiving.
756 @return error code */
757 int init_redo_archiving();
758
759 /** Initialize snapshot state for file copy
760 @param[in] new_state state to move for apply
761 @return error code */
762 int init_file_copy(Snapshot_State new_state);
763
764 /** Initialize disk byte estimate. */
765 void init_disk_estimate() {
766 /* Initial size is set to the redo file size on disk. */
767 IB_mutex_guard latch{&(log_sys->limits_mutex), UT_LOCATION_HERE};
768 m_data_bytes_disk = log_sys->m_capacity.current_physical_capacity();
769 }
770
771 /** Initialize snapshot state for page copy
772 @param[in] new_state state to move for apply
773 @param[in] page_buffer temporary buffer to copy page IDs
774 @param[in] page_buffer_len buffer length
775 @return error code */
776 int init_page_copy(Snapshot_State new_state, byte *page_buffer,
777 uint page_buffer_len);
778
779 /** Initialize snapshot state for redo copy
780 @param[in] new_state state to move for apply
781 @param[in] cbk alert callback for long wait
782 @return error code */
783 int init_redo_copy(Snapshot_State new_state, Clone_Alert_Func cbk);
784
785 /** Initialize state while applying cloned data
786 @param[in] state_desc snapshot state descriptor
787 @return error code */
788 int init_apply_state(Clone_Desc_State *state_desc);
789
790 /** Extend and flush files after copying data
791 @param[in] flush_redo if true flush redo, otherwise data
792 @return error code */
793 int extend_and_flush_files(bool flush_redo);
794
795 /** Create file descriptor and add to current file list
796 @param[in] data_dir destination data directory
797 @param[in] file_meta file metadata from donor
798 @param[in] is_ddl if ddl temporary file
799 @param[out] file_ctx file context
800 @return error code */
801 int create_desc(const char *data_dir, const Clone_File_Meta *file_meta,
802 bool is_ddl, Clone_file_ctx *&file_ctx);
803
804 /** Get file context for current chunk
805 @param[in] file_vector clone file vector
806 @param[in] chunk_num current chunk number
807 @param[in] start_index index for starting the search
808 @return file context */
809 Clone_file_ctx *get_file(Clone_File_Vec &file_vector, uint32_t chunk_num,
810 uint32_t start_index);
811
812 /** Get next page from buffer pool
813 @param[in] chunk_num current chunk
814 @param[in,out] block_num current, next block
815 @param[in,out] file_ctx current, next block file context
816 @param[out] data_offset offset in file
817 @param[out] data_buf page data
818 @param[out] data_size page data size
819 @param[out] file_size updated file size if extended
820 @return error code */
821 int get_next_page(uint chunk_num, uint &block_num,
822 const Clone_file_ctx *&file_ctx, uint64_t &data_offset,
823 byte *&data_buf, uint32_t &data_size, uint64_t &file_size);
824
825 /** Get page from buffer pool and make ready for write
826 @param[in] page_id page ID chunk
827 @param[in] page_size page size descriptor
828 @param[in] file_ctx clone file context
829 @param[out] page_data data page
830 @param[out] data_size page size in bytes
831 @return error code */
832 int get_page_for_write(const page_id_t &page_id, const page_size_t &page_size,
833 const Clone_file_ctx *file_ctx, byte *&page_data,
834 uint &data_size);
835
836 /* Make page ready for flush by updating LSN anc checksum
837 @param[in] page_size page size descriptor
838 @param[in] page_lsn LSN to update the page with
839 @param[in,out] page_data data page */
840 void page_update_for_flush(const page_size_t &page_size, lsn_t page_lsn,
841 byte *&page_data);
842
843 /** Build file metadata entry
844 @param[in] file_name name of the file
845 @param[in] file_size file size in bytes
846 @param[in] file_offset start offset
847 @param[in] num_chunks total number of chunks in the file
848 @return file context */
849 Clone_file_ctx *build_file(const char *file_name, uint64_t file_size,
850 uint64_t file_offset, uint &num_chunks);
851
852 /** Allocate and set clone file name.
853 @param[in,out] file_meta file metadata
854 @param[in] file_name file name
855 @return true iff successful. */
856 bool build_file_name(Clone_File_Meta *file_meta, const char *file_name);
857
858 /** Add buffer pool dump file to the file list
859 @return error code */
860 int add_buf_pool_file();
861
862 /** Add file to snapshot
863 @param[in] name file name
864 @param[in] size_bytes file size in bytes
865 @param[in] alloc_bytes allocation size on disk for sparse file
866 @param[in] node file node
867 @param[in] by_ddl node is added concurrently by DDL
868 @return error code. */
869 int add_file(const char *name, uint64_t size_bytes, uint64_t alloc_bytes,
870 fil_node_t *node, bool by_ddl);
871
872 /** Check if file context has been changed by ddl.
873 @param[in] node tablespace file node
874 @param[out] file_ctx file context if exists
875 @return true iff file is created or modified by DDL. */
876 bool file_ctx_changed(const fil_node_t *node, Clone_file_ctx *&file_ctx);
877
878 /** Get chunk size
879 @return chunk size in pages */
880 inline uint32_t chunk_size() const {
881 auto size = static_cast<uint32_t>(ut_2_exp(m_chunk_size_pow2));
882 return size;
883 }
884
885 /** Get block size for file copy
886 @return block size in pages */
887 uint32_t block_size() {
888 ut_a(m_block_size_pow2 <= SNAPSHOT_MAX_BLOCK_SIZE_POW2);
889 auto size = static_cast<uint32_t>(ut_2_exp(m_block_size_pow2));
890
891 return size;
892 }
893
894 /** Get number of blocks per chunk for file copy
895 @return blocks per chunk */
896 inline uint32_t blocks_per_chunk() const {
897 ut_a(m_block_size_pow2 <= m_chunk_size_pow2);
898 return (1 << (m_chunk_size_pow2 - m_block_size_pow2));
899 }
900
901 /** Update system file name from configuration.
902 @param[in] replace if replacing current data directory
903 @param[in] file_meta file descriptor
904 @param[in,out] file_name file name to update
905 @return error code */
906 int update_sys_file_name(bool replace, const Clone_File_Meta *file_meta,
907 std::string &file_name);
908
909 /** Build file name along with path for cloned data files.
910 @param[in] data_dir clone data directory
911 @param[in] file_desc file descriptor
912 @param[out] file_path built file path if returned 0
913 @return error code (0 on success) */
914 int build_file_path(const char *data_dir, const Clone_File_Meta *file_desc,
915 std::string &file_path);
916
917 /** Build file context from file path.
918 @param[in] extn file extension type
919 @param[in] file_meta file descriptor
920 @param[in] file_path data file along with path
921 @param[out] file_ctx created file context
922 @return error code */
923 int build_file_ctx(Clone_file_ctx::Extension extn,
924 const Clone_File_Meta *file_meta,
925 const std::string &file_path, Clone_file_ctx *&file_ctx);
926
927 /** Check for existing file and if clone extension is needed. This function
928 has the side effect to add undo file indexes.
929 @param[in] replace if data directory is replaced
930 @param[in] undo_file if undo tablespace file
931 @param[in] redo_file if redo file
932 @param[in] data_file_index index of file
933 @param[in] data_file data file name
934 @param[out] extn file extension needs to be used
935 @return error code */
936 int handle_existing_file(bool replace, bool undo_file, bool redo_file,
937 uint32_t data_file_index,
938 const std::string &data_file,
939 Clone_file_ctx::Extension &extn);
940
941 /** @return number of data files to transfer. */
942 inline size_t num_data_files() const { return m_data_file_vector.size(); }
943
944 /** @return number of redo files to transfer. */
945 inline size_t num_redo_files() const { return m_redo_file_vector.size(); }
946
947 private:
948 /** @name Snapshot type and ID */
949
950 /** Snapshot handle type */
951 Clone_Handle_Type m_snapshot_handle_type;
952
953 /** Clone type */
954 Ha_clone_type m_snapshot_type;
955
956 /** Unique snapshot ID */
957 uint64_t m_snapshot_id;
958
959 /** Index in global snapshot array */
960 uint m_snapshot_arr_idx;
961
962 /** @name Snapshot State */
963
964 /** Mutex to handle access by concurrent clones */
965 mutable ib_mutex_t m_snapshot_mutex;
966
967 /** Number of blockers for state change. Usually DDLs for short duration. */
968 uint32_t m_num_blockers;
969
970 /** Set to true only if clone is aborted after error. */
971 bool m_aborted;
972
973 /** Number of clones attached to this snapshot */
974 uint m_num_clones;
975
976 /** Number of clones in in state transition */
977 uint m_num_clones_transit;
978
979 /** Current state */
980 Snapshot_State m_snapshot_state;
981
982 /** Next state to move to. Set only during state transfer. */
983 Snapshot_State m_snapshot_next_state;
984
985 /** @name Snapshot data block */
986
987 /** Memory allocation heap */
988 mem_heap_t *m_snapshot_heap;
989
990 /** Chunk size in power of 2 */
991 uint m_chunk_size_pow2;
992
993 /** Block size in power of 2 */
994 uint m_block_size_pow2;
995
996 /** Number of chunks in current state */
997 uint m_num_current_chunks;
998
999 /** Maximum file name length observed till now. */
1000 size_t m_max_file_name_len;
1001
1002 /** @name Snapshot file data */
1003
1004 /** All data files for transfer */
1005 Clone_File_Vec m_data_file_vector;
1006
1007 /** Map space ID to file vector index */
1008 Clone_File_Map m_data_file_map;
1009
1010 /** Total number of data chunks */
1011 uint m_num_data_chunks;
1012
1013 /** Number of bytes on disk. */
1014 uint64_t m_data_bytes_disk;
1015
1016 /** Index into m_data_file_vector for all undo files. */
1017 std::vector<int> m_undo_file_indexes;
1018
1019 /** @name Snapshot page data */
1020
1021 /** Page archiver client */
1022 Page_Arch_Client_Ctx m_page_ctx;
1023
1024 /** Set of unique page IDs */
1025 Clone_Page_Set m_page_set;
1026
1027 /** Sorted page IDs to transfer */
1028 Clone_Page_Vec m_page_vector;
1029
1030 /** Number of pages to transfer */
1031 uint m_num_pages;
1032
1033 /** Number of duplicate pages found */
1034 uint m_num_duplicate_pages;
1035
1036 /** @name Snapshot redo data */
1037
1038 /** redo log archiver client */
1039 Log_Arch_Client_Ctx m_redo_ctx;
1040
1041 /** All archived redo files to transfer */
1042 Clone_File_Vec m_redo_file_vector;
1043
1044 /** Start offset in first redo file */
1045 uint64_t m_redo_start_offset;
1046
1047 /** Redo header block */
1048 byte *m_redo_header;
1049
1050 /** Redo header size */
1051 uint m_redo_header_size;
1052
1053 /** Redo trailer block */
1054 byte *m_redo_trailer;
1055
1056 /** Redo trailer size */
1057 uint m_redo_trailer_size;
1058
1059 /** Redo trailer block offset */
1060 uint64_t m_redo_trailer_offset;
1061
1062 /** Archived redo file size */
1063 uint64_t m_redo_file_size;
1064
1065 /** Total number of redo data chunks */
1066 uint m_num_redo_chunks;
1067
1068 /** Enable PFS monitoring */
1069 bool m_enable_pfs;
1070
1071 /** Performance Schema accounting object to monitor stage progress */
1072 Clone_Monitor m_monitor;
1073 };
1074
1075 #endif /* CLONE_SNAPSHOT_INCLUDE */