Presentation is loading. Please wait.

Presentation is loading. Please wait.

COS 418: Advanced Computer Systems Lecture 5 Michael Freedman

Similar presentations


Presentation on theme: "COS 418: Advanced Computer Systems Lecture 5 Michael Freedman"— Presentation transcript:

1 COS 418: Advanced Computer Systems Lecture 5 Michael Freedman
Transactions: ACID, Concurrency control (2PL, OCC) Intro to distributed txns COS 418: Advanced Computer Systems Lecture 5 Michael Freedman

2 The transaction Definition: A unit of work:
May consist of multiple data accesses or updates Must commit or abort as a single atomic unit Transactions can either commit, or abort When commit, all updates performed on database are made permanent, visible to other transactions When abort, database restored to a state such that the aborting transaction never executed The key technique we have for doing that is called a transaction. A transaction is a concept from databases, but useful to have in your distributed systems “toolbox.”

3 Defining properties of transactions
Atomicity: Either all constituent operations of the transaction complete successfully, or none do Consistency: Each transaction in isolation preserves a set of integrity constraints on the data Isolation: Transactions’ behavior not impacted by presence of other concurrent transactions Durability: The transaction’s effects survive failure of volatile (memory) or non-volatile (disk) storage The set of defining properties of transactions goes by the mnemonic ACID. Consistency: e.g., we may stipulate the constraint that a bank account balance cannot be negative.

4 Goal #1: Handle failures Atomicity and Durability
So now this is the "I" in the ACID transactions properties: maintaining isolation between transactions.

5 Account transfer transaction
Transfers $10 from account A to account B Txn transfer(A, B): begin_tx a  read(A) if a < 10 then abort_tx else write(A, a−10) b  read(B) write(B, b+10) commit_tx So let’s give ourselves a sample transaction, one that transfers $10 from account A to account B. It checks A’s balance and then either aborts (abort_tx) or issues two WRITE operations to update the balances, then COMMITS (commit_tx).

6 Lack atomicity in the presence of failures
Problem Txn transfer(A, B): begin_tx a  read(A) if a < 10 then abort_tx else write(A, a−10) b  read(B) write(B, b+10) commit_tx Suppose $100 in A, $100 in B commit_tx starts commit protocol: write(A, $90) to disk write(B, $110) to disk What happens if system crash after first write, but before second write? After recovery: Partial writes, money is lost But here’s the problem, and we’ve seen it before. Lack atomicity in the presence of failures

7 How to ensure atomicity?
Log: A sequential file that stores information about transactions and system state Resides in separate, non-volatile storage One entry in the log for each update, commit, abort operation: called a log record Log record contains: Monotonic-increasing log sequence number (LSN) Old value (before image) of the item for undo New value (after image) of the item for redo And the way we typically implement undo & redo is using a LOG. LSN uniquely identifies each log record.

8 Write-ahead Logging (WAL)
Ensures atomicity in the event of system crashes under no-force/steal buffer management Force all log records pertaining to an updated page into the (non-volatile) log before any writes to page itself A transaction is not considered committed until all log records (including commit record) are forced into log The main algorithm for managing the log under a no-force/steal buffer management strategy is called WAL. This ensures that the UNDO information required by the STEAL policy is present in the event of a crash. This ensures that REDO information required by the NO-FORCE policy is present in the event of a crash.

9 Does not have to flush to disk
WAL example force_log_entry(A, old=$100, new=$90) force_log_entry(B, old=$100, new=$110) write(A, $90) write(B, $110) force_log_entry(commit) What if the commit log record size > the page size? How to ensure each log record is written atomically? Write a checksum of entire log entry Does not have to flush to disk So going back to the example of our transfer transaction. >>> What if the commit log record is greater in size than a disk page? (Write a checksum. Then later, consider the log record committed if and only if the checksum matches the data.)

10 Goal #2: Concurrency control Transaction Isolation
So now this is the "I" in the ACID transactions properties: maintaining isolation between transactions.

11 Two concurrent transactions
transaction transfer(A, B): begin_tx a  read(A) if a < 10 then abort_tx else write(A, a−10) b  read(B) write(B, b+10) commit_tx transaction sum(A, B): begin_tx a  read(A) b  read(B) print a + b commit_tx So let's think back to our TRANSFER transaction, and now let's think about how it may interact with another transaction SUM that reads both bank accounts.

12 Isolation between transactions
Isolation: sum appears to happen either completely before or completely after transfer Schedule for transactions is an ordering of the operations performed by those transactions

13 Problem for concurrent execution: Inconsistent retrieval
Serial execution of transactions—transfer then sum: transfer: rA wA rB wB © sum: rA rB © Concurrent execution resulting in inconsistent retrieval, result differing from any serial execution: transfer: rA wA rB wB © Time  © = commit debit credit Initially accounts both have $100: expect sum to print $200. Any serial execution of the two transactions will print $200. >>> BUT there are some operation interleavings that will result in the sum transaction seeing an inconsistent state of the database in which $10 is debited from account A but not yet credited into account B. debit credit

14 Equivalence of schedules
Two operations from different transactions are conflicting if: They read and write to the same data item The write and write to the same data item Two schedules are equivalent if: They contain the same transactions and operations They order all conflicting operations of non- aborting transactions in the same way So to define equivalent schedules, let’s first start with the individual operations.

15 Serializability A schedule is conflict serializable if it is equivalent to some serial schedule i.e., non-conflicting operations can be reordered to get a serial schedule So b/c we're thinking about conflicts between ops, this way of thinking about isolation between transactions is called conflict serializability.

16 How to ensure a serializable schedule?
Locking-based approaches Strawman 1: Big Global Lock Acquire the lock when transaction starts Release the lock when transaction ends Results in a serial transaction schedule at the cost of performance

17 Locking Locks maintained by transaction manager Lock types
Transaction requests lock for a data item Transaction manager grants or denies lock Lock types Shared: Need to have before read object Exclusive: Need to have before write object So to improve performance, we let transactions run concurrently, but add another module to our system called lock manager that maintains locks on individual data items. Shared (S) Exclusive (X) Yes No

18 How to ensure a serializable schedule?
Strawman 2: Grab locks independently, for each data item (e.g., bank accounts A and B transfer: ◢A rA wA ◣A ◢B rB wB ◣B © sum: ◿A rA ◺A ◿B rB ◺B © Time  © = commit ◢ /◿ = eXclusive- / Shared-lock; ◣ / ◺ = X- / S-unlock So let's now try to use locking to guarantee a serializable schedule. A simple way of doing this is for each transaction to grab locks independently for each data item. >>> SEGUE: Look at where TRANSFER releases its lock on A before writing B. This allowed SUM read B’s value TOO SOON. Permits this non-serializable interleaving

19 Two-phase locking (2PL)
2PL rule: Once a transaction has released a lock it is not allowed to obtain any other locks A growing phase when transaction acquires locks A shrinking phase when transaction releases locks In practice: Growing phase is the entire transaction Shrinking phase is during commit So that motivates the 2PL algorithm.

20 2PL allows only serializable schedules
2PL rule: Once a transaction has released a lock it is not allowed to obtain any other locks transfer: ◢A rA wA ◣A ◢B rB wB ◣B © sum: ◿A rA ◺A ◿B rB ◺B © Time  © = commit ◢ /◿ = X- / S-lock; ◣ / ◺ = X- / S-unlock 2PL precludes this non-serializable interleaving

21 2PL and transaction concurrency
2PL rule: Once a transaction has released a lock it is not allowed to obtain any other locks transfer: ◿A rA ◢A wA ◿B rB ◢B wB✻© sum: ◿A rA ◿B rB✻© Time  © = commit ◢ /◿ = X- / S-lock; ◣ / ◺ = X- / S-unlock ✻ = release all locks On the other hand, 2PL allows transactions to execute concurrently, improving performance. 2PL permits this serializable, interleaved schedule

22 Serializability versus linearizability
Linearizability is a guarantee about single operations on single objects Once write completes, all later reads (by wall clock) should reflect that write Serializability is a guarantee about transactions over one or more objects Doesn’t impose real-time constraints Strict serializability: The equivalent serial order cannot re-order commit-to-begin_tx ordering. Linearizability + serializability = strict serializability Transaction behavior equivalent to some serial execution And that serial execution agrees with real-time

23 Recall: lock-based concurrency control
Big Global Lock: Results in a serial transaction schedule at the cost of performance Two-phase locking with finer-grain locks: Growing phase when txn acquires locks Shrinking phase when txn releases locks (typically commit) Allows txn to execute concurrently, improvoing performance So that motivates the 2PL algorithm.

24 Q: What if access patterns rarely, if ever, conflict?

25 Be optimistic! Goal: Low overhead for non-conflicting txns
Assume success! Process transaction as if would succeed Check for serializability only at commit time If fails, abort transaction Optimistic Concurrency Control (OCC) Higher performance when few conflicts vs. locking Lower performance when many conflicts vs. locking

26 OCC: Three-phase approach
Begin: Record timestamp marking the transaction’s beginning Modify phase: Txn can read values of committed data items Updates only to local copies (versions) of items (in db cache) Validate phase Commit phase If validates, transaction’s updates applied to DB Otherwise, transaction restarted Care must be taken to avoid “TOCTTOU” issues Time to check t Time to use

27 OCC: Why validation is necessary
New txn creates shadow copies of P and Q P and Q’s copies at inconsistent state txn coord O txn coord P When commits txn updates, create new versions at some timestamp t Q

28 OCC: Validate Phase Transaction is about to commit System must ensure: Initial consistency: Versions of accessed objects at start consistent No conflicting concurrency: No other txn has committed an operation at object that conflicts with one of this txn’s invocations

29 OCC: Validate Phase Validation needed by transaction T to commit:
For all other txns O either committed or in validation phase, one of following holds: O completes commit before T starts modify T starts commit after O completes commit, and ReadSet T and WriteSet O are disjoint Both ReadSet T and WriteSet T are disjoint from WriteSet O, and O completes modify phase. When validating T, first check (A), then (B), then (C) If all fail, validation fails and T aborted

30 2PL & OCC = strict serialization
Provides semantics as if only one transaction was running on DB at time, in serial order + Real-time guarantees 2PL: Pessimistically get all the locks first OCC: Optimistically create copies, but then recheck all read + written items before commit

31 Distributed Transactions

32 Consider partitioned data over servers
L R U O L R W U P L W U Q When do you release locks before commit? Carefully – like when you are traversing a data structure but not actually using the data Why not just use 2PL? Grab locks over entire read and write set Perform writes Release locks (at commit time)

33 Consider partitioned data over servers
L R U O L R W U P L W U Q When do you release locks before commit? Carefully – like when you are traversing a data structure but not actually using the data How do you get serializability? On single machine, single COMMIT op in the WAL In distributed setting, assign global timestamp to txn (at sometime after lock acquisition and before commit) Centralized txn manager Distributed consensus on timestamp (not all ops)

34 Strawman: Consensus per txn group?
L R U O L R W U P L W U Q When do you release locks before commit? Carefully – like when you are traversing a data structure but not actually using the data R S Single Lamport clock, consensus per group? Linearizability composes! But doesn’t solve concurrent, non-overlapping txn problem

35 Wednesday Google Spanner Distributed Transactions: Calvin, Rococo


Download ppt "COS 418: Advanced Computer Systems Lecture 5 Michael Freedman"

Similar presentations


Ads by Google