MySQL 8.0.39
Source Code Documentation
log0files_capacity.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_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 /** Once checkpoint age exceeds this value, the flushing of pages starts to
113 be adaptive. The adaptive page flushing is becoming more and more aggressive
114 in the following range: adaptive_flush_min_age()..adaptive_flush_max_age().
115 @note This value changes only during calls to @see update or @see initialize.
116 @note Note that it must hold:
117 adaptive_flush_min_age() < adaptive_flush_max_age() <=
118 soft_logical_capacity().
119 @remarks
120 The diagram below shows how flushing / checkpointing becomes more aggressive
121 when the age of the oldest modified page gets increased:
122
123 adaptive_flush_min_age adaptive_flush_max_age aggressive_checkpoint_min_age
124 | | |
125 -------!------------------------!---------------------!----------------->age
126 regular adaptive flushing aggressive flushing aggr. checkpoints
127 @return limitation to start adaptive flushing */
129
130 /** Once checkpoint age exceeds that value, the flushing of pages is the most
131 aggressive possible since then. For more details @see adaptive_flush_min_age.
132 @note This value changes only during calls to @see update or @see initialize.
133 @return limitation to start furious flushing */
135
136 /** Once checkpoint age exceeds that value, the log checkpointer thread keeps
137 writing checkpoints aggressively (whatever the progress of last_checkpoint_lsn
138 would it make). Before that happens, checkpoints could be written periodically
139 (for more details @see adaptive_flush_min_age).
140 @note This value changes only during calls to @see update or @see initialize.
141 @note It holds: adaptive_flush_max_age() < aggressive_checkpoint_min_age().
142 @return limitation to start aggressive checkpointing */
144
145 /** Provides size of the next redo log file that will be created. The initial
146 value becomes set during a call to @see initialize. Since then, it changes
147 only when innodb_redo_log_capacity is changed, during a call to @see update.
148 @note Does not depend on whether the file actually might be created or not.
149 It is log_files_governor's responsibility not to exceed the physical capacity.
150 @remarks
151 The strategy used by the Log_files_capacity, guarantees that next redo log
152 file should always be possible to be created. That's because:
153 1. The next file size is always chosen as:
154 innodb_redo_log_capacity / LOG_N_FILES.
155 1. The logical capacity of the redo log is limited to:
156 (LOG_N_FILES - 2) / LOG_N_FILES * m_current_physical_capacity.
157 2. The m_current_physical_capacity is changed only after resize is finished,
158 and the resize is considered finished only when:
159 - all redo log files have size <= innodb_redo_log_capacity / LOG_N_FILES,
160 - and the logical size of the redo log can fit physical size of
161 LOG_N_FILES - 2 redo files, which guarantees that at most
162 LOG_N_FILES - 1 redo files will ever need to exist (consider scenario
163 in which oldest_lsn is at the very end of the oldest redo files and
164 newest_lsn is at the very beginning of the newest redo file if you
165 are curious why -2 is there instead of -1).
166 @return file size suggested for next log file to create */
168
169 /** Computes size of a next redo log file that would be chosen for a given
170 physical capacity.
171 @param[in] physical_capacity physical capacity assumed for computation
172 @return file size suggested for next log file to create */
173 static os_offset_t next_file_size(os_offset_t physical_capacity);
174
175 /** Provides margin which might be used ahead of the newest lsn to create
176 a next file earlier if needed (it will be created as unused redo file).
177 @note This value changes only during calls to @see update or @see initialize.
178 @return the maximum allowed margin ahead of the newest lsn to be reserved */
180
181 /** Computes margin which might be used ahead of the newest lsn to create
182 a next file earlier if needed (it will be created as unused redo file).
183 The computation is done for a given physical capacity.
184 @param[in] physical_capacity physical capacity assumed for computation
185 @return the maximum allowed margin ahead of the newest lsn to be reserved */
186 static lsn_t next_file_earlier_margin(os_offset_t physical_capacity);
187
188 /** Computes hard logical capacity, that corresponds to the provided
189 soft logical capacity of the redo log (@see soft_logical_capacity()).
190 @param[in] soft_logical_capacity logical capacity for user threads,
191 used in log_free_check() calls
192 @return hard logical capacity */
195
196 /** Computes soft logical capacity, that corresponds to the provided
197 hard logical capacity of the redo log (@see hard_logical_capacity()).
198 @param[in] hard_logical_capacity logical capacity for the log writer
199 @return soft logical capacity */
201
202 /** Computes hard logical capacity, that corresponds to the provided
203 physical capacity of the redo log (@see hard_logical_capacity()).
204 @param[in] physical_capacity physical capacity for the redo log
205 @return hard logical capacity */
207 os_offset_t physical_capacity);
208
209 /** Computes maximum age of dirty pages up to which there is no sync flush
210 enforced on page cleaners. This is a smaller value than soft logical capacity,
211 because sync flush must be started earlier than log_free_check() calls begin
212 to stop user threads.
213 @param[in] soft_logical_capacity logical capacity for user threads,
214 used in log_free_check() calls
215 @return maximum age of dirty pages before the sync flush is started */
218
219 /** Computes soft logical capacity, that corresponds to the provided
220 maximum age of dirty pages up to which there is no sync flush enforced
221 on page cleaners. This is a larger value than the provided maximum age,
222 because sync flush must be started earlier than log_free_check() calls
223 begin to stop user threads.
224 @param[in] adaptive_flush_max_age maximum age of dirty page without
225 sync flush started
226 @return soft logical capacity */
229
230 private:
231 /** @see m_exposed */
232 struct Exposed {
233 /** Value returned by @see soft_logical_capacity */
235
236 /** Value returned by @see hard_logical_capacity */
238
239 /** Value returned by @see adaptive_flush_min_age */
241
242 /** Value returned by @see adaptive_flush_max_age */
244
245 /** Value returned by @see aggressive_checkpoint_min_age */
247 };
248
249 /** Cache for values returned by getters in this object, which otherwise
250 would need to be computed on-demand. These values do not have impact on
251 state updates of this object.
252 @note Updated only during calls to @see initialize and @see update. */
254
255 /** This is limitation for space on disk we are never allowed to exceed.
256 This is the guard of disk space - current size of all log files on disk
257 is always not greater than this value.
258 @note Updated only during calls to @see initialize and @see update. */
260
261 /** Goal we are trying to achieve for m_current_physical_capacity when
262 resize operation is in progress, else: equal to m_current_physical_capacity.
263 During startup (when srv_is_being_started is true) it stays equal to the
264 m_current_physical_capacity (which is then computed for discovered log files).
265 After startup, it's set to srv_redo_log_capacity_used by calls to
266 @see update. */
268
269 /** Current resize direction. When user decides to resize down the redo log,
270 it becomes Log_resize_mode::RESIZING_DOWN until the resize is finished or
271 user decides to stop it (providing other capacity). Note, that resize is not
272 started during startup (when srv_is_being_started is true).
273 @note Updated only during calls to @see initialize and @see update. */
275
276 /** Cancels current resize operation immediately.
277 @remarks If the innodb_redo_log_capacity is changed when there is a previous
278 redo resize in progress, the previous resize is first cancelled. */
279 void cancel_resize();
280
281 /** Updates m_target_physical_capacity (reading srv_redo_log_capacity_used)
282 and possibly starts a new downsize operation. Might also update:
283 m_resize_mode, m_current_physical_capacity. */
284 void update_target();
285
286 /** Checks if target of the resize is reached, with regards to the criteria
287 based on the current logical size of the redo.
288 @param[in] current_logical_size current logical size of the redo log
289 @return true iff the target is reached */
290 bool is_target_reached_for_logical_size(lsn_t current_logical_size) const;
291
292 /** Checks if target of the resize is reached, with regards to the criteria
293 based on the current physical size of existing log files (excludes unused).
294 @param[in] current_physical_size total size of existing redo log files,
295 excluding unused (spare) files
296 @return true iff the target is reached */
298 os_offset_t current_physical_size) const;
299
300 /** Checks if target of the resize is reached, with regards to the criteria
301 based on the largest existing redo file.
302 @param[in] files in-memory dictionary of existing files
303 @return true iff the target is reached */
305
306 /** Checks if target of the resize is reached.
307 @param[in] files in-memory dictionary of existing files
308 @param[in] current_logical_size current logical size of the redo log
309 @return true iff the target is reached */
311 lsn_t current_logical_size) const;
312
313 /** Checks if target of the resize is reached with regards to all criteria
314 and updates the m_resize_mode, m_current_physical_capacity when that happens
315 (marking the resize operation as finished).
316 @param[in] files in-memory dictionary of existing files
317 @param[in] current_logical_size current logical size of the redo log */
319 lsn_t current_logical_size);
320
321 /** Updates value of server status variable: innodb_redo_log_resize_status. */
323
324 /** Updates values of server status variables:
325 innodb_redo_log_capacity_resized, innodb_redo_log_logical_size,
326 innodb_redo_log_physical_size, innodb_redo_log_resize_status.
327 @param[in] files in-memory dictionary of existing files
328 @param[in] current_logical_size current logical size of the redo log */
329 void update_status_variables(const Log_files_dict &files,
330 lsn_t current_logical_size);
331
332 /** Updates cached and exposed values related to the logical redo capacity:
333 - @see m_soft_logical_capacity
334 - @see m_hard_logical_capacity
335 - @see m_agressive_checkpoint_min_age
336 - @see m_adaptive_flush_min_age
337 - @see m_adaptive_flush_max_age
338 @param[in] current_logical_size current logical size of the redo log */
339 void update_exposed(lsn_t current_logical_size);
340
341 /** Computes suggested value for the current hard logical capacity.
342 @remarks This becomes non-trivial when the redo log is being resized down,
343 because this method is supposed to follow the checkpoint age then.
344 On the other hand, when the redo log is not being resized down, this method
345 computes the hard logical capacity by using simple math based on the current
346 physical capacity only (ie. ignoring the current checkpoint age).
347 @param[in] current_checkpoint_age current checkpoint age,
348 used only when resizing down
349 @return suggested value for current hard logical capacity */
350 lsn_t get_suggested_hard_logical_capacity(lsn_t current_checkpoint_age) const;
351};
352
353/** Retrieves limitations determined by the current state of log.m_capacity.
354These values are retrieved atomically (are consistent with each other).
355@param[in] log redo log
356@param[out] limit_for_free_check soft capacity of the redo decreased by
357 the current free check margin; this is
358 limit for size of redo until which the
359 log_free_check calls do not force waits
360@param[out] limit_for_dirty_page_age limit for the oldest dirty page until
361 which the async (adaptive) flushing is
362 not forced to be started (it might be
363 started if turned on explicitly by the
364 innodb_adaptive_flushing); note that
365 computation of this value include doing
366 the subtraction of the current log free
367 check margin */
369 lsn_t &limit_for_free_check,
370 lsn_t &limit_for_dirty_page_age);
371
372#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:371
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:342
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:455
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:554
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:355
void update_exposed(lsn_t current_logical_size)
Updates cached and exposed values related to the logical redo capacity:
Definition: log0files_capacity.cc:475
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:273
Log_resize_mode m_resize_mode
Current resize direction.
Definition: log0files_capacity.h:274
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:151
lsn_t soft_logical_capacity() const
Soft limit for logical capacity of the redo log.
Definition: log0files_capacity.cc:505
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:525
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:253
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:389
bool is_resizing_down() const
Definition: log0files_capacity.cc:521
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:447
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:431
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:439
lsn_t aggressive_checkpoint_min_age() const
Once checkpoint age exceeds that value, the log checkpointer thread keeps writing checkpoints aggress...
Definition: log0files_capacity.cc:517
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:335
void update_target()
Updates m_target_physical_capacity (reading srv_redo_log_capacity_used) and possibly starts a new dow...
Definition: log0files_capacity.cc:226
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:190
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:423
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:363
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:267
os_offset_t current_physical_capacity() const
Provides maximum limitation for space occupied on disk.
Definition: log0files_capacity.cc:529
lsn_t hard_logical_capacity() const
Hard limit for logical capacity of the redo log.
Definition: log0files_capacity.cc:501
void cancel_resize()
Cancels current resize operation immediately.
Definition: log0files_capacity.cc:215
lsn_t adaptive_flush_min_age() const
Once checkpoint age exceeds this value, the flushing of pages starts to be adaptive.
Definition: log0files_capacity.cc:509
os_offset_t m_current_physical_capacity
This is limitation for space on disk we are never allowed to exceed.
Definition: log0files_capacity.h:259
void update_resize_status_variable()
Updates value of server status variable: innodb_redo_log_resize_status.
Definition: log0files_capacity.cc:406
lsn_t adaptive_flush_max_age() const
Once checkpoint age exceeds that value, the flushing of pages is the most aggressive possible since t...
Definition: log0files_capacity.cc:513
os_offset_t next_file_size() const
Provides size of the next redo log file that will be created.
Definition: log0files_capacity.cc:533
In-memory dictionary of meta data of existing log files.
Definition: log0files_dict.h:57
void log_files_capacity_get_limits(const log_t &log, lsn_t &limit_for_free_check, lsn_t &limit_for_dirty_page_age)
Retrieves limitations determined by the current state of log.m_capacity.
Definition: log0files_capacity.cc:124
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:84
Definition: log0files_capacity.h:232
atomic_lsn_t m_agressive_checkpoint_min_age
Value returned by.
Definition: log0files_capacity.h:246
atomic_lsn_t m_hard_logical_capacity
Value returned by.
Definition: log0files_capacity.h:237
atomic_lsn_t m_soft_logical_capacity
Value returned by.
Definition: log0files_capacity.h:234
atomic_lsn_t m_adaptive_flush_max_age
Value returned by.
Definition: log0files_capacity.h:243
atomic_lsn_t m_adaptive_flush_min_age
Value returned by.
Definition: log0files_capacity.h:240
Redo log - single data structure with state of the redo log system.
Definition: log0sys.h:77