Week 10-1 Transactions
Week 10-1 Transactions
Distributed Systems
Transactions
Coulouris Chapter 16,17
Transaction
ITEC 801
Transactions
Transactions
Transaction
ITEC 801
Transactions
Atomic
Operations
atma is the Sanskrit word for the indivisible soul, expressing unity
ITEC 801
Transactions
Tina Turner playing The Acid Queen in the Whos movie Tommy.
ACID Properties
6JG#EKF3WGGP
ITEC 801
Transactions
ACID Properties
Atomicity
Consistency
Isolation
Durability
ITEC 801
Transactions
All or Nothing
Either all steps of a transaction are done or none.
A transaction is a whole single unit of work. It cannot be divided and its
substeps cannot be done alone or in part.
In a distributed system, all changes must be made at all participants.
Atomicity
All or Nothing
All changes to system state are
completed or none are done.
ITEC 801
Transactions
Consistency
A transaction is a correct
transformation of state
ITEC 801
Transactions
This means that for any transaction T, it appears that each of the other
Isolation
ITEC 801
Transactions
10
Once a transaction completes, its eects are stored and survive any
failures.
Durability
ITEC 801
Transactions
11
pH Test?
ACID is controversial
C.J. Date not strong enough for databases
C.J. Date Database Systems Eighth Edition
ITEC 801
Transactions
12
Transactions
13
ITEC 801
Transactions
14
Transactions
The isolation requirement would be met by executing
transaction serially one at a time
Not very efficient
Servers should maximise concurrency
ITEC 801
Transactions
15
Co-ordinators
Each transaction created and maintained by a coordinator
Each transaction gets an identifier (TID)
TID may be implicitly or explicitly associated with each
operation
When client invokes close_transaction co-ordinator saves
result of transaction
ITEC 801
Transactions
16
ITEC 801
Transactions
17
Aborted by client
Aborted by server
openTransaction
openTransaction
openTransaction
operation
operation
operation
operation
operation
operation
server aborts
transaction
operation
operation
closeTransaction
abortTransaction
ITEC 801
operation ERROR
reported to client
Transactions
18
Example
Initial balances
A $100
B $200
C $300
ITEC 801
Transactions
19
Transaction U
balance := b.balance
b.set_balance (balance*1.1)
a.withdraw (balance/10)
balance = b.balance
b.set_balance (balance*1.1)
c.withdraw (balance/10)
balance = b.balance
b.set_balance (balance*1.1)
a.withdraw (balance/10)
ITEC 801
$200
balance = b.balance
$200
b.set_balance (balance*1.1)
$220
c.withdraw (balance/10)
$280
$220
$80
Transactions
20
Transaction W
branch.total
$100
total = a.balance
$100
$300
ITEC 801
$300
Transactions
21
Serial Equivalence
ITEC 801
Transactions
22
Transaction U
balance = b.balance
b.set_balance (balance * 1.1)
a.withdraw (balance/10)
balance = b.balance
b.set_balance (balance*1.1)
c.withdraw (balance/10)
balance = b.balance
$200
$220
$80
c.withdraw (balance/10)
ITEC 801
Transactions
$278
23
Conflicting Operations
A pair of operations conflicts when their combined effect
depends upon the order in which they are executed.
For two transactions to be serially equivalent it is
necessary and sufficient that all pairs of conflicting
operations of the two transactions be executed in the
same order at all of the objects they both access
ITEC 801
Transactions
24
read
No
read
write
Yes
write
write
Yes
Reason
Because the effect of a pair of read operations
does not depend on the order in which they are
executed
Because the effect of a read and a write operation
depends on the order of their execution
Because the effect of a pair of write operations
depends on the order of their execution
ITEC 801
Transactions
25
Concurrency Control
Serial equivalence is a criterion for concurrency control
protocols
Three main approaches
Locking
Optimistic concurrency control
Timestamp ordering
ITEC 801
Transactions
26
Locking
Coulouris 16.4
ITEC 801
27
Transactions
Locking
One way to achieve serial equivalence of transaction is
to serialize access to the involved objects
This can be done using locks
The server locks an object that is to be used by a client
transaction
If another client requests access to an already locked
object that other request is suspended until the object is
unlocked
ITEC 801
Transactions
28
Transaction U
balance = b.balance
b.set_balance (bal*1.1)
c.withdraw (bal/10)
Operations
Locks
Operations
open_transaction
bal = b.balance
lock B
b.set_balance (bal*1.1)
a.withdraw (bal/10)
lock A
close_transaction
open_transaction
bal = b.balance
Locks
waits for Ts
lock on B
unlock A , B
lock B
b.set_balance (bal*1.1)
c.withdraw (bal/10)
lock C
close_transaction
ITEC 801
unlock B, C
Transactions
29
Locks
Serial equivalence requires that all of a transactions
access to a particular object be serialized with respect to
accesses by other transaction
All pairs of conflicting operation of two transaction must be executed in
the same order
ITEC 801
Transactions
30
Two-Phase Locking
A growing phase during which locks are acquired
A shrinking phase during which locks are released
ITEC 801
Transactions
31
ITEC 801
Transactions
32
Granularity
Servers have many objects
Want to lock the minimum number of objects for each
transaction
You wouldnt want to lock all accounts to do an
operation on one
ITEC 801
Transactions
33
ITEC 801
Transactions
34
Lock compatibility
Lock requested
read
write
ITEC 801
none
OK
OK
read
OK
wait
write
wait
wait
Transactions
35
Transactions
36
Lock Implementation
Handled by a separate object in server called lock
manager
Will hold a set of locks, each associated with a particular
object
Identified of locked object
Transaction identifies of transactions currently holding the lock
Lock type
ITEC 801
Transactions
37
ITEC 801
Transactions
38
ITEC 801
Transactions
39
ITEC 801
Transactions
40
Optimistic Locking
Coulouris 16.5
ITEC 801
41
Transactions
ITEC 801
Transactions
42
ITEC 801
Transactions
43
Timestamps
Coulouris 16.6
ITEC 801
Transactions
44
Timestamp Ordering
Pessimistic like locking
However, timestamps will abort transaction on access
Locks will make process wait.
ITEC 801
Transactions
45
Transactions
ITEC 801
Transactions
46
Transactions
A transaction is a correct
transformation of state
ITEC 801
Transactions
47
Flat Transactions
ITEC 801
Transactions
48
There are other kinds of applications that dont fit flat transactions.
This is somewhat a research topic, but there are some examples
Non-Flat Transactions
ITEC 801
Transactions
49
Trip Planning
ITEC 801
Transactions
50
Bulk Updates
UG
ITEC 801
H!
Transactions
51
Spheres
of
Control
ITEC 801
Transactions
52
Spheres of Control
Research that formed basis
of transactions in 1970s
Spheres of Control system
uses a hierarchy of ADTs
ITEC 801
Transactions
53
Spheres of Control
and correct.
Thus a commit in a transaction allows its eects to be seen by the outside
world (other processes and transactions).
Prior to commitment, the results are not available.
ITEC 801
Transactions
54
Spheres of Control
Process Control: Constrains
dependencies on other processes
Process Atomicity: Amount of
processing that has a single identity
Process Commitment: Changes
made by a process are internal
until changes committed
ITEC 801
Transactions
55
The important point about spheres of control is that they can be nested
Spheres of Control
ITEC 801
Transactions
and chained.
When an inner sphere completes, its results are released to the next outer
sphere, but not beyond.
Thus when an inner sphere commits, those results are only available to the
outer world if the outer sphere commits.
This is important to understand transactions beyond flat transactions.
56
Distributed
Transactions
Coulouris Chapter 17
ITEC 801
57
Transactions
Distributed Transactions
Previous section assumed that access to all the objects
that the transactions were dealing were stored on one
server
In the case of locking, that one server managed all the
locks
This is not always true in practice and we need to
consider distributed transactions
ITEC 801
Transactions
58
Savepoints
ITEC 801
Transactions
59
Chained Transactions
ITEC 801
Transactions
60
Distributed transactions
Flat transaction
Nested transactions
M
T11
X
T
T
T
T
Client
Client
12
21
Y
P
ITEC 801
22
Transactions
61
ITEC 801
a.withdraw (10)
b.withdraw (20)
Y
T
Z
T
T4
Transactions
c.deposit (10)
d.deposit (20)
62
Multi-Level Transactions
ITEC 801
Transactions
63
Distributed Transactions
Each server applies concurrency control as per what
weve already seen
But distributed transactions must also be serialized
globally
ITEC 801
Transactions
64
Co-ordinator
Atomicity requires either all servers commit results of the
transaction or they all abort
One server takes on the role of co-ordinator to achieve
this
ITEC 801
Transactions
65
Two-Phase
Commit
Coulouris 17.3
Tanenbaum 8.5
ITEC 801
Transactions
66
When two people get married, the celebrant asks crowd first if there is
anyone who objects to these two getting married. Celebrant then asks
bride if she takes this man, and asks groom if he takes this woman.
If everyone agrees, the celebrant commits the marriage and the two are
now married.
That is exactly what two-phase commit does. The master controller is the
celebrant, the other participants the resources that have participated in
the transaction.
ITEC 801
Transactions
67
openTransaction
closeTransaction
participant
A
a.withdraw (4)
join
BranchX
T
Client
participant
b.withdraw (T, 3)
T := open_transaction
a.withdraw (4)
c.deposit (4)
b.withdraw (3)
d.deposit (3)
close_transaction
B
join
BranchY
participant
ITEC 801
b.withdraw (3)
Transactions
c.deposit (4)
d.deposit (3)
BranchZ
68
ITEC 801
Transactions
69
Transactions
70
Transactions
71
Coordinator
step
status
prepared to commit
(waiting for votes)
committed
Participant
can_commit?
Yes
step
status
prepared to commit
commit
have_committed
(uncertain)
4
committed
done
ITEC 801
Transactions
72
ITEC 801
Transactions
73
Two-Phase Commit
Finite State Machines (FSM) in 2PC
74
Likewise, the coordinator can be blocked in state WAIT, waiting for the
votes of each participant. If not all votes have been collected after a
certain period of time, the coordinator should vote for an abort as well
and subsequently send global abort to all participants. Finally, a
participant can be blocked in state READY, waiting for the global vote
as sent by the coordinator. If that message is not received within a
Two-Phase Commit
Transactions
75
Two-Phase Commit
...
Outline of the steps taken by the coordinator in a twophase commit protocol.
ITEC 801
Transactions
76
Now suppose Q is in state INIT: This situation can occur when the
coordinator has sent a vote request to all participants but this message
has not reached Q. In other words, the coordinator has crashed while
multicasting vote request. In this case, it is safe to abort the
transaction, both P and Q can make a transition to ABORT.
Coordinator starts by sending a multicast (vote request) to all
participants in order to collect their votes. It subsequently records that
it is entering the WAIT state, after which it waits for incoming votes
from participants. If not all votes have been collected but no more
votes are received within a given time interval prescribed in advance,
the coordinator assumes that one or more participants have failed.
Consequently it must abort the transaction and multicast a global
abort message to all participants. If no failures occur, the coordinator
will eventually have collected all votes. If all participants as well as the
coordinator vote to commit, global commit is first logged and
subsequently sent to all processes. Otherwise, coordinator multicasts a
global abort.
Two-Phase Commit
ITEC 801
Transactions
77
First the process waits for a vote request from the coordinator. Note
that this waiting can be done by a separate thread running in the
processs address space. If no message comes in the transaction is
simply aborted. Apparently, the coordinator had failed.
Two-Phase Commit
(a) The steps taken by a participant process in 2PC.
ITEC 801
Transactions
78
After receiving the vote request, the participant may decide to vote for
commiting the transaction for which it records its decision in the local
log and then informs the coordinator by sending a vote commit
message. The participant must then wait for the global decision.
Assuming this decision comes on time, it is simply written to the local
log, after which it can be carried out. However if it times out, it
executes a termination protocol by first multicasting a decision request
message to all other processes., after which it blocks waiting for a
Two-Phase Commit
(b) The steps for handling incoming decision requests..
ITEC 801
Transactions