Professional Documents
Culture Documents
Concurrency Control
Wednesday, 4/26/2006
Outline
Serial and Serializable Schedules
Conflict Serializability
Concurrency control techniques:
Locks
Timestamps
Validation
The Problem
Multiple transactions are running concurrently
T1, T2,
They read/write some common elements
A1, A2,
How can we prevent unwanted interference ?
The SCHEDULER is responsible for that
3
Dirty Reads
TT11:: WRITE(A)
WRITE(A)
TT22:: READ(A)
READ(A)
TT11:: ABORT
ABORT
5
Lost Update
TT11:: READ(A)
READ(A)
TT11:: A
A:=
:= A+5
A+5
TT11:: WRITE(A)
WRITE(A)
TT22:: READ(A);
READ(A);
TT22:: A
A:=
:= A*1.3
A*1.3
TT22:: WRITE(A);
WRITE(A);
6
Inconsistent Read
TT11::
TT11::
A
A:=
:= 20;
20; BB :=
:= 20;
20;
WRITE(A)
WRITE(A)
TT11:: WRITE(B)
WRITE(B)
TT22::
TT22::
READ(A);
READ(A);
READ(B);
READ(B);
Schedules
Given multiple transactions
A schedule is a sequence of interleaved
actions from all transactions
Example
T1
READ(A, t)
t := t+100
WRITE(A, t)
READ(B, t)
t := t+100
WRITE(B,t)
T2
READ(A, s)
s := s*2
WRITE(A,s)
READ(B,s)
s := s*2
WRITE(B,s)
9
A Serial Schedule
T1
T2
READ(A, t)
t := t+100
WRITE(A, t)
READ(B, t)
t := t+100
WRITE(B,t)
READ(A,s)
s := s*2
WRITE(A,s)
10
Serializable Schedule
A schedule is serializable if it is equivalent
to a serial schedule
11
A Serializable Schedule
T1
T2
READ(A, t)
t := t+100
WRITE(A, t)
READ(A,s)
s := s*2
WRITE(A,s)
READ(B, t)
t := t+100
WRITE(B,t)
Notice: this is NOT a serial schedule READ(B,s)
12
A Non-Serializable Schedule
T1
T2
READ(A, t)
t := t+100
WRITE(A, t)
READ(A,s)
s := s*2
WRITE(A,s)
READ(B,s)
s := s*2
WRITE(B,s)
READ(B, t)
13
Ignoring Details
Sometimes transactions actions may
commute accidentally because of specific
updates
Serializability is undecidable !
Notation
TT11:: rr11(A);
(A); w
w11(A);
(A); rr11(B);
(B); w
w11(B)
(B)
TT22:: rr22(A);
(A); w
w22(A);
(A); rr22(B);
(B); w
w22(B)
(B)
15
Conflict Serializability
Conflicts:
Two actions by same transaction Ti:
rri(X);
w (Y)
i(X); wi i(Y)
w
w
(X)
wi(X);
(X);
w
j
i
j(X)
w
r (X)
wi(X);
i(X); rj j(X)
rri(X);
w
(X)
(X);
w
j
16
i
j(X)
Conflict Serializability
A schedule is conflict serializable if it can
be transformed into a serial schedule by a
series of swappings of adjacent nonconflicting actions
Example:
rr11(A);
(A); w
w11(A);
(A); rr22(A);
(A); w
w22(A);
(A); rr11(B);
(B); w
w11(B);
(B); rr22(B);
(B); w
w22(B)
(B)
rr11(A);
(A); w
w11(A);
(A); rr11(B);
(B); w
w11(B);
(B); rr22(A);
(A); w
w22(A);
(A); rr22(B);
(B); w
w17
(B)
22(B)
Conflict Serializability
Any conflict serializable schedule is also a
serializable schedule (why ?)
The converse is not true, even under the
worst case update assumption
Lost
write
w
w11(Y);
(Y); w
w22(Y);
(Y); w
w22(X);
(X); w
w11(X);
(X); w
w33(X);
(X);
Equivalent,
but cant swap
w
18
w11(Y);
(Y); w
w11(X);
(X); w
w22(Y);
(Y); w
w22(X);
(X); w
w33(X);
(X);
Example 1
rr22(A);
(A); rr11(B);
(B); w
w22(A);
(A); rr33(A);
(A); w
w11(B);
(B); w
w33(A);
(A); rr22(B);
(B); w
w22(B)
(B)
Example 2
rr22(A);
(A); rr11(B);
(B); w
w22(A);
(A); rr22(B);
(B); rr33(A);
(A); w
w11(B);
(B); w
w33(A);
(A); w
w22(B)
(B)
B
1
Scheduler
The scheduler is the module that schedules
the transactions actions, ensuring
serializability
How ? Three techniques:
Locks
Time stamps
Validation
22
Locking Scheduler
Simple idea:
Each element has a unique lock
Each transaction must first acquire the lock
before reading/writing that element
If the lock is taken by another transaction,
then wait
The transaction must release the lock(s)
23
Notation
24
Example
T1
T2
L1(A); READ(A, t)
t := t+100
WRITE(A, t); U1(A); L1(B)
L2(A); READ(A,s)
s := s*2
WRITE(A,s); U2(A);
L2(B); DENIED
READ(B, t)
t := t+100
The scheduler
has ensured a conflict-serializable schedule
WRITE(B,t);
U1(B);
25
Example
T1
T2
L1(A); READ(A, t)
t := t+100
WRITE(A, t); U1(A);
L2(A); READ(A,s)
s := s*2
WRITE(A,s); U2(A);
L2(B); READ(B,s)
s := s*2
WRITE(B,s); U2(B);
Locks did not enforce conflict-serializability !!!
L1(B); READ(B, t)
26
T1
28
Deadlock
Trasaction T1 waits for a lock held by T2;
But T2 waits for a lock held by T3;
While T3 waits for . . . .
...
. . .and T73 waits for a lock held by T1 !!
Could be avoided, by ordering all elements (see book); or
deadlock detection plus rollback
29
Lock Modes
S = Shared lock (for READ)
X = exclusive lock (for WRITE)
U = update lock
Initially like S
Later may be upgraded to X
31
32
33
34
Timestamps
Every transaction receives a unique timestamp
TS(T)
Could be:
The systems clock
A unique counter, incremented by the
scheduler
35
Timestaps
Main invariant:
The
The timestamp
timestamp order
order defines
defines
the
the searialization
searialization order
order of
of the
the transaction
transaction
36
Timestamps
Associate to each element X:
RT(X) = the highest timestamp of any
transaction that read X
WT(X) = the highest timestamp of any
transaction that wrote X
C(X) = the commit bit: says if the
transaction with highest timestamp that
wrote X commited
These are associated to each page X in the buffer pool 37
Main Idea
For any two conflicting actions, ensure that
their order is the serialized order:
Read too
In each of these cases
late ?
wU(X) . . . rT(X)
Write too
late ?
rU(X) . . . wT(X)
wU(X) . . . wT(X)
Check that TS(U) < TS(T)
No problem
(WHY ??)
When
When TT wants
wants to
to read
read X,
X, rrTT(X),
(X), how
how do
do we
we
know
know U,
U, and
and TS(U)
TS(U) ??
38
Details
Read too late:
T wants to read X, and TS(T) < WT(X)
START(T)
START(T)
START(U)
START(U)
w
wUU(X)
(X) .. .. .. rrTT(X)
(X)
Need to rollback T !
39
Details
Write too late:
T wants to write X, and
WT(X) < TS(T) < RT(X)
START(T)
START(T)
START(U)
START(U)
rrUU(X)
(X) .. .. .. w
wTT(X)
(X)
Need to rollback T !
Why do we check WT(X) < TS(T) ????
40
Details
Write too late, but we can still handle it:
T wants to write X, and
TS(T) < RT(X) but WT(X) > TS(T)
START(T)
START(T)
START(V)
START(V)
w
wVV(X)
(X) .. .. .. w
wTT(X)
(X)
Dont write X at all !
(but see later)
41
More Problems
Read dirty data:
T wants to read X, and WT(X) < TS(T)
Seems OK, but
START(U)
START(U)
START(T)
START(T)
w
wUU(X).
(X). .. .. rrTT(X)
(X)ABORT(U)
ABORT(U)
More Problems
Write dirty data:
T wants to write X, and WT(X) > TS(T)
Seems OK not to write at all, but
START(T)
START(T)
START(U)
START(U) w
wUU(X).
(X). .. .. w
wTT(X)
(X)ABORT(U)
ABORT(U)
Timestamp-based Scheduling
When a transaction T requests r(X) or w(X),
the scheduler examines RT(X), WT(X),
C(X), and decides one of:
To grant the request, or
To rollback T (and restart with later
timestamp)
To delay T until C(X) = 0
44
Timestamp-based Scheduling
RULES:
There are 4 long rules in the textbook, on
page 974
You should be able to understand them, or
even derive them yourself, based on the
previous slides
Make sure you understand them !
READING
READINGASSIGNMENT:
ASSIGNMENT:18.8.4
18.8.4
45
Multiversion Timestamp
When transaction T requests r(X)
but WT(X) > TS(T),
then T must rollback
Idea: keep multiple versions of X:
Xt, Xt-1, Xt-2, . . .
TS(X
TS(Xt)t) >> TS(X
TS(Xt-1t-1)) >> TS(X
TS(Xt-2t-2)) >> .. .. ..
Let T read an older version, with appropriate
timestamp
46
Details
When wT(X) occurs create a new version, denoted
Xt where t = TS(T)
When rT(X) occurs, find a version Xt such that t <
TS(T) and t is the largest such
WT(Xt) = t and it never chanes
RD(Xt) must also be maintained, to reject certain
writes (why ?)
When can we delete Xt: if we have a later version
Xt1 and all active transactions T have TS(T) > t1
47
Tradeoffs
Locks:
Great when there are many conflicts
Poor when there are few conflicts
Timestamps
Poor when there are many conflicts (rollbacks)
Great when there are few conflicts
Compromise
READ ONLY transactions timestamps
READ/WRITE transactions locks
48
Concurrency Control by
Validation
Each transaction T defines a read set RS(T) and a
write set WS(T)
Each transaction proceeds in three phases:
Read all elements in RS(T). Time = START(T)
Validate (may need to rollback). Time = VAL(T)
Write all elements in WS(T). Time = FIN(T)
Main
Main invariant:
invariant: the
the serialization
serialization order
order isisVAL(T)
VAL(T)
49
U: Read phase
VAL(U)
Validate
FIN(U)
Write phase
conflicts
T: Read phase
Validate ?
START(T)
IF
IF RS(T)
RS(T)
WS(U)
WS(U)and
andFIN(U)
FIN(U)>>START(T)
START(T)
(U
(Uhas
hasvalidated
validatedand
and UUhas
hasnot
notfinished
finishedbefore
beforeTTbegun)
begun)
Then
ThenROLLBACK(T)
ROLLBACK(T)
50
START(U)
U: Read phase
Validate
T: Read phase
START(T)
FIN(U)
Write phase
Validate
conflicts
Write phase ?
VAL(T)
IF
IF WS(T)
WS(T)
WS(U)
WS(U)and
andFIN(U)
FIN(U)>>VAL(T)
VAL(T)
(U
(Uhas
hasvalidated
validatedand
and UUhas
hasnot
notfinished
finishedbefore
beforeTTvalidates)
validates)
Then
ThenROLLBACK(T)
ROLLBACK(T)
51
Final comments
Locks and timestamps: SQL Server, DB2
Validation: Oracle
(more or less)
52