46
DEADLOCK HANDLING IN DISTRIBUTED DATABASE SYSTEM By: Anusha Pai

Deadlock by anusha pai

  • Upload
    admis

  • View
    187

  • Download
    1

Embed Size (px)

DESCRIPTION

Deadlock handling database

Citation preview

Page 1: Deadlock by anusha pai

DEADLOCK HANDLING IN DISTRIBUTED DATABASE SYSTEM By:

Anusha Pai

Page 2: Deadlock by anusha pai

COMPARISON BETWEEN CONCURRENCY CONTROL AND DEADLOCK PROBLEM

2

Page 3: Deadlock by anusha pai

DIRTY-DATA PROBLEM

T1 T2 A B

l1(A); r1(A);A := A + 100;w1(A); l1(B); u1(A);

25 25

l2(A); r2(A);A := A * 2;w2(A); l2(B); Denied

125

250

r1(B);Abort; u1(B);

l2(B); u2(A); r2(B);B := B * 2;w2(B); u2(B); 50

Page 4: Deadlock by anusha pai

CASCADING ROLLBACK

If dirty data is available to transactions, then we sometimes have to perform a cascading rollback

T U V

Write WriteReadRead

Database elements

Page 5: Deadlock by anusha pai

SCHEDULES

Strict – Transactions can neither read nor write an item x until the last transaction that wrote x has committed.

Cascade less –If every transaction in the schedule reads only items that were written by committed transactions.

5

Page 6: Deadlock by anusha pai

RECOVERABLE SCHEDULES A schedule is recoverable if no transaction T in S

commits until all transactions t that have written an item that T reads have committed.

Examples S1: w1(A); w1(B); w2(A); r2(B); c1; c2;

S2: w2(A); w1(B); w1(A); r2(B); c1; c2;

S3: w1(A); w1(B); w2(A); r2(B); c2; c1;

Serial (thus serializable) and

recoverableNot serializable,

but recoverable

Serial (thus serializable) But NO recoverable

Page 7: Deadlock by anusha pai

RECOVERABLE

These schedules are recoverable. F is recoverable because T1 commits before T2,Then T2 can commit itself.

In F2, if T1 aborted, T2 has to abort because the value of A it read is incorrect. In both cases, the database is left in a consistent state.

7

Page 8: Deadlock by anusha pai

UNRECOVERABLE

if a transaction T1 aborts, and a transaction T2 commits, but T2 relied on T1, we have an unrecoverable schedule.

G is unrecoverable, because T2 read the value of A written by T1, and committed. T1 later aborted, therefore the value read by T2 is wrong, but since T2 committed, this schedule is unrecoverable.

8

Page 9: Deadlock by anusha pai

ACR SCHEDULE A schedule avoids cascading rollback (ACR) if

transactions may read only values written by committed transactions

Every ACR schedule is recoverable Example

S4: w1(A); w1(B); w2(A); c1; r2(B); c2;

Page 10: Deadlock by anusha pai

Resolving Deadlocks

Page 11: Deadlock by anusha pai

A RISK OF DEADLOCK

Deadlock is a situation where several transactions are forced by the scheduler to wait forever for a lock held by another transaction

Page 12: Deadlock by anusha pai

EXAMPLE

T1 T2 A B

l1(A); r1(A);

A := A + 100;

w1(A);

25 25

B := B * 2;w2(B);

l2(A); Denied

12550

l1(B); Denied

l2(B); r2(B);

Page 13: Deadlock by anusha pai

TWO APPROACHES

Deadlock detection Detect deadlocks and fix them

Deadlock avoidance Manage transactions in such a way that deadlocks

are never able to form

Page 14: Deadlock by anusha pai

DEADLOCK DETECTION BY TIMEOUT

Put a limit on how long a transaction may be active, and if a transaction exceeds this time, roll it back

When typical transactions execute in milliseconds, a timeout of one minute would affect only transactions in a deadlock

Page 15: Deadlock by anusha pai

WAITS-FOR GRAPH Indicate which transactions are waiting for locks held

by another transaction Refuse to allow an action that creates a cycle in the

graph Nodes: transactions that currently hold a lock or is waiting for one Arc from node T to U if

U holds a lock on A, and T is waiting for a lock on A

If there is a cycle, the transactions in the cycle is in a deadlock

Page 16: Deadlock by anusha pai

WFG

Directed graph

Node->concurrent transactions.

Arc-> like Ti ->Tj, indicates transaction Ti is waiting for Tj to release a lock on some entity.

16

Page 17: Deadlock by anusha pai

EXAMPLET1 T2

l1(A); r1(A);

Denied

l1(B); Denied

l2(C); r2(C);

T3

l3(C); Denied

l3(B); r3(B);

T4

l4(A); Denied

l4(D); r4(D);l2(A);

1)2)3)4)5)6)7)8)

3 2 1

4

Page 18: Deadlock by anusha pai

LOCAL AND GLOBAL WAIT-FOR GRAPHS

18

Page 19: Deadlock by anusha pai

DISTRIBUTED DEADLOCK

19

Page 20: Deadlock by anusha pai

EXAMPLE WAIT-FOR GRAPH FOR FALSE CYCLES

20

Initial state:

Page 21: Deadlock by anusha pai

FALSE CYCLES (CONT.)

21

• Suppose that starting from the state shown in figure,

1. T2 releases resources at S1

• resulting in a message remove T1 T2 message from the Transaction Manager at site S1 to the coordinator)

2. And then T2 requests a resource held by T3 at site S2

• resulting in a message insert T2 T3 from S2 to the coordinator

• Suppose further that the insert message reaches before the delete message

– this can happen due to network delays

• The coordinator would then find a false cycle

T1 T2 T3 T1

• The false cycle above never existed in reality.

• False cycles cannot occur if two-phase locking is used.

Page 22: Deadlock by anusha pai

DEADLOCK PREVENTION BY ORDERING ELEMENTS

Every transaction is required to request locks on elements in order

Then, there can be no deadlock due to transactions waiting for locks

Page 23: Deadlock by anusha pai

DEADLOCK AVOIDANCE

By ordering the resources.

Use of transaction timestamps to prioritize transactions and resolve deadlocks.

23

Page 24: Deadlock by anusha pai

POLICIES FOR DETECTING DEADLOCKS

When a transaction T has to wait for a lock held by another transaction U

The Wait-Die Scheme If T is older than U, then T is allowed to wait for

the lock held by U If U is older than T, then T “dies”; it is rolled back

The Wound-Wait Scheme If T is older than U, it “wounds” U. If U is older than T, then T waits for the lock

Page 25: Deadlock by anusha pai

EXAMPLE OF THE WAIT-DIE SCHEMET1 T2

l1(A); r1(A);

l1(B); w1(B)

l2(A); Dies

T3

l3(C); w3(C)

l3(B); r3(B);

T4

l4(A); l4(D);

l4(A); Dies

l2(A);

1)2)3)4)5)6)7)8)9)10)11)12)13)14)

u3(B); u3(C)

u1(A); u1(B);

Waitsr4(D); w4(A);u4(A); u4(D):

l2(A); l2(C);r2(C); w2(A)

Page 26: Deadlock by anusha pai

COMPARISON OF THE METHODS

Timestamp-based methods No starvation because older transactions always

win Easy to implement Could roll back transactions when no deadlock

occurs Wait-for-graph

Computationally expensive Minimize the number of times we must abort a

transaction because of deadlock

Page 27: Deadlock by anusha pai

COMPARISON OF THE METHODS

Wait-Die scheme Roll back more transactions than Wound-Wait But, those transactions tend to do little work

Wound-Wait scheme Roll back is rare But, rolled back transactions tend to spend

significant time in their activities

Page 28: Deadlock by anusha pai

METHODS OF DETECTING DISTRIBUTED DEADLOCKS

Centralized

Distributed

Hierarchical

28

Page 29: Deadlock by anusha pai

CENTRALIZED DEADLOCK DETECTION

Single site appointed deadlock detection coordinator (DDC).

DDC has responsibility for constructing and maintaining GWFG.

If one or more cycles exist, DDC must break each cycle by selecting transactions to be rolled back and restarted.

29

Page 30: Deadlock by anusha pai

HIERARCHICAL DEADLOCK DETECTION

Sites are organized into a hierarchy.

Each site sends its LWFG to detection site above it in hierarchy.

Reduces dependence on centralized detection site.

30

Page 31: Deadlock by anusha pai

CONTINUE…

31

Page 32: Deadlock by anusha pai

DISTRIBUTED DEADLOCK DETECTION

Most well-known method developed by Obermarck (1982).

An external node, Text, is added to LWFG to indicate remote agent.

If a LWFG contains a cycle that does not involve Text, then site and DDBMS are in deadlock.

32

Page 33: Deadlock by anusha pai

CONTINUE…

Global deadlock may exist if LWFG contains a cycle involving Text.

To determine if there is deadlock, the graphs have to be merged.

Potentially more robust than other methods

33

Page 34: Deadlock by anusha pai

DISTRIBUTED DEADLOCK DETECTION

34

Page 35: Deadlock by anusha pai

DISTRIBUTED DEADLOCK DETECTION

S1: Text T3 T1 Text

S2: Text T1 T2 Text

S3: Text T2 T3 Text

Transmit LWFG for S1 to the site for which transaction T1 is waiting, site S2.

LWFG at S2 is extended and becomes:

S2: Text T3 T1 T2 Text

35

Page 36: Deadlock by anusha pai

DISTRIBUTED DEADLOCK DETECTION

Still contains potential deadlock, so transmit this WFG to S3:

S3: Text T3 T1 T2 T3 Text

GWFG contains cycle not involving Text, so deadlock exists.

36

Page 37: Deadlock by anusha pai

MODELS OF DEADLOCK

One-Resource Model

AND Model

OR Model

37

Page 38: Deadlock by anusha pai

ONE-RESOURCE MODEL

Transaction can have at most one out-standing resource request at a time.

Maximum out degree of the WFG is 1.

Finding deadlock means finding the cycle in WFG.

38

Page 39: Deadlock by anusha pai

AND MODEL

In the AND model, transactions are permitted to request a set of resources.

A transaction is blocked until it is granted all the resources it has requested.

May have out degree greater than one. Node t11 has two outstanding resource

requests, and in the case of the AND model both must be satisfied before t11 becomes active.

The cycle t11 t31 t33 t43 t41 t11.39

Page 40: Deadlock by anusha pai

EXAMPLE WFG

40

Page 41: Deadlock by anusha pai

OR MODEL

A request for numerous resources is satisfied by granting any requested resource.

In the OR model, discovery of a cycle is insufficient for deadlock detection.

Example WFG-Transaction T1 is not deadlocked because t22 has no outgoing edges, and after T2 releases the resources it holds,T1 can continue.

41

Page 42: Deadlock by anusha pai

OR MODEL

Deadlock in the OR model in terms of processes A process is blocked if it has an outstanding OR request. Associated with each blocked process is a set of processes, called its dependent set.

A blocked process starts executing upon receiving any grant message from a process in its dependent set. Otherwise it does not change state or its dependent set.

42

Page 43: Deadlock by anusha pai

PROBLEM

Determine whether each schedule is strict , cascade less , recoverable.

s3: r1(x); r2(z); r1(z); r3(x); r3(y); w1(x);c1;w3(y);c3; r2(y); w2(z); w2(y);c2

s4: r1(x); r2(z); r1(z); r3(x); r3(y); w1(x);w3(y);r2(y); w2(z); w2(y);c1;c2;c3

43

Page 44: Deadlock by anusha pai

SCHEDULE S3T1 T2 T3

r1(x)

r2(z)

r1(z)

r3(y)

w1(x)

c1

w3(y)

c3

r2(y)

w2(z)

w2(y)

c2

44

Page 45: Deadlock by anusha pai

SCHEDULE S4

45

T1 T2 T3

r1(x)

r2(z)

r1(z)

r3(x)

r3(y)

w1(x)

w3(y)

r2(y)

w2(z)

w2(y)

c1

c2

c3

Page 46: Deadlock by anusha pai

THANK YOU