Debian Codegraph @

storage/innobase/arch/arch0arch.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/arch0arch.cc
29 Common implementation for redo log and dirty page archiver system
30
31 *******************************************************/
32
33 #include "arch0arch.h"
34 #include "os0thread-create.h"
35
36 /** Log Archiver system global */
37 Arch_Log_Sys *arch_log_sys = nullptr;
38
39 /** Page Archiver system global */
40 Arch_Page_Sys *arch_page_sys = nullptr;
41
42 /** Event to signal the log archiver thread. */
43 os_event_t log_archiver_thread_event;
44
45 /** Wakes up archiver threads.
46 @return true iff any thread was still alive */
47 bool arch_wake_threads() {
48 bool found_alive = false;
49 if (srv_thread_is_active(srv_threads.m_log_archiver)) {
50 os_event_set(log_archiver_thread_event);
51 found_alive = true;
52 }
53 if (srv_thread_is_active(srv_threads.m_page_archiver)) {
54 os_event_set(page_archiver_thread_event);
55 found_alive = true;
56 }
57 return (found_alive);
58 }
59
60 void arch_remove_file(const char *file_path, const char *file_name) {
61 char path[MAX_ARCH_PAGE_FILE_NAME_LEN];
62
63 static_assert(MAX_ARCH_LOG_FILE_NAME_LEN <= MAX_ARCH_PAGE_FILE_NAME_LEN);
64 ut_ad(strlen(file_path) + 1 + strlen(file_name) <
65 MAX_ARCH_PAGE_FILE_NAME_LEN);
66
67 /* Remove only LOG and PAGE archival files. */
68 if (0 != strncmp(file_name, ARCH_LOG_FILE, strlen(ARCH_LOG_FILE)) &&
69 0 != strncmp(file_name, ARCH_PAGE_FILE, strlen(ARCH_PAGE_FILE)) &&
70 0 != strncmp(file_name, ARCH_PAGE_GROUP_DURABLE_FILE_NAME,
71 strlen(ARCH_PAGE_GROUP_DURABLE_FILE_NAME))) {
72 return;
73 }
74
75 snprintf(path, sizeof(path), "%s%c%s", file_path, OS_PATH_SEPARATOR,
76 file_name);
77
78 #ifdef UNIV_DEBUG
79 os_file_type_t type;
80 bool exists;
81
82 os_file_status(path, &exists, &type);
83 ut_ad(exists);
84 ut_ad(type == OS_FILE_TYPE_FILE);
85 #endif /* UNIV_DEBUG */
86
87 os_file_delete(innodb_arch_file_key, path);
88 }
89
90 void arch_remove_dir(const char *dir_path, const char *dir_name) {
91 char path[MAX_ARCH_DIR_NAME_LEN];
92
93 static_assert(sizeof(ARCH_LOG_DIR) <= sizeof(ARCH_PAGE_DIR));
94 ut_ad(strlen(dir_path) + 1 + strlen(dir_name) + 1 < sizeof(path));
95
96 /* Remove only LOG and PAGE archival directories. */
97 if (0 != strncmp(dir_name, ARCH_LOG_DIR, strlen(ARCH_LOG_DIR)) &&
98 0 != strncmp(dir_name, ARCH_PAGE_DIR, strlen(ARCH_PAGE_DIR))) {
99 return;
100 }
101
102 snprintf(path, sizeof(path), "%s%c%s", dir_path, OS_PATH_SEPARATOR, dir_name);
103
104 #ifdef UNIV_DEBUG
105 os_file_type_t type;
106 bool exists;
107
108 os_file_status(path, &exists, &type);
109 ut_ad(exists);
110 ut_ad(type == OS_FILE_TYPE_DIR);
111 #endif /* UNIV_DEBUG */
112
113 os_file_scan_directory(path, arch_remove_file, true);
114 }
115
116 /** Initialize Page and Log archiver system
117 @return error code */
118 dberr_t arch_init() {
119 if (arch_log_sys == nullptr) {
120 arch_log_sys =
121 ut::new_withkey<Arch_Log_Sys>(ut::make_psi_memory_key(mem_key_archive));
122
123 if (arch_log_sys == nullptr) {
124 return (DB_OUT_OF_MEMORY);
125 }
126
127 log_archiver_thread_event = os_event_create();
128 }
129
130 if (arch_page_sys == nullptr) {
131 arch_page_sys = ut::new_withkey<Arch_Page_Sys>(
132 ut::make_psi_memory_key(mem_key_archive));
133
134 if (arch_page_sys == nullptr) {
135 return (DB_OUT_OF_MEMORY);
136 }
137
138 page_archiver_thread_event = os_event_create();
139 }
140
141 if (srv_read_only_mode) {
142 arch_page_sys->set_read_only_mode();
143 return DB_SUCCESS;
144 }
145
146 arch_page_sys->recover();
147
148 return DB_SUCCESS;
149 }
150
151 /** Free Page and Log archiver system */
152 void arch_free() {
153 if (arch_log_sys != nullptr) {
154 ut::delete_(arch_log_sys);
155 arch_log_sys = nullptr;
156
157 os_event_destroy(log_archiver_thread_event);
158 }
159
160 if (arch_page_sys != nullptr) {
161 ut::delete_(arch_page_sys);
162 arch_page_sys = nullptr;
163
164 os_event_destroy(page_archiver_thread_event);
165 }
166 }
167
168 dberr_t Arch_Group::prepare_file_with_header(
169 uint64_t start_offset, Get_file_header_callback &get_header) {
170 ut::aligned_array_pointer<byte, OS_FILE_LOG_BLOCK_SIZE> header{};
171 header.alloc(ut::Count{m_header_len});
172
173 dberr_t err = get_header(start_offset, header);
174 if (err != DB_SUCCESS) {
175 return err;
176 }
177
178 err = m_file_ctx.open_new(m_begin_lsn, m_file_size, 0);
179
180 if (err != DB_SUCCESS) {
181 return err;
182 }
183
184 return m_file_ctx.write(nullptr, header, m_header_len);
185 }
186
187 dberr_t Arch_Group::write_to_file(Arch_File_Ctx *from_file, byte *from_buffer,
188 uint length, bool partial_write,
189 bool do_persist,
190 Get_file_header_callback get_header) {
191 dberr_t err = DB_SUCCESS;
192 uint write_size;
193
194 if (m_file_ctx.is_closed()) {
195 /* First file in the archive group. */
196 ut_ad(m_file_ctx.get_count() == 0);
197
198 DBUG_EXECUTE_IF("crash_before_archive_file_creation", DBUG_SUICIDE(););
199
200 err = prepare_file_with_header(0, get_header);
201
202 if (err != DB_SUCCESS) {
203 return err;
204 }
205 }
206
207 auto len_left = m_file_ctx.bytes_left();
208
209 uint64_t start_offset = 0;
210
211 /* New file is immediately opened when current file is over. */
212 ut_ad(len_left != 0);
213
214 while (length > 0) {
215 auto len_copy = static_cast<uint64_t>(length);
216
217 /* Write as much as possible in current file. */
218 if (len_left < len_copy) {
219 ut_ad(len_left <= std::numeric_limits<uint>::max());
220 write_size = static_cast<uint>(len_left);
221 } else {
222 write_size = length;
223 }
224
225 if (do_persist) {
226 Arch_Page_Dblwr_Offset dblwr_offset =
227 (partial_write ? ARCH_PAGE_DBLWR_PARTIAL_FLUSH_PAGE
228 : ARCH_PAGE_DBLWR_FULL_FLUSH_PAGE);
229
230 Arch_Group::write_to_doublewrite_file(from_file, from_buffer, write_size,
231 dblwr_offset);
232 }
233
234 if (partial_write) {
235 DBUG_EXECUTE_IF("crash_after_partial_block_dblwr_flush", DBUG_SUICIDE(););
236 err = m_file_ctx.write(from_file, from_buffer,
237 static_cast<uint>(m_file_ctx.get_offset()),
238 write_size);
239 } else {
240 DBUG_EXECUTE_IF("crash_after_full_block_dblwr_flush", DBUG_SUICIDE(););
241 err = m_file_ctx.write(from_file, from_buffer, write_size);
242 }
243
244 if (err != DB_SUCCESS) {
245 return (err);
246 }
247
248 if (do_persist) {
249 /* Flush the file to make sure the changes are made persistent as there
250 would be no way to recover the data otherwise in case of a crash. */
251 m_file_ctx.flush();
252 }
253
254 ut_ad(length >= write_size);
255 length -= write_size;
256 start_offset += write_size;
257
258 len_left = m_file_ctx.bytes_left();
259
260 /* Current file is over, switch to next file. */
261 if (len_left == 0) {
262 m_file_ctx.close();
263
264 err = prepare_file_with_header(start_offset, get_header);
265
266 if (err != DB_SUCCESS) {
267 return (err);
268 }
269
270 DBUG_EXECUTE_IF("crash_after_archive_file_creation", DBUG_SUICIDE(););
271
272 len_left = m_file_ctx.bytes_left();
273 }
274 }
275
276 return (DB_SUCCESS);
277 }
278
279 bool Arch_File_Ctx::delete_file(uint file_index, lsn_t begin_lsn) {
280 bool success;
281 char file_name[MAX_ARCH_PAGE_FILE_NAME_LEN];
282
283 build_name(file_index, begin_lsn, file_name, MAX_ARCH_PAGE_FILE_NAME_LEN);
284
285 os_file_type_t type;
286 bool exists;
287
288 success = os_file_status(file_name, &exists, &type);
289
290 if (!success || !exists) {
291 return (false);
292 }
293
294 ut_ad(type == OS_FILE_TYPE_FILE);
295
296 success = os_file_delete(innodb_arch_file_key, file_name);
297
298 return (success);
299 }
300
301 void Arch_File_Ctx::delete_files(lsn_t begin_lsn) {
302 bool exists;
303 os_file_type_t type;
304 char dir_name[MAX_ARCH_DIR_NAME_LEN];
305
306 build_dir_name(begin_lsn, dir_name, MAX_ARCH_DIR_NAME_LEN);
307 os_file_status(dir_name, &exists, &type);
308
309 if (exists) {
310 ut_ad(type == OS_FILE_TYPE_DIR);
311 os_file_scan_directory(dir_name, arch_remove_file, true);
312 }
313 }
314
315 dberr_t Arch_File_Ctx::init(const char *path, const char *base_dir,
316 const char *base_file, uint num_files) {
317 m_base_len = static_cast<uint>(strlen(path));
318
319 m_name_len =
320 m_base_len + static_cast<uint>(strlen(base_file)) + MAX_LSN_DECIMAL_DIGIT;
321
322 if (base_dir != nullptr) {
323 m_name_len += static_cast<uint>(strlen(base_dir));
324 m_name_len += MAX_LSN_DECIMAL_DIGIT;
325 }
326
327 /* Add some extra buffer. */
328 m_name_len += MAX_LSN_DECIMAL_DIGIT;
329
330 /* In case of reinitialise. */
331 if (m_name_buf != nullptr) {
332 ut::free(m_name_buf);
333 m_name_buf = nullptr;
334 }
335
336 m_name_buf = static_cast<char *>(
337 ut::malloc_withkey(ut::make_psi_memory_key(mem_key_archive), m_name_len));
338
339 if (m_name_buf == nullptr) {
340 return (DB_OUT_OF_MEMORY);
341 }
342
343 m_path_name = path;
344 m_dir_name = base_dir;
345 m_file_name = base_file;
346
347 strcpy(m_name_buf, path);
348
349 if (m_name_buf[m_base_len - 1] != OS_PATH_SEPARATOR) {
350 m_name_buf[m_base_len] = OS_PATH_SEPARATOR;
351 ++m_base_len;
352 m_name_buf[m_base_len] = '\0';
353 }
354
355 m_file.m_file = OS_FILE_CLOSED;
356
357 m_index = 0;
358 m_count = num_files;
359
360 m_offset = 0;
361
362 m_reset.clear();
363 m_stop_points.clear();
364
365 return (DB_SUCCESS);
366 }
367
368 dberr_t Arch_File_Ctx::open(bool read_only, lsn_t start_lsn, uint file_index,
369 uint64_t file_offset, uint64_t file_size) {
370 /* Close current file, if open. */
371 close();
372
373 m_index = file_index;
374 m_offset = file_offset;
375
376 build_name(m_index, start_lsn, nullptr, 0);
377
378 bool exists;
379 os_file_type_t type;
380
381 bool success = os_file_status(m_name_buf, &exists, &type);
382
383 if (!success) {
384 return (DB_CANNOT_OPEN_FILE);
385 }
386
387 os_file_create_t option;
388
389 if (read_only) {
390 if (!exists) {
391 return (DB_CANNOT_OPEN_FILE);
392 }
393
394 option = OS_FILE_OPEN;
395 } else {
396 option = exists ? OS_FILE_OPEN : OS_FILE_CREATE_PATH;
397 }
398
399 m_file = os_file_create(innodb_arch_file_key, m_name_buf, option,
400 OS_CLONE_LOG_FILE, read_only, &success);
401
402 if (!success) {
403 return (DB_CANNOT_OPEN_FILE);
404 }
405
406 /* For newly created file, zero fill the header section. This is required
407 for archived redo files that are just created. Clone expects the header
408 length to be written. */
409 if (!exists && file_offset != 0 && !read_only) {
410 /* This call would extend the length by multiple of UNIV_PAGE_SIZE. This is
411 not an issue but we need to lseek to keep the current position at offset. */
412 success = os_file_set_size(m_name_buf, m_file, 0, file_offset, false);
413
414 exists = success;
415 }
416
417 if (success) {
418 success = os_file_seek(m_name_buf, m_file.m_file, file_offset);
419 }
420
421 m_size = file_size == 0
422 ? (exists ? os_file_get_size(m_name_buf).m_total_size : 0)
423 : file_size;
424 ut_ad(m_offset <= m_size);
425
426 if (!success) {
427 close();
428 }
429
430 return (success ? DB_SUCCESS : DB_IO_ERROR);
431 }
432
433 dberr_t Arch_File_Ctx::open_new(lsn_t start_lsn, uint64_t new_file_size,
434 uint64_t initial_file_size) {
435 auto err = open(false, start_lsn, m_count, initial_file_size, new_file_size);
436 if (err != DB_SUCCESS) {
437 return err;
438 }
439 ++m_count;
440 return DB_SUCCESS;
441 }
442
443 dberr_t Arch_File_Ctx::open_next(lsn_t start_lsn, uint64_t file_offset,
444 uint64_t file_size) {
445 dberr_t error;
446
447 /* Get next file index. */
448 ++m_index;
449
450 /* Open next file. */
451 error = open(true, start_lsn, m_index, file_offset, file_size);
452
453 return (error);
454 }
455
456 dberr_t Arch_File_Ctx::read(byte *to_buffer, uint64_t offset, uint size) {
457 ut_ad(offset + size <= m_size);
458 ut_ad(!is_closed());
459
460 IORequest request(IORequest::READ);
461 request.disable_compression();
462 request.clear_encrypted();
463
464 auto err =
465 os_file_read(request, m_path_name, m_file, to_buffer, offset, size);
466
467 return (err);
468 }
469
470 dberr_t Arch_File_Ctx::resize_and_overwrite_with_zeros(uint64_t file_size) {
471 ut_ad(m_size <= file_size);
472
473 m_size = file_size;
474
475 byte *buf = static_cast<byte *>(
476 ut::zalloc_withkey(ut::make_psi_memory_key(mem_key_archive), file_size));
477
478 /* Make sure that the physical file size is the same as logical by filling
479 the file with all-zeroes. Page archiver recovery expects that the physical
480 file size is the same as logical file size. */
481 const dberr_t err = write(nullptr, buf, 0, file_size);
482
483 ut::free(buf);
484
485 if (err != DB_SUCCESS) {
486 return err;
487 }
488
489 flush();
490 return DB_SUCCESS;
491 }
492
493 dberr_t Arch_File_Ctx::write(Arch_File_Ctx *from_file, byte *from_buffer,
494 uint size) {
495 dberr_t err;
496
497 if (from_buffer == nullptr) {
498 /* write from File */
499 err = os_file_copy(from_file->m_file, from_file->m_offset, m_file, m_offset,
500 size);
501
502 if (err == DB_SUCCESS) {
503 from_file->m_offset += size;
504 ut_ad(from_file->m_offset <= from_file->m_size);
505 }
506
507 } else {
508 /* write from buffer */
509 IORequest request(IORequest::WRITE);
510 request.disable_compression();
511 request.clear_encrypted();
512
513 err = os_file_write(request, "Track file", m_file, from_buffer, m_offset,
514 size);
515 }
516
517 if (err != DB_SUCCESS) {
518 return (err);
519 }
520
521 m_offset += size;
522 ut_ad(m_offset <= m_size);
523
524 return (DB_SUCCESS);
525 }
526
527 void Arch_File_Ctx::build_name(uint idx, lsn_t dir_lsn, char *buffer,
528 uint length) {
529 char *buf_ptr;
530 uint buf_len;
531
532 /* If user has passed NULL, use pre-allocated buffer. */
533 if (buffer == nullptr) {
534 buf_ptr = m_name_buf;
535 buf_len = m_name_len;
536 } else {
537 buf_ptr = buffer;
538 buf_len = length;
539
540 strncpy(buf_ptr, m_name_buf, buf_len);
541 }
542
543 buf_ptr += m_base_len;
544 buf_len -= m_base_len;
545
546 if (m_dir_name == nullptr) {
547 snprintf(buf_ptr, buf_len, "%s%u", m_file_name, idx);
548
549 } else if (dir_lsn == LSN_MAX) {
550 snprintf(buf_ptr, buf_len, "%s%c%s%u", m_dir_name, OS_PATH_SEPARATOR,
551 m_file_name, idx);
552
553 } else {
554 snprintf(buf_ptr, buf_len, "%s" UINT64PF "%c%s%u", m_dir_name, dir_lsn,
555 OS_PATH_SEPARATOR, m_file_name, idx);
556 }
557 }
558
559 void Arch_File_Ctx::build_dir_name(lsn_t dir_lsn, char *buffer, uint length) {
560 ut_ad(buffer != nullptr);
561
562 if (m_dir_name != nullptr) {
563 snprintf(buffer, length, "%s%c%s" UINT64PF, m_path_name, OS_PATH_SEPARATOR,
564 m_dir_name, dir_lsn);
565 } else {
566 snprintf(buffer, length, "%s", m_path_name);
567 }
568 }
569
570 int start_log_archiver_background() {
571 bool ret;
572 char errbuf[MYSYS_STRERROR_SIZE];
573
574 ret = os_file_create_directory(ARCH_DIR, false);
575
576 if (ret) {
577 srv_threads.m_log_archiver =
578 os_thread_create(log_archiver_thread_key, 0, log_archiver_thread);
579
580 srv_threads.m_log_archiver.start();
581
582 } else {
583 my_error(ER_CANT_CREATE_FILE, MYF(0), ARCH_DIR, errno,
584 my_strerror(errbuf, sizeof(errbuf), errno));
585
586 return (ER_CANT_CREATE_FILE);
587 }
588
589 return (0);
590 }
591
592 int start_page_archiver_background() {
593 bool ret;
594 char errbuf[MYSYS_STRERROR_SIZE];
595
596 ret = os_file_create_directory(ARCH_DIR, false);
597
598 if (ret) {
599 srv_threads.m_page_archiver =
600 os_thread_create(page_archiver_thread_key, 0, page_archiver_thread);
601
602 srv_threads.m_page_archiver.start();
603
604 } else {
605 my_error(ER_CANT_CREATE_FILE, MYF(0), ARCH_DIR, errno,
606 my_strerror(errbuf, sizeof(errbuf), errno));
607
608 return (ER_CANT_CREATE_FILE);
609 }
610
611 return (0);
612 }
613
614 /** Archiver background thread */
615 void log_archiver_thread() {
616 Arch_File_Ctx log_file_ctx;
617 lsn_t log_arch_lsn = LSN_MAX;
618
619 bool log_abort = false;
620 bool log_wait = false;
621 bool log_init = true;
622
623 while (true) {
624 /* Archive available redo log data. */
625 log_abort = arch_log_sys->archive(log_init, &log_file_ctx, &log_arch_lsn,
626 &log_wait);
627
628 if (log_abort) {
629 ib::info(ER_IB_MSG_13) << "Exiting Log Archiver";
630 break;
631 }
632
633 log_init = false;
634
635 if (log_wait) {
636 /* Nothing to archive. Wait until next trigger. */
637 os_event_wait(log_archiver_thread_event);
638 os_event_reset(log_archiver_thread_event);
639 }
640 }
641 }