-
Notifications
You must be signed in to change notification settings - Fork 29
/
ring.go
130 lines (113 loc) · 2.7 KB
/
ring.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package chord
import (
"bytes"
"log"
"sort"
)
func (r *Ring) init(conf *Config, trans Transport) {
// Set our variables
r.config = conf
r.vnodes = make([]*localVnode, conf.NumVnodes)
r.transport = InitLocalTransport(trans)
r.delegateCh = make(chan func(), 32)
// Initializes the vnodes
for i := 0; i < conf.NumVnodes; i++ {
vn := &localVnode{}
r.vnodes[i] = vn
vn.ring = r
vn.init(i)
}
// Sort the vnodes
sort.Sort(r)
}
// Len is the number of vnodes
func (r *Ring) Len() int {
return len(r.vnodes)
}
// Less returns whether the vnode with index i should sort
// before the vnode with index j.
func (r *Ring) Less(i, j int) bool {
return bytes.Compare(r.vnodes[i].Id, r.vnodes[j].Id) == -1
}
// Swap swaps the vnodes with indexes i and j.
func (r *Ring) Swap(i, j int) {
r.vnodes[i], r.vnodes[j] = r.vnodes[j], r.vnodes[i]
}
// Returns the nearest local vnode to the key
func (r *Ring) nearestVnode(key []byte) *localVnode {
for i := len(r.vnodes) - 1; i >= 0; i-- {
if bytes.Compare(r.vnodes[i].Id, key) == -1 {
return r.vnodes[i]
}
}
// Return the last vnode
return r.vnodes[len(r.vnodes)-1]
}
// Schedules each vnode in the ring
func (r *Ring) schedule() {
if r.config.Delegate != nil {
go r.delegateHandler()
}
for i := 0; i < len(r.vnodes); i++ {
r.vnodes[i].schedule()
}
}
// Wait for all the vnodes to shutdown
func (r *Ring) stopVnodes() {
r.shutdown = make(chan bool, r.config.NumVnodes)
for i := 0; i < r.config.NumVnodes; i++ {
<-r.shutdown
}
}
// Stops the delegate handler
func (r *Ring) stopDelegate() {
if r.config.Delegate != nil {
// Wait for all delegate messages to be processed
<-r.invokeDelegate(r.config.Delegate.Shutdown)
close(r.delegateCh)
}
}
// Initializes the vnodes with their local successors
func (r *Ring) setLocalSuccessors() {
numV := len(r.vnodes)
numSuc := min(r.config.NumSuccessors, numV-1)
for idx, vnode := range r.vnodes {
for i := 0; i < numSuc; i++ {
vnode.successors[i] = &r.vnodes[(idx+i+1)%numV].Vnode
}
}
}
// Invokes a function on the delegate and returns completion channel
func (r *Ring) invokeDelegate(f func()) chan struct{} {
if r.config.Delegate == nil {
return nil
}
ch := make(chan struct{}, 1)
wrapper := func() {
defer func() {
ch <- struct{}{}
}()
f()
}
r.delegateCh <- wrapper
return ch
}
// This handler runs in a go routine to invoke methods on the delegate
func (r *Ring) delegateHandler() {
for {
f, ok := <-r.delegateCh
if !ok {
break
}
r.safeInvoke(f)
}
}
// Called to safely call a function on the delegate
func (r *Ring) safeInvoke(f func()) {
defer func() {
if r := recover(); r != nil {
log.Printf("Caught a panic invoking a delegate function! Got: %s", r)
}
}()
f()
}