首页 > 技术文章 > TPC(two phase commit 两阶段提交)

zgq25302111 2020-02-03 16:25 原文

the protocol consists of two phases:

The commit-request phase (or voting phase), in which a coordinator process attempts to prepare all the transaction's participating processes (named participants, cohorts, or workers) to take the necessary steps for either committing or aborting the transaction and to vote, either "Yes": commit (if the transaction participant's local portion execution has ended properly), or "No": abort (if a problem has been detected with the local portion), and


The commit phase, in which, based on voting of the participants, the coordinator decides whether to commit (only if all have voted "Yes") or abort the transaction (otherwise), and notifies the result to all the participants. The participants then follow with the needed actions (commit or abort) with their local transactional resources (also called recoverable resources; e.g., database data) and their respective portions in the transaction's other output (if applicable).

 

Assumptions:
The protocol works in the following manner: one node is a designated coordinator, which is the master site, and the rest of the nodes in the network are designated the participants. The protocol assumes that there is stable storage at each node with a write-ahead log, that no node crashes forever, that the data in the write-ahead log is never lost or corrupted in a crash, and that any two nodes can communicate with each other. The last assumption is not too restrictive, as network communication can typically be rerouted. The first two assumptions are much stronger; if a node is totally destroyed then data can be lost.


The protocol is initiated by the coordinator after the last step of the transaction has been reached. The participants then respond with an agreement message or an abort message depending on whether the transaction has been processed successfully at the participant.

 

Basic algorithm
一、Commit request (or voting) phase
1.The coordinator sends a query to commit message to all participants and waits until it has received a reply from all participants.
2.The participants execute the transaction up to the point where they will be asked to commit. They each write an entry to their undo log and an entry to their redo log.
3.Each participant replies with an agreement message (participant votes Yes to commit), if the participant's actions succeeded, or an abort message (participant votes No, not to commit), if the participant experiences a failure that will make it impossible to commit.

 

二、Commit (or completion) phase
Success
If the coordinator received an agreement message from all participants during the commit-request phase:
1.The coordinator sends a commit message to all the participants.
2.Each participant completes the operation, and releases all the locks and resources held during the transaction.
3.Each participant sends an acknowledgement to the coordinator.
4.The coordinator completes the transaction when all acknowledgments have been received.

 

Failure
If any participant votes No during the commit-request phase (or the coordinator's timeout expires):
1.The coordinator sends a rollback message to all the participants.
2.Each participant undoes the transaction using the undo log, and releases the resources and locks held during the transaction.
3.Each participant sends an acknowledgement to the coordinator.
4.The coordinator undoes the transaction when all acknowledgements have been received.

 

Message flow:

Coordinator                                                    Participant
                             QUERY TO COMMIT
                 -------------------------------->
                             VOTE YES/NO             prepare*/abort*
                 <-------------------------------
commit*/abort*               COMMIT/ROLLBACK
                 -------------------------------->
                             ACKNOWLEDGMENT          commit*/abort*
                 <--------------------------------  
end

 

An * next to the record type means that the record is forced to stable storage.

 

Disadvantages:
The greatest disadvantage of the two-phase commit protocol is that it is a blocking protocol. If the coordinator fails permanently, some participants will never resolve their transactions: After a participant has sent an agreement message to the coordinator, it will block until a commit or rollback is received.

reference:wikipedia

推荐阅读