76
Transaction 1 Principles of Computer System (2012 Fall) Before-or-after

Transaction

  • Upload
    phyre

  • View
    24

  • Download
    0

Embed Size (px)

DESCRIPTION

Transaction. Before-or-after. Principles of Computer System (2012 Fall). Where are we?. System Complexity Modularity & Naming Enforced Modularity Network Fault Tolerance Transaction All-or-nothing Before-or-after. Review. Atomicity All-or-nothing: to tolerant crash By shadow-copy - PowerPoint PPT Presentation

Citation preview

Page 1: Transaction

1

Transaction

Principles of Computer System (2012 Fall)

Before-or-after

Page 2: Transaction

2

Where are we?

• System Complexity• Modularity & Naming• Enforced Modularity• Network• Fault Tolerance• Transaction– All-or-nothing– Before-or-after

Page 3: Transaction

3

Review

• Atomicity– All-or-nothing: to tolerant crash• By shadow-copy• By log

– Before-or-after: to get more concurrency

Page 4: Transaction

Logging Protocol

• CHANGE Record– The identity of the all-or-nothing action– A component action redo• installs the intended value in cell storage

– After commit, if the system crashes , the recovery procedure can perform the install on behalf of the action

– A second component action undo• reverses the effect on cell storage of the install

– After aborts or the system crashes, it may be necessary for the recovery procedure to reverse the effect

4

Page 5: Transaction

Logging Protocol• The application• NEW_ACTION– log a BEGIN record that contains just the new identity– As the all-or-nothing action proceeds through its pre-commit

phase, it logs CHANGE records• To implement COMMIT or ABORT– logs an OUTCOME record– commit point

5

Page 6: Transaction

Logging Protocol1. procedure TRANSFER (debit_account, credit_account,

amount)2. my_id ← LOG (BEGIN_TRANSACTION)3. dbvalue.old ← GET (debit_account)4. dbvalue.new ← dbvalue.old - amount5. crvalue.old ← GET (credit_account, my_id)6. crvalue.new ← crvalue.old + amount7. LOG (CHANGE, my_id,8. “PUT (debit_account, dbvalue.new)”, //redo

action9. “PUT (debit_account, dbvalue.old)”) //undo

action

6

Page 7: Transaction

Logging Protocol10. LOG ( CHANGE, my_id,11. “PUT (credit_account, crvalue.new)”

//redo action

12. “PUT (credit_account, crvalue.old)”)//undo action

13. PUT (debit_account, dbvalue.new) // install14. PUT (credit_account, crvalue.new) // install15. if dbvalue.new > 0 then16. LOG ( OUTCOME, COMMIT, my_id)17. else18. LOG (OUTCOME, ABORT, my_id)19. signal(“Action not allowed. Would make debit

account negative.”)20. LOG (END_TRANSACTION, my_id)

7

Page 8: Transaction

Logging Protocol

8

Page 9: Transaction

Logging Protocol1. procedure ABORT (action_id)2. starting at end of log repeat until beginning3. log_record ← previous record of log4. if log_record.id = action_id then5. if (log_record.type = OUTCOME)6. then signal (“Can’t abort an already

completed action.”)7. if (log_record.type = CHANGE)8. then perform undo_action of log_record9. if (log_record.type = BEGIN)10. then break repeat11. LOG (action_id, OUTCOME, ABORTED) // Block future

undos.12. LOG (action_id, END)

9

Page 10: Transaction

Logging Protocol: non-volatile cell memory

10

Page 11: Transaction

Logging Protocol: non-volatile cell memory

11

Page 12: Transaction

Summary• Logging is a general technique for achieving all-or-

nothing atomicity.– Widely used: databases, file systems, ..

• Can achieve reasonable performance with logging.– Writes are always fast: sequential.– Reads can be fast with cell storage.– Key idea 1: write-ahead logging, makes it safe to update

cell storage.– Key idea 2: recovery protocol, undo losers / redo

winners.

Page 13: Transaction

Before-or-after atomicity

• All-or-nothing + before-or-after => transaction– All-or-nothing: crash– Before-or-after: concurrency– In the midst of multiple-step atomic action

• Definition of “correctness” (as in 9.1.5)– If every result is guaranteed to be one that could

have been obtained by some purely serial application of those same actions

13

Page 14: Transaction

Simple serialization

• Simple serialization: similar as lock-step– Transaction n waits transaction n-1 to complete

• Drawbacks: too strict– Prevents all concurrency among transactions– Suitable for applications without many transactions

• Next of this chapter are nothing but optimizations

14

Page 15: Transaction

15

Page 16: Transaction

16

• More relaxed disciplines that still guarantee correctness• We don’t care when things happen

– Transaction-3 can create new version of C before transaction-2– Transaction-4 can run concurrently with transaction-3

Page 17: Transaction

Mark-point discipline• Step-1: wait for pending version• Step-2: mark data to be updated– Create a pending versions of every variable it intends to

modify --- mark point– Announce when it is finished doing so

• By MARK_POINT_ANNOUNCE, simple set a flag

• Step-3: keep discipline of mark point:– No transaction can begin reading its inputs until the

preceding transaction has reached its mark point or is no longer pending

17

Page 18: Transaction

18

Waiting, instead of skipping

Page 19: Transaction

Mark-point versions of WRITE_NEW_VERSION()

19

Page 20: Transaction

20

Page 21: Transaction

21

Page 22: Transaction

Mark-point discipline• Distribute delays– Some in BEGIN_TRANSACTION()– Some in READ_CURRENT_VALUE()

• Bootstrapping– Goal: before-or-after for general programs– Solution: special case of a “new outcome record”

• Three layers

22

TICKET, REQUIRE, RELEASE

NEW_OUTCOME_RECORD

MARK_POINT -> before-or-after

Page 23: Transaction

Mark-point: no deadlock

• Wait only earlier transaction– Earliest ones wait no one– Guarantee progress– Lock will not guarantee progress

• Require additional mechanisms to ensure no deadlock

• Two minor points– Reduce to simple serialization discipline

• If wait to announce mark point until commit or abort– Two possible errors

• Never call NEW_VERSION after mark point• Never try to write a value without new version

23

Page 24: Transaction

Read-capture: optimistic atomicity

• Pessimistic methods– Presume that interference is likely– Prevent any possibility of interference actively– Simple serialization & mark-point

• Optimistic methods– Allow write in any order and at any time– With the risk that “sorry, interfere write, you must

abort, clear the history and then retry”– Read-capture discipline

24

Page 25: Transaction

25

Transaction-4 was lateTransaction-6 has already read ATransaction-4 has to redo as 7

Page 26: Transaction

26

Page 27: Transaction

Read-capture’s correctness• Correctness

– 1. WAIT for PENDING in READ ensures that transaction n will wait for k to commit or abort (k<n)

– 2. High-water mark in READ and test in NEW_VERSION ensures transaction j will abort if n has read the object (j < n)

– 3. Therefore, every value that READ returns to transaction n will include effect of 1…n-1

– 4. Therefore, every transaction n will act as if it serially follows transaction n-1

• Price of optimism– Later transaction may cause earlier ones to abort– Suitable for those application without a lot of data interference

27

Page 28: Transaction

Use version histories for before-or-after atomicity

• Register renaming– Pentium-4 only has 8 architectural register– Has 128 physical register– Renaming by reorder buffer• Assigning a slot in the reorder buffer

– NEW_OUTCOME_RECORD & NEW_VERSION• Committing instruction

– WRITE_VALUE & COMMIT

28

Page 29: Transaction

Register renaming

29

Page 30: Transaction

Use version histories for before-or-after atomicity

• Oracle database’s serializable– Snapshot isolation– When a transaction begins• System takes a snapshot of every committed value• Read all of its inputs from that snapshot

– If two concurrent transaction modify the same variable• The first one to commit wins• Aborts the other one with “serialization error”

30

Page 31: Transaction

Use version histories for before-or-after atomicity

• Transaction memory– Allow concurrent threads without locks• Mark the beginning of an atomic instruction sequence

with a “begin transaction” instruction• Direct all STORE to a hidden copy• Check interference at end

– Even more optimistic than read-capture– Most useful if interference is possible but unlikely– Hardware or software implementation

31

Page 32: Transaction

Pragmatics: lock• Lock: a flag associated with a data object to warn

concurrent actions not to read or write the object– ACQUIRE (A.lock) / RELEASE (A.lock)

• Only one will succeed– Problems

• Easy to make error to race• Difficult to find out why

– Three steps• Discipline specifies which locks must be acquired and when• Establish a compelling line of reasoning that concurrent

transactions that follow the discipline will ensure before-or-after• Interpose a lock manager to enforces the discipline

32

Page 33: Transaction

System-wide locking• System-wide lock– begin_transaction

ACQUIRE (System.lock)…

– …RELEASE (System.lock)end_transaction

• Allow only one transaction to run at a time• Serialize potentially concurrent transactions in the

order that they call ACQUIRE

33

Page 34: Transaction

Simple locking• Simple locking– 1. Acquire a lock for every shared data in advance– 2. Release locks only after commit or abort

• Lock point (similar as mark-point)– Lock set: locks acquired when reaches lock point– Lock manager’s enforcement

• Intercept read/write/commit/abort, and check

• Problems– How to enumerate all shared object to access?– The set of might access may be larger than does access

34

Page 35: Transaction

Two-phase locking• Two-phase locking (similar as read-capture)– Avoids to know lock set in advance– Acquire locks as it proceeds, access data as soon as it

acquires the lock• Constraints– Not release any locks until passes lock point– Only release a lock if never need to read/write again

• Problems– Widely used, but hard to argue correct

35

Page 36: Transaction

Two-phase locking

• Unnecessary blocking– Example• T1: READ X• T2: WRITE Y• T1: WRITE Y• T1’s & T2’s lock sets intersect at Y

– Two-phase locking prevents interleaving• But T1/T2/T1 = T2/T1/T1• NP-complete

36

Page 37: Transaction

Summary

Version History LocksSimple serialization System-wide lockMark-point discipline Simple lockingRead-capture Two-phase locking

37

Page 38: Transaction

Interactions between locks and logs

• Transaction abort– Restore its changed data before release lock– Just like committed transactions doing nothing

• System recovery– Whether the locks themselves should be logged?• No pending after recovery, thus no locks• Locks are in volatile memory

– Non-complete transactions have no overlapping lock sets at the moment of crash

38

Page 39: Transaction

Performance optimizations• Physical locking VS. logical locking– Choose lock granularity

• E.g. change 6-byte object of a 1000-byte disk sector,or change 1500-byte object on two disk sectors

• Which to lock: the object or sector?– Logical locking

• If objects are small: more concurrency, more logging– Physical locking

• New logical layer between app and disk– E.g. data object management and garbage collection

• Tailor the logging and locking design to match disk granularity– Disk sectors rather than object is a common practice

39

Page 40: Transaction

Two-phase locking• Two-phase locking (similar as read-capture)– Avoids to know lock set in advance– Acquire locks as it proceeds, access data as soon as it

acquires the lock• Constraints– Not release any locks until passes lock point– Only release a lock if never need to read/write again

• Problems– Widely used, but hard to argue correct

40

Page 41: Transaction

Two-phase locking

• Unnecessary blocking– Example• T1: READ X• T2: WRITE Y• T1: WRITE Y• T1’s & T2’s lock sets intersect at Y

– Two-phase locking prevents interleaving• But T1/T2/T1 = T2/T1/T1• NP-complete

41

Page 42: Transaction

Interactions between locks and logs

• Transaction abort– Restore its changed data before release lock– Just like committed transactions doing nothing

• System recovery– Whether the locks themselves should be logged?• No pending after recovery, thus no locks• Locks are in volatile memory

– Non-complete transactions have no overlapping lock sets at the moment of crash

42

Page 43: Transaction

Performance optimizations• Physical locking VS. logical locking– Choose lock granularity

• E.g. change 6-byte object of a 1000-byte disk sector,or change 1500-byte object on two disk sectors

• Which to lock: the object or sector?– Logical locking

• If objects are small: more concurrency, more logging– Physical locking

• New logical layer between app and disk– E.g. data object management and garbage collection

• Tailor the logging and locking design to match disk granularity– Disk sectors rather than object is a common practice

43

Page 44: Transaction

Performance optimizations

• Lock compatibility modes– Multiple-reader, single-writer protocol• Any number of readers is safe• Only one writer, wait for all reader to finish• Suitable for applications with a lot of reading• A writer may be delayed indefinitely

– More specific, more complex

44

Page 45: Transaction

Deadlock & making progress

• Inevitable if using locks in concurrency– 1. Waiting for one another– 2. Waiting for a lock by some deadlocked one– Correctness arguments ensures correctness, but

no progress• Methods– Pessimistic ones: take a priori action to prevent– Optimistic ones: detect deadlocks then fix up

45

Page 46: Transaction

Methods for solving deadlock• Lock ordering (pessimistic)– Number the locks uniquely– Require transactions acquire locks in order– Problem: some app may not predict all of the locks they

need before acquiring the first one• Backing out (optimistic)– Allow acquire locks in any order– If it encounters an already-acquired lock with an number

lower than one it has previously acquired itself, then• UNDO: Back up to release its higher-numbered locks• Wait for the lower-numbered lock and REDO

46

Page 47: Transaction

Methods for solving deadlock• Timer expiration (optimistic)– Set a timer at begin_transaction, abort if timeout– If still no progress, another one may abort– Problem: how to chose the interval?

• Cycle detection (optimistic)– Maintain a wait-for-graph in the lock manager

• Shows owner and waiting ones• Check when transaction tries to acquire a lock

– Prevent cycle (deadlock)• Select some cycle member to be a victim

47

Page 48: Transaction

Deadlock & making progress

• Live-lock still possible– Two transactions with the same timeout value

• Exponential random backoff– Delays the thread for a random time– Repeated retries failing indicate deeper problem

• End-to-end argument– Worth the effect?

48

Page 49: Transaction

Two problems: multi-layer & multi-site

• How to commit in multi-layer transaction?– UNDO: require the results of low-layer transaction be

visible only within the higher-layer one– Delay: delay low-layer commitment to when high-layer

commitment commits• How to provide atomicity in multi-site?– Communication delay & reliability, independent failure

• Solution– Nested transaction => two-phase commit– Specialized form of RPC to coordinate steps

49

Page 50: Transaction

Hierarchical composition of transactions

• Three layers all need atomicity– TRANSFER, PAY_INTEREST, MONTH_END_INTEREST

• What if transferring money from A to B between two PAY_INTEREST in one MONTH_END_INTEREST?

50

TRANSFER

PAY_INTEREST

MONTH_END_INTEREST

Page 51: Transaction

How to commit?

• PAY_INTEREST– TRANSFER commits before PAY_INTEREST commits• If crash, PAY_INTEREST cannot go back

– TRANSFER commits after PAY_INTEREST commits• If crash, TRANSFER may have not done and no redo

• MONTH_END_INTEREST– No other TRANSFER should occur– Do multiple TRANSFER itself• Which are before-or-after actions

51

Page 52: Transaction

Hierarchical record• Superior record & dependent record– Top-layer uses a flag to indicate no nest– Sub-layer contains higher-layer TID

• When create a nested transaction– Record both initial state of the new transaction and the ID

of the enclosing transaction• When check commit– The outcome of a transaction depends on its ancestors– Tentatively commit: sitting on a knife-edge

52

Page 53: Transaction

Hierarchical record• Delayed output visibility– A nested transaction waits for highest layer to commit

• E.g. open cash drawer

• Maintain tentatively committed state indefinitely– Maybe commit or abort– What about holding a lock?

• Interaction among transactions– May cause build-in cycle of waits

53

Page 54: Transaction

54

Page 55: Transaction

Two-phase commit

• Phase-1: preparation / voting– Lower-layer transactions either aborts or

tentatively committed– Higher-layer transaction evaluate lower situation

• Phase-2: commitment– If top-layer, then COMMIT or ABORT– If nested itself, then become tentatively

committed

55

Page 56: Transaction

Multiple-site atomicity: distributed two-phase commit

• Multiple-site atomicity– A transaction requires several transactions at different

sites in a best-effort network– Message may be lost, delayed or duplicated– Use RPC to communicate

• Ensure at-least-once by persistent sender• Ensure at-most-once by duplicate suppression• However, neither is enough to ensure atomicity here

– Combine three components• At-lease-once, at-most-once, one-site transaction

56

Page 57: Transaction

Multiple-site atomicity

• Worker: Bob, Charles, Dawn– Does three transactions: X, Y, Z

• Coordinator: Alice – Create a higher-layer transaction– Send three messages to the three workers

• Challenge: un-reliable communication

57

Page 58: Transaction

Commit Phase-1

Commit Phase-2

Bob: tentative committedState: PREPARED

Bob: COMMITTEDBob will perform post-commit

Page 59: Transaction

59

3N messages

Page 60: Transaction

Multiple-site atomicity

• Coordinator– Collect some ABORT or nothing: ABORT or assign

the work to another worker– Collect all COMMIT: then COMMIT

• Worker– When receive nothing: resend PREPARED• Coordinator will send current state if it receives

duplicate message– When receive COMMIT: then COMMIT

60

Page 61: Transaction

Worker crash recovery• If a worker uses logs and locks crashes– 1. Classify any PREPARED transaction as a tentative winner

that it should restore to the PREPARED state– 2. If worker uses lock, recovery reacquires any locks the

PREPARED transaction was holding when fail– 3. Restart the persistent sender to learn the current status

of higher-layer transaction• If a worker uses version history– Only step-3 is needed

61

Page 62: Transaction

Variation of 3N protocol• Carry PREPARE/PREPARED in initial RPC req/rep

– Lose the ability to ABORT when PREPARED– Must remain on the knife edge– It is preferred to delay the PREPARE/PREPARED pair

• 4N: with ACKs– Coordinator safely discard its outcome record after collecting all ACKs

• Presumed commit– Expect most transactions commit– Space-efficient representation of COMMITTED: non-existence

• COMMIT by destroy the outcome record, but not when ABORT

– Answers inquiry about a non-existent outcome record by sending a COMMITTED response

62

Page 63: Transaction

Unsolved problem• What if the coordinator crash after sending

PREPARE but before sending COMMIT?– Workers are left in PREPARED with no way to proceed

• A problem that cannot be solved– Several workers will do their parts eventually, not

necessarily simultaneously– Alice would in trouble if she had required simultaneous

action

• Dilemma of the two generals

63

Page 64: Transaction

The dilemma of the two generals

64

Page 65: Transaction

The conclusion of two generals

• There is no protocol with a bounded number of messages that can convince both generals that it is safe to march– If there is, then the last message must be

unnecessary, then could be deleted• Practical method– Consider the probability of accomplished within a

specified time– Then send several runners

65

Page 66: Transaction

Case studies: machine language atomicity

• Complex instruction sets: the general electric 600 line– Indirect and tally: set by one-bit of indirect address– Load register A from Y indirect.

• First Y++, then A = [Y.old]• Can be used to sweep through a table

– Problem: page fault?• The indirect word and operand are in different pages• Then the state of the processor is “half-way of a instruction”

– Solution: save internal state: 216-bit• Programmer get info not in the API• Be careful when restore

66

Page 67: Transaction

More elaborate instruction sets: the IBM system/370

• Multi-operand character-editing instruction– TRANSLATE contains 3 arguments

• Two address: string and table• 8-bit counter: length• Takes 1 byte at a time from string, as an offset in table, get the

byte in table, then replace the byte in string– Problem: page fault– Solution: dry run

• Hidden copy of register make no change to memory• Maybe several dry run for one instruction• Then run it again

– Problem: what if another process snatch a page?

67

Page 68: Transaction

The Apollo desktop computer and Motorola M68k

• Problem: ISA is not atomic when adding VM, and multiple processors

• Solution: install two 68k– When the first encounters a page fault, stops– The second fetches the missing page, restarts the first one

• Solution-2: – Modify compilers to generate atomic instruction only– More tricky but risky

• Solution-3:– Save internal state

68

Page 69: Transaction

Review problem set 37: two-phase commit• First phase: PREPARE / vote

– First, the coordinator sends a PREPARE message to each of the workers.– For each worker, if it is able to commit, it writes a log record indicating it is entering the

PREPARED state and send a YES vote to the coordinator; otherwise it votes NO.

• Second phase-1: COMMIT or ABORT– If all YES, coordinator logs a COMMIT and sends a COMMIT to all workers, which in turn

log a COMMIT record. ABORT is similar.

• Second phase-2: ACKNOWLEDGMENT / END– After they receive the transaction outcome, workers send an ACKNOWLEDGMENT

message to the coordinator.– Once the coordinator has received all ACK, it logs an END record.

• Crash recovery– Workers that have not learned the outcome of a transaction periodically contact the

coordinator asking for the outcome.– If the coordinator does not receive an ACKNOWLEDGMENT from some worker, after a

timer expiration it resends the outcome to that worker, persistently if necessary

69

Page 70: Transaction

70

Page 71: Transaction

Review so far

71

Page 72: Transaction

72

• Isolation last lecture: serializability• the schedule produced by interleaving of concurrent trans == a

schedule of serial trans• if not, then transaction system is incorrectly implemented• implementation: strict two-phase locking• acquire and hold lock until lock point• various techniques to handle deadlock• release read locks after lock point• hold write locks until commit/abort• isolation & recoverability• The log must have a serializable schedule• --> no special action require for isolation during recovery

Page 73: Transaction

73

Weaker Forms of Isolation

• Goal: get more concurrency– Approach: sacrifice serializability

• Example from hands-on– Snap-shot isolation– Read-committed

Page 74: Transaction

74

Snapshot Isolation• Setup: table with doctors, oncall=true

• T1: select count(*) from doctors where oncall=true;If count > 1update doctors set oncall=false where username = 'alice’;

• T2:select count(*) from doctors where oncall=true;If count > 1update doctors set oncall=false where username = 'bob';

Page 75: Transaction

75

Read-committed Isolation• Setup: table with doctors, oncall=false

• T1:select count(*) from doctors where oncall=false;select count(*) from doctors where oncall=false;

• T2:update doctors set oncall=true where username = 'bob';commit;

Page 76: Transaction

76