Data recovery method and device for time sequence database
By using MMAP technology and LSN recording mechanism in the time series database, combined with the UndoPut and RedoPut interfaces, batch writing and recovery of data are realized, which solves the problem of data loss in the time series database under abnormal circumstances and ensures the consistency and integrity of the data. sex.
Patent Information
- Application Number
- CN202410823735.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-25
- Publication Date
- 2024-09-10
AI Technical Summary
Under abnormal circumstances, such as a power outage, the time series database cannot effectively determine whether the data has been successfully downloaded to disk, making it difficult to guarantee data integrity and durability.
MMAP technology is used to realize data reading and writing, and the writing information of each batch of data is recorded through LSN (logical sequence number), which supports the writing and recovery of batch data. It implements data rollback and recovery operations through the UndoPut and RedoPut interfaces to ensure that the data is in Correctness in unusual scenarios.
While ensuring normal data writing performance, it can correctly restore data, ensure data consistency and integrity, and solve the problem of data loss in time series databases under abnormal circumstances.
Smart Images

Figure CN118626311A
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of time series databases, and specifically provides a data recovery method and device for a time series database. Background Art
[0002] Time series data is a series of data generated over time, in simple terms, data with timestamps. A time series database (TSDB) is a database optimized for ingesting, processing, and storing timestamped data. Such data may include metrics from servers and applications, readings from IoT sensors, user interactions on a website or application, or trading activities on financial markets.
[0003] One of the main characteristics of a time series database is that a large amount of data is inserted frequently, with tens of millions or even hundreds of millions of data inserted per second. At the same time, operations such as query and aggregation are mainly aimed at recently inserted data. Therefore, when the database crashes due to abnormal circumstances such as power outages, it is particularly important to ensure the integrity and validity of the data.
[0004] Currently, WAL (Write Ahead Log) is the most common means of ensuring the atomicity and persistence of data operations in database systems. However, in conventional databases, data is first written to WAL and then to memory. Once the data is persisted to disk, the write is considered successful.
[0005] However, for time series databases implemented based on MMAP technology, the writing process is controlled by the operating system and persisted to disk, so it is impossible to determine whether the data is successfully written to disk. Summary of the invention
[0006] The present invention aims at solving the above-mentioned deficiencies of the prior art and provides a data recovery method for a time series database with strong practicality.
[0007] A further technical task of the present invention is to provide a data recovery device for a time series database that is reasonably designed, safe and applicable.
[0008] The technical solution adopted by the present invention to solve its technical problem is:
[0009] A data recovery method for a time series database comprises the following steps:
[0010] S1. The stored data uses MMAP technology to realize data reading and writing. After the data file is mapped to the memory address, the writing and reading of data are memory API operations, and the synchronization of memory data to physical files is automatically completed by the operating system;
[0011] S2. The timing engine supports batch data writing. A batch data payload includes data from multiple devices and across multiple time partitions, and will modify multiple data files.
[0012] S3. When each batch of data payload is written, the corresponding partition table first applies for space and increases the space usage allocated, where allocated is the number of data items in the block that have applied for space.
[0013] S4. Implement data rollback operation through UndoPut interface;
[0014] S5. Implement data recovery operation through RedoPut interface.
[0015] Furthermore, in step S2, the data in the time series table is added with the LSN data type, and the LSN number of each batch of written data is recorded to ensure the atomic consistency of data writing;
[0016] During redo / undo, the batch data payload is rolled back or restored according to the LSN. All rows written in the same batch of data record the same LSN number. The LSN is strictly incremented, but the increment is not necessarily continuous.
[0017] Furthermore, in step S3, after the data is written into the applied space, when the batch of data is fully written, the data is published to a readable state, that is, pulished is increased, where pulished is the number of data read by block.
[0018] Furthermore, in step S4, it further includes:
[0019] S4-1. WAL records each batch of written data. If the upper-layer transaction detects that the data has not been committed within a certain period of time, the data will be rolled back.
[0020] S4-2. The storage engine parses the timestamp of the data, splits it into time partitions, and rolls back the data according to the corresponding partitions.
[0021] S4-3. If no corresponding data is found, it means that the current Payload has not been written and no processing is required.
[0022] Furthermore, in step S4-1, each time the WAL calls the UndoPut interface, it passes in a batch of data Payloads that need to be rolled back and the LSN numbers of the batch of Payloads.
[0023] Furthermore, in step S4-2, the partition table is traversed from the beginning through the incoming LSN to find data with consistent timestamps and LSNs, and the corresponding published is updated to an unreadable state. The space that has been written is not processed until all current data is processed or the current partition is traversed.
[0024] Furthermore, in step S5, it further includes:
[0025] S5-1. If an abnormal situation occurs and causes downtime, during the restart process, the WAL module will rewrite the data written within a period of time and call the RedoPut interface;
[0026] S5-2, first check whether there is space for application in the current partition. If there is no space for application, write the current Payload directly;
[0027] S5-3, if the space has been applied for, start traversing from the beginning of the partition table;
[0028] S5-4, take out the blocks in the queue one by one, and compare the allocated and published data of the blocks;
[0029] S5-5. If step S5-3 is completed and the queue is empty, it means that no space has been applied for the incoming payload, or no data has been written after the space has been applied for.
[0030] S5-6. Check the number of written entries of the incoming Payload to see if all have been written.
[0031] Furthermore, in step S5-1, the storage engine parses the timestamp of the payload, splits it according to the time partition, and restores the data according to the time partition, that is, the payload restored each time can only be the batch data of one time partition;
[0032] In step S5-2, if space has been applied for, traverse from the beginning of the partition table, find the block corresponding to the first data of the written Payload through the timestamp and LSN, and put the block and subsequent blocks into the queue;
[0033] In step S5-3, the blocks in the queue are taken out one by one, and the allocated data of the block is compared with the published data. If the two are consistent, it means that the block is full of corresponding data, and the next block is searched. If they are not consistent, it means that the current block has applied for space.
[0034] But no data is written. The corresponding data in the Payload is written to the requested space until all blocks in the queue have been traversed or the current Payload has been written.
[0035] Furthermore, in step S5-5, if the queue is empty, it means that no space has been applied for the incoming payload, or no data has been written to the space after the space has been applied. In this case, it is necessary to re-traverse the blocks under the partition to determine the allocated and published data of the block. If there is a difference, write the corresponding payload to the applied space.
[0036] Check the number of entries of the incoming Payload to see if all entries have been written. If there are entries left, it means that no space has been requested for the remaining Payloads. In this case, call the data writing interface push_back to write the remaining data into the partition table.
[0037] A data recovery device for a time series database, comprising: at least one memory and at least one processor;
[0038] The at least one memory is used to store a machine-readable program;
[0039] The at least one processor is used to call the machine-readable program to execute a data recovery method for a time series database.
[0040] Compared with the prior art, the data recovery method and device of a time series database of the present invention have the following outstanding beneficial effects:
[0041] According to the characteristics of MMAP, the present invention has developed a data rollback and recovery mechanism based on MMAP, which realizes data recovery and rollback in abnormal scenarios by recording the write time, LSN and corresponding batch data. It has been verified that while ensuring normal data write performance, data can be correctly restored in abnormal scenarios such as power outages, ensuring data consistency. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] 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.
[0043] Attached Figure 1 It is an UndoPut flow chart in a data recovery method for a time series database;
[0044] Attached Figure 2The RedoPut flowchart in a data recovery method for a time series database. DETAILED DESCRIPTION
[0045] In order to enable those skilled in the art to better understand the solution of the present invention, the present invention is further described in detail below in conjunction with specific implementation methods. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than 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 are within the scope of protection of the present invention.
[0046] A best embodiment is given below:
[0047] A data recovery method for a time series database in this embodiment has the following steps:
[0048] S1. The stored data uses MMAP technology to realize data reading and writing. After the data file is mapped to the memory address, the writing and reading of data are memory API operations, and the operating system automatically completes the synchronization of memory data to physical files (swapping in and out of memory pages, Sync, etc.).
[0049] S2. The timing engine supports batch data writing. A batch data payload includes data from multiple devices and across multiple time partitions, and will modify multiple data files.
[0050] The LSN data type is added to the data in the time series table to record the LSN number of each batch of written data to ensure the atomic consistency of data writing. During redo / undo, the batch data payload is rolled back or restored according to the LSN.
[0051] All rows written in the same batch of data record the same LSN number, and the LSN is strictly increasing (not necessarily continuous).
[0052] S3. When each batch of data payload is written, the corresponding partition table first applies for space and increases the space usage allocated, where allocated is the number of data items in the block that have applied for space.
[0053] Then the data is written into the requested space. When the data is fully written, the data is published to a readable state, that is, pulished (the number of data that can be read by the block) is increased.
[0054] S4. Implement data rollback operation through UndoPut interface;
[0055] like Figure 1 As shown, further comprising:
[0056] S4-1. WAL records each batch of written data. If the upper-layer transaction detects that the data has not been committed within a certain period of time, the data will be rolled back.
[0057] Each time WAL calls the UndoPut interface, it passes in a batch of data payloads that need to be rolled back and the LSN numbers of these payloads.
[0058] S4-2. The storage engine parses the timestamp of the batch data, splits it according to the time partition, and rolls back the data according to the corresponding partition. Through the incoming LSN, traverse from the beginning of the partition table, find the data with the same timestamp and LSN, update the corresponding published to an unreadable state, and do not process the space that has been written. This continues until all the current data is processed or the current partition is traversed.
[0059] S4-3. If no corresponding data is found, it means that the current Payload has not been written and no processing is required.
[0060] S5. Implement data recovery operation through RedoPut interface;
[0061] like Figure 2 As shown, further comprising:
[0062] S5-1. If a power outage or other abnormal situation causes downtime, during the restart process, the WAL module will rewrite the data written within a period of time and call the RedoPut interface.
[0063] The storage engine parses the timestamp of the payload, splits it according to time partitions, and restores data according to time partitions. That is, the payload restored each time can only be batch data of one time partition.
[0064] S5-2. First check whether there is space available in the current partition. If there is no space available, write the current Payload directly.
[0065] S5-3. If space has been applied for, start traversing from the beginning of the partition table, find the block corresponding to the first data of the written Payload (with the same timestamp and LSN) through the timestamp and LSN, and put the block and subsequent blocks into the queue.
[0066] S5-4. Take out the blocks in the queue one by one, and compare the allocated data of the block with the published data. If the two are consistent, it means that the block has been filled with the corresponding data, and continue to search for the next block. If they are not consistent, it means that the current block has applied for space.
[0067] But no data is written. The corresponding data in the Payload is written to the requested space until all blocks in the queue have been traversed or the current Payload has been written.
[0068] S5-5, step S5-3 is executed. If the queue is empty, it means that no space has been applied for the incoming payload, or no data has been written after the space has been applied. At this time, it is necessary to re-traverse the blocks under the partition to determine the allocated blocks and the published data.
[0069] If there is a difference, the corresponding Payload is written to the applied space.
[0070] S5-6. Check the number of written entries of the incoming Payload to see if all entries have been written. If there are any entries left, it means that no space has been requested for the remaining Payloads. In this case, call the data writing interface push_back to write the remaining data into the partition table.
[0071] Follow the above steps to recover the data in all partitions one by one.
[0072] Based on the above method, a data recovery device for a time series database in this embodiment includes: at least one memory and at least one processor;
[0073] The at least one memory is used to store a machine-readable program;
[0074] The at least one processor is used to call the machine-readable program to execute a data recovery method for a time series database.
[0075] The above-mentioned specific implementations are only specific cases of the present invention. The patent protection scope of the present invention includes but is not limited to the above-mentioned specific implementations. Any technical solutions that comply with the claims of the present invention and any appropriate changes or substitutions made by ordinary technicians in the relevant technical field shall fall within the patent protection scope of the present invention.
[0076] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.
Claims
1. A data recovery method for a time series database, characterized in that: The steps are as follows: S1. The stored data uses MMAP technology to realize data reading and writing. After the data file is mapped to the memory address, the writing and reading of data are memory API operations, and the synchronization of memory data to physical files is automatically completed by the operating system; S2. The timing engine supports batch data writing. A batch data payload includes data from multiple devices and across multiple time partitions, and will modify multiple data files. S3. When each batch of data payload is written, the corresponding partition table first applies for space and increases the space usage allocated, where allocated is the number of data items in the block that have applied for space. S4. Implement data rollback operation through UndoPut interface; S5. Implement data recovery operation through RedoPut interface.
2. The data recovery method of a time series database according to claim 1, characterized in that: In step S2, the data in the time series table is added with the LSN data type, and the LSN number of each batch of written data is recorded to ensure the atomic consistency of data writing; During redo / undo, the batch data payload is rolled back or restored according to the LSN. All rows written in the same batch of data record the same LSN number. The LSN is strictly incremented, but the increment is not necessarily continuous.
3. The data recovery method of a time series database according to claim 2, characterized in that: In step S3, after the data is written into the applied space, when the batch of data is fully written, the data is published to a readable state, that is, pulished is increased, where pulished is the number of data read by the block.
4. The method for recovering data from a time series database according to claim 3, characterized in that: In step S4, further comprising: S4-1. WAL records each batch of written data. If the upper-layer transaction detects that the data has not been committed within a certain period of time, the data will be rolled back. S4-2. The storage engine parses the timestamp of the data, splits it into time partitions, and rolls back the data according to the corresponding partitions. S4-3. If no corresponding data is found, it means that the current Payload has not been written and no processing is required.
5. The method for recovering data from a time series database according to claim 4, characterized in that: In step S4-1, each time WAL calls the UndoPut interface, it passes in a batch of data Payloads that need to be rolled back and the LSN numbers of these Payloads.
6. The method for recovering data of a time series database according to claim 5, characterized in that: In step S4-2, the partition table is traversed from the beginning through the incoming LSN to find data with consistent timestamps and LSNs, and the corresponding published is updated to an unreadable state. The space that has been written is not processed until all current data has been processed or the current partition has been traversed.
7. The method for recovering data of a time series database according to claim 6, characterized in that: In step S5, further comprising: S5-1. If an abnormal situation occurs and causes downtime, during the restart process, the WAL module will rewrite the data written within a period of time and call the RedoPut interface; S5-2, first check whether there is space available in the current partition. If there is no space available, just write the current Payload directly; S5-3, if the space has been applied for, start traversing from the beginning of the partition table; S5-4, take out the blocks in the queue one by one, and compare the allocated and published data of the blocks; S5-5. If step S5-3 is completed and the queue is empty, it means that no space has been applied for the incoming payload, or no data has been written after the space has been applied for. S5-6. Check the number of written entries of the incoming Payload to see if all have been written.
8. The method for recovering data of a time series database according to claim 7, characterized in that: In step S5-1, the storage engine parses the timestamp of the payload, splits it according to the time partition, and restores the data according to the time partition. That is, the payload restored each time can only be the batch data of one time partition. In step S5-2, if space has been applied for, traverse from the beginning of the partition table, find the block corresponding to the first data of the written Payload through the timestamp and LSN, and put the block and subsequent blocks into the queue; In step S5-3, the blocks in the queue are taken out one by one, and the allocated data of the block is compared with the published data. If the two are consistent, it means that the block is full of corresponding data, and the next block is searched. If they are not consistent, it means that the current block has applied for space. But no data is written. The corresponding data in the Payload is written to the requested space until all blocks in the queue have been traversed or the current Payload has been written.
9. The method for recovering data of a time series database according to claim 8, characterized in that: In step S5-5, if the queue is empty, it means that no space has been applied for the incoming payload, or no data has been written after the space has been applied for. In this case, it is necessary to re-traverse the blocks under the partition to determine the allocated and published data of the block. If there is a difference, write the corresponding payload to the applied space; Check the number of entries of the incoming Payload to see if all entries have been written. If there are entries left, it means that no space has been requested for the remaining Payloads. In this case, call the data writing interface push_back to write the remaining data into the partition table.
10. A data recovery device for a time series database, 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 9.
