Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Transaction-Oriented Computing COP 6730. 2 The Temporary Update Problem Problem: T2 reads the invalid “temporary” value. X=80 Y=60 Transfer $5 Deposit.

Similar presentations


Presentation on theme: "1 Transaction-Oriented Computing COP 6730. 2 The Temporary Update Problem Problem: T2 reads the invalid “temporary” value. X=80 Y=60 Transfer $5 Deposit."— Presentation transcript:

1 1 Transaction-Oriented Computing COP 6730

2 2 The Temporary Update Problem Problem: T2 reads the invalid “temporary” value. X=80 Y=60 Transfer $5 Deposit $4 Balance should be: $80 - $5 + $4 = $79

3 3 The Lost Update Problem This update is lost Balance should be: $80 - $5 + $4 = $79. Balance should be: $80 - $5 + $4 = $79. X=80 Y=60 Transfer $5 Deposit $4

4 4 The Incorrect Summary Problem 205  70 + 80 + 60  Inconsistent ! Transactions can be processed concurrently for efficiency. However, they must have the same results as in serial execution W X Y

5 5 Outline Atomic Action and Flat Transactions. Sphere of Control. Notations Transaction Models

6 6 ACID Properties A tomicity: A transaction’s changes to the state are atomic – either all happen or non happen. C onsistency: A transaction is a correct transformation of the state. I solation: Even though transaction execute concurrently, it appears to each transaction, T, that other executed either before or after T, but not both. D urability: Once a transaction completes successfully, its changes to the state survive failures (transaction’s effects are durable). A transaction is a collection of actions with the following ACID properties:

7 Transaction: Atomicity Property A transaction is an atomic sequence of actions (e.g., read/write database). Each transaction, executed completely, must leave the DB in a consistent state if DB is consistent when the transaction begins. – Users can specify some simple integrity constraints on the data, and the DBMS will enforce these constraints. – Beyond this, the DBMS does not really understand the semantics of the data. (e.g., it does not understand how the interest on a bank account is computed). – Thus, ensuring that a transaction (run alone) preserves consistency is ultimately the user’s responsibility!

8 8 Disk Writes as Atomic Actions (1) 1.Single disk write: Problem : If there is a power loss in the middle of the operation, it might happen that the first part of the block is written, the the rest is not. 2.Read-after-Write: First issues a single disk write, then reads the block from disk to verify the correctness of the data. Problem : There is no provision to return to the initial state. Atomicity of a disk write operation would mean that either the entire block is correctly transferred to the specified slot, or the slot remains unchanged.

9 9 3.Duplexed write : Each data block is written to two places on disk 4.Logged write : The old contents of the block are first read and then written to a different place. Then the block is modified, and eventually written to the old location Disk Writes as Atomic Actions (2) 5 11 5 RAM

10 Observation: Even simple operations cannot simply be declared atomic; rather, atomicity is a property for which the operations have to be designed and implemented. 10 Disk Writes as Atomic Actions (3)

11 11 Action Types Unprotected Actions : They lack all of the ACID properties except for consistency. –Example: A single disk write. Protected Actions : They have the ACID properties. –They do not externalize their result before they are completely done (i.e., Isolation). –They can roll back if anything goes wrong before the normal end (i.e., Atomicity and Consistency). –Once they have reached their normal end, what has happened remains (i.e., Durability). Real Actions: These actions affect the real, physical world in a way that is hard or impossible to reverse. –Example: Firing a missile, dispensing money from an ATM

12 12 Higher-Level Protected Actions (Transactions ) Since unprotected actions can be undone, they can be included in a higher-level operation (i.e., transaction), which as a whole has the ACID properties. unproted action Transaction (protected) Begin Work Commit Work

13 13 Handling Real Actions The higher-level operation must make sure that the real actions are executed only if all enclosing protected actions have reached a state in which they will not decide to roll back BEGIN_WORK SELECT … /*unprotected action*/ UPDATE … /*unprotected action*/ DRILL_HOLE … INSERT … SELECT … IF (Condition) COMMIT_WORK; ELSE ROLLBACK_WORK; Defer execution of real action Now do it Don’t do it at all Note: The ACID properties hold for everything executed between BEGIN_WORK and COMMIT_WORK.

14 Concurrency Control Concurrent execution of user programs is essential for good DBMS performance. – Since disk accesses are frequent and relatively slow, it is important to keep the cpu humming by working on several user programs concurrently. Interleaving actions of different user programs can lead to inconsistency: e.g., check is cleared while account balance is being computed. Transaction processing ensures such problems do not arise: users can pretend they are using a single-user system.

15 Scheduling Concurrent Transactions DBMS ensures that execution of {T1,..., Tn} is equivalent to some serial execution T1,... Tn. – Before reading/writing an object, a transaction requests a lock on the object, and waits till the DBMS gives it the lock. – All locks are released at the end of the transaction. (Strict 2-Phase locking protocol) XT1T1 T2T2 RW I have the lock I wait XT1T1 T2T2 W I am done I can lock now

16 2PL Locking Protocol Number of locks acquired Time What if I need the lock again before commit ? Strict 2PL 2PL 2PL offers more concurrency; but it is difficult to implement

17 Deadlock X T1T1 T2T2 R2R2 W3W3 I have the lock on X I wait for X A solution: T 1 or T 2 is aborted and restarted Y W4W4 W1W1 I have the lock on Y I wait for Y Deadlock

18 Ensuring Atomicity DBMS ensures atomicity (all-or-nothing property) even if system crashes in the middle of a transaction. Idea: Keep a log (history) of all actions carried out by the DBMS while executing a set of Xacts: – Write-Ahead Log (WAL) Protocol: Before a change is made to the database, the corresponding log entry is forced to a safe location. (OS support for this is often inadequate.) – Rollback: After a crash, the effects of partially executed transactions are undone using the log. Note: If log entry wasn’t saved before the crash, corresponding change was not applied to database!

19 19 A Flat Transaction Example: Debit/Credit (1) exec sql CREATE TABLE accounts( Aid NUMERIC(9), Bid NUMERIC(9) FOREIGN KEY REFERENCES branches, Abalance NUMERIC(10), fillerCHAR(48), PRIMARY KEY (Aid); accounts AidBidAbalancefiller

20 20 A Flat Transaction Example: Debit/Credit (2) /***main program with the invocation environment */ /* global declarations*/ exec sql BEGIN DECLARE SECTION; /* declare working storage*/ long Aid, Bid, Tid, delta, abalance;/* account id, branch id, teller id, debit or */ /* credit amount, account balance*/ exec sql END DECLARE SECTION;/* front end for the transaction program*/ DCApplication()/**/ {read input msg;/* deal with request messages*/ exec sql BEGIN WORK;/* start the flat transaction */ Abalance = DoDebitCredit(Bid, Tid, Aid, delta); /* invoke transaction program*/ send output msg;/* send response to terminal*/ exec sql COMMIT WORK;/* successful end of transaction*/ }/**/

21 21 A Flat Transaction Example: Debit/Credit (3) /* subroutine for doing the database accesses defined for TPC debit/credit transaction long DoDebitCredit(long Bid, long Tid, long Aid, long delta) { exec sql UPDATE accounts/* apply delta to the account balance SET Abalance = Abalance + :delta/**/ WHERE Aid = :Aid;/**/ exec sql SELECT Abalance INTO :Abalance/* read new value of balance*/ FROM accounts/**/ WHERE Aid = :Aid/**/ exec sql UPDATE tellers/* apply delta to teller balance*/ SET Tbalance = Tbalance + :delta/**/ WHERE Tid = :Tid;/**/ exec sql UPDATE branches/* apply delta to branch balance*/ SET Bbalance = Bbalance + delta/**/ WHERE Bid = :Bid;/**/ exec sql INSERT INTO history (Tid, Bid, Aid, delta, time) /* insert parameters of transaction*/ VALUES (:Tid, :Bid, :Aid, :delta, CURRENT)/* into application history*/ return(Abalance); }

22 22 ROLLBACK Statements DCApplication()/**/ {receive input message;/* deal with request messages*/ exec sql BEGIN WORK;/* start the flat transaction*/ Abalance = DoDebitCredit(Bid, Tid, Aid, delta);/* invoke transaction program*/ if (Abalance < 0 && delta < 0)/* check if it a debit and account overdrawn */ {exec sql ROLLABCK WORK; }/* if so: don’t do it*/ else/* this the good case: either credit or */ {/* enough money in account*/ send output message;/* send response to terminal*/ exec sql COMMIT WORK;/* successful end of transaction*/ } } ROLLBACK makes sure that all the records are returned to their Previous states.

23 23 Limitations of Flat Transactions (1) A one-layer control structure will not be able to model application structures that are significantly more complex. Example1: Trip Planning If there are no direct flights between two places, we must book a number of connecting flights. If a partially done travel plan is not acceptable, there is no need to give back the reservation on all the flights. Note: Partial rollback is not possible for flat transactions.

24 24 Limitations of Flat Transactions (2) Example 2: Bulk Updates At the end of a month, a bank has to modify all of its accounts by crediting or debiting the accumulated interest. If the database process crashes after a large number of accounts have been updated, undoing these effects is undesirable. Note: Partial commit is useful here.

25 25 SPHERES OF CONTROL Spheres of Control (SoC) are based on a hierarchy of abstract data types (ADTs) which execute atomically

26 26 Two Varieties of SPHERES OF CONTROL  One is statically established by structuring the system into a hierarchy of ADTs.  The other results from dynamic interactions among SoCs on shared data, which cannot be committed yet. B1 C2 C1 B3 B4 B5 B2 A1 A2 S D D is a noncommitable data item. Dynamically created for controlling the commitment of A1 Dynamically created for controlling the commitment of A1 Predefined hierarchy of functions Dynamic interaction

27 27 Dependency in Transactions Structural Dependency Dynamic Dependency

28 28 Structural Dependencies in Transaction Models Structural dependencies reflect the hierarchical organization of the system into ADTs of increasing complexity. Example: The commit of B3 depends on the commit of A2. The reason is that A2 appears as an atomic action to the outside. B1 C2 C1 B3 B4 B5 B2 A1 A2 D S D is a noncommitable data item.

29 S 29 Dynamic Dependencies in Transaction Models Dynamic dependencies arise from the use of shared data. Example: A2 depends on A1 in the sense that A2 can commit only if A1 does. B1 C2 C1 B3 B4 B5 B2 A1 A2 D D is a noncommitable data item. S is dynamically created for controlling the commitment of A1

30 30 Dynamic Behavior of SoC (1) 1.Beginning of scenario: At time t, a problem is discovered in the SoC B. Data from D1 is determined to be the case of the problem. t time Problem

31 31 Dynamic Behavior of SoC (2) 2. Tracing dependencies backward in time: A dynamic SoC, F, is extended backward in time to contain the SoC that created the invalid data item D1. F serves as the recovery environment. t Problem Recovery environment Recovery environment F

32 t time Problem 32 Dynamic Behavior of SoC (3) 3. Again going forward in time to do recovery:  The recovery SoC, F, is expanded forward in time.  F must encompass all processes that have become dependent on any data produced by the process that created D1. Note: Execution history must be kept for as long as control might still need to be exercised F

33 33 State-Transition Diagram for a Flat Transaction –Flat transactions can be described as state machines, –When describing phenomena for which it is not possible to define a prior a fixed number of states, this approach is not appropriate. –We will adapt the SoC model for describing transaction models. NULL active aborted committed BEGIN WORK ROLLBACK WORK WORK COMMIT terminate

34 34 Describing Transaction Models for creating the events that drive atomic actions, and for the conditions under which the rules can take effect. Transaction models are distinguished by different sets of rules

35 35 Describing Transaction Models - Example Dynamically created for controlling the commitment of A1 B1 C2 C1 B3 B4 B5 B2 A1 A2 D S D is a noncommitable data item. The event ROLLBACK WORK for atomic action B3 can be triggered by the program running inside B3, or by the abort of A2 The signal COMMIT WORK for B3 is not sufficient to actually commit B3, that can only happen if the condition “A2 is ready to commit” is also true.

36 36 Graphical Notation for Transaction Models ABC T AC State indicator of the action’s outcome Aborted Committed Begin Abort Commit Signal entries for the atomic action to perform a state transition Eternally unique identifier of the atomic action Each instance of an atomic action is depicted as a box with three entries at the top representing the signals for state transitions the action can receive, and two entries at the bottom representing the two (mutually exclusive) final outcomes of the action.

37 37 Describing Flat Transactions ABC System AC ABC T AC a) Transaction T is active: An abort of the system transaction will cause it to roll back, too. A flat transaction is an atomic action with only one structural dependency: If the system transaction aborts, the flat transaction is forced to abort, too. The system transaction is always in the active state, never commits, and aborts only as a result of a system crash. Once a flat transaction has committed, it stays around without any dependencies, only documenting which final result is relate to its name. A flat transaction is an atomic action with only one structural dependency: If the system transaction aborts, the flat transaction is forced to abort, too. The system transaction is always in the active state, never commits, and aborts only as a result of a system crash. Once a flat transaction has committed, it stays around without any dependencies, only documenting which final result is relate to its name. ABC System AC ABC T AC ABC AC ABC T AC b) Termination scenario 1: Transaction T has committed; all of its event ports are deactivated; the final state is highlighted. c) Termination scenario 2: Same as scenario 1, but T assumes the final abort sate. Incoming port that cannot receive events, or final state that cannot be assumed Trigger Final state Final state

38 38 Defining Transaction Models by Rules (1) The specifies the signal port this rule is for This rule is not activated until the preconditions are fulfilled : →,,. Wait Precondition Signal port S A (T1)

39 39 Defining Transaction Models by Rules (2) describes which signals are generated as part of the state transition. (The signals are simply the names of rules that are to be activated by the signal). is essentially redundant, but it is kept for clarity. : →,,. ABC System AC ABC T AC Activate S A (T) is a signal from S A (System)

40 40 Defining Transaction Models by Rules (3) is used to introduce transactional dependencies as they are needed, or to delete dependencies that have become obsolete. ::= +( | ) ::= - ( | ) ::= delete(x), x is an atomic action. is the name of the rule to be activated by the signal : →,,. ABC System AC ABC T AC Trigger +(S A (system)|S A (T)) adds transactional dependency !

41 41 vs : →,,. ABC T1 AC Signal port S A (T1) ABC T1 AC ABC T2 AC Signal port S A (T1) ABC T1 AC ABC T2 AC Trigger S B (T2) +(S A (T1)|S A (T2)) Signal port S A (T1)

42 42 Rules for Flat Transactions S B (T):  +(S A (system)|S A (T)),, BEGIN WORK The first rule establishes the dependency from the system transaction and then starts the flat transaction itself. ABC System AC ABC T AC a) Transaction T is active: Abort of the system transaction will cause it to roll back, too. ABC System AC ABC T AC ABC AC ABC T AC b) Termination scenario 1: Transaction T has committed; all of its event pots are deactivated; the final state is highlighted. c) Termination scenario 2: Same as scenario 1, but T assumes the final abort sate. Trigger Remove the rules Remove the rules : →,,. Note: No precondition, no signal list

43 43 Rules for Flat Transactions S B (T):  +(S A (system)|S A (T)),, BEGIN WORK S A (T):  (delete(S B (T)), delete(S C (T))),,ROLLBACK WORK S C (T):  (delete(S B (T)), delete(S A (T))),,COMMIT WORK The first rule establishes the dependency from the system transaction and then starts the flat transaction itself. The other rules specify the effects of the abort and commit events – since both resulting states are final states, the rules pertaining to the terminated transaction must be removed. ABC System AC ABC T AC a) Transaction T is active: Abort of the system transaction will cause it to roll back, too. ABC System AC ABC T AC ABC AC ABC T AC b) Termination scenario 1: Transaction T has committed; all of its event pots are deactivated; the final state is highlighted. c) Termination scenario 2: Same as scenario 1, but T assumes the final abort sate. Trigger Remove the rules Remove the rules

44 44 Flat Transaction with Savepoints (1) BEGIN WORK (get transaction started) implicit SAVE WORK: 1 Action SAVE WORK: 2 Action SAVE WORK: 3 Action SAVE WORK: 4 Action ROLLBACK WORK (2) Action SAVE WORK: 5 Action SAVE WORK: 6 Action SAVE WORK: 7 Action ROLLBACK WORK (7) Action SAVE WORK: 8 Action COMMIT WORK Work “covered” by savepoint number 5 Work “covered” by savepoint number 2 undone

45 45 Flat Transaction with Savepoints (2) A savepoint is established by invoking the SAVE WORK function, which causes the system to record the current state of processing. –This returns to the application program a handle that can subsequently be used to reestablish (return to) that savepoint. A ROLLBACK does not affect the savepoint counter. –Advantage: Increasing Numbers monotonically allows the complete execution history to be maintained. –Disadvantage: It may lead to very unstructured programs. There is nothing that prevents the application program, after having generated savepoint number 8, from requesting a rollback to savepoint 4 (see next slide).

46 46 Unstructured Program BEGIN WORK (get transaction started) implicit SAVE WORK: 1 Action SAVE WORK: 2 Action SAVE WORK: 3 Action SAVE WORK: 4 Action ROLLBACK WORK (2) Action SAVE WORK: 5 Action SAVE WORK: 6 Action SAVE WORK: 7 Action ROLLBACK WORK (7) Action SAVE WORK: 8 Action ROLLBACK WORK (4) undone There is nothing that prevents the application program, after having generated savepoint number 8, from requesting a rollback to savepoint 4. “SAVE WORK: 4” work never happended

47 47 Graphical Representation of the Savepoint Model (1) The basic idea is to regard the execution between two consecutive savepoints as an atomic action in the sense of SoC. The completion of a savepoint creates a new atomic action that is dependent on its predecessor. ABC System AC ABC S1 AC Trigger ABC S2 AC Trigger Dependency SoC Trigger

48 48 Graphical Representation of the Savepoint Model (2) ABC System AC ABC S1 AC Trigger a) Transaction has started, establishing savepoint 1. ABC System AC ABC S1 AC Trigger b) Next savepoint, S2, has been taken. ABC S2 AC Trigger ABC System AC ABC S1 AC Trigger ABC S2 AC Trigger ABC S3 AC Trigger c) Next savepoint, S3, has been taken. Forbidden state or event

49 49 The Rules for Savepoint Model Note: The delete clauses in the abort and commit rules look exactly like in the case of flat transactions, and are omitted here. First Savepoint, S 1 : S B (S 1 ) :  +(S A (system)|S A (S 1 )),, BEGIN WORK S A (S T ) : (S T <S 1 ) ,, ROLLBACK WORK S C (S 1 ) : ,, COMMIT WORK S S (S 1 ) :  +(S A (S 1 )|S A (S 2 )), S B (S 2 ). S T : The target savepoint of the rollback S T : The target savepoint of the rollback (2) +(S A (S 1 )|S A (S 2 )) (1) S B (S 2 )

50 50 The Rules for Savepoint Model Note: The delete clauses in the abort and commit rules look exactly like in the case of flat transactions, and are omitted here. During a rollback, all the atomic actions on the way back are aborted. First Savepoint, S 1 : S B (S 1 ) :  +(S A (system)|S A (S 1 )),, BEGIN WORK S A (S T ) : (S T <S 1 ) ,, ROLLBACK WORK S C (S 1 ) : ,, COMMIT WORK S S (S 1 ) :  +(S A (S 1 )|S A (S 2 )), S B (S 2 ). Intermediate Savepoint, S n : S B (S n ) : ,, BEGIN WORK S A (S T ) : (S T <S n ) , S A (S n-1 ), ROLLBACK WORK S C (S n ) : , S C (S n-1 ), COMMIT WORK S S (S n ) :  +(S A (S n )|S A (S n+1 )), S B (S n+1 ). S T : The target savepoint of the rollback

51 51 Limitation of Savepoint All savepoints perish in case of a system crash A solution is to make savepoints persistent the state of the transaction must be kept in durable (persistent) storage.

52 52 Persistent Savepoints Restart Strategy The last unfinished savepoint interval is roll back, but the state as of the previous successful persistent savepoint is reestablished. Potential Issue: Conventional programming languages do not understand transactions: the data contents will return to the state as of the specified savepoint, but the local programming language variables will not.

53 53 CHAINED TRANSACTIONS Chained transactions are modeled by a sequence of atomic actions, executed one at a time. Each transaction behaves like a flat transaction (i.e., no rollback after commit) The commitment of one transaction and the beginning of the next are wrapped together into one atomic operation.  The database content remains intact across the transaction boundaries. Effect: The amount of work lost after a crash can be minimized.

54 Rules for Chained Transactions S B (Cn):  (delete(S B (Cn)),+(S A (system)|S A (Cn))),, BEGIN WORK S A (Cn):  (delete(S A (Cn)), delete(S C (Cn))),, ROLLBACK WORK S C (Cn):  (delete(S A (Cn)), delete(S C (Cn))), S B (Cn+1), COMMIT WORK ABC System AC ABC C1 AC ABC C2 AC Trigger a)The first transaction in the chain has been started. b)Start of the second one will be triggered by commit of the first one. ABC System AC ABC C1 AC ABC C2 AC Trigger c)The first transaction in the chain has been committed; the second one got started as part of commit of C1. Now the second one is structurally dependent on “system”. 54

55 55 Chained Transaction v.s. Savepoints Since the chaining step irrevocably completes a transaction, rollback is limited to the currently active transaction (Not as flexible as Savepoint) The COMMIT allows the application to free locks that it does not later need (Note: Persistent Savepoint does not have this property) After a crash, the entire transaction is rolled back irrespective of any savepoints taken so far. The chained transaction schemes, on the other hand, can reestablish the state of the most recent commit. ABC System AC ABC C1 AC ABC C2 AC Trigger Active transaction Active transaction Committed transaction Committed transaction

56 56 Restart Processing in Chained Transactions From the application’s point of view, the whole chain is likely to be the sphere of control. What should actually happen in case one of the transactions in the chain aborts? Abort: the application has to determine how to fix that. ABC System AC ABC C1 AC ABC C2 AC Trigger commit

57 57 Restart Processing in Chained Transactions From the application’s point of view, the whole chain is likely to be the sphere of control. What should actually happen in case one of the transactions in the chain aborts? Abort: the application has to determine how to fix that. Crash: the last transaction, after having been rollback, should be restarted. ABC System AC ABC C1 AC ABC C2 AC ABC C1’ AC ABC Restart AC Trigger C1’ is the new instance of C1 New instance of C1 starts over again New instance of C1 starts over again Restart C2

58 Chained Transaction vs. Savepoints 58 Savepoints Persistent Savepoints Chained Transaction Rollback Any previous savepoint Limited to currently active transaction Recovery Entire transaction is rolled back Reestablish most recent savepoint Reestablish the state of most recent commit Concurrency Locks kept until transaction commit Free locks early

59 59 Nested Transactions A nested transaction is a tree of subtransactions. A subtransaction can either commit or rollback. –Its commit takes effect only if the parent transaction commits. –Any subtransaction can finally commit only if the root transaction commits. Starting at the root. each transaction can create lower-level subtransactions, which are embedded in the sphere of control of the parent. Transactions at the leaf level are flat transactions, except that they lack the durability of non-nested flat transactions.

60 Nested Transaction - SOC 60 T1 T21 T22 T212 T211 T222 T221 T2122 T2121 T2112 T2111 Any subtransaction can finally commit only if the root transaction commits Any subtransaction can finally commit only if the root transaction commits Its commit takes effect only if the parent transaction commits Its commit takes effect only if the parent transaction commits

61 61 Behavior of Nested Transactions Commit & Rollback Commit rule: The commit of a subtransaction makes its results accessible only to the parent transaction. Rollback rule: The rollback of a subtransaction causes all of its subtransactions to rollback. –Subtransactions have only A,C, I, but not D.

62 62 Behavior of Nested Transactions Visibility Rule All changes done by a subtransaction become visible to the parent transaction upon the subtransaction’s commit. All objects held by a parent transaction can be made accessible to its subtransactions. Changes made by a subtransaction are not visible to its siblings, in case they execute concurrently.

63 63 Rules for Nested Transactions Note: The arrows labeled “wait” correspond to the precondition clauses in the rules. S B (T kn ):  +(S A (T k )|S A (T kn )),, BEGIN WORK S A (T kn ): ,, ROLLBACK WORK S c (T kn ): C(T k ) ,, COMMIT WORK ABC T AC ABC T AC ABC T AC ABC T1 AC ABC T2 AC ABC T11 AC ABC T1 AC System Trigger Wait a) Root transaction T is running. b) Subtransaction T1 has been started. c) T1 has created subtransaction T11, and after that the root transaction starts another subtransaction, T2. Wait T k is the parent of T kn

64 64 Using Nested Transaction Strong relationship between modularization in software engineering and the nested transaction mechanism. –Modular design takes care of the application structure and encapsulates local (dynamic) data structures; –the transactions make sure that the global data used by these modules are isolated and recover with the same granularity. Note: Database is a very large global variable

65 65 Transactional C Transactional C (IBM) is a programming language supported by the Encina TP monitor. The run-time system of Transactional C supports the notion of nested transactions.

66 66 Emulating Nested Transaction by Savepoints Strategy: A savepoint is generated at the beginning of each subtransaction rolling back to a savepoint has the same effect as an abort of the corresponding subtransaction. Savepoint generated at beginning of each subtransaction Savepoint generated at beginning of each subtransaction Rollback to savepoint s121 has the effect of aborting Tk121 Rollback to savepoint s121 has the effect of aborting Tk121

67 67 Limitations of the Emulation of Nesting by Savepoints (1) –Reestablishing the state of the savepoint associated with a transaction can affect arbitrary subtransactions, rather then only the subtree of the aborting subtransaction. The emulations is limited to systems where everything within a top-level transaction is executed sequentially. Reason: –If subtransactions are allowed to execute in parallel, the nesting order will no longer be monotonically mapped onto the sequence of savepoints.

68 68 Limitations of the Emulation of Nesting by Savepoints (2) In nested transactions, subtransactions inherit locks from their parent transactions, and parent transactions counter-inherit locks acquired by their subtransactions. This mechanism cannot be emulated by savepoints. Reason: There is only one flat transaction in using savepoints.

69 69 Distributed Transactions A distributed transaction is typically a flat transaction that runs in a distributed environment. The decomposition into distributed transactions does not reflect a hierarchical structure in the programs to be executed, but is induced by the placement of the data in the network. In a distributed transaction, if a subtransaction commits, it forces all other subtransactions to commit. Note: By comparison, in a nested transaction, this is simply a local commit of that subtransactions. Distributed subtransactions normally cannot roll back independently. Their decision to abort affects the entire transaction.

70 70 Multi-Level Transactions ABC T AC ABC N AC Trigger a) Root transaction T is running. b) Subtransaction N has been started. ABC System AC Trigger ABC T AC ABC System AC Trigger ABC N AC ABC CN AC c) Subtransaction N has committed and has installed its compensation transaction, CN, that will get started if T aborts. CN must commit. Trigger N can commit independently of T, When it does so; however, CN enter the scence CN is compensating transaction for N. It can semantically reverse what N has done Abort of T is unconditionally linked to the commit of CN

71 71 Multi-Level Transactions vs Nested Transactions ABC T AC ABC System AC Trigger ABC N AC ABC CN AC Trigger ABC T AC ABC T1 AC ABC T2 AC ABC T11 AC System Trigger Wait T2 does not have the durability property T2 does not have the durability property Precommit: N has the durability property Precommit: N has the durability property Nested transaction Multi-level transaction

72 72 Rules for Multi-Level Transactions Rules for subtransaction N : S B (N): ,, BEGIN WORK S A (N): ,, ROLLBACK WORK S C (N):  +(S A (T)|S B (CN)), S B (CN), COMMIT WORK Rules for the compensating translation CN: S B (CN):  +(S C (restart)|S B (CN’)),, BEGIN WORK S A (CN): , S B (CN’), ROLLBACK WORK S C (CN):  delete(CN’),, COMMIT WORK Restart: A system restart transaction runs after an abort caused by some internal malfunction Note: CN’ is an instance of CN. It ensures that the compensation continues after restart.

73 73 Advantage & Disadvantage of Multi-Level Transactions Advantage: Multi-level transactions are nested transactions with the ability to precommit the results of subtransactions, and therefore improve concurrency. Disadvantage: The compensation actions can be very expensive. Note: Since rollback is not frequent, the advantages of having smaller units of commitment control outweigh the cost of compensation.

74 Summary: Transaction Models 74 Flat Transaction Savepoint Persistent Savepoint Chained Transaction Multilevel Transaction Nested Transaction Support partial rollback Minimize lost work in case of system crash Improve concurrency Support structured programming Improve concurrency

75 75 Transaction Processing Context Programs frequently use contextual information in addition to the input parameters; this information is called context and is (potentially) modified every time the program is invoked. f(input_message, context)  {output_message, context’}

76 :a Computation 76 Transaction Processing Context Example exec sql declare cursor x select a, b, c from rel_a where d = 10 order by a ascending; open cursor x; do { exec sql fetch next x into :a, :b, :c; /* perfrom computation */ } while (sqlcode == 0); exec sql close cursor x; Private context: The cursor position is a context that is private to the program. Global context: The contents of the database itself determines which “next” tuple can be retrieved. a b c d x 10 3 7 9 12 16 21 :c :b

77 77 COMPUTE INTERESTS If all accounts are updated within one transaction, there is no need for maintaining context Reason: If it fails, the same thing can be done over again (i.e., it is context-free) Splitting the job up into a sequence of transactions requires keeping context in durable memory

78 78 COMPUTE INTERESTS ComputeInterest () { read (interest_rate); for (account_no = 1; account_no <= 1,000,000; account_no++) {SingleAccount(interest_rate, account_no);} reply (“done”) }; SingleAccount (interest_rate, account_no) { BEGIN WORK /*do account update*/ COMMIT WORK; return (“OK”); } This transaction is context-free Because the next account number is passed in the input message This transaction is context-free Because the next account number is passed in the input message The surrounding program needs to keep the context just in case something happens along the way The surrounding program needs to keep the context just in case something happens along the way

79 79 DURABLE CONTEXT Context can be volatile or durable: –An end-of-file pointer is volatile context because it needs not be recovered. –The context that says how far the sequence of ComputeInterest transactions has gotten must be durable because the program (reinstantiated version) and the database are out of synch after a crash.

80 80 THE MINI BATCH (1) A solution to the ComputeInterest problem: –Executing a sequence of transactions, each of them updating only a small number of accounts (called mini batch). –The application maintains its context as a database record. n-2 0 1 … n-1 n n+1 … i  stepsize  … Accounts |   | BATCHCONTEXT Last_account_done PROCESS Note: Isolation is guaranteed only for the tuples the step transaction is accessing. Database

81 81 THE MINI BATCH (2) while (last_account_done < max_account_no) {EXEC SQL BEGIN WORK;/* initiate next mini-batch */ EXEC SQL UPDATE accounts /* compute current mini-batch */ SET account_total = account_total * (1+interest_rate) WHEREaccount_no BETWEEN :last_account_done + 1 AND :last_account_done + :stepsize; EXEC SQL UPDATE batchcontext /* update context */ SET last_account_done = last_account_done + stepsize; EXEC SQL COMMIT WORK; last_account_done = last_account_done + stepsize; /* next mini-batch */ } 0 1 … n-1 n n+1 … i  stepsize  … Accounts |   | Last_account_done Last_account_done + stepsize

82 82 Mini-batch is not Strictly Atomic Mini-batch does not achieve strict atomicity for the entire computation Nevertheless, it is suitable for the ComputeInterest application because the interests must eventually be computed (i.e., transaction eventually commits) Commit cannot always be guaranteed for other types of long-lived transactions –Examples include many engineering design and office automation applications

83 83 LONG-LIVED TRANSACTIONS General Requirements 1.Minimize lost work: It must be possible to split up bulk transactions in order to control the amount of lost work in case of a system crash. 2.Recoverable computation: There must be ways to temporarily stop the computation without having to commit the results (ACID has no notion of “suspending” a transaction). 3.Explicit control flow: Under all failure conditions, it must be possible to either proceed along the pre-specified path or remove from the system what has been done so far.

84 84 SAGAS Saga is an extension of the notion of chained transactions. 1.It defines a chain of transactions as a unit of control. 2.It uses the compensation idea from multi-level transactions to make the entire chain atomic.

85 Added to Chained Transaction 85 SAGAS - Properties Commit case: S 1, S 2, …, S i, …, S n-1, S n Rollback scenario: S 1, S 2, …, S j (abort), CS j-1 …, CS 2, CS 1 A B C A C A B C A C A B C A C A B C A C A B C A C A B C A C System S1S2 S3 CS1 CS2 Trigger A backward chain of compensating transactions is established as the original chain proceeds in a forward direction.

86 86 COOPERATING TRANSACTIONS (1) Cooperative transactions allow for explicit interactions among collaborating users on shared (design) objects. Transaction A Transaction B create object X X do some design work show me object X granted Use object X Give back X Time Preliminary, must not be modified B may resume computation on X

87 87 COOPERATING TRANSACTIONS (2) Prerelease upon request: –object X is isolated until the surrounding SoC has decided to commit. –However, there may be special transactions that are allowed to access it without creating a commit dependency on it. Explicit return: A transaction T that selectively releases commitment control knows: –who has access to the object –what kind of access is requested to the object, and –T gets informed as soon as the object is returned to its original SoC.

88 Summary: Transaction Models 88 Savepoints Flat transaction Flat transaction Persistent savepoints Persistent savepoints Chained transaction Chained transaction Single layer of control Can rollback to a savepoint Reestablish last savepoint after recovery – Minimize lost work in case of system crash Free locks early

89 Summary: Transaction Models 89 Savepoints Flat transaction Flat transaction Persistent savepoints Persistent savepoints Chained transaction Chained transaction Nested Transaction Nested Transaction Multilevel Transaction Multilevel Transaction Supports modularization in software engineering Precommit allows early release of locks SAGAS Chained transaction with compensating transactions

90 Summary: Transaction Models 90 Transaction 1 Transaction 1 Transaction 2 Transaction 2 May I Use “X” Cooperating Transaction I am done with “X” I know how Transaction 2 uses “X”


Download ppt "1 Transaction-Oriented Computing COP 6730. 2 The Temporary Update Problem Problem: T2 reads the invalid “temporary” value. X=80 Y=60 Transfer $5 Deposit."

Similar presentations


Ads by Google