Rambling: how to explain to his girlfriend what is 2PC (two-phase commit protocol)?

Saturday night, the family was watching a variety show, ran girlfriend suddenly came to me to play "king of glory."

Playing a few games, I can finally rest for a while, and I am prepared to continue to see the variety, but his girlfriend came to me to talk about him in the end what is the two-phase commit protocol.

Distributed consistency

Fortunately, dedicated to his girlfriend before we introduced what is distributed , or else to say on this topic a long story.

Before introducing distributed, we Houchu to the hotel, for example, today continued the previous example is to say what is distributed consistency.

With the development of the hotel, slowly evolved from more than just a chef to cook, and then evolved into a more industrial jobs have vegetables, side dishes and cooks and so on.

After the division of labor with a variety, it is bound to require coordination and cooperation among these people.

Such as restaurant guests ordered a tomato and egg, and then began to prepare Houchu up, workers began to wash vegetables tomatoes, garnish division began to prepare eggs, cook began to prepare cooking pot filling. This is the case of one kind of normal.

However, if the message conveyed is not in place, or vegetables such as kitchen chef temporary absence, will lead some people have begun to prepare themselves, but some people are not ready.

It's like a distributed system, like when we are alone in the electricity supplier site, the need for multiple distributed services while services such as payment systems payment, red envelopes deduction system, inventory system, inventory deductions , logistics and other logistics information system updates.

However, if a system in which a failure during execution, or due to network reasons not received the request, then the whole system may have inconsistent phenomenon, namely: paid, buckle up red, but the inventory is not deductible .

This is called a distributed system data consistency problems.

Two-phase commit protocol

The reason why there will be just the consistency of case, because every employee is only concerned about what you do not focus on other people, then, in order to ensure overall consistency, we need to introduce one in Houchu new role, responsible for co-ordination, and this role is to coordinate the deployment of all.

So, the introduction of a coordinator is responsible for coordinating the work of all the participants, this is actually distributed transaction processing model X / Open organization defined in the distributed system, and the two-phase commit protocol is derived based on this model.

For example, five people meet to play king of glory, you want to play with the following steps are required:

A man wants to play five black and glory of the King, so he began to contact his little friends.

Organizer: small A, we are ready to play king of glory, if you can come to it, now you log in the game, then give me the reply message on gaming friends.

A small sign in to your game account, and then told the organizers: A small already in place.

Organizer: Small B, small C, small D, we are ready to play king of glory, if you can come to it, now you log in the game, then give me the reply message on gaming friends.

Small B, small C, D are small log in to your game account, and then told the organizers: Little B, a small C, D has a small place.

Organizers found that everyone in place, so one by one to inform you in the game,

Organizer: Small A, I invite you, you come in.

A small invitation

Organizer: Small B, small C, small D, I invite you, you come in.

Little B, a small C, small D receives the invitation

As a result, five people in King Canyon happy to play again.

For five people to open the black this transaction operation, we began to prepare five men were idle, busy with their own affairs. Following the organizers had been coordinated, we have reached a consistent state, that is, one of two things:

  • 1, five people happy to play the game together

  • 2, five people have quit the game, or go do your own thing.

Finally, if some people have been waiting in the game, did not go into the other part of the game, then the data is inconsistent.

Process the above process, is a typical two-phase commit (2PC) in distributed systems, also we have the same problem and the same solution can be used.

In a distributed system, each node can be aware of their own although the success or failure of the operation, but can not know the success or failure of the operation of other nodes (only know that they have time to play with the King of glory, I do not know if anyone else has).

When a transaction across multiple nodes, in order to maintain the ACID properties of transactions need to be introduced as a component of a coordinator to unify control of operating results of all participants and the final result indicating whether or not to operate these nodes genuine submission (organizer the game went into the room to advise participants).

Therefore, the idea of ​​two-phase commit protocol algorithm can be summarized as follows: Participants will inform the coordinator of the success of the operation, and then decide based on the feedback information from the coordinator of all participants each participant whether to commit operation or abort the operation.

It refers to the so-called two phases: the first phase: the preparation phase (voting phase) and the second stage: submission stage (execution stage)

Preparation Phase

Prepare transaction coordinator sends a message to each participant, each participant either directly returns failure (no time to inform the organizers of their own, can not play the game together), or in the local implementation of the transaction (Login glory of the King), but does not submit (first do not start the game).

The preparation stage can be further divided into the following three steps:

  • 1) ask the coordinator node to node if all participants can do a commit, and began to wait for a response each participant nodes. (Ask if you can play the game together)

  • 2) participant node performing an interrogation operation initiated until all transactions and information into the log Undo and Redo information. (Log glory of the King game)

  • 3) Each participant node response coordinator node initiating the inquiry. If the transaction participant node operation actually executed successfully, it returns a "yes" message; (the organizers were informed that they have a successful login) if the transaction operations participant node fails actual implementation, it returns a "suspend" message. (Inform the organizers themselves temporarily unable to play games, such as their account is restricted from playing qualifying)

Commit Phase

If the coordinator of the participants received a message or a timeout failure (some people can not play games together, or there has been no reply), send messages directly to each participant rolled back (inform others suspended game); otherwise, send a commit message (invited everyone into the game room); participant to perform commit or roll back (into the room to play a game or quit the game to do something else) in accordance with the instruction coordinator.

The next two cases were filed during the discussion stage.

When the coordinator node corresponding message obtained from all participants nodes are "agree":

  • 1) coordinator requesting node "formally submitted" (which requires all logged friends to join the game room) to all participants nodes.

  • 2) participant node formally complete the operation, and release resources occupied during the life of the transaction (accepted an invitation to enter the room).

  • 3) participant node sends a "complete" message (click "ready" to enter the ready state) to the coordinator node.

  • 4) coordinator node being "done" message to all the participant nodes feedback to complete the transaction (enter King Canyon).

If any participant node returns a response message in the first stage as "suspend" or ask the coordinator node in the first phase of the timeout before the response message can not be obtained when all participants nodes:

  • 1) coordinator requesting node "rollback" of (inform the owner to cancel the game) to all participants nodes.

  • 2) Undo information is written before participant nodes use the rollback, and releases the resources occupied during the life of the transaction (quit the game, to do their own thing).

  • 3) participant node sends a "complete rollback" message (to tell the organizers of his knowledge, have the opportunity to play with the back) to the coordinator node.

  • 4) the coordinator node by node feedback to all participants of "rollback complete" message, cancel the transaction (to cancel the games).

2PC shortcomings

The above process is the fact there are some disadvantages, such as

1, when the organizers of the participants received the message, you need to log in the game, waiting once again invited the organizers of the game, this process is a waste of time.

2, if in the process, the organizers suddenly there is anything to be interrupted, then those players have entered the game may wait forever.

3, log in the game after all, invited by the organizers require everyone to join his room, at this time if there are a number of network anomalies, or not participants in front of the phone, etc., may be part of the user to join the room, there part did not join.

4, if the organizers invite all participants start the game when, after he invited the first person, and that he was invited to his people is down. This time three other people do not know in the end should be how to do.

The above problems, there is a stage 2PC distributed systems, as corresponding to the following questions:

1, synchronized blocking problem .

During execution, all participating nodes are transaction blocking type. When a participant possession of public resources, and other third-party access nodes public resources have blocked state.

2, a single point of failure .

Because of the importance coordinator, once the coordinator has failed. Participants will have blocked it. Especially in the second phase, the coordinator fails, then all the players are still in the locked state of affairs resources, and can not continue to complete the transaction operation. (If it is hung coordinator, a coordinator may be re-elected, but can not be solved because the participants coordinator downtime caused by problems in the blocked state)

3, data inconsistencies .

In the two stage two-phase commit protocol, when a request to the coordinator sends commit participants, local network anomaly occurs or a fault has occurred in the coordinator transmits commit request process, this time resulting in only a portion of the participants received a commit request. In this partial participant after the commit request will commit to the operation. But other parts of the machine has not received a request can not be executed commit transaction commits. So the whole distributed system giving rise to a consistency of the data part of the phenomenon.

4, the second phase can not solve the problem:

After the commit coordinator and then send the message down, and the only receive this message down the participant also. Then created a new coordinator coordinator even by election protocol, this state of affairs is uncertain, no one knows whether the transaction was already submitted.

To sum up, that 2PC is not perfect, he questions the existence of synchronous blocking, single points of failure, can not guarantee 100% data consistency issues.



Guess you like

Origin juejin.im/post/5d6c7821518825415d062509