| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License, version 2.0, |
| 7 |
as published by the 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, |
| 18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
GNU General Public License, version 2.0, for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License |
| 23 |
along with this program; if not, write to the Free Software |
| 24 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/**************************************************/ /** |
| 29 |
@file log/log0buf.cc |
| 30 |
|
| 31 |
Redo log buffer implementation, including functions to: |
| 32 |
|
| 33 |
-# Reserve space in the redo log buffer, |
| 34 |
|
| 35 |
-# Write to the reserved space in the log buffer, |
| 36 |
|
| 37 |
-# Add link to the log recent written buffer, |
| 38 |
|
| 39 |
-# Add link to the log recent closed buffer. |
| 40 |
|
| 41 |
*******************************************************/ |
| 42 |
|
| 43 |
#ifndef UNIV_HOTBACKUP |
| 44 |
|
| 45 |
/* std::atomic_thread_fence */ |
| 46 |
#include <atomic> |
| 47 |
|
| 48 |
/* std::memcpy */ |
| 49 |
#include <cstring> |
| 50 |
|
| 51 |
/* Log_handle, ... */ |
| 52 |
#include "log0buf.h" |
| 53 |
|
| 54 |
/* log_checkpointer_mutex */ |
| 55 |
#include "log0chkp.h" |
| 56 |
|
| 57 |
/* log_translate_sn_to_lsn, ... */ |
| 58 |
#include "log0log.h" |
| 59 |
|
| 60 |
/* recv_recovery_is_on(), ... */ |
| 61 |
#include "log0recv.h" |
| 62 |
|
| 63 |
/* log_t::X */ |
| 64 |
#include "log0sys.h" |
| 65 |
|
| 66 |
/* log_sync_point, log_test */ |
| 67 |
#include "log0test.h" |
| 68 |
|
| 69 |
/* OS_FILE_LOG_BLOCK_SIZE, ... */ |
| 70 |
#include "log0types.h" |
| 71 |
|
| 72 |
/* log_writer_mutex */ |
| 73 |
#include "log0write.h" |
| 74 |
|
| 75 |
/* DBUG_EXECUTE_IF, ... */ |
| 76 |
#include "my_dbug.h" |
| 77 |
|
| 78 |
/* srv_read_only_mode */ |
| 79 |
#include "srv0srv.h" |
| 80 |
|
| 81 |
/* srv_shutdown_state */ |
| 82 |
#include "srv0start.h" |
| 83 |
|
| 84 |
/* ut_uint64_align_down */ |
| 85 |
#include "ut0byte.h" |
| 86 |
|
| 87 |
// clang-format off |
| 88 |
/**************************************************/ /** |
| 89 |
@page PAGE_INNODB_REDO_LOG_BUF Redo log buffer |
| 90 |
|
| 91 |
When mtr commits, data has to be moved from internal buffer of the mtr |
| 92 |
to the redo log buffer. For a better concurrency, procedure for writing |
| 93 |
to the log buffer consists of following steps: |
| 94 |
-# @ref sect_redo_log_buf_reserve |
| 95 |
-# @ref sect_redo_log_buf_write |
| 96 |
-# @ref sect_redo_log_buf_add_links_to_recent_written |
| 97 |
|
| 98 |
Afterwards pages modified during the mtr, need to be added to flush lists. |
| 99 |
Because there is no longer a mutex protecting order in which dirty pages |
| 100 |
are added to flush lists, additional mechanism is required to ensure that |
| 101 |
lsn available for checkpoint is determined properly. Hence the procedure |
| 102 |
consists of following steps: |
| 103 |
-# @ref sect_redo_log_mark_dirty_pages |
| 104 |
-# @ref sect_redo_log_add_dirty_pages |
| 105 |
-# @ref sect_redo_log_add_link_to_recent_closed |
| 106 |
|
| 107 |
@section sect_redo_log_buf_reserve Reservation of space in the redo |
| 108 |
|
| 109 |
Range of lsn values is reserved for a provided number of data bytes. |
| 110 |
The reserved range will directly address space for the data in both |
| 111 |
the log buffer and the log files. |
| 112 |
|
| 113 |
Procedure used to reserve the range of lsn values: |
| 114 |
|
| 115 |
-# Acquiring shared access to the redo log (through sharded rw_lock) |
| 116 |
|
| 117 |
-# Increase the global number of reserved data bytes (@ref subsect_redo_log_sn) |
| 118 |
by number of data bytes we need to write. |
| 119 |
|
| 120 |
This is performed by an atomic fetch_add operation: |
| 121 |
|
| 122 |
start_sn = log.sn.fetch_add(len) |
| 123 |
end_sn = start_sn + len |
| 124 |
|
| 125 |
where _len_ is number of data bytes we need to write. |
| 126 |
|
| 127 |
Then range of sn values is translated to range of lsn values: |
| 128 |
|
| 129 |
start_lsn = log_translate_sn_to_lsn(start_sn) |
| 130 |
end_lsn = log_translate_sn_to_lsn(end_sn) |
| 131 |
|
| 132 |
The required translations are performed by simple calculations, because: |
| 133 |
|
| 134 |
lsn = sn / LOG_BLOCK_DATA_SIZE * OS_FILE_LOG_BLOCK_SIZE |
| 135 |
+ sn % LOG_BLOCK_DATA_SIZE |
| 136 |
+ LOG_BLOCK_HDR_SIZE |
| 137 |
|
| 138 |
-# Wait until the reserved range corresponds to free space in the log buffer. |
| 139 |
|
| 140 |
In this step we could be forced to wait for the |
| 141 |
[log writer thread](@ref sect_redo_log_writer), |
| 142 |
which reclaims space in the log buffer by writing data to system buffers. |
| 143 |
|
| 144 |
The user thread waits until the reserved range of lsn values maps to free |
| 145 |
space in the log buffer, which is true when: |
| 146 |
|
| 147 |
end_lsn - log.write_lsn <= log.buf_size |
| 148 |
|
| 149 |
@remarks |
| 150 |
The waiting is performed by a call to log_write_up_to(end_lsn - |
| 151 |
log.buf_size), which has a loop with short sleeps. We assume that it is |
| 152 |
unlikely that the waiting is actually needed. The |
| 153 |
_MONITOR_LOG_ON_BUFFER_SPACE_* counters track number of iterations spent in the |
| 154 |
waiting loop. If they are not nearby 0, DBA should try to increase the size of |
| 155 |
the log buffer. |
| 156 |
|
| 157 |
@note The log writer thread could be waiting on the write syscall, but it |
| 158 |
also could be waiting for other user threads, which need to complete writing |
| 159 |
their data to the log buffer for smaller sn values! Hopefully these user |
| 160 |
threads have not been scheduled out. If we controlled scheduling (e.g. if |
| 161 |
we have used fiber-based approach), we could avoid such problem. |
| 162 |
|
| 163 |
-# Wait until the reserved range corresponds to free space in the log files. |
| 164 |
|
| 165 |
In this step we could be forced to wait for page cleaner threads or the |
| 166 |
[log checkpointer thread](@ref sect_redo_log_checkpointer) until it made |
| 167 |
a next checkpoint. |
| 168 |
|
| 169 |
The user thread waits until the reserved range of lsn values maps to free |
| 170 |
space in the log files, which is true when: |
| 171 |
|
| 172 |
end_lsn - log.last_checkpoint_lsn <= redo lsn capacity |
| 173 |
|
| 174 |
@remarks |
| 175 |
The waiting is performed by a loop with progressive sleeps. |
| 176 |
The _MONITOR_LOG_ON_FILE_SPACE_* counters track number of iterations |
| 177 |
spent in the waiting loop. If they are not nearby 0, DBA should try to |
| 178 |
use more page cleaner threads, increase size of the log files or ask for |
| 179 |
better storage device. |
| 180 |
|
| 181 |
This mechanism could lead to a __deadlock__, because the user thread waiting |
| 182 |
during commit of mtr, keeps the dirty pages locked, which makes it impossible |
| 183 |
to flush them. Now, if these pages have very old modifications, it could be |
| 184 |
impossible to move checkpoint further without flushing them. In such case the |
| 185 |
log checkpointer thread will be unable to |
| 186 |
[reclaim space in the log files](@ref sect_redo_log_reclaim_space). |
| 187 |
|
| 188 |
To avoid such problems, user threads call log_free_check() from time to time, |
| 189 |
when they don't keep any latches. They try to do it at least every 4 modified |
| 190 |
pages and if they detected that there is not much free space in the log |
| 191 |
files, they wait until the free space is reclaimed (but without holding |
| 192 |
latches!). |
| 193 |
|
| 194 |
@note Note that multiple user threads could check the free space without |
| 195 |
holding latches and then proceed with writes. Therefore this mechanism only |
| 196 |
works because the minimum required free space is based on assumptions: |
| 197 |
- maximum number of such concurrent user threads is limited, |
| 198 |
- maximum size of write between two checks within a thread is limited. |
| 199 |
|
| 200 |
This mechanism does not provide safety when concurrency is not limited! |
| 201 |
In such case we only do the best effort but the deadlock is still possible |
| 202 |
in theory. |
| 203 |
|
| 204 |
@see log_buffer_reserve() |
| 205 |
|
| 206 |
|
| 207 |
@section sect_redo_log_buf_write Copying data to the reserved space |
| 208 |
|
| 209 |
After a range of lsn values has been reserved, the data is copied to the log |
| 210 |
buffer's fragment related to the range of lsn values. |
| 211 |
|
| 212 |
The log buffer is a ring buffer, directly addressed by lsn values, which means |
| 213 |
that there is no need for shifting of data in the log buffer. Byte for a given |
| 214 |
lsn is stored at lsn modulo size of the buffer. It is then easier to reach |
| 215 |
higher concurrency with such the log buffer, because shifting would require |
| 216 |
an exclusive access. |
| 217 |
|
| 218 |
@note However, when writing the wrapped fragment of the log buffer to disk, |
| 219 |
extra IO operation could happen (because we need to copy two disjoint areas |
| 220 |
of memory). First of all, it's a rare case so it shouldn't matter at all. |
| 221 |
Also note that the wrapped fragment results only in additional write to |
| 222 |
system buffer, so still number of real IO operations could stay the same. |
| 223 |
|
| 224 |
Writes to different ranges of lsn values happen concurrently without any |
| 225 |
synchronization. Each user thread writes its own sequence of log records |
| 226 |
to the log buffer, copying them from the internal buffer of the mtr, leaving |
| 227 |
holes for headers and footers of consecutive log blocks. |
| 228 |
|
| 229 |
@note There is some hidden synchronization when multiple user threads write to |
| 230 |
the same memory cache line. That happens when they write to the same 64 bytes, |
| 231 |
because they have reserved small consecutive ranges of lsn values. Fortunately |
| 232 |
each mtr takes in average more than few bytes, which limits number of such user |
| 233 |
threads that meet within a cache line. |
| 234 |
|
| 235 |
When mtr_commit() finishes writing the group of log records, it is responsible |
| 236 |
for updating the _first_rec_group_ field in the header of the block to which |
| 237 |
_end_lsn_ belongs, unless it is the same block to which _start_lsn_ belongs |
| 238 |
(in which case user ending at _start_lsn_ is responsible for the update). |
| 239 |
|
| 240 |
@see log_buffer_write() |
| 241 |
|
| 242 |
|
| 243 |
@section sect_redo_log_buf_add_links_to_recent_written Adding links to the |
| 244 |
recent written buffer |
| 245 |
|
| 246 |
Fragment of the log buffer, which is close to current lsn, is very likely being |
| 247 |
written concurrently by multiple user threads. There is no restriction on order |
| 248 |
in which such concurrent writes might be finished. Each user thread which has |
| 249 |
finished writing, proceeds further without waiting for any other user threads. |
| 250 |
|
| 251 |
@diafile storage/innobase/log/user_thread_writes_to_buffer.dia "One of many concurrent writes" |
| 252 |
|
| 253 |
@note Note that when a user thread has finished writing, still some other user |
| 254 |
threads could be writing their data for smaller lsn values. It is still fine, |
| 255 |
because it is the [log writer thread](@ref sect_redo_log_writer) that needs to |
| 256 |
ensure, that it writes only complete fragments of the log buffer. For that we |
| 257 |
need information about the finished writes. |
| 258 |
|
| 259 |
The log recent written buffer is responsible for tracking which of concurrent |
| 260 |
writes to the log buffer, have been finished. It allows the log writer thread |
| 261 |
to update @ref subsect_redo_log_buf_ready_for_write_lsn, which allows to find |
| 262 |
the next complete fragment of the log buffer to write. It is enough to track |
| 263 |
only recent writes, because we know that up to _log.buf_ready_for_write_lsn_, |
| 264 |
all writes have been finished. Hence this lsn value defines the beginning of |
| 265 |
lsn range represented by the recent written buffer in a given time. The recent |
| 266 |
written buffer is a ring buffer, directly addressed by lsn value. When there |
| 267 |
is no space in the buffer, user thread needs to wait. |
| 268 |
|
| 269 |
@note Size of the log recent written buffer is limited, so concurrency might |
| 270 |
be limited if the recent written buffer is too small and user threads start |
| 271 |
to wait for each other then (indirectly by waiting for the space reclaimed |
| 272 |
in the recent written buffer by the log writer thread). |
| 273 |
|
| 274 |
Let us describe the procedure used for adding the links. |
| 275 |
|
| 276 |
Suppose, user thread has just written some of mtr's log records to a range |
| 277 |
of lsn values _tmp_start_lsn_ .. _tmp_end_lsn_, then: |
| 278 |
|
| 279 |
-# User thread waits for free space in the recent written buffer, until: |
| 280 |
|
| 281 |
tmp_end_lsn - log.buf_ready_for_write_lsn <= S |
| 282 |
|
| 283 |
where _S_ is a number of slots in the log recent_written buffer. |
| 284 |
|
| 285 |
-# User thread adds the link by setting value of slot for _tmp_start_lsn_: |
| 286 |
|
| 287 |
log.recent_written[tmp_start_lsn % S] = tmp_end_lsn |
| 288 |
|
| 289 |
The value gives information about how much to advance lsn when traversing |
| 290 |
the link. |
| 291 |
|
| 292 |
@note Note that possibly _tmp_end_lsn_ < _end_lsn_. In such case, next write |
| 293 |
of log records in the mtr will start at _tmp_end_lsn_. After all the log |
| 294 |
records are written, the _tmp_end_lsn_ should become equal to the _end_lsn_ |
| 295 |
of the reservation (we must not reserve more bytes than we write). |
| 296 |
|
| 297 |
The log writer thread follows path created by the added links, updates |
| 298 |
@ref subsect_redo_log_buf_ready_for_write_lsn and clears the links, allowing |
| 299 |
to reuse them (for lsn larger by _S_). |
| 300 |
|
| 301 |
Before the link is added, release barrier is required, to avoid compile time |
| 302 |
or memory reordering of writes to the log buffer and the recent written buffer. |
| 303 |
It is extremely important to ensure, that write to the log buffer will precede |
| 304 |
write to the recent written buffer. |
| 305 |
|
| 306 |
The same will apply to reads in the log writer thread, so then the log writer |
| 307 |
thread will be sure, that after reading the link from the recent written buffer |
| 308 |
it will read the proper data from the log buffer's fragment related to the |
| 309 |
link. |
| 310 |
|
| 311 |
Copying data and adding links is performed in loop for consecutive log records |
| 312 |
within the group of log records in the mtr. |
| 313 |
|
| 314 |
@note Note that until some user thread finished writing all the log records, |
| 315 |
any log records which have been written to the log buffer for larger lsn |
| 316 |
(by other user threads), cannot be written to disk. The log writer thread |
| 317 |
will stop at the missing links in the log recent written buffer and wait. |
| 318 |
It follows connected links only. |
| 319 |
|
| 320 |
@see log_buffer_write_completed() |
| 321 |
|
| 322 |
|
| 323 |
@section sect_redo_log_mark_dirty_pages Marking pages as dirty |
| 324 |
|
| 325 |
Range of lsn values _start_lsn_ .. _end_lsn_, acquired during the reservation |
| 326 |
of space, represents the whole group of log records. It is used to mark all |
| 327 |
the pages in the mtr as dirty. |
| 328 |
|
| 329 |
@note During recovery the whole mtr needs to be recovered or skipped at all. |
| 330 |
Hence we don't need more detailed ranges of lsn values when marking pages. |
| 331 |
|
| 332 |
Each page modified in the mtr is locked and its _oldest_modification_ is |
| 333 |
checked to see if this is the first modification or the page had already been |
| 334 |
modified when its modification in this mtr started. |
| 335 |
|
| 336 |
Page, which was modified the first time, will have updated: |
| 337 |
- _oldest_modification_ = _start_lsn_, |
| 338 |
- _newest_modification_ = _end_lsn_, |
| 339 |
|
| 340 |
and will be added to the flush list for corresponding buffer pool (buffer pools |
| 341 |
are sharded by page_id). |
| 342 |
|
| 343 |
For other pages, only _newest_modification_ field is updated (with _end_lsn_). |
| 344 |
|
| 345 |
@note Note that some pages could already be modified earlier (in a previous |
| 346 |
mtr) and still unflushed. Such pages would have _oldest_modification_ != 0 |
| 347 |
during this phase and they would belong already to flush lists. Hence it is |
| 348 |
enough to update their _newest_modification_. |
| 349 |
|
| 350 |
|
| 351 |
@section sect_redo_log_add_dirty_pages Adding dirty pages to flush lists |
| 352 |
|
| 353 |
After writes of all log records in a mtr_commit() have been finished, dirty |
| 354 |
pages have to be moved to flush lists. Hopefully, after some time the pages |
| 355 |
will become flushed and space in the log files could be reclaimed. |
| 356 |
|
| 357 |
The procedure for adding pages to flush lists: |
| 358 |
|
| 359 |
-# Wait for the recent closed buffer covering _end_lsn_. |
| 360 |
|
| 361 |
Before moving the pages, user thread waits until there is free space for |
| 362 |
a link pointing from _start_lsn_ to _end_lsn_ in the recent closed buffer. |
| 363 |
The free space is available when: |
| 364 |
|
| 365 |
end_lsn - log.buf_dirty_pages_added_up_to_lsn < L |
| 366 |
|
| 367 |
where _L_ is a number of slots in the log recent closed buffer. |
| 368 |
|
| 369 |
This way we have guarantee, that the maximum delay in flush lists is limited |
| 370 |
by _L_. That's because we disallow adding dirty page with too high lsn value |
| 371 |
until pages with smaller lsn values (smaller by more than _L_), have been |
| 372 |
added! |
| 373 |
|
| 374 |
-# Add the dirty pages to corresponding flush lists. |
| 375 |
|
| 376 |
During this step pages are locked and marked as dirty as described in |
| 377 |
@ref sect_redo_log_mark_dirty_pages. |
| 378 |
|
| 379 |
Multiple user threads could perform this step in any order of them. |
| 380 |
Hence order of dirty pages in a flush list, is not the same as order by |
| 381 |
their oldest modification lsn. |
| 382 |
|
| 383 |
@diafile storage/innobase/log/relaxed_order_of_dirty_pages.dia "Relaxed order of dirty pages" |
| 384 |
|
| 385 |
@note Note that still the @ref subsect_redo_log_buf_dirty_pages_added_up_to_lsn |
| 386 |
cannot be advanced further than to _start_lsn_. That's because the link from |
| 387 |
_start_lsn_ to _end_lsn_, has still not been added at this stage. |
| 388 |
|
| 389 |
@see log_buffer_write_completed_before_dirty_pages_added() |
| 390 |
|
| 391 |
|
| 392 |
@section sect_redo_log_add_link_to_recent_closed Adding link to the log recent |
| 393 |
closed buffer |
| 394 |
|
| 395 |
After all the dirty pages have been added to flush lists, a link pointing from |
| 396 |
_start_lsn_ to _end_lsn_ is added to the log recent closed buffer. |
| 397 |
|
| 398 |
This is performed by user thread, by setting value of slot for start_lsn: |
| 399 |
|
| 400 |
log.recent_closed[start_lsn % L] = end_lsn |
| 401 |
|
| 402 |
where _L_ is size of the log recent closed buffer. The value gives information |
| 403 |
about how much to advance lsn when traversing the link. |
| 404 |
|
| 405 |
@see log_buffer_write_completed_and_dirty_pages_added() |
| 406 |
|
| 407 |
After the link is added, the shared-access for log buffer is released. |
| 408 |
This possibly allows any thread waiting for an exclussive access to proceed. |
| 409 |
|
| 410 |
|
| 411 |
@section sect_redo_log_reclaim_space Reclaiming space in redo log |
| 412 |
|
| 413 |
Recall that recovery always starts at the last written checkpoint lsn. |
| 414 |
Therefore @ref subsect_redo_log_last_checkpoint_lsn defines the beginning of |
| 415 |
the log files. Because size of the log files is fixed, it is easy to determine |
| 416 |
if a given range of lsn values corresponds to free space in the log files or |
| 417 |
not (in which case it would overwrite tail of the redo log for smaller lsns). |
| 418 |
|
| 419 |
Space in the log files is reclaimed by writing a checkpoint for a higher lsn. |
| 420 |
This could be possible when more dirty pages have been flushed. The checkpoint |
| 421 |
cannot be written for higher lsn than the _oldest_modification_ of any of the |
| 422 |
dirty pages (otherwise we would have lost modifications for that page in case |
| 423 |
of crash). It is [log checkpointer thread](@ref sect_redo_log_checkpointer), |
| 424 |
which calculates safe lsn for a next checkpoint |
| 425 |
(@ref subsect_redo_log_available_for_checkpoint_lsn) and writes the checkpoint. |
| 426 |
User threads doing writes to the log buffer, no longer hold mutex, which would |
| 427 |
disallow to determine such lsn and write checkpoint meanwhile. |
| 428 |
|
| 429 |
Suppose user thread has just finished writing to the log buffer, and it is just |
| 430 |
before adding the corresponding dirty pages to flush lists, but suddenly became |
| 431 |
scheduled out. Now, the log checkpointer thread comes in and tries to determine |
| 432 |
lsn available for a next checkpoint. If we allowed the thread to take minimum |
| 433 |
_oldest_modification_ of dirty pages in flush lists and write checkpoint at |
| 434 |
that lsn value, we would logically erase all log records for smaller lsn |
| 435 |
values. However the dirty pages, which the user thread was trying to add to |
| 436 |
flush lists, could have smaller value of _oldest_modification_. Then log |
| 437 |
records protecting the modifications would be logically erased and in case of |
| 438 |
crash we would not be able to recover the pages. |
| 439 |
|
| 440 |
That's why we need to protect from doing checkpoint at such lsn value, which |
| 441 |
would logically erase the just written data to the redo log, until the related |
| 442 |
dirty pages have been added to flush lists. |
| 443 |
|
| 444 |
When user thread has added all the dirty pages related to _start_lsn_ .. |
| 445 |
_end_lsn_, it creates link in the log recent closed buffer, pointing from |
| 446 |
_start_lsn_ to _end_lsn_. The log closer thread tracks the links in the recent |
| 447 |
closed buffer, clears the slots (so they could be safely reused) and updates |
| 448 |
the @ref subsect_redo_log_buf_dirty_pages_added_up_to_lsn, reclaiming space |
| 449 |
in the recent closed buffer and potentially allowing to advance checkpoint |
| 450 |
further. |
| 451 |
|
| 452 |
Order of pages added to flush lists became relaxed so we also cannot rely |
| 453 |
directly on the lsn of the earliest added page to a given flush list. |
| 454 |
It is not guaranteed that it has the minimum _oldest_modification_ anymore. |
| 455 |
However it is guaranteed that it has _oldest_modification_ not higher than |
| 456 |
the minimum by more than _L_. Hence we subtract _L_ from its value and use |
| 457 |
that as lsn available for checkpoint according to the given flush list. |
| 458 |
For more details |
| 459 |
[read about adding dirty pages](@ref sect_redo_log_add_dirty_pages). |
| 460 |
|
| 461 |
@note Note there are two reasons for which lsn available for checkpoint could |
| 462 |
be updated: |
| 463 |
- because @ref subsect_redo_log_buf_dirty_pages_added_up_to_lsn was updated, |
| 464 |
- because the earliest added dirty page in one of flush lists became flushed. |
| 465 |
|
| 466 |
*******************************************************/ |
| 467 |
// clang-format on |
| 468 |
|
| 469 |
/** Waits until there is free space in log buffer up to reserved handle.end_sn. |
| 470 |
If there was no space, it basically waits for log writer thread which copies |
| 471 |
data from log buffer to log files and advances log.write_lsn, reclaiming space |
| 472 |
in the log buffer (it's a ring buffer). |
| 473 |
|
| 474 |
There is a special case - if it turned out, that log buffer is too small for |
| 475 |
the reserved range of lsn values, it resizes the log buffer. |
| 476 |
|
| 477 |
It's used during reservation of lsn values, when the reserved handle.end_sn is |
| 478 |
greater than log.buf_limit_sn. |
| 479 |
|
| 480 |
@param[in,out] log redo log |
| 481 |
@param[in] handle handle for the reservation */ |
| 482 |
static void log_wait_for_space_after_reserving(log_t &log, |
| 483 |
const Log_handle &handle); |
| 484 |
|
| 485 |
/** Waits until there is free space in the log buffer. The free space has |
| 486 |
to be available for range of sn values ending at the provided sn. |
| 487 |
@param[in] log redo log |
| 488 |
@param[in] end_sn end of the range of sn values */ |
| 489 |
static void log_wait_for_space_in_log_buf(log_t &log, sn_t end_sn); |
| 490 |
|
| 491 |
/**************************************************/ /** |
| 492 |
|
| 493 |
@name Locking for the redo log |
| 494 |
|
| 495 |
*******************************************************/ |
| 496 |
|
| 497 |
/** @{ */ |
| 498 |
|
| 499 |
/** Waits for the start_sn unlocked and allowed to write to the buffer. |
| 500 |
@param[in,out] log redo log |
| 501 |
@param[in] start_sn target sn value to start to write */ |
| 502 |
static inline void log_buffer_s_lock_wait(log_t &log, const sn_t start_sn) { |
| 503 |
int64_t signal_count = 0; |
| 504 |
uint32_t i = 0; |
| 505 |
|
| 506 |
if (log.sn_locked.load(std::memory_order_acquire) <= start_sn) { |
| 507 |
do { |
| 508 |
if (srv_spin_wait_delay) { |
| 509 |
ut_delay(ut::random_from_interval_fast(0, srv_spin_wait_delay)); |
| 510 |
} |
| 511 |
if (i < srv_n_spin_wait_rounds) { |
| 512 |
i++; |
| 513 |
} else { |
| 514 |
signal_count = os_event_reset(log.sn_lock_event); |
| 515 |
if ((log.sn.load(std::memory_order_acquire) & SN_LOCKED) == 0 || |
| 516 |
log.sn_locked.load(std::memory_order_acquire) > start_sn) { |
| 517 |
break; |
| 518 |
} |
| 519 |
os_event_wait_time_low(log.sn_lock_event, std::chrono::seconds{1}, |
| 520 |
signal_count); |
| 521 |
} |
| 522 |
} while ((log.sn.load(std::memory_order_acquire) & SN_LOCKED) != 0 && |
| 523 |
log.sn_locked.load(std::memory_order_acquire) <= start_sn); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
/** Acquires the log buffer s-lock. |
| 528 |
And reserve space in the log buffer. |
| 529 |
The corresponding unlock operation is adding link to log.recent_closed. |
| 530 |
@param[in,out] log redo log |
| 531 |
@param[in] len number of data bytes to reserve for write |
| 532 |
@return start sn of reserved */ |
| 533 |
static inline sn_t log_buffer_s_lock_enter_reserve(log_t &log, size_t len) { |
| 534 |
#ifdef UNIV_PFS_RWLOCK |
| 535 |
PSI_rwlock_locker *locker = nullptr; |
| 536 |
PSI_rwlock_locker_state state; |
| 537 |
if (log.pfs_psi != nullptr) { |
| 538 |
if (log.pfs_psi->m_enabled) { |
| 539 |
/* Instrumented to inform we are acquiring a shared rwlock */ |
| 540 |
locker = PSI_RWLOCK_CALL(start_rwlock_rdwait)( |
| 541 |
&state, log.pfs_psi, PSI_RWLOCK_SHAREDLOCK, __FILE__, |
| 542 |
static_cast<uint>(__LINE__)); |
| 543 |
} |
| 544 |
} |
| 545 |
#endif /* UNIV_PFS_RWLOCK */ |
| 546 |
|
| 547 |
/* Reserve space in sequence of data bytes: */ |
| 548 |
sn_t start_sn = log.sn.fetch_add(len); |
| 549 |
if (UNIV_UNLIKELY((start_sn & SN_LOCKED) != 0)) { |
| 550 |
start_sn &= ~SN_LOCKED; |
| 551 |
/* log.sn is locked. Should wait for unlocked. */ |
| 552 |
log_buffer_s_lock_wait(log, start_sn); |
| 553 |
} |
| 554 |
|
| 555 |
ut_d( |
| 556 |
rw_lock_add_debug_info(log.sn_lock_inst, 0, RW_LOCK_S, UT_LOCATION_HERE)); |
| 557 |
#ifdef UNIV_PFS_RWLOCK |
| 558 |
if (locker != nullptr) { |
| 559 |
PSI_RWLOCK_CALL(end_rwlock_rdwait)(locker, 0); |
| 560 |
} |
| 561 |
#endif /* UNIV_PFS_RWLOCK */ |
| 562 |
|
| 563 |
return start_sn; |
| 564 |
} |
| 565 |
|
| 566 |
/** Releases the log buffer s-lock. |
| 567 |
@param[in,out] log redo log |
| 568 |
@param[in] start_lsn start lsn of the reservation |
| 569 |
@param[in] end_lsn end lsn of the reservation */ |
| 570 |
static inline void log_buffer_s_lock_exit_close(log_t &log, lsn_t start_lsn, |
| 571 |
lsn_t end_lsn) { |
| 572 |
#ifdef UNIV_PFS_RWLOCK |
| 573 |
if (log.pfs_psi != nullptr) { |
| 574 |
if (log.pfs_psi->m_enabled) { |
| 575 |
/* Inform performance schema we are unlocking the lock */ |
| 576 |
PSI_RWLOCK_CALL(unlock_rwlock) |
| 577 |
(log.pfs_psi, PSI_RWLOCK_SHAREDUNLOCK); |
| 578 |
} |
| 579 |
} |
| 580 |
#endif /* UNIV_PFS_RWLOCK */ |
| 581 |
ut_d(rw_lock_remove_debug_info(log.sn_lock_inst, 0, RW_LOCK_S)); |
| 582 |
|
| 583 |
log.recent_closed.add_link_advance_tail(start_lsn, end_lsn); |
| 584 |
} |
| 585 |
|
| 586 |
void log_buffer_x_lock_enter(log_t &log) { |
| 587 |
log_sync_point("log_buffer_x_lock_enter_before_lock"); |
| 588 |
|
| 589 |
#ifdef UNIV_PFS_RWLOCK |
| 590 |
PSI_rwlock_locker *locker = nullptr; |
| 591 |
PSI_rwlock_locker_state state; |
| 592 |
if (log.pfs_psi != nullptr) { |
| 593 |
if (log.pfs_psi->m_enabled) { |
| 594 |
/* Record the acquisition of a read-write lock in exclusive |
| 595 |
mode in performance schema */ |
| 596 |
locker = PSI_RWLOCK_CALL(start_rwlock_wrwait)( |
| 597 |
&state, log.pfs_psi, PSI_RWLOCK_EXCLUSIVELOCK, __FILE__, |
| 598 |
static_cast<uint>(__LINE__)); |
| 599 |
} |
| 600 |
} |
| 601 |
#endif /* UNIV_PFS_RWLOCK */ |
| 602 |
|
| 603 |
/* locks log.sn_locked value */ |
| 604 |
mutex_enter(&(log.sn_x_lock_mutex)); |
| 605 |
|
| 606 |
/* locks log.sn value */ |
| 607 |
sn_t sn = log.sn.load(std::memory_order_acquire); |
| 608 |
sn_t sn_locked; |
| 609 |
do { |
| 610 |
ut_ad((sn & SN_LOCKED) == 0); |
| 611 |
sn_locked = sn | SN_LOCKED; |
| 612 |
/* needs to update log.sn_locked before log.sn */ |
| 613 |
/* Indicates x-locked sn value */ |
| 614 |
log.sn_locked.store(sn, std::memory_order_relaxed); |
| 615 |
} while ( |
| 616 |
!log.sn.compare_exchange_weak(sn, sn_locked, std::memory_order_acq_rel)); |
| 617 |
|
| 618 |
/* Some s-lockers might wait for the new log.sn_locked value. */ |
| 619 |
os_event_set(log.sn_lock_event); |
| 620 |
|
| 621 |
if (sn > 0) { |
| 622 |
/* redo log system has been started */ |
| 623 |
const lsn_t current_lsn = log_translate_sn_to_lsn(sn); |
| 624 |
lsn_t closed_lsn = log_buffer_dirty_pages_added_up_to_lsn(log); |
| 625 |
uint32_t i = 0; |
| 626 |
/* must wait for closed_lsn == current_lsn */ |
| 627 |
while (i < srv_n_spin_wait_rounds && closed_lsn < current_lsn) { |
| 628 |
if (srv_spin_wait_delay) { |
| 629 |
ut_delay(ut::random_from_interval_fast(0, srv_spin_wait_delay)); |
| 630 |
} |
| 631 |
i++; |
| 632 |
closed_lsn = log_buffer_dirty_pages_added_up_to_lsn(log); |
| 633 |
} |
| 634 |
if (closed_lsn < current_lsn) { |
| 635 |
log.recent_closed.advance_tail(); |
| 636 |
closed_lsn = log_buffer_dirty_pages_added_up_to_lsn(log); |
| 637 |
} |
| 638 |
if (closed_lsn < current_lsn) { |
| 639 |
std::this_thread::yield(); |
| 640 |
closed_lsn = log_buffer_dirty_pages_added_up_to_lsn(log); |
| 641 |
} |
| 642 |
while (closed_lsn < current_lsn) { |
| 643 |
std::this_thread::sleep_for(std::chrono::microseconds(20)); |
| 644 |
log.recent_closed.advance_tail(); |
| 645 |
closed_lsn = log_buffer_dirty_pages_added_up_to_lsn(log); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
ut_d( |
| 650 |
rw_lock_add_debug_info(log.sn_lock_inst, 0, RW_LOCK_X, UT_LOCATION_HERE)); |
| 651 |
#ifdef UNIV_PFS_RWLOCK |
| 652 |
if (locker != nullptr) { |
| 653 |
PSI_RWLOCK_CALL(end_rwlock_wrwait)(locker, 0); |
| 654 |
} |
| 655 |
#endif /* UNIV_PFS_RWLOCK */ |
| 656 |
|
| 657 |
log_sync_point("log_buffer_x_lock_enter_after_lock"); |
| 658 |
} |
| 659 |
|
| 660 |
void log_buffer_x_lock_exit(log_t &log) { |
| 661 |
log_sync_point("log_buffer_x_lock_exit_before_unlock"); |
| 662 |
|
| 663 |
#ifdef UNIV_PFS_RWLOCK |
| 664 |
if (log.pfs_psi != nullptr) { |
| 665 |
if (log.pfs_psi->m_enabled) { |
| 666 |
/* Inform performance schema we are unlocking the lock */ |
| 667 |
PSI_RWLOCK_CALL(unlock_rwlock) |
| 668 |
(log.pfs_psi, PSI_RWLOCK_EXCLUSIVEUNLOCK); |
| 669 |
} |
| 670 |
} |
| 671 |
#endif /* UNIV_PFS_RWLOCK */ |
| 672 |
ut_d(rw_lock_remove_debug_info(log.sn_lock_inst, 0, RW_LOCK_X)); |
| 673 |
|
| 674 |
/* unlocks log.sn */ |
| 675 |
sn_t sn = log.sn.load(std::memory_order_acquire); |
| 676 |
ut_a((sn & SN_LOCKED) != 0); |
| 677 |
sn_t sn_unlocked; |
| 678 |
do { |
| 679 |
sn_unlocked = sn & ~SN_LOCKED; |
| 680 |
log.sn_locked.store(sn_unlocked, std::memory_order_relaxed); |
| 681 |
} while (!log.sn.compare_exchange_weak(sn, sn_unlocked, |
| 682 |
std::memory_order_acq_rel)); |
| 683 |
os_event_set(log.sn_lock_event); |
| 684 |
|
| 685 |
/* unlocks log.sn_locked */ |
| 686 |
mutex_exit(&(log.sn_x_lock_mutex)); |
| 687 |
|
| 688 |
log_sync_point("log_buffer_x_lock_exit_after_unlock"); |
| 689 |
} |
| 690 |
|
| 691 |
/** @} */ |
| 692 |
|
| 693 |
/**************************************************/ /** |
| 694 |
|
| 695 |
@name Reservation of space in the redo log |
| 696 |
|
| 697 |
*******************************************************/ |
| 698 |
|
| 699 |
/** @{ */ |
| 700 |
|
| 701 |
static void log_wait_for_space_after_reserving(log_t &log, |
| 702 |
const Log_handle &handle) { |
| 703 |
ut_ad(rw_lock_own(log.sn_lock_inst, RW_LOCK_S)); |
| 704 |
|
| 705 |
const sn_t start_sn = log_translate_lsn_to_sn(handle.start_lsn); |
| 706 |
|
| 707 |
const sn_t end_sn = log_translate_lsn_to_sn(handle.end_lsn); |
| 708 |
|
| 709 |
const sn_t len = end_sn - start_sn; |
| 710 |
|
| 711 |
/* If we had not allowed to resize log buffer, it would have |
| 712 |
been sufficient here to simply call: |
| 713 |
- log_wait_for_space_in_log_buf(log, end_sn). |
| 714 |
|
| 715 |
However we do allow, and we need to handle the possible race |
| 716 |
condition, when user tries to set very small log buffer size |
| 717 |
and other threads try to write large groups of log records. |
| 718 |
|
| 719 |
Note that since this point, log.buf_size_sn may only be |
| 720 |
increased from our point of view. That's because: |
| 721 |
|
| 722 |
1. Other threads doing mtr_commit will only try to |
| 723 |
increase the size (if needed). |
| 724 |
|
| 725 |
2. If user wanted to manually resize the log buffer, |
| 726 |
he needs to obtain x-lock for the redo log, but |
| 727 |
we keep s-lock. */ |
| 728 |
|
| 729 |
log_wait_for_space_in_log_buf(log, start_sn); |
| 730 |
|
| 731 |
/* Now start_sn fits the log buffer or is at the boundary. |
| 732 |
Therefore all previous reservations (for smaller LSN), fit |
| 733 |
the log buffer [1]. |
| 734 |
|
| 735 |
We check if len > log.buf_size_sn. If that's the case, our |
| 736 |
range start_sn..end_sn will cover more than size of the log |
| 737 |
buffer and we need to extend the size. Note that users that |
| 738 |
reserved smaller LSN will not try to extend because of [1]. |
| 739 |
Users that reserved larger LSN, will not have their start_sn |
| 740 |
in the log buffer, because our end_sn already does not fit. |
| 741 |
Such users will first wait to reach invariant [1]. */ |
| 742 |
|
| 743 |
log_sync_point("log_wfs_after_reserving_before_buf_size_1"); |
| 744 |
|
| 745 |
if (len > log.buf_size_sn.load()) { |
| 746 |
DBUG_EXECUTE_IF("ib_log_buffer_is_short_crash", DBUG_SUICIDE();); |
| 747 |
|
| 748 |
log_write_up_to(log, log_translate_sn_to_lsn(start_sn), false); |
| 749 |
|
| 750 |
/* Now the whole log has been written to disk up to start_sn, |
| 751 |
so there are no pending writes to log buffer for smaller sn. */ |
| 752 |
log_sync_point("log_wfs_after_reserving_before_buf_size_2"); |
| 753 |
|
| 754 |
/* Reservations for larger LSN could not increase size of log |
| 755 |
buffer as they could not have reached [1], because end_sn did |
| 756 |
not fit the log buffer (end_sn - start_sn > buf_size_sn), and |
| 757 |
next reservations would have their start_sn even greater. */ |
| 758 |
ut_a(len > log.buf_size_sn.load()); |
| 759 |
|
| 760 |
/* Note that the log.write_lsn could not be changed since it |
| 761 |
reached start_sn, until current thread continues and finishes |
| 762 |
writing its data to the log buffer. |
| 763 |
|
| 764 |
Note that any other thread will not attempt to write |
| 765 |
concurrently to the log buffer, because the log buffer |
| 766 |
represents range of sn: |
| 767 |
[start_sn, start_sn + log.buf_size_sn) |
| 768 |
and it holds: |
| 769 |
end_sn > start_sn + log_buf_size_sn. |
| 770 |
This will not change until we finished resizing log |
| 771 |
buffer and updated log.buf_size_sn, which therefore |
| 772 |
must happen at the very end of the resize procedure. */ |
| 773 |
ut_a(log_translate_lsn_to_sn(log.write_lsn.load()) == start_sn); |
| 774 |
|
| 775 |
ib::info(ER_IB_MSG_1231) |
| 776 |
<< "The transaction log size is too large" |
| 777 |
<< " for srv_log_buffer_size (" << len << " > " |
| 778 |
<< log.buf_size_sn.load() << "). Trying to extend it."; |
| 779 |
|
| 780 |
/* Resize without extra locking required. |
| 781 |
|
| 782 |
We cannot call log_buffer_resize() because it would try |
| 783 |
to acquire x-lock for the redo log and we keep s-lock. |
| 784 |
|
| 785 |
We already have ensured, that there are no possible |
| 786 |
concurrent writes to the log buffer. Note, we have also |
| 787 |
ensured that log writer finished writing up to start_sn. |
| 788 |
|
| 789 |
However, for extra safety, we prefer to acquire writer_mutex, |
| 790 |
and checkpointer_mutex. We consider this rare event. */ |
| 791 |
|
| 792 |
log_checkpointer_mutex_enter(log); |
| 793 |
log_writer_mutex_enter(log); |
| 794 |
|
| 795 |
/* We multiply size at least by 1.382 to avoid case |
| 796 |
in which we keep resizing by few bytes only. */ |
| 797 |
|
| 798 |
lsn_t new_lsn_size = log_translate_sn_to_lsn( |
| 799 |
static_cast<sn_t>(1.382 * len + OS_FILE_LOG_BLOCK_SIZE)); |
| 800 |
|
| 801 |
new_lsn_size = ut_uint64_align_up(new_lsn_size, OS_FILE_LOG_BLOCK_SIZE); |
| 802 |
|
| 803 |
log_buffer_resize_low(log, new_lsn_size, handle.start_lsn); |
| 804 |
|
| 805 |
log_writer_mutex_exit(log); |
| 806 |
log_checkpointer_mutex_exit(log); |
| 807 |
|
| 808 |
} else { |
| 809 |
/* Note that the size cannot get decreased. |
| 810 |
We are safe to continue. */ |
| 811 |
} |
| 812 |
|
| 813 |
ut_a(len <= log.buf_size_sn.load()); |
| 814 |
|
| 815 |
log_wait_for_space_in_log_buf(log, end_sn); |
| 816 |
} |
| 817 |
|
| 818 |
void log_update_buf_limit(log_t &log) { |
| 819 |
log_update_buf_limit(log, log.write_lsn.load()); |
| 820 |
} |
| 821 |
|
| 822 |
void log_update_buf_limit(log_t &log, lsn_t write_lsn) { |
| 823 |
ut_ad(write_lsn <= log.write_lsn.load()); |
| 824 |
|
| 825 |
const sn_t limit_for_end = log_translate_lsn_to_sn(write_lsn) + |
| 826 |
log.buf_size_sn.load() - |
| 827 |
2 * OS_FILE_LOG_BLOCK_SIZE; |
| 828 |
|
| 829 |
log.buf_limit_sn.store(limit_for_end); |
| 830 |
} |
| 831 |
|
| 832 |
static void log_wait_for_space_in_log_buf(log_t &log, sn_t end_sn) { |
| 833 |
lsn_t lsn; |
| 834 |
Wait_stats wait_stats; |
| 835 |
|
| 836 |
const sn_t write_sn = log_translate_lsn_to_sn(log.write_lsn.load()); |
| 837 |
|
| 838 |
log_sync_point("log_wait_for_space_in_buf_middle"); |
| 839 |
|
| 840 |
const sn_t buf_size_sn = log.buf_size_sn.load(); |
| 841 |
|
| 842 |
if (end_sn + OS_FILE_LOG_BLOCK_SIZE <= write_sn + buf_size_sn) { |
| 843 |
return; |
| 844 |
} |
| 845 |
|
| 846 |
/* We preserve this counter for backward compatibility with 5.7. */ |
| 847 |
srv_stats.log_waits.inc(); |
| 848 |
|
| 849 |
lsn = log_translate_sn_to_lsn(end_sn + OS_FILE_LOG_BLOCK_SIZE - buf_size_sn); |
| 850 |
|
| 851 |
wait_stats = log_write_up_to(log, lsn, false); |
| 852 |
|
| 853 |
MONITOR_INC_WAIT_STATS(MONITOR_LOG_ON_BUFFER_SPACE_, wait_stats); |
| 854 |
|
| 855 |
ut_a(end_sn + OS_FILE_LOG_BLOCK_SIZE <= |
| 856 |
log_translate_lsn_to_sn(log.write_lsn.load()) + buf_size_sn); |
| 857 |
} |
| 858 |
|
| 859 |
Log_handle log_buffer_reserve(log_t &log, size_t len) { |
| 860 |
Log_handle handle; |
| 861 |
|
| 862 |
/* In 5.7, we incremented log_write_requests for each single |
| 863 |
write to log buffer in commit of mini-transaction. |
| 864 |
|
| 865 |
However, writes which were solved by log_reserve_and_write_fast |
| 866 |
missed to increment the counter. Therefore it wasn't reliable. |
| 867 |
|
| 868 |
Dimitri and I have decided to change meaning of the counter |
| 869 |
to reflect mtr commit rate. */ |
| 870 |
srv_stats.log_write_requests.inc(); |
| 871 |
|
| 872 |
ut_ad(srv_shutdown_state_matches([](auto state) { |
| 873 |
return state <= SRV_SHUTDOWN_FLUSH_PHASE || |
| 874 |
state == SRV_SHUTDOWN_EXIT_THREADS; |
| 875 |
})); |
| 876 |
|
| 877 |
ut_a(len > 0); |
| 878 |
|
| 879 |
/* Reserve space in sequence of data bytes: */ |
| 880 |
const sn_t start_sn = log_buffer_s_lock_enter_reserve(log, len); |
| 881 |
|
| 882 |
/* Ensure that redo log has been initialized properly. */ |
| 883 |
ut_a(start_sn > 0); |
| 884 |
|
| 885 |
#ifdef UNIV_DEBUG |
| 886 |
if (!recv_recovery_is_on()) { |
| 887 |
log_background_threads_active_validate(log); |
| 888 |
} else { |
| 889 |
ut_a(!recv_no_ibuf_operations); |
| 890 |
} |
| 891 |
#endif |
| 892 |
|
| 893 |
/* Headers in redo blocks are not calculated to sn values: */ |
| 894 |
const sn_t end_sn = start_sn + len; |
| 895 |
|
| 896 |
log_sync_point("log_buffer_reserve_before_buf_limit_sn"); |
| 897 |
|
| 898 |
/* Translate sn to lsn (which includes also headers in redo blocks): */ |
| 899 |
handle.start_lsn = log_translate_sn_to_lsn(start_sn); |
| 900 |
handle.end_lsn = log_translate_sn_to_lsn(end_sn); |
| 901 |
|
| 902 |
if (unlikely(end_sn > log.buf_limit_sn.load())) { |
| 903 |
log_wait_for_space_after_reserving(log, handle); |
| 904 |
} |
| 905 |
|
| 906 |
ut_a(log_is_data_lsn(handle.start_lsn)); |
| 907 |
ut_a(log_is_data_lsn(handle.end_lsn)); |
| 908 |
|
| 909 |
return handle; |
| 910 |
} |
| 911 |
|
| 912 |
/** @} */ |
| 913 |
|
| 914 |
/**************************************************/ /** |
| 915 |
|
| 916 |
@name Writing to the redo log buffer |
| 917 |
|
| 918 |
*******************************************************/ |
| 919 |
|
| 920 |
/** @{ */ |
| 921 |
|
| 922 |
lsn_t log_buffer_write(log_t &log, const byte *str, size_t str_len, |
| 923 |
lsn_t start_lsn) { |
| 924 |
ut_ad(rw_lock_own(log.sn_lock_inst, RW_LOCK_S)); |
| 925 |
|
| 926 |
ut_a(log.buf != nullptr); |
| 927 |
ut_a(log.buf_size > 0); |
| 928 |
ut_a(log.buf_size % OS_FILE_LOG_BLOCK_SIZE == 0); |
| 929 |
ut_a(str != nullptr); |
| 930 |
ut_a(str_len > 0); |
| 931 |
|
| 932 |
/* We should first resize the log buffer, if str_len is that big. */ |
| 933 |
ut_a(str_len < log.buf_size_sn.load()); |
| 934 |
|
| 935 |
/* The start_lsn points a data byte (not a header of log block). */ |
| 936 |
ut_a(log_is_data_lsn(start_lsn)); |
| 937 |
|
| 938 |
/* We neither write with holes, nor overwrite any fragments of data. */ |
| 939 |
ut_ad(log.write_lsn.load() <= start_lsn); |
| 940 |
ut_ad(log_buffer_ready_for_write_lsn(log) <= start_lsn); |
| 941 |
|
| 942 |
/* That's only used in the assertion at the very end. */ |
| 943 |
const sn_t end_sn = log_translate_lsn_to_sn(start_lsn) + sn_t{str_len}; |
| 944 |
|
| 945 |
/* A guard used to detect when we should wrap (to avoid overflowing |
| 946 |
outside the log buffer). */ |
| 947 |
byte *buf_end = log.buf + log.buf_size; |
| 948 |
|
| 949 |
/* Pointer to next data byte to set within the log buffer. */ |
| 950 |
byte *ptr = log.buf + (start_lsn % log.buf_size); |
| 951 |
|
| 952 |
/* Lsn value for the next byte to copy. */ |
| 953 |
lsn_t lsn = start_lsn; |
| 954 |
|
| 955 |
/* Copy log records to the reserved space in the log buffer. |
| 956 |
Decrease number of bytes to copy (str_len) after some are |
| 957 |
copied. Proceed until number of bytes to copy reaches zero. */ |
| 958 |
while (true) { |
| 959 |
/* Calculate offset from the beginning of log block. */ |
| 960 |
const auto offset = lsn % OS_FILE_LOG_BLOCK_SIZE; |
| 961 |
|
| 962 |
ut_a(offset >= LOG_BLOCK_HDR_SIZE); |
| 963 |
ut_a(offset < OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE); |
| 964 |
|
| 965 |
/* Calculate how many free data bytes are available |
| 966 |
within current log block. */ |
| 967 |
const auto left = OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE - offset; |
| 968 |
|
| 969 |
ut_a(left > 0); |
| 970 |
ut_a(left < OS_FILE_LOG_BLOCK_SIZE); |
| 971 |
|
| 972 |
size_t len, lsn_diff; |
| 973 |
|
| 974 |
if (left > str_len) { |
| 975 |
/* There are enough free bytes to finish copying |
| 976 |
the remaining part, leaving at least single free |
| 977 |
data byte in the log block. */ |
| 978 |
|
| 979 |
len = str_len; |
| 980 |
|
| 981 |
lsn_diff = str_len; |
| 982 |
|
| 983 |
} else { |
| 984 |
/* We have more to copy than the current log block |
| 985 |
has remaining data bytes, or exactly the same. |
| 986 |
|
| 987 |
In both cases, next lsn value will belong to the |
| 988 |
next log block. Copy data up to the end of the |
| 989 |
current log block and start a next iteration if |
| 990 |
there is more to copy. */ |
| 991 |
|
| 992 |
len = left; |
| 993 |
|
| 994 |
lsn_diff = left + LOG_BLOCK_TRL_SIZE + LOG_BLOCK_HDR_SIZE; |
| 995 |
} |
| 996 |
|
| 997 |
ut_a(len > 0); |
| 998 |
ut_a(ptr + len <= buf_end); |
| 999 |
|
| 1000 |
log_sync_point("log_buffer_write_before_memcpy"); |
| 1001 |
|
| 1002 |
/* This is the critical memcpy operation, which copies data |
| 1003 |
from internal mtr's buffer to the shared log buffer. */ |
| 1004 |
std::memcpy(ptr, str, len); |
| 1005 |
|
| 1006 |
ut_a(len <= str_len); |
| 1007 |
|
| 1008 |
str_len -= len; |
| 1009 |
str += len; |
| 1010 |
lsn += lsn_diff; |
| 1011 |
ptr += lsn_diff; |
| 1012 |
|
| 1013 |
ut_a(log_is_data_lsn(lsn)); |
| 1014 |
|
| 1015 |
if (ptr >= buf_end) { |
| 1016 |
/* Wrap - next copy operation will write at the |
| 1017 |
beginning of the log buffer. */ |
| 1018 |
|
| 1019 |
ptr -= log.buf_size; |
| 1020 |
} |
| 1021 |
|
| 1022 |
if (lsn_diff > left) { |
| 1023 |
/* We have crossed boundaries between consecutive log |
| 1024 |
blocks. Either we finish in next block, in which case |
| 1025 |
user will set the proper first_rec_group field after |
| 1026 |
this function is finished, or we finish even further, |
| 1027 |
in which case next block should have 0. In both cases, |
| 1028 |
we reset next block's value to 0 now, and in the first |
| 1029 |
case, user will simply overwrite it afterwards. */ |
| 1030 |
|
| 1031 |
ut_a((uintptr_t(ptr) % OS_FILE_LOG_BLOCK_SIZE) == LOG_BLOCK_HDR_SIZE); |
| 1032 |
|
| 1033 |
ut_a((uintptr_t(ptr) & ~uintptr_t(LOG_BLOCK_HDR_SIZE)) % |
| 1034 |
OS_FILE_LOG_BLOCK_SIZE == |
| 1035 |
0); |
| 1036 |
|
| 1037 |
log_block_set_first_rec_group( |
| 1038 |
reinterpret_cast<byte *>(uintptr_t(ptr) & |
| 1039 |
~uintptr_t(LOG_BLOCK_HDR_SIZE)), |
| 1040 |
0); |
| 1041 |
|
| 1042 |
if (str_len == 0) { |
| 1043 |
/* We have finished at the boundary. */ |
| 1044 |
break; |
| 1045 |
} |
| 1046 |
|
| 1047 |
} else { |
| 1048 |
/* Nothing more to copy - we have finished! */ |
| 1049 |
break; |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
ut_a(ptr >= log.buf); |
| 1054 |
ut_a(ptr <= buf_end); |
| 1055 |
ut_a(buf_end == log.buf + log.buf_size); |
| 1056 |
ut_a(log_translate_lsn_to_sn(lsn) == end_sn); |
| 1057 |
|
| 1058 |
return lsn; |
| 1059 |
} |
| 1060 |
|
| 1061 |
void log_buffer_write_completed(log_t &log, lsn_t start_lsn, lsn_t end_lsn) { |
| 1062 |
ut_ad(rw_lock_own(log.sn_lock_inst, RW_LOCK_S)); |
| 1063 |
|
| 1064 |
ut_a(log_is_data_lsn(start_lsn)); |
| 1065 |
ut_a(log_is_data_lsn(end_lsn)); |
| 1066 |
ut_a(end_lsn > start_lsn); |
| 1067 |
|
| 1068 |
/* Let M = log.recent_written_size (number of slots). |
| 1069 |
For any integer k, all lsn values equal to: start_lsn + k*M |
| 1070 |
correspond to the same slot, and only the smallest of them |
| 1071 |
may use the slot. At most one of them can fit the range |
| 1072 |
[log.buf_ready_for_write_lsn..log.buf_ready_ready_write_lsn+M). |
| 1073 |
Any smaller values have already used the slot. Hence, we just |
| 1074 |
need to wait until start_lsn will fit the mentioned range. */ |
| 1075 |
|
| 1076 |
uint64_t wait_loops = 0; |
| 1077 |
|
| 1078 |
while (!log.recent_written.has_space(start_lsn)) { |
| 1079 |
os_event_set(log.writer_event); |
| 1080 |
++wait_loops; |
| 1081 |
std::this_thread::sleep_for(std::chrono::microseconds(20)); |
| 1082 |
} |
| 1083 |
|
| 1084 |
if (unlikely(wait_loops != 0)) { |
| 1085 |
MONITOR_INC_VALUE(MONITOR_LOG_ON_RECENT_WRITTEN_WAIT_LOOPS, wait_loops); |
| 1086 |
} |
| 1087 |
|
| 1088 |
/* Disallow reordering of writes to log buffer after this point. |
| 1089 |
This is actually redundant, because we use seq_cst inside the |
| 1090 |
log.recent_written.add_link(). However, we've decided to leave |
| 1091 |
the separate acq-rel synchronization between user threads and |
| 1092 |
log writer. Reasons: |
| 1093 |
1. Not to rely on internals of Link_buf::add_link. |
| 1094 |
2. Stress that this synchronization is required in |
| 1095 |
case someone decided to weaken memory ordering |
| 1096 |
inside Link_buf. */ |
| 1097 |
std::atomic_thread_fence(std::memory_order_release); |
| 1098 |
|
| 1099 |
log_sync_point("log_buffer_write_completed_before_store"); |
| 1100 |
|
| 1101 |
ut_ad(log.write_lsn.load() <= start_lsn); |
| 1102 |
ut_ad(log_buffer_ready_for_write_lsn(log) <= start_lsn); |
| 1103 |
|
| 1104 |
/* Note that end_lsn will not point to just before footer, |
| 1105 |
because we have already validated that end_lsn is valid. */ |
| 1106 |
log.recent_written.add_link_advance_tail(start_lsn, end_lsn); |
| 1107 |
|
| 1108 |
/* if someone is waiting for, set the event. (if possible) */ |
| 1109 |
lsn_t ready_lsn = log_buffer_ready_for_write_lsn(log); |
| 1110 |
|
| 1111 |
if (log.current_ready_waiting_lsn > 0 && |
| 1112 |
log.current_ready_waiting_lsn <= ready_lsn && |
| 1113 |
!os_event_is_set(log.closer_event) && |
| 1114 |
log_closer_mutex_enter_nowait(log) == 0) { |
| 1115 |
if (log.current_ready_waiting_lsn > 0 && |
| 1116 |
log.current_ready_waiting_lsn <= ready_lsn && |
| 1117 |
!os_event_is_set(log.closer_event)) { |
| 1118 |
log.current_ready_waiting_lsn = 0; |
| 1119 |
os_event_set(log.closer_event); |
| 1120 |
} |
| 1121 |
log_closer_mutex_exit(log); |
| 1122 |
} |
| 1123 |
} |
| 1124 |
|
| 1125 |
void log_wait_for_space_in_log_recent_closed(log_t &log, lsn_t lsn) { |
| 1126 |
ut_a(log_is_data_lsn(lsn)); |
| 1127 |
|
| 1128 |
ut_ad(lsn >= log_buffer_dirty_pages_added_up_to_lsn(log)); |
| 1129 |
|
| 1130 |
uint64_t wait_loops = 0; |
| 1131 |
|
| 1132 |
while (!log.recent_closed.has_space(lsn)) { |
| 1133 |
++wait_loops; |
| 1134 |
std::this_thread::sleep_for(std::chrono::microseconds(20)); |
| 1135 |
} |
| 1136 |
|
| 1137 |
if (unlikely(wait_loops != 0)) { |
| 1138 |
MONITOR_INC_VALUE(MONITOR_LOG_ON_RECENT_CLOSED_WAIT_LOOPS, wait_loops); |
| 1139 |
} |
| 1140 |
} |
| 1141 |
|
| 1142 |
void log_buffer_close(log_t &log, const Log_handle &handle) { |
| 1143 |
const lsn_t start_lsn = handle.start_lsn; |
| 1144 |
const lsn_t end_lsn = handle.end_lsn; |
| 1145 |
|
| 1146 |
ut_a(log_is_data_lsn(start_lsn)); |
| 1147 |
ut_a(log_is_data_lsn(end_lsn)); |
| 1148 |
ut_a(end_lsn > start_lsn); |
| 1149 |
|
| 1150 |
ut_ad(start_lsn >= log_buffer_dirty_pages_added_up_to_lsn(log)); |
| 1151 |
|
| 1152 |
ut_ad(rw_lock_own(log.sn_lock_inst, RW_LOCK_S)); |
| 1153 |
|
| 1154 |
std::atomic_thread_fence(std::memory_order_release); |
| 1155 |
|
| 1156 |
log_sync_point("log_buffer_write_completed_dpa_before_store"); |
| 1157 |
|
| 1158 |
log_buffer_s_lock_exit_close(log, start_lsn, end_lsn); |
| 1159 |
} |
| 1160 |
|
| 1161 |
void log_buffer_set_first_record_group(log_t &log, lsn_t rec_group_end_lsn) { |
| 1162 |
ut_ad(rw_lock_own(log.sn_lock_inst, RW_LOCK_S)); |
| 1163 |
|
| 1164 |
ut_a(log_is_data_lsn(rec_group_end_lsn)); |
| 1165 |
|
| 1166 |
const lsn_t last_block_lsn = |
| 1167 |
ut_uint64_align_down(rec_group_end_lsn, OS_FILE_LOG_BLOCK_SIZE); |
| 1168 |
|
| 1169 |
byte *buf = log.buf; |
| 1170 |
|
| 1171 |
ut_a(buf != nullptr); |
| 1172 |
|
| 1173 |
byte *last_block_ptr = buf + (last_block_lsn % log.buf_size); |
| 1174 |
|
| 1175 |
log_sync_point("log_buffer_set_first_record_group_before_update"); |
| 1176 |
|
| 1177 |
/* User thread needs to set proper first_rec_group value before |
| 1178 |
link is added to recent written buffer. */ |
| 1179 |
ut_ad(log_buffer_ready_for_write_lsn(log) < rec_group_end_lsn); |
| 1180 |
|
| 1181 |
/* This also guarantees, that log buffer could not become resized |
| 1182 |
meanwhile. */ |
| 1183 |
ut_a(buf + (last_block_lsn % log.buf_size) == last_block_ptr); |
| 1184 |
|
| 1185 |
/* This field is not overwritten. It is set to 0, when user thread |
| 1186 |
crosses boundaries of consecutive log blocks. */ |
| 1187 |
ut_a(log_block_get_first_rec_group(last_block_ptr) == 0); |
| 1188 |
|
| 1189 |
log_block_set_first_rec_group(last_block_ptr, |
| 1190 |
rec_group_end_lsn % OS_FILE_LOG_BLOCK_SIZE); |
| 1191 |
} |
| 1192 |
|
| 1193 |
void log_buffer_flush_to_disk(log_t &log, bool sync) { |
| 1194 |
ut_a(!srv_read_only_mode); |
| 1195 |
ut_a(!recv_recovery_is_on()); |
| 1196 |
|
| 1197 |
const lsn_t lsn = log_get_lsn(log); |
| 1198 |
|
| 1199 |
/* Google's patch introduced log_buffer_sync_in_background which was calling |
| 1200 |
log_write_up_to, and this is the left-over from that. */ |
| 1201 |
log_write_up_to(log, lsn, sync); |
| 1202 |
} |
| 1203 |
|
| 1204 |
void log_buffer_sync_in_background() { |
| 1205 |
log_t &log = *log_sys; |
| 1206 |
|
| 1207 |
/* Just to be sure not to miss advance */ |
| 1208 |
log.recent_closed.advance_tail(); |
| 1209 |
|
| 1210 |
/* If the log flusher thread is working, no need to call. */ |
| 1211 |
if (log.writer_threads_paused.load(std::memory_order_acquire)) { |
| 1212 |
log.recent_written.advance_tail(); |
| 1213 |
log_buffer_flush_to_disk(log, true); |
| 1214 |
} |
| 1215 |
} |
| 1216 |
|
| 1217 |
void log_buffer_get_last_block(log_t &log, lsn_t &last_lsn, byte *last_block, |
| 1218 |
uint32_t &block_len) { |
| 1219 |
ut_ad(last_block != nullptr); |
| 1220 |
|
| 1221 |
/* We acquire x-lock for the log buffer to prevent: |
| 1222 |
a) resize of the log buffer |
| 1223 |
b) overwrite of the fragment which we are copying */ |
| 1224 |
|
| 1225 |
log_buffer_x_lock_enter(log); |
| 1226 |
|
| 1227 |
/* Because we have acquired x-lock for the log buffer, current |
| 1228 |
lsn will not advance and all users that reserved smaller lsn |
| 1229 |
have finished writing to the log buffer. */ |
| 1230 |
|
| 1231 |
last_lsn = log_get_lsn(log); |
| 1232 |
|
| 1233 |
byte *buf = log.buf; |
| 1234 |
|
| 1235 |
ut_a(buf != nullptr); |
| 1236 |
|
| 1237 |
/* Copy last block from current buffer. */ |
| 1238 |
|
| 1239 |
const lsn_t block_lsn = |
| 1240 |
ut_uint64_align_down(last_lsn, OS_FILE_LOG_BLOCK_SIZE); |
| 1241 |
|
| 1242 |
byte *src_block = buf + block_lsn % log.buf_size; |
| 1243 |
|
| 1244 |
const auto data_len = last_lsn % OS_FILE_LOG_BLOCK_SIZE; |
| 1245 |
|
| 1246 |
ut_ad(data_len >= LOG_BLOCK_HDR_SIZE); |
| 1247 |
|
| 1248 |
std::memcpy(last_block, src_block, data_len); |
| 1249 |
|
| 1250 |
/* We have copied data from the log buffer. We can release |
| 1251 |
the x-lock and let new writes to the buffer go. Since now, |
| 1252 |
we work only with our local copy of the data. */ |
| 1253 |
|
| 1254 |
log_buffer_x_lock_exit(log); |
| 1255 |
|
| 1256 |
std::memset(last_block + data_len, 0x00, OS_FILE_LOG_BLOCK_SIZE - data_len); |
| 1257 |
|
| 1258 |
Log_data_block_header block_header; |
| 1259 |
block_header.set_lsn(block_lsn); |
| 1260 |
block_header.m_data_len = data_len; |
| 1261 |
block_header.m_first_rec_group = log_block_get_first_rec_group(last_block); |
| 1262 |
ut_ad(block_header.m_first_rec_group <= data_len); |
| 1263 |
|
| 1264 |
log_data_block_header_serialize(block_header, last_block); |
| 1265 |
|
| 1266 |
block_len = OS_FILE_LOG_BLOCK_SIZE; |
| 1267 |
} |
| 1268 |
|
| 1269 |
/** @} */ |
| 1270 |
|
| 1271 |
/**************************************************/ /** |
| 1272 |
|
| 1273 |
@name Traversing links in the redo log recent buffers |
| 1274 |
|
| 1275 |
@todo Consider refactoring to extract common logic of |
| 1276 |
two recent buffers to a common class (Links_buffer ?). |
| 1277 |
|
| 1278 |
*******************************************************/ |
| 1279 |
|
| 1280 |
/** @{ */ |
| 1281 |
|
| 1282 |
void log_advance_ready_for_write_lsn(log_t &log) { |
| 1283 |
ut_ad(log_writer_mutex_own(log)); |
| 1284 |
ut_d(log_writer_thread_active_validate()); |
| 1285 |
|
| 1286 |
const lsn_t write_lsn = log.write_lsn.load(); |
| 1287 |
|
| 1288 |
const auto write_max_size = srv_log_write_max_size; |
| 1289 |
|
| 1290 |
ut_a(write_max_size > 0); |
| 1291 |
|
| 1292 |
auto stop_condition = [&](lsn_t prev_lsn, lsn_t next_lsn) { |
| 1293 |
ut_a(log_is_data_lsn(prev_lsn)); |
| 1294 |
ut_a(log_is_data_lsn(next_lsn)); |
| 1295 |
|
| 1296 |
ut_a(next_lsn > prev_lsn); |
| 1297 |
ut_a(prev_lsn >= write_lsn); |
| 1298 |
|
| 1299 |
log_sync_point("log_advance_ready_for_write_before_reclaim"); |
| 1300 |
|
| 1301 |
return prev_lsn - write_lsn >= write_max_size; |
| 1302 |
}; |
| 1303 |
|
| 1304 |
const lsn_t previous_lsn = log_buffer_ready_for_write_lsn(log); |
| 1305 |
|
| 1306 |
ut_a(previous_lsn >= write_lsn); |
| 1307 |
|
| 1308 |
if (log.recent_written.advance_tail_until(stop_condition)) { |
| 1309 |
log_sync_point("log_advance_ready_for_write_before_update"); |
| 1310 |
|
| 1311 |
/* Validation of recent_written is optional because |
| 1312 |
it takes significant time (delaying the log writer). */ |
| 1313 |
if (log_test != nullptr && |
| 1314 |
log_test->enabled(Log_test::Options::VALIDATE_RECENT_WRITTEN)) { |
| 1315 |
/* All links between ready_lsn and lsn have |
| 1316 |
been traversed. The slots can't be re-used |
| 1317 |
before we updated the tail. */ |
| 1318 |
log.recent_written.validate_no_links(previous_lsn, |
| 1319 |
log_buffer_ready_for_write_lsn(log)); |
| 1320 |
} |
| 1321 |
|
| 1322 |
ut_a(log_buffer_ready_for_write_lsn(log) > previous_lsn); |
| 1323 |
|
| 1324 |
std::atomic_thread_fence(std::memory_order_acquire); |
| 1325 |
} |
| 1326 |
} |
| 1327 |
|
| 1328 |
/** @} */ |
| 1329 |
|
| 1330 |
#endif /* !UNIV_HOTBACKUP */ |