B+Tree-indexconcurrency
B+Tree-indexconcurrency
Control
ADMINISTRIVIA
O B S E R VAT I O N
O B S E R VAT I O N
CONCURRENCY CONTROL
CONCURRENCY CONTROL
T O D AY ' S A G E N D A
Latch Modes
Index Crabbing/Coupling
Leaf Scans
Delayed Parent Updates
L O C K S V S . L AT C H E S
Locks
→ Protects the index’s logical contents from other txns.
→ Held for txn duration.
→ Need to be able to rollback changes.
Latches
→ Protects the critical sections of the index’s internal data
structure from other threads.
→ Held for operation duration.
→ Do not need to be able to rollback changes.
L O C K S V S . L AT C H E S
Locks Latches
User transactions Threads
Database Contents In-Memory Data Structures
Entire Transactions Critical Sections
Shared, Exclusive, Update, Read, Write
Intention
Deadlock Detection & Resolution Avoidance
Waits-for, Timeout, Aborts Coding Discipline
Kept Lock Manager Protected Data Structure
Source: Goetz Graefe
CMU 15-445/645 (Fall 2018)
7
L O C K S V S . L AT C H E S
Locks Latches
User transactions Threads
Database Contents In-Memory Data Structures
Entire Transactions Critical Sections
Shared, Exclusive, Update, Read, Write
Intention
Deadlock Detection & Resolution Avoidance
Waits-for, Timeout, Aborts Coding Discipline
Kept Lock Manager Protected Data Structure
Source: Goetz Graefe
CMU 15-445/645 (Fall 2018)
7
L O C K S V S . L AT C H E S
Locks Latches
User transactions Threads
Database Contents In-Memory Data Structures
Entire Transactions Critical Sections
Shared, Exclusive, Update, Read, Write
Intention
Deadlock Detection & Resolution Avoidance
Waits-for, Timeout, Aborts Coding Discipline
Kept Lock Manager Protected Data Structure
Source: Goetz Graefe
CMU 15-445/645 (Fall 2018)
8
L AT C H M O D E S
Write Mode
→ Only one thread is allowed to access the
item.
→ A thread cannot acquire a write latch if
another thread holds the latch in any mode.
B + T R E E M U LT I -T H R E A D E D E X A M P L E
20 A T1: Delete 44
10 35 B
6 12 23 31 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
10
B + T R E E M U LT I -T H R E A D E D E X A M P L E
20 A T1: Delete 44
10 35 B
6 12 23 31 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
10
B + T R E E M U LT I -T H R E A D E D E X A M P L E
20 A T1: Delete 44
10 35 B
6 12 23 31 C 38 44 D
Rebalance!
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
10
B + T R E E M U LT I -T H R E A D E D E X A M P L E
20 A T1: Delete 44
T2: Find 41
10 35 B
6 12 23 31 C 38 44 D
Rebalance!
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
10
B + T R E E M U LT I -T H R E A D E D E X A M P L E
20 A T1: Delete 44
T2: Find 41
10 35 B
6 12 23 31 C 38 44 D
Rebalance!
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
10
B + T R E E M U LT I -T H R E A D E D E X A M P L E
20 A T1: Delete 44
T2: Find 41
10 35 B
6 12 23 31 C 38 41
44 D
Rebalance!
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 44
41
E F G H I
CMU 15-445/645 (Fall 2018)
10
B + T R E E M U LT I -T H R E A D E D E X A M P L E
20 A T1: Delete 44
T2: Find 41
10 35 B
6 12 23 31 C 38 41
44 D
Rebalance!
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 44
41
E F G H I
???
CMU 15-445/645 (Fall 2018)
11
L AT C H C R A B B I N G / C O U P L I N G
L AT C H C R A B B I N G / C O U P L I N G
EXAMPLE #1 SEARCH 38
R
20 A
10 35 B
6 12 23 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
13
EXAMPLE #1 SEARCH 38
R
20 A
R
10 35 B
It’s safe to release the
latch on A.
6 12 23 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
13
EXAMPLE #1 SEARCH 38
20 A
R
10 35 B
6 12 23 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
13
EXAMPLE #1 SEARCH 38
20 A
10 35 B
R
6 12 23 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
13
EXAMPLE #1 SEARCH 38
20 A
10 35 B
6 12 23 C 38 44 D
R
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
13
EXAMPLE #1 SEARCH 38
20 A
10 35 B
6 12 23 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
14
EXAMPLE #2 DELETE 38
W
20 A
10 35 B
6 12 23 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
14
EXAMPLE #2 DELETE 38
W
20 A
W
10 35 B
We may need to coalesce B, so
we can’t release the latch on A.
6 12 23 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
14
EXAMPLE #2 DELETE 38
W
20 A
W
10 35 B
W
6 12 23 C 38 44 D
We know that D will not need
to merge with C, so it’s safe to
3 4 6 9 10 11 12 13 20latches
release 22 23on31A and35 B.36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
14
EXAMPLE #2 DELETE 38
20 A
10 35 B
W
6 12 23 C 38 44 D
We know that D will not need
to merge with C, so it’s safe to
3 4 6 9 10 11 12 13 20latches
release 22 23on31A and35 B.36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
14
EXAMPLE #2 DELETE 38
20 A
10 35 B
6 12 23 C 38 44 D
W
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
14
EXAMPLE #2 DELETE 38
20 A
10 35 B
6 12 23 C 38 44 D
W
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
14
EXAMPLE #2 DELETE 38
20 A
10 35 B
6 12 23 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
15
EXAMPLE #3 INSERT 45
W
20 A
W
10 35 B
6 12 23 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44 45
E F G H I
CMU 15-445/645 (Fall 2018)
15
EXAMPLE #3 INSERT 45
W
20 A
W
10 35 B
6 12 23 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44 45
E F G H I
CMU 15-445/645 (Fall 2018)
15
EXAMPLE #3 INSERT 45
20 A
W
10 35 B
W
6 12 23 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44 45
E F G H I
CMU 15-445/645 (Fall 2018)
15
EXAMPLE #3 INSERT 45
20 A
W
10 35 B
W
6 12 23 C 38 44 D
W
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44 45
EI has room
F so it won’t
G split,
H I
so we can release B+D.
CMU 15-445/645 (Fall 2018)
15
EXAMPLE #3 INSERT 45
20 A
10 35 B
6 12 23 C 38 44 D
W
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44 45
EI has room
F so it won’t
G split,
H I
so we can release B+D.
CMU 15-445/645 (Fall 2018)
16
EXAMPLE #4 INSERT 25
W
20 A
W
10 35 B
6 12 23 31 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
16
EXAMPLE #4 INSERT 25
20 A
W
10 35 B
6 12 23 31 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
16
EXAMPLE #4 INSERT 25
20 A
W
10 35 B
W
6 12 23 31 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
16
EXAMPLE #4 INSERT 25
20 A
10 35 B
W
6 12 23 31 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
16
EXAMPLE #4 INSERT 25
20 A
10 35 B
W
6 12 23 31 C 38 44 D
W
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
We need to split F so we need toE F G H I
keep the latch on its parent node.
CMU 15-445/645 (Fall 2018)
16
EXAMPLE #4 INSERT 25
20 A
10 35 B
W
6 12 23 31 C 38 44 D
W
3 4 6 9 10 11 12 13 20 22 23 31
25 35 36 38 41 44
We need to split F so we need toE F G H I
keep the latch on its parent node.
CMU 15-445/645 (Fall 2018)
16
EXAMPLE #4 INSERT 25
20 A
10 35 B
W
6 12 23 31 C 38 44 D
W
3 4 6 9 10 11 12 13 20 22 23 31
25 35 36 38 41 44
We need to split F so we need toE F G H I
keep the latch on its parent node.
31
CMU 15-445/645 (Fall 2018)
17
O B S E R VAT I O N
W W W
20 A 20 A 20 A
O B S E R VAT I O N
Can we do better?
B E T T E R L AT C H I N G A L G O R I T H M
EXAMPLE #2 DELETE 38
R
20 A
10 35 B
6 12 23 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
20
EXAMPLE #2 DELETE 38
20 A
R
10 35 B
6 12 23 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
20
EXAMPLE #2 DELETE 38
20 A
10 35 B
R
6 12 23 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
20
EXAMPLE #2 DELETE 38
20 A
10 35 B
R
6 12 23 C 38 44 D
W
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
20
EXAMPLE #2 DELETE 38
20 A
10 35 B
6 12 23 C 38 44 D
W
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
HE
will not F G
need to coalesce,Hso I
we’re safe!
CMU 15-445/645 (Fall 2018)
20
EXAMPLE #2 DELETE 38
20 A
10 35 B
6 12 23 C 38 44 D
W
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
HE
will not F G
need to coalesce,Hso I
we’re safe!
CMU 15-445/645 (Fall 2018)
20
EXAMPLE #2 DELETE 38
20 A
10 35 B
6 12 23 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
HE
will not F G
need to coalesce,Hso I
we’re safe!
CMU 15-445/645 (Fall 2018)
21
EXAMPLE #4 INSERT 25
20 A
10 35 B
6 12 23 31 C 38 44 D
W
3 4 6 We
9 need
10 11 12 13
to split 20 22 23 31
F so we 25 35 36 38 41 44
have to restart and re- E F G H I
execute like before.
CMU 15-445/645 (Fall 2018)
22
B E T T E R L AT C H I N G A L G O R I T H M
O B S E R VAT I O N
1 2 3 4
B C
R
1 2 3 4
B C
R
1 2 3 4
B C
1 2 3 4
B C
R R
1 2 3 4
B C
R R
1 2 3 4
B C
R R
1 2 3 4
B C
R R
1 2 3 4
B C
1 2 3 4
B C
R W
1 2 3 4
B C
D E L AY E D PA R E N T U P D AT E S
EXAMPLE #4 INSERT 25
R
20 A
R
10 35 B
6 12 23 31 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
29
EXAMPLE #4 INSERT 25
20 A
R
10 35 B
R
6 12 23 31 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
29
EXAMPLE #4 INSERT 25
20 A
10 35 B
R
6 12 23 31 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
E F G H I
CMU 15-445/645 (Fall 2018)
29
EXAMPLE #4 INSERT 25
20 A
10 35 B
6 12 23 31 C 38 44 D
W
3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44
Add the new leaf node as a E F G H I
sibling to F, but do not update C
CMU 15-445/645 (Fall 2018)
29
EXAMPLE #4 INSERT 25
20 A
10 35 B
6 12 23 31 C 38 44 D
W
3 4 6 9 10 11 12 13 20 22 23 31
25 35 36 38 41 44
Add the new leaf node as a E F G H I
sibling to F, but do not update C
31
CMU 15-445/645 (Fall 2018)
29
EXAMPLE #4 INSERT 25
20 A
10 35 B
6 12 23 31 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31
25 35 36 38 41 44
Add the new leaf node as a E F G H I
sibling to F, but do not update C
31
CMU 15-445/645 (Fall 2018)
29
EXAMPLE #4 INSERT 25
20 A
10 35 B
Update C the next time that a
thread takes a write latch on it.
6 12 23 31 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31
25 35 36 38 41 44
E F G H I
31
CMU 15-445/645 (Fall 2018)
29
EXAMPLE #4 INSERT 25
R
20 A
10 35 B
Update C the next time that a
thread takes a write latch on it.
6 12 23 31 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31
25 35 36 38 41 44
E F G H I
31
CMU 15-445/645 (Fall 2018)
29
EXAMPLE #4 INSERT 25
20 A
R
10 35 B
Update C the next time that a
thread takes a write latch on it.
6 12 23 31 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31
25 35 36 38 41 44
E F G H I
31
CMU 15-445/645 (Fall 2018)
29
EXAMPLE #4 INSERT 25
20 A
R
10 35 B
Update C the next time that a
thread takes a write latch on it.
W
6 12 23 31 C 38 44 D
3 4 6 9 10 11 12 13 20 22 23 31
25 35 36 38 41 44
E F G H I
31
CMU 15-445/645 (Fall 2018)
30
CONCLUSION
PROJECT #2
+
You will build a thread-safe B+tree.
→ Page Layout
→ Data Structure
→ STL Iterator
→ Latch Crabbing
https://15445.courses.cs.cmu.edu/fall2018/project2/
CHECKPOINT #1
Page Layouts
→ How each node will store its key/values in a page.
→ You only need to support unique keys.
CHECKPOINT #2
Index Iterator
→ Create a STL iterator for range scans.
Concurrent Index
→ Implement latch crabbing/coupling.
CMU 15-445/645 (Fall 2018)
34
DEVELOPMENT HINTS
THINGS TO NOTE
Do not change any file other than the ten that you
have to hand it.
P L A G I A R I S M WA R N I N G
NEXT CLASS