MySQL 8.4.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 / recent_closed buffers. */
47#ifndef UNIV_HOTBACKUP
48
49/* log.recent_written, log.recent_closed */
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 */
135void log_buffer_write_completed(log_t &log, lsn_t start_lsn, lsn_t end_lsn);
136
137/** Modifies header of log block in the log buffer, which contains
138a given lsn value, and sets offset to the first group of log records
139within the block.
140
141This is used by mtr after writing a log record group which ends at
142lsn belonging to different log block than lsn at which the group was
143started. When write was finished at the last data byte of log block,
144it is considered ended in the next log block, because the next data
145byte belongs to that block.
146
147During recovery, when recovery is started in the middle of some group
148of log records, it first looks for the beginning of the next group.
149
150@param[in,out] log redo log
151@param[in] rec_group_end_lsn lsn at which the first log record
152group starts within the block containing this lsn value */
153void log_buffer_set_first_record_group(log_t &log, lsn_t rec_group_end_lsn);
154
155/** Adds a link start_lsn -> end_lsn to the log recent closed buffer.
156
157This is called after all dirty pages related to [start_lsn, end_lsn)
158have been added to corresponding flush lists.
159For detailed explanation - @see log0write.cc.
160
161@param[in,out] log redo log
162@param[in] handle handle for the reservation of space */
163void log_buffer_close(log_t &log, const Log_handle &handle);
164
165/** @} */
166
167/**************************************************/ /**
168
169 @name Log - management of the log buffer.
170
171 *******************************************************/
172
173/** @{ */
174
175/** Updates limit used when writing to log buffer. Note that the
176log buffer may have space for log records for which we still do
177not have space in log files (for larger lsn values).
178@param[in,out] log redo log */
179void log_update_buf_limit(log_t &log);
180
181/** Updates limit used when writing to log buffer, according to provided
182write_lsn. It must be <= log.write_lsn.load() to protect from log buffer
183overwrites.
184@param[in,out] log redo log
185@param[in] write_lsn value <= log.write_lsn.load() */
186void log_update_buf_limit(log_t &log, lsn_t write_lsn);
187
188/** Write to the log file up to the last log entry.
189@param[in,out] log redo log
190@param[in] sync whether we want the written log
191also to be flushed to disk. */
192void log_buffer_flush_to_disk(log_t &log, bool sync = true);
193
194/** Requests flush of the log buffer.
195@param[in] sync true: wait until the flush is done */
196void log_buffer_flush_to_disk(bool sync = true);
197
198/** Writes the log buffer to the log file. It is intended to be called from
199background master thread periodically. If the log writer threads are active,
200this function writes nothing. */
202
203/** Get last redo block from redo buffer and end LSN. Note that it takes
204x-lock on the log buffer for a short period. Out values are always set,
205even when provided last_block is nullptr.
206@param[in,out] log redo log
207@param[out] last_lsn end lsn of last mtr
208@param[out] last_block last redo block
209@param[in,out] block_len length in bytes */
210void log_buffer_get_last_block(log_t &log, lsn_t &last_lsn, byte *last_block,
211 uint32_t &block_len);
212
213/** Changes size of the log buffer. This is a thread-safe version.
214It is used by SET GLOBAL innodb_log_buffer_size = X.
215@param[in,out] log redo log
216@param[in] new_size requested new size
217@return true iff succeeded in resize */
218bool log_buffer_resize(log_t &log, size_t new_size);
219
220/** Changes size of the log buffer. This is a non-thread-safe version
221which might be invoked only when there are no concurrent possible writes
222to the log buffer. It is used in log_buffer_reserve() when a requested
223size to reserve is larger than size of the log buffer.
224@param[in,out] log redo log
225@param[in] new_size requested new size
226@param[in] end_lsn maximum lsn written to log buffer
227@return true iff succeeded in resize */
228bool log_buffer_resize_low(log_t &log, size_t new_size, lsn_t end_lsn);
229
230/** @} */
231
232/**************************************************/ /**
233
234 @name Log - the recent written, the recent closed buffers.
235
236 *******************************************************/
237
238/** @{ */
239
240#define log_closer_mutex_enter(log) mutex_enter(&((log).closer_mutex))
241
242#define log_closer_mutex_enter_nowait(log) \
243 mutex_enter_nowait(&((log).closer_mutex))
244
245#define log_closer_mutex_exit(log) mutex_exit(&((log).closer_mutex))
246
247/** @return lsn up to which all writes to log buffer have been finished */
249 return log.recent_written.tail();
250}
251
252/** @return lsn up to which all dirty pages have been added to flush list */
254 return log.recent_closed.tail();
255}
256
257/** @return capacity of the recent_closed, or 0 if !log_use_threads() */
259 return log.recent_closed.capacity();
260}
261
262/** Advances log.buf_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/** Validates that all slots in log recent closed buffer for lsn values
277in range between begin and end, are empty. Used during tests, crashes the
278program if validation does not pass.
279@param[in] log redo log which buffer is validated
280@param[in] begin validation start (inclusive)
281@param[in] end validation end (exclusive) */
283
284/** Waits until there is free space in the log recent closed buffer
285for any links start_lsn -> end_lsn, which start at provided start_lsn.
286It does not add any link.
287
288This is called just before dirty pages for [start_lsn, end_lsn)
289are added to flush lists. That's because we need to guarantee,
290that the delay until dirty page is added to flush list is limited.
291For detailed explanation - @see log0write.cc.
292
293@param[in,out] log redo log
294@param[in] lsn lsn on which we wait (for any link: lsn -> x) */
296
297/** @} */
298
299#endif /* !UNIV_HOTBACKUP */
300
301#endif /* !log0buf_h */
void log_advance_ready_for_write_lsn(log_t &log)
Advances log.buf_ready_for_write_lsn using links in the recent written buffer.
Definition: log0buf.cc:1286
lsn_t log_buffer_dirty_pages_added_up_to_lsn(const log_t &log)
Definition: log0buf.h:253
void log_buffer_write_completed(log_t &log, lsn_t start_lsn, lsn_t end_lsn)
Adds a link start_lsn -> end_lsn to the log recent written buffer.
Definition: log0buf.cc:1061
void log_buffer_close(log_t &log, const Log_handle &handle)
Adds a link start_lsn -> end_lsn to the log recent closed buffer.
Definition: log0buf.cc:1142
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:1193
void log_buffer_x_lock_enter(log_t &log)
Acquires the log buffer x-lock.
Definition: log0buf.cc:586
Log_handle log_buffer_reserve(log_t &log, size_t len)
Reserves space in the redo log for following write operations.
Definition: log0buf.cc:859
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:922
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:1221
lsn_t log_buffer_flush_order_lag(const log_t &log)
Definition: log0buf.h:258
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:1161
void log_wait_for_space_in_log_recent_closed(log_t &log, lsn_t lsn)
Waits until there is free space in the log recent closed buffer for any links start_lsn -> end_lsn,...
Definition: log0buf.cc:1125
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:1231
void log_buffer_sync_in_background()
Writes the log buffer to the log file.
Definition: log0buf.cc:1208
void log_update_buf_limit(log_t &log)
Updates limit used when writing to log buffer.
Definition: log0buf.cc:818
void log_buffer_x_lock_exit(log_t &log)
Releases the log buffer x-lock.
Definition: log0buf.cc:660
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:1282
lsn_t log_buffer_ready_for_write_lsn(const log_t &log)
Definition: log0buf.h:248
void log_recent_closed_empty_validate(const log_t &log, lsn_t begin, lsn_t end)
Validates that all slots in log recent closed buffer for lsn values in range between begin and end,...
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:1073
const char * begin(const char *const c)
Definition: base64.h:44
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
static int handle(int sql_errno, const char *sqlstate, const char *message, void *state)
Bridge function between the C++ API offered by this module and the C API of the parser service.
Definition: services.cc:64
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
Link_buf< lsn_t > recent_closed
The recent closed buffer.
Definition: log0sys.h:157
static uint64_t lsn
Definition: xcom_base.cc:446