File Processing : Concurrency Control

21
em Spatiotemporal Database Laboratory P usan National University File Processing : Concurrency Control 2004, Spring Pusan National University Ki-Joune Li

description

File Processing : Concurrency Control. 2004, Spring Pusan National University Ki-Joune Li. For given transactions T 1 , T 2 ,.., T n , Schedule (History) S is serializable if Result( S )  Result( S k ) where S k is a serial excution schedule. - PowerPoint PPT Presentation

Transcript of File Processing : Concurrency Control

Page 1: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

File Processing : Concurrency Control

2004, Spring

Pusan National University

Ki-Joune Li

Page 2: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Serializability

For given transactions T1, T2,.., Tn, Schedule (History) S is serializable if

Result(S) Result(Sk) where Sk is a serial excution schedule.

Note that Result(Si ) may be different from Result(Sj ) (i j )

How to detect whether S is serializable Conflict Graph

Page 3: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Conflict Graph

T1

T2

r(a)

w(a)

affects

r(b)

w(b)

S1S1

T1

T2

r(a)

w(a)

affects

r(b)

w(b)

S2S2

Res(S1) Res( (T1, T2) )Res(S1) Res( (T1, T2) )

Res(S1) Res( (T1, T2) )Res(S1) Res( (T1, T2) )

Res(S1) Res( (T2, T1) )Res(S1) Res( (T2, T1) )

Page 4: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Detect Cycle in Conflict Graph

T1

T2

r(a)

w(a)

affects

r(b)

w(b) T2T2

T1T1

If Cycle in Conflict Graph Then Not Serializable Otherwise Serializable

Page 5: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

How to make it serializable

How to make it serializable Control the order of execution of operations in concurrent transactions.

Timestamping Ordering by timestamp on each transaction and each operation

Two Phase Locking Protocol Locking on each operation

Page 6: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Lock-Based Protocols

A lock mechanism to control concurrent access to a data item

Data items can be locked in two modes : Exclusive (X) mode : Data item can be both read as well as written. X-lock is requested using lock-X instruction. Shared (S) mode : Data item can only be read. S-lock is requested using lock-S instruction.

Lock requests are made to concurrency-control manager. Transaction can proceed only after request is granted.

Page 7: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Lock-Based Protocols (Cont.)

Lock-compatibility matrix

A transaction may be granted a lock on an item if the requested lock is compatible with locks already held Any number of transactions can hold shared locks

If a lock cannot be granted, the requesting transaction is made to wait till all incompatible locks held have been released. the lock is then granted.

Page 8: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Lock-Based Protocols

Example of a transaction performing locking:

T2: lock-S(A);

read (A);

unlock(A);

lock-S(B);

read (B);

unlock(B);

Page 9: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

The Two-Phase Locking Protocol

This is a protocol which ensures conflict-serializable schedules. Phase 1: Growing Phase

transaction may obtain locks transaction may not release locks

Phase 2: Shrinking Phase transaction may release locks transaction may not obtain locks

The protocol assures serializability

Page 10: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Lock Conversions

Two-phase locking with lock conversions: First Phase:

can acquire a lock-S on item can acquire a lock-X on item can convert a lock-S to a lock-X (upgrade)

Second Phase: can release a lock-S can release a lock-X can convert a lock-X to a lock-S (downgrade)

This protocol assures serializability

Page 11: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Where to insert Lock related operations ?

A transaction Ti issues the standard read/write instruction, without explicit locking calls.

Automatic Acquitistion of Locks The operation read(D) is processed as:

if Ti has a lock on D

then read(D) else

begin

wait until no other transaction has a lock-X on D

grant Ti a lock-S on D;

read(D) end

Page 12: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Automatic Acquisition of Locks

write(D) is processed as: if Ti has a lock-X on D then write(D) else begin

if necessary wait until no other trans. has any lock on D,

if Ti has a lock-S on D then upgrade lock on D to lock-X else grant Ti a lock-X on D

write(D) end;

All locks are released after commit or abort

Page 13: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Lock Manager

Transaction Transaction sends lock request to

Lock Manager Lock Manager determines whether to

allow or not

Lock Table Keeps granted locks and pending

locks for each item In-memory Hash Table

Page 14: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Problem of Two Phase Locking Protocol

Deadlock Growing Phase and Shrinking Phase

Prevention and Avoidence : Impossible Only Detetion may be possible

When a deadlock occurs Detection of Deadlock : Wait-For-Graph Abort a transaction

How to choose a transaction to kill ?

Page 15: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Tree Lock : A Special Locking Mechanism

Only exclusive locks are allowed. The first lock by T may be on any

data item. Subsequently, a data Q can be

locked by T

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

Data items may be unlocked at any time.

Pairwise lock

Not Allowed

Page 16: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Timestamp-Based Protocols

Each transaction is issued a timestamp when TS(Ti) <TS(Tj) :

old transaction Ti and new transaction Tj

Each data Q, two timestamp : W-timestamp(Q) : largest time-stamp for sucessful write(Q) R-timestamp(Q) : largest time-stamp for sucessful read(Q)

Page 17: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Timestamp-Based Protocols : Read

Transaction Ti issues a read(Q) If TS(Ti) W-timestamp(Q),

then Ti needs to read a value of Q that was already overwritten.

Hence, the read operation is rejected, and Ti is rolled back.

If TS(Ti) W-timestamp(Q), then the read operation is executed, and R-timestamp(Q) is set set

Page 18: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Timestamp-Based Protocols : Write

Transaction Ti issues write(Q). If TS(Ti) < R-timestamp(Q),

then the value of Q that Ti is producing was needed previously, and the system assumed that that value would never be produced. Hence, the write operation is rejected, and Ti is rolled back.

If TS(Ti) < W-timestamp(Q), then Ti is attempting to write an obsolete value of Q.

Hence, this write operation is rejected, and Ti is rolled back.

Otherwise, the write operation is executed, and W-timestamp(Q) is reset

Page 19: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Correctness of Timestamp-Ordering Protocol

The timestamp-ordering protocol guarantees serializability since all the arcs in the precedence graph are of the form:

Thus, there will be no cycles in the conflict graph Timestamp protocol : free from deadlock

transactionwith smallertimestamp

transactionwith largertimestamp

Page 20: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Problem of Timestamping Protocol

Cascade Rollback Thomas Rule

Rollback and Restarting Overhead Instead of Deadlock Detection

Page 21: File Processing : Concurrency Control

emS

pat

iote

mp

ora

l D

atab

ase

Lab

ora

tory

P

usa

n N

atio

na

l U

niv

ersi

ty

Long Duration Transaction

Transaction of Long Duration with large number of operations

Problem Expensive rollback and restart Degradation of concurrency

Approach Nest Transaction Semantic Consistency rather than Serializability