concurrency control
play

Concurrency Control Shan-Hung Wu & DataLab CS, NTHU Tx - PowerPoint PPT Presentation

Transaction Management Part I: Concurrency Control Shan-Hung Wu & DataLab CS, NTHU Tx Management VanillaCore JDBC Interface (at Client Side) Remote.JDBC (Client/Server) Server Query Interface Tx Planner Parse Algebra Storage


  1. Transaction Management Part I: Concurrency Control Shan-Hung Wu & DataLab CS, NTHU

  2. Tx Management VanillaCore JDBC Interface (at Client Side) Remote.JDBC (Client/Server) Server Query Interface Tx Planner Parse Algebra Storage Interface Sql/Util Concurrency Recovery Metadata Index Record Log Buffer File 2

  3. VanillaDb. init("studentdb"); // Step 1 Native API Revisited Transaction tx = VanillaDb. txMgr().newTransaction( Connection. TRANSACTION_SERIALIZABLE, true); // Step 2 Planner planner = VanillaDb. newPlanner(); • A tx is created upon accepting String query = "SELECT s-name, d-name FROM departments, " an JDBC connection + "students WHERE major-id = d-id"; Plan plan = planner.createQueryPlan(query, – by tx); VanillaDb.txMgr().newT Scan scan = plan.open(); ransaction() // Step 3 System. out.println("name\tmajor"); • Passed as a parameter to System. out.println("-------\t-------"); Planners/Scanners/RecordFiles while (scan.next()) { String sName = (String) scan.getVal("s- name").asJavaVal(); String dName = (String) scan.getVal("d- name").asJavaVal(); System. out.println(sName + "\t" + dName); } scan.close(); // Step 4 tx.commit(); 3

  4. Transaction Manager in VanillaDB • VanillaDb.txMgr() is responsible for creating new transaction and maintaining the active transaction list TransactionMgr + serialConcurMgrCls : Class<?> + rrConcurMgrCls : Class<?> + rcConcurMgrCls : Class<?> + recoveryMgrCls : Class<?> + TransactionMgr() + onTxCommit(tx : Transaction) + onTxRollback(tx : Transaction) + onTxEndStatement(tx : Transaction) + createCheckpoint(tx : Transaction) + newTransaction(isolationLevel : int, readOnly : boolean) : Tranasction + newTransaction(isolationLevel : int, readOnly : boolean, txNum : long) : Transaction + getNextTxNum() : long 4

  5. Transactions • Ensures ACID Transaction • Concurrency manager for C + Transaction(concurMgr : ConcurrencyMgr, and I recoveryMgr : RecoveryMgr, bufferMgr : BufferMgr readOnly : boolean, txNum : long) • Recovery + addLifeCycleListener(l : TransactionLifeCycleListener) + commit() + rollback() manager for A + endStatement() + getTransactionNumber() : long + isReadOnly() : boolean and D + concurrencyMgr() : ConcurrencyMgr + recoveryMgr() : RecoveryMgr + bufferMgr() : BufferMgr 5

  6. Transaction Lifecycle 1. Begin Transaction 2. End statement – If spanning + Transaction(concurMgr : ConcurrencyMgr, across multiple recoveryMgr : RecoveryMgr, bufferMgr : BufferMgr readOnly : boolean, txNum : long) statements + addLifeCycleListener(l : TransactionLifeCycleListener) + commit() + rollback() 3. Commit or + endStatement() + getTransactionNumber() : long rollback + isReadOnly() : boolean + concurrencyMgr() : ConcurrencyMgr + recoveryMgr() : RecoveryMgr + bufferMgr() : BufferMgr 6

  7. Lifecycle Listeners • Tx lifecycle listener – Takes actions to tx life cycle events <<interface>> TransactionLifecycleListener + onTxCommit(tx : Transaction) + onTxRollback(tx : Transaction) + onTxEndStatement(tx : Transaction) <<abstract>> RecoveryMgr BufferMgr ConcurrencyMgr 7

  8. Lifecycle Listener: Buffer Mgr • Buffer manager – On tx rollback/commit: unpins all pages pinned by the current tx – Registered itself as a life cycle listener on start of each tx @Override public void onTxCommit(Transaction tx) { unpinAll(tx); } @Override public void onTxRollback(Transaction tx) { unpinAll(tx); } @Override public void onTxEndStatement(Transaction tx) { // do nothing } 8

  9. Lifecycle Listener: Recovery Mgr • (Naive) Recovery manager – Commit: flushes dirty pages and then commit log – Rollback: undo all modifications by reading log records @Override public void onTxCommit(Transaction tx) { VanillaDb. bufferMgr().flushAll(txNum); long lsn = new CommitRecord(txNum).writeToLog(); VanillaDb. logMgr().flush(lsn); } @Override public void onTxRollback(Transaction tx) { doRollback(tx); VanillaDb. bufferMgr().flushAll(txNum); long lsn = new RollbackRecord(txNum).writeToLog(); VanillaDb. logMgr().flush(lsn); } @Override public void onTxEndStatement(Transaction tx) { // do nothing } 9

  10. Lifecycle Listener: Concurrency Mgr • (Naive) Concurrency manager – On tx commit/rollback: releases all locks @Override public void onTxCommit(Transaction tx) { lockTbl.releaseAll(txNum, false); } @Override public void onTxRollback(Transaction tx) { lockTbl.releaseAll(txNum, false); } @Override public void onTxEndStatement(Transaction tx) { // do nothing } 10

  11. Today’s Focus: Concurrency Mgr VanillaCore JDBC Interface (at Client Side) Remote.JDBC (Client/Server) Server Query Interface Tx Planner Parse Algebra Storage Interface Sql/Util Concurrency Recovery Metadata Index Record Log Buffer File 11

  12. Outline • Schedules • Anomalies • Lock-based concurrency control – 2PL and S2PL – Deadlock – Granularity of locks • Dynamic databases – Phantom – Isolation levels • Meta-structures • Concurrency manager in VanillaCore 12

  13. Outline • Schedules • Anomalies • Lock-based concurrency control – 2PL and S2PL – Deadlock – Granularity of locks • Dynamic databases – Phantom – Isolation levels • Meta-structures • Concurrency manager in VanillaCore 13

  14. Concurrency Manager • Ensures consistency and isolation 14

  15. Consistency • Consistency – Txs will leave the database in a consistent state – I.e., all integrity constraints (ICs) are meet • Primary and foreign key constrains • Non-null constrain • (Field) type constrain • … – Users are responsible for issuing “valid” txs 15

  16. Isolation • Isolation – Interleaved execution of txs should have the net effect identical to executing tx in some serial order – 𝑈 1 and 𝑈 2 are executed concurrently, isolation gives that the net effect to be equivalent to either • 𝑈 1 followed by 𝑈 2 or • 𝑈 2 followed by 𝑈 1 – The DBMS does not guarantee to result in which particular order 16

  17. Why do we need to interleave txs? 17

  18. Concurrent Txs • Since I/O is slow, it is better to execute Tx1 and Tx2 concurrently to reduce CPU idle time Tx1 Tx2 Tx1 Tx2 Tx1 Tx2 Tx1 Tx2 R(A) R(A) R(A) R(A) CPU CPU R(A) CPU R(A) CPU W(A) W(A) CPU CPU R(A) idle R(A) R(A) R(A) = CPU CPU CPU W(A) CPU W(B) R(A) W(B) W(B) R(A) CPU CPU W(B) W(A) • The concurrent result should be the same as serial execution in some order – Better concurrency 18

  19. Concurrent Txs • Pros: – Increases throughput (via CPU and I/O pipelining) – Shortens response time for short txs • But operations must be interleaved correctly 19

  20. Transactions and Schedules • Before executing 𝑈 1 and 𝑈 2 : – A = 300, B = 400 • Two possible execution results – 𝑈 1 followed by 𝑈 2 • A = 400, B = 300  A = 424, B = 318 – 𝑈 2 followed by 𝑈 1 • A = 318, B = 424  A = 418, B = 324 20

  21. Transactions and Schedules • A schedule is a list of actions/operations from a set of transaction • If the actions of different transactions are not interleaved, we call this schedule a serial schedule 21

  22. Transactions and Schedules • Equivalent schedules – The effect of executing the first schedule is identical to the effect of executing the second schedule • Serializable schedule – A schedule that is equivalent to some serial execution of the transactions 22

  23. Transactions and Schedules • A possible interleaving schedule – Result: A = 424, B = 318 – A serializable schedule • Equivalent to 𝑈 1 followed by 𝑈 2 23

  24. Transactions and Schedules • How about this schedule? – Result: A = 424 , B = 324 – A non-serializable schedule – Violates the isolation requirement 24

  25. Goal • Interleave operations while making sure the schedules are serializable • How? 25

  26. Outline • Schedules • Anomalies • Lock-based concurrency control – 2PL and S2PL – Deadlock – Granularity of locks • Dynamic databases – Phantom – Isolation levels • Meta-structures • Concurrency manager in VanillaCore 26

  27. Simplified Notation • Can be simplified to: • Here, we care about operations, not values 27

  28. Anomalies • Weird situations that would happen when interleaving operations – But not in serial schedules • Mainly due to the conflicting operations 28

  29. Conflict Operations • Two operations on the same object are conflict if they are operated by different txs and at least one of these operations is a write 29

  30. Types • Write-read conflict • Read-write conflict • Write-write conflict • Read-read conflict? – No anomaly 30

  31. Anomalies due to Write-Read Conflict • Reading uncommitted data – Dirty reads • A unrecoverable schedule – T1 cannot abort! – Cascading aborts if T2 completes after T1 aborts 31

  32. Anomalies due to Read-Write Conflict • Unrepeatable reads : – 𝑈 1 : 𝑗𝑔 (𝐵 > 0) 𝐵 = 𝐵 − 1 ; – 𝑈 2 : 𝑗𝑔 (𝐵 > 0) 𝐵 = 𝐵 − 1 ; – IC on 𝐵 : cannot be negative A=0 , A=-1, C T1 A=1 A=1, A=0, C T2 32

  33. Anomalies due to Write-Write Conflict • Lost updates : – 𝑈 1 : 𝐵 = 𝐵 + 1 ; 𝐶 = 𝐶 ∗ 10 ; – 𝑈 2 : 𝐵 = 𝐵 + 2 ; 𝐶 = 𝐶 ∗ 5 ; – Start with A=10, B=10 T1 A=11 B=500, C A=13, B=50, C T2 33

Recommend


More recommend