CS 764: Topics in Database Management Systems Lecture 7...

39
Xiangyao Yu 9/28/2020 CS 764: Topics in Database Management Systems Lecture 7: Optimistic Concurrency Control 1

Transcript of CS 764: Topics in Database Management Systems Lecture 7...

Page 1: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Xiangyao Yu9/28/2020

CS 764: Topics in Database Management SystemsLecture 7: Optimistic Concurrency Control

1

Page 2: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

AnnouncementGuest lecture on Wednesday (Sep. 30) by Shasank Chavan from Oracle on “Hardware Acceleration with Oracle Database In-Memory”

Student round-table discussion after the talk (2:30—3:30)

2

Page 3: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Today’s Paper: Optimistic Concurrency Control

ACM Trans. Database Syst. 1981 3

Page 4: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Agenda

4

Pessimistic concurrency controlOptimistic concurrency control

Page 5: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Concurrency Control

5

Concurrency control ensures the correctness for concurrent operations

Assume serializable isolation level for this lecture

Page 6: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Concurrency Control

6

Concurrency control ensures the correctness for concurrent operations

Assume serializable isolation level for this lecture

Pessimistic: Resolve conflicts eagerly Optimistic: Ignore conflicts during a transaction’s execution and resolve conflicts lazily only when at a transaction’s completion time

Page 7: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Concurrency Control

7

Concurrency control ensures the correctness for concurrent operations

Assume serializable isolation level for this lecture

Pessimistic: Resolve conflicts eagerly Optimistic: Ignore conflicts during a transaction’s execution and resolve conflicts lazily only when at a transaction’s completion time

Other common concurrency control protocols• Timestamp ordering (T/O)• Multi-version concurrency control (MVCC)

Page 8: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Pessimistic Concurrency ControlStrict two-phase locking (2PL)• Acquire the right type of locks before accessing data• Release locks when the transaction commits

8

Page 9: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Pessimistic Concurrency ControlStrict two-phase locking (2PL)• Acquire the right type of locks before accessing data• Release locks when the transaction commits

9

T1Beginacquire S lock on ARead(A)

Time

S

Page 10: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Pessimistic Concurrency ControlStrict two-phase locking (2PL)• Acquire the right type of locks before accessing data• Release locks when the transaction commits

10

T1Beginacquire S lock on ARead(A)…acquire X lock on BWrite(B)

Time

S

X

Page 11: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Pessimistic Concurrency ControlStrict two-phase locking (2PL)• Acquire the right type of locks before accessing data• Release locks when the transaction commits

11

T1Beginacquire S lock on ARead(A)…acquire X lock on BWrite(B)…release lock on Arelease lock on BCommit

Time

S

X

Page 12: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Conflicts in 2PL

12

Time

T1BeginRead(X)…Write(Y)…release Xrelease YCommit

S

X

T2BeginWrite(X)

Conflict

Solution 1: T2 waits for T1 to release lock (e.g., wait-die, deadlock-detection)Solution 2: T2 self aborts (e.g., wait-die, no-wait)Solution 3: T2 forces T1 to abort (e.g., wound-wait)

Page 13: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Deadlock

13

T1BeginRead(X) S T2

Time

Page 14: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Deadlock

14

T1BeginRead(X) S T2

Time

XBeginWrite(Y)

Page 15: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Deadlock

15

T1BeginRead(X)…

Write(Y)

S

X

T2BeginWrite(Y)

Wait on conflictTime

Page 16: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Deadlock

16

T1BeginRead(X)…

Write(Y)

S

X

T2BeginWrite(Y)…

Write(X)Wait on conflict Wait on

conflict

Both transactions wait for each other=> Deadlock

Time

Page 17: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Deadlock Resolution

17

Deadlock detection (DL_DETECT)• Maintain a wait-for graph among transactions; abort a transaction if a cycle is

formed

Page 18: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Deadlock Resolution

18

Deadlock detection (DL_DETECT)• Maintain a wait-for graph among transactions; abort a transaction if a cycle is

formed

NO_WAIT• The requesting transaction self aborts when a conflict occurs

Page 19: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Deadlock Resolution

19

Deadlock detection (DL_DETECT)• Maintain a wait-for graph among transactions; abort a transaction if a cycle is

formed

NO_WAIT• The requesting transaction self aborts when a conflict occurs

WAIT_DIE• The requesting transaction waits if its priority is higher than the lock owner (wait),

otherwise the requesting transaction self aborts (die)

Page 20: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Deadlock Resolution

20

Deadlock detection (DL_DETECT)• Maintain a wait-for graph among transactions; abort a transaction if a cycle is

formed

NO_WAIT• The requesting transaction self aborts when a conflict occurs

WAIT_DIE• The requesting transaction waits if its priority is higher than the lock owner (wait),

otherwise the requesting transaction self aborts (die)WOUND_WAIT

• The requesting transaction forces the lock owner to abort (wound) if its priority is higher than the lock owner, otherwise the requesting transaction waits (wait)

Page 21: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Issues with Pessimistic CCOverhead • Overhead of acquiring/releasing locks and maintaining lock metadata• Even read-only transactions acquire locks

DeadlocksLimited concurrency Locks are held till the end of a transactionReal workloads have low contention• Locking is unnecessary if no contention exists

21

Page 22: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Optimistic Concurrency Control (OCC)Goal: eliminating pessimistic lockingThree executing phases: • Read• Validation• Write

22

Page 23: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Read Phasen = tcreate

23

Page 24: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Read Phasen = tcreatetwrite(n, i, v)

24

Page 25: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Read Phasen = tcreatetwrite(n, i, v)value = tread(n, i)

25

Page 26: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Read Phasen = tcreatetwrite(n, i, v)value = tread(n, i)tdelete(n)

26

Page 27: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Read Phasen = tcreatetwrite(n, i, v)value = tread(n, i)tdelete(n)

All changes (i.e., inserts, updates, deletes) are kept local to the transaction without updating the database

27

Page 28: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Write PhaseAll written values become “global”

All created nodes become accessibleAll deleted nodes become inaccessible

28

Page 29: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Validation PhaseA transaction i is assigned a transaction number t(i) when it enters the validation phase• t(i) < t(j) => exists a serial schedule where Ti is before Tj

29

Page 30: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Validation PhaseA transaction i is assigned a transaction number t(i) when it enters the validation phase• t(i) < t(j) => exists a serial schedule where Ti is before Tj

For t(i) < t(j), one of the following must be true1. Ti completes its write phase before Tj starts its read phase.2. The write set of Ti does not intersect the read set of Tj, and Ti completes its

write phase before Tj starts its write phase.3. The write set of Ti does not intersect the read set or the write set of Tj, and

Ti completes its read phase before Tj completes its read phase.

30

Page 31: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Validation PhaseA transaction i is assigned a transaction number t(i) when it enters the validation phase• t(i) < t(j) => exists a serial schedule where Ti is before Tj

For t(i) < t(j), one of the following must be true1. Ti completes its write phase before Tj starts its read phase.2. The write set of Ti does not intersect the read set of Tj, and Ti completes its

write phase before Tj starts its write phase.3. The write set of Ti does not intersect the read set or the write set of Tj, and

Ti completes its read phase before Tj completes its read phase.

31

Ti

Tj

Page 32: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Validation PhaseA transaction i is assigned a transaction number t(i) when it enters the validation phase• t(i) < t(j) => exists a serial schedule where Ti is before Tj

For t(i) < t(j), one of the following must be true1. Ti completes its write phase before Tj starts its read phase.2. The write set of Ti does not intersect the read set of Tj, and Ti completes its

write phase before Tj starts its write phase.3. The write set of Ti does not intersect the read set or the write set of Tj, and

Ti completes its read phase before Tj completes its read phase.

32

Ti

Tj

Page 33: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Validation PhaseA transaction i is assigned a transaction number t(i) when it enters the validation phase• t(i) < t(j) => exists a serial schedule where Ti is before Tj

For t(i) < t(j), one of the following must be true1. Ti completes its write phase before Tj starts its read phase.2. The write set of Ti does not intersect the read set of Tj, and Ti completes its

write phase before Tj starts its write phase.3. The write set of Ti does not intersect the read set or the write set of Tj, and

Ti completes its read phase before Tj completes its read phase.

33

Ti

Tj

Page 34: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Serial Validation

34

Critical Section

T1T2T3T4

Problem: The entire validate process happens in the critical section

Which transactions will T2, T3, and T4 be validated against?

Page 35: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Improved Serial Validation

35

Critical Section

Part of the validation process happens outside the critical section

The optimization can be applied repeatedly

Readonly transactions do not enter the critical section

T1T2T3T4

Page 36: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Parallel Validation

36

Validation against other transactions and writes both happen outside the critical section

Length of the critical section is independent of the number of validating transactions

Leading to unnecessary aborts

T1T2T3T4

Critical Sections

Page 37: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Q/A – OCC

37

Why write and validation phases likely take place in RAM?Hybrid CC that combines OCC and 2PL?

• Yes. Checkout MOCC and CormCCConcurrent way to deal with unnecessary aborts in parallel validation?tbegin vs. tcreate?Why any serial order of transactions acceptable? Shouldn’t it be the submission order?

• Strict serializability: If T1 finishes before T2 starts, T1 is before T2 in the global serial order

Practical systems using 2PL vs. OCC?OCC vs. 2PL in performance?

Page 38: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Group Discussion

38

What are the downsides of OCC compared to 2PL?

Page 39: CS 764: Topics in Database Management Systems Lecture 7 ...pages.cs.wisc.edu/~yxy/cs764-f20/slides/L7.pdf · •Even read-only transactions acquire locks Deadlocks Limited concurrency

Before Next LectureSubmit discussion summary to https://wisc-cs764-f20.hotcrp.com• Title: Lecture 7 discussion. group ##• Authors: Names of students who joined the discussion• Summary submission Deadline: Tuesday 11:59pm

Submit review for• Philip L. Lehman, S. Bing Yao: Efficient Locking for Concurrent Operations

on B-Trees. ACM Trans. Database Syst. 1981.• Before next Monday (Oct. 5)

39