A method for realizing the slave machine to quickly maintain data consistency with the master machine in the master-slave replication mode of OpenGauss database
By designing a WAL parallel playback solution in the OpenGauss database, the problem of data inconsistency between the slave and the host caused by the slowdown of WAL playback in the standby database was solved, data consistency in the master-slave replication mode was achieved, and database performance and service capabilities were improved.
Patent Information
- Application Number
- CN202111592861.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-23
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2041-12-23
AI Technical Summary
In the master-slave replication mode of the OpenGauss database, the WAL playback speed of the standby database slows down, causing data inconsistency between the slave and the host, reducing database performance.
Design a WAL parallel playback solution, transform the WAL playback thread into a multi-threaded parallel playback, and distinguish transaction logs and non-transaction logs. Use a separate thread to play back transaction logs, multi-threads to play back non-transaction logs in parallel, and handle dependencies between transactions through dispatch functions. , ensure data consistency.
It improves the WAL playback speed of the standby database, reduces WAL accumulation, keeps the WAL difference between the main database and the standby database within the set threshold, and significantly improves the external service capabilities and performance of the database.
Smart Images

Figure CN114281833B
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of relational database management and operating system, and in particular to a method and use thereof for realizing a slave machine to quickly maintain data consistency with a host machine in an OpenGauss database master-slave replication mode. Background Art
[0002] OpenGauss is an open source relational database management system, whose kernel is derived from PostgreSQL. As an open source and free database platform, it aims to encourage community contribution and cooperation. At present, the OpenGauss database already supports streaming physical replication (streaming replication) of write-ahead logs (WAL). Database users can use streaming replication to receive WAL from the master database while replaying the received WAL during the backup process. The backup database continuously receives and replays WAL from the master database to complete the construction and data synchronization of the backup database. However, as the pressure on the master database increases, a large amount of WAL will be generated. The playback speed of the backup database WAL slows down, resulting in WAL accumulation. When the difference between the master and backup database WAL reaches the set threshold, the database performance will be significantly reduced. Therefore, if there is a technical solution that can enable the backup database to quickly complete WAL playback, reduce WAL accumulation, and keep the difference between the master and backup database WAL always within the set threshold range, it can significantly improve the external service capabilities of the OpenGauss database in the master-slave replication mode, and provide a strong technical guarantee for the normal performance of the database performance. Summary of the invention
[0003] In order to overcome the defect that the slow WAL playback speed of the standby database in the master-slave replication mode of the OpenGauss database causes the inconsistency between the slave and the master data, thereby reducing the database performance, the present invention provides a solution. The purpose of the present invention is to design a method for the slave to quickly maintain data consistency with the master in the master-slave replication mode for the OpenGauss database, and the method is achieved by designing a set of WAL parallel playback solutions.
[0004] Specifically, the present invention provides a method for realizing that a slave machine quickly maintains data consistency with a host machine in a master-slave replication mode of an OpenGauss database. The method transforms an existing WAL playback thread, splits the playback thread work of a standby database from a single thread playback to a multi-thread parallel playback, and distinguishes WAL types into transaction logs and non-transaction logs, wherein the transaction logs include transaction operation-related logs, and the non-transaction logs include data operation-related logs; for transaction logs, the method still uses a single thread playback to avoid the occurrence of disordered events, and for non-transaction logs, the method uses multi-thread parallel playback to speed up WAL playback.
[0005] Furthermore, the transaction log in the method of the present invention includes a CSN (COMMIT SEQUENCE NUM) log and a transaction commit log; and the non-transaction log includes an insert operation log and an update operation log.
[0006] Furthermore, during the implementation of the method of the present invention, when multi-threaded parallel playback processing is used for non-transaction logs, if there are dependencies between the non-transaction logs, the dependencies are divided into intra-transaction dependencies and inter-transaction dependencies. For different types of dependencies, the following different processing methods are used:
[0007] (1) Regarding the dependency within a transaction, when there are multiple WALs for the same operation object within a transaction, during parallel playback, the worker thread number for playing back the log is calculated based on the digital label identifying the physical location of the operation object obtained from the WAL information. Since the digital label identifying the physical location of the operation object is unique, it is ensured that all non-transaction logs containing the same operation object are played back by the same worker thread.
[0008] (2) In view of the dependencies between transactions, the WALs played back in parallel are encapsulated and the relevant tag information is saved in the encapsulation structure. At the same time, the mapping relationship between the WAL type and the dispatch function is set in the database kernel. The dispatch function that matches the different types of WALs is called, and the dependencies between transactions are handled by the dispatch function.
[0009] Furthermore, the above processing method (2) calls the dispatch function matching the different types of WALs in the dependency relationship between transactions, and the dispatch function processes the dependency relationship between transactions, specifically including the following processing methods:
[0010] (1) When the WAL type is RM_HEAP_ID, the corresponding dispatch function is DispatchHeapRecord. DispatchHeapRecord determines which REDO_BATCH thread will replay the WAL based on whether the WAL involves data block changes:
[0011] (a) If max_bloc_id>=0, it indicates that data blocks are modified. Each data block is traversed and groups are selected based on the physical file OID of the data block to ensure that WAL with the same physical file OID is played back by the same working thread;
[0012] (b) If max_block_id < 0, indicating that no data block changes are involved, the WAL is assigned to multiple worker threads for parallel playback;
[0013] (2) When the WAL type is RM_STANDBY_ID, the corresponding dispatch function is DispatchStandbyRecord, which dispatches this WAL to the REDO_TRXN_MNG thread for playback;
[0014] (3) When the WAL type is RM_XACT_ID, the corresponding dispatch function is DispatchXactRecord. DispatchXactRecord first determines whether this WAL will delete the file:
[0015] (a) If the file will be deleted, this WAL is dispatched to all REDO_BATCH threads for playback;
[0016] (b) If the file is not deleted, this WAL is dispatched to the REDO_TRXN_MNG thread for playback.
[0017] Preferably, the present invention implements a method for a slave machine to quickly maintain data consistency with a host machine in a master-slave replication mode of an OpenGauss database, comprising the following steps:
[0018] (1) The log receiving thread writes the WAL sent by the master database to the disk;
[0019] (2) The Startup thread reads the WAL from the disk and divides the WAL into transaction logs and non-transaction logs, where:
[0020] (a) The transaction log is played back by the REDO_TRXN_WORKER thread alone.
[0021] (b) Non-transaction logs are played back in parallel by multiple threads;
[0022] (3) For non-transaction logs, different dispatch functions are called according to the type of non-transaction logs. The dispatch function dispatches the non-transaction logs and puts them into the SPSC queue of the corresponding REDO_PAGE_WORKER thread. If the queue is full, the Startup thread will wait in a loop.
[0023] (4) The REDO_PAGE_WORKER thread reads the non-transaction log from the SPSC queue and plays it back, and updates the position of the non-transaction log that has been played back by this thread;
[0024] (5) The Startup thread obtains the minimum value of the non-transaction log that has been played back by the REDO_PAGE_WORKER thread, determines whether the transaction log can be played back, and calls the corresponding playback function if it can.
[0025] In addition, the present invention also relates to the use of the above-mentioned method for realizing the slave machine to quickly maintain data consistency with the host machine in the master-slave replication mode of the OpenGauss database in relational database management or operating system.
[0026] In summary, the present invention proposes for the first time a method for realizing the slave machine to quickly maintain data consistency with the host machine in the master-slave replication mode of the OpenGauss database. The method, by designing a set of WAL parallel playback schemes, well overcomes the defect that the slave machine and the host machine data are inconsistent due to the slowdown of the WAL playback speed of the standby database in the master-slave replication mode of the OpenGauss database, thereby reducing the performance of the database. The method can enable the standby database to quickly complete WAL playback, reduce WAL accumulation, and keep the WAL difference between the master database and the standby database always within the set threshold range. Through the stress test performed in the master-slave mode, the results show that the database performance is significantly better than that of the non-enabled state after WAL parallel playback is enabled. The WAL parallel playback strategy of the method can significantly improve the external service capability of the OpenGauss database in the master-slave replication mode, and provide a strong technical guarantee for the normal performance of the database performance. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments of the present invention. Obviously, the following drawings are only some embodiments recorded in the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0028] Figure 1 It is a schematic diagram of the implementation process of the method of the present invention.
[0029] Figure Note: Startup thread: WAL processing thread;
[0030] SPSC: used to save the WAL queue;
[0031] REDO_BATCH thread: reads and parses WAL from the SPSC queue of the Startup thread, and then puts it into the SPSC queue of the REDO_PAGE_MNG thread;
[0032] REDO_PAGE_MNG thread: reads the parsed WAL from its own SPSC queue and dispatches it to the REDO_PAGE_WORKER thread;
[0033] REDO_PAGE_WORKER thread: responsible for the actual non-transaction log playback;
[0034] REDO_TRXN_MNG thread: reads a WAL from its own SPSC queue each time, and then dispatches it to the REDO_TRXN_WORKER thread for processing;
[0035] REDO_TRXN_WORKER thread: responsible for the actual transaction log playback. DETAILED DESCRIPTION
[0036] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with specific embodiments and corresponding drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of them. The present invention can also be implemented or applied through other different specific implementation methods. The details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present invention.
[0037] At the same time, it should be understood that the protection scope of the present invention is not limited to the specific embodiments described below; it should also be understood that the terms used in the embodiments of the present invention are for describing specific embodiments rather than for limiting the protection scope of the present invention.
[0038] Example 1: A method for realizing a slave machine to quickly maintain data consistency with a master machine in the master-slave replication mode of the OpenGauss database (see Figure 1 ), this method transforms the existing WAL playback thread, splits the standby database playback thread work from a single thread playback to multi-thread parallel playback, and distinguishes the WAL type into transaction log and non-transaction log; transaction log includes transaction operation related logs, such as CSN (COMMIT SEQUENCE NUM) log and transaction commit log; non-transaction log includes data operation related logs, such as insert operation log and update operation log; for transaction log, this method still uses a single thread playback to avoid the occurrence of disordered events, and for non-transaction log, this method uses multi-thread parallel playback to speed up WAL playback.
[0039] Specifically, the method comprises the following steps:
[0040] Step 1: The log receiving thread writes the WAL sent by the master database to the disk;
[0041] Step 2: The Startup thread reads the WAL from the disk and divides the WAL into transaction logs and non-transaction logs, where:
[0042] (a) The transaction log is played back by the REDO_TRXN_WORKER thread alone.
[0043] (b) Non-transaction logs are played back in parallel by multiple threads;
[0044] Step 3: For non-transaction logs, different dispatch functions are called according to the type of non-transaction logs. The dispatch function dispatches the non-transaction logs and puts them into the SPSC queue of the corresponding REDO_PAGE_WORKER thread. If the queue is full, the Startup thread will wait in a loop.
[0045] Step 4: The REDO_PAGE_WORKER thread reads the non-transaction log from the SPSC queue and plays it back, and updates the position of the non-transaction log that has been played back by this thread;
[0046] Step 5: The Startup thread obtains the minimum value of the non-transaction log that has been played back by the REDO_PAGE_WORKER thread, determines whether the transaction log can be played back, and calls the corresponding playback function if it can.
[0047] Embodiment 2: In this method, when multi-threaded parallel playback is used for non-transaction logs, if there are dependencies between non-transaction logs, the dependencies are divided into intra-transaction dependencies and inter-transaction dependencies. For different types of dependencies, the following different processing methods are used:
[0048] (1) Regarding the dependency within a transaction, when there are multiple WALs for the same operation object (for example, the same table) within a transaction (for example, first INSERT and then DELETE), during parallel playback, the worker thread number for playing back the log is calculated based on the digital label identifying the physical location of the operation object obtained from the WAL information (the relfilenode of the table, which can be obtained based on the WAL information). Since the digital label identifying the physical location of the operation object is unique, it is ensured that all non-transaction logs containing the same operation object (the same table) are played back by the same worker thread.
[0049] (2) Based on the dependencies between transactions, the WALs played back in parallel are encapsulated, and the relevant marking information (such as whether the logs need to be synchronized to other groups) is saved in the encapsulation structure. At the same time, the mapping relationship between the WAL type and the dispatch function is set in the database kernel. The dispatch function that matches the different types of WALs is called, and the dispatch function dispatches the non-transaction logs.
[0050] In order to explain this technical solution more clearly, the following examples are given for demonstration:
[0051] The database master database operations are as follows:
[0052] create table test(a int);
[0053] insert into test values(1);
[0054] Database insert data log:
[0055]
[0056] A total of 3 WALs were generated:
[0057] a.XLOG_HEAP_INSERT: performs insert operation;
[0058] b.XLOG_STANDBY_CSN_COMMITING: This WAL is used to indicate that the transaction is being committed;
[0059] c.XLOG_XACT_COMMIT_COMPACT: transaction commit log.
[0060] Replay of the standby database:
[0061] (a) The type of the first WAL is RM_HEAP_ID (which can be obtained from the desc information in the WAL, the Heap mapping type is RM_HEAP_ID), and the corresponding dispatch function is DispatchHeapRecord (obtained through the mapping relationship between the WAL type and the dispatch function). DispatchHeapRecord determines which REDO_BATCH thread will replay the WAL based on whether the WAL involves data block changes:
[0062] (1) If max_bloc_id>=0, it indicates that data blocks are modified. Each data block is traversed and groups are selected based on the physical file OID (relfilenode) of the data block to ensure that WAL with the same physical file OID is played back by the same working thread;
[0063] (2) If max_block_id < 0, indicating that no data block changes are involved, the WAL is assigned to multiple worker threads for parallel playback;
[0064] (b) The type of the second WAL is RM_STANDBY_ID. When wal_level>=hot_standby, the master database will record a WAL of this type during the transaction commit phase. The corresponding dispatch function is DispatchStandbyRecord, which dispatches this WAL to the REDO_TRXN_MNG thread for playback.
[0065] (c) The type of the third WAL is RM_XACT_ID, and the corresponding dispatch function is DispatchXactRecord. DispatchXactRecord first determines whether this WAL will delete the file:
[0066] (1) If the file will be deleted, this WAL is dispatched to all REDO_BATCH threads for playback;
[0067] (2) If the file is not deleted, this WAL is dispatched to the REDO_TRXN_MNG thread for playback.
[0068] The above description is only an embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, replacement, etc. made within the spirit and principle of the present invention should be included in the scope of protection of the claims of the present invention.
Claims
1. A method for realizing the slave machine to quickly maintain data consistency with the host machine in the master-slave replication mode of the OpenGauss database, characterized in that: This method transforms the existing WAL playback thread, splits the standby database playback thread work from single thread playback to multi-thread parallel playback, and distinguishes the WAL type into transaction log and non-transaction log, the transaction log includes transaction operation related log, and the non-transaction log includes data operation related log; for transaction log, this method still uses single thread playback to avoid the occurrence of disordered events, and for non-transaction log, this method uses multi-thread parallel playback to speed up WAL playback; When multi-threaded parallel playback is used for non-transaction logs, if there are dependencies between non-transaction logs, the dependencies are divided into intra-transaction dependencies and inter-transaction dependencies. Different processing methods are used for different types of dependencies: (I) Regarding the dependency within a transaction, when there are multiple WALs for the same operation object within a transaction, during parallel playback, the worker thread number for playing back the log is calculated based on the digital label identifying the physical location of the operation object obtained from the WAL information. Since the digital label identifying the physical location of the operation object is unique, it is ensured that all non-transaction logs containing the same operation object are played back by the same worker thread; (ii) In view of the dependencies between transactions, the WALs played back in parallel are encapsulated, and the relevant tag information is saved in the encapsulation structure. At the same time, the mapping relationship between the WAL type and the dispatch function is set in the database kernel. The dispatch function matching different types of WALs is called, and the dependencies between transactions are processed by the dispatch function; the dispatch function matching different types of WALs is called, and the dependencies between transactions are processed by the dispatch function, which specifically includes the following processing methods: (1) When the WAL type is RM_HEAP_ID, the corresponding dispatch function is DispatchHeapRecord. DispatchHeapRecord determines which REDO_BATCH thread will replay the WAL based on whether the WAL involves data block changes: (a) If max_block_id >= 0, it indicates that data blocks are modified. Each data block is traversed and groups are selected based on the physical file OID of the data block to ensure that WALs with the same physical file OID are played back by the same worker thread; (b) If max_block_id < 0, indicating that no data block changes are involved, the WAL is assigned to multiple worker threads for parallel playback; (2) When the WAL type is RM_STANDBY_ID, the corresponding dispatch function is DispatchStandbyRecord, which dispatches this WAL to the REDO_TRXN_MNG thread for playback; (3) When the WAL type is RM_XACT_ID, the corresponding dispatch function is DispatchXactRecord. DispatchXactRecord first determines whether this WAL will delete the file: (a) If the file will be deleted, this WAL is dispatched to all REDO_BATCH threads for playback; (b) If the file is not deleted, this WAL is dispatched to the REDO_TRXN_MNG thread for playback.
2. The method for realizing the master-slave replication mode of OpenGauss database in the slave machine to quickly maintain the consistency of data with the master machine according to claim 1, characterized in that: The transaction log includes a CSN log and a transaction commit log; the non-transaction log includes an insert operation log and an update operation log.
3. The method for realizing the master-slave replication mode of OpenGauss database in the slave machine to quickly maintain the consistency of data with the master machine according to claim 1, characterized in that: The method comprises the following steps: (1) The log receiving thread writes the WAL sent by the main database to the disk; (2) The Startup thread reads the WAL from the disk and divides the WAL into transaction logs and non-transaction logs, where: (a) The transaction log is played back by the REDO_TRXN_WORKER thread alone, (b) Non-transaction logs are played back in parallel by multiple threads; (3) For non-transaction logs, different dispatch functions are called according to the type of non-transaction logs. The dispatch function dispatches the non-transaction logs and puts them into the SPSC queue of the corresponding REDO_PAGE_WORKER thread. If the queue is full, the Startup thread will wait in a loop. (4) The REDO_PAGE_WORKER thread reads the non-transaction log from the SPSC queue and replays it, and updates the position of the non-transaction log that has been replayed by this thread; (5) The Startup thread obtains the minimum value of the non-transaction log that has been played back by the REDO_PAGE_WORKER thread, determines whether the transaction log can be played back, and calls the corresponding playback function if it can.
4. Use of the method for realizing the rapid maintenance of data consistency between a slave and a host in the master-slave replication mode of OpenGauss database as described in any one of claims 1 to 3 in relational database management or operating system.
