Distributed Transaction Seata principle and source code analysis

Two elements:

1.Seata principle

AT mode (Small Business invasion)
Seata AT mode is a distributed transaction middleware evolution comes XA transactions, XA is one and two-phase commit, the need for database support distributed transactions protocol is essentially based on database implementation, Mysql5 .6 above support XA protocol, other databases such as Oracle, DB2 also implements the XA interface

Roles are as follows

Here Insert Picture Description
Transaction Coordinator (TC): Transaction Coordinator, maintain global transactions running, is responsible for coordinating and driving the global transaction is committed or rolled back
Transaction Manager ™: border control global affairs, is responsible for opening a global transaction, and eventually launch a global commit or global rollback resolution
Resource Manager (RM): transaction control branch, the branch is responsible for registration, the status report, and the transaction coordinator receives an instruction commit and rollback drive legs (local) transaction
substantially follows the processing logic

Here Insert Picture Description

Branch refers to the distributed transaction in the local affairs of each individual local

The first stage
Seata proxy JDBC data source through the SQL parsing service, the service data before and after updating data mirroring rollback logs into tissue, local transaction ACID properties using the updated traffic data and rollback journal writing submitted in the same local transaction.

This ensures that: update any business data submitted must have a corresponding rollback log exists

, The local branch of the transaction will be submitted to, and in the first phase of a global transaction based on the transaction locking mechanism immediately release local resources

This is the difference between the Seata and XA transactions, often locking of two-phase commit resources need to continue to commit or roll back the actual operation of the second stage, after the log has been rolled back, the resource can be released in the first phase lock, the lock range is reduced, improving efficiency, even if an abnormality occurs rollback second stage, only to find the corresponding data and back-undolog parsed into sql to achieve the purposes of rollback

同时Seata通过代理数据源将业务sql的执行解析成undolog来与业务数据的更新同时入库,达到了对业务无侵入的效果

Here Insert Picture Description

第二阶段
如果决议是全局提交,此时分支事务此时已经完成提交,不需要同步协调处理(只需要异步清理回滚日志),Phase2 可以非常快速地完成

Here Insert Picture Description
如果决议是全局回滚,RM 收到协调器发来的回滚请求,通过 XID 和 Branch ID 找到相应的回滚日志记录,通过回滚记录生成反向的更新 SQL 并执行,以完成分支的回滚


TCC(高性能)
seata也针对TCC做了适配兼容,支持TCC事务方案,原理前面已经介绍过,基本思路就是使用侵入业务上的补偿及事务管理器的协调来达到全局事务的一起提交及回滚,详情参考demo回滚

Here Insert Picture Description

2.Seata 源码分析:

重要流程如下:

拿到我们的主键,拿到更新的字段,

拿到更新的where 添加:

模板方法:

1.主要看update 方法,

一张表描述另一张表:

row_key,xid,table-name,bracnhedID:

 

2.分布式分析:

模板方法:

全局提交

----------------------------------------

总得来说就是分为如下几步

业务方调用各个微服务的try()方法,执行资源检查及预留操作
当所有try()方法均执行成功时,对全局事物进行提交,即由事物管理器调用每个微服务的confirm()方法
当任意一个方法try()失败(预留资源不足,抑或网络异常,代码异常等任何异常),由事物管理器调用每个微服务的cancle()方法对全局事务进行回滚
资源预留、提交、回滚都由业务方编码控制写在try()、confirm()、cancle()中

The key point is: TCC transaction coordinator know how to call each micro services in the business side try () after the call is to confirm or cancle, and a method which Resource Manager (RM) specifically calls

At this point the second question, how to find the TC method confirm the call service on which machines, rollback method also solves the
basic idea is

TM launched submit a request (with xid)
TC found by xid global session, then remove all branches session
by session in the branch resourceId, to find the cache buffer rpcContext objects, remove the corresponding channel, establish communication
summarize global transaction submitted the general process

Business-party call micro-services without exception, submit a request by TM to initiate a transaction
TC received after the transaction submit a request to find the global transaction by Xid, then remove all the branches of the transaction
traversing branch affairs, issued a branch transaction commit request
TCC resource manager RM received after submitting the request, the method corresponding to remove the bean, reflection commit method is called from the local cache according resourceId TCCResource

 

Source Analysis: https://blog.csdn.net/f4761/article/details/89077400

Reference Bowen: https://blog.csdn.net/f4761/article/details/89077400

 

 

 

 

 

Published 564 original articles · won praise 10 · views 90000 +

Guess you like

Origin blog.csdn.net/xiamaocheng/article/details/104575827