A Distributed Database Middleware Solution
By integrating local transactions, two-phase transactions and flexible transactions, and combining XA two-phase transactions and SEATA flexible transactions, a unified distributed transaction interface is provided, which solves the performance and function trade-off problem of transaction management in distributed application scenarios, and realizes cross-database The atomicity and strong consistency of transactions reduce development costs and performance losses.
Patent Information
- Application Number
- CN202011102929.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-10-15
- Publication Date
- 2024-09-13
- Estimated Expiration
- 2040-10-15
AI Technical Summary
In distributed application scenarios, it is difficult for existing technologies to make a free trade-off between performance and functionality. The APIs and functions of strongly consistent transactions and flexible transactions are not exactly the same, resulting in increased design and development costs and the inability to effectively cope with the challenges of the Internet. Long transaction scenarios with high concurrency or complex systems.
Provide a distributed database middleware solution that integrates local transactions, two-phase transactions and flexible transactions, provides a unified distributed transaction interface, combines XA two-phase transactions and SEATA flexible transactions, and uses an embedded transaction manager and transaction coordinator , realizes the management and coordination of global transactions, supports XA and non-XA connection pools, and uses multi-threaded execution and undo snapshots in sharded SQL.
It realizes the selection of the most suitable distributed transaction solution in different scenarios, supports the atomicity and strong consistency of cross-database transactions, automatically restores submitted or rolled back transactions, and reduces development costs and performance losses.
Smart Images

Figure CN112395102B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer databases, and in particular to a distributed database middleware solution. Background Art
[0002] In the popular environment of distributed applications based on microservices, more and more application scenarios require that access to multiple services and their corresponding multiple database resources be included in the same transaction, resulting in the emergence of distributed transactions. In distributed application scenarios, it becomes a constraint on system performance. How to make the database meet the ACID characteristics in distributed scenarios or find corresponding alternatives is the key solution direction for distributed transactions. At present, the main solutions for transactions are local transactions, two-phase commit transactions, and flexible transactions.
[0003] Local transactions: Without enabling any distributed transaction manager, each data node manages its own transactions. They have no coordination or communication capabilities, and are not aware of the success or failure of transactions on other data nodes. Local transactions do not have any performance loss, but are not strong consistency and eventual consistency.
[0004] Two-phase commit transaction: During the transaction execution, all required resources need to be locked. It is more suitable for short transactions with a certain execution time. For long transactions, the exclusive use of data during the entire transaction will cause a significant decline in the concurrent performance of business systems that rely on hot data.
[0005] Flexible transactions: The concept of flexible transactions is to move the mutex operation from the resource level to the business level through business logic, and to increase system throughput by relaxing the requirements for strong consistency.
[0006]
[0007] In the daily work of developers, due to different application scenarios, developers need to be able to reasonably balance various distributed transactions between performance and functionality.
[0008] However, the APIs and functions of strongly consistent transactions and flexible transactions are not exactly the same, and there is no free and transparent switching between them. In the development decision-making stage, you have to choose between strongly consistent transactions and flexible transactions, which greatly increases the design and development costs.
[0009] XA-based strongly consistent transactions are relatively simple to use, but cannot cope well with high concurrency on the Internet or long transaction scenarios in complex systems. Flexible transactions require developers to transform applications, have very high access costs, and require developers to implement resource locking and reverse compensation on their own. Summary of the invention
[0010] Therefore, in view of the above problems, the present invention integrates existing mature transactions, provides a unified distributed transaction interface for local transactions, two-phase transactions and flexible transactions, makes up for the shortcomings of current methods, and provides a distributed transaction solution.
[0011] In order to achieve the above object, the present invention adopts the following technical solutions:
[0012] On one hand, the present invention provides a distributed database middleware solution, which integrates existing mature transactions to provide a unified distributed transaction interface for local transactions, two-phase transactions and flexible transactions. The integrated transactions include:
[0013] Based on XA two-phase transactions and SEATA flexible transactions;
[0014] in,
[0015] The XA two-phase transaction integrates the XA transaction and the two-phase transaction;
[0016] The SEATA flexible transaction integrates the Seata AT transaction in the SEATA framework.
[0017] In a specific embodiment, the method includes integrating an embedded transaction manager to provide services in the form of a jar package.
[0018] In a specific embodiment, the XA-based two-phase transaction submission adopts the application engineering abstracted by the DTP model defined by the X / OPEN organization, and the transaction manager and the resource manager use the XA protocol for bidirectional communication.
[0019] In a specific embodiment, the XA-based two-phase transaction includes a preparation phase. In addition to passively accepting commit instructions, the database can also reversely notify the caller whether the transaction can be committed; the transaction manager collects the preparation results of all branch transactions and performs atomic commit at the end.
[0020] In a specific embodiment, the method adopts a method of separating XA transaction management and connection pool management when integrating XA transactions, and the XA-based two-phase transaction can use XA and non-XA connection pools at the same time.
[0021] In a specific embodiment, the SEATA-based flexible transaction model includes a transaction manager, a resource manager and a transaction coordinator;
[0022] in,
[0023] The transaction coordinator is an independently deployed service. The transaction manager and resource manager are deployed together with the business application in the form of jar packages. They establish a persistent connection with the transaction coordinator and maintain remote communication throughout the transaction lifecycle.
[0024] The transaction manager is the initiator of global transactions and is responsible for opening, committing, and rolling back global transactions.
[0025] The resource manager is a participant in the global transaction and is responsible for reporting the execution results of branch transactions and committing and rolling back branch transactions through the coordination of the transaction coordinator.
[0026] In a specific embodiment, the data source configured by the user is aggregated based on the SEATA flexible transaction oriented DataSource interface.
[0027] In a specific embodiment, when integrating Seata AT transactions, the method encapsulates the DataSource interface into a DataSource interface based on SEATA.
[0028] In a specific embodiment, the method executes the sharded physical SQL in a multi-threaded manner, and when integrating SeataAT transactions, the context of the global transaction ID is transferred between the main thread and the child thread.
[0029] In a specific embodiment, the implementation method based on XA two-phase transaction includes the following steps:
[0030] S101: Start global transaction;
[0031] When receiving the set autoCommit=0 signal from the access end, XADCSTransactionManager will call the specific XA transaction manager to start the XA global transaction and mark it in the form of XID;
[0032] S102: Execute the real sharding SQL;
[0033] After XADCSTransactionManager registers the XAResource corresponding to the database connection into the current XA transaction, the transaction manager sends the XAResource.start command to the database at this stage; all SQL operations before the database receives the XAResource.end command are marked as XA transactions;
[0034] S103: Commit or rollback the transaction;
[0035] After receiving the commit command from the access end, XADCSTransactionManager entrusts the actual XA transaction manager to perform the commit action. The transaction manager collects all XAResources in the current thread and sends the XAResource.end command to mark the boundary of this XA transaction. Then, it sends the prepare command in sequence to collect votes from all participating XAResources.
[0036] If the feedback results of all XAResources are correct, call the commit command for final submission;
[0037] If the feedback result of any XAResource is incorrect, call the rollback instruction to roll back;
[0038] After the transaction manager issues a commit command, any exceptions generated by XAResource will be retried through the recovery log;
[0039] in,
[0040] XADCSTransactionManager is an XA implementation class of distributed transactions of the distributed database middleware solution; it manages and adapts multiple data sources, and entrusts the opening, committing and rolling back operations of corresponding transactions to a specific XA transaction manager.
[0041] In a specific embodiment, the implementation method based on SEATA flexible transaction includes the following steps:
[0042] S201: Engine initialization;
[0043] When an application based on SEATA flexible transactions is started, the data source configured by the user is adapted to the DataSourceProxy required by SEATA transactions according to the configuration of seata.conf and is registered in the resource manager;
[0044] S202: Start global transaction;
[0045] The transaction manager controls the boundaries of the global transaction. The transaction manager obtains the global transaction ID by sending a Begin instruction to the transaction coordinator. All branch transactions participate in the global transaction through this global transaction ID. The context of the global transaction ID is stored in the current thread variable.
[0046] S203: Execute the real sharding SQL;
[0047] The shard SQL in the Seata global transaction generates an undo snapshot through the resource manager and sends a participate command to the transaction coordinator to join the global transaction;
[0048] S204: Commit or rollback the transaction;
[0049] When committing a SEATA-based flexible transaction, the transaction manager sends a commit or rollback instruction for the global transaction to the transaction coordinator, and the transaction coordinator coordinates all branch transactions to commit or rollback based on the global transaction ID.
[0050] The beneficial effects of the present invention are as follows:
[0051] The present invention is fully compatible with all distributed transaction scenarios and achieves optimal performance, but under the guidance of the CAP theorem, distributed transactions must make some trade-offs. The present invention hopes to give the user the right to choose distributed transactions and use the most suitable distributed transaction solution in different scenarios. The method integrates existing mature transactions, provides a unified distributed transaction interface for local transactions, two-phase transactions and flexible transactions, and makes up for the shortcomings of current methods.
[0052] The method provided by the present invention supports cross-database transactions after data sharding based on the XA two-phase transaction support item; the two-phase commit ensures the atomicity of the operation and the strong consistency of the data;
[0053] After the service is down and restarted, the transactions in progress of committing / rolling back can be automatically restored;
[0054] Supports using both XA and non-XA connection pools.
[0055] The support item based on SEATA flexible transaction in the method provided by the present invention supports cross-database transactions after data sharding;
[0056] Support RC isolation level;
[0057] Roll back transactions through undo snapshots;
[0058] Supports automatic recovery of committed transactions after service downtime. BRIEF DESCRIPTION OF THE DRAWINGS
[0059] In order to more clearly illustrate the specific implementation methods of the present application or the existing technical solutions, the drawings required for use in the specific implementation methods or the existing technical descriptions will be briefly introduced below. Obviously, the drawings described below are an implementation method of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0060] Figure 1A diagram showing an architecture of transaction manager and resource manager transaction exchange information according to an embodiment of the present invention.
[0061] Figure 2 A diagram showing an XA two-phase transaction architecture according to an embodiment of the present invention is shown.
[0062] Figure 3 A diagram of the SEATA architecture according to an embodiment of the present invention is shown.
[0063] Figure 4 A diagram of a SEATA flexible transaction architecture according to an embodiment of the present invention is shown. DETAILED DESCRIPTION
[0064] In order to make the technical solution of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. The present invention is described in detail below through specific embodiments, but these do not constitute a limitation of the present invention. Without departing from the principle of the present invention, those skilled in the art may make variations and improvements, which should also be regarded as the protection scope of the present invention.
[0065] The present invention provides a distributed database middleware solution (the method is referred to as DCS for short), which integrates existing mature transactions and provides a unified distributed transaction interface for local transactions, two-phase transactions and flexible transactions. The integrated transactions include:
[0066] Based on XA two-phase transactions (XA is the XA protocol, a specification for distributed transaction processing proposed by the X / Open organization) and SEATA flexible transactions (SEATA is a distributed transaction framework jointly created by Alibaba Group and Ant Financial);
[0067] The first is based on XA two-phase transactions:
[0068] XA two-phase transaction integrates XA transaction and two-phase transaction;
[0069] Two-phase transaction commit uses the abstract application engineering of the DTP model defined by the X / OPEN organization, and uses the concepts of TM (abbreviation of TransactionManager) and RM (abbreviation of ResourceManager) to ensure the strong consistency of distributed transactions. The transaction manager TM and the resource manager RM use the XA protocol for two-way communication. Figure 1 , Figure 1 A transaction manager and resource manager transaction information exchange architecture diagram according to an embodiment of the present invention is shown. In the diagram, an application engineering AP uses a group of resources from resource managers RMs and defines transaction boundaries through a TX interface.
[0070] Compared with traditional local transactions, XA transactions have an additional preparation phase. In addition to passively accepting commit instructions, the database can also notify the caller whether the transaction can be committed. TM can collect the preparation results of all branch transactions and perform atomic commits at the end to ensure strong consistency of transactions.
[0071] The XA two-phase transaction integrates the above XA transaction and the two-phase transaction. Therefore, the characteristics of the above XA transaction and the two-phase transaction are also based on the characteristics of the XA two-phase transaction.
[0072] Java implements the XA model by defining the JTA interface. The resource manager in the JTA interface requires the database vendor to provide an XA driver implementation, and the transaction manager requires the transaction manager vendor to implement it. Traditional transaction managers need to be bound to application servers, so the cost of using them is very high. The embedded transaction manager can provide services in the form of a jar package. After integration with the DCS, it can ensure strong consistency of cross-database transactions after sharding.
[0073] Usually, only by using the XA transaction connection pool provided by the transaction manager vendor can XA transactions be supported. When integrating XA transactions, DCS separates XA transaction management from connection pool management, and can use XA and non-XA connection pools at the same time, achieving zero intrusion on the application.
[0074] like Figure 2 , Figure 2 A diagram showing an XA two-phase transaction architecture according to an embodiment of the present invention is shown.
[0075] The implementation method of the present invention based on XA two-phase transaction is as follows:
[0076] S101: Start global transaction;
[0077] When receiving the set autoCommit=0 signal from the access end, XADCSTransactionManager will call the specific XA transaction manager to start the XA global transaction and mark it in the form of XID;
[0078] S102: Execute the real sharding SQL;
[0079] After XADCSTransactionManager registers the XAResource corresponding to the database connection into the current XA transaction, the transaction manager sends the XAResource.start command to the database at this stage; all SQL operations before the database receives the XAResource.end command are marked as XA transactions;
[0080] For example:
[0081] XAResource1.start##Enlist phase execution
[0082] statement.execute("sql1");##Simulate the execution of a shard SQL1
[0083] statement.execute("sql2");##Simulate the execution of a shard SQL2
[0084] XAResource1.end##Commit phase execution
[0085] In the above example, sql1 and sql2 will be marked as XA transactions.
[0086] S103: Commit or rollback the transaction;
[0087] After receiving the commit command from the access end, XADCSTransactionManager entrusts the actual XA transaction manager to perform the commit action. The transaction manager collects all XAResources in the current thread and sends the XAResource.end command to mark the boundary of this XA transaction. Then, it sends the prepare command in sequence to collect votes from all participating XAResources.
[0088] If the feedback results of all XAResources are correct, call the commit command for final submission;
[0089] If the feedback result of any XAResource is incorrect, call the rollback instruction to roll back;
[0090] After the transaction manager issues a commit command, any exceptions generated by XAResource will be retried through the recovery log to ensure the atomicity of operations in the commit phase and strong data consistency.
[0091] For example:
[0092] XAResource1.prepare##ack:yes
[0093] XAResource2.prepare##ack:yes
[0094] XAResource1.commit
[0095] XAResource2.commit
[0096] XAResource1.prepare##ack:yes
[0097] XAResource2.prepare##ack:no
[0098] XAResource1.rollback
[0099] XAResource2.rollback
[0100] in,
[0101] XADCSTransactionManager is the XA implementation class of the distributed transaction of the DCS method; it manages and adapts multiple data sources, and delegates the opening, committing, and rolling back operations of the corresponding transactions to the specific XA transaction manager.
[0102] Based on SEATA flexible transactions:
[0103] Based on SEATA flexible transactions, Seata AT transactions in the SEATA framework are integrated.
[0104] SEATA is a distributed transaction framework jointly created by Alibaba Group and Ant Financial. The goal of its AT transaction is to provide incremental transaction ACID semantics under the microservice architecture, allowing developers to use distributed transactions like local transactions. The core concept is the same as DCS.
[0105] The Seata AT transaction model includes TM, RM and TC (abbreviation of Transaction coordinator).
[0106] TC is an independently deployed service. TM and RM are deployed together with business applications in the form of jar packages. They establish a long connection with TC and maintain remote communication throughout the transaction life cycle.
[0107] TM is the initiator of global transactions and is responsible for starting, committing, and rolling back global transactions.
[0108] RM is a participant in the global transaction and is responsible for reporting the execution results of branch transactions and committing and rolling back branch transactions through the coordination of TC.
[0109] The typical life cycle of a distributed transaction managed by SEATA is as follows:
[0110] TM asks TC to start a new global transaction, and TC generates an XID representing the global transaction;
[0111] XID runs through the entire call chain of microservices;
[0112] As part of the global transaction under the TC corresponding to the XID, the RM registers the local transaction;
[0113] TM requires TC to commit or rollback the global transaction corresponding to XID;
[0114] All branch transactions under the global transaction corresponding to the TC driver XID are committed or rolled back.
[0115] like Figure 3 , Figure 3 A diagram of the SEATA architecture according to an embodiment of the present invention is shown.
[0116] When integrating Seata AT transactions, this method needs to integrate the models of TM, RM and TC into the distributed transaction method of DCS. In terms of database resources, Seata allows JDBC operations to communicate remotely with TC by docking with the DataSource interface. Similarly, DCS also aggregates user-configured data sources for the DataSource interface. Therefore, after encapsulating the DataSource interface as a Seata-based DataSource interface, the Seata AT transaction can be integrated into the DCS method.
[0117] Because the Seata AT transaction in the Seata framework is integrated based on the Seata flexible transaction, the above-mentioned Seata AT transaction features are also based on the Seata flexible transaction.
[0118] like Figure 4 , Figure 4 A diagram of a SEATA flexible transaction architecture according to an embodiment of the present invention is shown, wherein TM C / R is a transaction manager commit or rollback.
[0119] The implementation method of this embodiment based on SEATA flexible transaction is as follows:
[0120] S201: Engine initialization;
[0121] When an application based on SEATA flexible transactions is started, the data source configured by the user is adapted to the DataSourceProxy required by SEATA transactions according to the configuration of seata.conf and is registered in the resource manager;
[0122] S202: Start global transaction;
[0123] The transaction manager controls the boundaries of the global transaction. The transaction manager obtains the global transaction ID by sending a Begin instruction to the transaction coordinator. All branch transactions participate in the global transaction through this global transaction ID. The context of the global transaction ID is stored in the current thread variable.
[0124] S203: Execute the real sharding SQL;
[0125] The shard SQL in the Seata global transaction generates an undo snapshot through the resource manager and sends a participate command to the transaction coordinator to join the global transaction;
[0126] S204: Commit or rollback the transaction;
[0127] When committing a SEATA-based flexible transaction, the transaction manager sends a commit or rollback instruction for the global transaction to the transaction coordinator, and the transaction coordinator coordinates all branch transactions to commit or rollback based on the global transaction ID.
[0128] Among them, since the sharded physical SQL of DCS is executed in a multi-threaded manner, when integrating Seata AT transactions, it is necessary to transfer the context of the global transaction ID between the main thread and the child thread.
[0129] Obviously, the above embodiments of the present invention are merely examples for clearly illustrating the present invention, and are not limitations on the implementation methods of the present invention. For ordinary technicians in the relevant field, other different forms of changes or modifications can be made based on the above description. It is impossible to list all the implementation methods here. All obvious changes or modifications derived from the technical solution of the present invention are still within the protection scope of the present invention.
Claims
1. A distributed database middleware solution, It is characterized in that The method integrates existing mature transactions to provide a unified distributed transaction interface for local transactions, two-phase transactions, and flexible transactions. The integrated transactions include: Based on XA two-phase transactions and SEATA flexible transactions; in, The XA two-phase transaction integrates the XA transaction and the two-phase transaction; The SEATA flexible transaction integrates the SEATA AT transaction in the SEATA framework; The implementation method based on SEATA flexible transactions includes the following steps: S201: Engine initialization; When an application based on SEATA flexible transactions is started, the data source configured by the user is adapted to the DataSourceProxy required by SEATA transactions according to the configuration of seata.conf and is registered in the resource manager; S202: Start global transaction; The transaction manager controls the boundaries of the global transaction. The transaction manager obtains the global transaction ID by sending a Begin instruction to the transaction coordinator. All branch transactions participate in the global transaction through this global transaction ID. The context of the global transaction ID is stored in the current thread variable. S203: Execute the real sharding SQL; The shard SQL in the Seata global transaction generates an undo snapshot through the resource manager and sends a participate command to the transaction coordinator to join the global transaction; S204: Commit or rollback the transaction; When committing a SEATA-based flexible transaction, the transaction manager sends a commit or rollback instruction for the global transaction to the transaction coordinator, and the transaction coordinator coordinates all branch transactions to commit or rollback based on the global transaction ID.
2. The method according to claim 1, It is characterized in that The method includes integrating an embedded transaction manager and providing services in the form of a jar package.
3. The method according to claim 1, It is characterized in that The XA-based two-phase transaction submission adopts the application engineering abstracted by the DTP model defined by the X / OPEN organization, and the transaction manager and the resource manager use the XA protocol for two-way communication.
4. The method according to claim 1, It is characterized in that The XA-based two-phase transaction includes a preparation phase. In addition to passively accepting the commit instruction, the database can also reversely notify the caller whether the transaction can be committed. The transaction manager collects the preparation results of all branch transactions and performs an atomic commit at the end.
5. The method according to claim 1, It is characterized in that The method adopts a method of separating XA transaction management and connection pool management when integrating XA transactions, and the XA-based two-phase transaction can use XA and non-XA connection pools at the same time.
6. The method according to claim 1, It is characterized in that The SEATA-based flexible transaction model includes a transaction manager, a resource manager and a transaction coordinator; in, The transaction coordinator is an independently deployed service. The transaction manager and resource manager are deployed together with the business application in the form of jar packages. They establish a persistent connection with the transaction coordinator and maintain remote communication throughout the transaction lifecycle. The transaction manager is the initiator of global transactions and is responsible for opening, committing, and rolling back global transactions. The resource manager is a participant in the global transaction and is responsible for reporting the execution results of branch transactions and committing and rolling back branch transactions through the coordination of the transaction coordinator.
7. The method according to claim 1, It is characterized in that The SEATA flexible transaction is oriented to the DataSource interface to aggregate the data sources configured by the user.
8. The method according to claim 1, It is characterized in that When integrating Seata AT transactions, the method encapsulates the DataSource interface into a DataSource interface based on SEATA.
9. The method according to claim 1, It is characterized in that The method adopts a multi-threaded execution mode when the sharded physical SQL is executed, and when the Seata AT transaction is integrated, the context of the global transaction ID is transferred between the main thread and the child thread.
10. The method according to claim 1, It is characterized in that The implementation method based on XA two-phase transaction includes the following steps: S101: Start global transaction; When receiving the set autoCommit=0 command from the access end, the XA DCS TransactionManager will call the specific XA transaction manager to start the XA global transaction and mark it in the form of XID; S102: Execute the real sharding SQL; After XA DCS TransactionManager registers the XA Resource corresponding to the database connection into the current XA transaction, the transaction manager sends the XA Resource.start command to the database at this stage; all SQL operations before the database receives the XAResource.end command are marked as XA transactions; S103: Commit or rollback the transaction; After receiving the commit command from the access end, the XA DCS TransactionManager entrusts the actual XA transaction manager to perform the commit action. The transaction manager collects all XA Resources in the current thread and sends the XAResource.end command to mark the boundary of this XA transaction. Then, it sends the prepare command in sequence to collect votes from all participating XAResources. If the feedback results of all XA Resources are correct, call the commit command for final submission; If the feedback result of any XA Resource is incorrect, call the rollback command to roll back; After the transaction manager issues a commit command, any exceptions generated by the XA Resource will be retried through the recovery log; in, XA DCS TransactionManager is an XA implementation class of distributed transactions in the distributed database middleware solution; it manages and adapts multiple data sources, and delegates the start, commit and rollback operations of corresponding transactions to a specific XA transaction manager.
