dbms architecture transaction...

10
1 Transaction Management Yanlei Diao UMass Amherst Slides Courtesy of R. Ramakrishnan and J. Gehrke 2 DBMS Architecture Disk Space Manager DB Access Methods Buffer Manager Query Parser Query Rewriter Query Optimizer Query Executor Lock Manager Log Manager Concurrency Control Recovery 4 Motivating Examples Banking Ticketing Have you tried to get popular concert tickets online? E-commerce Have you experienced online shopping during sales? Voting Telecommunications Online gaming… 5 Concurrent User Queries Concurrent execution of queries for improved performance. E.g., when Query 1 is doing I/O, Query 2 can utilize CPU. Improve average response time (average delay that a user query experiences). Improve system throughput (number of user queries processed in each time unit). 6 Transactions User programs (e.g., your php code) include queries but may do many things on data retrieved. E.g., add a user rating, update avg rating. E.g., search for a ticket, think about it…, and buy it. E.g., operations on Bobs bank account. DBMS is only concerned about what data is read from/ written to the database backend. A transaction is DBMSs abstract view of a user program: a sequence of reads and writes of database objects They are translated from a series of queries. We use A, B, C… to denote objects, e.g. tables, tuples 7 Concurrency Many users submit xacts, but each user thinks of his as executing by itself. DMBS interleaves reads and writes of xacts for concurrency. Consistency : each xact starts and ends with a consistent state (i.e., satisfying all integrity constraints). E.g., if an IC states that all accounts must have a positive balance, no transaction can violate this rule. Isolation : execution of one xact appears isolated from others. Nobody else can see the data in its intermediate state, e.g., account A being debited but B not being credited.

Upload: others

Post on 25-Apr-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DBMS Architecture Transaction Managementavid.cs.umass.edu/courses/445/s2015/lectures/Lec16-Xact-6up.pdf · " DBMS logs all actions (a form of redundant storage) so that it can redo

1

Transaction Management

Yanlei Diao UMass Amherst

Slides Courtesy of R. Ramakrishnan and J. Gehrke 2

DBMS Architecture

Disk Space Manager

DB

Access Methods

Buffer Manager

Query Parser

Query Rewriter

Query Optimizer

Query Executor

Lock Manager Log Manager

Concurrency Control Recovery

4

Motivating Examples v  Banking v  Ticketing

§  Have you tried to get popular concert tickets online? v  E-commerce

§  Have you experienced online shopping during sales? v  Voting v  Telecommunications v  Online gaming…

5

Concurrent User Queries

v  Concurrent execution of queries for improved performance.

§  E.g., when Query 1 is doing I/O, Query 2 can utilize CPU.

§  Improve average response time (average delay that a user query experiences).

§  Improve system throughput (number of user queries processed in each time unit).

6

Transactions

v  User programs (e.g., your php code) include queries but may do many things on data retrieved. §  E.g., add a user rating, update avg rating. §  E.g., search for a ticket, think about it…, and buy it. §  E.g., operations on Bob’s bank account.

v  DBMS is only concerned about what data is read from/written to the database backend.

v  A transaction is DBMS’s abstract view of a user program: a sequence of reads and writes of database objects §  They are translated from a series of queries. §  We use A, B, C… to denote objects, e.g. tables, tuples

7

Concurrency v  Many users submit xacts, but each user thinks of his as

executing by itself. §  DMBS interleaves reads and writes of xacts for concurrency.

v  Consistency: each xact starts and ends with a consistent state (i.e., satisfying all integrity constraints). §  E.g., if an IC states that all accounts must have a positive

balance, no transaction can violate this rule.

v  Isolation: execution of one xact appears isolated from others. §  Nobody else can see the data in its intermediate state, e.g.,

account A being debited but B not being credited.

Page 2: DBMS Architecture Transaction Managementavid.cs.umass.edu/courses/445/s2015/lectures/Lec16-Xact-6up.pdf · " DBMS logs all actions (a form of redundant storage) so that it can redo

2

8

Recovery

v  A transaction might commit after completing all its actions, or it could be aborted after executing some actions.

v  Atomicity: either all actions of a xact are performed or none of them is (all-or-none). §  DBMS logs all actions so that it can undo the actions of

aborted xacts.

v  Durability: once a user program has been notified of success, its effect will persist despite system failure. §  DBMS logs all actions (a form of redundant storage) so that it

can redo the actions of committed xacts.

9

James Gray & Turing Award

v  Jim Gray won Turing Award in 1998 for “for seminal contributions to database and

transaction processing research and technical leadership in system implementation”

10

Outline

v  Transaction management overview

v  Serializability & recoverability

v  Lock-based concurrency control

v  Recovery

11

Example

v  Consider two transactions:

T1: BEGIN A=A+100, B=B-100 END T2: BEGIN A=1.06*A, B=1.06*B END

§  No guarantee that T1 will execute before T2 or vice-versa, if both are submitted together.

v  However, the net effect must be equivalent to these two transactions running serially in some order!

12

Example (Contd.)

v  Consider a possible interleaving schedule:

T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B

v  But what about:

T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B

The DBMS’s view of the second schedule: T1: R(A), W(A), R(B), W(B) T2: R(A), W(A), R(B), W(B)

13

Scheduling Transactions

v  Serial schedule: Schedule that does not interleave the actions of different transactions.

v  Equivalent schedules: For any database state, the effect of executing the first schedule is identical to the effect of executing the second schedule.

v  Serializable schedule: A schedule that is equivalent to some serial execution of the transactions. §  If each transaction preserves consistency, every serializable

schedule preserves consistency.

Page 3: DBMS Architecture Transaction Managementavid.cs.umass.edu/courses/445/s2015/lectures/Lec16-Xact-6up.pdf · " DBMS logs all actions (a form of redundant storage) so that it can redo

3

14

Serializability

v  Serializability theory concerns the schedules of transactions that are not (explicitly) aborted.

v  Given a set of such xacts submitted on the fly, ideally want to allow any serializable schedule. §  Recognizing any serializable schedule is highly complex, if

possible.

v  Instead, allow only a subset of serializable schedules that are easy to detect.

15

Conflict Serializability

v  Two schedules are conflict equivalent if: §  Involve the same actions of the same transactions. §  Every pair of conflicting actions is ordered the same way.

v  Schedule S is conflict serializable if S is conflict equivalent to some serial schedule.

16

Dependency Graph

v  Precedence graph:

§  One node per Xact;

§  Edge from Xact Ti to Xact Tj if an action of Ti precedes and conflicts with one of Tj‘s actions (RW, WR, WW operations on the same object).

v  Theorem: Schedule is conflict serializable if and only if its precedence graph is acyclic.

17

Example

v  The schedule is not conflict serializable: §  The cycle in the graph reveals the problem. The output of

T1 depends on T2, and vice-versa. v  Solution: abort one xact to break the cycle, and restart it!

T1: R(A), W(A), R(B), W(B) T2: R(A), W(A), R(B), W(B)

T1 T2 A

B

Precedence graph

18

Recoverability

v  Recoverability theory concerns schedules that involve aborted transactions.

T1: R(A),W(A) Abort T2: R(A),W(A) Commit

Unrecoverable!

v  A schedule S is recoverable if each xact commits only after all xacts from which it read have committed.

19

Recoverability (Contd.)

v  S avoids cascading rollback if each xact may read only those values written by committed xacts.

T1: R(A),W(A) Abort T2: R(A),W(A) Abort

Recoverable, but with cascading aborts.

Page 4: DBMS Architecture Transaction Managementavid.cs.umass.edu/courses/445/s2015/lectures/Lec16-Xact-6up.pdf · " DBMS logs all actions (a form of redundant storage) so that it can redo

4

20

Recoverability (Contd.)

v  S is strict if each xact may read and write only objects previously written by committed xacts. §  No cascading aborts. §  Actions of aborted xacts can be simply undone by restoring

the original values of modified objects.

T1: R(A), W(A) Abort T2: R(A) W(A) (Commit) Recoverable, no cascading aborts, but update by T2 may be overwritten when aborting T1. So need to consider other xacts during abort!

21

Venn Diagram for Schedules

Recoverable

Avoid cascading aborts

Strict

Serial

Conflict serializable

Serializable All schedules

Commited Xacts

Also Aborted Xacts

22

An Exercise:

v  Regarding only committed xacts: §  Serializable? §  Conflict serializable?

v  Regarding both committed & aborted xacts: §  Recoverable? §  Avoids cascading aborts? §  Strict?

T1: R(A) W(A) Commit T2: W(A) Abort

23

Outline

v  Transaction management overview

v  Serializability & recoverability

v  Lock-based concurrency controls

v  Recovery

24

(1) Locking Protocol: Strict 2PL

v  Strict Two-Phase Locking (Strict 2PL) Protocol: 1.  Each Xact must obtain a S (shared) lock on object before

reading, an X (exclusive) lock on object before writing. 2.  If an Xact holds an X lock on an object, other Xact’s cannot

get a lock (S or X) on that object and become blocked. 3.  All locks held by a xact are released when it completes.

•  Other blocked xact’s can resume now.

time

#. locks

commit

Shared Exclusive

Shared Y N

Exclusive N N

Compatibility

25

Strict 2PL (contd.)

v  Theorem: Strict 2PL allows only schedules whose precedence graph is acyclic. §  Strict 2PL only allows conflict serializable schedules!

v  Strict 2PL is strict with respect to recoverability. §  A schedule is strict if each xact may read and write only

objects previously written by committed xacts. §  Strict 2PL is recoverable without anomalies related to

aborted transactions. §  Hence, it simplifies transaction aborts.

Page 5: DBMS Architecture Transaction Managementavid.cs.umass.edu/courses/445/s2015/lectures/Lec16-Xact-6up.pdf · " DBMS logs all actions (a form of redundant storage) so that it can redo

5

26

Locking Protocol: Nonstrict 2PL

v  Nonstrict Two-Phase Locking Protocol 1.  Each Xact must obtain a S (shared) lock on object before

reading, an X (exclusive) lock on object before writing. 2.  If an Xact holds an X lock on an object, other Xact’s cannot

get a lock (S or X) on that object and become blocked. 3.  A xact cannot request additional locks once it releases any locks.

•  It releases locks earlier, so blocked xact’s can resume earlier.

commit time

#. locks

Growing Shrinking

27

Nonstrict 2PL (contd.)

v  Thm: Nonstrict 2PL ensures acyclicity of precedence graph. §  Nonstrict 2PL allows only conflict serializable schedules. §  An equivalent serial schedule is given by the order of xacts

entering their shrinking phase.

v  Nonstrict 2PL is recoverable but not strict! §  A schedule S is recoverable if each xact commits only after all

xacts from which it read have committed. §  Nonstrict 2PL involves complex abort processing, but allows

xacts to go through more quickly.

C time

#. locks

28

(2) Deadlocks

v  Deadlock: Cycle of transactions waiting for locks to be released by each other.

v  Two ways of dealing with deadlocks: §  Deadlock detection §  Deadlock prevention

29

Deadlock Detection

v  Create a waits-for graph: §  Nodes are Xacts. §  There is an edge from Xact Ti to Xact Tj if Ti is waiting for Tj

to release a lock. §  Cycles indicate deadlocks!

v  Periodically check for cycles in the waits-for graph. §  Resolve a deadlock by aborting a transaction on the cycle

and releasing all its locks.

30

Deadlock Detection (Contd.)

T1: S(A), R(A), S(B) T2: X(B),W(B) X(C) T3: S(C), R(C) T4: X(B)

T1 T2

T4 T3

T1 T2

T4 T3

X(A)

Now T1, T2, T3 are all blocked, waiting infinitely… Abort one!

31

Waits-for vs. Precedence Graphs

v  Precedence graph is a theoretical tool to detect conflict serializable schedules.

v  Real implementation: (strict) 2PL + deadlock handling §  2PL prevents conflicting actions from being materialized in

the DB by blocking xact’s. §  Waits-for graph fixes infinite blocking when xact’s are

waiting for each other to release locks.

Page 6: DBMS Architecture Transaction Managementavid.cs.umass.edu/courses/445/s2015/lectures/Lec16-Xact-6up.pdf · " DBMS logs all actions (a form of redundant storage) so that it can redo

6

32

Deadlock Prevention

v  Assign priorities based on timestamps. §  The older the timestamp, the higher the xact’s priority.

v  Wait-Die: Ti wants a lock that Tj holds. If Ti has higher priority, Ti waits for Tj; otherwise Ti aborts. §  Lower priority xacts can never wait.

v  Wound-wait: Ti wants a lock that Tj holds. If Ti has higher priority, Tj aborts; otherwise Ti waits. §  Higher priority xacts never wait.

v  If a transaction re-starts, make sure it has its original timestamp so its priority increases.

36

Finally,

(Strict) 2PL

Deadlock detection/prevention

Index locking

+

+

Concurrency Control

37

Outline

v  Transaction management overview

v  Serializability & recoverability

v  Lock-based concurrency control

v  Recovery

38

39

Recovery in DBMS: Motivation

v  Atomicity: all-or-none §  Transactions may abort (“rollback”).

v  Durability: §  Effects of committed xacts should survive crashes.

  Desired behavior after system restarts: –  T1, T2 & T3 should be

durable. –  T4 & T5 should be

aborted (effects not seen).

crash! T1 T2 T3 T4 T5

40

Assumptions

v  Concurrency control is in effect. §  Strict 2PL, in particular.

v  Updates are happening “in place”. §  i.e. updates of an object are written from memory to

the only copy of it of on disk.

v  A simple scheme to guarantee atomicity & durability?

Page 7: DBMS Architecture Transaction Managementavid.cs.umass.edu/courses/445/s2015/lectures/Lec16-Xact-6up.pdf · " DBMS logs all actions (a form of redundant storage) so that it can redo

7

41

Handling the Buffer Pool

v  Force every write to disk at commit time? §  Provides durability. §  Poor response time. Why? G  No force, how can we ensure

durability?

v  Steal buffer-pool frames from uncommitted Xacts? §  If not, poor throughput. Why? G  If steal, how can we ensure

atomicity?

Force

No Force

No Steal Steal

Simple

Desired

42

Logging (a form of redundant storage)

v  Log: A history of actions executed by DBMS §  Records for REDO/UNDO information for every update

<XID, pageID, offset, length, old data, new data> §  All commits and aborts §  And additional control info (which we’ll see soon).

v  Writing log records to disk is more efficient than data pages §  Sequential writes to log (put it on a separate disk). §  Minimal info written to log, often smaller than a data record;

multiple updates fit in a single log page.

43

Protocol: Write-Ahead Logging

v  Write-Ahead Logging (WAL) Protocol: 1.  Must force the log record for an update before the

corresponding data page gets to disk (when steal). •  Guarantees atomicity

2.  Must write all log records for a Xact before commit. •  Guarantees durability

v  Exactly how is logging (and recovery) done? §  We’ll study the ARIES algorithm.

44

ARIES

v  Each log record has a unique Log Sequence Number (LSN). §  LSNs always increasing.

v  Each data page contains a pageLSN. §  The LSN of the most recent log record

for an update to that page.

v  System keeps track of flushedLSN. §  The max LSN written to disk so far.

LSNs

DB

pageLSNs

RAM

flushedLSN

Log records flushed to disk

“Log tail” in RAM data

page

pageLSN

45

ARIES

v  Each log record has a unique Log Sequence Number (LSN). §  LSNs always increasing.

v  Each data page contains a pageLSN. §  The LSN of the most recent log record

for an update to that page.

v  System keeps track of flushedLSN. §  The max LSN written to disk so far.

v  WAL (1): Before a page is written, flush its log record: §  pageLSN ≤ flushedLSN

LSNs

DB

pageLSNs

RAM

flushedLSN

Log records flushed to disk

“Log tail” in RAM data

page

pageLSN

49

Big Picture: What’s Stored Where

DB

Xact Table (TT) lastLSN status Dirty Page Table (DPT) recLSN (earliest log record that updated the page) flushedLSN

RAM

LSN, prevLSN XID type

length pageID

offset before-image after-image

LogRecords

LOG

Data pages each w. a pageLSN

master record latest checkpoint

flushed In RAM

Page 8: DBMS Architecture Transaction Managementavid.cs.umass.edu/courses/445/s2015/lectures/Lec16-Xact-6up.pdf · " DBMS logs all actions (a form of redundant storage) so that it can redo

8

50

1. Transaction Commit

v  When an Xact is running, §  Generate a log record with LSN for each update operation. §  Update Xact table and Dirty Page table in memory.

v  Upon commit, §  Write commit record to log.

§  WAL (2): All log records up to Xact’s lastLSN are flushed. • Guarantees that flushedLSN ≥ lastLSN. • Log writes are sequential; many log records per log page.

§  When commit returns, write end record to log.

51

2. Simple Transaction Abort

v  For now, consider an explicit abort of a Xact. §  No crash involved.

v  “Play back” the log in reverse order, UNDOing updates. §  Get lastLSN of Xact from Xact table. §  Follow chain of log records backward via prevLSN field.

• To perform UNDO, also need to have a lock on data!

v  Logging continues in UNDOs!

1.  Before starting UNDO, write an Abort log record. §  For recovering from crash during UNDO!

52

Abort (Contd.)

2.  Before restoring old value of a page, write a Compensation Log Record (CLR): §  CLR has one extra field, undonextLSN, pointing to the next LSN

to undo (i.e. the prevLSN of the record we’re undoing now). 3.  At end of UNDO, write an end log record.

1234

53

Example of Rollback

begin_checkpoint end_checkpoint update: T1 writes P5 update: T2 writes P3 update: T1 writes P2 T1 aborts

CLR: Undo T1 LSN 30 CLR: Undo T1 LSN 10 T1 ends

LSN LOG

00 05 10 20 30 40

45 50 60

prevLSNs Xact Table (TT) lastLSN status Dirty Page Table (DPT) recLSN (earliest) flushedLSN

RAM

54

3. Crash Recovery: Big Picture

v  Analysis figures which Xacts had committed or failed at crash time. §  Checkpoint: a snapshot of xact table and

dirty page table. §  Start from most recent checkpoint (via

master record).

v  REDO all actions; repeat history §  Start from smallest recLSN in Dirty

Page Table.

v  UNDO effects of failed Xacts. §  Back to oldest LSN of a running xact at

crash.

Oldest log rec. of Xact active at crash

Smallest recLSN in dirty page table after Analysis

Last chkpt

CRASH

A R U 55

(1) Recovery: The Analysis Phase

v  First, reconstruct state at checkpoint. §  Get begin_checkpoint record via the master recod. §  Find its end_checkpoint record, read the xact table and dirty

page table (DPT).

v  Reconstruct Xact Table and Dirty Page Table at crash; scan log forward (from last checkpoint).

§  Xact Table (TT): •  End record: Remove xact from Xact Table. •  Other records: Add xact to Xact Table (if not there), set its

lastLSN to this LSN, change xact status upon commit/abort.

§  Dirty Page Table (DPT): •  Update record: If page P not in Dirty Page Table, add P to

D.P.T., set its recLSN (earliest update) to this LSN.

Page 9: DBMS Architecture Transaction Managementavid.cs.umass.edu/courses/445/s2015/lectures/Lec16-Xact-6up.pdf · " DBMS logs all actions (a form of redundant storage) so that it can redo

9

56

(2) Recovery: The REDO Phase v  Repeat history to reconstruct DB state at crash:

§  Reapply all updates, even those of aborted Xacts and redo CLRs.

a.  Scan forward from the log record containing smallest recLSN in D.P.T.

b.  For each update or CLR log record, REDO the action. •  Optimizations are in textbook, but not required in this class. •  No additional logging!

57

(3) Recovery: The UNDO Phase

ToUndo = { l | l: lastLSN of a “loser” xact} Repeat:

§  Choose largest LSN among ToUndo.

§  If this LSN is a CLR and undonextLSN==NULL • Write an End record for this Xact.

§  If this LSN is a CLR, and undonextLSN != NULL • Add undonextLSN to ToUndo

§  Else this LSN is an update. Undo the update, write a CLR, add prevLSN to ToUndo.

Until ToUndo is empty.

v  Take a set of loser xacts, undo all in reverse order of LSN!

58

Example of Recovery

begin_checkpoint end_checkpoint update: T1 writes P5 update: T2 writes P3 T1 aborts CLR: Undo T1 LSN 10

T1 ends update: T3 writes P1 update: T2 writes P5 CRASH, RESTART

LSN LOG

00 05 10 20 30 40

45 50 60

prevLSNs Xact Table (TT) lastLSN, status Dirty Page Table (DPT) recLSN (earliest) flushedLSN

RAM

Atomicity & Durability: none of T1, T2, T3 should have any effect on DB!

59

Example of Recovery

begin_checkpoint end_checkpoint update: T1 writes P5 update: T2 writes P3 T1 aborts CLR: Undo T1 LSN 10

T1 ends update: T3 writes P1 update: T2 writes P5 CRASH, RESTART

LSN LOG

00 05 10 20 30 40

45 50 60

prevLSNs Xact Table (TT) lastLSN, status Dirty Page Table (DPT) recLSN (earliest) flushedLSN

RAM

Recovery algorithm: ANALYSIS: TT & DPT? REDO: UNDO:

T2: 60, running T3: 50, running

P5: 10 P3: 20 P1: 50

T1, T2, T3 T2, T3

61

Crash During Restart!

begin_checkpoint, end_checkpoint update: T1 writes P5 update T2 writes P3 T1 abort CLR: Undo T1 LSN 10, T1 End update: T3 writes P1

update: T2 writes P5 CRASH, RESTART T2 abort, CLR: Undo T2 LSN 60 T3 abort, CLR: Undo T3 LSN 50, T3 end

LSN LOG 00,05 10 20 30 40,45 50

60

65,70 75,80,85

undonextLSN

RAM

Atomicity & Durability: none of T1, T2, T3 should have any effect on DB!

Recovery algorithm: ANALYSIS: TT & DPT? REDO: UNDO:

CRASH, RESTART

Xact Table (TT) lastLSN, status Dirty Page Table (DPT) recLSN (earliest) flushedLSN

T1, T2, T3 T2

62

Crash During Restart!

begin_checkpoint, end_checkpoint update: T1 writes P5 update T2 writes P3 T1 abort CLR: Undo T1 LSN 10, T1 End update: T3 writes P1

update: T2 writes P5 CRASH, RESTART T2 abort, CLR: Undo T2 LSN 60 T3 abort, CLR: Undo T3 LSN 50, T3 end

CRASH, RESTART CLR: Undo T2 LSN 20, T2 end

LSN LOG 00,05 10 20 30 40,45 50

60

65,70 75,80,85

90,95

undonextLSN

RAM

Xact Table (TT) lastLSN, status Dirty Page Table (DPT) recLSN (earliest) flushedLSN

Page 10: DBMS Architecture Transaction Managementavid.cs.umass.edu/courses/445/s2015/lectures/Lec16-Xact-6up.pdf · " DBMS logs all actions (a form of redundant storage) so that it can redo

10

63

Main Principles behind ARIES v  Write-ahead logging (WAL)

§  Any change to an object is recorded in the log. The log is written to disk before the change to the object is written, or upon commit.

v  Repeating history during REDO §  On restart after a crash, repeat all actions before the crash, brings the

system back to the exact state that it was in before the crash. §  Then undo the xacts still active at crash time.

v  Logging changes during UNDO §  Changes made to DB while undoing are logged to ensure such an

action isn't repeated in the event of repeated restarts.

v  Fuzzy checkpointing to expedite recovery §  An efficient way to create a snapshot of Xact Table & D.P.T.

64

Logging + Replication + ARIES recovery (w. checkpoints)! NO!