What is Concurrent Process (CP)? What is Concurrent Process (CP)? • Multiple users access databases and use Multiple users access databases and use computer systems computer systems simultaneously. simultaneously. • Example: Airline reservation system. Example: Airline reservation system. – An airline reservation system is used by An airline reservation system is used by hundreds of travel agents and reservation hundreds of travel agents and reservation clerks concurrently. clerks concurrently. Why Concurrent Process? Why Concurrent Process? • Better transaction throughput and Better transaction throughput and response time response time • Better utilization of resource Better utilization of resource Introduction Introduction
38
Embed
What is Concurrent Process (CP)? Multiple users access databases and use computer systems Multiple users access databases and use computer systems simultaneously.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
What is Concurrent Process (CP)?What is Concurrent Process (CP)?• Multiple users access databases and use computer systems Multiple users access databases and use computer systems
simultaneously.simultaneously.
• Example: Airline reservation system.Example: Airline reservation system.– An airline reservation system is used by hundreds of travel agents An airline reservation system is used by hundreds of travel agents and reservation clerks concurrently.and reservation clerks concurrently.
Why Concurrent Process?Why Concurrent Process?• Better transaction throughput and response timeBetter transaction throughput and response time
• Better utilization of resourceBetter utilization of resource
IntroductionIntroduction
TransactionTransaction• What is Transaction?What is Transaction?• A sequence of many actions which are considered to be oneA sequence of many actions which are considered to be one atomic unit of work.atomic unit of work.• Basic operations a transaction can include “actions”:Basic operations a transaction can include “actions”:
• ACID properties of transactionACID properties of transaction• AAtomicitytomicity:: Transaction is either performed in its entirety or not performed at all, Transaction is either performed in its entirety or not performed at all,
this should be DBMS’ responsibilitythis should be DBMS’ responsibility
• CConsistencyonsistency:: Transaction must take the database from one consistent state to Transaction must take the database from one consistent state to another. It is user’s responsibility to insure consistencyanother. It is user’s responsibility to insure consistency
• IIsolationsolation:: Transaction should appear as though it is being executed in isolation from Transaction should appear as though it is being executed in isolation from other transactionssother transactionss
• DDurabilityurability:: Changes applied to the database by a committed transaction must persist, Changes applied to the database by a committed transaction must persist, even if the system fail before all changes reflected on diskeven if the system fail before all changes reflected on disk
Concurrent TransactionsConcurrent Transactions
interleaved processing parallel processing
t1 t2 t1 t2
time
CPU1
CPU2
A
B
A
B
CPU1
SchedulesSchedules
• What is SchedulesWhat is Schedules– A schedule S of n transactions T1,T2,…Tn is an ordering of the operations of A schedule S of n transactions T1,T2,…Tn is an ordering of the operations of
the transactions subject to the constraint that, for each transaction Ti that the transactions subject to the constraint that, for each transaction Ti that participates in S, the operations of Ti in S must appear in the same order in participates in S, the operations of Ti in S must appear in the same order in which they occur in Ti.which they occur in Ti.
Oops, something’s wrongOops, something’s wrong• Reserving a seat for a flightReserving a seat for a flight• If concurrent access to data in DBMS, two users may try to book the same seat If concurrent access to data in DBMS, two users may try to book the same seat
simultaneouslysimultaneously
Agent 1 findsseat 35G empty Agent 2 finds
seat 35G empty
time
Agent 1 setsseat 35G occupied
Agent 2 setsseat 35G occupied
Another exampleAnother example• Problems can occur when concurrent transactions execute in an uncontrolled Problems can occur when concurrent transactions execute in an uncontrolled
manner.manner.• Examples of one problem.Examples of one problem.
– A original equals to 100, after execute T1 and T2, A is supposed to be 100+10-A original equals to 100, after execute T1 and T2, A is supposed to be 100+10-8=1028=102
Add 10 To AAdd 10 To A Minus 8 from AMinus 8 from A
T1T1 T2T2
Read(A)Read(A)
A=A+10A=A+10
Write(A)Write(A)
Read(A)Read(A)
A=A-8A=A-8
Write(A)Write(A)
Value of A Value of A on the diskon the disk
100100
100100
100100
100100
110110
9292
What Can Go Wrong?What Can Go Wrong?
• Concurrent process may end up violating Isolation property of Concurrent process may end up violating Isolation property of transaction if not carefully scheduledtransaction if not carefully scheduled
• Transaction may be aborted before committedTransaction may be aborted before committed- undo the uncommitted transactions- undo the uncommitted transactions
- undo transactions that sees the uncommitted change before the crash- undo transactions that sees the uncommitted change before the crash
Conflict operationsConflict operations• Two operations in a schedule are said to be Two operations in a schedule are said to be conflict conflict if they satisfy if they satisfy
all three of the following conditions:all three of the following conditions:(1)(1) They belong to different transactionsThey belong to different transactions
(2)(2) They access the same item A;They access the same item A;
(3)(3) at least one of the operations is a write(A)at least one of the operations is a write(A)
Example in Sa: r1(A),r2(A),w1(A),w2(A), a1,c2;Example in Sa: r1(A),r2(A),w1(A),w2(A), a1,c2;
– r1(A),w2(A) conflict, so do r2(A),w1(A), r1(A),w2(A) conflict, so do r2(A),w1(A),
– r1(A), w1(A) do not conflict because they belong to the same r1(A), w1(A) do not conflict because they belong to the same transaction,transaction,
– r1(A),r2(A) do not conflict because they are both read operations.r1(A),r2(A) do not conflict because they are both read operations.
Serializability of schedulesSerializability of schedules• Serial Serial
– A schedule S is A schedule S is serial serial if, for every transaction T participating in the schedule, if, for every transaction T participating in the schedule, all the operations of T are executed consecutively in the schedule.( No all the operations of T are executed consecutively in the schedule.( No interleaving occurs in a serial schedule)interleaving occurs in a serial schedule)
• SerializableSerializable– A schedule S of n transactions is A schedule S of n transactions is serializableserializable if it is if it is equivalentequivalent to some to some serialserial
schedule of the same n transactions.schedule of the same n transactions.
• schedules are conflict equivalent if:schedules are conflict equivalent if:– they have the same sets of actions, and they have the same sets of actions, and – each pair of conflicting actions is ordered in the same wayeach pair of conflicting actions is ordered in the same way
• schedules are conflict equivalent if:schedules are conflict equivalent if:– they have the same sets of actions, and they have the same sets of actions, and – each pair of conflicting actions is ordered in the same wayeach pair of conflicting actions is ordered in the same way
• Conflict SerializableConflict Serializable– A schedule is said to be conflict serializable if it is conflict equivalent to a A schedule is said to be conflict serializable if it is conflict equivalent to a
– – But T2 has already committed!But T2 has already committed!• A recoverable schedule is one in A recoverable schedule is one in
which this cannot happen.which this cannot happen.– – i.e. a X act commits only after all i.e. a X act commits only after all the X acts it “depends on” (i.e. it the X acts it “depends on” (i.e. it reads from) commit.reads from) commit.– – ACA implies recoverable (but not ACA implies recoverable (but not vice-versa!).vice-versa!).
3. strict schedule3. strict schedule
T1T1 T2T2
Read(Read(A)A)
Write(Write(A)A)
AbortAbort
Read(Read(A)A)
Write(Write(A)A)
T1T1 T2T2
Read(Read(A)A)
Write(Write(A)A)
commicommitt
Read(Read(A)A)
Write(Write(A)A)T1T1 T2T2
Read(ARead(A))
Write(Write(A)A)
AbortAbort
Read(Read(A)A)
Write(Write(A)A)
CommiCommitt
T1T1 T2T2
Read(Read(A)A)
Write(Write(A)A)
CommiCommitt
Read(Read(A)A)
Write(Write(A)A)
CommiCommitt
T1T1 T2T2
Read(Read(A)A)
Write(Write(A)A)
CommiCommitt
Read(Read(A)A)
Write(Write(A)A)
CommiCommitt
No No Yes Yes
Venn Diagram for SchedulesVenn Diagram for Schedules
• serializable: Yes, equivalent to T1,T2serializable: Yes, equivalent to T1,T2• conflict-serializable: Yes, conflict-conflict-serializable: Yes, conflict-
equivalent to T1,T2equivalent to T1,T2• recoverable: No. Yes, if C1 and C2 are recoverable: No. Yes, if C1 and C2 are
switched.switched.• ACA: No. Yes, if T1 commits before T2 ACA: No. Yes, if T1 commits before T2
writes X.writes X.
Sample Transaction Sample Transaction (informal)(informal)• Example: Move $40 from checking to Example: Move $40 from checking to
savings accountsavings account• To user, appears as one activityTo user, appears as one activity• To database:To database:
– Read balance of checking account: read( X)Read balance of checking account: read( X)– Read balance of savings account: read (Y)Read balance of savings account: read (Y)– Subtract $40 from XSubtract $40 from X– Add $40 to YAdd $40 to Y– Write new value of X back to diskWrite new value of X back to disk– Write new value of Y back to diskWrite new value of Y back to disk
Focus on concurrency Focus on concurrency controlcontrol
• Real DBMS does not test for serializabilityReal DBMS does not test for serializability– Very inefficient since transactions are continuously Very inefficient since transactions are continuously
arrivingarriving
– Would require a lot of undoingWould require a lot of undoing
• If followed by every transaction, and enforced If followed by every transaction, and enforced by transaction processing system, guarantee by transaction processing system, guarantee serializability of schedulesserializability of schedules
Concurrency Control Through Concurrency Control Through LocksLocks
• Lock:Lock: variable associated with each variable associated with each data itemdata item– Describes status of item wrt operations that Describes status of item wrt operations that
Let’s run T1 and T2 in interleafed fashionLet’s run T1 and T2 in interleafed fashion
Schedule S
Two-Phase Locking (2PL)Two-Phase Locking (2PL)
• Def.: Transaction is said to follow Def.: Transaction is said to follow the the two-phase-locking protocoltwo-phase-locking protocol if if all locking operations precede the all locking operations precede the firstfirst unlock operation unlock operation
ExampleExampleT1’T1’
read_lock(Y);read_lock(Y);
read_item(Y);read_item(Y);
write_lock(X);write_lock(X);
unlock(Y);unlock(Y);
read_item(X);read_item(X);
X:=X+Y;X:=X+Y;
write_item(X);write_item(X);
unlock(X);unlock(X);
T2’T2’read_lock(X);read_lock(X);
read_item(X);read_item(X);
write_lock(Y);write_lock(Y);
unlock(X);unlock(X);
read_item(Y);read_item(Y);
Y:=X+Y;Y:=X+Y;
write_item(Y);write_item(Y);
unlock(Y);unlock(Y);
• Both T1’ and T2’ follow the 2PL protocolBoth T1’ and T2’ follow the 2PL protocol• Any schedule including T1’ and T2’ is guaranteed to be serializableAny schedule including T1’ and T2’ is guaranteed to be serializable• Limits the amount of concurrencyLimits the amount of concurrency
Variations to the Basic Variations to the Basic ProtocolProtocol
• Previous technique knows as Previous technique knows as basic basic 2PL2PL
• ConservativeConservative 2PL (static) 2PL: Lock 2PL (static) 2PL: Lock all items needed BEFORE all items needed BEFORE execution begins by predeclaring execution begins by predeclaring its read and write setits read and write set– If any of the items in read or write set If any of the items in read or write set
is already locked (by other is already locked (by other transactions), transaction waits (does transactions), transaction waits (does not acquire any locks)not acquire any locks)
– Deadlock free but not very realisticDeadlock free but not very realistic
Variations to the Basic Variations to the Basic ProtocolProtocol• StrictStrict 2PL: Transaction does not 2PL: Transaction does not
release its write locks until AFTER release its write locks until AFTER it aborts/commitsit aborts/commits– Not deadlock free but guarantees Not deadlock free but guarantees
recoverable schedules (strict recoverable schedules (strict schedule: transaction can neither schedule: transaction can neither read/write X until last transaction read/write X until last transaction that wrote X has that wrote X has committed/aborted)committed/aborted)
– Most popular variation of 2PLMost popular variation of 2PL
Concluding RemarksConcluding Remarks
• Concurrency control subsystem is Concurrency control subsystem is responsible for inserting locks at right responsible for inserting locks at right places into your transactionplaces into your transaction– Strict 2PL is widely usedStrict 2PL is widely used– Requires use of waiting queueRequires use of waiting queue
• All 2PL locking protocols guarantee All 2PL locking protocols guarantee serializabilityserializability
• Does not permit all possible serial Does not permit all possible serial schedulesschedules
• AACICIDD properties of Transaction properties of Transaction
Time
T1T2
T3
Crash
Database system should guaranteeDatabase system should guarantee
- DDurability : Applied changes by transactions urability : Applied changes by transactions must not be lost. ~ T3must not be lost. ~ T3
- AAtomicity : Transactions can be aborted.tomicity : Transactions can be aborted.
~ T1, T2~ T1, T2
System crashTransaction errorSystem errorLocal errorDisk failureCatastrophe
Basic Idea : “Logging”Basic Idea : “Logging”
• Undo/Redo by the Log Undo/Redo by the Log recover Non-catastrophic failure recover Non-catastrophic failure
Time
T1T2
T3
Crash
System LogSystem Log
- keeps info of changes - keeps info of changes applied by transactions applied by transactions
CheckpointBackup
• Full DB Backup Full DB Backup > Differential Backup> Differential Backup > > (Transaction) Log(Transaction) Log
Catastrophic failureCatastrophic failure
Physical View Physical View - How they work -- How they work - (1) (1)
Action :Action :
1)1) CheckCheck the directory the directory whetherwhether in the cache in the cache
2) If none, copy from 2) If none, copy from disk pagesdisk pages to to the cachethe cache
DiskMemory
DBMS cache
(buffers)
Disk pages/blocks
(address:A,a,1)(address:B,b,0)
A
B a
b
Directory
copy
flush
3) For the copy, old buffers needs to be 3) For the copy, old buffers needs to be flushed flushed from from the cachethe cache to to the disk pagesthe disk pages
B’
Physical View Physical View - How they work -- How they work - (2) (2)
DiskMemory
DBMS cache
(buffers)
Disk pages/blocks
(address:A,a,1)(address:B,b,0)
A
B a
b
Directory
copy
flush
4) Flush only if 4) Flush only if a dirty bita dirty bit is 1 is 1
Dirty bit : (in the directory) whether there is Dirty bit : (in the directory) whether there is a change after copy to the cachea change after copy to the cache 1 – updated in the cache1 – updated in the cache 0 – not updated in the cache (no need to 0 – not updated in the cache (no need to
flush) flush)
update
B’
Physical View Physical View - How they work -- How they work - (3) (3)
DiskMemory
DBMS cache
(buffers)
Disk pages/blocks
A
B a
b
copy
flush
A-a : A-a : “in-place updating”“in-place updating” - when flushing, overwrite at the same - when flushing, overwrite at the same locationlocation - - logginglogging is required is requiredB-b : B-b : “shadowing”“shadowing”
B’
Physical View Physical View - How they work -- How they work - (4) (4)
DiskMemory
DBMS cache
Data blocks
Bb
(1) copy (from the disk to the cache)
(2) update the cached data, record it in the log(3) flush the log and the data (from the cache to the disk)
copy
flush
Log blocks
update
B’ Logblocks
Datablocks
update
WAL : Write-Ahead Logging (1)WAL : Write-Ahead Logging (1)• in-place updating in-place updating A log A log is necessaryis necessary
Log entries flushed Log entries flushed before overwritingbefore overwriting main data main data
1) flush
UNDO-type log record
WAL : Write-Ahead Logging (2)WAL : Write-Ahead Logging (2)• WAL protocol requires WAL protocol requires UNDOUNDO and and REDOREDO
Time
Tcommit
LogLog
- BFIM cannot be overwritten by AFIM on diskBFIM cannot be overwritten by AFIM on disk until all until all UNDO-type logUNDO-type log have force-written to disk. have force-written to disk.
- The commit operation cannot be completedThe commit operation cannot be completed until all until all UNDO/REDO-type logUNDO/REDO-type log have force-written. have force-written.
UNDO
REDO
Steal & No-Force (1)Steal & No-Force (1)• Typical DBTypical DB employsemploys aa steal/no-forcesteal/no-force strategystrategy
Time
T1T2
T3
commitcommit
cache
Updated data by T2
cache Can be Used for other transactions (T3)
before T2 commits
• StealSteal strategy : a transaction strategy : a transaction cancan be written to disk be written to disk before it commitsbefore it commits
Advantage : Advantage : buffer space saving buffer space saving
Steal & No-Force (2)Steal & No-Force (2)
Time
T1T2
T3
commitcommit
cache
Updated data by T2
cache
If T3 needs the same data, it must be copied again
when T2 commits
• No-ForceNo-Force strategy : a transaction strategy : a transaction need notneed not to be to be
written to disk written to disk immediatelyimmediately when it commitswhen it commits
CheckpointingCheckpointing• Checkpoint Checkpoint - All DMBS buffers modified are wrote out to disk.- All DMBS buffers modified are wrote out to disk. - A record is written into the log. (- A record is written into the log. ([checkpoint][checkpoint])) - Periodically done- Periodically done (e.g. every n min. or every n transaction(e.g. every n min. or every n transaction
Time
T1T2
T3
Crash
Checkpoint
Transaction Rollback (1)Transaction Rollback (1)
• Rollback / Roll fowardRollback / Roll foward
Time
T1T2
T3
Crash
T4
T5
1 : Not necesary
2 : Roll foward
3 : Rollback
4 : Roll forward
5 : Roll back
Checkpoint
- Steal : transaction may be written on disk Steal : transaction may be written on disk before it commits before it commits
Recovery method
• example :example :
NamNamee
AccouAccountnt
Mr.AMr.A $10$10
Mr.BMr.B $2,000$2,000
Mr.CMr.C $30,00$30,0000
Transaction Rollback (2)Transaction Rollback (2)
TimeT2
Checkpoint Crash
T1 : A company pays salary to employees i) transfer $2,000 to Mr. A’s account ii) transfer $2,500 to Mr B’s account …
write(A)
read(A)
T1read(B)read(A) write(B)
write(C)read(C)write(A)
T2 : Mr.A pays the monthly rent. i) withdraw $1,500 from Mr.A’s account ii) transfer $1,500 to Mr.C’s account