MySQL 8.0.31
Source Code Documentation
log0buf.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1995, 2022, 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 also distributed with certain software (including but not
17limited to OpenSSL) that is licensed under separate terms, as designated in a
18particular file or component or in included license documentation. The authors
19of MySQL hereby grant you an additional permission to link the program and
20your derivative works with the separately licensed software that they have
21included with MySQL.
22
23This program is distributed in the hope that it will be useful, but WITHOUT
24ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
25FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
26for more details.
27
28You should have received a copy of the GNU General Public License along with
29this program; if not, write to the Free Software Foundation, Inc.,
3051 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31
32*****************************************************************************/
33
34/**************************************************/ /**
35 @file include/log0buf.h
36
37 Redo log functions related to the log buffer.
38
39 *******************************************************/
40
41#ifndef log0buf_h
42#define log0buf_h
43
44/* MEB should neither write to the log buffer nor maintain
45the log buffer or recent_written / recent_closed buffers. */
46#ifndef UNIV_HOTBACKUP
47
48/* log.recent_written, log.recent_closed */
49#include "log0sys.h"
50
51/* log_t&, lsn_t, constants */
52#include "log0types.h"
53
54/**************************************************/ /**
55
56 @name Log - writing to the log buffer.
57
58 @remarks
59 These functions are designed for mtr_commit(),
60 and used only there (except log0log-t.cc).
61
62 *******************************************************/
63
64/** @{ */
65
66/** Handle which is used for writes to the log buffer. */
67struct Log_handle {
68 /** LSN of the first data byte. */
70
71 /** LSN after the last data byte. */
73};
74
75/** Acquires the log buffer x-lock.
76@param[in,out] log redo log */
78
79/** Releases the log buffer x-lock.
80@param[in,out] log redo log */
82
83/** Reserves space in the redo log for following write operations.
84Space is reserved for a given number of data bytes. Additionally
85bytes for required headers and footers of log blocks are reserved.
86
87After the space is reserved, range of lsn values from a start_lsn
88to an end_lsn is assigned. The log writer thread cannot proceed
89further than to the start_lsn, until a link start_lsn -> end_lsn
90has been added to the log recent written buffer.
91
92NOTE that the link is added after data is written to the reserved
93space in the log buffer. It is very critical to do all these steps
94as fast as possible, because very likely the log writer thread is
95waiting for the link.
96
97@param[in,out] log redo log
98@param[in] len number of data bytes to reserve for write
99@return handle that represents the reservation */
100Log_handle log_buffer_reserve(log_t &log, size_t len);
101
102/** Writes data to the log buffer. The space in the redo log has to be
103reserved before calling to this function and lsn pointing to inside the
104reserved range of lsn values has to be provided.
105
106The write does not have to cover the whole reserved space, but may not
107overflow it. If it does not cover, then returned value should be used
108to start the next write operation. Note that finally we must use exactly
109all the reserved space.
110
111@param[in,out] log redo log
112@param[in] str memory to write data from
113@param[in] str_len number of bytes to write
114@param[in] start_lsn lsn to start writing at (the reserved space)
115
116@return end_lsn after writing the data (in the reserved space), could be
117used to start the next write operation if there is still free space in
118the reserved space */
119lsn_t log_buffer_write(log_t &log, const byte *str, size_t str_len,
120 lsn_t start_lsn);
121
122/** Adds a link start_lsn -> end_lsn to the log recent written buffer.
123
124This function must be called after the data has been written to the
125fragment of log buffer represented by range [start_lsn, end_lsn).
126After the link is added, the log writer may write the data to disk.
127
128NOTE that still dirty pages for the [start_lsn, end_lsn) are not added
129to flush lists when this function is called.
130
131@param[in,out] log redo log
132@param[in] start_lsn start_lsn of the link to add
133@param[in] end_lsn end_lsn of the link to add */
134void log_buffer_write_completed(log_t &log, lsn_t start_lsn, lsn_t end_lsn);
135
136/** Modifies header of log block in the log buffer, which contains
137a given lsn value, and sets offset to the first group of log records
138within the block.
139
140This is used by mtr after writing a log record group which ends at
141lsn belonging to different log block than lsn at which the group was
142started. When write was finished at the last data byte of log block,
143it is considered ended in the next log block, because the next data
144byte belongs to that block.
145
146During recovery, when recovery is started in the middle of some group
147of log records, it first looks for the beginning of the next group.
148
149@param[in,out] log redo log
150@param[in] rec_group_end_lsn lsn at which the first log record
151group starts within the block containing this lsn value */
152void log_buffer_set_first_record_group(log_t &log, lsn_t rec_group_end_lsn);
153
154/** Adds a link start_lsn -> end_lsn to the log recent closed buffer.
155
156This is called after all dirty pages related to [start_lsn, end_lsn)
157have been added to corresponding flush lists.
158For detailed explanation - @see log0write.cc.
159
160@param[in,out] log redo log
161@param[in] handle handle for the reservation of space */
162void log_buffer_close(log_t &log, const Log_handle &handle);
163
164/** @} */
165
166/**************************************************/ /**
167
168 @name Log - management of the log buffer.
169
170 *******************************************************/
171
172/** @{ */
173
174/** Updates limit used when writing to log buffer. Note that the
175log buffer may have space for log records for which we still do
176not have space in log files (for larger lsn values).
177@param[in,out] log redo log */
178void log_update_buf_limit(log_t &log);
179
180/** Updates limit used when writing to log buffer, according to provided
181write_lsn. It must be <= log.write_lsn.load() to protect from log buffer
182overwrites.
183@param[in,out] log redo log
184@param[in] write_lsn value <= log.write_lsn.load() */
185void log_update_buf_limit(log_t &log, lsn_t write_lsn);
186
187/** Write to the log file up to the last log entry.
188@param[in,out] log redo log
189@param[in] sync whether we want the written log
190also to be flushed to disk. */
191void log_buffer_flush_to_disk(log_t &log, bool sync = true);
192
193/** Requests flush of the log buffer.
194@param[in] sync true: wait until the flush is done */
195void log_buffer_flush_to_disk(bool sync = true);
196
197/** Writes the log buffer to the log file. It is intended to be called from
198background master thread periodically. If the log writer threads are active,
199this function writes nothing. */
201
202/** Get last redo block from redo buffer and end LSN. Note that it takes
203x-lock on the log buffer for a short period. Out values are always set,
204even when provided last_block is nullptr.
205@param[in,out] log redo log
206@param[out] last_lsn end lsn of last mtr
207@param[out] last_block last redo block
208@param[in,out] block_len length in bytes */
209void log_buffer_get_last_block(log_t &log, lsn_t &last_lsn, byte *last_block,
210 uint32_t &block_len);
211
212/** Changes size of the log buffer. This is a thread-safe version.
213It is used by SET GLOBAL innodb_log_buffer_size = X.
214@param[in,out] log redo log
215@param[in] new_size requested new size
216@return true iff succeeded in resize */
217bool log_buffer_resize(log_t &log, size_t new_size);
218
219/** Changes size of the log buffer. This is a non-thread-safe version
220which might be invoked only when there are no concurrent possible writes
221to the log buffer. It is used in log_buffer_reserve() when a requested
222size to reserve is larger than size of the log buffer.
223@param[in,out] log redo log
224@param[in] new_size requested new size
225@param[in] end_lsn maximum lsn written to log buffer
226@return true iff succeeded in resize */
227bool log_buffer_resize_low(log_t &log, size_t new_size, lsn_t end_lsn);
228
229/** @} */
230
231/**************************************************/ /**
232
233 @name Log - the recent written, the recent closed buffers.
234
235 *******************************************************/
236
237/** @{ */
238
239#define log_closer_mutex_enter(log) mutex_enter(&((log).closer_mutex))
240
241#define log_closer_mutex_enter_nowait(log) \
242 mutex_enter_nowait(&((log).closer_mutex))
243
244#define log_closer_mutex_exit(log) mutex_exit(&((log).closer_mutex))
245
246/** @return lsn up to which all writes to log buffer have been finished */
248 return log.recent_written.tail();
249}
250
251/** @return lsn up to which all dirty pages have been added to flush list */
253 return log.recent_closed.tail();
254}
255
256/** @return capacity of the recent_closed, or 0 if !log_use_threads() */
258 return log.recent_closed.capacity();
259}
260
261/** Advances log.buf_ready_for_write_lsn using links in the recent written
262buffer. It's used by the log writer thread only.
263@param[in] log redo log */
265
266/** Validates that all slots in log recent written buffer for lsn values
267in range between begin and end, are empty. Used during tests, crashes the
268program if validation does not pass.
269@param[in] log redo log which buffer is validated
270@param[in] begin validation start (inclusive)
271@param[in] end validation end (exclusive) */
273 lsn_t end);
274
275/** Validates that all slots in log recent closed buffer for lsn values
276in range between begin and end, are empty. Used during tests, crashes the
277program if validation does not pass.
278@param[in] log redo log which buffer is validated
279@param[in] begin validation start (inclusive)
280@param[in] end validation end (exclusive) */
282
283/** Waits until there is free space in the log recent closed buffer
284for any links start_lsn -> end_lsn, which start at provided start_lsn.
285It does not add any link.
286
287This is called just before dirty pages for [start_lsn, end_lsn)
288are added to flush lists. That's because we need to guarantee,
289that the delay until dirty page is added to flush list is limited.
290For detailed explanation - @see log0write.cc.
291
292@param[in,out] log redo log
293@param[in] lsn lsn on which we wait (for any link: lsn -> x) */
295
296/** @} */
297
298#endif /* !UNIV_HOTBACKUP */
299
300#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:252
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:1060
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:1141
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:1192
void log_buffer_x_lock_enter(log_t &log)
Acquires the log buffer x-lock.
Definition: log0buf.cc:585
Log_handle log_buffer_reserve(log_t &log, size_t len)
Reserves space in the redo log for following write operations.
Definition: log0buf.cc:858
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:921
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:1220
lsn_t log_buffer_flush_order_lag(const log_t &log)
Definition: log0buf.h:257
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:1160
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:1124
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:1172
void log_buffer_sync_in_background()
Writes the log buffer to the log file.
Definition: log0buf.cc:1207
void log_update_buf_limit(log_t &log)
Updates limit used when writing to log buffer.
Definition: log0buf.cc:817
void log_buffer_x_lock_exit(log_t &log)
Releases the log buffer x-lock.
Definition: log0buf.cc:659
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:1223
lsn_t log_buffer_ready_for_write_lsn(const log_t &log)
Definition: log0buf.h:247
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:62
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1063
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:191
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:67
lsn_t start_lsn
LSN of the first data byte.
Definition: log0buf.h:69
lsn_t end_lsn
LSN after the last data byte.
Definition: log0buf.h:72
Redo log - single data structure with state of the redo log system.
Definition: log0sys.h:74
Link_buf< lsn_t > recent_written
The recent written buffer.
Definition: log0sys.h:129
Link_buf< lsn_t > recent_closed
The recent closed buffer.
Definition: log0sys.h:143
static uint64_t lsn
Definition: xcom_base.cc:442