Concurrency Control Basics
Total Page:16
File Type:pdf, Size:1020Kb
Outline l Introduction/problems, l definitions Introduction/ (transaction, history, conflict, equivalence, Problems serializability, ...), Definitions l locking. Chapter 2: Locking Concurrency Control Basics Klemens Böhm Distributed Data Management: Concurrency Control Basics – 1 Klemens Böhm Distributed Data Management: Concurrency Control Basics – 2 Atomicity, Isolation Synchronisation, Distributed (1) l Transactional guarantees – l Essential feature of databases: in particular, atomicity and isolation. Many users can access the same data concurrently – be it read, be it write. Introduction/ l Atomicity Introduction/ Problems Problems u Example, „bank scenario“: l Consistency must be guaranteed – Definitions Definitions task of synchronization component. Locking Number Person Balance Locking Klemens 5000 l Multi-user mode shall be hidden from users as far as possible: concurrent processing Gunter 200 of requests shall be transparent, u Money transfer – two elementary operations. ‚illusion‘ of being the only user. – debit(Klemens, 500), – credit(Gunter, 500). l Isolation – can be explained with this example, too. l Transactions. Klemens Böhm Distributed Data Management: Concurrency Control Basics – 3 Klemens Böhm Distributed Data Management: Concurrency Control Basics – 4 Synchronisation, Distributed (2) Synchronization in General l Serial execution of application programs Uncontrolled non-serial execution u achieves that illusion leads to other problems, notably inconsistency: l Introduction/ without any synchronization effort, Introduction/ lost updates, Problems u database consistency Problems l Inconsistent analysis („non-repeatable read“), Definitions Definitions Locking at the end of each program, Locking l dirty reads, i.e., reads of uncommitted updates, u but extremely long delays l phantoms. and insufficient utilization of resources. (Processor is idle during communication and I/O.) Klemens Böhm Distributed Data Management: Concurrency Control Basics – 5 Klemens Böhm Distributed Data Management: Concurrency Control Basics – 6 Lost Update Dirty Read l Program T1 transfers EUR 300,- l Program T2 credits interest rate based on a value from Account A to Account B, that is not part of a consistent state. Program T2 credits 3 % interest rate to Account A. l Introduction/ Introduction/ Namely, T1 is aborted later on. Problems Problems l Interest credited in Step 5 by T2 is lost, Definitions Definitions Step T1 T2 because value is overwritten in Step 6 by T1. Locking Locking 1 Read(A, a1) 2 a1 := a1-300 Step T1 T2 3 Write(A, a1) 1 Read(A, a1) 4 Read(A, a2) 2 a1 := a1-300 5 a2 := a2 *1.03 3 Read(A, a2) 6 Write(A, a2) 4 a2 := a2 *1.03 7 commit 5 Write(A, a2) 8 Read(B, b1) 6 Write(A, a1) 9 … Read(B, b1) 7 10 abort 8 b1 := b1 + 300 Not necessar ily 9 Write(B, b1) invoked by user. Klemens Böhm Distributed Data Management: Concurrency Control Basics – 7 Klemens Böhm Distributed Data Management: Concurrency Control Basics – 8 Non-Repeatable Reads Transactions l Program reads data object more than once l Execution of a program and sees modification of another program. that manipulates the database. l Representation of the execution that identifies Introduction/ Step T1 T2 Introduction/ Problems Problems u the reads and writes, Definitions 1 Read(A, a1) Definitions Locking 2 a1 := a1-300 Locking u the order of their execution, 3 Write(A, a1) u whether or not there is a commit at the end 4 Read(A, a2) (or abort). 5 a2 := a2 *1.03 6 Write(A, a2) 7 Read(A, a3) 8 … l Explain why this is neither a lost update nor a dirty read. z Klemens Böhm Distributed Data Management: Concurrency Control Basics – 9 Klemens Böhm Distributed Data Management: Concurrency Control Basics – 10 Transactions Conflict l Example: l Two operations p, q conflict := Procedure P begin p, q operate on the same data object, Start; and p or q is a write. Introduction/ Introduction/ Problems temp := Read(x); Problems l Further operations – operations Definitions temp := temp + 1; Definitions definition of conflict must be extended. Locking Write(x, temp); Locking l Example. Compatibility matrix: Commit end Read Write Increment Decrement l Representation: r [x] → w [x] → c Read y n n n 1 1 1 Write n n n n l Transaction is partial order (Σ, <) Increment n n y y (Σ will mostly be omitted in what follows.) Decrement n n y y Klemens Böhm Distributed Data Management: Concurrency Control Basics – 11 Klemens Böhm Distributed Data Management: Concurrency Control Basics – 12 Transaction – Formal Definition Reads-from Relationship between Transactions Transaction is partial order with ordering relation <, s.t. l Transaction T reads-from transaction T 1. T ⊆ {r [x], w [x]|x is a data object} ∪ {a , c }, i j i i i i i in a certain execution if Introduction/ 2. ai∈Ti ⇔ ci∉Ti; Introduction/ Problems Problems 1. Tj reads x after Ti has written x; 3. Definitions if t is ci or ai Definitions 2. Ti does not abort before Ti reads x; and Locking then for each other operation p∈Ti holds: p<i t; and Locking 3. Each transaction that writes x before Ti reads 4. if ri[x], wi[x] ∈Ti then ri[x] <i wi[x] or wi[x] <i ri[x]. x and after Tj writes x aborts before Ti reads x. Examples: l Examples: u w1[x] w2[x] r3[x] r4[x] c1 c2 c3 c4 r[x] w[x] r[x] w[x] r[x] w[y] reads-from relationships: c r[z] c T from T , T from T . Nothing else. r[y] w[y] r[y] w[y] r[y] w[x] 3 2 4 2 u w1[x] w2[x] r3[x] r4[x] c1 a2 c3 c4 is a transaction. is not a TA. is not a TA. reads-from relationships: T3 from T2, T4 from T2. Nothing else. Klemens Böhm Distributed Data Management: Concurrency Control Basics – 13 Klemens Böhm Distributed Data Management: Concurrency Control Basics – 14 Histories (1) Histories – Examples l Execution of the operations of several transactions l Two transactions given: that are ‘intertangled’ with each other, T1 r1[x] w1[x] i.e., concurrent. c1 T2 r2[x] w2[x] c2 Introduction/ Introduction/ Problems l Formally – Problems r1[y] w1[y] Definitions T = {T , T , …, T } be a set of transactions. Definitions 1 2 n l An OK complete history: Locking Complete history H over T := Locking r2[x] w2[x] c2 partial order with order relation <H, such that n r [x] w [x] 1. H = 8 T 1 1 i=1 i c1 r1[y] w1[y] n 2. < ⊇ 8 < H i=1 i l Not complete histories: r [x] w [x] c r2[x] w2[x] c2 2 2 2 3. p, q∈H have conflict p<H q or q<H p u history := prefix of a complete history. r1[x] w1[x] r1[x] w1[x] c1 c1 r1[y] w1[y] r1[y] w1[y] Klemens Böhm Distributed Data Management: Concurrency Control Basics – 15 Klemens Böhm Distributed Data Management: Concurrency Control Basics – 16 Histories (2) Histories (3) l Committed projection of history H – l History need not be ‘correct’. Abbreviation: C(H) := results from H E.g., it may contain lost updates. by removing all operations that are not committed. l Introduction/ Introduction/ Objective in what follows: Problems l Illustration: Problems Formal definition of correctness. Definitions Definitions Locking Locking r2[x] w2[x] c2 r1[x] w1[x] c1 r1[y] w1[y] Klemens Böhm Distributed Data Management: Concurrency Control Basics – 17 Klemens Böhm Distributed Data Management: Concurrency Control Basics – 18 Prefix Commit-Closedness (1) Prefix Commit-Closedness (2) l Characteristic of histories is prefix commit-closed H=o1 ... on α=”history β=”all operations γ=”history (linear contains less are reads” contains more if the following holds: to keep than 10 than 10 characteristic holds for H. Introduction/ things operations” operations” Introduction/ Problems Problems characteristic holds for C(H’), H’ is prefix of H. simple) Definitions. Definitions. (C(H) := committed projection, i.e., Prefixes: If α holds for H, If β holds for H, γ may not hold Locking Locking H’=o1 ... ol it also holds for it also holds for for H’ even if it only operations from committed transactions) H”=o1 ... H’ and H” and H’ and H” and holds for H. γ is l Note that we do not take just any prefix om any other prefix. any other prefix. not prefix into account, only committed projections. α is prefix β is prefix commit-closed. commit-closed. commit-closed. l Come up with an example of your own. Klemens Böhm Distributed Data Management: Concurrency Control Basics – 19 Klemens Böhm Distributed Data Management: Concurrency Control Basics – 20 Prefix Commit-Closedness (3) Equivalence of Histories l Rationale: l More than one definition: u correctness criterion for histories u conflict equivalence, Introduction/ must have this characteristic. Introduction/ u view equivalence. Problems Problems u Scheduler generates history, l Definitions Definitions Definition ‘conflict equivalence’: Locking but also each prefix. Locking Histories H, H’ are (conflict) equivalent if u Failure of the DBMS – 1. same transactions, same operations; history after recovery has the characteristic 2. they establish the same order as well. I.e., history should be correct as well. of operations with conflict. I.e., pi, qj, belong to Ti and Tj, respectively. ai, aj ∉H. If pi <H qj then pi <H’ qj. Klemens Böhm Distributed Data Management: Concurrency Control Basics – 21 Klemens Böhm Distributed Data Management: Concurrency Control Basics – 22 Equivalence of Histories Equivalence of Histories – Examples (1) – Examples (2) l Two transactions given: l History 1: T1 r1[x] w1[x] Step T1 T2 T3 Introduction/ c r [x] w [x] c Introduction/ 1 Read(A) Problems 1 T2 2 2 2 Problems All transactions eventually commit.