MySQL 8.0.40
Source Code Documentation
log0chkp.h File Reference

Redo log functions related to checkpointing and log free check. More...

#include "log0log.h"
#include "log0sys.h"
#include "log0types.h"
#include "srv0srv.h"
#include "sync0debug.h"
#include "sync0types.h"

Go to the source code of this file.

Functions

Log - basic information about checkpoints.
lsn_t log_get_checkpoint_lsn (const log_t &log)
 Gets the last checkpoint lsn stored and flushed to disk. More...
 
lsn_t log_get_checkpoint_age (const log_t &log)
 Calculates age of current checkpoint as number of bytes since last checkpoint. More...
 
Log_checkpoint_header_no log_next_checkpoint_header (Log_checkpoint_header_no checkpoint_header_no)
 Provides opposite checkpoint header number to the given checkpoint header number. More...
 
lsn_t log_sync_flush_lsn (log_t &log)
 Computes lsn up to which sync flush should be done or returns 0 if there is no need to execute sync flush now. More...
 
Log - requests to make checkpoint.
void log_request_checkpoint (log_t &log, bool sync)
 Requests a fuzzy checkpoint write (for currently available lsn). More...
 
void log_request_checkpoint_in_next_file (log_t &log)
 Requests a checkpoint written in the next log file (not in the one, to which current log.last_checkpoint_lsn belongs to). More...
 
bool log_request_latest_checkpoint (log_t &log, lsn_t &requested_lsn)
 Requests a checkpoint at the current lsn. More...
 
bool log_make_latest_checkpoint (log_t &log)
 Make a checkpoint at the current lsn. More...
 
bool log_make_latest_checkpoint ()
 Make a checkpoint at the current lsn. More...
 
void log_set_dict_max_allowed_checkpoint_lsn (log_t &log, lsn_t max_lsn)
 Updates the field log.dict_max_allowed_checkpoint_lsn. More...
 
Log - concurrency margins.
sn_t log_concurrency_margin (lsn_t log_capacity, bool &is_safe)
 Computes concurrency margin to be used within log_free_check calls, for a given redo log capacity (soft_logical_capacity). More...
 
void log_update_concurrency_margin (log_t &log)
 Updates log.concurrency_margin and log.concurrency_margin_is_safe for the current capacity of the redo log and current innodb_thread_concurrency value. More...
 
Log - free check waiting.
void log_free_check_wait (log_t &log)
 Waits until there is free space in log files which includes concurrency margin required for all threads. More...
 
lsn_t log_free_check_margin (const log_t &log)
 Provides current margin used in the log_free_check calls. More...
 
lsn_t log_free_check_capacity (const log_t &log, lsn_t free_check_margin)
 Computes capacity of redo log available until log_free_check() needs to wait. More...
 
lsn_t log_free_check_capacity (const log_t &log)
 Computes capacity of redo log available until log_free_check() needs to wait. More...
 
bool log_free_check_is_required (const log_t &log)
 Checks if log_free_check() call should better be executed. More...
 
bool log_free_check_is_required ()
 Checks if log_free_check() call should better be executed. More...
 
void log_free_check_validate ()
 Performs debug checks to validate some of the assumptions. More...
 
void log_free_check (log_t &log)
 Reserves free_check_margin in the redo space for the current thread. More...
 
void log_free_check ()
 Checks for free space in the redo log. More...
 
Log - free check updates.
void log_update_limits_low (log_t &log)
 Updates log.free_check_limit_lsn in the log. More...
 
void log_set_dict_persist_margin (log_t &log, sn_t margin)
 Updates log.dict_persist_margin and recompute free check limit. More...
 
Log - other functions related to checkpoints.
dberr_t log_files_write_checkpoint_low (log_t &log, Log_file_handle &checkpoint_file_handle, Log_checkpoint_header_no checkpoint_header_no, lsn_t next_checkpoint_lsn)
 Writes checkpoint to the file containing the written checkpoint_lsn. More...
 
dberr_t log_files_write_first_data_block_low (log_t &log, Log_file_handle &file_handle, lsn_t checkpoint_lsn, lsn_t file_start_lsn)
 Writes the first data block to the log file using the provided handle to the opened log file. More...
 
dberr_t log_files_next_checkpoint (log_t &log, lsn_t lsn)
 Writes the next checkpoint to the log file, by writing a single checkpoint header with the checkpoint lsn. More...
 

Log - checkpointer thread and checkpointer mutex.

#define log_checkpointer_mutex_enter(log)    mutex_enter(&((log).checkpointer_mutex))
 
#define log_checkpointer_mutex_exit(log)   mutex_exit(&((log).checkpointer_mutex))
 
#define log_checkpointer_mutex_own(log)    (mutex_own(&((log).checkpointer_mutex)) || !log_checkpointer_is_active())
 
void log_checkpointer (log_t *log_ptr)
 The log checkpointer thread routine. More...
 
bool log_checkpointer_is_active ()
 Checks if log checkpointer thread is active. More...
 

Detailed Description

Redo log functions related to checkpointing and log free check.

Macro Definition Documentation

◆ log_checkpointer_mutex_enter

#define log_checkpointer_mutex_enter (   log)     mutex_enter(&((log).checkpointer_mutex))

◆ log_checkpointer_mutex_exit

#define log_checkpointer_mutex_exit (   log)    mutex_exit(&((log).checkpointer_mutex))

◆ log_checkpointer_mutex_own

#define log_checkpointer_mutex_own (   log)     (mutex_own(&((log).checkpointer_mutex)) || !log_checkpointer_is_active())

Function Documentation

◆ log_checkpointer()

void log_checkpointer ( log_t log_ptr)

The log checkpointer thread routine.

Parameters
[in,out]log_ptrpointer to redo log

◆ log_checkpointer_is_active()

bool log_checkpointer_is_active ( )
inline

Checks if log checkpointer thread is active.

Returns
true if and only if the log checkpointer thread is active

◆ log_concurrency_margin()

sn_t log_concurrency_margin ( lsn_t  log_capacity,
bool &  is_safe 
)

Computes concurrency margin to be used within log_free_check calls, for a given redo log capacity (soft_logical_capacity).

Parameters
[in]log_capacityredo log capacity (soft)
[out]is_safetrue iff the computed margin wasn't truncated because of too small log_capacity
Returns
the computed margin

◆ log_files_next_checkpoint()

dberr_t log_files_next_checkpoint ( log_t log,
lsn_t  lsn 
)

Writes the next checkpoint to the log file, by writing a single checkpoint header with the checkpoint lsn.

Flushes the file after the write and updates the log.last_checkpoint_lsn.

Remarks
Note that two checkpoint headers are used alternately for consecutive checkpoints. If InnoDB crashed during the write, it would still have the previous checkpoint info and recovery would work.
Parameters
[in,out]logredo log
[in]lsnwrites checkpoint at this lsn
Returns
DB_SUCCESS or error

◆ log_files_write_checkpoint_low()

dberr_t log_files_write_checkpoint_low ( log_t log,
Log_file_handle checkpoint_file_handle,
Log_checkpoint_header_no  checkpoint_header_no,
lsn_t  next_checkpoint_lsn 
)

Writes checkpoint to the file containing the written checkpoint_lsn.

The checkpoint is written to the given checkpoint header. Unless InnoDB is starting: checkpointer, writer and files mutexes must be acquired before calling this function.

Parameters
[in,out]logredo log
[in]checkpoint_file_handlehandle to opened file
[in]checkpoint_header_nocheckpoint header to be written
[in]next_checkpoint_lsnthe checkpoint lsn to write
Returns
DB_SUCCESS or error

◆ log_files_write_first_data_block_low()

dberr_t log_files_write_first_data_block_low ( log_t log,
Log_file_handle file_handle,
lsn_t  checkpoint_lsn,
lsn_t  file_start_lsn 
)

Writes the first data block to the log file using the provided handle to the opened log file.

The block is addressed by the given checkpoint_lsn, filled with 0x00 and its data length points to checkpoint_lsn inside, making the block logically empty.

Remarks
This is used only during creation of new log files.
Parameters
[in,out]logredo log
[in]file_handlehandle to the opened log file
[in]checkpoint_lsnthe checkpoint lsn
[in]file_start_lsnstart_lsn of the file
Returns
DB_SUCCESS or error

◆ log_free_check() [1/2]

void log_free_check ( )
inline

Checks for free space in the redo log.

Must be called when no latches are held (except those listed as exceptions). Any database operation must call this before it has produced LOG_CHECKPOINT_FREE_PER_THREAD * UNIV_PAGE_SIZE bytes of redo log records. That's because that is the margin in redo log we reserve by calling this function.

Remarks
Checks if lsn corresponding to current log.sn exceeds log.free_check_limit_lsn, in which case waits (until it does not exceed). This function is called before starting a mini-transaction, because thread must not hold block latches when calling this function. It is also important that the caller does NOT hold any latch, that might be tried to be acquired:
  • by the page cleaner (e.g. page/block latches),
  • or by the log flush process (e.g. file space latches),
  • or by any other thread, which might at that time already hold another latch, that could further lead to a similar problem in chain of threads. For example, suppose a thread holding some latch X, which is neither used by the page cleaners nor by the log flush process, called log_free_check() and started to wait for the free space. Another thread, holding block's latch (which obviously might be needed for the page cleaners) tries to acquire the latch X. It needs to wait, because X has already been taken. Therefore, the latched block cannot be flushed. If this block had old modifications (low oldest_modification), it could effectively prevent any further attempts to reclaim space in the redo log. The chain of waiting for each other threads could obviously be even longer than the one in example. Therefore it is very important not to call log_free_check() if we are holding any latchs which might exist in any of such chains. As you can see, it is not that easy to see if log_free_check() might be called. It is not only about direct holding of block latches, but also such X (or Y acquired by thread holding such X), could lead to a deadlock.

For sake of simplicity, you should better not keep any latch when calling to the log_free_check() unless you are really sure about what you are doing.

◆ log_free_check() [2/2]

void log_free_check ( log_t log)
inline

Reserves free_check_margin in the redo space for the current thread.

For further details please look at description of

See also
log_free_check_margin().
Parameters
[in]logredo log

We prefer to wait now for the space in log file, because now are not holding any latches of dirty pages.

◆ log_free_check_capacity() [1/2]

lsn_t log_free_check_capacity ( const log_t log)

Computes capacity of redo log available until log_free_check() needs to wait.

It calls log_free_check_margin(log) to obtain the current log_free_check_margin.

Parameters
[in]logredo log
Returns
lsn capacity up to free_check_wait happens

◆ log_free_check_capacity() [2/2]

lsn_t log_free_check_capacity ( const log_t log,
lsn_t  free_check_margin 
)

Computes capacity of redo log available until log_free_check() needs to wait.

It uses a provided size of the log_free_check_margin.

Parameters
[in]logredo log
[in]free_check_marginsize of the log_free_check_margin;
See also
log_free_check_margin(log)
Returns
lsn capacity up to free_check_wait happens

◆ log_free_check_is_required() [1/2]

bool log_free_check_is_required ( )
inline

Checks if log_free_check() call should better be executed.

Returns
true iff log_free_check should be executed

◆ log_free_check_is_required() [2/2]

bool log_free_check_is_required ( const log_t log)
inline

Checks if log_free_check() call should better be executed.

Parameters
[in]logredo log
Returns
true iff log_free_check should be executed

◆ log_free_check_margin()

lsn_t log_free_check_margin ( const log_t log)

Provides current margin used in the log_free_check calls.

It is a sum of dict_persist_margin and concurrency_margin.

Parameters
[in]logredo log
Returns
margin that would be used in log_free_check()

◆ log_free_check_validate()

void log_free_check_validate ( )

Performs debug checks to validate some of the assumptions.

◆ log_free_check_wait()

void log_free_check_wait ( log_t log)

Waits until there is free space in log files which includes concurrency margin required for all threads.

You should rather use log_free_check().

Parameters
[in]logredo log

◆ log_get_checkpoint_age()

lsn_t log_get_checkpoint_age ( const log_t log)
inline

Calculates age of current checkpoint as number of bytes since last checkpoint.

This includes bytes for headers and footers of all log blocks. The calculation is based on the latest written checkpoint lsn, and the current lsn, which points to the first non reserved data byte. Note that the current lsn could not fit the free space in the log files. This means that the checkpoint age could potentially be larger than capacity of the log files. However we do the best effort to avoid such situations, and if they happen, user threads wait until the space is reclaimed.

Parameters
[in]logredo log
Returns
checkpoint age as number of bytes

◆ log_get_checkpoint_lsn()

lsn_t log_get_checkpoint_lsn ( const log_t log)
inline

Gets the last checkpoint lsn stored and flushed to disk.

Returns
last checkpoint lsn

◆ log_make_latest_checkpoint() [1/2]

bool log_make_latest_checkpoint ( )

Make a checkpoint at the current lsn.

Reads current lsn and waits until all dirty pages have been flushed up to that lsn. Afterwards requests a checkpoint write and waits until it is finished.

Returns
true iff current lsn was greater than last checkpoint lsn

◆ log_make_latest_checkpoint() [2/2]

bool log_make_latest_checkpoint ( log_t log)

Make a checkpoint at the current lsn.

Reads current lsn and waits until all dirty pages have been flushed up to that lsn. Afterwards requests a checkpoint write and waits until it is finished.

Parameters
[in,out]logredo log
Returns
true iff current lsn was greater than last checkpoint lsn

◆ log_next_checkpoint_header()

Log_checkpoint_header_no log_next_checkpoint_header ( Log_checkpoint_header_no  checkpoint_header_no)

Provides opposite checkpoint header number to the given checkpoint header number.

Parameters
[in]checkpoint_header_nothe given checkpoint header number
Returns
the opposite checkpoint header number

◆ log_request_checkpoint()

void log_request_checkpoint ( log_t log,
bool  sync 
)

Requests a fuzzy checkpoint write (for currently available lsn).

Parameters
[in,out]logredo log
[in]syncwhether request is sync (function should wait)

◆ log_request_checkpoint_in_next_file()

void log_request_checkpoint_in_next_file ( log_t log)

Requests a checkpoint written in the next log file (not in the one, to which current log.last_checkpoint_lsn belongs to).

Parameters
[in,out]logredo log

◆ log_request_latest_checkpoint()

bool log_request_latest_checkpoint ( log_t log,
lsn_t requested_lsn 
)

Requests a checkpoint at the current lsn.

Parameters
[in,out]logredo log
[out]requested_lsnlsn for which checkpoint was requested, or stays unmodified if it wasn't requested
Returns
true iff requested (false if checkpoint_lsn was already at that lsn)

◆ log_set_dict_max_allowed_checkpoint_lsn()

void log_set_dict_max_allowed_checkpoint_lsn ( log_t log,
lsn_t  max_lsn 
)

Updates the field log.dict_max_allowed_checkpoint_lsn.

This is limitation for lsn at which checkpoint might be written, imposed by cached changes to the DD table buffer. It is called from DD code.

Parameters
[in,out]logredo log
[in]max_lsnnew value for the limitation

◆ log_set_dict_persist_margin()

void log_set_dict_persist_margin ( log_t log,
sn_t  margin 
)

Updates log.dict_persist_margin and recompute free check limit.

Parameters
[in,out]logredo log
[in]marginnew value for log.dict_persist_margin

◆ log_sync_flush_lsn()

lsn_t log_sync_flush_lsn ( log_t log)

Computes lsn up to which sync flush should be done or returns 0 if there is no need to execute sync flush now.

Parameters
[in,out]logredo log
Returns
lsn for which we want to have oldest_lsn >= lsn in each BP, or 0 if there is no need for sync flush

◆ log_update_concurrency_margin()

void log_update_concurrency_margin ( log_t log)

Updates log.concurrency_margin and log.concurrency_margin_is_safe for the current capacity of the redo log and current innodb_thread_concurrency value.

Parameters
[in,out]logredo log

◆ log_update_limits_low()

void log_update_limits_low ( log_t log)

Updates log.free_check_limit_lsn in the log.

The log_limits_mutex must be acquired before a call (unless srv_is_being_started is true).

Parameters
[in,out]logredo log