MySQL 9.1.0
Source Code Documentation
log0buf.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1995, 2024, Oracle and/or its affiliates.
4Copyright (c) 2009, Google Inc.
5
6Portions of this file contain modifications contributed and copyrighted by
7Google, Inc. Those modifications are gratefully acknowledged and are described
8briefly in the InnoDB documentation. The contributions by Google are
9incorporated with their permission, and subject to the conditions contained in
10the file COPYING.Google.
11
12This program is free software; you can redistribute it and/or modify it under
13the terms of the GNU General Public License, version 2.0, as published by the
14Free Software Foundation.
15
16This program is designed to work with certain software (including
17but not limited to OpenSSL) that is licensed under separate terms,
18as designated in a particular file or component or in included license
19documentation. The authors of MySQL hereby grant you an additional
20permission to link the program and your derivative works with the
21separately licensed software that they have either included with
22the program or referenced in the documentation.
23
24This program is distributed in the hope that it will be useful, but WITHOUT
25ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
26FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
27for more details.
28
29You should have received a copy of the GNU General Public License along with
30this program; if not, write to the Free Software Foundation, Inc.,
3151 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32
33*****************************************************************************/
34
35/**************************************************/ /**
36 @file include/log0buf.h
37
38 Redo log functions related to the log buffer.
39
40 *******************************************************/
41
42#ifndef log0buf_h
43#define log0buf_h
44
45/* MEB should neither write to the log buffer nor maintain
46the log buffer or recent_written buffer. */
47#ifndef UNIV_HOTBACKUP
48
49/* log.recent_written */
50#include "log0sys.h"
51
52/* log_t&, lsn_t, constants */
53#include "log0types.h"
54
55/**************************************************/ /**
56
57 @name Log - writing to the log buffer.
58
59 @remarks
60 These functions are designed for mtr_commit(),
61 and used only there (except log0log-t.cc).
62
63 *******************************************************/
64
65/** @{ */
66
67/** Handle which is used for writes to the log buffer. */
68struct Log_handle {
69 /** LSN of the first data byte. */
71
72 /** LSN after the last data byte. */
74};
75
76/** Acquires the log buffer x-lock.
77@param[in,out] log redo log */
79
80/** Releases the log buffer x-lock.
81@param[in,out] log redo log */
83
84/** Reserves space in the redo log for following write operations.
85Space is reserved for a given number of data bytes. Additionally
86bytes for required headers and footers of log blocks are reserved.
87
88After the space is reserved, range of lsn values from a start_lsn
89to an end_lsn is assigned. The log writer thread cannot proceed
90further than to the start_lsn, until a link start_lsn -> end_lsn
91has been added to the log recent written buffer.
92
93NOTE that the link is added after data is written to the reserved
94space in the log buffer. It is very critical to do all these steps
95as fast as possible, because very likely the log writer thread is
96waiting for the link.
97
98@param[in,out] log redo log
99@param[in] len number of data bytes to reserve for write
100@return handle that represents the reservation */
101Log_handle log_buffer_reserve(log_t &log, size_t len);
102
103/** Writes data to the log buffer. The space in the redo log has to be
104reserved before calling to this function and lsn pointing to inside the
105reserved range of lsn values has to be provided.
106
107The write does not have to cover the whole reserved space, but may not
108overflow it. If it does not cover, then returned value should be used
109to start the next write operation. Note that finally we must use exactly
110all the reserved space.
111
112@param[in,out] log redo log
113@param[in] str memory to write data from
114@param[in] str_len number of bytes to write
115@param[in] start_lsn lsn to start writing at (the reserved space)
116
117@return end_lsn after writing the data (in the reserved space), could be
118used to start the next write operation if there is still free space in
119the reserved space */
120lsn_t log_buffer_write(log_t &log, const byte *str, size_t str_len,
121 lsn_t start_lsn);
122
123/** Adds a link start_lsn -> end_lsn to the log recent written buffer.
124
125This function must be called after the data has been written to the
126fragment of log buffer represented by range [start_lsn, end_lsn).
127After the link is added, the log writer may write the data to disk.
128
129NOTE that still dirty pages for the [start_lsn, end_lsn) are not added
130to flush lists when this function is called.
131
132@param[in,out] log redo log
133@param[in] start_lsn start_lsn of the link to add
134@param[in] end_lsn end_lsn of the link to add
135@param[in] is_last_block A flag to let log writer know that this
136[start_lsn,end_lsn) range was the last fragment of the range reserved by this
137thread earlier through log_buffer_reserve(..) , i.e. this thread has finished
138writing to the log buffer. */
139void log_buffer_write_completed(log_t &log, lsn_t start_lsn, lsn_t end_lsn,
140 bool is_last_block);
141
142/** Modifies header of log block in the log buffer, which contains
143a given lsn value, and sets offset to the first group of log records
144within the block.
145
146This is used by mtr after writing a log record group which ends at
147lsn belonging to different log block than lsn at which the group was
148started. When write was finished at the last data byte of log block,
149it is considered ended in the next log block, because the next data
150byte belongs to that block.
151
152During recovery, when recovery is started in the middle of some group
153of log records, it first looks for the beginning of the next group.
154
155@param[in,out] log redo log
156@param[in] rec_group_end_lsn lsn at which the first log record
157group starts within the block containing this lsn value */
158void log_buffer_set_first_record_group(log_t &log, lsn_t rec_group_end_lsn);
159
160/** @} */
161
162/**************************************************/ /**
163
164 @name Log - management of the log buffer.
165
166 *******************************************************/
167
168/** @{ */
169
170/** Updates limit used when writing to log buffer. Note that the
171log buffer may have space for log records for which we still do
172not have space in log files (for larger lsn values).
173@param[in,out] log redo log */
174void log_update_buf_limit(log_t &log);
175
176/** Updates limit used when writing to log buffer, according to provided
177write_lsn. It must be <= log.write_lsn.load() to protect from log buffer
178overwrites.
179@param[in,out] log redo log
180@param[in] write_lsn value <= log.write_lsn.load() */
181void log_update_buf_limit(log_t &log, lsn_t write_lsn);
182
183/** Write to the log file up to the last log entry.
184@param[in,out] log redo log
185@param[in] sync whether we want the written log
186also to be flushed to disk. */
187void log_buffer_flush_to_disk(log_t &log, bool sync = true);
188
189/** Requests flush of the log buffer.
190@param[in] sync true: wait until the flush is done */
191inline void log_buffer_flush_to_disk(bool sync = true) {
193}
194
195/** Writes the log buffer to the log file. It is intended to be called from
196background master thread periodically. If the log writer threads are active,
197this function writes nothing. */
199
200/** Get last redo block from redo buffer and end LSN. Note that it takes
201x-lock on the log buffer for a short period. Out values are always set,
202even when provided last_block is nullptr.
203@param[in,out] log redo log
204@param[out] last_lsn end lsn of last mtr
205@param[out] last_block last redo block
206@param[in,out] block_len length in bytes */
207void log_buffer_get_last_block(log_t &log, lsn_t &last_lsn, byte *last_block,
208 uint32_t &block_len);
209
210/** Changes size of the log buffer. This is a thread-safe version.
211It is used by SET GLOBAL innodb_log_buffer_size = X.
212@param[in,out] log redo log
213@param[in] new_size requested new size
214@return true iff succeeded in resize */
215bool log_buffer_resize(log_t &log, size_t new_size);
216
217/** Changes size of the log buffer. This is a non-thread-safe version
218which might be invoked only when there are no concurrent possible writes
219to the log buffer. It is used in log_buffer_reserve() when a requested
220size to reserve is larger than size of the log buffer.
221@param[in,out] log redo log
222@param[in] new_size requested new size
223@param[in] end_lsn maximum lsn written to log buffer
224@return true iff succeeded in resize */
225bool log_buffer_resize_low(log_t &log, size_t new_size, lsn_t end_lsn);
226
227/** @} */
228
229/**************************************************/ /**
230
231 @name Log - the recent written buffer.
232
233 *******************************************************/
234
235/** @{ */
236
237#define log_closer_mutex_enter(log) mutex_enter(&((log).closer_mutex))
238
239#define log_closer_mutex_enter_nowait(log) \
240 mutex_enter_nowait(&((log).closer_mutex))
241
242#define log_closer_mutex_exit(log) mutex_exit(&((log).closer_mutex))
243
244/** @return lsn up to which all writes to log buffer have been finished */
246 return log.recent_written.tail();
247}
248
249/** Wait until log_buffer_ready_for_write_lsn() returns at least min_lsn.
250@param[in] log redo log
251@param[in] min_lsn The awaited minimum value of
252 log_buffer_ready_for_write_lsn().
253@param[out] waits_on_event If not null, then the pointed integer will be set
254 to the number of times this function had to wait
255 on closer_event.
256@return The seen value of log_buffer_ready_for_write_lsn() which was at least
257 equal to min_lsn.
258*/
260 log_t &log, lsn_t min_lsn, uint32_t *waits_on_event = nullptr);
261
262/** Advances log_buffer_ready_for_write_lsn() using links in the recent written
263buffer. It's used by the log writer thread only.
264@param[in] log redo log */
266
267/** Validates that all slots in log recent written buffer for lsn values
268in range between begin and end, are empty. Used during tests, crashes the
269program if validation does not pass.
270@param[in] log redo log which buffer is validated
271@param[in] begin validation start (inclusive)
272@param[in] end validation end (exclusive) */
274 lsn_t end);
275/** @} */
276
277#endif /* !UNIV_HOTBACKUP */
278
279#endif /* !log0buf_h */
void log_advance_ready_for_write_lsn(log_t &log)
Advances log_buffer_ready_for_write_lsn() using links in the recent written buffer.
Definition: log0buf.cc:1269
void log_buffer_flush_to_disk(log_t &log, bool sync=true)
Write to the log file up to the last log entry.
Definition: log0buf.cc:1183
void log_buffer_x_lock_enter(log_t &log)
Acquires the log buffer x-lock.
Definition: log0buf.cc:633
Log_handle log_buffer_reserve(log_t &log, size_t len)
Reserves space in the redo log for following write operations.
Definition: log0buf.cc:884
lsn_t log_buffer_write(log_t &log, const byte *str, size_t str_len, lsn_t start_lsn)
Writes data to the log buffer.
Definition: log0buf.cc:946
void log_buffer_get_last_block(log_t &log, lsn_t &last_lsn, byte *last_block, uint32_t &block_len)
Get last redo block from redo buffer and end LSN.
Definition: log0buf.cc:1204
void log_buffer_set_first_record_group(log_t &log, lsn_t rec_group_end_lsn)
Modifies header of log block in the log buffer, which contains a given lsn value, and sets offset to ...
Definition: log0buf.cc:1151
lsn_t log_buffer_wait_for_ready_for_write_lsn(log_t &log, lsn_t min_lsn, uint32_t *waits_on_event=nullptr)
Wait until log_buffer_ready_for_write_lsn() returns at least min_lsn.
Definition: log0buf.cc:593
bool log_buffer_resize_low(log_t &log, size_t new_size, lsn_t end_lsn)
Changes size of the log buffer.
Definition: log0log.cc:1220
void log_buffer_sync_in_background()
Writes the log buffer to the log file.
Definition: log0buf.cc:1194
void log_update_buf_limit(log_t &log)
Updates limit used when writing to log buffer.
Definition: log0buf.cc:843
void log_buffer_write_completed(log_t &log, lsn_t start_lsn, lsn_t end_lsn, bool is_last_block)
Adds a link start_lsn -> end_lsn to the log recent written buffer.
Definition: log0buf.cc:1085
void log_buffer_x_lock_exit(log_t &log)
Releases the log buffer x-lock.
Definition: log0buf.cc:685
void log_recent_written_empty_validate(const log_t &log, lsn_t begin, lsn_t end)
Validates that all slots in log recent written buffer for lsn values in range between begin and end,...
bool log_buffer_resize(log_t &log, size_t new_size)
Changes size of the log buffer.
Definition: log0log.cc:1271
lsn_t log_buffer_ready_for_write_lsn(const log_t &log)
Definition: log0buf.h:245
Redo log - the log_sys.
log_t * log_sys
Redo log system (singleton).
Definition: log0log.cc:431
Redo log basic types.
uint64_t lsn_t
Type used for all log sequence number storage and arithmetic.
Definition: log0types.h:63
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1105
const char * begin(const char *const c)
Definition: base64.h:44
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
Handle which is used for writes to the log buffer.
Definition: log0buf.h:68
lsn_t start_lsn
LSN of the first data byte.
Definition: log0buf.h:70
lsn_t end_lsn
LSN after the last data byte.
Definition: log0buf.h:73
Redo log - single data structure with state of the redo log system.
Definition: log0sys.h:77
Link_buf< lsn_t > recent_written
The recent written buffer.
Definition: log0sys.h:143