2. Recovery Management
2.1 log semantic implementation
As mentioned above, at the underlying implementation of log, log is only responsible for the implementation of log in byte granularity and does not know the log semantic information. The log semantic information is implemented by the recovery manager.
Figure 1 log record class diagram under the recovery Manager
Logrecord is an abstract interface that defines the basic interfaces for all log records. The system provides six different log types: {checkpoint, start, commit, rollback, setint, setstring }. Specifically, setint and setstring are the data related to the query operation that is actually written to the log file, while checkpoint, start, commit, and rollback are the data recovery for the database, auxiliary information added to the log file.
Here we will focus on the Undo in logrecord.
Take setint as an example.Code:
Figure 2 undo method code under setint
Instead, replace the new value at the specified position with the old value saved in the log record.
Note the following two points:
1)-1When writing data, the false lsn system generates a log record at the same time, but when the lsn is-1, this log record can only be a temporary record and will not be saved to the disk, see the following code.
Figure 3 log records with lsn-1
2)ValThis Val is initialized when the setintrecord object is created. The setintrecord constructor has two types:
Public setintrecord (INT txnum, block BLK, int offset, int Val); // create a new setintrecord
Public setintrecord (basiclogrecord REC) // wrap a log record with semantics through a non-semantic byte record
Obviously, in the object obtained by the second constructor, Val must be the old value.
Figure 4 setint method of recovermgr
Similarly, Val is the old value.
This confirms the role of Undo in setintrecord, restores the corresponding data to the old value, and does not generate new log records.
2.2 recoveryAlgorithm
Standard Aries Restoration Algorithm:
1) Write logs first;
2) Redo, reproduce the history, and then perform the reverse operation to cancel the unfinished transaction;
3) log records are reversed to avoid reverse operations;
Simpledb implements a variant Restoration Algorithm:Redo onlyRestoration Algorithm
A prerequisite for an algorithm is:When a transaction is committed, the data in the cache is forcibly written to the disk.In this way, the redo process does not need to be completed by using logs during recovery. The redo step of the Aries algorithm is skipped, and the new value of the updated log record does not need to be saved.
Compared with the standard Aries algorithm, the following benefits are: A. Smaller logs; B. faster recovery.
The disadvantage is that the transaction commit is slow.
SoRedo onlyThe restoration algorithm is suitable for situations where the system is unstable and often needs to be restored. When recovery rarely occurs, the advantages are not obvious, and the submission efficiency is too low, it is not suitable.
Next, let's look at the implementation of this algorithm through code.
>Commit ():
Figure 5 transaction commit
First flushall, write all the transaction-related cache slices to the disk, and then write the transaction commit mark log records.
>Rollback ():
Figure 6 rollback method
First, write the dirty pages associated with the current transaction to the disk, then roll back the transaction, restore all operations of the current transaction, add a rollback record in the log file, and directly fl the disk.
You can see the Implementation Details of rollback through dorollback:
In reverse tracing of log files, only the log records related to the current transaction are viewed until start is stopped. Otherwise, this record is reversed.
As mentioned above, only the Undo of setintrecord and setstringrecord have actual actions in the six types of log records. Due to the polymorphism of OO, the REC here. when the record of Undo (txnum) is setint and setstring,The two undo statements are implicitly called. Taking setintrecord as an example, in this case, the constructor "Public setintrecord (basiclogrecord REC)" is used to read the old values from the log.And then reset to the original position.
------------ Specifically --------------
Completing Data Writing through a transaction and performing necessary event rollback and data recovery are two opposite and some symmetric operations. Here we will take a look at the write data operations of the transaction in advance:
Figure 7 setint method of transaction
Use recoverymgr to create logs and then write data. See figure 4. The setint method of recoverymgr. In fact, no operations are performed with newval, read the original oldval from the location where newval is to be written and save it to the log.
This is the opposite of Undo in setintrecord, read the oldval in the log, reset to the specified location, overwrite newval, roll back the transaction, and restore the old state.
-----------------------------------------
>Recover ():
All recovery operations, as mentioned aboveRedo onlyThe restoration algorithms are all implemented in this method.
Write all the dirty data to the disk and start recovery:
1) maintain the finishedtx list of completed transactions
2) scan log files in reverse order:
A) if a commit flag is encountered, the transaction corresponding to the log record has been completed. You do not need to reverse the transaction and add txnum to finishedtx.
B) if a rollback flag is encountered, the transaction corresponding to the log record has been reversed, which means that the transaction has not done anything and does not need to be reversed. Add txnum to finishedtx
C) if the checkpoint mark is encountered, it indicates that all the transactions have been reversed or persisted since the previous system recovery, and the recovery has ended.
D) if the txnum of the log is no longer in the finishedtx list, it indicates that the transaction corresponding to the log is interrupted and needs to be reversed.
After the restoration is complete, write a checkpoint record at the end of the log to mark the restoration operation. This record is immediately written to the disk.
Figure 8 recover Method