数据库系统英文课件:ch16 Concurrency Control_第1页
数据库系统英文课件:ch16 Concurrency Control_第2页
数据库系统英文课件:ch16 Concurrency Control_第3页
数据库系统英文课件:ch16 Concurrency Control_第4页
数据库系统英文课件:ch16 Concurrency Control_第5页
已阅读5页,还剩68页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Chapter 16 : Concurrency Control Chapter 16: Concurrency ControlLock-Based Protocols T1 mestamp-Based ProtocolsValidation-Based ProtocolsMultiple GranularityMultiversion SchemesDeadlock HandlingInsert and Delete OperationsConcurrency in Index StructuresLock-Based ProtocolsA lock is a mechanism to co

2、ntrol concurrent access to a data itemData items can be locked in two modes : 1. exclusive独占 (X) mode. Data item can be both read as well as written. X-lock is requested using lock-X instruction. 2. shared 共享(S) mode. Data item can only be read. S-lock is requested using lock-S instruction.Lock requ

3、ests are made to concurrency-control manager. Transaction can proceed only after request is granted.Lock-Based Protocols (Cont.)Lock-compatibility matrixA transaction may be granted a lock on an item if the requested lock is compatible with locks already held on the item by other transactionsAny num

4、ber of transactions can hold shared locks on an item, but if any transaction holds an exclusive on the item no other transaction may hold any lock on the item.If a lock cannot be granted, the requesting transaction is made to wait till all incompatible locks held by other transactions have been rele

5、ased. The lock is then granted.Lock-Based Protocols (Cont.)Example of a transaction performing locking: T2: lock-S(A); read (A); unlock(A); lock-S(B); read (B); unlock(B); display(A+B)Locking as above is not sufficient to guarantee serializability if A and B get updated in-between the read of A and

6、B, the displayed sum would be wrong.A locking protocol is a set of rules followed by all transactions while requesting and releasing locks. Locking protocols restrict the set of possible schedules.Pitfalls of Lock-Based ProtocolsConsider the partial scheduleNeither T3 nor T4 can make progress.Such a

7、 situation is called a deadlock. To handle a deadlock one of T3 or T4 must be rolled back and its locks released.Pitfalls of Lock-Based Protocols (Cont.)The potential for deadlock exists in most locking protocols. Deadlocks are a necessary evil.Starvation is also possible if concurrency control mana

8、ger is badly designed. For example:T1 L-S(Q); T2 request L-X(Q); T3 request L-S(Q); T4 request L-S(Q); T5 request L-S(Q); T6 request L-S(Q);. if T3 T4 T5 T6 are granted an S-lock, then T2 is starved.The same transaction is repeatedly rolled back due to deadlocks.Concurrency control manager can be de

9、signed to prevent starvation.The Two-Phase Locking ProtocolThis is a protocol which ensures conflict-serializable 冲突可串行 schedules.Phase 1: Growing Phase 增长阶段transaction may obtain locks transaction may not release locksPhase 2: Shrinking Phase 收缩阶段transaction may release lockstransaction may not obt

10、ain locksThe protocol assures serializability. It can be proved that the transactions can be serialized in the order of their lock points 锁点 (i.e. the point where a transaction acquired its final lock). The Two-Phase Locking Protocol (Cont.)Two-phase locking does not ensure freedom from deadlocksCas

11、cading roll-back is possible under two-phase locking. To avoid this, follow a modified protocol called strict 严格 two-phase locking. Here a transaction must hold all its exclusive locks till it commits/aborts.Rigorous 强 two-phase locking is even stricter: here all locks are held till commit/abort. In

12、 this protocol transactions can be serialized in the order in which they commit.The Two-Phase Locking Protocol (Cont.)There can be conflict serializable schedules that cannot be obtained if two-phase locking is used. However, in the absence of extra information (e.g., ordering of access to data), tw

13、o-phase locking is necessary for conflict serializability in the following sense: Given a transaction T1 that does not follow two-phase locking, we can find 1. a transaction T2 that uses two-phase locking, 2. a schedule for T1 and T2 that is not conflict serializable.Lock Conversions 加锁转换Two-phase l

14、ocking with lock conversions: Growing Phases allowable actions: acquire a lock-S on itemacquire a lock-X on itemconvert a lock-S to a lock-X (upgrade) Shrinking Phases allowable actions: release a lock-Srelease a lock-Xconvert a lock-X to a lock-S (downgrade)This protocol assures serializability. Bu

15、t still relies on the programmer to insert the various locking instructions.Automatic Acquisition of LocksA transaction T1 issues the standard read/write instruction, without explicit locking calls.The operation read(D) is processed as: 读取之前, 自动加锁 if T1 has a lock on D then read(D) else begin if nec

16、essary wait until no other transaction has a lock-X on D grant T1 a lock-S on D; read(D) endAutomatic Acquisition of Locks (Cont.)write(D) is processed as: if T1 has a lock-X on D then write(D) else begin if necessary wait until no other trans. has any lock on D, if T1 has a lock-S on D then upgrade

17、 lock on D to lock-X else grant T1 a lock-X on D write(D) end;All locks are released after commit or abortImplementation of Locking 不要求A lock manager can be implemented as a separate process to which transactions send lock and unlock requestsThe lock manager replies to a lock request by sending a lo

18、ck grant messages (or a message asking the transaction to roll back, in case of a deadlock)The requesting transaction waits until its request is answeredThe lock manager maintains a data-structure called a lock table to record granted locks and pending requestsThe lock table is usually implemented a

19、s an in-memory hash table indexed on the name of the data item being lockedLock Table 不要求Black rectangles indicate granted locks, white ones indicate waiting requestsLock table also records the type of lock granted or requestedNew request is added to the end of the queue of requests for the data ite

20、m, and granted if it is compatible with all earlier locksUnlock requests result in the request being deleted, and later requests are checked to see if they can now be grantedIf transaction aborts, all waiting or granted requests of the transaction are deleted lock manager may keep a list of locks he

21、ld by each transaction, to implement this efficientlyGraph-Based ProtocolsGraph-based protocols are an alternative to two-phase lockingImpose a partial ordering on the set D = d1, d2 ,., dh of all data items.If di dj then any transaction accessing both di and dj must access di before accessing dj.Im

22、plies that the set D is a directed acyclic graph, called a database graph.The tree-protocol is a simple kind of graph protocol. Tree ProtocolOnly exclusive locks are allowed.The first lock by T1 may be on any data item. Subsequently, a data Q can be locked by T1 only if the parent of Q is currently

23、locked by T1 .Data items may be unlocked at any time. If T1 unlock a data item Q, then it can not relock Q .Graph-Based Protocols (Cont.)The tree protocol ensures conflict serializability as well as freedom from deadlock.Unlocking may occur earlier in the tree-locking protocol than in the two-phase

24、locking protocol.shorter waiting times, and increase in concurrencyprotocol is deadlock-free, no rollbacks are requiredDrawbacksProtocol does not guarantee recoverability or cascade freedomNeed to introduce commit dependencies to ensure recoverability Transactions may have to lock data items that th

25、ey do not access. to lock son, we have to lock its parent first, which is useless for us.increased locking overhead, and additional waiting timepotential decrease in concurrency Timestamp-Based ProtocolsEach transaction is issued a timestamp when it enters the system. If an old transaction T1 has ti

26、me-stamp TS( T1 ), a new transaction T2 is assigned time-stamp TS( T2 ) such that TS( T1 ) 对象的时间戳If TS( T1 ) W-timestamp(Q), the read operation is rejected, and T1 is rolled back. why ? T1 tries to read a value of Q that was already overwritten 改写. 例子: TS( T1 )=9点; W-timestamp(Q)=10点; 并发管理器处理read(Q)

27、请求的时间是11点. 显然, 11点时刻的Q值, 不是9点时刻的Q值. 如果授权T读取Q, 那么效果与按照时间串行的效果不一样. 2. If TS( T1 ) W-timestamp(Q), then the read operation is executed, and R-timestamp(Q) is set to the maximum of R-timestamp(Q) and TS( T1 ). Timestamp-Based Protocols (Cont.)Suppose that transaction T1 issues write(Q). 基本规则: 自己比对象Q年轻;

28、即 事务的时间戳对象的时间戳1. If TS( T1 ) R-timestamp(Q), then the write operation is rejected, and T1 is rolled back. Why ? 分析方法同上页.If TS( T1 ) W-timestamp(Q), then this write operation is rejected, and T1 is rolled back. Why ? T1 is attempting to write an obsolete value of Q. 3. Otherwise, the write operation

29、is executed, and W-timestamp(Q) is set to TS( T1 ).Example Use of the ProtocolA partial schedule for several data items for transactions withtimestamps 1, 2, 3, 4, 5T1T2T3T4T5read(Y)read(X) read(Y)write(Y) write(Z) read(Z) read(X) read(X) write(Z) abort write(Y) write(Z) Correctness of Timestamp-Ord

30、ering ProtocolThe timestamp-ordering protocol guarantees serializability since all the arcs in the precedence graph are of the form: Thus, there will be no cycles in the precedence graph Ti mestamp protocol 1. ensures freedom from deadlock. 任何事务T, 一旦不能分配资源, 就拒绝+回滚, 而不是等待.2. may not be cascade-free,

31、3. may not even be recoverable.transactionwith smallertimestamptransactionwith largertimestamp Recoverability and Cascade FreedomProblem with timestamp-ordering protocol:Suppose T1 aborts, but T2 has read a data item written by T1 Then T2 must abort; if T2 had been allowed to commit earlier, the sch

32、edule is not recoverable.Further, any transaction that has read a data item written by T2 must abortThis can lead to cascading rollback - that is, a chain of rollbacks Solution 1 achieve recoverability and cascade freedom :A transaction is structured such that its writes are all performed at the end

33、 of its processingAll writes of a transaction form an atomic action; no transaction may execute while a transaction is being writtenA transaction that aborts is restarted with a new timestampSolution 2 achieve recoverability and cascade freedom : Limited form of locking: wait for data to be committe

34、d before reading itSolution 3: Use commit dependencies to ensure recoverabilityThomas Write RuleModified version of the timestamp-ordering protocol in which obsolete write operations may be ignored under certain circumstances.Suppose that T1 issues write(Q): 1. If TS( T1 ) R-timestamp(Q), do the sam

35、e things as before. 即拒绝 2. If TS( T1 ) W-timestamp(Q), ignore write(Q). 3. Otherwise, do the same things as beforeThomas Write Rule allows greater potential concurrency. Allows some view-serializable schedules that are not conflict-serializable.Validation-Based ProtocolExecution of transaction T1 is

36、 done in three phases. 1. Read and execution phase: Transaction T1 writes only to temporary local variables 2. Validation phase: Transaction T1 performs a validation test to determine if local variables can be written without violating serializability. 3. Write phase: If T1 is validated, the updates

37、 are applied to the database; otherwise, T1 is rolled back.The three phases of concurrently executing transactions can be interleaved, but each transaction must go through the three phases in that order.Assume for simplicity that the validation and write phase occur together, atomically and serially

38、I.e., only one transaction executes validation/write at a time. Also called as optimistic concurrency control since transaction executes fully in the hope that all will go well during validationValidation-Based Protocol (Cont.)Each transaction T1 has 3 timestampsStart( T1 ) : the time when T1 starte

39、d its executionValidation( T1 ): the time when T1 entered its validation phaseFinish( T1 ) : the time when T1 finished its write phaseSerializability order is determined by timestamp given at validation time, to increase concurrency. Thus TS( T1 ) =Validation( T1 ).This protocol is useful and gives

40、greater degree of concurrency if probability of conflicts is low. because the serializability order is not pre-decided 验证时间戳, andrelatively few transactions will have to be rolled back.Validation Test for Transaction T2 If for all T1 with TS ( T1 ) TS ( T2 ) either one of the following condition hol

41、ds:finish( T1 ) start( T2 ) start( T2 ) finish( T1 ) validation( T2 ) and the set of data items written by T1 does not intersect with the set of data items read by T2 . then validation succeeds and T2 can be committed. Otherwise, validation fails and T2 is aborted. 简单记忆: finish( T1 ) validation( T2

42、) Justification: Either the first condition is satisfied, and there is no overlapped execution, or the second condition is satisfied andthe writes of T2 do not affect reads of T1 since they occur after T1 has finished its reads.the writes of T1 do not affect reads of T2 since T2 does not read any it

43、em written by T1 .Schedule Produced by ValidationExample of schedule produced using validationT14T15read(B)read(B)B:= B-50read(A)A:= A+50read(A)(validate)display (A+B)(validate)write (B)write (A)Multiple GranularityAllow data items to be of various sizes and define a hierarchy of data granularities

44、数据粒度阶梯化, where the small granularities are nested within larger onesCan be represented graphically as a tree (but dont confuse with tree-locking protocol)When a transaction locks a node in the tree explicitly, it implicitly locks all the nodes descendents in the same mode.Granularity of locking (lev

45、el in tree where locking is done):fine granularity 细粒度 (lower in tree, 发生在树的底层): high concurrency, high locking overheadcoarse granularity (higher in tree): low locking overhead, low concurrencyExample of Granularity Hierarchy The levels, starting from the coarsest (top) level aredatabase 例如整个数据库系统a

46、rea 例如某个特定的数据库file 例如某张表record Intention Lock ModesIn addition to S and X lock modes, there are three additional lock modes with multiple granularity:intention-shared (IS) 意向共享: indicates explicit locking at a lower level of the tree but only with shared ention-exclusive (IX)意向独占: indicates

47、 explicit locking at a lower level with exclusive or shared locksshared and intention-exclusive (SIX)共享和意向独占: the subtree rooted by that node is locked explicitly in shared mode and explicit locking is being done at a lower level with exclusive-mode locks.意向本质就是一个标记, 不是真的锁intention locks allow a hig

48、her level node to be locked in S or X mode without having to check all descendent nodes.Compatibility Matrix with Intention Lock ModesThe compatibility matrix for all lock modes is: ISIXSS IXX ISIXSS IXX Multiple Granularity Locking Schemelocks are acquired in root-to-leaf order, whereas they are re

49、leased in leaf-to-root order.Transaction T1 can lock a node Q, using the following rules:The lock compatibility matrix must be observed.The root of the tree must be locked first, and may be locked in any mode. A node Q can be locked by T1 in S or IS mode only if the parent of Q is currently locked b

50、y T1 in either IX or IS mode. 孩子的锁只能往宽松方向走A node Q can be locked by T1 in X, SIX, or IX mode only if the parent of Q is currently locked by T1 in either IX or SIX mode. T1 can lock a node only if it has not previously unlocked any node (that is, T1 is two-phase). T1 can unlock a node Q only if none

51、of the children of Q are currently locked by T1 .Multiversion Schemes大致了解Multiversion schemes keep old versions of data item to increase concurrency.Multiversion Timestamp OrderingMultiversion Two-Phase LockingEach successful write results in the creation of a new version of the data item written.Us

52、e timestamps to label versions.When a read(Q) operation is issued, select an appropriate version of Q based on the timestamp of the transaction, and return the value of the selected version. reads never have to wait, as an appropriate version is returned immediately.Multiversion Timestamp OrderingEa

53、ch data item Q has a sequence of versions . Each version Qk contains three data fields:Content - the value of version Qk.W-timestamp(Qk) - timestamp of the transaction that created (wrote) version QkR-timestamp(Qk) - largest timestamp of a transaction that successfully read version Qkwhen a transact

54、ion T1 creates a new version Qk of Q, Qks W-timestamp and R-timestamp are initialized to TS( T1 ). R-timestamp of Qk is updated whenever a transaction T2 reads Qk, and TS( T2 ) R-timestamp(Qk).时间值越大, 说明出现的时间越晚Multiversion Timestamp Ordering (Cont)Suppose that transaction T1 issues a read(Q) or write

55、(Q) operation. Let Qk denote the version of Q whose write timestamp is the largest write timestamp less than or equal to TS( T1 ). T-W(Qk )=TS( T1 )If transaction T1 issues a read(Q), then the value returned is the content of version Qk.If transaction T1 issues a write(Q)if TS( T1 ) R-timestamp(Qk),

56、 then transaction T1 is rolled back. if TS( T1 ) = W-timestamp(Qk), the contents of Qk are overwrittenelse a new version of Q is created. T1 写入新值.Observe thatReads always succeedA write by T1 is rejected if some other transaction has already read a version created by a transaction older than T1 .Pro

57、tocol guarantees serializabilityMultiversion Two-Phase LockingDifferentiates between read-only transactions and update transactionsUpdate transactions acquire read and write locks, and hold all locks up to the end of the transaction. That is, update transactions follow rigorous two-phase locking.Eac

58、h successful write results in the creation of a new version of the data item written.each version of a data item has a single timestamp whose value is obtained from a counter ts-counter that is incremented during commit processing.Read-only transactions are assigned a timestamp by reading the curren

59、t value of ts-counter before they start execution; they follow the multiversion timestamp-ordering protocol for performing reads.Multiversion Two-Phase Locking (Cont.)When an update transaction wants to read a data item:it obtains a shared lock on it, and reads the latest version. When it wants to w

60、rite an itemit obtains X lock on; it then creates a new version of the item and sets this versions timestamp to .When update transaction T1 completes, commit processing occurs: T1 sets timestamp on the versions it has created to ts-counter + 1 T1 increments ts-counter by 1Read-only transactions that

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论