MySQL 8.0.31
Source Code Documentation
log0files_dict.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2019, 2022, 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 also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/**************************************************/ /**
28 @file include/log0files_dict.h
29
30 In-memory dictionary of log files (keeps their meta data).
31
32 The dictionary is built by the @see log_files_find_and_analyze().
33
34 *******************************************************/
35
36#ifndef log0files_dict_h
37#define log0files_dict_h
38
39/* std::for_each */
40#include <algorithm>
41
42/* std::iterator */
43#include <iterator>
44
45/* Log_file, Log_file_id, LOG_START_LSN */
46#include "log0types.h"
47
48/* os_offset_t */
49#include "os0file.h"
50
51/* ut::map */
52#include "ut0ut.h"
53
54/** In-memory dictionary of meta data of existing log files.
55This is a plain data structure only. It has no dependency. */
57 private:
59 using Log_files_map_iterator = typename Log_files_map::const_iterator;
60
61 public:
63 public:
64 using iterator_category = std::bidirectional_iterator_tag;
66 using difference_type = std::ptrdiff_t;
67 using pointer = const Log_file *;
68 using reference = const Log_file &;
69
71 const Log_file &operator*() const;
72 const Log_file *operator->() const;
77 bool operator==(const Const_iterator &rhs) const;
78 bool operator!=(const Const_iterator &rhs) const;
79
80 private:
82 };
83
84 /** Constructs an empty dictionary. */
85 explicit Log_files_dict(Log_files_context &files_ctx);
86
89
90 /** @return context within which files exist */
91 const Log_files_context &ctx() const;
92
93 /** Searches for an existing log file, that contains the provided lsn.
94 @param[in] lsn lsn to search for
95 @retval iterator pointing to the log file if file has been found
96 @retval end() if there was no file containing the provided lsn */
98
99 /** Provides log file for the given file id.
100 @param[in] file_id id of the log file
101 @return pointer to the log file */
102 Const_iterator file(Log_file_id file_id) const;
103
104 /** Clears the whole dictionary. */
105 void clear();
106
107 /** Removes the meta data about the given log file (which denotes
108 the file does not exist anymore) from this data structure.
109 @param[in] file_id id of the log file */
110 void erase(Log_file_id file_id);
111
112 /** Add meta data for the existing log file. It asserts that the meta data
113 for that file has not been added yet to this data structure.
114 @see Log_file_handle::m_encryption_metadata
115 @param[in] file_id id of the log file
116 @param[in] size_in_bytes size of the log file (in bytes)
117 @param[in] start_lsn lsn of the first data byte in file
118 @param[in] full true iff file is marked as full
119 @param[in] consumed true iff file is marked as consumed
120 @param[in] encryption_metadata encryption metadata */
121 void add(Log_file_id file_id, os_offset_t size_in_bytes, lsn_t start_lsn,
122 bool full, bool consumed, Encryption_metadata &encryption_metadata);
123
124 /** Add meta data for the existing log file. It asserts that the meta data
125 for that file has not been added yet to this data structure.
126 @see Log_file_handle::m_encryption_metadata
127 @param[in] file_id id of the log file
128 @param[in] size_in_bytes size of the log file (in bytes)
129 @param[in] start_lsn lsn of the first data byte in file
130 @param[in] full true iff file is marked as full
131 @param[in] encryption_metadata encryption metadata */
132 void add(Log_file_id file_id, os_offset_t size_in_bytes, lsn_t start_lsn,
133 bool full, Encryption_metadata &encryption_metadata);
134
135 /** Marks a given log file as consumed.
136 @param[in] file_id id of the log file */
137 void set_consumed(Log_file_id file_id);
138
139 /** Marks a given log file as full.
140 @param[in] file_id id of the log file */
141 void set_full(Log_file_id file_id);
142
143 /** Marks a given log file as incomplete (undo marking as full).
144 @param[in] file_id id of the log file */
145 void set_incomplete(Log_file_id file_id);
146
147 /** Changes size of the file. Updates m_end_lsn accordingly.
148 @param[in] file_id id of the log file
149 @param[in] new_size new size (expressed in bytes) */
150 void set_size(Log_file_id file_id, os_offset_t new_size);
151
152 /** @return iterator to the first log file (with the smallest id) */
153 Const_iterator begin() const;
154
155 /** @return iterator after the last log file */
156 Const_iterator end() const;
157
158 /** @return true iff structure is empty */
159 bool empty() const;
160
161 /** @return the oldest redo log file */
162 const Log_file &front() const;
163
164 /** @return the newest redo log file */
165 const Log_file &back() const;
166
167 private:
168 /** Context within which log files exist. */
170
171 /** Meta information about each existing redo log file. */
173};
174
175template <typename F>
176void log_files_for_each(const Log_files_dict &files, F functor) {
177 std::for_each(files.begin(), files.end(), functor);
178}
179
180/** Calls the given functor for each of existing log files on path
181from a file containing start_lsn to a file containing end_lsn - 1.
182Asserts that such a path exists (going through existing log files).
183When the range is empty (start_lsn >= end_lsn), no file is visited.
184@param[in] files in-memory dictionary of log files
185@param[in] start_lsn path starts at file reported by find(start_lsn)
186@param[in] end_lsn path ends in file with m_end_lsn >= end_lsn
187@param[in] functor functor receiving a reference to Log_file */
188template <typename F>
189void log_files_for_each(const Log_files_dict &files, lsn_t start_lsn,
190 lsn_t end_lsn, F functor) {
191 ut_a(start_lsn >= LOG_START_LSN);
192 ut_a(start_lsn <= end_lsn);
193 if (start_lsn == end_lsn) {
194 return;
195 }
196
197 auto begin = files.find(start_lsn);
198 ut_a(begin != files.end());
199 ut_a(begin->m_start_lsn <= start_lsn);
200
201 auto end = files.find(end_lsn - 1);
202 ut_a(end != files.end());
203 ut_a(end_lsn <= end->m_end_lsn);
204 end++;
205
206 ut_a(end == files.end() || end_lsn <= end->m_start_lsn);
207
208 std::for_each(begin, end, functor);
209}
210
211/** Computes logical capacity for the given physical size of the redo log file.
212@param[in] file_size_in_bytes total size of file, expressed in bytes
213@param[out] lsn_capacity logical capacity of the file
214@retval true succeeded to compute the logical capacity
215@retval false params were invalid (file size was too small or too big) */
216bool log_file_compute_logical_capacity(os_offset_t file_size_in_bytes,
217 lsn_t &lsn_capacity);
218
219/** Computes end_lsn for the given: start_lsn and size of the redo log file.
220@param[in] start_lsn LSN of the first data byte within the file
221@param[in] file_size_in_bytes total size of file, expressed in bytes
222@param[out] end_lsn LSN after the last data byte within the file
223@retval true succeeded to compute end_lsn
224@retval false params were invalid */
225bool log_file_compute_end_lsn(lsn_t start_lsn, os_offset_t file_size_in_bytes,
226 lsn_t &end_lsn);
227
228/** Counts the total number of existing log files.
229@param[in] files in-memory dictionary of log files
230@return number of existing log files. */
232
233/** Counts the total number of existing and marked as consumed log files.
234@param[in] files in-memory dictionary of log files
235@return number of existing and marked as consumed log files. */
237
238/** Computes the total size of the existing log files (sum of sizes).
239@note Each file starts with LOG_FILE_HDR_SIZE bytes of headers.
240@param[in] files in-memory dictionary of log files
241@return computed total size of existing log files */
243
244/** Computes the total capacity of the existing log files (sum of capacities).
245@note Capacity of a file is smaller than size of the file by LOG_FILE_HDR_SIZE.
246@param[in] files in-memory dictionary of log files
247@return computed total capacity of existing log files */
249
250/** Finds the largest existing log file (with the largest m_size_in_bytes).
251@param[in] files in-memory dictionary of log files
252@return the largest file or files.end() if there is no file at all */
254 const Log_files_dict &files);
255
256#endif /* !log0files_dict_h */
Definition: log0files_dict.h:62
bool operator==(const Const_iterator &rhs) const
Definition: log0files_dict.cc:222
Const_iterator & operator++()
Definition: log0files_dict.cc:200
std::ptrdiff_t difference_type
Definition: log0files_dict.h:66
const Log_file * operator->() const
Definition: log0files_dict.cc:196
Log_files_dict::Log_files_map_iterator m_it
Definition: log0files_dict.h:81
Const_iterator & operator--()
Definition: log0files_dict.cc:211
std::bidirectional_iterator_tag iterator_category
Definition: log0files_dict.h:64
Const_iterator(Log_files_dict::Log_files_map_iterator it)
Definition: log0files_dict.cc:188
bool operator!=(const Const_iterator &rhs) const
Definition: log0files_dict.cc:227
const Log_file & operator*() const
Definition: log0files_dict.cc:192
In-memory dictionary of meta data of existing log files.
Definition: log0files_dict.h:56
Const_iterator begin() const
Definition: log0files_dict.cc:232
const Log_files_context & ctx() const
Definition: log0files_dict.cc:66
const Log_file & front() const
Definition: log0files_dict.cc:168
typename Log_files_map::const_iterator Log_files_map_iterator
Definition: log0files_dict.h:59
Log_files_map m_files_by_id
Meta information about each existing redo log file.
Definition: log0files_dict.h:172
void set_size(Log_file_id file_id, os_offset_t new_size)
Changes size of the file.
Definition: log0files_dict.cc:151
Const_iterator file(Log_file_id file_id) const
Provides log file for the given file id.
Definition: log0files_dict.cc:98
void clear()
Clears the whole dictionary.
Definition: log0files_dict.cc:68
ut::map< Log_file_id, Log_file > Log_files_map
Definition: log0files_dict.h:58
Log_files_dict(Log_files_context &files_ctx)
Constructs an empty dictionary.
Definition: log0files_dict.cc:53
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:70
const Log_file & back() const
Definition: log0files_dict.cc:173
Log_files_dict & operator=(Log_files_dict &&other)
Definition: log0files_dict.cc:60
Const_iterator end() const
Definition: log0files_dict.cc:236
Const_iterator find(lsn_t lsn) const
Searches for an existing log file, that contains the provided lsn.
Definition: log0files_dict.cc:79
void set_full(Log_file_id file_id)
Marks a given log file as full.
Definition: log0files_dict.cc:139
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:108
bool empty() const
Definition: log0files_dict.cc:166
void set_consumed(Log_file_id file_id)
Marks a given log file as consumed.
Definition: log0files_dict.cc:133
void set_incomplete(Log_file_id file_id)
Marks a given log file as incomplete (undo marking as full).
Definition: log0files_dict.cc:145
const Log_files_context & m_files_ctx
Context within which log files exist.
Definition: log0files_dict.h:169
constexpr lsn_t LOG_START_LSN
The counting of lsn's starts from this value: this must be non-zero.
Definition: log0constants.h:146
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:250
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:278
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:305
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:287
void log_files_for_each(const Log_files_dict &files, F functor)
Definition: log0files_dict.h:176
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:260
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:282
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:294
Redo log basic types.
size_t Log_file_id
Log file id (0 for ib_redo0)
Definition: log0types.h:65
uint64_t lsn_t
Type used for all log sequence number storage and arithmetic.
Definition: log0types.h:62
void for_each(const Shards< COUNT > &shards, Function &&f) noexcept
Iterate over the shards.
Definition: ut0counter.h:322
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:191
std::map< Key, Value, Compare, ut::allocator< std::pair< const Key, Value > > > map
Specialization of map which uses ut_allocator.
Definition: ut0new.h:2873
The interface to the operating system file io.
uint64_t os_offset_t
File offset in bytes.
Definition: os0file.h:83
Encryption metadata.
Definition: os0enc.h:452
Meta information about single log file.
Definition: log0types.h:459
Configures path to the root directory, where redo subdirectory might be located (or redo log files if...
Definition: log0types.h:203
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:56
Various utilities.
static uint64_t lsn
Definition: xcom_base.cc:442