overview of dbms recovery and concurrency control: eksemplerne er fra kapitel 3 i bogen: lars fank...

22
Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Upload: doreen-snow

Post on 14-Jan-2016

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Overview of DBMS recovery and concurrency control:

Eksemplerne er fra kapitel 3 I bogen:

Lars FankDatabaser

Teori og PraksisISBN 87-571-2397-7

Page 2: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Overview of DBMS recovery and concurrency control:

1. Recovery (Retablering )

2. Concurrency control (Samtidighedskontrol )

3. Concurrency control versus availability (Samtidighedskontrol kontra stor tilgængelighed).

Page 3: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Atomic transations:

Page 4: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Why should a database transaction be atomic?

Page 5: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

The two-Phase-Commit (2PC) protocol:

All updates must be executed in the first phase. That is before the commit. If the transaction fails in the first phase it must be aborted.If the transaction fails in the second phase the updates of the transaction must be stored in the database even though the updates may not have been transferred to the disc at the time of the commit.

1.Why must all answers to the user about the result of the updates only be send in the second phase of 2PC?

2.What should the user do if the user does not get an answer from the updating program?

Page 6: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Layout of the DBMS log record:

Recordidentification

Transactionidentification

Operationcode

Record beforeimage

Record afterimage

InsertDeleteUpdateBegin transactionCommit/Abort

What is the operation code used for when the recovery program reads the log file for roll back recovery?

Page 7: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Record types of the Log file: • Begin transaction.• Before and after images.• End transaction (commit or abort).• Database checkpoint with the identifications of all the

non-committed transactions.

Page 8: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Desired behavior after DBMS crash:

crash!

– Transactions T1, T2 & T3 should be durable.

– T4 & T5 should be aborted .

T1T2T3T4T5

Checkpoint

Page 9: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Restart by using DBMS checkpoint data:

crash!

– Transactions T1, T2 & T3 should be durable.

– T4 & T5 should be aborted .

T1T2T3T4T5

Checkpoint

Undo-queue. Undo-queue.Trans2 Trans4Trans4 Trans5

Redo-queue. Redo-queue.Trans2Trans3

Page 10: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

How would you recommend to restart?

Page 11: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

The ACID properties of transactions:

• Atomicity = the all or nothing update property.

• Concistency = if a database is consistent before a transaction is execued, then the database must alo be consistent after the transaction is executed.

• Isolation = The updates of a transaction must not be seen by other transactions before the transaction is committed.

• Durability = Committed data can be log-recovered in case of failures (What is disasters recovery?).

-----------------------------------------------

Page 12: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

DBMS Implementation of ACID Properties:

Logging and Recovery: Guarantees Atomicity and Durability.

Concurrency control: Guarantees Consistency and Isolation.

Page 13: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Overview of DBMS recovery and concurrency control:

1. Recovery (Retablering )

2. Concurrency control (Samtidighedskontrol )

3. Concurrency control versus availability (Samtidighedskontrol kontra stor tilgængelighed).

Page 14: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Two phase locking (2PL):

All locks must be obtained in first phase of 2PLand released in second phase.

Commit point

Lock growing phase

shrinking phase

Time

Number of

locks

Is it the same phases that are used in 2PL and 2PC?

Page 15: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Deadlock illustration

Deadlock = Deadly embrace (dødelig omfavnelse) = A blocked situation where transactions mutually wait for the other transactions to unlock.

Page 16: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Deadlock (Dødelig omfavnelse):

Suppose we have the following scenario:

• T1 asks for an exclusive lock on record R1.• T2 asks for an exclusive lock on record R2.• T1 asks for a shared lock on record R2.• T2 asks for a shared lock on record R1.

Page 17: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Deadlock detection by using a ”waits-for-graf”:In a waits-for-graf the nodes are active transactions and an arrow between two nodes signifies that a transaction waits for the unlock of another transaction.

A deadloch has occurred when the graf has a cyclic loop.

Page 18: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

End of session

Thank you !!!Thank you !!!

Page 19: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Deadlock Prevention• Give each transaction a timestamp. “Older” transactions have higher priority.

• Assume Ti requests a lock, but Tj holds a conflicting lock. We can follow two strategies:

• Wait-die: if Ti has higher priority, it waits; else Ti aborts.• Wound-wait: if Ti has higher priority, abort Tj; else Ti waits.

• Note: after aborting, restart with original timestamp!

Both strategies guarantee deadlock-free behavior!

Page 20: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

An Alternative to Prevention

• In theory, deadlock can involve many transactions:T1 waits-for T2 waits-for T3 ...waits-for T1

• In practice, most “deadlock cycles” involve only 2 transactions.

• Don’t need to prevent deadlock!What’s the problem with prevention?

• Allow it to happen, then notice it and fix it.Deadlock detection.

Page 21: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Deadlock Detection

• Lock Manager maintains a “Waits-for” graph:• Node for each transaction.• Arc from Ti to Tj if Tj holds a lock and Ti is waiting for it.

• Periodically check graph for cycles.

• “Shoot” some transaction to break the cycle.

• Simpler hack: time-outs.T1 made no progress for a while? Shoot it.

Page 22: Overview of DBMS recovery and concurrency control: Eksemplerne er fra kapitel 3 I bogen: Lars Fank Databaser Teori og Praksis ISBN 87-571-2397-7

Detection Versus Prevention

• Prevention might abort too many transactions.• Detection might allow deadlocks to tie up

resources for a while.– Can detect more often, but it’s time-consuming.

• The usual answer:– Detection is the winner.– Deadlocks are pretty rare.– If you get a lot of deadlocks, reconsider your

schema/workload!