WO2022206429A1 - 一种分布式事务实现方法及分布式系统 - Google Patents

一种分布式事务实现方法及分布式系统 Download PDF

Info

Publication number
WO2022206429A1
WO2022206429A1 PCT/CN2022/081697 CN2022081697W WO2022206429A1 WO 2022206429 A1 WO2022206429 A1 WO 2022206429A1 CN 2022081697 W CN2022081697 W CN 2022081697W WO 2022206429 A1 WO2022206429 A1 WO 2022206429A1
Authority
WO
WIPO (PCT)
Prior art keywords
transaction
branch
database server
request
coordinator
Prior art date
Application number
PCT/CN2022/081697
Other languages
English (en)
French (fr)
Inventor
姜宇
Original Assignee
华为云计算技术有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 华为云计算技术有限公司 filed Critical 华为云计算技术有限公司
Publication of WO2022206429A1 publication Critical patent/WO2022206429A1/zh
Priority to US18/474,695 priority Critical patent/US20240013218A1/en

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/27Replication, distribution or synchronisation of data between databases or within a distributed database system; Distributed database system architectures therefor
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5027Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals
    • G06F9/5038Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals considering the execution order of a plurality of tasks, e.g. taking priority or time dependency constraints into consideration
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5083Techniques for rebalancing the load in a distributed system

Definitions

  • the present application relates to the technical field of databases, and in particular, to a distributed transaction implementation method and a distributed system.
  • Distributed systems are based on the micro-service architecture, deploying multiple services in different regions or different nodes, and a transaction operation can be completed through remote cooperation between services. For example, when it is necessary to transfer 100 yuan from account a of bank A to account b of bank B, the first service needs to be called to deduct 100 yuan from the balance of account a in bank A's database, and the second service needs to be called to transfer bank B. The balance of account b in the database is increased by 100 yuan.
  • This kind of transaction in which a distributed system operates multiple independent databases to complete a work together is called a distributed transaction, also known as a global transaction.
  • a distributed transaction includes multiple branch transactions.
  • the database operation required to call the first service is a branch transaction
  • the database operation required to call the second service is another branch transaction.
  • a transaction coordinator will be selected first, and the execution devices of all branch transactions of the global transaction will interact with the transaction coordinator, such as branch transaction start, branch transaction end, transaction coordinator and lock server.
  • the service instance implementing the branch transaction and the transaction coordinator are not in the same region, the above interactions need to be called across regions, which will increase network latency and reduce transaction processing efficiency.
  • the embodiments of the present application disclose a distributed transaction implementation method and a distributed system, which can reduce the mutual calls of devices in different regions during the execution of the distributed transaction and improve the processing efficiency of the distributed transaction.
  • an embodiment of the present application provides a distributed transaction implementation method, and the method is applied to a distributed system, where the distributed system includes a client deployed in a first area, a first database server, and a first transaction coordinator, and a second database server and a second transaction coordinator deployed in the second area, wherein the first database server is used to run the first service, and the second database server is used to run the second service, and the method includes:
  • the client initiates a distributed transaction request to the first transaction coordinator
  • the client invokes the first database server to execute the first branch transaction according to the distributed transaction response returned by the first transaction coordinator, wherein the first branch transaction is a database transaction related to the first service;
  • the first database server After the first database server completes the first branch transaction through the first transaction coordinator, the first database server calls the second database server to execute the second branch transaction, and the second branch transaction is a database transaction related to the second service;
  • the second database server executes the second branch transaction through the second transaction coordinator, wherein the executing the second branch transaction includes performing a locking operation on the data involved in the second branch transaction through the second transaction coordinator.
  • each service corresponds to a branch transaction.
  • the database server executes the corresponding branch transaction and needs to lock the data of the branch transaction operation, the database server will be deployed in the same branch transaction.
  • the transaction coordinator of the region performs operations such as data locking without the need to implement the locking operation through the transaction coordinator of the region where the client that initiates the distributed transaction is located, which can reduce the number of devices located in different regions during the execution of the distributed transaction. The mutual calls between them reduce latency and improve the execution efficiency of distributed transactions.
  • the method before the second database server executes the second branch transaction through the second transaction coordinator, the method further includes: the second database server determines that the second database server is located in a different area from the first transaction coordinator, and the second database The server determines that it is in the same region as the second coordinator.
  • the above-mentioned distributed system further includes a second lock server deployed in the second area, and the second database server executes the second branch transaction through the second transaction coordinator, including:
  • the second database server sends a start request of the second branch transaction to the second transaction coordinator, wherein the start request of the second branch transaction includes location information of the data involved in the second branch transaction in the second database;
  • the second transaction coordinator sends a second lock request to the second lock server, where the second lock request includes location information of the data involved in the second branch transaction in the second database;
  • the second lock server performs a locking operation on the data involved in the second branch transaction according to the second locking request, and sends the locking result to the second transaction coordinator;
  • the second transaction coordinator feeds back the locking result to the second database server
  • the second database server executes the second branch transaction according to the locking result, and feeds back the execution result of the second branch transaction to the client through the first database server.
  • the database server When the database server executes the corresponding branch transaction and needs to register the branch transaction with the transaction coordinator, the database server will send a branch transaction start request to the transaction coordinator deployed in the same region to register the branch transaction, without the need to initiate a distributed transaction.
  • the transaction coordinator in the region where the client is located realizes the registration, and the transaction coordinator that receives the request to start the branch transaction realizes the lock through the lock server in the same region. Therefore, it can reduce the mutual calls between devices located in different regions during the execution of distributed transactions, reduce the delay, and improve the execution efficiency of distributed transactions.
  • the second database server executes the second branch transaction according to the locking result, and after the execution result of the second branch transaction is fed back to the client through the first database server, the client executes the second branch transaction according to the second database server.
  • the execution result of the second branch transaction is fed back to determine the execution result of the distributed transaction, wherein the execution result of the distributed transaction includes execution success and execution failure.
  • the method includes:
  • the client sends a global operation request to the first transaction coordinator according to the execution result of the distributed transaction, wherein, when the execution result of the distributed transaction is successful, the global operation request is a global transaction submission request. When the execution fails, the global operation request is a global transaction rollback request;
  • the first transaction coordinator sends a two-stage operation request of the global transaction to the second transaction coordinator according to the global operation request, and the two-stage operation request is a two-stage commit request or a two-stage rollback request, wherein the global operation request is a global transaction
  • the two-stage operation request is a two-stage commit request
  • the global operation request is a global transaction rollback request
  • the two-stage operation request is a two-stage rollback request
  • the second transaction coordinator schedules the second database server to execute the two-phase operation request.
  • the second database server executes the second branch transaction according to the locking result, and feeds back the execution result of the second branch transaction to the client, including: when the locking result is that the locking is successful and the second When the database server successfully completes the second branch transaction, the second database server feeds back the execution result of the successful execution of the second branch transaction to the client; or, when the locking result is a locking failure or the second database server has not completed the second branch transaction.
  • the second database server feeds back to the client the execution result that the execution of the second branch transaction fails.
  • the client determines the execution result of the distributed transaction according to the execution result of the second branch transaction fed back by the second database server, including: the execution result of the client in the second branch transaction is that the execution is successful and When the execution results of other branch transactions included in the distributed transaction are all successful, the execution result of the distributed transaction is determined to be successful; or, when the execution result of the second branch transaction is an execution failure, the execution result of the distributed transaction is determined. Failed to execute.
  • the method further includes: a second transaction The coordinator sends a lock release request to the second lock server; the second lock server releases the lock added when performing the lock operation on the data involved in the second branch transaction, and sends a lock release success message to the second transaction coordinator.
  • the transaction coordinator Since the database server locks data through the transaction coordinator deployed in the same region, the transaction coordinator realizes the locking through the lock server in the same region. Therefore, when a lock needs to be released, the transaction coordinator does not need to implement the lock release operation through cross-region calls, which can reduce the mutual calls between devices located in different regions during the execution of distributed transactions, reduce latency, and improve distributed transactions. execution efficiency.
  • the method before the first database server calls the second database server to execute the second branch transaction, the method further includes: the first database server sends a start request of the first branch transaction to the first transaction coordinator; the first transaction The coordinator creates the first branch transaction identifier according to the start request of the first branch transaction, and sends the first branch transaction identifier to the first database server; after receiving the first branch transaction identifier, the first database server successfully executes the first branch affairs.
  • the distributed system further includes a first lock server, and before the first transaction coordinator creates the first branch transaction identifier, further includes:
  • the first transaction coordinator receives a start request of the first branch transaction sent by the first database server, where the first branch transaction start request includes location information of the data involved in the first branch transaction in the first database;
  • the first transaction coordinator sends a first lock request to the first lock server, where the first lock request includes location information of the data involved in the first branch transaction in the first database;
  • the first lock server performs a lock operation on the data involved in the first branch transaction according to the first lock request, and sends a lock success message to the second transaction coordinator;
  • the first transaction coordinator receives a lock success message sent by the first lock server.
  • an embodiment of the present application provides a distributed system, the distributed system includes a client deployed in a first area, a first database server and a first transaction coordinator, and a second database deployed in a second area A server and a second transaction coordinator, wherein the first database server is used to run the first service and the second database server is used to run the second service, wherein,
  • the client is used to initiate a distributed transaction request to the first transaction coordinator
  • the first database server to execute the first branch transaction according to the distributed transaction response returned by the first transaction coordinator, wherein the first branch transaction is a database transaction related to the first service;
  • the first database server is configured to call the second database server to execute the second branch transaction after completing the first branch transaction through the first transaction coordinator, and the second branch transaction is a database transaction related to the second service;
  • the second database server is configured to execute the second branch transaction through the second transaction coordinator, wherein executing the second branch transaction includes performing a locking operation on the data involved in the second branch transaction through the second transaction coordinator.
  • the second database server is further configured to determine that the first transaction coordinator is located in a different region, and the second database server determines that the second transaction coordinator is located in the same region.
  • the distributed system further includes a second lock server deployed in the second area,
  • the second database server executes the second branch transaction through the second transaction coordinator, including:
  • the second database server is further configured to send a start request of the second branch transaction to the second transaction coordinator, where the start request of the second branch transaction includes location information of the data involved in the second branch transaction in the second database;
  • the second transaction coordinator is further configured to send a second lock request to the second lock server, where the second lock request includes location information of the data involved in the second branch transaction in the second database;
  • a second lock server configured to perform a locking operation on the data involved in the second branch transaction according to the second locking request, and send the locking result to the second transaction coordinator;
  • the second transaction coordinator is also used to feed back the locking result to the second database server;
  • the second database server is further configured to execute the second branch transaction according to the locking result, and feed back the execution result of the second branch transaction to the client.
  • the client is further configured to determine the execution result of the distributed transaction according to the execution result of the second branch transaction fed back by the second database server;
  • the client is further configured to send a global operation request to the first transaction coordinator according to the execution result of the distributed transaction, wherein when the execution result of the distributed transaction is that the execution is successful, the global operation request Submit a request for a global transaction.
  • the global operation request is a global transaction rollback request;
  • the first transaction coordinator is further configured to send a two-stage operation request of the global transaction to the second transaction coordinator according to the global operation request, where the two-stage operation request is a two-stage commit request or a two-stage rollback request, wherein the When the request is a global transaction commit request, the two-phase operation request is a two-phase commit request, and when the global operation request is a global transaction rollback request, the two-phase operation request is a two-phase rollback request;
  • the second transaction coordinator is further configured to schedule the second database server to execute the second-stage operation request.
  • the second database server is further configured to execute the second branch transaction according to the locking result, and feed back the execution result of the second branch transaction to the client, specifically including:
  • the second database server feeds back the execution result of the successful execution of the second branch transaction to the client; or, when the result of the locking is that the locking fails Or when the second database server fails to complete the second branch transaction, the second database server feeds back the execution result of the failure of the second branch transaction to the client.
  • the client is further configured to determine the execution result of the distributed transaction according to the execution result of the second branch transaction fed back by the second database server, which specifically includes: the execution result of the client in the second branch transaction If the execution is successful and the execution results of other branch transactions included in the distributed transaction are all successful, the execution result of the distributed transaction is determined to be successful; or, when the execution result of the second branch transaction is execution failure, it is determined that the distributed transaction The execution result of the transaction is execution failure.
  • the second transaction coordinator when the second lock server successfully locks the data involved in the second branch transaction, the second transaction coordinator is further configured to schedule the second database server to execute the second-stage operation request before the second transaction The coordinator is also used to send a lock release request to the second lock server; the second lock server is also used to release the lock added when performing the locking operation on the data involved in the second branch transaction, and send the lock to the second transaction coordinator A message that the lock was released successfully.
  • the method before the first database server invokes the second database server to execute the second branch transaction, the method further includes:
  • the first database server further configured to send a start request of the first branch transaction to the first transaction coordinator;
  • the first transaction coordinator is further configured to create the first branch transaction identifier according to the start request of the first branch transaction, and send the first branch transaction identifier to the first database server;
  • the first database server is further configured to successfully execute the first branch transaction after receiving the first branch transaction identifier.
  • the distributed system further includes a first lock server,
  • the first transaction coordinator Before the first transaction coordinator is used to create the first branch transaction identifier, it further includes:
  • the first transaction coordinator is further configured to receive a start request of the first branch transaction sent by the first database server, where the first branch transaction start request includes location information of the data involved in the first branch transaction in the first database;
  • a first lock server configured to perform a lock operation on the data involved in the first branch transaction according to the first lock request, and send a lock success message to the second transaction coordinator;
  • the first transaction coordinator is further configured to receive a lock success message sent by the first lock server.
  • FIG. 1 is a schematic diagram of a distributed system provided by an embodiment of the present application.
  • FIG. 2 is an interactive schematic diagram of a distributed transaction implementation method provided by an embodiment of the present application.
  • FIG. 3 is an interactive schematic diagram of another distributed transaction implementation method provided by an embodiment of the present application.
  • FIG. 4 is an interactive schematic diagram of another distributed transaction implementation method provided by an embodiment of the present application.
  • FIG. 5 is a schematic structural diagram of a second transaction coordination device provided by an embodiment of the present application.
  • a transaction refers to an operation task of accessing or updating data in a database.
  • a transaction is limited to accessing a single database resource, which is called a local transaction.
  • microservice-based distributed applications a business usually needs to call multiple microservices, and multiple microservice calls usually correspond to multiple database local transactions.
  • distributed transactions are required to ensure the data consistency of these multiple database local transactions.
  • Distributed transaction refers to a transaction that needs to operate multiple independent databases to complete a business together.
  • a distributed transaction can include multiple branch transactions.
  • the responsibility of a distributed transaction is to coordinate the branch transactions under its jurisdiction to reach an agreement, either to commit successfully together or to roll back if they fail together.
  • each branch transaction itself is a relational database. local affairs.
  • the user initiates a transfer service through the terminal to transfer 100 yuan from the account a of bank A to the account b of bank B.
  • the business needs to operate the data in the database of Bank A and the database of Bank B respectively.
  • the business needs to call the first service to complete the operation of deducting 100 yuan from the account a of the database of Bank A, and call the second service to complete the operation to B.
  • the operation of adding 100 yuan to account b in the bank's database is a branch transaction, and the database operation required to call the second service is another branch transaction. Since a distributed transaction is usually a global transaction spanning multiple regions or multiple databases, a distributed transaction in this application is also called a global transaction, and in the description of the embodiments of this application, a global transaction means a distributed transaction transaction.
  • the distributed system provided by the embodiment of the present application includes multiple regions (regions), and each region includes one or more user terminals deployed with clients (clients), one or more transaction coordinators (transaction coordinators, TCs), One or more database servers for providing services (eg microservices), one or more lock servers, and one or more databases.
  • FIG. 1 is a schematic diagram of a distributed system.
  • the distributed system includes area 1 and area 2, and area 1 includes user terminal A, a first database server, a first transaction coordinator, and a first lock.
  • user terminal A is deployed with client A, and the first database server is deployed with the first service;
  • area 2 includes user terminal B, second database server, second transaction coordinator, second lock server and database B , the user terminal B is deployed with the client B, and the second database server is deployed with the second service.
  • client A in region 1 initiates a distributed transaction to the first transaction coordinator, and the distributed transaction includes the first branch transaction and the second branch transaction as an example, in the implementation manner of the related art, the execution of the distributed transaction
  • the process (taking the successful commit of a distributed transaction as an example) includes:
  • Client A initiates a distributed transaction request to the first transaction coordinator, and the first transaction coordinator creates a global transaction identifier (identity, ID), and returns the global transaction ID to client A, where the global transaction ID uses
  • a transaction manager TM is embedded in the client.
  • the TM is responsible for initiating a distributed transaction request to start a distributed transaction, and initiates a global commit or global rollback to the transaction coordinator.
  • the transaction coordinator is used to maintain the running state of the distributed transaction, receives the instruction of the transaction manager to initiate the commit and rollback of the distributed transaction, and is responsible for communicating with the resource manager to coordinate the commit or rollback of each branch transaction;
  • Client A calls the first service deployed in the first database server in area 1, and the first service registers the first branch transaction with the first transaction coordinator, wherein the database server is embedded with a resource manager , RM), which is responsible for branch registration, status reporting, and receives instructions from the transaction coordinator, operates on the database, and drives the commit and rollback of branch (local) transactions;
  • RM resource manager
  • the first transaction coordinator determines that the locking data of the first branch transaction belongs to the first lock server, and initiates a lock request to the first lock server. After the first lock server is successfully locked, it returns the lock to the first transaction coordinator. A lock success message, wherein the data involved in the first branch transaction is locked to prevent conflicts or errors from other concurrent transactions operating on the data;
  • the first transaction coordinator creates the first branch transaction ID and sends it to the first RM of the first service.
  • the first RM receives the first branch transaction ID, and performs the corresponding operation on the data in the database A that the first branch transaction needs to operate. After the operation, submit the local transaction (that is, the first branch transaction), and return to the first transaction coordinator a message that the first branch transaction was successfully executed;
  • the first service invokes the second service in the second database server in the area 2, and the second RM of the second service registers the second branch transaction with the first transaction coordinator;
  • the first transaction coordinator determines that the locked data of the second branch transaction belongs to the second lock server in area 2, and initiates a lock request to the second lock server. After the second lock server is successfully locked, it coordinates with the first transaction.
  • the device returns a lock success message;
  • the first transaction coordinator creates the second branch transaction ID and sends it to the second RM of the second service.
  • the second RM receives the second branch transaction ID and performs corresponding operations on the data in the database B that the second branch transaction needs to operate. After the operation, submit the local transaction (that is, the second branch transaction), and return to the first transaction coordinator a message that the second branch transaction was successfully executed;
  • the second service After the second service completes the second branch transaction, it returns the second service completion message to the first service, and the first service returns the first service and the second service completion message to the client A. After the client determines that the call is completed , initiate a distributed transaction commit request to the transaction coordinator;
  • the first transaction coordinator After receiving the distributed transaction submission request, the first transaction coordinator sends a lock release request to the first lock server and the second lock server respectively, and the first lock server and the second lock server send a release request to the first transaction coordinator.
  • Lock success message
  • the first transaction coordinator After the first transaction coordinator receives the message that the lock is successfully released, it sends a two-phase commit request to the first RM and the second RM respectively. After the first RM and the second RM complete the two-phase commit, they respectively send a Send a successful submission message.
  • multiple calls are involved between the device in area 1 and the device in area 2, including: in step (5), the first service calls the second service, and in step (5) the second service calls the second service to the first service.
  • a transaction coordinator registers the second branch transaction.
  • the first transaction coordinator initiates a lock request to the second server, and in step (7), the second service returns to the first transaction coordinator that the second branch transaction is successfully executed.
  • message in step (9), the first transaction coordinator sends a lock release request to the second lock server, and in step (10) the first transaction coordinator calls the second service to perform two-phase commit, etc., that is, a distributed transaction includes at least the above 6 calls across regions.
  • a distributed transaction that needs to be completed across regions needs to generate multiple cross-region calls, which will increase the network delay, increase the time to complete the transaction, and affect the efficiency of distributed transaction processing.
  • the embodiments of the present application provide a distributed transaction implementation method, which is used to reduce cross-region calls in the process of distributed transaction implementation and improve the processing efficiency of distributed transactions.
  • the method is applied to the above-mentioned distributed system. Taking the distributed system shown in FIG. 1 implementing a distributed transaction including a first branch transaction and a second branch transaction as an example, the distributed transaction implementation method provided by the embodiment of the present application is introduced. As shown in FIG. 2 , FIG. 2 is a schematic diagram of interaction for implementing a distributed transaction provided by an embodiment of the present application.
  • the method includes the following S201 to S214.
  • Client A sends a distributed transaction request to the first transaction coordinator.
  • a user initiates a distributed transaction through client A in user terminal A in area 1, and client A sends a distributed transaction request to the first transaction coordinator in area 1.
  • the distributed transaction includes a first branch transaction and a second branch transaction affairs.
  • the first transaction coordinator After receiving the distributed transaction request, the first transaction coordinator returns a distributed transaction response to the client A.
  • the first transaction coordinator After receiving the distributed transaction request, the first transaction coordinator creates a global transaction ID and records the global transaction ID, and then returns a distributed transaction response including the global transaction ID to client A of user terminal A.
  • client A After receiving the distributed transaction response, client A calls the first database server to execute the first branch transaction.
  • the distributed transaction initiated by client A needs to call the first service to complete the first branch transaction and the second service to complete the second branch transaction, wherein the first branch transaction is a database transaction related to the first service, The first service is deployed on the first database server in area 1; the second branch transaction is a database transaction related to the second service, and the second service is deployed on the second database server in area 2; the data that the first branch transaction needs to operate is located in the database A, the data to be operated by the second branch transaction is located in database B.
  • the first branch transaction is a database transaction related to the first service
  • the first service is deployed on the first database server in area 1
  • the second branch transaction is a database transaction related to the second service
  • the second service is deployed on the second database server in area 2
  • the data that the first branch transaction needs to operate is located in the database A
  • the data to be operated by the second branch transaction is located in database B.
  • the first database server sends a first branch start request to the first transaction coordinator.
  • the first database server executes the first branch transaction, it needs to execute the first branch transaction through the first transaction coordinator, including the first transaction coordinator registering the first branch transaction and completing the first branch transaction through the first branch transaction. For the locking operation of the involved data, a first branch start request is sent to the first transaction coordinator.
  • the first transaction coordinator sends a first lock request to the first lock server.
  • the first lock request indicates to perform a lock operation on the data involved in the first branch transaction.
  • the first lock server implements a locking operation on the data involved in the first branch transaction, and returns a lock success message to the first transaction coordinator.
  • the first transaction coordinator returns a response message for the first branch start request to the first database server, and the first database server successfully executes the first branch transaction after receiving the response message.
  • the first database server invokes the second database server to execute the second branch transaction.
  • the second database server sends a second branch start request to the second transaction coordinator.
  • the second database server determines that the first transaction coordinator is located in a different region, and the second database server determines that the second transaction coordinator is located in the same region, the second database server sends the second branch start to the second transaction coordinator
  • the request when the second database server executes the second branch transaction, needs to execute the second branch transaction through the second transaction coordinator, including the second transaction coordinator registering the second branch transaction and completing the second branch transaction through the second branch transaction.
  • the second branch start request includes location information in the second database for data involved in the second branch transaction.
  • the second transaction coordinator sends a second lock request to the second lock server.
  • the second locking request instructs to perform a locking operation on the data involved in the second branch transaction, and the second locking request includes location information of the data involved in the second branch transaction in the second database.
  • the second lock server implements the locking operation on the data involved in the second branch transaction according to the second locking request, and returns the locking result to the second transaction coordinator.
  • the locking result includes locking success and locking failure.
  • the second transaction coordinator feeds back the locking result to the second database server.
  • the second database server executes the two-branch transaction according to the locking result, and feeds back the execution result of the second branch transaction to the client.
  • the second database server when the locking result is that the locking fails, the second database server cannot operate the data involved in the second branch transaction, and the execution result of the second branch transaction is the execution failure; when the locking result is that the locking succeeds, the second database server The server can operate the data involved in the second branch transaction. If the second database server can successfully perform the local commit, the execution result of the second branch transaction is successful; if the second database server cannot successfully perform the local commit, the second branch transaction The execution result is execution failure.
  • the second database server sends the execution result of the second branch transaction to the first database server, and the first database server
  • the client determines the execution result of the distributed transaction according to the execution result of the second branch transaction fed back by the second database server.
  • the client determines that the execution result of the distributed transaction is successful; When the execution fails, determine that the execution result of the distributed transaction is execution failure.
  • FIG. 3 The method includes S1 to S30.
  • Client A sends a distributed transaction request to the first transaction coordinator.
  • the user initiates a distributed transaction through the client A in the user terminal A of the area 1, and the first TM of the client A sends a distributed transaction request to the first transaction coordinator of the area 1.
  • the distributed transaction includes a first branch transaction and a second branch transaction, and the first service needs to be called to complete the first branch transaction and the second service to complete the second branch transaction, wherein the first service is deployed on the first database server in area 1, The second service is deployed on the second database server in area 2, the data to be operated by the first branch transaction is located in database A, and the data to be operated by the second branch transaction is located in database B.
  • the first transaction coordinator creates a global transaction ID according to the distributed transaction request, and sends it to client A.
  • the first transaction coordinator After the first transaction coordinator receives the distributed transaction request, it creates a global transaction ID and records the global transaction ID, and then returns the global transaction ID to the client A of the user terminal A, wherein the global transaction ID is used for unique identification A distributed transaction.
  • Client A constructs a transaction context corresponding to the distributed transaction according to the global transaction ID and sends it to the first RM.
  • client A After acquiring the global transaction ID, client A builds a transaction context corresponding to the distributed transaction, calls the first service deployed in the first database server, and sends the transaction context to the first database server.
  • a transaction context is an association between a transactional operation on a resource and the component that invokes the operation.
  • a transaction context logically encapsulates all operations performed on transactional resources during a transaction.
  • the transaction context includes a global transaction ID and address information of the first transaction coordinator, wherein the global transaction ID indicates the distributed transaction to which the branch transaction currently executed by the first database server belongs, and the address information is used to indicate that the first database server is executing the global transaction. Interact with the first transaction coordinator when the transaction corresponding to the transaction ID is performed.
  • the first RM constructs the first mirror data and the first locking information according to the transaction context, and sends the first branch start request and the first locking information to the first transaction coordinator.
  • the first mirror data includes a first front mirror record and a second back mirror record
  • the structured query language executed by the first RM on the first service, SQL
  • the input parameters include the information of the data that the first branch transaction needs to operate.
  • the distributed transaction is a transfer operation from account a to account b
  • the first branch transaction is to deduct 100 yuan from the balance in account a
  • the input parameters include the ID of account a, the ID of account b, and the transferred amount, etc.
  • the first pre-mirror record records the first target data of the transaction operation of the first branch before executing SQL. For example, account a is transferring money.
  • the first rear mirror record records the second target data obtained after executing the SQL statement to modify the first target data, such as the amount of account a before the transfer;
  • the first lock information includes the location of the first target data The name of the database, the name of the table where the first target data is located, and the primary key of the row where the first target data is located.
  • the first RM constructs the locking information, it determines that the first transaction coordinator and the first database server are located in the same area according to the address information of the first transaction coordinator, and sends a first branch start request to the first transaction coordinator, the first The branch start request includes a global transaction ID and first locking information, wherein the global transaction ID is used by the first transaction coordinator to associate the first branch transaction with the distributed transaction, and the first locking information indicates that the first branch transaction needs to be added. lock data.
  • the first transaction coordinator After receiving the first lock information, the first transaction coordinator sends a first lock request to the first lock server.
  • the first transaction coordinator After receiving the first locking information, the first transaction coordinator determines that the data indicated to be locked in the first locking information belongs to the management of the first lock server, and the first transaction coordinator sends the first lock request to the first lock server,
  • the first lock request includes first lock information, which is used to instruct to lock the data row where the first target data in the first lock information is located.
  • the first lock server executes the lock operation according to the first lock request, and returns information that the lock is successful to the first transaction coordinator.
  • the first transaction coordinator creates the first branch transaction ID, and sends the first branch transaction ID to the first RM.
  • the first transaction coordinator After receiving the successful locking information, the first transaction coordinator creates the first branch transaction ID corresponding to the first branch transaction, associates the global transaction ID with the first branch transaction ID, and sends the first branch transaction ID to the first branch transaction ID.
  • the first branch transaction ID is used to uniquely identify the first branch transaction.
  • the first RM performs a local commit of the first branch transaction.
  • the first RM of the first service After receiving the above-mentioned first branch transaction ID, the first RM of the first service performs local submission, that is, performs the submission of the first branch transaction.
  • the first RM sends a message that the first branch transaction is successfully submitted to the first transaction coordinator.
  • the message that the first branch transaction is successfully submitted includes the first branch transaction ID of the first branch transaction.
  • the first transaction coordinator records that the first branch transaction is successfully submitted, and returns a confirmation message to notify the first RM that it has received the message that the first branch transaction is successfully submitted.
  • the first service invokes the second service, and the first RM sends the transaction context to the second RM of the second service.
  • the first service invokes the second service, and the first database server sends the transaction context to the second database server through the first RM.
  • the second RM constructs the second mirror data and the second locking information according to the transaction context, and sends the second branch start request and the second locking information to the second transaction coordinator.
  • the second mirror data includes a second front mirror record and a second back mirror record.
  • the second RM of the second service obtains the transaction context
  • the second RM parses the structured query language executed by the second service, and according to the second RM
  • An input parameter of a service constructs a second pre-mirror record corresponding to the second transaction branch, then executes SQL, constructs a second post-mirror record according to the execution result, and constructs transaction second lock information.
  • the input parameters of the first service include the information of the data that the second branch transaction needs to operate, such as the above-mentioned transfer operation from account a to account b, and the second branch transaction is to increase the balance in account b by 100 yuan, then the second service
  • the input parameters of the first service include the ID of account b and the amount transferred; Amount; the second post-mirror record records the fourth target data obtained after executing the SQL statement to modify the third target data, such as the amount in account b after the transfer;
  • the second locking information includes the data of the database where the third target data is located. Name, the name of the table where the third target data is located, and the primary key of the row where the third target data is located.
  • the second RM constructs the second locking information, it determines according to the address information of the first transaction coordinator that the first transaction coordinator and the second database server are located in different regions, and the second RM determines according to the configuration information that the second transaction coordinator is the first transaction coordinator.
  • a transaction coordinator associated with a transaction coordinator in region 2 the second RM sends a second branch start request to the second transaction coordinator, where the second branch start request includes the global transaction ID and the second locking information, wherein the global The transaction ID is used by the second transaction coordinator to associate the second branch transaction with the distributed transaction, and the second locking information indicates data to be locked by the second branch transaction.
  • the second transaction coordinator After receiving the second lock information, the second transaction coordinator sends a second lock request to the second lock server.
  • the second transaction coordinator After receiving the second locking information, the second transaction coordinator determines that the data indicated to be locked in the second locking information belongs to the management of the second lock server, and the second transaction coordinator sends the second lock request to the second lock server,
  • the second lock request includes second lock information, which is used to instruct to lock the data row where the third target data in the second lock information is located.
  • the execution result of the distributed transaction includes global transaction commit and global transaction rollback, and client A needs to determine whether the distributed transaction finally executes global transaction commit or global transaction according to the execution results of all branch transactions included in the distributed transaction. rollback.
  • client A determines to execute the global transaction commit; the execution result of any branch transaction included in the distributed transaction is In the case of execution failure, client A determines to perform global transaction rollback.
  • the execution result of the first branch transaction is that the execution is successful.
  • the following takes the first service calling the second service to execute the second branch transaction as an example, and respectively introduces two types of successful execution of the second branch transaction and failure of the second branch transaction. case, the distributed transaction implements the execution process of the method.
  • the following first introduces the distributed transaction implementation method on the premise that the second branch transaction is successfully executed.
  • the above distributed transaction implementation method further includes the following S14 to S30.
  • the second lock server performs a locking operation according to the second locking request, and returns information of successful locking to the second transaction coordinator.
  • the second transaction coordinator creates a second branch transaction ID, and sends the second branch transaction ID to the second RM.
  • the second transaction coordinator After receiving the successful locking information, the second transaction coordinator creates a second branch transaction ID corresponding to the second branch transaction, associates the global transaction ID with the second branch transaction ID, and sends the second branch transaction ID to the second branch transaction ID.
  • the second RM performs a local commit of the second branch transaction.
  • the second RM of the second service After receiving the above-mentioned second branch transaction ID, the second RM of the second service submits the local transaction, that is, performs the submission of the second branch transaction.
  • the second RM sends a message that the second branch transaction is successfully committed to the second transaction coordinator.
  • the message that the second branch transaction is successfully submitted includes the second branch transaction ID of the second branch transaction.
  • the second transaction coordinator records that the second branch transaction is successfully submitted, and returns a confirmation message to notify the second RM that it has received the message that the second branch transaction is successfully submitted.
  • the second RM After receiving the confirmation message sent by the second transaction coordinator, the second RM returns a message that the call is successful to the first RM.
  • the first RM After receiving the call success message sent by the second RM, the first RM returns a call success message to the client A.
  • Client A sends a global commit request to the first transaction coordinator.
  • the global submission request includes the above-mentioned global transaction ID.
  • client A After receiving the successful call message sent by the first RM, client A determines that all branch transactions of the distributed transaction are successfully executed. Client A determines that the distributed transaction is successfully executed, and sends The first transaction coordinator sends a global commit request, and the global commit request instructs the database server executing the branch transaction to perform a two-phase commit operation.
  • the first transaction coordinator sends a first lock release request to the first lock server.
  • the first transaction coordinator After the first transaction coordinator receives the global commit request, it determines, according to the first branch transaction ID associated with the global transaction ID, that a lock release operation needs to be performed on the lock added to the first branch transaction, and the first transaction coordinator sends the first lock server to the first transaction coordinator. Send the first lock release request.
  • the first lock release request includes the above-mentioned first lock information to indicate the data that the first lock server needs to release the lock.
  • the first lock server performs a lock release operation, and sends a lock release success message to the first transaction coordinator.
  • the first lock server After receiving the first lock release request, the first lock server releases the lock added for the first branch transaction, and sends a lock release success message to the first transaction coordinator after the lock release is successful.
  • the first transaction coordinator sends a two-phase commit request of the distributed transaction to the second transaction coordinator.
  • the two-stage commit request includes a global transaction ID and a second branch transaction ID, which are used to indicate a branch transaction that needs to be submitted.
  • the second transaction coordinator sends a second lock release request to the second lock server.
  • the above-mentioned second lock release request includes the above-mentioned second lock information to indicate the data that the second lock server needs to release the lock.
  • the second lock server performs a lock release operation, and sends a lock release success message to the second transaction coordinator.
  • the second lock server After receiving the second lock release request, the second lock server releases the lock added for the second branch transaction, and after the second lock server successfully releases the lock, it sends a lock release success message to the second transaction coordinator.
  • the second transaction coordinator returns a message that the two-phase commit is completed to the first transaction coordinator.
  • the first transaction coordinator returns to the client A a message that the distributed transaction is completed.
  • the first transaction coordinator returns a distributed transaction completion message to client A, for client A, the distributed transaction has been completed.
  • the first transaction coordinator initiates a two-stage submission request for the first branch transaction to the first RM. After the first RM receives the request, it clears the first mirror data created for the first branch transaction, and sends a request to the first transaction coordinator. Returns a two-phase commit complete message.
  • the second transaction coordinator initiates a two-phase commit request for the second branch transaction to the second RM. After receiving the request, the second RM clears the second mirror data created for the second branch transaction, and sends the request to the second transaction coordinator. Returns a two-phase commit complete message.
  • FIG. 4 is another schematic diagram of interaction for implementing a distributed transaction provided by an embodiment of the present application. Taking the locking failure of the second lock server as an example, in the execution process of the distributed transaction implementation method, the process before the above S14 is as described in the above S1 to S13, and the second transaction coordinator receives the second locking information. After that, after sending the second lock request to the second lock server, the execution process of the distributed transaction further includes S14 to S24.
  • the second lock server performs the lock operation according to the second lock request, and returns information of the lock failure to the second transaction coordinator.
  • the second lock server After the second lock server receives the second lock request, it queries the lock information of the row where the third target data already exists, that is, there are other branch transactions operating on the changed row data, and the second lock server fails to lock , and send the lock failure message to the second transaction coordinator.
  • the second transaction coordinator returns a lock failure message to the second RM.
  • the second RM After receiving the lock failure message sent by the second transaction coordinator, the second RM returns a call failure message to the first RM.
  • the first RM After receiving the call failure message sent by the second RM, the first RM returns the call failure message to client A.
  • Client A sends a global rollback request to the first transaction coordinator.
  • the global rollback request includes the above-mentioned global transaction ID, which is used to indicate the data that the first transaction coordinator needs to perform the rollback operation.
  • client A After client A receives the call failure message sent by the first RM, client A determines that the execution of the distributed transaction fails, and sends a global rollback request to the first transaction coordinator.
  • the global rollback request instructs the database server that executes the branch transaction Perform a two-phase rollback operation.
  • the first transaction coordinator sends a first lock release request to the first lock server.
  • the first lock release request includes the first lock information to indicate the data that the first lock server needs to release the lock.
  • the first lock server performs a lock release operation, and sends a lock release success message to the first transaction coordinator.
  • the first lock server After receiving the first lock release request, the first lock server releases the lock added for the first branch transaction, and sends a lock release success message to the first transaction coordinator after the lock release is successful.
  • the first transaction coordinator returns to client A a message that the distributed transaction is completed.
  • the first transaction coordinator sends a two-phase rollback request to the second transaction coordinator.
  • the first transaction coordinator initiates a two-stage rollback request of the first branch transaction to the first RM.
  • the first RM receives the request, it performs a rollback operation, and restores the second target data to the first target according to the mirror data data, and then clears the first mirror data created for the first branch transaction.
  • the second transaction coordinator initiates a two-phase rollback request for the second branch transaction to the second RM. After receiving the request, the second RM clears the second mirror data created for the second branch transaction.
  • the database server executes the corresponding branch transaction, the branch transaction registration needs to be performed and the data of the branch transaction operation needs to be locked. Or when the lock operation is performed, the database server can register branch transactions and lock or release data through the transaction coordinator deployed in the same region, without the need to pass through the region where the client that initiated the distributed transaction is located.
  • the implementation of the transaction coordinator can reduce the mutual calls between devices located in different regions during the execution of distributed transactions.
  • a distributed transaction only involves the first service call in S11
  • FIG. 5 is a schematic diagram of a second transaction coordination device 500 provided by an embodiment of the present application.
  • the second transaction coordination device 500 includes: one or more processors 510, a communication interface 520, and a memory 530.
  • the processor 510 , the communication interface 520 and the memory 530 are connected to each other through the bus 540 .
  • the second transaction coordinator may be a software module running in the second transaction coordination device 500, or the second transaction coordinator is the second transaction coordination device 500, and the second transaction coordination device 500 can be used to implement the above diagram 3 or the operations performed by the second transaction coordinator in FIG. 4 will not be described in detail here.
  • the processor 510 may have various specific implementation forms.
  • the processor 510 may be a central processing unit (central processing unit, CPU) or a graphics processing unit (graphics processing unit, GPU), etc.
  • the processor 510 may also be Can be a single-core processor or a multi-core processor.
  • the processor 810 may be a combination of a CPU and a hardware chip.
  • the above-mentioned hardware chip may be an application-specific integrated circuit (ASIC), a programmable logic device (PLD) or a combination thereof.
  • the above-mentioned PLD may be a complex program logic device (CPLD), a field-programmable gate array (FPGA), a general array logic (generic array logic, GAL) or any combination thereof.
  • the processor 510 may also be independently implemented by a logic device with built-in processing logic, such as an FPGA or a digital signal processor (digital signal processor, DSP).
  • the communication interface 520 can be a wired interface or a wireless interface for communicating with other modules or devices, the wired interface can be an Ethernet interface, a local interconnect network (LIN), etc., and the wireless interface can be a cellular network interface or use Wireless LAN interface, etc.
  • the communication interface 520 in this embodiment of the present application may be used to perform operations such as receiving the second locking information sent by the second database server and returning the second branch transaction ID to the second database server as shown in FIG. 3 .
  • the memory 530 may be a non-volatile memory such as read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), Electrically Erasable Programmable Read Only Memory (electrically EPROM, EEPROM) or flash memory.
  • ROM read-only memory
  • PROM programmable read-only memory
  • EPROM erasable programmable read-only memory
  • EPROM erasable programmable read-only memory
  • Electrically Erasable Programmable Read Only Memory electrically Erasable Programmable Read Only Memory
  • EEPROM Electrically Erasable Programmable Read Only Memory
  • flash memory electrically Erasable Programmable Read Only Memory
  • Memory 830 may also be volatile memory, which may be random access memory (RAM), which acts as an external cache.
  • RAM random access memory
  • RAM static random access memory
  • DRAM dynamic random access memory
  • SDRAM synchronous DRAM
  • SDRAM double data rate synchronous dynamic random access memory
  • ESDRAM enhanced synchronous dynamic random access memory
  • SLDRAM synchronous link dynamic random access memory
  • direct rambus RAM direct rambus RAM
  • the memory 530 can be used to store program codes and data, so that the processor 510 can invoke the program codes stored in the memory 530 to execute the operation steps of the second transaction coordinator in the above method embodiments.
  • the second transaction coordination device 500 may include more or less components than those shown in FIG. 5 , or have a different configuration of the components.
  • the bus 540 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus, or the like.
  • PCI peripheral component interconnect
  • EISA extended industry standard architecture
  • the bus 540 can be divided into an address bus, a data bus, a control bus, and the like. For ease of presentation, only one thick line is used in FIG. 5, but it does not mean that there is only one bus or one type of bus.
  • the embodiment of the present application further provides a first transaction coordinator device, and the structure of the first transaction coordinator device is the same as that of the above-mentioned second transaction coordinator.
  • the first transaction coordinator may be a software module running in the first transaction coordination device, or the first transaction coordinator is the first transaction coordination device, and the first transaction coordination device can be used to implement the above-mentioned FIG. 3 or FIG. The operations performed by the first transaction coordinator in 4 will not be described in detail here.
  • the embodiment of the present application further provides a first database server, and the structure of the first database server is the same as that of the second transaction coordinator 500 described above.
  • the first database server can be used to implement the operations performed by the first database server in the foregoing FIG. 3 or FIG. 4 , which will not be described in detail here.
  • the embodiment of the present application further provides a second database server, and the structure of the second database server is the same as that of the second transaction coordinator 500 described above.
  • the second database server can be used to implement the operations performed by the second database server in the foregoing FIG. 3 or FIG. 4 , which will not be described in detail here.
  • the embodiment of the present application further provides a first lock server, and the structure of the first lock server is the same as that of the second transaction coordinator 500 described above.
  • the first lock server can be used to implement the operations performed by the first lock server in FIG. 3 or FIG. 4 , which will not be described in detail here.
  • the embodiment of the present application further provides a second lock server, and the structure of the second lock server is the same as that of the second transaction coordinator 500 described above.
  • the second lock server can be used to implement the operations performed by the second lock server in FIG. 3 or FIG. 4 , which will not be described in detail here.
  • the embodiment of the present application further provides a user terminal, and the structure of the user terminal is the same as that of the second transaction coordinator 500 described above.
  • the user terminal can be used to implement the operations performed by the user terminal A in the foregoing FIG. 3 or FIG. 4 , which will not be described in detail here.
  • the above embodiments may be implemented in whole or in part by software, hardware, firmware or any other combination.
  • the above-described embodiments may be implemented in whole or in part in the form of a computer program product.
  • the computer program product includes one or more computer program instructions. When the computer program instructions are loaded or executed on a computer, all or part of the processes or functions described in the embodiments of the present application are generated.
  • the computer may be a general purpose computer, special purpose computer, computer network, or other programmable device.
  • the computer program instructions may be stored on or transmitted from one computer readable storage medium to another computer readable storage medium, for example, the computer program instructions may be downloaded from a website, computer, server or A data center transmits to another website site, computer, server, or data center by wire (eg, coaxial cable, optical fiber, digital subscriber line) or wireless (eg, infrared, wireless, microwave, etc.).
  • the computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server, a data center, or the like that contains one or more sets of available media.
  • the usable media may be magnetic media (eg, floppy disks, hard disks, magnetic tapes), optical media, or semiconductor media, which may be solid state drives.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Databases & Information Systems (AREA)
  • Computing Systems (AREA)
  • Data Mining & Analysis (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

一种分布式事务实现方法及分布式系统。该分布式系统包括位于第一区域的客户端、第一事务协调器与第一数据库服务器,以及位于第二区域的第二事务协调器与第二数据库服务器,客户端在调用第一数据库服务器执行第一分支事务时,第一数据库服务器通过第一事务协调器对第一分支事务涉及的数据进行加锁操作,第一数据库服务器执行第一分支事务后,第一数据库服务器调用第二数据库服务器执行第二分支事务,第二数据库服务器执行第二分支事务时,通过第二事务协调器对第二分支事务涉及的数据进行加锁操作。执行上述分布式事务实现方法,能够减少不同区域间设备的相互调用,提高分布式事务处理效率。

Description

一种分布式事务实现方法及分布式系统
本申请要求于2021年03月30日提交中国专利局,申请号为202110343752.8,申请名称为“一种分布式事务实现方法及分布式系统”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。
技术领域
本申请涉及数据库技术领域,尤其涉及一种分布式事务实现方法及分布式系统。
背景技术
随着网络技术的快速发展,分布式系统逐渐普遍,分布式系统基于微服务架构,在不同的区域或者不同的节点部署多个服务,服务与服务之间可以通过远程协作完成一项事务操作。例如,当需要实现A银行的账户a向B银行的账户b转账100元转账操作时,需要调用第一服务将A银行的数据库中账户a的余额扣除100元,并调用第二服务将B银行数据库中账户b的余额增加100元。这种由分布式系统操作多个独立的数据库共同完成一个工作的事务称之为分布式事务,也称为全局事务。一个分布式事务包括多个分支事务,例如上述转账业务中调用第一服务需要进行的数据库操作是一个分支事务,调用第二服务需要进行的数据库操作是另一个分支事务。当分布式事务开始时,会首先选定一个事务协调器,该全局事务的所有分支事务的执行设备都会与该事务协调器进行交互,例如分支事务开始、分支事务结束、事务协调器与锁服务器之间的加锁与放锁请求等,当实现分支事务的服务实例与事务协调器不在一个区域(region)时,上述交互都需要跨区域调用,这样会增加网络时延,降低事务处理效率。
发明内容
本申请实施例公开了一种分布式事务实现方法及分布式系统,能够减少分布式事务执行过程中不同区域间设备的相互调用,提高分布式事务的处理效率。
第一方面,本申请实施例提供一种分布式事务实现方法,该方法应用于分布式系统,该分布式系统包括部署于第一区域的客户端、第一数据库服务器和第一事务协调器,以及部署于第二区域的第二数据库服务器和第二事务协调器,其中,第一数据库服务器用于运行第一服务,第二数据库服务器用于运行第二服务,该方法包括:
客户端向第一事务协调器发起分布式事务请求;
客户端根据第一事务协调器返回的分布式事务响应,调用第一数据库服务器执行第一分支事务,其中,第一分支事务为与第一服务相关的数据库事务;
第一数据库服务器在通过第一事务协调器完成第一分支事务后,第一数据库服务器调用第二数据库服务器执行第二分支事务,第二分支事务为与第二服务相关的数据库事务;
第二数据库服务器通过第二事务协调器执行第二分支事务,其中,执行第二分支事务包括通过第二事务协调器对第二分支事务涉及的数据进行加锁操作。
当分布式事务需要调用部署于不同区域的服务时,每个服务对应一个分支事务,当数据库服务器执行对应的分支事务,需要对分支事务操作的数据进行加锁时,数据库服务器会通过部署于同一区域的事务协调器进行数据的加锁等操作,而不需要通过发起分布式事务的客户端所在的区域的事务协调器实现加锁操作,能够减少分布式事务在执行过程中位于不同区 域的设备之间的互相调用,降低时延,提高分布式事务的执行效率。
在一种具体的实现方式中,上述第二数据库服务器通过第二事务协调器执行第二分支事务之前,还包括:第二数据库服务器确定与第一事务协调器位于不同的区域,且第二数据库服务器确定与第二事务协调器位于相同的区域。
在一种具体的实现方式中,上述分布式系统还包括部署于第二区域的第二锁服务器,第二数据库服务器通过第二事务协调器执行第二分支事务,包括:
第二数据库服务器向第二事务协调器发送第二分支事务的开始请求,其中,第二分支事务的开始请求包括第二分支事务涉及的数据在第二数据库中的位置信息;
第二事务协调器向第二锁服务器发送第二加锁请求,第二加锁请求包括第二分支事务涉及的数据在第二数据库中的位置信息;
第二锁服务器根据第二加锁请求对第二分支事务涉及的数据执行加锁操作,向第二事务协调器发送加锁结果;
第二事务协调器将加锁结果反馈给第二数据库服务器;
第二数据库服务器根据加锁结果执行第二分支事务,并将第二分支事务的执行结果通过第一数据库服务器反馈给客户端。
当数据库服务器执行对应的分支事务,需要向事务协调器注册分支事务时,数据库服务器会向部署于同一区域的事务协调器发送分支事务开始请求以注册分支事务,而不需要通过发起分布式事务的客户端所在区域的事务协调器实现注册,同时接收到分支事务开始请求的事务协调器通过同一区域的锁服务器实现加锁,不需要客户端所在区域的事务协调器调用执行分支事务的数据库服务器所在区域的所服务器,因此能够减少分布式事务在执行过程中位于不同区域的设备之间的互相调用,降低时延,提高分布式事务的执行效率。
在一种具体的实现方式中,第二数据库服务器根据加锁结果执行第二分支事务,并将第二分支事务的执行结果通过第一数据库服务器反馈给客户端后,客户端根据第二数据库服务器反馈的第二分支事务的执行结果,确定分布式事务的执行结果,其中,分布式事务的执行结果包括执行成功和执行失败。
在一种具体的实现方式中,客户端根据第二数据库服务器反馈的第二分支事务的执行结果,确定分布式事务的执行结果之后,包括:
客户端根据分布式事务的执行结果向第一事务协调器发送全局操作请求,其中,在分布式事务的执行结果为执行成功时,全局操作请求为全局事务提交请求,在分布式事务的执行结果为执行失败时,全局操作请求为全局事务回滚请求;
第一事务协调器根据全局操作请求,向第二事务协调器发送全局事务的二阶段操作请求,二阶段操作请求为二阶段提交请求或二阶段回滚请求,其中,在全局操作请求为全局事务提交请求时,二阶段操作请求为二阶段提交请求,在全局操作请求为全局事务回滚请求时,二阶段操作请求为二阶段回滚请求;
第二事务协调器调度第二数据库服务器执行二阶段操作请求。
在一种具体的实现方式中,第二数据库服务器根据加锁结果执行第二分支事务,并将第二分支事务的执行结果反馈给客户端,包括:在加锁结果为加锁成功且第二数据库服务器成功完成所述第二分支事务时,第二数据库服务器将第二分支事务执行成功的执行结果反馈给客户端;或者,在加锁结果为加锁失败或者第二数据库服务器没有完成第二分支事务时,第二数据库服务器将第二分支事务执行失败的执行结果反馈给客户端。
在一种具体的实现方式中,客户端根据第二数据库服务器反馈的第二分支事务的执行结 果,确定分布式事务的执行结果,包括:客户端在第二分支事务的执行结果为执行成功且分布式事务包括的其他分支事务的执行结果均为执行成功时,确定分布式事务的执行结果为执行成功;或者,在第二分支事务的执行结果为执行失败时,确定分布式事务的执行结果为执行失败。
在一种具体的实现方式中,在第二锁服务器对第二分支事务涉及的数据加锁成功时,第二事务协调器调度第二数据库服务器执行二阶段操作请求之前,还包括:第二事务协调器向所述第二锁服务器发送放锁请求;第二锁服务器释放对第二分支事务涉及的数据执行加锁操作时所加的锁,向第二事务协调器发送放锁成功的消息。
由于数据库服务器通过部署于同一区域的事务协调器进行数据的加锁操作,事务协调器通过同一区域的锁服务器实现加锁。因此在需要放锁时,事务协调器也不需要通过跨区域调用实现放锁操作,能够减少分布式事务在执行过程中位于不同区域的设备之间的互相调用,降低时延,提高分布式事务的执行效率。
在一种具体的实现方式中,第一数据库服务器调用第二数据库服务器执行第二分支事务之前,还包括:第一数据库服务器向第一事务协调器发送第一分支事务的开始请求;第一事务协调器根据第一分支事务的开始请求,创建第一分支事务标识,并将第一分支事务标识发送给第一数据库服务器;第一数据库服务器接收到第一分支事务标识后,成功执行第一分支事务。
在一种具体的实现方式中,分布式系统还包括第一锁服务器,第一事务协调器创建第一分支事务标识之前,还包括:
第一事务协调器接收所述第一数据库服务器发送的所述第一分支事务的开始请求,该第一分支事务开始请求包括第一分支事务涉及的数据在第一数据库中的位置信息;
第一事务协调器向第一锁服务器发送第一加锁请求,第一加锁请求包括第一分支事务涉及的数据在第一数据库中的位置信息;
第一锁服务器根据第一加锁请求对第一分支事务涉及的数据执行加锁操作,向第二事务协调器发送加锁成功的消息;
第一事务协调器接收第一锁服务器发送的加锁成功的消息。
第二方面,本申请实施例提供一种分布式系统,该分布式系统包括部署于第一区域的客户端、第一数据库服务器和第一事务协调器,以及部署于第二区域的第二数据库服务器和第二事务协调器,其中,第一数据库服务器用于运行第一服务,第二数据库服务器用于运行第二服务,其中,
客户端用于向第一事务协调器发起分布式事务请求;
根据第一事务协调器返回的分布式事务响应,调用第一数据库服务器执行第一分支事务,其中,第一分支事务为与第一服务相关的数据库事务;
第一数据库服务器用于在通过第一事务协调器完成第一分支事务后,第一数据库服务器调用第二数据库服务器执行第二分支事务,第二分支事务为与第二服务相关的数据库事务;
第二数据库服务器用于通过第二事务协调器执行第二分支事务,其中,执行第二分支事务包括通过第二事务协调器对第二分支事务涉及的数据进行加锁操作。
在一种具体的实现方式中,第二数据库服务器用于通过第二事务协调器执行第二分支事务之前,
第二数据库服务器还用于确定与第一事务协调器位于不同的区域,且第二数据库服务器确定与第二事务协调器位于相同的区域。
在一种具体的实现方式中,分布式系统还包括部署于第二区域的第二锁服务器,
第二数据库服务器通过第二事务协调器执行第二分支事务,包括:
第二数据库服务器,还用于向第二事务协调器发送第二分支事务的开始请求,第二分支事务的开始请求包括第二分支事务涉及的数据在第二数据库中的位置信息;
第二事务协调器,还用于向第二锁服务器发送第二加锁请求,第二加锁请求包括第二分支事务涉及的数据在第二数据库中的位置信息;
第二锁服务器,用于根据第二加锁请求对第二分支事务涉及的数据执行加锁操作,向第二事务协调器发送加锁结果;
第二事务协调器,还用于将加锁结果反馈给第二数据库服务器;
第二数据库服务器,还用于根据加锁结果执行第二分支事务,并将第二分支事务的执行结果反馈给客户端。
在一种具体的实现方式中,客户端还用于根据第二数据库服务器反馈的第二分支事务的执行结果,确定分布式事务的执行结果;
在一种具体的实现方式中,客户端,还用于根据分布式事务的执行结果向第一事务协调器发送全局操作请求,其中,在分布式事务的执行结果为执行成功时,全局操作请求为全局事务提交请求,在分布式事务的执行结果为执行失败时,全局操作请求为全局事务回滚请求;
第一事务协调器,还用于根据全局操作请求,向第二事务协调器发送全局事务的二阶段操作请求,二阶段操作请求为二阶段提交请求或二阶段回滚请求,其中,在全局操作请求为全局事务提交请求时,二阶段操作请求为二阶段提交请求,在全局操作请求为全局事务回滚请求时,二阶段操作请求为二阶段回滚请求;
第二事务协调器,还用于调度第二数据库服务器执行二阶段操作请求。
在一种具体的实现方式中,第二数据库服务器还用于根据加锁结果执行第二分支事务,并将第二分支事务的执行结果反馈给客户端,具体包括:
在加锁结果为加锁成功且第二数据库服务器成功完成第二分支事务时,第二数据库服务器将第二分支事务执行成功的执行结果反馈给客户端;或者,在加锁结果为加锁失败或者第二数据库服务器没有完成第二分支事务时,第二数据库服务器将第二分支事务执行失败的执行结果反馈给客户端。
在一种具体的实现方式中,客户端还用于根据第二数据库服务器反馈的第二分支事务的执行结果,确定分布式事务的执行结果,具体包括:客户端在第二分支事务的执行结果为执行成功且分布式事务包括的其他分支事务的执行结果均为执行成功时,确定分布式事务的执行结果为执行成功;或者,在第二分支事务的执行结果为执行失败时,确定分布式事务的执行结果为执行失败。
在一种具体的实现方式中,在第二锁服务器对第二分支事务涉及的数据加锁成功时,第二事务协调器还用于调度第二数据库服务器执行二阶段操作请求之前,第二事务协调器,还用于向第二锁服务器发送放锁请求;第二锁服务器,还用于释放对第二分支事务涉及的数据执行加锁操作时所加的锁,向第二事务协调器发送放锁成功的消息。
在一种具体的实现方式中,第一数据库服务器调用第二数据库服务器执行第二分支事务之前,还包括:
第一数据库服务器,还用于向第一事务协调器发送第一分支事务的开始请求;
第一事务协调器,还用于根据第一分支事务的开始请求,创建第一分支事务标识,并将第一分支事务标识发送给第一数据库服务器;
第一数据库服务器,还用于接收到第一分支事务标识后,成功执行第一分支事务。
在一种具体的实现方式中,分布式系统还包括第一锁服务器,
第一事务协调器用于创建第一分支事务标识之前,还包括:
第一事务协调器,还用于接收第一数据库服务器发送的第一分支事务的开始请求,第一分支事务开始请求包括第一分支事务涉及的数据在第一数据库中的位置信息;
向第一锁服务器发送第一加锁请求,第一加锁请求包括第一分支事务涉及的数据在第一数据库中的位置信息;
第一锁服务器,用于根据第一加锁请求对第一分支事务涉及的数据执行加锁操作,向第二事务协调器发送加锁成功的消息;
第一事务协调器,还用于接收第一锁服务器发送的加锁成功的消息。
附图说明
为了更清楚地说明本申请实施例技术方案,下面将对实施例描述中所需要使用的附图作简单地介绍,显而易见地,下面描述中的附图是本申请的一些实施例,对于本领域普通技术人员来讲,在不付出创造性劳动的前提下,还可以根据这些附图获得其他的附图。
图1是本申请实施例提供的分布式系统示意图;
图2是本申请实施例提供的一种分布式事务实现方法的交互示意图;
图3是本申请实施例提供的另一种分布式事务实现方法的交互示意图;
图4是本申请实施例提供的另一种分布式事务实现方法的交互示意图;
图5是本申请实施例提供的一种第二事务协调设备的结构示意图。
具体实施方式
下面结合附图对本申请实施例提供的分布式事务实现方法和分布式系统进行介绍。事务是指访问或更新数据库中的数据的一种操作任务,传统的集中式应用中,事务仅限于对单一数据库资源进行访问,这种事务称为本地事务。在基于微服务的分布式应用中,一个业务通常需要调用多个微服务,多个微服务调用通常对应多个数据库本地事务。当一个业务需要调用多个微服务实现多个数据库本地事务时,需要分布式事务保证这多个数据库本地事务的数据一致性。
分布式事务是指需要操作多个独立的数据库共同完成一个业务的事务。一个分布式事务可以包括多个分支事务,分布式事务的职责是协调其下管辖的分支事务达成一致,要么一起成功提交,要么一起失败回滚,通常,每个分支事务本身就是一个关系型数据库的本地事务。示例性的,用户通过终端发起一项转账业务,以实现A银行账户a向B银行的账户b转账100元。该业务需要分别对A银行的数据库与B银行的数据库中的数据进行操作,该业务需要调用第一服务完成从A银行的数据库的账户a中扣除100元的操作,调用第二服务完成向B银行的数据库的账户b中增加100元的操作。其中,调用第一服务需要进行的数据库操作是一个分支事务,调用第二服务需要进行的数据库操作是另一个分支事务。由于分布式事务通常是跨多个区域、或者跨多个数据库的全局事务,因此,本申请中分布式事务也称为全局事务,在本申请实施例的描述中,全局事务即表示一项分布式事务。
本申请实施例提供的分布式系统包括多个区域(region),每个区域包括一个或多个部署有客户端(client)的用户终端、一个或多个事务协调器(transaction coordinator,TC)、一个 或多个用于提供服务(例如微服务)的数据库服务器、一个或多个锁服务器以及一个或多个数据库。如图1所示,图1是一种分布式系统示意图,图1中分布式系统包括区域1和区域2,区域1包括用户终端A、第一数据库服务器、第一事务协调器、第一锁服务器和数据库A,用户终端A部署有客户端A,第一数据库服务器中部署有第一服务;区域2包括用户终端B、第二数据库服务器、第二事务协调器、第二锁服务器和数据库B,用户终端B部署有客户端B,第二数据库服务器中部署有第二服务。如果以区域1的客户端A向第一事务协调器发起分布式事务,该分布式事务包括第一分支事务和第二分支事务为例,在相关技术的实现方式中,该分布式事务的执行过程(以分布式事务成功提交为例)包括:
(1)客户端A向第一事务协调器发起分布式事务请求,第一事务协调器创建全局事务标识(identity,ID),并将全局事务ID返回给客户端A,其中,全局事务ID用于唯一的标识一个分布式事务,客户端中嵌有事务管理器(transaction manager,TM),TM负责发起分布式事务请求以开启一个分布式事务,并向事务协调器发起全局提交或全局回滚的指令,事务协调器用于维护分布式事务的运行状态,接收事务管理器的指令发起分布式事务的提交与回滚,负责与资源管理器通信以协调各分支事务的提交或回滚;
(2)客户端A调用位于区域1中部署于第一数据库服务器的第一服务,第一服务向第一事务协调器注册第一分支事务,其中,数据库服务器中嵌有资源管理器(resource manager,RM),用于负责分支注册、状态汇报,并接收事务协调器的指令,对数据库进行操作,驱动分支(本地)事务的提交和回滚;
(3)第一事务协调器确定第一分支事务的加锁数据属于第一锁服务器,向第一锁服务器发起加锁请求,第一锁服务器加锁成功之后,向第一事务协调器返回加锁成功的消息,其中,通过对第一分支事务涉及的数据进行加锁,以防止其他并发事务对该数据进行操作而产生冲突或错误;
(4)第一事务协调器创建第一分支事务ID并发送给第一服务的第一RM,第一RM接收到第一分支事务ID,对数据库A中第一分支事务需要操作的数据执行对应的操作之后,提交本地事务(即第一分支事务),并向第一事务协调器返回第一分支事务执行成功的消息;
(5)第一服务调用位于区域2的第二数据库服务器中的第二服务,第二服务的第二RM向第一事务协调器注册第二分支事务;
(6)第一事务协调器确定第二分支事务的加锁数据属于区域2的第二锁服务器,向第二锁服务器发起加锁请求,第二锁服务器加锁成功之后,向第一事务协调器返回加锁成功的消息;
(7)第一事务协调器创建第二分支事务ID并发送给第二服务的第二RM,第二RM接收到第二分支事务ID,对数据库B中第二分支事务需要操作的数据执行对应的操作之后,提交本地事务(即第二分支事务),并向第一事务协调器返回第二分支事务执行成功的消息;
(8)第二服务完成第二分支事务之后,向第一服务返回第二服务完成的消息,第一服务向客户端A返回第一服务和第二服务完成的消息,客户端确定调用完成之后,向事务协调器发起分布式事务提交请求;
(9)第一事务协调器接收到分布式事务提交请求之后,分别向第一锁服务器和第二锁服务器发送放锁请求,第一锁服务器和第二锁服务器向第一事务协调器发送放锁成功的消息;
(10)第一事务协调器接收到放锁成功的消息之后,分别向第一RM和第二RM发送二阶段提交请求,第一RM和第二RM完成二阶段提交之后,分别向事务协调器发送提交成功的消息。
上述分布式事务的执行过程中,区域1的设备与区域2的设备之间会涉及多次调用,包括:步骤(5)中第一服务调用第二服务,步骤(5)第二服务向第一事务协调器注册第二分支事务,步骤(6)中第一事务协调器向第二服务器发起加锁请求、步骤(7)中第二服务向第一事务协调器返回第二分支事务执行成功的消息、步骤(9)中第一事务协调器向第二锁服务器发送放锁请求以及步骤(10)第一事务协调器调用第二服务进行二阶段提交等,即一次分布式事务至少包括上述6次跨区域的调用。一个需要跨区域完成的分布式事务需要产生多次跨区调用,这样会增加网络时延,增加完成事务的时间,影响分布式事务的处理的效率。
基于上述问题,本申请实施例提供一种分布式事务实现方法,用以减少分布式事务实现过程中的跨区调用,提高分布式事务的处理效率。该方法应用于上述分布式系统,以图1所示的分布式系统实现包括第一分支事务与第二分支事务的分布式事务为例,介绍本申请实施例提供的分布式事务实现方法。如图2所示,图2是本申请实施例提供的一种实现分布式事务的交互示意图。该方法包括以下S201至S214。
S201.客户端A向第一事务协调器发送分布式事务请求。
用户通过区域1的用户终端A中的客户端A发起一个分布式事务,客户端A向区域1的第一事务协调器发送分布式事务请求,该分布式事务包括第一分支事务与第二分支事务。
S202.第一事务协调器接收到分布式事务请求后,向客户端A返回分布式事务响应。
第一事务协调器接收到分布式事务请求之后,创建全局事务ID并记录该全局事务ID,然后将包括该全局事务ID的分布式事务响应返回给用户终端A的客户端A。
S203.客户端A接收到分布式事务响应后,调用第一数据库服务器执行第一分支事务。
本申请实施例中,客户端A发起的分布式事务需要调用第一服务完成第一分支事务与第二服务完成第二分支事务,其中,第一分支事务是与第一服务相关的数据库事务,第一服务部署于区域1的第一数据库服务器;第二分支事务是与第二服务相关的数据库事务,第二服务部署于区域2的第二数据库服务器;第一分支事务需要操作的数据位于数据库A,第二分支事务需要操作的数据位于数据库B。
S204.第一数据库服务器向第一事务协调器发送第一分支开始请求。
第一数据库服务器在执行第一分支事务时,需要通过第一事务协调器执行第一分支事务,包括第一事务协调器对第一分支事务进行注册以及通过第一分支事务完成对第一分支事务涉及的数据的加锁操作,向第一事务协调器发送第一分支开始请求。
S205.第一事务协调器向第一锁服务器发送第一加锁请求。
其中,第一加锁请求指示对第一分支事务涉及的数据进行加锁操作。
S206.第一锁服务器实现对第一分支事务涉及的数据的加锁操作,向第一事务协调器返回加锁成功的消息。
S207.第一事务协调器向第一数据库服务器返回针对第一分支开始请求的响应消息,第一数据库服务器接收到响应消息后,成功执行第一分支事务。
S208.第一数据库服务器调用第二数据库服务器执行第二分支事务。
S209.第二数据库服务器向第二事务协调器发送第二分支开始请求。
第二数据库服务器在确定与第一事务协调器位于不同的区域,且第二数据库服务器确定与第二事务协调器位于相同的区域时,第二数据库服务器向第二事务协调器发送第二分支开始请求,第二数据库服务器在执行第二分支事务时,需要通过第二事务协调器执行第二分支事务,包括第二事务协调器对第二分支事务进行注册以及通过第二分支事务完成对第二分支事务涉及的数据的加锁操作。第二分支开始请求包括第二分支事务涉及的数据在第二数据库 中的位置信息。
S210.第二事务协调器向第二锁服务器发送第二加锁请求。
其中,第二加锁请求指示对第二分支事务涉及的数据进行加锁操作,第二加锁请求包括第二分支事务涉及的数据在第二数据库中的位置信息。
S211.第二锁服务器根据第二加锁请求实现对第二分支事务涉及的数据的加锁操作,向第二事务协调器返回加锁结果。
其中,加锁结果包括加锁成功和加锁失败。
S212.第二事务协调器将加锁结果反馈给第二数据库服务器。
S213.第二数据库服务器根据加锁结果执行所述二分支事务,并将第二分支事务的执行结果反馈给所述客户端。
其中,当加锁结果为加锁失败时,第二数据库服务器不能操作第二分支事务涉及的数据,第二分支事务的执行结果为执行失败;当加锁结果为加锁成功时,第二数据库服务器能操作第二分支事务涉及的数据,如果第二数据库服务器能够成功执行本地提交,则第二分支事务的执行结果为执行成功;如果第二数据库服务器不能成功执行本地提交,则第二分支事务的执行结果为执行失败。
在一种可能实现方式中,第二数据库服务器将第二分支事务的执行结果发送给第一数据库服务器,第一数据库服务器
S214.客户端根据第二数据库服务器反馈的第二分支事务的执行结果,确定分布式事务的执行结果。
客户端在第二分支事务的执行结果为执行成功且分布式事务包括的其他分支事务的执行结果均为执行成功时,确定分布式事务的执行结果为执行成功;在第二分支事务的执行结果为执行失败时,确定分布式事务的执行结果为执行失败。
下面结合图3和图4,对本申请实施例中的分布式事务实现方法进行详细介绍,如图3所示,图3是本申请实施例提供的另一种实现分布式事务的交互示意图。该方法包括S1至S30。
S1.客户端A向第一事务协调器发送分布式事务请求。
用户通过区域1的用户终端A中的客户端A发起一个分布式事务,客户端A的第一TM向区域1的第一事务协调器发送分布式事务请求。该分布式事务包括第一分支事务与第二分支事务,需要调用第一服务完成第一分支事务与第二服务完成第二分支事务,其中,第一服务部署于区域1的第一数据库服务器,第二服务部署于区域2的第二数据库服务器,第一分支事务需要操作的数据位于数据库A,第二分支事务需要操作的数据位于数据库B。
S2.第一事务协调器根据分布式事务请求创建全局事务ID,并发送给客户端A。
第一事务协调器接收到分布式事务请求之后,创建全局事务ID并记录该全局事务ID,然后将该全局事务ID返回给用户终端A的客户端A,其中,全局事务ID用于唯一的标识一个分布式事务。
S3.客户端A根据全局事务ID构建该分布式事务对应的事务上下文并发送给第一RM。
客户端A获取到全局事务ID后构建分布式事务对应的事务上下文,并调用第一数据库服务器中部署的第一服务,将事务上下文发送给第一数据库服务器。其中,事务上下文是在资源上的事务性操作和调用操作的构件之间的一个关联(association)。在一个事务执行期间,所有的参与事务的线程共享事务上下文。事务上下文在逻辑上封装了在一个事务期间在事务性资源上的完成的所有操作。事务上下文包括全局事务ID与第一事务协调器的地址信息,其 中,全局事务ID指示第一数据库服务器当前执行的分支事务所属于的分布式事务,地址信息用于指示第一数据库服务器在执行全局事务ID对应的事务时,与第一事务协调器进行交互。
S4.第一RM根据事务上下文构建第一镜像数据与第一加锁信息,向第一事务协调器发送第一分支开始请求与第一加锁信息。
其中,第一镜像数据包括第一前镜像记录与第二后镜像记录,第一服务的第一RM获取到事务上下文后,第一RM对第一服务执行的结构化查询语言(structured query language,SQL)进行解析,根据客户端或者调用第一服务的输入参数构建第一事务分支对应的第一前镜像记录,然后执行SQL,根据执行结果构建第一后镜像记录,并构建事务第一加锁信息。其中,输入参数包括第一分支事务需要操作的数据的信息,例如分布式事务为账户a向账户b的转账操作,第一分支事务是将账户a中的余额扣减100元,则第一服务为转账服务,输入参数包括账户a的ID、账户b的ID以及转出的金额等;第一前镜像记录中记录有执行SQL前第一分支事务操作的第一目标数据,例如账户a在转账前的金额;第一后镜像记录中记录有执行SQL语句对第一目标数据进行修改之后得到的第二目标数据,例如账户a在转账前的金额;第一加锁信息包括第一目标数据所在的数据库的名称、第一目标数据所在的表的名称以及第一目标数据所在的行的主键。
第一RM构建加锁信息之后,根据第一事务协调器的地址信息确定第一事务协调器与第一数据库服务器位于相同的区域,向第一事务协调器发送第一分支开始请求,该第一分支开始请求包括全局事务ID和第一加锁信息,其中,全局事务ID用于第一事务协调器将第一分支事务与分布式事务进行关联,第一加锁信息指示第一分支事务需要加锁的数据。
S5.第一事务协调器接收到第一加锁信息之后,向第一锁服务器发送第一加锁请求。
第一事务协调器接收到第一加锁信息后,确定第一加锁信息中指示加锁的数据属于第一锁服务器管理,第一事务协调器向第一锁服务器发送第一加锁请求,该第一加锁请求包括第一加锁信息,用于指示对第一加锁信息中第一目标数据所在的数据行进行加锁。
S6.第一锁服务器根据第一加锁请求执行加锁操作,并返回加锁成功的信息给第一事务协调器。
S7.第一事务协调器创建第一分支事务ID,并将第一分支事务ID发送给第一RM。
第一事务协调器接收到加锁成功的信息后,创建第一分支事务对应的第一分支事务ID,将全局事务ID与第一分支事务ID进行关联,并将第一分支事务ID发送给第一数据库服务器,第一分支事务ID用于唯一的标识第一分支事务。
S8.第一RM执行第一分支事务的本地提交。
第一服务的第一RM在接收到上述第一分支事务ID后,进行本地提交,即执行第一分支事务的提交。
S9.第一RM向第一事务协调器发送第一分支事务提交成功的消息。
其中,第一分支事务提交成功的消息包括第一分支事务的第一分支事务ID。
S10.第一事务协调器记录第一分支事务提交成功,并返回确认消息通知第一RM其收到第一分支事务提交成功的消息。
S11.第一服务调用第二服务,第一RM将上述事务上下文发送给第二服务的第二RM。
第一服务调用第二服务,第一数据库服务器通过第一RM将事务上下文发送给第二数据库服务器。
S12.第二RM根据事务上下文构建第二镜像数据与第二加锁信息,向第二事务协调器发送第二分支开始请求与第二加锁信息。
其中,第二镜像数据包括第二前镜像记录与第二后镜像记录,第二服务的第二RM获取到事务上下文后,第二RM对第二服务执行的结构化查询语言进行解析,根据第一服务的输入参数构建第二事务分支对应的第二前镜像记录,然后执行SQL,根据执行结果构建第二后镜像记录,并构建事务第二加锁信息。其中,第一服务的输入参数包括第二分支事务需要操作的数据的信息,例如上述账户a向账户b的转账操作,第二分支事务是将账户b中的余额增加100元,则第二服务为入账服务,第一服务的输入参数包括账户b的ID与转入的金额等;第二前镜像记录记录有执行SQL前第二分支事务操作的第三目标数据,例如转账之前账户b中的金额;第二后镜像记录记录有执行SQL语句对第三目标数据进行修改之后得到的第四目标数据,例如转账之后账户b中的金额;第二加锁信息包括第三目标数据所在的数据库的名称、第三目标数据所在的表的名称以及第三目标数据所在的行的主键。
第二RM构建第二加锁信息之后,根据第一事务协调器的地址信息确定第一事务协调器与第二数据库服务器位于不同的区域,第二RM根据配置信息确定第二事务协调器是第一事务协调器在区域2中关联的事务协调器,第二RM向第二事务协调器发送第二分支开始请求,该第二分支开始请求包括全局事务ID和第二加锁信息,其中,全局事务ID用于第二事务协调器将第二分支事务与分布式事务进行关联,第二加锁信息指示第二分支事务需要加锁的数据。
S13.第二事务协调器接收到第二加锁信息之后,向第二锁服务器发送第二加锁请求。
第二事务协调器接收到第二加锁信息后,确定第二加锁信息中指示加锁的数据属于第二锁服务器管理,第二事务协调器向第二锁服务器发送第二加锁请求,该第二加锁请求包括第二加锁信息,用于指示对第二加锁信息中第三目标数据所在的数据行进行加锁。
应理解,分布式事务的执行结果包括全局事务提交与全局事务回滚,客户端A需要根据分布式事务所包括的所有分支事务的执行结果确定分布式事务最终是执行全局事务提交还是执行全局事务回滚。在分布式事务所包括的所有分支事务的执行结果均是执行成功(本地提交成功)的情况下,客户端A确定执行全局事务提交;在分布式事务所包括的任一分支事务的执行结果是执行失败的情况下,客户端A确定执行全局事务回滚。上述S1至S12中,第一分支事务执行结果为执行成功,下面以第一服务调用第二服务执行第二分支事务为例,分别介绍第二分支事务执行成功和第二分支事务执行失败两种情况下,分布式事务实现方法的执行过程。
下面首先以第二分支事务执行成功为前提,介绍分布式事务实现方法。第二RM向第二事务协调器发送第二分支事务的开始请求与第二加锁信息后,上述分布式事务实现方法还包括以下S14至S30。
S14.第二锁服务器根据第二加锁请求执行加锁操作,并返回加锁成功的信息给第二事务协调器。
S15.第二事务协调器创建第二分支事务ID,并将第二分支事务ID发送给第二RM。
第二事务协调器接收到加锁成功的信息后,创建第二分支事务对应的第二分支事务ID,将全局事务ID与第二分支事务ID进行关联,并将第二分支事务ID发送给第二RM。
S16.第二RM执行第二分支事务的本地提交。
第二服务的第二RM在接收到上述第二分支事务ID后,提交本地事务,即执行第二分支事务的提交。
S17.第二RM向第二事务协调器发送第二分支事务提交成功的消息。
其中,第二分支事务提交成功的消息包括第二分支事务的第二分支事务ID。
S18.第二事务协调器记录第二分支事务提交成功,并返回确认消息通知第二RM其收到第二分支事务提交成功的消息。
S19.第二RM接收到第二事务协调器发送的确认消息之后,向第一RM返回调用成功的消息。
S20.第一RM接收到第二RM发送的调用成功的消息后,向客户端A返回调用成功的消息。
S21.客户端A向第一事务协调器发送全局提交请求。
其中,全局提交请求包括上述全局事务ID,客户端A在接收到第一RM发送的调用成功的消息后,确定分布式事务的所有分支事务执行成功,客户端A确定分布式事务执行成功,向第一事务协调器发送全局提交请求,全局提交请求指示执行分支事务的数据库服务器执行二阶段提交操作。
S22.第一事务协调器向第一锁服务器发送第一放锁请求。
第一事务协调器接收到全局提交请求后,根据全局事务ID关联的第一分支事务ID,确定需要对第一分支事务所加的锁执行放锁操作,第一事务协调器向第一锁服务器发送第一放锁请求。其中,第一放锁请求包括上述第一加锁信息,以指示第一锁服务器需要放锁的数据。
S23.第一锁服务器执行放锁操作,将放锁成功的消息发送给第一事务协调器。
第一锁服务器接收到第一放锁请求后,释放为第一分支事务所加的锁,放锁成功后将放锁成功的消息发送给第一事务协调器。
S24.第一事务协调器向第二事务协调器发送分布式事务的二阶段提交请求。
其中,二阶段提交请求包括全局事务ID和第二分支事务ID,用于指示需要提交的分支事务。
S25.第二事务协调器向第二锁服务器发送第二放锁请求。
其中,上述第二放锁请求包括上述第二加锁信息,以指示第二锁服务器需要放锁的数据。
S26.第二锁服务器执行放锁操作,将放锁成功的消息发送给第二事务协调器。
第二锁服务器接收到第二放锁请求后,释放为第二分支事务所加的锁,第二锁服务器放锁成功后将放锁成功的消息发送给第二事务协调器。
S27.第二事务协调器向第一事务协调器返回二阶段提交完成的消息。
S28.第一事务协调器向客户端A返回分布式事务完成的消息。
需要说明的是,第一锁服务器和第二锁服务器放锁成功,第一事务协调器向客户端A返回分布式事务完成的消息后,对于客户端A,分布式事务已经完成。
S29.第一事务协调器向第一RM发起第一分支事务的二阶段提交请求,第一RM接收到请求之后,清除为第一分支事务创建的第一镜像数据,并向第一事务协调器返回二阶段提交完成的消息。
S30.第二事务协调器向第二RM发起第二分支事务的二阶段提交请求,第二RM接收到请求之后,清除为第二分支事务创建的第二镜像数据,并向第二事务协调器返回二阶段提交完成的消息。
上述是以分布式事务最终能够成功提交为例,介绍了本申请实施例提供的分布式事务实现方法。应理解的是,如果分布式事务包括的任意分支事务出现异常,例如第二锁服务器加锁失败、第二RM本地提交失败等,分布式事务会执行全局事务回滚操作。如图4所示,图4是本申请实施例提供的另一种实现分布式事务的交互示意图。以第二锁服务器加锁失败为例,该分布式事务实现方法的执行过程中,在上述S14之前的过程如上述S1至S13中所述, 在第二事务协调器接收到第二加锁信息之后,向第二锁服务器发送第二加锁请求之后,该分布式事务的执行过程还包括S14至S24。
S14’.第二锁服务器根据第二加锁请求执行加锁操作,并返回加锁失败的信息给第二事务协调器。
第二锁服务器在接收到第二加锁请求之后,查询到已存在第三目标数据所在的行的加锁信息,即有其他分支事务在对改行数据进行操作,则第二锁服务器加锁失败,将加锁失败的消息发送给第二事务协调器。
S15’.第二事务协调器向第二RM返回加锁失败的消息。
S16’.第二RM接收到第二事务协调器发送的加锁失败的消息之后,向第一RM返回调用失败的消息。
S17’.第一RM接收到第二RM发送的调用失败的消息后,向客户端A返回调用失败的消息。
S18’.客户端A向第一事务协调器发送全局回滚请求。
其中,全局回滚请求包括上述全局事务ID,用于指示第一事务协调器需要执行回滚操作的数据。客户端A在接收到第一RM发送的调用失败的消息后,客户端A确定分布式事务执行失败,向第一事务协调器发送全局回滚请求,全局回滚请求指示执行分支事务的数据库服务器执行二阶段回滚操作。
S19’.第一事务协调器向第一锁服务器发送第一放锁请求。
其中,所述第一放锁请求包括所述第一加锁信息,以指示第一锁服务器需要放锁的数据。
S20’.第一锁服务器执行放锁操作,将放锁成功的消息发送给第一事务协调器。
第一锁服务器接收到第一放锁请求后,释放为第一分支事务所加的锁,放锁成功后将放锁成功的消息发送给第一事务协调器。
S21’.第一事务协调器向客户端A返回分布式事务完成的消息。
S22’.第一事务协调器向第二事务协调器发送二阶段回滚请求。
S23’.第一事务协调器向第一RM发起第一分支事务的二阶段回滚请求,第一RM接收到请求之后,执行回滚操作,根据镜像数据将第二目标数据还原为第一目标数据,然后清除为第一分支事务创建的第一镜像数据。
S24’.第二事务协调器向第二RM发起第二分支事务的二阶段回滚请求,第二RM接收到请求之后,清除为第二分支事务创建的第二镜像数据。
通过上述对本申请实施例提供的分布式事务实现方法的描述以及图3和图4能够看出,当数据库服务器执行对应的分支事务,需要进行分支事务注册、对分支事务操作的数据进行加锁操作或者放锁操作时,数据库服务器能够通过部署于同一区域的事务协调器实现分支事务的注册以及对数据的加锁或放锁等操作,而不需要通过发起分布式事务的客户端所在的区域的事务协调器实现,能够减少分布式事务在执行过程中位于不同区域的设备之间的互相调用,以上述分布式事务执行结果为执行成功为例,一次分布式事务仅涉及S11中第一服务调用第二服务以及S241中第一事务协调器向第二事务协调器发送分布式事务的二阶段提交请求这两次跨区域的调用。因此执行本申请实施例提供的分布式事务实现方法,能够减少分布式事务执行过程中的网络时延,提升分布式事务处理效率,降低一个分布式事务的事务锁的持有时间,能够降低高并发场景下出现数据冲突的概率。
需要说明的是,对于上述方法实施例,为了简单描述,故将其都表述为一系列的动作组合,但是本领域技术人员应该知悉,本发明并不受所描述的动作顺序的限制,其次,本领域 技术人员也应该知悉,说明书中所描述的实施例均属于优选实施例,所涉及的动作并不一定是本发明所必须的。
本领域的技术人员根据以上描述的内容,能够想到的其他合理的步骤组合,也属于本发明的保护范围内。其次,本领域技术人员也应该熟悉,说明书中所描述的实施例均属于优选实施例,所涉及的动作并不一定是本发明所必须的。
上文中结合图1至图4详细介绍了本申请实施例所提供的分布式系统以及分布式事务实现方法,下面介绍本申请实施例所提供的计算设备。
参见图5,图5是本申请实施例提供的一种第二事务协调设备500的示意图,第二事务协调设备500包括:一个或者多个处理器510、通信接口520以及存储器530,处理器510、通信接口520以及存储器530通过总线540相互连接。其中,上述第二事务协调器可以是运行在第二事务协调设备500中的软件模块,或者第二事务协调器即为第二事务协调设备500,第二事务协调设备500能够用于实现上述图3或者图4中关于第二事务协调器所执行的操作,在此不再详细介绍。
本申请实施例中,处理器510可以有多种具体实现形式,例如处理器510可以是中央处理器(central processing unit,CPU)或图像处理器(graphics processing unit,GPU)等,处理器510还可以是单核处理器或多核处理器。处理器810可以由CPU和硬件芯片的组合。上述硬件芯片可以是专用集成电路(application-specific integrated circuit,ASIC)、可编程逻辑器件(programmable logic device,PLD)或其组合。上述PLD可以是复杂程序逻辑器件(complex programmable logical device,CPLD),现场可编程门阵列(field-programmable gate array,FPGA),通用阵列逻辑(generic array logic,GAL)或其任意组合。处理器510也可以单独采用内置处理逻辑的逻辑器件来实现,例如FPGA或数字信号处理器(digital signal processor,DSP)等。
通信接口520可以为有线接口或无线接口,用于与其他模块或设备进行通信,有线接口可以是以太接口、局域互联网络(local interconnect network,LIN)等,无线接口可以是蜂窝网络接口或使用无线局域网接口等。例如,本申请实施例中通信接口520可用于执行上述图3中接收第二数据库服务器发送的第二加锁信息、向第二数据库服务器返回第二分支事务ID等操作。
存储器530可以是非易失性存储器,例如,只读存储器(read-only memory,ROM)、可编程只读存储器(programmable ROM,PROM)、可擦除可编程只读存储器(erasable PROM,EPROM)、电可擦除可编程只读存储器(electrically EPROM,EEPROM)或闪存。存储器830也可以是易失性存储器,易失性存储器可以是随机存取存储器(random access memory,RAM),其用作外部高速缓存。通过示例性但不是限制性说明,许多形式的RAM可用,例如静态随机存取存储器(static RAM,SRAM)、动态随机存取存储器(dynamic RAM,DRAM)、同步动态随机存取存储器(synchronous DRAM,SDRAM)、双倍数据速率同步动态随机存取存储器(double data rate SDRAM,DDR SDRAM)、增强型同步动态随机存取存储器(enhanced SDRAM,ESDRAM)、同步连接动态随机存取存储器(synchlink DRAM,SLDRAM)和直接内存总线随机存取存储器(direct rambus RAM,DRRAM)。
存储器530可用于存储程序代码和数据,以便于处理器510调用存储器530中存储的程序代码执行上述方法实施例中第二事务协调器的操作步骤。此外,第二事务协调设备500可能包含相比于图5展示的更多或者更少的组件,或者有不同的组件配置方式。
总线540可以是外设部件互连标准(peripheral component interconnect,PCI)总线或扩展 工业标准结构(extended industry standard architecture,EISA)总线等。所述总线540可以分为地址总线、数据总线、控制总线等。为便于表示,图5中仅用一条粗线表示,但并不表示仅有一根总线或一种类型的总线。
具体地,上述第二事务协调设备500执行各种操作的具体实现可参照上述方法实施例中第二事务协调器的具体操作,在此不再赘述。
本申请实施例还提供一种第一事务协调设备,该第一事务协调器设备的结构与上述第二事务协调器的结构相同。其中,上述第一事务协调器可以是运行在第一事务协调设备中的软件模块,或者第一事务协调器即为第一事务协调设备,第一事务协调设备能够用于实现上述图3或者图4中关于第一事务协调器所执行的操作,在此不再详细介绍。
本申请实施例还提供一种第一数据库服务器,该第一数据库服务器的结构与上述第二事务协调器500的结构相同。其中,第一数据库服务器能够用于实现上述图3或者图4中关于第一数据库服务器所执行的操作,在此不再详细介绍。
本申请实施例还提供一种第二数据库服务器,该第二数据库服务器的结构与上述第二事务协调器500的结构相同。其中,第二数据库服务器能够用于实现上述图3或者图4中关于第二数据库服务器所执行的操作,在此不再详细介绍。
本申请实施例还提供一种第一锁服务器,该第一锁服务器的结构与上述第二事务协调器500的结构相同。其中,第一锁服务器能够用于实现上述图3或者图4中关于第一锁服务器所执行的操作,在此不再详细介绍。
本申请实施例还提供一种第二锁服务器,该第二锁服务器的结构与上述第二事务协调器500的结构相同。其中,第二锁服务器能够用于实现上述图3或者图4中关于第二锁服务器所执行的操作,在此不再详细介绍。
本申请实施例还提供一种用户终端,该用户终端的结构与上述第二事务协调器500的结构相同。其中,用户终端能够用于实现上述图3或者图4中关于用户终端A所执行的操作,在此不再详细介绍。
在上述实施例中,对各个实施例的描述都各有侧重,某个实施例中没有详述的部分,可以参见其它实施例的相关描述。
上述实施例,可以全部或部分地通过软件、硬件、固件或其他任意组合来实现。当使用软件实现时,上述实施例可以全部或部分地以计算机程序产品的形式实现。所述计算机程序产品包括一个或多个计算机程序指令。在计算机上加载或执行所述计算机程序指令时,全部或部分地产生按照本申请实施例所述的流程或功能。所述计算机可以为通用计算机、专用计算机、计算机网络、或者其他可编程装置。所述计算机程序指令可以存储在计算机可读存储介质中,或者从一个计算机可读存储介质向另一个计算机可读存储介质传输,例如,所述计算机程序指令可以从一个网站站点、计算机、服务器或数据中心通过有线(例如同轴电缆、光纤、数字用户线)或无线(例如红外、无线、微波等)方式向另一个网站站点、计算机、服务器或数据中心进行传输。所述计算机可读存储介质可以是计算机能够存取的任何可用介质或者是包含一个或多个可用介质集合的服务器、数据中心等数据存储设备。所述可用介质可以是磁性介质(例如,软盘、硬盘、磁带)、光介质、或者半导体介质,半导体介质可以是固态硬盘。
以上所述,仅为本申请的具体实施方式。熟悉本技术领域的技术人员根据本申请提供的具体实施方式,可想到变化或替换,都应涵盖在本申请的保护范围之内。

Claims (20)

  1. 一种分布式事务实现方法,其特征在于,所述方法应用于分布式系统,所述分布式系统包括部署于第一区域的客户端、第一数据库服务器和第一事务协调器,还包括部署于第二区域的第二数据库服务器和第二事务协调器,其中,所述第一数据库服务器用于运行第一服务,所述第二数据库服务器用于运行第二服务,所述方法包括:
    所述客户端向所述第一事务协调器发起分布式事务请求;
    所述客户端根据所述第一事务协调器返回的分布式事务响应,调用所述第一数据库服务器执行第一分支事务,其中,所述第一分支事务为与所述第一服务相关的数据库事务;
    所述第一数据库服务器在通过所述第一事务协调器完成所述第一分支事务后,所述第一数据库服务器调用所述第二数据库服务器执行第二分支事务,所述第二分支事务为与所述第二服务相关的数据库事务;
    所述第二数据库服务器通过所述第二事务协调器执行所述第二分支事务,其中,所述执行所述第二分支事务包括通过所述第二事务协调器对所述第二分支事务涉及的数据进行加锁操作。
  2. 根据权利要求1所述的方法,其特征在于,所述第二数据库服务器通过所述第二事务协调器执行所述第二分支事务之前,还包括:
    所述第二数据库服务器确定与所述第一事务协调器位于不同的区域,且所述第二数据库服务器确定与所述第二事务协调器位于相同的区域。
  3. 根据权利要求1或2所述的方法,其特征在于,所述分布式系统还包括部署于所述第二区域的第二锁服务器,
    所述第二数据库服务器通过所述第二事务协调器执行所述第二分支事务,包括:
    所述第二数据库服务器向所述第二事务协调器发送第二分支事务的开始请求,所述第二分支事务的开始请求包括所述第二分支事务涉及的数据在第二数据库中的位置信息;
    所述第二事务协调器向所述第二锁服务器发送第二加锁请求,所述第二加锁请求包括所述第二分支事务涉及的数据在第二数据库中的位置信息;
    所述第二锁服务器根据所述第二加锁请求对所述第二分支事务涉及的数据执行加锁操作,向所述第二事务协调器发送加锁结果;
    所述第二事务协调器将所述加锁结果反馈给所述第二数据库服务器;
    所述第二数据库服务器根据所述加锁结果执行所述第二分支事务,并将所述第二分支事务的执行结果反馈给所述客户端。
  4. 根据权利要求3所述的方法,其特征在于,所述方法还包括:
    所述客户端根据所述第二数据库服务器反馈的第二分支事务的执行结果,确定分布式事务的执行结果。
  5. 根据权利要求4所述的方法,其特征在于,所述客户端根据所述第二数据库服务器反馈的第二分支事务的执行结果,确定分布式事务的执行结果之后,还包括:
    所述客户端根据所述分布式事务的执行结果向所述第一事务协调器发送全局操作请求,其中,在所述分布式事务的执行结果为执行成功时,所述全局操作请求为全局事务提交请求, 在所述分布式事务的执行结果为执行失败时,所述全局操作请求为全局事务回滚请求;
    所述第一事务协调器根据所述全局操作请求,向所述第二事务协调器发送所述全局事务的二阶段操作请求,所述二阶段操作请求为二阶段提交请求或二阶段回滚请求,其中,在所述全局操作请求为全局事务提交请求时,所述二阶段操作请求为二阶段提交请求,在所述全局操作请求为全局事务回滚请求时,所述二阶段操作请求为二阶段回滚请求;
    所述第二事务协调器调度所述第二数据库服务器执行所述二阶段操作请求。
  6. 根据权利要求4所述的方法,其特征在于,所述第二数据库服务器根据所述加锁结果执行所述第二分支事务,并将所述第二分支事务的执行结果反馈给所述客户端,包括:
    在所述加锁结果为加锁成功且所述第二数据库服务器成功完成所述第二分支事务时,所述第二数据库服务器将所述第二分支事务执行成功的执行结果反馈给所述客户端;
    或者,
    在所述加锁结果为加锁失败或者所述第二数据库服务器没有完成所述第二分支事务时,所述第二数据库服务器将所述第二分支事务执行失败的执行结果反馈给所述客户端。
  7. 根据权利要求6所述的方法,其特征在于,所述客户端根据所述第二数据库服务器反馈的第二分支事务的执行结果,确定分布式事务的执行结果,包括:
    所述客户端在所述第二分支事务的执行结果为执行成功且所述分布式事务包括的其他分支事务的执行结果均为执行成功时,确定所述分布式事务的执行结果为执行成功;或者,在所述第二分支事务的执行结果为执行失败时,确定所述分布式事务的执行结果为执行失败。
  8. 根据权利要求5所述的方法,其特征在于,在所述第二锁服务器对所述第二分支事务涉及的数据加锁成功时,
    所述第二事务协调器调度所述第二数据库服务器执行所述二阶段操作请求之前,还包括:
    所述第二事务协调器向所述第二锁服务器发送放锁请求;
    所述第二锁服务器释放对所述第二分支事务涉及的数据执行加锁操作时所加的锁,向所述第二事务协调器发送放锁成功的消息。
  9. 根据权利要求1至7任一项所述的方法,其特征在于,所述第一数据库服务器调用所述第二数据库服务器执行所述第二分支事务之前,还包括:
    所述第一数据库服务器向所述第一事务协调器发送第一分支事务的开始请求;
    所述第一事务协调器根据所述第一分支事务的开始请求,创建第一分支事务标识,并将所述第一分支事务标识发送给所述第一数据库服务器;
    所述第一数据库服务器接收到所述第一分支事务标识后,成功执行所述第一分支事务。
  10. 根据权利要求8所述的方法,其特征在于,所述分布式系统还包括第一锁服务器,
    所述第一事务协调器创建所述第一分支事务标识之前,还包括:
    所述第一事务协调器接收所述第一数据库服务器发送的所述第一分支事务的开始请求,所述第一分支事务开始请求包括所述第一分支事务涉及的数据在第一数据库中的位置信息;
    所述第一事务协调器向所述第一锁服务器发送第一加锁请求,所述第一加锁请求包括所述第一分支事务涉及的数据在第一数据库中的位置信息;
    所述第一锁服务器根据所述第一加锁请求对所述第一分支事务涉及的数据执行加锁操作,向所述第二事务协调器发送加锁成功的消息;
    所述第一事务协调器接收所述第一锁服务器发送的加锁成功的消息。
  11. 一种分布式系统,其特征在于,所述分布式系统包括部署于第一区域的客户端、第一数据库服务器和第一事务协调器,还包括部署于第二区域的第二数据库服务器和第二事务协调器,其中,所述第一数据库服务器用于运行第一服务,所述第二数据库服务器用于运行第二服务,
    所述客户端,用于向所述第一事务协调器发起分布式事务请求,根据所述第一事务协调器返回的分布式事务响应,调用所述第一数据库服务器执行第一分支事务,其中,所述第一分支事务为与所述第一服务相关的数据库事务;
    所述第一数据库服务器,用于在通过所述第一事务协调器完成所述第一分支事务后,调用所述第二数据库服务器执行第二分支事务,所述第二分支事务为与所述第二服务相关的数据库事务;
    所述第二数据库服务器,用于通过所述第二事务协调器执行所述第二分支事务,其中,所述执行所述第二分支事务包括通过所述第二事务协调器对所述第二分支事务涉及的数据进行加锁操作。
  12. 根据权利要求11所述的系统,其特征在于,所述第二数据库服务器用于通过所述第二事务协调器执行所述第二分支事务之前,
    所述第二数据库服务器还用于确定与所述第一事务协调器位于不同的区域,且所述第二数据库服务器确定与所述第二事务协调器位于相同的区域。
  13. 根据权利要求11或12所述的系统,其特征在于,所述分布式系统还包括部署于所述第二区域的第二锁服务器,
    所述第二数据库服务器通过所述第二事务协调器执行所述第二分支事务,包括:
    所述第二数据库服务器,还用于向所述第二事务协调器发送第二分支事务的开始请求,所述第二分支事务的开始请求包括所述第二分支事务涉及的数据在第二数据库中的位置信息;
    所述第二事务协调器,还用于向所述第二锁服务器发送第二加锁请求,所述第二加锁请求包括所述第二分支事务涉及的数据在第二数据库中的位置信息;
    所述第二锁服务器,用于根据所述第二加锁请求对所述第二分支事务涉及的数据执行加锁操作,向所述第二事务协调器发送加锁结果;
    所述第二事务协调器,还用于将所述加锁结果反馈给所述第二数据库服务器;
    所述第二数据库服务器,还用于根据所述加锁结果执行所述第二分支事务,并将所述第二分支事务的执行结果反馈给所述客户端。
  14. 根据权利要求13所述的系统,其特征在于,
    所述客户端还用于根据所述第二数据库服务器反馈的第二分支事务的执行结果,确定分布式事务的执行结果。
  15. 根据权利要求14所述的系统,其特征在于,
    所述客户端,还用于根据所述分布式事务的执行结果向所述第一事务协调器发送全局操作请求,其中,在所述分布式事务的执行结果为执行成功时,所述全局操作请求为全局事务提交请求,在所述分布式事务的执行结果为执行失败时,所述全局操作请求为全局事务回滚请求;
    所述第一事务协调器,还用于根据所述全局操作请求,向所述第二事务协调器发送所述全局事务的二阶段操作请求,所述二阶段操作请求为二阶段提交请求或二阶段回滚请求,其中,在所述全局操作请求为全局事务提交请求时,所述二阶段操作请求为二阶段提交请求,在所述全局操作请求为全局事务回滚请求时,所述二阶段操作请求为二阶段回滚请求;
    所述第二事务协调器,还用于调度所述第二数据库服务器执行所述二阶段操作请求。
  16. 根据权利要求14所述的系统,其特征在于,所述第二数据库服务器还用于根据所述加锁结果执行所述第二分支事务,并将所述第二分支事务的执行结果反馈给所述客户端,具体包括:
    在所述加锁结果为加锁成功且所述第二数据库服务器成功完成所述第二分支事务时,所述第二数据库服务器将所述第二分支事务执行成功的执行结果反馈给所述客户端;
    或者,
    在所述加锁结果为加锁失败或者所述第二数据库服务器没有完成所述第二分支事务时,所述第二数据库服务器将所述第二分支事务执行失败的执行结果反馈给所述客户端。
  17. 根据权利要求16所述的系统,其特征在于,所述客户端还用于根据所述第二数据库服务器反馈的第二分支事务的执行结果,确定分布式事务的执行结果,具体包括:
    所述客户端在所述第二分支事务的执行结果为执行成功且所述分布式事务包括的其他分支事务的执行结果均为执行成功时,确定所述分布式事务的执行结果为执行成功;或者,在所述第二分支事务的执行结果为执行失败时,确定所述分布式事务的执行结果为执行失败。
  18. 根据权利要求15所述的系统,其特征在于,在所述第二锁服务器对所述第二分支事务涉及的数据加锁成功时,
    所述第二事务协调器还用于调度所述第二数据库服务器执行所述二阶段操作请求之前,
    所述第二事务协调器,还用于向所述第二锁服务器发送放锁请求;
    所述第二锁服务器,还用于释放对所述第二分支事务涉及的数据执行加锁操作时所加的锁,向所述第二事务协调器发送放锁成功的消息。
  19. 根据权利要求11至17任一项所述的系统,其特征在于,所述第一数据库服务器调用所述第二数据库服务器执行所述第二分支事务之前,还包括:
    所述第一数据库服务器,还用于向所述第一事务协调器发送第一分支事务的开始请求;
    所述第一事务协调器,还用于根据所述第一分支事务的开始请求,创建第一分支事务标识,并将所述第一分支事务标识发送给所述第一数据库服务器;
    所述第一数据库服务器,还用于接收到所述第一分支事务标识后,成功执行所述第一分支事务。
  20. 根据权利要求18所述的系统,其特征在于,所述分布式系统还包括第一锁服务器,
    所述第一事务协调器用于创建所述第一分支事务标识之前,还包括:
    所述第一事务协调器,还用于接收所述第一数据库服务器发送的所述第一分支事务的开始请求,所述第一分支事务开始请求包括所述第一分支事务涉及的数据在第一数据库中的位置信息;
    向所述第一锁服务器发送第一加锁请求,所述第一加锁请求包括所述第一分支事务涉及的数据在第一数据库中的位置信息;
    所述第一锁服务器,用于根据所述第一加锁请求对所述第一分支事务涉及的数据执行加锁操作,向所述第二事务协调器发送加锁成功的消息;
    所述第一事务协调器,还用于接收所述第一锁服务器发送的加锁成功的消息。
PCT/CN2022/081697 2021-03-30 2022-03-18 一种分布式事务实现方法及分布式系统 WO2022206429A1 (zh)

Priority Applications (1)

Application Number Priority Date Filing Date Title
US18/474,695 US20240013218A1 (en) 2021-03-30 2023-09-26 Distributed Transaction Processing Method, System, and Related Device

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202110343752.8A CN115145997A (zh) 2021-03-30 2021-03-30 一种分布式事务实现方法及分布式系统
CN202110343752.8 2021-03-30

Related Child Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2022/081655 Continuation WO2022206426A1 (zh) 2021-03-30 2022-03-18 一种分布式事务处理方法、系统及相关设备

Publications (1)

Publication Number Publication Date
WO2022206429A1 true WO2022206429A1 (zh) 2022-10-06

Family

ID=83403680

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2022/081697 WO2022206429A1 (zh) 2021-03-30 2022-03-18 一种分布式事务实现方法及分布式系统

Country Status (2)

Country Link
CN (1) CN115145997A (zh)
WO (1) WO2022206429A1 (zh)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116069868B (zh) * 2023-03-13 2023-06-09 徐工汉云技术股份有限公司 基于企业微服务的分布式柔性事务处理方法和装置

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106156174A (zh) * 2015-04-16 2016-11-23 中国移动通信集团山西有限公司 一种数据库事务处理的系统和方法
CN106453592A (zh) * 2016-11-02 2017-02-22 华为技术有限公司 异步调用处理方法、设备及分布式系统
CN107797849A (zh) * 2016-08-30 2018-03-13 阿里巴巴集团控股有限公司 分布式事务处理的方法、装置与系统
US20190075084A1 (en) * 2016-05-05 2019-03-07 Huawei Technologies Co., Ltd. Distributed Lock Management Method, Apparatus, and System
CN109844731A (zh) * 2016-10-13 2019-06-04 华为技术有限公司 分散式分布式数据库一致性

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106156174A (zh) * 2015-04-16 2016-11-23 中国移动通信集团山西有限公司 一种数据库事务处理的系统和方法
US20190075084A1 (en) * 2016-05-05 2019-03-07 Huawei Technologies Co., Ltd. Distributed Lock Management Method, Apparatus, and System
CN107797849A (zh) * 2016-08-30 2018-03-13 阿里巴巴集团控股有限公司 分布式事务处理的方法、装置与系统
CN109844731A (zh) * 2016-10-13 2019-06-04 华为技术有限公司 分散式分布式数据库一致性
CN106453592A (zh) * 2016-11-02 2017-02-22 华为技术有限公司 异步调用处理方法、设备及分布式系统

Also Published As

Publication number Publication date
CN115145997A (zh) 2022-10-04

Similar Documents

Publication Publication Date Title
CN110990182B (zh) 事务处理方法、装置、设备及存储介质
US9940183B2 (en) Commit-one-phase distributed transactions with multiple starting participants
US7743083B2 (en) Common transaction manager interface for local and global transactions
US8352421B2 (en) Recording distributed transactions using probabalistic data structures
US11681683B2 (en) Transaction compensation for single phase resources
US9417906B2 (en) Transaction participant registration with caveats
US20200082025A1 (en) Atomically executed application program interfaces
US7610305B2 (en) Simultaneous global transaction and local transaction management in an application server
CN111522631A (zh) 分布式事务处理方法、装置、服务器及介质
US20200201843A1 (en) Optimization of chaincode statements
EP3869434B1 (en) Blockchain-based data processing method and apparatus, device, and medium
CN110888718A (zh) 分布式事务的实现方法及装置
US6233587B1 (en) Extensible framework of key resource manager and transaction manager events for providing native support for foreign-initiated transactions
US7082432B2 (en) Specifying transaction manager type at various application levels
WO2022206429A1 (zh) 一种分布式事务实现方法及分布式系统
CN114327799B (zh) 分布式事务处理方法及装置、电子设备、存储介质
CN113377875B (zh) 跨链数据处理方法、装置、电子设备及可读存储介质
CN112596801A (zh) 事务处理方法、装置、设备、存储介质、数据库
JPH10149286A (ja) 効率的な共通オブジェクトリクエストブローカアーキテクチャトランザクションを実行するための方法および装置
CN113296897B (zh) 一种分布式事务的处理方法、装置、设备和机器可读介质
CN114443232A (zh) 事务管理方法、装置、电子设备及存储介质
CN113992750A (zh) 一种全局事务协同方法、装置、设备及介质
CN112162988A (zh) 一种分布式事务的处理方法、装置和电子设备
CN113077241B (zh) 审批处理方法、装置、设备及存储介质
US7395264B2 (en) Promotable transactions with promotable single phase enlistments

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 22778612

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 22778612

Country of ref document: EP

Kind code of ref document: A1