| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2018, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License, version 2.0, |
| 7 |
as published by the 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, |
| 18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
GNU General Public License, version 2.0, for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License |
| 23 |
along with this program; if not, write to the Free Software |
| 24 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/**************************************************/ /** |
| 29 |
@file include/arch0recv.h |
| 30 |
Interface for crash recovery for page archiver system. |
| 31 |
|
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef ARCH_RECV_INCLUDE |
| 35 |
#define ARCH_RECV_INCLUDE |
| 36 |
|
| 37 |
#include "arch0arch.h" |
| 38 |
#include "arch0page.h" |
| 39 |
|
| 40 |
/** Info related to each group parsed at different stages of page archive |
| 41 |
recovery. */ |
| 42 |
class Arch_Recv_Group_Info { |
| 43 |
public: |
| 44 |
Arch_Recv_Group_Info() { |
| 45 |
m_reset_pos.init(); |
| 46 |
m_write_pos.init(); |
| 47 |
|
| 48 |
m_last_reset_block = static_cast<byte *>(ut::zalloc_withkey( |
| 49 |
ut::make_psi_memory_key(mem_key_archive), ARCH_PAGE_BLK_SIZE)); |
| 50 |
m_last_data_block = static_cast<byte *>(ut::zalloc_withkey( |
| 51 |
ut::make_psi_memory_key(mem_key_archive), ARCH_PAGE_BLK_SIZE)); |
| 52 |
} |
| 53 |
|
| 54 |
~Arch_Recv_Group_Info() { |
| 55 |
ut::free(m_last_reset_block); |
| 56 |
ut::free(m_last_data_block); |
| 57 |
} |
| 58 |
|
| 59 |
/** Disable assignment. */ |
| 60 |
Arch_Recv_Group_Info &operator=(const Arch_Recv_Group_Info &) = delete; |
| 61 |
|
| 62 |
/** Disable copy construction. */ |
| 63 |
Arch_Recv_Group_Info(const Arch_Recv_Group_Info &) = delete; |
| 64 |
|
| 65 |
/** Group data. */ |
| 66 |
Arch_Group *m_group{nullptr}; |
| 67 |
|
| 68 |
/** Number of archived files belonging to the group. */ |
| 69 |
uint m_num_files{0}; |
| 70 |
|
| 71 |
/** Group is active or not. */ |
| 72 |
bool m_active{false}; |
| 73 |
|
| 74 |
/** True if group is from durable archiving, false if left over from a crash |
| 75 |
during clone operation. */ |
| 76 |
bool m_durable{false}; |
| 77 |
|
| 78 |
/** True if a new empty file was present in the group directory. |
| 79 |
This can happen in case of a crash while writing to a new file. */ |
| 80 |
bool m_new_empty_file{false}; |
| 81 |
|
| 82 |
/** The file index which is part of the file name may not necessarily |
| 83 |
be 0 always. It's possible that purge might have purged files in the |
| 84 |
group leading to the file index of the first file in the group being |
| 85 |
greater than 0. So we need this info to know the index of the first |
| 86 |
file in the group. */ |
| 87 |
uint m_file_start_index{std::numeric_limits<uint>::max()}; |
| 88 |
|
| 89 |
/** Last reset position of the group. */ |
| 90 |
Arch_Page_Pos m_reset_pos; |
| 91 |
|
| 92 |
/** Last write position of the group. */ |
| 93 |
Arch_Page_Pos m_write_pos; |
| 94 |
|
| 95 |
/** Reset block of the last reset file in a group. */ |
| 96 |
byte *m_last_reset_block{nullptr}; |
| 97 |
|
| 98 |
/** Data block of the last reset file in a group. */ |
| 99 |
byte *m_last_data_block{nullptr}; |
| 100 |
|
| 101 |
/** Reset file structure of the last reset file */ |
| 102 |
Arch_Reset_File m_last_reset_file; |
| 103 |
|
| 104 |
/** Start LSN of the group. */ |
| 105 |
lsn_t m_start_lsn{LSN_MAX}; |
| 106 |
|
| 107 |
/** Last stop LSN of the group if active, else end LSN. */ |
| 108 |
lsn_t m_last_stop_lsn{LSN_MAX}; |
| 109 |
}; |
| 110 |
|
| 111 |
/** Mapping of group directory name to information related to the recovery |
| 112 |
group info. */ |
| 113 |
using Arch_Dir_Group_Info_Map = |
| 114 |
std::unordered_map<std::string, Arch_Recv_Group_Info>; |
| 115 |
|
| 116 |
/** Doublewrite buffer block along with their info. */ |
| 117 |
struct Arch_Dblwr_Block { |
| 118 |
/** Type of block flushed into the doublewrite block */ |
| 119 |
Arch_Blk_Type m_block_type; |
| 120 |
|
| 121 |
/** Flush type of the block flushed into the doublewrite buffer */ |
| 122 |
Arch_Blk_Flush_Type m_flush_type; |
| 123 |
|
| 124 |
/** Block number of the block flushed into the doublewrite buffer */ |
| 125 |
uint64_t m_block_num; |
| 126 |
|
| 127 |
/** Doublewrite buffer block */ |
| 128 |
byte *m_block; |
| 129 |
}; |
| 130 |
|
| 131 |
/** Vector of doublewrite buffer blocks and their info. */ |
| 132 |
using Arch_Dblwr_Blocks = std::vector<Arch_Dblwr_Block>; |
| 133 |
|
| 134 |
/** Doublewrite buffer context. */ |
| 135 |
class Arch_Dblwr_Ctx { |
| 136 |
public: |
| 137 |
/** Constructor: Initialize members */ |
| 138 |
Arch_Dblwr_Ctx() = default; |
| 139 |
|
| 140 |
~Arch_Dblwr_Ctx() { |
| 141 |
ut::free(m_buf); |
| 142 |
m_file_ctx.close(); |
| 143 |
} |
| 144 |
|
| 145 |
/** Initialize the doublewrite buffer. |
| 146 |
@param[in] path path to the file |
| 147 |
@param[in] base_file file name prefix |
| 148 |
@param[in] num_files initial number of files |
| 149 |
@param[in] file_size file size in bytes |
| 150 |
@return error code. */ |
| 151 |
dberr_t init(const char *path, const char *base_file, uint num_files, |
| 152 |
uint64_t file_size); |
| 153 |
|
| 154 |
/** Read the doublewrite buffer file. |
| 155 |
@return error code */ |
| 156 |
dberr_t read_file(); |
| 157 |
|
| 158 |
/** Validate the blocks contained in the m_buf buffer and load only the valid |
| 159 |
buffers into m_blocks. |
| 160 |
@param[in] num_files number of files in the group information required to |
| 161 |
validate the blocks */ |
| 162 |
void validate_and_fill_blocks(size_t num_files); |
| 163 |
|
| 164 |
/** Get doubewrite buffer blocks. |
| 165 |
@return doublewrite buffer blocks */ |
| 166 |
Arch_Dblwr_Blocks blocks() { return m_blocks; } |
| 167 |
|
| 168 |
/** Disable copy construction. */ |
| 169 |
Arch_Dblwr_Ctx(Arch_Dblwr_Ctx const &) = delete; |
| 170 |
|
| 171 |
/** Disable assignment. */ |
| 172 |
Arch_Dblwr_Ctx &operator=(Arch_Dblwr_Ctx const &) = delete; |
| 173 |
|
| 174 |
private: |
| 175 |
/** Buffer to hold the contents of the doublwrite buffer. */ |
| 176 |
byte *m_buf{nullptr}; |
| 177 |
|
| 178 |
/** Total file size of the file which holds the doublewrite buffer. */ |
| 179 |
uint64_t m_file_size{}; |
| 180 |
|
| 181 |
/** Doublewrite buffer file context. */ |
| 182 |
Arch_File_Ctx m_file_ctx; |
| 183 |
|
| 184 |
/** List of doublewrite buffer blocks. */ |
| 185 |
Arch_Dblwr_Blocks m_blocks{}; |
| 186 |
}; |
| 187 |
|
| 188 |
/** Recovery system data structure for the archiver. */ |
| 189 |
class Arch_Page_Sys::Recovery { |
| 190 |
public: |
| 191 |
/** Constructor: Initialize members |
| 192 |
@param[in,out] page_sys global dirty page archive system |
| 193 |
@param[in] dir_name main archiver directory name */ |
| 194 |
Recovery(Arch_Page_Sys *page_sys, const char *dir_name) |
| 195 |
: m_arch_dir_name(dir_name), m_page_sys(page_sys) {} |
| 196 |
|
| 197 |
/** Destructor: Close open file and free resources */ |
| 198 |
~Recovery() = default; |
| 199 |
|
| 200 |
/** Initialise the archiver's recovery system. |
| 201 |
@return error code. */ |
| 202 |
dberr_t init_dblwr(); |
| 203 |
|
| 204 |
/** Scan the archive directory and fetch all info related to group |
| 205 |
directories and its files. |
| 206 |
@return true if the scan was successful. */ |
| 207 |
bool scan_for_groups(); |
| 208 |
|
| 209 |
#ifdef UNIV_DEBUG |
| 210 |
/** Print information related to the archiver recovery system added |
| 211 |
for debugging purposes. */ |
| 212 |
void print(); |
| 213 |
#endif |
| 214 |
|
| 215 |
/** Parse for group information and fill the group. |
| 216 |
@return error code. */ |
| 217 |
dberr_t recover(); |
| 218 |
|
| 219 |
/** Load archiver with the related data and start tracking if required. |
| 220 |
@return error code. */ |
| 221 |
dberr_t load_archiver(); |
| 222 |
|
| 223 |
/** Disable copy construction */ |
| 224 |
Recovery(Recovery const &) = delete; |
| 225 |
|
| 226 |
/** Disable assignment */ |
| 227 |
Recovery &operator=(Recovery const &) = delete; |
| 228 |
|
| 229 |
private: |
| 230 |
/** Read all the group directories and store information related to them |
| 231 |
required for parsing. |
| 232 |
@param[in] file_path file path information */ |
| 233 |
void read_group_dirs(const std::string file_path); |
| 234 |
|
| 235 |
/** Read all the archived files belonging to a group and store information |
| 236 |
related to them required for parsing. |
| 237 |
@param[in] dir_path dir path information |
| 238 |
@param[in] file_path file path information */ |
| 239 |
void read_group_files(const std::string dir_path, |
| 240 |
const std::string file_path); |
| 241 |
|
| 242 |
private: |
| 243 |
/** Archive directory. */ |
| 244 |
std::string m_arch_dir_name; |
| 245 |
|
| 246 |
/** Global dirty page archive system */ |
| 247 |
Arch_Page_Sys *m_page_sys; |
| 248 |
|
| 249 |
/** Doublewrite buffer context. */ |
| 250 |
Arch_Dblwr_Ctx m_dblwr_ctx{}; |
| 251 |
|
| 252 |
/** Mapping of group directory names and group information related to |
| 253 |
the group. */ |
| 254 |
Arch_Dir_Group_Info_Map m_dir_group_info_map{}; |
| 255 |
}; |
| 256 |
|
| 257 |
/** Recovery system data structure for the archiver. */ |
| 258 |
class Arch_Group::Recovery { |
| 259 |
public: |
| 260 |
/** Constructor. |
| 261 |
@param[in] group the parent class group object */ |
| 262 |
Recovery(Arch_Group *group) { |
| 263 |
ut_ad(group != nullptr); |
| 264 |
m_group = group; |
| 265 |
} |
| 266 |
|
| 267 |
/** Destructor. */ |
| 268 |
~Recovery() {} |
| 269 |
|
| 270 |
/** Check and replace blocks in archived files belonging to a group |
| 271 |
from the doublewrite buffer if required. |
| 272 |
@param[in] dblwr_ctx Doublewrite context which has the doublewrite |
| 273 |
buffer blocks |
| 274 |
@return error code */ |
| 275 |
dberr_t replace_pages_from_dblwr(Arch_Dblwr_Ctx *dblwr_ctx); |
| 276 |
|
| 277 |
/** Delete the last file if there are no blocks flushed to it. |
| 278 |
@param[in,out] info information related to group required for recovery |
| 279 |
@return error code. */ |
| 280 |
dberr_t cleanup_if_required(Arch_Recv_Group_Info &info); |
| 281 |
|
| 282 |
/** Start parsing the archive file for archive group information. |
| 283 |
@param[in,out] info information related to group required for recovery |
| 284 |
@return error code */ |
| 285 |
dberr_t parse(Arch_Recv_Group_Info &info); |
| 286 |
|
| 287 |
/** Attach system client to the archiver during recovery if any group was |
| 288 |
active at the time of crash. */ |
| 289 |
void attach() { ++m_group->m_dur_ref_count; } |
| 290 |
|
| 291 |
/** Disable copy construction */ |
| 292 |
Recovery(Recovery const &) = delete; |
| 293 |
|
| 294 |
/** Disable assignment */ |
| 295 |
Recovery &operator=(Recovery const &) = delete; |
| 296 |
|
| 297 |
private: |
| 298 |
/** The parent class group object. */ |
| 299 |
Arch_Group *m_group{nullptr}; |
| 300 |
}; |
| 301 |
|
| 302 |
/** Recovery system data structure for the archiver. */ |
| 303 |
class Arch_File_Ctx::Recovery { |
| 304 |
public: |
| 305 |
/** Constructor. |
| 306 |
@param[in] file_ctx file context to be used by this recovery class */ |
| 307 |
Recovery(Arch_File_Ctx &file_ctx) : m_file_ctx(file_ctx) {} |
| 308 |
|
| 309 |
/** Destructor. */ |
| 310 |
~Recovery() {} |
| 311 |
|
| 312 |
#ifdef UNIV_DEBUG |
| 313 |
/** Print recovery related data. |
| 314 |
@param[in] file_start_index file index from where to begin */ |
| 315 |
void reset_print(uint file_start_index); |
| 316 |
#endif |
| 317 |
|
| 318 |
/** Fetch the reset points pertaining to a file. |
| 319 |
@param[in] file_index file index of the file from which reset points |
| 320 |
needs to be fetched |
| 321 |
@param[in] last_file true if the file for which the stop point is |
| 322 |
being fetched for is the last file |
| 323 |
@param[in,out] info information related to group required for recovery |
| 324 |
@return error code. */ |
| 325 |
dberr_t parse_reset_points(uint file_index, bool last_file, |
| 326 |
Arch_Recv_Group_Info &info); |
| 327 |
|
| 328 |
/** Fetch the stop lsn pertaining to a file. |
| 329 |
@param[in] last_file true if the file for which the stop point is |
| 330 |
being fetched for is the last file |
| 331 |
@param[in,out] info information related to group required for recovery |
| 332 |
@return error code. */ |
| 333 |
dberr_t parse_stop_points(bool last_file, Arch_Recv_Group_Info &info); |
| 334 |
|
| 335 |
/** Disable copy construction */ |
| 336 |
Recovery(Recovery const &) = delete; |
| 337 |
|
| 338 |
/** Disable assignment */ |
| 339 |
Recovery &operator=(Recovery const &) = delete; |
| 340 |
|
| 341 |
private: |
| 342 |
/** File context. */ |
| 343 |
Arch_File_Ctx &m_file_ctx; |
| 344 |
}; |
| 345 |
|
| 346 |
#endif /* ARCH_RECV_INCLUDE */ |