verification-friendly concurrent binary search tree

65
Practical Concurrent Binary Search Trees via Logical Ordering Dana Drachsler (Technion, Israel) Martin Vechev (ETH, Switzerland) Eran Yahav (Technion , Israel)

Upload: dinhcong

Post on 14-Feb-2017

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Verification-Friendly Concurrent Binary Search Tree

Practical Concurrent Binary Search

Trees via Logical Ordering

Dana Drachsler (Technion, Israel)

Martin Vechev (ETH, Switzerland)

Eran Yahav (Technion , Israel)

Page 2: Verification-Friendly Concurrent Binary Search Tree

Binary Search Tree

• A data-structure that stores elements

▫ Each element has a unique key

▫ Duplications are not allowed

• The BST consists of nodes

▫ Each node stores an element

• For each node

▫ Keys in the left sub-tree are smaller

▫ Keys in the right sub-tree are bigger

2

6

3 12

24

Page 3: Verification-Friendly Concurrent Binary Search Tree

Binary Search Tree Operations

• Contains(𝑘): check if 𝑘 is present

• Insert(𝑘): insert 𝑘 if it is not yet present

• Remove 𝑘 : remove 𝑘 if present

3

Page 4: Verification-Friendly Concurrent Binary Search Tree

Binary Search Tree Operations

• Contains(𝑘): check if 𝑘 is present

• Insert(𝑘): insert 𝑘 if it is not yet present

• Remove 𝑘 : remove 𝑘 if present

▫ Removal of a node with 2 children

Find the successor: the left-most node

of the right sub-tree

3

6

3 12

9

Remove(6)

Page 5: Verification-Friendly Concurrent Binary Search Tree

Binary Search Tree Operations

• Contains(𝑘): check if 𝑘 is present

• Insert(𝑘): insert 𝑘 if it is not yet present

• Remove 𝑘 : remove 𝑘 if present

▫ Removal of a node with 2 children

Find the successor: the left-most node

of the right sub-tree

Relocate the successor

3

3 12

9

Remove(6)

Page 6: Verification-Friendly Concurrent Binary Search Tree

BST: Challenge with Concurrency

1. Thread A searches for 9

6

3 12

9

4

Thread A: Contains(9)

Page 7: Verification-Friendly Concurrent Binary Search Tree

BST: Challenge with Concurrency

1. Thread A searches for 9

and pauses

6

3 12

9

4

Thread A: Contains(9)

Page 8: Verification-Friendly Concurrent Binary Search Tree

BST: Challenge with Concurrency

1. Thread A searches for 9

and pauses

2. Thread B removes 6

6

3 12

9

4

Thread B: Remove(6)

Thread A: Contains(9)

Page 9: Verification-Friendly Concurrent Binary Search Tree

BST: Challenge with Concurrency

1. Thread A searches for 9

and pauses

2. Thread B removes 6

3 12

9

4

Thread B: Remove(6)

Thread A: Contains(9)

Page 10: Verification-Friendly Concurrent Binary Search Tree

BST: Challenge with Concurrency

1. Thread A searches for 9

and pauses

2. Thread B removes 6

3. Thread A resumes and misses 9

3 12

9

4

Thread B: Remove(6)

Search operation unaware of concurrent changes to BST layout

Thread A: Contains(9)

Page 11: Verification-Friendly Concurrent Binary Search Tree

Our Contribution

• We present a new perspective on BST

▫ Locking is based on a logical ordering layout, and not only on the BST layout

• The additional layout requires

▫ Extra space for the new links

▫ Extra time for maintaining the new links

▫ Extra lock acquires of the new links

• Yet, it performs as state-of-the-art algorithms

▫ Sometimes even better

5

Page 12: Verification-Friendly Concurrent Binary Search Tree

Key Idea

• There is a total order between the keys

6

6

3 12

24

Page 13: Verification-Friendly Concurrent Binary Search Tree

Key Idea

• There is a total order between the keys

6

6 3 12 24 <

<

<

-∞ ∞ <

<

Page 14: Verification-Friendly Concurrent Binary Search Tree

Key Idea

• There is a total order between the keys

• The order induces intervals

▫ A key is present in the tree if it is an end point of some interval

• We explicitly maintain the intervals

▫ The logical ordering layout

6

6 3 12 24 <

<

<

-∞ ∞ <

<

(−∞, 3) (3,6) (6,12) (12,24) (24, ∞)

Page 15: Verification-Friendly Concurrent Binary Search Tree

Logical Ordering Layout

• Connect 𝑛 to its predecessor, 𝑝, and successor, 𝑠

▫ 𝑛 can access efficiently to (𝑝, 𝑛),(𝑛, 𝑠)

7

6

3 12

9

Page 16: Verification-Friendly Concurrent Binary Search Tree

Logical Ordering Layout

• Connect 𝑛 to its predecessor, 𝑝, and successor, 𝑠

▫ 𝑛 can access efficiently to (𝑝, 𝑛),(𝑛, 𝑠)

• To query whether 𝑘 is in the tree

▫ Find (𝑝, 𝑠) such that 𝑘 ∈ 𝑝, 𝑠

7

6

3 12

9

8?

Page 17: Verification-Friendly Concurrent Binary Search Tree

Logical Ordering Layout

• Connect 𝑛 to its predecessor, 𝑝, and successor, 𝑠

▫ 𝑛 can access efficiently to (𝑝, 𝑛),(𝑛, 𝑠)

• To query whether 𝑘 is in the tree

▫ Find (𝑝, 𝑠) such that 𝑘 ∈ 𝑝, 𝑠

• For (𝑝, 𝑠): 𝑝, 𝑠 might be

non adjacent in the tree

7

6

3 12

9

8?

(6,9)

Page 18: Verification-Friendly Concurrent Binary Search Tree

Main Advantages

• Efficiently answer membership queries even under concurrent updates to the BST layout

▫ Includes relocating the successor in a removal

▫ Includes applying sequential balancing operations

• Efficiently find the successor of a node

▫ Important for the removal of a two-nodes parent

• Efficiently find the minimal/maximal keys

▫ Can be used to implement a priority queue

8

Page 19: Verification-Friendly Concurrent Binary Search Tree

The Sequential Contains(𝑘)

• Traverse downwards in the tree

6

3 12

9

9

9?

Contains(9)

Page 20: Verification-Friendly Concurrent Binary Search Tree

The Sequential Contains(𝑘)

• Traverse downwards in the tree

• If 𝑘 was found, return true

6

3 12

9

9 9?

Contains(9)

Page 21: Verification-Friendly Concurrent Binary Search Tree

The Sequential Contains(𝑘)

• Traverse downwards in the tree

6

3 12

10

9

8?

Contains(8)

Page 22: Verification-Friendly Concurrent Binary Search Tree

The Sequential Contains(𝑘)

• Traverse downwards in the tree

• If reached to an end of a path,

return false

6

3 12

10

9

8?

Contains(8)

Page 23: Verification-Friendly Concurrent Binary Search Tree

The Concurrent Contains(𝑘)

• Traverse downwards in the tree

11

6

3 12

9

9?

Contains(9)

Tree link Interval link

Page 24: Verification-Friendly Concurrent Binary Search Tree

The Concurrent Contains(𝑘)

• Traverse downwards in the tree

• If 𝑘 was found, return true

11

6

3 12

9 9?

Contains(9)

Tree link Interval link

Page 25: Verification-Friendly Concurrent Binary Search Tree

The Concurrent Contains(𝑘)

• Traverse downwards in the tree

6

3 12

9

8?

12

Contains(8)

Tree link Interval link

Page 26: Verification-Friendly Concurrent Binary Search Tree

The Concurrent Contains(𝑘)

• Traverse downwards in the tree

• If reached to an end of a path,

traverse via the ordering layout to

find (𝑝, 𝑠) such that 𝑘 ∈ [𝑝, 𝑠] ▫ Return false iff 𝑘 ≠ 𝑝, 𝑠

6

3 12

9

8? 12

Contains(8)

Tree link Interval link

Page 27: Verification-Friendly Concurrent Binary Search Tree

The Concurrent Contains(𝑘)

• Traverse downwards in the tree

• If reached to an end of a path,

traverse via the ordering layout to

find (𝑝, 𝑠) such that 𝑘 ∈ [𝑝, 𝑠] ▫ Return false iff 𝑘 ≠ 𝑝, 𝑠

• This operation is non-blocking

6

3 12

9

8?

12

Contains(8)

(6,9)

Tree link Interval link

Page 28: Verification-Friendly Concurrent Binary Search Tree

Insert and Remove Operations

• The synchronization is based on locks

• The operations lock

▫ The relevant nodes in the tree

▫ The relevant intervals

13

6

3 12

9 Interval lock

Node lock

Page 29: Verification-Friendly Concurrent Binary Search Tree

Insert and Remove Operations

• The synchronization is based on locks

• The operations lock

▫ The relevant nodes in the tree

▫ The relevant intervals

13

6

3 12

9

(9,12)

Interval lock

Node lock

Page 30: Verification-Friendly Concurrent Binary Search Tree

The Sequential Insert(𝑘)

• Traverse downwards in the tree

• If 𝑘 was found: cannot insert

• Otherwise, let 𝑙 be the node at

the end of a path

14

6

3 12

9

7

Insert(7)

Page 31: Verification-Friendly Concurrent Binary Search Tree

The Sequential Insert(𝑘)

• Traverse downwards in the tree

• If 𝑘 was found: cannot insert

• Otherwise, let 𝑙 be the node at

the end of a path

▫ Connect 𝑙 to the new node

14

6

3 12

9

7

Insert(7)

Page 32: Verification-Friendly Concurrent Binary Search Tree

The Concurrent Insert(𝑘)

• Traverse downwards in the tree

• If 𝑘 was found: cannot insert

• Otherwise, let 𝑙 be the node at

the end of a path

6

3 12

9

7

15

Insert(7)

Page 33: Verification-Friendly Concurrent Binary Search Tree

The Concurrent Insert(𝑘)

• Traverse downwards in the tree

• If 𝑘 was found: cannot insert

• Otherwise, let 𝑙 be the node at

the end of a path

• Lock relevant interval

▫ If 𝑘 ≤ 𝑙: lock (𝑙’s pred, 𝑙)

6

3 12

9

7

15

Insert(7)

(6,9)

Page 34: Verification-Friendly Concurrent Binary Search Tree

The Concurrent Insert(𝑘)

• Traverse downwards in the tree

• If 𝑘 was found: cannot insert

• Otherwise, let 𝑙 be the node at

the end of a path

• Lock relevant interval

▫ If 𝑘 ≤ 𝑙: lock (𝑙’s pred, 𝑙)

• Lock 𝑙

6

3 12

9

7

15

Insert(7)

(6,9)

Page 35: Verification-Friendly Concurrent Binary Search Tree

The Concurrent Insert(𝑘)

• Traverse downwards in the tree

• If 𝑘 was found: cannot insert

• Otherwise, let 𝑙 be the node at

the end of a path

• Lock relevant interval

▫ If 𝑘 ≤ 𝑙: lock (𝑙’s pred, 𝑙)

• Lock 𝑙

• Update predecessor-successor

6

3 12

9

7

15

Insert(7)

(6,7)

(7,9)

Page 36: Verification-Friendly Concurrent Binary Search Tree

The Concurrent Insert(𝑘)

• Traverse downwards in the tree

• If 𝑘 was found: cannot insert

• Otherwise, let 𝑙 be the node at

the end of a path

• Lock relevant interval

▫ If 𝑘 ≤ 𝑙: lock (𝑙’s pred, 𝑙)

• Lock 𝑙

• Update predecessor-successor

• Connect 𝑙 to the new node

6

3 12

9

7

15

Insert(7)

(6,7)

(7,9)

Page 37: Verification-Friendly Concurrent Binary Search Tree

The Sequential Remove(𝑘)

• Traverse downwards in the tree

• If the node to remove has at most 1 child

▫ Set its parent to point to its child

(may be null)

6

3 12

9

11

Remove(9)

0

Page 38: Verification-Friendly Concurrent Binary Search Tree

The Sequential Remove(𝑘)

• Traverse downwards in the tree

• If the node to remove has at most 1 child

▫ Set its parent to point to its child

(may be null)

6

3 12

11

16

Remove(9)

0

Page 39: Verification-Friendly Concurrent Binary Search Tree

The Sequential Remove(𝑘)

• Traverse downwards in the tree

• If the node to remove has at most 1 child

▫ Set its parent to point to its child

(may be null)

• If the node to remove has 2 children

▫ Search for its successor, 𝑠

The left most node in the right sub-tree

▫ Relocate 𝑠 to its location

6

3 12

9

11

Remove(6)

0

Page 40: Verification-Friendly Concurrent Binary Search Tree

The Sequential Remove(𝑘)

• Traverse downwards in the tree

• If the node to remove has at most 1 child

▫ Set its parent to point to its child

(may be null)

• If the node to remove has 2 children

▫ Search for its successor, 𝑠

The left most node in the right sub-tree

▫ Relocate 𝑠 to its location

3 12

9

11

16

Remove(6)

0

Page 41: Verification-Friendly Concurrent Binary Search Tree

The Concurrent Remove(𝑘)

• Let 𝑛 be the node to remove

• Lock (𝑛’s pred, 𝑛)

• Lock (𝑛, 𝑛’s succ)

6

3 12

9

11

0

Remove(6)

(3,6)

(6,9)

Page 42: Verification-Friendly Concurrent Binary Search Tree

The Concurrent Remove(𝑘)

• Let 𝑛 be the node to remove

• Lock (𝑛’s pred, 𝑛)

• Lock (𝑛, 𝑛’s succ)

• Lock 𝑛 and its parent

6

3 12

9

11

0

Remove(6)

(3,6)

(6,9)

Page 43: Verification-Friendly Concurrent Binary Search Tree

The Concurrent Remove(𝑘)

• Let 𝑛 be the node to remove

• Lock (𝑛’s pred, 𝑛)

• Lock (𝑛, 𝑛’s succ)

• Lock 𝑛 and its parent

• If 𝑛 has 2 children

▫ Lock 𝑛’s successor, its parent

and child

6

3 12

9

11

0

Remove(6)

(3,6)

(6,9)

Page 44: Verification-Friendly Concurrent Binary Search Tree

The Concurrent Remove(𝑘)

• Let 𝑛 be the node to remove

• Lock (𝑛’s pred, 𝑛)

• Lock (𝑛, 𝑛’s succ)

• Lock 𝑛 and its parent

• If 𝑛 has 2 children

▫ Lock 𝑛’s successor, its parent

and child

▫ Update predecessor-successor

6

3 12

9

11

0

Remove(6)

(3,9)

Page 45: Verification-Friendly Concurrent Binary Search Tree

The Concurrent Remove(𝑘)

• Let 𝑛 be the node to remove

• Lock (𝑛’s pred, 𝑛)

• Lock (𝑛, 𝑛’s succ)

• Lock 𝑛 and its parent

• If 𝑛 has 2 children

▫ Lock 𝑛’s successor, its parent

and child

▫ Update predecessor-successor

▫ Relocate the successor to 𝑛’s location

3 12

9

11

0

17

Remove(6)

(3,9)

Page 46: Verification-Friendly Concurrent Binary Search Tree

BST: Challenge with Concurrency

1. Thread A searches for 9

6

3 12

9

18

Thread A: Contains(9)

Page 47: Verification-Friendly Concurrent Binary Search Tree

BST: Challenge with Concurrency

1. Thread A searches for 9

and pauses

6

3 12

9

18

Thread A: Contains(9)

Page 48: Verification-Friendly Concurrent Binary Search Tree

BST: Challenge with Concurrency

1. Thread A searches for 9

and pauses

2. Thread B removes 6

6

3 12

9

18

Thread B: Remove(6)

Thread A: Contains(9)

Page 49: Verification-Friendly Concurrent Binary Search Tree

BST: Challenge with Concurrency

1. Thread A searches for 9

and pauses

2. Thread B removes 6

3 12

9

18

Thread B: Remove(6)

Thread A: Contains(9)

Page 50: Verification-Friendly Concurrent Binary Search Tree

BST: Challenge with Concurrency

1. Thread A searches for 9

and pauses

2. Thread B removes 6

3. Thread A resumes and misses 9

3 12

9

18

Thread B: Remove(6)

Thread A: Contains(9)

Page 51: Verification-Friendly Concurrent Binary Search Tree

Solution

• Consult the logical ordering

layout before making final

decisions

6

3 12

9

19

Thread A: Contains(9)

Tree link Interval link

Page 52: Verification-Friendly Concurrent Binary Search Tree

Solution

• Consult the logical ordering

layout before making final

decisions

6

3 12

9

19

Thread A: Contains(9)

Tree link Interval link

Page 53: Verification-Friendly Concurrent Binary Search Tree

Solution

• Consult the logical ordering

layout before making final

decisions

6

3 12

9

19

Thread B: Remove(6)

Thread A: Contains(9)

Tree link Interval link

Page 54: Verification-Friendly Concurrent Binary Search Tree

Solution

• Consult the logical ordering

layout before making final

decisions

3 12

9

19

Thread A: Contains(9)

Tree link Interval link

Page 55: Verification-Friendly Concurrent Binary Search Tree

Solution

• Consult the logical ordering

layout before making final

decisions

3 12

9

19

Thread A: Contains(9) Tree link

Interval link

Page 56: Verification-Friendly Concurrent Binary Search Tree

Solution

• Consult the logical ordering

layout before making final

decisions

3 12

9

19

Thread A: Contains(9)

(9,12)

Tree link Interval link

Page 57: Verification-Friendly Concurrent Binary Search Tree

From BST to AVL Tree

• After each update, apply balancing operations

• Balancing operations relocate nodes in the tree

▫ Requires only node locks

• Concurrent threads cannot miss keys, since they consult the logical ordering layout

20

Page 58: Verification-Friendly Concurrent Binary Search Tree

Implementation

• We implemented our BST and AVL tree in Java

• We compared to state-of-the-art algorithms

21

Page 59: Verification-Friendly Concurrent Binary Search Tree

Comparison to Existing Algorithms

• Partially external trees

▫ Internal nodes are only marked as removed

A follow-up insert can revive them

• Locked-based, partially external trees

▫ Bronson et al., PPoPP 2010 (BCCO)

▫ A variation of our work (Our LR-AVL)

22

Page 60: Verification-Friendly Concurrent Binary Search Tree

Comparison to Existing Algorithms

• External tree

▫ Elements are kept only in the leaves

▫ Inner nodes serve as routing nodes

▫ Only leaves can be asked to be removed

▫ Traversal paths are typically longer

• Non-Blocking external tree

▫ Brown et al., PPoPP 2014 (Chromatic)

23

3

3 9

9 24

Page 61: Verification-Friendly Concurrent Binary Search Tree

Evaluation

• A 4-socket AMD Opteron, with 64 h/w threads

• Threads randomly chose operation type and key

▫ Different workloads for the operation type

100% contains, 0% insert, 0% remove

70% contains, 20% insert, 10% remove

▫ Different key ranges

2 ⋅ 106,2 ⋅ 105

24

Page 62: Verification-Friendly Concurrent Binary Search Tree

Evaluation

25

• 100% contains, 0% insert, 0% remove

• Key range: 2 ⋅ 106

Number of Threads

Th

rou

gh

pu

t (m

illi

on

op

s/se

c)

Page 63: Verification-Friendly Concurrent Binary Search Tree

Evaluation

26

• 70% contains, 20% insert, 10% remove

• Key range: 2 ⋅ 106

Number of Threads

Th

rou

gh

pu

t (m

illi

on

op

s/se

c)

Page 64: Verification-Friendly Concurrent Binary Search Tree

Evaluation

27

• 70% contains, 20% insert, 10% remove

• Key range: 2 ⋅ 105

Number of Threads

Th

rou

gh

pu

t (m

illi

on

op

s/se

c)

Page 65: Verification-Friendly Concurrent Binary Search Tree

Summary

• We presented a new practical concurrent BST

▫ Non-blocking search

▫ Balanced

▫ Efficient

▫ Simple

• Our main insight

▫ Maintain explicitly the intervals

28

Thank you!