| 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 buf/buf0dblwr.cc |
| 29 |
Atomic writes handling. */ |
| 30 |
|
| 31 |
#include <sys/types.h> |
| 32 |
|
| 33 |
#include "buf0buf.h" |
| 34 |
#include "buf0checksum.h" |
| 35 |
#include "log0chkp.h" |
| 36 |
#include "os0enc.h" |
| 37 |
#include "os0thread-create.h" |
| 38 |
#include "page0zip.h" |
| 39 |
#include "srv0srv.h" |
| 40 |
#include "srv0start.h" |
| 41 |
#include "ut0mpmcbq.h" |
| 42 |
#include "ut0mutex.h" |
| 43 |
#include "ut0test.h" |
| 44 |
|
| 45 |
#include <iomanip> |
| 46 |
#include <iostream> |
| 47 |
#include <regex> |
| 48 |
#include <vector> |
| 49 |
|
| 50 |
/** Doublewrite buffer */ |
| 51 |
|
| 52 |
/** fseg header of the fseg containing the doublewrite buffer */ |
| 53 |
constexpr ulint DBLWR_V1_FSEG = 0; |
| 54 |
|
| 55 |
/** We repeat DBLWR_VER, DBLWR_V1_BLOCK1, DBLWR_V1_BLOCK2 so that if the trx |
| 56 |
sys header is half-written to disk, we still may be able to recover the |
| 57 |
information. */ |
| 58 |
constexpr ulint DBLWR_V1_REPEAT = 12; |
| 59 |
|
| 60 |
/** If this is not yet set to DBLWR_V1_SPACE_ID_STORED_N, |
| 61 |
we must reset the doublewrite buffer, because starting from 4.1.x the |
| 62 |
space id of a data page is stored into FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID. */ |
| 63 |
constexpr ulint DBLWR_V1_SPACE_ID_STORED = (24 + FSEG_HEADER_SIZE); |
| 64 |
|
| 65 |
/** Contents of DBLWR_VER. Legacy version, stores the blocks in the system |
| 66 |
tablespace. */ |
| 67 |
constexpr ulint DBLWR_V1 = 536853855; |
| 68 |
|
| 69 |
/** Contents of DBLWR_V1_SPACE_ID_STORED. */ |
| 70 |
constexpr ulint DBLWR_V1_SPACE_ID_STORED_N = 1783657386; |
| 71 |
|
| 72 |
/** DBLWR file pages reserved per instance for single page flushes. */ |
| 73 |
constexpr uint32_t SYNC_PAGE_FLUSH_SLOTS = 512; |
| 74 |
|
| 75 |
namespace dblwr { |
| 76 |
|
| 77 |
std::string dir{"."}; |
| 78 |
|
| 79 |
ulong n_files{1}; |
| 80 |
|
| 81 |
ulong batch_size{}; |
| 82 |
|
| 83 |
ulong n_pages{64}; |
| 84 |
|
| 85 |
ulong g_mode{Mode::ON}; |
| 86 |
|
| 87 |
bool is_reduced_inited = false; |
| 88 |
|
| 89 |
const char *Mode::to_string(ulong mode) { |
| 90 |
switch (mode) { |
| 91 |
case OFF: |
| 92 |
return ("OFF"); |
| 93 |
case ON: |
| 94 |
return ("ON"); |
| 95 |
case DETECT_ONLY: |
| 96 |
return ("DETECT_ONLY"); |
| 97 |
case DETECT_AND_RECOVER: |
| 98 |
return ("DETECT_AND_RECOVER"); |
| 99 |
case TRUEE: |
| 100 |
return "TRUE"; |
| 101 |
case FALSEE: |
| 102 |
return "FALSE"; |
| 103 |
} |
| 104 |
ut_ad(0); |
| 105 |
return (""); |
| 106 |
} |
| 107 |
|
| 108 |
bool Mode::is_same(ulong new_value) { |
| 109 |
switch (g_mode) { |
| 110 |
case OFF: |
| 111 |
case FALSEE: |
| 112 |
if (new_value == OFF || new_value == FALSEE) { |
| 113 |
return true; |
| 114 |
} |
| 115 |
break; |
| 116 |
case ON: |
| 117 |
case DETECT_AND_RECOVER: |
| 118 |
case TRUEE: |
| 119 |
if (new_value == ON || new_value == TRUEE || |
| 120 |
new_value == DETECT_AND_RECOVER) { |
| 121 |
return true; |
| 122 |
} |
| 123 |
break; |
| 124 |
case DETECT_ONLY: |
| 125 |
if (new_value == DETECT_ONLY) { |
| 126 |
return true; |
| 127 |
} |
| 128 |
break; |
| 129 |
} |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
/** Legacy dblwr buffer first segment page number. */ |
| 134 |
static page_no_t LEGACY_PAGE1; |
| 135 |
|
| 136 |
/** Legacy dblwr buffer second segment page number. */ |
| 137 |
static page_no_t LEGACY_PAGE2; |
| 138 |
|
| 139 |
inline bool is_odd(uint32_t val) { return val & 1; } |
| 140 |
|
| 141 |
struct File { |
| 142 |
/** Check if the file ID is an odd number. |
| 143 |
@return true if the file ID is odd. */ |
| 144 |
bool is_odd() const { return m_id & 1; } |
| 145 |
|
| 146 |
/** Files with odd number as ID are used for LRU batch segments. |
| 147 |
Also, only these odd files are used for LRU single segments. |
| 148 |
@return true if the file is to be used for LRU batch segments. */ |
| 149 |
bool is_for_lru() const { return is_odd(); } |
| 150 |
|
| 151 |
/** ID of the file. */ |
| 152 |
uint32_t m_id{}; |
| 153 |
|
| 154 |
/** File name. */ |
| 155 |
std::string m_name{}; |
| 156 |
|
| 157 |
/** File handle. */ |
| 158 |
pfs_os_file_t m_pfs{}; |
| 159 |
|
| 160 |
/** Number of batched pages per doublwrite file. */ |
| 161 |
static uint32_t s_n_pages; |
| 162 |
|
| 163 |
/** Serialize the object into JSON format. |
| 164 |
@return the object in JSON format. */ |
| 165 |
[[nodiscard]] std::string to_json() const noexcept { |
| 166 |
std::ostringstream out; |
| 167 |
out << "{"; |
| 168 |
out << "\"className\": \"dblwr::File\","; |
| 169 |
out << "\"m_id\": \"" << m_id << "\","; |
| 170 |
out << "\"m_name\": \"" << m_name << "\","; |
| 171 |
out << "\"s_n_pages\": \"" << s_n_pages << "\""; |
| 172 |
out << "}"; |
| 173 |
|
| 174 |
return out.str(); |
| 175 |
} |
| 176 |
|
| 177 |
/** Print this object into the given stream. |
| 178 |
@param[in] out output stream into which the current object is printed. |
| 179 |
@return the output stream. */ |
| 180 |
std::ostream &print(std::ostream &out) const noexcept { |
| 181 |
out << to_json(); |
| 182 |
return out; |
| 183 |
} |
| 184 |
}; |
| 185 |
|
| 186 |
/** Overload the global output operator to work with dblwr::File type. |
| 187 |
@param[in] out output stream into which the object is printed. |
| 188 |
@param[in] obj An object to type dblwr::File. |
| 189 |
@return the output stream. */ |
| 190 |
inline std::ostream &operator<<(std::ostream &out, const File &obj) noexcept { |
| 191 |
return obj.print(out); |
| 192 |
} |
| 193 |
|
| 194 |
uint32_t File::s_n_pages{}; |
| 195 |
|
| 196 |
#ifdef UNIV_DEBUG |
| 197 |
/** Crash the server after writing this page to the data file. */ |
| 198 |
page_id_t Force_crash{UINT32_UNDEFINED, UINT32_UNDEFINED}; |
| 199 |
#endif /* UNIV_DEBUG */ |
| 200 |
|
| 201 |
namespace recv { |
| 202 |
|
| 203 |
/** Page recovered from the doublewrite buffer */ |
| 204 |
struct Page { |
| 205 |
/** Constructor |
| 206 |
@param[in] page_no Page number in the doublewrite buffer |
| 207 |
@param[in] page Page read from the double write buffer |
| 208 |
@param[in] n_bytes Length of the page data. */ |
| 209 |
Page(page_no_t page_no, const byte *page, uint32_t n_bytes) |
| 210 |
: m_no(page_no), m_buffer(1), m_recovered() { |
| 211 |
ut_a(n_bytes <= univ_page_size.physical()); |
| 212 |
|
| 213 |
auto success = m_buffer.append(page, n_bytes); |
| 214 |
ut_a(success); |
| 215 |
} |
| 216 |
|
| 217 |
/** Page number in the doublewrite buffer. */ |
| 218 |
page_no_t m_no{}; |
| 219 |
|
| 220 |
/** Double write buffer page contents */ |
| 221 |
dblwr::Buffer m_buffer; |
| 222 |
|
| 223 |
/** true if page was recovered. */ |
| 224 |
bool m_recovered{}; |
| 225 |
|
| 226 |
// Disable copying |
| 227 |
Page(const Page &) = delete; |
| 228 |
Page(Page &&) = delete; |
| 229 |
Page &operator=(Page &&) = delete; |
| 230 |
Page &operator=(const Page &) = delete; |
| 231 |
}; |
| 232 |
|
| 233 |
/** A record from reduced doublewrite buffer. */ |
| 234 |
struct Page_entry { |
| 235 |
Page_entry(space_id_t space_id, page_no_t page_no, lsn_t lsn) |
| 236 |
: m_space_id(space_id), m_page_no(page_no), m_lsn(lsn) {} |
| 237 |
|
| 238 |
/** Tablespace id */ |
| 239 |
space_id_t m_space_id; |
| 240 |
|
| 241 |
/** Tablespace page number */ |
| 242 |
page_no_t m_page_no; |
| 243 |
|
| 244 |
/** Page LSN */ |
| 245 |
lsn_t m_lsn; |
| 246 |
}; |
| 247 |
|
| 248 |
/** Pages recovered from the doublewrite buffer */ |
| 249 |
class Pages { |
| 250 |
public: |
| 251 |
using Buffers = std::vector<Page *, ut::allocator<Page *>>; |
| 252 |
using Page_entries = std::vector<Page_entry, ut::allocator<Page_entry>>; |
| 253 |
|
| 254 |
/** Default constructor */ |
| 255 |
Pages() : m_pages(), m_page_entries() {} |
| 256 |
|
| 257 |
/** Destructor */ |
| 258 |
~Pages() noexcept { |
| 259 |
for (auto &page : m_pages) { |
| 260 |
ut::delete_(page); |
| 261 |
} |
| 262 |
|
| 263 |
m_pages.clear(); |
| 264 |
m_page_entries.clear(); |
| 265 |
} |
| 266 |
|
| 267 |
/** Add a page frame to the doublewrite recovery buffer. |
| 268 |
@param[in] page_no Page number in the doublewrite buffer |
| 269 |
@param[in] page Page contents |
| 270 |
@param[in] n_bytes Size in bytes */ |
| 271 |
void add(page_no_t page_no, const byte *page, uint32_t n_bytes) noexcept; |
| 272 |
|
| 273 |
/** Add a page entry from reduced doublewrite buffer to vector |
| 274 |
@param[in] pg_entry Reduced doublewrite buffer entry */ |
| 275 |
void add_entry(Page_entry &pg_entry) { m_page_entries.push_back(pg_entry); } |
| 276 |
|
| 277 |
/** Find a doublewrite copy of a page. |
| 278 |
@param[in] page_id Page number to lookup |
| 279 |
@return page frame |
| 280 |
@retval nullptr if no page was found */ |
| 281 |
const byte *find(const page_id_t &page_id) const noexcept; |
| 282 |
|
| 283 |
/** @return true if page is recovered from the regular doublewrite buffer |
| 284 |
@param[in] page_id Page number to lookup */ |
| 285 |
bool is_recovered(const page_id_t &page_id) const noexcept; |
| 286 |
|
| 287 |
/** Recover a page from the doublewrite buffer. |
| 288 |
@param[in] dblwr_page_no Page number if the doublewrite buffer |
| 289 |
@param[in] space Tablespace the page belongs to |
| 290 |
@param[in] page_no Page number in the tablespace |
| 291 |
@param[in] page Data to write to <space, page_no> |
| 292 |
@return true if page was restored to the tablespace */ |
| 293 |
bool dblwr_recover_page(page_no_t dblwr_page_no, fil_space_t *space, |
| 294 |
page_no_t page_no, byte *page) noexcept; |
| 295 |
|
| 296 |
/** Checks if page in tablespace is corrupted or an all-zero page |
| 297 |
@param[in] space Tablespace object |
| 298 |
@param[in] page_id Page number to check for corruption |
| 299 |
@return tuple<0> - true if corrupted |
| 300 |
tuple<1> - true if the page is all zero page */ |
| 301 |
std::tuple<bool, bool> is_actual_page_corrupted(fil_space_t *space, |
| 302 |
page_id_t &page_id); |
| 303 |
|
| 304 |
/** Check if page was logged in reduced doublewrite buffer mode, if so also |
| 305 |
return the page LSN. Note if there are multiple entries of same page, we |
| 306 |
return the max_LSN of all entries. |
| 307 |
@param[in] page_id Page number to lookup |
| 308 |
@retval tuple<0> - true if the page is found in reduced doublewrite |
| 309 |
buffer, false if no page was found |
| 310 |
tuple<1> - if tuple<0> is true (ie page |
| 311 |
found in reduced dblwr mode), then return the |
| 312 |
max page LSN */ |
| 313 |
std::tuple<bool, lsn_t> find_entry(const page_id_t &page_id) const noexcept { |
| 314 |
bool found = false; |
| 315 |
lsn_t max_lsn = 0; |
| 316 |
// There can be multiple entries with different LSNs, we are interested in |
| 317 |
// the entry with max_lsn |
| 318 |
for (auto &pe : m_page_entries) { |
| 319 |
if (page_id.space() == pe.m_space_id && |
| 320 |
page_id.page_no() == pe.m_page_no) { |
| 321 |
if (pe.m_lsn > max_lsn) { |
| 322 |
found = true; |
| 323 |
max_lsn = pe.m_lsn; |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
return (std::tuple<bool, lsn_t>(found, max_lsn)); |
| 329 |
} |
| 330 |
|
| 331 |
/** Recover double write buffer pages |
| 332 |
@param[in] space Tablespace pages to recover, if set to nullptr then try |
| 333 |
and recovery all. */ |
| 334 |
void recover(fil_space_t *space) noexcept; |
| 335 |
|
| 336 |
/** Check if some pages could be restored because of missing |
| 337 |
tablespace IDs */ |
| 338 |
void check_missing_tablespaces() const noexcept; |
| 339 |
|
| 340 |
/** Object the vector of pages. |
| 341 |
@return the vector of pages. */ |
| 342 |
[[nodiscard]] Buffers &get_pages() noexcept { return m_pages; } |
| 343 |
|
| 344 |
private: |
| 345 |
/** Check if page is logged in reduced doublewrite buffer. We cannot recover |
| 346 |
page because the entire page is not logged only an entry of |
| 347 |
space_id, page_id, LSN is logged. So we abort the server. It is expected |
| 348 |
that the user restores from backup |
| 349 |
@param[in] space Tablespace pages to check in reduced |
| 350 |
dblwr, if set to nullptr then try and recovery all. */ |
| 351 |
void reduced_recover(fil_space_t *space) noexcept; |
| 352 |
|
| 353 |
/** Recovered doublewrite buffer page frames */ |
| 354 |
Buffers m_pages; |
| 355 |
|
| 356 |
/** Page entries from reduced doublewrite buffer */ |
| 357 |
Page_entries m_page_entries; |
| 358 |
|
| 359 |
// Disable copying |
| 360 |
Pages(const Pages &) = delete; |
| 361 |
Pages(Pages &&) = delete; |
| 362 |
Pages &operator=(Pages &&) = delete; |
| 363 |
Pages &operator=(const Pages &) = delete; |
| 364 |
}; |
| 365 |
|
| 366 |
#ifndef UNIV_HOTBACKUP |
| 367 |
std::tuple<bool, bool> Pages::is_actual_page_corrupted(fil_space_t *space, |
| 368 |
page_id_t &page_id) { |
| 369 |
auto result = std::make_tuple(false, false); |
| 370 |
|
| 371 |
if (page_id.page_no() >= space->size) { |
| 372 |
/* Do not report the warning if the tablespace is going to be truncated. |
| 373 |
*/ |
| 374 |
if (!undo::is_active(space->id)) { |
| 375 |
ib::warn(ER_IB_MSG_DBLWR_1313) |
| 376 |
<< "Page# " << page_id.page_no() |
| 377 |
<< " stored in the doublewrite file is" |
| 378 |
" not within data file space bounds " |
| 379 |
<< space->size << " bytes: page : " << page_id; |
| 380 |
} |
| 381 |
return (result); |
| 382 |
} |
| 383 |
|
| 384 |
Buffer buffer{1}; |
| 385 |
const page_size_t page_size(space->flags); |
| 386 |
|
| 387 |
/* We want to ensure that for partial reads the |
| 388 |
unread portion of the page is NUL. */ |
| 389 |
memset(buffer.begin(), 0x0, page_size.physical()); |
| 390 |
|
| 391 |
IORequest request; |
| 392 |
|
| 393 |
request.dblwr(); |
| 394 |
|
| 395 |
/* Read in the page from the data file to compare. */ |
| 396 |
auto err = fil_io(request, true, page_id, page_size, 0, page_size.physical(), |
| 397 |
buffer.begin(), nullptr); |
| 398 |
|
| 399 |
if (err != DB_SUCCESS) { |
| 400 |
ib::warn(ER_IB_MSG_DBLWR_1314) |
| 401 |
<< "Double write fle recovery: " << page_id << " read failed with " |
| 402 |
<< "error: " << ut_strerr(err); |
| 403 |
} |
| 404 |
|
| 405 |
/* Is the page read from the data file corrupt? */ |
| 406 |
BlockReporter data_file_page(true, buffer.begin(), page_size, |
| 407 |
fsp_is_checksum_disabled(space->id)); |
| 408 |
|
| 409 |
bool is_all_zero = buf_page_is_zeroes(buffer.begin(), page_size); |
| 410 |
|
| 411 |
std::get<0>(result) = data_file_page.is_corrupted(); |
| 412 |
std::get<1>(result) = is_all_zero; |
| 413 |
return (result); |
| 414 |
} |
| 415 |
#endif /* !UNIV_HOTBACKUP */ |
| 416 |
|
| 417 |
} // namespace recv |
| 418 |
} // namespace dblwr |
| 419 |
|
| 420 |
using namespace dblwr; |
| 421 |
using namespace dblwr::recv; |
| 422 |
|
| 423 |
#ifndef UNIV_HOTBACKUP |
| 424 |
|
| 425 |
// Forward declaration. |
| 426 |
class Segment; |
| 427 |
class Batch_segment; |
| 428 |
|
| 429 |
/** Doublewrite implementation. Assumes it can use DBLWR_PAGES. */ |
| 430 |
class Double_write { |
| 431 |
public: |
| 432 |
/** Number of instances. */ |
| 433 |
static uint32_t s_n_instances; |
| 434 |
|
| 435 |
/** For collecting pages to write. */ |
| 436 |
struct Buf_pages { |
| 437 |
/** Constructor. |
| 438 |
@param[in] size Number of pages to reserve. */ |
| 439 |
explicit Buf_pages(uint32_t size) : m_pages(size) { |
| 440 |
ut_a(size > 0); |
| 441 |
ut_a(m_pages.capacity() == size); |
| 442 |
ut_a(m_pages.size() == m_pages.capacity()); |
| 443 |
} |
| 444 |
|
| 445 |
/** Add a page to the collection. |
| 446 |
@param[in] bpage Page to write. |
| 447 |
@param[in] e_block encrypted block. */ |
| 448 |
void push_back(buf_page_t *bpage, const file::Block *e_block) noexcept { |
| 449 |
ut_a(m_size < m_pages.capacity()); |
| 450 |
#ifdef UNIV_DEBUG |
| 451 |
{ |
| 452 |
byte *e_frame = |
| 453 |
(e_block == nullptr) ? nullptr : os_block_get_frame(e_block); |
| 454 |
if (e_frame != nullptr) { |
| 455 |
ut_ad(mach_read_from_4(e_frame + FIL_PAGE_OFFSET) == |
| 456 |
bpage->page_no()); |
| 457 |
ut_ad(mach_read_from_4(e_frame + FIL_PAGE_SPACE_ID) == |
| 458 |
bpage->space()); |
| 459 |
} |
| 460 |
} |
| 461 |
#endif /* UNIV_DEBUG */ |
| 462 |
m_pages[m_size++] = std::make_tuple(bpage, e_block); |
| 463 |
} |
| 464 |
|
| 465 |
/** Clear the collection. */ |
| 466 |
void clear() noexcept { m_size = 0; } |
| 467 |
|
| 468 |
/** @return check if collection is empty. */ |
| 469 |
bool empty() const noexcept { return size() == 0; } |
| 470 |
|
| 471 |
/** @return number of active elements. */ |
| 472 |
uint32_t size() const noexcept { return m_size; } |
| 473 |
|
| 474 |
/** @return the capacity of the collection. */ |
| 475 |
[[nodiscard]] uint32_t capacity() const noexcept { |
| 476 |
return m_pages.capacity(); |
| 477 |
} |
| 478 |
|
| 479 |
typedef std::tuple<buf_page_t *, const file::Block *> Dblwr_tuple; |
| 480 |
using Pages = std::vector<Dblwr_tuple, ut::allocator<Dblwr_tuple>>; |
| 481 |
|
| 482 |
/** Collection of pages. */ |
| 483 |
Pages m_pages{}; |
| 484 |
|
| 485 |
/** Number of live elements. */ |
| 486 |
uint32_t m_size{}; |
| 487 |
}; |
| 488 |
|
| 489 |
/** Constructor |
| 490 |
@param[in] id Instance ID |
| 491 |
@param[in] n_pages Number of pages handled by this instance. */ |
| 492 |
Double_write(uint16_t id, uint32_t n_pages) noexcept; |
| 493 |
|
| 494 |
/** Destructor */ |
| 495 |
virtual ~Double_write() noexcept; |
| 496 |
|
| 497 |
/** @return instance ID */ |
| 498 |
[[nodiscard]] uint16_t id() const noexcept { return m_id; } |
| 499 |
|
| 500 |
/** Process the requests in the flush queue, write the blocks to the |
| 501 |
double write file, sync the file if required and then write to the |
| 502 |
data files. */ |
| 503 |
void write(buf_flush_t flush_type) noexcept; |
| 504 |
|
| 505 |
/** @return the double write instance to use for flushing. |
| 506 |
@param[in] buf_pool_index Buffer pool instance number. |
| 507 |
@param[in] flush_type LRU or Flush list write. |
| 508 |
@param[in] is_reduced true if reduced mode of dblwr is being used. |
| 509 |
@return instance that will handle the flush to disk. */ |
| 510 |
[[nodiscard]] static Double_write *instance(buf_flush_t flush_type, |
| 511 |
uint32_t buf_pool_index, |
| 512 |
bool is_reduced) noexcept { |
| 513 |
ut_a(buf_pool_index < srv_buf_pool_instances); |
| 514 |
|
| 515 |
auto midpoint = s_instances->size() / 2; |
| 516 |
auto i = midpoint > 0 ? buf_pool_index % midpoint : 0; |
| 517 |
|
| 518 |
if (flush_type == BUF_FLUSH_LIST) { |
| 519 |
i += midpoint; |
| 520 |
} |
| 521 |
|
| 522 |
return (is_reduced ? s_r_instances->at(i) : s_instances->at(i)); |
| 523 |
} |
| 524 |
|
| 525 |
/** Wait for any pending batch to complete. |
| 526 |
@return true if the thread had to wait for another batch. */ |
| 527 |
bool wait_for_pending_batch() noexcept { |
| 528 |
ut_ad(mutex_own(&m_mutex)); |
| 529 |
|
| 530 |
auto sig_count = os_event_reset(m_event); |
| 531 |
|
| 532 |
std::atomic_thread_fence(std::memory_order_acquire); |
| 533 |
|
| 534 |
if (m_batch_running.load(std::memory_order_acquire)) { |
| 535 |
mutex_exit(&m_mutex); |
| 536 |
|
| 537 |
MONITOR_INC(MONITOR_DBLWR_FLUSH_WAIT_EVENTS); |
| 538 |
os_event_wait_low(m_event, sig_count); |
| 539 |
sig_count = os_event_reset(m_event); |
| 540 |
return true; |
| 541 |
} |
| 542 |
|
| 543 |
return false; |
| 544 |
} |
| 545 |
|
| 546 |
/** Flush buffered pages to disk, clear the buffers. |
| 547 |
@param[in] flush_type FLUSH LIST or LRU LIST flush. |
| 548 |
@return false if there was a write batch already in progress. */ |
| 549 |
bool flush_to_disk(buf_flush_t flush_type) noexcept { |
| 550 |
ut_ad(mutex_own(&m_mutex)); |
| 551 |
|
| 552 |
/* Wait for any batch writes that are in progress. */ |
| 553 |
if (wait_for_pending_batch()) { |
| 554 |
ut_ad(!mutex_own(&m_mutex)); |
| 555 |
return false; |
| 556 |
} |
| 557 |
|
| 558 |
MONITOR_INC(MONITOR_DBLWR_FLUSH_REQUESTS); |
| 559 |
|
| 560 |
/* Write the pages to disk and free up the buffer. */ |
| 561 |
write_pages(flush_type); |
| 562 |
|
| 563 |
ut_a(m_buffer.empty()); |
| 564 |
ut_a(m_buf_pages.empty()); |
| 565 |
|
| 566 |
return true; |
| 567 |
} |
| 568 |
|
| 569 |
/** Process the requests in the flush queue, write the blocks to the |
| 570 |
double write file, sync the file if required and then write to the |
| 571 |
data files. |
| 572 |
@param[in] flush_type LRU or FLUSH request. */ |
| 573 |
void write_pages(buf_flush_t flush_type) noexcept; |
| 574 |
|
| 575 |
virtual uint16_t write_dblwr_pages(buf_flush_t flush_type) noexcept; |
| 576 |
|
| 577 |
void write_data_pages(buf_flush_t flush_type, uint16_t batch_id) noexcept; |
| 578 |
|
| 579 |
/** Force a flush of the page queue. |
| 580 |
@param[in] flush_type FLUSH LIST or LRU LIST flush. */ |
| 581 |
void force_flush(buf_flush_t flush_type) noexcept { |
| 582 |
for (;;) { |
| 583 |
mutex_enter(&m_mutex); |
| 584 |
if (!m_buf_pages.empty() && !flush_to_disk(flush_type)) { |
| 585 |
ut_ad(!mutex_own(&m_mutex)); |
| 586 |
continue; |
| 587 |
} |
| 588 |
break; |
| 589 |
} |
| 590 |
mutex_exit(&m_mutex); |
| 591 |
} |
| 592 |
|
| 593 |
/** Add a page to the flush batch. If the flush batch is full then write |
| 594 |
the batch to disk. |
| 595 |
@param[in] flush_type Flush type. |
| 596 |
@param[in] bpage Page to flush to disk. |
| 597 |
@param[in] e_block Encrypted block frame or nullptr. */ |
| 598 |
void enqueue(buf_flush_t flush_type, buf_page_t *bpage, |
| 599 |
const file::Block *e_block) noexcept { |
| 600 |
ut_ad(buf_page_in_file(bpage)); |
| 601 |
|
| 602 |
void *frame{}; |
| 603 |
uint32_t len{}; |
| 604 |
byte *e_frame = |
| 605 |
(e_block == nullptr) ? nullptr : os_block_get_frame(e_block); |
| 606 |
|
| 607 |
if (e_frame != nullptr) { |
| 608 |
frame = e_frame; |
| 609 |
len = e_block->m_size; |
| 610 |
} else { |
| 611 |
prepare(bpage, &frame, &len); |
| 612 |
} |
| 613 |
|
| 614 |
ut_a(len <= univ_page_size.physical()); |
| 615 |
|
| 616 |
for (;;) { |
| 617 |
mutex_enter(&m_mutex); |
| 618 |
|
| 619 |
if (m_buffer.append(frame, len)) { |
| 620 |
break; |
| 621 |
} |
| 622 |
|
| 623 |
if (flush_to_disk(flush_type)) { |
| 624 |
auto success = m_buffer.append(frame, len); |
| 625 |
ut_a(success); |
| 626 |
break; |
| 627 |
} |
| 628 |
|
| 629 |
ut_ad(!mutex_own(&m_mutex)); |
| 630 |
} |
| 631 |
|
| 632 |
m_buf_pages.push_back(bpage, e_block); |
| 633 |
|
| 634 |
mutex_exit(&m_mutex); |
| 635 |
} |
| 636 |
|
| 637 |
/** Note that the IO batch has started. */ |
| 638 |
void batch_started() noexcept { |
| 639 |
m_batch_running.store(true, std::memory_order_release); |
| 640 |
} |
| 641 |
|
| 642 |
/** Wake up all the threads that were waiting for the batch to complete. */ |
| 643 |
void batch_completed() noexcept { |
| 644 |
m_batch_running.store(false, std::memory_order_release); |
| 645 |
std::atomic_thread_fence(std::memory_order_release); |
| 646 |
os_event_set(m_event); |
| 647 |
} |
| 648 |
|
| 649 |
/** Create the batch write segments. |
| 650 |
@param[in] segments_per_file Number of configured segments per file. |
| 651 |
@return DB_SUCCESS or error code. */ |
| 652 |
[[nodiscard]] static dberr_t create_batch_segments( |
| 653 |
uint32_t segments_per_file) noexcept; |
| 654 |
|
| 655 |
/** Create the Reduced batch write segments. These segments are mapped |
| 656 |
to separate file which has extension .bdblwr |
| 657 |
@return DB_SUCCESS or error code. */ |
| 658 |
[[nodiscard]] static dberr_t create_reduced_batch_segments() noexcept; |
| 659 |
|
| 660 |
/** Create the single page flush segments. |
| 661 |
@return DB_SUCCESS or error code. */ |
| 662 |
[[nodiscard]] static dberr_t create_single_segments() noexcept; |
| 663 |
|
| 664 |
/** Get the instance that handles a particular page's IO. Submit the |
| 665 |
write request to the a double write queue that is empty. |
| 666 |
@param[in] flush_type Flush type. |
| 667 |
@param[in] bpage Page from the buffer pool. |
| 668 |
@param[in] e_block compressed + encrypted frame contents or nullptr.*/ |
| 669 |
static void submit(buf_flush_t flush_type, buf_page_t *bpage, |
| 670 |
const file::Block *e_block) noexcept { |
| 671 |
if (s_instances == nullptr) { |
| 672 |
return; |
| 673 |
} |
| 674 |
|
| 675 |
auto dblwr = instance(flush_type, bpage); |
| 676 |
dblwr->enqueue(flush_type, bpage, e_block); |
| 677 |
} |
| 678 |
|
| 679 |
/** Writes a single page to the doublewrite buffer on disk, syncs it, |
| 680 |
then writes the page to the datafile. |
| 681 |
@param[in] bpage Data page to write to disk. |
| 682 |
@param[in] e_block Encrypted data block. |
| 683 |
@return DB_SUCCESS or error code */ |
| 684 |
[[nodiscard]] static dberr_t sync_page_flush(buf_page_t *bpage, |
| 685 |
file::Block *e_block) noexcept; |
| 686 |
|
| 687 |
/** @return the double write instance to use for flushing. |
| 688 |
@param[in] flush_type LRU or Flush list write. |
| 689 |
@param[in] bpage Page to write to disk. |
| 690 |
@return instance that will handle the flush to disk. */ |
| 691 |
[[nodiscard]] static Double_write *instance( |
| 692 |
buf_flush_t flush_type, const buf_page_t *bpage) noexcept { |
| 693 |
return instance(flush_type, buf_pool_index(buf_pool_from_bpage(bpage)), |
| 694 |
is_reduced()); |
| 695 |
} |
| 696 |
|
| 697 |
/** Updates the double write buffer when a write request is completed. |
| 698 |
@param[in,out] bpage Block that has just been written to disk. |
| 699 |
@param[in] flush_type Flush type that triggered the write. */ |
| 700 |
static void write_complete(buf_page_t *bpage, |
| 701 |
buf_flush_t flush_type) noexcept; |
| 702 |
|
| 703 |
/** REad the V1 doublewrite buffer extents boundaries. |
| 704 |
@param[in,out] block1 Starting block number for the first extent. |
| 705 |
@param[in,out] block2 Starting block number for the second extent. |
| 706 |
@return true if successful, false if not. */ |
| 707 |
[[nodiscard]] static bool init_v1(page_no_t &block1, |
| 708 |
page_no_t &block2) noexcept; |
| 709 |
|
| 710 |
/** Creates the V1 doublewrite buffer extents. The header of the |
| 711 |
doublewrite buffer is placed on the trx system header page. |
| 712 |
@param[in,out] block1 Starting block number for the first extent. |
| 713 |
@param[in,out] block2 Starting block number for the second extent. |
| 714 |
@return true if successful, false if not. */ |
| 715 |
[[nodiscard]] static bool create_v1(page_no_t &block1, |
| 716 |
page_no_t &block2) noexcept; |
| 717 |
|
| 718 |
/** Writes a page that has already been written to the |
| 719 |
doublewrite buffer to the data file. It is the job of the |
| 720 |
caller to sync the datafile. |
| 721 |
@param[in] in_bpage Page to write. |
| 722 |
@param[in] sync true if it's a synchronous write. |
| 723 |
@param[in] e_block block containing encrypted data frame. |
| 724 |
@return DB_SUCCESS or error code */ |
| 725 |
[[nodiscard]] static dberr_t write_to_datafile( |
| 726 |
const buf_page_t *in_bpage, bool sync, |
| 727 |
const file::Block *e_block) noexcept; |
| 728 |
|
| 729 |
/** Force a flush of the page queue. |
| 730 |
@param[in] flush_type FLUSH LIST or LRU LIST flush. |
| 731 |
@param[in] buf_pool_index Buffer pool instance for which called. */ |
| 732 |
static void force_flush(buf_flush_t flush_type, |
| 733 |
uint32_t buf_pool_index) noexcept { |
| 734 |
if (s_instances == nullptr) { |
| 735 |
return; |
| 736 |
} |
| 737 |
auto dblwr = instance(flush_type, buf_pool_index, false); |
| 738 |
|
| 739 |
dblwr->force_flush(flush_type); |
| 740 |
|
| 741 |
if (is_reduced_inited) { |
| 742 |
auto dblwr = instance(flush_type, buf_pool_index, true); |
| 743 |
dblwr->force_flush(flush_type); |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
/** Load the doublewrite buffer pages from an external file. |
| 748 |
@param[in,out] file File handle |
| 749 |
@param[in,out] pages For storing the doublewrite pages |
| 750 |
read from the file |
| 751 |
@return DB_SUCCESS or error code */ |
| 752 |
[[nodiscard]] static dberr_t load(dblwr::File &file, |
| 753 |
recv::Pages *pages) noexcept; |
| 754 |
|
| 755 |
/** Load the reduced doublewrite buffer page entries from an reduced batch |
| 756 |
double write buffer file (.bdblwr) |
| 757 |
@param[in,out] file File handle |
| 758 |
@param[in,out] pages For storing the doublewrite pages |
| 759 |
read from the file |
| 760 |
@return DB_SUCCESS or error code */ |
| 761 |
[[nodiscard]] static dberr_t load_reduced_batch(dblwr::File &file, |
| 762 |
recv::Pages *pages) noexcept; |
| 763 |
|
| 764 |
/** Write zeros to the file if it is "empty" |
| 765 |
@param[in] file File instance. |
| 766 |
@param[in] n_pages Size in physical pages. |
| 767 |
@param[in] phy_size Physical page size in DBLWR file (For reduced DBLW |
| 768 |
file, it is not UNIV_PAGE_SIZE, it is hardcoded to 8K page size) |
| 769 |
@return DB_SUCCESS or error code */ |
| 770 |
[[nodiscard]] static dberr_t init_file( |
| 771 |
dblwr::File &file, uint32_t n_pages, |
| 772 |
uint32_t phy_size = univ_page_size.physical()) noexcept; |
| 773 |
|
| 774 |
/** Reset the size in bytes to the configured size. |
| 775 |
@param[in,out] file File to reset. |
| 776 |
@param[in] truncate Truncate the file to configured size if true. */ |
| 777 |
static void reset_file(dblwr::File &file, bool truncate) noexcept; |
| 778 |
|
| 779 |
/** Reset the size in bytes to the configured size. |
| 780 |
@param[in,out] file File to reset |
| 781 |
@param[in] pages_per_file Number of pages to be created |
| 782 |
in doublewrite file |
| 783 |
@param[in] phy_size physical page size */ |
| 784 |
static void reduced_reset_file(dblwr::File &file, uint32_t pages_per_file, |
| 785 |
uint32_t phy_size) noexcept; |
| 786 |
|
| 787 |
/** Reset the size in bytes to the configured size of all files. */ |
| 788 |
static void reset_files() noexcept { |
| 789 |
for (auto &file : Double_write::s_files) { |
| 790 |
/* Physically truncate the file: true. */ |
| 791 |
Double_write::reset_file(file, true); |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
/** Create the v2 data structures |
| 796 |
@return DB_SUCCESS or error code */ |
| 797 |
[[nodiscard]] static dberr_t create_v2() noexcept; |
| 798 |
|
| 799 |
/** Create the data structures for reduced doublewrite buffer |
| 800 |
@return DB_SUCCESS or error code */ |
| 801 |
[[nodiscard]] static dberr_t create_reduced() noexcept; |
| 802 |
|
| 803 |
#ifndef _WIN32 |
| 804 |
/** @return true if we need to fsync to disk */ |
| 805 |
[[nodiscard]] static bool is_fsync_required() noexcept { |
| 806 |
/* srv_unix_file_flush_method is a dynamic variable. */ |
| 807 |
return srv_unix_file_flush_method != SRV_UNIX_O_DIRECT && |
| 808 |
srv_unix_file_flush_method != SRV_UNIX_O_DIRECT_NO_FSYNC; |
| 809 |
} |
| 810 |
#endif /* _WIN32 */ |
| 811 |
|
| 812 |
/** Extract the data and length to write to the doublewrite file |
| 813 |
@param[in] bpage Page to write |
| 814 |
@param[out] ptr Start of buffer to write |
| 815 |
@param[out] len Length of the data to write */ |
| 816 |
static void prepare(const buf_page_t *bpage, void **ptr, |
| 817 |
uint32_t *len) noexcept; |
| 818 |
|
| 819 |
/** Free the data structures. */ |
| 820 |
static void shutdown() noexcept; |
| 821 |
|
| 822 |
/** Toggle the doublewrite buffer dynamically |
| 823 |
@param[in] value Current value */ |
| 824 |
static void toggle(ulong value) noexcept { |
| 825 |
if (s_instances == nullptr) { |
| 826 |
return; |
| 827 |
} |
| 828 |
|
| 829 |
if (Mode::is_atomic(value)) { |
| 830 |
ib::info(ER_IB_MSG_DBLWR_1304) << "Atomic write enabled"; |
| 831 |
} else { |
| 832 |
ib::info(ER_IB_MSG_DBLWR_1305) << "Atomic write disabled"; |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
/** Write the data to disk synchronously. |
| 837 |
@param[in] segment Segment to write to. |
| 838 |
@param[in] bpage Page to write. |
| 839 |
@param[in] e_block Encrypted block. Can be nullptr. */ |
| 840 |
static void single_write(Segment *segment, const buf_page_t *bpage, |
| 841 |
file::Block *e_block) noexcept; |
| 842 |
|
| 843 |
private: |
| 844 |
/** Create the singleton instance, start the flush thread |
| 845 |
@return DB_SUCCESS or error code */ |
| 846 |
[[nodiscard]] static dberr_t start() noexcept; |
| 847 |
|
| 848 |
/** Asserts when a corrupt block is found during writing out |
| 849 |
data to the disk. |
| 850 |
@param[in] block Block that was corrupt */ |
| 851 |
static void croak(const buf_block_t *block) noexcept; |
| 852 |
|
| 853 |
/** Check the LSN values on the page with which this block |
| 854 |
is associated. Also validate the page if the option is set. |
| 855 |
@param[in] block Block to check */ |
| 856 |
static void check_block(const buf_block_t *block) noexcept; |
| 857 |
|
| 858 |
/** Check the LSN values on the page. |
| 859 |
@param[in] page Page to check */ |
| 860 |
static void check_page_lsn(const page_t *page) noexcept; |
| 861 |
|
| 862 |
/** Calls buf_page_get() on the TRX_SYS_PAGE and returns |
| 863 |
a pointer to the doublewrite buffer within it. |
| 864 |
@param[in,out] mtr To manage the page latches |
| 865 |
@return pointer to the doublewrite buffer within the filespace |
| 866 |
header page. */ |
| 867 |
[[nodiscard]] static byte *get(mtr_t *mtr) noexcept; |
| 868 |
|
| 869 |
protected: |
| 870 |
using Segments = mpmc_bq<Segment *>; |
| 871 |
using Instances = std::vector<Double_write *>; |
| 872 |
using Batch_segments = mpmc_bq<Batch_segment *>; |
| 873 |
|
| 874 |
/** Instance ID */ |
| 875 |
uint16_t m_id{}; |
| 876 |
|
| 877 |
/** Protects m_buf_pages. */ |
| 878 |
ib_mutex_t m_mutex; |
| 879 |
|
| 880 |
/** Wait for IO batch to complete. */ |
| 881 |
os_event_t m_event; |
| 882 |
|
| 883 |
/** true if the the batch hasn't completed yet. */ |
| 884 |
std::atomic_bool m_batch_running{false}; |
| 885 |
|
| 886 |
/** The copy of the page frame, the page must be in in m_buf_pages. */ |
| 887 |
Buffer m_buffer; |
| 888 |
|
| 889 |
/** Pages that should be written to the data files. */ |
| 890 |
Buf_pages m_buf_pages; |
| 891 |
|
| 892 |
/** File segments to use for LRU batched writes. */ |
| 893 |
static Batch_segments *s_LRU_batch_segments; |
| 894 |
|
| 895 |
/** File segments to use for flush list batched writes. */ |
| 896 |
static Batch_segments *s_flush_list_batch_segments; |
| 897 |
|
| 898 |
/** File segments to use for single page writes. */ |
| 899 |
static Segments *s_single_segments; |
| 900 |
|
| 901 |
/** File segments to use for LRU batched writes in reduced dblwr mode */ |
| 902 |
static Batch_segments *s_r_LRU_batch_segments; |
| 903 |
|
| 904 |
/** File segments to use for flush list batched writes (reduced mode) */ |
| 905 |
static Batch_segments *s_r_flush_list_batch_segments; |
| 906 |
|
| 907 |
/** For indexing batch segments by ID. */ |
| 908 |
static std::vector<Batch_segment *> s_segments; |
| 909 |
|
| 910 |
/** Utility function to free batch segments |
| 911 |
@param[in] segments batch segment to free */ |
| 912 |
static void free_segments(Batch_segments *&segments) noexcept; |
| 913 |
|
| 914 |
/** Last used batch_id for regular batch segments. Any id greater |
| 915 |
than this belongs to reduced double write */ |
| 916 |
static uint32_t s_regular_last_batch_id; |
| 917 |
|
| 918 |
/** @return true if batch belonged to reduced dblwr. When returning |
| 919 |
a batch segment to lock-free queue, we should know which lock-free |
| 920 |
queue(Batch_segments) to return to |
| 921 |
@param[in] batch_id Batch segment id */ |
| 922 |
static bool is_reduced_batch_id(uint32_t batch_id); |
| 923 |
|
| 924 |
public: |
| 925 |
/** Files to use for atomic writes. */ |
| 926 |
static std::vector<dblwr::File> s_files; |
| 927 |
|
| 928 |
/** Reduced batch doublewrite files to use for atomic writes. */ |
| 929 |
static std::vector<dblwr::File> s_r_files; |
| 930 |
|
| 931 |
/** The global instances */ |
| 932 |
static Instances *s_instances; |
| 933 |
|
| 934 |
/** The global Reduced Doublewrite instances */ |
| 935 |
static Instances *s_r_instances; |
| 936 |
|
| 937 |
// Disable copying |
| 938 |
Double_write(const Double_write &) = delete; |
| 939 |
Double_write(Double_write &&) = delete; |
| 940 |
Double_write &operator=(Double_write &&) = delete; |
| 941 |
Double_write &operator=(const Double_write &) = delete; |
| 942 |
|
| 943 |
/** Number of bytes written to disk by this instance. It is never reset. |
| 944 |
It is printed to server log during shutdown. */ |
| 945 |
unsigned long long m_bytes_written{}; |
| 946 |
}; |
| 947 |
|
| 948 |
/** File segment of a double write file. */ |
| 949 |
class Segment { |
| 950 |
public: |
| 951 |
/** Constructor. |
| 952 |
@param[in] file File that owns the segment. |
| 953 |
@param[in] start Offset (page number) of segment in the file. |
| 954 |
@param[in] n_pages Number of pages in the segment. */ |
| 955 |
Segment(dblwr::File &file, page_no_t start, uint32_t n_pages) |
| 956 |
: m_file(file), |
| 957 |
m_phy_size(univ_page_size.physical()), |
| 958 |
m_start(start * m_phy_size), |
| 959 |
m_end(m_start + (n_pages * m_phy_size)) {} |
| 960 |
|
| 961 |
/** Constructor. |
| 962 |
@param[in] file File that owns the segment. |
| 963 |
@param[in] phy_size Size of an entry in segment |
| 964 |
@param[in] start Offset (page number) of segment in the file. |
| 965 |
@param[in] n_pages Number of pages in the segment. */ |
| 966 |
Segment(dblwr::File &file, uint32_t phy_size, page_no_t start, |
| 967 |
uint32_t n_pages) |
| 968 |
: m_file(file), |
| 969 |
m_phy_size(phy_size), |
| 970 |
m_start(start * m_phy_size), |
| 971 |
m_end(m_start + (n_pages * m_phy_size)) {} |
| 972 |
|
| 973 |
/** Destructor. */ |
| 974 |
virtual ~Segment() = default; |
| 975 |
|
| 976 |
/** Write to the segment. |
| 977 |
@param[in] ptr Start writing from here. |
| 978 |
@param[in] len Number of bytes to write. */ |
| 979 |
void write(const void *ptr, uint32_t len) noexcept { |
| 980 |
ut_a(len <= m_end - m_start); |
| 981 |
IORequest req(IORequest::WRITE | IORequest::DO_NOT_WAKE); |
| 982 |
|
| 983 |
req.dblwr(); |
| 984 |
|
| 985 |
auto err = os_file_write_retry(req, m_file.m_name.c_str(), m_file.m_pfs, |
| 986 |
ptr, m_start, len); |
| 987 |
ut_a(err == DB_SUCCESS); |
| 988 |
} |
| 989 |
|
| 990 |
/** Flush the segment to disk. */ |
| 991 |
void flush() noexcept { os_file_flush(m_file.m_pfs); } |
| 992 |
|
| 993 |
/** File that owns the segment. */ |
| 994 |
dblwr::File &m_file; |
| 995 |
|
| 996 |
/** Physical page size of each entry/Segment */ |
| 997 |
uint32_t m_phy_size{}; |
| 998 |
|
| 999 |
/** Physical offset in the file for the segment. */ |
| 1000 |
os_offset_t m_start{}; |
| 1001 |
|
| 1002 |
/** Physical offset up to which this segment is responsible for. */ |
| 1003 |
os_offset_t m_end{}; |
| 1004 |
|
| 1005 |
// Disable copying |
| 1006 |
Segment(Segment &&) = delete; |
| 1007 |
Segment(const Segment &) = delete; |
| 1008 |
Segment &operator=(Segment &&) = delete; |
| 1009 |
Segment &operator=(const Segment &) = delete; |
| 1010 |
}; |
| 1011 |
|
| 1012 |
/** Segment for batched writes. */ |
| 1013 |
class Batch_segment : public Segment { |
| 1014 |
public: |
| 1015 |
/** Constructor. |
| 1016 |
@param[in] id Segment ID. |
| 1017 |
@param[in] file File that owns the segment. |
| 1018 |
@param[in] start Offset (page number) of segment in the file. |
| 1019 |
@param[in] n_pages Number of pages in the segment. */ |
| 1020 |
Batch_segment(uint16_t id, dblwr::File &file, page_no_t start, |
| 1021 |
uint32_t n_pages) |
| 1022 |
: Segment(file, start, n_pages), m_id(id) { |
| 1023 |
reset(); |
| 1024 |
} |
| 1025 |
|
| 1026 |
/** Constructor. |
| 1027 |
@param[in] id Segment ID. |
| 1028 |
@param[in] file File that owns the segment. |
| 1029 |
@param[in] phy_size physical size of each segment entry |
| 1030 |
@param[in] start Offset (page number) of segment in the file. |
| 1031 |
@param[in] n_pages Number of pages in the segment. */ |
| 1032 |
Batch_segment(uint16_t id, dblwr::File &file, uint32_t phy_size, |
| 1033 |
page_no_t start, uint32_t n_pages) |
| 1034 |
: Segment(file, phy_size, start, n_pages), m_id(id) { |
| 1035 |
reset(); |
| 1036 |
} |
| 1037 |
|
| 1038 |
/** Destructor. */ |
| 1039 |
~Batch_segment() noexcept override { |
| 1040 |
ut_a(m_uncompleted.load(std::memory_order_relaxed) == 0); |
| 1041 |
ut_a(m_batch_size == 0); |
| 1042 |
} |
| 1043 |
|
| 1044 |
/** @return the batch segment ID. */ |
| 1045 |
uint16_t id() const noexcept { return m_id; } |
| 1046 |
|
| 1047 |
/** Write a batch to the segment. |
| 1048 |
@param[in] buffer Buffer to write. */ |
| 1049 |
void write(const Buffer &buffer) noexcept; |
| 1050 |
|
| 1051 |
/** Write a batch to the segment. |
| 1052 |
@param[in] buf Buffer to write |
| 1053 |
@param[in] len amount of data to write */ |
| 1054 |
void write(const byte *buf, uint32_t len) noexcept; |
| 1055 |
|
| 1056 |
/** Called on page write completion. |
| 1057 |
@return if batch ended. */ |
| 1058 |
[[nodiscard]] bool write_complete() noexcept { |
| 1059 |
/* We "release our reference" here, so can't access the segment after this |
| 1060 |
fetch_sub() unless we decreased it to 0 and handle requeuing it. */ |
| 1061 |
const auto n = m_uncompleted.fetch_sub(1, std::memory_order_relaxed); |
| 1062 |
ut_ad(0 < n); |
| 1063 |
return n == 1; |
| 1064 |
} |
| 1065 |
|
| 1066 |
/** Reset the state. */ |
| 1067 |
void reset() noexcept { |
| 1068 |
/* We shouldn't reset() the batch while it's being processed. */ |
| 1069 |
ut_ad(m_uncompleted.load(std::memory_order_relaxed) == 0); |
| 1070 |
m_uncompleted.store(0, std::memory_order_relaxed); |
| 1071 |
m_batch_size = 0; |
| 1072 |
} |
| 1073 |
|
| 1074 |
/** Set the batch size. |
| 1075 |
@param[in] size Number of pages to write to disk. */ |
| 1076 |
void set_batch_size(uint32_t size) noexcept { |
| 1077 |
/* We should only call set_batch_size() on new or reset()ed instance. */ |
| 1078 |
ut_ad(m_uncompleted.load(std::memory_order_relaxed) == 0); |
| 1079 |
ut_ad(m_batch_size == 0); |
| 1080 |
m_batch_size = size; |
| 1081 |
m_uncompleted.store(size, std::memory_order_relaxed); |
| 1082 |
} |
| 1083 |
|
| 1084 |
/** @return the batch size. */ |
| 1085 |
uint32_t batch_size() const noexcept { return m_batch_size; } |
| 1086 |
|
| 1087 |
/** Note that the batch has started for the double write instance. |
| 1088 |
@param[in] dblwr Instance for which batch has started. */ |
| 1089 |
void start(Double_write *dblwr) noexcept { |
| 1090 |
m_dblwr = dblwr; |
| 1091 |
m_dblwr->batch_started(); |
| 1092 |
} |
| 1093 |
|
| 1094 |
/** Note that the batch has completed. */ |
| 1095 |
void completed() noexcept { |
| 1096 |
m_dblwr->batch_completed(); |
| 1097 |
m_dblwr = nullptr; |
| 1098 |
} |
| 1099 |
|
| 1100 |
/** Batch segment ID. */ |
| 1101 |
uint16_t m_id{}; |
| 1102 |
|
| 1103 |
/** The instance that is being written to disk. */ |
| 1104 |
Double_write *m_dblwr{}; |
| 1105 |
|
| 1106 |
byte m_pad1[ut::INNODB_CACHE_LINE_SIZE]; |
| 1107 |
|
| 1108 |
/** Size of the batch. |
| 1109 |
Set to number of pages to be written with set_batch_size() before scheduling |
| 1110 |
writes to data pages. |
| 1111 |
Reset to zero with reset() after all IOs are completed. |
| 1112 |
Read only by the thread which has observed the last IO completion, the one |
| 1113 |
which will reset it back to zero and enqueue the segment for future reuse. |
| 1114 |
Accesses to this field are ordered by happens-before relation: |
| 1115 |
set_batch_size() sequenced-before |
| 1116 |
fil_io() happens-before |
| 1117 |
dblwr::write_complete() entry sequenced-before |
| 1118 |
batch_size() sequenced-before |
| 1119 |
reset() sequenced-before |
| 1120 |
enqueue() synchronizes-with |
| 1121 |
dequeue() sequenced-before |
| 1122 |
set_batch_size() ... |
| 1123 |
*/ |
| 1124 |
uint32_t m_batch_size{}; |
| 1125 |
|
| 1126 |
byte m_pad2[ut::INNODB_CACHE_LINE_SIZE]; |
| 1127 |
|
| 1128 |
/** Number of page writes in the batch which are still not completed. |
| 1129 |
Set to equal m_batch_size by set_batch_size(), and decremented when a page |
| 1130 |
write is finished (either by failing/not attempting or in IO completion). |
| 1131 |
It serves a role of a reference counter: when it drops to zero, the segment |
| 1132 |
can be enqueued back to the pool of available segments. |
| 1133 |
Accessing a segment which has m_uncompleted == 0 is safe only from the thread |
| 1134 |
which knows it can not be recycled - for example because it's the thread which |
| 1135 |
has caused the m_uncompleted drop to 0 and will enqueue it, or it's the thread |
| 1136 |
which has just dequeued it, or it is handling shutdown.*/ |
| 1137 |
std::atomic_int m_uncompleted{}; |
| 1138 |
}; |
| 1139 |
|
| 1140 |
/** Reduced doublewrite implementation. Uses separate |
| 1141 |
.bdblwr files and can coexist with regular doublewrite buffer |
| 1142 |
implementation */ |
| 1143 |
class Reduced_double_write : public Double_write { |
| 1144 |
public: |
| 1145 |
/** Constructor |
| 1146 |
@param[in] id Instance ID |
| 1147 |
@param[in] n_pages Number of pages handled by this instance. */ |
| 1148 |
Reduced_double_write(uint16_t id, uint32_t n_pages) |
| 1149 |
: Double_write(id, n_pages), m_buf(nullptr), m_page(nullptr) {} |
| 1150 |
|
| 1151 |
/** Destructor */ |
| 1152 |
~Reduced_double_write() override { |
| 1153 |
if (m_buf != nullptr) { |
| 1154 |
ut::free(m_buf); |
| 1155 |
} |
| 1156 |
} |
| 1157 |
/** Process the requests in the flush queue, write the space_id, page_id, LSN |
| 1158 |
to the reduced double write file (.bdblwr), sync the file if required and |
| 1159 |
then write to the data files. |
| 1160 |
@param[in] flush_type LRU or FLUSH request. */ |
| 1161 |
virtual uint16_t write_dblwr_pages(buf_flush_t flush_type) noexcept override; |
| 1162 |
|
| 1163 |
/** Allocate a temporary buffer for writing page entries. |
| 1164 |
@return DB_SUCCESS on success, error code on failure. */ |
| 1165 |
dberr_t allocate(); |
| 1166 |
|
| 1167 |
private: |
| 1168 |
/** Clear the temporary buffer used for writing reduced dblwr page */ |
| 1169 |
void clear() { memset(m_page, 0, REDUCED_BATCH_PAGE_SIZE); } |
| 1170 |
|
| 1171 |
/** Create Reduced dblwr page header |
| 1172 |
@param[in] batch_id Batch_id of the Reduced dblwr segment |
| 1173 |
@param[in] checksum Checksum of the page |
| 1174 |
@param[in] data_len Length of data in page |
| 1175 |
@param[in] flush_type LRU of FLUSH_LIST type*/ |
| 1176 |
void create_header(uint32_t batch_id, uint32_t checksum, uint16_t data_len, |
| 1177 |
buf_flush_t flush_type) { |
| 1178 |
mach_write_to_4(m_page + RB_OFF_BATCH_ID, batch_id); |
| 1179 |
mach_write_to_4(m_page + RB_OFF_CHECKSUM, checksum); |
| 1180 |
mach_write_to_2(m_page + RB_OFF_DATA_LEN, data_len); |
| 1181 |
mach_write_to_1(m_page + RB_OFF_BATCH_TYPE, flush_type); |
| 1182 |
memset(m_page + RB_OFF_UNUSED, 0, RB_UNUSED_BYTES_SIZE); |
| 1183 |
} |
| 1184 |
|
| 1185 |
/** Calculate checksum for the Reduced dblwr page |
| 1186 |
@param[in] data_len amount of data in page |
| 1187 |
@return checksum calcuated */ |
| 1188 |
uint32_t calculate_checksum(uint16_t data_len) { |
| 1189 |
return (ut_crc32(m_page + REDUCED_HEADER_SIZE, data_len)); |
| 1190 |
} |
| 1191 |
|
| 1192 |
private: |
| 1193 |
/** Un-aligned temporary buffer */ |
| 1194 |
byte *m_buf; |
| 1195 |
|
| 1196 |
/** aligned temporary buffer. Created from m_buf */ |
| 1197 |
byte *m_page; |
| 1198 |
}; |
| 1199 |
|
| 1200 |
dberr_t Reduced_double_write::allocate() { |
| 1201 |
ut_ad(m_buf == nullptr); |
| 1202 |
|
| 1203 |
m_buf = static_cast<byte *>(ut::zalloc(2 * REDUCED_BATCH_PAGE_SIZE)); |
| 1204 |
|
| 1205 |
if (m_buf == nullptr) { |
| 1206 |
return DB_OUT_OF_MEMORY; |
| 1207 |
} |
| 1208 |
|
| 1209 |
/* Align the memory for file i/o if we might have O_DIRECT set */ |
| 1210 |
m_page = static_cast<byte *>(ut_align(m_buf, REDUCED_BATCH_PAGE_SIZE)); |
| 1211 |
|
| 1212 |
return DB_SUCCESS; |
| 1213 |
} |
| 1214 |
|
| 1215 |
uint16_t Reduced_double_write::write_dblwr_pages( |
| 1216 |
buf_flush_t flush_type) noexcept { |
| 1217 |
ut_ad(mutex_own(&m_mutex)); |
| 1218 |
ut_a(!m_buffer.empty()); |
| 1219 |
ut_ad(m_buf != nullptr); |
| 1220 |
|
| 1221 |
uint16_t data_len{}; |
| 1222 |
byte *ptr = m_page + REDUCED_HEADER_SIZE; |
| 1223 |
#ifdef UNIV_DEBUG |
| 1224 |
byte *ptr_orig = ptr; |
| 1225 |
const byte *end_ptr = m_page + REDUCED_BATCH_PAGE_SIZE; |
| 1226 |
const size_t bytes_needed = m_buf_pages.size() * REDUCED_ENTRY_SIZE; |
| 1227 |
ut_ad(bytes_needed <= REDUCED_DATA_SIZE); |
| 1228 |
#endif /* UNIV_DEBUG */ |
| 1229 |
for (uint32_t i = 0; i < m_buf_pages.size(); ++i) { |
| 1230 |
const auto bpage = std::get<0>(m_buf_pages.m_pages[i]); |
| 1231 |
|
| 1232 |
Reduced_entry entry(bpage); |
| 1233 |
ut_ad(ptr + REDUCED_ENTRY_SIZE < end_ptr); |
| 1234 |
ptr = entry.write(ptr); |
| 1235 |
data_len += REDUCED_ENTRY_SIZE; |
| 1236 |
} |
| 1237 |
ut_ad(ptr - ptr_orig == data_len); |
| 1238 |
ut_ad(data_len <= REDUCED_DATA_SIZE); |
| 1239 |
|
| 1240 |
// Calculate checksum for the data written |
| 1241 |
uint32_t checksum = calculate_checksum(data_len); |
| 1242 |
|
| 1243 |
Batch_segment *batch_segment{}; |
| 1244 |
|
| 1245 |
auto segments = flush_type == BUF_FLUSH_LRU ? s_r_LRU_batch_segments |
| 1246 |
: s_r_flush_list_batch_segments; |
| 1247 |
|
| 1248 |
while (!segments->dequeue(batch_segment)) { |
| 1249 |
std::this_thread::yield(); |
| 1250 |
} |
| 1251 |
|
| 1252 |
// Create Page header |
| 1253 |
create_header(batch_segment->id(), checksum, data_len, flush_type); |
| 1254 |
|
| 1255 |
ut_ad(data_len / REDUCED_ENTRY_SIZE == m_buf_pages.size()); |
| 1256 |
|
| 1257 |
batch_segment->start(this); |
| 1258 |
|
| 1259 |
batch_segment->write(m_page, REDUCED_BATCH_PAGE_SIZE); |
| 1260 |
|
| 1261 |
m_bytes_written += REDUCED_BATCH_PAGE_SIZE; |
| 1262 |
|
| 1263 |
m_buffer.clear(); |
| 1264 |
clear(); |
| 1265 |
|
| 1266 |
#ifndef _WIN32 |
| 1267 |
if (is_fsync_required()) { |
| 1268 |
batch_segment->flush(); |
| 1269 |
} |
| 1270 |
#endif /* !_WIN32 */ |
| 1271 |
|
| 1272 |
batch_segment->set_batch_size(m_buf_pages.size()); |
| 1273 |
return batch_segment->id(); |
| 1274 |
} |
| 1275 |
|
| 1276 |
uint32_t Double_write::s_n_instances{}; |
| 1277 |
std::vector<dblwr::File> Double_write::s_files; |
| 1278 |
std::vector<dblwr::File> Double_write::s_r_files; |
| 1279 |
Double_write::Segments *Double_write::s_single_segments{}; |
| 1280 |
Double_write::Batch_segments *Double_write::s_LRU_batch_segments{}; |
| 1281 |
Double_write::Batch_segments *Double_write::s_flush_list_batch_segments{}; |
| 1282 |
std::vector<Batch_segment *> Double_write::s_segments{}; |
| 1283 |
|
| 1284 |
Double_write::Instances *Double_write::s_instances{}; |
| 1285 |
uint32_t Double_write::s_regular_last_batch_id{}; |
| 1286 |
|
| 1287 |
Double_write::Batch_segments *Double_write::s_r_LRU_batch_segments{}; |
| 1288 |
Double_write::Batch_segments *Double_write::s_r_flush_list_batch_segments{}; |
| 1289 |
Double_write::Instances *Double_write::s_r_instances{}; |
| 1290 |
|
| 1291 |
Double_write::Double_write(uint16_t id, uint32_t n_pages) noexcept |
| 1292 |
: m_id(id), m_buffer(n_pages), m_buf_pages(n_pages) { |
| 1293 |
ut_a(n_pages == dblwr::n_pages); |
| 1294 |
ut_a(m_buffer.capacity() / UNIV_PAGE_SIZE == m_buf_pages.capacity()); |
| 1295 |
|
| 1296 |
mutex_create(LATCH_ID_DBLWR, &m_mutex); |
| 1297 |
m_event = os_event_create(); |
| 1298 |
} |
| 1299 |
|
| 1300 |
Double_write::~Double_write() noexcept { |
| 1301 |
mutex_free(&m_mutex); |
| 1302 |
os_event_destroy(m_event); |
| 1303 |
} |
| 1304 |
|
| 1305 |
void Double_write::prepare(const buf_page_t *bpage, void **ptr, |
| 1306 |
uint32_t *len) noexcept { |
| 1307 |
auto block = reinterpret_cast<const buf_block_t *>(bpage); |
| 1308 |
auto state = buf_block_get_state(block); |
| 1309 |
|
| 1310 |
/* No simple validate for compressed pages exists. */ |
| 1311 |
if (state == BUF_BLOCK_FILE_PAGE && block->page.zip.data == nullptr) { |
| 1312 |
/* Check that the actual page in the buffer pool is |
| 1313 |
not corrupt and the LSN values are sane. */ |
| 1314 |
check_block(block); |
| 1315 |
} |
| 1316 |
|
| 1317 |
if (bpage->size.is_compressed()) { |
| 1318 |
UNIV_MEM_ASSERT_RW(bpage->zip.data, bpage->size.physical()); |
| 1319 |
|
| 1320 |
*ptr = bpage->zip.data; |
| 1321 |
*len = bpage->size.physical(); |
| 1322 |
|
| 1323 |
} else { |
| 1324 |
if (state != BUF_BLOCK_FILE_PAGE) { |
| 1325 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_DBLWR_1297) |
| 1326 |
<< "Invalid page state: state: " << static_cast<unsigned>(state) |
| 1327 |
<< " block state: " |
| 1328 |
<< static_cast<unsigned>(buf_page_get_state(bpage)); |
| 1329 |
} else { |
| 1330 |
ut_ad(state == buf_block_get_state(block)); |
| 1331 |
} |
| 1332 |
|
| 1333 |
*ptr = |
| 1334 |
reinterpret_cast<buf_block_t *>(const_cast<buf_page_t *>(bpage))->frame; |
| 1335 |
|
| 1336 |
UNIV_MEM_ASSERT_RW(*ptr, bpage->size.logical()); |
| 1337 |
|
| 1338 |
*len = bpage->size.logical(); |
| 1339 |
} |
| 1340 |
} |
| 1341 |
|
| 1342 |
void Double_write::single_write(Segment *segment, const buf_page_t *bpage, |
| 1343 |
file::Block *e_block) noexcept { |
| 1344 |
uint32_t len{}; |
| 1345 |
void *frame{}; |
| 1346 |
|
| 1347 |
if (e_block != nullptr) { |
| 1348 |
frame = os_block_get_frame(e_block); |
| 1349 |
len = e_block->m_size; |
| 1350 |
} else { |
| 1351 |
prepare(bpage, &frame, &len); |
| 1352 |
} |
| 1353 |
|
| 1354 |
ut_ad(len <= univ_page_size.physical()); |
| 1355 |
|
| 1356 |
segment->write(frame, len); |
| 1357 |
} |
| 1358 |
|
| 1359 |
void Batch_segment::write(const Buffer &buffer) noexcept { |
| 1360 |
Segment::write(buffer.begin(), buffer.size()); |
| 1361 |
} |
| 1362 |
|
| 1363 |
void Batch_segment::write(const byte *buf, uint32_t len) noexcept { |
| 1364 |
Segment::write(buf, len); |
| 1365 |
} |
| 1366 |
|
| 1367 |
dberr_t Double_write::create_v2() noexcept { |
| 1368 |
ut_a(!s_files.empty()); |
| 1369 |
ut_a(s_instances == nullptr); |
| 1370 |
|
| 1371 |
s_instances = ut::new_withkey<Instances>(UT_NEW_THIS_FILE_PSI_KEY); |
| 1372 |
|
| 1373 |
if (s_instances == nullptr) { |
| 1374 |
return DB_OUT_OF_MEMORY; |
| 1375 |
} |
| 1376 |
|
| 1377 |
dberr_t err{DB_SUCCESS}; |
| 1378 |
|
| 1379 |
for (uint32_t i = 0; i < s_n_instances; ++i) { |
| 1380 |
auto ptr = ut::new_withkey<Double_write>(UT_NEW_THIS_FILE_PSI_KEY, i, |
| 1381 |
dblwr::n_pages); |
| 1382 |
|
| 1383 |
if (ptr == nullptr) { |
| 1384 |
err = DB_OUT_OF_MEMORY; |
| 1385 |
break; |
| 1386 |
} |
| 1387 |
|
| 1388 |
s_instances->push_back(ptr); |
| 1389 |
} |
| 1390 |
|
| 1391 |
if (err != DB_SUCCESS) { |
| 1392 |
for (auto &dblwr : *s_instances) { |
| 1393 |
ut::delete_(dblwr); |
| 1394 |
} |
| 1395 |
ut::delete_(s_instances); |
| 1396 |
s_instances = nullptr; |
| 1397 |
} |
| 1398 |
|
| 1399 |
return err; |
| 1400 |
} |
| 1401 |
|
| 1402 |
dberr_t Double_write::create_reduced() noexcept { |
| 1403 |
ut_a(!s_files.empty()); |
| 1404 |
ut_a(s_r_instances == nullptr); |
| 1405 |
|
| 1406 |
s_r_instances = ut::new_<Instances>(); |
| 1407 |
|
| 1408 |
if (s_r_instances == nullptr) { |
| 1409 |
return DB_OUT_OF_MEMORY; |
| 1410 |
} |
| 1411 |
|
| 1412 |
dberr_t err{DB_SUCCESS}; |
| 1413 |
|
| 1414 |
for (uint32_t i = 0; i < s_n_instances; ++i) { |
| 1415 |
auto ptr = ut::new_<Reduced_double_write>(i, dblwr::n_pages); |
| 1416 |
|
| 1417 |
if (ptr == nullptr) { |
| 1418 |
err = DB_OUT_OF_MEMORY; |
| 1419 |
break; |
| 1420 |
} |
| 1421 |
|
| 1422 |
err = ptr->allocate(); |
| 1423 |
|
| 1424 |
if (err != DB_SUCCESS) { |
| 1425 |
return err; |
| 1426 |
} |
| 1427 |
|
| 1428 |
s_r_instances->push_back(ptr); |
| 1429 |
} |
| 1430 |
|
| 1431 |
if (err != DB_SUCCESS) { |
| 1432 |
for (auto &dblwr : *s_r_instances) { |
| 1433 |
ut::delete_(dblwr); |
| 1434 |
} |
| 1435 |
ut::delete_(s_r_instances); |
| 1436 |
s_r_instances = nullptr; |
| 1437 |
} |
| 1438 |
|
| 1439 |
return err; |
| 1440 |
} |
| 1441 |
|
| 1442 |
void Double_write::free_segments(Batch_segments *&segments) noexcept { |
| 1443 |
if (segments != nullptr) { |
| 1444 |
Batch_segment *s{}; |
| 1445 |
while (segments->dequeue(s)) { |
| 1446 |
ut::delete_(s); |
| 1447 |
} |
| 1448 |
ut::delete_(segments); |
| 1449 |
segments = nullptr; |
| 1450 |
} |
| 1451 |
} |
| 1452 |
|
| 1453 |
void Double_write::shutdown() noexcept { |
| 1454 |
if (s_instances == nullptr) { |
| 1455 |
return; |
| 1456 |
} |
| 1457 |
|
| 1458 |
unsigned long long n_bytes = 0; |
| 1459 |
for (auto dblwr : *s_instances) { |
| 1460 |
n_bytes += dblwr->m_bytes_written; |
| 1461 |
ut::delete_(dblwr); |
| 1462 |
} |
| 1463 |
|
| 1464 |
ib::info(ER_IB_DBLWR_BYTES_INFO) |
| 1465 |
<< "Bytes written to disk by DBLWR (ON): " << n_bytes; |
| 1466 |
|
| 1467 |
n_bytes = 0; |
| 1468 |
if (s_r_instances != nullptr) { |
| 1469 |
for (auto dblwr : *s_r_instances) { |
| 1470 |
n_bytes += dblwr->m_bytes_written; |
| 1471 |
ut::delete_(dblwr); |
| 1472 |
} |
| 1473 |
|
| 1474 |
ib::info(ER_IB_RDBLWR_BYTES_INFO) |
| 1475 |
<< "Bytes written to disk by DBLWR (REDUCED): " << n_bytes; |
| 1476 |
} |
| 1477 |
|
| 1478 |
for (auto &file : s_files) { |
| 1479 |
if (file.m_pfs.m_file != OS_FILE_CLOSED) { |
| 1480 |
os_file_close(file.m_pfs); |
| 1481 |
} |
| 1482 |
} |
| 1483 |
|
| 1484 |
s_files.clear(); |
| 1485 |
|
| 1486 |
for (auto &file : s_r_files) { |
| 1487 |
if (file.m_pfs.m_file != OS_FILE_CLOSED) { |
| 1488 |
os_file_close(file.m_pfs); |
| 1489 |
} |
| 1490 |
} |
| 1491 |
|
| 1492 |
s_r_files.clear(); |
| 1493 |
|
| 1494 |
free_segments(s_LRU_batch_segments); |
| 1495 |
free_segments(s_flush_list_batch_segments); |
| 1496 |
free_segments(s_r_LRU_batch_segments); |
| 1497 |
free_segments(s_r_flush_list_batch_segments); |
| 1498 |
|
| 1499 |
if (s_single_segments != nullptr) { |
| 1500 |
Segment *s{}; |
| 1501 |
while (s_single_segments->dequeue(s)) { |
| 1502 |
ut::delete_(s); |
| 1503 |
} |
| 1504 |
ut::delete_(s_single_segments); |
| 1505 |
s_single_segments = nullptr; |
| 1506 |
} |
| 1507 |
|
| 1508 |
ut::delete_(s_instances); |
| 1509 |
ut::delete_(s_r_instances); |
| 1510 |
s_instances = nullptr; |
| 1511 |
s_r_instances = nullptr; |
| 1512 |
} |
| 1513 |
|
| 1514 |
void Double_write::check_page_lsn(const page_t *page) noexcept { |
| 1515 |
if (memcmp( |
| 1516 |
page + (FIL_PAGE_LSN + 4), |
| 1517 |
page + (univ_page_size.physical() - FIL_PAGE_END_LSN_OLD_CHKSUM + 4), |
| 1518 |
4)) { |
| 1519 |
const uint32_t lsn1 = mach_read_from_4(page + FIL_PAGE_LSN + 4); |
| 1520 |
|
| 1521 |
const uint32_t lsn2 = mach_read_from_4(page + univ_page_size.physical() - |
| 1522 |
FIL_PAGE_END_LSN_OLD_CHKSUM + 4); |
| 1523 |
|
| 1524 |
ib::error(ER_IB_MSG_111) << "The page to be written seems corrupt!" |
| 1525 |
" The low 4 bytes of LSN fields do not match" |
| 1526 |
" (" |
| 1527 |
<< lsn1 << " != " << lsn2 |
| 1528 |
<< ")!" |
| 1529 |
" Noticed in the buffer pool."; |
| 1530 |
} |
| 1531 |
} |
| 1532 |
|
| 1533 |
void Double_write::croak(const buf_block_t *block) noexcept { |
| 1534 |
buf_page_print(block->frame, univ_page_size, BUF_PAGE_PRINT_NO_CRASH); |
| 1535 |
|
| 1536 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_112) |
| 1537 |
<< "Apparent corruption of an index page " << block->page.id |
| 1538 |
<< " to be written to data file. We intentionally crash" |
| 1539 |
" the server to prevent corrupt data from ending up in" |
| 1540 |
" data files."; |
| 1541 |
} |
| 1542 |
|
| 1543 |
void Double_write::check_block(const buf_block_t *block) noexcept { |
| 1544 |
ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE); |
| 1545 |
|
| 1546 |
check_page_lsn(block->frame); |
| 1547 |
|
| 1548 |
switch (fil_page_get_type(block->frame)) { |
| 1549 |
case FIL_PAGE_INDEX: |
| 1550 |
case FIL_PAGE_RTREE: |
| 1551 |
case FIL_PAGE_SDI: |
| 1552 |
if (page_is_comp(block->frame)) { |
| 1553 |
if (page_simple_validate_new(block->frame)) { |
| 1554 |
return; |
| 1555 |
} |
| 1556 |
} else if (page_simple_validate_old(block->frame)) { |
| 1557 |
return; |
| 1558 |
} |
| 1559 |
/* While it is possible that this is not an index page |
| 1560 |
but just happens to have wrongly set FIL_PAGE_TYPE, |
| 1561 |
such pages should never be modified to without also |
| 1562 |
adjusting the page type during page allocation or |
| 1563 |
buf_flush_init_for_writing() or fil_page_reset_type(). */ |
| 1564 |
break; |
| 1565 |
case FIL_PAGE_TYPE_FSP_HDR: |
| 1566 |
case FIL_PAGE_IBUF_BITMAP: |
| 1567 |
case FIL_PAGE_TYPE_UNKNOWN: |
| 1568 |
/* Do not complain again, we already reset this field. */ |
| 1569 |
case FIL_PAGE_UNDO_LOG: |
| 1570 |
case FIL_PAGE_INODE: |
| 1571 |
case FIL_PAGE_IBUF_FREE_LIST: |
| 1572 |
case FIL_PAGE_TYPE_SYS: |
| 1573 |
case FIL_PAGE_TYPE_TRX_SYS: |
| 1574 |
case FIL_PAGE_TYPE_XDES: |
| 1575 |
case FIL_PAGE_TYPE_BLOB: |
| 1576 |
case FIL_PAGE_TYPE_ZBLOB: |
| 1577 |
case FIL_PAGE_TYPE_ZBLOB2: |
| 1578 |
case FIL_PAGE_SDI_BLOB: |
| 1579 |
case FIL_PAGE_SDI_ZBLOB: |
| 1580 |
case FIL_PAGE_TYPE_LOB_INDEX: |
| 1581 |
case FIL_PAGE_TYPE_LOB_DATA: |
| 1582 |
case FIL_PAGE_TYPE_LOB_FIRST: |
| 1583 |
case FIL_PAGE_TYPE_ZLOB_FIRST: |
| 1584 |
case FIL_PAGE_TYPE_ZLOB_DATA: |
| 1585 |
case FIL_PAGE_TYPE_ZLOB_INDEX: |
| 1586 |
case FIL_PAGE_TYPE_ZLOB_FRAG: |
| 1587 |
case FIL_PAGE_TYPE_ZLOB_FRAG_ENTRY: |
| 1588 |
case FIL_PAGE_TYPE_RSEG_ARRAY: |
| 1589 |
case FIL_PAGE_TYPE_LEGACY_DBLWR: |
| 1590 |
|
| 1591 |
/* TODO: validate also non-index pages */ |
| 1592 |
return; |
| 1593 |
|
| 1594 |
case FIL_PAGE_TYPE_ALLOCATED: |
| 1595 |
/* Empty pages should never be flushed. Unless we are creating the |
| 1596 |
legacy doublewrite buffer. */ |
| 1597 |
break; |
| 1598 |
} |
| 1599 |
|
| 1600 |
croak(block); |
| 1601 |
} |
| 1602 |
|
| 1603 |
dberr_t Double_write::write_to_datafile(const buf_page_t *in_bpage, bool sync, |
| 1604 |
const file::Block *e_block) noexcept { |
| 1605 |
ut_ad(buf_page_in_file(in_bpage)); |
| 1606 |
ut_ad(in_bpage->current_thread_has_io_responsibility()); |
| 1607 |
ut_ad(in_bpage->is_io_fix_write()); |
| 1608 |
uint32_t len; |
| 1609 |
void *frame{}; |
| 1610 |
|
| 1611 |
if (e_block == nullptr) { |
| 1612 |
Double_write::prepare(in_bpage, &frame, &len); |
| 1613 |
} else { |
| 1614 |
frame = os_block_get_frame(e_block); |
| 1615 |
len = e_block->m_size; |
| 1616 |
} |
| 1617 |
|
| 1618 |
/* Our IO API is common for both reads and writes and is |
| 1619 |
therefore geared towards a non-const parameter. */ |
| 1620 |
auto bpage = const_cast<buf_page_t *>(in_bpage); |
| 1621 |
|
| 1622 |
uint32_t type = IORequest::WRITE; |
| 1623 |
|
| 1624 |
if (sync) { |
| 1625 |
type |= IORequest::DO_NOT_WAKE; |
| 1626 |
} |
| 1627 |
|
| 1628 |
IORequest io_request(type); |
| 1629 |
io_request.set_encrypted_block(e_block); |
| 1630 |
|
| 1631 |
#ifdef UNIV_DEBUG |
| 1632 |
{ |
| 1633 |
byte *page = static_cast<byte *>(frame); |
| 1634 |
ut_ad(mach_read_from_4(page + FIL_PAGE_OFFSET) == bpage->page_no()); |
| 1635 |
ut_ad(mach_read_from_4(page + FIL_PAGE_SPACE_ID) == bpage->space()); |
| 1636 |
} |
| 1637 |
#endif /* UNIV_DEBUG */ |
| 1638 |
|
| 1639 |
io_request.set_original_size(bpage->size.physical()); |
| 1640 |
auto err = |
| 1641 |
fil_io(io_request, sync, bpage->id, bpage->size, 0, len, frame, bpage); |
| 1642 |
|
| 1643 |
/* When a tablespace is deleted with BUF_REMOVE_NONE, fil_io() might |
| 1644 |
return DB_PAGE_IS_STALE or DB_TABLESPACE_DELETED. */ |
| 1645 |
ut_a(err == DB_SUCCESS || err == DB_TABLESPACE_DELETED || |
| 1646 |
err == DB_PAGE_IS_STALE); |
| 1647 |
|
| 1648 |
return err; |
| 1649 |
} |
| 1650 |
|
| 1651 |
dberr_t Double_write::sync_page_flush(buf_page_t *bpage, |
| 1652 |
file::Block *e_block) noexcept { |
| 1653 |
#ifdef UNIV_DEBUG |
| 1654 |
ut_d(auto page_id = bpage->id); |
| 1655 |
|
| 1656 |
if (dblwr::Force_crash == page_id) { |
| 1657 |
auto frame = reinterpret_cast<const buf_block_t *>(bpage)->frame; |
| 1658 |
const auto p = reinterpret_cast<byte *>(frame); |
| 1659 |
|
| 1660 |
ut_ad(page_get_space_id(p) == dblwr::Force_crash.space()); |
| 1661 |
ut_ad(page_get_page_no(p) == dblwr::Force_crash.page_no()); |
| 1662 |
} |
| 1663 |
#endif /* UNIV_DEBUG */ |
| 1664 |
|
| 1665 |
Segment *segment{}; |
| 1666 |
|
| 1667 |
while (!s_single_segments->dequeue(segment)) { |
| 1668 |
std::this_thread::yield(); |
| 1669 |
} |
| 1670 |
|
| 1671 |
single_write(segment, bpage, e_block); |
| 1672 |
|
| 1673 |
#ifndef _WIN32 |
| 1674 |
if (is_fsync_required()) { |
| 1675 |
segment->flush(); |
| 1676 |
} |
| 1677 |
#endif /* !_WIN32 */ |
| 1678 |
|
| 1679 |
#ifdef UNIV_DEBUG |
| 1680 |
if (dblwr::Force_crash == page_id) { |
| 1681 |
DBUG_SUICIDE(); |
| 1682 |
} |
| 1683 |
#endif /* UNIV_DEBUG */ |
| 1684 |
|
| 1685 |
auto err = write_to_datafile(bpage, true, e_block); |
| 1686 |
|
| 1687 |
if (err == DB_SUCCESS) { |
| 1688 |
fil_flush(bpage->id.space()); |
| 1689 |
} else { |
| 1690 |
/* This block is not freed if the write_to_datafile doesn't succeed. */ |
| 1691 |
if (e_block != nullptr) { |
| 1692 |
os_free_block(e_block); |
| 1693 |
} |
| 1694 |
} |
| 1695 |
|
| 1696 |
while (!s_single_segments->enqueue(segment)) { |
| 1697 |
UT_RELAX_CPU(); |
| 1698 |
} |
| 1699 |
|
| 1700 |
/* true means we want to evict this page from the LRU list as well. */ |
| 1701 |
buf_page_io_complete(bpage, true); |
| 1702 |
|
| 1703 |
return DB_SUCCESS; |
| 1704 |
} |
| 1705 |
|
| 1706 |
void Double_write::reset_file(dblwr::File &file, bool truncate) noexcept { |
| 1707 |
auto cur_size = os_file_get_size(file.m_pfs); |
| 1708 |
auto new_size = dblwr::File::s_n_pages * univ_page_size.physical(); |
| 1709 |
|
| 1710 |
if (s_files.size() == 1) { |
| 1711 |
new_size += SYNC_PAGE_FLUSH_SLOTS * univ_page_size.physical(); |
| 1712 |
} else if (file.is_for_lru()) { |
| 1713 |
const auto n_bytes = (SYNC_PAGE_FLUSH_SLOTS / (s_files.size() / 2)) * |
| 1714 |
univ_page_size.physical(); |
| 1715 |
new_size += n_bytes; |
| 1716 |
} |
| 1717 |
|
| 1718 |
auto pfs_file = file.m_pfs; |
| 1719 |
|
| 1720 |
if (new_size < cur_size && truncate) { |
| 1721 |
ib::info(ER_IB_MSG_DBLWR_1306) |
| 1722 |
<< file.m_name << " size reduced to " << new_size << " bytes from " |
| 1723 |
<< cur_size << " bytes"; |
| 1724 |
|
| 1725 |
auto success = os_file_truncate(file.m_name.c_str(), pfs_file, new_size); |
| 1726 |
|
| 1727 |
if (!success) { |
| 1728 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_DBLWR_1320, file.m_name.c_str()); |
| 1729 |
} |
| 1730 |
|
| 1731 |
} else if (new_size > cur_size) { |
| 1732 |
auto err = os_file_write_zeros(pfs_file, file.m_name.c_str(), |
| 1733 |
univ_page_size.physical(), cur_size, |
| 1734 |
new_size - cur_size); |
| 1735 |
|
| 1736 |
if (err != DB_SUCCESS) { |
| 1737 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_DBLWR_1321, file.m_name.c_str()); |
| 1738 |
} |
| 1739 |
|
| 1740 |
ib::info(ER_IB_MSG_DBLWR_1307) |
| 1741 |
<< file.m_name << " size increased to " << new_size << " bytes " |
| 1742 |
<< "from " << cur_size << " bytes"; |
| 1743 |
} |
| 1744 |
} |
| 1745 |
|
| 1746 |
void Double_write::reduced_reset_file(dblwr::File &file, |
| 1747 |
uint32_t pages_per_file, |
| 1748 |
uint32_t phy_size) noexcept { |
| 1749 |
auto cur_size = os_file_get_size(file.m_pfs); |
| 1750 |
auto new_size = pages_per_file * phy_size; |
| 1751 |
auto pfs_file = file.m_pfs; |
| 1752 |
|
| 1753 |
if (new_size > cur_size) { |
| 1754 |
auto err = os_file_write_zeros(pfs_file, file.m_name.c_str(), phy_size, |
| 1755 |
cur_size, new_size - cur_size); |
| 1756 |
|
| 1757 |
if (err != DB_SUCCESS) { |
| 1758 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_DBLWR_1321, file.m_name.c_str()); |
| 1759 |
} |
| 1760 |
|
| 1761 |
ib::info(ER_IB_MSG_DBLWR_1307) |
| 1762 |
<< file.m_name << " size increased to " << new_size << " bytes " |
| 1763 |
<< "from " << cur_size << " bytes"; |
| 1764 |
} |
| 1765 |
} |
| 1766 |
|
| 1767 |
dberr_t Double_write::init_file(dblwr::File &file, uint32_t n_pages, |
| 1768 |
uint32_t phy_size) noexcept { |
| 1769 |
auto pfs_file = file.m_pfs; |
| 1770 |
auto size = os_file_get_size(pfs_file); |
| 1771 |
|
| 1772 |
ut_ad(dblwr::File::s_n_pages > 0); |
| 1773 |
|
| 1774 |
if (size == 0) { |
| 1775 |
auto err = os_file_write_zeros(pfs_file, file.m_name.c_str(), phy_size, 0, |
| 1776 |
n_pages * phy_size); |
| 1777 |
|
| 1778 |
if (err != DB_SUCCESS) { |
| 1779 |
return err; |
| 1780 |
} |
| 1781 |
} |
| 1782 |
|
| 1783 |
return DB_SUCCESS; |
| 1784 |
} |
| 1785 |
|
| 1786 |
static bool is_buffer_pool_size_ok() noexcept { |
| 1787 |
const auto min_doublewrite_size = |
| 1788 |
((2 * DBLWR_V1_EXTENT_SIZE + FSP_EXTENT_SIZE / 2 + 100) * |
| 1789 |
univ_page_size.physical()); |
| 1790 |
|
| 1791 |
if (buf_pool_get_curr_size() < min_doublewrite_size) { |
| 1792 |
ib::error(ER_IB_MSG_DBLWR_1309) |
| 1793 |
<< "Buffer pool size is too small, must be at least " |
| 1794 |
<< min_doublewrite_size << " bytes"; |
| 1795 |
|
| 1796 |
return false; |
| 1797 |
} |
| 1798 |
|
| 1799 |
return true; |
| 1800 |
} |
| 1801 |
|
| 1802 |
byte *Double_write::get(mtr_t *mtr) noexcept { |
| 1803 |
const page_id_t sys_page_id(TRX_SYS_SPACE, TRX_SYS_PAGE_NO); |
| 1804 |
|
| 1805 |
auto block = buf_page_get(sys_page_id, univ_page_size, RW_X_LATCH, |
| 1806 |
UT_LOCATION_HERE, mtr); |
| 1807 |
|
| 1808 |
buf_block_dbg_add_level(block, SYNC_NO_ORDER_CHECK); |
| 1809 |
|
| 1810 |
return buf_block_get_frame(block) + TRX_SYS_DBLWR_V1; |
| 1811 |
} |
| 1812 |
|
| 1813 |
bool Double_write::init_v1(page_no_t &page_no1, page_no_t &page_no2) noexcept { |
| 1814 |
mtr_t mtr; |
| 1815 |
|
| 1816 |
mtr.start(); |
| 1817 |
|
| 1818 |
bool init{}; |
| 1819 |
auto doublewrite = get(&mtr); |
| 1820 |
|
| 1821 |
if (mach_read_from_4(doublewrite + DBLWR_VER) == DBLWR_V1) { |
| 1822 |
/* The doublewrite buffer has already been created. */ |
| 1823 |
|
| 1824 |
page_no1 = mach_read_from_4(doublewrite + DBLWR_V1_BLOCK1); |
| 1825 |
page_no2 = mach_read_from_4(doublewrite + DBLWR_V1_BLOCK2); |
| 1826 |
|
| 1827 |
init = true; |
| 1828 |
} else { |
| 1829 |
ib::warn(ER_IB_MSG_DBLWR_1327) |
| 1830 |
<< "Legacy double write doesn't exist in the system tablespace!"; |
| 1831 |
init = false; |
| 1832 |
} |
| 1833 |
|
| 1834 |
mtr_commit(&mtr); |
| 1835 |
|
| 1836 |
return init; |
| 1837 |
} |
| 1838 |
|
| 1839 |
bool Double_write::create_v1(page_no_t &page_no1, |
| 1840 |
page_no_t &page_no2) noexcept { |
| 1841 |
mtr_t mtr; |
| 1842 |
|
| 1843 |
ib::info(ER_IB_MSG_95) << "Legacy doublewrite buffer not found: creating new"; |
| 1844 |
|
| 1845 |
if (!is_buffer_pool_size_ok()) { |
| 1846 |
return false; |
| 1847 |
} |
| 1848 |
|
| 1849 |
mtr.start(); |
| 1850 |
|
| 1851 |
auto block2 = fseg_create(TRX_SYS_SPACE, TRX_SYS_PAGE_NO, |
| 1852 |
TRX_SYS_DBLWR_V1 + DBLWR_V1_FSEG, &mtr); |
| 1853 |
|
| 1854 |
if (block2 == nullptr) { |
| 1855 |
ib::error(ER_IB_MSG_DBLWR_1287); |
| 1856 |
mtr.commit(); |
| 1857 |
return false; |
| 1858 |
} |
| 1859 |
|
| 1860 |
/* fseg_create acquires a second latch on the page, |
| 1861 |
therefore we must declare it. */ |
| 1862 |
buf_block_dbg_add_level(block2, SYNC_NO_ORDER_CHECK); |
| 1863 |
|
| 1864 |
uint32_t prev_page_no = 0; |
| 1865 |
byte *doublewrite = get(&mtr); |
| 1866 |
byte *fseg_header = doublewrite + DBLWR_V1_FSEG; |
| 1867 |
const uint32_t n_blocks = 2 * DBLWR_V1_EXTENT_SIZE + FSP_EXTENT_SIZE / 2; |
| 1868 |
|
| 1869 |
for (uint32_t i = 0; i < n_blocks; ++i) { |
| 1870 |
auto new_block = |
| 1871 |
fseg_alloc_free_page(fseg_header, prev_page_no + 1, FSP_UP, &mtr); |
| 1872 |
|
| 1873 |
if (new_block == nullptr) { |
| 1874 |
ib::error(ER_IB_MSG_DBLWR_1288); |
| 1875 |
mtr.commit(); |
| 1876 |
return false; |
| 1877 |
} |
| 1878 |
|
| 1879 |
/* Note: We don't redo log this because we don't care. */ |
| 1880 |
mach_write_to_2(new_block->frame + FIL_PAGE_TYPE, |
| 1881 |
FIL_PAGE_TYPE_LEGACY_DBLWR); |
| 1882 |
|
| 1883 |
/* We read the allocated pages to the buffer pool; |
| 1884 |
when they are written to disk in a flush, the space |
| 1885 |
id and page number fields are also written to the |
| 1886 |
pages. At database startup read pages from the |
| 1887 |
doublewrite buffer, we know that if the space id and |
| 1888 |
page number in them are the same as the page position |
| 1889 |
in the tablespace, then the page has not been written |
| 1890 |
to in doublewrite. */ |
| 1891 |
|
| 1892 |
ut_ad(rw_lock_get_x_lock_count(&new_block->lock) == 1); |
| 1893 |
|
| 1894 |
auto page_no = new_block->page.id.page_no(); |
| 1895 |
|
| 1896 |
if (i == FSP_EXTENT_SIZE / 2) { |
| 1897 |
ut_a(page_no == FSP_EXTENT_SIZE); |
| 1898 |
|
| 1899 |
mlog_write_ulint(doublewrite + DBLWR_V1_BLOCK1, page_no, MLOG_4BYTES, |
| 1900 |
&mtr); |
| 1901 |
|
| 1902 |
mlog_write_ulint(doublewrite + DBLWR_V1_REPEAT + DBLWR_V1_BLOCK1, page_no, |
| 1903 |
MLOG_4BYTES, &mtr); |
| 1904 |
|
| 1905 |
page_no1 = page_no; |
| 1906 |
|
| 1907 |
} else if (i == FSP_EXTENT_SIZE / 2 + DBLWR_V1_EXTENT_SIZE) { |
| 1908 |
ut_a(page_no == 2 * FSP_EXTENT_SIZE); |
| 1909 |
|
| 1910 |
mlog_write_ulint(doublewrite + DBLWR_V1_BLOCK2, page_no, MLOG_4BYTES, |
| 1911 |
&mtr); |
| 1912 |
|
| 1913 |
mlog_write_ulint(doublewrite + DBLWR_V1_REPEAT + DBLWR_V1_BLOCK2, page_no, |
| 1914 |
MLOG_4BYTES, &mtr); |
| 1915 |
|
| 1916 |
page_no2 = page_no; |
| 1917 |
|
| 1918 |
} else if (i > FSP_EXTENT_SIZE / 2) { |
| 1919 |
ut_a(page_no == prev_page_no + 1); |
| 1920 |
} |
| 1921 |
|
| 1922 |
if (((i + 1) & 15) == 0) { |
| 1923 |
/* rw_locks can only be recursively x-locked |
| 1924 |
2048 times. (on 32 bit platforms, (lint) 0 - (X_LOCK_DECR * 2049) |
| 1925 |
is no longer a negative number, and thus lock_word becomes like a |
| 1926 |
shared lock). For 4k page size this loop will lock the fseg header |
| 1927 |
too many times. Since this code is not done while any other threads |
| 1928 |
are active, restart the MTR occasionally. */ |
| 1929 |
|
| 1930 |
mtr.commit(); |
| 1931 |
|
| 1932 |
mtr.start(); |
| 1933 |
|
| 1934 |
doublewrite = get(&mtr); |
| 1935 |
|
| 1936 |
fseg_header = doublewrite + DBLWR_V1_FSEG; |
| 1937 |
} |
| 1938 |
|
| 1939 |
prev_page_no = page_no; |
| 1940 |
} |
| 1941 |
|
| 1942 |
auto ptr = doublewrite + DBLWR_VER; |
| 1943 |
|
| 1944 |
mlog_write_ulint(ptr, DBLWR_V1, MLOG_4BYTES, &mtr); |
| 1945 |
|
| 1946 |
ptr += DBLWR_V1_REPEAT; |
| 1947 |
|
| 1948 |
mlog_write_ulint(ptr, DBLWR_V1, MLOG_4BYTES, &mtr); |
| 1949 |
|
| 1950 |
ptr = doublewrite + DBLWR_V1_SPACE_ID_STORED; |
| 1951 |
|
| 1952 |
mlog_write_ulint(ptr, DBLWR_V1_SPACE_ID_STORED_N, MLOG_4BYTES, &mtr); |
| 1953 |
|
| 1954 |
mtr.commit(); |
| 1955 |
|
| 1956 |
/* Flush the modified pages to disk and make a checkpoint. */ |
| 1957 |
log_make_latest_checkpoint(); |
| 1958 |
|
| 1959 |
/* Remove doublewrite pages from the LRU list. */ |
| 1960 |
buf_pool_invalidate(); |
| 1961 |
|
| 1962 |
ib::info(ER_IB_MSG_99) << "Legacy atomic write buffer created"; |
| 1963 |
|
| 1964 |
return true; |
| 1965 |
} |
| 1966 |
|
| 1967 |
dberr_t Double_write::load(dblwr::File &file, recv::Pages *pages) noexcept { |
| 1968 |
os_offset_t size = os_file_get_size(file.m_pfs); |
| 1969 |
|
| 1970 |
if (size == 0) { |
| 1971 |
/* Double write buffer is empty. */ |
| 1972 |
ib::info(ER_IB_MSG_DBLWR_1285, file.m_name.c_str()); |
| 1973 |
|
| 1974 |
return DB_SUCCESS; |
| 1975 |
} |
| 1976 |
|
| 1977 |
if ((size % univ_page_size.physical())) { |
| 1978 |
ib::warn(ER_IB_MSG_DBLWR_1319, file.m_name.c_str(), (ulint)size, |
| 1979 |
(ulint)univ_page_size.physical()); |
| 1980 |
} |
| 1981 |
|
| 1982 |
const uint32_t n_pages = size / univ_page_size.physical(); |
| 1983 |
|
| 1984 |
Buffer buffer{n_pages}; |
| 1985 |
IORequest read_request(IORequest::READ); |
| 1986 |
|
| 1987 |
read_request.disable_compression(); |
| 1988 |
|
| 1989 |
auto err = os_file_read(read_request, file.m_name.c_str(), file.m_pfs, |
| 1990 |
buffer.begin(), 0, buffer.capacity()); |
| 1991 |
|
| 1992 |
if (err != DB_SUCCESS) { |
| 1993 |
ib::error(ER_IB_MSG_DBLWR_1301, ut_strerr(err)); |
| 1994 |
|
| 1995 |
return err; |
| 1996 |
} |
| 1997 |
|
| 1998 |
auto page = buffer.begin(); |
| 1999 |
|
| 2000 |
for (uint32_t i = 0; i < n_pages; ++i) { |
| 2001 |
pages->add(i, page, univ_page_size.physical()); |
| 2002 |
page += univ_page_size.physical(); |
| 2003 |
} |
| 2004 |
|
| 2005 |
return DB_SUCCESS; |
| 2006 |
} |
| 2007 |
|
| 2008 |
/** Reduced doublewrite file deserializer. Used during crash recovery. */ |
| 2009 |
class Reduced_batch_deserializer { |
| 2010 |
public: |
| 2011 |
/** Constructor |
| 2012 |
@param[in] buf Buffer to hold the Reduced dblwr pages |
| 2013 |
@param[in] n_pages Number of reduced dblwr pages */ |
| 2014 |
explicit Reduced_batch_deserializer(Buffer *buf, uint32_t n_pages) |
| 2015 |
: m_buf(buf), m_n_pages(n_pages) {} |
| 2016 |
|
| 2017 |
/** Deserialize page and call Functor f for each page_entry found |
| 2018 |
from reduced dblwr page |
| 2019 |
@param[in] f Functor to process page entry from dblwr page |
| 2020 |
@return DB_SUCCESS on success, others of checksum or parsing failures */ |
| 2021 |
template <typename F> |
| 2022 |
dberr_t deserialize(F &f) { |
| 2023 |
auto page = m_buf->begin(); |
| 2024 |
for (uint32_t i = 0; i < m_n_pages; ++i) { |
| 2025 |
if (ut::is_zeros(page, REDUCED_BATCH_PAGE_SIZE)) { |
| 2026 |
page += REDUCED_BATCH_PAGE_SIZE; |
| 2027 |
continue; |
| 2028 |
} |
| 2029 |
dberr_t err = parse_page(page, f); |
| 2030 |
if (err != DB_SUCCESS) { |
| 2031 |
ib::error(ER_REDUCED_DBLWR_FILE_CORRUPTED, i); |
| 2032 |
return (err); |
| 2033 |
} |
| 2034 |
page += REDUCED_BATCH_PAGE_SIZE; |
| 2035 |
} |
| 2036 |
return (DB_SUCCESS); |
| 2037 |
} |
| 2038 |
|
| 2039 |
private: |
| 2040 |
/** Parse reduced dblwr batch page header |
| 2041 |
@param[in] page Page to parse |
| 2042 |
@param[in] data_len length of data in page |
| 2043 |
@return DB_SUCCESS on success, others on failure */ |
| 2044 |
dberr_t parse_header(const byte *page, uint16_t *data_len) noexcept { |
| 2045 |
// uint32_t batch_id = mach_read_from_4(page + RB_OFF_BATCH_ID); |
| 2046 |
uint32_t checksum = mach_read_from_4(page + RB_OFF_CHECKSUM); |
| 2047 |
*data_len = mach_read_from_2(page + RB_OFF_DATA_LEN); |
| 2048 |
// buf_flush_t flush_type = |
| 2049 |
// static_cast<buf_flush_t>(page[RB_OFF_BATCH_TYPE]); |
| 2050 |
|
| 2051 |
if (*data_len == 0) { |
| 2052 |
return (DB_CORRUPTION); |
| 2053 |
} |
| 2054 |
if (*data_len % REDUCED_ENTRY_SIZE != 0) { |
| 2055 |
return (DB_CORRUPTION); |
| 2056 |
} |
| 2057 |
|
| 2058 |
uint32_t calc_checksum = ut_crc32(page + REDUCED_HEADER_SIZE, *data_len); |
| 2059 |
if (checksum != calc_checksum) { |
| 2060 |
return (DB_CORRUPTION); |
| 2061 |
} |
| 2062 |
return (DB_SUCCESS); |
| 2063 |
} |
| 2064 |
|
| 2065 |
/* Utility function to parse page |
| 2066 |
@param[in] page reduced dblwr batch page |
| 2067 |
@param[in] f Callback function that process page entries |
| 2068 |
@return DB_SUCCESS on success */ |
| 2069 |
template <typename F> |
| 2070 |
dberr_t parse_page(const byte *page, F &f) noexcept { |
| 2071 |
uint16_t data_len{}; |
| 2072 |
|
| 2073 |
dberr_t err = parse_header(page, &data_len); |
| 2074 |
if (err != DB_SUCCESS) { |
| 2075 |
return (err); |
| 2076 |
} |
| 2077 |
|
| 2078 |
parse_page_data(page, data_len, f); |
| 2079 |
return (DB_SUCCESS); |
| 2080 |
} |
| 2081 |
|
| 2082 |
/** Utility function to parse page data |
| 2083 |
@param[in] page reduced dblwr batch page |
| 2084 |
@param[in] data_len length of data in page |
| 2085 |
@param[in] f Callback function that process page entries */ |
| 2086 |
template <typename F> |
| 2087 |
void parse_page_data(const byte *page, uint16_t data_len, F &f) noexcept { |
| 2088 |
const byte *page_data = page + REDUCED_HEADER_SIZE; |
| 2089 |
#ifdef UNIV_DEBUG |
| 2090 |
const byte *page_start [[maybe_unused]] = page + REDUCED_HEADER_SIZE; |
| 2091 |
#endif /* UNIV_DEBUG */ |
| 2092 |
const uint32_t expected_entries = data_len / REDUCED_ENTRY_SIZE; |
| 2093 |
for (uint32_t entry = 1; entry <= expected_entries; ++entry) { |
| 2094 |
space_id_t space_id = mach_read_from_4(page_data); |
| 2095 |
page_data += 4; |
| 2096 |
page_no_t page_num = mach_read_from_4(page_data); |
| 2097 |
page_data += 4; |
| 2098 |
lsn_t lsn = mach_read_from_8(page_data); |
| 2099 |
page_data += 8; |
| 2100 |
Page_entry pe(space_id, page_num, lsn); |
| 2101 |
f(pe); |
| 2102 |
} |
| 2103 |
|
| 2104 |
ut_ad(static_cast<uint32_t>(page_data - page_start) == |
| 2105 |
(expected_entries * REDUCED_ENTRY_SIZE)); |
| 2106 |
} |
| 2107 |
|
| 2108 |
private: |
| 2109 |
/** Temporary buffer to hold Reduced dblwr pages */ |
| 2110 |
Buffer *m_buf; |
| 2111 |
/** Number of reduced dblwr pages */ |
| 2112 |
uint32_t m_n_pages; |
| 2113 |
}; |
| 2114 |
|
| 2115 |
dberr_t Double_write::load_reduced_batch(dblwr::File &file, |
| 2116 |
recv::Pages *pages) noexcept { |
| 2117 |
os_offset_t size = os_file_get_size(file.m_pfs); |
| 2118 |
|
| 2119 |
if (srv_read_only_mode) { |
| 2120 |
ib::info() << "Skipping doublewrite buffer processing due to " |
| 2121 |
"InnoDB running in read only mode"; |
| 2122 |
return (DB_SUCCESS); |
| 2123 |
} |
| 2124 |
|
| 2125 |
if (size == 0) { |
| 2126 |
/* Double write buffer is empty. */ |
| 2127 |
ib::info(ER_IB_MSG_DBLWR_1285, file.m_name.c_str()); |
| 2128 |
|
| 2129 |
return DB_SUCCESS; |
| 2130 |
} |
| 2131 |
|
| 2132 |
if ((size % REDUCED_BATCH_PAGE_SIZE) != 0) { |
| 2133 |
ib::warn(ER_IB_MSG_DBLWR_1319, file.m_name.c_str(), (ulint)size, |
| 2134 |
(ulint)REDUCED_BATCH_PAGE_SIZE); |
| 2135 |
} |
| 2136 |
|
| 2137 |
const uint32_t n_pages = size / REDUCED_BATCH_PAGE_SIZE; |
| 2138 |
Buffer buffer(n_pages, REDUCED_BATCH_PAGE_SIZE); |
| 2139 |
IORequest read_request(IORequest::READ); |
| 2140 |
|
| 2141 |
read_request.disable_compression(); |
| 2142 |
|
| 2143 |
auto err = os_file_read(read_request, file.m_name.c_str(), file.m_pfs, |
| 2144 |
buffer.begin(), 0, buffer.capacity()); |
| 2145 |
|
| 2146 |
if (err != DB_SUCCESS) { |
| 2147 |
ib::error(ER_IB_MSG_DBLWR_1301, ut_strerr(err)); |
| 2148 |
|
| 2149 |
return err; |
| 2150 |
} |
| 2151 |
|
| 2152 |
auto page_entry_processor = [&](Page_entry &pe) { pages->add_entry(pe); }; |
| 2153 |
|
| 2154 |
Reduced_batch_deserializer rbd(&buffer, n_pages); |
| 2155 |
err = rbd.deserialize(page_entry_processor); |
| 2156 |
|
| 2157 |
return (err); |
| 2158 |
} |
| 2159 |
|
| 2160 |
uint16_t Double_write::write_dblwr_pages(buf_flush_t flush_type) noexcept { |
| 2161 |
ut_ad(mutex_own(&m_mutex)); |
| 2162 |
ut_a(!m_buffer.empty()); |
| 2163 |
|
| 2164 |
Batch_segment *batch_segment{}; |
| 2165 |
|
| 2166 |
auto segments = flush_type == BUF_FLUSH_LRU ? s_LRU_batch_segments |
| 2167 |
: s_flush_list_batch_segments; |
| 2168 |
|
| 2169 |
while (!segments->dequeue(batch_segment)) { |
| 2170 |
std::this_thread::yield(); |
| 2171 |
} |
| 2172 |
|
| 2173 |
batch_segment->start(this); |
| 2174 |
|
| 2175 |
batch_segment->write(m_buffer); |
| 2176 |
|
| 2177 |
m_bytes_written += m_buffer.size(); |
| 2178 |
|
| 2179 |
m_buffer.clear(); |
| 2180 |
|
| 2181 |
#ifndef _WIN32 |
| 2182 |
if (is_fsync_required()) { |
| 2183 |
batch_segment->flush(); |
| 2184 |
} |
| 2185 |
#endif /* !_WIN32 */ |
| 2186 |
|
| 2187 |
batch_segment->set_batch_size(m_buf_pages.size()); |
| 2188 |
|
| 2189 |
return batch_segment->id(); |
| 2190 |
} |
| 2191 |
|
| 2192 |
void Double_write::write_data_pages(buf_flush_t flush_type, |
| 2193 |
uint16_t batch_id) noexcept { |
| 2194 |
ut_ad(mutex_own(&m_mutex)); |
| 2195 |
|
| 2196 |
for (uint32_t i = 0; i < m_buf_pages.size(); ++i) { |
| 2197 |
const auto bpage = std::get<0>(m_buf_pages.m_pages[i]); |
| 2198 |
|
| 2199 |
ut_d(auto page_id = bpage->id); |
| 2200 |
|
| 2201 |
bpage->set_dblwr_batch_id(batch_id); |
| 2202 |
|
| 2203 |
ut_d(bpage->take_io_responsibility()); |
| 2204 |
auto err = |
| 2205 |
write_to_datafile(bpage, false, std::get<1>(m_buf_pages.m_pages[i])); |
| 2206 |
|
| 2207 |
if (err == DB_PAGE_IS_STALE || err == DB_TABLESPACE_DELETED) { |
| 2208 |
/* For async operation, if space is deleted, fil_io already |
| 2209 |
does buf_page_io_complete and returns DB_TABLESPACE_DELETED. |
| 2210 |
buf_page_free_stale_during_write() asserts if not IO fixed |
| 2211 |
and does similar things as buf_page_io_complete(). This is a |
| 2212 |
temp fix to address this situation. Ideally we should handle |
| 2213 |
these errors in single place possibly by one function. */ |
| 2214 |
if (bpage->was_io_fixed()) { |
| 2215 |
write_complete(bpage, flush_type); |
| 2216 |
buf_page_free_stale_during_write( |
| 2217 |
bpage, buf_page_get_state(bpage) == BUF_BLOCK_FILE_PAGE); |
| 2218 |
} |
| 2219 |
|
| 2220 |
const file::Block *block = std::get<1>(m_buf_pages.m_pages[i]); |
| 2221 |
if (block != nullptr) { |
| 2222 |
os_free_block(const_cast<file::Block *>(block)); |
| 2223 |
} |
| 2224 |
} else { |
| 2225 |
ut_a(err == DB_SUCCESS); |
| 2226 |
} |
| 2227 |
/* We don't hold io_responsibility here no matter which path through ifs and |
| 2228 |
elses we've got here, but we can't assert: |
| 2229 |
ut_ad(!bpage->current_thread_has_io_responsibility()); |
| 2230 |
because bpage could be freed by the time we got here. */ |
| 2231 |
|
| 2232 |
#ifdef UNIV_DEBUG |
| 2233 |
if (dblwr::Force_crash == page_id) { |
| 2234 |
DBUG_SUICIDE(); |
| 2235 |
} |
| 2236 |
#endif /* UNIV_DEBUG */ |
| 2237 |
} |
| 2238 |
|
| 2239 |
srv_stats.dblwr_writes.inc(); |
| 2240 |
|
| 2241 |
m_buf_pages.clear(); |
| 2242 |
|
| 2243 |
os_aio_simulated_wake_handler_threads(); |
| 2244 |
} |
| 2245 |
|
| 2246 |
void Double_write::write_pages(buf_flush_t flush_type) noexcept { |
| 2247 |
ut_ad(mutex_own(&m_mutex)); |
| 2248 |
ut_a(!m_buffer.empty()); |
| 2249 |
|
| 2250 |
const uint16_t batch_id = write_dblwr_pages(flush_type); |
| 2251 |
write_data_pages(flush_type, batch_id); |
| 2252 |
} |
| 2253 |
|
| 2254 |
dberr_t Double_write::create_batch_segments( |
| 2255 |
uint32_t segments_per_file) noexcept { |
| 2256 |
const uint32_t n_segments = segments_per_file * s_files.size(); |
| 2257 |
|
| 2258 |
/* Maximum size of the queue needs to be a power of 2. */ |
| 2259 |
const auto max_queue_size = |
| 2260 |
std::max(ulint{2}, ut_2_power_up((n_segments + 1))); |
| 2261 |
|
| 2262 |
ut_a(s_LRU_batch_segments == nullptr); |
| 2263 |
|
| 2264 |
s_LRU_batch_segments = |
| 2265 |
ut::new_withkey<Batch_segments>(UT_NEW_THIS_FILE_PSI_KEY, max_queue_size); |
| 2266 |
|
| 2267 |
if (s_LRU_batch_segments == nullptr) { |
| 2268 |
return DB_OUT_OF_MEMORY; |
| 2269 |
} |
| 2270 |
|
| 2271 |
ut_a(s_flush_list_batch_segments == nullptr); |
| 2272 |
|
| 2273 |
s_flush_list_batch_segments = |
| 2274 |
ut::new_withkey<Batch_segments>(UT_NEW_THIS_FILE_PSI_KEY, max_queue_size); |
| 2275 |
|
| 2276 |
if (s_flush_list_batch_segments == nullptr) { |
| 2277 |
return DB_OUT_OF_MEMORY; |
| 2278 |
} |
| 2279 |
|
| 2280 |
const uint32_t total_pages = segments_per_file * dblwr::n_pages; |
| 2281 |
|
| 2282 |
uint16_t id{}; |
| 2283 |
|
| 2284 |
for (auto &file : s_files) { |
| 2285 |
for (uint32_t i = 0; i < total_pages; i += dblwr::n_pages, ++id) { |
| 2286 |
auto s = ut::new_withkey<Batch_segment>(UT_NEW_THIS_FILE_PSI_KEY, id, |
| 2287 |
file, i, dblwr::n_pages); |
| 2288 |
|
| 2289 |
if (s == nullptr) { |
| 2290 |
return DB_OUT_OF_MEMORY; |
| 2291 |
} |
| 2292 |
|
| 2293 |
Batch_segments *segments{}; |
| 2294 |
|
| 2295 |
if (s_files.size() > 1) { |
| 2296 |
segments = file.is_for_lru() ? s_LRU_batch_segments |
| 2297 |
: s_flush_list_batch_segments; |
| 2298 |
} else { |
| 2299 |
segments = |
| 2300 |
is_odd(id) ? s_LRU_batch_segments : s_flush_list_batch_segments; |
| 2301 |
} |
| 2302 |
|
| 2303 |
auto success = segments->enqueue(s); |
| 2304 |
ut_a(success); |
| 2305 |
s_segments.push_back(s); |
| 2306 |
s_regular_last_batch_id = id; |
| 2307 |
} |
| 2308 |
} |
| 2309 |
|
| 2310 |
return DB_SUCCESS; |
| 2311 |
} |
| 2312 |
|
| 2313 |
dberr_t Double_write::create_reduced_batch_segments() noexcept { |
| 2314 |
ut_ad(Double_write::s_n_instances >= 4); |
| 2315 |
|
| 2316 |
/* Maximum size of the queue needs to be a power of 2. */ |
| 2317 |
const auto max_queue_size = |
| 2318 |
std::max(ulint{2}, ut_2_power_up(Double_write::s_n_instances / 2)); |
| 2319 |
|
| 2320 |
ut_a(s_r_LRU_batch_segments == nullptr); |
| 2321 |
|
| 2322 |
s_r_LRU_batch_segments = |
| 2323 |
ut::new_withkey<Batch_segments>(UT_NEW_THIS_FILE_PSI_KEY, max_queue_size); |
| 2324 |
|
| 2325 |
if (s_r_LRU_batch_segments == nullptr) { |
| 2326 |
return DB_OUT_OF_MEMORY; |
| 2327 |
} |
| 2328 |
|
| 2329 |
ut_a(s_r_flush_list_batch_segments == nullptr); |
| 2330 |
|
| 2331 |
s_r_flush_list_batch_segments = |
| 2332 |
ut::new_withkey<Batch_segments>(UT_NEW_THIS_FILE_PSI_KEY, max_queue_size); |
| 2333 |
|
| 2334 |
if (s_r_flush_list_batch_segments == nullptr) { |
| 2335 |
return DB_OUT_OF_MEMORY; |
| 2336 |
} |
| 2337 |
|
| 2338 |
const uint32_t total_pages = Double_write::s_n_instances; |
| 2339 |
const uint32_t pages_per_segment = 1; |
| 2340 |
|
| 2341 |
/* Batch_ids for new segments should start after old batch ids. */ |
| 2342 |
uint16_t id = Double_write::s_segments.size(); |
| 2343 |
|
| 2344 |
/* The number of pages in the reduced dblwr file is equal to the number of |
| 2345 |
instances. Use half of it for LRU batch segments and the rest for flush list |
| 2346 |
batch segments. */ |
| 2347 |
const auto lru_segs = Double_write::s_n_instances / 2; |
| 2348 |
|
| 2349 |
/* Reduced Batch file */ |
| 2350 |
auto &file = Double_write::s_r_files[0]; |
| 2351 |
for (uint32_t i = 0; i < total_pages; ++i, ++id) { |
| 2352 |
auto s = ut::new_withkey<Batch_segment>(UT_NEW_THIS_FILE_PSI_KEY, id, file, |
| 2353 |
REDUCED_BATCH_PAGE_SIZE, i, |
| 2354 |
pages_per_segment); |
| 2355 |
|
| 2356 |
if (s == nullptr) { |
| 2357 |
return DB_OUT_OF_MEMORY; |
| 2358 |
} |
| 2359 |
|
| 2360 |
Batch_segments *segments = |
| 2361 |
(i < lru_segs) ? s_r_LRU_batch_segments : s_r_flush_list_batch_segments; |
| 2362 |
auto success = segments->enqueue(s); |
| 2363 |
ut_a(success); |
| 2364 |
s_segments.push_back(s); |
| 2365 |
} |
| 2366 |
return DB_SUCCESS; |
| 2367 |
} |
| 2368 |
|
| 2369 |
dberr_t Double_write::create_single_segments() noexcept { |
| 2370 |
ut_a(s_single_segments == nullptr); |
| 2371 |
|
| 2372 |
/* This needs to be a power of 2. */ |
| 2373 |
const auto max_queue_size = |
| 2374 |
std::max(ulint{2}, ut_2_power_up(SYNC_PAGE_FLUSH_SLOTS)); |
| 2375 |
|
| 2376 |
s_single_segments = |
| 2377 |
ut::new_withkey<Segments>(UT_NEW_THIS_FILE_PSI_KEY, max_queue_size); |
| 2378 |
|
| 2379 |
if (s_single_segments == nullptr) { |
| 2380 |
return DB_OUT_OF_MEMORY; |
| 2381 |
} |
| 2382 |
|
| 2383 |
uint32_t n_pages{}; |
| 2384 |
|
| 2385 |
if (s_files.size() == 1) { |
| 2386 |
n_pages = SYNC_PAGE_FLUSH_SLOTS; |
| 2387 |
} else { |
| 2388 |
n_pages = SYNC_PAGE_FLUSH_SLOTS / (s_files.size() / 2); |
| 2389 |
} |
| 2390 |
|
| 2391 |
for (auto &file : s_files) { |
| 2392 |
if (!file.is_for_lru() && s_files.size() > 1) { |
| 2393 |
/* Skip the flush list files. */ |
| 2394 |
continue; |
| 2395 |
} |
| 2396 |
const auto start = dblwr::File::s_n_pages; |
| 2397 |
|
| 2398 |
for (uint32_t i = start; i < start + n_pages; ++i) { |
| 2399 |
auto s = ut::new_withkey<Segment>(UT_NEW_THIS_FILE_PSI_KEY, file, i, 1UL); |
| 2400 |
|
| 2401 |
if (s == nullptr) { |
| 2402 |
return DB_OUT_OF_MEMORY; |
| 2403 |
} |
| 2404 |
|
| 2405 |
auto success = s_single_segments->enqueue(s); |
| 2406 |
ut_a(success); |
| 2407 |
} |
| 2408 |
} |
| 2409 |
|
| 2410 |
return DB_SUCCESS; |
| 2411 |
} |
| 2412 |
|
| 2413 |
file::Block *dblwr::get_encrypted_frame(buf_page_t *bpage) noexcept { |
| 2414 |
space_id_t space_id = bpage->space(); |
| 2415 |
page_no_t page_no = bpage->page_no(); |
| 2416 |
|
| 2417 |
if (page_no == 0) { |
| 2418 |
/* The first page of any tablespace is never encrypted. |
| 2419 |
So return early. */ |
| 2420 |
return nullptr; |
| 2421 |
} |
| 2422 |
|
| 2423 |
if (fsp_is_undo_tablespace(space_id) && !srv_undo_log_encrypt) { |
| 2424 |
/* It is an undo tablespace and undo encryption is not enabled. */ |
| 2425 |
return nullptr; |
| 2426 |
} |
| 2427 |
|
| 2428 |
fil_space_t *space = bpage->get_space(); |
| 2429 |
if (space->encryption_op_in_progress == Encryption::Progress::DECRYPTION || |
| 2430 |
!space->is_encrypted()) { |
| 2431 |
return nullptr; |
| 2432 |
} |
| 2433 |
|
| 2434 |
if (!space->can_encrypt()) { |
| 2435 |
/* Encryption key information is not available. */ |
| 2436 |
return nullptr; |
| 2437 |
} |
| 2438 |
|
| 2439 |
IORequest type(IORequest::WRITE); |
| 2440 |
void *frame{}; |
| 2441 |
uint32_t len{}; |
| 2442 |
|
| 2443 |
fil_node_t *node = space->get_file_node(&page_no); |
| 2444 |
type.block_size(node->block_size); |
| 2445 |
|
| 2446 |
Double_write::prepare(bpage, &frame, &len); |
| 2447 |
|
| 2448 |
ulint n = len; |
| 2449 |
|
| 2450 |
file::Block *compressed_block{}; |
| 2451 |
|
| 2452 |
/* Transparent page compression (TPC) is disabled if punch hole is not |
| 2453 |
supported. A similar check is done in Fil_shard::do_io(). */ |
| 2454 |
const bool do_compression = |
| 2455 |
space->is_compressed() && !bpage->size.is_compressed() && |
| 2456 |
IORequest::is_punch_hole_supported() && node->punch_hole; |
| 2457 |
|
| 2458 |
if (do_compression) { |
| 2459 |
/* @note Compression needs to be done before encryption. */ |
| 2460 |
|
| 2461 |
/* The page size must be a multiple of the OS punch hole size. */ |
| 2462 |
ut_ad(n % type.block_size() == 0); |
| 2463 |
|
| 2464 |
type.compression_algorithm(space->compression_type); |
| 2465 |
compressed_block = os_file_compress_page(type, frame, &n); |
| 2466 |
} |
| 2467 |
|
| 2468 |
type.get_encryption_info().set(space->m_encryption_metadata); |
| 2469 |
auto e_block = os_file_encrypt_page(type, frame, n); |
| 2470 |
|
| 2471 |
if (compressed_block != nullptr) { |
| 2472 |
file::Block::free(compressed_block); |
| 2473 |
} |
| 2474 |
|
| 2475 |
return e_block; |
| 2476 |
} |
| 2477 |
|
| 2478 |
dberr_t dblwr::write(buf_flush_t flush_type, buf_page_t *bpage, |
| 2479 |
bool sync) noexcept { |
| 2480 |
dberr_t err; |
| 2481 |
const space_id_t space_id = bpage->id.space(); |
| 2482 |
|
| 2483 |
ut_ad(bpage->current_thread_has_io_responsibility()); |
| 2484 |
/* This is not required for correctness, but it aborts the processing early. |
| 2485 |
*/ |
| 2486 |
if (bpage->was_stale()) { |
| 2487 |
/* Disable batch completion in write_complete(). */ |
| 2488 |
bpage->set_dblwr_batch_id(std::numeric_limits<uint16_t>::max()); |
| 2489 |
buf_page_free_stale_during_write( |
| 2490 |
bpage, buf_page_get_state(bpage) == BUF_BLOCK_FILE_PAGE); |
| 2491 |
/* We don't hold io_responsibility here no matter which path through ifs and |
| 2492 |
elses we've got here, but we can't assert: |
| 2493 |
ut_ad(!bpage->current_thread_has_io_responsibility()); |
| 2494 |
because bpage could be freed by the time we got here. */ |
| 2495 |
return DB_SUCCESS; |
| 2496 |
} |
| 2497 |
|
| 2498 |
if (srv_read_only_mode || fsp_is_system_temporary(space_id) || |
| 2499 |
!dblwr::is_enabled() || Double_write::s_instances == nullptr || |
| 2500 |
mtr_t::s_logging.dblwr_disabled()) { |
| 2501 |
/* Skip the double-write buffer since it is not needed. Temporary |
| 2502 |
tablespaces are never recovered, therefore we don't care about |
| 2503 |
torn writes. */ |
| 2504 |
bpage->set_dblwr_batch_id(std::numeric_limits<uint16_t>::max()); |
| 2505 |
err = Double_write::write_to_datafile(bpage, sync, nullptr); |
| 2506 |
if (err == DB_PAGE_IS_STALE || err == DB_TABLESPACE_DELETED) { |
| 2507 |
if (bpage->was_io_fixed()) { |
| 2508 |
buf_page_free_stale_during_write( |
| 2509 |
bpage, buf_page_get_state(bpage) == BUF_BLOCK_FILE_PAGE); |
| 2510 |
} |
| 2511 |
err = DB_SUCCESS; |
| 2512 |
} else if (sync) { |
| 2513 |
ut_ad(flush_type == BUF_FLUSH_LRU || flush_type == BUF_FLUSH_SINGLE_PAGE); |
| 2514 |
|
| 2515 |
if (err == DB_SUCCESS) { |
| 2516 |
fil_flush(space_id); |
| 2517 |
} |
| 2518 |
/* true means we want to evict this page from the LRU list as well. */ |
| 2519 |
buf_page_io_complete(bpage, true); |
| 2520 |
} |
| 2521 |
|
| 2522 |
} else { |
| 2523 |
ut_d(auto page_id = bpage->id); |
| 2524 |
|
| 2525 |
/* Encrypt the page here, so that the same encrypted contents are written |
| 2526 |
to the dblwr file and the data file. */ |
| 2527 |
file::Block *e_block = dblwr::get_encrypted_frame(bpage); |
| 2528 |
|
| 2529 |
if (!sync && flush_type != BUF_FLUSH_SINGLE_PAGE) { |
| 2530 |
MONITOR_INC(MONITOR_DBLWR_ASYNC_REQUESTS); |
| 2531 |
|
| 2532 |
ut_d(bpage->release_io_responsibility()); |
| 2533 |
Double_write::submit(flush_type, bpage, e_block); |
| 2534 |
err = DB_SUCCESS; |
| 2535 |
#ifdef UNIV_DEBUG |
| 2536 |
if (dblwr::Force_crash == page_id) { |
| 2537 |
force_flush(flush_type, buf_pool_index(buf_pool_from_bpage(bpage))); |
| 2538 |
} |
| 2539 |
#endif /* UNIV_DEBUG */ |
| 2540 |
} else { |
| 2541 |
MONITOR_INC(MONITOR_DBLWR_SYNC_REQUESTS); |
| 2542 |
/* Disable batch completion in write_complete(). */ |
| 2543 |
bpage->set_dblwr_batch_id(std::numeric_limits<uint16_t>::max()); |
| 2544 |
err = Double_write::sync_page_flush(bpage, e_block); |
| 2545 |
} |
| 2546 |
} |
| 2547 |
/* We don't hold io_responsibility here no matter which path through ifs and |
| 2548 |
elses we've got here, but we can't assert: |
| 2549 |
ut_ad(!bpage->current_thread_has_io_responsibility()); |
| 2550 |
because bpage could be freed by the time we got here. */ |
| 2551 |
return err; |
| 2552 |
} |
| 2553 |
|
| 2554 |
bool Double_write::is_reduced_batch_id(uint32_t batch_id) { |
| 2555 |
ut_ad(s_regular_last_batch_id != 0); |
| 2556 |
return (batch_id > s_regular_last_batch_id); |
| 2557 |
} |
| 2558 |
|
| 2559 |
void Double_write::write_complete(buf_page_t *bpage, |
| 2560 |
buf_flush_t flush_type) noexcept { |
| 2561 |
if (s_instances == nullptr) { |
| 2562 |
/* Not initialized yet. */ |
| 2563 |
return; |
| 2564 |
} |
| 2565 |
|
| 2566 |
const auto batch_id = bpage->get_dblwr_batch_id(); |
| 2567 |
|
| 2568 |
switch (flush_type) { |
| 2569 |
case BUF_FLUSH_LRU: |
| 2570 |
case BUF_FLUSH_LIST: |
| 2571 |
case BUF_FLUSH_SINGLE_PAGE: |
| 2572 |
if (batch_id != std::numeric_limits<uint16_t>::max()) { |
| 2573 |
ut_ad(batch_id < s_segments.size()); |
| 2574 |
auto batch_segment = s_segments[batch_id]; |
| 2575 |
|
| 2576 |
if (batch_segment->write_complete()) { |
| 2577 |
batch_segment->completed(); |
| 2578 |
|
| 2579 |
srv_stats.dblwr_pages_written.add(batch_segment->batch_size()); |
| 2580 |
|
| 2581 |
batch_segment->reset(); |
| 2582 |
|
| 2583 |
Batch_segments *segments{nullptr}; |
| 2584 |
|
| 2585 |
if (is_reduced_batch_id(batch_id)) { |
| 2586 |
segments = (flush_type == BUF_FLUSH_LRU) |
| 2587 |
? Double_write::s_r_LRU_batch_segments |
| 2588 |
: Double_write::s_r_flush_list_batch_segments; |
| 2589 |
} else { |
| 2590 |
segments = (flush_type == BUF_FLUSH_LRU) |
| 2591 |
? Double_write::s_LRU_batch_segments |
| 2592 |
: Double_write::s_flush_list_batch_segments; |
| 2593 |
} |
| 2594 |
|
| 2595 |
fil_flush_file_spaces(); |
| 2596 |
|
| 2597 |
while (!segments->enqueue(batch_segment)) { |
| 2598 |
std::this_thread::yield(); |
| 2599 |
} |
| 2600 |
} |
| 2601 |
} |
| 2602 |
bpage->set_dblwr_batch_id(std::numeric_limits<uint16_t>::max()); |
| 2603 |
break; |
| 2604 |
|
| 2605 |
case BUF_FLUSH_N_TYPES: |
| 2606 |
ut_error; |
| 2607 |
} |
| 2608 |
} |
| 2609 |
|
| 2610 |
void dblwr::write_complete(buf_page_t *bpage, buf_flush_t flush_type) noexcept { |
| 2611 |
Double_write::write_complete(bpage, flush_type); |
| 2612 |
} |
| 2613 |
|
| 2614 |
void dblwr::recv::recover(recv::Pages *pages, fil_space_t *space) noexcept { |
| 2615 |
#ifndef UNIV_HOTBACKUP |
| 2616 |
pages->recover(space); |
| 2617 |
#endif /* UNIV_HOTBACKUP */ |
| 2618 |
} |
| 2619 |
|
| 2620 |
/** Create the file and or open it if it exists. |
| 2621 |
@param[in] dir_name Directory where to create the file. |
| 2622 |
@param[in] id Instance ID. |
| 2623 |
@param[out] file File handle. |
| 2624 |
@param[in] file_type The file type. |
| 2625 |
@param[in] extension .dblwr/.bdblwr |
| 2626 |
@return DB_SUCCESS if all went well. */ |
| 2627 |
static dberr_t dblwr_file_open(const std::string &dir_name, int id, |
| 2628 |
dblwr::File &file, ulint file_type, |
| 2629 |
ib_file_suffix extension = DWR) noexcept { |
| 2630 |
bool dir_exists; |
| 2631 |
bool file_exists; |
| 2632 |
os_file_type_t type; |
| 2633 |
std::string dir(dir_name); |
| 2634 |
|
| 2635 |
Fil_path::normalize(dir); |
| 2636 |
|
| 2637 |
os_file_status(dir.c_str(), &dir_exists, &type); |
| 2638 |
|
| 2639 |
switch (type) { |
| 2640 |
case OS_FILE_TYPE_DIR: |
| 2641 |
/* This is an existing directory. */ |
| 2642 |
break; |
| 2643 |
case OS_FILE_TYPE_MISSING: |
| 2644 |
/* This path is missing but otherwise usable. It will be created. */ |
| 2645 |
ut_ad(!dir_exists); |
| 2646 |
break; |
| 2647 |
case OS_FILE_TYPE_LINK: |
| 2648 |
case OS_FILE_TYPE_FILE: |
| 2649 |
case OS_FILE_TYPE_BLOCK: |
| 2650 |
case OS_FILE_TYPE_UNKNOWN: |
| 2651 |
case OS_FILE_TYPE_FAILED: |
| 2652 |
case OS_FILE_PERMISSION_ERROR: |
| 2653 |
case OS_FILE_TYPE_NAME_TOO_LONG: |
| 2654 |
|
| 2655 |
ib::error(ER_IB_MSG_DBLWR_1290, dir_name.c_str()); |
| 2656 |
|
| 2657 |
return DB_WRONG_FILE_NAME; |
| 2658 |
} |
| 2659 |
|
| 2660 |
file.m_id = id; |
| 2661 |
|
| 2662 |
file.m_name = std::string(dir_name) + OS_PATH_SEPARATOR + "#ib_"; |
| 2663 |
|
| 2664 |
file.m_name += std::to_string(srv_page_size) + "_" + std::to_string(id); |
| 2665 |
|
| 2666 |
file.m_name += dot_ext[extension]; |
| 2667 |
|
| 2668 |
uint32_t mode; |
| 2669 |
if (dir_exists) { |
| 2670 |
os_file_status(file.m_name.c_str(), &file_exists, &type); |
| 2671 |
|
| 2672 |
if (type == OS_FILE_TYPE_FILE) { |
| 2673 |
mode = OS_FILE_OPEN_RETRY; |
| 2674 |
} else if (type == OS_FILE_TYPE_MISSING) { |
| 2675 |
mode = OS_FILE_CREATE; |
| 2676 |
} else { |
| 2677 |
ib::error(ER_IB_MSG_BAD_DBLWR_FILE_NAME, file.m_name.c_str()); |
| 2678 |
|
| 2679 |
return DB_CANNOT_OPEN_FILE; |
| 2680 |
} |
| 2681 |
} else { |
| 2682 |
auto err = os_file_create_subdirs_if_needed(file.m_name.c_str()); |
| 2683 |
if (err != DB_SUCCESS) { |
| 2684 |
return err; |
| 2685 |
} |
| 2686 |
|
| 2687 |
mode = OS_FILE_CREATE; |
| 2688 |
} |
| 2689 |
|
| 2690 |
if (mode == OS_FILE_CREATE && id >= (int)Double_write::s_n_instances) { |
| 2691 |
/* Don't create files if not configured by the user. */ |
| 2692 |
return DB_NOT_FOUND; |
| 2693 |
} |
| 2694 |
|
| 2695 |
bool success; |
| 2696 |
file.m_pfs = os_file_create(innodb_dblwr_file_key, file.m_name.c_str(), mode, |
| 2697 |
file_type, srv_read_only_mode, &success); |
| 2698 |
|
| 2699 |
if (!success) { |
| 2700 |
ib::error(ER_IB_MSG_DBLWR_1293, file.m_name.c_str()); |
| 2701 |
return DB_IO_ERROR; |
| 2702 |
} else { |
| 2703 |
ib::info(ER_IB_MSG_DBLWR_1286, file.m_name.c_str()); |
| 2704 |
} |
| 2705 |
|
| 2706 |
return DB_SUCCESS; |
| 2707 |
} |
| 2708 |
|
| 2709 |
dberr_t dblwr::reduced_open() noexcept { |
| 2710 |
ut_a(Double_write::s_r_files.empty()); |
| 2711 |
|
| 2712 |
/* The number of files for reduced dblwr is always one. */ |
| 2713 |
Double_write::s_r_files.resize(1); |
| 2714 |
|
| 2715 |
dberr_t err{DB_SUCCESS}; |
| 2716 |
|
| 2717 |
/* Create the batch file */ |
| 2718 |
auto &file = Double_write::s_r_files[0]; |
| 2719 |
|
| 2720 |
uint32_t pages_per_file = Double_write::s_n_instances; |
| 2721 |
ib_file_suffix extension{BWR}; |
| 2722 |
|
| 2723 |
err = dblwr_file_open(dblwr::dir, 0, file, OS_DBLWR_FILE, extension); |
| 2724 |
|
| 2725 |
if (err != DB_SUCCESS) { |
| 2726 |
return (err); |
| 2727 |
} |
| 2728 |
const uint32_t phy_size = REDUCED_BATCH_PAGE_SIZE; |
| 2729 |
|
| 2730 |
err = Double_write::init_file(file, pages_per_file, phy_size); |
| 2731 |
|
| 2732 |
if (err != DB_SUCCESS) { |
| 2733 |
return (err); |
| 2734 |
} |
| 2735 |
|
| 2736 |
auto file_size = os_file_get_size(file.m_pfs); |
| 2737 |
|
| 2738 |
if (file_size == 0 || (file_size % phy_size)) { |
| 2739 |
ib::warn(ER_IB_MSG_DBLWR_1322, file.m_name.c_str(), (ulint)file_size, |
| 2740 |
(ulint)phy_size); |
| 2741 |
} |
| 2742 |
|
| 2743 |
Double_write::reduced_reset_file(file, pages_per_file, phy_size); |
| 2744 |
|
| 2745 |
return (DB_SUCCESS); |
| 2746 |
} |
| 2747 |
|
| 2748 |
dberr_t dblwr::open() noexcept { |
| 2749 |
ut_a(!dblwr::dir.empty()); |
| 2750 |
ut_a(Double_write::s_files.empty()); |
| 2751 |
ut_a(Double_write::s_n_instances == 0); |
| 2752 |
|
| 2753 |
/* Separate instances for LRU and FLUSH list write requests. */ |
| 2754 |
Double_write::s_n_instances = std::max(4UL, srv_buf_pool_instances * 2); |
| 2755 |
|
| 2756 |
/* Batch segments per dblwr file. */ |
| 2757 |
uint32_t segments_per_file{}; |
| 2758 |
|
| 2759 |
if (dblwr::n_files == 0) { |
| 2760 |
dblwr::n_files = 2; |
| 2761 |
} |
| 2762 |
|
| 2763 |
ib::info(ER_IB_MSG_DBLWR_1324) |
| 2764 |
<< "Double write buffer files: " << dblwr::n_files; |
| 2765 |
|
| 2766 |
if (dblwr::n_pages == 0) { |
| 2767 |
dblwr::n_pages = srv_n_write_io_threads; |
| 2768 |
} |
| 2769 |
|
| 2770 |
ib::info(ER_IB_MSG_DBLWR_1323) |
| 2771 |
<< "Double write buffer pages per instance: " << dblwr::n_pages; |
| 2772 |
|
| 2773 |
if (Double_write::s_n_instances < dblwr::n_files) { |
| 2774 |
segments_per_file = 1; |
| 2775 |
Double_write::s_files.resize(Double_write::s_n_instances); |
| 2776 |
} else { |
| 2777 |
Double_write::s_files.resize(dblwr::n_files); |
| 2778 |
segments_per_file = (Double_write::s_n_instances / dblwr::n_files) + 1; |
| 2779 |
} |
| 2780 |
|
| 2781 |
dberr_t err{DB_SUCCESS}; |
| 2782 |
|
| 2783 |
ut_ad(dblwr::File::s_n_pages == 0); |
| 2784 |
dblwr::File::s_n_pages = dblwr::n_pages * segments_per_file; |
| 2785 |
|
| 2786 |
const auto first = &Double_write::s_files[0]; |
| 2787 |
|
| 2788 |
/* Create the files (if required) and make them the right size. */ |
| 2789 |
for (auto &file : Double_write::s_files) { |
| 2790 |
err = dblwr_file_open(dblwr::dir, &file - first, file, OS_DBLWR_FILE); |
| 2791 |
|
| 2792 |
if (err != DB_SUCCESS) { |
| 2793 |
break; |
| 2794 |
} |
| 2795 |
|
| 2796 |
auto pages_per_file = dblwr::n_pages * segments_per_file; |
| 2797 |
|
| 2798 |
if (Double_write::s_files.size() == 1) { |
| 2799 |
pages_per_file += SYNC_PAGE_FLUSH_SLOTS; |
| 2800 |
} else if ((file.m_id & 1)) { |
| 2801 |
pages_per_file += |
| 2802 |
SYNC_PAGE_FLUSH_SLOTS / (Double_write::s_files.size() / 2); |
| 2803 |
} |
| 2804 |
|
| 2805 |
err = Double_write::init_file(file, pages_per_file); |
| 2806 |
|
| 2807 |
if (err != DB_SUCCESS) { |
| 2808 |
break; |
| 2809 |
} |
| 2810 |
|
| 2811 |
auto file_size = os_file_get_size(file.m_pfs); |
| 2812 |
|
| 2813 |
if (file_size == 0 || (file_size % univ_page_size.physical())) { |
| 2814 |
ib::warn(ER_IB_MSG_DBLWR_1322, file.m_name.c_str(), (ulint)file_size, |
| 2815 |
(ulint)univ_page_size.physical()); |
| 2816 |
} |
| 2817 |
|
| 2818 |
/* Truncate the size after recovery: false. */ |
| 2819 |
Double_write::reset_file(file, false); |
| 2820 |
} |
| 2821 |
|
| 2822 |
/* Create the segments that for LRU and FLUSH list batches writes */ |
| 2823 |
if (err == DB_SUCCESS) { |
| 2824 |
err = Double_write::create_batch_segments(segments_per_file); |
| 2825 |
} |
| 2826 |
|
| 2827 |
/* Create the segments for the single page flushes. */ |
| 2828 |
if (err == DB_SUCCESS) { |
| 2829 |
err = Double_write::create_single_segments(); |
| 2830 |
} |
| 2831 |
|
| 2832 |
if (err == DB_SUCCESS) { |
| 2833 |
err = Double_write::create_v2(); |
| 2834 |
} else { |
| 2835 |
Double_write::shutdown(); |
| 2836 |
} |
| 2837 |
|
| 2838 |
if (err != DB_SUCCESS) { |
| 2839 |
return (err); |
| 2840 |
} |
| 2841 |
|
| 2842 |
if (!dblwr::is_reduced()) { |
| 2843 |
return (DB_SUCCESS); |
| 2844 |
} |
| 2845 |
|
| 2846 |
if (err == DB_SUCCESS) { |
| 2847 |
err = dblwr::enable_reduced(); |
| 2848 |
} |
| 2849 |
|
| 2850 |
return err; |
| 2851 |
} |
| 2852 |
|
| 2853 |
dberr_t dblwr::enable_reduced() noexcept { |
| 2854 |
if (is_reduced_inited) { |
| 2855 |
return (DB_SUCCESS); |
| 2856 |
} |
| 2857 |
|
| 2858 |
dberr_t err = dblwr::reduced_open(); |
| 2859 |
|
| 2860 |
/* Create the segments that for LRU and FLUSH list batches writes */ |
| 2861 |
if (err == DB_SUCCESS) { |
| 2862 |
err = Double_write::create_reduced_batch_segments(); |
| 2863 |
} |
| 2864 |
|
| 2865 |
if (err == DB_SUCCESS) { |
| 2866 |
err = Double_write::create_reduced(); |
| 2867 |
} |
| 2868 |
|
| 2869 |
if (err == DB_SUCCESS) { |
| 2870 |
is_reduced_inited = true; |
| 2871 |
} |
| 2872 |
|
| 2873 |
return (err); |
| 2874 |
} |
| 2875 |
|
| 2876 |
void dblwr::close() noexcept { Double_write::shutdown(); } |
| 2877 |
|
| 2878 |
void dblwr::set() { |
| 2879 |
#ifndef UNIV_HOTBACKUP |
| 2880 |
Double_write::toggle(dblwr::g_mode); |
| 2881 |
#endif /* !UNIV_HOTBACKUP */ |
| 2882 |
} |
| 2883 |
|
| 2884 |
void dblwr::reset_files() noexcept { Double_write::reset_files(); } |
| 2885 |
|
| 2886 |
dberr_t dblwr::v1::init() noexcept { |
| 2887 |
if (!Double_write::init_v1(LEGACY_PAGE1, LEGACY_PAGE2)) { |
| 2888 |
return DB_V1_DBLWR_INIT_FAILED; |
| 2889 |
} |
| 2890 |
|
| 2891 |
return DB_SUCCESS; |
| 2892 |
} |
| 2893 |
|
| 2894 |
dberr_t dblwr::v1::create() noexcept { |
| 2895 |
if (!Double_write::create_v1(LEGACY_PAGE1, LEGACY_PAGE2)) { |
| 2896 |
return DB_V1_DBLWR_CREATE_FAILED; |
| 2897 |
} |
| 2898 |
|
| 2899 |
return DB_SUCCESS; |
| 2900 |
} |
| 2901 |
|
| 2902 |
bool dblwr::v1::is_inside(page_no_t page_no) noexcept { |
| 2903 |
if (LEGACY_PAGE1 == 0) { |
| 2904 |
ut_a(LEGACY_PAGE2 == 0); |
| 2905 |
/* We don't want our own reads being checked here during initialisation. */ |
| 2906 |
return false; |
| 2907 |
} |
| 2908 |
if (page_no >= LEGACY_PAGE1 && |
| 2909 |
page_no < LEGACY_PAGE1 + DBLWR_V1_EXTENT_SIZE) { |
| 2910 |
return true; |
| 2911 |
} |
| 2912 |
|
| 2913 |
if (page_no >= LEGACY_PAGE2 && |
| 2914 |
page_no < LEGACY_PAGE2 + DBLWR_V1_EXTENT_SIZE) { |
| 2915 |
return true; |
| 2916 |
} |
| 2917 |
|
| 2918 |
return false; |
| 2919 |
} |
| 2920 |
|
| 2921 |
/** Check if the dblwr page is corrupted. |
| 2922 |
@param[in] page the dblwr page. |
| 2923 |
@param[in] space tablespace to which the page belongs. |
| 2924 |
@param[in] page_no page_no within the actual tablespace. |
| 2925 |
@param[out] err error code to check if decryption or decompression failed. |
| 2926 |
@return true if dblwr page is corrupted, false otherwise. */ |
| 2927 |
static bool is_dblwr_page_corrupted(byte *page, fil_space_t *space, |
| 2928 |
page_no_t page_no, dberr_t *err) noexcept { |
| 2929 |
const page_size_t page_size(space->flags); |
| 2930 |
const bool is_checksum_disabled = fsp_is_checksum_disabled(space->id); |
| 2931 |
bool corrupted = false; |
| 2932 |
|
| 2933 |
BlockReporter dblwr_page(true, page, page_size, is_checksum_disabled); |
| 2934 |
|
| 2935 |
if (dblwr_page.is_encrypted()) { |
| 2936 |
Encryption en; |
| 2937 |
IORequest req_type; |
| 2938 |
size_t z_page_size; |
| 2939 |
|
| 2940 |
en.set(space->m_encryption_metadata); |
| 2941 |
fil_node_t *node = space->get_file_node(&page_no); |
| 2942 |
req_type.block_size(node->block_size); |
| 2943 |
|
| 2944 |
page_type_t page_type = fil_page_get_type(page); |
| 2945 |
ut_ad(fil_is_page_type_valid(page_type)); |
| 2946 |
|
| 2947 |
if (page_type == FIL_PAGE_COMPRESSED_AND_ENCRYPTED) { |
| 2948 |
uint16_t z_len = mach_read_from_2(page + FIL_PAGE_COMPRESS_SIZE_V1); |
| 2949 |
z_page_size = z_len + FIL_PAGE_DATA; |
| 2950 |
|
| 2951 |
/* @note The block size needs to be the same when the page was compressed |
| 2952 |
and encrypted. */ |
| 2953 |
z_page_size = ut_calc_align(z_page_size, req_type.block_size()); |
| 2954 |
} else { |
| 2955 |
z_page_size = page_size.physical(); |
| 2956 |
} |
| 2957 |
|
| 2958 |
*err = en.decrypt(req_type, page, z_page_size, nullptr, 0); |
| 2959 |
if (*err != DB_SUCCESS) { |
| 2960 |
/* Could not decrypt. Consider it corrupted. */ |
| 2961 |
corrupted = true; |
| 2962 |
|
| 2963 |
if (*err == DB_IO_DECRYPT_FAIL) { |
| 2964 |
std::ostringstream out; |
| 2965 |
out << "space_id=" << space->id << ", page_no=" << page_no |
| 2966 |
<< ", page_size=" << z_page_size << ", space_name=" << space->name; |
| 2967 |
ib::warn(ER_IB_DBLWR_DECRYPT_FAILED, out.str().c_str()); |
| 2968 |
|
| 2969 |
if (en.is_none()) { |
| 2970 |
std::ostringstream out; |
| 2971 |
out << "space_id=" << space->id << ", space_name=" << space->name; |
| 2972 |
ib::warn(ER_IB_DBLWR_KEY_MISSING, out.str().c_str()); |
| 2973 |
} |
| 2974 |
} |
| 2975 |
|
| 2976 |
} else { |
| 2977 |
/* Check if the page is compressed. */ |
| 2978 |
page_type_t page_type = fil_page_get_type(page); |
| 2979 |
ut_ad(fil_is_page_type_valid(page_type)); |
| 2980 |
|
| 2981 |
if (page_type == FIL_PAGE_COMPRESSED) { |
| 2982 |
*err = os_file_decompress_page(true, page, nullptr, 0); |
| 2983 |
|
| 2984 |
if (*err != DB_SUCCESS) { |
| 2985 |
/* Could not decompress. Consider it corrupted. */ |
| 2986 |
size_t orig_size = mach_read_from_2(page + FIL_PAGE_ORIGINAL_SIZE_V1); |
| 2987 |
ib::error(ER_IB_DBLWR_DECOMPRESS_FAILED, *err, orig_size); |
| 2988 |
corrupted = true; |
| 2989 |
} |
| 2990 |
} |
| 2991 |
} |
| 2992 |
} |
| 2993 |
|
| 2994 |
if (!corrupted) { |
| 2995 |
BlockReporter check(true, page, page_size, is_checksum_disabled); |
| 2996 |
corrupted = check.is_corrupted(); |
| 2997 |
} |
| 2998 |
|
| 2999 |
return (corrupted); |
| 3000 |
} |
| 3001 |
|
| 3002 |
/** Recover a page from the doublewrite buffer. |
| 3003 |
@param[in] dblwr_page_no Page number if the doublewrite buffer |
| 3004 |
@param[in] space Tablespace the page belongs to |
| 3005 |
@param[in] page_no Page number in the tablespace |
| 3006 |
@param[in] page Data to write to <space, page_no> |
| 3007 |
@return true if page was restored to the tablespace */ |
| 3008 |
bool dblwr::recv::Pages::dblwr_recover_page(page_no_t dblwr_page_no, |
| 3009 |
fil_space_t *space, |
| 3010 |
page_no_t page_no, |
| 3011 |
byte *page) noexcept { |
| 3012 |
/* For cloned database double write pages should be ignored. However, |
| 3013 |
given the control flow, we read the pages in anyway but don't recover |
| 3014 |
from the pages we read in. */ |
| 3015 |
ut_a(!recv_sys->is_cloned_db); |
| 3016 |
|
| 3017 |
Buffer buffer{1}; |
| 3018 |
|
| 3019 |
if (page_no >= space->size) { |
| 3020 |
/* Do not report the warning if the tablespace is going to be truncated. */ |
| 3021 |
if (!undo::is_active(space->id)) { |
| 3022 |
ib::warn(ER_IB_MSG_DBLWR_1313) |
| 3023 |
<< "Page# " << dblwr_page_no |
| 3024 |
<< " stored in the doublewrite file is" |
| 3025 |
" not within data file space bounds " |
| 3026 |
<< space->size << " bytes: page : " << page_id_t(space->id, page_no); |
| 3027 |
} |
| 3028 |
|
| 3029 |
return false; |
| 3030 |
} |
| 3031 |
|
| 3032 |
const page_size_t page_size(space->flags); |
| 3033 |
const page_id_t page_id(space->id, page_no); |
| 3034 |
|
| 3035 |
/* We want to ensure that for partial reads the |
| 3036 |
unread portion of the page is NUL. */ |
| 3037 |
memset(buffer.begin(), 0x0, page_size.physical()); |
| 3038 |
|
| 3039 |
IORequest request; |
| 3040 |
|
| 3041 |
request.dblwr(); |
| 3042 |
|
| 3043 |
/* Read in the page from the data file to compare. */ |
| 3044 |
auto err = fil_io(request, true, page_id, page_size, 0, page_size.physical(), |
| 3045 |
buffer.begin(), nullptr); |
| 3046 |
|
| 3047 |
if (err != DB_SUCCESS) { |
| 3048 |
ib::warn(ER_IB_MSG_DBLWR_1314) |
| 3049 |
<< "Double write file recovery: " << page_id << " read failed with " |
| 3050 |
<< "error: " << ut_strerr(err); |
| 3051 |
} |
| 3052 |
|
| 3053 |
/* Is the page read from the data file corrupt? */ |
| 3054 |
BlockReporter data_file_page(true, buffer.begin(), page_size, |
| 3055 |
fsp_is_checksum_disabled(space->id)); |
| 3056 |
|
| 3057 |
if (data_file_page.is_corrupted()) { |
| 3058 |
ib::info(ER_IB_MSG_DBLWR_1315) << "Database page corruption or" |
| 3059 |
<< " a failed file read of page " << page_id |
| 3060 |
<< ". Trying to recover it from the" |
| 3061 |
<< " doublewrite file."; |
| 3062 |
|
| 3063 |
dberr_t dblwr_err; |
| 3064 |
|
| 3065 |
const bool dblwr_corrupted = |
| 3066 |
is_dblwr_page_corrupted(page, space, page_no, &dblwr_err); |
| 3067 |
|
| 3068 |
if (dblwr_corrupted) { |
| 3069 |
std::ostringstream out; |
| 3070 |
|
| 3071 |
out << "Dumping the data file page (page_id=" << page_id << "):"; |
| 3072 |
ib::error(ER_IB_MSG_DBLWR_1304, out.str().c_str()); |
| 3073 |
|
| 3074 |
buf_page_print(buffer.begin(), page_size, BUF_PAGE_PRINT_NO_CRASH); |
| 3075 |
|
| 3076 |
out.str(""); |
| 3077 |
out << "Dumping the DBLWR page (dblwr_page_no=" << dblwr_page_no << "):"; |
| 3078 |
ib::error(ER_IB_MSG_DBLWR_1295, out.str().c_str()); |
| 3079 |
|
| 3080 |
buf_page_print(page, page_size, BUF_PAGE_PRINT_NO_CRASH); |
| 3081 |
|
| 3082 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_DBLWR_1306); |
| 3083 |
} |
| 3084 |
|
| 3085 |
} else { |
| 3086 |
bool data_page_zeroes = buf_page_is_zeroes(buffer.begin(), page_size); |
| 3087 |
bool dblwr_zeroes = buf_page_is_zeroes(page, page_size); |
| 3088 |
dberr_t dblwr_err; |
| 3089 |
const bool dblwr_corrupted = |
| 3090 |
is_dblwr_page_corrupted(page, space, page_no, &dblwr_err); |
| 3091 |
|
| 3092 |
if (data_page_zeroes && !dblwr_zeroes && !dblwr_corrupted) { |
| 3093 |
/* Database page contained only zeroes, while a valid copy is |
| 3094 |
available in dblwr buffer. */ |
| 3095 |
} else { |
| 3096 |
/* Database page is fine. No need to restore from dblwr. */ |
| 3097 |
return false; |
| 3098 |
} |
| 3099 |
} |
| 3100 |
|
| 3101 |
ut_ad(!Encryption::is_encrypted_page(page)); |
| 3102 |
|
| 3103 |
bool found = false; |
| 3104 |
lsn_t reduced_lsn = LSN_MAX; |
| 3105 |
std::tie(found, reduced_lsn) = find_entry(page_id); |
| 3106 |
lsn_t dblwr_lsn = mach_read_from_8(page + FIL_PAGE_LSN); |
| 3107 |
|
| 3108 |
/* If we find a newer version of page that is in reduced dblwr, we |
| 3109 |
shouldn't restore the old/stale page from regular dblwr. We should |
| 3110 |
abort */ |
| 3111 |
if (found && reduced_lsn != LSN_MAX && reduced_lsn > dblwr_lsn) { |
| 3112 |
ib::fatal(UT_LOCATION_HERE, ER_REDUCED_DBLWR_PAGE_FOUND, |
| 3113 |
space->files.front().name, page_id.space(), page_id.page_no()); |
| 3114 |
} |
| 3115 |
|
| 3116 |
/* Recovered data file pages are written out as uncompressed. */ |
| 3117 |
IORequest write_request(IORequest::WRITE); |
| 3118 |
write_request.disable_compression(); |
| 3119 |
|
| 3120 |
/* Write the good page from the doublewrite buffer to the |
| 3121 |
intended position. */ |
| 3122 |
|
| 3123 |
err = fil_io(write_request, true, page_id, page_size, 0, page_size.physical(), |
| 3124 |
const_cast<byte *>(page), nullptr); |
| 3125 |
|
| 3126 |
ut_a(err == DB_SUCCESS || err == DB_TABLESPACE_DELETED); |
| 3127 |
|
| 3128 |
ib::info(ER_IB_MSG_DBLWR_1308) |
| 3129 |
<< "Recovered page " << page_id << " from the doublewrite buffer."; |
| 3130 |
|
| 3131 |
return true; |
| 3132 |
} |
| 3133 |
|
| 3134 |
void dblwr::force_flush(buf_flush_t flush_type, |
| 3135 |
uint32_t buf_pool_index) noexcept { |
| 3136 |
Double_write::force_flush(flush_type, buf_pool_index); |
| 3137 |
} |
| 3138 |
|
| 3139 |
void dblwr::force_flush_all() noexcept { |
| 3140 |
for (ulint i = 0; i < srv_buf_pool_instances; i++) { |
| 3141 |
force_flush(BUF_FLUSH_LRU, i); |
| 3142 |
force_flush(BUF_FLUSH_LIST, i); |
| 3143 |
} |
| 3144 |
} |
| 3145 |
|
| 3146 |
#endif /* !UNIV_HOTBACKUP */ |
| 3147 |
|
| 3148 |
void recv::Pages::recover(fil_space_t *space) noexcept { |
| 3149 |
#ifndef UNIV_HOTBACKUP |
| 3150 |
/* For cloned database double write pages should be ignored. However, |
| 3151 |
given the control flow, we read the pages in anyway but don't recover |
| 3152 |
from the pages we read in. */ |
| 3153 |
|
| 3154 |
if (!dblwr::is_enabled() || recv_sys->is_cloned_db) { |
| 3155 |
return; |
| 3156 |
} |
| 3157 |
|
| 3158 |
auto recover_all = (space == nullptr); |
| 3159 |
|
| 3160 |
for (const auto &page : m_pages) { |
| 3161 |
if (page->m_recovered) { |
| 3162 |
continue; |
| 3163 |
} |
| 3164 |
|
| 3165 |
auto ptr = page->m_buffer.begin(); |
| 3166 |
auto page_no = page_get_page_no(ptr); |
| 3167 |
auto space_id = page_get_space_id(ptr); |
| 3168 |
|
| 3169 |
if (recover_all) { |
| 3170 |
space = fil_space_get(space_id); |
| 3171 |
|
| 3172 |
if (space == nullptr) { |
| 3173 |
/* Maybe we have dropped the tablespace |
| 3174 |
and this page once belonged to it: do nothing. */ |
| 3175 |
continue; |
| 3176 |
} |
| 3177 |
|
| 3178 |
} else if (space->id != space_id) { |
| 3179 |
continue; |
| 3180 |
} |
| 3181 |
|
| 3182 |
fil_space_open_if_needed(space); |
| 3183 |
|
| 3184 |
page->m_recovered = |
| 3185 |
dblwr_recover_page(page->m_no, space, page_no, page->m_buffer.begin()); |
| 3186 |
} |
| 3187 |
|
| 3188 |
reduced_recover(space); |
| 3189 |
fil_flush_file_spaces(); |
| 3190 |
#endif /* !UNIV_HOTBACKUP */ |
| 3191 |
} |
| 3192 |
|
| 3193 |
void recv::Pages::reduced_recover(fil_space_t *space) noexcept { |
| 3194 |
#ifndef UNIV_HOTBACKUP |
| 3195 |
auto recover_all = (space == nullptr); |
| 3196 |
|
| 3197 |
for (const auto &entry : m_page_entries) { |
| 3198 |
auto space_id = entry.m_space_id; |
| 3199 |
page_id_t page_id(entry.m_space_id, entry.m_page_no); |
| 3200 |
|
| 3201 |
if (recover_all) { |
| 3202 |
space = fil_space_get(space_id); |
| 3203 |
|
| 3204 |
if (space == nullptr) { |
| 3205 |
/* Maybe we have dropped the tablespace |
| 3206 |
and this page once belonged to it: do nothing. */ |
| 3207 |
continue; |
| 3208 |
} |
| 3209 |
|
| 3210 |
} else if (space->id != space_id) { |
| 3211 |
continue; |
| 3212 |
} |
| 3213 |
|
| 3214 |
fil_space_open_if_needed(space); |
| 3215 |
|
| 3216 |
bool is_corrupted = false; |
| 3217 |
bool is_all_zero = false; |
| 3218 |
std::tie(is_corrupted, is_all_zero) = |
| 3219 |
is_actual_page_corrupted(space, page_id); |
| 3220 |
|
| 3221 |
if (is_corrupted) { |
| 3222 |
if (find(page_id) == nullptr || !is_recovered(page_id)) { |
| 3223 |
ib::fatal(UT_LOCATION_HERE, ER_REDUCED_DBLWR_PAGE_FOUND, |
| 3224 |
space->files.front().name, page_id.space(), |
| 3225 |
page_id.page_no()); |
| 3226 |
} |
| 3227 |
} |
| 3228 |
|
| 3229 |
if (is_all_zero) { |
| 3230 |
// is there a dblwr reduced entry with non-zero LSN? |
| 3231 |
bool found = false; |
| 3232 |
lsn_t reduced_lsn = LSN_MAX; |
| 3233 |
std::tie(found, reduced_lsn) = find_entry(page_id); |
| 3234 |
|
| 3235 |
if (!is_recovered(page_id) && found && reduced_lsn != LSN_MAX && |
| 3236 |
reduced_lsn != 0) { |
| 3237 |
ib::fatal(UT_LOCATION_HERE, ER_REDUCED_DBLWR_PAGE_FOUND, |
| 3238 |
space->files.front().name, page_id.space(), |
| 3239 |
page_id.page_no()); |
| 3240 |
} |
| 3241 |
} |
| 3242 |
} |
| 3243 |
#endif /* !UNIV_HOTBACKUP */ |
| 3244 |
} |
| 3245 |
|
| 3246 |
const byte *recv::Pages::find(const page_id_t &page_id) const noexcept { |
| 3247 |
if (!dblwr::is_enabled()) { |
| 3248 |
return nullptr; |
| 3249 |
} |
| 3250 |
using Matches = std::vector<const byte *, ut::allocator<const byte *>>; |
| 3251 |
|
| 3252 |
Matches matches; |
| 3253 |
const byte *page = nullptr; |
| 3254 |
|
| 3255 |
for (const auto &page : m_pages) { |
| 3256 |
auto &buffer = page->m_buffer; |
| 3257 |
|
| 3258 |
if (page_get_space_id(buffer.begin()) == page_id.space() && |
| 3259 |
page_get_page_no(buffer.begin()) == page_id.page_no()) { |
| 3260 |
matches.push_back(buffer.begin()); |
| 3261 |
} |
| 3262 |
} |
| 3263 |
|
| 3264 |
if (matches.size() == 1) { |
| 3265 |
page = matches[0]; |
| 3266 |
|
| 3267 |
} else if (matches.size() > 1) { |
| 3268 |
lsn_t max_lsn = 0; |
| 3269 |
|
| 3270 |
for (const auto &match : matches) { |
| 3271 |
lsn_t page_lsn = mach_read_from_8(match + FIL_PAGE_LSN); |
| 3272 |
|
| 3273 |
if (page_lsn > max_lsn) { |
| 3274 |
max_lsn = page_lsn; |
| 3275 |
page = match; |
| 3276 |
} |
| 3277 |
} |
| 3278 |
} |
| 3279 |
|
| 3280 |
return page; |
| 3281 |
} |
| 3282 |
|
| 3283 |
bool recv::Pages::is_recovered(const page_id_t &page_id) const noexcept { |
| 3284 |
for (const auto &page : m_pages) { |
| 3285 |
auto &buffer = page->m_buffer; |
| 3286 |
|
| 3287 |
if (page_get_space_id(buffer.begin()) == page_id.space() && |
| 3288 |
page_get_page_no(buffer.begin()) == page_id.page_no() && |
| 3289 |
page->m_recovered) { |
| 3290 |
return (true); |
| 3291 |
} |
| 3292 |
} |
| 3293 |
return (false); |
| 3294 |
} |
| 3295 |
|
| 3296 |
void recv::Pages::add(page_no_t page_no, const byte *page, |
| 3297 |
uint32_t n_bytes) noexcept { |
| 3298 |
if (!dblwr::is_enabled()) { |
| 3299 |
return; |
| 3300 |
} |
| 3301 |
/* Make a copy of the page contents. */ |
| 3302 |
auto dblwr_page = |
| 3303 |
ut::new_withkey<Page>(UT_NEW_THIS_FILE_PSI_KEY, page_no, page, n_bytes); |
| 3304 |
|
| 3305 |
m_pages.push_back(dblwr_page); |
| 3306 |
} |
| 3307 |
|
| 3308 |
void recv::Pages::check_missing_tablespaces() const noexcept { |
| 3309 |
/* For cloned database double write pages should be ignored. However, |
| 3310 |
given the control flow, we read the pages in anyway but don't recover |
| 3311 |
from the pages we read in. */ |
| 3312 |
if (!dblwr::is_enabled()) { |
| 3313 |
return; |
| 3314 |
} |
| 3315 |
|
| 3316 |
const auto end = recv_sys->deleted.end(); |
| 3317 |
|
| 3318 |
for (const auto &page : m_pages) { |
| 3319 |
if (page->m_recovered) { |
| 3320 |
continue; |
| 3321 |
} |
| 3322 |
|
| 3323 |
const auto &buffer = page->m_buffer; |
| 3324 |
auto space_id = page_get_space_id(buffer.begin()); |
| 3325 |
|
| 3326 |
/* Skip messages for undo tablespaces that are being truncated since |
| 3327 |
they can be deleted during undo truncation without an MLOG_FILE_DELETE. |
| 3328 |
*/ |
| 3329 |
|
| 3330 |
if (!fsp_is_undo_tablespace(space_id)) { |
| 3331 |
/* If the tablespace was in the missing IDs then we |
| 3332 |
know that the problem is elsewhere. If a file deleted |
| 3333 |
record was not found in the redo log and the tablespace |
| 3334 |
doesn't exist in the SYS_TABLESPACES file then it is |
| 3335 |
an error or data corruption. The special case is an |
| 3336 |
undo truncate in progress. */ |
| 3337 |
|
| 3338 |
if (recv_sys->deleted.find(space_id) == end && |
| 3339 |
recv_sys->missing_ids.find(space_id) != recv_sys->missing_ids.end()) { |
| 3340 |
auto page_no = page_get_page_no(buffer.begin()); |
| 3341 |
|
| 3342 |
ib::warn(ER_IB_MSG_DBLWR_1296) |
| 3343 |
<< "Doublewrite page " << page->m_no << " for {space: " << space_id |
| 3344 |
<< ", page_no:" << page_no << "} could not be restored." |
| 3345 |
<< " File name unknown for tablespace ID " << space_id; |
| 3346 |
} |
| 3347 |
} |
| 3348 |
} |
| 3349 |
} |
| 3350 |
|
| 3351 |
dberr_t dblwr::recv::load(recv::Pages *pages) noexcept { |
| 3352 |
#ifndef UNIV_HOTBACKUP |
| 3353 |
/* For cloned database double write pages should be ignored. */ |
| 3354 |
if (!dblwr::is_enabled()) { |
| 3355 |
return DB_SUCCESS; |
| 3356 |
} |
| 3357 |
|
| 3358 |
ut_ad(!dblwr::dir.empty()); |
| 3359 |
|
| 3360 |
/* The number of buffer pool instances can change. Therefore we must: |
| 3361 |
1. Scan the doublewrite directory for all *.dblwr files and load |
| 3362 |
their contents. |
| 3363 |
2. Reset the file sizes after recovery is complete. */ |
| 3364 |
|
| 3365 |
auto real_path_dir = Fil_path::get_real_path(dblwr::dir); |
| 3366 |
|
| 3367 |
/* Walk the sub-tree of dblwr::dir. */ |
| 3368 |
|
| 3369 |
std::vector<std::string> dblwr_files; |
| 3370 |
|
| 3371 |
Dir_Walker::walk(real_path_dir, false, [&](const std::string &path) { |
| 3372 |
ut_a(path.length() > real_path_dir.length()); |
| 3373 |
|
| 3374 |
if (Fil_path::get_file_type(path) != OS_FILE_TYPE_FILE) { |
| 3375 |
return; |
| 3376 |
} |
| 3377 |
|
| 3378 |
/* Make the filename relative to the directory that was scanned. */ |
| 3379 |
|
| 3380 |
auto file = path.substr(real_path_dir.length(), path.length()); |
| 3381 |
|
| 3382 |
/** 6 == strlen(".dblwr"). */ |
| 3383 |
if (file.size() <= 6) { |
| 3384 |
return; |
| 3385 |
} |
| 3386 |
|
| 3387 |
if (Fil_path::has_suffix(DWR, file.c_str())) { |
| 3388 |
dblwr_files.push_back(file); |
| 3389 |
} |
| 3390 |
}); |
| 3391 |
|
| 3392 |
/* We have to use all the dblwr files for recovery. */ |
| 3393 |
|
| 3394 |
std::string rexp{"#ib_([0-9]+)_([0-9]+)\\"}; |
| 3395 |
|
| 3396 |
rexp.append(dot_ext[DWR]); |
| 3397 |
|
| 3398 |
const std::regex regex{rexp}; |
| 3399 |
|
| 3400 |
std::vector<int> ids; |
| 3401 |
|
| 3402 |
for (auto &file : dblwr_files) { |
| 3403 |
std::smatch match; |
| 3404 |
|
| 3405 |
if (std::regex_match(file, match, regex) && match.size() == 3) { |
| 3406 |
/* Check if the page size matches. */ |
| 3407 |
int page_size = std::stoi(match[1].str()); |
| 3408 |
|
| 3409 |
if (page_size == (int)srv_page_size) { |
| 3410 |
int id = std::stoi(match[2].str()); |
| 3411 |
ids.push_back(id); |
| 3412 |
} else { |
| 3413 |
ib::info(ER_IB_MSG_DBLWR_1310) |
| 3414 |
<< "Ignoring " << file << " - page size doesn't match"; |
| 3415 |
} |
| 3416 |
} else { |
| 3417 |
ib::warn(ER_IB_MSG_DBLWR_1311) |
| 3418 |
<< file << " not in double write buffer file name format!"; |
| 3419 |
} |
| 3420 |
} |
| 3421 |
|
| 3422 |
std::sort(ids.begin(), ids.end()); |
| 3423 |
|
| 3424 |
for (uint32_t i = 0; i < ids.size(); ++i) { |
| 3425 |
if ((uint32_t)ids[i] != i) { |
| 3426 |
ib::warn(ER_IB_MSG_DBLWR_1312) << "Gap in the double write buffer files."; |
| 3427 |
ut_d(ut_error); |
| 3428 |
} |
| 3429 |
} |
| 3430 |
|
| 3431 |
uint32_t max_id; |
| 3432 |
|
| 3433 |
if (!ids.empty()) { |
| 3434 |
max_id = std::max((int)srv_buf_pool_instances, ids.back() + 1); |
| 3435 |
} else { |
| 3436 |
max_id = srv_buf_pool_instances; |
| 3437 |
} |
| 3438 |
|
| 3439 |
for (uint32_t i = 0; i < max_id; ++i) { |
| 3440 |
dblwr::File file; |
| 3441 |
|
| 3442 |
/* Open the file for reading. */ |
| 3443 |
auto err = dblwr_file_open(dblwr::dir, i, file, OS_DATA_FILE); |
| 3444 |
|
| 3445 |
if (err == DB_NOT_FOUND) { |
| 3446 |
continue; |
| 3447 |
} else if (err != DB_SUCCESS) { |
| 3448 |
return err; |
| 3449 |
} |
| 3450 |
|
| 3451 |
err = Double_write::load(file, pages); |
| 3452 |
|
| 3453 |
os_file_close(file.m_pfs); |
| 3454 |
|
| 3455 |
if (err != DB_SUCCESS) { |
| 3456 |
return err; |
| 3457 |
} |
| 3458 |
} |
| 3459 |
#endif /* UNIV_HOTBACKUP */ |
| 3460 |
return DB_SUCCESS; |
| 3461 |
} |
| 3462 |
|
| 3463 |
dberr_t dblwr::recv::reduced_load(recv::Pages *pages) noexcept { |
| 3464 |
#ifndef UNIV_HOTBACKUP |
| 3465 |
/* For cloned database double write pages should be ignored. */ |
| 3466 |
if (!dblwr::is_enabled()) { |
| 3467 |
return DB_SUCCESS; |
| 3468 |
} |
| 3469 |
|
| 3470 |
ut_ad(!dblwr::dir.empty()); |
| 3471 |
|
| 3472 |
/* The number of buffer pool instances can change. Therefore we must: |
| 3473 |
1. Scan the doublewrite directory for all *.dblwr files and load |
| 3474 |
their contents. |
| 3475 |
2. Reset the file sizes after recovery is complete. */ |
| 3476 |
|
| 3477 |
auto real_path_dir = Fil_path::get_real_path(dblwr::dir); |
| 3478 |
|
| 3479 |
/* Walk the sub-tree of dblwr::dir. */ |
| 3480 |
|
| 3481 |
std::vector<std::string> dblwr_files; |
| 3482 |
|
| 3483 |
Dir_Walker::walk(real_path_dir, false, [&](const std::string &path) { |
| 3484 |
ut_a(path.length() > real_path_dir.length()); |
| 3485 |
|
| 3486 |
if (Fil_path::get_file_type(path) != OS_FILE_TYPE_FILE) { |
| 3487 |
return; |
| 3488 |
} |
| 3489 |
|
| 3490 |
/* Make the filename relative to the directory that was scanned. */ |
| 3491 |
|
| 3492 |
auto file = path.substr(real_path_dir.length(), path.length()); |
| 3493 |
|
| 3494 |
if (file.size() <= strlen(dot_ext[BWR])) { |
| 3495 |
return; |
| 3496 |
} |
| 3497 |
|
| 3498 |
if (Fil_path::has_suffix(BWR, file.c_str())) { |
| 3499 |
dblwr_files.push_back(file); |
| 3500 |
} |
| 3501 |
}); |
| 3502 |
/* We have to use all the dblwr files for recovery. */ |
| 3503 |
|
| 3504 |
std::string rexp{"#ib_([0-9]+)_([0-9]+)\\"}; |
| 3505 |
|
| 3506 |
rexp.append(dot_ext[BWR]); |
| 3507 |
|
| 3508 |
const std::regex regex{rexp}; |
| 3509 |
|
| 3510 |
std::vector<int> ids; |
| 3511 |
|
| 3512 |
for (auto &file : dblwr_files) { |
| 3513 |
std::smatch match; |
| 3514 |
|
| 3515 |
if (std::regex_match(file, match, regex) && match.size() == 3) { |
| 3516 |
/* Check if the page size matches. */ |
| 3517 |
int page_size = std::stoi(match[1].str()); |
| 3518 |
|
| 3519 |
if (page_size == (int)srv_page_size) { |
| 3520 |
int id = std::stoi(match[2].str()); |
| 3521 |
ids.push_back(id); |
| 3522 |
} else { |
| 3523 |
ib::info(ER_IB_MSG_DBLWR_1310) |
| 3524 |
<< "Ignoring " << file << " - page size doesn't match"; |
| 3525 |
} |
| 3526 |
} else { |
| 3527 |
ib::warn(ER_IB_MSG_DBLWR_1311) |
| 3528 |
<< file << " not in double write buffer file name format!"; |
| 3529 |
} |
| 3530 |
} |
| 3531 |
|
| 3532 |
if (ids.size() == 0) { |
| 3533 |
// We are starting on older version that doesn't have reduced dblwr file |
| 3534 |
return (DB_SUCCESS); |
| 3535 |
} |
| 3536 |
|
| 3537 |
// There should be always only one Batch DBLWR file |
| 3538 |
ut_ad(ids.size() == 1); |
| 3539 |
|
| 3540 |
dblwr::File file; |
| 3541 |
|
| 3542 |
/* Open the file for reading. */ |
| 3543 |
auto err = dblwr_file_open(dblwr::dir, 0, file, OS_DATA_FILE, BWR); |
| 3544 |
|
| 3545 |
if (err == DB_NOT_FOUND) { |
| 3546 |
return (DB_SUCCESS); |
| 3547 |
} else if (err != DB_SUCCESS) { |
| 3548 |
return err; |
| 3549 |
} |
| 3550 |
|
| 3551 |
err = Double_write::load_reduced_batch(file, pages); |
| 3552 |
|
| 3553 |
os_file_close(file.m_pfs); |
| 3554 |
|
| 3555 |
if (err != DB_SUCCESS) { |
| 3556 |
return err; |
| 3557 |
} |
| 3558 |
|
| 3559 |
#endif /* UNIV_HOTBACKUP */ |
| 3560 |
return DB_SUCCESS; |
| 3561 |
} |
| 3562 |
|
| 3563 |
const byte *dblwr::recv::find(const recv::Pages *pages, |
| 3564 |
const page_id_t &page_id) noexcept { |
| 3565 |
return pages->find(page_id); |
| 3566 |
} |
| 3567 |
|
| 3568 |
std::tuple<bool, lsn_t> dblwr::recv::find_entry( |
| 3569 |
const recv::Pages *pages, const page_id_t &page_id) noexcept { |
| 3570 |
return pages->find_entry(page_id); |
| 3571 |
} |
| 3572 |
|
| 3573 |
void dblwr::recv::create(recv::Pages *&pages) noexcept { |
| 3574 |
ut_a(pages == nullptr); |
| 3575 |
pages = ut::new_withkey<recv::Pages>(UT_NEW_THIS_FILE_PSI_KEY); |
| 3576 |
} |
| 3577 |
|
| 3578 |
void dblwr::recv::destroy(recv::Pages *&pages) noexcept { |
| 3579 |
if (pages != nullptr) { |
| 3580 |
ut::delete_(pages); |
| 3581 |
pages = nullptr; |
| 3582 |
} |
| 3583 |
} |
| 3584 |
|
| 3585 |
void dblwr::recv::check_missing_tablespaces(const recv::Pages *pages) noexcept { |
| 3586 |
pages->check_missing_tablespaces(); |
| 3587 |
} |
| 3588 |
|
| 3589 |
namespace dblwr { |
| 3590 |
|
| 3591 |
#ifndef UNIV_HOTBACKUP |
| 3592 |
#ifdef UNIV_DEBUG |
| 3593 |
static bool is_encrypted_page(const byte *page) noexcept { |
| 3594 |
ulint page_type = mach_read_from_2(page + FIL_PAGE_TYPE); |
| 3595 |
|
| 3596 |
return page_type == FIL_PAGE_ENCRYPTED || |
| 3597 |
page_type == FIL_PAGE_COMPRESSED_AND_ENCRYPTED || |
| 3598 |
page_type == FIL_PAGE_ENCRYPTED_RTREE; |
| 3599 |
} |
| 3600 |
|
| 3601 |
bool has_encrypted_pages() noexcept { |
| 3602 |
bool st = false; |
| 3603 |
for (dblwr::File &file : Double_write::s_files) { |
| 3604 |
dblwr::recv::Pages pages; |
| 3605 |
TLOG("Loading= " << file); |
| 3606 |
|
| 3607 |
dberr_t err = Double_write::load(file, &pages); |
| 3608 |
if (err != DB_SUCCESS) { |
| 3609 |
TLOG("Failed to load= " << file); |
| 3610 |
return st; |
| 3611 |
} |
| 3612 |
|
| 3613 |
for (const auto &page : pages.get_pages()) { |
| 3614 |
auto &buffer = page->m_buffer; |
| 3615 |
byte *frame = buffer.begin(); |
| 3616 |
page_type_t page_type = fil_page_get_type(frame); |
| 3617 |
|
| 3618 |
TLOG("space_id=" << page_get_space_id(frame) |
| 3619 |
<< ", page_no=" << page_get_page_no(frame) |
| 3620 |
<< ", page_type=" << fil_get_page_type_str(page_type)); |
| 3621 |
|
| 3622 |
if (is_encrypted_page(frame)) { |
| 3623 |
st = true; |
| 3624 |
} |
| 3625 |
} |
| 3626 |
} |
| 3627 |
return st; |
| 3628 |
} |
| 3629 |
#endif /* UNIV_DEBUG */ |
| 3630 |
|
| 3631 |
Reduced_entry::Reduced_entry(buf_page_t *bpage) |
| 3632 |
: m_space_id(bpage->space()), |
| 3633 |
m_page_no(bpage->page_no()), |
| 3634 |
m_lsn(bpage->get_newest_lsn()) {} |
| 3635 |
|
| 3636 |
#endif /* !UNIV_HOTBACKUP */ |
| 3637 |
|
| 3638 |
} // namespace dblwr |