chap 16. transactions. transactions transaction: sequence of operations such that the entire...

13
Chap 16. Transactions

Upload: jerome-ramsey

Post on 17-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chap 16. Transactions. Transactions Transaction: sequence of operations such that the entire sequence appears as one indivisible operation Indivisibility

Chap 16. Transactions

Page 2: Chap 16. Transactions. Transactions Transaction: sequence of operations such that the entire sequence appears as one indivisible operation Indivisibility

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

Page 3: Chap 16. Transactions. Transactions Transaction: sequence of operations such that the entire sequence appears as one indivisible operation Indivisibility

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

Page 4: Chap 16. Transactions. Transactions Transaction: sequence of operations such that the entire sequence appears as one indivisible operation Indivisibility

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

Page 5: Chap 16. Transactions. Transactions Transaction: sequence of operations such that the entire sequence appears as one indivisible operation Indivisibility

Concurrency Control

T1 T2

May lead to violation of isolation if both are executed simultaneously

Page 6: Chap 16. Transactions. Transactions Transaction: sequence of operations such that the entire sequence appears as one indivisible operation Indivisibility

Two-Phase locking

Lock data before

accessing Unlock data only

at the end

Page 7: Chap 16. Transactions. Transactions Transaction: sequence of operations such that the entire sequence appears as one indivisible operation Indivisibility

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

Page 8: Chap 16. Transactions. Transactions Transaction: sequence of operations such that the entire sequence appears as one indivisible operation Indivisibility

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

Page 9: Chap 16. Transactions. Transactions Transaction: sequence of operations such that the entire sequence appears as one indivisible operation Indivisibility

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.

Page 10: Chap 16. Transactions. Transactions Transaction: sequence of operations such that the entire sequence appears as one indivisible operation Indivisibility

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.

Page 11: Chap 16. Transactions. Transactions Transaction: sequence of operations such that the entire sequence appears as one indivisible operation Indivisibility

//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();} } }

Page 12: Chap 16. Transactions. Transactions Transaction: sequence of operations such that the entire sequence appears as one indivisible operation Indivisibility

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();} } }

Page 13: Chap 16. Transactions. Transactions Transaction: sequence of operations such that the entire sequence appears as one indivisible operation Indivisibility

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