EE324 INTRO. TO DISTRIBUTED SYSTEMS LECTURE 13 TRANSACTIONS.

Slides:



Advertisements
Similar presentations
From Coulouris, Dollimore, Kindberg and Blair Distributed Systems: Concepts and Design Edition 5, © Addison-Wesley 2012 Slides for Chapter 16: Transactions.
Advertisements

Concurrency Control WXES 2103 Database. Content Concurrency Problems Concurrency Control Concurrency Control Approaches.
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
CS 542: Topics in Distributed Systems Transactions and Concurrency Control.
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Concurrency Control Chapter 17 Sections
Lock-Based Concurrency Control
Lecture 11 Recoverability. 2 Serializability identifies schedules that maintain database consistency, assuming no transaction fails. Could also examine.
1 Chapter 3. Synchronization. STEMPusan National University STEM-PNU 2 Synchronization in Distributed Systems Synchronization in a single machine Same.
1 TRANSACTION & CONCURRENCY CONTROL Huỳnh Văn Quốc Phương Thái Thị Thu Thủy
1 Distribuerede systemer og sikkerhed – 18. marts 2002 zFrom Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design zEdition 3, © Addison-Wesley.
Computer Science 425 Distributed Systems Lecture 21 Transaction Processing and Concurrency Control Reading: Sections
Lecture 11: Transactions and Concurrency Control Haibin Zhu, PhD. Assistant Professor Department of Computer Science Nipissing University © 2002.
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Concurrency Control Steve Ko Computer Sciences and Engineering University at Buffalo.
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Concurrency Control Steve Ko Computer Sciences and Engineering University at Buffalo.
CSE 486/586, Spring 2014 CSE 486/586 Distributed Systems Concurrency Control Steve Ko Computer Sciences and Engineering University at Buffalo.
Transactions and concurrency control
L-16 Transactions 1. Important Lessons – Fault Tolerance Terminology & Background  Failure models Byzantine Fault Tolerance  Protocol design  with.
Concurrency Control and Recovery In real life: users access the database concurrently, and systems crash. Concurrent access to the database also improves.
Distributed Systems 2006 Styles of Client/Server Computing.
Distributed Systems Fall 2010 Transactions and concurrency control.
CS 582 / CMPE 481 Distributed Systems Concurrency Control.
Transaction Management and Concurrency Control
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Transaction Management
1 Minggu 8, Pertemuan 15 Transaction Management Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Transaction. A transaction is an event which occurs on the database. Generally a transaction reads a value from the database or writes a value to the.
Transactions and concurrency control
Academic Year 2014 Spring Academic Year 2014 Spring.
Distributed Mutex EE324 Lecture 11.
Exercises for Chapter 16: Transactions and Concurrency Control
08_Transactions_LECTURE2 DBMSs should guarantee ACID properties (Atomicity, Consistency, Isolation, Durability). This is typically done by guaranteeing.
Lecture 16-1 Computer Science 425 Distributed Systems CS 425 / ECE 428 Fall 2013 Indranil Gupta (Indy) October 17, 2013 Lecture 16 Concurrency Control.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
Distributed Systems CS 425 / CSE 424 / ECE 428 Transactions & Concurrency Control  2010, I. Gupta, K. Nahrtstedt, S. Mitra, N. Vaidya, M. T. Harandi,
Chapter 12 Transactions 12.1 Introduction 12.2 Transactions 12.3Nested Transactions 12.4 Locks.
Transactions CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley 2002)
Concurrency Server accesses data on behalf of client – series of operations is a transaction – transactions are atomic Several clients may invoke transactions.
Distributed Processing Systems ( Concurrency Control ) 오 상 규 서강대학교 정보통신 대학원
Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses are.
Transaction Management Transparencies. ©Pearson Education 2009 Chapter 14 - Objectives Function and importance of transactions. Properties of transactions.
Slides for Chapter 12: Transactions and Concurrency Control From Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edition 3,
TRANSACTION & CONCURRENCY CONTROL 1. CONTENT 2  Transactions & Nested transactions  Methods for concurrency control  Locks  Optimistic concurrency.
NOEA/IT - FEN: Databases/Transactions1 Transactions ACID Concurrency Control.
Slides for Chapter 12: Transactions and Concurrency Control From Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edition 3,
Multidatabase Transaction Management COP5711. Multidatabase Transaction Management Outline Review - Transaction Processing Multidatabase Transaction Management.
EE324 INTRO. TO DISTRIBUTED SYSTEMS LECTURE 13 TRANSACTIONS.
Switch off your Mobiles Phones or Change Profile to Silent Mode.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Distributed Systems Lecture 12 Concurrency and replication control 1.
CSE 486/586 CSE 486/586 Distributed Systems Concurrency Control Steve Ko Computer Sciences and Engineering University at Buffalo.
Transaction Management and Concurrency Control
Concurrency Control.
Distributed Mutex EE324 Lecture 11.
Transaction Properties
CSE 486/586 Distributed Systems Concurrency Control --- 1
Chapter 10 Transaction Management and Concurrency Control
Concurrency Control WXES 2103 Database.
Chapter 15 : Concurrency Control
CSE 486/586 Distributed Systems Concurrency Control --- 2
Transaction management
Transaction Management
UNIVERSITAS GUNADARMA
Slides for Chapter 12: Transactions and Concurrency Control
CSE 486/586 Distributed Systems Concurrency Control --- 2
CSE 486/586 Distributed Systems Concurrency Control --- 1
Transactions, Properties of Transactions
Presentation transcript:

EE324 INTRO. TO DISTRIBUTED SYSTEMS LECTURE 13 TRANSACTIONS

Midterm  Midterm grading will take about a week and a half.  Assignment 3 will be out.  Thursday there will be a in-class session to prepare you for the assignment.

Last lecture  Distributed mutex

Lamport’s Shared Priority Queue  Each process i locally maintains Qi (its own version of the priority Q)  To execute critical section, you must have replies from all other processes AND your request must be at the front of Qi  When you have all replies:  All other processes are aware of your request (because the request happens before response)  You are aware of any earlier requests (assume messages from the same process are not reordered)

Lamport’s Shared Priority Queue  To enter critical section at process i :Stamp your request with the current time T  Add request to Qi  Broadcast REQUEST(T) to all processes  Wait for all replies and for T to reach front of Qi  To leave  Pop head of Qi, Broadcast RELEASE to all processes  On receipt of REQUEST(T’) from process j: Add T’ to Qi  If waiting for REPLY from j for an earlier request T, wait until j replies to you  Otherwise REPLY  On receipt of RELEASEPop head of Qi

Shared priority queue Node1: timeAction 40(start) 41Recv 42Reply to Node2: timeAction 11(start) 12Recv 13Reply to Node3: timeAction 14(start) 15Request Q:

Shared priority queue Node1: timeAction 40(start) 41Recv 42Reply to Node2: timeAction 11(start) 12Recv 13Reply to Node3: timeAction 14(start) 15Request 43Recv reply 1 44Recv reply 2 45Run critical section Q:

Shared priority queue Node1: timeAction 40(start) 41Recv 42Reply to 43Requet Node2: timeAction 11(start) 16Recv 17Reply to 18Request Node3: timeAction 14(start) 15Request 43Recv reply 1 44Recv reply 2 45Run critical section 46Recv Reply 48Recv Q:, Q:,, Q:,

Shared priority queue Node1: timeAction 40(start) 41Recv 42Reply to 43Request Node2: timeAction 11(start) 16Recv 17Reply to 18Request 50Recv reply from 1 51Recv Delay reply because is my earlier request which 1 didn’t reply Node3: timeAction 14(start) 15Request 43Recv reply 1 44Recv reply 2 45Run critical section 46Recv 47Reply to 1 48Recv 49Reply to 2 Q:, Q:,,

Shared priority queue Node1: timeAction 40(start) 41Recv 42Reply to 43Request Recv Reply to 1 Node2: timeAction 11(start) 16Recv 17Reply to 18Request 50Recv reply from 3 51Recv Recv reply from 1 Node3: timeAction 14(start) 15Request 43Recv reply 1 44Recv reply 2 45Run critical section 46Recv 47Reply to 1 48Recv 49Reply to 2 Q:, Q:,,

Shared Queue approach  Everyone eventually sees the same ordering  Ordered by Lamport’s clock.  Disadvantages: Very unreliable  Any process failure halts progress  3(N-1) messages per entry/exit  Advantages: Fair, Short synchronization delay

Lamport’s Shared Priority Queue  Advantages:  Fair  Short synchronization delay  Disadvantages:  Very unreliable (Any process failure halts progress)  3(N-1) messages per entry/exit

Today  We want to look at distributed transactions, but first we need to understand transactions in a single machine.

Today's Lecture  Reading CDK5 16.2~.4  Transaction basics  Locking and deadlock in transactions 14

Transactions  A group of operations often represent a unit of “work”.  Fundamental abstraction to group operations into a single unit of work  begin: begins the transaction  commit: attempts to complete the transaction  rollback / abort: aborts the transaction

Transactions  A transaction is a sequence of server operations that is guaranteed by the server to be atomic in the presence of multiple clients and server cr ashes.  Free from interference by operations being performed on behalf of other co ncurrent clients  Either all of the operations must be completed successfully or they must have no effect at all in the presence of server crashes 16

Transactions – The ACID Properties  The four desirable properties for reliable handling of concurren t transactions. (The alternative definition of transactions.)  Atomicity: “All or Nothing”  Consistency: Each transaction, if executed by itself, maintains the correctn ess of the database.  Isolation (Serializability): each transaction runs as if alone  Durability: once a transaction is done, it stays done. Cannot be undone. 17

Bank Operations 18 deposit(amount) deposit amount in the account withdraw(amount) withdraw amount from the account getBalance() -> amount return the balance of the account setBalance(amount) set the balance of the account to amount Operations of the Account interface bool xfer(Account src, Account dest, long x) { Transaction t = begin(); if (src.getBalance() >= x) { src.setBalance(src.getBalance() – x); dest.setBalance(dest.getBalance() + x); return t.commit(); } t.abort(); return FALSE; } A client’s banking transaction

The transactional model  Applications are coded in a stylized way: begin transaction Perform a series of read, update operations Terminate by commit or abort.  Terminology  The application is the transaction manager  The data manager is presented with operations from concurrently activ e transactions  It schedules them in an interleaved but serializable order 19

Transaction and Data Managers Transactions read update read update transactions are stateful: transaction “knows” about database contents and updates Data (and Lock) Managers 20

Transaction life histories  openTransaction()  trans;  starts a new transaction and delivers a unique TID trans. This identifier will be used in the other ope rations in the transaction.  closeTransaction(trans)  (commit, abort);  ends a transaction: a commit return value indicates that the transaction has committed; an abort retu rn value indicates that it has aborted.  abortTransaction(trans);  aborts the transaction. 21 SuccessfulAborted by clientAborted by server openTransaction operation server aborts transaction operation operation ERROR reported to client closeTransactionabortTransaction

Transactional Execution Log  As the transaction runs, it creates a history of its actions. Suppose we were to write down the sequence of operations it performs.  Data manager does this, one by one  This yields a “schedule”  Operations and order they executed  Can infer order in which transactions ran  Scheduling is called “concurrency control” 22

Instructor’s Guide for Coulouris, Dollimore, Kindberg and Blair, Distributed Systems: Concepts and Design Edn. 5 © Pearson Education 2012 Figure 16.5 The lost update problem TransactionT: balance = b.getBalance(); b.setBalance(balance*1.1); a.withdraw(balance/10) TransactionU: balance = b.getBalance(); b.setBalance(balance*1.1); c.withdraw(balance/10) balance = b.getBalance(); $200 balance = b.getBalance(); $200 b.setBalance(balance*1.1); $220 b.setBalance(balance*1.1); $220 a.withdraw(balance/10) $80 c.withdraw(balance/10) $280

Instructor’s Guide for Coulouris, Dollimore, Kindberg and Blair, Distributed Systems: Concepts and Design Edn. 5 © Pearson Education 2012 Figure 16.6 The inconsistent retrievals problem TransactionV: a.withdraw(100) b.deposit(100) TransactionW : aBranch.branchTotal() a.withdraw(100); $100 total = a.getBalance() $100 total = total+b.getBalance() $300 total = total+c.getBalance() b.deposit(100) $300

Concurrency control  Motivation: without concurrency control, we have lost updates, inconsistent retrievals, etc.  Concurrency control schemes are designed to allow two or more transacti ons to be executed correctly while maintaining serial equivalence  Serial Equivalence is correctness criterion Schedule produced by concurrency control scheme should be equivalent to a seri al schedule in which transactions are executed one after the other  Schemes: locking, optimistic concurrency control, time-stamp based concur rency control 25

Serially Equivalent Interleaving  Means that effect of the interleaved execution is indistinguishable from s ome possible serial execution of the committed transactions  For example: T1 and T2 are interleaved but it “looks like” T2 ran before T 1  Idea is that transactions can be coded to be correct if run in isolation, an d yet will run correctly when executed concurrently (and hence gain a sp eedup) 26

Need for serially equivalent interleaving Data manager interleaves operations to improve concurrency DB: R 1 (X) R 2 (X) W 2 (X) R 1 (Y) W 1 (X) W 2 (Y) commit 1 commit 2 27 T 1 : R 1 (X) R 1 (Y) W 1 (X) commit 1 T 2 : R 2 (X) W 2 (X) W 2 (Y) commit 2

Need for serially equivalent interleaving Problem: transactions may “interfere”. Here, T 2 changes x, henc e T 1 should have either run first (read and write) or after (reading the changed value). Unsafe! Not serially equivalent DB: R 1 (X) R 2 (X) W 2 (X) R 1 (Y) W 1 (X) W 2 (Y) commit 2 commit 1 T 1 : R 1 (X) R 1 (Y) W 1 (X) commit 1 T 2 : R 2 (X) W 2 (X) W 2 (Y) commit 2 28

Serially equivalent interleaving Data manager interleaves operations to improve concurrency but schedules the m so that it looks as if one transaction ran at a time. This schedule “looks” like T 2 ran first. DB: R 2 (X) W 2 (X) R 1 (X) W 1 (X) W 2 (Y) R 1 (Y) commit 2 commit 1 29 T 1 : R 1 (X) R 1 (Y) W 1 (X) commit 1 T 2 : R 2 (X) W 2 (X) W 2 (Y) commit 2

Conflicting operations  A pair of operations conflicts when their combined effect depends on the ordering.  Read and write operation conflict rules Operations of different transactions ConflictReason read NoBecause the effect of a pair of read operations does not depend on the order in which they are executed readwriteYesBecause the effect of aread and awrite operation depends on the order of their execution write YesBecause the effect of a pair ofwrite operations depends on the order of their execution

Serial equivalence property  For two transactions to be serially equivalent, it is necessary and sufficient that all pairs of conflicting operations of the two transactions be executed in the same order at all of the objects they both access.

Recovery from abort  Servers must record all the effects of committed transactions and non o f the effects of aborted transactions.  Aborted transactions can cause “dirty reads” and “premature writes”.

33 A dirty read when transaction T aborts TransactionT: a.getBalance() a.setBalance(balance + 10) TransactionU: a.getBalance() a.setBalance(balance + 20) balance = a.getBalance()$100 a.setBalance(balance + 10)$110 balance = a.getBalance()$110 a.setBalance(balance + 20) $130 commit transaction abort transaction uses result of uncommitted transaction!

Today's Lecture  Transaction basics  Locking and deadlock 34

Schemes for Concurrency control  Locking  Server attempts to gain an exclusive ‘lock’ that is about to be used by one o f its operations in a transaction.  Can use different lock types (read/write for example)  Two-phase locking  Optimistic concurrency control  Time-stamp based concurrency control 35

What about the locks?  Unlike other kinds of distributed systems, transactional systems t ypically lock the data they access  They obtain these locks as they run:  Before accessing “x” get a lock on “x”  Usually we assume that the application knows enough to get the right kind of lock. It is not good to get a read lock if you’ll later need to update the object  In clever applications, one lock will often cover many objects 36

Locking rule  Suppose that transaction T will access object x.  We need to know that first, T gets a lock that “covers” x  What does coverage entail?  We need to know that if any other transaction T’ tries to access x it will attempt to get the same lock 37

Examples of lock coverage  We could have one lock per object  … or one lock for the whole database (a global lock)  … or one lock for a category of objects  In a tree, we could have one lock for the whole tree associated with the root  In a table we could have one lock for row, or one for each column, or one for the w hole table  All transactions must use the same rules!  And if you will update the object, the lock must be a “write” lock, not a “ read” lock 38

Global lock?  Only let one transaction run at a time  Poor solution  Performance issues. bool xfer(Account src, Account dest, long x) { lock(); if (src.getBalance() >= x) { src.setBalance(src.getBalance() – x); dest.setBalance(dest.getBalance() + x); unlock(); return TRUE; } unlock(); return FALSE; }

Per-Object Locking  Other transactions can execute concurrently, as long as they don’t read or write the src or dest accounts bool xfer(Account src, Account dest, long x) { lock(src); if (src.getBalance() >= x) { src.setBalance(src.getBalance() – x); unlock(src); lock(dest); dest.setBalance(dest.getBalance() + x); unlock(dest); return TRUE; } unlock(src); return FALSE; } See any problem?

Read/Write locks  We can use different type of locks to increase concurrency.  Read/write locks. Need to respect the conflict rule.

42 Read/Write locks: Lock compatibility For one objectLock requested readwrite Lock already set noneOK readOKwait writewait Operation Conflict rules: 1. If a transaction T has already performed a read operation on a particular object, then a concurrent transaction U must not write that object until T commits or aborts 2. If a transaction T has already performed a read operation on a particular object, then a concurrent transaction U must not read or write that object until T commits or aborts

43 Strict Two-Phase Locking  Strict two-phase locking. Automatically release all locks upon commit or abort.

Why does strict 2PL imply serializability?  Suppose that T’ will perform an operation that conflicts with an operation that T has done:  T’ will update data item X that T read or updated  T updated item Y and T’ will read or update it  T must have had a lock on X/Y that conflicts with the lock that T’ wants  T won’t release it until it commits or aborts  So T’ will wait until T commits or aborts 44

45 Use of locks in strict two-phase locking 1. When an operation accesses an object within a transaction: (a)If the object is not already locked, it is locked and the operation proceeds. (b)If the object has a conflicting lock set by another transaction, the transaction m ust wait until it is unlocked. (c)If the object has a non-conflicting lock set by another transaction, the lock is s hared and the operation proceeds. (d)If the object has already been locked in the same transaction, the lock will be p romoted if necessary and the operation proceeds. (Where promotion is prevent ed by a conflicting lock, rule (b) is used.) Lock promotion: getting a more exclusive lock (e.g., read  write lock) 2. When a transaction is committed or aborted, the server unlocks all objects it locked for the transaction.

46 Deadlock with write locks TransactionT U OperationsLocksOperationsLocks a.deposit(100); write lockA b.deposit(200) write lockB b.withdraw(100) waits forU’sa.withdraw(200);waits forT’s lock onB A

Dealing with Deadlock in two-phase locking  Deadlock prevention  Acquire all needed locks in a single atomic operation  Acquire locks in a particular order  Often impractical in practice: transactions may not know which lock they may need in the future 47

Dealing with Deadlock in two-phase locking  Deadlock detection  Keep graph of locks held. Check for cycles periodically or each time an edge is added  Cycles can be eliminated by aborting transactions  Timeouts (“ignoring”)  Aborting transactions when time expires  Most transactions are short. Long-lived ones are probably deadlocked, so abort and retry. 48

49 Deadlock detection: The wait-for graph B A Waits for Held by T U U T Waits for

50 Timeouts Transaction TTransaction U OperationsLocksOperationsLocks a.deposit(100); write lock A b.deposit(200) write lock B b.withdraw(100) waits for U ’s a.withdraw(200); waits for T’s lock onB A (timeout elapses) T’s lock onA becomes vulnerable, unlockA, abort T a.withdraw(200); write locksA unlockA, B