+ All Categories
Home > Education > Database concurrency control & recovery (1)

Database concurrency control & recovery (1)

Date post: 07-Apr-2017
Category:
Upload: rashid-khan
View: 42 times
Download: 1 times
Share this document with a friend
52
Concurrency Control & Recovery Database Consistency Multi User Environment(Data Sharing) Transactions interference System Crash Hardware failure Software failure Concurrency Control Safeguard against transaction interference Database Recovery Restore database to earlier consistent state
Transcript
Page 1: Database concurrency control & recovery (1)

Concurrency Control & Recovery Database Consistency

Multi User Environment(Data Sharing)

Transactions interferenceSystem Crash

Hardware failureSoftware failure

Concurrency ControlSafeguard against transaction interference

Database RecoveryRestore database to earlier consistent state

Page 2: Database concurrency control & recovery (1)

The concept of Transaction Action(s) by user or program to read/write in the database

Logical unit of work against a database

either done entirely or not even a bit of it

Consist of SQL query and/or programming instructions

Page 3: Database concurrency control & recovery (1)

DBMS/Database Architecture

Page 4: Database concurrency control & recovery (1)

The concept of Transaction

States of Transaction

Page 5: Database concurrency control & recovery (1)

The concept of TransactionACID Properties of Transaction Atomicity : All or Nothing

Consistency : Transform database from one consistent state to next

Isolation : Independent of each other

Durability : Permanent effects

Page 6: Database concurrency control & recovery (1)

Concurrency ControlDatabase: Shared DataMultiple transactions, concurrent access, potential interferenceMultiple reads, No problemMultiple reads, at least one write: Potential interferenceConcurrency: Managing concurrent access to avoid interference

Page 7: Database concurrency control & recovery (1)

Transactional Interference: Potential Problems

Lost Update

Page 8: Database concurrency control & recovery (1)

Transactional Interference: Potential Problems

Uncommitted Dependency(Dirty Read)

Page 9: Database concurrency control & recovery (1)

Transactional Interference: Potential Problems

Inconsistent Analysis

Page 10: Database concurrency control & recovery (1)

SerializabilitySchedule: A sequence of the operations by a set of concurrent transactions that preserves the order of the operations in each of the individual transactions.

Serial Schedule: A schedule where the operations of each transaction are executed consecutively without any interleaved operations from other transactions.

Nonserial Schedule: A schedule where the operations from a set of concurrent transactions are interleaved.

Page 11: Database concurrency control & recovery (1)

SerializabilityTwo transactions—same data item—only read

(No Problem)Two transaction—different data items—read/write

(No problem)Two transactions—same data items—either of them write (Potential Problem, the order matters)

Serializable Schedule: If the interleaved operations of the two concurrent transactions produce the same results, are called seriazable schedule.

Page 12: Database concurrency control & recovery (1)

Concurrency Control: Serializability

(a) And (b) are two equivalent serializable schedules

(c) Is the serial schedule

Page 13: Database concurrency control & recovery (1)

Concurrency Control: RecoverabilitySerializability

Serialiazable schedules maitain consistencyAssumption: No failurePotential Problem: Irrecoverable Schedule

Irrecoverable Schedule

Page 14: Database concurrency control & recovery (1)

Concurrency Control: RecoverabilityRecoverable ScheduleIf ITEM(a) was updated by Transaction Ti and latter on read by Tj, then Ti should commit prior to Tj.

Concurrency Control Techniques• Locking• Time Stamping• Optimistic Techniques

Page 15: Database concurrency control & recovery (1)

Concurrency Control Techniques: LockingAn item accessed/updated by one transaction may be denied access by another transaction.

Locking• Shared Lock(read): can only read• Exclusive Lock(write): can do both read and

write• System support for upgrade/downgrade of locks

Page 16: Database concurrency control & recovery (1)

Two-Phase 2PL Locking ProtocolAll the locking operation in a transaction must precede the first unlock

Growing Phase: A lock is required as soon as a data item is accessed. May be read or write. All locks are secured. No Unlock

Shrinking Phase: No new lock could be acquired after first unlock. Locks are only released.

Upgrade allowed only in growing phase

Downgrade allowed only in shrinking phase

Page 17: Database concurrency control & recovery (1)

Two-Phase 2PL Locking Protocol

Preventing Lost Update Problem

Page 18: Database concurrency control & recovery (1)

Two-Phase 2PL Locking Protocol

Preventing Uncommitted Dependence Problem

Page 19: Database concurrency control & recovery (1)

Two-Phase 2PL Locking Protocol

Preventing Inconsistent Analysis Problem

Page 20: Database concurrency control & recovery (1)

Two-Phase 2PL Locking Protocol

Creating Cascade rollback Problem

Page 21: Database concurrency control & recovery (1)

Two-Phase 2PL Locking ProtocolPossible Solution to cascade rollback

Rigorous 2PL: Release all unlock at the end Strict 2PL: hold only exclusive unlocks till end

Page 22: Database concurrency control & recovery (1)

Deadlock: A locking problemWhen two(or more) transactions wait for each other to release their corresponding locks.

Problem: Deadlock

Page 23: Database concurrency control & recovery (1)

Deadlock: A locking problemSolution: Rollback certain transaction(s) and restart User should be unaware of deadlock and solution

Solution: Timing Deadlock Prevention Deadlock detection and recovery

Page 24: Database concurrency control & recovery (1)

Deadlock: SolutionsTiming• System defined time slice• If transaction timed out, aborted and restarted automatically• transaction may not necessarily be in deadlock• Simple protocol, used by many commercial DBMSs

Page 25: Database concurrency control & recovery (1)

Deadlock: SolutionsDeadlock Prevention

•Two solutions by proposed by Rosenkrants et. al. (1978)•Timestamp assigned to each transaction•Wait-Die: older transaction wait for younger•If younger request lock hold by older, younger aborted(die), restarted with same timestamp (eventually gets oldest)•Wound-Wait: younger wait for older•If older request a lock hold by younger, younger is aborted(wounded)

Conservative 2PL•Acquire and release all locks at once•Advantage if lock contention is heavy: No blocking, no wait•Low Contention: Locks are held longer•High lock setting overheads: Must release all locks even if single of them not granted.•Not practical: Advanced knowledge of locks required

Page 26: Database concurrency control & recovery (1)

Deadlock: SolutionsDeadlock detection and Recovery•TiTj shows Ti is dependent on Tj

•Shows Tj hold a resource required by Ti

•Deadlock exists if WFG contains circle TiTjTk•Frequency of deadlock detection•Too large: deadlock undetected•Too small: time waisted •Dynamic approach

Wait-for-graph

Page 27: Database concurrency control & recovery (1)

Deadlock: SolutionsRecovery•Choice of deadlock victim•Transaction that has been running the long•How many dataitems have been updated•How many dataitems to update

•How far to rallback•Avoiding starvation•The same transaction is the victim repeatidly•Use a counter to count number of time a transaction rollbacked•If reach upper limit, use different protocol

Page 28: Database concurrency control & recovery (1)

Timestamping (Another concurrency control protocol)

Timestamp: A unique identifier, represent the

relative starting time/order of the transactions

-- System clock or logical counter is usedTimestamping:

Older transactions get priority incase of conflict

A read and write by a transaction on a data item is allowed only if the preceding update to the data item was made by older transaction

Page 29: Database concurrency control & recovery (1)

Timestamping ( Timestamping continues…)

A transact T has timestamp ts(T) A dataitem x has read timestamp as read_timestamp(x) and write timestamp as

write_timestamp(x) Transaction T wants read x

Allowed only if ts(T)>write_timestamp(x) otherwise an older(earlier) transaction is trying to read a value updated by younger(newer) transaction

Older transaction is too late, rollbacked and restarted with new time stamp

Set read_timestamp=max(ts(T), read_timestamp(x)

Page 30: Database concurrency control & recovery (1)

Timestamping ( Timestamping continues…)

Transaction T wants write x Allowed only if ts(T)>read_timestamp(x) and ts(T)>write_timestamp(x)If ts(T)<read_timestamp(x) then younger (newer) transaction has already read it and is using it and older is late in updatingSimilarly if ts(T)<write_timestamp(x) then T is trying to update x to an obsolete valueIn both cases Restart T with later timestampOtherwise the transaction can proceedTimestamping is serializable, but not recoverable

Page 31: Database concurrency control & recovery (1)

Timestamping(Thomas’s Write Rule) Transaction T wants write x

Allowed only if ts(T)>read_timestamp(x) and ts(T)>write_timestamp(x)If ts(T)<read_timestamp(x) then younger (newer) transaction has already read it and is using it and older is late in updatingSimilarly if ts(T)<write_timestamp(x) then T is trying to update x to an obsolete valueIn first case Restart T with later timestamp, as beforeIn the second case simply ignore update, called Ignore Obsolete Write Rule

Otherwise the transaction can proceed

Page 32: Database concurrency control & recovery (1)

Optimistic Techniques Conflict (interaction between transactions) is rare, is the basic premise No conflict checking, No delays Efficient policy where conflicts are less frequent Before commit, check for conflict, rollback if found Very efficient: No locks, no concurrency checks According to premise less transaction rollback Intolerable in environment where conflicts are frequent Choose another concurrency control

Page 33: Database concurrency control & recovery (1)

Optimistic TechniquesThree phases in OT

Read PhaseExtends from start to commitAll values are read and stored in locallyChanges are made to local variables

Validation PhaseChecks serializibilty not violated, database remain consistentRestart transaction if conflict occurred, restart

Write PhaseIf update transaction, apply changes to database stored locally

Page 34: Database concurrency control & recovery (1)

Optimistic Techniques Assign timestamps start(T), validation(T), finish(T) to each transaction T Validation is passed only if

All earlier should finish before T i.e. finish(E)<start(T)If finish(E)>start(T) then

Data items written by E are diff than read by T

(Writes done serially)Start(T)<finish(E)<validation(T)

Page 35: Database concurrency control & recovery (1)

Granularity for Dataitems

The size of data item used as unit of protection

Page 36: Database concurrency control & recovery (1)

Granularity of Data item The size of data item used as unit of protection Granularity has greater performance implications on concurrency control algorith There is a tradeof between coarse vs fine granularity E.g. Granularity is not the same for updating a single record vs 80% records of a tableCoarse granularity, low degree of concurrency, low locking information maintenance Fine granularity, High degree of concurrency, more locking information maintenance A better approach, mixed granularity, upgrade and downgrade of locks

Page 37: Database concurrency control & recovery (1)

Database RecoveryRestore database to correct state incase of failure DBMS is resilient if it is fault tolerant

Page 38: Database concurrency control & recovery (1)

Storage Media Types Volatile Primary Memory, random access, fast, but expensive Non-volatile online secondary memory disk storage, random access Other non volatile offline secondary storages: Magnetic Tape and Optical Disk Suit only backup, slow, MT sequential access

Page 39: Database concurrency control & recovery (1)

Types of Failures System crash Media failure Application software errors Natural physical disasters Carelessness Sabotage

All failures involve either main memory or disk copy

Page 40: Database concurrency control & recovery (1)

Transactions and Recovery Unit of recovery is transaction

Recovery manager guarantee atomicity and durability

Database buffer complicate the issue

Durability guarantees when database buffer flushed

Committed transactions may not reach the database

Buffer flushed either when full or forced written

Page 41: Database concurrency control & recovery (1)

Transactions and Recovery (continue)In the event of failure

oActive transactions(incomplete) udone, i.e. rollbackedoCommitted tranactions are redone, called rollforwardoPartial undo, when single transaction roll backoGlobal undo, when all active transactions rollbacked

Page 42: Database concurrency control & recovery (1)

Transactions and Recovery (continue)

Example transactions rollback/rollforward

Page 43: Database concurrency control & recovery (1)

Buffer ManagementPages brought in as soon as requestedWhen buffer is full, old pages replaced with new onesPage replacement policies: FIFO, LRUTwo var associated with each page: pinCount, DirtyOn each request pinCount is incr, also called pinnedDecr by the system when donePinned pages can’nt be replacedWrite to disk if Dirty is set, on replacementFor new page Dirty is set to 0

Page 44: Database concurrency control & recovery (1)

Buffer Management (Continued…)When writing pages two policies used

o Steal PolicyPinned pages could be stolen from the transaction, i.e. written to diskAlternative is no-steal

o Force PolicyDirty pages are immediately written to disk on committAlternative no-force

No steal and force is simple to implementWith no-steal no rollback, with force no rollforward Steal and forceSteal obviate the need for large buffer spaceno-force provide opportunity for later transaction to update and then write

Page 45: Database concurrency control & recovery (1)

Recovery Facilitieso Backup Mechanismo Logging (also called journaling)o Checkpointingo Recovery meneger

BackupOffline storage of data and log filesUsed if database is distroyed or damagedTacken at regular intervalEither complete or incremental BackupIncr is changes after last full/incr backup

Page 46: Database concurrency control & recovery (1)

Log/Journal FileOnly for

insert and update

Type of

Record

Only for

delete and

Update

Next Record of this transaction

Page 47: Database concurrency control & recovery (1)

Log/Journal File (Continues…)

Important in both recovery and performance• Log file is some times duplexed or triplexed• for performance, log stored on separate physical drive• backup log file where log data is huge• minor failures recovered from online log in short time• Major failure from offline log

Page 48: Database concurrency control & recovery (1)

Checkpoint The point of synchronization b/w data and log files Buffers are force written Force write all committed and active transactions Also a check point record is written(consist of IDs of Active transactions)

During Recoveryo Rollforward transactions with commit record after the last checkpointo Rollback transactions without commit record

Page 49: Database concurrency control & recovery (1)

Recovery TechniquesTwo types• Major recovery if database file damaged• Restore last back • Apply changes from log file after last backup• Assumption: Log file not damaged, separate storage

• Minor recovery such as after system crash• Rollforward/rollback certain transactions• Use the before and after image in log file• Two protocols deferred update and immediate update are used

Page 50: Database concurrency control & recovery (1)

Recovery Techniques(continues…)

Deferred Update Don’t write until commit, no undoing if aborted Requires redoing committed transaction

Log File Use1. Write start record at start2. During write, write log record except before

image, don’t write anything to buffer or database

3. If transaction commit, write commit log record, also record changes to database buffer/database

4. If transaction abort, do nothing, just ignore log record

During RecoveryOnly rollforward, repeated failures, write operations

idempotant

Page 51: Database concurrency control & recovery (1)

Recovery Techniques(continues…)

Deferred Update Immediately record every change, need undoing if aborted But still requires redoing committed transaction

Log File Use1. Write start record at start2. During write, write log record, both before and after image3. After writing log, now record the changes to buffer4. Actual changes will reach database when buffer next

flushed5. If transaction commit, write commit to database buffer6. If transaction abort, undo required, use before image log7. Write-ahead log protocol is a must

During RecoveryBoth rollforward, using after-image, and rollback, using

before-image

Page 52: Database concurrency control & recovery (1)

Recovery Techniques(continues…)

Shadow Pagingo Log-less protocolo Maintains two page tables for a transaction, current page table and shadow page tableo Both are the same in starto Shadow never changed, used for recoveryo changes recorded to currento After transaction complete, current page becomes shadow

Advantages No log no log overheads faster recovery, no undo/redo

Disadvantage Data fragmentation Periodic garbage collection of inaccessible blocks


Recommended