MySQL 9.0.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 */
191void log_buffer_flush_to_disk(bool sync = true);
192
193/** Writes the log buffer to the log file. It is intended to be called from
194background master thread periodically. If the log writer threads are active,
195this function writes nothing. */
197
198/** Get last redo block from redo buffer and end LSN. Note that it takes
199x-lock on the log buffer for a short period. Out values are always set,
200even when provided last_block is nullptr.
201@param[in,out] log redo log
202@param[out] last_lsn end lsn of last mtr
203@param[out] last_block last redo block
204@param[in,out] block_len length in bytes */
205void log_buffer_get_last_block(log_t &log, lsn_t &last_lsn, byte *last_block,
206 uint32_t &block_len);
207
208/** Changes size of the log buffer. This is a thread-safe version.
209It is used by SET GLOBAL innodb_log_buffer_size = X.
210@param[in,out] log redo log
211@param[in] new_size requested new size
212@return true iff succeeded in resize */
213bool log_buffer_resize(log_t &log, size_t new_size);
214
215/** Changes size of the log buffer. This is a non-thread-safe version
216which might be invoked only when there are no concurrent possible writes
217to the log buffer. It is used in log_buffer_reserve() when a requested
218size to reserve is larger than size of the log buffer.
219@param[in,out] log redo log
220@param[in] new_size requested new size
221@param[in] end_lsn maximum lsn written to log buffer
222@return true iff succeeded in resize */
223bool log_buffer_resize_low(log_t &log, size_t new_size, lsn_t end_lsn);
224
225/** @} */
226
227/**************************************************/ /**
228
229 @name Log - the recent written buffer.
230
231 *******************************************************/
232
233/** @{ */
234
235#define log_closer_mutex_enter(log) mutex_enter(&((log).closer_mutex))
236
237#define log_closer_mutex_enter_nowait(log) \
238 mutex_enter_nowait(&((log).closer_mutex))
239
240#define log_closer_mutex_exit(log) mutex_exit(&((log).closer_mutex))
241
242/** @return lsn up to which all writes to log buffer have been finished */
244 return log.recent_written.tail();
245}
246
247/** Wait until log_buffer_ready_for_write_lsn() returns at least min_lsn.
248@param[in] log redo log
249@param[in] min_lsn The awaited minimum value of
250 log_buffer_ready_for_write_lsn().
251@param[out] waits_on_event If not null, then the pointed integer will be set
252 to the number of times this function had to wait
253 on closer_event.
254@return The seen value of log_buffer_ready_for_write_lsn() which was at least
255 equal to min_lsn.
256*/
258 log_t &log, lsn_t min_lsn, uint32_t *waits_on_event = nullptr);
259
260/** Advances log_buffer_ready_for_write_lsn() using links in the recent written
261buffer. It's used by the log writer thread only.
262@param[in] log redo log */
264
265/** Validates that all slots in log recent written buffer for lsn values
266in range between begin and end, are empty. Used during tests, crashes the
267program if validation does not pass.
268@param[in] log redo log which buffer is validated
269@param[in] begin validation start (inclusive)
270@param[in] end validation end (exclusive) */
272 lsn_t end);
273/** @} */
274
275#endif /* !UNIV_HOTBACKUP */
276
277#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:1273
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:1208
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:1198
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:243
Redo log - the log_sys.
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:1081
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