Docsity

CS 347: Concurrency Control and Serializability - Timestamp Ordering Schedulers, Slides of Distributed Database Management Systems

Toggle sidebar
bg1
4/13/2012
2
CS 347 Notes05 7
Example
(T1)r1[X] W1[X]
(T2)r2[X] W2[Y] W2[X]
(T3)r3[X] W3[X] W3[Y] W3[Z]
r2[X] W2[Y] W2[X]
S1:r3[Y] W3[X] W3[Y] W3[Z]
r1[X] W1[X]
CS 347 Notes05 8
Definition of P(S)
• The precedence graph for schedule S, P(S), is a
directed graph where
– nodes: the transactions in S
– edges: TiTjis an edge IFF
p Ti, q Tjsuch that
p, q conflict and p <Sq
CS 347 Notes05 9
Example
S1: r1[X] w1[X] w1[Y]
r3[X] w3[X]
r2[X] w2[Y]
P(S1): T2T1T3
CS 347 Notes05 10
Serializability Theorem
Theorem: A schedule S is serializable IFF
P(S) is acyclic.
CS 347 Notes05 11
Enforcing Serializability
• Locking
•Timestamps
CS 347 Notes05 12
Locking
• Just like in a centralized system…
• But with multiple lock managers
D1
locks
for
D1
scheduler 1
node 1
D2
locks
for
D2
scheduler 2
node 2
...
docsity.com
pf3
pf4
pf5
pf8

Partial preview of the text

Download CS 347: Concurrency Control and Serializability - Timestamp Ordering Schedulers and more Slides Distributed Database Management Systems in PDF only on Docsity!

CS 347 Notes05 7

Example

(T 1 ) r 1 [X]  W 1 [X]

(T 2 ) r 2 [X]  W 2 [Y]  W 2 [X]

(T 3 ) r 3 [X]  W 3 [X]  W 3 [Y]  W 3 [Z]

r 2 [X]  W 2 [Y]  W 2 [X]

S 1 : r 3 [Y]  W 3 [X]  W 3 [Y]  W 3 [Z]

r 1 [X]  W 1 [X]

CS 347 Notes05 8

Definition of P(S)

  • The precedence graph for schedule S, P(S), is a
directed graph where
  • nodes: the transactions in S
  • edges: Ti  Tj is an edge IFF
 p  Ti , q  Tj such that
p, q conflict and p <S q

CS 347 Notes05 9

Example

S 1 : r 1 [X]  w 1 [X]  w 1 [Y]

r 3 [X]  w 3 [X]

r 2 [X]  w 2 [Y]

P(S 1 ): T 2  T 1  T 3

CS 347 Notes05 10

Serializability Theorem

Theorem: A schedule S is serializable IFF

P(S) is acyclic.

CS 347 Notes05 11

Enforcing Serializability

  • Locking
  • Timestamps

CS 347 Notes05 12

Locking

  • Just like in a centralized system…
  • But with multiple lock managers
D 1

locks for D 1

scheduler 1

node 1
D 2

locks for D 2

scheduler 2

node 2

CS 347 Notes05 13

Locking

  • Just like in a centralized system…
  • But with multiple lock managers
D 1

locks for D 1

scheduler 1

node 1
D 2

locks for D 2

scheduler 2

node 2

T

access &lock data lock dataaccess & (release all locks at end) CS 347 Notes05 14

Locking Rules

  • Well-formed transactions
  • Legal schedulers
  • Two-phase transactions
  • These rules guarantee serializable

schedules

CS 347 Notes05 15

What about

  • Locking in a shared-memory architecture?
  • Locking in a shared-disks architecture?

Locking with Shared Memory

  • Where does lock table live?
  • How do we avoid race conditions? CS 347 Notes05 16

P P ... P

M

Locking with Shared Disks

CS 347 Notes05 17

P

M

P P

M M

  • Where does lock table live?
  • How do we avoid race conditions?

Shared Disk Locking, Cases to Discuss

  • Control of Data Partitioned;

fixed partition function known by everyone

  • Dynamic partition of control
    • For each DB object i we need
      • LT(i): lock table entry for i (transaction that has lock, waiting transactions, lock mode, etc...)
      • W(i): at what processor is LT(i) currently?
    • Replicate W(i) at all processors (why?)
    • Need replicated data management scheme! CS 347 Notes05 18

CS 347 Notes05 25

Strict T.O.

  • Lock written items until it is certain that

writing transaction has been successful

(avoid cascading rollbacks)

CS 347 Notes05 26

Example Revisited

(Node X) (Node Y)

(T 1 ) a  X (T 2 ) d  Y

(T 1 ) X  a+100 (T 2 ) Y  2d

(T 2 ) c  X (T 1 ) b  Y

ts(T 1 ) < ts(T 2 )
reject!
abort T 1
delay

CS 347 Notes05 27

Example Revisited

(Node X) (Node Y)

(T 1 ) a  X (T 2 ) d  Y

(T 1 ) X  a+100 (T 2 ) Y  2d

(T 2 ) c  X (T 1 ) b  Y

ts(T 1 ) < ts(T 2 )
reject!
abort T 1
delay
abort T 1

(T 2 ) c  X

(T 2 ) X  2c

CS 347 Notes05 28

Enforcing T.O.

  • For each data item X: MAX_R[X]: maximum timestamp of a transaction that read X MAX_W[X]: maximum timestamp of a transaction that wrote X

rL[X]: # of transactions currently reading X (0,1,2,…) wL[X]: # of transactions currently writing X (0 or 1)

CS 347 Notes05 29

T.O. Scheduler - Part 1

ri[X] arrives IF ts(Ti) < MAX_W[X] THEN ABORT Ti

ELSE [ IF ts(T i) > MAX_R[X] THEN MAX_R[X]  ts(T i);

IF queue is empty AND wL[X] = 0 THEN

[rL[X]  rL[X]+1; START READ OF X]

ELSE add (r, T i) to queue]

CS 347 Notes05 30

W i[X] arrives IF ts(Ti) < MAX_W[X] OR ts(Ti) < MAX_R[X] THEN ABORT Ti

ELSE [ MAX_W[X]  ts(T i);

IF queue is empty AND wL[X]=0 AND rL[X]=

THEN [wL[X]  1; WRITE X;

WAIT FOR Ti TO FINISH ] ELSE add (w, T i) to queue ]

T.O. Scheduler - Part 2

CS 347 Notes05 31

When o finishes (o is r or w) on X

oL[X]  oL[X] - 1; NDONE  TRUE

WHILE NDONE DO

[ let head of queue be (q, Tj );(smallest timestamp) IF q=w AND rL[X]=0 AND wL[X]=0 THEN

[remove (q,T j ); wL[X]  1;

WRITE X AND WAIT FOR T j TO FINISH ] ELSE IF q=r AND wL[X]=0 THEN

[remove (q,T j ); rL[X]  rL[X] +1; START READ OF X]

ELSE NDONE  FALSE ]

T.O. Scheduler - Part 3

CS 347 Notes05 32

Note about the code

for reads: [rL[X]  rL[X]+1; START READ OF X]

for writes: [wL[X]  1; WRITE X;

WAIT FOR Ti TO FINISH ]

Meaning: In Part 3, the end of a write is

only processed when all writes for its

transaction have completed.

CS 347 Notes05 33

 If a transaction is aborted, it must be

retired with a new, larger timestamp

MAX_R[X]=10 T ts(T)= MAX_W[X]=

X

read X.. .

CS 347 Notes05 34

 If a transaction is aborted, it must be

retired with a new, larger timestamp

MAX_R[X]=10 T ts(T)= MAX_W[X]=

X

read X.. .

ts(T)=

 Starvation possible

CS 347 Notes05 35

Thomas Write Rule

MAX_R[X] MAX_W[X]

ts(T i)

T i wants to write X

CS 347 Notes05 36

Change in T.O. Scheduler:

When Wi[X] arrives

IF ts(Ti)<MAX_R[X] THEN ABORT Ti ELSE IF ts(Ti)<MAX_W[X] THEN [IGNORE THIS WRITE (tell Ti it was OK)] ELSE [process write as before…

MAX_W[X]  ts(Ti);

IF queue is empty AND wL[X]=0 AND rL[X] =0 THEN

[wL[X]  1; WRITE X and WAIT FOR Ti TO FINISH]

ELSE add (W, Ti) to queue]

MAX_R[X] MAX_W[X] ts(Ti ) Ti wants to write X

CS 347 Notes05 43

Theorem

If S is a schedule representing an execution

by a T.O. scheduler, then S is serializable

Proof:

1) say T i  T j in P(S)

  conflicting pi[x], q j [x] in S,

such that p i[x] <S q j [x]

Then by T.O. rule, ts(Ti)< ts(Tj )

CS 347 Notes05 44

2) Say there is a cycle

T 1  T 2  ...T n  T 1 in P(S)

then:

ts(T 1 )<ts(T 2 ) <ts(T 3 )…<ts(T n )<ts(T 1 )

A contradiction!

3) So P(S) is acyclic

 S is serializable

Proof - Continued

CS 347 Notes05 45

Timestamp management

Item data MAX_R MAX_W

X

X2 - too much space!

.. - more IO

Xn

CS 347 Notes05 46

Timestamp cache

Item MAX_R MAX_W

X

Y

Z

tsMIN

  • If transaction reads or writes X, make entry in cache
for X (add row if not in)
  • Periodically purge all items X with
MAX_R[X] < tsMIN, MAX_W[X] < tsMIN
and remember tsMIN (choose tsMIN  current time - d)

CS 347 Notes05 47

  • To enforce T.O. rule for pi[X]
    • if X entry in cache,
use MAX_R, MAX_W values in cache
  • else assume
MAX_R[X]=tsMIN, MAX_W[X]=tsMIN
  • Use hashing (on items) for cache
(same as lock table)

Timestamp cache

CS 347 Notes05 48

Distributed T.O. Scheduler

D 1

D 1 ts cache

scheduler 1

node 1
D 2

D 2 ts cache

scheduler 2

node 2
  • Each scheduler is “independent”
  • At end of transaction, signal all schedulers involved to release all wL[X] locks

CS 347 Notes05 49

Distributed T.O. Scheduler

D 1

D 1 ts cache

scheduler 1

node 1
D 2

D 2 ts cache

scheduler 2

node 2

T

access data access data

  • Each scheduler is “independent”
  • At end of transaction, signal all schedulers involved to release all wL[X] locks CS 347 Notes05 50

Summary

  • 2PL - the most popular
    • useful in a distributed system
    • deadlocks possible
    • several variations
  • T.O. - good for multiple versions
    • aborts more likely
    • no deadlocks
    • useful in a distributed sytem

CS 347 Notes05 51

  • Others concurrency control schemes

e.g., Certifiers, serialization graph testing

hard to not very practical implement in need global data a distributed structure system