Debian Codegraph @

storage/innobase/arch/arch0page.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2017, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file arch/arch0page.cc
29 Innodb implementation for page archive
30
31 *******************************************************/
32
33 #include "arch0page.h"
34 #include "arch0recv.h"
35 #include "clone0clone.h"
36 #include "log0buf.h"
37 #include "log0chkp.h"
38 #include "srv0start.h"
39
40 #ifdef UNIV_DEBUG
41 /** Archived page file default size in number of blocks. */
42 uint ARCH_PAGE_FILE_CAPACITY =
43 (ARCH_PAGE_BLK_SIZE - ARCH_PAGE_BLK_HEADER_LENGTH) / ARCH_BLK_PAGE_ID_SIZE;
44
45 /** Archived page data file size (without header) in number of blocks. */
46 uint ARCH_PAGE_FILE_DATA_CAPACITY =
47 ARCH_PAGE_FILE_CAPACITY - ARCH_PAGE_FILE_NUM_RESET_PAGE;
48 #endif
49
50 /** Event to signal the page archiver thread. */
51 os_event_t page_archiver_thread_event;
52
53 /** Archiver background thread */
54 void page_archiver_thread() {
55 bool page_wait = false;
56
57 Arch_Group::init_dblwr_file_ctx(
58 ARCH_DBLWR_DIR, ARCH_DBLWR_FILE, ARCH_DBLWR_NUM_FILES,
59 static_cast<uint64_t>(ARCH_PAGE_BLK_SIZE) * ARCH_DBLWR_FILE_CAPACITY);
60
61 while (true) {
62 /* Archive in memory data blocks to disk. */
63 auto page_abort = arch_page_sys->archive(&page_wait);
64
65 if (page_abort) {
66 ib::info(ER_IB_MSG_14) << "Exiting Page Archiver";
67 break;
68 }
69
70 if (page_wait) {
71 /* Nothing to archive. Wait until next trigger. */
72 os_event_wait(page_archiver_thread_event);
73 os_event_reset(page_archiver_thread_event);
74 }
75 }
76 }
77
78 void Arch_Reset_File::init() {
79 m_file_index = 0;
80 m_lsn = LSN_MAX;
81 m_start_point.clear();
82 }
83
84 Arch_File_Ctx Arch_Group::s_dblwr_file_ctx;
85
86 Arch_Group::~Arch_Group() {
87 ut_ad(!m_is_active);
88
89 m_file_ctx.close();
90
91 if (m_active_file.m_file != OS_FILE_CLOSED) {
92 os_file_close(m_active_file);
93 }
94
95 if (m_durable_file.m_file != OS_FILE_CLOSED) {
96 os_file_close(m_durable_file);
97 }
98
99 if (m_active_file_name != nullptr) {
100 ut::free(m_active_file_name);
101 }
102
103 if (m_durable_file_name != nullptr) {
104 ut::free(m_durable_file_name);
105 }
106
107 if (!is_durable()) {
108 m_file_ctx.delete_files(m_begin_lsn);
109 }
110 }
111
112 dberr_t Arch_Group::write_to_doublewrite_file(Arch_File_Ctx *from_file,
113 byte *from_buffer,
114 uint write_size,
115 Arch_Page_Dblwr_Offset offset) {
116 dberr_t err = DB_SUCCESS;
117
118 ut_ad(!s_dblwr_file_ctx.is_closed());
119
120 switch (offset) {
121 case ARCH_PAGE_DBLWR_RESET_PAGE:
122 DBUG_EXECUTE_IF("crash_before_reset_block_dblwr_flush", DBUG_SUICIDE(););
123 break;
124
125 case ARCH_PAGE_DBLWR_PARTIAL_FLUSH_PAGE:
126 DBUG_EXECUTE_IF("crash_before_partial_block_dblwr_flush",
127 DBUG_SUICIDE(););
128 break;
129
130 case ARCH_PAGE_DBLWR_FULL_FLUSH_PAGE:
131 DBUG_EXECUTE_IF("crash_before_full_block_dblwr_flush", DBUG_SUICIDE(););
132 break;
133 }
134
135 err = s_dblwr_file_ctx.write(from_file, from_buffer,
136 offset * ARCH_PAGE_BLK_SIZE, write_size);
137
138 if (err != DB_SUCCESS) {
139 return (err);
140 }
141
142 s_dblwr_file_ctx.flush();
143
144 return (err);
145 }
146
147 dberr_t Arch_Group::init_dblwr_file_ctx(const char *path, const char *base_file,
148 uint num_files, uint64_t file_size) {
149 auto err = s_dblwr_file_ctx.init(ARCH_DIR, path, base_file, num_files);
150
151 if (err != DB_SUCCESS) {
152 ut_ad(s_dblwr_file_ctx.get_phy_size() == file_size);
153 return (err);
154 }
155
156 err = s_dblwr_file_ctx.open(false, LSN_MAX, 0, 0, 0);
157
158 if (err != DB_SUCCESS) {
159 return (err);
160 }
161
162 return s_dblwr_file_ctx.resize_and_overwrite_with_zeros(file_size);
163 }
164
165 dberr_t Arch_Group::build_active_file_name() {
166 char dir_name[MAX_ARCH_DIR_NAME_LEN];
167 auto length = MAX_ARCH_DIR_NAME_LEN + 1 + MAX_ARCH_PAGE_FILE_NAME_LEN + 1;
168
169 if (m_active_file_name != nullptr) {
170 return (DB_SUCCESS);
171 }
172
173 m_active_file_name = static_cast<char *>(
174 ut::malloc_withkey(ut::make_psi_memory_key(mem_key_archive), length));
175
176 if (m_active_file_name == nullptr) {
177 return (DB_OUT_OF_MEMORY);
178 }
179
180 get_dir_name(dir_name, MAX_ARCH_DIR_NAME_LEN);
181
182 snprintf(m_active_file_name, length, "%s%c%s", dir_name, OS_PATH_SEPARATOR,
183 ARCH_PAGE_GROUP_ACTIVE_FILE_NAME);
184
185 return (DB_SUCCESS);
186 }
187
188 dberr_t Arch_Group::build_durable_file_name() {
189 char dir_name[MAX_ARCH_DIR_NAME_LEN];
190 auto length = MAX_ARCH_DIR_NAME_LEN + 1 + MAX_ARCH_PAGE_FILE_NAME_LEN + 1;
191
192 if (m_durable_file_name != nullptr) {
193 return (DB_SUCCESS);
194 }
195
196 m_durable_file_name = static_cast<char *>(
197 ut::malloc_withkey(ut::make_psi_memory_key(mem_key_archive), length));
198
199 if (m_durable_file_name == nullptr) {
200 return (DB_OUT_OF_MEMORY);
201 }
202
203 get_dir_name(dir_name, MAX_ARCH_DIR_NAME_LEN);
204
205 snprintf(m_durable_file_name, length, "%s%c%s", dir_name, OS_PATH_SEPARATOR,
206 ARCH_PAGE_GROUP_DURABLE_FILE_NAME);
207
208 return (DB_SUCCESS);
209 }
210
211 int Arch_Group::mark_active() {
212 dberr_t db_err = build_active_file_name();
213
214 if (db_err != DB_SUCCESS) {
215 return (ER_OUTOFMEMORY);
216 }
217
218 os_file_create_t option;
219 os_file_type_t type;
220
221 bool success;
222 bool exists;
223
224 success = os_file_status(m_active_file_name, &exists, &type);
225
226 if (!success) {
227 return (ER_CANT_OPEN_FILE);
228 }
229
230 ut_ad(!exists);
231 option = OS_FILE_CREATE_PATH;
232
233 ut_ad(m_active_file.m_file == OS_FILE_CLOSED);
234
235 m_active_file = os_file_create(innodb_arch_file_key, m_active_file_name,
236 option, OS_CLONE_LOG_FILE, false, &success);
237
238 int err = (success ? 0 : ER_CANT_OPEN_FILE);
239
240 return (err);
241 }
242
243 int Arch_Group::mark_durable() {
244 dberr_t db_err = build_durable_file_name();
245
246 if (db_err != DB_SUCCESS) {
247 return (ER_OUTOFMEMORY);
248 }
249
250 os_file_create_t option;
251 os_file_type_t type;
252
253 bool success;
254 bool exists;
255
256 success = os_file_status(m_durable_file_name, &exists, &type);
257
258 if (exists) {
259 return (0);
260 }
261
262 if (!success) {
263 return (ER_CANT_OPEN_FILE);
264 }
265
266 option = OS_FILE_CREATE_PATH;
267
268 ut_ad(m_durable_file.m_file == OS_FILE_CLOSED);
269
270 m_durable_file = os_file_create(innodb_arch_file_key, m_durable_file_name,
271 option, OS_CLONE_LOG_FILE, false, &success);
272
273 int err = (success ? 0 : ER_CANT_OPEN_FILE);
274
275 return (err);
276 }
277
278 int Arch_Group::mark_inactive() {
279 os_file_type_t type;
280
281 bool success;
282 bool exists;
283
284 dberr_t db_err;
285
286 db_err = build_active_file_name();
287
288 if (db_err != DB_SUCCESS) {
289 return (ER_OUTOFMEMORY);
290 }
291
292 success = os_file_status(m_active_file_name, &exists, &type);
293
294 if (!success) {
295 return (ER_CANT_OPEN_FILE);
296 }
297
298 if (!exists) {
299 return (0);
300 }
301
302 if (m_active_file.m_file != OS_FILE_CLOSED) {
303 os_file_close(m_active_file);
304 m_active_file.m_file = OS_FILE_CLOSED;
305 }
306
307 success = os_file_delete(innodb_arch_file_key, m_active_file_name);
308
309 int err = (success ? 0 : ER_CANT_OPEN_FILE);
310
311 return (err);
312 }
313
314 dberr_t Arch_Group::write_file_header(byte *from_buffer, uint length) {
315 dberr_t err;
316
317 ut_ad(!m_file_ctx.is_closed());
318
319 /* Write to the doublewrite buffer before writing to the actual file */
320 Arch_Group::write_to_doublewrite_file(nullptr, from_buffer, length,
321 ARCH_PAGE_DBLWR_RESET_PAGE);
322
323 DBUG_EXECUTE_IF("crash_after_reset_block_dblwr_flush", DBUG_SUICIDE(););
324
325 err = m_file_ctx.write(nullptr, from_buffer, 0, length);
326
327 if (err == DB_SUCCESS) {
328 /* Flush the file to make sure the changes are made persistent as there
329 would be no way to recover the data otherwise in case of a crash. */
330 m_file_ctx.flush();
331 }
332
333 return (err);
334 }
335
336 dberr_t Arch_Group::open_file(Arch_Page_Pos write_pos, bool create_new) {
337 dberr_t err;
338
339 ut_d(auto count = get_file_count());
340 ut_ad(count > 0);
341
342 ut_a(m_file_size == ARCH_PAGE_BLK_SIZE * ARCH_PAGE_FILE_CAPACITY);
343
344 if (!create_new) {
345 auto block_num = write_pos.m_block_num;
346 uint file_index = Arch_Block::get_file_index(block_num, ARCH_DATA_BLOCK);
347 uint offset = Arch_Block::get_file_offset(block_num, ARCH_DATA_BLOCK);
348
349 ut_ad(file_index + 1 == count);
350
351 err = m_file_ctx.open(false, m_begin_lsn, file_index, offset, m_file_size);
352 } else {
353 err = m_file_ctx.open_new(m_begin_lsn, m_file_size, m_header_len);
354 }
355
356 return err;
357 }
358
359 void Arch_File_Ctx::update_stop_point(uint file_index, lsn_t stop_lsn) {
360 auto last_point_index = m_stop_points.size() - 1;
361
362 if (!m_stop_points.size() || last_point_index != file_index) {
363 m_stop_points.push_back(stop_lsn);
364 } else {
365 m_stop_points[last_point_index] = stop_lsn;
366 }
367 }
368
369 void Arch_File_Ctx::save_reset_point_in_mem(lsn_t lsn, Arch_Page_Pos pos) {
370 uint current_file_index =
371 Arch_Block::get_file_index(pos.m_block_num, ARCH_DATA_BLOCK);
372
373 Arch_Point reset_point;
374 reset_point.lsn = lsn;
375 reset_point.pos = pos;
376
377 Arch_Reset_File reset_file;
378
379 if (m_reset.size()) {
380 reset_file = m_reset.back();
381
382 if (reset_file.m_file_index == current_file_index) {
383 reset_file.m_start_point.push_back(reset_point);
384 m_reset[m_reset.size() - 1] = reset_file;
385 return;
386 }
387 }
388
389 /* Reset info maintained in a new file. */
390 reset_file.init();
391 reset_file.m_file_index = current_file_index;
392 reset_file.m_lsn = lsn;
393 reset_file.m_start_point.push_back(reset_point);
394 m_reset.push_back(reset_file);
395 }
396
397 bool Arch_File_Ctx::find_reset_point(lsn_t check_lsn, Arch_Point &reset_point) {
398 if (!m_reset.size()) {
399 return (false);
400 }
401
402 Arch_Reset_File file_reset_compare;
403 file_reset_compare.m_lsn = check_lsn;
404
405 /* Finds the file which has the element that is >= to check_lsn */
406 auto reset_it = std::lower_bound(
407 m_reset.begin(), m_reset.end(), file_reset_compare,
408 [](const Arch_Reset_File &lhs, const Arch_Reset_File &rhs) {
409 return (lhs.m_lsn < rhs.m_lsn);
410 });
411
412 if (reset_it != m_reset.end() && reset_it->m_lsn == check_lsn) {
413 reset_point = reset_it->m_start_point.front();
414 return (true);
415 }
416
417 if (reset_it == m_reset.begin()) {
418 return (false);
419 }
420
421 /* The element that is less than check_lsn, which we're interested in,
422 will be in the previous position. */
423 --reset_it;
424 ut_ad(reset_it->m_lsn < check_lsn);
425
426 auto reset_file = *reset_it;
427 auto reset_start_point = reset_file.m_start_point;
428
429 Arch_Point reset_point_compare;
430 reset_point_compare.lsn = check_lsn;
431
432 /* Find the first start point whose lsn is >= to check_lsn. */
433 auto reset_point_it = std::lower_bound(
434 reset_start_point.begin(), reset_start_point.end(), reset_point_compare,
435 [](const Arch_Point &lhs, const Arch_Point &rhs) {
436 return (lhs.lsn < rhs.lsn);
437 });
438
439 if (reset_point_it == reset_start_point.end() ||
440 reset_point_it->lsn != check_lsn) {
441 ut_ad(reset_point_it != reset_start_point.begin());
442 --reset_point_it;
443 }
444
445 reset_point = *reset_point_it;
446
447 return (true);
448 }
449
450 dberr_t Arch_File_Ctx::write(Arch_File_Ctx *from_file, byte *from_buffer,
451 uint offset, uint size) {
452 dberr_t err;
453
454 ut_ad(offset + size <= m_size);
455 ut_ad(!is_closed());
456
457 if (from_buffer == nullptr) {
458 ut_ad(offset + size <= from_file->get_size());
459 ut_ad(!from_file->is_closed());
460
461 err = os_file_copy(from_file->m_file, offset, m_file, offset, size);
462 } else {
463 IORequest request(IORequest::WRITE);
464 request.disable_compression();
465 request.clear_encrypted();
466
467 err = os_file_write(request, "Page Track File", m_file, from_buffer, offset,
468 size);
469 }
470
471 return (err);
472 }
473
474 bool Arch_File_Ctx::find_stop_point(Arch_Group *group, lsn_t check_lsn,
475 Arch_Point &stop_point,
476 Arch_Page_Pos last_pos) {
477 stop_point.lsn = LSN_MAX;
478 stop_point.pos.init();
479
480 arch_page_sys->arch_oper_mutex_enter();
481
482 if (!m_stop_points.size()) {
483 arch_page_sys->arch_oper_mutex_exit();
484 return (false);
485 }
486
487 ut_ad(m_stop_points.back() <= arch_page_sys->get_latest_stop_lsn());
488
489 /* 1. Find the file where the block we need to stop at is present */
490
491 uint file_index = 0;
492
493 for (uint i = 0; i < m_stop_points.size(); ++i) {
494 file_index = i;
495
496 if (m_stop_points[i] >= check_lsn) {
497 break;
498 }
499 }
500
501 ut_ad((m_stop_points[file_index] >= check_lsn &&
502 (file_index == 0 || m_stop_points[file_index - 1] < check_lsn)));
503
504 arch_page_sys->arch_oper_mutex_exit();
505
506 /* 2. Find the block in the file where to stop. */
507
508 byte header_buf[ARCH_PAGE_BLK_HEADER_LENGTH];
509
510 Arch_Page_Pos left_pos;
511 left_pos.m_block_num = ARCH_PAGE_FILE_DATA_CAPACITY * file_index;
512
513 Arch_Page_Pos right_pos;
514
515 if (file_index < m_stop_points.size() - 1) {
516 right_pos.m_block_num =
517 left_pos.m_block_num + ARCH_PAGE_FILE_DATA_CAPACITY - 1;
518 } else {
519 right_pos.m_block_num = last_pos.m_block_num;
520 }
521
522 lsn_t block_stop_lsn;
523 int err;
524
525 while (left_pos.m_block_num <= right_pos.m_block_num) {
526 Arch_Page_Pos middle_pos;
527 middle_pos.init();
528 middle_pos.m_offset = 0;
529
530 middle_pos.m_block_num = left_pos.m_block_num +
531 (right_pos.m_block_num - left_pos.m_block_num) / 2;
532
533 /* Read the block header for data length and stop lsn info. */
534 err = group->read_data(middle_pos, header_buf, ARCH_PAGE_BLK_HEADER_LENGTH);
535
536 if (err != 0) {
537 return (false);
538 }
539
540 block_stop_lsn = Arch_Block::get_stop_lsn(header_buf);
541 auto data_len = Arch_Block::get_data_len(header_buf);
542
543 middle_pos.m_offset = data_len + ARCH_PAGE_BLK_HEADER_LENGTH;
544
545 if (block_stop_lsn >= check_lsn) {
546 stop_point.lsn = block_stop_lsn;
547 stop_point.pos = middle_pos;
548 }
549
550 if (left_pos.m_block_num == right_pos.m_block_num ||
551 block_stop_lsn == check_lsn) {
552 break;
553 }
554
555 if (block_stop_lsn > check_lsn) {
556 right_pos.m_block_num = middle_pos.m_block_num - 1;
557 } else {
558 left_pos.m_block_num = middle_pos.m_block_num + 1;
559 }
560 }
561
562 ut_ad(stop_point.lsn != LSN_MAX);
563
564 return (true);
565 }
566
567 #ifdef UNIV_DEBUG
568
569 bool Arch_File_Ctx::validate_stop_point_in_file(Arch_Group *group,
570 pfs_os_file_t file,
571 uint file_index) {
572 lsn_t stop_lsn = LSN_MAX;
573 bool last_file = file_index + 1 == m_count;
574
575 if (last_file && group->is_active() && group->get_end_lsn() == LSN_MAX) {
576 /* Just return true if this is the case as the block might not have been
577 flushed to disk yet */
578 return (true);
579 }
580
581 if (file_index >= m_stop_points.size()) {
582 ut_error;
583 }
584
585 /* Read from file to the user buffer. */
586 IORequest request(IORequest::READ);
587 request.disable_compression();
588 request.clear_encrypted();
589
590 uint64_t offset;
591
592 if (!last_file) {
593 offset = ARCH_PAGE_FILE_DATA_CAPACITY * ARCH_PAGE_BLK_SIZE;
594 } else {
595 offset = Arch_Block::get_file_offset(group->get_stop_pos().m_block_num,
596 ARCH_DATA_BLOCK);
597 }
598
599 byte buf[ARCH_PAGE_BLK_SIZE];
600
601 /* Read the entire reset block. */
602 dberr_t err =
603 os_file_read(request, m_path_name, file, buf, offset, ARCH_PAGE_BLK_SIZE);
604
605 if (err != DB_SUCCESS) {
606 return (false);
607 }
608
609 stop_lsn = Arch_Block::get_stop_lsn(buf);
610
611 if (stop_lsn != m_stop_points[file_index]) {
612 ut_error;
613 }
614
615 DBUG_PRINT("page_archiver", ("File stop point: %" PRIu64 "", stop_lsn));
616
617 return (true);
618 }
619
620 bool Arch_File_Ctx::validate_reset_block_in_file(pfs_os_file_t file,
621 uint file_index,
622 uint &reset_count) {
623 /* Read from file to the user buffer. */
624 IORequest request(IORequest::READ);
625 request.disable_compression();
626 request.clear_encrypted();
627
628 byte buf[ARCH_PAGE_BLK_SIZE];
629
630 /* Read the entire reset block. */
631 dberr_t err =
632 os_file_read(request, m_path_name, file, buf, 0, ARCH_PAGE_BLK_SIZE);
633
634 if (err != DB_SUCCESS) {
635 return (false);
636 }
637
638 auto data_length = Arch_Block::get_data_len(buf);
639
640 if (data_length == 0) {
641 /* No reset, move to the next file. */
642 return (true);
643 }
644
645 ut_ad(data_length >= ARCH_PAGE_FILE_HEADER_RESET_LSN_SIZE +
646 ARCH_PAGE_FILE_HEADER_RESET_POS_SIZE);
647
648 Arch_Reset_File reset_file;
649
650 if (!m_reset.size() || reset_count >= m_reset.size()) {
651 ut_error;
652 }
653
654 reset_file = m_reset.at(reset_count);
655
656 if (reset_file.m_file_index != file_index) {
657 ut_error;
658 }
659
660 byte *block_data = buf + ARCH_PAGE_BLK_HEADER_LENGTH;
661
662 lsn_t file_reset_lsn = mach_read_from_8(block_data);
663 uint length = ARCH_PAGE_FILE_HEADER_RESET_LSN_SIZE;
664
665 if (reset_file.m_lsn != file_reset_lsn) {
666 ut_error;
667 }
668
669 DBUG_PRINT("page_archiver", ("File lsn : %" PRIu64 "", file_reset_lsn));
670
671 uint index = 0;
672 Arch_Point start_point;
673
674 while (length < data_length) {
675 if (index >= reset_file.m_start_point.size()) {
676 ut_error;
677 }
678
679 start_point = reset_file.m_start_point.at(index);
680
681 uint64_t block_num = mach_read_from_2(block_data + length);
682 length += ARCH_PAGE_FILE_HEADER_RESET_BLOCK_NUM_SIZE;
683
684 uint64_t block_offset = mach_read_from_2(block_data + length);
685 length += ARCH_PAGE_FILE_HEADER_RESET_BLOCK_OFFSET_SIZE;
686
687 if (block_num != start_point.pos.m_block_num ||
688 block_offset != start_point.pos.m_offset) {
689 ut_error;
690 }
691
692 DBUG_PRINT("page_archiver",
693 ("Reset point %u : %" PRIu64 ", %" PRIu64 ", %" PRIu64 "", index,
694 start_point.lsn, block_num, block_offset));
695
696 ++index;
697 }
698
699 ut_ad(length == data_length);
700
701 if (reset_file.m_start_point.size() != index) {
702 ut_error;
703 }
704
705 ++reset_count;
706
707 return (true);
708 }
709
710 bool Arch_Group::validate_info_in_files() {
711 uint reset_count = 0;
712 uint file_count = m_file_ctx.get_count();
713 bool success = true;
714
715 DBUG_PRINT("page_archiver", ("RESET PAGE"));
716
717 for (uint file_index = 0; file_index < file_count; ++file_index) {
718 bool last_file = file_index + 1 == file_count;
719
720 if (last_file && m_file_ctx.get_phy_size() == 0) {
721 success = false;
722 break;
723 }
724
725 success = m_file_ctx.validate(this, file_index, m_begin_lsn, reset_count);
726
727 if (!success) {
728 break;
729 }
730 }
731
732 DBUG_PRINT("page_archiver", ("\n"));
733
734 return (success);
735 }
736
737 bool Arch_File_Ctx::validate(Arch_Group *group, uint file_index,
738 lsn_t start_lsn, uint &reset_count) {
739 char file_name[MAX_ARCH_PAGE_FILE_NAME_LEN];
740
741 build_name(file_index, start_lsn, file_name, MAX_ARCH_PAGE_FILE_NAME_LEN);
742
743 if (!os_file_exists(file_name)) {
744 /* Could be the case if files are purged. */
745 return (true);
746 }
747
748 bool success;
749 pfs_os_file_t file;
750
751 file = os_file_create(innodb_arch_file_key, file_name, OS_FILE_OPEN,
752 OS_CLONE_LOG_FILE, true, &success);
753
754 if (!success) {
755 return (false);
756 }
757
758 DBUG_PRINT("page_archiver", ("File : %u", file_index));
759
760 success = validate_reset_block_in_file(file, file_index, reset_count);
761
762 ut_ad(success);
763 if (!success) {
764 if (file.m_file != OS_FILE_CLOSED) {
765 os_file_close(file);
766 }
767 return (false);
768 }
769
770 success = validate_stop_point_in_file(group, file, file_index);
771
772 if (file.m_file != OS_FILE_CLOSED) {
773 os_file_close(file);
774 }
775
776 if (!success ||
777 (file_index + 1 == m_count && reset_count != m_reset.size())) {
778 ut_error;
779 }
780
781 return (true);
782 }
783
784 #endif /* UNIV_DEBUG */
785
786 lsn_t Arch_File_Ctx::purge(lsn_t begin_lsn, lsn_t end_lsn, lsn_t purge_lsn) {
787 Arch_Point reset_point;
788
789 /* Find reset lsn which is <= purge_lsn. */
790 auto success = find_reset_point(purge_lsn, reset_point);
791
792 if (!success || reset_point.lsn == begin_lsn) {
793 ib::info(ER_IB_MSG_PAGE_ARCH_NO_RESET_POINTS);
794 return (LSN_MAX);
795 }
796
797 ut_ad(begin_lsn < reset_point.lsn && reset_point.lsn <= end_lsn);
798
799 Arch_Reset_File file_reset_compare;
800 file_reset_compare.m_lsn = reset_point.lsn;
801
802 /* Finds the file which has the element that is >= to reset_point.lsn. */
803 auto reset_file_it = std::lower_bound(
804 m_reset.begin(), m_reset.end(), file_reset_compare,
805 [](const Arch_Reset_File &lhs, const Arch_Reset_File &rhs) {
806 return (lhs.m_lsn < rhs.m_lsn);
807 });
808
809 /* The element that is less than check_lsn, which we're interested in,
810 will be in the previous position. */
811 if (reset_file_it != m_reset.begin() &&
812 (reset_file_it == m_reset.end() || reset_file_it->m_lsn != purge_lsn)) {
813 --reset_file_it;
814 }
815
816 if (reset_file_it == m_reset.begin()) {
817 return (LSN_MAX);
818 }
819
820 lsn_t purged_lsn = reset_file_it->m_lsn;
821
822 for (auto it = m_reset.begin(); it != reset_file_it;) {
823 bool success = delete_file(it->m_file_index, begin_lsn);
824
825 if (success) {
826 /** Removes the deleted file from reset info, thereby incrementing the
827 iterator. */
828 it = m_reset.erase(it);
829 } else {
830 purged_lsn = it->m_lsn;
831 reset_file_it = it;
832 ut_d(ut_error);
833 ut_o(break);
834 }
835 }
836
837 /** Only files which have a reset would be purged in the above loop. We want
838 to purge all the files preceding reset_file_it regardless of whether it has
839 a reset or not. */
840 for (uint file_index = 0; file_index < reset_file_it->m_file_index;
841 ++file_index) {
842 delete_file(file_index, begin_lsn);
843 }
844
845 return (purged_lsn);
846 }
847
848 uint Arch_Group::purge(lsn_t purge_lsn, lsn_t &group_purged_lsn) {
849 ut_ad(mutex_own(m_arch_mutex));
850
851 if (m_begin_lsn > purge_lsn) {
852 group_purged_lsn = LSN_MAX;
853 return (0);
854 }
855
856 /** For a group (active or non-active) if there are any non-durable clients
857 attached then we don't purge the group at all. */
858 if (m_ref_count > 0) {
859 group_purged_lsn = LSN_MAX;
860 return (ER_PAGE_TRACKING_CANNOT_PURGE);
861 }
862
863 if (!m_is_active && m_end_lsn <= purge_lsn) {
864 m_file_ctx.delete_files(m_begin_lsn);
865 group_purged_lsn = m_end_lsn;
866 return (0);
867 }
868
869 lsn_t purged_lsn = m_file_ctx.purge(m_begin_lsn, m_end_lsn, purge_lsn);
870
871 group_purged_lsn = purged_lsn;
872
873 return (0);
874 }
875
876 #ifdef UNIV_DEBUG
877 void Page_Arch_Client_Ctx::print() {
878 DBUG_PRINT("page_archiver", ("CLIENT INFO"));
879 DBUG_PRINT("page_archiver", ("Transient Client - %u", !m_is_durable));
880 DBUG_PRINT("page_archiver", ("Start LSN - %" PRIu64 "", m_start_lsn));
881 DBUG_PRINT("page_archiver", ("Stop LSN - %" PRIu64 "", m_stop_lsn));
882 DBUG_PRINT("page_archiver",
883 ("Last Reset LSN - %" PRIu64 "", m_last_reset_lsn));
884 DBUG_PRINT("page_archiver", ("Start pos - %" PRIu64 ", %u",
885 m_start_pos.m_block_num, m_start_pos.m_offset));
886 DBUG_PRINT("page_archiver", ("Stop pos - %" PRIu64 ", %u\n",
887 m_stop_pos.m_block_num, m_stop_pos.m_offset));
888 }
889 #endif
890
891 int Page_Arch_Client_Ctx::start(bool recovery, uint64_t *start_id) {
892 bool reset = false;
893 int err = 0;
894
895 arch_client_mutex_enter();
896
897 switch (m_state) {
898 case ARCH_CLIENT_STATE_STOPPED:
899 if (!m_is_durable) {
900 arch_client_mutex_exit();
901 return (ER_PAGE_TRACKING_NOT_STARTED);
902 }
903 DBUG_PRINT("page_archiver", ("Archiver in progress"));
904 DBUG_PRINT("page_archiver", ("[->] Starting page archiving."));
905 break;
906
907 case ARCH_CLIENT_STATE_INIT:
908 DBUG_PRINT("page_archiver", ("Archiver in progress"));
909 DBUG_PRINT("page_archiver", ("[->] Starting page archiving."));
910 break;
911
912 case ARCH_CLIENT_STATE_STARTED:
913 DBUG_PRINT("page_archiver", ("[->] Resetting page archiving."));
914 ut_ad(m_group != nullptr);
915 reset = true;
916 break;
917
918 default:
919 ut_d(ut_error);
920 }
921
922 /* Start archiving. */
923 err = arch_page_sys->start(&m_group, &m_last_reset_lsn, &m_start_pos,
924 m_is_durable, reset, recovery);
925
926 if (err != 0) {
927 arch_client_mutex_exit();
928 return (err);
929 }
930
931 if (!reset) {
932 m_start_lsn = m_last_reset_lsn;
933 }
934
935 if (start_id != nullptr) {
936 *start_id = m_last_reset_lsn;
937 }
938
939 if (!is_active()) {
940 m_state = ARCH_CLIENT_STATE_STARTED;
941 }
942
943 arch_client_mutex_exit();
944
945 if (!m_is_durable) {
946 /* Update DD table buffer to get rid of recovery dependency for auto INC */
947 dict_persist_to_dd_table_buffer();
948
949 /* Make sure all written pages are synced to disk. */
950 fil_flush_file_spaces();
951
952 ib::info(ER_IB_MSG_20) << "Clone Start PAGE ARCH : start LSN : "
953 << m_start_lsn << ", checkpoint LSN : "
954 << log_get_checkpoint_lsn(*log_sys);
955 }
956
957 return (err);
958 }
959
960 int Page_Arch_Client_Ctx::init_during_recovery(Arch_Group *group,
961 lsn_t last_lsn) {
962 /* Initialise the sys client */
963 m_state = ARCH_CLIENT_STATE_STARTED;
964 m_group = group;
965 m_start_lsn = group->get_begin_lsn();
966 m_last_reset_lsn = last_lsn;
967 m_start_pos.init();
968
969 /* Start page archiving. */
970 int error = start(true, nullptr);
971
972 ut_d(print());
973
974 return (error);
975 }
976
977 int Page_Arch_Client_Ctx::stop(lsn_t *stop_id) {
978 arch_client_mutex_enter();
979
980 if (!is_active()) {
981 arch_client_mutex_exit();
982 ib::error(ER_PAGE_TRACKING_NOT_STARTED);
983 return (ER_PAGE_TRACKING_NOT_STARTED);
984 }
985
986 ut_ad(m_group != nullptr);
987
988 /* Stop archiving. */
989 auto err =
990 arch_page_sys->stop(m_group, &m_stop_lsn, &m_stop_pos, m_is_durable);
991
992 ut_d(print());
993
994 /* We stop the client even in cases of an error. */
995 m_state = ARCH_CLIENT_STATE_STOPPED;
996
997 if (stop_id != nullptr) {
998 *stop_id = m_stop_lsn;
999 }
1000
1001 arch_client_mutex_exit();
1002
1003 ib::info(ER_IB_MSG_21) << "Clone Stop PAGE ARCH : end LSN : " << m_stop_lsn
1004 << ", log sys LSN : " << log_get_lsn(*log_sys);
1005
1006 return (err);
1007 }
1008
1009 int Page_Arch_Client_Ctx::get_pages(Page_Arch_Cbk *cbk_func, void *cbk_ctx,
1010 byte *buff, uint buf_len) {
1011 int err = 0;
1012 uint num_pages;
1013 uint read_len;
1014
1015 arch_client_mutex_enter();
1016
1017 ut_ad(m_state == ARCH_CLIENT_STATE_STOPPED);
1018
1019 auto cur_pos = m_start_pos;
1020
1021 while (true) {
1022 ut_ad(cur_pos.m_block_num <= m_stop_pos.m_block_num);
1023
1024 /* Check if last block */
1025 if (cur_pos.m_block_num >= m_stop_pos.m_block_num) {
1026 if (cur_pos.m_offset > m_stop_pos.m_offset) {
1027 my_error(ER_INTERNAL_ERROR, MYF(0), "Wrong Archiver page offset");
1028 err = ER_INTERNAL_ERROR;
1029 ut_d(ut_error);
1030 ut_o(break);
1031 }
1032
1033 read_len = m_stop_pos.m_offset - cur_pos.m_offset;
1034
1035 if (read_len == 0) {
1036 break;
1037 }
1038
1039 } else {
1040 if (cur_pos.m_offset > ARCH_PAGE_BLK_SIZE) {
1041 my_error(ER_INTERNAL_ERROR, MYF(0), "Wrong Archiver page offset");
1042 err = ER_INTERNAL_ERROR;
1043 ut_d(ut_error);
1044 ut_o(break);
1045 }
1046
1047 read_len = ARCH_PAGE_BLK_SIZE - cur_pos.m_offset;
1048
1049 /* Move to next block. */
1050 if (read_len == 0) {
1051 cur_pos.set_next();
1052 continue;
1053 }
1054 }
1055
1056 if (read_len > buf_len) {
1057 read_len = buf_len;
1058 }
1059
1060 err = m_group->read_data(cur_pos, buff, read_len);
1061
1062 if (err != 0) {
1063 break;
1064 }
1065
1066 cur_pos.m_offset += read_len;
1067 num_pages = read_len / ARCH_BLK_PAGE_ID_SIZE;
1068
1069 err = cbk_func(cbk_ctx, buff, num_pages);
1070
1071 if (err != 0) {
1072 break;
1073 }
1074 }
1075
1076 arch_client_mutex_exit();
1077
1078 return (err);
1079 }
1080
1081 void Page_Arch_Client_Ctx::release() {
1082 arch_client_mutex_enter();
1083
1084 switch (m_state) {
1085 case ARCH_CLIENT_STATE_INIT:
1086 arch_client_mutex_exit();
1087 return;
1088
1089 case ARCH_CLIENT_STATE_STARTED:
1090 arch_client_mutex_exit();
1091 stop(nullptr);
1092 break;
1093
1094 case ARCH_CLIENT_STATE_STOPPED:
1095 break;
1096
1097 default:
1098 ut_d(ut_error);
1099 }
1100
1101 ut_ad(m_group != nullptr);
1102
1103 arch_page_sys->release(m_group, m_is_durable, m_start_pos);
1104
1105 m_state = ARCH_CLIENT_STATE_INIT;
1106 m_group = nullptr;
1107 m_start_lsn = LSN_MAX;
1108 m_stop_lsn = LSN_MAX;
1109 m_last_reset_lsn = LSN_MAX;
1110 m_start_pos.init();
1111 m_stop_pos.init();
1112
1113 arch_client_mutex_exit();
1114 }
1115
1116 bool wait_flush_archiver(Page_Wait_Flush_Archiver_Cbk cbk_func) {
1117 ut_ad(mutex_own(arch_page_sys->get_oper_mutex()));
1118
1119 while (cbk_func()) {
1120 /* Need to wait for flush. We don't expect it
1121 to happen normally. With no duplicate page ID
1122 dirty page growth should be very slow. */
1123 os_event_set(page_archiver_thread_event);
1124
1125 bool is_timeout = false;
1126 int alert_count = 0;
1127
1128 auto err = Clone_Sys::wait_default(
1129 [&](bool alert, bool &result) {
1130 ut_ad(mutex_own(arch_page_sys->get_oper_mutex()));
1131 result = cbk_func();
1132
1133 int err2 = 0;
1134 if (srv_shutdown_state.load() == SRV_SHUTDOWN_LAST_PHASE ||
1135 srv_shutdown_state.load() == SRV_SHUTDOWN_EXIT_THREADS ||
1136 arch_page_sys->is_abort()) {
1137 err2 = ER_QUERY_INTERRUPTED;
1138
1139 } else if (result) {
1140 os_event_set(page_archiver_thread_event);
1141 if (alert && ++alert_count == 12) {
1142 alert_count = 0;
1143 ib::info(ER_IB_MSG_22) << "Clone Page Tracking: waiting "
1144 "for block to flush";
1145 }
1146 }
1147 return (err2);
1148 },
1149 arch_page_sys->get_oper_mutex(), is_timeout);
1150
1151 if (err != 0) {
1152 return (false);
1153
1154 } else if (is_timeout) {
1155 ib::warn(ER_IB_MSG_22) << "Clone Page Tracking: wait for block flush "
1156 "timed out";
1157 ut_d(ut_error);
1158 ut_o(return false);
1159 }
1160 }
1161 return (true);
1162 }
1163
1164 uint Arch_Block::get_file_index(uint64_t block_num, Arch_Blk_Type type) {
1165 size_t file_index = std::numeric_limits<size_t>::max();
1166
1167 switch (type) {
1168 case ARCH_RESET_BLOCK:
1169 file_index = block_num;
1170 break;
1171
1172 case ARCH_DATA_BLOCK:
1173 file_index = block_num / ARCH_PAGE_FILE_DATA_CAPACITY;
1174 break;
1175
1176 default:
1177 ut_d(ut_error);
1178 }
1179
1180 return file_index;
1181 }
1182
1183 Arch_Blk_Type Arch_Block::get_type(byte *block) {
1184 return static_cast<Arch_Blk_Type>(
1185 mach_read_from_1(block + ARCH_PAGE_BLK_HEADER_TYPE_OFFSET));
1186 }
1187
1188 uint Arch_Block::get_data_len(byte *block) {
1189 return (mach_read_from_2(block + ARCH_PAGE_BLK_HEADER_DATA_LEN_OFFSET));
1190 }
1191
1192 lsn_t Arch_Block::get_stop_lsn(byte *block) {
1193 return (mach_read_from_8(block + ARCH_PAGE_BLK_HEADER_STOP_LSN_OFFSET));
1194 }
1195
1196 uint64_t Arch_Block::get_block_number(byte *block) {
1197 return (mach_read_from_8(block + ARCH_PAGE_BLK_HEADER_NUMBER_OFFSET));
1198 }
1199
1200 lsn_t Arch_Block::get_reset_lsn(byte *block) {
1201 return (mach_read_from_8(block + ARCH_PAGE_BLK_HEADER_RESET_LSN_OFFSET));
1202 }
1203
1204 uint32_t Arch_Block::get_checksum(byte *block) {
1205 return (mach_read_from_4(block + ARCH_PAGE_BLK_HEADER_CHECKSUM_OFFSET));
1206 }
1207
1208 uint64_t Arch_Block::get_file_offset(uint64_t block_num, Arch_Blk_Type type) {
1209 uint64_t offset = 0;
1210
1211 switch (type) {
1212 case ARCH_RESET_BLOCK:
1213 offset = 0;
1214 break;
1215
1216 case ARCH_DATA_BLOCK:
1217 offset = block_num % ARCH_PAGE_FILE_DATA_CAPACITY;
1218 offset += ARCH_PAGE_FILE_NUM_RESET_PAGE;
1219 offset *= ARCH_PAGE_BLK_SIZE;
1220 break;
1221
1222 default:
1223 ut_d(ut_error);
1224 }
1225
1226 return offset;
1227 }
1228
1229 bool Arch_Block::validate(byte *block) {
1230 auto data_length = Arch_Block::get_data_len(block);
1231 auto block_checksum = Arch_Block::get_checksum(block);
1232 auto checksum = ut_crc32(block + ARCH_PAGE_BLK_HEADER_LENGTH, data_length);
1233
1234 if (checksum != block_checksum) {
1235 ib::warn(ER_IB_ERR_PAGE_ARCH_INVALID_DOUBLE_WRITE_BUF)
1236 << Arch_Block::get_block_number(block);
1237 ut_d(ut_error);
1238 ut_o(return (false));
1239 } else if (ut::is_zeros(block, ARCH_PAGE_BLK_SIZE)) {
1240 return (false);
1241 }
1242
1243 return (true);
1244 }
1245
1246 void Arch_Block::update_block_header(lsn_t stop_lsn, lsn_t reset_lsn) {
1247 mach_write_to_2(m_data + ARCH_PAGE_BLK_HEADER_DATA_LEN_OFFSET, m_data_len);
1248
1249 if (stop_lsn != LSN_MAX) {
1250 m_stop_lsn = stop_lsn;
1251 mach_write_to_8(m_data + ARCH_PAGE_BLK_HEADER_STOP_LSN_OFFSET, m_stop_lsn);
1252 }
1253
1254 if (reset_lsn != LSN_MAX) {
1255 m_reset_lsn = reset_lsn;
1256 mach_write_to_8(m_data + ARCH_PAGE_BLK_HEADER_RESET_LSN_OFFSET,
1257 m_reset_lsn);
1258 }
1259 }
1260
1261 /** Set the block ready to begin writing page ID
1262 @param[in] pos position to initiate block number */
1263 void Arch_Block::begin_write(Arch_Page_Pos pos) {
1264 m_data_len = 0;
1265
1266 m_state = ARCH_BLOCK_ACTIVE;
1267
1268 m_number =
1269 (m_type == ARCH_DATA_BLOCK
1270 ? pos.m_block_num
1271 : Arch_Block::get_file_index(pos.m_block_num, ARCH_DATA_BLOCK));
1272
1273 m_oldest_lsn = LSN_MAX;
1274 m_reset_lsn = LSN_MAX;
1275
1276 if (m_type == ARCH_DATA_BLOCK) {
1277 arch_page_sys->update_stop_info(this);
1278 }
1279 }
1280
1281 /** End writing to a block.
1282 Change state to #ARCH_BLOCK_READY_TO_FLUSH */
1283 void Arch_Block::end_write() { m_state = ARCH_BLOCK_READY_TO_FLUSH; }
1284
1285 /** Add page ID to current block
1286 @param[in] page page from buffer pool
1287 @param[in] pos Archiver current position
1288 @return true, if successful
1289 false, if no more space in current block */
1290 bool Arch_Block::add_page(buf_page_t *page, Arch_Page_Pos *pos) {
1291 space_id_t space_id;
1292 page_no_t page_num;
1293 byte *data_ptr;
1294
1295 ut_ad(pos->m_offset <= ARCH_PAGE_BLK_SIZE);
1296 ut_ad(m_type == ARCH_DATA_BLOCK);
1297 ut_ad(pos->m_offset == m_data_len + ARCH_PAGE_BLK_HEADER_LENGTH);
1298
1299 if ((pos->m_offset + ARCH_BLK_PAGE_ID_SIZE) > ARCH_PAGE_BLK_SIZE) {
1300 ut_ad(pos->m_offset == ARCH_PAGE_BLK_SIZE);
1301 return (false);
1302 }
1303
1304 data_ptr = m_data + pos->m_offset;
1305
1306 /* Write serialized page ID: tablespace ID and offset */
1307 space_id = page->id.space();
1308 page_num = page->id.page_no();
1309
1310 mach_write_to_4(data_ptr + ARCH_BLK_SPCE_ID_OFFSET, space_id);
1311 mach_write_to_4(data_ptr + ARCH_BLK_PAGE_NO_OFFSET, page_num);
1312
1313 /* Update position. */
1314 pos->m_offset += ARCH_BLK_PAGE_ID_SIZE;
1315 m_data_len += ARCH_BLK_PAGE_ID_SIZE;
1316
1317 /* Update oldest LSN from page. */
1318 if (arch_page_sys->get_latest_stop_lsn() > m_oldest_lsn ||
1319 m_oldest_lsn > page->get_oldest_lsn()) {
1320 m_oldest_lsn = page->get_oldest_lsn();
1321 }
1322
1323 return (true);
1324 }
1325
1326 bool Arch_Block::get_data(Arch_Page_Pos *read_pos, uint read_len,
1327 byte *read_buff) {
1328 ut_ad(read_pos->m_offset + read_len <= m_size);
1329
1330 if (m_state == ARCH_BLOCK_INIT || m_number != read_pos->m_block_num) {
1331 /* The block is already overwritten. */
1332 return (false);
1333 }
1334
1335 byte *src = m_data + read_pos->m_offset;
1336
1337 memcpy(read_buff, src, read_len);
1338
1339 return (true);
1340 }
1341
1342 bool Arch_Block::set_data(uint read_len, byte *read_buff, uint read_offset) {
1343 ut_ad(m_state != ARCH_BLOCK_INIT);
1344 ut_ad(read_offset + read_len <= m_size);
1345
1346 byte *dest = m_data + read_offset;
1347
1348 memcpy(dest, read_buff, read_len);
1349
1350 set_reset_lsn(Arch_Block::get_reset_lsn(m_data));
1351
1352 return true;
1353 }
1354
1355 /** Flush this block to the file group.
1356 @param[in] file_group current archive group
1357 @param[in] type flush type
1358 @return error code. */
1359 dberr_t Arch_Block::flush(Arch_Group *file_group, Arch_Blk_Flush_Type type) {
1360 dberr_t err = DB_SUCCESS;
1361 uint32_t checksum;
1362
1363 checksum = ut_crc32(m_data + ARCH_PAGE_BLK_HEADER_LENGTH, m_data_len);
1364
1365 /* Update block's header. */
1366 mach_write_to_1(m_data + ARCH_PAGE_BLK_HEADER_VERSION_OFFSET,
1367 ARCH_PAGE_FILE_VERSION);
1368 mach_write_to_1(m_data + ARCH_PAGE_BLK_HEADER_TYPE_OFFSET, m_type);
1369 mach_write_to_2(m_data + ARCH_PAGE_BLK_HEADER_DATA_LEN_OFFSET, m_data_len);
1370 mach_write_to_4(m_data + ARCH_PAGE_BLK_HEADER_CHECKSUM_OFFSET, checksum);
1371 mach_write_to_8(m_data + ARCH_PAGE_BLK_HEADER_STOP_LSN_OFFSET, m_stop_lsn);
1372 mach_write_to_8(m_data + ARCH_PAGE_BLK_HEADER_RESET_LSN_OFFSET, m_reset_lsn);
1373 mach_write_to_8(m_data + ARCH_PAGE_BLK_HEADER_NUMBER_OFFSET, m_number);
1374
1375 switch (m_type) {
1376 case ARCH_RESET_BLOCK:
1377 err = file_group->write_file_header(m_data, m_size);
1378 break;
1379
1380 case ARCH_DATA_BLOCK: {
1381 bool is_partial_flush = (type == ARCH_FLUSH_PARTIAL);
1382
1383 /* Callback responsible for setting up file's header starting at offset 0.
1384 This header is left empty within this flush operation. */
1385 auto get_empty_file_header_cbk = [](uint64_t, byte *) {
1386 return DB_SUCCESS;
1387 };
1388
1389 /* We allow partial flush to happen even if there were no pages added
1390 since the last partial flush as the block's header might contain some
1391 useful info required during recovery. */
1392 err = file_group->write_to_file(nullptr, m_data, m_size, is_partial_flush,
1393 true, get_empty_file_header_cbk);
1394 break;
1395 }
1396
1397 default:
1398 ut_d(ut_error);
1399 }
1400
1401 return (err);
1402 }
1403
1404 void Arch_Block::add_reset(lsn_t reset_lsn, Arch_Page_Pos reset_pos) {
1405 ut_ad(m_type == ARCH_RESET_BLOCK);
1406 ut_ad(m_data_len <= ARCH_PAGE_BLK_SIZE);
1407 ut_ad(m_data_len + ARCH_PAGE_FILE_HEADER_RESET_POS_SIZE <=
1408 ARCH_PAGE_BLK_SIZE);
1409
1410 byte *buf = m_data + ARCH_PAGE_BLK_HEADER_LENGTH;
1411
1412 if (m_data_len == 0) {
1413 /* Write file lsn. */
1414
1415 mach_write_to_8(buf, reset_lsn);
1416 m_data_len += ARCH_PAGE_FILE_HEADER_RESET_LSN_SIZE;
1417 }
1418
1419 ut_ad(m_data_len >= ARCH_PAGE_FILE_HEADER_RESET_LSN_SIZE);
1420
1421 mach_write_to_2(buf + m_data_len, reset_pos.m_block_num);
1422 m_data_len += ARCH_PAGE_FILE_HEADER_RESET_BLOCK_NUM_SIZE;
1423
1424 mach_write_to_2(buf + m_data_len, reset_pos.m_offset);
1425 m_data_len += ARCH_PAGE_FILE_HEADER_RESET_BLOCK_OFFSET_SIZE;
1426 }
1427
1428 void Arch_Block::copy_data(const Arch_Block *block) {
1429 m_data_len = block->m_data_len;
1430 m_size = block->m_size;
1431 m_state = block->m_state;
1432 m_number = block->m_number;
1433 m_type = block->m_type;
1434 m_stop_lsn = block->m_stop_lsn;
1435 m_reset_lsn = block->m_reset_lsn;
1436 m_oldest_lsn = block->m_oldest_lsn;
1437 set_data(m_size, block->m_data, 0);
1438 }
1439
1440 /** Initialize a position */
1441 void Arch_Page_Pos::init() {
1442 m_block_num = 0;
1443 m_offset = ARCH_PAGE_BLK_HEADER_LENGTH;
1444 }
1445
1446 /** Position in the beginning of next block */
1447 void Arch_Page_Pos::set_next() {
1448 m_block_num++;
1449 m_offset = ARCH_PAGE_BLK_HEADER_LENGTH;
1450 }
1451
1452 /** Allocate buffer and initialize blocks
1453 @return true, if successful */
1454 bool ArchPageData::init() {
1455 uint alloc_size;
1456 uint index;
1457 byte *mem_ptr;
1458
1459 ut_ad(m_buffer == nullptr);
1460
1461 m_block_size = ARCH_PAGE_BLK_SIZE;
1462 m_num_data_blocks = ARCH_PAGE_NUM_BLKS;
1463
1464 /* block size and number must be in power of 2 */
1465 ut_ad(ut_is_2pow(m_block_size));
1466 ut_ad(ut_is_2pow(m_num_data_blocks));
1467
1468 alloc_size = m_block_size * m_num_data_blocks;
1469
1470 /* For reset block. */
1471 alloc_size += m_block_size;
1472
1473 /* For partial flush block. */
1474 alloc_size += m_block_size;
1475
1476 /* Allocate buffer for memory blocks. */
1477 m_buffer = static_cast<byte *>(ut::aligned_zalloc_withkey(
1478 ut::make_psi_memory_key(mem_key_archive), alloc_size, m_block_size));
1479
1480 if (m_buffer == nullptr) {
1481 return (false);
1482 }
1483 mem_ptr = m_buffer;
1484
1485 Arch_Block *cur_blk;
1486
1487 /* Create memory blocks. */
1488 for (index = 0; index < m_num_data_blocks; index++) {
1489 cur_blk =
1490 ut::new_withkey<Arch_Block>(ut::make_psi_memory_key(mem_key_archive),
1491 mem_ptr, m_block_size, ARCH_DATA_BLOCK);
1492
1493 if (cur_blk == nullptr) {
1494 return (false);
1495 }
1496
1497 m_data_blocks.push_back(cur_blk);
1498 mem_ptr += m_block_size;
1499 }
1500
1501 m_reset_block =
1502 ut::new_withkey<Arch_Block>(ut::make_psi_memory_key(mem_key_archive),
1503 mem_ptr, m_block_size, ARCH_RESET_BLOCK);
1504
1505 if (m_reset_block == nullptr) {
1506 return (false);
1507 }
1508
1509 mem_ptr += m_block_size;
1510
1511 m_partial_flush_block =
1512 ut::new_withkey<Arch_Block>(ut::make_psi_memory_key(mem_key_archive),
1513 mem_ptr, m_block_size, ARCH_DATA_BLOCK);
1514
1515 if (m_partial_flush_block == nullptr) {
1516 return (false);
1517 }
1518
1519 return (true);
1520 }
1521
1522 /** Delete blocks and buffer */
1523 void ArchPageData::clean() {
1524 for (auto &block : m_data_blocks) {
1525 ut::delete_(block);
1526 block = nullptr;
1527 }
1528
1529 if (m_reset_block != nullptr) {
1530 ut::delete_(m_reset_block);
1531 m_reset_block = nullptr;
1532 }
1533
1534 if (m_partial_flush_block != nullptr) {
1535 ut::delete_(m_partial_flush_block);
1536 m_partial_flush_block = nullptr;
1537 }
1538
1539 ut::aligned_free(m_buffer);
1540 }
1541
1542 /** Get the block for a position
1543 @param[in] pos position in page archive sys
1544 @param[in] type block type
1545 @return page archive in memory block */
1546 Arch_Block *ArchPageData::get_block(Arch_Page_Pos *pos, Arch_Blk_Type type) {
1547 switch (type) {
1548 case ARCH_DATA_BLOCK: {
1549 /* index = block_num % m_num_blocks */
1550 ut_ad(ut_is_2pow(m_num_data_blocks));
1551
1552 uint index = pos->m_block_num & (m_num_data_blocks - 1);
1553 return (m_data_blocks[index]);
1554 }
1555
1556 case ARCH_RESET_BLOCK:
1557 return (m_reset_block);
1558
1559 default:
1560 ut_d(ut_error);
1561 }
1562
1563 ut_o(return (nullptr));
1564 }
1565
1566 Arch_Page_Sys::Arch_Page_Sys() {
1567 mutex_create(LATCH_ID_PAGE_ARCH, &m_mutex);
1568 mutex_create(LATCH_ID_PAGE_ARCH_OPER, &m_oper_mutex);
1569
1570 m_ctx = ut::new_withkey<Page_Arch_Client_Ctx>(
1571 ut::make_psi_memory_key(mem_key_archive), true);
1572
1573 DBUG_EXECUTE_IF("page_archiver_simulate_more_archived_files",
1574 ARCH_PAGE_FILE_CAPACITY = 8;
1575 ARCH_PAGE_FILE_DATA_CAPACITY =
1576 ARCH_PAGE_FILE_CAPACITY - ARCH_PAGE_FILE_NUM_RESET_PAGE;);
1577 }
1578
1579 Arch_Page_Sys::~Arch_Page_Sys() {
1580 ut_ad(m_state == ARCH_STATE_INIT || m_state == ARCH_STATE_ABORT ||
1581 m_state == ARCH_STATE_READ_ONLY);
1582 ut_ad(m_current_group == nullptr);
1583
1584 for (auto group : m_group_list) {
1585 ut::delete_(group);
1586 }
1587
1588 Arch_Group::shutdown();
1589
1590 m_data.clean();
1591
1592 ut::delete_(m_ctx);
1593 mutex_free(&m_mutex);
1594 mutex_free(&m_oper_mutex);
1595 }
1596
1597 void Arch_Page_Sys::post_recovery_init() {
1598 if (!is_active()) {
1599 return;
1600 }
1601
1602 arch_oper_mutex_enter();
1603 m_latest_stop_lsn = log_get_checkpoint_lsn(*log_sys);
1604 auto cur_block = m_data.get_block(&m_write_pos, ARCH_DATA_BLOCK);
1605 update_stop_info(cur_block);
1606 arch_oper_mutex_exit();
1607 }
1608
1609 void Arch_Page_Sys::flush_at_checkpoint(lsn_t checkpoint_lsn) {
1610 arch_oper_mutex_enter();
1611
1612 if (!is_active()) {
1613 arch_oper_mutex_exit();
1614 return;
1615 }
1616
1617 lsn_t end_lsn = m_current_group->get_end_lsn();
1618
1619 if (m_write_pos.m_offset == ARCH_PAGE_BLK_HEADER_LENGTH) {
1620 arch_oper_mutex_exit();
1621 return;
1622 }
1623
1624 Arch_Page_Pos request_flush_pos;
1625
1626 if (end_lsn == LSN_MAX) {
1627 Arch_Block *cur_block = m_data.get_block(&m_write_pos, ARCH_DATA_BLOCK);
1628
1629 ut_ad(cur_block->get_state() == ARCH_BLOCK_ACTIVE);
1630
1631 m_latest_stop_lsn = checkpoint_lsn;
1632 update_stop_info(cur_block);
1633
1634 if (cur_block->get_oldest_lsn() != LSN_MAX &&
1635 cur_block->get_oldest_lsn() <= checkpoint_lsn) {
1636 /* If the oldest modified page in the block added since the last
1637 checkpoint was modified before the checkpoint_lsn then the block needs to
1638 be flushed*/
1639
1640 request_flush_pos = m_write_pos;
1641 } else {
1642 /* Wait for blocks that are not active to be flushed. */
1643
1644 if (m_write_pos.m_block_num == 0) {
1645 arch_oper_mutex_exit();
1646 return;
1647 }
1648
1649 request_flush_pos.init();
1650 request_flush_pos.m_block_num = m_write_pos.m_block_num - 1;
1651 }
1652
1653 if (request_flush_pos < m_flush_pos) {
1654 arch_oper_mutex_exit();
1655 return;
1656 }
1657
1658 if (m_request_flush_pos < request_flush_pos) {
1659 m_request_flush_pos = request_flush_pos;
1660 }
1661
1662 } else {
1663 request_flush_pos = m_current_group->get_stop_pos();
1664 m_request_flush_pos = request_flush_pos;
1665 }
1666
1667 if (request_flush_pos.m_block_num == m_write_pos.m_block_num) {
1668 MONITOR_INC(MONITOR_PAGE_TRACK_CHECKPOINT_PARTIAL_FLUSH_REQUEST);
1669 }
1670
1671 /* We need to ensure that blocks are flushed until request_flush_pos */
1672 auto cbk = [&] { return (request_flush_pos < m_flush_pos ? false : true); };
1673
1674 if (!wait_flush_archiver(cbk)) {
1675 ib::warn(ER_IB_WRN_PAGE_ARCH_FLUSH_DATA);
1676 }
1677
1678 arch_oper_mutex_exit();
1679 }
1680
1681 /** Check and add page ID to archived data.
1682 Check for duplicate page.
1683 @param[in] bpage page to track
1684 @param[in] track_lsn LSN when tracking started
1685 @param[in] frame_lsn current LSN of the page
1686 @param[in] force if true, add page ID without check */
1687 void Arch_Page_Sys::track_page(buf_page_t *bpage, lsn_t track_lsn,
1688 lsn_t frame_lsn, bool force) {
1689 Arch_Block *cur_blk;
1690 uint count = 0;
1691
1692 if (!force) {
1693 /* If the frame LSN is bigger than track LSN, it
1694 is already added to tracking list. */
1695 if (frame_lsn > track_lsn) {
1696 return;
1697 }
1698 }
1699
1700 /* We need to track this page. */
1701 arch_oper_mutex_enter();
1702
1703 while (true) {
1704 if (m_state != ARCH_STATE_ACTIVE) {
1705 break;
1706 }
1707
1708 /* Can possibly loop only two times. */
1709 if (count >= 2) {
1710 if (srv_shutdown_state.load() >= SRV_SHUTDOWN_CLEANUP) {
1711 arch_oper_mutex_exit();
1712 return;
1713 }
1714
1715 ib::warn(ER_IB_MSG_23) << "Fail to add page for tracking."
1716 << " Space ID: " << bpage->id.space();
1717
1718 m_state = ARCH_STATE_ABORT;
1719 arch_oper_mutex_exit();
1720 ut_d(ut_error);
1721 ut_o(return);
1722 }
1723
1724 cur_blk = m_data.get_block(&m_write_pos, ARCH_DATA_BLOCK);
1725
1726 if (cur_blk->get_state() == ARCH_BLOCK_ACTIVE) {
1727 if (cur_blk->add_page(bpage, &m_write_pos)) {
1728 /* page added successfully. */
1729 break;
1730 }
1731
1732 /* Current block is full. Move to next block. */
1733 cur_blk->end_write();
1734
1735 m_write_pos.set_next();
1736
1737 /* Writing to a new file so move to the next reset block. */
1738 if (m_write_pos.m_block_num % ARCH_PAGE_FILE_DATA_CAPACITY == 0) {
1739 Arch_Block *reset_block =
1740 m_data.get_block(&m_reset_pos, ARCH_RESET_BLOCK);
1741 reset_block->end_write();
1742
1743 m_reset_pos.set_next();
1744 }
1745
1746 os_event_set(page_archiver_thread_event);
1747
1748 ++count;
1749 continue;
1750
1751 } else if (cur_blk->get_state() == ARCH_BLOCK_INIT ||
1752 cur_blk->get_state() == ARCH_BLOCK_FLUSHED) {
1753 ut_ad(m_write_pos.m_offset == ARCH_PAGE_BLK_HEADER_LENGTH);
1754
1755 cur_blk->begin_write(m_write_pos);
1756
1757 if (!cur_blk->add_page(bpage, &m_write_pos)) {
1758 /* Should always succeed. */
1759 ut_d(ut_error);
1760 }
1761
1762 /* page added successfully. */
1763 break;
1764
1765 } else {
1766 bool success;
1767
1768 ut_a(cur_blk->get_state() == ARCH_BLOCK_READY_TO_FLUSH);
1769
1770 auto cbk = std::bind(&Arch_Block::is_flushable, *cur_blk);
1771
1772 /* Might release operation mutex temporarily. Need to
1773 loop again verifying the state. */
1774 success = wait_flush_archiver(cbk);
1775 count = success ? 0 : 2;
1776
1777 continue;
1778 }
1779 }
1780 arch_oper_mutex_exit();
1781 }
1782
1783 /** Get page IDs from a specific position.
1784 Caller must ensure that read_len doesn't exceed the block.
1785 @param[in] group group whose pages we're interested in
1786 @param[in] read_pos position in archived data
1787 @param[in] read_len amount of data to read
1788 @param[out] read_buff buffer to return the page IDs.
1789 @note Caller must allocate the buffer.
1790 @return true if we could successfully read the block. */
1791 bool Arch_Page_Sys::get_pages(Arch_Group *group, Arch_Page_Pos *read_pos,
1792 uint read_len, byte *read_buff) {
1793 Arch_Block *read_blk;
1794 bool success;
1795
1796 arch_oper_mutex_enter();
1797
1798 if (group != m_current_group) {
1799 arch_oper_mutex_exit();
1800 return (false);
1801 }
1802
1803 /* Get the block to read from. */
1804 read_blk = m_data.get_block(read_pos, ARCH_DATA_BLOCK);
1805
1806 read_blk->update_block_header(LSN_MAX, LSN_MAX);
1807
1808 /* Read from the block. */
1809 success = read_blk->get_data(read_pos, read_len, read_buff);
1810
1811 arch_oper_mutex_exit();
1812
1813 return (success);
1814 }
1815
1816 int Arch_Page_Sys::get_pages(MYSQL_THD thd, Page_Track_Callback cbk_func,
1817 void *cbk_ctx, lsn_t &start_id, lsn_t &stop_id,
1818 byte *buf, uint buf_len) {
1819 DBUG_PRINT("page_archiver", ("Fetch pages"));
1820
1821 arch_mutex_enter();
1822
1823 if (m_state == ARCH_STATE_READ_ONLY) {
1824 arch_mutex_exit();
1825 return (0);
1826 }
1827
1828 /** 1. Get appropriate LSN range. */
1829 Arch_Group *group = nullptr;
1830
1831 int error = fetch_group_within_lsn_range(start_id, stop_id, &group);
1832
1833 DBUG_PRINT("page_archiver", ("Start id: %" PRIu64 ", stop id: %" PRIu64 "",
1834 start_id, stop_id));
1835
1836 if (error != 0) {
1837 arch_mutex_exit();
1838 return (error);
1839 }
1840
1841 ut_ad(group != nullptr);
1842
1843 /** 2. Get block position from where to start. */
1844
1845 Arch_Page_Pos start_pos;
1846 Arch_Point reset_point;
1847
1848 auto success = group->find_reset_point(start_id, reset_point);
1849 start_pos = reset_point.pos;
1850 start_id = reset_point.lsn;
1851
1852 if (!success) {
1853 arch_mutex_exit();
1854 DBUG_PRINT("page_archiver",
1855 ("Can't fetch pages - No matching reset point."));
1856 return (ER_PAGE_TRACKING_RANGE_NOT_TRACKED);
1857 }
1858
1859 /* 3. Fetch tracked pages. */
1860
1861 DBUG_PRINT("page_archiver",
1862 ("Trying to get pages between %" PRIu64 " to %" PRIu64 "",
1863 start_id, stop_id));
1864
1865 byte header_buf[ARCH_PAGE_BLK_HEADER_LENGTH];
1866
1867 int err = 0;
1868 auto cur_pos = start_pos;
1869 Arch_Page_Pos temp_pos;
1870 uint num_pages;
1871 bool new_block = true;
1872 bool last_block = false;
1873 lsn_t block_stop_lsn = LSN_MAX;
1874 uint read_len = 0;
1875 uint bytes_left = 0;
1876
1877 arch_oper_mutex_enter();
1878
1879 auto end_lsn = group->get_end_lsn();
1880
1881 Arch_Page_Pos last_pos =
1882 (end_lsn == LSN_MAX) ? m_write_pos : group->get_stop_pos();
1883 arch_oper_mutex_exit();
1884
1885 while (true) {
1886 if (new_block) {
1887 temp_pos.m_block_num = cur_pos.m_block_num;
1888 temp_pos.m_offset = 0;
1889
1890 /* Read the block header for data length and stop lsn info. */
1891 err = group->read_data(temp_pos, header_buf, ARCH_PAGE_BLK_HEADER_LENGTH);
1892
1893 if (err != 0) {
1894 break;
1895 }
1896
1897 block_stop_lsn = Arch_Block::get_stop_lsn(header_buf);
1898 auto data_len = Arch_Block::get_data_len(header_buf);
1899 bytes_left = data_len + ARCH_PAGE_BLK_HEADER_LENGTH;
1900
1901 ut_ad(bytes_left <= ARCH_PAGE_BLK_SIZE);
1902 ut_ad(block_stop_lsn != LSN_MAX);
1903
1904 bytes_left -= cur_pos.m_offset;
1905
1906 if (data_len == 0 || cur_pos.m_block_num == last_pos.m_block_num ||
1907 block_stop_lsn > stop_id) {
1908 ut_ad(block_stop_lsn >= stop_id);
1909 stop_id = block_stop_lsn;
1910 last_block = true;
1911 }
1912
1913 DBUG_PRINT("page_archiver",
1914 ("%" PRIu64 " -> length : %u, stop lsn : %" PRIu64
1915 ", last block : %u",
1916 cur_pos.m_block_num, data_len, block_stop_lsn, last_block));
1917 }
1918
1919 ut_ad(cur_pos.m_offset <= ARCH_PAGE_BLK_SIZE);
1920
1921 /* Read how much ever is left to be read in the block. */
1922 read_len = bytes_left;
1923
1924 if (last_block && read_len == 0) {
1925 /* There is nothing to read. */
1926 break;
1927 }
1928
1929 if (read_len > buf_len) {
1930 read_len = buf_len;
1931 }
1932
1933 /* Read the block for list of pages */
1934 err = group->read_data(cur_pos, buf, read_len);
1935
1936 if (err != 0) {
1937 break;
1938 }
1939
1940 cur_pos.m_offset += read_len;
1941 bytes_left -= read_len;
1942 num_pages = read_len / ARCH_BLK_PAGE_ID_SIZE;
1943
1944 err = cbk_func(thd, buf, buf_len, num_pages, cbk_ctx);
1945
1946 if (err != 0) {
1947 break;
1948 }
1949
1950 if (bytes_left == 0) {
1951 /* We have read all the pages in the block. */
1952
1953 if (last_block) {
1954 break;
1955 } else {
1956 new_block = true;
1957 bytes_left = 0;
1958 read_len = 0;
1959 cur_pos.set_next();
1960 continue;
1961 }
1962 } else {
1963 /* We still have some bytes to read from the current block. */
1964 new_block = false;
1965 }
1966 }
1967
1968 arch_mutex_exit();
1969
1970 return (0);
1971 }
1972
1973 bool Arch_Page_Sys::get_num_pages(Arch_Page_Pos start_pos,
1974 Arch_Page_Pos stop_pos, uint64_t &num_pages) {
1975 if (start_pos.m_block_num > stop_pos.m_block_num ||
1976 ((start_pos.m_block_num == stop_pos.m_block_num) &&
1977 (start_pos.m_offset >= stop_pos.m_offset))) {
1978 return (false);
1979 }
1980
1981 uint length = 0;
1982
1983 if (start_pos.m_block_num != stop_pos.m_block_num) {
1984 length = ARCH_PAGE_BLK_SIZE - start_pos.m_offset;
1985 length += stop_pos.m_offset - ARCH_PAGE_BLK_HEADER_LENGTH;
1986
1987 uint64_t num_blocks;
1988 num_blocks = stop_pos.m_block_num - start_pos.m_block_num - 1;
1989 length += num_blocks * (ARCH_PAGE_BLK_SIZE - ARCH_PAGE_BLK_HEADER_LENGTH);
1990
1991 } else {
1992 length = stop_pos.m_offset - start_pos.m_offset;
1993 }
1994
1995 num_pages = length / ARCH_BLK_PAGE_ID_SIZE;
1996
1997 return (true);
1998 }
1999
2000 int Arch_Page_Sys::get_num_pages(lsn_t &start_id, lsn_t &stop_id,
2001 uint64_t *num_pages) {
2002 DBUG_PRINT("page_archiver", ("Fetch num pages"));
2003
2004 arch_mutex_enter();
2005
2006 /** 1. Get appropriate LSN range. */
2007 Arch_Group *group = nullptr;
2008
2009 int error = fetch_group_within_lsn_range(start_id, stop_id, &group);
2010
2011 #ifdef UNIV_DEBUG
2012 arch_oper_mutex_enter();
2013
2014 DBUG_PRINT("page_archiver", ("Start id: %" PRIu64 ", stop id: %" PRIu64 "",
2015 start_id, stop_id));
2016 if (is_active()) {
2017 DBUG_PRINT("page_archiver",
2018 ("Write_pos : %" PRIu64 ", %u", m_write_pos.m_block_num,
2019 m_write_pos.m_offset));
2020 }
2021 DBUG_PRINT("page_archiver",
2022 ("Latest stop lsn : %" PRIu64 "", m_latest_stop_lsn));
2023
2024 arch_oper_mutex_exit();
2025 #endif
2026
2027 if (error != 0) {
2028 arch_mutex_exit();
2029 return (error);
2030 }
2031
2032 ut_ad(group != nullptr);
2033
2034 /** 2. Get block position from where to start. */
2035
2036 Arch_Point start_point;
2037 bool success;
2038
2039 success = group->find_reset_point(start_id, start_point);
2040
2041 if (!success) {
2042 DBUG_PRINT("page_archiver",
2043 ("Can't fetch pages - No matching reset point."));
2044 arch_mutex_exit();
2045 return (ER_PAGE_TRACKING_RANGE_NOT_TRACKED);
2046 }
2047
2048 DBUG_PRINT(
2049 "page_archiver",
2050 ("Start point - lsn : %" PRIu64 " \tpos : %" PRIu64 ", %u",
2051 start_point.lsn, start_point.pos.m_block_num, start_point.pos.m_offset));
2052
2053 Arch_Page_Pos start_pos = start_point.pos;
2054 start_id = start_point.lsn;
2055
2056 /** 3. Get block position where to stop */
2057
2058 Arch_Point stop_point;
2059
2060 success = group->find_stop_point(stop_id, stop_point, m_write_pos);
2061 ut_ad(success);
2062
2063 DBUG_PRINT(
2064 "page_archiver",
2065 ("Stop point - lsn : %" PRIu64 " \tpos : %" PRIu64 ", %u", stop_point.lsn,
2066 stop_point.pos.m_block_num, stop_point.pos.m_offset));
2067
2068 arch_mutex_exit();
2069
2070 Arch_Page_Pos stop_pos = stop_point.pos;
2071 stop_id = stop_point.lsn;
2072
2073 /** 4. Fetch number of pages tracked. */
2074
2075 ut_ad(start_point.lsn <= stop_point.lsn);
2076 ut_ad(start_point.pos.m_block_num <= stop_point.pos.m_block_num);
2077
2078 success = get_num_pages(start_pos, stop_pos, *num_pages);
2079
2080 if (!success) {
2081 num_pages = nullptr;
2082 }
2083
2084 DBUG_PRINT("page_archiver",
2085 ("Number of pages tracked : %" PRIu64 "", *num_pages));
2086
2087 return (0);
2088 }
2089
2090 /** Wait for archive system to come out of #ARCH_STATE_PREPARE_IDLE.
2091 If the system is preparing to idle, #start needs to wait
2092 for it to come to idle state.
2093 @return true, if successful
2094 false, if needs to abort */
2095 bool Arch_Page_Sys::wait_idle() {
2096 ut_ad(mutex_own(&m_mutex));
2097
2098 if (m_state == ARCH_STATE_PREPARE_IDLE) {
2099 os_event_set(page_archiver_thread_event);
2100 bool is_timeout = false;
2101 int alert_count = 0;
2102
2103 auto err = Clone_Sys::wait_default(
2104 [&](bool alert, bool &result) {
2105 ut_ad(mutex_own(&m_mutex));
2106 result = (m_state == ARCH_STATE_PREPARE_IDLE);
2107
2108 if (srv_shutdown_state.load() >= SRV_SHUTDOWN_CLEANUP) {
2109 return (ER_QUERY_INTERRUPTED);
2110 }
2111
2112 if (result) {
2113 os_event_set(page_archiver_thread_event);
2114
2115 /* Print messages every 1 minute - default is 5 seconds. */
2116 if (alert && ++alert_count == 12) {
2117 alert_count = 0;
2118 ib::info(ER_IB_MSG_24) << "Page Tracking start: waiting for "
2119 "idle state.";
2120 }
2121 }
2122 return (0);
2123 },
2124 &m_mutex, is_timeout);
2125
2126 if (err == 0 && is_timeout) {
2127 err = ER_INTERNAL_ERROR;
2128 ib::info(ER_IB_MSG_25) << "Page Tracking start: wait for idle state "
2129 "timed out";
2130 ut_d(ut_error);
2131 }
2132
2133 if (err != 0) {
2134 return (false);
2135 }
2136 }
2137 return (true);
2138 }
2139
2140 /** Check if the gap from last reset is short.
2141 If not many page IDs are added till last reset, we avoid taking a new reset
2142 point
2143 @return true, if the gap is small. */
2144 bool Arch_Page_Sys::is_gap_small() {
2145 ut_ad(m_last_pos.m_block_num <= m_write_pos.m_block_num);
2146
2147 if (m_last_pos.m_block_num == m_write_pos.m_block_num) {
2148 return (true);
2149 }
2150
2151 auto next_block_num = m_last_pos.m_block_num + 1;
2152 auto length = ARCH_PAGE_BLK_SIZE - m_last_pos.m_offset;
2153
2154 if (next_block_num != m_write_pos.m_block_num) {
2155 return (false);
2156 }
2157
2158 length += m_write_pos.m_offset - ARCH_PAGE_BLK_HEADER_LENGTH;
2159
2160 /* Pages added since last reset. */
2161 auto num_pages = length / ARCH_BLK_PAGE_ID_SIZE;
2162
2163 return (num_pages < ARCH_PAGE_RESET_THRESHOLD);
2164 }
2165
2166 /** Track pages for which IO is already started. */
2167 void Arch_Page_Sys::track_initial_pages() {
2168 uint index;
2169 buf_pool_t *buf_pool;
2170
2171 for (index = 0; index < srv_buf_pool_instances; ++index) {
2172 buf_pool = buf_pool_from_array(index);
2173
2174 mutex_enter(&buf_pool->flush_state_mutex);
2175
2176 /* Page tracking must already be active. */
2177 ut_ad(buf_pool->track_page_lsn != LSN_MAX);
2178
2179 buf_flush_list_mutex_enter(buf_pool);
2180
2181 buf_page_t *bpage;
2182
2183 bpage = buf_pool->oldest_hp.get();
2184 if (bpage != nullptr) {
2185 ut_ad(bpage->in_flush_list);
2186 } else {
2187 bpage = UT_LIST_GET_LAST(buf_pool->flush_list);
2188 }
2189
2190 /* Add all pages for which IO is already started. */
2191 while (bpage != nullptr) {
2192 if (fsp_is_system_temporary(bpage->id.space())) {
2193 bpage = UT_LIST_GET_PREV(list, bpage);
2194 continue;
2195 }
2196
2197 /* There cannot be any more IO fixed pages. */
2198
2199 /* Check if we could finish traversing flush list
2200 earlier. Order of pages in flush list became relaxed,
2201 but the distortion is limited by the flush_order_lag.
2202
2203 You can think about this in following way: pages
2204 start to travel to flush list when they have the
2205 oldest_modification field assigned. They start in
2206 proper order, but they can be delayed when traveling
2207 and they can finish their travel in different order.
2208
2209 However page is disallowed to finish its travel,
2210 if there is other page, which started much much
2211 earlier its travel and still haven't finished.
2212 The "much much" part is defined by the maximum
2213 allowed lag - log_buffer_flush_order_lag(). */
2214 if (bpage->get_oldest_lsn() >
2215 buf_pool->max_lsn_io + log_buffer_flush_order_lag(*log_sys)) {
2216 /* All pages with oldest_modification
2217 smaller than bpage->oldest_modification
2218 minus the flush_order_lag have already
2219 been traversed. So there is no page which:
2220 - we haven't traversed
2221 - and has oldest_modification
2222 smaller than buf_pool->max_lsn_io. */
2223 break;
2224 }
2225
2226 /** We read the io_fix flag without holding buf_page_get_mutex(bpage), but
2227 we hold flush_state_mutex which is also taken when transitioning:
2228 - from BUF_IO_NONE to BUF_IO_WRITE in buf_flush_page()
2229 - from BUF_IO_WRITE to BUF_IO_NONE in buf_flush_write_complete()
2230 which are the only transitions to and from BUF_IO_WRITE state that we care
2231 about. */
2232 if (bpage->is_io_fix_write()) {
2233 /* IO has already started. Must add the page */
2234 track_page(bpage, LSN_MAX, LSN_MAX, true);
2235 }
2236
2237 bpage = UT_LIST_GET_PREV(list, bpage);
2238 }
2239
2240 buf_flush_list_mutex_exit(buf_pool);
2241 mutex_exit(&buf_pool->flush_state_mutex);
2242 }
2243 }
2244
2245 /** Enable tracking pages in all buffer pools.
2246 @param[in] tracking_lsn track pages from this LSN */
2247 void Arch_Page_Sys::set_tracking_buf_pool(lsn_t tracking_lsn) {
2248 uint index;
2249 buf_pool_t *buf_pool;
2250
2251 for (index = 0; index < srv_buf_pool_instances; ++index) {
2252 buf_pool = buf_pool_from_array(index);
2253
2254 mutex_enter(&buf_pool->flush_state_mutex);
2255
2256 ut_ad(buf_pool->track_page_lsn == LSN_MAX ||
2257 buf_pool->track_page_lsn <= tracking_lsn);
2258
2259 buf_pool->track_page_lsn = tracking_lsn;
2260
2261 mutex_exit(&buf_pool->flush_state_mutex);
2262 }
2263 }
2264
2265 int Arch_Page_Sys::recovery_load_and_start(const Arch_Recv_Group_Info &info) {
2266 /* Initialise the page archiver with the info parsed from the files. */
2267
2268 m_current_group = info.m_group;
2269
2270 m_write_pos = info.m_write_pos;
2271 m_reset_pos = info.m_reset_pos;
2272 m_flush_pos = m_write_pos;
2273
2274 Arch_Reset_File last_reset_file = info.m_last_reset_file;
2275 ut_ad(last_reset_file.m_start_point.size() > 0);
2276 Arch_Point reset_point = last_reset_file.m_start_point.back();
2277
2278 m_last_pos = reset_point.pos;
2279 m_last_lsn = reset_point.lsn;
2280 m_last_reset_file_index = last_reset_file.m_file_index;
2281
2282 ut_ad(m_last_lsn != LSN_MAX);
2283
2284 auto err = m_ctx->init_during_recovery(m_current_group, m_last_lsn);
2285
2286 if (err != 0) {
2287 return err;
2288 }
2289
2290 if (info.m_new_empty_file) {
2291 m_flush_pos.set_next();
2292 m_write_pos.set_next();
2293 m_reset_pos.set_next();
2294 m_last_reset_file_index = m_reset_pos.m_block_num;
2295 }
2296
2297 /* Reload both reset block and write block active at the time of a crash. */
2298
2299 auto cur_blk = m_data.get_block(&m_write_pos, ARCH_DATA_BLOCK);
2300 auto reset_block = m_data.get_block(&m_reset_pos, ARCH_RESET_BLOCK);
2301
2302 arch_mutex_enter();
2303 arch_oper_mutex_enter();
2304
2305 cur_blk->begin_write(m_write_pos);
2306 reset_block->begin_write(m_write_pos);
2307
2308 if (!info.m_new_empty_file) {
2309 cur_blk->set_data_len(m_write_pos.m_offset - ARCH_PAGE_BLK_HEADER_LENGTH);
2310 cur_blk->set_data(ARCH_PAGE_BLK_SIZE, info.m_last_data_block, 0);
2311
2312 reset_block->set_data_len(m_reset_pos.m_offset -
2313 ARCH_PAGE_BLK_HEADER_LENGTH);
2314 reset_block->set_data(ARCH_PAGE_BLK_SIZE, info.m_last_reset_block, 0);
2315 }
2316
2317 ut_d(print());
2318
2319 arch_oper_mutex_exit();
2320 arch_mutex_exit();
2321
2322 return err;
2323 }
2324
2325 int Arch_Page_Sys::start(Arch_Group **group, lsn_t *start_lsn,
2326 Arch_Page_Pos *start_pos, bool is_durable,
2327 bool restart, bool recovery) {
2328 /* Check if archiver task needs to be started. */
2329 arch_mutex_enter();
2330
2331 if (m_state == ARCH_STATE_READ_ONLY) {
2332 arch_mutex_exit();
2333 return (0);
2334 }
2335
2336 bool start_archiver = true;
2337 bool attach_to_current = false;
2338 bool acquired_oper_mutex = false;
2339
2340 lsn_t log_sys_lsn = LSN_MAX;
2341
2342 start_archiver = is_init();
2343
2344 /* Wait for idle state, if preparing to idle. */
2345 if (!wait_idle()) {
2346 int err = 0;
2347
2348 if (srv_shutdown_state.load() >= SRV_SHUTDOWN_CLEANUP) {
2349 err = ER_QUERY_INTERRUPTED;
2350 my_error(err, MYF(0));
2351 } else {
2352 err = ER_INTERNAL_ERROR;
2353 my_error(err, MYF(0), "Page Archiver wait too long");
2354 }
2355
2356 arch_mutex_exit();
2357 return (err);
2358 }
2359
2360 switch (m_state) {
2361 case ARCH_STATE_ABORT:
2362 arch_mutex_exit();
2363 my_error(ER_QUERY_INTERRUPTED, MYF(0));
2364 return (ER_QUERY_INTERRUPTED);
2365
2366 case ARCH_STATE_INIT:
2367 case ARCH_STATE_IDLE:
2368 [[fallthrough]];
2369
2370 case ARCH_STATE_ACTIVE:
2371
2372 if (m_current_group != nullptr) {
2373 /* If gap is small, just attach to current group */
2374 attach_to_current = (recovery ? false : is_gap_small());
2375
2376 if (attach_to_current) {
2377 DBUG_PRINT("page_archiver",
2378 ("Gap is small - last pos : %" PRIu64
2379 " %u, write_pos : %" PRIu64 " %u",
2380 m_last_pos.m_block_num, m_last_pos.m_offset,
2381 m_write_pos.m_block_num, m_write_pos.m_offset));
2382 }
2383 }
2384
2385 if (!attach_to_current) {
2386 log_buffer_x_lock_enter(*log_sys);
2387
2388 if (!recovery) {
2389 MONITOR_INC(MONITOR_PAGE_TRACK_RESETS);
2390 }
2391
2392 log_sys_lsn = (recovery ? m_last_lsn : log_get_lsn(*log_sys));
2393
2394 /* Enable/Reset buffer pool page tracking. */
2395 set_tracking_buf_pool(log_sys_lsn);
2396
2397 /* Take operation mutex before releasing log_sys to
2398 ensure that all pages modified after log_sys_lsn are
2399 tracked. */
2400 arch_oper_mutex_enter();
2401 acquired_oper_mutex = true;
2402
2403 log_buffer_x_lock_exit(*log_sys);
2404 } else {
2405 arch_oper_mutex_enter();
2406 acquired_oper_mutex = true;
2407 }
2408 break;
2409
2410 case ARCH_STATE_PREPARE_IDLE:
2411 default:
2412 ut_d(ut_error);
2413 }
2414
2415 if (is_init() && !m_data.init()) {
2416 ut_ad(!attach_to_current);
2417 acquired_oper_mutex = false;
2418 arch_oper_mutex_exit();
2419 arch_mutex_exit();
2420
2421 my_error(ER_OUTOFMEMORY, MYF(0), ARCH_PAGE_BLK_SIZE);
2422 return (ER_OUTOFMEMORY);
2423 }
2424
2425 /* Start archiver background task. */
2426 if (start_archiver) {
2427 ut_ad(!attach_to_current);
2428
2429 auto err = start_page_archiver_background();
2430
2431 if (err != 0) {
2432 acquired_oper_mutex = false;
2433 arch_oper_mutex_exit();
2434 arch_mutex_exit();
2435
2436 ib::error(ER_IB_MSG_26) << "Could not start "
2437 << "Archiver background task";
2438 return (err);
2439 }
2440 }
2441
2442 /* Create a new archive group. */
2443 if (m_current_group == nullptr) {
2444 ut_ad(!attach_to_current);
2445
2446 m_last_pos.init();
2447 m_flush_pos.init();
2448 m_write_pos.init();
2449 m_reset_pos.init();
2450 m_request_flush_pos.init();
2451 m_request_blk_num_with_lsn = std::numeric_limits<uint64_t>::max();
2452 m_flush_blk_num_with_lsn = std::numeric_limits<uint64_t>::max();
2453
2454 m_last_lsn = log_sys_lsn;
2455 m_last_reset_file_index = 0;
2456
2457 m_current_group = ut::new_withkey<Arch_Group>(
2458 ut::make_psi_memory_key(mem_key_archive), log_sys_lsn,
2459 ARCH_PAGE_FILE_HDR_SIZE, &m_mutex);
2460
2461 if (m_current_group == nullptr) {
2462 acquired_oper_mutex = false;
2463 arch_oper_mutex_exit();
2464 arch_mutex_exit();
2465
2466 my_error(ER_OUTOFMEMORY, MYF(0), sizeof(Arch_Group));
2467 return (ER_OUTOFMEMORY);
2468 }
2469
2470 const uint64_t new_file_size =
2471 static_cast<uint64_t>(ARCH_PAGE_BLK_SIZE) * ARCH_PAGE_FILE_CAPACITY;
2472
2473 /* Initialize archiver file context. */
2474 auto db_err = m_current_group->init_file_ctx(
2475 ARCH_DIR, ARCH_PAGE_DIR, ARCH_PAGE_FILE, 0, new_file_size, 0);
2476
2477 if (db_err != DB_SUCCESS) {
2478 arch_oper_mutex_exit();
2479 arch_mutex_exit();
2480
2481 my_error(ER_OUTOFMEMORY, MYF(0), sizeof(Arch_File_Ctx));
2482 return (ER_OUTOFMEMORY);
2483 }
2484
2485 m_group_list.push_back(m_current_group);
2486
2487 Arch_Block *reset_block = m_data.get_block(&m_reset_pos, ARCH_RESET_BLOCK);
2488 reset_block->begin_write(m_write_pos);
2489
2490 DBUG_PRINT("page_archiver", ("Creating a new archived group."));
2491
2492 } else if (!attach_to_current && !recovery) {
2493 /* It's a reset. */
2494 m_last_lsn = log_sys_lsn;
2495 m_last_pos = m_write_pos;
2496
2497 DBUG_PRINT("page_archiver", ("It's a reset."));
2498 }
2499
2500 m_state = ARCH_STATE_ACTIVE;
2501 *start_lsn = m_last_lsn;
2502
2503 bool wait_for_block_flush = false;
2504
2505 if (!recovery) {
2506 if (!attach_to_current) {
2507 wait_for_block_flush = save_reset_point(is_durable);
2508
2509 } else if (is_durable && !m_current_group->is_durable()) {
2510 /* In case this is the first durable archiving of the group and if the
2511 gap is small for a reset then set the below variable and wait for the
2512 reset info to be flushed before we return to the caller. */
2513
2514 wait_for_block_flush = true;
2515 m_request_blk_num_with_lsn = m_last_pos.m_block_num;
2516 }
2517 }
2518
2519 acquired_oper_mutex = false;
2520 arch_oper_mutex_exit();
2521
2522 ut_ad(m_last_lsn != LSN_MAX);
2523 ut_ad(m_current_group != nullptr);
2524
2525 if (!restart) {
2526 /* Add pages to tracking for which IO has already started. */
2527 track_initial_pages();
2528
2529 *group = m_current_group;
2530
2531 *start_pos = m_last_pos;
2532
2533 arch_oper_mutex_enter();
2534 acquired_oper_mutex = true;
2535
2536 /* Attach to the group. */
2537 m_current_group->attach(is_durable);
2538
2539 } else if (recovery) {
2540 arch_oper_mutex_enter();
2541 acquired_oper_mutex = true;
2542
2543 /* Attach to the group. */
2544 m_current_group->attach(is_durable);
2545 }
2546
2547 ut_ad(*group == m_current_group);
2548
2549 if (acquired_oper_mutex) {
2550 arch_oper_mutex_exit();
2551 }
2552
2553 arch_mutex_exit();
2554
2555 if (wait_for_block_flush) {
2556 bool success = wait_for_reset_info_flush(m_request_blk_num_with_lsn);
2557
2558 if (!success) {
2559 ib::warn(ER_IB_WRN_PAGE_ARCH_FLUSH_DATA);
2560 }
2561
2562 ut_ad(m_current_group->get_file_count());
2563 }
2564
2565 if (!recovery) {
2566 if (is_durable && !restart) {
2567 m_current_group->mark_active();
2568 m_current_group->mark_durable();
2569 }
2570
2571 /* Request checkpoint */
2572 log_request_checkpoint(*log_sys, true);
2573 }
2574
2575 return (0);
2576 }
2577
2578 int Arch_Page_Sys::stop(Arch_Group *group, lsn_t *stop_lsn,
2579 Arch_Page_Pos *stop_pos, bool is_durable) {
2580 Arch_Block *cur_blk;
2581
2582 arch_mutex_enter();
2583
2584 if (m_state == ARCH_STATE_READ_ONLY) {
2585 arch_mutex_exit();
2586 return (0);
2587 }
2588
2589 ut_ad(group == m_current_group);
2590 ut_ad(m_state == ARCH_STATE_ACTIVE);
2591
2592 arch_oper_mutex_enter();
2593
2594 *stop_lsn = m_latest_stop_lsn;
2595 cur_blk = m_data.get_block(&m_write_pos, ARCH_DATA_BLOCK);
2596 update_stop_info(cur_blk);
2597
2598 auto count = group->detach(*stop_lsn, &m_write_pos);
2599 arch_oper_mutex_exit();
2600
2601 int err = 0;
2602 bool wait_for_block_flush = false;
2603
2604 /* If no other active client, let the system get into idle state. */
2605 if (count == 0 && m_state != ARCH_STATE_ABORT) {
2606 set_tracking_buf_pool(LSN_MAX);
2607
2608 arch_oper_mutex_enter();
2609
2610 m_state = ARCH_STATE_PREPARE_IDLE;
2611 *stop_pos = m_write_pos;
2612
2613 cur_blk->end_write();
2614 m_request_flush_pos = m_write_pos;
2615 m_write_pos.set_next();
2616
2617 os_event_set(page_archiver_thread_event);
2618
2619 wait_for_block_flush = m_current_group->is_durable() ? true : false;
2620
2621 } else {
2622 if (m_state != ARCH_STATE_ABORT && is_durable &&
2623 !m_current_group->is_durable_client_active()) {
2624 /* In case the non-durable clients are still active but there are no
2625 active durable clients we need to mark the group inactive for recovery
2626 to know that no durable clients were active. */
2627 err = m_current_group->mark_inactive();
2628 }
2629
2630 arch_oper_mutex_enter();
2631
2632 *stop_pos = m_write_pos;
2633 }
2634
2635 if (m_state == ARCH_STATE_ABORT) {
2636 my_error(ER_QUERY_INTERRUPTED, MYF(0));
2637 err = ER_QUERY_INTERRUPTED;
2638 }
2639
2640 arch_oper_mutex_exit();
2641 arch_mutex_exit();
2642
2643 if (wait_for_block_flush) {
2644 /* Wait for flush archiver to flush the blocks. */
2645 auto cbk = [&] {
2646 return (m_flush_pos.m_block_num > m_request_flush_pos.m_block_num ? false
2647 : true);
2648 };
2649
2650 arch_oper_mutex_enter();
2651
2652 if (!wait_flush_archiver(cbk)) {
2653 ib::warn(ER_IB_WRN_PAGE_ARCH_FLUSH_DATA);
2654 }
2655
2656 arch_oper_mutex_exit();
2657
2658 ut_ad(group->validate_info_in_files());
2659 }
2660
2661 return (err);
2662 }
2663
2664 void Arch_Page_Sys::release(Arch_Group *group, bool is_durable,
2665 Arch_Page_Pos start_pos [[maybe_unused]]) {
2666 arch_mutex_enter();
2667 arch_oper_mutex_enter();
2668
2669 group->release(is_durable);
2670
2671 arch_oper_mutex_exit();
2672
2673 if (group->is_active()) {
2674 arch_mutex_exit();
2675 return;
2676 }
2677
2678 ut_ad(group != m_current_group);
2679
2680 if (!group->is_referenced()) {
2681 m_group_list.remove(group);
2682 ut::delete_(group);
2683 }
2684
2685 arch_mutex_exit();
2686 }
2687
2688 dberr_t Arch_Page_Sys::flush_inactive_blocks(Arch_Page_Pos &cur_pos,
2689 Arch_Page_Pos end_pos) {
2690 dberr_t err = DB_SUCCESS;
2691 Arch_Block *cur_blk;
2692
2693 /* Write all blocks that are ready for flushing. */
2694 while (cur_pos.m_block_num < end_pos.m_block_num) {
2695 cur_blk = m_data.get_block(&cur_pos, ARCH_DATA_BLOCK);
2696
2697 err = cur_blk->flush(m_current_group, ARCH_FLUSH_NORMAL);
2698
2699 if (err != DB_SUCCESS) {
2700 break;
2701 }
2702
2703 MONITOR_INC(MONITOR_PAGE_TRACK_FULL_BLOCK_WRITES);
2704
2705 arch_oper_mutex_enter();
2706
2707 m_flush_blk_num_with_lsn = cur_pos.m_block_num;
2708 cur_pos.set_next();
2709 cur_blk->set_flushed();
2710 m_flush_pos.set_next();
2711
2712 arch_oper_mutex_exit();
2713 }
2714
2715 return (err);
2716 }
2717
2718 dberr_t Arch_Page_Sys::flush_active_block(Arch_Page_Pos cur_pos,
2719 bool partial_reset_block_flush) {
2720 Arch_Block *cur_blk;
2721 cur_blk = m_data.get_block(&cur_pos, ARCH_DATA_BLOCK);
2722
2723 arch_oper_mutex_enter();
2724
2725 if (!cur_blk->is_active()) {
2726 arch_oper_mutex_exit();
2727 return (DB_SUCCESS);
2728 }
2729
2730 /* Copy block data so that we can release the arch_oper_mutex soon. */
2731 Arch_Block *flush_blk = m_data.get_partial_flush_block();
2732 flush_blk->copy_data(cur_blk);
2733
2734 arch_oper_mutex_exit();
2735
2736 dberr_t err = flush_blk->flush(m_current_group, ARCH_FLUSH_PARTIAL);
2737
2738 if (err != DB_SUCCESS) {
2739 return (err);
2740 }
2741
2742 MONITOR_INC(MONITOR_PAGE_TRACK_PARTIAL_BLOCK_WRITES);
2743
2744 if (partial_reset_block_flush) {
2745 arch_oper_mutex_enter();
2746
2747 Arch_Block *reset_block = m_data.get_block(&m_reset_pos, ARCH_RESET_BLOCK);
2748
2749 arch_oper_mutex_exit();
2750
2751 err = reset_block->flush(m_current_group, ARCH_FLUSH_NORMAL);
2752
2753 if (err != DB_SUCCESS) {
2754 return (err);
2755 }
2756 }
2757
2758 arch_oper_mutex_enter();
2759
2760 m_flush_pos.m_offset =
2761 flush_blk->get_data_len() + ARCH_PAGE_BLK_HEADER_LENGTH;
2762
2763 arch_oper_mutex_exit();
2764
2765 return (err);
2766 }
2767
2768 dberr_t Arch_Page_Sys::flush_blocks(bool *wait) {
2769 arch_oper_mutex_enter();
2770
2771 auto request_flush_pos = m_request_flush_pos;
2772 auto cur_pos = m_flush_pos;
2773 auto end_pos = m_write_pos;
2774 auto request_blk_num_with_lsn = m_request_blk_num_with_lsn;
2775 auto flush_blk_num_with_lsn = m_flush_blk_num_with_lsn;
2776
2777 arch_oper_mutex_exit();
2778
2779 uint64_t ARCH_UNKNOWN_BLOCK = std::numeric_limits<uint64_t>::max();
2780
2781 ut_ad(cur_pos.m_block_num <= end_pos.m_block_num);
2782
2783 /* Caller needs to wait/sleep, if nothing to flush. */
2784 *wait = (cur_pos.m_block_num == end_pos.m_block_num);
2785
2786 dberr_t err;
2787
2788 err = flush_inactive_blocks(cur_pos, end_pos);
2789
2790 if (err != DB_SUCCESS) {
2791 return (err);
2792 }
2793
2794 if (cur_pos.m_block_num == end_pos.m_block_num) {
2795 /* Partial Flush */
2796
2797 bool data_block_flush =
2798 request_flush_pos.m_block_num == cur_pos.m_block_num &&
2799 request_flush_pos.m_offset > cur_pos.m_offset;
2800 bool reset_block_flush =
2801 request_blk_num_with_lsn != ARCH_UNKNOWN_BLOCK &&
2802 (flush_blk_num_with_lsn == ARCH_UNKNOWN_BLOCK ||
2803 request_blk_num_with_lsn > flush_blk_num_with_lsn);
2804
2805 /* We do partial flush only if we're explicitly requested to flush. */
2806 if (data_block_flush || reset_block_flush) {
2807 err = flush_active_block(cur_pos, reset_block_flush);
2808
2809 if (err != DB_SUCCESS) {
2810 return (err);
2811 }
2812 }
2813
2814 arch_oper_mutex_enter();
2815
2816 if (request_blk_num_with_lsn != ARCH_UNKNOWN_BLOCK &&
2817 (flush_blk_num_with_lsn == ARCH_UNKNOWN_BLOCK ||
2818 request_blk_num_with_lsn > flush_blk_num_with_lsn)) {
2819 m_flush_blk_num_with_lsn = request_blk_num_with_lsn;
2820 }
2821
2822 arch_oper_mutex_exit();
2823 }
2824
2825 return (err);
2826 }
2827
2828 bool Arch_Page_Sys::archive(bool *wait) {
2829 dberr_t db_err;
2830
2831 auto is_abort = (srv_shutdown_state.load() == SRV_SHUTDOWN_LAST_PHASE ||
2832 srv_shutdown_state.load() == SRV_SHUTDOWN_EXIT_THREADS ||
2833 m_state == ARCH_STATE_ABORT);
2834
2835 arch_oper_mutex_enter();
2836
2837 /* Check if archiving state is inactive. */
2838 if (m_state == ARCH_STATE_IDLE || m_state == ARCH_STATE_INIT) {
2839 *wait = true;
2840
2841 if (is_abort) {
2842 m_state = ARCH_STATE_ABORT;
2843 arch_oper_mutex_exit();
2844
2845 return (true);
2846 }
2847
2848 arch_oper_mutex_exit();
2849
2850 return (false);
2851 }
2852
2853 /* ARCH_STATE_ABORT is set for flush timeout which is asserted in debug. */
2854 ut_ad(m_state == ARCH_STATE_ACTIVE || m_state == ARCH_STATE_PREPARE_IDLE);
2855
2856 auto set_idle = (m_state == ARCH_STATE_PREPARE_IDLE);
2857 arch_oper_mutex_exit();
2858
2859 db_err = flush_blocks(wait);
2860
2861 if (db_err != DB_SUCCESS) {
2862 is_abort = true;
2863 }
2864
2865 /* Move to idle state or abort, if needed. */
2866 if (set_idle || is_abort) {
2867 arch_mutex_enter();
2868 arch_oper_mutex_enter();
2869
2870 m_current_group->disable(LSN_MAX);
2871 m_current_group->close_file_ctxs();
2872
2873 int err = 0;
2874
2875 if (!is_abort && m_current_group->is_durable()) {
2876 err = m_current_group->mark_inactive();
2877
2878 Arch_Group::init_dblwr_file_ctx(
2879 ARCH_DBLWR_DIR, ARCH_DBLWR_FILE, ARCH_DBLWR_NUM_FILES,
2880 static_cast<uint64_t>(ARCH_PAGE_BLK_SIZE) * ARCH_DBLWR_FILE_CAPACITY);
2881
2882 ut_ad(m_current_group->validate_info_in_files());
2883 }
2884
2885 if (err != 0) {
2886 is_abort = true;
2887 }
2888
2889 /* Cleanup group, if no reference. */
2890 if (!m_current_group->is_referenced()) {
2891 m_group_list.remove(m_current_group);
2892 ut::delete_(m_current_group);
2893 }
2894
2895 m_current_group = nullptr;
2896 m_state = is_abort ? ARCH_STATE_ABORT : ARCH_STATE_IDLE;
2897
2898 arch_oper_mutex_exit();
2899 arch_mutex_exit();
2900 }
2901
2902 return (is_abort);
2903 }
2904
2905 int Arch_Group::read_from_file(Arch_Page_Pos *read_pos, uint read_len,
2906 byte *read_buff) {
2907 char errbuf[MYSYS_STRERROR_SIZE];
2908 char file_name[MAX_ARCH_PAGE_FILE_NAME_LEN];
2909
2910 /* Build file name */
2911 auto file_index = static_cast<uint>(
2912 Arch_Block::get_file_index(read_pos->m_block_num, ARCH_DATA_BLOCK));
2913
2914 get_file_name(file_index, file_name, MAX_ARCH_PAGE_FILE_NAME_LEN);
2915
2916 /* Find offset to read from. */
2917 os_offset_t offset =
2918 Arch_Block::get_file_offset(read_pos->m_block_num, ARCH_DATA_BLOCK);
2919 offset += read_pos->m_offset;
2920
2921 bool success;
2922
2923 /* Open file in read only mode. */
2924 pfs_os_file_t file =
2925 os_file_create(innodb_arch_file_key, file_name, OS_FILE_OPEN,
2926 OS_CLONE_LOG_FILE, true, &success);
2927
2928 if (!success) {
2929 my_error(ER_CANT_OPEN_FILE, MYF(0), file_name, errno,
2930 my_strerror(errbuf, sizeof(errbuf), errno));
2931
2932 return (ER_CANT_OPEN_FILE);
2933 }
2934
2935 /* Read from file to the user buffer. */
2936 IORequest request(IORequest::READ);
2937
2938 request.disable_compression();
2939 request.clear_encrypted();
2940
2941 auto db_err =
2942 os_file_read(request, file_name, file, read_buff, offset, read_len);
2943
2944 os_file_close(file);
2945
2946 if (db_err != DB_SUCCESS) {
2947 my_error(ER_ERROR_ON_READ, MYF(0), file_name, errno,
2948 my_strerror(errbuf, sizeof(errbuf), errno));
2949 return (ER_ERROR_ON_READ);
2950 }
2951
2952 return (0);
2953 }
2954
2955 int Arch_Group::read_data(Arch_Page_Pos cur_pos, byte *buff, uint buff_len) {
2956 int err = 0;
2957
2958 /* Attempt to read from in memory buffer. */
2959 auto success = arch_page_sys->get_pages(this, &cur_pos, buff_len, buff);
2960
2961 if (!success) {
2962 /* The buffer is overwritten. Read from file. */
2963 err = read_from_file(&cur_pos, buff_len, buff);
2964 }
2965
2966 return (err);
2967 }
2968
2969 bool Arch_Page_Sys::save_reset_point(bool is_durable) {
2970 /* 1. Add the reset info to the reset block */
2971
2972 uint current_file_index =
2973 Arch_Block::get_file_index(m_last_pos.m_block_num, ARCH_DATA_BLOCK);
2974
2975 auto reset_block = m_data.get_block(&m_reset_pos, ARCH_RESET_BLOCK);
2976
2977 /* If the reset info should belong to a new file then re-intialize the
2978 block as the block from now on will contain reset information belonging
2979 to the new file */
2980 if (m_last_reset_file_index != current_file_index) {
2981 ut_ad(current_file_index > m_last_reset_file_index);
2982 reset_block->begin_write(m_last_pos);
2983 }
2984
2985 m_last_reset_file_index = current_file_index;
2986
2987 reset_block->add_reset(m_last_lsn, m_last_pos);
2988
2989 m_current_group->save_reset_point_in_mem(m_last_lsn, m_last_pos);
2990
2991 auto cur_block = m_data.get_block(&m_last_pos, ARCH_DATA_BLOCK);
2992
2993 if (cur_block->get_state() == ARCH_BLOCK_INIT ||
2994 cur_block->get_state() == ARCH_BLOCK_FLUSHED) {
2995 cur_block->begin_write(m_last_pos);
2996 }
2997
2998 m_latest_stop_lsn = log_get_checkpoint_lsn(*log_sys);
2999 update_stop_info(cur_block);
3000
3001 /* 2. Add the reset lsn to the current write_pos block header and request the
3002 flush archiver to flush the data block and reset block */
3003
3004 cur_block->update_block_header(LSN_MAX, m_last_lsn);
3005
3006 ut_d(auto ARCH_UNKNOWN_BLOCK = std::numeric_limits<uint64_t>::max());
3007
3008 /* Reset LSN for a block can be updated only once. */
3009 ut_ad(m_flush_blk_num_with_lsn == ARCH_UNKNOWN_BLOCK ||
3010 m_flush_blk_num_with_lsn < cur_block->get_number());
3011 ut_ad(m_request_blk_num_with_lsn == ARCH_UNKNOWN_BLOCK ||
3012 m_request_blk_num_with_lsn < cur_block->get_number());
3013
3014 uint64_t request_blk_num_with_lsn = cur_block->get_number();
3015
3016 m_request_blk_num_with_lsn = request_blk_num_with_lsn;
3017
3018 DBUG_PRINT("page_archiver",
3019 ("Saved reset point at %u - %" PRIu64 ", %" PRIu64 ", %u\n",
3020 m_last_reset_file_index, m_last_lsn, m_last_pos.m_block_num,
3021 m_last_pos.m_offset));
3022
3023 return is_durable;
3024 }
3025
3026 bool Arch_Page_Sys::wait_for_reset_info_flush(uint64_t request_blk) {
3027 auto ARCH_UNKNOWN_BLOCK = std::numeric_limits<uint64_t>::max();
3028
3029 auto cbk = [&] {
3030 if (m_flush_blk_num_with_lsn == ARCH_UNKNOWN_BLOCK ||
3031 request_blk > m_flush_blk_num_with_lsn) {
3032 return (true);
3033 }
3034
3035 return (false);
3036 };
3037
3038 arch_oper_mutex_enter();
3039
3040 bool success = wait_flush_archiver(cbk);
3041
3042 arch_oper_mutex_exit();
3043
3044 return (success);
3045 }
3046
3047 int Arch_Page_Sys::fetch_group_within_lsn_range(lsn_t &start_id, lsn_t &stop_id,
3048 Arch_Group **group) {
3049 ut_ad(mutex_own(&m_mutex));
3050
3051 if (start_id != 0 && stop_id != 0 && start_id >= stop_id) {
3052 return (ER_PAGE_TRACKING_RANGE_NOT_TRACKED);
3053 }
3054
3055 arch_oper_mutex_enter();
3056 auto latest_stop_lsn = m_latest_stop_lsn;
3057 arch_oper_mutex_exit();
3058
3059 ut_ad(latest_stop_lsn != LSN_MAX);
3060
3061 if (start_id == 0 || stop_id == 0) {
3062 if (m_current_group == nullptr || !m_current_group->is_active()) {
3063 return (ER_PAGE_TRACKING_RANGE_NOT_TRACKED);
3064 }
3065
3066 *group = m_current_group;
3067
3068 ut_ad(m_last_lsn != LSN_MAX);
3069
3070 start_id = (start_id == 0) ? m_last_lsn : start_id;
3071 stop_id = (stop_id == 0) ? latest_stop_lsn : stop_id;
3072 }
3073
3074 if (start_id >= stop_id || start_id == LSN_MAX || stop_id == LSN_MAX) {
3075 return (ER_PAGE_TRACKING_RANGE_NOT_TRACKED);
3076 }
3077
3078 if (*group == nullptr) {
3079 for (auto it : m_group_list) {
3080 *group = it;
3081
3082 if (start_id < (*group)->get_begin_lsn() ||
3083 (!(*group)->is_active() && stop_id > (*group)->get_end_lsn()) ||
3084 ((*group)->is_active() && stop_id > latest_stop_lsn)) {
3085 *group = nullptr;
3086 continue;
3087 }
3088
3089 break;
3090 }
3091 }
3092
3093 if (*group == nullptr) {
3094 return (ER_PAGE_TRACKING_RANGE_NOT_TRACKED);
3095 }
3096
3097 return (0);
3098 }
3099
3100 uint Arch_Page_Sys::purge(lsn_t *purge_lsn) {
3101 lsn_t purged_lsn = LSN_MAX;
3102 uint err = 0;
3103
3104 if (*purge_lsn == 0) {
3105 *purge_lsn = log_get_checkpoint_lsn(*log_sys);
3106 }
3107
3108 DBUG_PRINT("page_archiver", ("Purging of files - %" PRIu64 "", *purge_lsn));
3109
3110 arch_mutex_enter();
3111
3112 for (auto it = m_group_list.begin(); it != m_group_list.end();) {
3113 lsn_t group_purged_lsn = LSN_MAX;
3114 auto group = *it;
3115
3116 DBUG_PRINT("page_archiver",
3117 ("End lsn - %" PRIu64 "", group->get_end_lsn()));
3118
3119 err = group->purge(*purge_lsn, group_purged_lsn);
3120
3121 if (group_purged_lsn == LSN_MAX) {
3122 break;
3123 }
3124
3125 DBUG_PRINT("page_archiver",
3126 ("Group purged lsn - %" PRIu64 "", group_purged_lsn));
3127
3128 if (purged_lsn == LSN_MAX || group_purged_lsn > purged_lsn) {
3129 purged_lsn = group_purged_lsn;
3130 }
3131
3132 if (!group->is_active() && group->get_end_lsn() <= group_purged_lsn) {
3133 it = m_group_list.erase(it);
3134 ut::delete_(group);
3135
3136 DBUG_PRINT("page_archiver", ("Purged entire group."));
3137
3138 continue;
3139 }
3140
3141 ++it;
3142 }
3143
3144 DBUG_PRINT("page_archiver",
3145 ("Purged archived file until : %" PRIu64 "", purged_lsn));
3146
3147 *purge_lsn = purged_lsn;
3148
3149 if (purged_lsn == LSN_MAX) {
3150 arch_mutex_exit();
3151 return (err);
3152 }
3153
3154 m_latest_purged_lsn = purged_lsn;
3155
3156 arch_mutex_exit();
3157
3158 return (err);
3159 }
3160
3161 void Arch_Page_Sys::update_stop_info(Arch_Block *cur_blk) {
3162 ut_ad(mutex_own(&m_oper_mutex));
3163
3164 if (cur_blk != nullptr) {
3165 cur_blk->update_block_header(m_latest_stop_lsn, LSN_MAX);
3166 }
3167
3168 if (m_current_group != nullptr) {
3169 m_current_group->update_stop_point(m_write_pos, m_latest_stop_lsn);
3170 }
3171 }
3172
3173 #ifdef UNIV_DEBUG
3174 void Arch_Page_Sys::print() {
3175 DBUG_PRINT("page_archiver", ("State : %u", m_state));
3176 DBUG_PRINT("page_archiver", ("Last pos : %" PRIu64 ", %u",
3177 m_last_pos.m_block_num, m_last_pos.m_offset));
3178 DBUG_PRINT("page_archiver", ("Last lsn : %" PRIu64 "", m_last_lsn));
3179 DBUG_PRINT("page_archiver",
3180 ("Latest stop lsn : %" PRIu64 "", m_latest_stop_lsn));
3181 DBUG_PRINT("page_archiver", ("Flush pos : %" PRIu64 ", %u",
3182 m_flush_pos.m_block_num, m_flush_pos.m_offset));
3183 DBUG_PRINT("page_archiver", ("Write pos : %" PRIu64 ", %u",
3184 m_write_pos.m_block_num, m_write_pos.m_offset));
3185 DBUG_PRINT("page_archiver", ("Reset pos : %" PRIu64 ", %u",
3186 m_reset_pos.m_block_num, m_reset_pos.m_offset));
3187 DBUG_PRINT("page_archiver",
3188 ("Last reset file index : %u", m_last_reset_file_index));
3189
3190 Arch_Block *reset_block = m_data.get_block(&m_reset_pos, ARCH_RESET_BLOCK);
3191 Arch_Block *data_block = m_data.get_block(&m_write_pos, ARCH_DATA_BLOCK);
3192
3193 DBUG_PRINT("page_archiver", ("Latest reset block data length: %u",
3194 reset_block->get_data_len()));
3195 DBUG_PRINT("page_archiver",
3196 ("Latest data block data length: %u", data_block->get_data_len()));
3197 }
3198 #endif