当前位置:网站首页>TCC of distributed transaction solutions
TCC of distributed transaction solutions
2022-07-07 05:40:00 【Qin Tian】
Catalog
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 .
边栏推荐
- English语法_名词 - 所有格
- App clear data source code tracking
- Most commonly used high number formula
- JD commodity details page API interface, JD commodity sales API interface, JD commodity list API interface, JD app details API interface, JD details API interface, JD SKU information interface
- Taobao store release API interface (New), Taobao oauth2.0 store commodity API interface, Taobao commodity release API interface, Taobao commodity launch API interface, a complete set of launch store i
- 淘宝店铺发布API接口(新),淘宝oAuth2.0店铺商品API接口,淘宝商品发布API接口,淘宝商品上架API接口,一整套发布上架店铺接口对接分享
- 一条 update 语句的生命经历
- Paper reading [MM21 pre training for video understanding challenge:video captioning with pre training techniqu]
- 京东商品详情页API接口、京东商品销量API接口、京东商品列表API接口、京东APP详情API接口、京东详情API接口,京东SKU信息接口
- ssm框架的简单案例
猜你喜欢
[PM products] what is cognitive load? How to adjust cognitive load reasonably?
Under the trend of Micah, orebo and apple homekit, how does zhiting stand out?
论文阅读【Semantic Tag Augmented XlanV Model for Video Captioning】
分布式事务解决方案之2PC
English语法_名词 - 所有格
Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
C#可空类型
LabVIEW is opening a new reference, indicating that the memory is full
Paper reading [semantic tag enlarged xlnv model for video captioning]
1. AVL tree: left-right rotation -bite
随机推荐
京东商品详情页API接口、京东商品销量API接口、京东商品列表API接口、京东APP详情API接口、京东详情API接口,京东SKU信息接口
Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
C nullable type
Lombok插件
Design, configuration and points for attention of network arbitrary source multicast (ASM) simulation using OPNET
DOM node object + time node comprehensive case
JVM(十九) -- 字节码与类的加载(四) -- 再谈类的加载器
淘宝商品详情页API接口、淘宝商品列表API接口,淘宝商品销量API接口,淘宝APP详情API接口,淘宝详情API接口
淘寶商品詳情頁API接口、淘寶商品列錶API接口,淘寶商品銷量API接口,淘寶APP詳情API接口,淘寶詳情API接口
论文阅读【Semantic Tag Augmented XlanV Model for Video Captioning】
Web Authentication API兼容版本信息
MySQL-CentOS7通过YUM安装MySQL
How Alibaba cloud's DPCA architecture works | popular science diagram
In memory, I moved from CSDN to blog park!
Getting started with DES encryption
Dj-zbs2 leakage relay
分布式事务解决方案之2PC
A cool "ghost" console tool
MySQL数据库学习(8) -- mysql 内容补充
Paper reading [MM21 pre training for video understanding challenge:video captioning with pre training techniqu]