MySQL 26.7.0
Source Code Documentation
log0files_capacity.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2019, 2026, 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_capacity.h
30
31 Redo log management of capacity.
32
33 *******************************************************/
34
35#ifndef log0files_capacity_h
36#define log0files_capacity_h
37
38/* Log_files_dict */
39#include "log0files_dict.h"
40
41/* log_t&, lsn_t */
42#include "log0types.h"
43
44/* os_offset_t */
45#include "os0file.h"
46
47/** Responsible for the redo log capacity computations. Computes size for the
48next log file that will be created. Tracks the redo resize operation when the
49innodb_redo_log_capacity gets changed. Computes maximum ages for dirty pages,
50which are then used by page cleaner coordinator.
51
52@remarks When downsize is started, the limits for ages are decreased, forcing
53page cleaners to flush more dirty pages then. File sizes for new redo files
54are adjusted accordingly, so they could always be effectively used to hold
55the whole existing redo log data (for the given current logical size). */
57 public:
58 /** Initialize on discovered set of redo log files (empty set if new redo
59 is being created).
60 @param[in] files in-memory dictionary of existing files
61 @param[in] current_logical_size current logical size of data in redo,
62 or 0 if new redo is being created
63 @param[in] current_checkpoint_age current checkpoint age */
64 void initialize(const Log_files_dict &files, lsn_t current_logical_size,
65 lsn_t current_checkpoint_age);
66
67 /** Updates all internal limits according to the provided parameters.
68 If there are any values outside this class, on which computations of
69 limits depend on, they should be explicitly provided here, except the
70 server variables (srv_thread_concurrency, srv_redo_log_capacity_used).
71 @param[in] files in-memory dictionary of files
72 @param[in] current_logical_size current logical size of data
73 in the redo log; it depends
74 directly on the oldest redo
75 log consumer
76 @param[in] current_checkpoint_age current checkpoint age */
77 void update(const Log_files_dict &files, lsn_t current_logical_size,
78 lsn_t current_checkpoint_age);
79
80 /** @return true iff resize-down is pending */
81 bool is_resizing_down() const;
82
83 /** Provides maximum limitation for space occupied on disk.
84 @note This value changes only during calls to @see update or @see initialize.
85 @return maximum allowed size on disk, in bytes */
87
88 /** If a redo downsize is in progress, it is the targeted value for the
89 current_physical_capacity() (is equal if there is no resize in progress).
90 It is set to srv_redo_log_capacity_used when @see update is called.
91 @note This value changes only during calls to @see update or @see initialize.
92 @return targeted physical capacity, in bytes */
94
95 /** Soft limit for logical capacity of the redo log. When the log writer
96 exceeds this limitation, all user threads are paused during log_free_check()
97 calls and message is emitted to the error log. The log writer can still
98 continue to write until it reaches the hard limit for logical capacity
99 (value returned by hard_logical_capacity()).
100 @note This value changes only during calls to @see update or @see initialize.
101 @return limitation for free space in the redo log for user threads */
103
104 /** Hard limit for logical capacity of the redo log. This limitation includes
105 "extra_writer_margin" that belongs to the log writer thread. The log writer
106 does not to exceed this limit. If space isn't reclaimed after 1 sec wait,
107 it writes only as much as possible or crashes the InnoDB.
108 @note This value changes only during calls to @see update or @see initialize.
109 @return limitation for free space in the redo log for the log writer thread */
111
112 /** Provides size of the next redo log file that will be created. The initial
113 value becomes set during a call to @see initialize. Since then, it changes
114 only when innodb_redo_log_capacity is changed, during a call to @see update.
115 @note Does not depend on whether the file actually might be created or not.
116 It is log_files_governor's responsibility not to exceed the physical capacity.
117 @remarks
118 The strategy used by the Log_files_capacity, guarantees that next redo log
119 file should always be possible to be created. That's because:
120 1. The next file size is always chosen as:
121 innodb_redo_log_capacity / LOG_N_FILES.
122 1. The logical capacity of the redo log is limited to:
123 (LOG_N_FILES - 2) / LOG_N_FILES * m_current_physical_capacity.
124 2. The m_current_physical_capacity is changed only after resize is finished,
125 and the resize is considered finished only when:
126 - all redo log files have size <= innodb_redo_log_capacity / LOG_N_FILES,
127 - and the logical size of the redo log can fit physical size of
128 LOG_N_FILES - 2 redo files, which guarantees that at most
129 LOG_N_FILES - 1 redo files will ever need to exist (consider scenario
130 in which oldest_lsn is at the very end of the oldest redo files and
131 newest_lsn is at the very beginning of the newest redo file if you
132 are curious why -2 is there instead of -1).
133 @return file size suggested for next log file to create */
135
136 /** Computes size of a next redo log file that would be chosen for a given
137 physical capacity.
138 @param[in] physical_capacity physical capacity assumed for computation
139 @return file size suggested for next log file to create */
140 static os_offset_t next_file_size(os_offset_t physical_capacity);
141
142 /** Provides margin which might be used ahead of the newest lsn to create
143 a next file earlier if needed (it will be created as unused redo file).
144 @note This value changes only during calls to @see update or @see initialize.
145 @return the maximum allowed margin ahead of the newest lsn to be reserved */
147
148 /** Computes margin which might be used ahead of the newest lsn to create
149 a next file earlier if needed (it will be created as unused redo file).
150 The computation is done for a given physical capacity.
151 @param[in] physical_capacity physical capacity assumed for computation
152 @return the maximum allowed margin ahead of the newest lsn to be reserved */
153 static lsn_t next_file_earlier_margin(os_offset_t physical_capacity);
154
155 /** Computes hard logical capacity, that corresponds to the provided
156 soft logical capacity of the redo log (@see soft_logical_capacity()).
157 @param[in] soft_logical_capacity logical capacity for user threads,
158 used in log_free_check() calls
159 @return hard logical capacity */
162
163 /** Computes soft logical capacity, that corresponds to the provided
164 hard logical capacity of the redo log (@see hard_logical_capacity()).
165 @param[in] hard_logical_capacity logical capacity for the log writer
166 @return soft logical capacity */
168
169 /** Computes hard logical capacity, that corresponds to the provided
170 physical capacity of the redo log (@see hard_logical_capacity()).
171 @param[in] physical_capacity physical capacity for the redo log
172 @return hard logical capacity */
174 os_offset_t physical_capacity);
175
176 /** Computes maximum age of dirty pages up to which there is no sync flush
177 enforced on page cleaners. This is a smaller value than soft logical capacity,
178 because sync flush must be started earlier than log_free_check() calls begin
179 to stop user threads.
180 @param[in] soft_logical_capacity logical capacity for user threads,
181 used in log_free_check() calls
182 @return maximum age of dirty pages before the sync flush is started */
185
186 /** Computes soft logical capacity, that corresponds to the provided
187 maximum age of dirty pages up to which there is no sync flush enforced
188 on page cleaners. This is a larger value than the provided maximum age,
189 because sync flush must be started earlier than log_free_check() calls
190 begin to stop user threads.
191 @param[in] adaptive_flush_max_age maximum age of dirty page without
192 sync flush started
193 @return soft logical capacity */
196
197 private:
198 /** @see m_exposed */
199 struct Exposed {
200 /** Value returned by @see soft_logical_capacity */
202
203 /** Value returned by @see hard_logical_capacity */
205 };
206
207 /** Cache for values returned by getters in this object, which otherwise
208 would need to be computed on-demand. These values do not have impact on
209 state updates of this object.
210 @note Updated only during calls to @see initialize and @see update. */
212
213 /** This is limitation for space on disk we are never allowed to exceed.
214 This is the guard of disk space - current size of all log files on disk
215 is always not greater than this value.
216 @note Updated only during calls to @see initialize and @see update. */
218
219 /** Goal we are trying to achieve for m_current_physical_capacity when
220 resize operation is in progress, else: equal to m_current_physical_capacity.
221 During startup (when srv_is_being_started is true) it stays equal to the
222 m_current_physical_capacity (which is then computed for discovered log files).
223 After startup, it's set to srv_redo_log_capacity_used by calls to
224 @see update. */
226
227 /** Current resize direction. When user decides to resize down the redo log,
228 it becomes Log_resize_mode::RESIZING_DOWN until the resize is finished or
229 user decides to stop it (providing other capacity). Note, that resize is not
230 started during startup (when srv_is_being_started is true).
231 @note Updated only during calls to @see initialize and @see update. */
233
234 /** Cancels current resize operation immediately.
235 @remarks If the innodb_redo_log_capacity is changed when there is a previous
236 redo resize in progress, the previous resize is first cancelled. */
237 void cancel_resize();
238
239 /** Updates m_target_physical_capacity (reading srv_redo_log_capacity_used)
240 and possibly starts a new downsize operation. Might also update:
241 m_resize_mode, m_current_physical_capacity. */
242 void update_target();
243
244 /** Checks if target of the resize is reached, with regards to the criteria
245 based on the current logical size of the redo.
246 @param[in] current_logical_size current logical size of the redo log
247 @return true iff the target is reached */
248 bool is_target_reached_for_logical_size(lsn_t current_logical_size) const;
249
250 /** Checks if target of the resize is reached, with regards to the criteria
251 based on the current physical size of existing log files (excludes unused).
252 @param[in] current_physical_size total size of existing redo log files,
253 excluding unused (spare) files
254 @return true iff the target is reached */
256 os_offset_t current_physical_size) const;
257
258 /** Checks if target of the resize is reached, with regards to the criteria
259 based on the largest existing redo file.
260 @param[in] files in-memory dictionary of existing files
261 @return true iff the target is reached */
263
264 /** Checks if target of the resize is reached.
265 @param[in] files in-memory dictionary of existing files
266 @param[in] current_logical_size current logical size of the redo log
267 @return true iff the target is reached */
269 lsn_t current_logical_size) const;
270
271 /** Checks if target of the resize is reached with regards to all criteria
272 and updates the m_resize_mode, m_current_physical_capacity when that happens
273 (marking the resize operation as finished).
274 @param[in] files in-memory dictionary of existing files
275 @param[in] current_logical_size current logical size of the redo log */
277 lsn_t current_logical_size);
278
279 /** Updates value of server status variable: innodb_redo_log_resize_status. */
281
282 /** Updates values of server status variables:
283 innodb_redo_log_capacity_resized, innodb_redo_log_logical_size,
284 innodb_redo_log_physical_size, innodb_redo_log_resize_status.
285 @param[in] files in-memory dictionary of existing files
286 @param[in] current_logical_size current logical size of the redo log */
287 void update_status_variables(const Log_files_dict &files,
288 lsn_t current_logical_size);
289
290 /** Updates cached and exposed values related to the logical redo capacity:
291 - @see m_soft_logical_capacity
292 - @see m_hard_logical_capacity
293 @param[in] current_logical_size current logical size of the redo log */
294 void update_exposed(lsn_t current_logical_size);
295
296 /** Computes suggested value for the current hard logical capacity.
297 @remarks This becomes non-trivial when the redo log is being resized down,
298 because this method is supposed to follow the checkpoint age then.
299 On the other hand, when the redo log is not being resized down, this method
300 computes the hard logical capacity by using simple math based on the current
301 physical capacity only (ie. ignoring the current checkpoint age).
302 @param[in] current_checkpoint_age current checkpoint age,
303 used only when resizing down
304 @return suggested value for current hard logical capacity */
305 lsn_t get_suggested_hard_logical_capacity(lsn_t current_checkpoint_age) const;
306};
307
308#endif /* !log0files_capacity_h */
Responsible for the redo log capacity computations.
Definition: log0files_capacity.h:56
void update_if_target_reached(const Log_files_dict &files, lsn_t current_logical_size)
Checks if target of the resize is reached with regards to all criteria and updates the m_resize_mode,...
Definition: log0files_capacity.cc:344
bool is_target_reached_for_physical_size(os_offset_t current_physical_size) const
Checks if target of the resize is reached, with regards to the criteria based on the current physical...
Definition: log0files_capacity.cc:315
lsn_t get_suggested_hard_logical_capacity(lsn_t current_checkpoint_age) const
Computes suggested value for the current hard logical capacity.
Definition: log0files_capacity.cc:428
lsn_t next_file_earlier_margin() const
Provides margin which might be used ahead of the newest lsn to create a next file earlier if needed (...
Definition: log0files_capacity.cc:500
bool is_target_reached_for_max_file_size(const Log_files_dict &files) const
Checks if target of the resize is reached, with regards to the criteria based on the largest existing...
Definition: log0files_capacity.cc:328
void update_exposed(lsn_t current_logical_size)
Updates cached and exposed values related to the logical redo capacity:
Definition: log0files_capacity.cc:448
static lsn_t hard_logical_capacity_for_physical(os_offset_t physical_capacity)
Computes hard logical capacity, that corresponds to the provided physical capacity of the redo log (.
Definition: log0files_capacity.cc:246
Log_resize_mode m_resize_mode
Current resize direction.
Definition: log0files_capacity.h:232
void initialize(const Log_files_dict &files, lsn_t current_logical_size, lsn_t current_checkpoint_age)
Initialize on discovered set of redo log files (empty set if new redo is being created).
Definition: log0files_capacity.cc:124
lsn_t soft_logical_capacity() const
Soft limit for logical capacity of the redo log.
Definition: log0files_capacity.cc:463
os_offset_t target_physical_capacity() const
If a redo downsize is in progress, it is the targeted value for the current_physical_capacity() (is e...
Definition: log0files_capacity.cc:471
Exposed m_exposed
Cache for values returned by getters in this object, which otherwise would need to be computed on-dem...
Definition: log0files_capacity.h:211
void update_status_variables(const Log_files_dict &files, lsn_t current_logical_size)
Updates values of server status variables: innodb_redo_log_capacity_resized, innodb_redo_log_logical_...
Definition: log0files_capacity.cc:362
bool is_resizing_down() const
Definition: log0files_capacity.cc:467
static lsn_t sync_flush_logical_capacity_for_soft(lsn_t soft_logical_capacity)
Computes maximum age of dirty pages up to which there is no sync flush enforced on page cleaners.
Definition: log0files_capacity.cc:420
static lsn_t soft_logical_capacity_for_hard(lsn_t hard_logical_capacity)
Computes soft logical capacity, that corresponds to the provided hard logical capacity of the redo lo...
Definition: log0files_capacity.cc:404
static lsn_t guess_soft_logical_capacity_for_sync_flush(lsn_t adaptive_flush_max_age)
Computes soft logical capacity, that corresponds to the provided maximum age of dirty pages up to whi...
Definition: log0files_capacity.cc:412
bool is_target_reached_for_logical_size(lsn_t current_logical_size) const
Checks if target of the resize is reached, with regards to the criteria based on the current logical ...
Definition: log0files_capacity.cc:308
void update_target()
Updates m_target_physical_capacity (reading srv_redo_log_capacity_used) and possibly starts a new dow...
Definition: log0files_capacity.cc:199
void update(const Log_files_dict &files, lsn_t current_logical_size, lsn_t current_checkpoint_age)
Updates all internal limits according to the provided parameters.
Definition: log0files_capacity.cc:163
static lsn_t guess_hard_logical_capacity_for_soft(lsn_t soft_logical_capacity)
Computes hard logical capacity, that corresponds to the provided soft logical capacity of the redo lo...
Definition: log0files_capacity.cc:396
bool is_target_reached_for_resizing_down(const Log_files_dict &files, lsn_t current_logical_size) const
Checks if target of the resize is reached.
Definition: log0files_capacity.cc:336
os_offset_t m_target_physical_capacity
Goal we are trying to achieve for m_current_physical_capacity when resize operation is in progress,...
Definition: log0files_capacity.h:225
os_offset_t current_physical_capacity() const
Provides maximum limitation for space occupied on disk.
Definition: log0files_capacity.cc:475
lsn_t hard_logical_capacity() const
Hard limit for logical capacity of the redo log.
Definition: log0files_capacity.cc:459
void cancel_resize()
Cancels current resize operation immediately.
Definition: log0files_capacity.cc:188
os_offset_t m_current_physical_capacity
This is limitation for space on disk we are never allowed to exceed.
Definition: log0files_capacity.h:217
void update_resize_status_variable()
Updates value of server status variable: innodb_redo_log_resize_status.
Definition: log0files_capacity.cc:379
os_offset_t next_file_size() const
Provides size of the next redo log file that will be created.
Definition: log0files_capacity.cc:479
In-memory dictionary of meta data of existing log files.
Definition: log0files_dict.h:57
static lsn_t adaptive_flush_max_age(lsn_t soft_logical_capacity)
Definition: log0chkp.cc:658
In-memory dictionary of log files (keeps their meta data).
Redo log basic types.
Log_resize_mode
Direction of resize operation.
Definition: log0types.h:193
@ NONE
No pending resize.
std::atomic< lsn_t > atomic_lsn_t
Alias for atomic based on lsn_t.
Definition: log0types.h:82
uint64_t lsn_t
Type used for all log sequence number storage and arithmetic.
Definition: log0types.h:63
The interface to the operating system file io.
uint64_t os_offset_t
File offset in bytes.
Definition: os0file.h:87
Definition: log0files_capacity.h:199
atomic_lsn_t m_hard_logical_capacity
Value returned by.
Definition: log0files_capacity.h:204
atomic_lsn_t m_soft_logical_capacity
Value returned by.
Definition: log0files_capacity.h:201