Presentation is loading. Please wait.

Presentation is loading. Please wait.

TRANSACTIONS (AND EVENT-DRIVEN PROGRAMMING) EE324.

Similar presentations


Presentation on theme: "TRANSACTIONS (AND EVENT-DRIVEN PROGRAMMING) EE324."— Presentation transcript:

1 TRANSACTIONS (AND EVENT-DRIVEN PROGRAMMING) EE324

2 Concurrency Control  General organization of managers for handling transactions. 2

3 Two-phase locking is “pessimistic”  Acts to prevent non-serializable schedules from arising: pessimistically ass umes conflicts are fairly likely  Can deadlock, e.g. T1 reads x then writes y; T2 reads y then writes x. T his doesn’t always deadlock but it is capable of deadlocking  Overcome by aborting if we wait for too long,  Or by designing transactions to obtain locks in a known and agreed upon ordering 3

4 4 Transactions T and U with exclusive locks

5 Schemes for Concurrency control  Locking  Optimistic concurrency control (CDK516.5)  Time-stamp based concurrency control (not going to cover) 5

6 6 Optimistic Concurrency Control  Drawbacks of locking  Overhead of lock maintenance  Deadlocks  Reduced concurrency  Optimistic Concurrency Control  In most applications, likelihood of conflicting accesses by concurrent transacti ons is low  Transactions proceed as though there are no conflicts

7 Optimistic Concurrency Control 7  Three phases:  Working Phase – transactions read and write private copies of objects ( most recently committed)  Validation Phase – Once transaction is done, the transaction is validated to establish whether or not its operations on objects conflict with operation s of other transactions on the same object. If not conflict, can commit; else some form of conflict resolution is needed and the transaction may abort.  Update Phase – if commit, private copies are used to make permanent c hange.

8 Validation of transactions 8 Earlier committed transactions WorkingValidationUpdate T 1 T v Transaction being validated T 2 T 3 Later active transactions active 1 2

9 Validation – conflict detection TvTiRule WriteReadTi must not read objects written by Tv ReadWriteTv must not read objects written by Ti Write Ti must not write objects written by Tv and Tv must not write objects written by Ti

10 Optimistic concurrency control  Transactions are numbered.  Validation and update occurs in side the critical section. (Satisfies rule 3)  Backward validation  Rule 1 is satisfied because all read operations of earlier overlapping transactions were performed before the validation of Tv started; they cannot be affected by Tv’s write.  Read set of Tv must be compared with the write sets of T2 and T3. (Rule 2)  In other words, the read set of the transaction being validated is compared with the write set of other overlapping transactions that have already committed.  Thus, we need to keep the write set of T2 and T3 for a while after their commit.

11 Today's Lecture 11  Distributed transactions

12 Distributed Transactions 12  Motivation  Provide distributed atomic operations at multiple servers that maintain share d data for clients  Provide recoverability from server crashes  Properties  Atomicity, Consistency, Isolation, Durability (ACID)  Concepts: commit, abort, distributed commit

13 Transactions in distributed systems 13  Main issue that arises is that now we can have multiple database serve rs that are touched by one transaction  Reasons?  Data spread around: each owns subset  Could have replicated some data object on multiple servers, e.g. to load-ba lance read access for large client set  Might do this for high availability

14 Atomic Commit Protocols 14  The atomicity of a transaction requires that when a distributed transact ion comes to an end, either all of its operations are carried out or non e of them  One phase commit  Coordinator tells all participants (servers) to commit Keep on repeating it until all participants reply If a participant cannot commit (say because of concurrency control), no way to infor m coordinator. Also, no way for the coordinator to abort.

15 The two-phase commit protocol (2PC)  Designed to allow any participant to abort its part of transaction  But this means, the whole transaction must be aborted  Why?  First phase: all participants vote (abort or commit). If voted commit, make all changed permanent (durability) and go to prepared state. Log this fact.  Participants will eventually commit (if the coordinator says so) even it crashes.  Second phase: Joint decision

16 The two-phase commit protocol - 1 16 Phase 1 (voting phase): 1. The coordinator sends a canCommit? (VOTE_REQUEST) request to each of the participants in the transaction. 2. When a participant receives a canCommit? request it repli es with its vote Yes (VOTE_COMMIT) or No (VOTE_AB ORT) to the coordinator. Before voting Yes, it prepares to commit by saving objects in permanent storage. If the vot e is No the participant aborts immediately.

17 The two-phase commit protocol - 2 17 Phase 2 (completion according to outcome of vote): 3. The coordinator collects the votes (including its own). (a)If there are no failures and all the votes are Yes the coordinator d ecides to commit the transaction and sends a doCommit (GLOBA L_COMMIT) request to each of the participants. (b)Otherwise the coordinator decides to abort the transaction and se nds doAbort (GLOBAL_ABORT) requests to all participants that voted Yes. 4. Participants that voted Yes are waiting for a doCommit or doAbort reques t from the coordinator. When a participant receives one of these messag es it acts accordingly and in the case of commit, makes a haveCommitte d call as confirmation to the coordinator.

18 Communication in two-phase commit protocol 18 canCommit? Yes doCommit haveCommitted Coordinator 1 3 (waiting for votes) committed done prepared to commit step Participant 2 4 (uncertain) prepared to commit committed statusstepstatus

19 Commit protocol illustrated 19 ok to commit?

20 Commit protocol illustrated 20 ok to commit? ok with us commit

21 Operations for two-phase commit protocol 21 canCommit?(trans)-> Yes / No Call from coordinator to participant to ask whether it can commit a transaction. Participa nt replies with its vote. doCommit(trans) Call from coordinator to participant to tell participant to commit its part of a transaction. doAbort(trans) Call from coordinator to participant to tell participant to abort its part of a transaction. haveCommitted(trans, participant) Call from participant to coordinator to confirm that it has committed the transaction. getDecision(trans) -> Yes / No Call from participant to coordinator to ask for the decision on a transaction after it has vo ted Yes but has still had no reply after some delay. Used to recover from server crash or d elayed messages.

22 Two-Phase Commit protocol – 3 (TV sec 8.5) 22  actions by coordinator: while START _2PC to local log; multicast VOTE_REQUEST to all participants; while not all votes have been collected { wait for any incoming vote; if timeout { write GLOBAL_ABORT to local log; multicast GLOBAL_ABORT to all participants; exit; } record vote; } if all participants sent VOTE_COMMIT and coordinator votes COMMIT{ write GLOBAL_COMMIT to local log; multicast GLOBAL_COMMIT to all participants; } else { write GLOBAL_ABORT to local log; multicast GLOBAL_ABORT to all participants; }

23 Two-Phase Commit protocol - 4 23  actions by participant: write INIT to local log; wait for VOTE_REQUEST from coordinator; if timeout { write VOTE_ABORT to local log; exit; } if participant votes COMMIT { write VOTE_COMMIT to local log; send VOTE_COMMIT to coordinator; wait for DECISION from coordinator; if timeout { multicast DECISION_REQUEST to other participants; wait until DECISION is received; /* remain blocked */ write DECISION to local log; } if DECISION == GLOBAL_COMMIT write GLOBAL_COMMIT to local log; else if DECISION == GLOBAL_ABORT write GLOBAL_ABORT to local log; } else { write VOTE_ABORT to local log; send VOTE ABORT to coordinator; }

24 Two-Phase Commit protocol - 5 24 a) The finite state machine for the coordinator in 2PC. b) The finite state machine for a participant.  Coordinator and participants have blocking state. When a fa ilure occurs, other process may be indefinitely waiting.  There needs to be a timeout mechanism.

25 Two Phase Commit Protocol - 6 25 Timeouts  ‘Wait’ in Coordinator – use a time-out mechanism to detect participant crashes. Send GLOBAL_ABORT  ‘Init’ in Participant – Can also use a time-out and send VOTE_ABORT  ‘Ready’ in Participant P – abort is not an option (since already voted to COM MIT and so coordinator might eventually send GLOBAL_COMMIT). Can contac t another participant Q and choose an action based on its state. State of QAction by P COMMITTransition to COMMIT ABORTTransition to ABORT INITBoth P and Q transition to ABORT (Q sends VOTE_ABORT) READYContact more participants. If all participants are ‘READY’, must wait for coordinator to recover

26 Two Phase Commit Protocol - 7  Recovery  To ensure that a process can actually recover, it must save its state to persistent storage.  If a participant was in INIT (before crash), it can safely decide to locally abort when it recovers and inform the coordinator.  If it was COMMIT and ABORT, retransmit its decision to the coordinator.  If it was READY, contact other participant Q (Send DECISION_REQUEST), similar to the timeout situation.

27 Two-Phase Commit protocol - 8 27  actions for handling decision requests: /* executed by separate thread */ while true { wait until any incoming DECISION_REQUEST is received; /* rem ain blocked */ read most recently recorded STATE from the local log; if STATE == GLOBAL_COMMIT send GLOBAL_COMMIT to requesting participant; else if STATE == INIT or STATE == GLOBAL_ABORT send GLOBAL_ABORT to requesting participant; else skip; /* participant remains blocked */

28 Three Phase Commit protocol - 1 28  Problem with 2PC  If coordinator crashes, participants cannot reach a decision, stay blocked unt il coordinator recovers  Three Phase Commit (3PC): proof in [SS 1983]  There is no single state from which it is possible to make a transition directly to either COMMIT or ABORT states  There is no state in which it is not possible to make a final decision, and from which a transition to COMMIT can be made

29 Three-Phase Commit protocol - 2 29 a) Finite state machine for the coordinator in 3PC b) Finite state machine for a participant

30 Three Phase Commit Protocol - 3 30 Recovery  ‘Wait’ in Coordinator – same  ‘Init’ in Participant – same  ‘PreCommit’ in Coordinator – Some participant has crashed but we know it wanted to commit. GLOBAL_COMMIT the application knowing that once the participant recovers, it will commit.  ‘Ready’ or ‘PreCommit’ in Participant P – (i.e. P has voted to COMMIT) State of QAction by P PRECOMMITTransition to PRECOMMIT. If all participants in PRECOMMIT and form a majority, then COMMIT the transaction ABORTTransition to ABORT INITBoth P (in READY) and Q transition to ABORT (Q sends VOTE_ABORT). It can be shown that no other participants can be in PRECOMMIT READYContact more participants. If can contact a majority and they are in ‘Ready’, then ABORT the transaction. If the participants contacted in ‘PreCommit’ it is safe to COMMIT the transaction Note: if any participant is in state PRECOMMIT, it is impossible for any other participant to be in any state other than READY or PRECOMMIT.

31 Things we have learned so far…  ACID  Concurrency control  Distributed atomic commit

32 Two Views of Distributed Systems  Optimist: A distributed system is a collection of independent computer s that appears to its users as a single coherent system  Pessimist: “You know you have one when the crash of a computer you’ ve never heard of stops you from getting any work done.” (Lamport) 32

33 Recurring Theme  Academics like:  Clean abstractions  Strong semantics  Things that prove they are smart  Users like:  Systems that work (most of the time)  Systems that scale  Consistency per se isn’t important  Eric Brewer had the following observations 33

34 A Clash of Cultures  Classic distributed systems: focused on ACID semantics (transaction seman tics)  Atomicity: either the operation (e.g., write) is performed on all replicas or is no t performed on any of them  Consistency: after each operation all replicas reach the same state  Isolation: no operation (e.g., read) can see the data from another operation (e. g., write) in an intermediate state  Durability: once a write has been successful, that write will persist indefinitely  Modern Internet systems: focused on BASE  Basically Available  Soft-state (or scalable)  Eventually consistent 34

35 ACID vs BASE ACID  Strong consistency for transacti ons highest priority  Availability less important  Pessimistic  Rigorous analysis  Complex mechanisms BASE  Availability and scaling highest priorities  Weak consistency  Optimistic  Best effort  Simple and fast 35

36 Why Not ACID+BASE?  What goals might you want from a system?  C, A, P  Strong Consistency: all clients see the same view, even in the presence of u pdates  High Availability: all clients can find some replica of the data, even in the presence of failures  Partition-tolerance: the system properties hold even when the system is part itioned 36

37 CAP Theorem [Brewer]  You can only have two out of these three properties  The choice of which feature to discard determines the nature of your s ystem 37

38 Consistency and Availability  Comment:  Providing transactional semantics requires all functioning nodes to be in contact with each other (no partition)  Examples:  Single-site and clustered databases  Other cluster-based designs  Typical Features:  Two-phase commit  Cache invalidation protocols  Classic DS style 38

39 Partition-Tolerance and Availability  Comment:  Once consistency is sacrificed, life is easy….  Examples:  DNS  Web caches  Practical distributed systems for mobile environments: Coda, Bayou  Typical Features:  Optimistic updating with conflict resolution  This is the “Internet design style”  TTLs and lease cache management 39

40 Voting with their Clicks  In terms of large-scale systems, the world has voted with their clicks:  Consistency less important than availability and partition-tolerance 40


Download ppt "TRANSACTIONS (AND EVENT-DRIVEN PROGRAMMING) EE324."

Similar presentations


Ads by Google