• “This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Previous: In order to preview the database failover mini-project, there may be a practical focus and some non-essential theoretical content will be ignored!

I. Transaction introduction

  • A transaction is an all-or-nothing sequence of operations and is an indivisible unit of work.
  • Four characteristics of transactions
  1. Atomicity: All or none of the sequences of operations.
  2. Consistency: The state of the database must be consistent after a transaction is executed.
  3. Isolation: Transactions are executed without interference.
  4. Durability: Their influence on the database is persistent after a transaction is committed i.e. they change the data in the database.

Two, the type of failure

Internal transaction failure

  • Most of the failures that occur within a transaction are unexpected and cannot be handled by the application, which is what database failover is all about. Can be executed for this type of faultTransaction UndoPerform recovery.

System failure

  • The event causes the system to stop running and needs to be restarted. Can be executed for this type of faultTransaction redoPerform recovery.

Three, restore the implementation technology

Data dump

  • The process of periodically copying the entire database to tape, disk, or other storage media for storage. The backup data is calledA copy of the backup

  • Static dump: Indicates the dump operation when no transaction is running. Dumps are simple, but reduce database availability.
  • Dynamic dump: refers to operations performed on the database while the transaction is running. It overcomes the disadvantages of static dump, but does not guarantee the validity of duplicate data. But this can already be establishedLog filesSolve this problem.

The log file

  • A log file is used to record updates made by transactions to the database. There are two main types:
  • 1. Log file contents in unit of record include:
  1. BEGIN TRANSACTION mark for each TRANSACTION
  2. The end of each transaction (COMMIT or ROLLBACK) flag
  3. All update operations for individual transactions
  • 2. Log file contents in the unit of data block include:
  1. Transaction identifier (identifies which transaction)
  2. Operation type (Insert, Delete, modify)
  3. Action object (record internal identity)
  4. Pre-update data value (old value, null during insert operation)
  5. Updated data (new value, null for delete operation)
  • Register log Compliance1. Order of registration strictly in accordance with the time order of concurrent transaction execution,2. Keep a journal first.Post-write databaseThe principle.
  • Log files can be used for transaction and system failure recovery. See below

4. Recovery strategy

Transaction fault recovery steps

  1. Reverse scan log files for update operations for transactions.
  2. Performing the reverse operation willValue before updateWrite to the database.
  3. Continue the reverse scan to find and process update operations for other transactions.
  4. Until the start flag of the transaction is read, at which point failover is complete.

Procedure for system fault recovery

  1. Forward scan log files for transactions committed before the fault occurs and add them to the redo queue (reDO_list). At the same time, find the transactions that were not completed at the time of the failure and add them to the undo queue (undo_list).
  2. To undo the queueCancellation (undo)To deal with. Same as aboveTransaction fault recovery.
  3. Redo the redo queue. Forward scan the log file, re-register the log file for each redo transaction and write the updated value to the database.

Including checkpoint recovery technology

  • Check points include:
  1. Creates a list of all transactions being executed at checkpoint time.
  2. The last logged address of these transactions.

  • The checkpoint approach can improve recovery efficiency during failure recovery, usually by periodically establishing checkpoints to maintain log files and saving database state operations.
  • Steps for setting up a checkpoint:
  1. Writes all logs from the current log buffer to the log file on disk.
  2. Writes a checkpoint record to the log file.
  3. Writes all data records of the current data buffer to the disk-local database.
  4. Write the address of the checkpoint recorded in the log file to a restart file.