Docsity

Distributed Software Dev Transactions: Serial Equivalence & Locking, Study notes of Software Engineering

Toggle sidebar
bg1
Distributed Software Development
Transactions
Chris Brooks
Department of Computer Science
University of San Francisco
Departmentof Computer Science —University of San Francisco – p.1/??
17-0: Transactions
Features of transactions
Serial equivalence
Locking and deadlock
Distributed transactions
Two-phase commit
Distributed deadlock
Departmentof Computer Science —University of San Francisco – p.2/??
17-1: Transactions
A transaction is a sequence of operations between a client
and a server.
Goal: make sure that:
Objects remain in a consistent state
System is tolerant to crash failures
Transaction effects are independent of other
transactions
Transactions are either completed or not started.
Departmentof Computer Science —University of San Francisco – p.3/??
17-2: Example
As an example, we’ll look at an interface to a banking
system.
We’d like to be able to do the following operations on
accounts:
deposit(amt)
withdraw(amt)
getBalance()
setBalance(amt)
We’d also like the following operations to be available for
branches:
CreateAccount(name)
lookUpAccount(name)
totalAccounts()
Departmentof Computer Science —University of San Francisco – p.4/??
17-3: Example transaction
A transaction may involve several operations, each of
which changes the state of a different object:
Transaction T:
1. alexAcct.withdraw(100)
2. nancyAcct.deposit(100)
3. nancyAcct.widthdraw(200)
4. brooksAcct.deposit(200)
We can’t stop in the middle, lose any of the operations,
or do them in the wrong order.
Departmentof Computer Science —University of San Francisco – p.5/??
17-4: Committing and Aborting
A transaction may be either committed or aborted.
When all operations are complete and the transaction is
ready to be accepted, it is committed.
Written to permanent storage
After this point, it cannot be undone
If the server decided that a transaction cannot be
processed (undone by the client, or it will leave the system
in an inconsistent state), it is aborted.
All operations are undone
Departmentof Computer Science —University of San Francisco – p.6/??
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Distributed Software Dev Transactions: Serial Equivalence & Locking and more Study notes Software Engineering in PDF only on Docsity!

Distributed Software Development

Transactions

Chris Brooks Department of Computer Science University of San Francisco ?? Department of Computer Science — University of San Francisco – p.1/

Transactions 17-0:

Features of transactions Serial equivalence Locking and deadlock Distributed transactions Two-phase commit Distributed deadlock ?? Department of Computer Science — University of San Francisco – p.2/

Transactions 17-1:

A transaction is a sequence of operations between a client and a server. Goal: make sure that: Objects remain in a consistent state System is tolerant to crash failures Transaction effects are independent of other transactions Transactions are either completed or not started. ?? Department of Computer Science — University of San Francisco – p.3/

Example 17-2:

As an example, we’ll look at an interface to a banking system. We’d like to be able to do the following operations on accounts: deposit(amt) withdraw(amt) getBalance() setBalance(amt) We’d also like the following operations to be available for branches: CreateAccount(name) lookUpAccount(name) totalAccounts() ?? Department of Computer Science — University of San Francisco – p.4/

Example transaction 17-3:

A transaction may involve several operations, each of which changes the state of a different object: Transaction T:

  1. alexAcct.withdraw(100)
  2. nancyAcct.deposit(100)
  3. nancyAcct.widthdraw(200)
  4. brooksAcct.deposit(200) We can’t stop in the middle, lose any of the operations, or do them in the wrong order.

Committing and Aborting 17-4:

A transaction may be either committed or aborted. When all operations are complete and the transaction is

. committed ready to be accepted, it is Written to permanent storage After this point, it cannot be undone If the server decided that a transaction cannot be processed (undone by the client, or it will leave the system . aborted in an inconsistent state), it is All operations are undone

ACID 17-5:

The desirable features of a transactional DBMS are sometimes referred to as ACID Atomicity Consistency Isolation Durability ?? Department of Computer Science — University of San Francisco – p.7/

Atomicity 17-6:

Atomicity is sometimes referred to as “all-or-nothing”. Either a transaction completes successfully, and all effects are applied to all objects, or it has no effect at all. Either all withdraws and deposits are made, or none of them are. ?? Department of Computer Science — University of San Francisco – p.8/

Consistency 17-7:

A transaction must move the system from consistent state to consistent state. For example, the sum of all the accountBalances must always be equal to the branch’s totalAccounts() Depending on the application, at database may have other constraints No negative balances on an account No post-dated transactions If the system is in an inconsistent state after a transaction, the transaction must be undone, so as to restore consistency. ?? Department of Computer Science — University of San Francisco – p.9/

Isolation 17-8:

The intermediate effects of a transaction must not be visible to other transactions. In our example, Nancy’s bank account balance briefly went up by $100. (the money was then transferred to Brooks’ account) No other process or transaction should see that balance. In other words, to the outside world, a transaction must appear as a single operation. ?? Department of Computer Science — University of San Francisco – p.10/

Isolation 17-9:

How to provide isolation? Perform all transactions in a single thread Works, but doesn’t scale. Use locks to control concurrent access. Better, although now we need to detect (and undo) deadlocks.

Durability 17-10:

After a transaction has been processed, its effects are saved to permanent storage. The transaction will never be undone or lost, even in the presence of a server crash. This typically also requires some guarantees about the nature of the permanent storage.

Conflicting operations 17-17:

The trick to achieving serial equivalence is to identify operations that conflict with each other. Read/write and write/write (read/read is OK) For two transactions to be serially equivalent, all pairs of conflicting operations must be executed in the same order on all objects they both access. ?? Department of Computer Science — University of San Francisco – p.19/

Conflicting operations 17-18:

For example: T: x=read(i); write(i,10); write(j,20) U: read(j); write(j,30); z=read(i) To have serial equivalence, one of the following conditions must hold: T accesses i before U does and T accesses j before U does U accesses I before T does and U accesses J before T does ?? Department of Computer Science — University of San Francisco – p.20/

Locking 17-19:

The most common way to achieve serial equivalence is through the use of locks. Before a client transaction uses an object, it requests an associated lock. The object cannot be used until the lock is acquired. To ensure serial equivalence, a process may not acquire new locks within a transaction once a lock has been released. This is called two-phase locking. There is a “lock-growing phase” and a “lock-shrinking” phase ?? Department of Computer Science — University of San Francisco – p.21/

Locking example 17-20:

T: begin transaction (T) = balance1 = B.getBalance() ($200) T locks B (T) = b.setBalance(balance1 * 1.1)($220) U begins transaction (T) = A.withdraw(balance1 / 10)T locks A (U) = balance2 = B.getBalance() ($200) B is locked - U must wait. T ends transaction: A and B unlocked. (U) = b.setBalance(balance2 * 1.1)($220) U locks B (U) = C.withdraw(balance2 / 10) U locks C ?? Department of Computer Science — University of San Francisco – p.22/ U ends transaction: B and C unlocked.

Locking 17-21:

To prevent “dirty reads” and “premature writes”, all locks must be held until the transaction is committed. This is called strict two-phase locking. “dirty read” - one transaction sees a value that is part of another transaction that is later aborted. T: b1 = a.getBalance() ($100) T: a.setBalance(b1 + 10) ($110) U: b2 = a.getBalance() ($110) U: a.setBalance(b2 + 20) ($130) U committed. T aborted - U cannot be undone. ?? Department of Computer Science — University of San Francisco – p.23/

Locking 17-22:

To prevent “dirty reads” and “premature writes”, all locks must be held until the transaction is committed. This is called strict two-phase locking. “premature writes” - an aborted operation is reset to the wrong value. Some systems will store the ’before image’ with a write and roll back to that in the event of an abort A: initial balance: $ T: a.setBalance(105) (’before image’: 100) U: a.setBalance(110) (’before image’: 105) U commits. T aborts: A reset to 100. (should remain 110) What if T aborts, then U aborts (balance will be 105, should be 100) ?? Department of Computer Science — University of San Francisco – p.24/

Locks 17-23:

There is a tradeoff between the number of locks a system has and the degree of concurrency allowed of the system granularity This is called the Goal: allow many objects the ability to read an object, but only one the ability to write the object. Provide read locks and write locks. ?? Department of Computer Science — University of San Francisco – p.25/

Deadlocks 17-24:

. deadlock Whenever locks are used, a system can reach Two or more processes are each waiting for a lock held by the other process. Neither can proceed until the other gives up its lock. Neither can give up its lock until it proceeds. ?? Department of Computer Science — University of San Francisco – p.26/

Deadlock example 17-25:

T wants to transfer $100 from A to B. U wants to transfer $50 from B to A. T: A.deposit(100) - write lock on A. U: B.deposit(50) - write lock on B. T: B.withdraw(100) - wait for write lock for B U: A.withdraw(50) - wait for write lock on A. Since neither process will release its locks without committing or aborting, we are in deadlock. ?? Department of Computer Science — University of San Francisco – p.27/

Preventing deadlock 17-26:

One simple (but not efficient) method to prevent deadlock is to require a transaction to (atomically) acquire all locks at the beginning. Restricts access to shared resources May not be possible to predict at the beginning of a transaction, as in interactive applications. Can also specify a static order. ?? Department of Computer Science — University of San Francisco – p.28/

Detecting deadlock 17-27:

A more common approach is to have the process responsible for administering locks detect deadlock and force transactions to abort. We can represent processes as nodes and lock wait-for graph dependencies as edges in a When a lock is requested and must be waited on, an edge is added. When a lock is freed, the edge is removed. If the graph has a cycle, then we have deadlock. The transaction at one of the nodes in the cycle can then be aborted. Choosing which transaction to abort can be tricky

Distributed Transactions 17-28:

So far, we’ve focused on how to provide concurrent transactions within a single server. involve multiple servers that must Distributed transactions maintain a consistent state. If a transaction is committed at one server, it must commit at all servers. If a transaction is aborted at one server, it must abort at all servers.

Two-phase commit 17-35:

Phase 1: The coordinator asks each participant whether it is able to commit. A participant may not change its vote to abort once it has voted to commit. This means that a participant can’t vote until it knows that it can complete its portion of the transaction. ?? Department of Computer Science — University of San Francisco – p.37/

Two-phase commit 17-36:

Phase 2: Once all participants have voted, if any participant voted to abort, the doAbort message is sent to all participants. If all participants voted to commit, the doCommit message is sent to all participants. Clients each inform the coordinator that they have committed. Once all participants have replied, the client is informed that the transaction has been processed. ?? Department of Computer Science — University of San Francisco – p.38/

Two-phase commit 17-37:

A B C, D Client openTransaction T id: a.withdraw(10)c.deposit(10) b.withdraw(5)d.deposit(10) a.withdraw(T,10) b.withdraw(T,10) c.deposit(T,10)d.deposit(T,5) (join)^ (join) Client wants to perform a transaction involving four accounts at three branches. Sends openTransaction to coordinator at A and receives a transaction ID. Includes transaction ID with each request to other branches. each branch is con- As they send a join tacted, message to A. ?? Department of Computer Science — University of San Francisco – p.39/

Two-phase commit 17-38:

A B C, D Client openTransaction T id: a.withdraw(10)c.deposit(10) b.withdraw(5)d.deposit(10) a.withdraw(T,10) b.withdraw(T,10) c.deposit(T,10)d.deposit(T,5) (join)^ (join) If the client wants to abort, it sends a doAbort message to the coordinator. all to forwarded is This group members, who im- mediately abort. ?? Department of Computer Science — University of San Francisco – p.40/

Two-phase commit 17-39:

A B C, D Client openTransaction T id: a.withdraw(10)c.deposit(10) b.withdraw(5)d.deposit(10) a.withdraw(T,10) b.withdraw(T,10) c.deposit(T,10)d.deposit(T,5) (join)^ (join) If the client wants to commit, it sends a doCommit message to the coordinator. The coordinator sends a canCommit message to all participants. When they are ready to commit, they send back yes. they If they must abort, send back no. ?? Department of Computer Science — University of San Francisco – p.41/

Two-phase commit 17-40:

A B C, D Client openTransaction T id: a.withdraw(10)c.deposit(10) b.withdraw(5)d.deposit(10) a.withdraw(T,10) b.withdraw(T,10) c.deposit(T,10)d.deposit(T,5) (join)^ (join) Once all votes have been collected, if there are any aborts, the coordinator sends doAbort to all participants. Otherwise, a doCommit is sent to all participants. Once a participant commits, it returns haveCommitted to the coordinator. haveCommit all When received, are messages the client is updated and ?? Department of Computer Science — University of San Francisco – p.42/the transaction closed.

Nested two-phase commit 17-41:

We can extend two-phase commit to work with nested transactions. When a subtransaction is started, a coordinator for that subtransaction is selected. If one of the participants in that subtransaction aborts, the coordinator can choose whether to abort, based on the specifics of the transaction. Example: withdraw fees from all accounts. If one account becomes overdrawn, that subtransaction is aborted, but others may proceed. If the parent is aborted, the subtransactions must abort. ?? Department of Computer Science — University of San Francisco – p.43/

Failure in Two-phase commit 17-42:

Message loss is dealt with via timeouts. Server failure is dealt with by writing progress out to permanent storage. As operations are processed, they are logged. A replica is then able to start up and recreate progress. Couloris has a great deal of detail on the way logs can be managed to do recovery. ?? Department of Computer Science — University of San Francisco – p.44/

Locking and Deadlock 17-43:

In a distributed transactional system, a lock manager that is local to the resource manages access to that resource. Remote processes request access to the locks as usual. Since there are many independent lock managers, deadlock can arise. Transaction T locks variable X on server A. Transaction U locks variable Y on server B. Now each transaction wants to modify the other variable. ?? Department of Computer Science — University of San Francisco – p.45/

Distributed Deadlock 17-44:

In a centralized system, we would detect deadlock by finding a cycle in the wait-for graph. This is the simplest approach in a distributed system All lock managers send their information to a central authority. All the usual problems with this approach apply. ?? Department of Computer Science — University of San Francisco – p.46/

Edge chasing 17-45:

When a server that is acting as a lock manager receives a request for a locked resource, it adds an edge to its local wait-for graph. For example, say server X controls access to database

A.

Initially, transaction T wants to execute A.withdraw(10) X grants T the lock to A. Then transaction U wants to execute A.deposit(20) T is added to X’s wait-for graph. → A is locked, so U

Edge chasing 17-46:

When a transaction begins waiting for a lock, the coordinator is informed. When a process receives a request for a lock, and another transaction is holding that lock, it checks with the coordinator to see if the other process is waiting. if it is, it sends a probe to the server administering that lock. Includes the edge in the wait-for graph When the probe is received, if there are waiting processes, those edges are added and the probe is forwarded. As edges are added, cycles are checked for.