transactions and concurrency control. concurrent accesses to an object multiple threads atomic...

63
Transactions and Concurrency Control

Upload: brian-clark

Post on 20-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Transactionsand

Concurrency Control

Page 2: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Concurrent Accesses to an Object

• Multiple threads

• Atomic operations

• Thread communication

• Fairness

Page 3: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Operations of the Account Interface

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 4: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Operations of the Branch Interface

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 5: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Transactions (on Objects)

• The basic concept: a sequence of client requests performed as an indivisible unit.

• Properties: Atomicity, Consistency, Isolation, and Durability.

• Means to maximize concurrency: serializable interleavings

Page 6: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

A Client’s Bank Transaction

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 7: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

A Failure Model

• Writes to permanent storage may fail

• Processors may crash

• Messages may be delayed or even lost

The faults are assumed to be detectable.

Page 8: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Operations in the Coordinator Interface

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 9: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Transaction Life Histories

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 10: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

The Lost Update Problem

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 11: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

The Inconsistent Retrievals Problem

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 12: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Serializable Interleavings

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 13: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Serializable Interleavings (cont’d)

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 14: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Ensuring Serializability

All pairs of conflicting operations of two transactions should be executed in the same order to ensure serializability.

(Two operations conflict if their combined effect depends on the order in which they are executed.)

Page 15: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Conflict Rules for Read and Write

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 16: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

A Non-Serializable Interleaving

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 17: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Recoverability from Aborts

• Dirty reads

• Cascading aborts

• Premature writes

• Strict executions of transactions

• Use of tentative versions

Page 18: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

A Dirty Read

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 19: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Overwriting Uncommitted Values

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 20: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Nested Transactions

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 21: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Advantages of Nested Transactions

• Subtransactions at the same level may run concurrently

• Subtransactions can commit or abort independently

Page 22: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Methods of Concurrency Control

• Locking: locks are used to order transactions according to the order of arrival of their operations at the same data item

• Optimistic concurrency control: transactions are allowed to proceed until they are ready to commit, whereupon a check is made to see whether they have performed conflicting operations

• Timestamp ordering: timestamps are used to order transactions according to their starting times

Page 23: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Exclusive Locks

Exclusive locks are a simple serializing mechanism:

• Two-phase locking: all locks are acquired in the first phase and released in the second

• Strict two-phase locking: locks are held until the transaction commits or aborts.

Granularity is an important issue.

Page 24: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Using Exclusive Locks

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 25: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Locking

• A pair of read operations on the same data item do not conflict.

• Exclusive locks reduce concurrency more than is necessary.

• The “many reader/single write” scheme distinguishes two types of lock: read (shared) locks and write locks.

• Two-phase locking or strict two-phase locking still applies for ensuring serializability.

Page 26: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Lock Compatibility

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 27: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Lock Management

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 28: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

A Lock Implementation

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 29: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

A Lock Implementation (cont’d)

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 30: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

A Lock Manager Implementation

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 31: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Deadlocks

• The use of locks may lead to deadlock.

• Deadlock is a state in which each member of a group of transactions is waiting for some other member to release a lock.

• A wait-for graph can be used to represent the waiting relationships between concurrent transactions at a server.

Page 32: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

A Deadlock

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 33: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

A Wait-For Graph

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 34: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

A Cycle in a Wait-For Graph

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 35: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Another Wait-For Graph

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 36: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Deadlock Prevention Techniques

• Lock (in one atomic step) all data items used by a transaction when it starts.

• Every transaction requests locks on data items in a predefined order.

• Each lock is given a limited period, after which it becomes vulnerable.

Page 37: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Deadlock Detection

• Deadlocks may be detected by finding cycles in the wait-for graph.

• Two design issues:

* Frequency of checking the existence of a cycle

* Choice of transactions to be aborted

Page 38: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Resolving a Deadlock

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 39: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Increasing Concurrency

• Two-version locking: allows one transaction to write tentative versions of data items while other transactions read from the committed version of the same data item; read operations wait only if another transaction is currently committing the same data item

• Hierarchic locks: at each level, the setting of a parent lock has the same effect as setting all the equivalent child locks

Page 40: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Lock Compatibility

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 41: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

A Lock Hierarchy

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 42: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Compatibility of Hierarchical Locks

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 43: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Drawbacks of Locking

• Overhead of lock maintenance: some locks may be unnecessary

• Reduced concurrency due to

* deadlock prevention

* holding locks until the end of a transaction (to avoid cascade aborts)

In some applications, the likelihood of conflict is low.

Page 44: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Optimistic Concurrency Control

• Transactions are allowed to proceed as though there were no possibility of conflict with other transactions until the client issues a CloseTransaction request.

• When a conflict is detected, some transaction is aborted.

Page 45: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Optimistic Concurrency Control (cont’d)

Each transaction has three phases:

• Read phase: use a tentative version for each updated data item

• Validation phase: checking if there is a conflict

• Write phase: if validated, all tentative versions are made permanent

Page 46: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Naive Read/Write Rules

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 47: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Validation

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 48: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Validation of Transactions

• For validation purposes, each transaction is assigned (in an ascending order) a transaction number when it enters the validation phase.

• A transaction always finishes its read phase after all transactions with lower numbers.

Page 49: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Validation of Transactions (cont’d)

• Validation phases may overlap (but transaction numbers should be assigned sequentially).

• All write phases are executed sequentially according to their transaction numbers so that there is no need to check write-write conflicts.

* Reuse of transaction numbers (as suggested in the book) is not a very good idea.

Page 50: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Validation

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 51: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Comparing Backward and Forward Validation

• Backward: abort the transaction being validated

Forward: three options:* defer the validation until the conflicting (and active) transactions have finished

* abort all the conflicting active transactions and commit the one being validated

* abort the transaction being validated

Page 52: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Comparing Backward and Forward Validation (cont’d)

• Backward: must retain the write sets of committed transactions that may conflict with active transactions

Forward: must allow for new transactions to start during validation

• Backward: compare a possibly large read set against old write sets

Forward: compare a small write set against the read sets of active transactions

Page 53: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Timestamp Ordering

• Each transaction is assigned a unique timestamp value when it starts.

• Every operation bears the timestamp of its issuing transaction and is validated when it is carried out.

• If the operation cannot be validated, then the transaction is aborted immediately.

Page 54: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Read/Write Rules

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 55: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Writes and Timestamp Ordering

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 56: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Write Rules in Timestamp Ordering

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 57: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Reads and Timestamp Ordering

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 58: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Read Rules in Timestamp Ordering

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 59: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Timestamp Ordering

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 60: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Relaxing the Write Rule

• If a write is too late it can be ignored instead of aborting the transaction, because if it had arrived in time its effects would have been overwritten anyway.

• However, if another transaction has read the data item, the transaction with the late write fails due to the read timestamp on the item.

Page 61: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Multiversion Timestamp Ordering

• The server keeps old committed versions as well as tentative versions in its list of versions of data items.

• With the list, Read operations that arrive too late need not be rejected.

• A Read operation of a transaction is directed to the version with the largest write timestamp less than the transaction timestamp.

Page 62: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

A Late Write Invalidating a Read

Source: G. Coulouris et al., Distributed Systems: Concepts and Design, Third Edition.

Page 63: Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness

Comparison

Locks Timestamps

"Attitude" pessimistic optimistic

OrderDecision

dynamic static

BenefitingTrans.

more writesthan reads

read-only

ConflictResolution

wait; mayabort later

abortimmediately