Debian Codegraph @

storage/innobase/include/buf0dblwr.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 1995, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file include/buf0dblwr.h
29 Doublewrite buffer module
30
31 Created 2011/12/19 Inaam Rana
32 *******************************************************/
33
34 #ifndef buf0dblwr_h
35 #define buf0dblwr_h
36
37 #include "buf0types.h"
38 #include "fil0fil.h"
39 #include "log0recv.h"
40 #include "ut0byte.h"
41
42 /** Size of the doublewrite block in pages. */
43 #define DBLWR_V1_EXTENT_SIZE FSP_EXTENT_SIZE
44
45 /** Offset of the doublewrite buffer header on the trx system header page. */
46 #define TRX_SYS_DBLWR_V1 (UNIV_PAGE_SIZE - 200)
47
48 /** 4-byte ver number which shows if we have created the doublewrite buffer. */
49 constexpr ulint DBLWR_VER = FSEG_HEADER_SIZE;
50
51 /** Page number of the first page in the first sequence of 64 (=
52 FSP_EXTENT_SIZE) consecutive pages in the doublewrite buffer. */
53 constexpr ulint DBLWR_V1_BLOCK1 = (4 + FSEG_HEADER_SIZE);
54
55 /** Page number of the first page in the second sequence of 64 consecutive
56 pages in the doublewrite buffer. */
57 constexpr ulint DBLWR_V1_BLOCK2 = (8 + FSEG_HEADER_SIZE);
58
59 namespace dblwr {
60
61 /** IO buffer in UNIV_PAGE_SIZE units and aligned on UNIV_PAGE_SIZE */
62 struct Buffer {
63 /** Constructor
64 @param[in] n_pages Number of pages to create */
65 explicit Buffer(size_t n_pages) noexcept
66 : Buffer(n_pages, univ_page_size.physical()) {}
67
68 /** Constructor
69 @param[in] n_pages Number of pages to create
70 @param[in] phy_size physical page size. */
71 explicit Buffer(size_t n_pages, uint32_t phy_size) noexcept
72 : m_phy_size(phy_size), m_n_bytes(n_pages * phy_size) {
73 ut_a(n_pages > 0);
74 m_ptr = static_cast<byte *>(ut::aligned_zalloc(m_n_bytes, phy_size));
75
76 m_next = m_ptr;
77 }
78
79 /** Destructor */
80 ~Buffer() noexcept {
81 ut::aligned_free(m_ptr);
82 m_ptr = nullptr;
83 }
84
85 /** Add the contents of ptr up to n_bytes to the buffer.
86 @return false if it won't fit. Nothing is copied if it won't fit. */
87 bool append(const void *ptr, size_t n_bytes) noexcept {
88 ut_a(m_next >= m_ptr && m_next <= m_ptr + m_n_bytes);
89
90 if (m_next + m_phy_size > m_ptr + m_n_bytes) {
91 return false;
92 }
93
94 memcpy(m_next, ptr, n_bytes);
95 m_next += m_phy_size;
96
97 return true;
98 }
99
100 /** @return the start of the buffer to write from. */
101 byte *begin() noexcept { return m_ptr; }
102
103 /** @return the start of the buffer to write from. */
104 const byte *begin() const noexcept { return m_ptr; }
105
106 /** @return the size of of the buffer to write. */
107 size_t size() const noexcept {
108 ut_a(m_next >= m_ptr);
109 return std::ptrdiff_t(m_next - m_ptr);
110 }
111
112 /** @return the capacity of the buffer in bytes. */
113 size_t capacity() const noexcept { return m_n_bytes; }
114
115 /** @return true if the buffer is empty. */
116 bool empty() const noexcept { return size() == 0; }
117
118 /** Empty the buffer. */
119 void clear() noexcept { m_next = m_ptr; }
120
121 /** Page size on disk (aka physical page size). */
122 uint32_t m_phy_size;
123
124 /** Write buffer used in writing to the doublewrite buffer,
125 aligned to an address divisible by UNIV_PAGE_SIZE (which is
126 required by Windows AIO) */
127 byte *m_ptr{};
128
129 /** Start of next write to the buffer. */
130 byte *m_next{};
131
132 /** Size of the unaligned (raw) buffer. */
133 const size_t m_n_bytes{};
134
135 // Disable copying
136 Buffer(const Buffer &) = delete;
137 Buffer(Buffer &&) = delete;
138 Buffer &operator=(Buffer &&) = delete;
139 Buffer &operator=(const Buffer &) = delete;
140 };
141
142 } // namespace dblwr
143
144 #ifndef UNIV_HOTBACKUP
145
146 // Forward declaration
147 class buf_page_t;
148 class Double_write;
149
150 namespace dblwr {
151
152 /** Double write files location. */
153 extern std::string dir;
154
155 #ifdef UNIV_DEBUG
156 /** Crash the server after writing this page to the data file. */
157 extern page_id_t Force_crash;
158 #endif /* UNIV_DEBUG */
159
160 /** Startup the background thread(s) and create the instance.
161 @return DB_SUCCESS or error code */
162 [[nodiscard]] dberr_t open() noexcept;
163
164 /** Enable the doublewrite reduced mode by creating the necessary dblwr files
165 and in-memory structures
166 @return DB_SUCCESS or error code */
167 [[nodiscard]] dberr_t enable_reduced() noexcept;
168
169 /** Check and open the reduced doublewrite files if necessary
170 @return DB_SUCCESS or error code */
171 [[nodiscard]] dberr_t reduced_open() noexcept;
172
173 /** Shutdown the background thread and destroy the instance */
174 void close() noexcept;
175
176 /** Force a write of all pages in the queue.
177 @param[in] flush_type FLUSH LIST or LRU_LIST flush request.
178 @param[in] buf_pool_index Buffer pool instance for which called. */
179 void force_flush(buf_flush_t flush_type, uint32_t buf_pool_index) noexcept;
180
181 /** Force a write of all pages in all dblwr segments (reduced or regular)
182 This is used only when switching the doublewrite mode dynamically */
183 void force_flush_all() noexcept;
184
185 /** Writes a page to the doublewrite buffer on disk, syncs it,
186 then writes the page to the datafile.
187 @param[in] flush_type Flush type
188 @param[in] bpage Buffer block to write
189 @param[in] sync True if sync IO requested
190 @return DB_SUCCESS or error code */
191 [[nodiscard]] dberr_t write(buf_flush_t flush_type, buf_page_t *bpage,
192 bool sync) noexcept;
193
194 /** Obtain the encrypted frame and store it in bpage->m_io_frame
195 @param[in,out] bpage the buffer page containing the unencrypted frame.
196 @return the memory block containing the compressed + encrypted frame. */
197 [[nodiscard]] file::Block *get_encrypted_frame(buf_page_t *bpage) noexcept;
198
199 /** Updates the double write buffer when a write request is completed.
200 @param[in] bpage Block that has just been writtent to disk.
201 @param[in] flush_type Flush type that triggered the write. */
202 void write_complete(buf_page_t *bpage, buf_flush_t flush_type) noexcept;
203
204 /** Delete or adjust the dblwr file size if required. */
205 void reset_files() noexcept;
206
207 namespace v1 {
208 /** Read the boundaries of the legacy dblwr buffer extents.
209 @return DB_SUCCESS or error code. */
210 [[nodiscard]] dberr_t init() noexcept;
211
212 /** Create the dblwr data structures in the system tablespace.
213 @return DB_SUCCESS or error code. */
214 [[nodiscard]] dberr_t create() noexcept;
215
216 /** Check if the read is of a page inside the legacy dblwr buffer.
217 @param[in] page_no Page number to check.
218 @return true if offset inside legacy dblwr buffer. */
219 [[nodiscard]] bool is_inside(page_no_t page_no) noexcept;
220
221 } // namespace v1
222 } // namespace dblwr
223
224 #endif /* !UNIV_HOTBACKUP */
225
226 namespace dblwr {
227
228 /** Number of pages per doublewrite thread/segment */
229 extern ulong n_pages;
230
231 const uint32_t REDUCED_BATCH_PAGE_SIZE = 8192;
232
233 /* 20-byte header.
234 Fields : [batch id][checksum][data len][batch type][unused ]
235 Field Width : [4 bytes ][4 bytes ][2 bytes ][ 1 byte ][ 9 bytes]
236 Field Offsets : [ 0 ][ 4 ][ 8 ][ 10 ][ 11 ] */
237 const uint32_t RB_BATCH_ID_SIZE = 4;
238 const uint32_t RB_CHECKSUM_SIZE = 4;
239 const uint32_t RB_DATA_LEN_SIZE = 2;
240 const uint32_t RB_BATCH_TYPE_SIZE = 1;
241 const uint32_t RB_UNUSED_BYTES_SIZE = 9;
242
243 /* Offsets of the header fields. */
244 constexpr const uint32_t RB_OFF_BATCH_ID = 0;
245 constexpr const uint32_t RB_OFF_CHECKSUM = RB_OFF_BATCH_ID + RB_BATCH_ID_SIZE;
246 constexpr const uint32_t RB_OFF_DATA_LEN = RB_OFF_CHECKSUM + RB_CHECKSUM_SIZE;
247 constexpr const uint32_t RB_OFF_BATCH_TYPE = RB_OFF_DATA_LEN + RB_DATA_LEN_SIZE;
248 constexpr const uint32_t RB_OFF_UNUSED = RB_OFF_BATCH_TYPE + RB_BATCH_TYPE_SIZE;
249
250 constexpr const uint32_t REDUCED_HEADER_SIZE =
251 RB_BATCH_ID_SIZE /* BATCH_ID */
252 + RB_CHECKSUM_SIZE /* CHECKSUM */
253 + RB_DATA_LEN_SIZE /* DATA_LEN */
254 + RB_BATCH_TYPE_SIZE /* BATCH_TYPE */
255 + 9 /* UNUSED BYTES */;
256
257 constexpr const uint32_t REDUCED_ENTRY_SIZE =
258 sizeof(space_id_t) + sizeof(page_no_t) + sizeof(lsn_t);
259
260 constexpr const uint32_t REDUCED_DATA_SIZE =
261 REDUCED_BATCH_PAGE_SIZE - REDUCED_HEADER_SIZE;
262
263 constexpr const uint32_t REDUCED_MAX_ENTRIES =
264 REDUCED_DATA_SIZE / REDUCED_ENTRY_SIZE;
265
266 /** When --innodb-doublewrite=DETECT_ONLY, page contents are not written to the
267 dblwr buffer. Only the following Reduced_entry information is stored in the
268 dblwr buffer. */
269 struct Reduced_entry {
270 space_id_t m_space_id;
271 page_no_t m_page_no;
272 lsn_t m_lsn;
273
274 Reduced_entry(buf_page_t *bpage);
275
276 Reduced_entry(space_id_t space_id, page_no_t page_no, lsn_t lsn)
277 : m_space_id(space_id), m_page_no(page_no), m_lsn(lsn) {}
278
279 byte *write(byte *ptr) {
280 uint16_t offset = 0;
281 mach_write_to_4(ptr + offset, m_space_id);
282 offset += sizeof(m_space_id);
283
284 mach_write_to_4(ptr + offset, m_page_no);
285 offset += sizeof(m_page_no);
286
287 mach_write_to_8(ptr + offset, m_lsn);
288 offset += sizeof(m_lsn);
289
290 return ptr + offset;
291 }
292 };
293
294 struct Mode {
295 /** The operating mode of doublewrite. The modes ON, TRUEE and
296 DETECT_AND_RECOVER are equal to one another. The modes OFF and FALSEE are
297 equal to one another.
298
299 @note If you change order or add new values, please update
300 innodb_doublewrite_names enum in handler/ha_innodb.cc */
301 enum mode_t {
302 /** Equal to FALSEE. In this mode, dblwr is disabled. */
303 OFF,
304
305 /** Equal to TRUEE and DETECT_AND_RECOVER modes. */
306 ON,
307
308 /** In this mode, dblwr is used only to detect torn writes. At code level,
309 this mode is also called as reduced mode. It is called reduced because the
310 number of bytes written to the dblwr file is reduced in this mode. */
311 DETECT_ONLY,
312
313 /** This mode is synonymous with ON, TRUEE. */
314 DETECT_AND_RECOVER,
315
316 /** Equal to OFF mode. Intentionally wrong spelling because of compilation
317 issues on Windows. */
318 FALSEE,
319
320 /** Equal to ON, DETECT_AND_RECOVER mode. Intentionally wrong spelling
321 because of compilation issues on Windows platform. */
322 TRUEE
323 };
324
325 /** Check if doublewrite is enabled.
326 @param[in] mode dblwr ENUM
327 @return true if dblwr is enabled. */
328 static inline bool is_enabled_low(ulong mode);
329
330 /** Check if the doublewrite mode is disabled.
331 @param[in] mode dblwr ENUM
332 @return true if dblwr mode is OFF. */
333 static inline bool is_disabled_low(ulong mode);
334
335 /** Check if the doublewrite mode is detect-only (aka reduced).
336 @param[in] mode dblwr ENUM
337 @return true if dblwr mode is DETECT_ONLY. */
338 static inline bool is_reduced_low(ulong mode);
339
340 /** Check if the dblwr mode provides atomic writes.
341 @return true if mode is ON, TRUEE or DETECT_AND_RECOVER.
342 @return false if mode is OFF, FALSE or DETECT_ONLY. */
343 static inline bool is_atomic(ulong mode);
344
345 /** Convert the dblwr mode into a string representation.
346 @param[in] mode the dblwr mode.
347 @return string representation of the dblwr mode. */
348 static const char *to_string(ulong mode);
349
350 /** Check if the mode transition is from enabled to disabled.
351 @param[in] new_value the new value of dblwr mode.
352 @return true if mode transition is from enabled to disabled. */
353 static inline bool is_enabled_to_disabled(ulong new_value);
354
355 /** Check if the mode transition is from disabled to enabled.
356 @param[in] new_value the new value of dblwr mode.
357 @return true if mode transition is from disabled to enabled. */
358 static inline bool is_disabled_to_enabled(ulong new_value);
359
360 /** Check if the mode transition is equivalent.
361 @param[in] new_value the new value of dblwr mode.
362 @return true if mode transition is equivalent. */
363 static bool is_same(ulong new_value);
364 };
365
366 bool Mode::is_atomic(ulong mode) {
367 return mode == ON || mode == TRUEE || mode == DETECT_AND_RECOVER;
368 }
369
370 bool Mode::is_enabled_low(ulong mode) {
371 return mode == ON || mode == TRUEE || mode == DETECT_AND_RECOVER ||
372 mode == DETECT_ONLY;
373 }
374
375 bool Mode::is_disabled_low(ulong mode) { return mode == OFF || mode == FALSEE; }
376
377 bool Mode::is_reduced_low(ulong mode) { return mode == DETECT_ONLY; }
378
379 /** DBLWR mode. */
380 extern ulong g_mode;
381
382 /** true if DETECT_ONLY (aka reduced) mode is inited */
383 extern bool is_reduced_inited;
384
385 /** Check if doublewrite is enabled.
386 @return true if dblwr mode is ON, DETECT_ONLY, DETECT_AND_RECOVER
387 @return false if dblwr mode is OFF. */
388 inline bool is_enabled() { return Mode::is_enabled_low(g_mode); }
389
390 /** Check if the doublewrite mode is detect-only (aka reduced).
391 @return true if dblwr mode is DETECT_ONLY. */
392 inline bool is_reduced() { return Mode::is_reduced_low(g_mode); }
393
394 /** Check if the doublewrite mode is disabled.
395 @return true if dblwr mode is OFF. */
396 inline bool is_disabled() { return Mode::is_disabled_low(g_mode); }
397
398 bool Mode::is_enabled_to_disabled(ulong new_value) {
399 return is_enabled() && Mode::is_disabled_low(new_value);
400 }
401
402 bool Mode::is_disabled_to_enabled(ulong new_value) {
403 return is_disabled() && Mode::is_enabled_low(new_value);
404 }
405
406 /** @return string version of dblwr numeric values
407 @param[in] mode dblwr ENUM */
408 const char *to_string(ulong mode);
409
410 /** Number of files to use for the double write buffer. It must be <= than
411 the number of buffer pool instances. */
412 extern ulong n_files;
413
414 /** Maximum number of pages to write in one batch. */
415 extern ulong batch_size;
416
417 /** Toggle the doublewrite buffer. */
418 void set();
419
420 namespace recv {
421
422 class Pages;
423
424 /** Create the recovery dblwr data structures
425 @param[out] pages Pointer to newly created instance */
426 void create(Pages *&pages) noexcept;
427
428 /** Load the doublewrite buffer pages.
429 @param[in,out] pages For storing the doublewrite pages read
430 from the double write buffer
431 @return DB_SUCCESS or error code */
432 [[nodiscard]] dberr_t load(Pages *pages) noexcept;
433
434 /** Load the doublewrite buffer pages.
435 @param[in,out] pages For storing the doublewrite pages read
436 from the double write buffer
437 @return DB_SUCCESS or error code */
438 [[nodiscard]] dberr_t reduced_load(Pages *pages) noexcept;
439
440 /** Restore pages from the double write buffer to the tablespace.
441 @param[in,out] pages Pages from the doublewrite buffer
442 @param[in] space Tablespace pages to restore, if set to nullptr then try and
443 restore all. */
444 void recover(Pages *pages, fil_space_t *space) noexcept;
445
446 /** Find a doublewrite copy of a page.
447 @param[in] pages Pages read from the doublewrite buffer
448 @param[in] page_id Page number to lookup
449 @return page frame
450 @retval NULL if no page was found */
451 [[nodiscard]] const byte *find(const Pages *pages,
452 const page_id_t &page_id) noexcept;
453
454 /** Find the LSN of the given page id in the dblwr.
455 @param[in] pages Pages read from the doublewrite buffer
456 @param[in] page_id Page number to lookup
457 @return 0th element is true if page_id found in double write buffer.
458 @return 1st element is valid only if 0th element is true.
459 @return 1st element contains the LSN of the page in dblwr. */
460 [[nodiscard]] std::tuple<bool, lsn_t> find_entry(
461 const Pages *pages, const page_id_t &page_id) noexcept;
462
463 /** Check if some pages from the double write buffer could not be
464 restored because of the missing tablespace IDs.
465 @param[in] pages Pages to check */
466 void check_missing_tablespaces(const Pages *pages) noexcept;
467
468 /** Free the recovery dblwr data structures
469 @param[out] pages Free the instance */
470 void destroy(Pages *&pages) noexcept;
471
472 /** Redo recovery configuration. */
473 class DBLWR {
474 public:
475 /** Constructor. */
476 explicit DBLWR() noexcept { create(m_pages); }
477
478 /** Destructor */
479 ~DBLWR() noexcept { destroy(m_pages); }
480
481 /** @return true if empty. */
482 bool empty() const noexcept { return (m_pages == nullptr); }
483
484 /** Load the doublewrite buffer pages. Doesn't create the doublewrite
485 @return DB_SUCCESS or error code */
486 [[nodiscard]] dberr_t load() noexcept { return (dblwr::recv::load(m_pages)); }
487
488 /** Load the doublewrite buffer pages. Doesn't create the doublewrite
489 @return DB_SUCCESS or error code */
490 [[nodiscard]] dberr_t reduced_load() noexcept {
491 return (dblwr::recv::reduced_load(m_pages));
492 }
493
494 /** Restore pages from the double write buffer to the tablespace.
495 @param[in] space Tablespace pages to restore,
496 if set to nullptr then try
497 and restore all. */
498 void recover(fil_space_t *space = nullptr) noexcept {
499 dblwr::recv::recover(m_pages, space);
500 }
501
502 /** Find a doublewrite copy of a page.
503 @param[in] page_id Page number to lookup
504 @return page frame
505 @retval nullptr if no page was found */
506 [[nodiscard]] const byte *find(const page_id_t &page_id) noexcept {
507 return (dblwr::recv::find(m_pages, page_id));
508 }
509
510 /** Find the LSN of the given page id in the dblwr.
511 @param[in] page_id Page number to lookup
512 @return 0th element is true if page_id found in double write buffer.
513 @return 1st element is valid only if 0th element is true.
514 @return 1st element contains the LSN of the page in dblwr. */
515 [[nodiscard]] std::tuple<bool, lsn_t> find_entry(
516 const page_id_t &page_id) noexcept {
517 return (dblwr::recv::find_entry(m_pages, page_id));
518 }
519
520 /** Check if some pages from the double write buffer
521 could not be restored because of the missing tablespace IDs. */
522 void check_missing_tablespaces() noexcept {
523 dblwr::recv::check_missing_tablespaces(m_pages);
524 }
525
526 #ifndef UNIV_HOTBACKUP
527 /** Note that recovery is complete. Adjust the file sizes if necessary. */
528 void recovered() noexcept { dblwr::reset_files(); }
529 #endif /* !UNIV_HOTBACKUP */
530
531 /** Disably copying. */
532 DBLWR(const DBLWR &) = delete;
533 DBLWR(DBLWR &&) = delete;
534 DBLWR &operator=(DBLWR &&) = delete;
535 DBLWR &operator=(const DBLWR &) = delete;
536
537 private:
538 /** Pages read from the double write file. */
539 Pages *m_pages{};
540 };
541 } // namespace recv
542
543 #ifdef UNIV_DEBUG
544 /** Check if the dblwr files contain encrypted pages.
545 @return true if dblwr file contains any encrypted pages,
546 false if dblwr file contains no encrypted pages. */
547 [[nodiscard]] bool has_encrypted_pages() noexcept;
548 #endif /* UNIV_DEBUG */
549 } // namespace dblwr
550
551 #endif /* buf0dblwr_h */