| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2019, 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 |
/**************************************************/ /** |
| 29 |
@file include/log0files_dict.h |
| 30 |
|
| 31 |
In-memory dictionary of log files (keeps their meta data). |
| 32 |
|
| 33 |
The dictionary is built by the @see log_files_find_and_analyze(). |
| 34 |
|
| 35 |
*******************************************************/ |
| 36 |
|
| 37 |
#ifndef log0files_dict_h |
| 38 |
#define log0files_dict_h |
| 39 |
|
| 40 |
/* std::for_each */ |
| 41 |
#include <algorithm> |
| 42 |
|
| 43 |
/* std::iterator */ |
| 44 |
#include <iterator> |
| 45 |
|
| 46 |
/* Log_file, Log_file_id, LOG_START_LSN */ |
| 47 |
#include "log0types.h" |
| 48 |
|
| 49 |
/* os_offset_t */ |
| 50 |
#include "os0file.h" |
| 51 |
|
| 52 |
/* ut::map */ |
| 53 |
#include "ut0ut.h" |
| 54 |
|
| 55 |
/** In-memory dictionary of meta data of existing log files. |
| 56 |
This is a plain data structure only. It has no dependency. */ |
| 57 |
class Log_files_dict { |
| 58 |
private: |
| 59 |
using Log_files_map = ut::map<Log_file_id, Log_file>; |
| 60 |
using Log_files_map_iterator = typename Log_files_map::const_iterator; |
| 61 |
|
| 62 |
public: |
| 63 |
class Const_iterator { |
| 64 |
public: |
| 65 |
using iterator_category = std::bidirectional_iterator_tag; |
| 66 |
using value_type = Log_file; |
| 67 |
using difference_type = std::ptrdiff_t; |
| 68 |
using pointer = const Log_file *; |
| 69 |
using reference = const Log_file &; |
| 70 |
|
| 71 |
explicit Const_iterator(Log_files_dict::Log_files_map_iterator it); |
| 72 |
const Log_file &operator*() const; |
| 73 |
const Log_file *operator->() const; |
| 74 |
Const_iterator &operator++(); |
| 75 |
Const_iterator operator++(int); |
| 76 |
Const_iterator &operator--(); |
| 77 |
Const_iterator operator--(int); |
| 78 |
bool operator==(const Const_iterator &rhs) const; |
| 79 |
bool operator!=(const Const_iterator &rhs) const; |
| 80 |
|
| 81 |
private: |
| 82 |
Log_files_dict::Log_files_map_iterator m_it; |
| 83 |
}; |
| 84 |
|
| 85 |
/** Constructs an empty dictionary. */ |
| 86 |
explicit Log_files_dict(Log_files_context &files_ctx); |
| 87 |
|
| 88 |
Log_files_dict(Log_files_dict &&other); |
| 89 |
Log_files_dict &operator=(Log_files_dict &&other); |
| 90 |
|
| 91 |
/** @return context within which files exist */ |
| 92 |
const Log_files_context &ctx() const; |
| 93 |
|
| 94 |
/** Searches for an existing log file, that contains the provided lsn. |
| 95 |
@param[in] lsn lsn to search for |
| 96 |
@retval iterator pointing to the log file if file has been found |
| 97 |
@retval end() if there was no file containing the provided lsn */ |
| 98 |
Const_iterator find(lsn_t lsn) const; |
| 99 |
|
| 100 |
/** Provides log file for the given file id. |
| 101 |
@param[in] file_id id of the log file |
| 102 |
@return pointer to the log file */ |
| 103 |
Const_iterator file(Log_file_id file_id) const; |
| 104 |
|
| 105 |
/** Clears the whole dictionary. */ |
| 106 |
void clear(); |
| 107 |
|
| 108 |
/** Removes the meta data about the given log file (which denotes |
| 109 |
the file does not exist anymore) from this data structure. |
| 110 |
@param[in] file_id id of the log file */ |
| 111 |
void erase(Log_file_id file_id); |
| 112 |
|
| 113 |
/** Add meta data for the existing log file. It asserts that the meta data |
| 114 |
for that file has not been added yet to this data structure. |
| 115 |
@see Log_file_handle::m_encryption_metadata |
| 116 |
@param[in] file_id id of the log file |
| 117 |
@param[in] size_in_bytes size of the log file (in bytes) |
| 118 |
@param[in] start_lsn lsn of the first data byte in file |
| 119 |
@param[in] full true iff file is marked as full |
| 120 |
@param[in] consumed true iff file is marked as consumed |
| 121 |
@param[in] encryption_metadata encryption metadata */ |
| 122 |
void add(Log_file_id file_id, os_offset_t size_in_bytes, lsn_t start_lsn, |
| 123 |
bool full, bool consumed, Encryption_metadata &encryption_metadata); |
| 124 |
|
| 125 |
/** Add meta data for the existing log file. It asserts that the meta data |
| 126 |
for that file has not been added yet to this data structure. |
| 127 |
@see Log_file_handle::m_encryption_metadata |
| 128 |
@param[in] file_id id of the log file |
| 129 |
@param[in] size_in_bytes size of the log file (in bytes) |
| 130 |
@param[in] start_lsn lsn of the first data byte in file |
| 131 |
@param[in] full true iff file is marked as full |
| 132 |
@param[in] encryption_metadata encryption metadata */ |
| 133 |
void add(Log_file_id file_id, os_offset_t size_in_bytes, lsn_t start_lsn, |
| 134 |
bool full, Encryption_metadata &encryption_metadata); |
| 135 |
|
| 136 |
/** Marks a given log file as consumed. |
| 137 |
@param[in] file_id id of the log file */ |
| 138 |
void set_consumed(Log_file_id file_id); |
| 139 |
|
| 140 |
/** Marks a given log file as full. |
| 141 |
@param[in] file_id id of the log file */ |
| 142 |
void set_full(Log_file_id file_id); |
| 143 |
|
| 144 |
/** Marks a given log file as incomplete (undo marking as full). |
| 145 |
@param[in] file_id id of the log file */ |
| 146 |
void set_incomplete(Log_file_id file_id); |
| 147 |
|
| 148 |
/** Changes size of the file. Updates m_end_lsn accordingly. |
| 149 |
@param[in] file_id id of the log file |
| 150 |
@param[in] new_size new size (expressed in bytes) */ |
| 151 |
void set_size(Log_file_id file_id, os_offset_t new_size); |
| 152 |
|
| 153 |
/** @return iterator to the first log file (with the smallest id) */ |
| 154 |
Const_iterator begin() const; |
| 155 |
|
| 156 |
/** @return iterator after the last log file */ |
| 157 |
Const_iterator end() const; |
| 158 |
|
| 159 |
/** @return true iff structure is empty */ |
| 160 |
bool empty() const; |
| 161 |
|
| 162 |
/** @return the oldest redo log file */ |
| 163 |
const Log_file &front() const; |
| 164 |
|
| 165 |
/** @return the newest redo log file */ |
| 166 |
const Log_file &back() const; |
| 167 |
|
| 168 |
private: |
| 169 |
/** Context within which log files exist. */ |
| 170 |
const Log_files_context &m_files_ctx; |
| 171 |
|
| 172 |
/** Meta information about each existing redo log file. */ |
| 173 |
Log_files_map m_files_by_id; |
| 174 |
}; |
| 175 |
|
| 176 |
template <typename F> |
| 177 |
void log_files_for_each(const Log_files_dict &files, F functor) { |
| 178 |
std::for_each(files.begin(), files.end(), functor); |
| 179 |
} |
| 180 |
|
| 181 |
/** Calls the given functor for each of existing log files on path |
| 182 |
from a file containing start_lsn to a file containing end_lsn - 1. |
| 183 |
Asserts that such a path exists (going through existing log files). |
| 184 |
When the range is empty (start_lsn >= end_lsn), no file is visited. |
| 185 |
@param[in] files in-memory dictionary of log files |
| 186 |
@param[in] start_lsn path starts at file reported by find(start_lsn) |
| 187 |
@param[in] end_lsn path ends in file with m_end_lsn >= end_lsn |
| 188 |
@param[in] functor functor receiving a reference to Log_file */ |
| 189 |
template <typename F> |
| 190 |
void log_files_for_each(const Log_files_dict &files, lsn_t start_lsn, |
| 191 |
lsn_t end_lsn, F functor) { |
| 192 |
ut_a(start_lsn >= LOG_START_LSN); |
| 193 |
ut_a(start_lsn <= end_lsn); |
| 194 |
if (start_lsn == end_lsn) { |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
auto begin = files.find(start_lsn); |
| 199 |
ut_a(begin != files.end()); |
| 200 |
ut_a(begin->m_start_lsn <= start_lsn); |
| 201 |
|
| 202 |
auto end = files.find(end_lsn - 1); |
| 203 |
ut_a(end != files.end()); |
| 204 |
ut_a(end_lsn <= end->m_end_lsn); |
| 205 |
end++; |
| 206 |
|
| 207 |
ut_a(end == files.end() || end_lsn <= end->m_start_lsn); |
| 208 |
|
| 209 |
std::for_each(begin, end, functor); |
| 210 |
} |
| 211 |
|
| 212 |
/** Computes logical capacity for the given physical size of the redo log file. |
| 213 |
@param[in] file_size_in_bytes total size of file, expressed in bytes |
| 214 |
@param[out] lsn_capacity logical capacity of the file |
| 215 |
@retval true succeeded to compute the logical capacity |
| 216 |
@retval false params were invalid (file size was too small or too big) */ |
| 217 |
bool log_file_compute_logical_capacity(os_offset_t file_size_in_bytes, |
| 218 |
lsn_t &lsn_capacity); |
| 219 |
|
| 220 |
/** Computes end_lsn for the given: start_lsn and size of the redo log file. |
| 221 |
@param[in] start_lsn LSN of the first data byte within the file |
| 222 |
@param[in] file_size_in_bytes total size of file, expressed in bytes |
| 223 |
@param[out] end_lsn LSN after the last data byte within the file |
| 224 |
@retval true succeeded to compute end_lsn |
| 225 |
@retval false params were invalid */ |
| 226 |
bool log_file_compute_end_lsn(lsn_t start_lsn, os_offset_t file_size_in_bytes, |
| 227 |
lsn_t &end_lsn); |
| 228 |
|
| 229 |
/** Counts the total number of existing log files. |
| 230 |
@param[in] files in-memory dictionary of log files |
| 231 |
@return number of existing log files. */ |
| 232 |
size_t log_files_number_of_existing_files(const Log_files_dict &files); |
| 233 |
|
| 234 |
/** Counts the total number of existing and marked as consumed log files. |
| 235 |
@param[in] files in-memory dictionary of log files |
| 236 |
@return number of existing and marked as consumed log files. */ |
| 237 |
size_t log_files_number_of_consumed_files(const Log_files_dict &files); |
| 238 |
|
| 239 |
/** Computes the total size of the existing log files (sum of sizes). |
| 240 |
@note Each file starts with LOG_FILE_HDR_SIZE bytes of headers. |
| 241 |
@param[in] files in-memory dictionary of log files |
| 242 |
@return computed total size of existing log files */ |
| 243 |
os_offset_t log_files_size_of_existing_files(const Log_files_dict &files); |
| 244 |
|
| 245 |
/** Computes the total capacity of the existing log files (sum of capacities). |
| 246 |
@note Capacity of a file is smaller than size of the file by LOG_FILE_HDR_SIZE. |
| 247 |
@param[in] files in-memory dictionary of log files |
| 248 |
@return computed total capacity of existing log files */ |
| 249 |
lsn_t log_files_capacity_of_existing_files(const Log_files_dict &files); |
| 250 |
|
| 251 |
/** Finds the largest existing log file (with the largest m_size_in_bytes). |
| 252 |
@param[in] files in-memory dictionary of log files |
| 253 |
@return the largest file or files.end() if there is no file at all */ |
| 254 |
Log_files_dict::Const_iterator log_files_find_largest( |
| 255 |
const Log_files_dict &files); |
| 256 |
|
| 257 |
#endif /* !log0files_dict_h */ |