One or two phase commit agreement
generally divided into the coordination deviceCand several transaction performersSitwo types of characters:
when a transaction is executedTof all SitesSiare notifiedCtransaction execution Complete,CThe two-phase commit protocol is initiated.
(1) FirstCto allSiHair<prepare>message(Cthe first<prepare>message is written to the native log) ,Sireceived<prepare>message, according to the nativeTthe execution of the case, if the successful return<ready t>, does not return successfully<abort t>. (The message to be returned should be written to the log before returning)
(2) CCollect allSiafter the return message(or after a timeout period), if all returns are<ready t>, the transaction succeeds and is sent to all sites<commit t>, or the transaction fails to send<abort t>. The message should be written in the log before sending it. SiteSireceived aCof the<commit t>or<abort t>The message is written to the log and then submitted or rolled back according to the message.
note: c si si After recovering from a failure, check the log for this machine first, if you have received <COMMIT&NBSP;T> then commits, if <ABORT&NBSP;T> rollback. If <READY&NBSP;T> c Ask and determine the next step. If there is nothing, it is likely that the <PREPARE> si It collapsed, so it needed to be rolled back.
the flaw in the two-phase submission is that if C crash, all Si may all need to wait C , which creates a blockage.
Two or two phase commit process
first stage:
First, the coordinator writes a log record in the log of its own node, and then all participants send a message prepare t (including itself) , whether the transaction can be submitted;
prepare t ready t information, At the same time, it enters the pre-commit state state, if the transaction cannot be committed, logs the log and returns a not commit t . Span style= "font-family: the song Body;" > information to the facilitator, and revoke the database changes made on its own;
Participants were able to postpone sending the response time, But eventually it needs to be sent.
Phase II:
The facilitator collects the views of all the participants, if they receiveNot commit Tinformation, it identifies that the transaction cannot be committed and the Coordinator willAbort Tlog, and send a message to all participantsAbort Tinformation that allows all participants to revoke all pre-operations on their own;
if the coordinator receives all the participants,Prepare Tinformation, then the facilitator willCommit TThe log is written to disk and sent to all participants aCommit Tinformation to commit the transaction. If the facilitator has not received a message from a participant, the participant is deemed to have sent aVote_abortinformation, which cancels the execution of the transaction.
participants received the Coordinator'sAbort Tinformation, the participant terminates the submission andAbort Trecorded in the log, if the participant received aCommit Tinformation, the transaction is committed and the record is written.
In General, a two-phase commit mechanism can run well, and when a participant goes down during a transaction, it can be restarted by asking other participants or coordinators to know if the transaction was committed. Of course, the premise is that each participant will write the log in advance of each step of the operation.
Two-phase commit of distributed transaction