Directory:
-
Basic concept
-
Distributed transaction theory
-
Distributed transaction solution 2PC
-
TCC for Distributed Transaction Solutions (This Chapter)
-
Reliable message final consistency for distributed transaction solutions
-
Best efforts notification for distributed transaction solutions
-
Distributed transaction synthesis case study
1. Basic concepts
2. Basic theory of distributed transactions
3. 2PC for Distributed Transaction Solution (Two-phase Commit)
4. TCC for distributed transaction solutions
4.1. What are TCC transactions
TCC stands for Try, Confifirm, and Cancel. TCC requires each branch transaction to perform three operations: preprocess a Try, confirm a Confifirm, and Cancel. The Try operation performs service check and resource reservation, the Confifirm operation performs service confirmation, and the Cancel operation performs a rollback operation opposite to the Try operation. TM will first initiate the try operation of all branch transactions. If the try operation of any branch transaction fails, TM will initiate the Cancel operation of all branch transactions. If the try operation succeeds, TM will initiate Confifirm operation of all branch transactions. If the Confifirm/Cancel operation fails, TM will retry.
- In the Try stage, services are checked (consistency) and resources are reserved (isolation). This stage is only a preliminary operation, which together with subsequent Confifirm can really constitute a complete business logic.
- In the Confifirm phase, confirm submission is performed. In the Try phase, Confifirm is executed after all branch transactions are successfully executed. Generally, TCC is used to assume that the Confifirm stage is infallible. If Try succeeds, Confifirm will succeed. If the Confifirm phase does fail, a retry mechanism or manual processing should be introduced.
- In the Cancel phase, a branch transaction is cancelled and reserved resources are released when a service execution error needs to be rolled back. In general, the Cancel phase is considered a certain success when TCC is used. If the Cancel phase does go wrong, a retry mechanism or manual processing should be introduced.
TM transaction manager, TM transaction manager can be implemented as an independent service, or global transaction initiator can act as TM. TM is independent in order to become a common component, to consider the system architecture and software reuse.
TM in a global transaction generated when global transaction records, throughout the distributed transaction chain, global transaction ID is used to record a transaction context, track and record the state, the failure Confifirm and cancel to retry, so need to implement for the idempotent, idempotence refers to the same operation no matter how many times the request, the results are the same.
4.2.TCC solution
Name of the framework | Gitbub address | Number of star |
---|---|---|
tcc-transaction | [github.com/changmingxi…]. (https://github.com/changmingxie/tcc-transaction) | 3850 |
Hmily | [github.com/yu199195/hm…]. (https://github.com/yu199195/hmily) | 2407 |
ByteTCC | [github.com/liuyangming…]. (https://github.com/liuyangming/ByteTCC) | 1947 |
EasyTransaction | [github.com/QNJR-GROUP/…]. (https://github.com/QNJR-GROUP/EasyTransaction) | 1690 |
Hmily is a high performance distributed transaction TCC open source framework. Based on Java language to develop (JDK1.8), support Dubbo, Spring Cloud and other RPC framework for distributed transactions. It currently supports the following features:
- Nested transaction support is supported.
- Disruptor framework for asynchronous reads and writes to transaction logs is no different from RPC framework performance.
- Supports the springboot-starter project, which is easy to use.
- RPC framework support: Dubbo, Motan, SpringCloud.
- Local transactions storage support: redis, directing, zookeeper, fifile, mysql.
- Transaction log serialization support: Java, Hessian, Kryo, ProtoSTUFFFF.
- Seamless integration with Spring with Aspect AOP Aspect thinking, natural support for clustering.
- RPC transaction recovery, timeout abnormal recovery, etc.
Hmily don’t need a transaction coordination service, but there is a need to provide a data (mysql/mongo/zookeeper/redis/fifile) for logging stored.
The TCC service implemented by Hmily, like a normal service, only needs to expose one interface, its Try business. The Confifirm/Cancel business logic is provided only because of the need for global transaction commit/rollback, so the Confifirm/Cancel business only needs to be discovered by the Hmily TCC transaction framework, not by other business services that invoke it.
Empty rollback:
The reason is that when the service of a branch transaction breaks down or the network is abnormal, the call of the branch transaction is recorded as failure. At this time, the Try stage is not executed. When the fault is recovered, the two-stage Cancel method will be called for the rollback of the distributed transaction, thus creating an empty rollback.
Idempotent:
Add the execution status to Branch Transaction Record and query the status before each execution.
Suspension is when, for a distributed transaction, the two-stage Cancel interface executes before the Try interface.
The solution is that if the two-phase execution is complete, the two-phase execution cannot be continued. When executing a one-phase transaction, determine whether there are already two-phase transaction records in the Branch Transaction Records table under the global transaction. If there are, do not execute a Try.
Plan 1:
Try to check the balance for $30 subtract $30 confirm cancel add $30 copy the codeCopy the code
Try: adds 30 yuan confirm: empty Cancel: reduces the replication code by 30 yuanCopy the code
1) Account A, the balance here is the so-called business resources. According to the principle mentioned above, the business resources need to be checked and reserved in the first stage. Therefore, we first check whether the balance of account A is enough in the Try interface of TCC resources, if so, 30 yuan will be deducted. The Confifirm interface represents a formal commit, and since the business resource has been deducted from the Try interface, nothing can be done in the second phase of the Confifirm interface. The transaction is rolled back by executing the Cancel interface. If account A is rolled back, the $30 deducted from the Try interface is returned to the account.
2) Account B is added to account B in the Try interface of the first stage. The execution of Cancel interface indicates the rollback of the whole transaction. The rollback of account B requires subtracting the 30 yuan added to the Try interface.
- If account A’s try is not executed at Cancel, it adds $30.
- Since try, cancel, and confifirm are all called by separate threads and repeated calls occur, they all need to be idempotent.
- Account B adds $30 to the try, which may be consumed by other threads when the try completes.
- If account B’s try is not executed at Cancel, that’s $30 more.
- The cancel method of account A needs to check whether the try method is executed. Cancel can be executed only after the try method is executed properly.
- The try, cancel, and confifirm methods are idempotent.
- Account B is not allowed to update the account amount in try method, but in confifirm.
- The cancel method of account B needs to check whether the try method is executed, and cancel can be executed only after the try method is executed properly.
Account A
Idempotent check try suspend processing check whether balance is sufficient for $30 deduct $30 CONFIRM empty cancel Idempotent check cancel Empty rollback processing increase available balance $30 Copy codeCopy the code
Try: empty Confirm: confirm The idempotent check is formally added by 30 yuan cancel: emptyCopy the code