chap 16. transactions. transactions transaction: sequence of operations such that the entire...
TRANSCRIPT
Chap 16. Transactions
Transactions
Transaction: sequence of operations such that the entire sequence appears as one indivisible operation
Indivisibility is maintained even in the case of concurrency and failures
Example: Begin transaction
withdraw x from account A;deposit x to account B;
End transaction
Transaction: Implementing the following primitives begin_transaction end_transaction : commit the transaction abort_transaction : all values prior to
the transaction are restored read, write : the program can read or
write objects
ACID properties
Atomicity: all or nothing Consistency: should not violate integrity
constraints of the system Isolation: transactions are isolated from the
effects of concurrent transactions Durability: once a transaction has been
committed it remains permanent even if there are failures
Concurrency Control
T1 T2
May lead to violation of isolation if both are executed simultaneously
Two-Phase locking
Lock data before
accessing Unlock data only
at the end
Dealing with failures
Private workspace : Copy the objects that have been updated by the
transaction Discard copy if transaction aborts, overwrite
original if transaction commits Logging:
Maintain a trail of all writes so that they can be undone if necessary
Distributed commit
Agreement: No two processes decide on different outcomes of the transaction
Validity: If any process starts with abort then abort is the only possible final outcome
Weak termination: If there are no failures, all processes eventually decide
Non-blocking: All processes eventually decide
Two phase protocol satisfying first 3 properties Phase 1 The coordinator sends a request message to
all participants On receiving a request message, each
participant replies with either a ‘yes’ or a ‘no’ message. A ‘yes’ message signifies that the participant can
commit all the actions performed at its site.
Two Phase Protocol (contd.)
Phase 2 The coordinator waits to receive messages
from all participants. If all of them are ‘yes’, then the coordinator sends
the finalCommit message. Otherwise, it sends a finalAbort message.
The participant carries out the action associated with the message received from the coordinator.
//Coordinatorpublic class TwoPhaseCoord extends Process { boolean globalCommit = false; boolean donePhase1 = false; boolean noReceived = false; int numParticipants; int numReplies = 0; public TwoPhaseCoord(Linker initComm) { super(initComm); numParticipants = N - 1; } public synchronized void doCoordinator() { // Phase 1 broadcastMsg("request", myId); while (!donePhase1) myWait();
// Phase 2 if (noReceived) broadcastMsg("finalAbort", myId); else { globalCommit = true; broadcastMsg("finalCommit", myId); } } public synchronized void handleMsg(Msg m, int src, String tag) { if (tag.equals("yes")) { numReplies++; if (numReplies == numParticipants) { donePhase1 = true; notify(); } } else if (tag.equals("no")) { noReceived = true; donePhase1 = true; notify();} } }
public class TwoPhaseParticipant extends Process { boolean localCommit; boolean globalCommit; boolean done = false; boolean hasProposed = false; public TwoPhaseParticipant(Linker initComm) { super(initComm); } public synchronized void propose(boolean vote) { localCommit = vote; hasProposed = true; notify(); } public synchronized boolean decide() { while (!done) myWait(); return globalCommit; } public synchronized void handleMsg(Msg m, int src, String tag) { while (!hasProposed) myWait(); if (tag.equals("request")) { if (localCommit) sendMsg(src, "yes"); else sendMsg(src, "no"); } else if (tag.equals("finalCommit")) { globalCommit = true; done = true; notify(); } else if (tag.equals("finalAbort")) { globalCommit = false; done = true; notify();} } }
Analysis
If coordinator fails in the second phase before informing any participant then all participants have to wait for the coordinator to recover
Hence this protocol is blocking