Local transactions and distributed transactions (section 03) Distributed Transaction Solution -2PC (two-phase commit)

What is a 2PC

2PC That two-phase commit protocol, the entire global transaction into a preparatory phase, the commit phase.

  1. Preparation phase (Prepare Phase): Transaction Manager sent to each local affairs Prepare messages, each local transaction execution local affairs, but does not commit. And write local Undo / Redo logs. And continue to hold the lock resources.
    Undo: recording data before the database changes for roll back the transaction.
    Redo: Modify the database record data, the user submits the transaction.
  2. Commit phase (Commit Phase): If you receive a message that a transaction manager local transaction fails, the message is sent rollback (Rollback) directly to each local affairs, otherwise, the transaction manager to send each local transaction commit message ( Commit). Each local affairs in accordance with the transaction manager roll back or to submit news, resources and release the lock.

Success Legend

Legend failure

Two 2PC XA implementation of specific programs

  2PC的传统方案是在数据库层面实现的,如Oracle、MySQL都支持2PC协议,为了统一标准减少行业内不必要的对接成本,需要制定标准化的处理模型及接口标准,国际开放标准组织Open Group定义了分布式事务处理模型 DTP(Distributed Transaction Processing Reference Model)。 

DTP model defines several roles:

  • RM (Resource Manager): Explorer, can be understood as participants in the transaction, control of branch transactions.
  • TM (Transaction Manager): transaction management, control global transaction, the global transaction life cycle control, coordination of the various RM.
  • AP (Application Program): application, it is understood that use of DTP.

XA protocol interface specification that communication between the TM and RM
XA embodiment is implemented based on the database scheme XA 2PC protocol, for example: JTA (Java Transaction Manager): is a Java specification is implemented in the Java XA.

Process For example , new user registration send integration, user database and integration library is not the same library:

  1. Application (AP) holds two user libraries and library data source integration.
  2. Application (AP) inform the user library by TM RM new users, and notify the library RM integration for users to add points, RM did not commit the transaction at this time, when the user points and resource locking.
  3. TM收到执行回复,只要有一方失败则分别向其他RM发起回滚事务,回滚完毕,资源锁释放。
  4. TM收到执行回复,全部成功,此时向所有RM发起提交事务,提交完毕,资源锁释放。

XA方案的缺点:

  • 需要数据库支持XA协议,不过大部分常用数据库都支持(MySql、Oracle)。
  • 资源锁持续了两个阶段,性能较差。

三 2PC具体实现方案之Seata方案

3.1 什么是Seata

Seata是阿里巴巴中间件团队发起的开源项目Fescar,后更名为Seata,是一款开源的分布式事务框架。
Seata作用于应用层通过对本地事务的协调完成全局事务。对业务0侵入来解决微服务下的分布式事务问题,目前提供AT模式(即2PC)和TCC模式的分布式事务解决方案。

3.2 Seata三大组件

Seata定义了三大组件来处理分布式事务:

  • 事务协调器(Transaction Coordinator):独立的中间件,需要独立部署。维持全局事务的状态,接受TM发起全局事务的提交与回滚,负责RM通信协调各分支事务的提交与回滚。
  • 事务管理器(Transaction Manager):需要嵌入应用程序中工作,负责向TC发起全局事务,并最终向TC发起全局提交或全局回滚。
  • 资源管理器(Resource Manager):控制分支事务,接受TC的指令,驱动分支事务的提交与回滚。

3.2 Seata工作流程

拿新用户注册送积分举例,用户库与积分库不是同一个库:

  1. 用户服务的TM向TC申请开启一个全局事务,TC全局事务开启成功,并生成一个全局唯一XID返回给TM。
  2. 用户服务RM带着XID向TC注册分支事务,TC将其纳入XID对应的全局事务,并返回BranchID。
  3. 用户服务执行分支事务,向用户表插入数据,并向undo_log写入数据(用于回滚),并提交分支事务,然后向TC上报分支事务执行结果。
  4. 用户服务带着XID调用积分服务。
  5. 积分服务RM带着XID向TC注册分支事务,TC将其纳入XID对应的全局事务,并返回BranchID。
  6. 积分服务执行分支事务,向积分表插入数据,并向undo_log写入数据(用于回滚),积分服务返回用户服务,并提交分支事务,然后向TC上报分支事务执行结果。
  7. 用户服务TM向TC发起针对XID的全局提交或回滚。
  8. TC调度XID下的全部分支事务完成提交或回滚。

成功图例:

回滚图例:

3.3 Seata与传统2PC比较

  • RM区别:传统2PC的RM在数据库层,实际是数据库本身,通过XA协议实现。而Seata以Jar的形式作为中间件部署在应用层。
  • 分支事务提交时间:2PC在第二阶段,Seata在第一阶段。提高了性能。

Guess you like

Origin www.cnblogs.com/NEWHOM/p/12399649.html