A method for implementing CockroachDB database snapshot

By adding snapshot tables and redesigning the garbage collection strategy in CockroachDB, the problems of data loss and multi-copy storage failure caused by user misoperation are solved, efficient data backup and recovery capabilities are achieved, and the reliability and stability of the database are ensured. .

CN111752929BActive Publication Date: 2024-09-27SHANDONG LANGCHAO YUNTOU INFORMATION TECH CO LTD
View PDF 6 Cites 0 Cited by

Patent Information

Application Number
CN202010616119.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-07-01
Publication Date
2024-09-27
Estimated Expiration
2040-07-01

AI Technical Summary

Technical Problem

CockroachDB cannot recover when data is cleaned due to user misoperation, and multi-copy storage suffers from data loss when facing a single-machine system failure.

Method used

By adding a system snapshot table in CockroachDB and redesigning the garbage collection strategy of MVCC data, DDL statements such as CREATE SNAPSHOT, SHOW SNAPSHOT, DROP SNAPSHOT, REVERT SNAPSHOT and SELECT ... AS OF SNAPSHOT are provided to realize the database snapshot function and in garbage collection Add snapshot time point comparison during the recycling process to protect critical data.

Benefits of technology

It achieves efficient data backup and recovery capabilities, prevents data loss caused by user misoperation, and responds to single-machine system failures under a multi-copy storage strategy, providing the function of quickly creating and restoring snapshot points.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN111752929B
    Figure CN111752929B
Patent Text Reader

Abstract

The present invention particularly relates to a method for implementing a CockroachDB database snapshot. The method for implementing a CockroachDB database snapshot utilizes the CockroachDB multi-version management and historical data garbage collection mechanisms, adds system snapshot table management and defines snapshot DDL statements, and redesigns the MVCC data garbage collection strategy, thereby implementing the database snapshot function. The method for implementing a CockroachDB database snapshot can not only create snapshots for data tables at certain time points to protect the data at the snapshot time point from being modified, but can also quickly restore the snapshot point data as needed, thereby achieving efficient and reliable data backup and recovery capabilities.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of distributed databases, and in particular to a method for implementing a CockroachDB database snapshot. Background Art

[0002] CockroachDB is an open source distributed database system built on the Google Spanner concept. It has NoSQL's ability to store and manage massive data, while maintaining features such as ACID and SQL supported by traditional databases. It also supports cross-region, decentralization, high concurrency, strong consistency among multiple copies, and high availability.

[0003] CockroachDB's underlying storage is a KV (Key-Value, distributed storage system) database, and uses "KEY+Hybrid Logical Clock (HLC)" to implement multi-version concurrency control (MVCC-Multiversion concurrency control) of data. Data updates are added to the underlying data storage in an append (append) manner. In actual operation, in order to quickly read the latest data, the HLC will be inverted and encoded to the end of the KEY (keyword). When reading, an iterator is started with the KEY as the prefix, so that the latest data is read first. At the same time, the garbage collection (GC-Garbage collection) mechanism will be started for regular cleaning of expired data.

[0004] Based on CockroachDB's distributed, multi-copy storage architecture, its system stability and data reliability are greatly improved compared to stand-alone databases. In theory, data reliability can be guaranteed by increasing the number of copies without the need for a separate backup solution. However, it still cannot cope with scenarios where users make mistakes. For example, if a user accidentally clears a data table, all copies of the data will be cleared and cannot be restored.

[0005] CockroachDB uses MVCC to store KV data and uses HLC (hybrid logical clock) timestamps as data version numbers. There will be multiple versions of Key data in the storage engine, such as Figure 1 As shown. Multiple versions of the same key are sorted in reverse order by timestamp. Data with timestamp = 0 indicates that the transaction for writing data has not been committed. The data is in WriteIntent and the value is Metadata. Multiple versions of historical data will be cleaned up regularly through GC.

[0006] Based on this, the present invention proposes a method for implementing a CockroachDB database snapshot. Summary of the invention

[0007] In order to overcome the defects of the prior art, the present invention provides a simple and efficient method for implementing a CockroachDB database snapshot.

[0008] The present invention is achieved through the following technical solutions:

[0009] A method for implementing a CockroachDB database snapshot, characterized by utilizing the CockroachDB multi-version management and historical data garbage collection mechanisms, adding system snapshot table management and DDL (Data Definition Language) statements for defining snapshots, and redesigning the garbage collection strategy for MVCC (Multi-Version Concurrency Control) data, thereby implementing the snapshot function of the database.

[0010] The system snapshot table is used to record the ID and snapshot time point of the snapshot object, and the snapshot object is a database or a data table. By adding a snapshot object ID and snapshot time point record to the system snapshot table, a snapshot can be created for the database or data table.

[0011] The snapshot DDL statements include creating a snapshot (CREATE SNAPSHOT), querying a snapshot and defining SHOWSNAPSHOT, deleting a snapshot (DROP SNAPSHOT), restoring the data at a certain snapshot point to the current data (REVERT SNAPSHOT), and querying the data at a certain snapshot point (SELECT...FROM...AS OF SNAPSHOT).<snapshot_name> .

[0012] To query snapshot point data (SELECT), the system snapshot table is queried according to the snapshot name to obtain the snapshot time point, and then the MVCC data of "time point t <= snapshot time point" in the underlying storage engine is queried.

[0013] To restore snapshot data, you need to query the data that has changed from the snapshot time point to the current time point. Start a new transaction and perform the following steps:

[0014] S11. The newly added data after the snapshot time point is overwritten with the DELETE mark;

[0015] S12. Data modified or deleted after the snapshot time point is overwritten with the value before the snapshot;

[0016] During the recovery of snapshot data, no other transactions can be performed on the database or table.

[0017] The redesigned garbage collection strategy for MVCC data adds snapshot time comparison when comparing the timestamps of MVCC data in the garbage collection process. The process is as follows:

[0018] S21. Get the database or data table ID to which the current shard to be garbage collected belongs;

[0019] S22. Query the system snapshot table to check whether the database or table has a snapshot defined and obtain the snapshot point time;

[0020] S23. Traverse all MVCC data of the current shard one by one;

[0021] S24. Compare the timestamp of the MVCC data key with the snapshot point time and the current time:

[0022] If the Key timestamp = 0, it means that the transaction is not committed and garbage collection will not be performed;

[0023] If the Key timestamp has not expired, garbage collection will not be performed;

[0024] If the Key is the latest data, garbage collection will not be performed;

[0025] If the Key timestamp is the closest to the snapshot point, garbage collection will not be performed;

[0026] Other expired keys can be garbage collected.

[0027] For the earliest snapshot point, if the Key timestamp is the time closest to the snapshot point, garbage collection can be performed when the Key is marked DELETE.

[0028] The device for implementing a CockroachDB database snapshot of the present invention is characterized by comprising: at least one memory and at least one processor;

[0029] The at least one memory is used to store a machine-readable program;

[0030] The at least one processor is configured to call the machine-readable program to execute the above method.

[0031] The computer-readable medium is characterized in that computer instructions are stored on the computer-readable medium, and when the computer instructions are executed by a processor, the processor is caused to execute the above-mentioned method.

[0032] The beneficial effect of the present invention is that the implementation method of the CockroachDB database snapshot can not only create a snapshot for a data table at a certain time point to protect the data at the snapshot time point from being modified, but also quickly restore the snapshot point data as needed, thereby achieving efficient and reliable data backup and recovery capabilities. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0034] Attached Figure 1 This is a schematic diagram of the multi-version data structure of the present invention.

[0035] Attached Figure 2 This is a schematic diagram of the data structure of the snapshot table of the present invention.

[0036] Attached Figure 3 A schematic diagram of timestamp definition for garbage collection example of the present invention.

[0037] Attached Figure 4 Schematic diagram of an example data processing diagram for garbage collection of the present invention. DETAILED DESCRIPTION

[0038] In order to enable those skilled in the art to better understand the technical solutions in the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.

[0039] The implementation method of the CockroachDB database snapshot utilizes the CockroachDB multi-version management and historical data garbage collection mechanisms, adds system snapshot table management and DDL (Data Definition Language) statements for defining snapshots, and redesigns the garbage collection strategy of MVCC (Multi-Version Concurrency Control) data, thereby realizing the snapshot function of the database.

[0040] The system snapshot table is used to record the ID and snapshot time point of the snapshot object, and the snapshot object is a database or a data table. By adding a snapshot object ID and snapshot time point record to the system snapshot table, a snapshot can be created for the database or data table.

[0041] Based on the system snapshot table model and DDL design, creating a snapshot for a database or table is very simple and quick, and is much more efficient than backing up the entire table data.

[0042] The snapshot DDL statements include creating a snapshot (CREATE SNAPSHOT), querying a snapshot and defining SHOWSNAPSHOT, deleting a snapshot (DROP SNAPSHOT), restoring the data at a certain snapshot point to the current data (REVERTSNAPSHOT), and querying the data at a certain snapshot point (SELECT...FROM...AS OF SNAPSHOT).<snapshot_name> .

[0043] To query snapshot point data (SELECT), the system snapshot table is queried according to the snapshot name to obtain the snapshot time point, and then the MVCC data of "time point t <= snapshot time point" in the underlying storage engine is queried.

[0044] To restore snapshot data, you need to query the data that has changed from the snapshot time point to the current time point. Start a new transaction and perform the following steps:

[0045] S11. The newly added data after the snapshot time point is overwritten with the DELETE mark;

[0046] S12. Data modified or deleted after the snapshot time point is overwritten with the value before the snapshot;

[0047] During the recovery of snapshot data, no other transactions can be performed on the database or table.

[0048] CockroachDB's original historical data GC (Garbage collection) compares the expiration time of the data table with the timestamp of the historical data key, and selects the key whose timestamp is less than the expiration time for cleaning.

[0049] The redesigned garbage collection strategy for MVCC data adds snapshot time comparison when comparing the timestamps of MVCC data in the garbage collection process. The process is as follows:

[0050] S21. Get the database or data table ID to which the current shard to be garbage collected belongs;

[0051] S22. Query the system snapshot table to check whether the database or table has a snapshot defined and obtain the snapshot point time;

[0052] S23. Traverse all MVCC data of the current shard one by one;

[0053] S24. Compare the timestamp of the MVCC data key with the snapshot time and current time:

[0054] If the Key timestamp = 0, it means that the transaction is not committed and garbage collection will not be performed;

[0055] If the Key timestamp has not expired, garbage collection will not be performed;

[0056] If the Key is the latest data, garbage collection will not be performed;

[0057] If the Key timestamp is the closest to the snapshot point, garbage collection will not be performed;

[0058] Other expired keys can be garbage collected.

[0059] For the earliest snapshot point, if the Key timestamp is the time closest to the snapshot point, garbage collection can be performed when the Key is marked DELETE.

[0060] Attached Figure 3 The following is the timestamp variable definition. Two snapshot points are defined: "2 days ago - 1 nanosecond" and "25 hours ago - 1 nanosecond". The GC expiration time is defined as 25 hours. That is, the historical data before 25 hours is considered expired and can be GCed.

[0061] Attached Figure 4 Shown is an example of GC treatment results.

[0062] Under the redesigned MVCC data garbage collection strategy, snapshot point data will be permanently retained and can be used for snapshot data query and full table recovery.

[0063] The device for implementing the CockroachDB database snapshot includes: at least one memory and at least one processor;

[0064] The at least one memory is used to store a machine-readable program;

[0065] The at least one processor is configured to call the machine-readable program to execute the above method.

[0066] The computer readable medium stores computer instructions, which, when executed by a processor, cause the processor to perform the above-described method.

[0067] Compared with the current existing technologies, the implementation method of CockroachDB database snapshot has the following characteristics:

[0068] First, it can effectively solve the problem of data loss caused by user misoperation, and based on CockroachDB's multi-copy storage strategy, it can also cope with the problem of data loss caused by single-machine system failure.

[0069] Second, since this method only defines a new system snapshot table to store the database snapshot definition, no processing of the stored data is required when creating a database snapshot, so snapshot creation is very fast; and when restoring the snapshot, the data that has changed from the snapshot point to the current time point is queried for restoration, ensuring efficient recovery capabilities.

[0070] The embodiment described above is only one specific implementation of the present invention. Common changes and substitutions made by those skilled in the art within the scope of the technical solution of the present invention should be included in the protection scope of the present invention.

Claims

1. A method for implementing a CockroachDB database snapshot, characterized in that: By leveraging CockroachDB's multi-version management and historical data garbage collection mechanisms, we added system snapshot table management and snapshot definition DDL statements, and redesigned the MVCC data garbage collection strategy to implement the database snapshot function. The system snapshot table is used to record the ID and snapshot time point of the snapshot object, and the snapshot object is a database or a data table. By adding a snapshot object ID and snapshot time point record to the system snapshot table, a snapshot can be created for the database or the data table. The snapshot DDL statements include creating a snapshot (CREATE SNAPSHOT), querying a snapshot and defining it (SHOW SNAPSHOT), deleting a snapshot (DROP SNAPSHOT), restoring the data at a certain snapshot point to the current data (REVERT SNAPSHOT), and querying the data at a certain snapshot point (SELECT ... FROM ... AS OF SNAPSHOT).<snapshot_name> ; To query snapshot point data, query the system snapshot table according to the snapshot name to obtain the snapshot time point, and then query the MVCC data of "time point t <= snapshot time point" in the underlying storage engine; To restore snapshot data, you need to query the data that has changed from the snapshot time point to the current time point. Start a new transaction and perform the following steps: S11. New data added after the snapshot time point is overwritten with the DELETE mark; S12. Data modified or deleted after the snapshot time point is overwritten with the value before the snapshot; During the recovery of snapshot data, no other transactions can be performed on the database or table.

2. The method for implementing a CockroachDB database snapshot according to claim 1, wherein: The redesigned garbage collection strategy for MVCC data adds snapshot time comparison when comparing the timestamps of MVCC data in the garbage collection process. The process is as follows: S21. Get the database or data table ID to which the current shard to be garbage collected belongs; S22. Query the system snapshot table to check whether the database or table has a snapshot defined and obtain the snapshot point time; S23. Traverse all MVCC data of the current shard one by one; S24. Compare the timestamp of the MVCC data key with the snapshot time and current time: If the Key timestamp = 0, it means that the transaction is not committed and garbage collection will not be performed; If the Key timestamp has not expired, garbage collection will not be performed; If the Key is the latest data, garbage collection will not be performed; If the Key timestamp is the closest to the snapshot point, garbage collection will not be performed; Other expired keys can be garbage collected.

3. The method for implementing a CockroachDB database snapshot according to claim 2, wherein: For the earliest snapshot point, if the Key timestamp is the time closest to the snapshot point, garbage collection can be performed when the Key is marked DELETE.

4. A device for implementing a CockroachDB database snapshot, characterized in that: include: at least one memory and at least one processor; The at least one memory is used to store a machine-readable program; The at least one processor is configured to call the machine-readable program to execute the method according to any one of claims 1 to 3.

5. A computer-readable medium, characterized in that: The computer readable medium stores computer instructions, which, when executed by a processor, enable the processor to execute any one of the methods of claims 1 to 3.

Citation Information

Patent Citations

  • Efficient methods and systems for consistent read in record-based multi-version concurrency control

    CN106462586A

  • Backup and restore in a distributed database utilizing consistent database snapshots

    CN107835983A

  • Version Garbage Collection Using Snapshot Lists

    US20140149353A1

  • Group garbage collection for multi-version concurrency control in database systems

    US20170357576A1

  • Hybrid Database Concurrent Transaction Control

    US20180074919A1