| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2017, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/arch0page.h |
| 29 |
Innodb interface for modified page archive |
| 30 |
|
| 31 |
*******************************************************/ |
| 32 |
|
| 33 |
#ifndef ARCH_PAGE_INCLUDE |
| 34 |
#define ARCH_PAGE_INCLUDE |
| 35 |
|
| 36 |
#include "arch0arch.h" |
| 37 |
#include "buf0buf.h" |
| 38 |
|
| 39 |
/** Archived page header file size (RESET Page) in number of blocks. */ |
| 40 |
constexpr uint ARCH_PAGE_FILE_NUM_RESET_PAGE = 1; |
| 41 |
|
| 42 |
/** Archived file header size. No file header for this version. */ |
| 43 |
constexpr uint ARCH_PAGE_FILE_HDR_SIZE = |
| 44 |
ARCH_PAGE_FILE_NUM_RESET_PAGE * ARCH_PAGE_BLK_SIZE; |
| 45 |
|
| 46 |
/** @name Page Archive doublewrite buffer file name prefix and constant length |
| 47 |
parameters. @{ */ |
| 48 |
|
| 49 |
/** Archive doublewrite buffer directory prefix */ |
| 50 |
constexpr char ARCH_DBLWR_DIR[] = "ib_dblwr"; |
| 51 |
|
| 52 |
/** Archive doublewrite buffer file prefix */ |
| 53 |
constexpr char ARCH_DBLWR_FILE[] = "dblwr_"; |
| 54 |
|
| 55 |
/** File name for the active file which indicates whether a group is active or |
| 56 |
not. */ |
| 57 |
constexpr char ARCH_PAGE_GROUP_ACTIVE_FILE_NAME[] = "active"; |
| 58 |
|
| 59 |
/** Archive doublewrite buffer number of files */ |
| 60 |
constexpr uint ARCH_DBLWR_NUM_FILES = 1; |
| 61 |
|
| 62 |
/** Archive doublewrite buffer file capacity in no. of blocks */ |
| 63 |
constexpr uint ARCH_DBLWR_FILE_CAPACITY = 3; |
| 64 |
|
| 65 |
/** @} */ |
| 66 |
|
| 67 |
/** @name Archive block header elements |
| 68 |
@{ */ |
| 69 |
|
| 70 |
/** Block Header: Version is in first 1 byte. */ |
| 71 |
constexpr uint ARCH_PAGE_BLK_HEADER_VERSION_OFFSET = 0; |
| 72 |
|
| 73 |
/** Block Header: Block Type is in next 1 byte. */ |
| 74 |
constexpr uint ARCH_PAGE_BLK_HEADER_TYPE_OFFSET = 1; |
| 75 |
|
| 76 |
/** Block Header: Checksum is in next 4 bytes. */ |
| 77 |
constexpr uint ARCH_PAGE_BLK_HEADER_CHECKSUM_OFFSET = 2; |
| 78 |
|
| 79 |
/** Block Header: Data length is in next 2 bytes. */ |
| 80 |
constexpr uint ARCH_PAGE_BLK_HEADER_DATA_LEN_OFFSET = 6; |
| 81 |
|
| 82 |
/** Block Header: Stop LSN is in next 8 bytes */ |
| 83 |
constexpr uint ARCH_PAGE_BLK_HEADER_STOP_LSN_OFFSET = 8; |
| 84 |
|
| 85 |
/** Block Header: Reset LSN is in next 8 bytes */ |
| 86 |
constexpr uint ARCH_PAGE_BLK_HEADER_RESET_LSN_OFFSET = 16; |
| 87 |
|
| 88 |
/** Block Header: Block number is in next 8 bytes */ |
| 89 |
constexpr uint ARCH_PAGE_BLK_HEADER_NUMBER_OFFSET = 24; |
| 90 |
|
| 91 |
/** Block Header: Total length. |
| 92 |
Keep header length in multiple of #ARCH_BLK_PAGE_ID_SIZE */ |
| 93 |
constexpr uint ARCH_PAGE_BLK_HEADER_LENGTH = 32; |
| 94 |
|
| 95 |
/** @} */ |
| 96 |
|
| 97 |
/** @name Page Archive reset block elements size. |
| 98 |
@{ */ |
| 99 |
|
| 100 |
/** Serialized Reset ID: Reset LSN total size */ |
| 101 |
constexpr uint ARCH_PAGE_FILE_HEADER_RESET_LSN_SIZE = 8; |
| 102 |
|
| 103 |
/** Serialized Reset ID: Reset block number size */ |
| 104 |
constexpr uint ARCH_PAGE_FILE_HEADER_RESET_BLOCK_NUM_SIZE = 2; |
| 105 |
|
| 106 |
/** Serialized Reset ID: Reset block offset size */ |
| 107 |
constexpr uint ARCH_PAGE_FILE_HEADER_RESET_BLOCK_OFFSET_SIZE = 2; |
| 108 |
|
| 109 |
/** Serialized Reset ID: Reset position total size */ |
| 110 |
constexpr uint ARCH_PAGE_FILE_HEADER_RESET_POS_SIZE = |
| 111 |
ARCH_PAGE_FILE_HEADER_RESET_BLOCK_NUM_SIZE + |
| 112 |
ARCH_PAGE_FILE_HEADER_RESET_BLOCK_OFFSET_SIZE; |
| 113 |
|
| 114 |
/** @} */ |
| 115 |
|
| 116 |
/** @name Page Archive data block elements |
| 117 |
@{ */ |
| 118 |
|
| 119 |
/** Serialized page ID: tablespace ID in First 4 bytes */ |
| 120 |
constexpr uint ARCH_BLK_SPCE_ID_OFFSET = 0; |
| 121 |
|
| 122 |
/** Serialized page ID: Page number in next 4 bytes */ |
| 123 |
constexpr uint ARCH_BLK_PAGE_NO_OFFSET = 4; |
| 124 |
|
| 125 |
/** Serialized page ID: Total length */ |
| 126 |
constexpr uint ARCH_BLK_PAGE_ID_SIZE = 8; |
| 127 |
|
| 128 |
/** @} */ |
| 129 |
|
| 130 |
/** Number of memory blocks */ |
| 131 |
constexpr uint ARCH_PAGE_NUM_BLKS = 32; |
| 132 |
|
| 133 |
/** Archived file format version */ |
| 134 |
constexpr uint ARCH_PAGE_FILE_VERSION = 1; |
| 135 |
|
| 136 |
#ifdef UNIV_DEBUG |
| 137 |
/** Archived page file default size in number of blocks. */ |
| 138 |
extern uint ARCH_PAGE_FILE_CAPACITY; |
| 139 |
|
| 140 |
/** Archived page data file size (without header) in number of blocks. */ |
| 141 |
extern uint ARCH_PAGE_FILE_DATA_CAPACITY; |
| 142 |
#else |
| 143 |
/** Archived page file default size in number of blocks. */ |
| 144 |
constexpr uint ARCH_PAGE_FILE_CAPACITY = |
| 145 |
(ARCH_PAGE_BLK_SIZE - ARCH_PAGE_BLK_HEADER_LENGTH) / ARCH_BLK_PAGE_ID_SIZE; |
| 146 |
|
| 147 |
/** Archived page data file size (without header) in number of blocks. */ |
| 148 |
constexpr uint ARCH_PAGE_FILE_DATA_CAPACITY = |
| 149 |
ARCH_PAGE_FILE_CAPACITY - ARCH_PAGE_FILE_NUM_RESET_PAGE; |
| 150 |
#endif |
| 151 |
|
| 152 |
/** Threshold for page archive reset. Attach to current reset if the number of |
| 153 |
tracked pages between the reset request and the current reset is less than this |
| 154 |
threshold as we allow only one reset per data block. */ |
| 155 |
constexpr uint ARCH_PAGE_RESET_THRESHOLD = |
| 156 |
(ARCH_PAGE_BLK_SIZE - ARCH_PAGE_BLK_HEADER_LENGTH) / ARCH_BLK_PAGE_ID_SIZE; |
| 157 |
|
| 158 |
/** Callback for retrieving archived page IDs |
| 159 |
@param[in] ctx context passed by caller |
| 160 |
@param[in] buff buffer with page IDs |
| 161 |
@param[in] num_pages number of page IDs in buffer |
| 162 |
@return error code */ |
| 163 |
using Page_Arch_Cbk = int(void *ctx, byte *buff, uint num_pages); |
| 164 |
|
| 165 |
/** Callback function to check if we need to wait for flush archiver to flush |
| 166 |
more blocks */ |
| 167 |
using Page_Wait_Flush_Archiver_Cbk = std::function<bool(void)>; |
| 168 |
|
| 169 |
/** Dirty page archiver client context */ |
| 170 |
class Page_Arch_Client_Ctx { |
| 171 |
public: |
| 172 |
/** Constructor: Initialize elements |
| 173 |
@param[in] is_durable true if the client requires durability, else |
| 174 |
false */ |
| 175 |
Page_Arch_Client_Ctx(bool is_durable) : m_is_durable(is_durable) { |
| 176 |
m_start_pos.init(); |
| 177 |
m_stop_pos.init(); |
| 178 |
mutex_create(LATCH_ID_PAGE_ARCH_CLIENT, &m_mutex); |
| 179 |
} |
| 180 |
|
| 181 |
/** Destructor. */ |
| 182 |
~Page_Arch_Client_Ctx() { mutex_free(&m_mutex); } |
| 183 |
|
| 184 |
/** Start dirty page tracking and archiving |
| 185 |
@param[in] recovery true if the tracking is being started as part of |
| 186 |
recovery process |
| 187 |
@param[out] start_id fill the start lsn |
| 188 |
@return error code. */ |
| 189 |
int start(bool recovery, uint64_t *start_id); |
| 190 |
|
| 191 |
/** Stop dirty page tracking and archiving |
| 192 |
@param[out] stop_id fill the stop lsn |
| 193 |
@return error code. */ |
| 194 |
int stop(uint64_t *stop_id); |
| 195 |
|
| 196 |
/** Release archived data so that system can purge it */ |
| 197 |
void release(); |
| 198 |
|
| 199 |
/** Initialize context during recovery. |
| 200 |
@param[in] group Group which needs to be attached to the client |
| 201 |
@param[in] last_lsn last reset lsn |
| 202 |
@return error code. */ |
| 203 |
int init_during_recovery(Arch_Group *group, lsn_t last_lsn); |
| 204 |
|
| 205 |
/** Check if this client context is active. |
| 206 |
@return true if active, else false */ |
| 207 |
bool is_active() const { return (m_state == ARCH_CLIENT_STATE_STARTED); } |
| 208 |
|
| 209 |
/** Get archived page Ids. |
| 210 |
Attempt to read blocks directly from in memory buffer. If overwritten, |
| 211 |
copy from archived files. |
| 212 |
@param[in] cbk_func called repeatedly with page ID buffer |
| 213 |
@param[in] cbk_ctx callback function context |
| 214 |
@param[in,out] buff buffer to fill page IDs |
| 215 |
@param[in] buf_len buffer length in bytes |
| 216 |
@return error code */ |
| 217 |
int get_pages(Page_Arch_Cbk *cbk_func, void *cbk_ctx, byte *buff, |
| 218 |
uint buf_len); |
| 219 |
|
| 220 |
#ifdef UNIV_DEBUG |
| 221 |
/** Print information related to the archiver client for debugging purposes. |
| 222 |
*/ |
| 223 |
void print(); |
| 224 |
#endif |
| 225 |
|
| 226 |
/** Disable copy construction */ |
| 227 |
Page_Arch_Client_Ctx(Page_Arch_Client_Ctx const &) = delete; |
| 228 |
|
| 229 |
/** Disable assignment */ |
| 230 |
Page_Arch_Client_Ctx &operator=(Page_Arch_Client_Ctx const &) = delete; |
| 231 |
|
| 232 |
private: |
| 233 |
/** Acquire client archiver mutex. |
| 234 |
It synchronizes members on concurrent start and stop operations. */ |
| 235 |
void arch_client_mutex_enter() { mutex_enter(&m_mutex); } |
| 236 |
|
| 237 |
/** Release client archiver mutex */ |
| 238 |
void arch_client_mutex_exit() { mutex_exit(&m_mutex); } |
| 239 |
|
| 240 |
private: |
| 241 |
/** Page archiver client state */ |
| 242 |
Arch_Client_State m_state{ARCH_CLIENT_STATE_INIT}; |
| 243 |
|
| 244 |
/** Archive group the client is attached to */ |
| 245 |
Arch_Group *m_group{nullptr}; |
| 246 |
|
| 247 |
/** True if the client requires durablity */ |
| 248 |
bool m_is_durable; |
| 249 |
|
| 250 |
/** Start LSN for archived data */ |
| 251 |
lsn_t m_start_lsn{LSN_MAX}; |
| 252 |
|
| 253 |
/** Stop LSN for archived data */ |
| 254 |
lsn_t m_stop_lsn{LSN_MAX}; |
| 255 |
|
| 256 |
/** Reset LSN at the time of last reset. */ |
| 257 |
lsn_t m_last_reset_lsn{LSN_MAX}; |
| 258 |
|
| 259 |
/** Start position for client in archived file group */ |
| 260 |
Arch_Page_Pos m_start_pos; |
| 261 |
|
| 262 |
/** Stop position for client in archived file group */ |
| 263 |
Arch_Page_Pos m_stop_pos; |
| 264 |
|
| 265 |
/** Mutex protecting concurrent operation on data */ |
| 266 |
ib_mutex_t m_mutex; |
| 267 |
}; |
| 268 |
|
| 269 |
#endif /* ARCH_PAGE_INCLUDE */ |