-
Notifications
You must be signed in to change notification settings - Fork 5
/
hook_debug.go
108 lines (100 loc) · 2.15 KB
/
hook_debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//go:build hashring_debug
// +build hashring_debug
package hashring
import (
"fmt"
"log"
"strings"
"github.com/gobwas/avl"
)
const debug = true
func assertNotExists(tree avl.Tree, p *point) {
if x := tree.Search(p); x != nil && x.(*point) == p {
// NOTE: x could be another point collided with p.
panic(fmt.Sprintf(
"hashring: internal error: point must not exist on the ring",
))
}
}
func setupRingTrace(r *Ring) {
log.SetFlags(0)
var depth int
enter := func() {
depth++
log.SetPrefix(strings.Repeat(" ", depth*4))
}
leave := func() {
depth--
log.SetPrefix(strings.Repeat(" ", depth*4))
}
r.trace = r.trace.Compose(traceRing{
OnInsert: func(p *point) traceRingInsert {
log.Println("inserting:", pointInfo(p))
enter()
return traceRingInsert{
OnDone: func(inserted bool) {
leave()
if inserted {
log.Println("inserted")
} else {
log.Println("not inserted")
}
},
OnCollision: func(prev *point) {
log.Println("collision:")
enter()
log.Println("prev:", pointInfo(prev))
log.Println("next:", pointInfo(p))
leave()
},
}
},
OnDelete: func(p *point) traceRingDelete {
log.Println("deleting:", pointInfo(p))
enter()
return traceRingDelete{
OnDone: func(deleted bool) {
leave()
if deleted {
log.Println("deleted")
} else {
log.Println("not deleted")
}
},
OnProcessing: func(p *point) func() {
log.Println("processing:", pointInfo(p))
enter()
return func() {
leave()
log.Println("processed")
}
},
OnTwinDelete: func(p *point) {
log.Println("deleting twin", pointInfo(p))
},
OnTwinRestore: func(p *point) {
log.Println("restoring twin", pointInfo(p))
},
}
},
OnFixNeeded: func(p *point) {
log.Println("enqueued for fix:", pointInfo(p))
},
OnFix: func(p *point) traceRingFix {
log.Println("fixing:", pointInfo(p))
enter()
return traceRingFix{
OnDone: func() {
leave()
fmt.Println("fixed")
},
}
},
})
}
func pointInfo(p *point) string {
return fmt.Sprintf(
"%p: %s[%d] %v %d",
p, p.bucket.item, p.index, p.stack, p.val,
)
}