| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2013, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
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, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/**************************************************/ /** |
| 29 |
@file include/log0sys.h |
| 30 |
|
| 31 |
Redo log - the log_sys. |
| 32 |
|
| 33 |
*******************************************************/ |
| 34 |
|
| 35 |
#ifndef log0sys_h |
| 36 |
#define log0sys_h |
| 37 |
|
| 38 |
#include "univ.i" |
| 39 |
|
| 40 |
/* Log_consumer */ |
| 41 |
#include "log0consumer.h" |
| 42 |
|
| 43 |
/* Log_files_capacity */ |
| 44 |
#include "log0files_capacity.h" |
| 45 |
|
| 46 |
/* Log_files_dict */ |
| 47 |
#include "log0files_dict.h" |
| 48 |
|
| 49 |
/* lsn_t, Log_clock, ... */ |
| 50 |
#include "log0types.h" |
| 51 |
|
| 52 |
/* os_event_t */ |
| 53 |
#include "os0event.h" |
| 54 |
|
| 55 |
/* OS_FILE_LOG_BLOCK_SIZE, os_offset_t */ |
| 56 |
#include "os0file.h" |
| 57 |
|
| 58 |
/* rw_lock_t */ |
| 59 |
#include "sync0rw.h" |
| 60 |
|
| 61 |
/* ut::INNODB_CACHE_LINE_SIZE */ |
| 62 |
#include "ut0cpu_cache.h" |
| 63 |
|
| 64 |
/* Link_buf */ |
| 65 |
#include "ut0link_buf.h" |
| 66 |
|
| 67 |
/* ib_mutex_t */ |
| 68 |
#include "ut0mutex.h" |
| 69 |
|
| 70 |
/* aligned_array_pointer, ut::vector */ |
| 71 |
#include "ut0new.h" |
| 72 |
|
| 73 |
class THD; |
| 74 |
|
| 75 |
/** Redo log - single data structure with state of the redo log system. |
| 76 |
In future, one could consider splitting this to multiple data structures. */ |
| 77 |
struct alignas(ut::INNODB_CACHE_LINE_SIZE) log_t { |
| 78 |
#ifndef UNIV_HOTBACKUP |
| 79 |
|
| 80 |
/**************************************************/ /** |
| 81 |
|
| 82 |
@name Users writing to log buffer |
| 83 |
|
| 84 |
*******************************************************/ |
| 85 |
|
| 86 |
/** @{ */ |
| 87 |
|
| 88 |
/** Event used for locking sn */ |
| 89 |
os_event_t sn_lock_event; |
| 90 |
|
| 91 |
#ifdef UNIV_DEBUG |
| 92 |
/** The rw_lock instance only for the debug info list */ |
| 93 |
/* NOTE: Just "rw_lock_t sn_lock_inst;" and direct minimum initialization |
| 94 |
seem to hit the bug of Sun Studio of Solaris. */ |
| 95 |
rw_lock_t *sn_lock_inst; |
| 96 |
#endif /* UNIV_DEBUG */ |
| 97 |
|
| 98 |
/** Current sn value. Used to reserve space in the redo log, |
| 99 |
and used to acquire an exclusive access to the log buffer. |
| 100 |
Represents number of data bytes that have ever been reserved. |
| 101 |
Bytes of headers and footers of log blocks are not included. |
| 102 |
Its highest bit is used for locking the access to the log buffer. */ |
| 103 |
alignas(ut::INNODB_CACHE_LINE_SIZE) atomic_sn_t sn; |
| 104 |
|
| 105 |
/** Intended sn value while x-locked. */ |
| 106 |
atomic_sn_t sn_locked; |
| 107 |
|
| 108 |
/** Mutex which can be used for x-lock sn value */ |
| 109 |
mutable ib_mutex_t sn_x_lock_mutex; |
| 110 |
|
| 111 |
/** Aligned log buffer. Committing mini-transactions write there |
| 112 |
redo records, and the log_writer thread writes the log buffer to |
| 113 |
disk in background. |
| 114 |
Protected by: locking sn not to add. */ |
| 115 |
alignas(ut::INNODB_CACHE_LINE_SIZE) |
| 116 |
ut::aligned_array_pointer<byte, LOG_BUFFER_ALIGNMENT> buf; |
| 117 |
|
| 118 |
/** Size of the log buffer expressed in number of data bytes, |
| 119 |
that is excluding bytes for headers and footers of log blocks. */ |
| 120 |
atomic_sn_t buf_size_sn; |
| 121 |
|
| 122 |
/** Size of the log buffer expressed in number of total bytes, |
| 123 |
that is including bytes for headers and footers of log blocks. */ |
| 124 |
size_t buf_size; |
| 125 |
|
| 126 |
#ifdef UNIV_PFS_RWLOCK |
| 127 |
/** The instrumentation hook. |
| 128 |
@remarks This field is rarely modified, so can not be the cause of |
| 129 |
frequent cache line invalidations. However, user threads read it only during |
| 130 |
mtr.commit(), which in some scenarios happens rarely enough, that the cache |
| 131 |
line containing pfs_psi is evicted between mtr.commit()s causing a cache miss, |
| 132 |
a stall and in consequence MACHINE_CLEARS during mtr.commit(). As this miss |
| 133 |
seems inevitable, we at least want to make it really worth it. So, we put the |
| 134 |
pfs_psi in the same cache line which contains buf, buf_size_sn and buf_size, |
| 135 |
which are also needed during mtr.commit(). This way instead of two separate |
| 136 |
cache misses, we have just one. |
| 137 |
TBD: We could additionally use `lfence` to limit MACHINE_CLEARS.*/ |
| 138 |
struct PSI_rwlock *pfs_psi; |
| 139 |
#endif /* UNIV_PFS_RWLOCK */ |
| 140 |
|
| 141 |
/** The recent written buffer. |
| 142 |
Protected by: locking sn not to add. */ |
| 143 |
alignas(ut::INNODB_CACHE_LINE_SIZE) Link_buf<lsn_t> recent_written; |
| 144 |
|
| 145 |
/** Used for pausing the log writer threads. |
| 146 |
When paused, each user thread should write log as in the former version. */ |
| 147 |
std::atomic_bool writer_threads_paused; |
| 148 |
|
| 149 |
/** Some threads waiting for the ready for write lsn by closer_event. */ |
| 150 |
lsn_t current_ready_waiting_lsn; |
| 151 |
|
| 152 |
/** current_ready_waiting_lsn is waited using this sig_count. */ |
| 153 |
int64_t current_ready_waiting_sig_count; |
| 154 |
|
| 155 |
/** The recent closed buffer. |
| 156 |
Protected by: locking sn not to add. */ |
| 157 |
alignas(ut::INNODB_CACHE_LINE_SIZE) Link_buf<lsn_t> recent_closed; |
| 158 |
|
| 159 |
/** @} */ |
| 160 |
|
| 161 |
/**************************************************/ /** |
| 162 |
|
| 163 |
@name Users <=> writer |
| 164 |
|
| 165 |
*******************************************************/ |
| 166 |
|
| 167 |
/** @{ */ |
| 168 |
|
| 169 |
/** Maximum sn up to which there is free space in both the log buffer |
| 170 |
and the log files. This is limitation for the end of any write to the |
| 171 |
log buffer. Threads, which are limited need to wait, and possibly they |
| 172 |
hold latches of dirty pages making a deadlock possible. |
| 173 |
Protected by: writer_mutex (writes). */ |
| 174 |
alignas(ut::INNODB_CACHE_LINE_SIZE) atomic_sn_t buf_limit_sn; |
| 175 |
|
| 176 |
/** Up to this lsn, data has been written to disk (fsync not required). |
| 177 |
Protected by: writer_mutex (writes). */ |
| 178 |
alignas(ut::INNODB_CACHE_LINE_SIZE) atomic_lsn_t write_lsn; |
| 179 |
|
| 180 |
/** Unaligned pointer to array with events, which are used for |
| 181 |
notifications sent from the log write notifier thread to user threads. |
| 182 |
The notifications are sent when write_lsn is advanced. User threads |
| 183 |
wait for write_lsn >= lsn, for some lsn. Log writer advances the |
| 184 |
write_lsn and notifies the log write notifier, which notifies all users |
| 185 |
interested in nearby lsn values (lsn belonging to the same log block). |
| 186 |
Note that false wake-ups are possible, in which case user threads |
| 187 |
simply retry waiting. */ |
| 188 |
alignas(ut::INNODB_CACHE_LINE_SIZE) os_event_t *write_events; |
| 189 |
|
| 190 |
/** Number of entries in the array with writer_events. */ |
| 191 |
size_t write_events_size; |
| 192 |
|
| 193 |
/** Approx. number of requests to write/flush redo since startup. */ |
| 194 |
alignas(ut::INNODB_CACHE_LINE_SIZE) |
| 195 |
std::atomic<uint64_t> write_to_file_requests_total; |
| 196 |
|
| 197 |
/** How often redo write/flush is requested in average. |
| 198 |
Measures in microseconds. Log threads do not spin when |
| 199 |
the write/flush requests are not frequent. */ |
| 200 |
alignas(ut::INNODB_CACHE_LINE_SIZE) |
| 201 |
std::atomic<std::chrono::microseconds> write_to_file_requests_interval; |
| 202 |
static_assert(decltype(write_to_file_requests_interval)::is_always_lock_free); |
| 203 |
|
| 204 |
/** @} */ |
| 205 |
|
| 206 |
/**************************************************/ /** |
| 207 |
|
| 208 |
@name Users <=> flusher |
| 209 |
|
| 210 |
*******************************************************/ |
| 211 |
|
| 212 |
/** @{ */ |
| 213 |
|
| 214 |
/** Unaligned pointer to array with events, which are used for |
| 215 |
notifications sent from the log flush notifier thread to user threads. |
| 216 |
The notifications are sent when flushed_to_disk_lsn is advanced. |
| 217 |
User threads wait for flushed_to_disk_lsn >= lsn, for some lsn. |
| 218 |
Log flusher advances the flushed_to_disk_lsn and notifies the |
| 219 |
log flush notifier, which notifies all users interested in nearby lsn |
| 220 |
values (lsn belonging to the same log block). Note that false |
| 221 |
wake-ups are possible, in which case user threads simply retry |
| 222 |
waiting. */ |
| 223 |
alignas(ut::INNODB_CACHE_LINE_SIZE) os_event_t *flush_events; |
| 224 |
|
| 225 |
/** Number of entries in the array with events. */ |
| 226 |
size_t flush_events_size; |
| 227 |
|
| 228 |
/** This event is in the reset state when a flush is running; |
| 229 |
a thread should wait for this without owning any of redo mutexes, |
| 230 |
but NOTE that to reset this event, the thread MUST own the writer_mutex */ |
| 231 |
os_event_t old_flush_event; |
| 232 |
|
| 233 |
/** Up to this lsn data has been flushed to disk (fsynced). */ |
| 234 |
alignas(ut::INNODB_CACHE_LINE_SIZE) atomic_lsn_t flushed_to_disk_lsn; |
| 235 |
|
| 236 |
/** @} */ |
| 237 |
|
| 238 |
/**************************************************/ /** |
| 239 |
|
| 240 |
@name Log flusher thread |
| 241 |
|
| 242 |
*******************************************************/ |
| 243 |
|
| 244 |
/** @{ */ |
| 245 |
|
| 246 |
/** Last flush start time. Updated just before fsync starts. */ |
| 247 |
alignas(ut::INNODB_CACHE_LINE_SIZE) Log_clock_point last_flush_start_time; |
| 248 |
|
| 249 |
/** Last flush end time. Updated just after fsync is finished. |
| 250 |
If smaller than start time, then flush operation is pending. */ |
| 251 |
Log_clock_point last_flush_end_time; |
| 252 |
|
| 253 |
/** Flushing average time (in microseconds). */ |
| 254 |
double flush_avg_time; |
| 255 |
|
| 256 |
/** Mutex which can be used to pause log flusher thread. */ |
| 257 |
mutable ib_mutex_t flusher_mutex; |
| 258 |
|
| 259 |
alignas(ut::INNODB_CACHE_LINE_SIZE) os_event_t flusher_event; |
| 260 |
|
| 261 |
/** @} */ |
| 262 |
|
| 263 |
/**************************************************/ /** |
| 264 |
|
| 265 |
@name Log writer thread |
| 266 |
|
| 267 |
*******************************************************/ |
| 268 |
|
| 269 |
/** @{ */ |
| 270 |
|
| 271 |
/** Size of buffer used for the write-ahead (in bytes). */ |
| 272 |
alignas(ut::INNODB_CACHE_LINE_SIZE) uint32_t write_ahead_buf_size; |
| 273 |
|
| 274 |
/** Aligned buffer used for some of redo log writes. Data is copied |
| 275 |
there from the log buffer and written to disk, in following cases: |
| 276 |
- when writing ahead full kernel page to avoid read-on-write issue, |
| 277 |
- to copy, prepare and write the incomplete block of the log buffer |
| 278 |
(because mini-transactions might be writing new redo records to |
| 279 |
the block in parallel, when the block is being written to disk) */ |
| 280 |
ut::aligned_array_pointer<byte, LOG_WRITE_AHEAD_BUFFER_ALIGNMENT> |
| 281 |
write_ahead_buf; |
| 282 |
|
| 283 |
/** Up to this file offset in the log files, the write-ahead |
| 284 |
has been done or is not required (for any other reason). */ |
| 285 |
os_offset_t write_ahead_end_offset; |
| 286 |
|
| 287 |
/** File within which write_lsn is located, so the newest file in m_files |
| 288 |
in the same time - updates are protected by the m_files_mutex. This field |
| 289 |
exists, because the log_writer thread needs to locate offsets each time |
| 290 |
it writes data blocks to disk, but we do not want to acquire and release |
| 291 |
the m_files_mutex for each such write, because that would slow down the |
| 292 |
log_writer thread a lot. Instead of that, the log_writer uses this object |
| 293 |
to locate the offsets. |
| 294 |
|
| 295 |
Updates of this field require two mutexes: writer_mutex and m_files_mutex. |
| 296 |
Its m_id is updated only when the write_lsn moves to the next log file. */ |
| 297 |
Log_file m_current_file{m_files_ctx, m_encryption_metadata}; |
| 298 |
|
| 299 |
/** Handle for the opened m_current_file. The log_writer uses this handle |
| 300 |
to do writes (protected by writer_mutex). The log_flusher uses this handle |
| 301 |
to do fsyncs (protected by flusher_mutex). Both these threads might use |
| 302 |
this handle in parallel. The required synchronization between writes and |
| 303 |
fsyncs will happen on the OS side. When m_current_file is repointed to |
| 304 |
other file, this field is also updated, in the same critical section. |
| 305 |
Updates of this field are protected by: writer_mutex, m_files_mutex |
| 306 |
and flusher_mutex acquired all together. The reason for flusher_mutex |
| 307 |
is to avoid a need to acquire / release m_files_mutex in the log_flusher |
| 308 |
thread for each fsync. Instead of that, the log_flusher thread keeps the |
| 309 |
log_flusher_mutex, which is released less often, but still prevents from |
| 310 |
updates of this field. */ |
| 311 |
Log_file_handle m_current_file_handle{m_encryption_metadata}; |
| 312 |
|
| 313 |
/** True iff the log writer has entered extra writer margin and still |
| 314 |
hasn't exited since then. Each time the log_writer enters that margin, |
| 315 |
it pauses all user threads at log_free_check() calls and emits warning |
| 316 |
to the log. When the writer exits the extra margin, notice is emitted. |
| 317 |
Protected by: log_limits_mutex and writer_mutex. */ |
| 318 |
bool m_writer_inside_extra_margin; |
| 319 |
|
| 320 |
#endif /* !UNIV_HOTBACKUP */ |
| 321 |
|
| 322 |
/** Number of performed IO operations (only for printing stats). */ |
| 323 |
uint64_t n_log_ios; |
| 324 |
|
| 325 |
#ifndef UNIV_HOTBACKUP |
| 326 |
|
| 327 |
/** Mutex which can be used to pause log writer thread. */ |
| 328 |
mutable ib_mutex_t writer_mutex; |
| 329 |
|
| 330 |
#ifdef UNIV_DEBUG |
| 331 |
/** THD used by the log_writer thread. */ |
| 332 |
THD *m_writer_thd; |
| 333 |
#endif /* UNIV_DEBUG */ |
| 334 |
|
| 335 |
alignas(ut::INNODB_CACHE_LINE_SIZE) os_event_t writer_event; |
| 336 |
|
| 337 |
/** A recently seen value of log_consumer_get_oldest()->get_consumed_lsn(). |
| 338 |
It serves as a lower bound for future values of this expression, because it is |
| 339 |
guaranteed to be monotonic in time: each individual consumer can only go |
| 340 |
forward, and new consumers must start at least from checkpoint lsn, and the |
| 341 |
checkpointer is always one of the consumers. |
| 342 |
Protected by: writer_mutex. */ |
| 343 |
lsn_t m_oldest_need_lsn_lowerbound; |
| 344 |
|
| 345 |
/** @} */ |
| 346 |
|
| 347 |
/**************************************************/ /** |
| 348 |
|
| 349 |
@name Log closer thread |
| 350 |
|
| 351 |
*******************************************************/ |
| 352 |
|
| 353 |
/** @{ */ |
| 354 |
|
| 355 |
/** Event used by the log closer thread to wait for tasks. */ |
| 356 |
alignas(ut::INNODB_CACHE_LINE_SIZE) os_event_t closer_event; |
| 357 |
|
| 358 |
/** Mutex which can be used to pause log closer thread. */ |
| 359 |
mutable ib_mutex_t closer_mutex; |
| 360 |
|
| 361 |
/** @} */ |
| 362 |
|
| 363 |
/**************************************************/ /** |
| 364 |
|
| 365 |
@name Log flusher <=> flush_notifier |
| 366 |
|
| 367 |
*******************************************************/ |
| 368 |
|
| 369 |
/** @{ */ |
| 370 |
|
| 371 |
/** Event used by the log flusher thread to notify the log flush |
| 372 |
notifier thread, that it should proceed with notifying user threads |
| 373 |
waiting for the advanced flushed_to_disk_lsn (because it has been |
| 374 |
advanced). */ |
| 375 |
alignas(ut::INNODB_CACHE_LINE_SIZE) os_event_t flush_notifier_event; |
| 376 |
|
| 377 |
/** The next flushed_to_disk_lsn can be waited using this sig_count. */ |
| 378 |
int64_t current_flush_sig_count; |
| 379 |
|
| 380 |
/** Mutex which can be used to pause log flush notifier thread. */ |
| 381 |
mutable ib_mutex_t flush_notifier_mutex; |
| 382 |
|
| 383 |
/** @} */ |
| 384 |
|
| 385 |
/**************************************************/ /** |
| 386 |
|
| 387 |
@name Log writer <=> write_notifier |
| 388 |
|
| 389 |
*******************************************************/ |
| 390 |
|
| 391 |
/** @{ */ |
| 392 |
|
| 393 |
/** Mutex which can be used to pause log write notifier thread. */ |
| 394 |
alignas(ut::INNODB_CACHE_LINE_SIZE) mutable ib_mutex_t write_notifier_mutex; |
| 395 |
|
| 396 |
/** Event used by the log writer thread to notify the log write |
| 397 |
notifier thread, that it should proceed with notifying user threads |
| 398 |
waiting for the advanced write_lsn (because it has been advanced). */ |
| 399 |
alignas(ut::INNODB_CACHE_LINE_SIZE) os_event_t write_notifier_event; |
| 400 |
|
| 401 |
/** @} */ |
| 402 |
|
| 403 |
/**************************************************/ /** |
| 404 |
|
| 405 |
@name Log files management |
| 406 |
|
| 407 |
*******************************************************/ |
| 408 |
|
| 409 |
/** @{ */ |
| 410 |
|
| 411 |
/** Mutex protecting set of existing log files and their meta data. */ |
| 412 |
alignas(ut::INNODB_CACHE_LINE_SIZE) mutable ib_mutex_t m_files_mutex; |
| 413 |
|
| 414 |
/** Context for all operations on redo log files from log0files_io.h. */ |
| 415 |
Log_files_context m_files_ctx; |
| 416 |
|
| 417 |
/** The in-memory dictionary of log files. |
| 418 |
Protected by: m_files_mutex. */ |
| 419 |
Log_files_dict m_files{m_files_ctx}; |
| 420 |
|
| 421 |
/** Number of existing unused files (those with _tmp suffix). |
| 422 |
Protected by: m_files_mutex. */ |
| 423 |
size_t m_unused_files_count; |
| 424 |
|
| 425 |
/** Size of each unused redo log file, to which recently all unused |
| 426 |
redo log files became resized. Expressed in bytes. */ |
| 427 |
os_offset_t m_unused_file_size; |
| 428 |
|
| 429 |
/** Capacity limits for the redo log. Responsible for resize. |
| 430 |
Mutex protection is decided per each Log_files_capacity method. */ |
| 431 |
Log_files_capacity m_capacity; |
| 432 |
|
| 433 |
/** True iff log_writer is waiting for a next log file available. |
| 434 |
Protected by: m_files_mutex. */ |
| 435 |
bool m_requested_files_consumption; |
| 436 |
|
| 437 |
/** Statistics related to redo log files consumption and creation. |
| 438 |
Protected by: m_files_mutex. */ |
| 439 |
Log_files_stats m_files_stats; |
| 440 |
|
| 441 |
/** Event used by log files governor thread to wait. */ |
| 442 |
os_event_t m_files_governor_event; |
| 443 |
|
| 444 |
/** Event used by other threads to wait until log files governor finished |
| 445 |
its next iteration. This is useful when some sys_var gets changed to wait |
| 446 |
until log files governor re-computed everything and then check if the |
| 447 |
concurrency_margin is safe to emit warning if needed (the warning would |
| 448 |
still belong to the sys_var's SET GLOBAL statement then). */ |
| 449 |
os_event_t m_files_governor_iteration_event; |
| 450 |
|
| 451 |
/** False if log files governor thread is allowed to add new redo records. |
| 452 |
This is set as intention, to tell the log files governor about what it is |
| 453 |
allowed to do. To ensure that the log_files_governor is aware of what has |
| 454 |
been told, user needs to wait on @see m_no_more_dummy_records_promised. */ |
| 455 |
std::atomic_bool m_no_more_dummy_records_requested; |
| 456 |
|
| 457 |
/** False if the log files governor thread is allowed to add new dummy redo |
| 458 |
records. This is set to true only by the log_files_governor thread, and |
| 459 |
after it observed @see m_no_more_dummy_records_requested being true. |
| 460 |
It can be used to wait until the log files governor thread promises not to |
| 461 |
generate any more dummy redo records. */ |
| 462 |
std::atomic_bool m_no_more_dummy_records_promised; |
| 463 |
|
| 464 |
#ifdef UNIV_DEBUG |
| 465 |
/** THD used by the log_files_governor thread. */ |
| 466 |
THD *m_files_governor_thd; |
| 467 |
#endif /* UNIV_DEBUG */ |
| 468 |
|
| 469 |
/** Event used for waiting on next file available. Used by log writer |
| 470 |
thread to wait when it needs to produce a next log file but there are |
| 471 |
no free (consumed) log files available. */ |
| 472 |
os_event_t m_file_removed_event; |
| 473 |
|
| 474 |
/** Buffer that contains encryption meta data encrypted with master key. |
| 475 |
Protected by: m_files_mutex */ |
| 476 |
byte m_encryption_buf[OS_FILE_LOG_BLOCK_SIZE]; |
| 477 |
|
| 478 |
#endif /* !UNIV_HOTBACKUP */ |
| 479 |
|
| 480 |
/** Encryption metadata. This member is passed to Log_file_handle objects |
| 481 |
created for redo log files. In particular, the m_current_file_handle has |
| 482 |
a reference to this field. When encryption metadata is updated, it needs |
| 483 |
to be written to the redo log file's header. Also, each write performed |
| 484 |
by the log_writer thread needs to use m_encryption_metadata (it's passed |
| 485 |
by reference to the m_current_file_handle) and the log_writer does not |
| 486 |
acquire m_files_mutex for its writes (it is a hot path and it's better to |
| 487 |
keep it shorter). Therefore it's been decided that updates of this field |
| 488 |
require both m_files_mutex and writer_mutex. |
| 489 |
Protected by: m_files_mutex, writer_mutex */ |
| 490 |
Encryption_metadata m_encryption_metadata; |
| 491 |
|
| 492 |
#ifndef UNIV_HOTBACKUP |
| 493 |
|
| 494 |
/** @} */ |
| 495 |
|
| 496 |
/**************************************************/ /** |
| 497 |
|
| 498 |
@name Consumers |
| 499 |
|
| 500 |
*******************************************************/ |
| 501 |
|
| 502 |
/** @{ */ |
| 503 |
|
| 504 |
/** Set of registered redo log consumers. Note, that this object |
| 505 |
is not responsible for freeing them (does not claim to be owner). |
| 506 |
If you wanted to register or unregister a redo log consumer, then |
| 507 |
please use following functions: @see log_consumer_register() and |
| 508 |
@see log_consumer_unregister(). The details of implementation |
| 509 |
related to redo log consumers can be found in log0consumer.cc. |
| 510 |
Protected by: m_files_mutex (unless it is the startup phase or |
| 511 |
the shutdown phase). */ |
| 512 |
ut::unordered_set<Log_consumer *> m_consumers; |
| 513 |
|
| 514 |
/** @} */ |
| 515 |
|
| 516 |
/**************************************************/ /** |
| 517 |
|
| 518 |
@name Maintenance |
| 519 |
|
| 520 |
*******************************************************/ |
| 521 |
|
| 522 |
/** @{ */ |
| 523 |
|
| 524 |
/** Used for stopping the log background threads. */ |
| 525 |
alignas(ut::INNODB_CACHE_LINE_SIZE) std::atomic_bool should_stop_threads; |
| 526 |
|
| 527 |
/** Event used for pausing the log writer threads. */ |
| 528 |
os_event_t writer_threads_resume_event; |
| 529 |
|
| 530 |
/** Used for resuming write notifier thread */ |
| 531 |
atomic_lsn_t write_notifier_resume_lsn; |
| 532 |
|
| 533 |
/** Used for resuming flush notifier thread */ |
| 534 |
atomic_lsn_t flush_notifier_resume_lsn; |
| 535 |
|
| 536 |
/** Number of total I/O operations performed when we printed |
| 537 |
the statistics last time. */ |
| 538 |
mutable uint64_t n_log_ios_old; |
| 539 |
|
| 540 |
/** Wall time when we printed the statistics last time. */ |
| 541 |
mutable time_t last_printout_time; |
| 542 |
|
| 543 |
/** @} */ |
| 544 |
|
| 545 |
/**************************************************/ /** |
| 546 |
|
| 547 |
@name Recovery |
| 548 |
|
| 549 |
*******************************************************/ |
| 550 |
|
| 551 |
/** @{ */ |
| 552 |
|
| 553 |
/** Lsn from which recovery has been started. */ |
| 554 |
lsn_t recovered_lsn; |
| 555 |
|
| 556 |
/** Format of the redo log: e.g., Log_format::CURRENT. */ |
| 557 |
Log_format m_format; |
| 558 |
|
| 559 |
/** Log creator name */ |
| 560 |
std::string m_creator_name; |
| 561 |
|
| 562 |
/** Log flags */ |
| 563 |
Log_flags m_log_flags; |
| 564 |
|
| 565 |
/** Log UUID */ |
| 566 |
Log_uuid m_log_uuid; |
| 567 |
|
| 568 |
/** Used only in recovery: recovery scan succeeded up to this lsn. */ |
| 569 |
lsn_t m_scanned_lsn; |
| 570 |
|
| 571 |
#ifdef UNIV_DEBUG |
| 572 |
|
| 573 |
/** When this is set, writing to the redo log should be disabled. |
| 574 |
We check for this in functions that write to the redo log. */ |
| 575 |
bool disable_redo_writes; |
| 576 |
|
| 577 |
#endif /* UNIV_DEBUG */ |
| 578 |
|
| 579 |
/** @} */ |
| 580 |
|
| 581 |
/**************************************************/ /** |
| 582 |
|
| 583 |
@name Fields protected by the log_limits_mutex. |
| 584 |
Related to free space in the redo log. |
| 585 |
|
| 586 |
*******************************************************/ |
| 587 |
|
| 588 |
/** @{ */ |
| 589 |
|
| 590 |
/** Mutex which protects fields: available_for_checkpoint_lsn, |
| 591 |
requested_checkpoint_lsn. It also synchronizes updates of: |
| 592 |
free_check_limit_lsn, concurrency_margin, dict_persist_margin. |
| 593 |
It protects reads and writes of m_writer_inside_extra_margin. |
| 594 |
It also protects the srv_checkpoint_disabled (together with the |
| 595 |
checkpointer_mutex). */ |
| 596 |
alignas(ut::INNODB_CACHE_LINE_SIZE) mutable ib_mutex_t limits_mutex; |
| 597 |
|
| 598 |
/** A new checkpoint could be written for this lsn value. |
| 599 |
Up to this lsn value, all dirty pages have been added to flush |
| 600 |
lists and flushed. Updated in the log checkpointer thread by |
| 601 |
taking minimum oldest_modification out of the last dirty pages |
| 602 |
from each flush list. However it will not be bigger than the |
| 603 |
current value of log.buf_dirty_pages_added_up_to_lsn. |
| 604 |
Read by: user threads when requesting fuzzy checkpoint |
| 605 |
Read by: log_print() (printing status of redo) |
| 606 |
Updated by: log_checkpointer |
| 607 |
Protected by: limits_mutex. */ |
| 608 |
lsn_t available_for_checkpoint_lsn; |
| 609 |
|
| 610 |
/** When this is larger than the latest checkpoint, the log checkpointer |
| 611 |
thread will be forced to write a new checkpoint (unless the new latest |
| 612 |
checkpoint lsn would still be smaller than this value). |
| 613 |
Read by: log_checkpointer |
| 614 |
Updated by: user threads (log_free_check() or for sharp checkpoint) |
| 615 |
Protected by: limits_mutex. */ |
| 616 |
lsn_t requested_checkpoint_lsn; |
| 617 |
|
| 618 |
/** Maximum lsn allowed for checkpoint by dict_persist or zero. |
| 619 |
This will be set by dict_persist_to_dd_table_buffer(), which should |
| 620 |
be always called before really making a checkpoint. |
| 621 |
If non-zero, up to this lsn value, dynamic metadata changes have been |
| 622 |
written back to mysql.innodb_dynamic_metadata under dict_persist->mutex |
| 623 |
protection. All dynamic metadata changes after this lsn have to |
| 624 |
be kept in redo logs, but not discarded. If zero, just ignore it. |
| 625 |
Updated by: DD (when persisting dynamic meta data) |
| 626 |
Updated by: log_checkpointer (reset when checkpoint is written) |
| 627 |
Protected by: limits_mutex. */ |
| 628 |
lsn_t dict_max_allowed_checkpoint_lsn; |
| 629 |
|
| 630 |
/** If should perform checkpoints every innodb_log_checkpoint_every ms. |
| 631 |
Disabled during startup / shutdown. Enabled in srv_start_threads. |
| 632 |
Updated by: starting thread (srv_start_threads) |
| 633 |
Read by: log_checkpointer */ |
| 634 |
bool periodical_checkpoints_enabled; |
| 635 |
|
| 636 |
/** If checkpoints are allowed. When this is set to false, neither new |
| 637 |
checkpoints might be written nor lsn available for checkpoint might be |
| 638 |
updated. This is useful in recovery period, when neither flush lists can |
| 639 |
be trusted nor DD dynamic metadata redo records might be reclaimed. |
| 640 |
This is never set from true to false after log_start(). */ |
| 641 |
std::atomic_bool m_allow_checkpoints; |
| 642 |
|
| 643 |
/** Maximum lsn up to which there is free space in the redo log. |
| 644 |
Threads check this limit and compare to current lsn, when they |
| 645 |
are outside mini-transactions and hold no latches. The formula used |
| 646 |
to compute the limitation takes into account maximum size of mtr and |
| 647 |
thread concurrency to include proper margins and avoid issues with |
| 648 |
race condition (in which all threads check the limitation and then |
| 649 |
all proceed with their mini-transactions). Also extra margin is |
| 650 |
there for dd table buffer cache (dict_persist_margin). |
| 651 |
Read by: user threads (log_free_check()) |
| 652 |
Updated by: log_checkpointer (after update of checkpoint_lsn) |
| 653 |
Updated by: log_writer (after pausing/resuming user threads) |
| 654 |
Updated by: DD (after update of dict_persist_margin) |
| 655 |
Protected by (updates only): limits_mutex. */ |
| 656 |
atomic_lsn_t free_check_limit_lsn; |
| 657 |
|
| 658 |
/** Margin used in calculation of @see free_check_limit_lsn. |
| 659 |
Protected by (updates only): limits_mutex. */ |
| 660 |
atomic_sn_t concurrency_margin; |
| 661 |
|
| 662 |
/** True iff current concurrency_margin isn't truncated because of too small |
| 663 |
redo log capacity. |
| 664 |
Protected by (updates only): limits_mutex. */ |
| 665 |
std::atomic<bool> concurrency_margin_is_safe; |
| 666 |
|
| 667 |
/** Margin used in calculation of @see free_check_limit_lsn. |
| 668 |
Read by: page_cleaners, log_checkpointer |
| 669 |
Updated by: DD |
| 670 |
Protected by (updates only): limits_mutex. */ |
| 671 |
atomic_sn_t dict_persist_margin; |
| 672 |
|
| 673 |
/** @} */ |
| 674 |
|
| 675 |
/**************************************************/ /** |
| 676 |
|
| 677 |
@name Log checkpointer thread |
| 678 |
|
| 679 |
*******************************************************/ |
| 680 |
|
| 681 |
/** @{ */ |
| 682 |
|
| 683 |
/** Event used by the log checkpointer thread to wait for requests. */ |
| 684 |
alignas(ut::INNODB_CACHE_LINE_SIZE) os_event_t checkpointer_event; |
| 685 |
|
| 686 |
/** Mutex which can be used to pause log checkpointer thread. |
| 687 |
This is used by log_position_lock() together with log_buffer_x_lock(), |
| 688 |
to pause any changes to current_lsn or last_checkpoint_lsn. */ |
| 689 |
mutable ib_mutex_t checkpointer_mutex; |
| 690 |
|
| 691 |
/** Latest checkpoint lsn. |
| 692 |
Read by: user threads, log_print (no protection) |
| 693 |
Read by: log_writer (under writer_mutex) |
| 694 |
Updated by: log_checkpointer (under both mutexes) |
| 695 |
Protected by (updates only): checkpointer_mutex + writer_mutex. */ |
| 696 |
atomic_lsn_t last_checkpoint_lsn; |
| 697 |
|
| 698 |
/** Next checkpoint header to use. |
| 699 |
Updated by: log_checkpointer |
| 700 |
Protected by: checkpointer_mutex */ |
| 701 |
Log_checkpoint_header_no next_checkpoint_header_no; |
| 702 |
|
| 703 |
/** Event signaled when last_checkpoint_lsn is advanced by |
| 704 |
the log_checkpointer thread. */ |
| 705 |
os_event_t next_checkpoint_event; |
| 706 |
|
| 707 |
/** Latest checkpoint wall time. |
| 708 |
Used by (private): log_checkpointer. */ |
| 709 |
Log_clock_point last_checkpoint_time; |
| 710 |
|
| 711 |
/** Redo log consumer which is always registered and which is responsible |
| 712 |
for protecting redo log records at lsn >= last_checkpoint_lsn. */ |
| 713 |
Log_checkpoint_consumer m_checkpoint_consumer{*this}; |
| 714 |
|
| 715 |
/** Throttles writing to log a message about the user threads not able to |
| 716 |
reserve space in the redo log. The default value is 10s. */ |
| 717 |
ib::Throttler m_THREADS_WAITING_FOR_REDO_throttler; |
| 718 |
|
| 719 |
#ifdef UNIV_DEBUG |
| 720 |
/** THD used by the log_checkpointer thread. */ |
| 721 |
THD *m_checkpointer_thd; |
| 722 |
#endif /* UNIV_DEBUG */ |
| 723 |
|
| 724 |
/** @} */ |
| 725 |
|
| 726 |
#endif /* !UNIV_HOTBACKUP */ |
| 727 |
}; |
| 728 |
|
| 729 |
/** Redo log system (singleton). */ |
| 730 |
extern log_t *log_sys; |
| 731 |
|
| 732 |
#ifdef UNIV_PFS_MEMORY |
| 733 |
/* PFS key for the redo log buffer's memory */ |
| 734 |
extern PSI_memory_key log_buffer_memory_key; |
| 735 |
#endif /* UNIV_PFS_MEMORY */ |
| 736 |
|
| 737 |
#endif /* !log0sys_h */ |