Debian Codegraph @

storage/innobase/include/arch0arch.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/arch0arch.h
29 Common interface for redo log and dirty page archiver system
30
31 *******************************************************/
32
33 #ifndef ARCH_ARCH_INCLUDE
34 #define ARCH_ARCH_INCLUDE
35
36 #include <mysql/components/services/page_track_service.h>
37 #include <list>
38 #include "buf0buf.h" /* buf_page_t */
39 #include "ut0mem.h"
40 #include "ut0mutex.h"
41
42 /** @name Archive file name prefix and constant length parameters. */
43 /** @{ */
44 /** Archive directory prefix */
45 const char ARCH_DIR[] = OS_FILE_PREFIX "ib_archive";
46
47 /** Archive Log group directory prefix */
48 const char ARCH_LOG_DIR[] = "log_group_";
49
50 /** Archive Page group directory prefix */
51 const char ARCH_PAGE_DIR[] = "page_group_";
52
53 /** Archive log file prefix */
54 const char ARCH_LOG_FILE[] = "ib_log_";
55
56 /** Archive page file prefix */
57 const char ARCH_PAGE_FILE[] = "ib_page_";
58
59 /** @} */
60
61 /** File name for the durable file which indicates whether a group was made
62 durable or not. Required to differentiate durable group from group left over by
63 crash during clone operation. */
64 constexpr char ARCH_PAGE_GROUP_DURABLE_FILE_NAME[] = "durable";
65
66 /** Byte length for printing LSN.
67 Each archive group name is appended with start LSN */
68 const uint MAX_LSN_DECIMAL_DIGIT = 32;
69
70 /** Max string length for archive log file name */
71 const uint MAX_ARCH_LOG_FILE_NAME_LEN =
72 sizeof(ARCH_DIR) + 1 + sizeof(ARCH_LOG_DIR) + MAX_LSN_DECIMAL_DIGIT + 1 +
73 sizeof(ARCH_LOG_FILE) + MAX_LSN_DECIMAL_DIGIT + 1;
74
75 /** Max string length for archive page file name */
76 const uint MAX_ARCH_PAGE_FILE_NAME_LEN =
77 sizeof(ARCH_DIR) + 1 + sizeof(ARCH_PAGE_DIR) + MAX_LSN_DECIMAL_DIGIT + 1 +
78 sizeof(ARCH_PAGE_FILE) + MAX_LSN_DECIMAL_DIGIT + 1;
79
80 /** Max string length for archive group directory name */
81 const uint MAX_ARCH_DIR_NAME_LEN =
82 sizeof(ARCH_DIR) + 1 + sizeof(ARCH_PAGE_DIR) + MAX_LSN_DECIMAL_DIGIT + 1;
83
84 /** Log archiver background thread */
85 void log_archiver_thread();
86
87 /** Archiver thread event to signal that data is available */
88 extern os_event_t log_archiver_thread_event;
89
90 /** Memory block size */
91 constexpr uint ARCH_PAGE_BLK_SIZE = UNIV_PAGE_SIZE_DEF;
92
93 /** Archiver client state.
94 Archiver clients request archiving for specific interval using
95 the start and stop interfaces. During this time the client is
96 attached to global Archiver system. A client copies archived
97 data for the interval after calling stop. System keeps the data
98 till the time client object is destroyed.
99
100 @startuml
101
102 state ARCH_CLIENT_STATE_INIT
103 state ARCH_CLIENT_STATE_STARTED
104 state ARCH_CLIENT_STATE_STOPPED
105
106 [*] -down-> ARCH_CLIENT_STATE_INIT
107 ARCH_CLIENT_STATE_INIT -down-> ARCH_CLIENT_STATE_STARTED : Attach and start \
108 archiving
109 ARCH_CLIENT_STATE_STARTED -right-> ARCH_CLIENT_STATE_STOPPED : Stop \
110 archiving
111 ARCH_CLIENT_STATE_STOPPED -down-> [*] : Detach client
112
113 @enduml */
114 enum Arch_Client_State {
115 /** Client is initialized */
116 ARCH_CLIENT_STATE_INIT = 0,
117
118 /** Archiving started by client */
119 ARCH_CLIENT_STATE_STARTED,
120
121 /** Archiving stopped by client */
122 ARCH_CLIENT_STATE_STOPPED
123 };
124
125 /** Remove files related to page and log archiving.
126 @param[in] file_path path to the file
127 @param[in] file_name name of the file */
128 void arch_remove_file(const char *file_path, const char *file_name);
129
130 /** Remove group directory and the files related to page and log archiving.
131 @param[in] dir_path path to the directory
132 @param[in] dir_name directory name */
133 void arch_remove_dir(const char *dir_path, const char *dir_name);
134
135 /** Archiver system state.
136 Archiver state changes are triggered by client request to start or
137 stop archiving and system wide events like shutdown fatal error etc.
138 Following diagram shows the state transfer.
139
140 @startuml
141
142 state ARCH_STATE_INIT
143 state ARCH_STATE_ACTIVE
144 state ARCH_STATE_PREPARE_IDLE
145 state ARCH_STATE_IDLE
146 state ARCH_STATE_ABORT
147
148 [*] -down-> ARCH_STATE_INIT
149
150 ARCH_STATE_INIT -down-> ARCH_STATE_ACTIVE : Start archiving
151 ARCH_STATE_ACTIVE -right-> ARCH_STATE_PREPARE_IDLE : Stop archiving
152 ARCH_STATE_PREPARE_IDLE -right-> ARCH_STATE_IDLE : All data archived
153 ARCH_STATE_IDLE -down-> ARCH_STATE_ABORT : Shutdown or Fatal Error
154 ARCH_STATE_PREPARE_IDLE --> ARCH_STATE_ACTIVE : Resume archiving
155 ARCH_STATE_IDLE --> ARCH_STATE_ACTIVE : Start archiving
156 ARCH_STATE_ABORT -down-> [*]
157
158 @enduml */
159 enum Arch_State {
160 /** Archiver is initialized */
161 ARCH_STATE_INIT = 0,
162
163 /** Archiver is active and archiving data */
164 ARCH_STATE_ACTIVE,
165
166 /** Archiver is processing last data chunks before idle state */
167 ARCH_STATE_PREPARE_IDLE,
168
169 /** Archiver is idle */
170 ARCH_STATE_IDLE,
171
172 /** Server is in read only mode, and hence the archiver */
173 ARCH_STATE_READ_ONLY,
174
175 /** Archiver is aborted */
176 ARCH_STATE_ABORT
177 };
178
179 /** Archived data block state.
180 A data block is a block in memory that holds dirty page IDs before persisting
181 into disk. Shown below is the state transfer diagram for a data block.
182
183 @startuml
184
185 state ARCH_BLOCK_INIT
186 state ARCH_BLOCK_ACTIVE
187 state ARCH_BLOCK_READY_TO_FLUSH
188 state ARCH_BLOCK_FLUSHED
189
190 [*] -down-> ARCH_BLOCK_INIT
191 ARCH_BLOCK_INIT -> ARCH_BLOCK_ACTIVE : Writing page ID
192 ARCH_BLOCK_ACTIVE -> ARCH_BLOCK_READY_TO_FLUSH : Block is full
193 ARCH_BLOCK_READY_TO_FLUSH -> ARCH_BLOCK_FLUSHED : Block is flushed
194 ARCH_BLOCK_FLUSHED --> ARCH_BLOCK_ACTIVE : Writing page ID
195 ARCH_BLOCK_FLUSHED -down-> [*]
196
197 @enduml */
198 enum Arch_Blk_State {
199 /** Data block is initialized */
200 ARCH_BLOCK_INIT = 0,
201
202 /** Data block is active and having data */
203 ARCH_BLOCK_ACTIVE,
204
205 /** Data block is full but not flushed to disk */
206 ARCH_BLOCK_READY_TO_FLUSH,
207
208 /** Data block is flushed and can be reused */
209 ARCH_BLOCK_FLUSHED
210 };
211
212 /** Archiver block type */
213 enum Arch_Blk_Type {
214 /* Block which holds reset information */
215 ARCH_RESET_BLOCK = 0,
216
217 /* Block which holds archived page IDs */
218 ARCH_DATA_BLOCK
219 };
220
221 /** Archiver block flush type */
222 enum Arch_Blk_Flush_Type {
223 /** Flush when block is full */
224 ARCH_FLUSH_NORMAL = 0,
225
226 /** Flush partial block.
227 Needed for persistent page tracking. */
228 ARCH_FLUSH_PARTIAL
229 };
230
231 /** Page Archive doublewrite buffer block offsets */
232 enum Arch_Page_Dblwr_Offset {
233 /** Archive doublewrite buffer page offset for RESET page. */
234 ARCH_PAGE_DBLWR_RESET_PAGE = 0,
235
236 /* Archive doublewrite buffer page offset for FULL FLUSH page. */
237 ARCH_PAGE_DBLWR_FULL_FLUSH_PAGE,
238
239 /* Archive doublewrite buffer page offset for PARTIAL FLUSH page. */
240 ARCH_PAGE_DBLWR_PARTIAL_FLUSH_PAGE
241 };
242
243 /** Initialize Page and Log archiver system
244 @return error code */
245 dberr_t arch_init();
246
247 /** Free Page and Log archiver system */
248 void arch_free();
249
250 /** Start log archiver background thread.
251 @return error code */
252 int start_log_archiver_background();
253
254 /** Start page archiver background thread.
255 @return error code */
256 int start_page_archiver_background();
257
258 /** Archiver thread event to signal that data is available */
259 extern os_event_t page_archiver_thread_event;
260
261 /** Page archiver background thread */
262 void page_archiver_thread();
263
264 /** Wakes up archiver threads.
265 @return true iff any thread was still alive */
266 bool arch_wake_threads();
267
268 /** Forward declarations */
269 class Arch_Group;
270 class Arch_Log_Sys;
271 class Arch_Dblwr_Ctx;
272 class Arch_Recv_Group_Info;
273
274 /** Guard to release resources safely */
275 class Arch_scope_guard {
276 public:
277 /** Attach a function to the guard which releases some resource. */
278 Arch_scope_guard(std::function<void()> function) { m_cleanup = function; }
279
280 /** Release the resources automatically at the time of destruction. */
281 ~Arch_scope_guard() {
282 if (m_cleanup) {
283 m_cleanup();
284 }
285 }
286
287 /** Manually release the resource. */
288 void cleanup() {
289 m_cleanup();
290 m_cleanup = nullptr;
291 }
292
293 private:
294 /** Function to release the resource. */
295 std::function<void()> m_cleanup{};
296 };
297
298 /** Position in page ID archiving system */
299 struct Arch_Page_Pos {
300 /** Initialize a position */
301 void init();
302
303 /** Position in the beginning of next block */
304 void set_next();
305
306 /** Unique block number */
307 uint64_t m_block_num;
308
309 /** Offset within a block */
310 uint m_offset;
311
312 bool operator<(Arch_Page_Pos pos) {
313 if (m_block_num < pos.m_block_num ||
314 (m_block_num == pos.m_block_num && m_offset <= pos.m_offset)) {
315 return (true);
316 }
317 return (false);
318 }
319 };
320
321 /** Structure which represents a point in a file. */
322 struct Arch_Point {
323 /** LSN of the point */
324 lsn_t lsn{LSN_MAX};
325
326 /** Position of the point */
327 Arch_Page_Pos pos;
328 };
329
330 /* Structure which represents a file in a group and its reset points. */
331 struct Arch_Reset_File {
332 /* Initialize the structure. */
333 void init();
334
335 /* Index of the file in the group */
336 uint m_file_index{0};
337
338 /* LSN of the first reset point in the vector of reset points this
339 structure maintains. Treated as the file LSN. */
340 lsn_t m_lsn{LSN_MAX};
341
342 /* Vector of reset points which belong to this file */
343 std::vector<Arch_Point> m_start_point;
344 };
345
346 /* Structure representing list of archived files. */
347 using Arch_Reset = std::deque<Arch_Reset_File>;
348
349 /** In memory data block in Page ID archiving system */
350 class Arch_Block {
351 public:
352 /** Constructor: Initialize elements
353 @param[in] blk_buf buffer for data block
354 @param[in] size buffer size
355 @param[in] type block type */
356 Arch_Block(byte *blk_buf, uint size, Arch_Blk_Type type)
357 : m_data(blk_buf), m_size(size), m_type(type) {}
358
359 /** Do a deep copy of the members of the block passed as the parameter.
360 @note This member needs to be updated whenever a new data member is added to
361 this class. */
362 void copy_data(const Arch_Block *block);
363
364 /** Set the block ready to begin writing page ID
365 @param[in] pos position to initiate block number */
366 void begin_write(Arch_Page_Pos pos);
367
368 /** End writing to a block.
369 Change state to #ARCH_BLOCK_READY_TO_FLUSH */
370 void end_write();
371
372 /** Check if block is initialised or not.
373 @return true if it has been initialised, else false */
374 bool is_init() const { return (m_state == ARCH_BLOCK_INIT); }
375
376 bool is_active() const { return (m_state == ARCH_BLOCK_ACTIVE); }
377 /** Check if the block can be flushed or not.
378 @return true, if the block cannot be flushed */
379 bool is_flushable() const { return (m_state != ARCH_BLOCK_READY_TO_FLUSH); }
380
381 /** Set current block flushed.
382 Must hold page archiver sys operation mutex. */
383 void set_flushed() { m_state = ARCH_BLOCK_FLUSHED; }
384
385 /** Add page ID to current block
386 @param[in] page page from buffer pool
387 @param[in] pos Archiver current position
388 @return true, if successful
389 false, if no more space in current block */
390 bool add_page(buf_page_t *page, Arch_Page_Pos *pos);
391
392 /* Add reset information to the current reset block.
393 @param[in] reset_lsn reset lsn info
394 @param[in] reset_pos reset pos info which needs to be added
395 to the current reset block */
396 void add_reset(lsn_t reset_lsn, Arch_Page_Pos reset_pos);
397
398 /** Copy page Ids from this block at read position to a buffer.
399 @param[in] read_pos current read position
400 @param[in] read_len length of data to copy
401 @param[out] read_buff buffer to copy page IDs.
402 Caller must allocate the buffer.
403 @return true, if successful
404 false, if block is already overwritten */
405 bool get_data(Arch_Page_Pos *read_pos, uint read_len, byte *read_buff);
406
407 /** Copy page Ids from a buffer to this block.
408 @param[in] read_len length of data to copy
409 @param[in] read_buff buffer to copy page IDs from
410 @param[in] read_offset offset from where to write
411 @return true if successful */
412 bool set_data(uint read_len, byte *read_buff, uint read_offset);
413
414 /** Flush this block to the file group
415 @param[in] file_group current archive group
416 @param[in] type flush type
417 @return error code. */
418 dberr_t flush(Arch_Group *file_group, Arch_Blk_Flush_Type type);
419
420 /* Update the block header with the given LSN
421 @param[in] stop_lsn stop LSN to update in the block header
422 @param[in] reset_lsn reset LSN to update in the blk header */
423 void update_block_header(lsn_t stop_lsn, lsn_t reset_lsn);
424
425 /** @return data length of the block. */
426 uint get_data_len() const { return m_data_len; }
427
428 /** Set the data length of the block.
429 @param[in] data_len data length */
430 void set_data_len(uint data_len) { m_data_len = data_len; }
431
432 /** Set the reset length of the block.
433 @param[in] reset_lsn reset lsn */
434 void set_reset_lsn(lsn_t reset_lsn) { m_reset_lsn = reset_lsn; }
435
436 /** @return block number of the block. */
437 uint64_t get_number() const { return (m_number); }
438
439 /** @return stop lsn */
440 lsn_t get_stop_lsn() const { return (m_stop_lsn); }
441
442 /** Get oldest LSN among the pages that are added to this block
443 @return oldest LSN in block pages */
444 lsn_t get_oldest_lsn() const { return (m_oldest_lsn); }
445
446 /** Get current state of the block
447 @return block state */
448 Arch_Blk_State get_state() const { return (m_state); }
449
450 /** Check if the block data is valid.
451 @param[in] block block to be validated
452 @return true if it's a valid block, else false */
453 static bool validate(byte *block);
454
455 /** Get file index of the file the block belongs to.
456 @return file index */
457 static uint get_file_index(uint64_t block_num, Arch_Blk_Type type);
458
459 /** Get block type from the block header.
460 @param[in] block block from where to get the type
461 @return block type */
462 static Arch_Blk_Type get_type(byte *block);
463
464 /** Get block data length from the block header.
465 @param[in] block block from where to get the data length
466 @return block data length */
467 static uint get_data_len(byte *block);
468
469 /** Get the stop lsn stored in the block header.
470 @param[in] block block from where to fetch the stop lsn
471 @return stop lsn */
472 static lsn_t get_stop_lsn(byte *block);
473
474 /** Get the block number from the block header.
475 @param[in] block block from where to fetch the block number
476 @return block number */
477 static uint64_t get_block_number(byte *block);
478
479 /** Get the reset lsn stored in the block header.
480 @param[in] block block from where to fetch the reset lsn
481 @return reset lsn */
482 static lsn_t get_reset_lsn(byte *block);
483
484 /** Get the checksum stored in the block header.
485 @param[in] block block from where to fetch the checksum
486 @return checksum */
487 static uint32_t get_checksum(byte *block);
488
489 /** Fetch the offset for a block in the archive file.
490 @param[in] block_num block number
491 @param[in] type type of block
492 @return file offset of the block */
493 static uint64_t get_file_offset(uint64_t block_num, Arch_Blk_Type type);
494
495 private:
496 /* @note member function copy_data needs to be updated whenever a new data
497 member is added to this class. */
498
499 /** Block data buffer */
500 byte *m_data;
501
502 /** Block data length in bytes */
503 uint m_data_len{};
504
505 /** Total block size in bytes */
506 uint m_size;
507
508 /** State of the block. */
509 Arch_Blk_State m_state{ARCH_BLOCK_INIT};
510
511 /** Unique block number */
512 uint64_t m_number{};
513
514 /** Type of block. */
515 Arch_Blk_Type m_type;
516
517 /** Checkpoint lsn at the time the last page ID was added to the
518 block. */
519 lsn_t m_stop_lsn{LSN_MAX};
520
521 /** Oldest LSN of all the page IDs added to the block since the last
522 checkpoint */
523 lsn_t m_oldest_lsn{LSN_MAX};
524
525 /** Start LSN or the last reset LSN of the group */
526 lsn_t m_reset_lsn{LSN_MAX};
527 };
528
529 /** Archiver file context.
530 Represents a set of fixed size files within a group */
531 class Arch_File_Ctx {
532 public:
533 class Recovery;
534
535 /** Constructor: Initialize members */
536 Arch_File_Ctx() { m_file.m_file = OS_FILE_CLOSED; }
537
538 /** Destructor: Close open file and free resources */
539 ~Arch_File_Ctx() {
540 close();
541
542 if (m_name_buf != nullptr) {
543 ut::free(m_name_buf);
544 }
545 }
546
547 /** Initializes archiver file context.
548 @param[in] path path to the file
549 @param[in] base_dir directory name prefix
550 @param[in] base_file file name prefix
551 @param[in] num_files initial number of files
552 @return error code. */
553 dberr_t init(const char *path, const char *base_dir, const char *base_file,
554 uint num_files);
555
556 /** Open a file at specific index
557 @param[in] read_only open in read only mode
558 @param[in] start_lsn start lsn for the group
559 @param[in] file_index index of the file within the group which needs
560 to be opened
561 @param[in] file_offset start offset
562 @param[in] file_size maximum allowed file size or 0 to use its current
563 real size on disk as the limitation (file is full)
564 @return error code. */
565 dberr_t open(bool read_only, lsn_t start_lsn, uint file_index,
566 uint64_t file_offset, uint64_t file_size);
567
568 /** Add a new file and open
569 @param[in] start_lsn start lsn for the group
570 @param[in] new_file_size size limit for the new file
571 @param[in] initial_file_size initial size of file to create
572 @return error code. */
573 dberr_t open_new(lsn_t start_lsn, uint64_t new_file_size,
574 uint64_t initial_file_size);
575
576 /** Open next file for read
577 @param[in] start_lsn start lsn for the group
578 @param[in] file_offset start offset
579 @param[in] file_size maximum allowed file size or 0 to use its current
580 real size on disk as the limitation (file is full)
581 @return error code. */
582 dberr_t open_next(lsn_t start_lsn, uint64_t file_offset, uint64_t file_size);
583
584 /** Read data from the current file that is open.
585 Caller must ensure that the size is within the limits of current file
586 context.
587 @param[in,out] to_buffer read data into this buffer
588 @param[in] offset file offset from where to read
589 @param[in] size size of data to read in bytes
590 @return error code */
591 dberr_t read(byte *to_buffer, const uint64_t offset, uint size);
592
593 /** Resize file to provided size and overwrite the whole file with 0x00.
594 @param[in] file_size new file size
595 @return error code */
596 dberr_t resize_and_overwrite_with_zeros(uint64_t file_size);
597
598 /** Write data to this file context from the given file offset.
599 Data source is another file context or buffer. If buffer is NULL, data is
600 copied from input file context. Caller must ensure that the size is within
601 the limits of current file for both source and destination file context.
602 @param[in] from_file file context to copy data from
603 @param[in] from_buffer buffer to copy data or NULL
604 @param[in] offset file offset from where to write
605 @param[in] size size of data to copy in bytes
606 @return error code */
607 dberr_t write(Arch_File_Ctx *from_file, byte *from_buffer, uint offset,
608 uint size);
609
610 /** Write data to this file context from the current offset.
611 Data source is another file context or buffer. If buffer is NULL, data is
612 copied from input file context. Caller must ensure that the size is within
613 the limits of current file for both source and destination file context.
614 @param[in] from_file file context to copy data from
615 @param[in] from_buffer buffer to copy data or NULL
616 @param[in] size size of data to copy in bytes
617 @return error code */
618 dberr_t write(Arch_File_Ctx *from_file, byte *from_buffer, uint size);
619
620 /** Flush file. */
621 void flush() {
622 if (m_file.m_file != OS_FILE_CLOSED) {
623 os_file_flush(m_file);
624 }
625 }
626
627 /** Close file, if open */
628 void close() {
629 if (m_file.m_file != OS_FILE_CLOSED) {
630 os_file_close(m_file);
631 m_file.m_file = OS_FILE_CLOSED;
632 }
633 }
634
635 /** Check if file is closed
636 @return true, if file is closed */
637 bool is_closed() const { return (m_file.m_file == OS_FILE_CLOSED); }
638
639 /** Check how much is left in current file
640 @return length left in bytes */
641 uint64_t bytes_left() const {
642 ut_ad(m_size >= m_offset);
643 return (m_size - m_offset);
644 }
645
646 /** Construct file name at specific index
647 @param[in] idx file index
648 @param[in] dir_lsn lsn of the group
649 @param[out] buffer file name including path.
650 The buffer is allocated by caller.
651 @param[in] length buffer length */
652 void build_name(uint idx, lsn_t dir_lsn, char *buffer, uint length);
653
654 /** Construct group directory name
655 @param[in] dir_lsn lsn of the group
656 @param[out] buffer directory name.
657 The buffer is allocated by caller.
658 @param[in] length buffer length */
659 void build_dir_name(lsn_t dir_lsn, char *buffer, uint length);
660
661 /** Get the logical size of a file.
662 @return logical file size. */
663 uint64_t get_size() const { return (m_size); }
664
665 /* Fetch offset of the file open in this context.
666 @return file offset */
667 uint64_t get_offset() const { return (m_offset); }
668
669 /** Get current file index.
670 @return current file index */
671 uint get_index() const { return m_index; }
672
673 /** Get number of files
674 @return current file count */
675 uint get_count() const { return (m_count); }
676
677 /** Get the physical size of a file that is open in this context.
678 @return physical file size */
679 uint64_t get_phy_size() const {
680 ut_ad(m_name_buf != nullptr);
681 os_file_size_t file_size = os_file_get_size(m_name_buf);
682 return (file_size.m_total_size);
683 }
684
685 /** Update stop lsn of a file in the group.
686 @param[in] file_index file_index the current write_pos belongs to
687 @param[in] stop_lsn stop point */
688 void update_stop_point(uint file_index, lsn_t stop_lsn);
689
690 #ifdef UNIV_DEBUG
691 /** Check if the information maintained in the memory is the same
692 as the information maintained in the files.
693 @return true if both sets of information are the same
694 @param[in] group group whose file is being validated
695 @param[in] file_index index of the file which is being validated
696 @param[in] start_lsn
697 @param[in,out] reset_count count of files which has been validated
698 @return true if both the sets of information are the same. */
699 bool validate(Arch_Group *group, uint file_index, lsn_t start_lsn,
700 uint &reset_count);
701 #endif
702
703 /** Update the reset information in the in-memory structure that we maintain
704 for faster access.
705 @param[in] lsn lsn at the time of reset
706 @param[in] pos pos at the time of reset */
707 void save_reset_point_in_mem(lsn_t lsn, Arch_Page_Pos pos);
708
709 /** Find the appropriate reset LSN that is less than or equal to the
710 given lsn and fetch the reset point.
711 @param[in] check_lsn LSN to be searched against
712 @param[out] reset_point reset position of the fetched reset point
713 @return true if the search was successful. */
714 bool find_reset_point(lsn_t check_lsn, Arch_Point &reset_point);
715
716 /** Find the first stop LSN that is greater than the given LSN and fetch
717 the stop point.
718 @param[in] group the group whose stop_point we're interested in
719 @param[in] check_lsn LSN to be searched against
720 @param[out] stop_point stop point
721 @param[in] last_pos position of the last block in the group;
722 m_write_pos if group is active and m_stop_pos if not
723 @return true if the search was successful. */
724 bool find_stop_point(Arch_Group *group, lsn_t check_lsn,
725 Arch_Point &stop_point, Arch_Page_Pos last_pos);
726
727 /** Delete a single file belonging to the specified file index.
728 @param[in] file_index file index of the file which needs to be deleted
729 @param[in] begin_lsn group's start lsn
730 @return true if successful, else false. */
731 bool delete_file(uint file_index, lsn_t begin_lsn);
732
733 /** Delete all files for this archive group
734 @param[in] begin_lsn group's start lsn */
735 void delete_files(lsn_t begin_lsn);
736
737 /** Purge archived files until the specified purge LSN.
738 @param[in] begin_lsn start LSN of the group
739 @param[in] end_lsn end LSN of the group
740 @param[in] purge_lsn purge LSN until which files needs to be purged
741 @return LSN until which purging was successful
742 @retval LSN_MAX if there was no purging done. */
743 lsn_t purge(lsn_t begin_lsn, lsn_t end_lsn, lsn_t purge_lsn);
744
745 /** Fetch the status of the page tracking system.
746 @param[out] status vector of a pair of (ID, bool) where ID is the
747 start/stop point and bool is true if the ID is a start point else false */
748 void get_status(std::vector<std::pair<lsn_t, bool>> &status) {
749 for (auto reset_file : m_reset) {
750 for (auto reset_point : reset_file.m_start_point) {
751 status.push_back(std::make_pair(reset_point.lsn, true));
752 }
753 }
754 }
755
756 private:
757 #ifdef UNIV_DEBUG
758 /** Check if the reset information maintained in the memory is the same
759 as the information maintained in the given file.
760 @param[in] file file descriptor
761 @param[in] file_index index of the file
762 @param[in,out] reset_count number of files processed containing
763 reset data
764 @return true if both sets of information are the same */
765 bool validate_reset_block_in_file(pfs_os_file_t file, uint file_index,
766 uint &reset_count);
767
768 /** Check if the stop LSN maintained in the memory is the same as the
769 information maintained in the files.
770 @param[in] group group whose file is being validated
771 @param[in] file file descriptor
772 @param[in] file_index index of the file for which the validation is
773 happening
774 @return true if both the sets of information are the same. */
775 bool validate_stop_point_in_file(Arch_Group *group, pfs_os_file_t file,
776 uint file_index);
777 #endif
778
779 /** Fetch reset lsn of a particular reset point pertaining to a file.
780 @param[in] block_num block number where the reset occurred.
781 @return reset lsn */
782 lsn_t fetch_reset_lsn(uint64_t block_num);
783
784 private:
785 /** File name buffer.
786 Used if caller doesn't allocate buffer. */
787 char *m_name_buf{nullptr};
788
789 /** File name buffer length */
790 uint m_name_len{};
791
792 /** Fixed length part of the file.
793 Path ended with directory separator. */
794 uint m_base_len{};
795
796 /** Fixed part of the path to file */
797 const char *m_path_name{nullptr};
798
799 /** Directory name prefix */
800 const char *m_dir_name{nullptr};
801
802 /** File name prefix */
803 const char *m_file_name{nullptr};
804
805 /** Current file descriptor */
806 pfs_os_file_t m_file;
807
808 /** File index within the archive group */
809 uint m_index{};
810
811 /** Current number of files in the archive group */
812 uint m_count{};
813
814 /** Current file offset */
815 uint64_t m_offset{};
816
817 /** File size limit in bytes */
818 uint64_t m_size{};
819
820 /** Queue of file structure holding reset information pertaining to
821 their respective files in a group.
822 Protected by Arch_Page_Sys::m_mutex and Arch_Page_Sys::m_oper_mutex.
823 @note used only by the page archiver */
824 Arch_Reset m_reset;
825
826 /** Vector of stop points corresponding to a file.
827 Stop point refers to the stop lsn (checkpoint lsn) until which the pages are
828 guaranteed to be tracked in a file. Each block in a file maintains this
829 information.
830 Protected by Arch_Page_Sys::m_oper_mutex.
831 @note used only by the page archiver */
832 std::vector<lsn_t> m_stop_points;
833 };
834
835 /** Number which tries to uniquely identify the archived data (unless it is
836 zero, which stands for unsupported identification). Currently only redo log
837 files are identified (by Log_uuid's value). */
838 typedef uint32_t Arch_group_uuid;
839
840 /** Contiguous archived data for redo log or page tracking.
841 If there is a gap, that is if archiving is stopped and started, a new
842 group is created. */
843 class Arch_Group {
844 public:
845 /** Function responsible to format the header of a new file which is
846 created, when the stream of data is written to a sequence of new files.
847 @param[in] start_offset offset at which a new file starts, expressed
848 in bytes from the beginning of the stream
849 @param[in] header header to format */
850 typedef std::function<dberr_t(uint64_t start_offset, byte *header)>
851 Get_file_header_callback;
852
853 /** Constructor: Initialize members
854 @param[in] start_lsn start LSN for the group
855 @param[in] header_len length of header for archived files
856 @param[in] mutex archive system mutex from caller */
857 Arch_Group(lsn_t start_lsn, uint header_len, ib_mutex_t *mutex)
858 : m_begin_lsn(start_lsn),
859 m_header_len(header_len) IF_DEBUG(, m_arch_mutex(mutex)) {
860 m_active_file.m_file = OS_FILE_CLOSED;
861 m_durable_file.m_file = OS_FILE_CLOSED;
862 m_stop_pos.init();
863 }
864
865 /** Destructor: Delete all files for non-durable archiving. */
866 ~Arch_Group();
867
868 /** Initialize the doublewrite buffer file context for the archive group.
869 @param[in] path path to the file
870 @param[in] base_file file name prefix
871 @param[in] num_files initial number of files
872 @param[in] file_size file size in bytes
873 @return error code. */
874 static dberr_t init_dblwr_file_ctx(const char *path, const char *base_file,
875 uint num_files, uint64_t file_size);
876
877 /** Initialize the file context for the archive group.
878 File context keeps the archived data in files on disk. There
879 is one file context for a archive group.
880 @param[in] path path to the file
881 @param[in] base_dir directory name prefix
882 @param[in] base_file file name prefix
883 @param[in] num_files initial number of files
884 @param[in] file_size size of file used when a new file is created
885 @param[in] uuid uuid of this arch group or 0 if unknown
886 @return error code. */
887 dberr_t init_file_ctx(const char *path, const char *base_dir,
888 const char *base_file, uint num_files,
889 uint64_t file_size, Arch_group_uuid uuid) {
890 m_uuid = uuid;
891 m_file_size = file_size;
892 return (m_file_ctx.init(path, base_dir, base_file, num_files));
893 }
894
895 /* Close the file contexts when they're not required anymore. */
896 void close_file_ctxs() {
897 m_file_ctx.close();
898
899 if (m_durable_file.m_file != OS_FILE_CLOSED) {
900 os_file_close(m_durable_file);
901 m_durable_file.m_file = OS_FILE_CLOSED;
902 }
903 }
904
905 /** Mark archive group inactive.
906 A group is marked inactive by archiver background before entering
907 into idle state ARCH_STATE_IDLE.
908 @param[in] end_lsn lsn where redo archiving is stopped */
909 void disable(lsn_t end_lsn) {
910 m_is_active = false;
911
912 if (end_lsn != LSN_MAX) {
913 m_end_lsn = end_lsn;
914 }
915 }
916
917 /** Attach a client to the archive group.
918 @param[in] is_durable true, if durable tracking is requested */
919 void attach(bool is_durable) {
920 ut_ad(mutex_own(m_arch_mutex));
921 ++m_num_active;
922
923 if (is_durable) {
924 ++m_dur_ref_count;
925 } else {
926 ut_ad(m_ref_count < std::numeric_limits<decltype(m_ref_count)>::max());
927 if (m_ref_count < std::numeric_limits<decltype(m_ref_count)>::max()) {
928 ++m_ref_count;
929 }
930 }
931 }
932
933 /** Detach a client when archiving is stopped by the client.
934 The client still has reference to the group so that the group
935 is not destroyed when it retrieves the archived data. The
936 reference is removed later by #Arch_Group::release.
937 @param[in] stop_lsn archive stop lsn for client
938 @param[in] stop_pos archive stop position for client. Used only by
939 the page_archiver.
940 @return number of active clients */
941 uint detach(lsn_t stop_lsn, Arch_Page_Pos *stop_pos) {
942 ut_ad(m_num_active > 0);
943 ut_ad(mutex_own(m_arch_mutex));
944 --m_num_active;
945
946 if (m_num_active == 0) {
947 m_end_lsn = stop_lsn;
948 if (stop_pos != nullptr) {
949 m_stop_pos = *stop_pos;
950 }
951 }
952
953 return (m_num_active);
954 }
955
956 /** Release the archive group from a client.
957 Reduce the reference count. When all clients release the group,
958 the reference count falls down to zero. The function would then
959 return zero and the caller can remove the group.
960 @param[in] is_durable the client needs durable archiving */
961 void release(bool is_durable) {
962 ut_ad(mutex_own(m_arch_mutex));
963 ut_ad(!is_durable);
964 if (is_durable) {
965 /* For durable, m_ref_count was not incremented. */
966 return;
967 }
968
969 ut_ad(m_ref_count > 0);
970 --m_ref_count;
971 /* If there was a bug, and m_ref_count was 0 before the decrement,
972 it would become std::numeric_limits<decltype(m_ref_count)>::max(),
973 and the caller would not remove the group. If we called attach()
974 afterwards (holding still the m_arch_mutex), the m_ref_count would
975 stay unchanged, because there is mechanism protecting from overflows.
976 This way, the scope of the potential bug, is limited to the group not
977 being removed. */
978 }
979
980 /** Construct file name for the active file which indicates whether a group
981 is active or not.
982 @note Used only by the page archiver.
983 @return error code. */
984 dberr_t build_active_file_name();
985
986 /** Construct file name for the durable file which indicates whether a group
987 was made durable or not.
988 @note Used only by the page archiver.
989 @return error code. */
990 dberr_t build_durable_file_name();
991
992 /** Mark the group active by creating a file in the respective group
993 directory. This is required at the time of recovery to know whether a group
994 was active or not in case of a crash.
995 @note Used only by the page archiver.
996 @return error code. */
997 int mark_active();
998
999 /** Mark the group durable by creating a file in the respective group
1000 directory. This is required at the time of recovery to differentiate durable
1001 group from group left over by crash during clone operation.
1002 @note Used only by the page archiver.
1003 @return error code. */
1004 int mark_durable();
1005
1006 /** Mark the group inactive by deleting the 'active' file. This is required
1007 at the time of crash recovery to know whether a group was active or not in
1008 case of a crash.
1009 @note Used only by the page archiver.
1010 @return error code */
1011 int mark_inactive();
1012
1013 /** Check if archiving is going on for this group
1014 @return true, if the group is active */
1015 bool is_active() const { return (m_is_active); }
1016
1017 /** Write the header (RESET page) to an archived file.
1018 @note Used only by the Page Archiver and not by the Redo Log Archiver.
1019 @param[in] from_buffer buffer to copy data
1020 @param[in] length size of data to copy in bytes
1021 @note Used only by the Page Archiver.
1022 @return error code */
1023 dberr_t write_file_header(byte *from_buffer, uint length);
1024
1025 /** Write to the doublewrite buffer before writing archived data to a file.
1026 The source is either a file context or buffer. Caller must ensure that data
1027 is in single file in source file context.
1028 @param[in] from_file file context to copy data from
1029 @param[in] from_buffer buffer to copy data or NULL
1030 @param[in] write_size size of data to write in bytes
1031 @param[in] offset offset from where to write
1032 @note Used only by the Page Archiver.
1033 @return error code */
1034 static dberr_t write_to_doublewrite_file(Arch_File_Ctx *from_file,
1035 byte *from_buffer, uint write_size,
1036 Arch_Page_Dblwr_Offset offset);
1037
1038 /** Archive data to one or more files.
1039 The source is either a file context or buffer. Caller must ensure that data
1040 is in single file in source file context.
1041 @param[in] from_file file context to copy data from
1042 @param[in] from_buffer buffer to copy data or NULL
1043 @param[in] length size of data to copy in bytes
1044 @param[in] partial_write true if the operation is part of partial flush
1045 @param[in] do_persist doublewrite to ensure persistence
1046 @param[in] new_file callback called for each new file being created
1047 @return error code */
1048 dberr_t write_to_file(Arch_File_Ctx *from_file, byte *from_buffer,
1049 uint length, bool partial_write, bool do_persist,
1050 Get_file_header_callback new_file);
1051
1052 /** Find the appropriate reset LSN that is less than or equal to the
1053 given lsn and fetch the reset point.
1054 @param[in] check_lsn LSN to be searched against
1055 @param[out] reset_point reset position of the fetched reset point
1056 @return true if the search was successful. */
1057 bool find_reset_point(lsn_t check_lsn, Arch_Point &reset_point) {
1058 return (m_file_ctx.find_reset_point(check_lsn, reset_point));
1059 }
1060
1061 /** Find the first stop LSN that is greater than the given LSN and fetch
1062 the stop point.
1063 @param[in] check_lsn LSN to be searched against
1064 @param[out] stop_point stop point
1065 @param[in] write_pos latest write_pos
1066 @return true if the search was successful. */
1067 bool find_stop_point(lsn_t check_lsn, Arch_Point &stop_point,
1068 Arch_Page_Pos write_pos) {
1069 ut_ad(validate_info_in_files());
1070 Arch_Page_Pos last_pos = is_active() ? write_pos : m_stop_pos;
1071 return (m_file_ctx.find_stop_point(this, check_lsn, stop_point, last_pos));
1072 }
1073
1074 #ifdef UNIV_DEBUG
1075 /** Adjust end LSN to end of file. This is used in debug
1076 mode to test the case when LSN is at file boundary.
1077 @param[in,out] stop_lsn stop lsn for client
1078 @param[out] blk_len last block length */
1079 void adjust_end_lsn(lsn_t &stop_lsn, uint32_t &blk_len);
1080
1081 /** Adjust redo copy length to end of file. This is used
1082 in debug mode to archive only till end of file.
1083 @param[in] arch_lsn LSN up to which data is already archived
1084 @param[in,out] copy_len length of data to copy in bytes */
1085 void adjust_copy_length(lsn_t arch_lsn, uint32_t &copy_len);
1086
1087 /** Check if the information maintained in the memory is the same
1088 as the information maintained in the files.
1089 @return true if both sets of information are the same */
1090 bool validate_info_in_files();
1091 #endif /* UNIV_DEBUG */
1092
1093 /** Get the total number of archived files belonging to this group.
1094 @return number of archived files */
1095 uint get_file_count() const { return (m_file_ctx.get_count()); }
1096
1097 /** Check if any client (durable or not) is attached to the archiver.
1098 @return true if any client is attached, else false */
1099 bool is_referenced() const {
1100 return (m_ref_count > 0) || (m_dur_ref_count > 0);
1101 }
1102
1103 /** Check if any client requiring durable archiving is active.
1104 @return true if any durable client is still attached, else false */
1105 bool is_durable_client_active() const {
1106 return (m_num_active != m_ref_count);
1107 }
1108
1109 /** Check if any client requires durable archiving.
1110 @return true if there is at least 1 client that requires durable archiving*/
1111 bool is_durable() const { return (m_dur_ref_count > 0); }
1112
1113 /** Purge archived files until the specified purge LSN.
1114 @param[in] purge_lsn LSN until which archived files needs to be
1115 purged
1116 @param[out] purged_lsn LSN until which purging is successful;
1117 LSN_MAX if there was no purging done
1118 @return error code */
1119 uint purge(lsn_t purge_lsn, lsn_t &purged_lsn);
1120
1121 /** Operations to be done at the time of shutdown. */
1122 static void shutdown() { s_dblwr_file_ctx.close(); }
1123
1124 /** Update the reset information in the in-memory structure that we maintain
1125 for faster access.
1126 @param[in] lsn lsn at the time of reset
1127 @param[in] pos pos at the time of reset */
1128 void save_reset_point_in_mem(lsn_t lsn, Arch_Page_Pos pos) {
1129 m_file_ctx.save_reset_point_in_mem(lsn, pos);
1130 }
1131
1132 /** Update stop lsn of a file in the group.
1133 @param[in] pos stop position
1134 @param[in] stop_lsn stop point */
1135 void update_stop_point(Arch_Page_Pos pos, lsn_t stop_lsn) {
1136 m_file_ctx.update_stop_point(
1137 Arch_Block::get_file_index(pos.m_block_num, ARCH_DATA_BLOCK), stop_lsn);
1138 }
1139
1140 /** Recover the information belonging to this group from the archived files.
1141 @param[in,out] group_info structure containing information of a
1142 group obtained during recovery by scanning files
1143 @param[in] dblwr_ctx file context related to doublewrite buffer
1144 @return error code */
1145 dberr_t recover(Arch_Recv_Group_Info &group_info, Arch_Dblwr_Ctx *dblwr_ctx);
1146
1147 /** Parse block for block info (header/data).
1148 @param[in] cur_pos position to read
1149 @param[in,out] buff buffer into which to write the parsed data
1150 @param[in] buff_len length of the buffer
1151 @return error code */
1152 int read_data(Arch_Page_Pos cur_pos, byte *buff, uint buff_len);
1153
1154 /** Get archived file name at specific index in this group.
1155 Caller would use it to open and copy data from archived files.
1156 @param[in] idx file index in the group
1157 @param[out] name_buf file name and path. Caller must
1158 allocate the buffer.
1159 @param[in] buf_len allocated buffer length */
1160 void get_file_name(uint idx, char *name_buf, uint buf_len) {
1161 ut_ad(name_buf != nullptr);
1162
1163 /* Build name from the file context. */
1164 m_file_ctx.build_name(idx, m_begin_lsn, name_buf, buf_len);
1165 }
1166
1167 /** Get the current file size for this group.
1168 Fixed size files are used for archiving data in a group.
1169 @return file size in bytes */
1170 uint64_t get_file_size() const { return m_file_size; }
1171
1172 /** Get start LSN for this group
1173 @return start LSN */
1174 lsn_t get_begin_lsn() const { return (m_begin_lsn); }
1175
1176 /** @return stop LSN for this group */
1177 lsn_t get_end_lsn() const { return (m_end_lsn); }
1178
1179 /** @return stop block position of the group. */
1180 Arch_Page_Pos get_stop_pos() const { return (m_stop_pos); }
1181
1182 /** @return uuid for the arch group */
1183 Arch_group_uuid get_uuid() const { return m_uuid; }
1184
1185 /** Fetch the status of the page tracking system.
1186 @param[out] status vector of a pair of (ID, bool) where ID is the
1187 start/stop point and bool is true if the ID is a start point else false */
1188 void get_status(std::vector<std::pair<lsn_t, bool>> &status) {
1189 m_file_ctx.get_status(status);
1190
1191 if (!is_active()) {
1192 status.push_back(std::make_pair(m_end_lsn, false));
1193 }
1194 }
1195
1196 /** Open the file which was open at the time of a crash, during crash
1197 recovery, and set the file offset to the last written offset.
1198 @param[in] write_pos latest write position at the time of crash/shutdown
1199 @param[in] create_new create new file if file not present
1200 @return error code. */
1201 dberr_t open_file(Arch_Page_Pos write_pos, bool create_new);
1202
1203 /** Disable copy construction */
1204 Arch_Group(Arch_Group const &) = delete;
1205
1206 /** Disable assignment */
1207 Arch_Group &operator=(Arch_Group const &) = delete;
1208
1209 private:
1210 class Recovery;
1211
1212 /** Get page IDs from archived file
1213 @param[in] read_pos position to read from
1214 @param[in] read_len length of data to read
1215 @param[in] read_buff buffer to read page IDs
1216 @return error code */
1217 int read_from_file(Arch_Page_Pos *read_pos, uint read_len, byte *read_buff);
1218
1219 /** Get the directory name for this archive group.
1220 It is used for cleaning up the archive directory.
1221 @param[out] name_buf directory name and path. Caller must
1222 allocate the buffer.
1223 @param[in] buf_len buffer length */
1224 void get_dir_name(char *name_buf, uint buf_len) {
1225 m_file_ctx.build_dir_name(m_begin_lsn, name_buf, buf_len);
1226 }
1227
1228 /** Create a new file and write the header.
1229 @param[in] start_offset start offste
1230 @param[in] get_header callback which prepares a header */
1231 dberr_t prepare_file_with_header(uint64_t start_offset,
1232 Get_file_header_callback &get_header);
1233
1234 private:
1235 /** If the group is active */
1236 bool m_is_active{true};
1237
1238 /** To know which group was active at the time of a crash/shutdown during
1239 recovery we create an empty file in the group directory. This holds the name
1240 of the file. */
1241 char *m_active_file_name{nullptr};
1242
1243 /** File descriptor for a file required to indicate that the group was
1244 active at the time of crash during recovery . */
1245 pfs_os_file_t m_active_file;
1246
1247 /** File name for the durable file which indicates whether a group was made
1248 durable or not. Required to differentiate durable group from group left over
1249 by crash during clone operation. */
1250 char *m_durable_file_name{nullptr};
1251
1252 /** File descriptor for a file to indicate that the group was made durable or
1253 not. Required to differentiate durable group from group left over by crash
1254 during clone operation. */
1255 pfs_os_file_t m_durable_file;
1256
1257 /** Number of clients referencing the group */
1258 uint m_ref_count{};
1259
1260 /** Number of clients referencing for durable archiving */
1261 uint m_dur_ref_count{};
1262
1263 /** Number of clients for which archiving is in progress */
1264 uint m_num_active{};
1265
1266 /** Start LSN for the archive group */
1267 lsn_t m_begin_lsn{LSN_MAX};
1268
1269 /** End lsn for this archive group */
1270 lsn_t m_end_lsn{LSN_MAX};
1271
1272 /** Stop position of the group, if it's not active. */
1273 Arch_Page_Pos m_stop_pos{};
1274
1275 /** Header length for the archived files */
1276 uint m_header_len{};
1277
1278 /** Size of file used when a new file is being created. */
1279 uint64_t m_file_size;
1280
1281 /** UUID generated for this arch group. */
1282 Arch_group_uuid m_uuid{};
1283
1284 /** Archive file context */
1285 Arch_File_Ctx m_file_ctx;
1286
1287 /** Doublewrite buffer file context.
1288 Note - Used only in the case of page archiver. */
1289 static Arch_File_Ctx s_dblwr_file_ctx;
1290
1291 #ifdef UNIV_DEBUG
1292 /** Mutex protecting concurrent operations by multiple clients.
1293 This is either the redo log or page archive system mutex. Currently
1294 used for assert checks. */
1295 ib_mutex_t *m_arch_mutex;
1296 #endif /* UNIV_DEBUG */
1297 };
1298
1299 /** A list of archive groups */
1300 using Arch_Grp_List = std::list<Arch_Group *, ut::allocator<Arch_Group *>>;
1301
1302 /** An iterator for archive group */
1303 using Arch_Grp_List_Iter = Arch_Grp_List::iterator;
1304
1305 class Arch_log_consumer : public Log_consumer {
1306 public:
1307 const std::string &get_name() const override;
1308
1309 lsn_t get_consumed_lsn() const override;
1310
1311 void consumption_requested(lsn_t request_lsn) override;
1312
1313 private:
1314 std::chrono::system_clock::time_point m_last_rushed_at;
1315 std::chrono::system_clock::time_point m_problem_started_at;
1316 };
1317
1318 /** Redo log archiving system */
1319 class Arch_Log_Sys {
1320 public:
1321 /** Constructor: Initialize members */
1322 Arch_Log_Sys()
1323 : m_state(ARCH_STATE_INIT),
1324 m_archived_lsn(LSN_MAX),
1325 m_group_list(),
1326 m_current_group() {
1327 mutex_create(LATCH_ID_LOG_ARCH, &m_mutex);
1328 }
1329
1330 /** Destructor: Free mutex */
1331 ~Arch_Log_Sys() {
1332 ut_ad(m_state == ARCH_STATE_INIT || m_state == ARCH_STATE_ABORT);
1333 ut_ad(m_current_group == nullptr);
1334 ut_ad(m_group_list.empty());
1335
1336 mutex_free(&m_mutex);
1337 }
1338
1339 /** Check if archiving is in progress.
1340 In #ARCH_STATE_PREPARE_IDLE state, all clients have already detached
1341 but archiver background task is yet to finish.
1342 @return true, if archiving is active */
1343 bool is_active() const {
1344 return (m_state == ARCH_STATE_ACTIVE || m_state == ARCH_STATE_PREPARE_IDLE);
1345 }
1346
1347 /** Check if archiver system is in initial state
1348 @return true, if redo log archiver state is #ARCH_STATE_INIT */
1349 bool is_init() const { return (m_state == ARCH_STATE_INIT); }
1350
1351 /** Get LSN up to which redo is archived
1352 @return last archived redo LSN */
1353 lsn_t get_archived_lsn() const {
1354 lsn_t archived_lsn = m_archived_lsn.load();
1355 ut_ad(archived_lsn == LSN_MAX ||
1356 archived_lsn % OS_FILE_LOG_BLOCK_SIZE == 0);
1357 if (archived_lsn != LSN_MAX && archived_lsn % OS_FILE_LOG_BLOCK_SIZE != 0) {
1358 archived_lsn = ut_uint64_align_down(archived_lsn, OS_FILE_LOG_BLOCK_SIZE);
1359 }
1360 return archived_lsn;
1361 }
1362
1363 /** Get recommended archived redo file size
1364 @return size of file in bytes */
1365 os_offset_t get_recommended_file_size() const;
1366
1367 /** Get current redo log archive group
1368 @return current archive group */
1369 Arch_Group *get_arch_group() { return (m_current_group); }
1370
1371 /** Start redo log archiving.
1372 If archiving is already in progress, the client
1373 is attached to current group.
1374 @param[out] group log archive group
1375 @param[out] start_lsn start lsn for client
1376 @param[out] header redo log header
1377 @param[in] is_durable if client needs durable archiving
1378 @return error code */
1379 int start(Arch_Group *&group, lsn_t &start_lsn, byte *header,
1380 bool is_durable);
1381
1382 /** Stop redo log archiving.
1383 If other clients are there, the client is detached from
1384 the current group.
1385 @param[out] group log archive group
1386 @param[out] stop_lsn stop lsn for client
1387 @param[out] log_blk redo log trailer block
1388 @param[in,out] blk_len length in bytes
1389 @return error code */
1390 int stop(Arch_Group *group, lsn_t &stop_lsn, byte *log_blk,
1391 uint32_t &blk_len);
1392
1393 /** Force to abort the archiver (state becomes ARCH_STATE_IDLE or
1394 ARCH_STATE_ABORT). */
1395 void force_abort();
1396
1397 /** Release the current group from client.
1398 @param[in] group group the client is attached to
1399 @param[in] is_durable if client needs durable archiving */
1400 void release(Arch_Group *group, bool is_durable);
1401
1402 /** Archive accumulated redo log in current group.
1403 This interface is for archiver background task to archive redo log
1404 data by calling it repeatedly over time.
1405 @param[in, out] init true when called the first time; it will
1406 then be set to false
1407 @param[in] curr_ctx system redo logs to copy data from
1408 @param[out] arch_lsn LSN up to which archiving is completed
1409 @param[out] wait true, if no more redo to archive
1410 @return true, if archiving is aborted */
1411 bool archive(bool init, Arch_File_Ctx *curr_ctx, lsn_t *arch_lsn, bool *wait);
1412
1413 /** Acquire redo log archiver mutex.
1414 It synchronizes concurrent start and stop operations by
1415 multiple clients. */
1416 void arch_mutex_enter() { mutex_enter(&m_mutex); }
1417
1418 /** Release redo log archiver mutex */
1419 void arch_mutex_exit() { mutex_exit(&m_mutex); }
1420
1421 /** Disable copy construction */
1422 Arch_Log_Sys(Arch_Log_Sys const &) = delete;
1423
1424 /** Disable assignment */
1425 Arch_Log_Sys &operator=(Arch_Log_Sys const &) = delete;
1426
1427 /** Abort the archiver if it is lagging behind and unable to advance. */
1428 void async_abort_if_below(lsn_t requested_lsn);
1429
1430 private:
1431 /** Wait for archive system to come out of #ARCH_STATE_PREPARE_IDLE.
1432 If the system is preparing to idle, #start needs to wait
1433 for it to come to idle state.
1434 @return true, if successful
1435 false, if needs to abort */
1436 bool wait_idle();
1437
1438 /** Wait for redo log archive up to the target LSN.
1439 We need to wait till current log sys LSN during archive stop.
1440 @param[in] target_lsn target archive LSN to wait for
1441 @return error code */
1442 int wait_archive_complete(lsn_t target_lsn);
1443
1444 /** Update checkpoint LSN and related information in redo
1445 log header block.
1446 @param[in,out] header redo log header buffer
1447 @param[in] file_start_lsn LSN of first data byte within file
1448 @param[in] checkpoint_lsn LSN of the checkpoint within the file or 0 */
1449 void update_header(byte *header, lsn_t file_start_lsn, lsn_t checkpoint_lsn);
1450
1451 /** Check and set log archive system state and output the
1452 amount of redo log available for archiving.
1453 @param[in] is_abort need to abort
1454 @param[in,out] archived_lsn LSN up to which redo log is archived
1455 @param[out] to_archive amount of redo log to be archived */
1456 Arch_State check_set_state(bool is_abort, lsn_t *archived_lsn,
1457 uint *to_archive);
1458
1459 /** Copy redo log from file context to archiver files.
1460 @param[in] file_ctx file context for system redo logs
1461 @param[in] start_lsn lsn at which we start copying
1462 @param[in] length data to copy in bytes
1463 @return error code */
1464 dberr_t copy_log(Arch_File_Ctx *file_ctx, lsn_t start_lsn, uint length);
1465
1466 /** Update m_state to the given state. Then check if Arch_Log_Sys is active
1467 and accordingly register or unregister the @see m_log_consumer. It is caller
1468 that should acquire log_sys's: m_files_mutex and writer_mutex prior before
1469 calling this method.
1470 @param[in] state state to assign to m_state */
1471 void update_state_low(Arch_State state);
1472
1473 /** Acquires log_sys's m_files_mutex, writer_mutex and calls
1474 @see update_state_low(state).
1475 @param[in] state state to assign to m_state */
1476 void update_state(Arch_State state);
1477
1478 private:
1479 /** Mutex to protect concurrent start, stop operations */
1480 ib_mutex_t m_mutex;
1481
1482 /** Archiver system state.
1483 #m_state is protected by #m_mutex and #log_t::writer_mutex. For changing
1484 the state both needs to be acquired. For reading, hold any of the two
1485 mutexes. Same is true for #m_archived_lsn. */
1486 Arch_State m_state;
1487
1488 /** System has archived log up to this LSN */
1489 atomic_lsn_t m_archived_lsn;
1490
1491 /** List of log archive groups */
1492 Arch_Grp_List m_group_list;
1493
1494 /** Current archive group */
1495 Arch_Group *m_current_group;
1496
1497 /** Chunk size to copy redo data */
1498 uint m_chunk_size;
1499
1500 /** System log file number where the archiving started */
1501 uint m_start_log_index;
1502
1503 /** System log file offset where the archiving started */
1504 uint64_t m_start_log_offset;
1505
1506 /** Redo log consumer that can be registered to prevent consumption
1507 of redo log files which still haven't been archived. */
1508 Arch_log_consumer m_log_consumer;
1509
1510 /** Non-zero if async_abort_if_below(request_lsn) was called, which sets it to
1511 request_lsn. Used to notify the thread calling Arch_Log_Sys::archive(..) that
1512 it should abort. Because this field is not cleared, and we don't want to
1513 prevent future CLONE operations, the operation should only be aborted if
1514 arch_lsn is below the value of this field */
1515 std::atomic<lsn_t> m_abort_if_below_lsn{0};
1516 };
1517
1518 /** Vector of page archive in memory blocks */
1519 using Arch_Block_Vec = std::vector<Arch_Block *, ut::allocator<Arch_Block *>>;
1520
1521 /** Page archiver in memory data */
1522 struct ArchPageData {
1523 /** Constructor */
1524 ArchPageData() = default;
1525
1526 /** Allocate buffer and initialize blocks
1527 @return true, if successful */
1528 bool init();
1529
1530 /** Delete blocks and buffer */
1531 void clean();
1532
1533 /** Get the block for a position
1534 @param[in] pos position in page archive sys
1535 @param[in] type block type
1536 @return page archive in memory block */
1537 Arch_Block *get_block(Arch_Page_Pos *pos, Arch_Blk_Type type);
1538
1539 /** @return temporary block used to copy active block for partial flush. */
1540 Arch_Block *get_partial_flush_block() const {
1541 return (m_partial_flush_block);
1542 }
1543
1544 /** Vector of data blocks */
1545 Arch_Block_Vec m_data_blocks{};
1546
1547 /** Reset block */
1548 Arch_Block *m_reset_block{nullptr};
1549
1550 /** Temporary block used to copy active block for partial flush. */
1551 Arch_Block *m_partial_flush_block{nullptr};
1552
1553 /** Block size in bytes */
1554 uint m_block_size{};
1555
1556 /** Total number of blocks */
1557 uint m_num_data_blocks{};
1558
1559 /** In memory buffer */
1560 byte *m_buffer{nullptr};
1561 };
1562
1563 /** Forward declaration. */
1564 class Page_Arch_Client_Ctx;
1565
1566 /** Dirty page archive system */
1567 class Arch_Page_Sys {
1568 public:
1569 /** Constructor: Initialize elements and create mutex */
1570 Arch_Page_Sys();
1571
1572 /** Destructor: Free memory buffer and mutexes */
1573 ~Arch_Page_Sys();
1574
1575 /** Start dirty page ID archiving.
1576 If archiving is already in progress, the client is attached to current group.
1577 @param[out] group page archive group the client gets attached to
1578 @param[out] start_lsn start lsn for client in archived data
1579 @param[out] start_pos start position for client in archived data
1580 @param[in] is_durable true if client needs durable archiving
1581 @param[in] restart true if client is already attached to current group
1582 @param[in] recovery true if archiving is being started during
1583 recovery
1584 @return error code */
1585 int start(Arch_Group **group, lsn_t *start_lsn, Arch_Page_Pos *start_pos,
1586 bool is_durable, bool restart, bool recovery);
1587
1588 /** Stop dirty page ID archiving.
1589 If other clients are there, the client is detached from the current group.
1590 @param[in] group page archive group the client is attached to
1591 @param[out] stop_lsn stop lsn for client
1592 @param[out] stop_pos stop position in archived data
1593 @param[in] is_durable true if client needs durable archiving
1594 @return error code */
1595 int stop(Arch_Group *group, lsn_t *stop_lsn, Arch_Page_Pos *stop_pos,
1596 bool is_durable);
1597
1598 /** Start dirty page ID archiving during recovery.
1599 @param[in,out] info information related to a group required for recovery
1600 @return error code */
1601 int recovery_load_and_start(const Arch_Recv_Group_Info &info);
1602
1603 /** Release the current group from client.
1604 @param[in] group group the client is attached to
1605 @param[in] is_durable if client needs durable archiving
1606 @param[in] start_pos start position when the client calling the
1607 release was started */
1608 void release(Arch_Group *group, bool is_durable, Arch_Page_Pos start_pos);
1609
1610 /** Check and add page ID to archived data.
1611 Check for duplicate page.
1612 @param[in] bpage page to track
1613 @param[in] track_lsn LSN when tracking started
1614 @param[in] frame_lsn current LSN of the page
1615 @param[in] force if true, add page ID without check */
1616 void track_page(buf_page_t *bpage, lsn_t track_lsn, lsn_t frame_lsn,
1617 bool force);
1618
1619 /** Flush all the unflushed inactive blocks and flush the active block if
1620 required.
1621 @note Used only during the checkpointing process.
1622 @param[in] checkpoint_lsn next checkpoint LSN */
1623 void flush_at_checkpoint(lsn_t checkpoint_lsn);
1624
1625 /** Archive dirty page IDs in current group.
1626 This interface is for archiver background task to flush page archive
1627 data to disk by calling it repeatedly over time.
1628 @param[out] wait true, if no more data to archive
1629 @return true, if archiving is aborted */
1630 bool archive(bool *wait);
1631
1632 /** Acquire dirty page ID archiver mutex.
1633 It synchronizes concurrent start and stop operations by multiple clients. */
1634 void arch_mutex_enter() { mutex_enter(&m_mutex); }
1635
1636 /** Release page ID archiver mutex */
1637 void arch_mutex_exit() { mutex_exit(&m_mutex); }
1638
1639 /** Acquire dirty page ID archive operation mutex.
1640 It synchronizes concurrent page ID write to memory buffer. */
1641 void arch_oper_mutex_enter() { mutex_enter(&m_oper_mutex); }
1642
1643 /** Release page ID archiver operatiion mutex */
1644 void arch_oper_mutex_exit() { mutex_exit(&m_oper_mutex); }
1645
1646 /* Save information at the time of a reset considered as the reset point.
1647 @param[in] is_durable true if it's durable page tracking
1648 @return true if the reset point information stored in the data block needs to
1649 be flushed to disk before returning to the caller, else false */
1650 bool save_reset_point(bool is_durable);
1651
1652 /** Wait for reset info to be flushed to disk.
1653 @param[in] request_block block number until which blocks need to be
1654 flushed
1655 @return true if flushed, else false */
1656 bool wait_for_reset_info_flush(uint64_t request_block);
1657
1658 /** Get the group which has tracked pages between the start_id and stop_id.
1659 @param[in,out] start_id start LSN from which tracked pages are
1660 required; updated to the actual start LSN used for the search
1661 @param[in,out] stop_id stop_lsn until when tracked pages are
1662 required; updated to the actual stop LSN used for the search
1663 @param[out] group group which has the required tracked
1664 pages, else nullptr.
1665 @return error */
1666 int fetch_group_within_lsn_range(lsn_t &start_id, lsn_t &stop_id,
1667 Arch_Group **group);
1668
1669 /** Purge the archived files until the specified purge LSN.
1670 @param[in] purge_lsn purge lsn until where files needs to be purged
1671 @return error code
1672 @retval 0 if purge was successful */
1673 uint purge(lsn_t *purge_lsn);
1674
1675 /** Update the stop point in all the required structures.
1676 @param[in] cur_blk block which needs to be updated with the stop info */
1677 void update_stop_info(Arch_Block *cur_blk);
1678
1679 /** Fetch the status of the page tracking system.
1680 @param[out] status vector of a pair of (ID, bool) where ID is the
1681 start/stop point and bool is true if the ID is a start point else false */
1682 void get_status(std::vector<std::pair<lsn_t, bool>> &status) {
1683 for (auto group : m_group_list) {
1684 group->get_status(status);
1685 }
1686 }
1687
1688 /** Given start and stop position find number of pages tracked between them
1689 @param[in] start_pos start position
1690 @param[in] stop_pos stop position
1691 @param[out] num_pages number of pages tracked between start and stop
1692 position
1693 @return false if start_pos and stop_pos are invalid else true */
1694 bool get_num_pages(Arch_Page_Pos start_pos, Arch_Page_Pos stop_pos,
1695 uint64_t &num_pages);
1696
1697 /** Get approximate number of tracked pages between two given LSN values.
1698 @param[in,out] start_id fetch archived page Ids from this LSN
1699 @param[in,out] stop_id fetch archived page Ids until this LSN
1700 @param[out] num_pages number of pages tracked between specified
1701 LSN range
1702 @return error code */
1703 int get_num_pages(lsn_t &start_id, lsn_t &stop_id, uint64_t *num_pages);
1704
1705 /** Get page IDs from a specific position.
1706 Caller must ensure that read_len doesn't exceed the block.
1707 @param[in] group group whose pages we're interested in
1708 @param[in] read_pos position in archived data
1709 @param[in] read_len amount of data to read
1710 @param[out] read_buff buffer to return the page IDs.
1711 @note Caller must allocate the buffer.
1712 @return true if we could successfully read the block. */
1713 bool get_pages(Arch_Group *group, Arch_Page_Pos *read_pos, uint read_len,
1714 byte *read_buff);
1715
1716 /** Get archived page Ids between two given LSN values.
1717 Attempt to read blocks directly from in memory buffer. If overwritten,
1718 copy from archived files.
1719 @param[in] thd thread handle
1720 @param[in] cbk_func called repeatedly with page ID buffer
1721 @param[in] cbk_ctx callback function context
1722 @param[in,out] start_id fetch archived page Ids from this LSN
1723 @param[in,out] stop_id fetch archived page Ids until this LSN
1724 @param[in] buf buffer to fill page IDs
1725 @param[in] buf_len buffer length in bytes
1726 @return error code */
1727 int get_pages(MYSQL_THD thd, Page_Track_Callback cbk_func, void *cbk_ctx,
1728 lsn_t &start_id, lsn_t &stop_id, byte *buf, uint buf_len);
1729
1730 /** Set the latest stop LSN to the checkpoint LSN at the time it's called. */
1731 void post_recovery_init();
1732
1733 /** Recover the archiver system at the time of startup. Recover information
1734 related to all the durable groups and start archiving if any group was active
1735 at the time of crash/shutdown.
1736 @return error code */
1737 dberr_t recover();
1738
1739 #ifdef UNIV_DEBUG
1740 /** Print information related to the archiver for debugging purposes. */
1741 void print();
1742 #endif
1743
1744 /** Set the state of the archiver system to read only. */
1745 void set_read_only_mode() { m_state = ARCH_STATE_READ_ONLY; }
1746
1747 /** Check if archiver system is in initial state
1748 @return true, if page ID archiver state is #ARCH_STATE_INIT */
1749 bool is_init() const { return (m_state == ARCH_STATE_INIT); }
1750
1751 /** Check if archiver system is active
1752 @return true, if page ID archiver state is #ARCH_STATE_ACTIVE or
1753 #ARCH_STATE_PREPARE_IDLE. */
1754 bool is_active() const {
1755 return (m_state == ARCH_STATE_ACTIVE || m_state == ARCH_STATE_PREPARE_IDLE);
1756 }
1757
1758 /** @return true if in abort state */
1759 bool is_abort() const { return (m_state == ARCH_STATE_ABORT); }
1760
1761 /** Get the mutex protecting concurrent start, stop operations required
1762 for initialising group during recovery.
1763 @return mutex */
1764 ib_mutex_t *get_mutex() { return (&m_mutex); }
1765
1766 /** @return operation mutex */
1767 ib_mutex_t *get_oper_mutex() { return (&m_oper_mutex); }
1768
1769 /** Fetch the system client context.
1770 @return system client context. */
1771 Page_Arch_Client_Ctx *get_sys_client() const { return (m_ctx); }
1772
1773 /** @return the latest stop LSN */
1774 lsn_t get_latest_stop_lsn() const { return (m_latest_stop_lsn); }
1775
1776 /** Disable copy construction */
1777 Arch_Page_Sys(Arch_Page_Sys const &) = delete;
1778
1779 /** Disable assignment */
1780 Arch_Page_Sys &operator=(Arch_Page_Sys const &) = delete;
1781
1782 private:
1783 class Recovery;
1784
1785 /** Wait for archive system to come out of #ARCH_STATE_PREPARE_IDLE.
1786 If the system is preparing to idle, #start needs to wait
1787 for it to come to idle state.
1788 @return true, if successful
1789 false, if needs to abort */
1790 bool wait_idle();
1791
1792 /** Check if the gap from last reset is short.
1793 If not many page IDs are added till last reset, we avoid
1794 taking a new reset point
1795 @return true, if the gap is small. */
1796 bool is_gap_small();
1797
1798 /** Enable tracking pages in all buffer pools.
1799 @param[in] tracking_lsn track pages from this LSN */
1800 void set_tracking_buf_pool(lsn_t tracking_lsn);
1801
1802 /** Track pages for which IO is already started. */
1803 void track_initial_pages();
1804
1805 /** Flush the blocks to disk.
1806 @param[out] wait true, if no more data to archive
1807 @return error code */
1808 dberr_t flush_blocks(bool *wait);
1809
1810 /** Flush all the blocks which are ready to be flushed but not flushed.
1811 @param[out] cur_pos position of block which needs to be flushed
1812 @param[in] end_pos position of block until which the blocks need to
1813 be flushed
1814 @return error code */
1815 dberr_t flush_inactive_blocks(Arch_Page_Pos &cur_pos, Arch_Page_Pos end_pos);
1816
1817 /** Do a partial flush of the current active block
1818 @param[in] cur_pos position of block which needs to be flushed
1819 @param[in] partial_reset_block_flush true if reset block needs to be
1820 flushed
1821 @return error code */
1822 dberr_t flush_active_block(Arch_Page_Pos cur_pos,
1823 bool partial_reset_block_flush);
1824
1825 private:
1826 /** Mutex protecting concurrent start, stop operations */
1827 ib_mutex_t m_mutex;
1828
1829 /** Archiver system state. */
1830 Arch_State m_state{ARCH_STATE_INIT};
1831
1832 /** List of log archive groups */
1833 Arch_Grp_List m_group_list{};
1834
1835 /** Position where last client started archiving */
1836 Arch_Page_Pos m_last_pos{};
1837
1838 /** LSN when last client started archiving */
1839 lsn_t m_last_lsn{LSN_MAX};
1840
1841 /** Latest LSN until where the tracked pages have been flushed. */
1842 lsn_t m_latest_stop_lsn{LSN_MAX};
1843
1844 /** LSN until where the groups are purged. */
1845 lsn_t m_latest_purged_lsn{LSN_MAX};
1846
1847 /** Mutex protecting concurrent operation on data */
1848 ib_mutex_t m_oper_mutex;
1849
1850 /** Current archive group */
1851 Arch_Group *m_current_group{nullptr};
1852
1853 /** In memory data buffer */
1854 ArchPageData m_data{};
1855
1856 /** Position to add new page ID */
1857 Arch_Page_Pos m_write_pos{};
1858
1859 /** Position to add new reset element */
1860 Arch_Page_Pos m_reset_pos{};
1861
1862 /** Position set to explicitly request the flush archiver to flush until
1863 this position.
1864 @note this is always increasing and is only updated by the requester thread
1865 like checkpoint */
1866 Arch_Page_Pos m_request_flush_pos{};
1867
1868 /** Block number set to explicitly request the flush archiver to partially
1869 flush the current active block with reset LSN.
1870 @note this is always increasing and is only updated by the requester thread
1871 like checkpoint */
1872 uint64_t m_request_blk_num_with_lsn{std::numeric_limits<uint64_t>::max()};
1873
1874 /** Block number set once the flush archiver partially flushes the current
1875 active block with reset LSN.
1876 @note this is always increasing and is only updated by the requester thread
1877 like checkpoint */
1878 uint64_t m_flush_blk_num_with_lsn{std::numeric_limits<uint64_t>::max()};
1879
1880 /** Position for start flushing
1881 @note this is always increasing and is only updated by the page archiver
1882 thread */
1883 Arch_Page_Pos m_flush_pos{};
1884
1885 /** The index of the file the last reset belonged to. */
1886 uint m_last_reset_file_index{0};
1887
1888 /** System client. */
1889 Page_Arch_Client_Ctx *m_ctx;
1890 };
1891
1892 /** Redo log archiver system global */
1893 extern Arch_Log_Sys *arch_log_sys;
1894
1895 /** Dirty page ID archiver system global */
1896 extern Arch_Page_Sys *arch_page_sys;
1897
1898 #endif /* ARCH_ARCH_INCLUDE */