当前位置:网站首页>2pc of distributed transaction solution

2pc of distributed transaction solution

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

Catalog

One 、 What is? 2PC

Two 、2pc Solution XA

3、 ... and 、2pc Solution Seata

1、seata Design idea

2、Seata The implementation process of

Four 、Seata With the traditional 2PC difference


XA It's a distributed transaction protocol , from Tuxedo Put forward .XA It's roughly divided into two parts : Transaction manager and local resource manager . The local resource manager is usually implemented by database , such as Oracle、DB2 These commercial databases are all implemented XA Interface , The transaction manager is the global scheduler , Responsible for the submission and rollback of local resources .

One 、 What is? 2PC

2PC I.e. two phase submission agreement , The whole transaction process is divided into two stages , Preparation stage (Prepare phase)、 Submission phase (commit phase),2 It means two stages ,P It means the preparation stage ,C It's the submission phase .

  • Preparation stage (Prepare phase):  The transaction manager sends... To each participant Prepare news  , Each database participant performs transactions locally , And write the local Undo/Redo journal , The transaction is not committed at this time .(Undo Log is to record the data before modification , For database rollback ,Redo Log is to record the modified data , Used to write data files after committing transactions );

  • Submission phase (commit phase): If the transaction manager receives an execution failure or timeout message from the participant , Send a rollback directly to each participant (Rollback) news ; otherwise , Send submit (Commit) news ; The participant performs commit or rollback operations according to the instructions of the transaction manager , And release the lock resources used in transaction processing .

Be careful : Lock resources must be released at the last stage .

Successful cases :

 

failures :

 

Two 、2pc Solution XA

        2PC Two phase submission , Also known as XA Transactions,MySQL from 5.5 Version starting support ,SQL Server 2005 Start supporting ,Oracle 7 Start supporting , Its advantage is consistency , The disadvantage is reduced availability .

        2PC Of   The traditional solution is at the database level   Realized , Such as Oracle、MySQL All support 2PC agreement , In order to unify standards and reduce unnecessary docking costs in the industry , Standardized processing models and interface standards need to be developed , International Open Standards Organization Open Group The distributed transaction processing model is defined DTP(Distributed Transaction Processing Reference Model).

 

Whole 2PC There are three roles involved in the transaction flow of AP、RM、TM.

AP Refers to the use of 2PC Applications for distributed transactions ;

RM refer to   Explorer  , It controls branch transactions ;

TM refer to   Transaction manager  , It controls the whole global transaction .

1、 stay   Preparation stage  RM Perform actual business operations , But don't commit the transaction , Resource locking ;

2、 stay   Submission phase  TM Will accept RM In the preparation stage of the implementation of the reply , As long as there is any one RM Execution failure ,TM Will inform all RM Perform a rollback operation , otherwise ,TM All will be informed RM Commit the transaction . Commit phase end resource lock release .

XA The problem of the plan :

  • Need local database support XA agreement .
  • The resource lock needs to wait until the end of two phases to release , Poor performance .

3、 ... and 、2pc Solution Seata

1、seata Design idea

Seata One of the design goals is to be non intrusive to the business , Therefore, there is no invasion from the business 2PC The plan begins , In tradition 2PC On the basis of evolution , And solve 2PC The problem with the program .

Seata hold A distributed transaction is understood as a global transaction containing several branch transactions . The responsibility of the global affairs is to coordinate the branch affairs under its jurisdiction to reach an agreement , Or submit it together successfully , Or fail to roll back together . Besides , Usually the branch transaction itself is a local transaction of a relational database , The diagram below shows the relationship between global transaction and branch transaction :

 

With the traditional 2PC The model of is similar to ,Seata Defined 3 A component to protocol the processing of distributed transactions :

 

  • ransaction Coordinator (TC):  Transaction coordinator  , It's independent middleware , need   Independent deployment   function , It maintains the running state of global transactions , receive TM Instruction initiate global transaction commit and rollback , Responsible for working with RM Communication coordinates the commit or rollback of individual branch transactions .

  • Transaction Manager (TM):  Transaction manager  ,TM Need to work embedded in the application , It is responsible for   Start a global transaction  , And finally to TC An instruction to initiate a global commit or global rollback .

  • Resource Manager (RM):  Control branch transactions  , Responsible for branch registration 、 Status report , And receive the transaction coordinator TC Instructions , Drive branch ( Local ) Transaction commit and rollback .

2、Seata The implementation process of

(1)  User service  TM towards TC Request to open a global transaction  , The global transaction is created successfully and   Generate a globally unique XID.

(2)  User service  RM towards TC register Branch business  , The branch transaction performs the new user logic in the user service , And include it in  XID Jurisdiction corresponding to global transaction  .

(3)  The user service performs branch transactions , Insert a record into the user table .

(4)  When the logic is executed to call the integration service remotely ( XID Propagate in the context of the microservice call link  ). Points service RM towards TC Register branch transactions , This branch transaction performs the logic of increasing integral , And include it in XID Jurisdiction corresponding to global transaction .

(5)  The points service performs branch transactions , Insert a record into the score sheet , After execution , Return to user service .

(6)  User service branch transaction execution completed .

(7) TM towards TC Launch against XID The global commit or rollback resolution for  .

(8) TC Dispatch XID All branch affairs under the jurisdiction of   Complete the commit or rollback request  .

 

Four 、Seata With the traditional 2PC difference

1、 Architecture level ,  Tradition 2PC  Of the plan RM It's actually in the   Database layer  ,RM Essentially, it's the database itself , adopt XA Protocol implementation , and  Seata Of RM In order to jar The package is deployed as the middleware layer   On the application side .

2、 Two phase submission , Tradition 2PC No matter the resolution of the second stage is commit still rollback,  The locks of transactional resources should be kept to Phase2 Release when finished  . and  Seata The way to do this is to Phase1 Commit local transaction  , So we can save it Phase2 Time to hold lock , whole   Increase of efficiency  .

原网站

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