KEMBAR78
Transactions (Distributed computing) | PPT
Distributed Transactions Paul Krzyzanowski [email_address] [email_address] Distributed Systems Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
Atomic Transactions Transaction : an operation composed of a number of discrete steps. All the steps must be completed for the transaction to be  committed . The results are made permanent. Otherwise, the transaction is  aborted  and the state of the system reverts to what it was before the transaction started.
Example Buying a house: Make an offer Sign contract Deposit money in escrow Inspect the house Critical problems from inspection? Get a mortgage Have seller make repairs Commit : sign closing papers & transfer deed Abort : return escrow and revert to pre-purchase state All or nothing property
Basic Operations Transaction primitives: Begin transaction: mark the start of a transaction End transaction: mark the end of a transaction; try to commit Abort transaction: kill the transaction, restore old values Read/write data from files (or object stores): data will have to be restored if the transaction is aborted.
Another Example Book a flight from Newark, New Jersey to Inyokern, California. No non-stop flights are available: Transaction begin Reserve a seat for Newark to Denver (EWR->DEN) Reserve a seat for Denver to Los Angeles (DEN->LAX) Reserve a seat for Denver to Inyokern (LAX->IYK) Transaction end If there are no seatsavailable on the LAX->IYK leg of the journey, the transaction is aborted and reservations for (1) and (2) are undone.
Properties of transactions: ACID Atomic The transaction happens as a single indivisible action. Others do not see intermediate results.  All or nothing. Consistent If the system has invariants, they must hold after the transaction. E.g., total amount of money in all accounts must be the same before and after a “transfer funds” transaction. Isolated (Serializable) If transactions run at the same time, the final result must be the same as if they executed in some serial order. Durable Once a transaction commits, the results are made permanent. No failures after a commit will cause the results to revert.
Nested Transactions A top-level transaction may create subtransactions Problem: subtransactions may commit (results are durable) but the parent transaction may abort. One solution : private workspace Each subtransaction is given a private copy of every object it manipulates. On commit, the private copy displaces the parent’s copy (which may also be a private copy of the parent’s parent)
Implementing a private workspace  [PLAN A] Consider a Unix-like file system: duplicate the file’s index (i-node) Create new block when it gets modified by the transaction:  shadow block Index copy points to the shadow block On abort: remove shadow blocks and private index On commit: update parent’s index with private index attributes -> Block 329 -> Block 251 -> Block 783 -> Block 696 myfile Block 329 Block 251 Block 783 Block 696 attributes -> Block 329 -> Block 933 -> Block 783 -> Block 936 myfile’ Block 933 Block 936
Implementing a private workspace  [PLAN B] Use a  writeahead log  ( intentions list ) Keep a log in  stable storage Before making any changes to the object, write a record to the log identifying {transaction ID, object ID, old value, new value) If transaction commits: Write a  commit  record onto the log If transaction aborts Use log to back up to the original state:  rollback Stable storage : data persists even if the system or application crashes.
Distributed Transactions Transaction that updates data on two or more systems Challenge: handle machine, software, & network failures while preserving transaction integrity.
Distributed Transactions Each computer runs a  transaction manager Responsible for subtransactions on that system Transaction managers communicate with other transaction managers Performs  prepare ,  commit , and  abort  calls for subtransactions Each subtransaction must agree to commit changes before the transaction can complete
Two-phase commit protocol Goal : reliably agree to commit or abort a collection of subtransactions All processes in the transaction will agree to commit or abort. One transaction manager is elected as a coordinator – the rest are cohorts Assume  write-ahead log  in stable storage
Two-phase commit protocol: Phase 1 Get distributed agreement: the coordinator asked each cohort if it will commit or abort and received replies from each coordinator Coordinator Cohort Write  prepare to commit  to log Send  prepare to commit  message Work on transaction Wait for message from coordinator Wait for reply from  each cohort Receive  prepare  message. When ready, write  agree to commit  or  abort  to the log Send  agree  or  abort  reply
Two-phase commit protocol: Phase 2 Ask all cohorts to commit or abort and get everyone’s response that they’re done. Coordinator Cohort Write  commit  to log Wait for  commit  message Send  commit  or  abort Receive  commit  or  abort Wait for all cohorts to respond If a  commit  was received, write “ commit ” to the log, release all locks, update databases. If an  abort  was received, undo all changes Send  done  message Clean up all state. Done!
The end.

Transactions (Distributed computing)

  • 1.
    Distributed Transactions PaulKrzyzanowski [email_address] [email_address] Distributed Systems Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
  • 2.
    Atomic Transactions Transaction: an operation composed of a number of discrete steps. All the steps must be completed for the transaction to be committed . The results are made permanent. Otherwise, the transaction is aborted and the state of the system reverts to what it was before the transaction started.
  • 3.
    Example Buying ahouse: Make an offer Sign contract Deposit money in escrow Inspect the house Critical problems from inspection? Get a mortgage Have seller make repairs Commit : sign closing papers & transfer deed Abort : return escrow and revert to pre-purchase state All or nothing property
  • 4.
    Basic Operations Transactionprimitives: Begin transaction: mark the start of a transaction End transaction: mark the end of a transaction; try to commit Abort transaction: kill the transaction, restore old values Read/write data from files (or object stores): data will have to be restored if the transaction is aborted.
  • 5.
    Another Example Booka flight from Newark, New Jersey to Inyokern, California. No non-stop flights are available: Transaction begin Reserve a seat for Newark to Denver (EWR->DEN) Reserve a seat for Denver to Los Angeles (DEN->LAX) Reserve a seat for Denver to Inyokern (LAX->IYK) Transaction end If there are no seatsavailable on the LAX->IYK leg of the journey, the transaction is aborted and reservations for (1) and (2) are undone.
  • 6.
    Properties of transactions:ACID Atomic The transaction happens as a single indivisible action. Others do not see intermediate results. All or nothing. Consistent If the system has invariants, they must hold after the transaction. E.g., total amount of money in all accounts must be the same before and after a “transfer funds” transaction. Isolated (Serializable) If transactions run at the same time, the final result must be the same as if they executed in some serial order. Durable Once a transaction commits, the results are made permanent. No failures after a commit will cause the results to revert.
  • 7.
    Nested Transactions Atop-level transaction may create subtransactions Problem: subtransactions may commit (results are durable) but the parent transaction may abort. One solution : private workspace Each subtransaction is given a private copy of every object it manipulates. On commit, the private copy displaces the parent’s copy (which may also be a private copy of the parent’s parent)
  • 8.
    Implementing a privateworkspace [PLAN A] Consider a Unix-like file system: duplicate the file’s index (i-node) Create new block when it gets modified by the transaction: shadow block Index copy points to the shadow block On abort: remove shadow blocks and private index On commit: update parent’s index with private index attributes -> Block 329 -> Block 251 -> Block 783 -> Block 696 myfile Block 329 Block 251 Block 783 Block 696 attributes -> Block 329 -> Block 933 -> Block 783 -> Block 936 myfile’ Block 933 Block 936
  • 9.
    Implementing a privateworkspace [PLAN B] Use a writeahead log ( intentions list ) Keep a log in stable storage Before making any changes to the object, write a record to the log identifying {transaction ID, object ID, old value, new value) If transaction commits: Write a commit record onto the log If transaction aborts Use log to back up to the original state: rollback Stable storage : data persists even if the system or application crashes.
  • 10.
    Distributed Transactions Transactionthat updates data on two or more systems Challenge: handle machine, software, & network failures while preserving transaction integrity.
  • 11.
    Distributed Transactions Eachcomputer runs a transaction manager Responsible for subtransactions on that system Transaction managers communicate with other transaction managers Performs prepare , commit , and abort calls for subtransactions Each subtransaction must agree to commit changes before the transaction can complete
  • 12.
    Two-phase commit protocolGoal : reliably agree to commit or abort a collection of subtransactions All processes in the transaction will agree to commit or abort. One transaction manager is elected as a coordinator – the rest are cohorts Assume write-ahead log in stable storage
  • 13.
    Two-phase commit protocol:Phase 1 Get distributed agreement: the coordinator asked each cohort if it will commit or abort and received replies from each coordinator Coordinator Cohort Write prepare to commit to log Send prepare to commit message Work on transaction Wait for message from coordinator Wait for reply from each cohort Receive prepare message. When ready, write agree to commit or abort to the log Send agree or abort reply
  • 14.
    Two-phase commit protocol:Phase 2 Ask all cohorts to commit or abort and get everyone’s response that they’re done. Coordinator Cohort Write commit to log Wait for commit message Send commit or abort Receive commit or abort Wait for all cohorts to respond If a commit was received, write “ commit ” to the log, release all locks, update databases. If an abort was received, undo all changes Send done message Clean up all state. Done!
  • 15.