advanced 6 database systemssearch: start at root and go down; repeatedly, →acquire read (r) latch...

121
Index Locking & Latching @Andy_Pavlo // 15-721 // Spring 2019 ADVANCED DATABASE SYSTEMS Lecture #06

Upload: others

Post on 03-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

Index Locking & Latching

@Andy_Pavlo // 15-721 // Spring 2019

ADVANCEDDATABASE SYSTEMS

Le

ctu

re #

06

Page 2: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

TODAY'S AGENDA

Index Locks vs. Latches

Latch Implementations

Index Latching (Logical)

Index Locking (Physical)

2

Page 3: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

DATABASE INDEX

A data structure that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space.

Indexes are used to quickly locate data without having to search every row in a table every time a table is accessed.

3

Page 4: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

DATA STRUCTURES

Order Preserving Indexes→ A tree-like structure that maintains keys in some sorted

order.→ Supports all possible predicates with O(log n) searches.

Hashing Indexes→ An associative array that maps a hash of the key to a

particular record.→ Only supports equality predicates with O(1) searches.

4

Page 5: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

B-TREE VS. B+TREE

The original B-tree from 1972 stored keys + values in all nodes in the tree.→ More memory efficient since each key only appears once

in the tree.

A B+tree only stores values in leaf nodes. Inner nodes only guide the search process.→ Easier to manage concurrent index access when the

values are only in the leaf nodes.

5

Page 6: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

OBSERVATION

We already know how to use locks to protect objects in the database.

But we have to treat indexes differently because the physical structure can change as long as the logical contents are consistent.

6

Page 7: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

SIMPLE EXAMPLE

7

AK0 K2

Txn #1:READ(K2)

Page 8: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

SIMPLE EXAMPLE

7

AK0 K2

Txn #2:

Txn #1:

INSERT(K1)

READ(K2)

Page 9: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

SIMPLE EXAMPLE

7

AK0 K2

BK0 K2 C

Txn #2:

Txn #1:

INSERT(K1)

READ(K2)

Page 10: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

SIMPLE EXAMPLE

7

AK0 K2

BK0 K2 C

Txn #2:

K1

K1 K2

Txn #1:

INSERT(K1)

READ(K2)

Page 11: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

SIMPLE EXAMPLE

7

AK0 K2

BK0 K2 C

Txn #2:

K1

Txn #1:

K1 K2

Txn #1:

INSERT(K1)

READ(K2)

READ(K2)

Page 12: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

LOCKS VS. L ATCHES

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.

8

A SURVEY OF B-TREE LOCKING TECHNIQUESTODS 2010

Page 13: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

LOCKS VS. L ATCHES

9

Locks Latches

Separate… User transactions Threads

Protect… Database Contents In-Memory Data Structures

During… Entire Transactions Critical Sections

Modes… Shared, Exclusive, Update, Intention

Read, Write

Deadlock Detection & Resolution Avoidance

…by… Waits-for, Timeout, Aborts Coding Discipline

Kept in… Lock Manager Protected Data StructureSource: Goetz Graefe

Page 14: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

LOCK-FREE INDEXES

Possibility #1: No Locks→ Txns don’t acquire locks to access/modify database.→ Still have to use latches to install updates.

Possibility #2: No Latches→ Swap pointers using atomic updates to install changes.→ Still have to use locks to validate txns.

10

Page 15: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Blocking OS Mutex

Test-and-Set Spinlock

Queue-based Spinlock

Reader-Writer Locks

11

Source: Anastasia Ailamaki

Page 16: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

COMPARE-AND-SWAP

Atomic instruction that compares contents of a memory location M to a given value V→ If values are equal, installs new given value V’ in M→ Otherwise operation fails

12

M__sync_bool_compare_and_swap(&M, 20, 30)20

Compare Value

AddressNew

Value

Page 17: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

COMPARE-AND-SWAP

Atomic instruction that compares contents of a memory location M to a given value V→ If values are equal, installs new given value V’ in M→ Otherwise operation fails

12

M__sync_bool_compare_and_swap(&M, 20, 30)30

Compare Value

AddressNew

Value

Page 18: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #1: Blocking OS Mutex→ Simple to use→ Non-scalable (about 25ns per lock/unlock invocation)→ Example: std::mutex

13

std::mutex m;⋮

m.lock();// Do something special...m.unlock();

pthread_mutex_t

Page 19: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #2: Test-and-Set Spinlock (TAS)→ Very efficient (single instruction to lock/unlock)→ Non-scalable, not cache friendly→ Example: std::atomic<T>

14

std::atomic_flag latch;⋮

while (latch.test_and_set(…)) {// Yield? Abort? Retry?

}

Page 20: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #2: Test-and-Set Spinlock (TAS)→ Very efficient (single instruction to lock/unlock)→ Non-scalable, not cache friendly→ Example: std::atomic<T>

14

std::atomic_flag latch;⋮

while (latch.test_and_set(…)) {// Yield? Abort? Retry?

}

Page 21: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

Page 22: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

CPU1

Page 23: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

CPU1

Page 24: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

CPU1

Page 25: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

CPU1

Page 26: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

next

CPU2 Latch

CPU1 CPU2

Page 27: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

next

CPU2 Latch

CPU1 CPU2

Page 28: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

next

CPU2 Latch

CPU1 CPU2

Page 29: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

next

CPU2 Latch

CPU1 CPU2

Page 30: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

next

CPU2 Latch

CPU1 CPU2 CPU3

next

CPU3 Latch

Page 31: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

next

CPU2 Latch

CPU1 CPU2 CPU3

next

CPU3 Latch

Page 32: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #3: Queue-based Spinlock (MCS)→ More efficient than mutex, better cache locality→ Non-trivial memory management→ Example: std::atomic<Latch*>

15

next

Base Latch

next

CPU1 Latch

next

CPU2 Latch

CPU1 CPU2 CPU3

next

CPU3 Latch

Page 33: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

16

read write

Latch

=0

=0

=0

=0

Page 34: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

16

read write

Latch

=0

=0

=0

=0

Page 35: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

16

read write

Latch

=0

=0

=0

=0

=1

Page 36: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

16

read write

Latch

=0

=0

=0

=0

=1

Page 37: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

16

read write

Latch

=0

=0

=0

=0

=1=2

Page 38: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

16

read write

Latch

=0

=0

=0

=0

=1=2

Page 39: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

16

read write

Latch

=0

=0

=0

=0

=1=2

=1

Page 40: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

16

read write

Latch

=0

=0

=0

=0

=1=2

=1

Page 41: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH IMPLEMENTATIONS

Choice #4: Reader-Writer Locks→ Allows for concurrent readers→ Have to manage read/write queues to avoid starvation→ Can be implemented on top of spinlocks

16

read write

Latch

=0

=0

=0

=0

=1=2

=1=1

Page 42: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH CRABBING /COUPLING

Acquire and release latches on B+Tree nodes when traversing the data structure.

A thread can release latch on a parent node if its child node considered safe.→ Any node that won’t split or merge when updated.→ Not full (on insertion)→ More than half-full (on deletion)

17

Page 43: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

L ATCH CRABBING

Search: Start at root and go down; repeatedly,→ Acquire read (R) latch on child→ Then unlock the parent node.

Insert/Delete: Start at root and go down, obtaining write (W) latches as needed.Once child is locked, check if it is safe:→ If child is safe, release all locks on ancestors.

18

Page 44: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #1: SEARCH 23

19

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 45: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #1: SEARCH 23

19

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

Page 46: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #1: SEARCH 23

19

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

R

We can release the latch on A as soon as we acquire the latch for C.

Page 47: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #1: SEARCH 23

19

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

We can release the latch on A as soon as we acquire the latch for C.

Page 48: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #1: SEARCH 23

19

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

R

We can release the latch on A as soon as we acquire the latch for C.

Page 49: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #1: SEARCH 23

19

A

B

D G

20

10 35

6 12 23 38 44

C

E FR

We can release the latch on A as soon as we acquire the latch for C.

Page 50: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #2: DELETE 44

20

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 51: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #2: DELETE 44

20

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

Page 52: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #2: DELETE 44

20

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

W

We may need to coalesce C, so we can’t release the latch on A.

Page 53: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #2: DELETE 44

20

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

W

W

We may need to coalesce C, so we can’t release the latch on A.

G will not merge with F, so we can release latches on A and C.

Page 54: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #2: DELETE 44

20

A

B

D G

20

10 35

6 12 23 38 44

C

E FW

We may need to coalesce C, so we can’t release the latch on A.

G will not merge with F, so we can release latches on A and C.

Page 55: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #2: DELETE 44

20

A

B

D G

20

10 35

6 12 23 38 44

C

E FW

We may need to coalesce C, so we can’t release the latch on A.

G will not merge with F, so we can release latches on A and C.

X

Page 56: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #3: INSERT 40

21

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 57: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #3: INSERT 40

21

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

Page 58: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #3: INSERT 40

21

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

W

C has room if its child has to split, so we can release the latch on A.

Page 59: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #3: INSERT 40

21

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

C has room if its child has to split, so we can release the latch on A.

Page 60: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #3: INSERT 40

21

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

W

C has room if its child has to split, so we can release the latch on A.

G has to split, so we can’t release the latch on C.

Page 61: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #3: INSERT 40

21

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

W

C has room if its child has to split, so we can release the latch on A.

G has to split, so we can’t release the latch on C.

H44

Page 62: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #3: INSERT 40

21

A

B

D G

20

10 35

6 12 23 38 44

C

E F

W

W

C has room if its child has to split, so we can release the latch on A.

G has to split, so we can’t release the latch on C.

H4440

44

Page 63: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #3: INSERT 40

21

A

B

D G

20

10 35

6 12 23 38 44

C

E F

C has room if its child has to split, so we can release the latch on A.

G has to split, so we can’t release the latch on C.

H4440

44

Page 64: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

OBSERVATION

What was the first step that the DBMS took in the two examples that updated the index?

22

Delete 44

A20

W

Insert 40

A20

W

Page 65: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

BET TER L ATCH CRABBING

Optimistically assume that the leaf is safe.→ Take R latches as you traverse the tree to reach it and

verify.→ If leaf is not safe, then do previous algorithm.

23

CONCURRENCY OF OPERATIONS ON B-TREESACTA INFORMATICA 1977

Page 66: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #4: DELETE 44

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 67: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #4: DELETE 44

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

Page 68: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #4: DELETE 44

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

R

We assume that C is safe, so we can release the latch on A.

Page 69: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #4: DELETE 44

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

We assume that C is safe, so we can release the latch on A.

Page 70: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #4: DELETE 44

24

A

B

D G

20

10 35

6 12 23 38 44

C

E F

R

We assume that C is safe, so we can release the latch on A.

Acquire an exclusive latch on G.

Page 71: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #4: DELETE 44

24

A

B

D G

20

10 35

6 12 23 38 44

C

E FW

We assume that C is safe, so we can release the latch on A.

Acquire an exclusive latch on G.

Page 72: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

EXAMPLE #4: DELETE 44

24

A

B

D G

20

10 35

6 12 23 38 44

C

E FW

We assume that C is safe, so we can release the latch on A.

Acquire an exclusive latch on G.

X

Page 73: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

OBSERVATION

Crabbing ensures that txns do not corrupt the internal data structure during modifications.

But because txns release latches on each node as soon as they are finished their operations, we cannot guarantee that phantoms do not occur…

25

Page 74: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Page 75: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1:RREAD(25)

Page 76: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1:R

R

READ(25)

Page 77: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1:

R

READ(25)

Page 78: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1:

R!

READ(25)

Page 79: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1:

Txn #2:INSERT(25)

READ(25)

Page 80: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1:

Txn #2:

W

INSERT(25)

READ(25)

Page 81: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1:

Txn #2:

25

W

INSERT(25)

READ(25)

Page 82: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1:

Txn #2:

Txn #1:

25

INSERT(25)

READ(25)

INSERT(25)

Page 83: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #1

26

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1:

Txn #2:

Txn #1:

25

W

INSERT(25)

READ(25)

INSERT(25)

Page 84: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #2

27

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1:[12,23]

Page 85: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #2

27

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1:

R

[12,23]

Page 86: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #2

27

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1:

R R

[12,23]

Page 87: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #2

27

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1:

Txn #2:W

W

INSERT(21)

[12,23]

Page 88: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #2

27

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1:

Txn #2:

2321

W

W

INSERT(21)

[12,23]

Page 89: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROBLEM SCENARIO #2

27

A

B

D G

20

10 35

6 12 23 38 44

C

E F

Txn #1:

Txn #2:

2321

Txn #1:R R

INSERT(21)

[12,23]

[12,23]

Page 90: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

INDEX LOCKS

Need a way to protect the index’s logical contents from other txns to avoid phantoms.

Difference with index latches:→ Locks are held for the entire duration of a txn.→ Only acquired at the leaf nodes.→ Not physically stored in index data structure.

Can be used with any order-preserving index.

28

Page 91: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

INDEX LOCKS

29

Lock Table

txn1

Xtxn2

Stxn3

S • • •

txn3

Stxn2

Stxn4

S • • •

txn4

IXtxn6

Xtxn5

S • • •

Page 92: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

INDEX LOCKING SCHEMES

Predicate Locks

Key-Value Locks

Gap Locks

Key-Range Locks

Hierarchical Locking

30

Page 93: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PREDICATE LOCKS

Proposed locking scheme from System R.→ Shared lock on the predicate in a WHERE clause of a

SELECT query.→ Exclusive lock on the predicate in a WHERE clause of any

UPDATE, INSERT, or DELETE query.

Never implemented in any system.

93

THE NOTIONS OF CONSISTENCY AND PREDICATE LOCKS IN A DATABASE SYSTEMCACM 1976

Page 94: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PREDICATE LOCKS

94

SELECT SUM(balance)FROM accountWHERE name = 'Biggie'

INSERT INTO account(name, balance)VALUES ('Biggie', 100);

name='Biggie'

name='Biggie'∧balance=100

Records in Table "account"

Page 95: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

KEY-VALUE LOCKS

Locks that cover a single key value.

Need “virtual keys” for non-existent values.

95

10 12 14 16

B+Tree Leaf NodeKey

[14, 14]

Page 96: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

GAP LOCKS

Each txn acquires a key-value lock on the single key that it wants to access. Then get a gap lock on the next key gap.

96

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf Node

Gap(14, 16)

Page 97: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

KEY-RANGE LOCKS

A txn takes locks on ranges in the key space.→ Each range is from one key that appears in the relation,

to the next that appears.→ Define lock modes so conflict table will capture

commutativity of the operations available.

97

Page 98: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

KEY-RANGE LOCKS

Locks that cover a key value and the gap to the next key value in a single index.→ Need “virtual keys” for artificial values (infinity)

98

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf Node

Next Key [14, 16)

Page 99: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

KEY-RANGE LOCKS

Locks that cover a key value and the gap to the next key value in a single index.→ Need “virtual keys” for artificial values (infinity)

98

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf Node

Prior Key (12, 14]

Page 100: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

HIERARCHICAL LOCKING

Allow for a txn to hold wider key-range locks with different locking modes.→ Reduces the number of visits to lock manager.

100

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf Node

Page 101: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

HIERARCHICAL LOCKING

Allow for a txn to hold wider key-range locks with different locking modes.→ Reduces the number of visits to lock manager.

100

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf NodeIX

[10, 16)

Page 102: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

HIERARCHICAL LOCKING

Allow for a txn to hold wider key-range locks with different locking modes.→ Reduces the number of visits to lock manager.

100

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf NodeIX

[10, 16)

[14, 16)X

Page 103: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

HIERARCHICAL LOCKING

Allow for a txn to hold wider key-range locks with different locking modes.→ Reduces the number of visits to lock manager.

100

10 12 14 16{Gap}{Gap} {Gap}

B+Tree Leaf NodeIX

[10, 16)

[14, 16)X

IX [12, 12]X

Page 104: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PARTING THOUGHTS

Hierarchical locking essentially provides predicate locking without complications.→ Index locking occurs only in the leaf nodes.→ Latching is to ensure consistent data structure.

Peloton currently does not support serializable isolation with range scans.

104

Page 105: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)105

ANDY’S

TIPS FOR PROFILING

Page 106: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

MOTIVATION

Consider a program with functions foo and bar.

How can we speed it up with only a debugger ?→ Randomly pause it during execution→ Collect the function call stack

106

Page 107: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

RANDOM PAUSE METHOD

Consider this scenario→ Collected 10 call stack samples→ Say 6 out of the 10 samples were in foo

What percentage of time was spent in foo?→ Roughly 60% of the time was spent in foo→ Accuracy increases with # of samples

107

Page 108: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

Say we optimized foo to run two times faster

What’s the expected overall speedup ?→ 60% of time spent in foo drops in half→ 40% of time spent in bar unaffected

By Amdahl’s law, overall speedup = 1

𝒑

𝒔+(1−𝒑)

→ p = percentage of time spent in optimized task→ s = speed up for the optimized task

→ Overall speedup =1

0.6

2+0.4

= 1.4 times faster

108

Page 109: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PROFILING TOOLS FOR REAL

Choice #1: Valgrind→ Heavyweight binary instrumentation framework with

different tools to measure different events.

Choice #2: Perf→ Lightweight tool that uses hardware counters to capture

events during execution.

109

Page 110: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

CHOICE #1: VALGRIND

Instrumentation framework for building dynamic analysis tools.→ memcheck: a memory error detector→ callgrind: a call-graph generating profiler→ massif: memory usage tracking.

110

Page 111: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

Using callgrind to profile the index test and Peloton in general:

Profile data visualization tool:$ kcachegrind callgrind.out.12345

KCACHEGRIND

111

$ valgrind --tool=callgrind --trace-children=yes ./relwithdebinfo/concurrent_read_benchmark

Page 112: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

Using callgrind to profile the index test and Peloton in general:

Profile data visualization tool:$ kcachegrind callgrind.out.12345

KCACHEGRIND

111

$ valgrind --tool=callgrind --trace-children=yes ./relwithdebinfo/concurrent_read_benchmark

Cumulative Time Distribution

Callgraph View

Page 113: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

CHOICE #2: PERF

Tool for using the performance counters subsystem in Linux.→ -e = sample the event cycles at the user level only→ -c = collect a sample every 2000 occurrences of event

Uses counters for tracking events→ On counter overflow, the kernel records a sample→ Sample contains info about program execution

113

$ perf record -e cycles:u -c 2000 ./relwithdebinfo/concurrent_read_benchmark

Page 114: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PERF VISUALIZATION

We can also use perf to visualize the generated profile for our application.

There are also third-party visualization tools:→ Hotspot

114

$ perf report

Page 115: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PERF VISUALIZATION

We can also use perf to visualize the generated profile for our application.

There are also third-party visualization tools:→ Hotspot

114

$ perf report

Cumulative Event Distribution

Page 116: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PERF VISUALIZATION

We can also use perf to visualize the generated profile for our application.

There are also third-party visualization tools:→ Hotspot

114

$ perf report

Page 117: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PERF VISUALIZATION

We can also use perf to visualize the generated profile for our application.

There are also third-party visualization tools:→ Hotspot

114

$ perf report

Page 118: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PERF VISUALIZATION

We can also use perf to visualize the generated profile for our application.

There are also third-party visualization tools:→ Hotspot

114

$ perf report

Page 119: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

PERF EVENTS

Supports several other events like:→ L1-dcache-load-misses→ branch-misses

To see a list of events:

Another usage example:

119

$ perf list

$ perf record -e cycles,LLC-load-misses -c 2000 ./relwithdebinfo/concurrent_read_benchmark

Page 121: ADVANCED 6 DATABASE SYSTEMSSearch: Start at root and go down; repeatedly, →Acquire read (R) latch on child →Then unlock the parent node. Insert/Delete: Start at root and go down,

CMU 15-721 (Spring 2019)

NEXT CL ASS

Index Key Representation

Memory Allocation & Garbage Collection

T-Trees (1980s / TimesTen)

Bw-Tree (Hekaton)

Concurrent Skip Lists (MemSQL)

121