concurrency control algorithms

35
Wolfgang Miller / 02.07.2009 / 1 Concurrency Controll Algorithms Concurrency Control Algorithms Chapter 4

Upload: vidal

Post on 22-Feb-2016

128 views

Category:

Documents


0 download

DESCRIPTION

Concurrency Control Algorithms. Chapter 4. Overview. Introduction B asics Locking schedulers Two-Phase Locking Non-Two-Phase Locking Protocols Write-Only Tree Locking Read/Write Tree Locking. III. Non-locking schedulers Timestamp Ordering Optimistic schedulers BOCC. Overview. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 1

Concurrency Controll Algorithms

Concurrency Control Algorithms

Chapter 4

Page 2: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 2

Concurrency Controll Algorithms

Overview

I. Introductiono Basics

II. Locking schedulerso Two-Phase Locking

• Non-Two-Phase Locking Protocolso Write-Only Tree Lockingo Read/Write Tree Locking

III. Non-locking schedulers

o Timestamp Ordering

IV. Optimistic schedulerso BOCC

Page 3: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 3

Concurrency Controll Algorithms

Overview

I. Introduction Basics

II. Locking schedulerso Two-Phase Locking

• Non-Two-Phase Locking Protocolso Write-Only Tree Lockingo Read/Write Tree Locking

III. Non-locking schedulers

o Timestamp Ordering

IV. Optimistic schedulerso BOCC

Page 4: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 4

Concurrency Controll AlgorithmsCSR – Class of conflict-serializable schedules

Definition: Conflict Equivalence

Schedules s and s‘ are conflict equivalent (denoted s c s‘), if: op(s) = op(s‘) and conf(s) = conf(s‘).

Definition: Conflicts and Conflict RelationsLet s be a schedule, t, t‘ trans(s), t t‘.

(i) Two data operations p t and q t‘ are in conflict in s ifthey access the same data item and at least one of them is a write. i.e.,(p = r(x) ˄ q = w(x)) ˅ (p = w(x) ˄ q = r(x)) ˅ (p = w(x) ˄ q = w(x))

(ii) {(p, q)} | p, q are in conflict and p occurs before q in s} is the conflict relation of s.

Page 5: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 5

Concurrency Controll AlgorithmsCSR – Class of conflict-serializable schedules

Definition: Conflict Serializability:

Schedule s is conflict serializable if there is a serial schedule s‘ such that s c s‘.CSR denotes the class of all conflict serializable schedules.

Example: s1 = r1(x) r2(x) r1(z) w1(x) w2(y) r3(z) w3(y) c1 c2 w3(z) c3 CSR

s2 = r2(x) w2(x) r1(x) r1(y) r2(y) w2(y) c1 c2 CSR

t2 writes on x before t 1 read x

Page 6: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 6

Concurrency Controll AlgorithmsScheduler Actions and Transaction States

Definition: CSR SafetyFor a scheduler S, Gen(S) denotes the set of all schedules that S can generate. A scheduler is called CSR safe if Gen(S) CSR

All of the following algorithms are CSR safe

Page 7: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 7

Concurrency Controll AlgorithmsScheduler Classification

Schedulers can be classified as pessimistic or optimistic:

optimistic Schedulers: - also called „aggressiv“, because they mostly let steps pass

and rarely block. This bears the danger of „getting stuck“ eventually when the serializability of the output can no longer be guaranteed

pessimistic Schedulers: - also called „conservative“, because they mostly block, in

extreme, albeit unlikely case, the output could become a serial schedule, if all transactions but one were blocked.

Page 8: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 8

Concurrency Controll AlgorithmsScheduler Classification – Algorithms Overview

Page 9: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 9

Concurrency Controll Algorithms

Overview

I. Introduction basics

II. Locking schedulersTwo-Phase Locking

• Non-Two-Phase Locking Protocolso Write-Only Tree Lockingo Read/Write Tree Locking

III. Non-locking schedulers

o Timestamp Ordering

IV. Optimistic schedulerso BOCC

Page 10: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 10

Concurrency Controll AlgorithmsLocking Schedulers

In a nuthsell this means that if a transaction holds a lock on a specific data item, this item is not available to other, concurrent transactions

The Idea behind locking schedules is to synchronize access to shared data by using locks.

lock notation:

rl(x) read lock xwl(x) write lock xru(x) write unlock xwu(x) write unlock x

compatibility of locks:

Page 11: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 11

Concurrency Controll AlgorithmsRules for well-formed locking

LR1:If ti contains a step of the form ri (x)[wi (x)], then the schedule s also contains a step of the form rli (x)[wli (x)] before the data operation. Moreover s contains a step of the form rui (x)[wui (x)] somewhere after the operation.

LR2:For each x accessed by ti, schedule s has at most one rli (x) and at most one wli (x) step.

locks of the same type are set at most once per transaction and per data item.LR3:No step of the form rui (.) or wui (.) is redundant (i.e., executed per transaction more than once)

LR4:If x is held locked by both ti and tj for ti, tj trans(s), i ≠ j, then these locks are not in conflict (i.e., they are compatible)

Page 12: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 12

Concurrency Controll AlgorithmsTwo-Phase Locking (2PL) - Definition

Definition: Two-Phase Locking

A locking protocol is two-phase if for every output s and every transaction ti trans(s) is true that no qli step follows the first oui step (o ,q {r, w}).

A locking protocol is two-phase if for every transaction a phase during which locks are set is distinguished from and strictly followed by a phase during which locks are released.

Page 13: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 13

Concurrency Controll AlgorithmsTwo-Phase Locking (2PL) - Example

2PL Example :s = w1(x) r2(x) w1(y) w1(z) r3(z) c1 w2(y) w3(y) c2 w3(z) c3

wl1(x) w1(x) wl1(y) w1(y) wl1(z) w1(z) wu1(x) rl2(x) r2(x) wu1(y) wu1(z) c1rl3(z) r3(z) wl2(y) w2(y) wu2(y) ru2(x) c2 wl3(y) w3(y) wl3(z) w3(z) wu3(z) wu3(y) c3

A 2PL output could be:

Page 14: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 14

Concurrency Controll Algorithms

Overview

I. Introduction basics

II. Locking schedulersTwo-Phase Locking

• Non-Two-Phase Locking Protocols Write-Only Tree Locking Read/Write Tree Locking

III. Non-locking schedulers

o Timestamp Ordering

IV. Optimistic schedulerso BOCC

V. Hybrid Schedulers

Page 15: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 15

Concurrency Controll AlgorithmsNon-Two-Phase Locking Protocols

The following two tree-based protocols are geared for transactions that exhibit treelike access patterns . In other cases they are susceptible to degradation.

The data items are viewed as nodes of a tree and accesses have to follow a path down the tree.

On the next slides we will have a look at those two tree-based locking schedulers:

Write-Only Tree Locking Read/Write Tree Locking

Page 16: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 16

Concurrency Controll AlgorithmsWrite-Only Tree Locking (WTL)

Note:the tree is a virtual data organization only, the relationship between the data items can be quite different

Under the write-only tree locking protocol, lock requests and releasesmust obey the locking rules LR1-LR4 and the following two rules on the next slide:

The Write-Only Tree Locking protocol uses a tree to organize the data items. In its model read operations are missing, they wouldcause problems we will see later.

Thus a transaction can only write data (or read and write are applied to the same item as collapsed into one operation) .

Page 17: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 17

Concurrency Controll AlgorithmsWrite-Only Tree Locking (WTL)

WTL1:If x is any node in the tree other than the root, wli (x) can be set only if ti currently holds a write lock on y, where y is parent of x

WTL2:After a wui (x), no further wli (x) is allowed (on the same data item x)

Example:

if a transaction t = w(c)w(e) wants to acquire wl(c) or wl(e) it has to hold wl(b)

Page 18: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 18

Concurrency Controll AlgorithmsWrite-Only Tree Locking (WTL)

Sample Transaction under the WTL protocol:

transaction t = w(d)w(i)w(k)

wl(a)wl(b)wu(a)wl(d)wl(e)wu(b)w(d)wu(d)wl(i)wu(e)w(i)wl(k)wu(i)w(k)wu(k)

Page 19: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 19

Concurrency Controll AlgorithmsRead/Write Tree Locking (RWTL)

The Read/Write Tree Locking is a generalization of the Write-Only Tree Locking protocol.

Unlike the the Write-Only Tree Locking protocol it supports seperate read operations, too.

Page 20: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 20

Concurrency Controll AlgorithmsRead/Write Tree Locking (RWTL)

Problem: ti locks root before tj does, but tj passes ti within a “read zone”

Example: rl1(a) rl1(b) r1(a) r1(b) wl1(a) w1(a) wl1(b) ul1(a) rl2(a) r2(a) w1(b) rl1(e) ul1(b) rl2(b) r2(b) ul2(a) rl2(e) rl2(i) ul2(b) r2(e) r1(e) r2(i) wl2(i) w2(i) wl2(k) ul2(e) ul2(i) rl1(i) ul1(e) r1(i) ...

Solution: formalize “read zone” and enforce two-phase property on “read zones”.This zones are called pitfall.

appears to follow TL rules but CSR

Page 21: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 21

Concurrency Controll AlgorithmsRead/Write Tree Locking (RWTL)

Definition: Read-Write Tree Locking

Under the RWTL lock requests and releases must obey LR1 - LR4, WTL1, WTL2, and the two-phase property within each pitfall.

Definition: pitfallFor transaction t with read set RS(t) and write set WS(t) let C1, ..., Cm be the connected components of RS(t).

A pitfall of t is a set of the form Ci {x WS(t) | x is a child or parent of some y Ci}.

Page 22: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 22

Concurrency Controll AlgorithmsRead/Write Tree Locking (RWTL)

Example:transaction t with RS(t) = {f, i, g} and WS(t) = {c, l, j, k, o}

has pitfalls pf1={c, f, i, l, j} and pf2={g, c, k}.

Page 23: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 23

Concurrency Controll Algorithms

Overview

I. Introduction basics

II. Locking schedulersTwo-Phase Locking

• Non-Two-Phase Locking Protocols Write-Only Tree Locking Read/Write Tree Locking

III. Non-locking schedulers

Timestamp Ordering

IV. Optimistic schedulerso BOCC

V. Hybrid Schedulers

Page 24: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 24

Concurrency Controll AlgorithmsNonlocking Schedulers

The next two protocols are alternatives to locking schedulers.They guarantee safety of their output without using locks.

The first one is the Basic Time Stamp Ordering, which counts tothe pessimistic protocols and with the BOCC protocol we will alsosee an optimistic scheduler

Page 25: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 25

Concurrency Controll AlgorithmsTimestamp Ordering (TO)

Timestamp Ordering Rule (TO rule):Each transaction ti is assigned a unique timestamp ts(ti) (e.g., the time of ti‘s beginning).

If pi(x) and qj(x) are in conflict, then the following must hold:

pi(x) is executed before qj(x) iff ts(ti) < ts(tj).

Timestamp Ordering protocols get rid of locks and use timestamps instead.

Page 26: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 26

Concurrency Controll AlgorithmsBasic Timestamp Odering (BTO)

Basic timestamp ordering protocol (BTO):

For each data item x maintain • max-r-scheduled(x): the value of the largest timestamp of a read operation on x

already sent to the scheduler• max-w-scheduled(x): the value of the largest timestamp of a write operation on

x already sent to the scheduler

Operation pi(x) is compared to max-q (x) for each conflicting q:• if ts(ti) < max-q (x) for some q then abort ti

• else schedule pi(x) for execution and set max-p (x) to ts(ti)

Page 27: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 27

Concurrency Controll AlgorithmsBasic Timestamp Ordering (BTO)

BTO Example:s = r1(x) w2(x) r3(y) w2(y) c2 w3(z) c3 r1(z) c1

r1(x) w2(x) r3(y) a2 w3(z) c3 a1

Page 28: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 28

Concurrency Controll AlgorithmsScheduler Classification – Algorithms Overview

Page 29: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 29

Concurrency Controll Algorithms

Overview

I. Introduction basics

II. Locking schedulersTwo-Phase Locking

• Non-Two-Phase Locking Protocols Write-Only Tree Locking Read/Write Tree Locking

III. Non-locking schedulers

Timestamp Ordering

IV. Optimistic schedulers BOCC

V. Hybrid Schedulers

Page 30: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 30

Concurrency Controll AlgorithmsOptimistic Protocols

In some scenarios optmistic protocols can do a better job then pessimistic.A product catalog application where 99% of the transactions are just read price information and descriptions of products. From time to time prices are updated or new products are added, but this occurs with a very low frequency compared to the read events.

A 2PL protocol for example would waste a considerable amount of time for managing locks, instead of reading data items.

optimistic schedulers do a good job in cases were conflicts aren‘t frequent

Page 31: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 31

Concurrency Controll AlgorithmsThe three phases of a optimistic scheduler

1. Read phase:The transaction is executed, but all writes applied to a workspace private to the transaction only (not the database)

2. Validation phase:The scheduler tests if its execution has been „correct“ in the sense of conflict serializability and whether the result can be copied to database – if not the transaction is aborted, otherwise the next phase is entered

3. Write phase:The workspace contents are transferred into the database to conclude the transaction

Page 32: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 32

Concurrency Controll AlgorithmsBOCC

BOCC validation of tj:compare tj to all previously committed ti

accept tj if one of the following holds• ti has ended before tj has started, or• RS(tj) WS(ti) = and ti has validated before tj

Under backward-oriented optimistic concurrency control (BOCC),a transaction under validation executes a conflict test against all those transactions that are already committed.

Page 33: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 33

Concurrency Controll AlgorithmsBOCC

Example:Execution of BOCC

Page 34: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 34

Concurrency Controll Algorithms

Overview

I. Introduction Basics

II. Locking schedulersTwo-Phase Locking

• Non-Two-Phase Locking Protocols Write-Only Tree Locking Read/Write Tree Locking

III. Non-locking schedulers

Timestamp Ordering

IV. Optimistic schedulers BOCC

Page 35: Concurrency Control Algorithms

Wolfgang Miller / 02.07.2009 / 35

Concurrency Controll Algorithms

Thank you for your attention.

Any questions?