View
6
Download
0
Category
Preview:
Citation preview
TransactionsCS6450: Distributed Systems
Lecture 14
Ryan Stutsman
1
Material taken/derived from Princeton COS-418 materials created by Michael Freedman and Kyle Jamieson at Princeton University.Licensed for use under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Some material taken/derived from MIT 6.824 by Robert Morris, Franz Kaashoek, and Nickolai Zeldovich.
• 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
2
The transaction
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
1. High transaction speed requirements• If always fsync() to disk for each result on
transaction, yields terrible performance
2. Atomic and durable writes to disk are difficult• In a manner to handle arbitrary crashes• Hard disks and solid-state storage use write
buffers in volatile memory
Challenges
1. Techniques for achieving ACID properties• Write-ahead logging and checkpointing
• Serializability and two-phase locking
2. Algorithms for Recovery and Isolation Exploiting Semantics (ARIES)
5
Today
• Transactions properties: ACID• Atomicity, Consistency, Isolation, Durability
• Application logic checks consistency (C)
• This leaves two main goals for the system:
1. Handle failures (A, D)
2. Handle concurrency (I)
6
What does the system need to do?
• Standard “crash failure” model:
• Machines are prone to crashes:• Disk contents (non-volatile storage) okay• Memory contents (volatile storage) lost
• Machines don’t misbehave (“Byzantine”)
7
Failure model: crash failures
• Transfers $10 from account A to account B
8
Account transfer transaction
transaction transfer(A, B):begin_txa ß read(A)if a < 10 then abort_txelse write(A, a−10)
b ß read(B)write(B, b+10)commit_tx
• Suppose $100 in A, $100 in B
• commit_tx starts the 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
9
Problemtransaction transfer(A, B):begin_txa ß read(A)if a < 10 then abort_txelse write(A, a−10)
b ß read(B)write(B, b+10)commit_tx
Lack atomicity in the presence of failures
• Smallest unit of storage that can be atomically written to non-volatile storage is called a page
• Buffer manager moves pages between buffer pool (in volatile memory) and disk (in non-volatile storage)
10
System structure
Buffer pool
Buffer managerPage
Non-volatile storage
Disk
1. Force all a transaction’s writes to disk beforetransaction commits?• Yes: force policy• No: no-force policy
2. May uncommitted transactions’ writes overwritecommitted values on disk?• Yes: steal policy• No: no-steal policy
11
Two design choices
1. Force all a transaction’s writes to disk beforetransaction commits?• Yes: force policy
2. May uncommitted transactions’ writes overwritecommitted values on disk?• No: no-steal policy
12
Performance implications
Then slower disk writes appear on the critical path of a committing transaction
Then buffer manager loses write scheduling flexibility
1. Force all a transaction’s writes to disk beforetransaction commits?• Choose no: no-force policy� Need support for redo: complete a committed transaction’s writes
on disk
2. May uncommitted transactions’ writes overwritecommitted values on disk?• Choose yes: steal policy� Need support for undo: removing the effects of an uncommitted
transaction on disk
13
Undo & redo
• 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
14
How to implement undo & redo?
Non-volatile storage
• Buffer pool (volatile memory) and disk (non-volatile)
• The log resides on a separate partition or disk (in non-volatile storage)
15
System structure
DiskLog
Buffer pool
Buffer manager Page
• Ensures atomicity in the event of system crashes under no-force/steal buffer management
1. Force all log records pertaining to an updated page into the (non-volatile) log before any writes to page itself
2. A transaction is not considered committed until all its log records (including commit record) are forced into the log
16
Write-ahead Logging (WAL)
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
17
WAL example
Does not have to flush to disk
Goal #2: Concurrency controlTransaction isolation
18
19
Two concurrent transactions
transaction sum(A, B):begin_txa ß read(A)b ß read(B)print a + bcommit_tx
transaction transfer(A, B):begin_txa ß read(A)if a < 10 then abort_txelse write(A, a−10)
b ß read(B)write(B, b+10)commit_tx
• Isolation: sum appears to happen either completely before or completely after transfer• Sometimes called before-after atomicity
• Schedule for transactions is an ordering of the operations performed by those transactions
20
Isolation between transactions
• 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 ©sum: rA rB ©
Time à© = commit
21
Problem for concurrent execution: Inconsistent retrieval
debit credit
debit credit
• Isolation: sum appears to happen either completely before or completely after transfer• Sometimes called before-after atomicity
• Given a schedule of operations:• Is that schedule in some way “equivalent”
to a serial execution of transactions?
22
Isolation between transactions
• Two operations from different transactions are conflicting if:
1. They read and write to the same data item2. The write and write to the same data item
• Two schedules are equivalent if:1. They contain the same transactions and operations2. They order all conflicting operations of non-aborting
transactions in the same way
23
Equivalence of schedules
• Ideal isolation semantics: conflict 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
24
Conflict serializability
• Ideal isolation semantics: conflict 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
transfer: rA wA rB wB ©sum: rA rB ©
Time à© = commit
25
A serializable schedule
Conflict-free!Serial schedule
rA
• Ideal isolation semantics: conflict 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
transfer: rA wA rB wB ©sum: rA rB ©
Time à© = commit
26
A non-serializable schedule
Conflicting ops Conflicting ops
But in a serial schedule, sum’s reads either both before wA or both after wB
• Each node t in the precedence graph represents a transaction t• Edge from s to t if some action of s precedes
and conflicts with some action of t
27
Testing for serializability
• Each node t in the precedence graph represents a transaction t• Edge from s to t if some action of s precedes
and conflicts with some action of t
transfer: rA wA rB wB ©sum: rA rB ©
Time à© = commit
28
Serializable schedule, acyclic graph
transfer sum
Serializable
• Each node t in the precedence graph represents a transaction t• Edge from s to t if some action of s precedes and
conflicts with some action of t
transfer: rA wA rB wB ©sum: rA rB ©
Time à© = commit
29
Non-serializable schedule, cyclic graph
transfer sum
Non-serializable
• Each node t in the precedence graph represents a transaction t• Edge from s to t if some action of s precedes and
conflicts with some action of t
30
Testing for serializability
In general, a schedule is conflict-serializable if and only if its precedence graph is acyclic
• Locking-based approaches
• Strawman 1: Big Global Lock• Acquire the lock when transaction starts• Release the lock when transaction ends
31
How do we ensure a serializable schedule?
Results in a serial transaction schedule at the cost of performance
• Locks maintained by transaction manager• 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
32
Locking
Shared (S) Exclusive (X)Shared (S) Yes NoExclusive (X) No No
• 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
33
How do we ensure a serializable schedule?
Permits this non-serializable interleaving
• 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
34
Two-phase locking (2PL)
• 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
35
2PL allows only serializable schedules
2PL precludes this non-serializable interleaving
• 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
36
2PL and transaction concurrency
2PL permits this serializable, interleaved schedule
• 2PL rule: Once a transaction has released a lock it is not allowed to obtain any other locks
transfer: rA wA rB wB ©sum: rA rB ©
Time à© = commit
(locking not shown)
37
2PL doesn’t exploit all opportunitiesfor concurrency
2PL precludes this serializable, interleaved schedule
• What if a lock is unavailable? Is deadlock possible?• Yes; but a central controller can detect deadlock cycles and
abort involved transactions
• The phantom problem• Database has fancier ops than key-value store• T1: begin_tx; update employee (set salary = 1.1×salary)
where dept = “CS”; commit_tx• T2: insert into employee (“carol”, “CS”)
• Even if they lock individual data items, could result in non-serializable execution
38
Issues with 2PL
• Linearizability: 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
39
Serializability versus linearizability
• Linearizability + serializability = strict serializability– Transaction behavior equivalent to some serial execution• And that serial execution agrees with real-time
1. Techniques for achieving ACID properties• Write-ahead logging and check-pointing à
A,D
• Serializability and two-phase locking à I
2. Algorithms for Recovery and Isolation Exploiting Semantics (ARIES)
40
Today
• In IBM DB2 & MSFT SQL Server, gold standard
• Key ideas:
1. Refinement of WAL (steal/no-force buffer management policy)
2. Repeating history after restart due to a crash (redo)
3. Log every change, even undo operations during crash recovery• Helps for repeated crash/restarts
41
ARIES (Mohan, 1992)
• Log, composed of log records, each containing:• LSN: Log sequence number (strictly increasing)• prevLSN: Pointer to the previous log record for the
same transaction• A linked list for each transaction, “threaded” through the log
• Pages• pageLSN: Uniquely identifies the log record for the
latest update applied to this page
42
ARIES’ stable storage data structures
• Transaction table (T-table): one entry per transaction• Transaction identifier• Transaction status (running, committed, aborted)• lastLSN: LSN of the most recent log record written by
the transaction
• Dirty page table: one entry per page• Page identifier• recoveryLSN: LSN of log record for earliest change to
that page not on disk
43
ARIES’ in-memory data structures
1. Write log records associated with this transaction to the log as transaction executes
2. Write end log record to the log• This is the actual “commit point”• Often also defines “the” equivalent serial order too
44
Transaction commit
• Happens while other transactions are running, as a separate transaction• Does not flush dirty pages to disk• Does tell us how much to fix on crash
1. Write “begin checkpoint” to log2. Write current transaction table, dirty page table,
and “end checkpoint” to log3. Force log to non-volatile storage4. Store “begin checkpoint” LSN à master record
45
Checkpoint
1. Start with checkpointed T- & dirty page-tables2. Read log forward from checkpoint, updating tables
• For end entries, remove T from T-table (T1, T3)• For other log entries, add (T3, T4) or update T-table
• Add LSN to dirty page table’s recoveryLSN46
Crash recovery: Phase 1 (Analysis)Checkpoint
T2:
T4:
T1:end
T3:end
firstLSN
Earliest recoveryLSN
Log:
• Start at firstLSN, scan log entries forward in time• Reapply action, update pageLSN
• Database state now matches state as recorded by log at the time of crash
47
Crash recovery: Phase 2 (REDO)
T2:
T4:
T1:
T3:
firstLSNLog:
• Scan log entries backwards from the end. For updates:• Write compensation log record (CLR) to log
• Contains prevLSN for update: UndoNextLSN• Undo the update’s operation
48
Crash recovery: Phase 3 (UNDO)
T2:
T4:
T1:
T3:
firstLSNLog:
• Scan log entries backwards from the end. For CLRs:• If UndoNextLSN = null, write end record
• Undo for that transaction is done• Else, skip to UndoNextLSN for processing
• Turned the undo into a redo, done in Phase 2
49
Crash recovery: Phase 3 (UNDO)
T2:
T4:
T1:
T3:
firstLSNLog:
50
Log (time )
Writepage 1
LSN: 10 20 30
Writepage 1
Writepage 1
Restart
CLR FORLSN 30Undo
CLR FORUndo
LSN 20
Restart
CLR FOR
Undo
LSN 10
40 50 60
X: 1 →
2
Y: 3 →
4
Z: 4 →
5
Z: 5 →
4
Y: 4 →
3
X: 2 →
1
• Brings together all the concepts we’ve discussed for ACID, concurrent transactions
• Introduced redo for “repeating history,” novel undo logging for repeated crashes
• For the interested: Compare with System R (not discussed in this class)
51
ARIES: Concluding thoughts
OCC: What if access patterns rarely, if ever, conflict?
52
Serializability
Execution of a set of transactions over multiple items is equivalent to some serial execution of the same set of transactions
53
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, improving performance
54
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
55
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
56
OCC: Three-phase approach
• Active phase: • Txn can read values of committed data items• Txn caches updates until validation phase
• Validation phase• Ensure reads are still up-to-date and no write-write conflicts• If validates, transaction’s updates applied to DB• Otherwise, transaction restarted• Validation order is typically serial; defines “the” order for
serializability
• Commit phase or Finished
57
58
OCC: Why validation is necessary
txncoord O
Q
PWhen commit txn updates,new versions overwrite old
• New txn reads into cache P and Q
• P and Q’s copies at inconsistent state
txncoord
Validation in a Nutshell
• Phases: active, validating, finished• To validate T:• If RS(T) ∩ WS(U) ≠ ∅ for U validated while T
active,then abort T• U is committing ahead of T
and it wrote into T’s read set• If WS(T) ∩ WS(U) ≠ ∅ for U validated while T
validating, then abort T• U is committing ahead of T
and it wrote into T’s write set
59
Validation Example
60
uWTpT LOBLY11ABLG LOB-1OE m G qZEvNZ-vOB Ang
- ]■’&• VY ]■’&• V
Y [W;’OW&O - [W;’OW&’G” Y “’G’•F
I’”!■ pv,nns T /WGGK& [W;’OW& ’“ ’& /K!;O &FG ]■’& •KY&F’G” WFWO K“ WG W■;’■ &■WG•W/&’KG
Y ]■K& V -“K■ T O’O, 6 YW< &F!• •!YYW■’> &F• K-•■[W&’KG• ]’&F &F “K;;K]’G” ■!; “K■ [W;’OW&’G” W &■WG•W/&’KG -s
£ 0F/X &FW& 1DdMl G 6™lRx' ® m “K■ WG< Z■[’K!•;< [W;’OW&O Y &FW& O’O GK& “’G’•F -“K■ - •&W■&O* ’,,* ’“ IU4l0C' P DM’1MdMlv
£ 0F/X &FW& 6™ lT' 9 6™l+x' k m “K■ WG< Z■[’K!•;< [W;’OW&O Y &FW& O’O GK& “’G’•F -“K■ - [W;’OW&O* ’,,* ’“ “’Gl+x' P I’mH-'T
b ~WY Z; pv,uAs I’”!■ pv,ng •FK]• W &’Y ;’G O!■’G” ]F’/F “K!■ &■WG•W/&’KG• T* Y* q* WGO ) W&&YZ& &K ~/!& WGO [W;’OW&, TF ■WO WGO ]■’& •&• “K■ W/F &■WG•W/&’KG W■ ’GO’/W&O KG &F O’W”■WY, - •&W■&• “’■•&* W;&FK!”F Y ’• &F “’■•& &K [W;’OW&,
1D W £m £ 3D W ® N £
Y
1D W :Z* N £ 3D W ® Z* L ±
)
U k •&W■&
3 k [W;’OW&
5 k “’G’•F- q
1D W ®’i n £ 1D W ®n £3D K : Z* L ± 3D W ® N* A ±
I’”!■ pv,ngs IK!■ &■WG•W/&’KG• WGO &F’■ [W;’OW&’KG
p, 1W;’OW&’KG K“ Ys 6FG Y [W;’OW&• &F■ W■ GK K&F■ [W;’OW&O &■WG•W/c&’KG•* •K &F■ ’• GK&F’G” &K /F/X, Y [W;’OW&• •!//••“!;;< WGO ]■’&• W [W;! “K■ OW&W-W• ;YG& NT
Tradeoffs
2PL
•Simple implementation•Fast under low
contention•Long stalls, blocking
when contention increases, but ok
•Moderate bookkeeping costs in normal case
•Deadlocks
Optimistic
•Great when low contention, collapses under high
•Low overhead•No stalls/blocking
inside database•Starvation
Compromise: Timestamp-based CC
• Simple approach• Allocate a timestamp to each tx when it starts• Ensure that txns appear to execute in that order• For enforcement tag values with timestamps• Popular now for in-memory databases
• Still optimistic in sense that it aborts on conflicts• More pessimistic since it “protects” reads• Doesn’t require validation*
62
Timestamp Management
• Let RT(X) be the read timestamp of object X• Let WT(X) be the write timestamp of object X• Let c(X) = true, if tx last wrote X known committed
• Global, strictly increasing timestamp on begin• On read check• If TS(T) ≥ WT(X), then read is in serial order• If c(X), allow read and if TS(T) > RT(X) let RT(X) = TS(T)• If !c(X), ...
63
Timestamp Management
• On write check• If TS(T) ≥ RT(X) and TS(T) ≥ WT(X),
then write is in serial order• Update X• WT(X) = TS(T)• Set c(X) = false
• If TS(T) ≥ RT(X) but TS(T) < WT(X),then write is in serial order, but a later write exists• If c(X), ignore T on X, if !c(X)... danger
• If TS(T) < RT(X), abort• On commit, mark all records X in T as c(X)
64
Read too late
65
uWTWT LOBLY11ABLG LOB-1OE m G -vfA7-Zf47 ASg
&F QK- K“ &F •/FO!;■* ’G WOO’&’KG &K W••’”G’G” &’Y•&WYZ• WGO !ZOW&’G” 1-* ) - * WGO L “K■ &F OW&W-W• ;YG&•* ’• &K /F/X &FW& ]FG[■ W ■WO K■ ]■’& K//!■•* ]FW& FWZZG• ’G ■W; &’Y (>^’} FW[ FWZZGO ’“ W/F &■WG•W/&’KG FWO ~/!&O ’G•&WG&WGK!•;< W& &F YKYG& K“ ’&• &’Y•&WYZ, U“ GK&* ] •W< &F -FW[’K■ ’• #•X“—(3’’X ^'■Q3’—!3j’QT TF■ W■ &]K X’GO• K“ Z■K-;Y• &FW& /WG K//!■s
p, 1Q3} ”>> ’3”Qs T■WG•W/&’KG - &■’• &K ■WO OW&W-W• ;YG& V * -!& &F ]■’& &’Y K“ V ’GO’/W&• &FW& &F /!■■G& [W;! K“ V ]W• ]■’&&G W“&■ - &FK■&’/W;;< ~/!&Of &FW& ’•* T ™ lT' M 6 T l 3 ' , I’”!■ pv,Sg ’;;!•&■W&• &F Z■K-;Y, TF FK■’>KG&W; W~’• ■Z■•G&• &F ■W; &’Y W& ]F’/F [G&• K//!■, 9K&&O ;’G• ;’GX &F W/&!W; [G&• &K &F &’Y• W& ]F’/F &F< &FK■&’/W;;< K//!■ ► &F &’Y•&WYZ K“ &F &■WG•W/&’KG &FW& Z■“K■Y• &F [G&, TF!•* ] • W &■WG•W/&’KG Y &FW& •&W■&O W“&■ &■WG•W/&’KG -* -!& ]■K& W [W;! “K■ V -“K■ T ■WO• V T - •FK!;O GK& - W-; &K ■WO &F [W;! ]■’&&G -< Y* -/W!• &FK■&’/W;;<* Y ~/!&O W“&■ - O’O, HK][■* ” FW• GK /FK’/* -/W!• 0C"• [W;! K“ V ’• &F KG &FW& - GK] ••, TF •K;!&’KG ’• &K W-K■& T ]FG &F Z■K-;Y ’• G/K!G&■O,
Y ]■’&• V- ■WO• V
- •&W■& Y •&W■&
I’”!■ pv,Sgs T■WG•W/&’KG - &■’• &K ■WO &KK ;W&
u, )■—”Q ”>> ’3”Qs T■WG•W/&’KG T &■’• &K ]■’& OW&W-W• ;YG& V T HK]c[■* &F ■WO &’Y K“ V ’GO’/W&• &FW& •KY K&F■ &■WG•W/&’KG •FK!;O FW[ ■WO &F [W;! ]■’&&G -< T* -!& ■WO •KY K&F■ [W;! ’G•&WO, TFW& ’•* 6 T l 3 ' M T ™ lT' M w T l 3 ' , TF Z■K-;Y ’• •FK]G ’G I’”, pv,S:, TF■ ] • W &■WG•W/&’KG Y &FW& •&W■&O W“&■ T* -!& ■WO V -“K■ - ”K& W /FWG/ &K ]■’& V T 6FG T &■’• &K ]■’& V * ] “’GO wTl,3 "' P T ™ lT'* YWG’G” &FW& V FW• W;■WO< -G ■WO -< W &■WG•W/&’KG Y &FW& &FK■&’c/W;;< ~/!&O ;W&■ &FWG -T 6 W;•K “’GO 6 T l 3 ' M &• lT '* ]F’/F YWG• &FW& GK K&F■ &■WG•W/&’KG ]■K& ’G&K V W [W;! &FW& ]K!;O FW[ K[■]■’&c&G T "• [W;!* &F!•* G”W&’G” T "• ■•ZKG•’-’;’&< &K ”& ’&• [W;! ’G&K V •K &■WG•W/&’KG Y /K!;O ■WO ’&,
w5v5vs T<>9{F•* 3 /▲” E/<▲X EK▲KTF■ ’• W /;W•• K“ Z■K-;Y• &FW& &F /KYY’& -’& ’• O•’”GO &K •K;[, 5G K“ &F• Z■K-;Y•* W *O’■&< ■WO*' ’• •!””•&O ’G I’”, pv,Sx, TF■* &■WG•W/&’KG
Write too late
66
AS: LRZ4-A1 uWT LOBLY11ABLG LOB-1OE
Y ■WO• V- ] ■’&• V
i i 0 '
- •&W■& Y •&W■&
I’”!■ pv,S:s T■WG•W/&’KG T &■’• &K ]■’& &KK ;W&
- ■WO• V * WGO V ]W• ;W•& ]■’&&G -< YT TF &’Y•&WYZ K“ Y ’• ;•• &FWG &FW& K“ T* WGO &F ■WO -< - K//!■• W“&■ &F ]■’& -< Y ’G ■W; &’Y* •K &F [G& •Y• &K - ZF<•’/W;;< ■W;’>W-;, HK][■* ’& ’• ZK••’-; &FW& W“&■ T ■WO• &F [W;! K“ V ]■’&&G -< Y* &■WG•W/&’KG Y ]’;; W-K■&f Z■FWZ• Y G/K!G&■• WG ■■K■ /KGO’&’KG ’G ’&• K]G OW&W* •!/F W• W O’[’•’KG -< m* K■ W• ] •FW;; • ’G ™/&’KG pv,v,n* &F •/FO!;■ “K■/• Y &K W-K■& -/W!• ’& &■’• &K OK •KY&F’G” ZF<•’/W;;< !G■W;’>W-;, TF!•* W;&FK!”F &F■ ’• GK&F’G” ZF<•’/W;;< !G■W;’>W-; W-K!& - ■WO’G” V * ’& ’• -&&■ &K O;W< T "• ■WO !G&’; Y /KYY’&• K■ W-K■&•, 6 /WG &;; &FW& Y ’• GK& /KYY’&&O -/W!• &F /KYY’& -’& 0 l 3 ' ]’;; - “W;•,
Y ] ■’&• V
- ■WO• V
HmOY •&W■& - •&W■& Y W-K■&•
I’”!■ pv,Sxs T /K!;O Z■“K■Y W O’■&< ■WO ’“ ’& ■WO• V ]FG •FK]G
© •/KGO ZK&G&’W; Z■K-;Y ’• •!””•&O -< I’”, Cpv,Sv, H■* Y* W &■WG•cW/&’KG ]’&F W ;W&■ &’Y•&WYZ &FWG T* FW• ]■’&&G V “’■•&, 6FG - &■’• &K ]■’&* &F WZZ■KZ■’W& W/&’KG ’• &K OK GK&F’G”, b[’OG&;< GK K&F■ &■WG•W/&’KG q &FW& •FK!;O FW[ ■WO T "• [W;! K“ V ”K& “ C"• [W;! ’G•&WO* -/W!• ’“ q &■’O &K ■WO V ’& ]K!;O FW[ W-K■&O -/W!• K“ W &KKc;W& ■WO, I!&!■ ■WO• K“ V ]’;; ]WG& Y"“ [W;! K■ W ;W&■ [W;! K“ V * GK& T "• [W;!, TF’• ’OW* &FW& ]■’&• /WG - •X’ZZO ]FG W ]■’& ]’&F W ;W&■ ]■’&c&’Y ’• W;■WO< ’G Z;W/* ’• /W;;O &F -•>{3“ =■—”Q ■^’QT
TF■ ’• W ZK&G&’W; Z■K-;Y ]’&F &F TFKYW• ]■’& ■!;* FK][■, U“ Y ;W&■ W-K■&•* W• ’• •!””•&O ’G I’”, pv,Sv* &FG ’&• [W;! K“ V •FK!;O - ■YK[O WGO &F Z■[’K!• [W;! WGO ]■’&c&’Y ■•&K■O, ™’G/ T ’• /KYY’&&O* ’& ]K!;O •Y &FW& &F [W;! K“ V •FK!;O - &F KG ]■’&&G -< T “K■ “!&!■ ■WO’G”, HK][■* ] W;■WO< •X’ZZO &F ]■’& -< T WGO ’& ’• &KK ;W& &K ■ZW’■ &F OWYW”,
6F’; &F■ W■ YWG< ]W<• &K OW; ]’&F &F Z■K-;Y• Q!•& O•/■’-O* ]
Dirty Reads
67
AS: LRZ4-A1 uWT LOBLY11ABLG LOB-1OE
Y ■WO• V- ] ■’&• V
i i 0 '
- •&W■& Y •&W■&
I’”!■ pv,S:s T■WG•W/&’KG T &■’• &K ]■’& &KK ;W&
- ■WO• V * WGO V ]W• ;W•& ]■’&&G -< YT TF &’Y•&WYZ K“ Y ’• ;•• &FWG &FW& K“ T* WGO &F ■WO -< - K//!■• W“&■ &F ]■’& -< Y ’G ■W; &’Y* •K &F [G& •Y• &K - ZF<•’/W;;< ■W;’>W-;, HK][■* ’& ’• ZK••’-; &FW& W“&■ T ■WO• &F [W;! K“ V ]■’&&G -< Y* &■WG•W/&’KG Y ]’;; W-K■&f Z■FWZ• Y G/K!G&■• WG ■■K■ /KGO’&’KG ’G ’&• K]G OW&W* •!/F W• W O’[’•’KG -< m* K■ W• ] •FW;; • ’G ™/&’KG pv,v,n* &F •/FO!;■ “K■/• Y &K W-K■& -/W!• ’& &■’• &K OK •KY&F’G” ZF<•’/W;;< !G■W;’>W-;, TF!•* W;&FK!”F &F■ ’• GK&F’G” ZF<•’/W;;< !G■W;’>W-; W-K!& - ■WO’G” V * ’& ’• -&&■ &K O;W< T "• ■WO !G&’; Y /KYY’&• K■ W-K■&•, 6 /WG &;; &FW& Y ’• GK& /KYY’&&O -/W!• &F /KYY’& -’& 0 l 3 ' ]’;; - “W;•,
Y ] ■’&• V
- ■WO• V
HmOY •&W■& - •&W■& Y W-K■&•
I’”!■ pv,Sxs T /K!;O Z■“K■Y W O’■&< ■WO ’“ ’& ■WO• V ]FG •FK]G
© •/KGO ZK&G&’W; Z■K-;Y ’• •!””•&O -< I’”, Cpv,Sv, H■* Y* W &■WG•cW/&’KG ]’&F W ;W&■ &’Y•&WYZ &FWG T* FW• ]■’&&G V “’■•&, 6FG - &■’• &K ]■’&* &F WZZ■KZ■’W& W/&’KG ’• &K OK GK&F’G”, b[’OG&;< GK K&F■ &■WG•W/&’KG q &FW& •FK!;O FW[ ■WO T "• [W;! K“ V ”K& “ C"• [W;! ’G•&WO* -/W!• ’“ q &■’O &K ■WO V ’& ]K!;O FW[ W-K■&O -/W!• K“ W &KKc;W& ■WO, I!&!■ ■WO• K“ V ]’;; ]WG& Y"“ [W;! K■ W ;W&■ [W;! K“ V * GK& T "• [W;!, TF’• ’OW* &FW& ]■’&• /WG - •X’ZZO ]FG W ]■’& ]’&F W ;W&■ ]■’&c&’Y ’• W;■WO< ’G Z;W/* ’• /W;;O &F -•>{3“ =■—”Q ■^’QT
TF■ ’• W ZK&G&’W; Z■K-;Y ]’&F &F TFKYW• ]■’& ■!;* FK][■, U“ Y ;W&■ W-K■&•* W• ’• •!””•&O ’G I’”, pv,Sv* &FG ’&• [W;! K“ V •FK!;O - ■YK[O WGO &F Z■[’K!• [W;! WGO ]■’&c&’Y ■•&K■O, ™’G/ T ’• /KYY’&&O* ’& ]K!;O •Y &FW& &F [W;! K“ V •FK!;O - &F KG ]■’&&G -< T “K■ “!&!■ ■WO’G”, HK][■* ] W;■WO< •X’ZZO &F ]■’& -< T WGO ’& ’• &KK ;W& &K ■ZW’■ &F OWYW”,
6F’; &F■ W■ YWG< ]W<• &K OW; ]’&F &F Z■K-;Y• Q!•& O•/■’-O* ]
Thomas’ Write Rule & Pitfalls
68
uWTWT LOBLY11ABLG LOB-1OE m G -vfA7-Zf47 ASx
Y ]■’&• V- ] ■’&• V
- V ~ o) •- •&W■& Y •&W■& - /KY Y ’&• Y W-K■&•
I’”!■ pv,Svs © ]■’& ’• /WG/;;O -/W!• K“ W ]■’& ]’&F W ;W&■ &’Y•&WYZ* -!& &F ]■’&■ &FG W-K■&•
•FW;; WOKZ& W ■;W&’[;< •’YZ; ZK;’/< -W•O KG &F “K;;K]’G” W••!YO /WZW-’;’&< K“ &F &’Y•&WYZc-W•O •/FO!;■,
£ 6FG W &■WG•W/&’KG - ]■’&• W OW&W-W• ;YG& V * &F ]■’& ’• *&G&Wc&’[' WGO YW< - !GOKG ’“ - W-K■&•, TF /KYY’& -’& L HV' ’• •& &K “W;•* WGO &F •/FO!;■ YWX• W /KZ< K“ &F K;O [W;! K“ V WGO ’&• Z■[’K!• 6 T l 3 ' ,
w5v5vy M”F 1&{F* z>< M/•F*▲K•^*nK*F) Dj”F)&{/—“6 /WG GK] •!YYW■’> &F ■!;• &FW& W •/FO!;■ !•’G” &’Y•&WYZ• Y!•& “K;;K] &K YWX •!■ &FW& GK&F’G” ZF<•’/W;;< !G■W;’>W-; YW< K//!■, TF •/FO!;■* ’G ■•ZKG• &K W ■WO K■ ]■’& ■?!•& “■KY W &■WG•W/&’KG - FW• &F /FK’/ K“s
W' .■WG&’G” &F ■?!•&*
-' ©-K■&’G” T l’“ T ]K!;O [’K;W& ZF<•’/W; ■W;’&<' WGO ■•&W■&’G” T ]’&F W G] &’Y•&WYZ lW-K■& “K;;K]O -< ■•&W■& ’• K“&G /W;;O ■>’’j3(\'* K■
/' 9;W<’G” T WGO ;W&■ O/’O’G” ]F&F■ &K W-K■& T K■ &K ”■WG& &F ■?!•& l’“ &F ■?!•& ’• W ■WO* WGO &F ■WO Y’”F& - O’■&<* W• ’G ™/&’KG pv,v,S',
TF ■!;• W■ W• “K;;K]•s
p, ™!ZZK• &F •/FO!;■ ■/’[• W ■?!•& ■■ HV 'T
lW' U“ T ™ lT' P 6 T l 3 ' * &F ■WO ’• ZF<•’/W;;< ■W;’>W-;,’, U“ ( HV ' ’• &■!* ”■WG& &F ■?!•&, U“ T ™ lT' P w T l 3 ' * •&
w T l 3 ' sk T ™ lT'f K&F■]’• OK GK& /FWG” w T l 3 ' ,
’’, U“ LHV' ’• “W;•* O;W< - !G&’; LHV' -/KY• &■!* K■ &F &■WG•cW/&’KG &FW& ]■K& V W-K■&•,
l-' U“ T ™ lT' M 6 T l 3 ' * &F ■WO ’• ZF<•’/W;;< !G■W;’>W-;, wK;;-W/X Tf &FW& ’•* W-K■& - WGO ■•&W■& ’& ]’&F W G]* ;W■”■ &’Y•&WYZ,
u, ™!ZZK• &F •/FO!;■ ■/’[• W ■?!•& J o , 0 ' Q
Timestamp-based CC Example
69
uWTWT LOBLY11ABLG LOB-1OE m G -vfA7-Zf47 ASA
'< '< '4 Z R LJNN pgm pxg wTkm
6 TkmwTkm6 Tkm
wTkm6Tkm
En R— -■k HZ'C wTkpgm
' ww &K N N
V< nR—-■!£h#X*
6TkummwTkpxg
&h/d’lf=k HL'f © -K■&f
=!£Z'2
6Tkumm
I’”!■ pv,SAs TF■ &■WG•W/&’KG• ~/!&’G” !GO■ W &’Y•&WYZc-W•O •/FO!;■
4~&* -— &■’• &K ]■’& 0, HK][■* L ]W• W;■WO< ■WO -< &■WG•W/&’KG Ts* ]F’/F &FK■&’/W;;< ~/!&O W& &’Y pxg* ]F’; -i ]K!;O FW[ ]■’&&G ’&• [W;! W& &’Y pgm, TF!•* -k ’• &■<’G” &K OK •KY&F’G” &FW& • ZF<•’/W;;< !G■W;’>W-;* WGO -— Y!•& - ■K;;O -W/X,
TF ;W•& •&Z ’• &F ]■’& K“ Z -< Ts, ™’G/ &F ■WO &’Y K“ Z* pgm* ’• ;•• &FWG &F &’Y•&WYZ K“ -e* pxg* &F ]■’& ’• ;”W;, HK][■* &F■ ’• W;■WO< W ;W&■ [W;! K“ Z •&K■O ’G &FW& OW&W-W• ;YG&* GWY;< &F [W;! ]■’&&G -< 3’* &FK■&’/W;;< W& &’Y umm, TF!•* Ts ’• GK& ■K;;O -W/X* -!& G’&F■ OK• ’& ]■’& ’&• [W;!, @
w5v5v0 G&{▲/$F<*/>— M/•F*▲K•^*©G ’YZK■&WG& [W■’W&’KG K“ &’Y•&WYZ’G” YW’G&W’G• K;O [■•’KG• K“ OW&W-W• ;YG&• ’G WOO’&’KG &K &F /!■■G& [■•’KG &FW& ’• •&K■O ’G &F OW&W-W• ’&•;“, TF Z!■ZK• ’• &K W;;K] ■WO• ■I£V' &FW& K&F■]’• ]K!;O /W!• &■WG•W/&’KG - &K W-K■& l-/W!• &F /!■■G& [■•’KG K“ V ]W• ]■’&&G ’G T "• “!&!■' &K Z■K/O -< ■WO’G” &F [■•’KG K“ V &FW& ’• WZZ■KZ■’W& “K■ W &■WG•W/&’KG ]’&F T "• &’Y•&WYZ, TF Y&FKO ’• •Z/’W;;< !•“!; ’“ OW&W-W• ;YG&• W■ O’•X -;K/X• K■ ZW”•* •’G/ &FG W;; &FW& Y!•& - OKG ’• “K■ &F -!““■ YWGW”■ &K XZ ’G YYK■< /■&W’G -;K/X• &FW& Y’”F& - !•“!; “K■ •KY /!■■G&;< W/&’[ &■WG•W/&’KG,
b ~WY Z; pv,uxs 0KG•’O■ &F •& K“ &■WG•W/&’KG• W//••’G” OW&W-W• ;YG& Z •FK]G ’G I’”, pv,nm, TF• &■WG•W/&’KG• W■ KZ■W&’G” !GO■ WG K■O’GW■< &’Y•&WYZc-W•O •/FO!;■* WGO ]FG Ts &■’• &K ■WO Z* ’& “’GO• 6Tl©' &K - ”■W&■ &FWG ’&• K]G &’Y•&WYZ* WGO Y!•& W-K■&, HK][■* &F■ ’• WG K;O [W;! K“ Z ]■’&&G -< T’ WGO K[■]■’&&G -< -— &FW& ]K!;O FW[ -G •!’&W-; “K■ Ts &K ■WOf &F’• [■•’KG K“ Z FWO W ]■’& &’Y K“ pgm* ]F’/F ’• ;•• &FWG Ts"• &’Y•&WYZ K“ pxg, U“ &F’• K;O [W;! K“ Z ]■ W[W’;W-;* Ts /K!;O - W;;K]O &K ■WO ’&* [G &FK!”F ’& ’• GK& &F */!■■G&' [W;! K“ ZT @
Can we do better?
70
Anm LRZ4-A1 uWT LOBLY11ABLG LOB-1OE
-„ -k -e -l Zpgm umm pxg uug wTkm
6Tkm■— HZ' wTkpgm=—HZ' 6Tkpgm
■k HZ' s UU &K K K
=k HZ' 6Tkumm■e HZ'© - K ■&
^ HZ ' wTkuug
I’”!■ pv,nms -e Y!•& W-K■& -/W!• ’& /WGGK& W//•• WG K;O [W;! K“ Z
© Y!;&’[■•’KGc&’Y•&WYZ •/FO!;■ O’““■• “■KY &F •/FO!;■ O•/■’-O ’G ™/&’KG pv,v,n ’G &F “K;;K]’G” ]W<•s
p, 6FG W G] ]■’& Jox0' K//!■•* ’“ ’& ’• ;”W;* &FG W G] [■•’KG K“ OW&W-W• ;YG& V ’• /■W&O, U&• ]■’& &’Y ’• T™lT'* WGO ] •FW;; ■“■ &K ’& W• V ”* ]F■ ” k T™lT',
u, 6FG W ■WO ■■£V ' K//!■•* &F •/FO!;■ “’GO• &F [■•’KG V ” K“ V •!/F &FW& ” M T™lT'* -!& &F■ ’• GK K&F■ [■•’KG V + ]’&F ” h ”. M T™H-'T TFW& ’•* &F [■•’KG K“ V ]■’&&G ’YYO’W&;< -“K■ - &FK■&’/W;;< ~c/!&O ’• &F [■•’KG &FW& - ■WO•,
S, 6■’& &’Y• W~ W••K/’W&O ]’&F <Q■“—>'“ K“ WG ;YG&* WGO &F< G[■ /FWG”,
n, wWO &’Y• W~ W;•K W••K/’W&O ]’&F [■•’KG•, TF< W■ !•O &K ■Q/& /■&W’G ]■’&•* GWY;< KG ]FK• &’Y ’• ;•• &FWG &F ■WO &’Y K“ &F Z■[’K!• [■•’KG, I’”!■ pv,np •!””•&• &F Z■K-;Y* ]F■ V FW• [■•’KG• V b> WGO ,3’KK* &F “K■Y■ ]W• ■WO -< W &■WG•W/&’KG ]’&F &’Y•&WYZ vm* WGO W G] ]■’& -< W &■WG•W/&’KG - ]FK• &’Y•&WYZ ’• :m K//!■•, TF’• ]■’& Y!•& /W!• T &K W-K■&* -/W!• ’&• [W;! K“ V •FK!;O FW[ -G ■WO -< &F &■WG•W/&’KG ]’&F &’Y•&WYZ vm* FWO - -G W;;K]O &K ~/!&,
g, 6FG W [■•’KG V ” FW• W ]■’& &’Y ” •!/F &FW& GK W/&’[ &■WG•W/&’KG FW• W &’Y•&WYZ ;•• &FWG ”* &FG ] YW< O;& WG< [■•’KG K“ V #■Q<—>^“ &KV ” T
b ~WY Z; pv,uvs J& !• ■/KG•’O■ &F W/&’KG• K“ I’”, pv,nm ’“ Y!;&’[■•’KG &’Y•&WYZ’G” ’• !•O, I’■•&* &F■ W■ &F■ [■•’KG• K“ Zs Z&* ]F’/F ~’•&• -“K■ &F• &■WG•W/&’KG• •&W~&* ©pgm* ]■’&&G -< T’* WGO c©uKKf ]■’&&G -< Tu, I’”!■ pv,nu •FK]• &F •?!G/ K“ [G&•* ]FG &F [■•’KG• W■ /■W&O* WGO ]FG &F< W■ ■WO, 4K&’/ ’G ZW■&’/!;W■ &FW& -e OK• GK& FW[ &K W-K■&* -/W!• ’& /WG ■WO WG W■;’■ [■•’KG K“ ZT @
Can we do better?
71
Anm LRZ4-A1 uWT LOBLY11ABLG LOB-1OE
-„ -k -e -l Zpgm umm pxg uug wTkm
6Tkm■— HZ' wTkpgm=—HZ' 6Tkpgm
■k HZ' s UU &K K K
=k HZ' 6Tkumm■e HZ'© - K ■&
^ HZ ' wTkuug
I’”!■ pv,nms -e Y!•& W-K■& -/W!• ’& /WGGK& W//•• WG K;O [W;! K“ Z
© Y!;&’[■•’KGc&’Y•&WYZ •/FO!;■ O’““■• “■KY &F •/FO!;■ O•/■’-O ’G ™/&’KG pv,v,n ’G &F “K;;K]’G” ]W<•s
p, 6FG W G] ]■’& Jox0' K//!■•* ’“ ’& ’• ;”W;* &FG W G] [■•’KG K“ OW&W-W• ;YG& V ’• /■W&O, U&• ]■’& &’Y ’• T™lT'* WGO ] •FW;; ■“■ &K ’& W• V ”* ]F■ ” k T™lT',
u, 6FG W ■WO ■■£V ' K//!■•* &F •/FO!;■ “’GO• &F [■•’KG V ” K“ V •!/F &FW& ” M T™lT'* -!& &F■ ’• GK K&F■ [■•’KG V + ]’&F ” h ”. M T™H-'T TFW& ’•* &F [■•’KG K“ V ]■’&&G ’YYO’W&;< -“K■ - &FK■&’/W;;< ~c/!&O ’• &F [■•’KG &FW& - ■WO•,
S, 6■’& &’Y• W~ W••K/’W&O ]’&F <Q■“—>'“ K“ WG ;YG&* WGO &F< G[■ /FWG”,
n, wWO &’Y• W~ W;•K W••K/’W&O ]’&F [■•’KG•, TF< W■ !•O &K ■Q/& /■&W’G ]■’&•* GWY;< KG ]FK• &’Y ’• ;•• &FWG &F ■WO &’Y K“ &F Z■[’K!• [■•’KG, I’”!■ pv,np •!””•&• &F Z■K-;Y* ]F■ V FW• [■•’KG• V b> WGO ,3’KK* &F “K■Y■ ]W• ■WO -< W &■WG•W/&’KG ]’&F &’Y•&WYZ vm* WGO W G] ]■’& -< W &■WG•W/&’KG - ]FK• &’Y•&WYZ ’• :m K//!■•, TF’• ]■’& Y!•& /W!• T &K W-K■&* -/W!• ’&• [W;! K“ V •FK!;O FW[ -G ■WO -< &F &■WG•W/&’KG ]’&F &’Y•&WYZ vm* FWO - -G W;;K]O &K ~/!&,
g, 6FG W [■•’KG V ” FW• W ]■’& &’Y ” •!/F &FW& GK W/&’[ &■WG•W/&’KG FW• W &’Y•&WYZ ;•• &FWG ”* &FG ] YW< O;& WG< [■•’KG K“ V #■Q<—>^“ &KV ” T
b ~WY Z; pv,uvs J& !• ■/KG•’O■ &F W/&’KG• K“ I’”, pv,nm ’“ Y!;&’[■•’KG &’Y•&WYZ’G” ’• !•O, I’■•&* &F■ W■ &F■ [■•’KG• K“ Zs Z&* ]F’/F ~’•&• -“K■ &F• &■WG•W/&’KG• •&W~&* ©pgm* ]■’&&G -< T’* WGO c©uKKf ]■’&&G -< Tu, I’”!■ pv,nu •FK]• &F •?!G/ K“ [G&•* ]FG &F [■•’KG• W■ /■W&O* WGO ]FG &F< W■ ■WO, 4K&’/ ’G ZW■&’/!;W■ &FW& -e OK• GK& FW[ &K W-K■&* -/W!• ’& /WG ■WO WG W■;’■ [■•’KG K“ ZT @
A@0 A@150 A@200
Multi-version concurrency control
Generalize use of multiple versions of objects
72
• Maintain multiple versions of objects, each with own timestamp. Deliver correct version to reads.• Reduces read/write conflicts• Can also use for weaker isolation models:
Snapshot Isolation• Unlike 2PL/OCC, reads never rejected
• Occasionally run garbage collection to clean up• No version of a record can be seen that is earlier than the
first version before the oldest active transaction
73
Multi-version concurrency control
• Transactions are assigned timestamps, which may get assigned to versions of records txns read/write• Every object version OV has both read and write TS• ReadTS: Largest timestamp of txn that read OV
• WriteTS: Timestamp of txn that wrote OV
74
Timestamps in MVCC
uWTWT LOBLY11ABLG LOB-1OE m G -vfA7-Zf47 Anp
wT k vm
A" C "
3 gm a 3 pmm© &&Y Z& &K
] ■’& -< &■WG•W/&’KG]’&F &’Y •&WY Z :m
I’”!■ pv,nps © &■WG•W/&’KG &■’• &K ]■’& W [■•’KG K“ V &FW& ]K!;O YWX [G&• ZF<•’/W;;< !G■W;’>W-;
-„ ” k ” e -— rj cDncpgm )ummpgm umm pxg uug■ ’ HZ' =—HZ'
■k HZ'/� yca
■S)n'■l HZ'
wWO0■W&wWO
wWO0■W&
wWO
I’”!■ pv,nus b~/!&’KG K“ &■WG•W/&’KG• !•’G” Y!;&’[■•’KG /KG/!■■G/< /KG&■K;
w5v5v7 M/•F*▲K•^* IF<*&* m>j=/—“.G■W;;<* &’Y•&WYZ’G” ’• •!Z■’K■ ’G •’&!W&’KG• ]F■ ’&F■ YK•& &■WG•W/&’KG• W■ ■WOcKG;<* K■ ’& ’• ■W■ &FW& /KG/!■■G& &■WG•W/&’KG• ]’;; &■< &K ■WO WGO ]■’& &F •WY ;YG&, UG F’”Fc/KG“;’/& •’&!W&’KG•* ;K/X’G” Z■“K■Y• -&&■, TF W■”!YG& “K■ &F’• ■!;cK“c&F!Y- ’•s
£ JK/X’G” ]’;; “■?!G&;< O;W< &■WG•W/&’KG• W• &F< ]W’& “K■ ;K/X•,
£ 2!& ’“ /KG/!■■G& &■WG•W/&’KG• “■?!G&;< ■WO WGO ]■’& ;YG&• ’G /KYcYKG* &FG ■K;;-W/X• ]’;; - “■?!G& ’G W &’Y•&WYZ •/FO!;■* ’G&■KO!/’G” [G YK■ O;W< &FWG W ;K/X’G” •<•&Y,
TF■ ’• WG ’G&■•&’G” /KYZ■KY’• !•O ’G •[■W; /KYY■/’W; •<•&Y•, TF •/FO!;■ O’[’O• &F &■WG•W/&’KG• ’G&K ■WOcKG;< &■WG•W/&’KG• WGO ■WOC]■’& &■WG•W/&’KG•, wWOC]■’& &■WG•W/&’KG• W■ ~/!&O !•’G” &]KcZFW• ;K/X’G”* &K XZ W;; &■WG•W/&’KG• “■KY W//••’G” &F ;YG&• &F< ;K/X,
wWOcKG;< &■WG•W/&’KG• W■ ~/!&O !•’G” Y!;&’[■•’KG &’Y•&WYZ’G”, ©• &F ■WOC]■’& &■WG•W/&’KG• /■W& G] [■•’KG• K“ W OW&W-W• ;YG&* &FK• [■•’KG• W■ YWGW”O W• ’G ™/&’KG pv,v,g, © ■WOcKG;< &■WG•W/&’KG ’• W;;K]O &K ■WO ]FW&[■ [■•’KG K“ W OW&W-W• ;YG& ’• WZZ■KZ■’W& “K■ ’&• &’Y•&WYZ, © ■WOcKG;< &■WG•W/&’KG &F!• G[■ FW• &K W-K■&* WGO ]’;; KG;< ■W■;< - O;W<O,
• Perform write of object O or abort if conflicting:• Find OV s.t. max { WriteTS(OV) | WriteTS(OV) <= TS(T) }• # Abort if another T’ has read O after T• If ReadTS(OV) > TS(T)
• Abort and roll-back T• Else
• Create new version OW• Set ReadTS(OW) = WriteTS(OW) = TS(T) 75
Executing transaction T in MVCC• Find version of object O to read:
– # Determine the last version written before tx timestamp– Find OV s.t. max { WriteTS(OV) | WriteTS(OV) <= TS(T) }– ReadTS(OV) = max(TS(T), ReadTS(OV))– Return OV to T
write(O)by TS=3
76
Digging deeper
O
TS = 3
txn txn
TS = 4
txn
TS = 5
Notation
W(1) = 3: Write creates version 1 with WriteTS = 3
R(1) = 3: Read of version 1 returns timestamp 3
write(O)by TS=5
77
Digging deeper
O
TS = 3
txn txn
TS = 4
txn
TS = 5
Notation
W(1) = 3: Write creates version 1 with WriteTS = 3
R(1) = 3: Read of version 1 returns timestamp 3
W(1) = 3R(1) = 3
78
Digging deeper
O
W(2) = 5R(2) = 5
TS = 3
txn txn
TS = 4
txn
TS = 5
Find v such that max WriteTS(v) <= (TS = 4)Þ v = 1 has (WriteTS = 3) <= 4
If ReadTS(1) > 4, abortÞ 3 > 4: false
Otherwise, write object
write(O)by TS = 4
Notation
W(1) = 3: Write creates version 1 with WriteTS = 3
R(1) = 3: Read of version 1 returns timestamp 3
W(1) = 3R(1) = 3
79
Digging deeper
O
W(2) = 5R(2) = 5
TS = 3
txn txn
TS = 4
txn
TS = 5
W(3) = 4R(3) = 4
Find v such that max WriteTS(v) <= (TS = 4)Þ v = 1 has (WriteTS = 3) <= 4
If ReadTS(1) > 4, abortÞ 3 > 4: false
Otherwise, write object
Notation
W(1) = 3: Write creates version 1 with WriteTS = 3
R(1) = 3: Read of version 1 returns timestamp 3
W(1) = 3R(1) = 3
80
Digging deeper
O
TS = 3
txn txn
TS = 4
txn
TS = 5
BEGIN Transactiontmp = READ(O)WRITE (O, tmp + 1)
END Transaction
Find v such that max WriteTS(v) <= (TS = 5)Þ v = 1 has (WriteTS = 3) <= 5
Set R(1) = max(5, R(1)) = 5
Notation
W(1) = 3: Write creates version 1 with WriteTS = 3
R(1) = 3: Read of version 1 returns timestamp 3
W(1) = 3R(1) = 3R(1) = 5
81
Digging deeper
O
TS = 3
txn txn
TS = 4
txn
TS = 5
Find v such that max WriteTS(v) <= (TS = 5)Þ v = 1 has (WriteTS = 3) <= 5
If ReadTS(1) > 5, abortÞ 5 > 5: false
Otherwise, write object
BEGIN Transactiontmp = READ(O)WRITE (O, tmp + 1)
END Transaction
W(2) = 5R(2) = 5
Notation
W(1) = 3: Write creates version 1 with WriteTS = 3
R(1) = 3: Read of version 1 returns timestamp 3
W(1) = 3R(1) = 3R(1) = 5
82
Digging deeper
O
TS = 3
txn txn
TS = 4
txn
TS = 5
W(2) = 5R(2) = 5
Find v such that max WriteTS(v) <= (TS = 4)Þ v = 1 has (WriteTS = 3) <= 4
If ReadTS(1) > 4, abortÞ 5 > 4: true
write(O)by TS = 4
W(1) = 3R(1) = 3
Notation
W(1) = 3: Write creates version 1 with WriteTS = 3
R(1) = 3: Read of version 1 returns timestamp 3
R(1) = 5
83
Digging deeper
O
TS = 3
txn txn
TS = 4
txn
TS = 5
W(2) = 5R(2) = 5
W(1) = 3R(1) = 3
Notation
W(1) = 3: Write creates version 1 with WriteTS = 3
R(1) = 3: Read of version 1 returns timestamp 3
BEGIN Transactiontmp = READ(O)WRITE (P, tmp + 1)
END Transaction
Find v such that max WriteTS(v) <= (TS = 4)Þ v = 1 has (WriteTS = 3) <= 4
Set R(1) = max(4, R(1)) = 5
R(1) = 5R(1) = 5
Read on O succeeds “in the past”,write on P doesn’t conflict, so commit ok
• Most commercial DBMS give up serializability to reduce aborts and CC overhead
• Simple relaxation:choose timestamp read from committed txns < timestampno need to protect/validate read set in MVCCbut read at a single timestamp
• Split transaction into read set and write set• All reads execute as if one “snapshot”• All writes execute as if one later “snapshot”
• Yields snapshot isolation < serializability
84
Snapshot Isolation (SI)
• Intuition: Bag of marbles: ½ white, ½ black• Transactions:• T1: Change all white marbles to black marbles• T2: Change all black marbles to white marbles
• Serializability (2PL, OCC) • T1 → T2 or T2 → T1• In either case, bag is either ALL white or ALL black
• Snapshot isolation (MVCC)• T1 → T2 or T2 → T1 or T1 || T2• Bag is ALL white, ALL black, or ½ white ½ black
85
Serializability vs. Snapshot isolation
86
Recommended