当前位置:网站首页>TCC of distributed transaction solutions

TCC of distributed transaction solutions

2022-07-07 05:40:00 Qin Tian

Catalog

One 、 What is? TCC

Two 、TM Transaction manager

3、 ... and 、TCC Solution

Four 、TCC Something to be aware of


One 、 What is? TCC

TCC yes Try、Confirm、Cancel Abbreviations of three words ,TCC Each branch transaction is required to implement three operations : Preprocessing Try、 confirm Confirm、 revoke Cancel.

Try  Do business check and resource reservation  , Confirm Do business confirmation  , Cancel To achieve a relationship with Try The reverse operation is the rollback operation  .

TM Initiate all branch transactions first try operation , Of any branch transaction try Operation execution failed ,TM Will initiate all branch transactions Cancel operation , if try The operation was all successful ,TM Will initiate all branch transactions Confifirm operation , among Confirm/Cancel If the operation fails ,TM Will try again .

Successful cases :

failures :

 

 

TCC There are three stages

  • Try The stage is to do business inspection ( Uniformity ) And resource reservation ( Isolation ) , This stage is only a preliminary operation , It and the following Confirm Together to really form a complete business logic .

  • Confirm Stage is to do   Confirm the submission  ,Try Start execution after all branch transactions of phase are executed successfully Confirm. Usually , use TCC Think Confirm There is no mistake in the stage . namely :  as long as Try success ,Confirm It must be successful  . if Confirm The stage really went wrong , Need to introduce retry mechanism or manual processing .

  • Cancel Phase is in business execution error   The business of executing branch transactions in the state of rollback is canceled  , reserve   Release resources . Usually , use TCC be   Think Cancel Stage is also certain to be successful   Of . if Cancel The stage really went wrong , Need to introduce retry mechanism or manual processing .

Two 、TM Transaction manager

TM The transaction manager can be implemented as a stand-alone service , You can also have the global transaction initiator act as TM Role ,TM Independence is to become a public Using components , In order to consider the system structure and software reuse .

TM Generate a global transaction record when initiating a global transaction , Global transaction ID Throughout the chain of distributed transaction calls , Used to record transaction context , Track and record status , because Confirm and Cancel Failure needs to be retried , So we need to be idempotent , Idempotency refers to the same operation, no matter how many times it is requested , The results are the same .

3、 ... and 、TCC Solution

Frame name

GitHub Address

tcc-transaction

https://github.com/changmingxie/tcc-transaction

Hmily

https://github.com/yu199195/hmily

ByteTCC

https://github.com/liuyangming/ByteTCC

EasyTransaction

https://github.com/QNJR-GROUP/EasyTransaction

Four 、TCC Something to be aware of

1、 Empty rollback

In no call TCC resources Try In the case of method , Called a two-stage Cancel Method ,Cancel Method needs   Recognize that this is an empty rollback  , Then go straight back to success .

Reasons for appearance : When a branch office is in service downtime or network abnormality , Branch transaction call recorded as failed , At this time, there is no implementation Try Stage , When the fault is recovered , When a distributed transaction is rolled back, it will call the two-stage Cancel Method , Thus, an empty rollback is formed .

resolvent : Identify this empty rollback . You need to know whether a phase is implemented , If executed , That's normal rollback ; If not implemented , That's empty rollback . I've said that before TM Generate a global transaction record when initiating a global transaction , Global transaction ID Throughout the chain of distributed transaction calls . Add an additional branch transaction record , There are global transactions ID And branch business ID, The first stage Try Method will insert a record , Indicates that a stage has been implemented .

// stay cancel in cancel Null rollback processing , If try No implementation ,cancel No execution is allowed 
if(accountInfoDao.isExistTry(transId)<=0){
    log.info("bank1  Null rollback processing ,try No implementation , Don't allow cancel perform ,xid:{}",transId);
    return ;
}

2、 idempotent

In order to ensure TCC The two-phase commit retry mechanism will not cause data inconsistency , requirement TCC The second stage of Try、Confirm and Cancel Interface guarantees idempotent , This does not reuse or release resources . If idempotent control is not done well , It is likely to lead to serious problems such as data inconsistency .

3、 Hang

Suspension is for a distributed transaction , The second stage Cancel Interface than Try Interface first .

Reasons for appearance :RPC Call branch transaction try when , Register a branch first , Re execution RPC call , If at this time RPC The called network is congested , Usually RPC The call has a timeout , RPC Overtime   in the future ,TM Will notify RM  Roll back   This distributed transaction , The rollback may be completed ,RPC The request arrives at the participant to actually execute , And one Try Method reserved business resources .

Solutions : If the second phase is completed , That stage can't be continued . When a phase transaction is executed, it is judged that under the global transaction , “ Branch transaction records ” Whether there are two-stage transaction records in the table  , If so, do not execute Try.

give an example , The scene is A Transfer accounts 30 Yuan to B,A and B Accounts in different services .

programme :

Account A

try:
     Check if the balance is enough 30 element 
     Deduction 30 element 
 
confirm:
     empty 
 
cancel:
     increase 30 element 

Account B

try:
     increase 30 element 
 
confirm:
     empty 
 
cancel:
     Reduce 30 element 

Solutions that

(1) Account A, The balance here is the so-called business resources , According to the principles mentioned earlier , In the first phase, we need to check and reserve business resources , therefore , We are withholding money TCC Resources Try Check the interface first A Whether the account balance is enough , If it is enough, deduct 30 element . Confirm Interface means formal submission , Because business resources are already in Try I deducted... From the interface , So in the second stage Confirm Nothing can be done in the interface .Cancel The execution of the interface indicates that the whole transaction is rolled back , Account A Rolling back requires that Try Deducted from the interface 30 Yuan back to account .

(2) account number B, In the first phase Try Interface to account B Add money ,Cancel The execution of the interface indicates that the whole transaction is rolled back , Account B Rolling back requires that Try In the interface 30 Yuan less .

Solution problem analysis

1、 If the account A Of Try Not implemented in Cancel Then add more 30 element .

2、 because Try、Cancel、Confirm It's all called by a separate thread , And there will be repeated calls , So we need to implement idempotent .

because Try、Cancel、Confirm It's all called by a separate thread , And there will be repeated calls , So we need to implement idempotent .

3、 account number B stay Try add 30 element , When Try Other threads may be consumed after execution .

4、 If the account B Of Try Not implemented in Cancel Then more and less 30 element .

Problem solving :

1、 Account A Of Cancel The method requires judgment Try Whether the method performs , Normal execution Try Can only be carried out after Cancel.

2、Try、Cancel、Confirm Method to implement idempotent .

3、 account number B stay Try Update of account amount... Is not allowed in method , stay Confirm Update account amount .

4、 Account B Of Cancel The method requires judgment Try Whether the method performs , Normal execution Try Can only be carried out after Cancel.

Optimization plan :

Account A

try:
    try Idempotent check 
    try Suspension handling 
     Check if the balance is enough 30 element 
     Deduction 30 element 
 
confirm:
     empty 
 
cancel:
    cancel Idempotent check 
    cancel Null rollback processing 
     Increase available balance 30 element 

Account B

try:
     empty 
confirm:
    confirm Idempotent check 
     Officially add 30 element 
cancel:
     empty 

summary :

If I take TCC Transaction processing flow and 2PC Two phase submission for comparison , 2PC It's usually across libraries DB level  , and  TCC In the application level  , It needs to be implemented through business logic . The implementation of this distributed transaction   advantage   lie in , The application can define the granularity of data operation by itself , bring   Reduce lock conflicts 、 Increase throughput   Make it possible .

and   deficiencies   It lies in the application   Very invasive  , Every branch of business logic needs to implement try、confirm、cancel Three actions . Besides , Its   It is also difficult to realize  , According to the network status 、 Different failure reasons such as system failure implement different rollback strategies .

原网站

版权声明
本文为[Qin Tian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207062341084367.html