当前位置:网站首页>Two stage submission and three stage submission

Two stage submission and three stage submission

2022-07-26 01:19:00 Long time no see, happy


Two stage submission and three stage submission

Two stage submission agreement

The establishment of the two-stage submission algorithm is based on the following assumptions :

  • In this distributed system , There is a node as The coordinator (Coordinator), Other nodes act as participants (Participants), And the nodes can communicate with each other ;
  • All nodes use write ahead logging , After being written, the log is saved on a reliable storage device , Even if the node is damaged, it will not cause the loss of log data ;
  • All nodes will not be permanently damaged , Even if it is damaged, it can still be recovered .

Two phases in a two-phase submission , refer to Commit-request Stage and Commit Stage , The two-stage submission process is as follows :

 Insert picture description here

Submit request phase

  1. Business inquiry
    The coordinator sends the transaction content to all participants , Ask whether you can perform the operation of transaction submission , And start waiting for the response of each participant .
  2. Perform transactions
    Each participant node performs transaction operations , And will undo and Redo Information is logged in the transaction log .
  3. Each participant feeds back the response of the transaction inquiry to the coordinator

Submission phase

The coordinator will decide whether the transaction submission operation can be carried out finally according to the feedback of each participant , There are two possibilities :

The first possibility : If the coordinator's feedback from all participants is yes Respond to , Then it will carry out Transaction submission .

  1. Send submit request
    The coordinator sends Commit request .
  2. Transaction submission
    The participants received Commit After the request , The transaction commit operation will be formally executed , And release the transaction resources occupied during the whole transaction execution after the commit .
  3. Feedback transaction submission results
    After the participant completes the transaction commit , Send... To the coordinator ack news .
  4. Complete the business
    The coordinator receives feedback from all participants ack After the message , Complete the business .

The second possibility : If any participant gives feedback to the coordinator No Respond to , Or after waiting for a timeout , The coordinator has yet to receive feedback from all participants , Then it will Interrupt the business .

  1. Send rollback request
    The coordinator sends RollBack request .
  2. Transaction rollback
    The participants received RollBack After the request , Will use it to record a kind of Undo Information to perform a transaction rollback operation , And release the resources occupied during the whole transaction execution after the rollback .
  3. Feedback transaction rollback result
    After the participant completes the transaction rollback , Send... To the coordinator ack news .
  4. Interrupt the business
    The coordinator receives feedback from all participants ack After the news , Complete transaction interrupt .

Problems with two phase commit

  • Resources are blocked by synchronization

In the process of execution , All participating nodes are transaction exclusive , When participants have public resources , Then the access of third-party nodes to public resources will be blocked .

  • The coordinator may have a single point of failure

Once the coordinator breaks down , The participants will keep blocking .

  • stay Commit Data inconsistencies occurred in phase

In the second phase , Suppose the coordinator issues the transaction Commit The notice of , However, due to network problems, the notification was only received and executed by some participants Commit, The rest of the participants were not notified , It's stuck , that , During this time, data inconsistency occurs .

Three stage submission agreement

In order to solve the problems of synchronization blocking in two-stage protocol , The three-phase commit protocol introduces a timeout mechanism in both coordinators and participants , And split the first phase of the two-phase commit protocol into two steps : inquiry , Then lock the resources , Finally, the real submission .

Of the three stages Three Phase Respectively CanCommit、PreCommit、DoCommit Stage .
 Insert picture description here

CanCommit Stage

  1. Business inquiry
    The coordinator sends a... With transaction content to all participants canCommit request , Ask if the transaction commit operation can be performed , And start waiting for the response of participants .
  2. The participants responded to the coordinator's inquiry by giving feedback
    Participants are receiving canCommit After the request , Under normal circumstances , If he thinks that the affairs can be carried out smoothly , Then feedback yes Respond to , And get ready , Otherwise feedback No Respond to .

PreCommit Stage

The coordinator decides whether to continue the transaction according to the response of the participants PreCommit operation . According to the response , There are two possibilities .

A. If the coordinator's feedback from all participants is Yes Respond to , Then the transaction will be pre executed :

  • Send pre submit request , The coordinator sends... To the participants PreCommit request , And enter Prepared Stage ;
  • Transaction pre commit , Participant received PreCommit After the request , Will perform transaction operations ;
  • Respond to feedback , If the participant successfully performs the transaction operation , Then return to ACK Respond to , And start waiting for the final order .

B. If any of the participants sent No Respond to , Or wait for the timeout , None of the coordinators received a response from the participants , Then interrupt the transaction :

  • Send interrupt request , The coordinator sends... To all participants abort request ;
  • Interrupt the business , The participants received... From the coordinator abort After the request , The interruption of the execution of a transaction .

DoCommit Stage

In this phase, the real transaction commit , It can also be divided into the following two situations .

A. Execute commit

  • Send submit request . The coordinator received... From the participant ACK After response , Then it will go from pre submission to submission , And send it to all participants doCommit request .
  • Transaction submission . Participant received doCommit After the request , Perform formal transaction submission , And release all transaction resources after transaction commit .
  • Respond to feedback . After the transaction is committed , Send... To the coordinator ACK Respond to .
  • Complete the business . The coordinator receives... From all participants ACK After responding , Complete the business .

B. Interrupt the business
Enter this stage , Suppose the coordination is really in normal working state , And any one of the participants fed back to the coordinator No Respond to , Or after waiting for a timeout , The coordinator has not yet been able to receive feedback from all participants , Then the transaction will be interrupted .

  1. Send interrupt request
    The coordinator sends abort request
  2. Transaction rollback
    Participant received abort After the request , Will use it to record in phase two Undo Information to perform a transaction rollback operation , And release the resources occupied during the whole transaction execution after rollback .
  3. Feedback transaction rollback result
    After completing the transaction rollback meeting , Send... To the coordinator Ack news .
  4. Interrupt the business
    The coordinator receives feedback from all participants Ack After the news , Interrupt the business .

What improvements have been made in the three-stage submission

Introduce timeout mechanism

stay 2PC in , Only the coordinator has a timeout mechanism , If the participant's message is not received within a certain period of time, it will fail by default ,3PC At the same time, the timeout mechanism is introduced in both the coordinator and the participants .

Add pre submit phase

stay 2PC Between the preparation stage and the submission stage , Insert a preparation stage , send 3PC Have CanCommit、PreCommit、DoCommit Three stages ,PreCommit It's a buffer , It ensures that the states of participating nodes are consistent before the final submission stage .

Problems in the three-stage submission agreement

The three-stage submission agreement also has problems , Specific performance: , In stage three , If participants receive PreCommit After the news , There is a problem that the coordinator cannot communicate normally , under these circumstances , Participants will still commit transactions , This leads to data inconsistency .

Applications submitted in two and three phases

Two phase commit is a relatively simplified consistency algorithm / agreement , Many relational databases use two-phase commit protocol to complete distributed transaction processing , A typical example is MySQL Of XA standard .

In transaction processing 、 Databases and Computer Networks , The two-phase commit protocol provides the guarantee of data consistency in distributed design , The participants of the whole transaction either commit all the consistency successfully , Or roll back all .MySQL Cluster Internal data synchronization is used 2PC agreement .

MySQL Master-slave replication of

stay MySQL in , The binary log is server layer , It's mainly used to do Master slave copy and Instant point recovery The use of ; Transaction log (Redo Log) yes InnoDB Storage engine layer , Used to secure transactions .

While the database is running , Need assurance Binlog and Redo Log The consistency of , If the order is inconsistent , Means that the Master-Slave It may not be the same .

In the open Binlog after , How to ensure Binlog and InnoDB redo The consistency of logs ?MySQL It uses two-phase submission , The internal system will automatically treat ordinary transactions as a XA Business ( Internal distributed transactions ) To deal with it :

  • Commit Will be automatically divided into Prepare and Commit Two phases ;
  • Binlog Will be regarded as the coordinator of affairs (Transaction Coordinator),Binlog Event It will be used as the coordinator log .
原网站

版权声明
本文为[Long time no see, happy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/207/202207260117271944.html