Debian Codegraph @

storage/innobase/include/log0files_capacity.h

raw
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_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
48 next log file that will be created. Tracks the redo resize operation when the
49 innodb_redo_log_capacity gets changed. Computes maximum ages for dirty pages,
50 which are then used by page cleaner coordinator.
51
52 @remarks When downsize is started, the limits for ages are decreased, forcing
53 page cleaners to flush more dirty pages then. File sizes for new redo files
54 are adjusted accordingly, so they could always be effectively used to hold
55 the whole existing redo log data (for the given current logical size). */
56 class Log_files_capacity {
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 */
86 os_offset_t current_physical_capacity() const;
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 */
93 os_offset_t target_physical_capacity() const;
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 */
102 lsn_t soft_logical_capacity() const;
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 */
110 lsn_t hard_logical_capacity() const;
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 */
128 lsn_t adaptive_flush_min_age() const;
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 */
134 lsn_t adaptive_flush_max_age() const;
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 */
143 lsn_t aggressive_checkpoint_min_age() const;
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 */
167 os_offset_t next_file_size() const;
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 */
179 lsn_t next_file_earlier_margin() const;
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 */
193 static lsn_t guess_hard_logical_capacity_for_soft(
194 lsn_t soft_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 */
200 static lsn_t soft_logical_capacity_for_hard(lsn_t hard_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 */
206 static lsn_t hard_logical_capacity_for_physical(
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 */
216 static lsn_t sync_flush_logical_capacity_for_soft(
217 lsn_t soft_logical_capacity);
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 */
227 static lsn_t guess_soft_logical_capacity_for_sync_flush(
228 lsn_t adaptive_flush_max_age);
229
230 private:
231 /** @see m_exposed */
232 struct Exposed {
233 /** Value returned by @see soft_logical_capacity */
234 atomic_lsn_t m_soft_logical_capacity{0};
235
236 /** Value returned by @see hard_logical_capacity */
237 atomic_lsn_t m_hard_logical_capacity{0};
238
239 /** Value returned by @see adaptive_flush_min_age */
240 atomic_lsn_t m_adaptive_flush_min_age{0};
241
242 /** Value returned by @see adaptive_flush_max_age */
243 atomic_lsn_t m_adaptive_flush_max_age{0};
244
245 /** Value returned by @see aggressive_checkpoint_min_age */
246 atomic_lsn_t m_agressive_checkpoint_min_age{0};
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. */
253 Exposed m_exposed{};
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. */
259 os_offset_t m_current_physical_capacity{0};
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. */
267 os_offset_t m_target_physical_capacity{0};
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. */
274 Log_resize_mode m_resize_mode{Log_resize_mode::NONE};
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 */
297 bool is_target_reached_for_physical_size(
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 */
304 bool is_target_reached_for_max_file_size(const Log_files_dict &files) const;
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 */
310 bool is_target_reached_for_resizing_down(const Log_files_dict &files,
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 */
318 void update_if_target_reached(const Log_files_dict &files,
319 lsn_t current_logical_size);
320
321 /** Updates value of server status variable: innodb_redo_log_resize_status. */
322 void update_resize_status_variable();
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.
354 These 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 */
368 void log_files_capacity_get_limits(const log_t &log,
369 lsn_t &limit_for_free_check,
370 lsn_t &limit_for_dirty_page_age);
371
372 #endif /* !log0files_capacity_h */