Distributed Systems 13 LECTURE 13 TRANSACTIONS AND CONCURRENCY
分布式系统 Distributed Systems 第 13 讲 事务和并发控制 LECTURE 13 TRANSACTIONS AND CONCURRENCY CONTROL 王晓阳、张 奇 复旦大学 计算机科学技术学院 1
13. 2 事务 事务T: 事务U: balance = b. get. Balance(); b. set. Balance(balance*1. 1); a. withdraw(balance/10) balance = b. get. Balance(); b. set. Balance(balance*1. 1); c. withdraw(balance/10) balance = b. get. Balance(); b. set. Balance(balance*1. 1); a. withdraw(balance/10) $200 balance = b. get. Balance(); $200 b. set. Balance(balance*1. 1); $220 c. withdraw(balance/10) $280 $220 $80 13
13. 2 事务 事务V: 事务W: a. withdraw(100) a. Branch. branch. Total() b. deposit(100) a. withdraw(100); $100 total = a. get. Balance() $100 total = total+b. get. Balance() $300 total = total+c. get. Balance() b. deposit(100) $300 15
13. 2 事务 事务T: balance = b. get. Balance() b. set. Balance(balance*1. 1) a. withdraw(balance/10) 事务U: balance = b. get. Balance() b. set. Balance(balance*1. 1) c. withdraw(balance/10) balance = b. get. Balance() $200 b. set. Balance(balance*1. 1) $220 a. withdraw(balance/10) balance = b. get. Balance() $220 b. set. Balance(balance*1. 1) $242 c. withdraw(balance/10) $278 $80 17
13. 2 事务 事务V: a. withdraw(100); b. deposit(100) 事务W: a. Branch. branch. Total() a. withdraw(100); $100 b. deposit(100) $300 total = a. get. Balance() $100 total = total+b. get. Balance() $400 total = total+c. get. Balance(). . . 18
13. 2 事务 事务T: 事务U: x = read(i) write(i, 10) y = read(j) write(j, 30) write(j, 20) z = read (i) 21
13. 2 事务 事务放弃时的恢复(一) ◦ 脏数据读取 某个事务读取了另一个未提交事务写入的数据 事务T: a. get. Balance() a. set. Balance(balance + 10) balance = a. get. Balance() a. set. Balance(balance + 10) 事务U: a. get. Balance() a. set. Balance(balance + 20) $100 $110 balance = a. get. Balance() $110 a. set. Balance(balance + 20) $130 commit transaction abort transaction 事务T放弃时的脏数据读取 23
13. 3 嵌套事务 T : 顶层事务 T 1 = open. Sub. Transaction T 1 : T 2 = open. Sub. Transaction commit T 2 : open. Sub. Transaction prov. commit T 11: T 12: abort T 21: open. Sub. Transaction prov. commit T 211: prov. commit 28
13. 4 锁 事务T balance = b. get. Balance() b. set. Balance(bal*1. 1) a. withdraw(bal/10) 操作 锁 事务U balance = b. get. Balance() b. set. Balance(bal*1. 1) c. withdraw(bal/10) 操作 锁 open. Transaction bal = b. get. Balance() 锁住B b. set. Balance(bal*1. 1) a. withdraw(bal/10) 锁住A open. Transaction close. Transaction 对A, B解锁 bal = b. get. Balance() 等待事务T在B上的锁 锁住B … b. set. Balance(bal*1. 1) 锁住C c. withdraw(bal/10) close. Transaction 对B, C解锁 31
13. 4 锁 对某一对象 已设置的锁 被请求的锁 read write none OK OK read OK wait write 等待 等待 34
13. 4 锁 1. When an operation accesses an object within a transaction: (a) If the object is not already locked, it is locked and the operation proceeds. (b) If the object has a conflicting lock set by another transaction, the transaction must wait until it is unlocked. (c) If the object has a non-conflicting lock set by another transaction, the lock is shared and the operation proceeds. (d) If the object has already been locked in the same transaction, the lock will be promoted if necessary and the operation proceeds. (Where promotion is prevented by a conflicting lock, rule (b) is used. ) 2. When a transaction is committed or aborted, the server unlocks all objects it locked for the transaction. Figure 16. 16 Use of locks in strict two-phase locking 35
13. 4 锁 public class Lock { private Object object; // the object being protected by the lock private Vector holders; // the TIDs of current holders private Lock. Type lock. Type; // the current type public synchronized void acquire(Trans. ID trans, Lock. Type a. Lock. Type ){ while(/*another transaction holds the lock in conflicing mode*/) { try { wait(); }catch ( Interrupted. Exception e){/*. . . */ } } if(holders. is. Empty()) { // no TIDs hold lock holders. add. Element(trans); lock. Type = a. Lock. Type; } else if(/*another transaction holds the lock, share it*/ ) ){ if(/* this transaction not a holder*/) holders. add. Element(trans); } else if (/* this transaction is a holder but needs a more exclusive lock*/) lock. Type. promote(); } } 37
13. 4 锁 public synchronized void release(Trans. ID trans ){ holders. remove. Element(trans); // remove this holder // set locktype to none notify. All(); } } 38
13. 4 锁 public class Lock. Manager { private Hashtable the. Locks; public void set. Lock(Object object, Trans. ID trans, Lock. Type lock. Type){ Lock found. Lock; synchronized(this){ // find the lock associated with object // if there isn’t one, create it and add to the hashtable } found. Lock. acquire(trans, lock. Type); } } // synchronize this one because we want to remove all entries public synchronized void un. Lock(Trans. ID trans) { Enumeration e = the. Locks. elements(); while(e. has. More. Elements()){ Lock a. Lock = (Lock)(e. next. Element()); if(/* trans is a holder of this lock*/ ) a. Lock. release(trans); } } 40
13. 4 锁 read 要设置的锁 write I-read I-write none OK OK read OK 等待 write 等待 等待 I-read OK 等待 OK OK I-write 等待 等待 OK OK 对某个对象 已设置的锁 53
13. 6 时间戳排序 T U 对象的不同版本及其时间戳 A B C RTS WTS {} S open. Transaction bal = b. get. Balance() b. set. Balance(bal*1. 1) RTS WTS {} S {T} open. Transaction bal = b. get. Balance() wait for T a. withdraw(bal/10) commit bal = b. get. Balance() b. set. Balance(bal*1. 1) c. withdraw(bal/10) S, T T T {U} T, U S, U 73
Question? 80
- Slides: 80