PMIT-6102 Advanced Database Systems ByJesmin Akhter Assistant Professor, IIT, Jahangirnagar University Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 1 Lecture -11 Distributed Concurrency Control Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 2 Outline Transaction Role of the distributed execution monitor Schedule Detailed Model of the Distributed Execution Monitor, Distributed Concurrency Control Serializability in Distributed DBMS Concurrency Control Algorithms time-stamping, Deadlock Centralized Deadlock Detection Distributed Deadlock Detection Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 3 Role of the distributed execution monitor The distributed execution monitor consists of two modules: a transaction manager (TM) and a scheduler (SC). The transaction manager is responsible for coordinating the execution of the database operations on behalf of an application. The scheduler, on the other hand, is responsible for the implementation of a specific concurrency control algorithm for synchronizing access to the database. A third component that participates in the management of distributed transactions is the local recovery managers (LRM) that exist at each site. Distributed DBMS Their function is to implement the local procedures by which the local database can be recovered to a consistent state following a failure. Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 4 Role of the distributed execution monitor Each transaction originates at one site, which we will call its originating site. The execution of the database operations of a transaction is coordinated by the TM at that transaction’s originating site. The transaction managers implement an interface for the application programs which consists of five commands: begin transaction, read, write, commit, and abort. Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 5 Role of the distributed execution monitor Begin transaction: This is an indicator to the TM that a new transaction is starting. The TM does some bookkeeping, such as recording the transaction’s name, the originating application, and so on, in coordination with the data processor. Read: If the data item to be read is stored locally, its value is read and returned to the transaction. Otherwise, the TM finds where the data item is stored and requests its value to be returned (after appropriate concurrency control measures are taken). Write: If the data item is stored locally, its value is updated (in coordination with the data processor). Otherwise, the TM finds where the data item is located and requests the update to be carried out at that site after appropriate concurrency control measures are taken). Commit: The TM coordinates the sites involved in updating data items on behalf of this transaction so that the updates are made permanent at every site. Abort The TM makes sure that no effects of the transaction are reflected in any of the databases at the sites where it updated data items. Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 6 Detailed Model of the Distributed Execution Monitor Begin_transaction, Read, Write, Commit, Abort Results Distributed Execution Monitor With other TMs Transaction Manager (TM) Scheduling/ Descheduling Requests Scheduler (SC) With other SCs To data processor Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 7 Serializability in Distributed DBMS Somewhat more involved. Two histories have to be considered: local histories global history For global transactions (i.e., global history) to be serializable, two conditions are necessary: Each local history should be serializable. Two conflicting operations should be in the same relative order in all of the local histories where they appear together. Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 8 Global Non-serializability T1: Read(x) x x5 Write(x) Commit T2: Read(x) x x15 Write(x) Commit The following two local histories are individually serializable (in fact serial), but the two transactions are not globally serializable. LH1={R1(x),W1(x),C1,R2(x),W2(x),C2} LH2={R2(x),W2(x),C2,R1(x),W1(x),C1} Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 9 Concurrency Control Algorithms Pessimistic Two-Phase Locking-based (2PL) Centralized (primary site) 2PL Primary copy 2PL Distributed 2PL Timestamp Ordering (TO) Basic TO Multiversion TO Conservative TO Hybrid Optimistic Locking-based Timestamp ordering-based Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 10 Locking-Based Algorithms Transactions indicate their intentions by requesting locks from the scheduler (called lock manager). Locks are either read lock (rl) [also called shared lock] or write lock (wl) [also called exclusive lock] Read locks and write locks conflict (because Read and Write operations are incompatible rl wl rl yes no wl no no Locking works nicely to allow concurrent processing of transactions. Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 11 Two-Phase Locking (2PL) A Transaction locks an object before using it. When an object is locked by another transaction, the requesting transaction must wait. When a transaction releases a lock, it may not request another lock. Lock point Obtain lock No. of locks Release lock Phase 1 BEGIN Distributed DBMS Phase 2 END Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 12 Strict 2PL Hold locks until the end. Obtain lock Release lock BEGIN END period of data item use Distributed DBMS Transaction duration Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 13 Testing for Serializability Consider transactions T1, T2, …, Tk Create a directed graph (called a conflict graph), whose nodes are transactions. Consider a history of transactions. If T1 unlocks an item and T2 locks it afterwards, draw an edge from T1 to T2 implying T1 must precede T2 in any serial history T1→T2 Repeat this for all unlock and lock actions for different transactions. If graph has a cycle, the history is not serializable. If graph is a cyclic, a topological sorting will give the serial history. Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 14 Example T1: T1: T2: T2: T2: T2: T3: T3: Lock X Unlock X Lock X Lock Y Unlock X Unlock Y Lock Y Unlock Y T1→T2 T2→T3 T T 1 2 T 3 Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 15 Theorem Two phase locking is a sufficient condition to ensure serializablility. Proof: By contradiction. If history is not serializable, a cycle must exist in the conflict graph. This means the existence of a path such as T1→T2→T3 … Tk → T1. This implies T1 unlocked before T2 and after Tk. T1 requested a lock again. This violates the condition of two phase locking. Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 16 Centralized 2PL Only one of the sites has a lock manager; The transaction managers at the other sites communicate with it rather than with their own lock managers. This approach is also known as the primary site 2PL algorithm The communication between the cooperating sites in executing a transaction A centralized 2PL (C2PL) algorithm is depicted in Figure in next slide. This communication is between the transaction manager at the site where the transaction is initiated (called the coordinating TM), the lock manager at the central site, and the data processors (DP) at the other participating sites. The participating sites are those that store the data item and at which the operation is to be carried out. The order of messages is denoted in the figure. Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 17 Centralized 2PL There is only one 2PL scheduler in the distributed system. Lock requests are issued to the central scheduler. Data Processors at participating sites Distributed DBMS Coordinating TM Central Site LM Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 18 Distributed 2PL 2PL schedulers are placed at each site. Each scheduler handles lock requests for data at that site. A transaction may read any of the replicated copies of item x, by obtaining a read lock on one of the copies of x. Writing into x requires obtaining write locks for all copies of x. Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 19 Distributed 2PL Execution Coordinating TM Distributed DBMS Participating LMs Participating DPs Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 20 Timestamp Ordering Transaction (Ti) is assigned a globally unique timestamp ts(Ti). Transaction manager attaches the timestamp to all operations issued by the transaction. Each data item is assigned a write timestamp (wts) and a read timestamp (rts): rts(x) = largest timestamp of any read on x wts(x) = largest timestamp of any read on x Conflicting operations are resolved by timestamp order. Basic T/O: for Ri(x) if ts(Ti) < wts(x) then reject Ri(x) else accept Ri(x) rts(x) ts(Ti) Distributed DBMS for Wi(x) if ts(Ti) < rts(x) and ts(Ti) < wts(x) then reject Wi(x) else accept Wi(x) wts(x) ts(Ti) Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 21 Optimistic Concurrency Control Algorithms Transaction execution model: divide into subtransactions each of which execute at a site Tij: transaction Ti that executes at site j Transactions run independently at each site until they reach the end of their read phases All subtransactions are assigned a timestamp at the end of their read phase Validation test performed during validation phase. If one fails, all rejected. Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 22 Optimistic Concurrency Control Processing Start Integrity Control & Local Validation Read, Compute, And Write Local Success Semi-Commit On Initiating Site Fail Commit, Global Write Finish Success Integrity Control & Local Validation Fail Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 23 Optimistic CC Validation Test If all transactions Tk where ts(Tk) < ts(Tij) have completed their write phase before Tij has started its read phase, then validation succeeds Transaction executions in serial order Tk R V W Tij Distributed DBMS R V Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU W Slide 24 Optimistic CC Validation Test If there is any transaction Tk such that ts(Tk)<ts(Tij) and which completes its write phase while Tij is in its read phase, then validation succeeds if WS(Tk) RS(Tij) = Ø Read and write phases overlap, but Tij does not read data items written by Tk Tk R V W Tij Distributed DBMS R V W Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 25 Optimistic CC Validation Test If there is any transaction Tk such that ts(Tk)< ts(Tij) and which completes its read phase before Tij completes its read phase, then validation succeeds if WS(Tk) RS(Tij) = Ø and WS(Tk) WS(Tij) = Ø They overlap, but don't access any common data items. R Tk Tij Distributed DBMS V R W V W Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 26 Deadlock A transaction is deadlocked if it is blocked and will remain blocked until there is intervention. Locking-based CC algorithms may cause deadlocks. TO-based algorithms that involve waiting may cause deadlocks. Wait-for graph If transaction Ti waits for another transaction Tj to release a lock on an entity, then Ti Tj in WFG. Ti Distributed DBMS Tj Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 27 Local versus Global WFG Assume T1 and T2 run at site 1, T3 and T4 run at site 2. Also assume T3 waits for a lock held by T4 which waits for a lock held by T1 which waits for a lock held by T2 which, in turn, waits for a lock held by T3. Local WFG Site 1 Site 2 T1 T4 T2 T3 Global WFG Distributed DBMS T1 T4 T2 T3 Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 28 Deadlock Management Ignore Let the application programmer deal with it, or restart the system Prevention Guaranteeing that deadlocks can never occur in the first place. Check transaction when it is initiated. Requires no run time support. Avoidance Detecting potential deadlocks in advance and taking action to insure that deadlock will not occur. Requires run time support. Detection and Recovery Allowing deadlocks to form and then finding and breaking them. As in the avoidance scheme, this requires run time support. Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 29 Deadlock Avoidance Transactions are not required to request resources a priori. Transactions are allowed to proceed unless a requested resource is unavailable. In case of conflict, transactions may be allowed to wait for a fixed time interval. Order either the data items or the sites and always request locks in that order. More attractive than prevention in a database environment. Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 30 Deadlock Avoidance – Wait-Die & Wound-Wait Algorithms WAIT-DIE Rule: If Ti requests a lock on a data item which is already locked by Tj, then Ti is permitted to wait iff ts(Ti)<ts(Tj). If ts(Ti)>ts(Tj), then Ti is aborted and restarted with the same timestamp. if ts(Ti)<ts(Tj) then Ti waits else Ti dies non-preemptive: Ti never preempts Tj prefers younger transactions WOUND-WAIT Rule: If Ti requests a lock on a data item which is already locked by Tj , then Ti is permitted to wait iff ts(Ti)>ts(Tj). If ts(Ti)<ts(Tj), then Tj is aborted and the lock is granted to Ti. if ts(Ti)<ts(Tj) then Tj is wounded else Ti waits preemptive: Ti preempts Tj if it is younger prefers older transactions Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 31 Deadlock Detection Transactions are allowed to wait freely. Wait-for graphs and cycles. Topologies for deadlock detection algorithms Centralized Distributed Hierarchical Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 32 Centralized Deadlock Detection One site is designated as the deadlock detector for the system. Each scheduler periodically sends its local WFG to the central site which merges them to a global WFG to determine cycles. How often to transmit? Too often higher communication cost but lower delays due to undetected deadlocks Too late higher delays due to deadlocks, but lower communication cost Would be a reasonable choice if the concurrency control algorithm is also centralized. Proposed for Distributed INGRES Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 33 Distributed Deadlock Detection Sites cooperate in detection of deadlocks. One example: The local WFGs are formed at each site and passed on to other sites. Each local WFG is modified as follows: Since each site receives the potential deadlock cycles from other sites, these edges are added to the local WFGs The edges in the local WFG which show that local transactions are waiting for transactions at other sites are joined with edges in the local WFGs which show that remote transactions are waiting for local ones. Each local deadlock detector: Distributed DBMS looks for a cycle that does not involve the external edge. If it exists, there is a local deadlock which can be handled locally. looks for a cycle involving the external edge. If it exists, it indicates a potential global deadlock. Pass on the information to the next site. Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 34 Thank you Distributed DBMS Lectured by, Jesmin Akhter, Assistant Professor, IIT, JU Slide 35