First recognized undo type Log System

Source: Internet
Author: User

First recognized undo type Log System

The log system is the basic mechanism to ensure that the database management system correctly executes transactions. Based on different functions, the log system can be divided into undo and redo. This article describes the principle of undo logs.

1 UNDO log Requirements
  • Logs record the old values before data modification;
  • Flushing logs before flushing data (consistency)
  • After flushing data, fl the log COMMIT. (Persistent)
2 UNDO log defects

The UNDO log provides sufficient information to ensure transaction consistency and durability. However, to maintain consistency, we adopt a passive and conservative strategy, that is, overwrite transactions that cannot guarantee success with old values. Unsuccessful transactions cannot be re-executed and can only be restored to the consistent state before the transaction.

2. Simulated code

It only simulates the Data Writing Process, does not simulate the data recovery process, and will be supplemented later.

/* Basic Process Simulation of UNDO logs */# include <stdio. h> # include <string. h> # include <stdlib. h> # include <err. h>/* log file, basic format: * T1_START * A = 100 * B = 100 * T1_COMMIT */# define LOG_FILE "test. log "/* data file, basic format: one key-value pair per line. The length is fixed to 1024, and the right side is filled with spaces. * A = 100 (padding) * B = 100 (padding) */# define DATA_FILE "test. data "# define LINE_MAX 1024/* key-value Pair */typedef struct KV; struct KV {char * key; char * value ;}; /* read data named k from the hard disk */static int fread_kv (char * k, char * v, FILE * fp) {rewind (fp ); char line [LINE_MAX + 1] = {0}; int lineNo = 0; while (fread (line, 1, LINE_MAX, fp) = LINE_MAX) {int I = 0; while (k [I] & line [I]! = ') {If (k [I]! = Line [I]) {break;} I ++;} if (line [I] = ') {strcpy (v, & line [I + 1]); return lineNo; 1, 1 Top} lineNo ++;} return-1 ;} /* fl data to the hard disk */static void fwrite_kv (char * k, char * v, FILE * fp) {int lineNo =-1; char newLine [LINE_MAX]; sprintf (newLine, "% s = % s", k, v); int offset = 0; if (lineNo = fread_kv (k, v, fp) <0) /* insert */{offset = fseek (fp, 0, SEEK_END);} else {/* update */offset = fseek (fp, LINE_M AX * lineNo, 0);} fprintf (fp, "%-1023s \ n", newLine); fflush (fp);} int main (int argc, char ** argv) {FILE * fpLog = fopen (LOG_FILE, "r +"); FILE * fpData = fopen (DATA_FILE, "r +"); if (! (FpLog & fpData) {perror (NULL);}/* START a transaction */char log [1024] = "T1 START \ n "; /* perform transaction operations in the memory */char v [LINE_MAX]; fread_kv ("A", v, fpData); int A = atoi (v ); fread_kv ("B", v, fpData); int B = atoi (v); char logA [1024]; sprintf (logA, "T1: A = % d \ n ", a);/* record the old value in the log */strcat (log, logA); A-= 50; char logB [1024]; sprintf (logB, "T1: B = % d \ n ", B);/* record the old value in the log */strcat (log, logB); B + = 50; /************** if this occurs Faults, logs and data are not written to the hard disk, and transactions are lost, but Database Consistency is maintained. * *********** // before flushing data to the disk, first fl the log to the hard disk */fputs (log, fpLog); fflush (fpLog ); /************* if a fault occurs at this time, the old log value has been written to the hard disk and the data has not been written, the old value must be restored during restoration. (The data is not flushed to the disk, but it is unknown) ******** // click the new data value to the hard disk */sprintf (v, "% d ", a); fwrite_kv ("A", v, fpData);/************** if A fault occurs at this time, the old log value has been written to the hard disk, and the data has not been written. You need to restore the old log value when restoring the log. * ******/abort ();/* simulate fault */sprintf (v, "% d", B); fwrite_kv ("B", v, fpData);/*************** as shown in Figure If a fault occurs at this time, the old log value has been written to the hard disk, and the data has been written to the hard disk, but the COMMIT log has not been written, and the old value needs to be restored. (although the data has been fully flushed, it is unknown) ******** // after the data is flushed, fl the submitted logs to the hard disk */fputs ("T1 COMMIT \ n", fpLog); fflush (fpLog ); /************* if a fault occurs at this time, the log COMMIT has been flushed to the disk to ensure that the data has been flushed. No need to recover *******/fclose (fpLog); fclose (fpData); return 0 ;}

Before code execution:
The data file content is as follows:

A=100B=100

After the above code is executed:
The data file content is as follows:

A=50B=100

The log file content is as follows:

T1 STARTT1:A=100T1:B=100

Based on the log file, because T1 COMMIT is not found, it is determined that transaction T1 fails, data may be inconsistent, and data recovery is required. Then, based on the log file, we can obtain the old data values A = 100 and B = 100 before the execution of transaction T1, so it is easy to restore the data as long as, all values of B are updated to their old values.

Data Files after recovery:

A=100B=100

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.