Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lect 19

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 43

Concurrency Control

General Overview
Relational model - SQL

Formal & commercial query languages


Functional Dependencies
Normalization
Physical Design
Indexing
Query Processing and Optimization
Transaction Processing and CC

Transaction Concept
A transaction is a unit of program execution that

accesses and possibly updates various data items.

A transaction must see a consistent database.


During transaction execution the database may be

inconsistent.

A transaction ends with a Commit or an Abort


When the transaction is committed, the database must

be consistent.

State 1

State 2

ACID Properties
To preserve integrity of data, the database system must ensure:
Atomicity. Either all operations of the transaction are
properly reflected in the database or none are.
Consistency. Execution of a transaction in isolation

preserves the consistency of the database.

Isolation. Each transaction must be unaware of other

concurrently executing transactions.

Durability. After a transaction completes successfully, the

changes it has made to the database persist, even if there


are system failures.

Review - Schedules
An interleaving of xction operations

T1
1
2
3

T2
A
B
C
D

A schedule for T1,T2

T1
1
2
3

Inter ops: may interleave


Intra ops: remain in order

T2
A
B

C
D

Review - Schedules
T1
Serial Schedule
- All operations of each xction executed together,
xctions executed in succesion

Serializable schedule
- Any schedule whose final effect matches
that of any serial schedule

T1
1
2

1
2
3

T2

A
B
C

T2

A
B
C

Concurrency Control
Concurrency Control

Ensures interleaving of operations amongst


concurrent xctions result in serializable schedules
We have seen two types of serializable schedules:
conflict and view serialaizable

How?

Xction operations interleaved following a protocol

How to enforce serializable schedules?


Prevent P(S) cycles from occurring using a concurrency control
manager: ensures interleaving of operations amongst
concurrent xctions only result in serializable schedules.
T1 T2 ..

Tn

CC Scheduler
Serializable
schedule

Concurrency Via Locks


Idea:

Data items modified by one xction at a time

Locks
Control access to a resource
Can block a xction until lock granted

Two modes:
Shared (read only)
eXclusive (read & write)

Granting Locks
Requesting locks

Must request before accessing a data item


Granting Locks

No lock on data item? Grant


Existing lock on data item?
Check compatibility:

Compatible? Grant
Not? Block xction

shared

exclusive

shared

Yes

No

exclusive

No

No

Lock instructions
New instructions
- lock-S: shared lock request
- lock-X: exclusive lock request
- unlock: release previously held lock

Example:

T1
lock-X(B)
read(B)
B B-50
write(B)
unlock(B)
lock-X(A)
read(A)
A A + 50
write(A)
unlock(A)

T2
lock-S(A)
read(A)
unlock(A)
lock-S(B)
read(B)
unlock(B)
display(A+B)

Locking Issues
Starvation

T1 holds shared lock on Q


T2 requests exclusive lock on Q: blocks
T3, T4, ..., Tn request shared locks: granted
T2 is starved!
Solution?
Do not grant locks if other xction is waiting

Locking Issues
No xction proceeds:

Deadlock

T1

- T1 waits for T2 to unlock A

lock-X(B)

- T2 waits for T1 to unlock B

read(B)

T2

B B-50
write(B)
lock-S(A)

More later

read(A)
lock-S(B)
lock-X(A)

Locking Issues
Does not ensure serializability by itself:
T1
lock-X(B)
read(B)
B B-50
write(B)
unlock(B)

lock-X(A)
read(A)
A A + 50
write(A)
unlock(A)

T2

lock-S(A)
read(A)
unlock(A)
lock-S(B)
read(B)
unlock(B)
display(A+B)

T2 displays 50 less!!

Two-Phase Locking: 2PL


Each xction has two phases

Growing : only lock request


Shrinking: only unlocks
Xctions start in growing phase
First unlock beging the shrinking phase
Ensures serializabile schedules !

Order concurrent xctions on the moment of final lock is granted

2PL
Example: T1 in 2PL

Growing phase

Shrinking phase

T1
lock-X(B)
read(B)
B B - 50
write(B)
lock-X(A)
read(A)
A A - 50

write(A)
unlock(B)
unlock(A)

2PL & Serializability


Recall: Precedence Graph
T1

T2

T3

read(Q)
write(Q)
read(R)
write(R)
read(S)
T1

R/W(Q)

T2

T3

W
R/

)
(R

2PL & Serializability


Recall: Precedence Graph
T1

T2

T3

read(Q)
write(Q)
read(R)
write(R)
read(S)
write(S)

R/W(Q)

T1
W
R/

T2

)
(S

T3

W
R/

)
(R

Cycle Non-serializable

2PL & Serializability


Relation between Growing & Shrinking phase:
T1G < T1S

T1

T2G < T2S

T2

T3G < T3S


T1 must release locks for other to proceed

T3

T1S < T2G


T2S < T3G
T3S < T1G

T1G < T1S< T2G <T2S<T3G<T3S<T1G

Not Possible under 2PL!


It can be generalized for any set of transactions...

2PL Issues
2PL does not prevent deadlock

T1

T2

lock-X(B)
read(B)
> 2 xctions involved?

- Rollbacks expensive

B B-50
write(B)
lock-S(A)
read(A)
lock-S(B)
lock-X(A)

2PL Variants
Strict two phase locking

Exclusive locks must be held until xction commits


Ensures data written by xction cant be read by others
Prevents cascading rollbacks, generates recoverable schedules

Strict 2PL
T1

T2

T3

lock-X(A)
read(A)
lock-S(B)
read(B)
write(A)
unlock(A)
lock-X(A)

Strict 2PL
will not
allow that

read(A)
write(A)
unlock(A)
lock-S(A)
read(A)
<xction fails>

Strict 2PL & Cascading Rollbacks


Ensures any data written by uncommited xction not

read by another

Strict 2PL would prevent T2 and T3 from reading A

T1 & T2 wouldnt rollback if T1 does

2PL Variants
Rigorous two-phase locking

Exclusive and shared locks must be held until


xction commits

Both variants often used in DB systems

Dealing with Deadlocks


How do you detect a deadlock?

Wait-for graph
Directed edge from Ti to Tj
Ti waiting for Tj

T1

T2

T3

T2
T4

T1
T4

T3

X(Z)
X(V)
X(W)
S(V)
S(W)
S(V)

Suppose T4 requests lock-S(Z)....

Detecting Deadlocks
Wait-for graph has a cycle deadlock
T2

T2, T3, T4 are deadlocked

T4

T1

Build wait-for graph, check for cycle

T3

How often?
- Tunable
Expect many deadlocks or many xctions involved
Run often to avoid aborts
Else run less often to reduce overhead

Recovering from Deadlocks


Rollback one or more xction

Which one?
Rollback the cheapest ones
Cheapest ill-defined

Was it almost done?


How much will it have to redo?
Will it cause other rollbacks?
How far?

May only need a partial rollback


Avoid starvation

Ensure same xction not always chosen to break deadlock

Deadlock Prevention
Assign priorities based on timestamps. Assume Ti wants a lock

that Tj holds. Two policies are possible:

Wait-Die: It Ti has higher priority, Ti waits for Tj; otherwise Ti aborts

Wound-wait: If Ti has higher priority, Tj aborts; otherwise Ti waits

If a transaction re-starts, make sure it has its original timestamp


Another approach is to use Conservative 2PL: obtain all the

locks at the beginning or block.

Aborting a Transaction
If a transaction Ti is aborted, all its actions have to be undone.

Not only that, if Tj reads an object last written by Ti, Tj must be


aborted as well!

Most systems avoid such cascading aborts by releasing a

transactions locks only at commit time.

If Ti writes an object, Tj can read this only after Ti commits.

In order to undo the actions of an aborted transaction, the

DBMS maintains a log in which every write is recorded. This


mechanism is also used to recover from system crashes: all
active Xacts at the time of the crash are aborted when the
system comes back up.

The Log
The following actions are recorded in the log:

Ti writes an object: the old value and the new value.


Log record must go to disk before the changed page!

Ti commits/aborts: a log record indicating this action.

Log records are chained together by Xact id, so its easy to

undo a specific Xact.

Log is often duplexed and archived on stable storage.


All log related activities (and in fact, all CC related activities

such as lock/unlock, dealing with deadlocks etc.) are


handled transparently by the DBMS.

How Locking works in practice


One approach is the following:
Ti
Read(A),Write(B)

lock
table

Scheduler, part I
S(A),Read(A),X(B),Write(B)

Scheduler, part II
Read(A),Write(B)

Lock Management
Lock and unlock requests are handled by the lock manager
Lock table entry:

Number of transactions currently holding a lock

Type of lock held (shared or exclusive)

Pointer to queue of lock requests

Locking and unlocking have to be atomic operations


Lock upgrade: transaction that holds a shared lock can be upgraded to

hold an exclusive lock

Latches, convoys

Graph-Based Protocols
Graph-based protocols are an alternative to two-phase locking
Impose a partial ordering on the set D = {d1, d2 ,..., dh} of all

data items.

If di dj then any transaction accessing both di and dj


must access di before accessing dj.

Implies that the set D may now be viewed as a directed


acyclic graph, called a database graph.
The tree-protocol is a simple kind of graph protocol.

Tree Protocol

Only exclusive locks are allowed.


The first lock by Ti may be on any data item. Subsequently, a data Q can be

locked by Ti only if the parent of Q is currently locked by Ti.

Data items may be unlocked at any time. After T i unlocks Q, it cannot relock Q

Dynamic Databases
If we relax the assumption that the DB is a fixed collection of

objects, even Strict 2PL will not assure serializability:

T1 locks all pages containing sailor records with rating = 1, and


finds oldest sailor (say, age = 71).
Next, T2 inserts a new sailor; rating = 1, age = 96.
T2 also deletes oldest sailor with rating = 2 (and, say, age = 80),
and commits.
T1 now locks all pages containing sailor records with rating = 2, and
finds oldest (say, age = 63).
No consistent DB state where T1 is correct!

The Problem
T1 implicitly assumes that it has locked the set of all sailor

records with rating = 1.

Assumption only holds if no sailor records are added while T1 is


executing!
Need some mechanism to enforce this assumption. (Index
locking and predicate locking.)
Example shows that conflict serializability guarantees

serializability only if the set of objects is fixed!

Index Locking
If there is a dense index on the rating field using Alternative (2),

T1 should lock the index page containing the data entries with
rating = 1.

If there are no records with rating = 1, T1 must lock the index page
where such a data entry would be, if it existed!
If there is no suitable index, T1 must lock all pages, and lock the

file/table to prevent new pages from being added, to ensure that


no new records with rating = 1 are added.

Index
r=1

Data

Predicate Locking
Grant lock on all records that satisfy some logical predicate, e.g.

age > 2*salary.

Index locking is a special case of predicate locking for which an

index supports efficient implementation of the predicate lock.


What is the predicate in the sailor example?

In general, predicate locking has a lot of locking overhead.

Locking in B+ Trees
How can we efficiently lock a particular leaf node?

Btw, dont confuse this with multiple granularity locking!


One solution: Ignore the tree structure, just lock pages while

traversing the tree, following 2PL.

This has terrible performance!

Root node (and many higher level nodes) becomes bottleneck because
every tree access begins at the root.

Two Useful Observations


Higher levels of the tree only direct searches for leaf pages.
For inserts, a node on a path from root to modified leaf must be

locked (in X mode, of course), only if a split can propagate up to


it from the modified leaf. (Similar point holds w.r.t. deletes.)

We can exploit these observations to design efficient locking

protocols that guarantee serializability even though they violate


2PL.

A Simple Tree Locking Algorithm


Search: Start at root and go down; repeatedly, S lock child then

unlock parent.

Insert/Delete: Start at root and go down, obtaining X locks and

keep them. When is needed, you obtain an X lock. Once child


is locked, check if it is safe:
As you go, if child is safe, release all locks on ancestors.

Safe node: Node such that changes will not propagate up

beyond this node.

Inserts: Node is not full.


Deletes: Node is not half-empty.

ROOT

20

35

23

G
20* 22*

H
23* 24*

Do:
1) Search 38
2) Insert 45
3) Insert 25

38

I
35* 36*

44

D
38* 41*

E
44*

A Better Tree Locking Algorithm

Search: As before.
Insert/Delete:

Set locks as if for search, get to leaf, and set X lock on leaf.
If leaf is not safe, release all locks, and restart Xact using
previous Insert/Delete protocol.
Gambles that only leaf node will be modified; if not, S locks set on

the first pass to leaf are wasteful. In practice, better than previous
alg.

You might also like