A database transaction group submission method, device, equipment and storage medium
By allowing slave threads to execute other transactions during the database transaction group submission process, the problem of low concurrency caused by slave thread waiting in the prior art is solved, and the concurrency capability and performance of the system are improved.
Patent Information
- Application Number
- CN202011261844.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-11-12
- Publication Date
- 2024-09-03
- Estimated Expiration
- 2040-11-12
AI Technical Summary
In the prior art, during the submission process of a transaction group executed by multiple threads respectively, the slave thread is in a waiting state, resulting in low concurrency capability of the system.
Ensure that the slave thread is not blocked waiting for the master thread to complete the transaction group commit by controlling the main thread to perform some or all processing stages during the transaction group submission process, and allowing the slave thread to perform other transactions in addition to the transaction group during the process. state.
This improves the utilization rate of a single thread, thereby improving the concurrency capability of the system, allowing the slave thread to continue processing other tasks during the transaction group submission process, and improving the overall performance of the system.
Smart Images

Figure CN112434007B
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of information technology, and in particular to a method, device, equipment and storage medium for submitting a database transaction group. Background Art
[0002] With the continuous development of information technology, people generate a large amount of data in their work and life, and usually store this large amount of data in databases.
[0003] The client can access the data in the database by sending an access request to the database. When the database receives the access request, it can start a transaction and execute the transaction through a thread in the database. When the thread executes the transaction, it needs to commit the transaction.
[0004] In the prior art, transactions executed by multiple threads can form a transaction group, which can be submitted by a master thread among the multiple threads, and the threads other than the master thread among the multiple threads are slave threads. During the submission of the transaction group by the master thread, the slave thread is always in a waiting state, i.e., a blocked state, resulting in a low concurrency capability of the system. Summary of the invention
[0005] In order to solve the above technical problems or at least partially solve the above technical problems, the present disclosure provides a database transaction group submission method, apparatus, device and storage medium, so that the slave thread will not be in a blocked state due to waiting for the main thread to complete the entire process or part of the stage of the transaction group submission process, thereby improving the utilization rate of the single thread and thus improving the concurrency capability of the system.
[0006] In a first aspect, an embodiment of the present disclosure provides a method for submitting a database transaction group, including:
[0007] Receive data access requests from clients;
[0008] Determining a transaction group to be submitted according to the data access request, wherein the transaction group includes a plurality of transactions to be submitted;
[0009] In a transaction group submission process for the transaction group, according to multiple processing stages included in the transaction group submission process, the main thread is controlled to execute a processing process of some or all of the multiple processing stages;
[0010] In the process of the main thread executing part of or all of the multiple processing stages, controlling the slave thread corresponding to the main thread to execute other transactions except the transaction group;
[0011] When all transactions in the transaction group are committed, commit completion information is generated.
[0012] In a second aspect, an embodiment of the present disclosure provides a database transaction group submission device, including:
[0013] A receiving module, used for receiving a data access request from a client;
[0014] A determination module, configured to determine a transaction group to be submitted according to the data access request, wherein the transaction group includes a plurality of transactions to be submitted;
[0015] a control module, configured to, during a transaction group submission process for the transaction group, control a main thread to execute a processing process of some or all of the multiple processing stages according to the multiple processing stages included in the transaction group submission process; and, during a processing process of the main thread executing some or all of the multiple processing stages, control a slave thread corresponding to the main thread to execute other transactions except the transaction group;
[0016] The generation module is used to generate submission completion information when all transactions in the transaction group are submitted.
[0017] In a third aspect, an embodiment of the present disclosure provides a database transaction group submission device, including:
[0018] Memory;
[0019] Processor; and
[0020] Computer programs;
[0021] The computer program is stored in the memory and is configured to be executed by the processor to implement the method as described in the first aspect.
[0022] In a fourth aspect, an embodiment of the present disclosure provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program is executed by a processor to implement the method described in the first aspect.
[0023] The database transaction group submission method, apparatus, device and storage medium provided by the embodiments of the present disclosure execute part of the processing stages or all of the processing stages in the multiple processing stages included in the transaction group submission process through the main thread, and control the slave thread to execute other transactions except the transaction group during the main thread's execution of the part of the processing stages or all of the processing stages, which is equivalent to separating the slave thread from the original transaction executed by it. Furthermore, when all transactions in the transaction group are submitted, submission completion information is generated, so that the slave thread will not be in a blocked state due to waiting for the main thread to complete the entire process or part of the stages of the transaction group submission process, thereby improving the utilization rate of the single thread and thus improving the concurrency capability of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the present disclosure.
[0025] 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, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0026] Figure 1 A schematic diagram of a transaction group submission process in the prior art;
[0027] Figure 2 is a schematic diagram of a first queue in the prior art;
[0028] Figure 3 A flowchart of a database transaction group submission method provided in an embodiment of the present disclosure;
[0029] Figure 4 A schematic diagram of an application scenario provided by an embodiment of the present disclosure;
[0030] Figure 5 A schematic diagram of a transaction group submission process provided by an embodiment of the present disclosure;
[0031] Figure 6 A schematic diagram of an application scenario provided by an embodiment of the present disclosure;
[0032] Figure 7 A flowchart of a database transaction group submission method provided in an embodiment of the present disclosure;
[0033] Figure 8 A flowchart of a database transaction group submission method provided in an embodiment of the present disclosure;
[0034] Fig. 9 A flowchart of a database transaction group submission method provided in an embodiment of the present disclosure;
[0035] Fig.10 A schematic diagram of the structure of a database transaction group submission device provided in an embodiment of the present disclosure;
[0036] Fig.11 A schematic diagram of the structure of a database transaction group submission device provided in an embodiment of the present disclosure. DETAILED DESCRIPTION
[0037] In order to more clearly understand the above-mentioned objectives, features and advantages of the present disclosure, the scheme of the present disclosure will be further described below. It should be noted that the embodiments of the present disclosure and the features in the embodiments can be combined with each other without conflict.
[0038] In the following description, many specific details are set forth to facilitate a full understanding of the present disclosure, but the present disclosure may also be implemented in other ways different from those described herein; it is obvious that the embodiments in the specification are only part of the embodiments of the present disclosure, rather than all of the embodiments.
[0039] Generally, a client can access data in a database by sending an access request to the database. When the database receives the access request, it can start a transaction and execute the transaction through a thread in the database. When the thread executes the transaction, it needs to commit the transaction. Transactions executed by multiple threads can form a transaction group, which can be committed by a master thread among the multiple threads. The threads other than the master thread are slave threads. The existing transaction group commit process includes the following: Figure 1 The three stages shown in FIG. 1 are the first stage, the second stage and the third stage. The first stage can be recorded as the Flush Stage, the second stage can be recorded as the Sync Stage, and the third stage can be recorded as the Commit Stage. Figure 1 As shown, each stage corresponds to a queue. For example, the first stage corresponds to the first queue, the second stage corresponds to the second queue, and the third stage corresponds to the third queue. The first queue is recorded as Flush Queue, the second queue is recorded as Sync Queue, and the third queue is recorded as Commit Queue.
[0040] In the embodiments of the present disclosure, a transaction specifically refers to a computer transaction or a database transaction, and a transaction is composed of a limited sequence of database operations. A database operation specifically may be an operation on a data row in a database, and the operation specifically may include: reading, writing, scanning, deleting, etc. Specifically, a thread can execute a transaction at the same time, and multiple threads can execute their respective transactions in parallel.
[0041] Specifically, the first queue may include multiple transactions, and the multiple transactions may be transactions executed by multiple threads respectively. For example, multiple threads may put the transactions executed by each thread into the first queue in order. Multiple transactions may be divided into one or more transaction groups. Among them, the first thread in each transaction group that puts a transaction into the first queue may be used as the main thread.
[0042] like Figure 2As shown, thread A, thread B, thread C and thread D sequentially put their respective executed transactions into the first queue, wherein thread A is used to execute transaction 1, thread B is used to execute transaction 2, thread C is used to execute transaction 3, and thread D is used to execute transaction 4. Taking thread A executing transaction 1 as an example, before thread A puts transaction 1 into the first queue, thread A can complete the following steps: 1) generating a physical log related to transaction 1; 2) operating on the data in the database according to transaction 1; 3) storing the operated data into the disk. As a result, after thread A puts transaction 1 into the first queue, transaction 1 is a transaction to be submitted. Similarly, after thread B, thread C and thread D sequentially put their respective executed transactions into the first queue, transaction 2, transaction 3, and transaction 4 also become transactions to be submitted. It can be understood that this embodiment does not limit the number of transactions in the first queue and the relevant information of the transactions.
[0043] Further, the multiple transactions in the first queue may be divided into one or more transaction groups. For example, transaction 1, transaction 2, transaction 3, and transaction 4 may constitute a transaction group. Alternatively, transaction 1 and transaction 2 may constitute transaction group 1, and transaction 3 and transaction 4 may constitute transaction group 2. It is understood that the transaction group division here is only a schematic illustration, and other division methods or other transaction group formation methods may also be used, which are not specifically limited here.
[0044] If transaction 1, transaction 2, transaction 3, and transaction 4 form a transaction group, since thread A corresponding to transaction 1 is the first thread to put a transaction into the first queue, thread A can be used as the main thread in the first stage. Thread A can group commit the transaction group. Specifically, in the first stage, thread A can execute the processing of the first stage, thereby processing the transactions in the transaction group, and putting the processed transactions into the second queue. In the second stage, thread A can execute the processing of the second stage, thereby processing the transactions in the second queue, and putting the processed transactions into the third queue. In the third stage, thread A can execute the processing of the third stage, thereby processing the transactions in the third queue. Thus, transaction group commit is realized. Among them, when thread A executes the processing of the first stage, the processing of the second stage, and the processing of the third stage, slave threads such as thread B, thread C, and thread D are always in a waiting state, that is, a blocked state. When thread A completes the processing of the third stage, the computer system can wake up thread B, thread C, and thread D. Further, thread A, thread B, thread C, and thread D respectively feedback responses to the client.
[0045] It is understandable that the transactions in the first queue may be increased in real time, so that the transactions in the second queue and the third queue may also change or increase in real time.
[0046] If transactions 1 and 2 form transaction group 1, and transactions 3 and 4 form transaction group 2, since in transaction group 1, thread A corresponding to transaction 1 is the first thread to put transactions into the first queue; in transaction group 2, thread C corresponding to transaction 3 is the first thread to put transactions into the first queue, therefore, in the first stage, thread A and thread C can be used as master threads, and thread B and thread D can be used as slave threads. In the first stage, thread A corresponds to transaction group 1, and thread B corresponds to transaction group 2. In the first stage, thread A and thread C respectively execute the processing of the first stage, and thread B and thread D are in a waiting state. Among them, when thread A executes the processing of the first stage, it can process the transactions in transaction group 1 and put the processed transactions into the second queue. Similarly, when thread C executes the processing of the first stage, it can process the transactions in transaction group 2 and put the processed transactions into the second queue. In the second stage, a main thread can be determined from thread A and thread C. For example, the main thread in the second stage can be the thread that first completes the processing of the first stage between thread A and thread C. For example, if thread A first completes the processing of the first stage, thread A can be used as the main thread in the second stage, and thread C can be used as the slave thread in the second stage and be in a waiting state in the second stage. In the second stage, thread A corresponds to transaction group 1 and transaction group 2. In the second stage, thread A can execute the processing of the second stage, thereby processing the transactions in the second queue, and putting the processed transactions into the third queue. Since new transactions can be added to the first queue, the second queue, and the third queue in real time, before the third stage begins, there may be multiple main threads in the second stage that execute the processing of the second stage. Similarly, the main thread that first completes the processing of the second stage is used as the main thread of the third stage. The main thread of the third stage performs the processing of the third stage on the transactions in the third queue.
[0047] The following are the steps involved in the first stage, second stage, and third stage processing respectively:
[0048] The first stage of processing includes the following steps:
[0049] 1) The leader thread holds the mutex Lock_logmutex for the first queue, and the follower threads are in a waiting state.
[0050] 2) The main thread generates a binary log (binlog) corresponding to each transaction in its corresponding transaction group.
[0051] 3) The main thread puts the binary log into a cache, for example, an input / output (I / O) cache.
[0052] 4) The main thread stores the physical log (redo log) corresponding to each transaction in its corresponding transaction group from the cache area to the disk.
[0053] The second stage of processing includes the following steps:
[0054] 1) The main thread releases the mutex Lock_logmutex of the first queue, holds the mutex Lock_syncmutex of the second queue, and the follower threads are in a waiting state.
[0055] 2) The main thread stores the binary log corresponding to each transaction in its corresponding transaction group from the cache area to the disk.
[0056] The third stage of processing includes the following steps:
[0057] 1) The main thread releases the mutex Lock_syncmutex of the second queue, holds the mutex Lock_commitmutex of the third queue, and the follower threads are in a waiting state.
[0058] 2) The main thread traverses the transactions in the third queue and commits the transactions in the third queue one by one at the database engine layer (innodb commit).
[0059] 3) The main thread releases the mutex Lock_commitmutex of the third queue.
[0060] 4) The main thread wakes up the waiting follower threads. The main thread and follower threads feed back responses to the client.
[0061] Among them, the main threads in the first stage, the second stage, and the third stage may be the same thread or different threads, as described above, and will not be repeated here.
[0062] Since the slave thread is in a waiting state, i.e., a blocked state, during the process of the main thread submitting the transaction group, the system's concurrency capability is low. To address this problem, the present disclosure provides a database transaction group submission method, which is described below in conjunction with a specific embodiment.
[0063] Figure 3 This is a flow chart of a database transaction group submission method provided in an embodiment of the present disclosure. The specific steps of the method are as follows:
[0064] S301: Receive a data access request from a client.
[0065] like Figure 4 As shown, 41 represents a client, 42 represents a server, and 43 represents a database. The database 43 may be integrated in the server 42. Alternatively, the database 43 and the server 42 are independently provided, and the database 43 and the server 42 may be connected in communication. The communication method between the client 41, the server 42, and the database 43 is not limited here. For example, it may be a wireless communication method or a wired communication method. Specifically, the client 41 may send a data access request to the server 42. When the server 42 receives the data access request, it accesses the data in the database 43. The server 42 may start a transaction each time it receives a data access request, and the server 42 may start multiple threads, and one thread may execute one transaction at the same time.
[0066] In a feasible implementation, the server 42 receives data access requests respectively sent by multiple clients. Each time the server 42 receives a data access request, it may start a transaction and a thread, and the thread may execute the transaction.
[0067] In another feasible implementation, the server 42 receives multiple data access requests sent by the client 41. Each time the server 42 receives a data access request, it may start a transaction and a thread, and the thread may execute the transaction.
[0068] S302: Determine a transaction group to be committed according to the data access request, where the transaction group includes multiple transactions to be committed.
[0069] For example, the server 42 receives four data access requests, and the server 42 starts four transactions, such as transaction 1, transaction 2, transaction 3, and transaction 4. In addition, the server 42 starts four threads according to the four data access requests, such as thread A, thread B, thread C, and thread D. Among them, thread A is used to execute transaction 1, thread B is used to execute transaction 2, thread C is used to execute transaction 3, and thread D is used to execute transaction 4.
[0070] After thread A generates the physical log related to transaction 1, operates the data in the database according to transaction 1, and stores the operated data in the disk, transaction 1 can be placed in the first queue. Similarly, after thread B, thread C, and thread D respectively perform operations similar to thread A, thread B, thread C, and thread D can place their respective executed transactions in the first queue in turn.
[0071] Furthermore, transactions 1, 2, 3, and 4 in the first queue constitute a transaction group to be submitted. If thread A is the first thread to put a transaction into the first queue, thread A can be used as the main thread. Threads B, C, and D can be used as slave threads. It can be seen that the transaction group includes transactions executed by the main thread and transactions executed by the slave threads.
[0072] Alternatively, thread A is the main thread, thread B is the slave thread, and transaction 1 executed by thread A and transaction 2 executed by thread B constitute transaction group 1. Thread C is the main thread, thread D is the slave thread, and transaction 3 executed by thread C and transaction 4 executed by thread D constitute transaction group 2.
[0073] S303: During the transaction group submission process for the transaction group, according to multiple processing stages included in the transaction group submission process, control the main thread to execute a portion of or all of the multiple processing stages.
[0074] The transaction group submission process described in this embodiment may include multiple processing stages, and the multiple processing stages may be as follows: Figure 1 The three stages shown, or can be as follows Figure 5 The 4 stages are shown.
[0075] exist Figure 1 The 3 stages shown or Figure 5 In the four stages shown, the server 42 can control the main thread of the first stage to execute the processing of the first stage, and control the main thread of the second stage to execute the processing of the second stage.
[0076] For example, if the main thread of the first phase is thread A, the server 42 controls thread A to perform the first phase processing on the transactions in the first queue. If the main thread of the second phase is still thread A, the server 42 controls thread A to perform the second phase processing on the transactions in the second queue.
[0077] For another example, if the main thread of the first stage includes thread A and thread C, the server 42 controls thread A to execute the first stage processing process for the transactions in transaction group 1, and controls thread C to execute the first stage processing process for the transactions in transaction group 2. If the main thread of the second stage is thread A, and thread C becomes a slave thread in the second stage, thread A executes the second stage processing process for the transactions in the second queue.
[0078] S304: During the process in which the main thread executes part of or all of the multiple processing stages, control the slave thread corresponding to the main thread to execute other transactions except the transaction group.
[0079] For example, in the first phase, if thread A is the main thread, and thread B, thread C, and thread D are slave threads, thread A performs the first phase processing on the transactions in the first queue, and adds the processed transactions to the second queue. While thread A performs the first phase processing on the transactions in the first queue, server 42 can control thread B, thread C, and thread D to execute other transactions except the transaction group, instead of waiting. Similarly, in the second phase, the main thread performs the second phase processing on the transactions in the second queue, and adds the processed transactions to the third queue, and while the main thread performs the second phase processing on the transactions in the second queue, the slave threads in the second phase can execute other transactions except the transaction group, instead of waiting.
[0080] For another example, if the main thread of the first phase includes thread A and thread C, then during the process of thread A executing the first phase processing on the transactions in transaction group 1, server 42 can control thread B to execute other transactions except transaction group 1 instead of waiting. During the process of thread C executing the first phase processing on the transactions in transaction group 2, server 42 can control thread D to execute other transactions except transaction group 2 instead of waiting. If in the second phase, thread A is the main thread and thread C is the slave thread, then during the process of thread A executing the second phase processing on the transactions in the second queue, server 42 can control thread C to execute other transactions except the second queue instead of waiting.
[0081] That is, the slave thread of the first phase can return to process other businesses after adding the transaction to the first queue. The slave thread of the second phase can return to process other businesses after adding the transaction to the second queue.
[0082] S305: When all transactions in the transaction group are committed, commit completion information is generated.
[0083] by Figure 1Taking the three stages shown as an example, after the main thread of the second stage completes the processing of the second stage for the transactions in the second queue, the server 42 can control the main thread of the second stage to execute other transactions except the transaction group instead of entering the third stage. In the third stage, the server 42 can call the first background thread, and the first background thread executes the processing of the third stage. Among them, the first background thread can be a thread that is started when the server 42 is started. The main thread and the slave thread as mentioned above are threads started according to the data access request of the client. After the first background thread commits the transactions of the database engine layer one by one for all the transactions in the third queue, it can be determined that all the transactions in the transaction group have been committed. At this time, the server 42 can generate a commit completion message and control the first background thread to feedback a response message to the client. The response message is the response message corresponding to the above-mentioned data access request, and the response message can be, for example, a commit completion message or a successful data access request. Specifically, if in S301 the server 42 receives data access requests respectively sent by multiple clients, or the server 42 receives multiple data access requests sent by the client 41, then in S305 the server 42 may call multiple first background threads, wherein each first background thread may perform a transaction commit at the database engine layer for a transaction in the third queue. In addition, since each transaction may include client information, after each first background thread performs a transaction commit at the database engine layer for a transaction in the third queue, the first background thread may feed back response information to the client based on the client information in the transaction, such as submission completion information or successful processing of the data access request. In other embodiments, such as Figure 1 The transaction commit step of the database engine layer in the third stage shown can be executed by the main thread that first completes the processing of the second stage. When the main thread completes the transaction commit of the database engine layer, the server 42 can call the first background thread and control the first background thread to feedback response information to the client.
[0084] by Figure 5 Taking the four stages shown as an example, after the main thread of the second stage completes the processing of the second stage for the transactions in the second queue, the server 42 can control the main thread of the second stage to execute other transactions except the second queue instead of entering the third stage. In the third stage, the server 42 can call the second background thread, and the second background thread executes the processing of the third stage. The second background thread is the thread that is started when the server 42 is started. Figure 5 The third of the four stages shown is Figure 1 The third of the three stages shown is different.
[0085] exist Figure 5In the third stage shown, the second background thread can traverse each transaction in the third queue and detect whether the binary log corresponding to each transaction is successfully backed up in the backup database. Figure 6 As shown, database 43 is the main database, and database 44 is the backup database. Server 42 can not only store the binary log corresponding to each transaction in the main database, but also store the binary log corresponding to each transaction in the backup database. Since new transactions can continuously enter the third queue, the second background thread can continuously traverse the transactions in the third queue. When there are no new transactions in the third queue for the second background thread to traverse, the second background thread can wait. When the second background thread determines that the binary log corresponding to the transaction it has traversed has been successfully backed up in the backup database, the second background thread can add the transaction to the fourth queue. Figure 5 In the fourth stage, the server 42 may call the first background thread, and the first background thread commits a transaction in the fourth queue at the database engine layer, and feeds back response information to the client after the transaction at the database engine layer is committed.
[0086] The disclosed embodiment executes part of the processing stages or all of the processing stages in the multiple processing stages included in the transaction group submission process through the main thread, and controls the slave thread to execute other transactions except the transaction group during the main thread's execution of the part of the processing stages or all of the processing stages, which is equivalent to separating the slave thread and the original transaction it executes. Furthermore, when all transactions in the transaction group are submitted, submission completion information is generated, so that the slave thread will not be in a blocked state due to waiting for the main thread to complete the entire process or part of the stage of the transaction group submission process, thereby improving the utilization rate of the single thread and thus improving the concurrency capability of the system.
[0087] Based on the above embodiment, the multiple processing stages include: a first stage, a second stage, a third stage and a fourth stage; wherein the first stage is used to implement the group submission of the physical log corresponding to the transaction group; the second stage is used to implement the group submission of the binary log corresponding to the transaction group; the third stage is used to detect whether the binary log corresponding to the transaction group is successfully backed up in the backup database; the fourth stage is used to perform transaction submission at the database engine layer for the transaction group.
[0088] For example, the transaction group submission process in this embodiment may include the following steps: Figure 5The four stages shown are the first stage, the second stage, the third stage and the fourth stage, wherein the first stage can be recorded as Flush Stage, the second stage can be recorded as Sync Stage, the third stage can be recorded as After SyncStage, and the fourth stage can be recorded as Commit Stage. The first stage, the second stage, the third stage and the fourth stage correspond to a queue respectively. For example, the first stage corresponds to the first queue, the second stage corresponds to the second queue, the third stage corresponds to the third queue, and the fourth stage corresponds to the fourth queue. After being processed in the first stage, the transactions in the first queue can enter the second queue, after being processed in the second stage, the transactions in the second queue can enter the third queue, and after being processed in the third stage, the transactions in the third queue can enter the fourth queue.
[0089] It is understandable that in the embodiments of the present disclosure, transactions in the database system can be performed as follows: Figure 1 The submission can be performed in three stages as shown. Figure 5 Commit is performed in four stages as shown.
[0090] Specifically, before a transaction enters the first queue, the transaction status can be set according to the transaction type, and the submission process of the transaction can be determined according to the transaction status. Figure 1 The three-phase submission process shown in Figure 5 The four-phase commit process shown in FIG. 1 is a four-phase commit process. For example, when the transaction type of a transaction is "hidden transaction", "stored procedure" or "multi-statement execution", the transaction status of the transaction can be marked as GROUP_COMMIT_OLD. If the transaction type of a transaction is a type other than "hidden transaction", "stored procedure" and "multi-statement execution", the transaction status of the transaction can be marked as GROUP_COMMIT_NEW. Further, for a transaction with a transaction status of GROUP_COMMIT_OLD, the following steps are followed: Figure 1 For transactions with a transaction status of GROUP_COMMIT_NEW, follow the steps below: Figure 5 Commit is performed in four stages as shown.
[0091] The following is a transaction with a transaction status of GROUP_COMMIT_NEW. Figure 5 The four-stage submission shown is used as an example for schematic explanation.
[0092] Optionally, controlling the main thread to execute a processing process of some or all of the multiple processing stages includes: controlling the main thread to execute the processing process of the first stage and the processing process of the second stage among the multiple processing stages.
[0093] Specifically, the first queue includes multiple transactions. The first thread that enters the first queue and puts the transaction into the first queue is recorded as the master thread of the first stage, and the thread that is not the first to enter the first queue can be recorded as the slave thread of the first stage. Figure 4 The server 42 shown can control the main thread to execute the first phase of the processing process for the transactions in the first queue, and add the processed transactions to the second queue. After the main thread completes the first phase of the processing process, the main thread can enter the second phase. Among them, the steps included in the first phase of the processing process are as described above and will not be repeated here. For example, the main thread of the first phase can store the physical log (redo log) corresponding to each transaction in the first queue from the cache area to the disk, thereby realizing the group submission of the physical log corresponding to the transaction group. In addition, when the main thread of the first phase executes the first phase of the processing process, the slave thread of the first phase returns, that is, the slave thread can execute other transactions or other business logic.
[0094] Since the main thread of the first stage may not be limited to one, that is, multiple main threads of the first stage may enter the second stage. Among them, the first thread among the multiple main threads of the first stage that enters the second queue and puts the transaction or transaction group into the second queue can be used as the main thread of the second stage, and the main thread that is not the first to enter the second queue among the multiple main threads of the first stage can be used as the slave thread of the second stage. Further, the main thread of the second stage can execute the processing of the second stage on the transactions in the second queue, and put the processed transactions into the third queue. Among them, the steps included in the processing of the second stage are as described above and will not be repeated here. For example, the main thread of the second stage can store the binary log corresponding to each transaction in the second queue from the cache area to the disk, thereby realizing the group submission of the binary log corresponding to the transaction group.
[0095] Optionally, after the main thread executes the processing of the second stage, the main thread is controlled to execute other transactions except the transaction group.
[0096] like Figure 5 As shown, after the main thread of the second stage completes the processing of the second stage, the server 42 can control the main thread to return, that is, after the main thread completes the processing of the second stage, the main thread can execute other transactions or other business logics except the transaction group. This further improves the concurrency capability of the system. In addition, when the main thread of the second stage executes the processing of the second stage, the slave thread of the second stage returns, that is, the slave thread can execute other transactions or other business logics except the transaction group.
[0097] Specifically, if the transaction executed by the main thread in the second phase includes a transaction of the type "hidden transaction", "stored procedure" or "multi-statement execution", and the transaction is in the second queue, the main thread enters the waiting state after completing the processing of the second phase. If the transaction executed by the main thread in the second phase does not include a transaction of the type "hidden transaction", "stored procedure" or "multi-statement execution", the main thread returns directly after completing the processing of the second phase.
[0098] Optionally, the method further includes: in the third stage, controlling the second background thread to detect whether the binary log corresponding to each transaction in the transaction group is successfully backed up in the backup database.
[0099] For example, in Figure 5 In the third stage shown, the database system, for example Figure 4 The server 42 shown may call the second background thread to detect whether the binary log corresponding to each transaction in the third queue is successfully backed up in the backup database. Figure 6 As shown, database 43 is the main database, and database 44 is the backup database. Server 42 can not only store the binary log corresponding to each transaction in the main database, but also store the binary log corresponding to each transaction in the backup database. Specifically, server 42 can send the binary log corresponding to each transaction to the backup database.
[0100] Optionally, the second background thread is used to detect whether the binary log corresponding to each transaction in the transaction group is successfully backed up in the backup database based on the storage location of the binary log corresponding to the transaction and the confirmation information fed back by the backup database.
[0101] For example, the process in which the second background thread detects whether the binary log corresponding to each transaction in the third queue is successfully backed up in the backup database can be recorded as the third stage of processing. The third stage of processing includes the following: Figure 7 The following steps are shown:
[0102] S701: Call the second background thread.
[0103] For example, the server 42 calls a second background thread.
[0104] S702: Traverse each transaction in the third queue.
[0105] The second background thread iterates through each transaction in the third queue.
[0106] S703: Determine whether the identifier of the binary log corresponding to the transaction is less than or equal to the latest ACK fed back by the backup database. If yes, execute S706; otherwise, execute S704.
[0107] The second background thread determines whether the identifier of the binary log corresponding to the transaction is less than or equal to the ACK of the latest feedback from the backup database. The identifier of the binary log corresponding to the transaction may be the storage position of the binary log corresponding to the transaction. For example, the binary log corresponding to each transaction in the third queue may be stored continuously. For example, the third queue includes transaction 1, transaction 2, and transaction 3, the storage position of the binary log corresponding to transaction 1 is 0-200, the storage position of the binary log corresponding to transaction 2 is 201-300, and the storage position of the binary log corresponding to transaction 3 is 301-400.
[0108] The latest acknowledgement information (Acknowledge character, ACK) fed back by the backup database can be the storage location of the latest binary log received by the backup database. For example, if the backup database has recently received the binary log corresponding to transaction 1, the ACK fed back by the backup database to the server 42 is 200. If the backup database has recently received the binary log corresponding to transaction 2, the ACK fed back by the backup database to the server 42 is 300. If the backup database has recently received the binary log corresponding to transaction 3, the ACK fed back by the backup database to the server 42 is 400.
[0109] If the storage position of the binary log corresponding to the transaction currently traversed by the second background thread is less than or equal to the ACK fed back by the backup database to the server 42, it means that the backup database has successfully backed up the binary log corresponding to the transaction. If the storage position of the binary log corresponding to the transaction is greater than the ACK, it means that the backup database has not successfully backed up or has not successfully received the binary log corresponding to the transaction.
[0110] S704, wait.
[0111] S705: Re-traverse the transaction.
[0112] For example, the third queue includes transaction 1, transaction 2, and transaction 3. In S703, the second background thread traverses transaction 2, but because the storage location of the binary log corresponding to transaction 2 is greater than ACK, the second background thread enters a waiting state. After waiting for a period of time, the second background thread can re-traverse transaction 2 and determine whether the backup database successfully backs up the binary log corresponding to transaction 2.
[0113] S706: Put the transaction into the fourth queue.
[0114] For example, the third queue includes transaction 1, transaction 2, and transaction 3. In S703, the second background thread traverses transaction 2. After the judgment step of S703, the second background thread determines that the backup database has successfully backed up the binary log corresponding to transaction 2, and the second background thread can put transaction 2 into the fourth queue.
[0115] S707. Traverse the next transaction.
[0116] After the second background thread puts the transaction 2 into the fourth queue, it can continue to traverse the next transaction of transaction 2, for example, transaction 3.
[0117] Since new transactions can be continuously received in the third queue, the second background thread can cyclically execute the processing of the third stage. When there are no transactions to be traversed in the third queue, the second background thread can wait.
[0118] It is understandable that if there is no backup database, or the binary log corresponding to each transaction does not need to be stored in the backup database, the transactions in the third queue can be directly added to the fourth queue. For example, it can be determined whether semi-synchronous replication is enabled. If semi-synchronous replication is enabled, it means that there is a backup database, and the backup database and the main database need to be consistent. If semi-synchronous replication is not enabled, it means that there is no backup database, or the binary log corresponding to each transaction does not need to be stored in the backup database.
[0119] In addition, if the semi-synchronous replication is abnormal, it means that the backup database has a flash disconnection or crash due to a network problem, resulting in the backup database having no ACK response. In this case, if the time for the second background thread to wait for the ACK response has timed out, the second background thread can also add the currently traversed transaction to the fourth queue, that is, the second background thread does not need to continue waiting.
[0120] This embodiment uses the second background thread to determine whether the identifier of the binary log corresponding to the transaction in the transaction group is less than or equal to the latest ACK fed back by the backup database, thereby detecting whether the binary log corresponding to the transaction is successfully backed up in the backup database. The second background thread waits for the ACK response from the backup database, so that the main thread in the second stage can return and execute other transactions, preventing multi-threaded waiting. In addition, in the scenario where the main database and the backup database are strongly synchronized, if the backup database and the main database communicate through the network, then the time required to wait for the backup database to feed back the ACK response is relatively long. Therefore, the second background thread waits for the ACK response from the backup database, so that the main thread in the second stage can return and execute other transactions, which can avoid the main thread from increasing the transaction submission time due to long-term waiting for the ACK response, and further improve the concurrency capability of the system.
[0121] Optionally, when all transactions in the transaction group are committed, commit completion information is generated, including: in the fourth stage, controlling one or more first background threads to perform transaction commit at the database engine layer for all transactions in the transaction group; when the transaction commit at the database engine layer is completed, generating the commit completion information, and controlling the one or more first background threads to feed back response information to the client.
[0122] like Figure 5 The fourth stage of processing shown includes the following: Figure 8 The following steps are shown:
[0123] S801. Call one or more first background threads.
[0124] For example, the server 42 invokes one or more first background threads.
[0125] S802: traverse each transaction in the fourth queue by using the one or more first background threads.
[0126] For example, when the server 42 calls a first background thread, each transaction in the fourth queue may be traversed by the first background thread.
[0127] For example, when the server 42 calls multiple first background threads, one transaction in the fourth queue may be traversed by each of the multiple first background threads.
[0128] S803: The first background thread commits the traversed transactions at the database engine layer.
[0129] For example, when the server 42 calls a first background thread, the first background thread can be used to submit transactions at the database engine layer one by one for all transactions in the fourth queue.
[0130] For example, when the server 42 calls a plurality of first background threads, each of the plurality of first background threads may submit a transaction in the fourth queue at the database engine layer.
[0131] S804, determine whether the traversal is completed, if so, execute S806 and S807, otherwise execute S805.
[0132] S805: traverse the next transaction. S806: generate submission completion information.
[0133] For example, when the one or more first background threads have completed traversing all transactions in the fourth queue, and the one or more first background threads have completed transaction submission at the database engine layer for all transactions in the fourth queue, it can be determined that all transactions in the transaction group have been submitted. At this time, the server 42 can generate submission completion information. S807: Feedback response information to the client.
[0134] For example, each first background thread may feed back response information to the client according to the client information in the transaction it traverses, such as submission completion information or successful processing of the data access request.
[0135] Optional, in Figure 5 In the third stage shown, the database system may call a background thread, that is, the second background thread may be one. The second background thread may be recorded as an E thread.
[0136] In such Figure 5 In the fourth stage shown, the database system may call multiple background threads, that is, there may be multiple first background threads. The multiple first background threads may be recorded as an F thread group.
[0137] like Fig. 9 As shown, the third queue can adopt a lock-free queue. When the E thread polls a committable transaction in the third queue, the transaction is taken out of the third queue and added to the fourth queue. Among them, the E thread can also add a transaction to the fourth queue in a lock-free manner. The fourth queue can be provided with a lock, for example, a mutex lock. For example, each thread in the F thread group can first lock the fourth queue, and further, after reading a transaction from the fourth queue, release the lock on the fourth queue, so that the next thread in the F thread group can read the next transaction in the fourth queue. Thus, multiple threads in the F thread group can serially read or traverse the transactions in the fourth queue. After multiple threads in the F thread group read transactions from the fourth queue in sequence, each thread can concurrently submit transactions at the database engine layer for the transactions it reads, and concurrently feedback response information to the client according to the client information included in the transactions it reads.
[0138] If a thread in the F thread group enters the fourth queue and there is no transaction available for processing in the fourth queue, the thread enters a waiting state.
[0139] The disclosed embodiment executes the processing of some or all of the multiple processing stages included in the transaction group submission process through the main thread, and in the processing of the main thread executing the said part or all of the processing stages, controls the slave thread to execute other transactions except the transaction group, which is equivalent to that the slave thread and the original transaction it executes can be separated. Furthermore, when all transactions in the transaction group are submitted, submission completion information is generated, so that the slave thread will not be in a blocked state due to waiting for the main thread to complete the entire process or part of the stage of the transaction group submission process, thereby improving the utilization rate of the single thread and thus improving the concurrency capability of the system. In addition, when all transactions in the transaction group are submitted, the response information is fed back to the client by controlling the first background thread, that is, the first background thread can replace the slave thread to continue to process the business logic that the slave thread has not processed, further improving the utilization rate of the single thread.
[0140] Fig.10 The structure diagram of the database transaction group submission device provided by the embodiment of the present disclosure is as follows. The database transaction group submission device provided by the embodiment of the present disclosure can execute the processing flow provided by the database transaction group submission method embodiment, such as Fig.10 As shown, the database transaction group submission device 100 includes:
[0141] Receiving module 101, used to receive a data access request from a client;
[0142] A determination module 102, configured to determine a transaction group to be submitted according to the data access request, wherein the transaction group includes a plurality of transactions to be submitted;
[0143] The control module 103 is used to control the main thread to execute a part of or all of the multiple processing stages in the transaction group submission process according to the multiple processing stages included in the transaction group submission process during the transaction group submission process; and control the slave thread corresponding to the main thread to execute other transactions except the transaction group during the process in which the main thread executes a part of or all of the multiple processing stages.
[0144] The generating module 104 is used to generate commit completion information when all transactions in the transaction group are committed.
[0145] Optionally, the multiple processing stages include: a first stage, a second stage, a third stage and a fourth stage;
[0146] Wherein, the first stage is used to implement the group submission of the physical log corresponding to the transaction group;
[0147] The second stage is used to implement group submission of the binary log corresponding to the transaction group;
[0148] The third stage is used to detect whether the binary log corresponding to the transaction group is successfully backed up in the backup database;
[0149] The fourth stage is used to commit transactions of the transaction group at the database engine layer.
[0150] Optionally, when the control module 103 controls the main thread to execute a processing process of some or all of the processing stages in the multiple processing stages, it is specifically used to:
[0151] The main thread is controlled to execute the processing process of the first stage and the processing process of the second stage in the multiple processing stages.
[0152] Optionally, the control module 103 is further used for:
[0153] After the main thread executes the processing of the second stage, the main thread is controlled to execute other transactions except the transaction group.
[0154] Optionally, the control module 103 is specifically used to: in the fourth stage, control one or more first background threads to perform transaction submission at the database engine layer for all transactions in the transaction group;
[0155] The generating module 104 is specifically used to: generate the commit completion information when the transaction commit of the database engine layer is completed;
[0156] The control module 103 is further used to control the one or more first background threads to feed back response information to the client.
[0157] Optionally, the control module 103 is further used for:
[0158] In the third stage, the second background thread is controlled to detect whether the binary log corresponding to each transaction in the transaction group is successfully backed up in the backup database.
[0159] Optionally, the second background thread is used to detect whether the binary log corresponding to each transaction in the transaction group is successfully backed up in the backup database based on the storage location of the binary log corresponding to the transaction and the confirmation information fed back by the backup database.
[0160] Fig.10 The database transaction group submission device of the illustrated embodiment can be used to execute the technical solution of the above-mentioned method embodiment. Its implementation principle and technical effect are similar and will not be repeated here.
[0161] Fig.11The structure diagram of the database transaction group submission device provided by the embodiment of the present disclosure is as follows. The database transaction group submission device provided by the embodiment of the present disclosure can execute the processing flow provided by the database transaction group submission method embodiment, such as Fig.11 As shown, the database transaction group submission device 110 includes: a memory 111, a processor 112, a computer program and a communication interface 113; wherein the computer program is stored in the memory 111 and is configured so that the processor 112 executes the database transaction group submission method as described above.
[0162] In addition, an embodiment of the present disclosure further provides a computer-readable storage medium on which a computer program is stored. The computer program is executed by a processor to implement the database transaction group submission method described in the above embodiment.
[0163] It should be noted that, in this article, relational terms such as "first" and "second" are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0164] The above description is only a specific embodiment of the present disclosure, so that those skilled in the art can understand or implement the present disclosure. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present disclosure. Therefore, the present disclosure will not be limited to the embodiments described herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A method for submitting a database transaction group, characterized in that: The method comprises: Receive data access requests from clients; Determining a transaction group to be submitted according to the data access request, wherein the transaction group includes a plurality of transactions to be submitted; In a transaction group submission process for the transaction group, according to multiple processing stages included in the transaction group submission process, the main thread is controlled to execute a processing process of some or all of the multiple processing stages; In the process of the main thread executing part of or all of the multiple processing stages, controlling the slave thread corresponding to the main thread to execute other transactions except the transaction group; When all transactions in the transaction group are committed, commit completion information is generated; The multiple processing stages include: a first stage, a second stage, a third stage, and a fourth stage; Wherein, the first stage is used to implement the group submission of the physical log corresponding to the transaction group; The second stage is used to implement group submission of the binary log corresponding to the transaction group; The third stage is used to detect whether the binary log corresponding to the transaction group is successfully backed up in the backup database; The fourth stage is used to commit the transaction of the transaction group at the database engine layer; The first stage corresponds to the first queue, and the second stage corresponds to the second queue. The first thread that enters the first queue and puts the transaction into the first queue is recorded as the main thread of the first stage. The first thread among multiple main threads of the first stage that enters the second queue and puts the transaction or transaction group into the second queue can be used as the main thread of the second stage. The second background thread called by the server is used as the main thread of the third stage, and the first background thread called by the server is used as the main thread of the fourth stage.
2. The method according to claim 1, characterized in that The process of controlling the main thread to execute a part of or all of the processing stages in the multiple processing stages includes: The main thread is controlled to execute the processing process of the first stage and the processing process of the second stage in the multiple processing stages.
3. The method according to claim 2, characterized in that The method further comprises: After the main thread executes the processing of the second stage, the main thread is controlled to execute other transactions except the transaction group.
4. The method according to claim 1, characterized in that When all transactions in the transaction group are committed, commit completion information is generated, including: In the fourth stage, controlling one or more first background threads to perform transaction submission at the database engine layer on all transactions in the transaction group; When the transaction of the database engine layer is committed, the commit completion information is generated, and the one or more first background threads are controlled to feed back response information to the client.
5. The method according to claim 1, characterized in that The method further comprises: In the third stage, the second background thread is controlled to detect whether the binary log corresponding to each transaction in the transaction group is successfully backed up in the backup database.
6. The method according to claim 5, characterized in that The second background thread is used to detect whether the binary log corresponding to each transaction in the transaction group is successfully backed up in the backup database according to the storage location of the binary log corresponding to the transaction and the confirmation information fed back by the backup database.
7. A database transaction group submission device, characterized in that: include: A receiving module, used for receiving a data access request from a client; A determination module, configured to determine a transaction group to be submitted according to the data access request, wherein the transaction group includes a plurality of transactions to be submitted; a control module, configured to control the main thread to execute a part of or all of the multiple processing stages in a transaction group submission process according to the multiple processing stages included in the transaction group submission process during the transaction group submission process; In the process of the main thread executing part of or all of the multiple processing stages, controlling the slave thread corresponding to the main thread to execute other transactions except the transaction group; A generation module, used for generating submission completion information when all transactions in the transaction group are submitted; The multiple processing stages include: a first stage, a second stage, a third stage, and a fourth stage; The first stage is used to implement the group submission of the physical log corresponding to the transaction group; the second stage is used to implement the group submission of the binary log corresponding to the transaction group; The third stage is used to detect whether the binary log corresponding to the transaction group is successfully backed up in the backup database; The fourth stage is used to commit the transaction of the transaction group at the database engine layer; The first stage corresponds to the first queue, and the second stage corresponds to the second queue. The first thread that enters the first queue and puts the transaction into the first queue is recorded as the main thread of the first stage. The first thread among multiple main threads of the first stage that enters the second queue and puts the transaction or transaction group into the second queue can be used as the main thread of the second stage. The second background thread called by the server is used as the main thread of the third stage, and the first background thread called by the server is used as the main thread of the fourth stage.
8. The database transaction group submission device according to claim 7, characterized in that: When the control module controls the main thread to execute the processing of some or all of the processing stages in the multiple processing stages, it is specifically used to: The main thread is controlled to execute the processing process of the first stage and the processing process of the second stage in the multiple processing stages.
9. The database transaction group submission device according to claim 8, characterized in that: The control module is also used for: After the main thread executes the processing of the second stage, the main thread is controlled to execute other transactions except the transaction group.
10. The database transaction group submission device according to claim 1, characterized in that: When the generation module generates the commit completion information when all transactions in the transaction group are committed, the control module is specifically used to: in the fourth stage, control one or more first background threads to perform transaction commit at the database engine layer on all transactions in the transaction group; The generating module is specifically used to: generate the commit completion information when the transaction commit of the database engine layer is completed; The control module is further used to control the one or more first background threads to feed back response information to the client.
11. The database transaction group submission device according to claim 1, characterized in that: The control module is also used for: In the third stage, the second background thread is controlled to detect whether the binary log corresponding to each transaction in the transaction group is successfully backed up in the backup database.
12. The database transaction group submission device according to claim 11, characterized in that: The second background thread is used to detect whether the binary log corresponding to each transaction in the transaction group is successfully backed up in the backup database according to the storage location of the binary log corresponding to the transaction and the confirmation information fed back by the backup database.
13. A database transaction group submission device, characterized in that: include: Memory; processor; and computer programs; The computer program is stored in the memory and is configured to be executed by the processor to implement the method according to any one of claims 1 to 6.
14. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.
