Docsity

Distributed Transactions: Timestamp Ordering and Two-Phase Commit in Distributed Systems -, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Distributed Transactions: Timestamp Ordering and Two-Phase Commit in Distributed Systems - and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

CS 425 / ECE 428

Distributed Systems

Lecture 21

Distributed Transactions

Chapter 14

sayan mitra

summary so far

• transaction properties

• implementing transactions

– locks

– exclusive locks (read-write)

– 2-version locks (read-write-commit)

– optimistic concurrency control

  • work-validate-update
  • timestamp-based

Optimistic concurrency control:

Timestamp Ordering

Don’t use locks, detect inconsistencies at individual operations (and abort), rather than at commit point.

Each transaction is assigned a unique timestamp value when it starts (@ openTransaction()). The timestamp defines its position in the time sequence of transactions.

A transaction’s “write” request is valid if the object has

been “read” and “written” only by earlier (timestamp)
transactions.

 A transaction’s “read” request is valid if the object has

been “written” only by earlier trans.

 Trans. with invalid operations are aborted

 This can never cause deadlocks (why?)

Transaction T { t = openTransaction(); x.write(t,2); closeTransaction(t); }

Timestamp Ordering (One Version Solution)

Assign timestamp

Timestamp Ordering ( 2 Version Solution)

  • Improves concurrency
  • Each object has
    • A write TS (of the committed version)
    • A set of tentative versions (per transaction). Each version has
      • A write TS and a read TS
    • A read TS (of the committed version) : represented by the maximum value of the tentative read TS set
  • For each object, write TS of committed version is smaller than write TS’s of its tentative versions.
  • Read TS of committed object is same as max of read TS’s
  • Whenever a transaction’s write operation is accepted, the server creates a new tentative version with write TS set to the transaction TS.
  • A transaction’s read operation is directed to the version with the maximum write TS less than the reading transaction’s TS. - The transaction’s TS is added to the list of the object’s read TS’s
  • When a transaction commits (@closeTransaction), TS values of tentative write versions become TS’s of corresponding committed objects

Operation Conflicts for Timestamp Ordering

Rules Tc Ti

  1. write read
  2. write write
  3. read write

Each read/write operation of a transaction is checked to see whether it conforms to the following rules.

The transaction that was done by some previous operation whose timestamp indicates that it should be later than Tc

Tc=current Ti > Tc means Ti is later than Tc

Tc must not write an object that has been read by any Ti where Ti > Tc; this requires Tc >= the max. read TS of object.

Tc must not write an object that has been written by any Ti where Ti > Tc; this requires that Tc > write TS of committed object.

Tc must not read an object that has been written by any Ti where Ti > Tc; this requires that Tc > the write TS of committed obj.

Read Operations and Timestamps

(write-TS’s only shown)

(b) T 3 read

Time

read proceeds

Selected

T 2

Time

read proceeds (read T2’s version) Selected

T 2 T 4

Time

read waits

Selected

T 1 T 2

Time
Transaction
T 4 aborts

Key:

Tentative

Committed

Ti

Ti

object produced by transaction Ti (write timestampTi) T 1 < T 2 < T 3 < T 4

(a) T 3 read

(c) T 3 read (d) T^3 read

T3 > T2 & T2 committed, hence T3 read proceeds

T3 > T2 & T2 committed, hence T3 read proceeds

T3 > T2 & since T2 is tentative, T Must wait until T2 becomes committed or abort

T3 < T4 (violation of rule 3)

When to accept a Write

if ( Tc ≥ maximum read TS on D && Tc > write TS on committed version of D )

then perform write operation on tentative version of D with write TS Tc

else /* write is too late */

Abort transaction Tc

Rules 1 & 2:

Distributed Transactions

Chapter 14

Distributed Transactions

a transaction (flat or nested) that invokes

operations in several servers.

T

A

Y

Z

B

C

D

T

T

T

T

T

T

T

A

B

C

D

F

H

K

Flat Distributed Transaction Nested Distributed Transaction

X

Distributed banking transaction

..

BranchZ

BranchX

participant

participant

C D

Client

BranchY

B

A

join participant

join

join

T

a.withdraw(4);

c.deposit(4);

b.withdraw(3);

d.deposit(3);

openTransaction

b.withdraw(T, 3);

closeTransaction

T = openTransaction a.withdraw(4); c.deposit(4); b.withdraw(3); d.deposit(3); closeTransaction

Note: the coordinator is in one of the servers, e.g. BranchX

 Atomicity principle requires that either all

the distributed operations of a transaction

complete or all abort.

At some stage, client executes

closeTransaction(). Now, atomicity requires

that either all participants and the

coordinator commit or all abort.

What problem statement is this?

I. Atomic Commit Problem

Operations for Two-Phase Commit

Protocol

canCommit?(trans)-> Yes / No Call from coordinator to participant to ask whether it can commit a transaction. Participant replies with its vote. doCommit(trans) Call from coordinator to participant to tell participant to commit its part of a transaction. doAbort(trans) Call from coordinator to participant to tell participant to abort its part of a transaction.

haveCommitted(trans, participant) Call from participant to coordinator to confirm that it has committed the transaction. getDecision(trans) -> Yes / No Call from participant to coordinator to ask for the decision on a transaction after it has voted Yes but has still had no reply after some delay. Used to recover from server crash or delayed messages.

The two-phase commit protocol

Phase 1 (voting phase):

  1. coordinator sends a canCommit? request to each of the participant
  2. participant replies with Yes or No ; before voting Yes , it prepares to commit by saving objects in permanent storage; if vote is No, the participant aborts immediately

Phase 2 (completion according to outcome of vote):

  1. coordinator collects the votes (including its own). (a) if there are no failures and all the votes are Yes, coordinator decides to commit the transaction and sends a doCommit to each of the participants.

(b) otherwise coordinator decides to abort and sends doAbort to all participants that voted Yes.

  1. Participants that voted Yes are waiting for a doCommit or doAbort from the coordinator. When a participant receives one of these messages it acts accordingly and in the case of commit, makes a haveCommitted call as confirmation to the coordinator

Recall that server may crash