1 / 71

Ch 4. Transaction-Oriented Computing

Ch 4. Transaction-Oriented Computing. The Temporary Update Problem. Problem: T2 reads the invalid “temporary” value. The Lost Update Problem. This update is lost!. The Incorrect Summary Problem. 210  75 + 80 + 60  Inconsistent!. Outline. Atomic Action and Flat Transactions.

oshin
Télécharger la présentation

Ch 4. Transaction-Oriented Computing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ch 4. Transaction-Oriented Computing Dr. Kien A. Hua

  2. The Temporary Update Problem Problem: T2 reads the invalid “temporary” value.

  3. The Lost Update Problem This update is lost!

  4. The Incorrect Summary Problem 210  75 + 80 + 60  Inconsistent!

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

  6. ACID Properties A transaction can be considered a collection of actions with the following properties: • Atomicity: A transaction’s changes to the state are atomic – either all happen or none happen. • Consistency: A transaction is a correct transformation of the state. • Isolation: Even though transactions execute concurrently, it appears to each transaction, T, that other executed either before or after T, but not both. • Durability: Once a transaction completes successfully, its changes to the state survive failures.

  7. Disk Writes as Atomic Actions (1) 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. • 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, but the rest is not. • Read-after-Write: First issues a single disk write, then reads the block from disk and compares the result with the original block. Problem: There is no provisions to return to the initial state.

  8. Disk Writes as Atomic Actions (2) • Duplexed write: Each block is written to two places on disk • 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 Observation: Even simple operations cannot simply be declared atomic; rather, atomicity is a property for which the operations have to be designed and implemented.

  9. Action Types • Unprotected Action: These actions lack all of the ACID properties except for consistency. • Example: A single disk write. • Protected Action: They have the ACID properties. • They do not externalize their results before they are completely done. • They can roll back if anything goes wrong before the normal end. • Once they have reached they normal end, what has happened remains. • Real Action: These actions affect the real, physical world in a way that is hard or impossible to reverse. • Example: Firing a missile.

  10. Higher-Level Protected Actions (Transactions) • Since unprotected action can be undone, they can be included in a higher-level operation, which as a whole has the ACID properties. • Real actions need special treatment. The higher-level operation must make sure that they are executed only if all enclosing protected actions have reached a state in which they will not decide to roll back.

  11. Higher-Level Protected Actions (Transactions) 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.

  12. FLAT TRANSATIONS • A flat transaction contains an arbitrary number of simple actions; these actions may, in term, be either protected, real, or even unprotected, if necessary. • A flat transaction has only one layer of control. • The transaction will either survive together with everything else (commit), or it will be rolled back with everything else (abort). Limitation: There is no way of either committing or aborting parts of such transactions, or committing results in several steps, and so forth.

  13. 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), filer CHAR(48), PRIMARY KEY (Aid);

  14. 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 */ } /* */

  15. 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 Aid = :Aid; /* */ 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); }

  16. 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 ROLLBACK 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.

  17. 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.

  18. 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.

  19. Dynamically created for controlling the commitment of A1 S A2 B2 B3 A1 C1 B4 B1 C2 B5 D D is a noncommitable data item. SPHERES OF CONTROL (1) • Spheres of Control (SoC) are based on a hierarchy of abstract data types (ADTs) which execute atomically

  20. SPHERES OF CONTROL (2) • Spheres of Control come in two varieties: • 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.

  21. Dynamically created for controlling the commitment of A1 S A2 B2 B3 A1 C1 B4 B1 C2 B5 D D is a noncommitable data item. Dependencies in Transaction Models (1) • Structural dependencies: These 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 the A2 appears as an atomic action to the outside.

  22. Dynamically created for controlling the commitment of A1 S A2 B2 B3 A1 C1 B4 B1 C2 B5 D D is a noncommitable data item. Dependencies in Transaction Models (2) • Dynamic dependencies: This type of dependency arises from the use of shared data. Example: A2 depends on A1 in the sense that A2 can commit only if A1 does.

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

  24. 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.

  25. 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

  26. NULL active BEGIN WORK COMMIT ROLLBACK WORK WORK terminate terminate aborted committed State-Transition Diagram for a Flat Transaction • Although it is useful to describe flat transactions as state machines, when describing phenomena for which it is not possible to define a priori a fixed number of states, this approach is not appropriate. • We will adapt the SoC model for describing transaction models.

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

  28. Dynamically created for controlling the commitment of A1 S A2 B2 B3 A1 C1 B4 B1 C2 B5 D D is a noncommitable data item. Describing Transaction Models -Example • 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 A2 is ready to commit too.

  29. Graphical Representation of Transaction Models (1) Begin Signal entries for the atomic action to perform a state transition Commit Aborted State indicator of the action’s outcome Eternally unique identifier of the atomic action Commit Aborted Fig 4.6: A graphical notation for general transaction models. For the graphical representation of a transaction model, 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.

  30. Graphical Representation of Transaction Models (2) Trigger a) Transaction T is active: An abort of the system transaction will cause it to roll back, too. 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. forbidden state or event Fig 4.7: Describing flat transactions by means of the graphical notation. A flat transaction is nothing but an atomic action with only one structural dependency. This dependency says that if the system transaction aborts, the flat transaction if 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 related to its name. All incoming ports that cannot receive events and all final state that cannot be assumed are shaded.

  31. Defining Transaction Models by Rules (1) • The rule id specifies which signal port the arrow points to, and the precondition says where it comes from. • Whenever a signal from a state transition arrives, it is not executed until the preconditions are fulfilled. <rule id>: <precondition> → <rule modifier list>, <signal list>, <state transition>.

  32. Defining Transaction Models by Rules (2) • <state transition> is essentially redundant, but it is kept for clarity. • <signal list> 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). <rule id>: <precondition> → <rule modifier list>, <signal list>, <state transition>.

  33. Defining Transaction Models by Rules (3) • <rule modifier>is used to introduce additional arrows as they are needed, or to delete arrows that have become obsolete. <rule modifier> ::= +(<rule id>|<signal>) <rule modifier> ::= - (<rule id>|<signal>) <rule modifier> ::= delete(x), x is an atomic action. <rule id>: <precondition> → <rule modifier list>, <signal list>, <state transition>.

  34. The Rules for the Flat Transaction Model SB(T):  +(SA(system)|SA(T)), , BEGIN WORK SA(T):  (delete(SB(T)), delete(SC(T))),,ROLLBACK WORK SC(T):  (delete(SB(T)), delete(SA(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. Trigger a) Transaction T is active: An abort of the system transaction will cause it to roll back, too. 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.

  35. Flat Transaction with Savepoints (1) Work “covered” by savepoint number 2 Work “covered” by savepoint number 5

  36. 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.

  37. Graphical Representation of the Savepoint Model Trigger Trigger Trigger Trigger Trigger Trigger Trigger a) Transaction has started, establishing savepoint 1. Trigger b) Next savepoint, S2, has been taken. Trigger c) Next savepoint, S3, has been taken. forbidden state or event

  38. Graphical Representation of the Savepoint Model (cont’d) • The basic idea is to regard the execution between two subsequent 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.

  39. The Rules for the Savepoint Model First Savepoint: SB(S1):  +(SA(system)|SA (S1)),, BEGIN WORK SA(R) :(R<S1)  ,, ROLLBACK WORK SC(S1):  ,, COMMIT WORK SS(S1):  +(SA(S1)|SA (S2)), SB(S2), Intermediate Savepoint: SB(Sn):  ,, BEGIN WORK SA(R) :(R<Sn)  , SA(Sn-1), ROLLBACK WORK SC(Sn):  , SC(Sn-1), COMMIT WORK SS(Sn):  +(SA(Sn)|SA (Sn+1)), SB(Sn+1), 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.

  40. Persistent Savepoints (1) • Making savepoints persistent implies that whenever a savepoint is taken, the state of the transaction must be kept in durable (persistent) storage. Restart Strategy: • The last unfinished savepoint interval is roll back, but • the state as of the previous successful persistent savepoint is reestablished.

  41. Persistent Savepoints (2) Problem: • Conventional programming languages do not understand transactions: the database contents will return to the state as of the specified savepoint, but the local programming language variables will not. • We will discuss the programming discipline imposed by the use of savepoints later.

  42. CHAINED TRANSACTIONS • Chained transactions are modeled by a sequence of atomic actions, executed one at a time. • Each transaction behaves like a flat transaction. • The commitment of one transaction and the beginning of the next are wrapped together into one atomic operation. • The database context remains intact across the transaction boundaries. Effect: The amount of work lost after a crash can be minimized.

  43. Rules for Chained Transactions a) The first transaction in the chain has been started; start of the second one will be triggered by commit of the first one. Trigger Trigger b) 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”. Trigger Trigger Note: Either C1 commits, then C2 begins, or C2 fails to begin, but then C1 does not commit either. SB(Cn):  +(SA(system)|SA (Cn)),, BEGIN WORK SA(Cn):  , , ROLLBACK WORK SC(Sn):  ,SB(C n+1), COMMIT WORK

  44. Chained Transaction vs. Savepoints • Since the chaining step irrevocably completes a transaction, roll back is limit to the currently active transaction • The COMMIT allows the application to free locks that it does not later need. • 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.

  45. 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. Trigger Trigger Trigger Trigger Trigger

  46. Nested Transactions • A nested transaction is a tree of subtransactions. • A subtransaction can either commit or rollback; its commit will not take effect, though, unless the parent transaction commits. • Any subtransaction can finally commit only if the root transaction commits. Fig 4.12: Nested transactions: the basic idea. A nested transaction is a tree of transactions. Starting at the root, each transaction can create lower-level transactions (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.

  47. The Behavior of Nested Transactions (1) 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.

  48. The Behavior of Nested Transactions (2) Visibility rule: • All changes done by a substransaction 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.

  49. Rules for Nested Transactions System System System Trigger a) Root transaction T is running. Trigger Trigger Wait Wait Wait b) Subtransaction T1 has been started. c) T1 has created subtransaction T11, and after that the root transaction starts another subtransaction, T2. Wait Note: The arrows labeled “wait” correspond to the precondition clauses in the rules. SB(Tkn):  +(SA(Tk)|SA (Tkn)),, BEGIN WORK SA(Tkn):  ,, ROLLBACK WORK SC(Tkn): C(Tk)  ,, COMMIT WORK

  50. Using Nested Transaction • There is a strong relationship between the concept of 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 recovered with the same granularity.

More Related