05distributed Transaction Processinglec5 (Feb 28)
05distributed Transaction Processinglec5 (Feb 28)
Transaction Concept
2
Required Properties of a Transaction
3
Required Properties of a Transaction
▪ Durability requirement — once the user has been notified that the
transaction has completed (i.e., the transfer of the $50 has taken place),
the updates to the database by the transaction must persist even if there
are software or hardware failures.
4
Required Properties of a Transaction (Cont.)
▪ Transaction to transfer $50 from account A to account B:
1. read(A)
2. A := A – 50
3. write(A)
4. read(B)
5. B := B + 50
6. write(B)
5
Required Properties of a Transaction (Cont.)
T1 T2
1. read(A)
2. A := A – 50
3. write(A)
read(A), read(B), print(A+B)
4. read(B)
5. B := B + 50
6. write(B
▪ Isolation can be ensured trivially by running transactions serially
• That is, one after the other.
6
Required Properties of a Transaction (Cont.)
T1 T2
1. read(A)
2. A := A – 50 However, executing
3. write(A) multiple transactions
read(A), read(B), print(A+B) concurrently has
significant benefits.
4. read(B)
5. B := B + 50
6. write(B
▪ Isolation can be ensured trivially by running transactions serially
• That is, one after the other.
7
Transaction State
▪ Active – the initial state; the transaction stays in this state while it is
executing
▪ Partially committed – after the final statement has been executed.
▪ Failed -- after the discovery that normal execution can no longer proceed.
▪ Aborted – after the transaction has been rolled back and the database
restored to its state prior to the start of the transaction. Two options after it
has been aborted:
• Restart the transaction
▪ can be done only if no internal logical error
• Kill the transaction
▪ Committed – after successful completion.
8
Transaction State (Cont.)
9
Distributed Transactions
▪ Local transactions
• Access/update data at only one database
▪ Global transactions
• Access/update data at more than one database
▪ Key issue: how to ensure ACID properties for transactions in a system with
global transactions spanning multiple database
10
Distributed Transactions
11
Distributed Transactions
Each transaction coordinator is responsible
for:
▪ Starting the execution of transactions
that originate at the site.
▪ Distributing subtransactions at
appropriate sites for execution.
▪ Coordinating the termination of each
Site
transaction that originates at the site 4TC4
• transaction must be committed at
all sites or aborted at all sites. Site TM Site
1TC1 4 3TC3
TM TM
1 3
Site
2TC2
TM
2
12
Distributed Transactions
Site TM Site
Question 5-1 1TC1 4 3TC3
Site 3 has initiated Transaction T to transfer
Tk. 1000 from account P at site 4 to account TM TM
Q at site 2. Discuss the responsibilities of 1 3
concerned transaction coordinator and
transaction managers.
Site
2TC2
TM
2
13
System Failure Modes
14
Coordinator
Site 1 Site 2
Transaction: Transfer Tk. 5000 from account A to
TC 1 TC 2 account B. A in site 2 and B in site 3. Transaction
has been initiated from site 1.
TM1 1 TM 2
LOG LOG
15
Commit Protocols
▪ Consensus protocols solve a more general problem, but can be used for
atomic commit
▪ The protocols we study all assume fail-stop model – failed sites simply stop
working, and do not cause any other harm, such as sending incorrect
messages to other sites.
16
Two Phase Commit Protocol (2PC)
▪ Execution of the protocol is initiated by the coordinator after the last step of
the transaction has been reached.
▪ The protocol involves all the local sites at which the transaction executed
17
Coordinator <Ready T>
Site 2
Phase 1: Obtaining a Decision
Site 1
18
Coordinator <Ready T>
Site 2
Site 1 Phase 2: Recording the Decision
TC 1 TC 2
<Prepare Transaction COMMIT by TC1
T> TM1 2
TM1 1
• T can be committed of TC1 received a
<commit
T>
<ready T> message from all the
<Prepar <ready participating sites.
e T> T> • Coordinator adds a decision record,
<comm
<Prepare <commit T> to the log and forces record
it T>
T> Site 3 <Ready T> onto stable storage.
• Once the record stable storage it is
<commit TC 3
T> irrevocable (even if failures occur)
TM1 3 • Coordinator sends a message to commit
to each participant informing it of the
decision.
<ready • Participants take appropriate action
T>
locally.
Question 5-3: Write down the activities of the coordinator in phase 1 and phase 2 for the case
when all the sites including coordinator wants to commit
19
Coordinator <Ready T>
Site 2
Site 1 Phase 2: Recording the Decision
TC 1 TC 2 Transaction ABORT by TC1
<Prepare
T> TM1 2
TM1 1 • Even after receiving <ready T> messages
<abort T> by TC1 from all the participating sites, TC1
<Prepar has the authority to abort the transaction T.
<ready
e T> • Coordinator adds a decision record, <abort
T>
<abort T> to the log and forces record onto stable
T> <Prepare
T> Site 3 <Ready T>
storage.
• Once the record stable storage it is
<abort T> TC 3 irrevocable.
• Coordinator sends a message to abort to
TM1 3
each participant informing it of the decision.
• Participants take appropriate action locally.
<ready
T>
20
Coordinator <Ready T>
Site 2
Phase 1: Obtaining a Decision
Site 1
21
Coordinator <Ready T>
Site 2
Site 1 Phase 2: Recording the Decision
TC 1 TC 2
<Prepare Transaction must be aborted by TC1
T> TM1 2
TM1 1
• T must be aborted by TC1 if it received a
<abort T>
<no T> message from any of the
<Prepar <ready participating sites.
e T> T> • Coordinator adds a decision record,
<abort
<Prepare <abortT> to the log and forces record
T>
T> Site 3 <abort T> onto stable storage.
• Once the record stable storage it is
<abort T> TC 3
irrevocable (even if failures occur)
TM1 3 • Coordinator sends a message to abort to
each participant informing it of the
decision.
<No • Participants take appropriate action
T>
locally.
Discussion 5-4: Write down the activities of the site 3 in phase 1 and coordinator in phase 2 in
the case when site 3 do not want to commit.
22
Two-Phase Commit Protocol
23
Handling of Failures - Site Failure
24
Handling of Failures - Site Failure
Site
Site 2 was failed 4TC4
Case 4: The log contains no control records
concerning T implies that S2 failed before Site TM Site
responding to the prepare T message from TC1. 1TC1 4 3TC3
TM TM
Since the failure of S2 precludes the sending of such 1 3
a response TC1 must abort T
TM
2
25
Handling of Failures - Coordinator Failure
If coordinator fails while the commit protocol for
T is executing then participating sites must
decide on T’s fate
Site
4TC4
Case 1: If an active site contains a <commit T>
record in its log, then T must be committed. Site TM Site
1TC1 4 3TC3
Case 2: If an active site contains an <abort T>
TM TM
record in its log, then T must be aborted. 1 3
26
Handling of Failures - Coordinator Failure
If coordinator fails while the commit protocol for
T is executing then participating sites must
decide on T’s fate
Site
4TC4
Case 4: If none of the above cases holds, then all
active sites must have a <ready T> record in their Site TM Site
logs, but no additional control records (such as 1TC1 4 3TC3
<abort T> or <commit T>). In this case active sites
must wait for Ci to recover, to find decision. TM TM
1 3
TM
Question 5-5: Write a discussion on coordinator failure 2
cases and blocking problem
27
Recovery and Concurrency Control
In-doubt transactions: When a failed site is recovered, the transactions those have a
<ready T>, but neither a <commit T>, nor an <abort T> log record.
The recovering site must determine the commit / abort status of In-doubt transactions by
contacting other sites;
o Because after recovery, all active transactions must have redo or undo based on
Commit / abort status
o In case of coordinator failure, site cannot decide commit/abort and indefinite wait
for coordinator
28
Recovery and Concurrency Control
▪ For faster recovery, Recovery algorithms can note lock information in the
log.
• Instead of <ready T>, write out <ready T, L> L = list of locks held by T
when the log is written (read locks can be omitted).
▪ A Transaction T5 has Read lock (A), Write Lock (D) and Write Lock
( E) at site 3. T5 has Write Lock ( G) at site 2. T5 is initiated by site 1.
The data item A, D and E are in site 3. Site 3 wants to commit T5.
What log record will be written by site 3? How will it be used for
recovery?
▪ <ready T5, {D,E}>
• For every in-doubt transaction T, all the locks noted in the
<ready T, L> log record are reacquired.
▪ After lock reacquisition, transaction processing can resume; the commit or
rollback of in-doubt transactions is performed concurrently with the execution
of new transactions.
29
Avoiding Blocking Using Consensus
TM TM
1 3
Just after that, site 1 failed.
▪ Site 2, 3 and 4 are blocked for T1 and wait
until site 1 is restored. Site
2TC2
▪ Idea: involve multiple nodes in decision
process, so failure of a few nodes does not
TM
cause blocking as long as majority don’t fail 2
30
Avoiding Blocking Using Consensus
TM
2
31
Using Consensus to Avoid Blocking
32
Using Consensus to Avoid Blocking
33
Using Consensus to Avoid Blocking
TM
2
34
Distributed Transactions via Persistent Messaging
35
Distributed DBMS Environment
Organization 1
Site 1
Site 2
Site 5
Communication
Network
Organization 2
Site 4 Site 3
36
Persistent Messaging
Atomicity issue
Once transaction sending a message is
committed, message must guaranteed to
be delivered
37
Error Conditions with Persistent Messaging
▪ Code to handle messages has to take care of variety of failure situations (even
assuming guaranteed message delivery)
▪ E.g., if destination account does not exist, failure message must be sent back to source
site
▪ When failure message is received from destination site, or destination site itself does
not exist, money must be deposited back in source account
▪ Problem if source account has been closed
• get humans to take care of problem
38
Persistent Messaging Implementation
Problem to solve 4-2: There is an account A under organization 1 and an account B under
organization 2. Transfer Tk. 5000 from account A to account B using persistent messaging
protocol.
39
Persistent Messaging (Cont.)
▪ Receiving site may get duplicate messages after a very long delay
• Stored messages older than the cutoff can be deleted at receiving site
40
Persistent Messaging (Cont.)
41