CS 764: Topics in Database Management Systems Lecture 11: Two-Phase Commit (2PC) Xiangyao Yu 10/12/2020 1
Announcement Submit a 1-page course project proposal by Oct. 21 A list of project ideas are uploaded to the course website (http://pages.cs.wisc.edu/~yxy/cs764-f20/CS764-Fall2020-project-ideas.pdf) 2
Today’s Paper: Distributed Transactions in R* ACM Trans. Database Syst. 1986. 3
Agenda Two-phase commit Presumed abort (PA) Presumed Commit (PC) Deadlock detection 4
Distributed Transactions Architectures: shared-nothing vs. shared-disk Data is partitioned and stored in each server A distributed transaction accesses data across multiple partitions CPU CPU CPU CPU CPU CPU Memory Memory Memory Memory Memory Memory Network HDD HDD HDD HDD HDD HDD Network Shared Nothing Shared Disk 5
Distributed Transactions Architectures: shared-nothing vs. shared-disk Data is partitioned and stored in each server A distributed transaction accesses data across multiple partitions Transaction T: CPU CPU CPU CPU CPU CPU write(A) Memory Memory Memory Memory Memory Memory write(B) Network tuple A tuple B HDD HDD HDD HDD HDD HDD Network Shared Nothing Shared Disk 6
Atomic Commit Protocol (ACP) Atomic commit protocol : applies a set of distinct changes as a single operation Example: Transaction T: write(A) tuple A tuple B write(B) The two updates must commit or abort atomically 7
The Challenge of Atomic Commit Node 1 Node 2 Transaction T: write(A) tuple A tuple B write(B) Commit Log and Log and commit commit back to caller A naïve approach: all nodes log and commit independently 8
The Challenge of Atomic Commit Node 1 Node 2 Transaction T: write(A) tuple A tuple B write(B) Commit Log and commit A naïve approach: all nodes log and commit independently Node 2 crashes before logging • Transaction T commits in node 1 but not in node 2 9
Two-Phase Commit (2PC) Key idea: let the coordinator log the tuple A tuple B final commit/abort decision Coordinator Subordinate 1 Subordinate 2 10
Two-Phase Commit (2PC) Key idea: let the coordinator log the tuple A tuple B final commit/abort decision Phase 1: prepare phase Coordinator Subordinate 1 Subordinate 2 PREPARE PREPARE [log] [log] prepare* prepare* VOTE YES VOTE YES 11
Two-Phase Commit (2PC) Key idea: let the coordinator log the tuple A tuple B final commit/abort decision Phase 1: prepare phase Coordinator Subordinate 1 Subordinate 2 PREPARE Phase 2: commit phase PREPARE [log] [log] • Coordinator logs the decision prepare* prepare* VOTE YES VOTE YES [log] commit* back to caller 12
Two-Phase Commit (2PC) Key idea: let the coordinator log the tuple A tuple B final commit/abort decision Phase 1: prepare phase Coordinator Subordinate 1 Subordinate 2 PREPARE Phase 2: commit phase PREPARE [log] [log] • Coordinator logs the decision prepare* prepare* VOTE YES • Coordinator sends the decision to VOTE YES subordinates [log] • commit* Coordinator forgets the transaction COMMIT after receiving ACKs back to caller [log] [log] commit* commit* ACK ACK end forget the txn 13
2PC – Abort Example Subordinate returns VOTE NO if Coord Subord1 Subord2 the transaction is aborted PREPARE • Subordinate can release locks and forget the transaction abort* prepare* VOTE NO VOTE YES 14
2PC – Abort Example Subordinate returns VOTE NO if Coord Subord1 Subord2 the transaction is aborted PREPARE • Subordinate can release locks and forget the transaction abort* prepare* VOTE NO VOTE YES Skip the commit phase for abort* aborted subordinates ABORT back to caller abort* ACK end forget the txn 15
2PC – All Subordinates Abort Skip the second phase entirely if Coord Subord1 Subord2 the transaction aborts at all the PREPARE subordinates abort* abort* VOTE NO VOTE NO abort* + end back to caller forget the txn 16
2PC – Failures Coord Subord Use timeout to detect failures PREPARE Time out prepare* / abort* Subordinate timeout VOTE YES/NO • Waiting for PREPARE: self abort commit* / abort* COMMIT/ABORT back to caller commit* / abort* ACK end forget the txn 17
2PC – Failures Coord Subord Use timeout to detect failures PREPARE prepare* / abort* Subordinate timeout VOTE YES/NO • Waiting for PREPARE: self abort Time out commit* / abort* COMMIT/ABORT back to caller Coordinator timeout commit* / abort* • Waiting for vote: self abort ACK end forget the txn 18
2PC – Failures Coord Subord Use timeout to detect failures PREPARE prepare* / abort* Subordinate timeout VOTE YES/NO • Waiting for PREPARE: self abort • Waiting for decision: contact commit* / abort* coordinator or peer subordinates COMMIT/ABORT ( may block and wait indefinitely ) Time out back to caller commit* / abort* Coordinator timeout • Waiting for vote: self abort ACK end forget the txn 19
2PC – Failures Coord Subord Use timeout to detect failures PREPARE prepare* / abort* Subordinate timeout VOTE YES/NO • Waiting for PREPARE: self abort • Waiting for decision: contact commit* / abort* coordinator or peer subordinates COMMIT/ABORT ( may block and wait indefinitely ) back to caller commit* / abort* Coordinator timeout • Waiting for vote: self abort ACK • Waiting for ACK: contact end Time out subordinates forget the txn 20
2PC – Alternative Designs? Coord Subord Subordinate returns vote to PREPARE coordinator before logging VOTE YES/NO prepare? prepare commit* COMMIT/ABORT back to caller commit* ACK end forget the txn 21
2PC – Alternative Designs? Coord Subord Subordinate returns vote to PREPARE coordinator before logging VOTE YES/NO prepare? prepare commit* COMMIT/ABORT Problem : subordinate may back to caller crash before the log record is commit* written to disk. The log record is thus lost but the coordinator ACK end already committed the forget the txn transaction 22
2PC – Alternative Designs? Coord Subord Coordinator sends decision to PREPARE subordinates before logging the VOTE YES/NO decision? prepare* COMMIT/ABORT commit commit* back to caller ACK end forget the txn 23
2PC – Alternative Designs? Coord Subord Coordinator sends decision to PREPARE subordinates before logging the VOTE YES/NO decision? prepare* COMMIT/ABORT Problem : coordinator crashes commit before logging the decision and commit* decides to abort after restart back to caller ACK end forget the txn 24
Optimization 1: Presumed Abort (PA) Observation : It is safe for a coordinator to “forget” a transaction immediately after it makes the decision to abort it and to write an abort record 25
PA: Aborted Transaction Coord Subord1 Subord2 Coord Subord1 Subord2 PREPARE back to PREPARE caller abort prepare* abort* prepare* VOTE NO VOTE NO VOTE YES Presumed Abort • The abort record is not forced in subordinate Standard 2PC 26
PA: Aborted Transaction Coord Subord1 Subord2 Coord Subord1 Subord2 PREPARE back to PREPARE caller abort prepare* abort* prepare* VOTE NO forget VOTE NO abort the txn VOTE YES VOTE YES abort* ABORT ABORT abort back to caller abort* Presumed Abort ACK • The abort record is not forced in subordinate end • The abort record is not forced in coordinator forget the txn Standard 2PC • Coordinator forgets the transaction early • No ACK for aborts • Behavior of committed transactions unchanged 27
PA: Partially Readonly Transactions Coord Subord1 Subord2 Coord Subord1 Subord2 PREPARE PREPARE prepare* prepare* VOTE READ prepare* VOTE YES VOTE YES VOTE YES commit* commit* COMMIT COMMIT back to caller back to caller commit* commit* commit* ACK ACK ACK end end forget the txn forget the txn Readonly subordinate does not log in prepare phase and skips commit phase 28
PA: Completely Readonly Transactions Coord Subord1 Subord2 Coord Subord1 Subord2 PREPARE PREPARE VOTE READ prepare* prepare* VOTE READ VOTE YES VOTE YES back to caller commit* forget the txn COMMIT back to caller commit* commit* ACK ACK end forget the txn Completely readonly transactions skip the commit phase entirely 29
Optimization 2: Presumed Commit (PC) Since most transactions are expected to commit, can we make commits cheaper by eliminating the ACKs for COMMITS? 30
PC: Committed Transaction Coord Subord1 Subord2 Coord Subord1 Subord2 PREPARE collecting* PREPARE prepare* prepare* VOTE YES prepare* prepare* VOTE YES VOTE YES commit* VOTE YES COMMIT commit* back to caller COMMIT commit* commit* back to caller ACK ACK commit commit end forget the txn Need to force log collecting due to potential abort of coordinator No need to send ACK for COMMITS 31
PC: Aborted Transaction Coord Subord1 Subord2 Coord Subord1 Subord2 PREPARE collecting* PREPARE abort* prepare* VOTE NO abort* prepare* VOTE YES VOTE NO abort* VOTE YES ABORT abort* back to caller COMM IT abort* back to caller ACK abort* ACK end end forget the txn forget the txn Abort behavior is similar to standard 2PC but requires logging collecting 32
Summary Presumed Abort (PA) is better than standard 2PC (widely used in practice) Presumed Commit (PC) is worse than PA in most cases 33
Recommend
More recommend