18 Concurrency Control Intro to Database Systems Andy Pavlo AP - - PowerPoint PPT Presentation

18
SMART_READER_LITE
LIVE PREVIEW

18 Concurrency Control Intro to Database Systems Andy Pavlo AP - - PowerPoint PPT Presentation

Timestamp Ordering 18 Concurrency Control Intro to Database Systems Andy Pavlo AP AP 15-445/15-645 Computer Science Carnegie Mellon University Fall 2019 2 CO N CURREN CY CO N TRO L APPROACH ES Two-Phase Locking (2PL) Pessimistic


slide-1
SLIDE 1

Intro to Database Systems 15-445/15-645 Fall 2019 Andy Pavlo Computer Science Carnegie Mellon University

AP AP

18

Timestamp Ordering Concurrency Control

slide-2
SLIDE 2

CMU 15-445/645 (Fall 2019)

CO N CURREN CY CO N TRO L APPROACH ES

Two-Phase Locking (2PL)

→ Determine serializability order of conflicting

  • perations at runtime while txns execute.

Timestamp Ordering (T/O)

→ Determine serializability order of txns before they execute.

2

Pessimistic Optimistic

slide-3
SLIDE 3

CMU 15-445/645 (Fall 2019)

T/ O CO N CURREN CY CO N TRO L

Use timestamps to determine the serializability

  • rder of txns.

If TS(Ti) < TS(Tj), then the DBMS must ensure that the execution schedule is equivalent to a serial schedule where Ti appears before Tj.

3

slide-4
SLIDE 4

CMU 15-445/645 (Fall 2019)

TIM ESTAM P ALLO CATIO N

Each txn Ti is assigned a unique fixed timestamp that is monotonically increasing.

→ Let TS(Ti) be the timestamp allocated to txn Ti. → Different schemes assign timestamps at different times during the txn.

Multiple implementation strategies:

→ System Clock. → Logical Counter. → Hybrid.

4

slide-5
SLIDE 5

CMU 15-445/645 (Fall 2019)

TO DAY'S AGEN DA

Basic Timestamp Ordering Protocol Optimistic Concurrency Control Partition-based Timestamp Ordering Isolation Levels

5

slide-6
SLIDE 6

CMU 15-445/645 (Fall 2019)

BASIC T/ O

Txns read and write objects without locks. Every object X is tagged with timestamp of the last txn that successfully did read/write:

→ W-TS(X) – Write timestamp on X → R-TS(X) – Read timestamp on X

Check timestamps for every operation:

→ If txn tries to access an object "from the future", it aborts and restarts.

6

slide-7
SLIDE 7

CMU 15-445/645 (Fall 2019)

BASIC T/ O READS

If TS(Ti) < W-TS(X), this violates timestamp

  • rder of Ti with regard to the writer of X.

→ Abort Ti and restart it with a newer TS.

Else:

→ Allow Ti to read X. → Update R-TS(X) to max(R-TS(X), TS(Ti)) → Have to make a local copy of X to ensure repeatable reads for Ti.

7

slide-8
SLIDE 8

CMU 15-445/645 (Fall 2019)

BASIC T/ O WRITES

If TS(Ti) < R-TS(X) or TS(Ti) < W-TS(X)

→ Abort and restart Ti.

Else:

→ Allow Ti to write X and update W-TS(X) → Also have to make a local copy of X to ensure repeatable reads for Ti.

8

slide-9
SLIDE 9

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 1

9

BEGIN R(B) R(A) COMMIT BEGIN R(B) W(B) R(A) W(A) COMMIT

Database

slide-10
SLIDE 10

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 1

9

BEGIN R(B) R(A) COMMIT BEGIN R(B) W(B) R(A) W(A) COMMIT

TS(T1)=1 TS(T2)=2

Database

slide-11
SLIDE 11

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 1

9

BEGIN R(B) R(A) COMMIT BEGIN R(B) W(B) R(A) W(A) COMMIT

TS(T1)=1 TS(T2)=2

Database

slide-12
SLIDE 12

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 1

9

BEGIN R(B) R(A) COMMIT BEGIN R(B) W(B) R(A) W(A) COMMIT

TS(T1)=1 TS(T2)=2

1

Database

slide-13
SLIDE 13

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 1

9

BEGIN R(B) R(A) COMMIT BEGIN R(B) W(B) R(A) W(A) COMMIT

TS(T1)=1 TS(T2)=2

1 2

Database

slide-14
SLIDE 14

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 1

9

BEGIN R(B) R(A) COMMIT BEGIN R(B) W(B) R(A) W(A) COMMIT

TS(T1)=1 TS(T2)=2

1 2 2

Database

slide-15
SLIDE 15

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 1

9

BEGIN R(B) R(A) COMMIT BEGIN R(B) W(B) R(A) W(A) COMMIT

TS(T1)=1 TS(T2)=2

1 1 2 2

Database

slide-16
SLIDE 16

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 1

9

BEGIN R(B) R(A) COMMIT BEGIN R(B) W(B) R(A) W(A) COMMIT

TS(T1)=1 TS(T2)=2

1 1 2 2 2

Database

slide-17
SLIDE 17

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 1

9

BEGIN R(B) R(A) COMMIT BEGIN R(B) W(B) R(A) W(A) COMMIT

TS(T1)=1 TS(T2)=2

1 1 2 2 2 2

Database

slide-18
SLIDE 18

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 1

9

BEGIN R(B) R(A) COMMIT BEGIN R(B) W(B) R(A) W(A) COMMIT

TS(T1)=1 TS(T2)=2

1 1 2 2 2 2

Database

No violations so both txns are safe to commit.

slide-19
SLIDE 19

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

Database

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 2

10

BEGIN R(A) W(A) R(A) COMMIT BEGIN W(A) COMMIT

slide-20
SLIDE 20

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

Database

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 2

10

BEGIN R(A) W(A) R(A) COMMIT BEGIN W(A) COMMIT

1

slide-21
SLIDE 21

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

Database

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 2

10

BEGIN R(A) W(A) R(A) COMMIT BEGIN W(A) COMMIT

1 2

slide-22
SLIDE 22

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

Database

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 2

10

BEGIN R(A) W(A) R(A) COMMIT BEGIN W(A) COMMIT

1 2

slide-23
SLIDE 23

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

Database

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 2

10

BEGIN R(A) W(A) R(A) COMMIT BEGIN W(A) COMMIT

1 2

Violation: TS(T1) < W-TS(A) T1 cannot overwrite update by T2, so the DBMS has to abort it!

slide-24
SLIDE 24

CMU 15-445/645 (Fall 2019)

TH O M AS WRITE RULE

If TS(Ti) < R-TS(X):

→ Abort and restart Ti.

If TS(Ti) < W-TS(X):

→ Thomas Write Rule: Ignore the write and allow the txn to continue. → This violates timestamp order of Ti.

Else:

→ Allow Ti to write X and update W-TS(X)

11

slide-25
SLIDE 25

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

Database

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 2

12

BEGIN R(A) W(A) R(A) COMMIT BEGIN W(A) COMMIT

slide-26
SLIDE 26

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

Database

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 2

12

BEGIN R(A) W(A) R(A) COMMIT BEGIN W(A) COMMIT

1

slide-27
SLIDE 27

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

Database

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 2

12

BEGIN R(A) W(A) R(A) COMMIT BEGIN W(A) COMMIT

1 2

slide-28
SLIDE 28

CMU 15-445/645 (Fall 2019)

Object R-TS W-TS A B

Database

TIM E

Schedule

T1 T2

BASIC T/ O EXAM PLE # 2

12

BEGIN R(A) W(A) R(A) COMMIT BEGIN W(A) COMMIT

1 2

We do not update W-TS(A) Ignore the write and allow T1 to commit.

slide-29
SLIDE 29

CMU 15-445/645 (Fall 2019)

BASIC T/ O

Generates a schedule that is conflict serializable if you do not use the Thomas Write Rule.

→ No deadlocks because no txn ever waits. → Possibility of starvation for long txns if short txns keep causing conflicts.

Permits schedules that are not recoverable.

13

slide-30
SLIDE 30

CMU 15-445/645 (Fall 2019)

RECOVERABLE SCH EDULES

A schedule is recoverable if txns commit only after all txns whose changes they read, commit. Otherwise, the DBMS cannot guarantee that txns read data that will be restored after recovering from a crash.

14

slide-31
SLIDE 31

CMU 15-445/645 (Fall 2019)

TIM E

Schedule

T1 T2

RECOVERABLE SCH EDULES

15

BEGIN W(A) ⋮ BEGIN R(A) W(B) COMMIT

T2 is allowed to read the writes of T1.

slide-32
SLIDE 32

CMU 15-445/645 (Fall 2019)

TIM E

Schedule

T1 T2

RECOVERABLE SCH EDULES

15

BEGIN W(A) ⋮ BEGIN R(A) W(B) COMMIT

T2 is allowed to read the writes of T1. T1 aborts after T2 has committed.

ABORT

slide-33
SLIDE 33

CMU 15-445/645 (Fall 2019)

TIM E

Schedule

T1 T2

RECOVERABLE SCH EDULES

15

BEGIN W(A) ⋮ BEGIN R(A) W(B) COMMIT

T2 is allowed to read the writes of T1. This is not recoverable because we cannot restart T1. T1 aborts after T2 has committed.

ABORT

slide-34
SLIDE 34

CMU 15-445/645 (Fall 2019)

BASIC T/ O PERFO RM AN CE ISSUES

High overhead from copying data to txn's workspace and from updating timestamps. Long running txns can get starved.

→ The likelihood that a txn will read something from a newer txn increases.

16

slide-35
SLIDE 35

CMU 15-445/645 (Fall 2019)

O BSERVATIO N

If you assume that conflicts between txns are rare and that most txns are short-lived, then forcing txns to wait to acquire locks adds a lot of overhead. A better approach is to optimize for the no- conflict case.

17

slide-36
SLIDE 36

CMU 15-445/645 (Fall 2019)

O PTIM ISTIC CO N CURREN CY CO N TRO L

The DBMS creates a private workspace for each txn.

→ Any object read is copied into workspace. → Modifications are applied to workspace.

When a txn commits, the DBMS compares workspace write set to see whether it conflicts with other txns. If there are no conflicts, the write set is installed into the "global" database.

18

slide-37
SLIDE 37

CMU 15-445/645 (Fall 2019)

O CC PH ASES

#1 – Read Phase:

→ Track the read/write sets of txns and store their writes in a private workspace.

#2 – Validation Phase:

→ When a txn commits, check whether it conflicts with

  • ther txns.

#3 – Write Phase:

→ If validation succeeds, apply private changes to database. Otherwise abort and restart the txn.

19

slide-38
SLIDE 38

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS A 123

  • TIM E

Schedule

T1 T2

O CC EXAM PLE

20

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

slide-39
SLIDE 39

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS A 123

  • TIM E

Schedule

T1 T2

O CC EXAM PLE

20

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

slide-40
SLIDE 40

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS A 123

  • TIM E

Schedule

T1 T2

O CC EXAM PLE

20

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

slide-41
SLIDE 41

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • T1 Workspace

Object Value W-TS A 123

  • TIM E

Schedule

T1 T2

O CC EXAM PLE

20

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

slide-42
SLIDE 42

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • T1 Workspace

Object Value W-TS A 123

  • TIM E

Schedule

T1 T2

O CC EXAM PLE

20

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

123 A

slide-43
SLIDE 43

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • Object Value

W-TS

  • T1 Workspace

Object Value W-TS A 123

  • TIM E

Schedule

T1 T2

O CC EXAM PLE

20

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

T2 Workspace

123 A

slide-44
SLIDE 44

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • Object Value

W-TS

  • T1 Workspace

Object Value W-TS A 123

  • TIM E

Schedule

T1 T2

O CC EXAM PLE

20

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

T2 Workspace

123 A 123 A

slide-45
SLIDE 45

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • Object Value

W-TS

  • T1 Workspace

Object Value W-TS A 123

  • TIM E

Schedule

T1 T2

O CC EXAM PLE

20

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

T2 Workspace

123 A 123 A

TS(T2)=1

slide-46
SLIDE 46

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • Object Value

W-TS

  • T1 Workspace

Object Value W-TS A 123

  • TIM E

Schedule

T1 T2

O CC EXAM PLE

20

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

T2 Workspace

123 A 123 A

TS(T2)=1

slide-47
SLIDE 47

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • T1 Workspace

Object Value W-TS A 123

  • TIM E

Schedule

T1 T2

O CC EXAM PLE

20

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

123 A

TS(T2)=1

slide-48
SLIDE 48

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • T1 Workspace

Object Value W-TS A 123

  • TIM E

Schedule

T1 T2

O CC EXAM PLE

20

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

456 1 123 A 456 ∞

TS(T2)=1

slide-49
SLIDE 49

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • T1 Workspace

Object Value W-TS A 123

  • TIM E

Schedule

T1 T2

O CC EXAM PLE

20

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

456 1 123 A 456 ∞

TS(T2)=1 TS(T1)=2

slide-50
SLIDE 50

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • T1 Workspace

Object Value W-TS A 123

  • TIM E

Schedule

T1 T2

O CC EXAM PLE

20

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

456 1 456 2 123 A 456 ∞

TS(T2)=1 TS(T1)=2

slide-51
SLIDE 51

CMU 15-445/645 (Fall 2019)

O CC VALIDATIO N PH ASE

The DBMS needs to guarantee only serializable schedules are permitted. Ti checks other txns for RW and WW conflicts and makes sure that all conflicts go one way (from

  • lder txns to younger txns).

21

slide-52
SLIDE 52

CMU 15-445/645 (Fall 2019)

O CC SERIAL VALIDATIO N

Maintain global view of all active txns. Record read set and write set while txns are running and write into private workspace. Execute Validation and Write phase inside a protected critical section.

22

slide-53
SLIDE 53

CMU 15-445/645 (Fall 2019)

O CC READ PH ASE

Track the read/write sets of txns and store their writes in a private workspace. The DBMS copies every tuple that the txn accesses from the shared database to its workspace ensure repeatable reads.

23

slide-54
SLIDE 54

CMU 15-445/645 (Fall 2019)

O CC VALIDATIO N PH ASE

Each txn's timestamp is assigned at the beginning

  • f the validation phase.

Check the timestamp ordering of the committing txn with all other running txns. If TS(Ti) < TS(Tj), then one of the following three conditions must hold…

24

slide-55
SLIDE 55

CMU 15-445/645 (Fall 2019)

O CC VALIDATIO N PH ASE

When the txn invokes COMMIT, the DBMS checks if it conflicts with other txns. Two methods for this phase:

→ Backward Validation → Forward Validation

25

slide-56
SLIDE 56

CMU 15-445/645 (Fall 2019)

O CC BACKWARD VALIDATIO N

Check whether the committing txn intersects its read/write sets with those of any txns that have already committed.

26

Txn #1 Txn #2 Txn #3

TIME

COMMIT COMMIT COMMIT

slide-57
SLIDE 57

CMU 15-445/645 (Fall 2019)

O CC BACKWARD VALIDATIO N

Check whether the committing txn intersects its read/write sets with those of any txns that have already committed.

26

Txn #1 Txn #2 Txn #3

TIME

COMMIT COMMIT COMMIT

slide-58
SLIDE 58

CMU 15-445/645 (Fall 2019)

O CC BACKWARD VALIDATIO N

Check whether the committing txn intersects its read/write sets with those of any txns that have already committed.

26

Txn #1 Txn #2 Txn #3

TIME

COMMIT COMMIT COMMIT

Validation Scope

slide-59
SLIDE 59

CMU 15-445/645 (Fall 2019)

O CC FO RWARD VALIDATIO N

Check whether the committing txn intersects its read/write sets with any active txns that have not yet committed.

27

Txn #1 Txn #2 Txn #3

TIME

COMMIT COMMIT COMMIT

slide-60
SLIDE 60

CMU 15-445/645 (Fall 2019)

O CC FO RWARD VALIDATIO N

Check whether the committing txn intersects its read/write sets with any active txns that have not yet committed.

27

Txn #1 Txn #2 Txn #3

TIME

COMMIT COMMIT COMMIT

slide-61
SLIDE 61

CMU 15-445/645 (Fall 2019)

O CC FO RWARD VALIDATIO N

Check whether the committing txn intersects its read/write sets with any active txns that have not yet committed.

27

Txn #1 Txn #2 Txn #3

TIME

COMMIT COMMIT COMMIT

Validation Scope

slide-62
SLIDE 62

CMU 15-445/645 (Fall 2019)

O CC VALIDATIO N STEP # 1

Ti completes all three phases before Tj begins.

30

slide-63
SLIDE 63

CMU 15-445/645 (Fall 2019)

O CC VALIDATIO N STEP # 1

31

BEGIN READ VALIDATE WRITE COMMIT BEGIN READ VALIDATE WRITE COMMIT

TIM E

Schedule

T1 T2

slide-64
SLIDE 64

CMU 15-445/645 (Fall 2019)

O CC VALIDATIO N STEP # 2

Ti completes before Tj starts its Write phase, and Ti does not write to any object read by Tj.

→ WriteSet(Ti) ∩ ReadSet(Tj) = Ø

32

slide-65
SLIDE 65

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • Object Value

W-TS

  • T1 Workspace

T2 Workspace

TIM E

Schedule

T1 T2

O CC VALIDATIO N STEP # 2

33

BEGIN READ R(A) W(A) VALIDATE BEGIN READ R(A) VALIDATE WRITE COMMIT

123 A 123 A ∞ Object Value W-TS A 123

slide-66
SLIDE 66

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • Object Value

W-TS

  • T1 Workspace

T2 Workspace

TIM E

Schedule

T1 T2

O CC VALIDATIO N STEP # 2

33

BEGIN READ R(A) W(A) VALIDATE BEGIN READ R(A) VALIDATE WRITE COMMIT

123 A 123 A ∞

T1 has to abort even though T2 will never write to the database.

Object Value W-TS A 123

slide-67
SLIDE 67

CMU 15-445/645 (Fall 2019)

TIM E

Schedule

T1 T2

O CC VALIDATIO N STEP # 2

34

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

Database

Object Value W-TS

  • Object Value

W-TS

  • T1 Workspace

T2 Workspace

456 A 123 A ∞ Object Value W-TS A 123

slide-68
SLIDE 68

CMU 15-445/645 (Fall 2019)

TIM E

Schedule

T1 T2

O CC VALIDATIO N STEP # 2

34

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

Database

Object Value W-TS

  • Object Value

W-TS

  • T1 Workspace

T2 Workspace

456 A 123 A ∞ Object Value W-TS A 123

slide-69
SLIDE 69

CMU 15-445/645 (Fall 2019)

TIM E

Schedule

T1 T2

O CC VALIDATIO N STEP # 2

34

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(A) VALIDATE WRITE COMMIT

Database

Object Value W-TS

  • Object Value

W-TS

  • T1 Workspace

T2 Workspace

456 A 123 A ∞ Object Value W-TS A 123

  • Safe to commit T1 because we

know that T2 will not write.

slide-70
SLIDE 70

CMU 15-445/645 (Fall 2019)

O CC VALIDATIO N STEP # 3

Ti completes its Read phase before Tj completes its Read phase And Ti does not write to any object that is either read or written by Tj:

→ WriteSet(Ti) ∩ ReadSet(Tj) = Ø → WriteSet(Ti) ∩ WriteSet(Tj) = Ø

35

slide-71
SLIDE 71

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • Object Value

W-TS

  • T1 Workspace

T2 Workspace

Object Value W-TS A 123 B XYZ

TIM E

Schedule

T1 T2

O CC VALIDATIO N STEP # 3

36

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(B) R(A) VALIDATE WRITE COMMIT

123 A XYZ B 456 ∞

slide-72
SLIDE 72

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • Object Value

W-TS

  • T1 Workspace

T2 Workspace

Object Value W-TS A 123 B XYZ

TIM E

Schedule

T1 T2

O CC VALIDATIO N STEP # 3

36

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(B) R(A) VALIDATE WRITE COMMIT

123 A XYZ B 456 ∞

TS(T1)=1 Safe to commit T1 because T2 sees the DB after T1 has executed.

slide-73
SLIDE 73

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • Object Value

W-TS

  • T1 Workspace

T2 Workspace

Object Value W-TS A 123 B XYZ

TIM E

Schedule

T1 T2

O CC VALIDATIO N STEP # 3

36

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(B) R(A) VALIDATE WRITE COMMIT

123 A XYZ B 456 ∞ 456 1

TS(T1)=1

slide-74
SLIDE 74

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • T2 Workspace

Object Value W-TS A 123 B XYZ

TIM E

Schedule

T1 T2

O CC VALIDATIO N STEP # 3

36

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(B) R(A) VALIDATE WRITE COMMIT

XYZ B 456 1

slide-75
SLIDE 75

CMU 15-445/645 (Fall 2019)

Database

Object Value W-TS

  • T2 Workspace

Object Value W-TS A 123 B XYZ

TIM E

Schedule

T1 T2

O CC VALIDATIO N STEP # 3

36

BEGIN READ R(A) W(A) VALIDATE WRITE COMMIT BEGIN READ R(B) R(A) VALIDATE WRITE COMMIT

XYZ B 456 1 A 456 1

slide-76
SLIDE 76

CMU 15-445/645 (Fall 2019)

O CC O BSERVATIO N S

OCC works well when the # of conflicts is low:

→ All txns are read-only (ideal). → Txns access disjoint subsets of data.

If the database is large and the workload is not skewed, then there is a low probability of conflict, so again locking is wasteful.

37

slide-77
SLIDE 77

CMU 15-445/645 (Fall 2019)

O CC PERFO RM AN CE ISSUES

High overhead for copying data locally. Validation/Write phase bottlenecks. Aborts are more wasteful than in 2PL because they

  • nly occur after a txn has already executed.

38

slide-78
SLIDE 78

CMU 15-445/645 (Fall 2019)

O BSERVATIO N

When a txn commits, all previous T/O schemes check to see whether there is a conflict with concurrent txns.

→ This requires latches.

If you have a lot of concurrent txns, then this is slow even if the conflict rate is low.

39

slide-79
SLIDE 79

CMU 15-445/645 (Fall 2019)

PARTITIO N- BASED T/ O

Split the database up in disjoint subsets called horizontal partitions (aka shards). Use timestamps to order txns for serial execution at each partition.

→ Only check for conflicts between txns that are running in the same partition.

40

slide-80
SLIDE 80

CMU 15-445/645 (Fall 2019)

DATABASE PARTITIO N IN G

41

CREATE TABLE customer ( c_id INT PRIMARY KEY, c_email VARCHAR UNIQUE, ⋮ ); CREATE TABLE orders (

  • _id INT PRIMARY KEY,
  • _c_id INT REFERENCES

⮱customer (c_id), ⋮ ); CREATE TABLE oitems (

  • i_id INT PRIMARY KEY,
  • i_o_id INT REFERENCES

⮱orders (o_id),

  • i_c_id INT REFERENCES

⮱orders (o_c_id), ⋮ );

slide-81
SLIDE 81

CMU 15-445/645 (Fall 2019)

DATABASE PARTITIO N IN G

41

CREATE TABLE customer ( c_id INT PRIMARY KEY, c_email VARCHAR UNIQUE, ⋮ ); CREATE TABLE orders (

  • _id INT PRIMARY KEY,
  • _c_id INT REFERENCES

⮱customer (c_id), ⋮ ); CREATE TABLE oitems (

  • i_id INT PRIMARY KEY,
  • i_o_id INT REFERENCES

⮱orders (o_id),

  • i_c_id INT REFERENCES

⮱orders (o_c_id), ⋮ );

slide-82
SLIDE 82

CMU 15-445/645 (Fall 2019)

H O RIZO N TAL PARTITIO N IN G

42

BEGIN

Application Server Partitions

OITEMS ORDERS CUSTOMERS OITEMS ORDERS CUSTOMERS

Customers 1-1000 Customers 1001-2000

slide-83
SLIDE 83

CMU 15-445/645 (Fall 2019)

H O RIZO N TAL PARTITIO N IN G

42

COMMIT

Application Server Partitions

OITEMS ORDERS CUSTOMERS OITEMS ORDERS CUSTOMERS

Customers 1-1000 Customers 1001-2000

slide-84
SLIDE 84

CMU 15-445/645 (Fall 2019)

PARTITIO N- BASED T/ O

Txns are assigned timestamps based on when they arrive at the DBMS. Partitions are protected by a single lock:

→ Each txn is queued at the partitions it needs. → The txn acquires a partition’s lock if it has the lowest timestamp in that partition’s queue. → The txn starts when it has all of the locks for all the partitions that it will read/write.

43

slide-85
SLIDE 85

CMU 15-445/645 (Fall 2019)

PARTITIO N - BASED T/ O READS

Txns can read anything that they want at the partitions that they have locked. If a txn tries to access a partition that it does not have the lock, it is aborted + restarted.

44

slide-86
SLIDE 86

CMU 15-445/645 (Fall 2019)

PARTITIO N - BASED T/ O WRITES

All updates occur in place.

→ Maintain a separate in-memory buffer to undo changes if the txn aborts.

If a txn tries to write to a partition that it does not have the lock, it is aborted + restarted.

45

slide-87
SLIDE 87

CMU 15-445/645 (Fall 2019)

PARTITIO N- BASED T/ O

46

Partitions

OITEMS ORDERS CUSTOMERS OITEMS ORDERS CUSTOMERS

Customers 1-1000 Customers 1001-2000

Server #1 Server #2 Txn Queue

slide-88
SLIDE 88

CMU 15-445/645 (Fall 2019)

PARTITIO N- BASED T/ O

46

BEGIN

Partitions

OITEMS ORDERS CUSTOMERS OITEMS ORDERS CUSTOMERS

Customers 1-1000 Customers 1001-2000

Server #1 Server #2 Txn Queue

BEGIN

Server1: 100 Server2: 101

slide-89
SLIDE 89

CMU 15-445/645 (Fall 2019)

PARTITIO N- BASED T/ O

46

BEGIN

Partitions

OITEMS ORDERS CUSTOMERS OITEMS ORDERS CUSTOMERS

Customers 1-1000 Customers 1001-2000

Server #1 Server #2 Txn Queue

BEGIN

Server1: 100 Server2: 101

Txn #100

slide-90
SLIDE 90

CMU 15-445/645 (Fall 2019)

PARTITIO N- BASED T/ O

46

Partitions

OITEMS ORDERS CUSTOMERS OITEMS ORDERS CUSTOMERS

Customers 1-1000 Customers 1001-2000

Server #1 Server #2 Txn Queue

BEGIN

Server1: 100 Server2: 101

Txn #100

Get C_ID=1

slide-91
SLIDE 91

CMU 15-445/645 (Fall 2019)

PARTITIO N- BASED T/ O

46

Partitions

OITEMS ORDERS CUSTOMERS OITEMS ORDERS CUSTOMERS

Customers 1-1000 Customers 1001-2000

Server #1 Server #2 Txn Queue

BEGIN

Server1: 100 Server2: 101

Txn #100

COMMIT

slide-92
SLIDE 92

CMU 15-445/645 (Fall 2019)

PARTITIO N- BASED T/ O

46

Partitions

OITEMS ORDERS CUSTOMERS OITEMS ORDERS CUSTOMERS

Customers 1-1000 Customers 1001-2000

Server #1 Server #2 Txn Queue

BEGIN

Server2: 101

slide-93
SLIDE 93

CMU 15-445/645 (Fall 2019)

PARTITIO N- BASED T/ O

46

Partitions

OITEMS ORDERS CUSTOMERS OITEMS ORDERS CUSTOMERS

Customers 1-1000 Customers 1001-2000

Server #1 Server #2 Txn Queue

BEGIN

Server2: 101

slide-94
SLIDE 94

CMU 15-445/645 (Fall 2019)

PARTITIO N- BASED T/ O

46

Partitions

OITEMS ORDERS CUSTOMERS OITEMS ORDERS CUSTOMERS

Customers 1-1000 Customers 1001-2000

Server #1 Server #2 Txn Queue

BEGIN

Server2: 101

Txn #101

slide-95
SLIDE 95

CMU 15-445/645 (Fall 2019)

PARTITIO N ED T/ O PERFO RM AN CE ISSUES

Partition-based T/O protocol is fast if:

→ The DBMS knows what partitions the txn needs before it starts. → Most (if not all) txns only need to access a single partition.

Multi-partition txns causes partitions to be idle while txn executes.

47

slide-96
SLIDE 96

CMU 15-445/645 (Fall 2019)

DYN AM IC DATABASES

Recall that so far we have only dealing with transactions that read and update data. But now if we have insertions, updates, and deletions, we have new problems…

48

slide-97
SLIDE 97

CMU 15-445/645 (Fall 2019)

TH E PH AN TO M PRO BLEM

49

BEGIN COMMIT BEGIN COMMIT

INSERT INTO people (age=96, status='lit')

72 96

TIM E

Schedule

T1 T2

SELECT MAX(age) FROM people WHERE status='lit'

CREATE TABLE people ( id SERIAL, name VARCHAR, age INT, status VARCHAR );

SELECT MAX(age) FROM people WHERE status='lit'

slide-98
SLIDE 98

CMU 15-445/645 (Fall 2019)

WTF?

How did this happen?

→ Because T1 locked only existing records and not ones under way!

Conflict serializability on reads and writes of individual items guarantees serializability only if the set of objects is fixed.

50

slide-99
SLIDE 99

CMU 15-445/645 (Fall 2019)

PREDICATE LO CKIN G

Lock records that satisfy a logical predicate:

→ Example: status='lit'

In general, predicate locking has a lot of locking

  • verhead.

Index locking is a special case of predicate locking that is potentially more efficient.

51

slide-100
SLIDE 100

CMU 15-445/645 (Fall 2019)

IN DEX LO CKIN G

If there is a dense index on the status field then the txn can lock index page containing the data with status='lit'. If there are no records with status='lit', the txn must lock the index page where such a data entry would be, if it existed.

52

slide-101
SLIDE 101

CMU 15-445/645 (Fall 2019)

LO CKIN G WITH O UT AN IN DEX

If there is no suitable index, then the txn must

  • btain:

→ A lock on every page in the table to prevent a record’s status='lit' from being changed to lit. → The lock for the table itself to prevent records with status='lit' from being added or deleted.

53

slide-102
SLIDE 102

CMU 15-445/645 (Fall 2019)

REPEATIN G SCAN S

An alternative is to just re-execute every scan again when the txn commits and check whether it gets the same result.

→ Have to retain the scan set for every range query in a txn. → Andy doesn't know of any commercial system that does this (only just Silo?).

54

slide-103
SLIDE 103

CMU 15-445/645 (Fall 2019)

WEAKER LEVELS O F ISO LATIO N

Serializability is useful because it allows programmers to ignore concurrency issues. But enforcing it may allow too little concurrency and limit performance. We may want to use a weaker level of consistency to improve scalability.

55

slide-104
SLIDE 104

CMU 15-445/645 (Fall 2019)

ISO LATIO N LEVELS

Controls the extent that a txn is exposed to the actions of other concurrent txns. Provides for greater concurrency at the cost of exposing txns to uncommitted changes:

→ Dirty Reads → Unrepeatable Reads → Phantom Reads

56

slide-105
SLIDE 105

CMU 15-445/645 (Fall 2019)

ISO LATIO N LEVELS

SERIALIZABLE: No phantoms, all reads repeatable, no dirty reads. REPEATABLE READS: Phantoms may happen. READ COMMITTED: Phantoms and unrepeatable reads may happen. READ UNCOMMITTED: All of them may happen.

57

Isola t ion (H igh )

slide-106
SLIDE 106

CMU 15-445/645 (Fall 2019)

ISO LATIO N LEVELS

58

Dirty Read Unrepeatable Read Phantom

SERIALIZABLE

No No No

REPEATABLE READ

No No Maybe

READ COMMITTED

No Maybe Maybe

READ UNCOMMITTED

Maybe Maybe Maybe

slide-107
SLIDE 107

CMU 15-445/645 (Fall 2019)

ISO LATIO N LEVELS

SERIALIZABLE: Obtain all locks first; plus index locks, plus strict 2PL. REPEATABLE READS: Same as above, but no index locks. READ COMMITTED: Same as above, but S locks are released immediately. READ UNCOMMITTED: Same as above, but allows dirty reads (no S locks).

59

slide-108
SLIDE 108

CMU 15-445/645 (Fall 2019)

SQ L- 9 2 ISO LATIO N LEVELS

You set a txn's isolation level before you execute any queries in that txn. Not all DBMS support all isolation levels in all execution scenarios

→ Replicated Environments

The default depends on implementation…

60

SET TRANSACTION ISOLATION LEVEL <isolation-level>; BEGIN TRANSACTION ISOLATION LEVEL <isolation-level>;

slide-109
SLIDE 109

CMU 15-445/645 (Fall 2019)

ISO LATIO N LEVELS (20 13)

61

Default Maximum

Actian Ingres 1 0.0/1 0S

SERIALIZABLE SERIALIZABLE

Aerospike

READ COMMITTED READ COMMITTED

Greenplum 4.1

READ COMMITTED SERIALIZABLE

MySQL 5.6

REPEATABLE READS SERIALIZABLE

MemSQL 1 b

READ COMMITTED READ COMMITTED

MS SQL Server 201 2

READ COMMITTED SERIALIZABLE

Oracle 1 1 g

READ COMMITTED SNAPSHOT ISOLATION

Postgres 9.2.2

READ COMMITTED SERIALIZABLE

SAP HANA

READ COMMITTED SERIALIZABLE

ScaleDB 1 .02

READ COMMITTED READ COMMITTED

VoltDB

SERIALIZABLE SERIALIZABLE

Source: Peter Bailis

slide-110
SLIDE 110

CMU 15-445/645 (Fall 2019)

SQ L- 9 2 ACCESS M O DES

You can provide hints to the DBMS about whether a txn will modify the database during its lifetime. Only two possible modes:

→ READ WRITE (Default) → READ ONLY

Not all DBMSs will optimize execution if you set a txn to in READ ONLY mode.

62

SET TRANSACTION <access-mode>; BEGIN TRANSACTION <access-mode>;

slide-111
SLIDE 111

CMU 15-445/645 (Fall 2019)

CO N CLUSIO N

Every concurrency control can be broken down into the basic concepts that I've described in the last two lectures. I'm not showing benchmark results because I don't want you to get the wrong idea.

63

slide-112
SLIDE 112

CMU 15-445/645 (Fall 2019)

N EXT CLASS

Multi-Version Concurrency Control

64