TransactionOriented Computing COP 6730 1 The Temporary Update

  • Slides: 90
Download presentation
Transaction-Oriented Computing COP 6730 1

Transaction-Oriented Computing COP 6730 1

The Temporary Update Problem: T 2 reads the invalid “temporary” value. X=80 Deposit $4

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

The Lost Update Problem This update is lost X=80 Transfer $5 Y=60 Deposit $4

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

The Incorrect Summary Problem Y W X 205 70 + 80 + 60 Inconsistent

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

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

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

ACID Properties A transaction is a collection of actions with the following ACID properties:

ACID Properties A transaction is a collection of actions with the following ACID properties: • Atomicity: A transaction’s changes to the state are atomic – either all happen or non happen. • Consistency: A transaction is a correct transformation of the state. • Isolation: Even though transaction 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 (transaction’s effects are durable). 6

Transaction: Atomicity Property • A transaction is an atomic sequence of actions (e. g.

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!

Disk Writes as Atomic Actions (1) Atomicity of a disk write operation would mean

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

Disk Writes as Atomic Actions (2) 3. Duplexed write: Each data block is written

Disk Writes as Atomic Actions (2) 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 RAM 11 5 5 9

Disk Writes as Atomic Actions (3) Observation: • Even simple operations cannot simply be

Disk Writes as Atomic Actions (3) 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

Action Types • Unprotected Actions: They lack all of the ACID properties except for

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 11 ATM

Higher-Level Protected Actions (Transactions) Since unprotected actions can be undone, they can be included

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. Transaction (protected) Begin Work unproted action Commit Work 12

Handling Real Actions The higher-level operation must make sure that the real actions are

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

Concurrency Control • Concurrent execution of user programs is essential for good DBMS performance.

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.

Scheduling Concurrent Transactions DBMS ensures that execution of {T 1, . . . ,

Scheduling Concurrent Transactions DBMS ensures that execution of {T 1, . . . , Tn} is equivalent to some serial execution T 1, . . . 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) I have the lock I wait T 2 W X R T 1 I can lock now T 2 W I am done X T 1

Number of locks acquired 2 PL Locking Protocol 2 PL Strict 2 PL What

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

Deadlock I wait for X X W 3 T 2 I have the lock

Deadlock I wait for X X W 3 T 2 I have the lock on Y W 1 Y I have the lock on X R 2 T 1 W 4 I wait for Y A solution: T 1 or T 2 is aborted and restarted

Ensuring Atomicity • DBMS ensures atomicity (all-or-nothing property) even if system crashes in the

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!

A Flat Transaction Example: Debit/Credit (1) exec sql CREATE TABLE accounts( Aid NUMERIC(9), Bid

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

A Flat Transaction Example: Debit/Credit (2) /***main program with the invocation environment */ /*

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 */ /* start the flat transaction */ exec sql BEGIN WORK; } Abalance = Do. Debit. Credit(Bid, Tid, Aid, delta); /* invoke transaction program */ send output msg; /* send response to terminal */ exec sql COMMIT WORK; /* successful end of transaction */ /* */ 20

A Flat Transaction Example: Debit/Credit (3) /* subroutine for doing the database accesses defined

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

ROLLBACK Statements DCApplication() /* */ { receive input message; /* deal with request messages

ROLLBACK Statements DCApplication() /* */ { receive input message; /* deal with request messages */ exec sql BEGIN WORK; /* start the flat transaction */ Abalance = Do. Debit. Credit(Bid, Tid, Aid, delta); /* invoke transaction program if (Abalance < 0 && delta < 0) { exec sql ROLLABCK WORK; } else */ /* check if it a debit and account overdrawn */ /* if so: don’t do it */ /* 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. 22

Limitations of Flat Transactions (1) A one-layer control structure will not be able to

Limitations of Flat Transactions (1) A one-layer control structure will not be able to model application structures that are significantly more complex. Example 1: 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. 23

Limitations of Flat Transactions (2) Example 2: Bulk Updates • At the end of

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

SPHERES OF CONTROL Spheres of Control (So. C) are based on a hierarchy of

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

Two Varieties of SPHERES OF CONTROL § One is statically established by structuring the

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 So. Cs on shared data, which cannot be committed yet. Predefined hierarchy of functions Dynamic interaction S A 1 B 1 Dynamically created for controlling the commitment of A 1 B 2 A 2 B 3 C 1 C 2 D D is a noncommitable data item. B 4 B 5 26

Dependency in Transactions • Structural Dependency • Dynamic Dependency 27

Dependency in Transactions • Structural Dependency • Dynamic Dependency 27

Structural Dependencies in Transaction Models Structural dependencies reflect the hierarchical organization of the system

Structural Dependencies in Transaction Models Structural dependencies reflect the hierarchical organization of the system into ADTs of increasing complexity. Example: The commit of B 3 depends on the commit of A 2. The reason is that A 2 appears as an atomic action to the outside. S A 1 B 2 C 1 A 2 B 3 B 4 C 2 B 5 D D is a noncommitable data item. 28

Dynamic Dependencies in Transaction Models Dynamic dependencies arise from the use of shared data.

Dynamic Dependencies in Transaction Models Dynamic dependencies arise from the use of shared data. S is dynamically created for controlling the commitment of A 1 Example: A 2 depends on A 1 in the sense that A 2 can commit only if A 1 does. S A 1 B 2 C 1 A 2 B 3 B 4 C 2 B 5 D D is a noncommitable data item. 29

Dynamic Behavior of So. C (1) 1. Beginning of scenario: At time t, a

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

Dynamic Behavior of So. C (2) 2. Tracing dependencies backward in time: • A

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

Dynamic Behavior of So. C (3) 3. Again going forward in time to do

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

State-Transition Diagram for a Flat Transaction – Flat transactions can be described as state

State-Transition Diagram for a Flat Transaction – Flat transactions can be described as state machines, NULL terminate committed BEGIN WORK IT K M R M CO WO active ROLLBACK WORK term inat e aborted – 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 So. C model for describing transaction models. 33

Describing Transaction Models Transaction models are distinguished by different sets of rules • for

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

Describing Transaction Models - Example • The event ROLLBACK WORK for atomic action B

Describing Transaction Models - Example • The event ROLLBACK WORK for atomic action B 3 can be triggered by the program running inside B 3, or by the abort of A 2 • The signal COMMIT WORK for B 3 is not sufficient to actually commit B 3, that can only happen if the condition “A 2 is ready to commit” is also true. S A 1 B 2 C 1 A 2 B 3 B 4 C 2 B 5 D Dynamically created for controlling the commitment of A 1 D is a noncommitable data item. 35

Graphical Notation for Transaction Models Begin Signal entries for the atomic action to perform

Graphical Notation for Transaction Models Begin Signal entries for the atomic action to perform a state transition Commit Abort A B C T Eternally unique identifier of the atomic action A Aborted State indicator of the action’s outcome C Committed 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) 36 final outcomes of the action.

Describing Flat Transactions A B C A System C A B C System C

Describing Flat Transactions A B C A System C A B C System C A C Trigger A B C A T A B C T C a) Transaction T is active: An abort of the system transaction will cause it to roll back, too. A Final state C b) Termination scenario 1: Transaction T has committed; all of its event ports are deactivated; the final state is highlighted. A B C T A C 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 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. 37

Defining Transaction Models by Rules (1) <rule id>: <precodition> → <rule modifier list>, <signal

Defining Transaction Models by Rules (1) <rule id>: <precodition> → <rule modifier list>, <signal list>, <state transition>. • The <rule id> specifies the signal port this rule is for Wait Signal port SA(T 1) • This rule is not activated until the preconditions are fulfilled Precondition 38

Defining Transaction Models by Rules (2) <rule id>: <precodition> → <rule modifier list>, <signal

Defining Transaction Models by Rules (2) <rule id>: <precodition> → <rule modifier list>, <signal list>, <state transition>. A B C System A C Activate A B C T A C SA(T) is a signal from SA(System) • <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). • <state transition> is essentially redundant, but it is kept for clarity. 39

Defining Transaction Models by Rules (3) <rule id>: <precodition> → <rule modifier list>, <signal

Defining Transaction Models by Rules (3) <rule id>: <precodition> → <rule modifier list>, <signal list>, <state transition>. A B C System A C Trigger A B C T A C • <rule modifier list> is used to introduce transactional dependencies as they are needed, or to delete dependencies that have become obsolete. <rule modifier> : : = +(<rule id>|<signal>) +(SA(system)|S A(T)) adds transactional <rule modifier> : : = - (<rule id>|<signal>) dependency ! <rule modifier> : : = delete(x), x is an atomic action. <signal> is the name of the rule to be activated by the signal 40

<rule modifier list> vs <signal list> <rule id>: <precodition> → <rule modifier list>, <signal

<rule modifier list> vs <signal list> <rule id>: <precodition> → <rule modifier list>, <signal list>, <state transition>. Signal port SA(T 1) SB(T 2) A B C A T 1 A B C A B T 1 C A C +(SA(T 1)|SA(T 2)) Trigger A B C Signal T 2 port SAA(T 1) C 41

Rules for Flat Transactions A B C A System A B C System C

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

Rules for Flat Transactions A B C A System A B C System C

Rules for Flat Transactions A B C A System A B C System C A C Trigger A B C A T A B A Remove the rules C B System A A T C A a) Transaction T is active: Abort of the system transaction will cause it to roll back, too. SB(T): SA(T): SC(T): C C B Remove the rules C T C b) Termination scenario 1: Transaction T has committed; all of its event pots are deactivated; the final state is highlighted. A C c) Termination scenario 2: Same as scenario 1, but T assumes the final abort sate. +(SA(system)|SA(T)), , BEGIN WORK (delete(SB(T)), delete(SC(T))), , ROLLBACK WORK (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. 43

Flat Transaction with Savepoints (1) BEGIN WORK (get transaction started) implicit SAVE WORK: 1

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

Flat Transaction with Savepoints (2) • A savepoint is established by invoking the SAVE

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

Unstructured Program BEGIN WORK (get transaction started) implicit SAVE WORK: 1 Action There is

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

Graphical Representation of the Savepoint Model (1) • The basic idea is to regard

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 So. C. • The completion of a savepoint creates a new atomic action that is dependent on its predecessor. A B C System A C Trigger A B C S 1 A C Trigger A B Dependency C S 2 A Trigger C So. C 47

Graphical Representation of the Savepoint Model (2) A B C A System A C

Graphical Representation of the Savepoint Model (2) A B C A System A C B A A C B B A A C A B A C B A Trigger b) Next savepoint, S 2, has been taken. C A Trigger B C S 2 C A C Trigger A Forbidden state or event C Trigger S 2 A C S 1 Trigger a) Transaction has started, establishing savepoint 1. C Trigger S 1 C C System Trigger S 1 A C System Trigger A B c) Next savepoint, S 3, has been taken. B C S 3 A C 48

The Rules for Savepoint Model First Savepoint, S 1: SB(S 1) : +(SA(system)|SA (S

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

The Rules for Savepoint Model First Savepoint, S 1: SB(S 1) : +(SA(system)|SA (S

The Rules for Savepoint Model First Savepoint, S 1: SB(S 1) : +(SA(system)|SA (S 1)), , BEGIN WORK SA(ST) : (ST<S 1) , , ROLLBACK WORK SC(S 1) : , , COMMIT WORK SS(S 1) : +(SA(S 1)|SA (S 2)), SB(S 2). Intermediate Savepoint, Sn: SB(Sn) : , , BEGIN WORK SA(ST) : (ST<Sn) , SA(Sn-1), ROLLBACK WORK SC(Sn) : , SC(Sn-1), COMMIT WORK SS(Sn) : +(SA(Sn)|SA (Sn+1)), SB(Sn+1). ST : The target savepoint of the rollback 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. 50

Limitation of Savepoint • All savepoints perish in case of a system crash •

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

Persistent Savepoints Restart Strategy • The last unfinished savepoint interval is roll back, but

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

CHAINED TRANSACTIONS • Chained transactions are modeled by a sequence of atomic actions, executed

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

Rules for Chained Transactions A B C A B System A C C B

Rules for Chained Transactions A B C A B System A C C B A C 2 C A B C A C 1 C A Trigger C C Trigger System A B C 1 Trigger A A 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. B C C 2 C A C c) The first transaction in the chain has been committed; the second one got started as part of commit of C 1. Now the second one is structurally dependent on “system”. SB(Cn): (delete(SB(Cn)), +(SA(system)|SA (Cn))), , BEGIN WORK SA(Cn): (delete(SA(Cn)), delete(SC(Cn))), , ROLLBACK WORK SC(Cn): (delete(SA(Cn)), delete(SC(Cn))), SB(Cn+1), COMMIT WORK 54

Chained Transaction v. s. Savepoints • Since the chaining step irrevocably completes a transaction,

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. Committed transaction A B C A System A C Trigger Active transaction B C A C 1 A B C C 2 C A Trigger C 55

Restart Processing in Chained Transactions From the application’s point of view, the whole chain

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. A B C A System A B C commit C 1 C A Trigger C A B C C 2 A C 56

Restart Processing in Chained Transactions From the application’s point of view, the whole chain

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. A B C A B System A C 1’ is the new instance of C 1 C A Trigger C New instance of C 1 starts over again Trigger C Trigger A B C C 2 A B C A Restart A C B C 1’ A A C C Trigger C Restart C 2 57

Chained Transaction vs. Savepoints Persistent Savepoints Chained Transaction Rollback Any previous savepoint Limited to

Chained Transaction vs. 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 58

Nested Transactions • A nested transaction is a tree of subtransactions. • A subtransaction

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

Nested Transaction - SOC T 2111 Its commit takes effect only if the parent

Nested Transaction - SOC T 2111 Its commit takes effect only if the parent transaction commits T 211 Any subtransaction can finally commit only if the root transaction commits T 2112 T 2121 T 2122 T 221 T 222 60

Behavior of Nested Transactions Commit & Rollback Commit rule: The commit of a subtransaction

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

Behavior of Nested Transactions Visibility Rule • All changes done by a subtransaction become

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

Rules for Nested Transactions System A B A C A C B A C

Rules for Nested Transactions System A B A C A C B A C C Trigger a) Root transaction T is running. C T T T A System Trigger Wait A A B C T 1 A b) Subtransaction T 1 has been started. +(SA(Tk)|SA (Tkn)), , BEGIN WORK SA(Tkn): , , ROLLBACK WORK Sc(Tkn): C(Tk) , , COMMIT WORK C C A C Wait A SB(Tkn): B T 2 C Tk is the parent of Tkn A B C T 11 A Wait C c) T 1 has created subtransaction T 11, and after that the root transaction starts another subtransaction, T 2. Note: The arrows labeled “wait” correspond to the precondition 63 clauses in the rules.

Using Nested Transaction Strong relationship between modularization in software engineering and the nested transaction

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 64

Transactional C • Transactional C (IBM) is a programming language supported by the Encina

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

Emulating Nested Transaction by Savepoints Savepoint generated at beginning of each subtransaction Rollback to

Emulating Nested Transaction by Savepoints Savepoint generated at beginning of each subtransaction Rollback to savepoint s 121 has the effect of aborting Tk 121 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. 66

Limitations of the Emulation of Nesting by Savepoints (1) • The emulations is limited

Limitations of the Emulation of Nesting by Savepoints (1) • 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. – Reestablishing the state of the savepoint associated with a transaction can affect arbitrary subtransactions, rather then only the subtree of the aborting subtransaction. 67

Limitations of the Emulation of Nesting by Savepoints (2) • In nested transactions, subtransactions

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

Distributed Transactions • A distributed transaction is typically a flat transaction that runs in

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

Multi-Level Transactions A B System A A C B A A A C N

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

Multi-Level Transactions vs Nested Transactions Nested transaction T 2 does not have the durability

Multi-Level Transactions vs Nested Transactions Nested transaction T 2 does not have the durability property System A B C Multi-level transaction A A C C Trigger A C System T A B Trigger B C A T 1 A Wait B C T 2 C A Precommit: N has the durability property A B C T A C C Trigger Wait A A B C T 11 A C A A B C CN N Wait C B C A C 71

Rules for Multi-Level Transactions • Rules for subtransaction N: SB(N): , , BEGIN WORK

Rules for Multi-Level Transactions • Rules for subtransaction N: SB(N): , , BEGIN WORK SA(N): , , ROLLBACK WORK SC(N): +(SA(T)|SB(CN)), SB(CN), COMMIT WORK • Rules for the compensating translation CN: SB(CN): +(SC(restart)|SB(CN’)), , BEGIN WORK SA(CN): , SB(CN’), ROLLBACK WORK SC(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. 72

Advantage & Disadvantage of Multi-Level Transactions • Advantage: Multi-level transactions are nested transactions with

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

Summary: Transaction Models Support partial rollback Minimize lost work in case of system crash

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

Transaction Processing Context Programs frequently use contextual information in addition to the input parameters;

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’} 75

Transaction Processing Context Example exec sql declare cursor x select a, b, c from

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; a x do { exec sql fetch next x into : a, : b, : c; /* perfrom computation */ } while (sqlcode == 0); exec sql close cursor x; b c 3 7 9 12 16 21 : a d 10 10 10 : b : c Computation 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. 76

COMPUTE INTERESTS • If all accounts are updated within one transaction, there is no

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 77

COMPUTE INTERESTS Compute. Interest () { read (interest_rate); for (account_no = 1; account_no <=

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

DURABLE CONTEXT Context can be volatile or durable: – An end-of-file pointer is volatile

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 Compute. Interest transactions has gotten must be durable because the program (reinstantiated version) and the database are out of synch after a crash. 79

THE MINI BATCH (1) A solution to the Compute. Interest problem: – Executing a

THE MINI BATCH (1) A solution to the Compute. Interest 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. | A mini batch | Accounts 0 1 … n-1 n n+1 … i Last_account_done BATCHCONTEXT PROCESS n-2 Database stepsize … Note: Isolation is guaranteed only for the tuples the step transaction is accessing. 80

THE MINI BATCH (2) while (last_account_done < max_account_no) { EXEC SQL BEGIN WORK; /*

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) WHERE account_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 */ } Accounts | A mini batch | 0 1 … Last_account_done n-1 n n+1 … i stepsize Last_account_done + stepsize … 81

Mini-batch is not Strictly Atomic • Mini-batch does not achieve strict atomicity for the

Mini-batch is not Strictly Atomic • Mini-batch does not achieve strict atomicity for the entire computation • Nevertheless, it is suitable for the Compute. Interest 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 82

LONG-LIVED TRANSACTIONS General Requirements 1. Minimize lost work: It must be possible to split

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

SAGAS Saga is an extension of the notion of chained transactions. 1. It defines

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

SAGAS - Properties • Commit case: S 1, S 2, …, Si, … ,

SAGAS - Properties • Commit case: S 1, S 2, …, Si, … , Sn-1, Sn • Rollback scenario: S 1, S 2, …, Sj(abort), CSj-1… , CS 2, CS 1 A A B C System C Trigger A A B S 1 C C A B S 2 A C A Trigger A A B C CS 1 C Added to Chained Transaction A B C CS 2 A C B S 3 C C A backward chain of compensating transactions is established as the original chain proceeds in a forward direction. Trigger 85

COOPERATING TRANSACTIONS (1) Cooperative transactions allow for explicit interactions among collaborating users on shared

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

COOPERATING TRANSACTIONS (2) • Prerelease upon request: – object X is isolated until the

COOPERATING TRANSACTIONS (2) • Prerelease upon request: – object X is isolated until the surrounding So. C 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 So. C. 87

Summary: Transaction Models Flat transaction Can rollback to a savepoint Savepoints Reestablish last savepoint

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

Summary: Transaction Models Flat transaction Persistent savepoints Savepoints Nested Transaction Supports modularization in software

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

Summary: Transaction Models I know how Transaction 2 uses “X” Transaction 1 X X

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