Balanced Binary Search Tree Dana Drachsler, Technion, Israel Joint - - PowerPoint PPT Presentation

balanced binary search tree
SMART_READER_LITE
LIVE PREVIEW

Balanced Binary Search Tree Dana Drachsler, Technion, Israel Joint - - PowerPoint PPT Presentation

Verification-Friendly Concurrent Balanced Binary Search Tree Dana Drachsler, Technion, Israel Joint work with: Martin Vechev, ETH, Switzerland Eran Yahav, Technion, Israel 2 Motivation Balanced Binary Search Tree (BST) is an efficient


slide-1
SLIDE 1

Verification-Friendly Concurrent Balanced Binary Search Tree

Dana Drachsler, Technion, Israel Joint work with: Martin Vechev, ETH, Switzerland Eran Yahav, Technion, Israel

slide-2
SLIDE 2

Motivation

  • Balanced Binary Search Tree (BST) is an

efficient data-structure for storing unique elements

▫ No repetitions are allowed

  • Formal verification:

▫ Given a program, prove some property ▫ In the tree:

 prove that repetitions of elements cannot occur

2

slide-3
SLIDE 3

Motivation

  • Formal verification was applied to the sequential

algorithm (e.g. using Isabelle [6])

  • However, in a concurrent setting, formal

verification is more complicated

3

slide-4
SLIDE 4

Motivation

  • There seems to be a trade-off between

algorithms that are easy to verify and algorithms that are practical

  • A concurrent BST that is protected by a global

lock is easy to verify

  • Practical concurrent trees use sophisticated

mechanisms

▫ Many different cases to reason about ▫ Harder to verify

4

slide-5
SLIDE 5

Goal

  • We gap this trade-off by presenting a concurrent

BST that is both practical and simple to reason about

  • Our key idea:

▫ Integrate the property into the algorithm

  • We achieve a fine-grained locking balanced BST
  • Our tree is very similar to the sequential tree
  • Our mechanism allows breaking the proof into

several separated proofs

5

slide-6
SLIDE 6

Outline

Binary Search Tree Balanced Binary Search Tree Concurrent Binary Search Tree Concurrent Balanced Binary Search Tree 6

slide-7
SLIDE 7

Binary Search Tree

  • A data-structure that stores elements
  • Consists of nodes
  • Each node represents an element

▫ Internal tree

  • Each element has a unique key

▫ Repetitions are not allowed

  • Each node in the tree holds:

▫ The left sub-tree has elements with smaller keys ▫ The right sub-tree has elements with bigger keys

7 6 3 12 24

slide-8
SLIDE 8

Binary Search Tree

  • In other words, BST maintains two types of

invariants:

▫ Set invariant

 Each key appears at most once

▫ BST invariants

 For each node:

 The keys in the left sub-tree are smaller  The keys in the right sub-tree are bigger

8

slide-9
SLIDE 9

Binary Search Tree

  • Supports the following operations:

▫ Contains

6 3 12 24 24? 9

slide-10
SLIDE 10

Binary Search Tree

  • Supports the following operations:

▫ Contains

6 3 12 24 24? 9

slide-11
SLIDE 11

Binary Search Tree

  • Supports the following operations:

▫ Insert

 The new node is always a leaf

6 3 12 9 24 24 10

slide-12
SLIDE 12

Binary Search Tree

  • Supports the following operations:

▫ Insert

 The new node is always a leaf

6 3 12 9 24 10

slide-13
SLIDE 13

Binary Search Tree

  • Supports the following operations:

▫ Remove

 The removed node, 𝑜, may be:

 A leaf 

6 3 12 9 24 11

slide-14
SLIDE 14

Binary Search Tree

  • Supports the following operations:

▫ Remove

 The removed node, 𝑜, may be:

 A leaf

6 3 12 24 11

slide-15
SLIDE 15

Binary Search Tree

  • Supports the following operations:

▫ Remove

 The removed node, 𝑜, may be:

 A leaf  A parent of a single child

▫ 𝑜’s parent is connected to 𝑜’s child

6 3 12 9 24 10 11

slide-16
SLIDE 16

Binary Search Tree

  • Supports the following operations:

▫ Remove

 The removed node, 𝑜, may be:

 A leaf  A parent of a single child

▫ 𝑜’s parent is connected to 𝑜’s child

6 3 12 24 10 11

slide-17
SLIDE 17

Binary Search Tree

  • Supports the following operations:

▫ Remove

 The removed node, 𝑜, may be:

 A leaf  A parent of a single child

▫ 𝑜’s parent is connected to 𝑜’s child

 A parent of two children

▫ 𝑜’s successor is relocated to 𝑜’s location

6 3 12 24 10 11

slide-18
SLIDE 18

Binary Search Tree

  • Supports the following operations:

▫ Remove

 The removed node, 𝑜, may be:

 A leaf  A parent of a single child

▫ 𝑜’s parent is connected to 𝑜’s child

 A parent of two children

▫ 𝑜’s successor is relocated to 𝑜’s location

3 12 24 10 11

slide-19
SLIDE 19

Outline

Balanced Binary Search Tree Concurrent Binary Search Tree Concurrent Balanced Binary Search Tree 12

slide-20
SLIDE 20

Challenges in Concurrent BST

  • Consider the following tree:

▫ Thread A searches for 9

6 3 12 9 9? 13

slide-21
SLIDE 21

Challenges in Concurrent BST

  • Consider the following tree:

▫ Thread A searches for 9 and pauses

6 3 12 9 9? 13

slide-22
SLIDE 22

Challenges in Concurrent BST

  • Consider the following tree:

▫ Thread A searches for 9 and pauses ▫ Thread B removes 6

6 3 12 9 9? 13

slide-23
SLIDE 23

Challenges in Concurrent BST

  • Consider the following tree:

▫ Thread A searches for 9 and pauses ▫ Thread B removes 6

3 12 9 9? 13

slide-24
SLIDE 24

Challenges in Concurrent BST

  • Consider the following tree:

▫ Thread A searches for 9 and pauses ▫ Thread B removes 6 ▫ Thread A resumes the search

3 12 9 9? 13

slide-25
SLIDE 25

Challenges in Concurrent BST

  • Consider the following tree:

▫ Thread A searches for 9 and pauses ▫ Thread B removes 6 ▫ Thread A resumes the search and observes that 9 is not present

3 12 9 9? 13

slide-26
SLIDE 26

How do others cope with this challenge?

  • By not supporting the remove operation

▫ Bender et al. [1]

14

slide-27
SLIDE 27

How do others cope with this challenge?

  • By using external trees

▫ Only leaves can be removed ▫ Use more space than internal trees ▫ Ellen et al. [4]

3 3 9 9 24 15

slide-28
SLIDE 28

How do others cope with this challenge?

  • Many concurrent algorithms for data-structures

remove elements in two steps:

▫ Marking the node as logically removed

6 3 12 16

slide-29
SLIDE 29

How do others cope with this challenge?

  • Many concurrent algorithms for data-structures

remove elements in two steps:

▫ Marking the node as logically removed ▫ Update pointers to physically remove the node

6 3 12 16

slide-30
SLIDE 30

How do others cope with this challenge?

  • By marking the node as removed without

physically removing it

▫ Also known as partially-external trees ▫ Bronson et al. [2] ▫ Crain et al. [3]

6 3 12 9 9? 17 A: contains(9)

slide-31
SLIDE 31

How do others cope with this challenge?

  • By marking the node as removed without

physically removing it

▫ Also known as partially-external trees ▫ Bronson et al. [2] ▫ Crain et al. [3]

6 3 12 9 9? 17 A: contains(9)

slide-32
SLIDE 32

How do others cope with this challenge?

  • By marking the node as removed without

physically removing it

▫ Also known as partially-external trees ▫ Bronson et al. [2] ▫ Crain et al. [3]

3 12 9 9? 17 A: contains(9) B: remove(6)

slide-33
SLIDE 33

How do others cope with this challenge?

  • By marking the node as removed without

physically removing it

▫ Also known as partially-external trees ▫ Bronson et al. [2] ▫ Crain et al. [3]

3 12 9 9? 17 A: contains(9) B: remove(6)

slide-34
SLIDE 34

How do others cope with this challenge?

  • By marking the node as removed without

physically removing it

▫ Howley et al. [5]

6 3 12 9 9? 18 A: contains(9)

slide-35
SLIDE 35

How do others cope with this challenge?

  • By marking the node as removed without

physically removing it

▫ Howley et al. [5]

6 3 12 9 9? 18 A: contains(9)

slide-36
SLIDE 36

How do others cope with this challenge?

  • By marking the node as removed without

physically removing it

▫ Howley et al. [5]

6 3 12 9? 9 18 A: contains(9) B: remove(6)

slide-37
SLIDE 37

How do others cope with this challenge?

  • By marking the node as removed without

physically removing it

▫ Howley et al. [5]

6 3 12 9? 9 18 A: contains(9) B: remove(6)

slide-38
SLIDE 38

How do others cope with this challenge?

  • These solutions leave removed nodes in the tree
  • Is it possible to physically remove nodes?
  • Trivial solution: use global lock

19 6 3 12 9

slide-39
SLIDE 39

How do others cope with this challenge?

  • These solutions leave removed nodes in the tree
  • Is it possible to physically remove nodes?
  • Trivial solution: use global lock
  • Observation: To determine

whether 𝑙 is in the tree it is enough to have 𝑞, 𝑡 such that:

▫ 𝑞, 𝑡 belong to the tree ▫ Any 𝑥 ∈ 𝑞, 𝑡 is not in the tree

19 6 3 12 9 7?

slide-40
SLIDE 40

Our Approach

  • Maintain the predecessor-successor relation

▫ The set layout

  • Consult this relation before

making final decisions

6 3 12 9 20

slide-41
SLIDE 41

Our Approach

  • Maintain the predecessor-successor relation

▫ The set layout

  • Consult this relation before

making final decisions

6 3 12 9 9? 20 A: contains(9)

slide-42
SLIDE 42

Our Approach

  • Maintain the predecessor-successor relation

▫ The set layout

  • Consult this relation before

making final decisions

6 3 12 9 9? 20 A: contains(9)

slide-43
SLIDE 43

Our Approach

  • Maintain the predecessor-successor relation

▫ The set layout

  • Consult this relation before

making final decisions

3 12 9 9? 20 A: contains(9) B: remove(6)

slide-44
SLIDE 44

Our Approach

  • Maintain the predecessor-successor relation

▫ The set layout

  • Consult this relation before

making final decisions

3 12 9 9? 20 A: contains(9) B: remove(6)

slide-45
SLIDE 45

Our Approach

  • Maintain the predecessor-successor relation

▫ The set layout

  • Consult this relation before

making final decisions

3 12 9 9? 20 A: contains(9) B: remove(6)

slide-46
SLIDE 46

Our Approach

  • Maintain the predecessor-successor relation

▫ The set layout

  • Consult this relation before

making final decisions

  • This relation allows us to lock

the required nodes even if they are not adjacent

▫ Enjoy the benefits of the global lock ▫ While enabling more parallelism

3 12 9 9? 20 A: contains(9) B: remove(6)

slide-47
SLIDE 47

Contains(k)

  • Traverse the tree using the tree pointers
  • If 𝑙 was found

▫ Return true

  • Otherwise, upon reaching to a leaf 𝑚, confirm:

▫ 𝑙 ∈ (𝑚′s predecessor, 𝑚) or 𝑙 ∈ 𝑚, 𝑚′s successor ▫ and return false

  • This operation does not acquire locks

47

slide-48
SLIDE 48

Update Operations

  • The synchronization is based on locks
  • Each update operation locks:

▫ The relevant nodes in the tree ▫ The relevant intervals

22 6 3 12 9

slide-49
SLIDE 49

Insert(k)

  • Traverse the tree to find the location

6 3 12 9 7 23

slide-50
SLIDE 50

Insert(k)

  • Traverse the tree to find the location
  • Let 𝑚 be the node found

6 3 12 9 7 23

slide-51
SLIDE 51

Insert(k)

  • Traverse the tree to find the location
  • Let 𝑚 be the node found
  • If 𝑙 ≤ 𝑚: lock 𝑚’s predecessor edge

6 3 12 9 7 23

slide-52
SLIDE 52

Insert(k)

  • Traverse the tree to find the location
  • Let 𝑚 be the node found
  • If 𝑙 ≤ 𝑚: lock 𝑚’s predecessor edge

▫ Lock 𝑚

6 3 12 9 7 23

slide-53
SLIDE 53

Insert(k)

  • Traverse the tree to find the location
  • Let 𝑚 be the node found
  • If 𝑙 ≤ 𝑚: lock 𝑚’s predecessor edge

▫ Lock 𝑚 ▫ Update predecessor-successor

6 3 12 9 7 23

slide-54
SLIDE 54

Insert(k)

  • Traverse the tree to find the location
  • Let 𝑚 be the node found
  • If 𝑙 ≤ 𝑚: lock 𝑚’s predecessor edge

▫ Lock 𝑚 ▫ Update predecessor-successor ▫ Add 𝑙

6 3 12 9 7 23

slide-55
SLIDE 55

Insert(k)

  • Traverse the tree to find the location
  • Let 𝑚 be the node found
  • If 𝑙 ≤ 𝑚: lock 𝑚’s predecessor edge

▫ Lock 𝑚 ▫ Update predecessor-successor ▫ Add 𝑙

  • Else: lock 𝑚’s successor

▫ Symmetric.

6 3 12 9 7 11 23

slide-56
SLIDE 56

Remove(k)

  • Traverse the tree to find 𝑙
  • Let 𝑜 be the node found
  • Lock 𝑜’s predecessor edge

  

6 3 12 9 7 24

slide-57
SLIDE 57

Remove(k)

  • Traverse the tree to find 𝑙
  • Let 𝑜 be the node found
  • Lock 𝑜’s predecessor edge

▫ Lock 𝑜’s successor edge

  

6 3 12 9 7 24

slide-58
SLIDE 58

Remove(k)

  • Traverse the tree to find 𝑙
  • Let 𝑜 be the node found
  • Lock 𝑜’s predecessor edge

▫ Lock 𝑜’s successor edge ▫ Lock 𝑜, 𝑜’s children and parent

  

6 3 12 9 7 24

slide-59
SLIDE 59

Remove(k)

  • Traverse the tree to find 𝑙
  • Let 𝑜 be the node found
  • Lock 𝑜’s predecessor edge

▫ Lock 𝑜’s successor edge ▫ Lock 𝑜, 𝑜’s children and parent ▫ If 𝑜 has at most 1 child:

 Mark 𝑜 as removed

6 3 12 7 24

slide-60
SLIDE 60

Remove(k)

  • Traverse the tree to find 𝑙
  • Let 𝑜 be the node found
  • Lock 𝑜’s predecessor edge

▫ Lock 𝑜’s successor edge ▫ Lock 𝑜, 𝑜’s children and parent ▫ If 𝑜 has at most 1 child:

 Mark 𝑜 as removed  Update predecessor-successor

6 3 12 7 24

slide-61
SLIDE 61

Remove(k)

  • Traverse the tree to find 𝑙
  • Let 𝑜 be the node found
  • Lock 𝑜’s predecessor edge

▫ Lock 𝑜’s successor edge ▫ Lock 𝑜, 𝑜’s children and parent ▫ If 𝑜 has at most 1 child:

 Mark 𝑜 as removed  Update predecessor-successor  Connect 𝑜’s parent and child

6 3 12 7 24

slide-62
SLIDE 62

Remove(k)

  • Traverse the tree to find 𝑙
  • Let 𝑜 be the node found
  • Lock 𝑜’s predecessor edge

▫ Lock 𝑜’s successor edge ▫ Lock 𝑜, 𝑜’s children and parent ▫ If 𝑜 has at most 1 child:

 Mark 𝑜 as removed  Update predecessor-successor  Connect 𝑜’s parent and child

6 3 12 7 24

slide-63
SLIDE 63

Remove(k)

▫ If 𝑜 has 2 children:

     

6 3 12 9 11 36 25

slide-64
SLIDE 64

Remove(k)

▫ If 𝑜 has 2 children:

 Lock 𝑜’s successor, its parent and child     

6 3 12 9 11 36 25

slide-65
SLIDE 65

Remove(k)

▫ If 𝑜 has 2 children:

 Lock 𝑜’s successor, its parent and child  Release 𝑜’s children locks    

6 3 12 9 11 36 25

slide-66
SLIDE 66

Remove(k)

▫ If 𝑜 has 2 children:

 Lock 𝑜’s successor, its parent and child  Release 𝑜’s children locks  Mark 𝑜 as removed   

3 12 9 11 36 25

slide-67
SLIDE 67

Remove(k)

▫ If 𝑜 has 2 children:

 Lock 𝑜’s successor, its parent and child  Release 𝑜’s children locks  Mark 𝑜 as removed  Update predecessor-successor  

3 12 9 11 36 25

slide-68
SLIDE 68

Remove(k)

▫ If 𝑜 has 2 children:

 Lock 𝑜’s successor, its parent and child  Release 𝑜’s children locks  Mark 𝑜 as removed  Update predecessor-successor  Connect the successor’s parent to the successor’s child and relocate 𝑜’s successor

3 12 9 11 36 25

slide-69
SLIDE 69

Remove(k)

▫ If 𝑜 has 2 children:

 Lock 𝑜’s successor, its parent and child  Release 𝑜’s children locks  Mark 𝑜 as removed  Update predecessor-successor  Connect the successor’s parent to the successor’s child and relocate 𝑜’s successor

3 12 9 11 36 25

slide-70
SLIDE 70

Update Operations Scheme

  • Traverse the tree to find 𝑙
  • Lock interval: [𝑞, 𝑡]
  • Confirm that the interval is appropriate:

▫ 𝑙 ∈ [𝑞, 𝑡] ▫ 𝑞 is not marked as removed

  • Lock tree locks
  • Update predecessor-successor relation
  • Update tree layout
  • Release all locks

70

slide-71
SLIDE 71

Correctness

  • The BST maintains two invariants

▫ Set invariant

 Protected by set-locks

▫ BST invariants

 Protected by tree-locks

  • The intervals allow us to separate the proof into

two proofs

27

slide-72
SLIDE 72

Correctness

  • Set invariant

▫ Each key appears at most once

  • A new key, 𝑙, is added only after locking an

interval 𝑞, 𝑡 such that 𝑙 ∈ (𝑞, 𝑡)

  • 𝑙 is not added if 𝑙 = 𝑞 or 𝑙 = 𝑡
  • 𝑙 cannot be added concurrently by another

thread

72

slide-73
SLIDE 73

Correctness

  • BST invariants

▫ For each node:

 The keys in the left sub-tree are smaller  The keys in the right sub-tree are bigger

  • The invariants may only be broken while

updating the tree layout

  • Any update operation locks all updated nodes
  • Locks are released only after the BST invariants

are held

73

slide-74
SLIDE 74

Outline

Balanced Binary Search Tree Concurrent Balanced Binary Search Tree 30

slide-75
SLIDE 75

Balanced Binary Search Tree

  • In BST, insert, remove and contains run in

𝑃 log 𝑜 in average.

  • In balanced BST, these operations run in

𝑃(log 𝑜) in the worst case.

  • There are several known implementations for

balanced BSTs

▫ We will focus on AVL trees

75

slide-76
SLIDE 76

AVL Trees

  • Each node maintains the invariant:

▫ The heights of the left and right sub-trees differ by at most 1

6 3 12 9 24 0:0 0:0 0:0 1:1 1:2 32

slide-77
SLIDE 77

AVL Trees

  • Each node maintains the invariant:

▫ The heights of the left and right sub-trees differ by at most 1

  • Insertion and removal may break

the invariant

6 3 12 9 24 18 0:0 0:0 0:0 1:1 1:2 1:0 1:2

1:3

32

slide-78
SLIDE 78

AVL Trees

  • Each node maintains the invariant:

▫ The heights of the left and right sub-trees differ by at most 1

  • Insertion and removal may break

the invariant

▫ Rotations are applied to fix it ▫ Rotations operate on adjacent nodes

6 3 12 9 24 18 0:0 0:0 0:0 1:1 1:2 1:0 1:2

1:3

32

slide-79
SLIDE 79

AVL Trees

  • Each node maintains the invariant:

▫ The heights of the left and right sub-trees differ by at most 1

  • Insertion and removal may break

the invariant

▫ Rotations are applied to fix it ▫ Rotations operate on adjacent nodes

6 3 12 9 24 18 33

slide-80
SLIDE 80

Outline

Concurrent Balanced Binary Search Tree 34

slide-81
SLIDE 81

Balancing Our Tree

  • After insertion or removal the tree is traversed

bottom-up beginning from the point where an update has occurred

  • If violation is detected, rotations are applied

▫ Only tree layout locks need to be acquired

35

slide-82
SLIDE 82

Balancing Our Tree

  • Rotations may lead to temporary disappearance
  • f nodes from the tree layout
  • However, the set-layout is unaffected by these

rotations

  • Since we consult the set-layout before making

final decisions, this cannot lead to wrong decisions

36

slide-83
SLIDE 83

Overview

37

slide-84
SLIDE 84

Evaluation

  • We compared our tree to state-of-the-art

implementations

  • Experiments ran on a machine with 32 cores

38

slide-85
SLIDE 85

Evaluation

39 200,000 keys 2,000,000 keys

  • 90% contains, 9% insert, 1% remove
slide-86
SLIDE 86

Summary

  • We presented a practical concurrent balanced

BST

  • Our main insight is that maintaining explicitly

the set layout results in a simpler algorithm for the concurrent balanced BST

40

Thank you!

slide-87
SLIDE 87

References

[1] BENDER, M. A., FINEMAN, J. T., GILBERT, S., AND KUSZMAUL, B. C. Concurrent cache-oblivious b-trees. In SPAA (2005), pp. 228–237. [2] BRONSON, N. G., CASPER, J., CHAFI, H., AND OLUKOTUN, K. A practical concurrent binary search tree. In PPoPP (2010), pp. 257–268. [3] CRAIN, T., GRAMOLI, V., AND RAYNAL, M. A contention-friendly binary search tree. In Euro-Par (2013), pp. 229–240. [4] ELLEN, F., FATOUROU, P., RUPPERT, E., AND VAN BREUGEL, F. Non-blocking binary search trees. In PODC (2010), pp. 131–140. [5] HOWLEY, S. V., AND JONES, J. A non-blocking internal binary search tree. In Proceedings of the 24th ACM symposium on Parallelism in algorithms and architectures (2012), SPAA ’12, pp. 161–171. [6] Nipkow, T., Pusch, C.: AVL trees. In Klein, G., Nipkow, T., Paulson, L. (eds.) The Archive of Formal Proofs. http://afp.sf.net/entries/AVL-Trees.shtml (2004) Formal proof development.

41