MySQL 9.0.0
Source Code Documentation
log0files_dict.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2019, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 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.
56This is a plain data structure only. It has no dependency. */
58 private:
60 using Log_files_map_iterator = typename Log_files_map::const_iterator;
61
62 public:
64 public:
65 using iterator_category = std::bidirectional_iterator_tag;
67 using difference_type = std::ptrdiff_t;
68 using pointer = const Log_file *;
69 using reference = const Log_file &;
70
72 const Log_file &operator*() const;
73 const Log_file *operator->() const;
78 bool operator==(const Const_iterator &rhs) const;
79 bool operator!=(const Const_iterator &rhs) const;
80
81 private:
83 };
84
85 /** Constructs an empty dictionary. */
86 explicit Log_files_dict(Log_files_context &files_ctx);
87
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 */
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. */
171
172 /** Meta information about each existing redo log file. */
174};
175
176template <typename F>
177void 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
182from a file containing start_lsn to a file containing end_lsn - 1.
183Asserts that such a path exists (going through existing log files).
184When 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 */
189template <typename F>
190void 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) */
217bool 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 */
226bool 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. */
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. */
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 */
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 */
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 */
255 const Log_files_dict &files);
256
257#endif /* !log0files_dict_h */
Definition: log0files_dict.h:63
bool operator==(const Const_iterator &rhs) const
Definition: log0files_dict.cc:223
Const_iterator & operator++()
Definition: log0files_dict.cc:201
std::ptrdiff_t difference_type
Definition: log0files_dict.h:67
const Log_file * operator->() const
Definition: log0files_dict.cc:197
Log_files_dict::Log_files_map_iterator m_it
Definition: log0files_dict.h:82
Const_iterator & operator--()
Definition: log0files_dict.cc:212
std::bidirectional_iterator_tag iterator_category
Definition: log0files_dict.h:65
Const_iterator(Log_files_dict::Log_files_map_iterator it)
Definition: log0files_dict.cc:189
bool operator!=(const Const_iterator &rhs) const
Definition: log0files_dict.cc:228
const Log_file & operator*() const
Definition: log0files_dict.cc:193
In-memory dictionary of meta data of existing log files.
Definition: log0files_dict.h:57
Const_iterator begin() const
Definition: log0files_dict.cc:233
const Log_files_context & ctx() const
Definition: log0files_dict.cc:67
const Log_file & front() const
Definition: log0files_dict.cc:169
typename Log_files_map::const_iterator Log_files_map_iterator
Definition: log0files_dict.h:60
Log_files_map m_files_by_id
Meta information about each existing redo log file.
Definition: log0files_dict.h:173
void set_size(Log_file_id file_id, os_offset_t new_size)
Changes size of the file.
Definition: log0files_dict.cc:152
Const_iterator file(Log_file_id file_id) const
Provides log file for the given file id.
Definition: log0files_dict.cc:99
void clear()
Clears the whole dictionary.
Definition: log0files_dict.cc:69
ut::map< Log_file_id, Log_file > Log_files_map
Definition: log0files_dict.h:59
Log_files_dict(Log_files_context &files_ctx)
Constructs an empty dictionary.
Definition: log0files_dict.cc:54
void erase(Log_file_id file_id)
Removes the meta data about the given log file (which denotes the file does not exist anymore) from t...
Definition: log0files_dict.cc:71
const Log_file & back() const
Definition: log0files_dict.cc:174
Log_files_dict & operator=(Log_files_dict &&other)
Definition: log0files_dict.cc:61
Const_iterator end() const
Definition: log0files_dict.cc:237
Const_iterator find(lsn_t lsn) const
Searches for an existing log file, that contains the provided lsn.
Definition: log0files_dict.cc:80
void set_full(Log_file_id file_id)
Marks a given log file as full.
Definition: log0files_dict.cc:140
void add(Log_file_id file_id, os_offset_t size_in_bytes, lsn_t start_lsn, bool full, bool consumed, Encryption_metadata &encryption_metadata)
Add meta data for the existing log file.
Definition: log0files_dict.cc:109
bool empty() const
Definition: log0files_dict.cc:167
void set_consumed(Log_file_id file_id)
Marks a given log file as consumed.
Definition: log0files_dict.cc:134
void set_incomplete(Log_file_id file_id)
Marks a given log file as incomplete (undo marking as full).
Definition: log0files_dict.cc:146
const Log_files_context & m_files_ctx
Context within which log files exist.
Definition: log0files_dict.h:170
constexpr lsn_t LOG_START_LSN
The counting of lsn's starts from this value: this must be non-zero.
Definition: log0constants.h:153
bool log_file_compute_logical_capacity(os_offset_t file_size_in_bytes, lsn_t &lsn_capacity)
Computes logical capacity for the given physical size of the redo log file.
Definition: log0files_dict.cc:251
size_t log_files_number_of_existing_files(const Log_files_dict &files)
Counts the total number of existing log files.
Definition: log0files_dict.cc:279
Log_files_dict::Const_iterator log_files_find_largest(const Log_files_dict &files)
Finds the largest existing log file (with the largest m_size_in_bytes).
Definition: log0files_dict.cc:306
os_offset_t log_files_size_of_existing_files(const Log_files_dict &files)
Computes the total size of the existing log files (sum of sizes).
Definition: log0files_dict.cc:288
void log_files_for_each(const Log_files_dict &files, F functor)
Definition: log0files_dict.h:177
bool log_file_compute_end_lsn(lsn_t start_lsn, os_offset_t file_size_in_bytes, lsn_t &end_lsn)
Computes end_lsn for the given: start_lsn and size of the redo log file.
Definition: log0files_dict.cc:261
size_t log_files_number_of_consumed_files(const Log_files_dict &files)
Counts the total number of existing and marked as consumed log files.
Definition: log0files_dict.cc:283
lsn_t log_files_capacity_of_existing_files(const Log_files_dict &files)
Computes the total capacity of the existing log files (sum of capacities).
Definition: log0files_dict.cc:295
Redo log basic types.
size_t Log_file_id
Log file id (0 for ib_redo0)
Definition: log0types.h:66
uint64_t lsn_t
Type used for all log sequence number storage and arithmetic.
Definition: log0types.h:63
void for_each(const Shards< COUNT > &shards, Function &&f) noexcept
Iterate over the shards.
Definition: ut0counter.h:323
const char * begin(const char *const c)
Definition: base64.h:44
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
std::map< Key, Value, Compare, ut::allocator< std::pair< const Key, Value > > > map
Specialization of map which uses ut_allocator.
Definition: ut0new.h:2893
The interface to the operating system file io.
uint64_t os_offset_t
File offset in bytes.
Definition: os0file.h:87
Encryption metadata.
Definition: os0enc.h:445
Meta information about single log file.
Definition: log0types.h:454
Configures path to the root directory, where redo subdirectory might be located (or redo log files if...
Definition: log0types.h:204
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:93
Various utilities.
static uint64_t lsn
Definition: xcom_base.cc:446