CS W186 Exam Prep 11
Distributed Transactions
Distributed
Transactions
Distributed Computing
2 Phase Commit (2PC)
Solution: Distributed voting: 2 Phase Commit (2PC)
2 Phase Commit (Algorithm)
Setup
2 Phase Commit (Algorithm)
Phase 1: Prepare (Agreement)
2 Phase Commit (Algorithm)
Phase 2: Commit (Finality)
2 Phase Commit (Algorithm)
Participant Algorithm
Handling Failures
Assume ARIES recovery
Handling Failures: Participant “down”
Abort
Wait….
Handling Failures: Coordinator “down”
NO
???
Two-Phase Commit (2PC): Logging
Two-Phase Commit (2PC): Logging
Two-Phase Commit (2PC): Logging
Coordinator
commit* or abort*
(with participant IDs)
end
Participant
prepare* or abort*
(with coordinator ID)
commit* or abort*
* indicates that the record must be flushed to disk immediately
PREPARE
VOTE YES/NO
COMMIT/ABORT
ACK
Two-Phase Commit (2PC): Optimization
Two-Phase Commit (2PC): Optimization
Two-Phase Commit (2PC): Optimization
Coordinator
commit*
(with participant IDs)
or abort
end
(if commit)
Participant
prepare* or abort
(with coordinator ID)
commit* or abort
* indicates that the record must be flushed to disk immediately
PREPARE
VOTE YES/NO
COMMIT/ABORT
ACK (if commit)
Two-Phase Commit (2PC): Blocking
Last tip: Solving 2PC Problems
Worksheet
Question 1
Question 1a
True or False: Resolving a deadlock at a local node will always resolve distributed deadlock.
False. Distributed deadlock occurs whenever the union of waits-for graphs across different nodes contains cycles. Resolving one deadlock may not solve deadlock in the union of the nodes' waits-for graphs.
Question 1b
True or False: Suppose we make the changes needed so only a majority of participants need to vote yes for a transaction to commit. Participants can write an ABORT record in phase 1 of 2PC.
False. Participants can't unilaterally abort anymore in this new scenario. The aborting participant can't determine the result of the vote by itself, so the participant must receive the result of the vote from the leader.
Question 1c
True or False: Presumed abort as presented in lecture no longer works in the scenario described in 1b.
True. If a participant crashes after voting no in phase 1, with presumed abort, no ABORT record will be flushed to disk. Coming back online, the participant would ordinarily presume abort, but in the scenario described, this may not be the case if the majority still votes to commit.
Question 1d
True or False: If any machine sees COMMIT record in its log upon recovery, the transaction will commit. Additionally, describe how to distinguish whether a machine with a COMMIT record is a coordinator or participant.
True. Whether participant or coordinator, a node seeing a COMMIT record in its log on recovery must take the actions.
If the commit record holds the participantIDs, then we know the machine must be a coordinator. Additionally, if this coordinator doesn't have an END record and only a COMMIT, it must send the result of the vote to all the participants. If there is a PREPARE record alongside the COMMIT record, then we know the node must be a participant.
Question 2a
Describe what happens in the following scenario with and without presumed abort: Participant recovers and sees just a phase 1 ABORT record.
Without presumed abort: Send "no" to the coordinator.
With presumed abort: Nothing! The coordinator will presume abort.
Question 2b
Describe what happens in the following scenario with and without presumed abort: Participant recovers and sees a PREPARE record.
The case is the same with or without presumed abort. The participant will ask the coordinator the result of the vote and continue the process.
Question 2c
Describe what happens in the following scenario with and without presumed abort: Participant recovers and sees a PREPARE and phase 2 ABORT record
Without presumed abort: Participant will send ACK back to the coordinator.
With presumed abort: Participants don't need to do anything after an abort record that was the result of the vote.
Question 2d
Describe what happens in the following scenario with and without presumed abort: Coordinator recovers and sees an ABORT record
Without presumed abort: Inform the participants about the outcome of the vote.
With presumed abort: Nothing! (Participants who don’t know the decision will just ask later.)
Question 2
Question 1
What is the first message Machine 1 sends?
Answer: (b) PREPARE. The coordinator (i.e. Machine 1) begins the first round of two-phase commit by sending a PREPARE message to all of the subordinates.
M1: Coordinator M2-4: Participants
Question 2
What is the second message Machine 1 sends?
Answer: (c) COMMIT. The coordinator (i.e. Machine 1) begins the second phase of two-phase commit by sending a COMMIT message to all of the subordinates. Note that the problem states that all subordinates vote yes in the first round which is why the coordinator sends a COMMIT message (instead of an ABORT message) to start the second round.
M1: Coordinator M2-4: Participants
Question 3
How much time passes from when Machine 1 sends its first message to when Machine 1 sends its second message?
Answer: This is just the maximum time for a round-trip between Machine 1 and Machines 2–4, since Machine 1 sends its second message (COMMIT) once its first message (PREPARE) reaches each of the other machines and each of the machines respond back with a VOTE YES message.
The time for a round-trip between Machine 1 and Machine i is 2 · 100 · i, and the largest of these is with Machine 4: 2 · 100 · 4 = 800ms.
M1: Coordinator M2-4: Participants
Question 4
What is the first message Machine 2 sends?
Answer: (a) VOTE YES. A subordinate (e.g. Machine 2) responds to a PREPARE message from the coordinator with a VOTE YES or VOTE NO. The problem states that all subordinates vote yes, so Machine 2 sends a VOTE YES message.
M1: Coordinator M2-4: Participants
Question 5
What is the second message Machine 2 sends?
Answer: (d) None of the above. The second message Machine 2 (a participant) sends is an ACK.
M1: Coordinator M2-4: Participants
Question 6
How much time passes from when Machine 2 sends its first message to when Machine 2 sends its second message?
Answer: Let’s have Machine 1 send the PREPARE message at time 0. This PREPARE message will reach Machine 2 at time 200ms, so Machine 2 sends its first message (VOTE YES) at time 200ms.
Machine 1 sends the COMMIT message at time 800ms (when it hears back from Machine 4), and this message takes 200ms to reach Machine 2. Machine 2 therefore receives the COMMIT message at time 1000ms, and sends its second message (ACK).
The time passed is therefore 1000ms - 200ms = 800ms.
M1: Coordinator M2-4: Participants
Question 7
True or False. A transaction is considered committed even if over half of the participants do not acknowledge the commit.
Answer: True. A transaction must commit once the COMMIT message is sent out, even if all the participants promptly crash repeatedly and do not respond with ACKs as a result.
M1: Coordinator M2-4: Participants
Question 8
Now suppose that our implementation of 2-Phase Commit has an off-by-one bug where the Coordinator receives, but does not use, Machine 4’s vote. That is, Machine 4’s vote does not affect whether or not the transaction commits or aborts. Answer True or False for the following questions:
A transaction that should normally commit may be aborted instead.
Answer: False. If the transaction would normally commit, then Machines 2 and 3 must have been functional and voted yes, so the transaction would still have to commit with this bug.
M1: Coordinator M2-4: Participants
Question 9
Now suppose that our implementation of 2-Phase Commit has an off-by-one bug where the Coordinator receives, but does not use, Machine 4’s vote. That is, Machine 4’s vote does not affect whether or not the transaction commits or aborts. Answer True or False for the following questions:
A transaction that should normally abort may be committed instead.
Answer: True. If Machine 4 votes no and other participants vote yes, then the transaction should be aborted but commits anyways.
M1: Coordinator M2-4: Participants
Question 10
Now suppose that our implementation of 2-Phase Commit has an off-by-one bug where the Coordinator receives, but does not use, Machine 4’s vote. That is, Machine 4’s vote does not affect whether or not the transaction commits or aborts. Answer True or False for the following questions:
A transaction that should normally commit may be committed properly.
Answer: True. If all machines vote yes, then the transaction commits even if we ignore Machine 4’s vote.
M1: Coordinator M2-4: Participants
Question 11
Now suppose that our implementation of 2-Phase Commit has an off-by-one bug where the Coordinator receives, but does not use, Machine 4’s vote. That is, Machine 4’s vote does not affect whether or not the transaction commits or aborts. Answer True or False for the following questions:
A transaction that should normally abort may be aborted properly.
Answer: True. If Machine 2 or 3 votes no, then the transaction aborts in both cases.
M1: Coordinator M2-4: Participants
Attendance Link