Debian Codegraph @

storage/innobase/include/log0types.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2013, 2026, Oracle and/or its affiliates.
4
5 Portions of this file contain modifications contributed and copyrighted by
6 Google, Inc. Those modifications are gratefully acknowledged and are described
7 briefly in the InnoDB documentation. The contributions by Google are
8 incorporated with their permission, and subject to the conditions contained in
9 the file COPYING.Google.
10
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License, version 2.0, as published by the
13 Free Software Foundation.
14
15 This program is designed to work with certain software (including
16 but not limited to OpenSSL) that is licensed under separate terms,
17 as designated in a particular file or component or in included license
18 documentation. The authors of MySQL hereby grant you an additional
19 permission to link the program and your derivative works with the
20 separately licensed software that they have either included with
21 the program or referenced in the documentation.
22
23 This program is distributed in the hope that it will be useful, but WITHOUT
24 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
25 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
26 for more details.
27
28 You should have received a copy of the GNU General Public License along with
29 this program; if not, write to the Free Software Foundation, Inc.,
30 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31
32 *****************************************************************************/
33
34 /**************************************************/ /**
35 @file include/log0types.h
36
37 Redo log basic types
38
39 *******************************************************/
40
41 #ifndef log0types_h
42 #define log0types_h
43
44 /* std::atomic<X> */
45 #include <atomic>
46
47 /* std::chrono::X */
48 #include <chrono>
49
50 /* std::string */
51 #include <string>
52
53 /* byte */
54 #include "univ.i"
55
56 /* os_offset_t */
57 #include "os0file.h"
58
59 /* ut::INNODB_CACHE_LINE_SIZE */
60 #include "ut0cpu_cache.h"
61
62 /** Type used for all log sequence number storage and arithmetic. */
63 typedef uint64_t lsn_t;
64
65 /** Log file id (0 for ib_redo0) */
66 typedef size_t Log_file_id;
67
68 /** Log flags (stored in file header of log file). */
69 typedef uint32_t Log_flags;
70
71 /** Number which tries to uniquely identify a created set of redo log files.
72 Redo log files, which have different values of Log_uuid, most likely have been
73 created for different directories and cannot be mixed. This way foreign redo
74 files might be easily recognized. When that is the case, most likely something
75 went wrong when copying files. */
76 typedef uint32_t Log_uuid;
77
78 /** Print format for lsn_t values, used in functions like printf. */
79 #define LSN_PF UINT64PF
80
81 /** Alias for atomic based on lsn_t. */
82 using atomic_lsn_t = std::atomic<lsn_t>;
83
84 /** Type used for sn values, which enumerate bytes of data stored in the log.
85 Note that these values skip bytes of headers and footers of log blocks. */
86 typedef uint64_t sn_t;
87
88 /** Alias for atomic based on sn_t. */
89 using atomic_sn_t = std::atomic<sn_t>;
90
91 /* The lsn_t, sn_t, Log_file_id are known so constants can be expressed. */
92 #include "log0constants.h"
93
94 /** Enumerates checkpoint headers in the redo log file. */
95 enum class Log_checkpoint_header_no : uint32_t {
96 /** The first checkpoint header. */
97 HEADER_1,
98
99 /** The second checkpoint header. */
100 HEADER_2
101 };
102
103 /** Type used for counters in log_t: flushes_requested and flushes_expected.
104 They represent number of requests to flush the redo log to disk. */
105 typedef std::atomic<int64_t> log_flushes_t;
106
107 /** Type of redo log file. */
108 enum class Log_file_type {
109 /** Usual redo log file, most likely with important redo data. */
110 NORMAL,
111 /** Unused redo log file, might always be removed. */
112 UNUSED
113 };
114
115 /** Callback called on each read or write operation on a redo log file.
116 @param[in] file_id id of the redo log file (target of the IO operation)
117 @param[in] file_type type of the redo log file
118 @param[in] offset offset in the file, at which read or write operation
119 is going to start (expressed in bytes and computed
120 from the beginning of the file)
121 @param[in] size size of data that is going to be read or written in
122 the IO operation */
123 typedef std::function<void(Log_file_id file_id, Log_file_type file_type,
124 os_offset_t offset, os_offset_t size)>
125 Log_file_io_callback;
126
127 /** Function used to calculate checksums of log blocks. */
128 typedef std::atomic<uint32_t (*)(const byte *log_block)>
129 Log_checksum_algorithm_atomic_ptr;
130
131 /** Clock used to measure time spent in redo log (e.g. when flushing). */
132 using Log_clock = std::chrono::high_resolution_clock;
133
134 /** Time point defined by the Log_clock. */
135 using Log_clock_point = std::chrono::time_point<Log_clock>;
136
137 /** Supported redo log formats. Stored in LOG_HEADER_FORMAT. */
138 enum class Log_format : uint32_t {
139 /** Unknown format of redo file. */
140 LEGACY = 0,
141
142 /** The MySQL 5.7.9 redo log format identifier. We can support recovery
143 from this format if the redo log is clean (logically empty). */
144 VERSION_5_7_9 = 1,
145
146 /** Remove MLOG_FILE_NAME and MLOG_CHECKPOINT, introduce MLOG_FILE_OPEN
147 redo log record. */
148 VERSION_8_0_1 = 2,
149
150 /** Allow checkpoint_lsn to point any data byte within redo log (before
151 it had to point the beginning of a group of log records). */
152 VERSION_8_0_3 = 3,
153
154 /** Expand ulint compressed form. */
155 VERSION_8_0_19 = 4,
156
157 /** Row versioning header. */
158 VERSION_8_0_28 = 5,
159
160 /** Introduced with innodb_redo_log_capacity:
161 - write LSN does not re-enter file with checkpoint_lsn,
162 - epoch_no is checked strictly during recovery. */
163 VERSION_8_0_30 = 6,
164
165 /** The redo log format identifier
166 corresponding to the current format version. */
167 CURRENT = VERSION_8_0_30
168 };
169
170 /** Ruleset defining how redo log files are named, where they are stored,
171 when they are created and what sizes could they have. */
172 enum class Log_files_ruleset {
173 /** Redo log files were named ib_logfile0, ib_logfile1, ... ib_logfile99.
174 Redo log files were pre-created during startup and re-used after wrapping.
175 Redo log files had the same file size and supported formats < VERSION_8_0_30.
176 The non-initialized set of redo log files was denoted by existence of the
177 ib_logfile101. The log files were located directly in the root directory
178 (innodb_log_group_home_dir if specified; else: datadir). */
179 PRE_8_0_30,
180
181 /** Redo log files are named #ib_redo0, #ib_redo1, ... and no longer wrapped.
182 Redo log files are created on-demand during runtime and might have different
183 sizes. Formats >= VERSION_8_0_30 are supported. The redo log files are located
184 in #innodb_redo subdirectory in the root directory - for example:
185 - if innodb_log_group_home_dir = '/srv/my_db/logs', then redo files are in
186 '/srv/my_db/logs/#innodb_redo/',
187 - if innodb_log_group_home_dir is not specified and datadir='/srv/my_db',
188 then redo files are in '/srv/my_db/#innodb_redo'. */
189 CURRENT
190 };
191
192 /** Direction of resize operation. */
193 enum class Log_resize_mode {
194 /** No pending resize. */
195 NONE,
196
197 /** Resizing down. */
198 RESIZING_DOWN
199 };
200
201 /** Configures path to the root directory, where redo subdirectory might be
202 located (or redo log files if the ruleset is older). Configures the ruleset
203 that should be used when locating redo log files. */
204 struct Log_files_context {
205 explicit Log_files_context(
206 const std::string &root_path = "",
207 Log_files_ruleset files_ruleset = Log_files_ruleset::CURRENT);
208
209 /** Path to the root directory. */
210 std::string m_root_path;
211
212 /** Ruleset determining how file paths are built. */
213 Log_files_ruleset m_files_ruleset;
214 };
215
216 /** Meta data stored in log file header. */
217 struct Log_file_header {
218 /** Format of the log file. */
219 uint32_t m_format;
220
221 /** LSN of the first log block (%512 == 0). */
222 lsn_t m_start_lsn;
223
224 /** Creator name. */
225 std::string m_creator_name;
226
227 /** Log flags. Meaning of bit positions is to be found in documentation
228 of LOG_HEADER_FLAG_* constants in log0constants.h. */
229 Log_flags m_log_flags;
230
231 /** UUID value describing the whole group of log files. */
232 Log_uuid m_log_uuid;
233 };
234
235 /** Meta data stored in one of two checkpoint headers. */
236 struct Log_checkpoint_header {
237 /** Checkpoint LSN (oldest_lsn_lwm from the moment of checkpoint). */
238 lsn_t m_checkpoint_lsn;
239 };
240
241 /** Meta data stored in header of a log data block. */
242 struct Log_data_block_header {
243 /** Together with m_hdr_no form unique identifier of this block,
244 @see LOG_BLOCK_EPOCH_NO. */
245 uint32_t m_epoch_no;
246
247 /** Together with m_epoch_no form unique identifier of this block,
248 @see log_block_get_hdr_no. Each next log data block has hdr_no
249 incremented by 1 (unless wrapped). */
250 uint32_t m_hdr_no;
251
252 /** Offset up to which this block has data inside, computed from the
253 beginning of the block. */
254 uint16_t m_data_len;
255
256 /** Offset to the first mtr starting in this block, or 0 if there is no
257 mtr starting in this block. */
258 uint16_t m_first_rec_group;
259
260 /** Sets m_epoch_no and m_hdr_no from a single lsn
261 */
262 void set_lsn(lsn_t lsn);
263 };
264
265 /** Pair of: log file id and log file size (expressed in bytes). */
266 struct Log_file_id_and_size {
267 Log_file_id_and_size() = default;
268
269 Log_file_id_and_size(Log_file_id id, os_offset_t size)
270 : m_id(id), m_size_in_bytes(size) {}
271
272 /** Id of the file. */
273 Log_file_id m_id{};
274
275 /** Size of file, expressed in bytes. */
276 os_offset_t m_size_in_bytes{};
277 };
278
279 /** Pair of: log file id and log file header. */
280 struct Log_file_id_and_header {
281 Log_file_id_and_header() = default;
282
283 Log_file_id_and_header(Log_file_id id, Log_file_header header)
284 : m_id(id), m_header(header) {}
285
286 /** Id of the file. */
287 Log_file_id m_id{};
288
289 /** Main header of the file. */
290 Log_file_header m_header{};
291 };
292
293 /** Type of access allowed for the opened redo log file. */
294 enum class Log_file_access_mode {
295 /** The opened file can be both read and written. */
296 READ_WRITE,
297 /** The opened file can be only read. */
298 READ_ONLY,
299 /** The opened file can be only written. */
300 WRITE_ONLY,
301 };
302
303 /** Handle which allows to do reads / writes for the opened file.
304 For particular kind of reads or writes (for checkpoint headers,
305 data blocks, main file header or encryption header) there are helper
306 functions defined outside this class. Unless you wanted to transfer
307 the whole file as-is, you should rather use those functions for
308 read/write operations. */
309 class Log_file_handle {
310 public:
311 explicit Log_file_handle(Encryption_metadata &encryption_metadata);
312
313 Log_file_handle(Log_file_handle &&other);
314 Log_file_handle &operator=(Log_file_handle &&rhs);
315
316 /** Closes handle if was opened (calling fsync if was modified).
317 Destructs the handle object. */
318 ~Log_file_handle();
319
320 /** @return true iff file is opened (by this handle) */
321 bool is_open() const;
322
323 /** Closes file represented by this handle (must be opened). */
324 void close();
325
326 /** @return id of the log file */
327 Log_file_id file_id() const;
328
329 /** @return path to the log file (including the file name) */
330 const std::string &file_path() const;
331
332 /** @return file size in bytes */
333 os_offset_t file_size() const;
334
335 /** Reads from the log file at the given offset (to the provided buffer).
336 @param[in] read_offset offset in bytes from the beginning of the file
337 @param[in] read_size number of bytes to read
338 @param[out] buf allocated buffer to fill when reading
339 @return DB_SUCCESS or error */
340 dberr_t read(os_offset_t read_offset, os_offset_t read_size, byte *buf);
341
342 /** Writes the provided buffer to the log file at the given offset.
343 @param[in] write_offset offset in bytes from the beginning of the file
344 @param[in] write_size number of bytes to write
345 @param[in] buf buffer to write
346 @return DB_SUCCESS or error */
347 dberr_t write(os_offset_t write_offset, os_offset_t write_size,
348 const byte *buf);
349
350 /** Executes fsync operation for this redo log file. */
351 void fsync();
352
353 /** @return number of fsyncs in-progress */
354 static uint64_t fsyncs_in_progress() { return s_fsyncs_in_progress.load(); }
355
356 /** @return total number of fsyncs that have been started since
357 the server has started */
358 static uint64_t total_fsyncs() { return s_total_fsyncs.load(); }
359
360 /** Callback called on each read operation. */
361 static Log_file_io_callback s_on_before_read;
362
363 /** Callback called on each write operation. */
364 static Log_file_io_callback s_on_before_write;
365
366 /** True iff all fsyncs should be no-op. */
367 static bool s_skip_fsyncs;
368
369 private:
370 friend struct Log_file;
371
372 /** Tries to open a given redo log file with with a given access mode.
373 If succeeded then this handle represents the opened file and allows to
374 performs reads and/or writes (depends on the requested access mode).
375 If encountered error during the attempt to open, error message is emitted
376 to the error log, in which case this handle remains closed.
377 @param[in] files_ctx context within which files exist
378 @param[in] id id of the log file
379 @param[in] access_mode access mode for the opened file
380 @param[in] encryption_metadata encryption metadata
381 @param[in] file_type type of redo file */
382 Log_file_handle(const Log_files_context &files_ctx, Log_file_id id,
383 Log_file_access_mode access_mode,
384 Encryption_metadata &encryption_metadata,
385 Log_file_type file_type);
386
387 Log_file_handle(const Log_file_handle &other) = delete;
388 Log_file_handle &operator=(const Log_file_handle &rhs) = delete;
389
390 /** Open the log file with the configured access mode.
391 @return DB_SUCCESS or error */
392 dberr_t open();
393
394 /** Creates and configures an io request object according to currently
395 configured encryption metadata (m_encryption_*) and m_block_size.
396 @param[in] req_type defines type of IO operation (read or write)
397 @param[in] offset offset in bytes from the beginning of the file
398 @param[in] size number of bytes to write or read
399 @param[in] can_use_encryption e.g. whether newly blocks should be encrypted
400 @return IORequest instance */
401 IORequest prepare_io_request(int req_type, os_offset_t offset,
402 os_offset_t size, bool can_use_encryption);
403
404 #ifdef UNIV_DEBUG
405 /** Number of all opened Log_file_handle existing currently. */
406 static std::atomic<size_t> s_n_open;
407 #endif /* UNIV_DEBUG */
408
409 /** Number of fsyncs in-progress. */
410 static std::atomic<uint64_t> s_fsyncs_in_progress;
411
412 /** Total number of fsyncs that have been started since
413 the server has started. */
414 static std::atomic<uint64_t> s_total_fsyncs;
415
416 /** Id of the redo log file (part of its file name) */
417 Log_file_id m_file_id;
418
419 /** Access mode allowed for this handle (if not yet closed). */
420 Log_file_access_mode m_access_mode;
421
422 /** Encryption metadata to be used for all IO operations on this file
423 except those related to the first LOG_FILE_HDR_SIZE bytes.
424 @remarks If Encryption::set_key() and Encryption::set_initial_vector()
425 started to accept their arguments as const pointers, this could become
426 a const pointer too). In such case, all usages of Encryption_metadata*
427 inside redo log code, could also become changed to usages of the const
428 pointer. */
429 Encryption_metadata &m_encryption_metadata;
430
431 /** Type of redo log file. */
432 Log_file_type m_file_type;
433
434 /** Whether file is opened */
435 bool m_is_open;
436
437 /** Whether file has been modified using this handle since it was opened. */
438 bool m_is_modified;
439
440 /** File name */
441 std::string m_file_path;
442
443 /** OS handle for file (if opened) */
444 pfs_os_file_t m_raw_handle;
445
446 /** Size of single physical block (if opened) */
447 os_offset_t m_block_size;
448
449 /** Size of file in bytes (if opened) */
450 os_offset_t m_file_size;
451 };
452
453 /** Meta information about single log file. */
454 struct Log_file {
455 Log_file(const Log_files_context &files_ctx,
456 Encryption_metadata &encryption_metadata);
457
458 Log_file(const Log_files_context &files_ctx, Log_file_id id, bool consumed,
459 bool full, os_offset_t size_in_bytes, lsn_t start_lsn, lsn_t end_lsn,
460 Encryption_metadata &encryption_metadata);
461
462 Log_file(const Log_file &other) = default;
463
464 Log_file &operator=(const Log_file &other);
465
466 /** Context within which this file exists. */
467 const Log_files_context &m_files_ctx;
468
469 /** ID of the file. */
470 Log_file_id m_id;
471
472 /** Set to true when file becomes consumed. */
473 bool m_consumed;
474
475 /** Set to true when file became full and next file exists. */
476 bool m_full;
477
478 /** Size, expressed in bytes, including LOG_FILE_HDR_SIZE. */
479 os_offset_t m_size_in_bytes;
480
481 /** LSN of the first byte within the file, aligned to
482 OS_FILE_LOG_BLOCK_SIZE. */
483 lsn_t m_start_lsn;
484
485 /** LSN of the first byte after the file, aligned to
486 OS_FILE_LOG_BLOCK_SIZE. */
487 lsn_t m_end_lsn;
488
489 /** Encryption metadata passed to opened file handles
490 @see Log_file_handle::m_encryption_metadata */
491 Encryption_metadata &m_encryption_metadata;
492
493 /** Checks if this object is equal to a given another object.
494 @param[in] rhs the object to compare against
495 @return true iff all related fields of the two objects are equal */
496 bool operator==(const Log_file &rhs) const {
497 return m_id == rhs.m_id && m_consumed == rhs.m_consumed &&
498 m_full == rhs.m_full && m_size_in_bytes == rhs.m_size_in_bytes &&
499 m_start_lsn == rhs.m_start_lsn && m_end_lsn == rhs.m_end_lsn;
500 }
501
502 /** Validates that lsn fields seem correct (m_start_lsn, m_end_lsn) */
503 void lsn_validate() const {
504 ut_a(m_start_lsn == 0 || LOG_START_LSN <= m_start_lsn);
505 ut_a(m_start_lsn < m_end_lsn);
506 ut_a(m_start_lsn % OS_FILE_LOG_BLOCK_SIZE == 0);
507 ut_a(m_end_lsn % OS_FILE_LOG_BLOCK_SIZE == 0);
508 }
509
510 /** Checks if a given lsn belongs to [m_start_lsn, m_end_lsn).
511 In other words, checks that the given lsn belongs to this file.
512 @param[in] lsn lsn to test against lsn range
513 @return true iff lsn is in the file */
514 bool contains(lsn_t lsn) const {
515 return m_start_lsn <= lsn && lsn < m_end_lsn;
516 }
517
518 /** Provides offset for the given LSN (from the beginning of the log file).
519 @param[in] lsn lsn to locate (must exist in the file)
520 @return offset from the beginning of the file for the given lsn */
521 os_offset_t offset(lsn_t lsn) const {
522 lsn_validate();
523 ut_a(contains(lsn) || lsn == m_end_lsn);
524 return offset(lsn, m_start_lsn);
525 }
526
527 /** Provides offset for the given LSN and log file with the given start_lsn
528 (offset from the beginning of the log file).
529 @param[in] lsn lsn to locate (must be >= file_start_lsn)
530 @param[in] file_start_lsn start lsn of the log file
531 @return offset from the beginning of the file for the given lsn */
532 static os_offset_t offset(lsn_t lsn, lsn_t file_start_lsn) {
533 return LOG_FILE_HDR_SIZE + (lsn - file_start_lsn);
534 }
535
536 /** Computes id of the next log file. Does not check if such file exists.
537 @return id of the next log file */
538 Log_file_id next_id() const { return next_id(m_id, 1); }
539
540 /** Opens this file and provides handle that allows to read from this file
541 and / or write to this file (depends on the requested access mode).
542 @param[in] access_mode requested access mode (reads and/or writes)
543 @return handle to the opened file or empty handle with error information */
544 Log_file_handle open(Log_file_access_mode access_mode) const;
545
546 /** Opens a given redo log file and provides handle that allows to read from
547 that file and / or write to that file (depends on the requested access mode).
548 @param[in] files_ctx context within which file exists
549 @param[in] file_id id of the redo log file to open
550 @param[in] access_mode requested access mode (reads and/or writes)
551 @param[in] encryption_metadata encryption metadata
552 @param[in] file_type type of underlying file on disk to open
553 @return handle to the opened file or empty handle with error information */
554 static Log_file_handle open(const Log_files_context &files_ctx,
555 Log_file_id file_id,
556 Log_file_access_mode access_mode,
557 Encryption_metadata &encryption_metadata,
558 Log_file_type file_type = Log_file_type::NORMAL);
559
560 /** Computes id + inc, asserting it does not overflow the maximum value.
561 @param[in] id base id of file, to which inc is added
562 @param[in] inc number of log files ahead (1 = directly next one)
563 @return id + inc */
564 static Log_file_id next_id(Log_file_id id, size_t inc = 1) {
565 constexpr Log_file_id MAX_FILE_ID = std::numeric_limits<Log_file_id>::max();
566 ut_a(0 < inc);
567 ut_a(inc <= MAX_FILE_ID);
568 ut_a(id <= MAX_FILE_ID - inc);
569 return id + inc;
570 }
571 };
572
573 struct alignas(ut::INNODB_CACHE_LINE_SIZE) log_t;
574
575 /** Runtime statistics related to redo log files management. These stats are
576 not persisted to disk. */
577 struct Log_files_stats {
578 /** Last time stats were updated (last successful call to @see update()). */
579 Log_clock_point m_last_update_time{};
580
581 /** LSN difference by which result of log_files_oldest_needed_lsn() advanced
582 during last second. This is basically average consumption speed.
583 Updated by successful calls to @see update(). */
584 lsn_t m_lsn_consumption_per_1s{0};
585
586 /** LSN difference by which result of log_files_newest_needed_lsn() advanced
587 during last second. This is basically average production speed.
588 Updated by successful calls to @see update(). */
589 lsn_t m_lsn_production_per_1s{0};
590
591 /** Oldest LSN returned by log_files_oldest_needed_lsn() during last
592 successful call to @see update(). */
593 lsn_t m_oldest_lsn_on_update{0};
594
595 /** Newest LSN returned by log_files_newest_needed_lsn() during last
596 successful call to @see update(). */
597 lsn_t m_newest_lsn_on_update{0};
598
599 /** Tries to update stats. Fails and skips updating if less than 1s elapsed
600 since last successful update, else: updates the stats and succeeds.
601 @param[in] log redo log */
602 void update(const log_t &log);
603 };
604
605 #endif /* !log0types_h */