欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

InnoDB On-Disk Structures(五)-- Redo Log & Undo Logs (转载)

程序员文章站 2022-04-20 18:07:16
1.Redo Log The redo log is a disk-based data structure used during crash recovery to correct data written by incomplete transactions. During normal op ......

1.redo log

the redo log is a disk-based data structure used during crash recovery to correct data written by incomplete transactions. during normal operations, the redo log encodes requests to change table data that result from sql statements or low-level api calls. modifications that did not finish updating the data files before an unexpected shutdown are replayed automatically during initialization, and before the connections are accepted.

by default, the redo log is physically represented on disk by two files named ib_logfile0 and ib_logfile1. mysql writes to the redo log files in a circular fashion. data in the redo log is encoded in terms of records affected; this data is collectively referred to as redo. the passage of data through the redo log is represented by an ever-increasing lsn value.

1.1 changing the number or size of redo log files

to change the number or the size of redo log files, perform the following steps:

  1. stop the mysql server and make sure that it shuts down without errors.

  2. edit my.cnf to change the log file configuration. to change the log file size, configure innodb_log_file_size. to increase the number of log files, configureinnodb_log_files_in_group.

  3. start the mysql server again.

if innodb detects that the innodb_log_file_size differs from the redo log file size, it writes a log checkpoint, closes and removes the old log files, creates new log files at the requested size, and opens the new log files.

1.2 group commit for redo log flushing

innodb, like any other acid-compliant database engine, flushes the redo log of a transaction before it is committed. innodb uses group commit functionality to group multiple such flush requests together to avoid one flush for each commit. with group commit, innodb issues a single write to the log file to perform the commit action for multiple user transactions that commit at about the same time, significantly improving throughput.

1.3 redo log archiving

backup utilities that copy redo log records may sometimes fail to keep pace with redo log generation while a backup operation is in progress, resulting in lost redo log records due to those records being overwritten. this issue most often occurs when there is significant mysql server activity during the backup operation, and the redo log file storage media operates at a faster speed than the backup storage media. the redo log archiving feature, introduced in mysql 8.0.17, addresses this issue by sequentially writing redo log records to an archive file in addition to the redo log files. backup utilities can copy redo log records from the archive file as necessary, thereby avoiding the potential loss of data.

if redo log archiving is configured on the server, mysql enterprise backup, available with the mysql enterprise edition, uses the redo log archiving feature when backing up a mysql server.

enabling redo log archiving on the server requires setting a value for the innodb_redo_log_archive_dirs system variable. the value is specified as a semicolon-separated list of labeled redo log archive directories. the label:directory pair is separated by a colon (:). for example:

mysql> set global innodb_redo_log_archive_dirs='label1:directory_path1[;label2:directory_path2;…]';

the label is an arbitrary identifier for the archive directory. it can be any string of characters, with the exception of colons (:), which are not permitted. an empty label is also permitted, but the colon (:) is still required in this case. a directory_path must be specified. the directory that is selected for the redo log archive file must exist when redo log archiving is activated, or an error is returned. the path can contain colons (':'), but semicolons (;) are not permitted.

the innodb_redo_log_archive_dirs variable must be configured before the redo log archiving can be activated. the default value is null, which does not permit activating redo log archiving.

注意事项:

the archive directories that you specify must satisfy the following requirements. (the requirements are enforced when redo log archiving is activated.):

  • directories must exist. directories are not created by the redo log archive process. otherwise, the following error is returned:

    error 3844 (hy000): redo log archive directory 'directory_path1' does not exist or is not a directory

  • directories must not be world-accessible. this is to prevent the redo log data from being exposed to unauthorized users on the system. otherwise, the following error is returned:

    error 3846 (hy000): redo log archive directory 'directory_path1' is accessible to all os users

  • directories cannot be those defined by datadirinnodb_data_home_dirinnodb_directoriesinnodb_log_group_home_dir,innodb_temp_tablespaces_dirinnodb_tmpdir innodb_undo_directory, or secure_file_priv, nor can they be parent directories or subdirectories of those directories. otherwise, an error similar to the following is returned:

    error 3845 (hy000): redo log archive directory 'directory_path1' is in, under, or over server directory 'datadir' - '/path/to/data_directory'

when a backup utility that supports redo log archiving initiates a backup, the backup utility activates redo log archiving by invoking the innodb_redo_log_archive_start() user-defined function.

if you are not using a backup utility that supports redo log archiving, redo log archiving can also be activated manually, as shown:

mysql> select innodb_redo_log_archive_start('label', 'subdir');
+------------------------------------------+
| innodb_redo_log_archive_start('label') |
+------------------------------------------+
| 0                                        |
+------------------------------------------+

or:

mysql> do innodb_redo_log_archive_start('label', 'subdir');
query ok, 0 rows affected (0.09 sec)

注意事项:

the mysql session that activates redo log archiving (using innodb_redo_log_archive_start()) must remain open for the duration of the archiving. the same session must deactivate redo log archiving (using innodb_redo_log_archive_stop()). if the session is terminated before the redo log archiving is explicitly deactivated, the server deactivates redo log archiving implicitly and removes the redo log archive file.

where label is a label defined by innodb_redo_log_archive_dirssubdir is an optional argument for specifying a subdirectory of the directory identified by label for saving the archive file; it must be a simple directory name (no slash (/), backslash (\), or colon (:) is permitted). subdir can be empty, null, or it can be left out.

only users with the innodb_redo_log_archive privilege can activate redo log archiving by invoking innodb_redo_log_archive_start(), or deactivate it usinginnodb_redo_log_archive_stop(). the mysql user running the backup utility or the mysql user activating and deactivating redo log archiving manually must have this privilege.

the redo log archive file path is directory_identified_by_label/[subdir/]archive.serveruuid.000001.log, where directory_identified_by_label is the archive directory identified by the label argument for innodb_redo_log_archive_start()subdir is the optional argument used for innodb_redo_log_archive_start().

for example, the full path and name for a redo log archive file appears similar to the following:

/directory_path/subdirectory/archive.e71a47dc-61f8-11e9-a3cb-080027154b4d.000001.log

after the backup utility finishes copying innodb data files, it deactivates redo log archiving by calling the innodb_redo_log_archive_stop() user-defined function.

if you are not using a backup utility that supports redo log archiving, redo log archiving can also be deactivated manually, as shown:

mysql> select innodb_redo_log_archive_stop();
+--------------------------------+
| innodb_redo_log_archive_stop() |
+--------------------------------+
| 0                              |
+--------------------------------+

or:

mysql> do innodb_redo_log_archive_stop();
query ok, 0 rows affected (0.01 sec)

after the stop function completes successfully, the backup utility looks for the relevant section of redo log data from the archive file and copies it into the backup.

after the backup utility finishes copying the redo log data and no longer needs the redo log archive file, it deletes the archive file.

removal of the archive file is the responsibility of the backup utility in normal situations. however, if the redo log archiving operation quits unexpectedly beforeinnodb_redo_log_archive_stop() is called, the mysql server removes the file.

1.4 performance considerations

activating redo log archiving typically has a minor performance cost due to the additional write activity.

on unix and unix-like operating systems, the performance impact is typically minor, assuming there is not a sustained high rate of updates. on windows, the performance impact is typically a bit higher, assuming the same.

if there is a sustained high rate of updates and the redo log archive file is on the same storage media as the redo log files, the performance impact may be more significant due to compounded write activity.

if there is a sustained high rate of updates and the redo log archive file is on slower storage media than the redo log files, performance is impacted arbitrarily.

writing to the redo log archive file does not impede normal transactional logging except in the case that the redo log archive file storage media operates at a much slower rate than the redo log file storage media, and there is a large backlog of persisted redo log blocks waiting to be written to the redo log archive file. in this case, the transactional logging rate is reduced to a level that can be managed by the slower storage media where the redo log archive file resides.

2. undo logs

an undo log is a collection of undo log records associated with a single read-write transaction. an undo log record contains information about how to undo the latest change by a transaction to a clustered index record. if another transaction needs to see the original data as part of a consistent read operation, the unmodified data is retrieved from undo log records. undo logs exist within undo log segments, which are contained within rollback segments. rollback segments reside in undo tablespacesand in the global temporary tablespace.

undo logs that reside in the global temporary tablespace are used for transactions that modify data in user-defined temporary tables. these undo logs are not redo-logged, as they are not required for crash recovery. they are used only for rollback while the server is running. this type of undo log benefits performance by avoiding redo logging i/o.

each undo tablespace and the global temporary tablespace individually support a maximum of 128 rollback segments. the innodb_rollback_segments variable defines the number of rollback segments.

the number of transactions that a rollback segment supports depends on the number of undo slots in the rollback segment and the number of undo logs required by each transaction.

the number of undo slots in a rollback segment differs according to innodb page size.

innodb page size number of undo slots in a rollback segment (innodb page size / 16)
4096 (4kb) 256
8192 (8kb) 512
16384 (16kb) 1024
32768 (32kb) 2048
65536 (64kb) 4096

a transaction is assigned up to four undo logs, one for each of the following operation types:

  1. insert operations on user-defined tables

  2. update and delete operations on user-defined tables

  3. insert operations on user-defined temporary tables

  4. update and delete operations on user-defined temporary tables

undo logs are assigned as needed. for example, a transaction that performs insertupdate, and delete operations on regular and temporary tables requires a full assignment of four undo logs. a transaction that performs only insert operations on regular tables requires a single undo log.

a transaction that performs operations on regular tables is assigned undo logs from an assigned undo tablespace rollback segment. a transaction that performs operations on temporary tables is assigned undo logs from an assigned global temporary tablespace rollback segment.

an undo log assigned to a transaction remains tied to the transaction for its duration. for example, an undo log assigned to a transaction for an insert operation on a regular table is used for all insert operations on regular tables performed by that transaction.

given the factors described above, the following formulas can be used to estimate the number of concurrent read-write transactions that innodb is capable of supporting.

  • if each transaction performs either an insert or an update or delete operation, the number of concurrent read-write transactions that innodb is capable of supporting is:

    (innodb_page_size / 16) * innodb_rollback_segments * number of undo tablespaces
  • if each transaction performs an insert and an update or delete operation, the number of concurrent read-write transactions that innodb is capable of supporting is:

    (innodb_page_size / 16 / 2) * innodb_rollback_segments * number of undo tablespaces
  • if each transaction performs an insert operation on a temporary table, the number of concurrent read-write transactions that innodb is capable of supporting is:

    (innodb_page_size / 16) * innodb_rollback_segments
  • if each transaction performs an insert and an update or delete operation on a temporary table, the number of concurrent read-write transactions that innodb is capable of supporting is:

(innodb_page_size / 16 / 2) * innodb_rollback_segments

 

转载、节选于