generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locator.go
306 lines (249 loc) · 6.88 KB
/
locator.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package hashy
import (
"sort"
"sync"
"unsafe"
)
const (
// DefaultVNodes is used for a Locator when no vnodes value is set.
DefaultVNodes int = 200
)
// Group represents the name of a logical group of services.
// Service Locators can return services from multiple groups.
type Group string
type locatorNode struct {
group Group
names Names
ring ring
}
type locatorNodes []locatorNode
func (ln locatorNodes) Len() int {
return len(ln)
}
func (ln locatorNodes) Less(i, j int) bool {
return ln[i].group < ln[j].group
}
func (ln locatorNodes) Swap(i, j int) {
ln[i], ln[j] = ln[j], ln[i]
}
// sort sorts this set of nodes by Group
func (ln locatorNodes) sort() {
sort.Sort(ln)
}
// find locates the node associated with the given group and its
// index, if that group exists. If the group wasn't found, this method
// always returns nil and the length of the slice.
func (ln locatorNodes) find(g Group) (*locatorNode, int) {
i := sort.Search(
len(ln),
func(i int) bool { return ln[i].group >= g },
)
if i < ln.Len() && ln[i].group == g {
return &ln[i], i
}
return nil, ln.Len()
}
// append adds more names to a group, or creates the group if it doesn't
// exist. If the more slice is empty, this method does nothing.
func (ln locatorNodes) append(g Group, more ...string) locatorNodes {
if len(more) > 0 {
if existing, _ := ln.find(g); existing != nil {
existing.names = existing.names.Merge(more...)
} else {
ln = append(ln, locatorNode{
group: g,
names: NewNames(more...),
})
ln.sort()
}
}
return ln
}
// remove deletes a group and its names from this slice, if
// that group exists.
func (ln locatorNodes) remove(g Group) locatorNodes {
if existing, index := ln.find(g); existing != nil {
last := ln.Len() - 1
ln[index], ln[last] = ln[last], locatorNode{}
ln = ln[:last]
ln.sort()
}
return ln
}
// insert puts or replaces a new node into this slice.
func (ln locatorNodes) insert(newNode locatorNode) locatorNodes {
if _, index := ln.find(newNode.group); index < ln.Len() {
ln[index] = newNode
} else {
ln = append(ln, newNode)
ln.sort()
}
return ln
}
// Service is a located service.
type Service struct {
// Group is any group associated with this service instance.
Group Group
// Name is the service name, which is normally a host name.
Name string
}
// LocatorOption represents a configurable option for a service Locator.
type LocatorOption interface {
apply(*locator) error
}
type locatorOptionFunc func(*locator) error
func (f locatorOptionFunc) apply(l *locator) error { return f(l) }
// WithVNodes configures the number of vnodes used per group by the locator.
// The default is DefaultVNodes. To effectively disable consistent hashing,
// set the number of vnodes to 1.
func WithVNodes(vnodes int) LocatorOption {
return locatorOptionFunc(func(l *locator) error {
l.vnodes = vnodes
return nil
})
}
// WithHash sets the hash algorithm used by the locator. The default
// used is Murmur3.
func WithHash(hash Hash) LocatorOption {
return locatorOptionFunc(func(l *locator) error {
l.hash = hash
return nil
})
}
// WithGroup initializes service names in a group for the locator. Multiple
// uses of this option are cumulative.
func WithGroup(g Group, more ...string) LocatorOption {
return locatorOptionFunc(func(l *locator) error {
l.nodes = l.nodes.append(g, more...)
return nil
})
}
// Locator is a service locator.
type Locator interface {
// Find locates services associated with the given value.
Find([]byte) []Service
// FindGroup locates services associated with the given value,
// but only within the specified group. If the given group does
// not exist, this method returns an empty slice.
FindGroup(Group, []byte) []Service
// Remove atomically removes a group and all its service names
// from this Locator. If no such group exists, this method
// does nothing.
Remove(Group)
// Update atomically updates the set of service names associated
// with the given group.
Update(Group, ...string)
}
// locator is the internal Locator implementation.
type locator struct {
vnodes int
hash Hash
lock sync.RWMutex
nodes locatorNodes
}
// initialize establishes defaults and builds any necessary hash rings for
// initial groups.
func (l *locator) initialize() {
if l.vnodes < 1 {
l.vnodes = DefaultVNodes
}
if l.hash == nil {
l.hash = Murmur3{}
}
for _, ln := range l.nodes {
ln.ring = l.newRing(ln.names)
}
}
// newRing creates a hash ring using this locator's configuration
// along with the given service names. This method does not
// require execution under the lock.
func (l *locator) newRing(names Names) ring {
return newRing(l.vnodes, l.hash.New64(), names)
}
func (l *locator) Find(v []byte) (services []Service) {
l.lock.RLock()
if len(l.nodes) > 0 {
services = make([]Service, 0, len(l.nodes))
target := l.hash.Sum64(v)
for _, ln := range l.nodes {
services = append(services, Service{
Group: ln.group,
Name: ln.ring.get(target),
})
}
}
l.lock.RUnlock()
return
}
func (l *locator) FindGroup(g Group, v []byte) (services []Service) {
l.lock.RLock()
if node, _ := l.nodes.find(g); node != nil {
services = []Service{
Service{
Group: node.group,
Name: node.ring.get(l.hash.Sum64(v)),
},
}
}
l.lock.RUnlock()
return
}
func (l *locator) Remove(g Group) {
l.lock.Lock()
l.nodes = l.nodes.remove(g)
l.lock.Unlock()
}
func (l *locator) Update(g Group, list ...string) {
var updatedNames Names
needsUpdate := true
l.lock.RLock()
existing, _ := l.nodes.find(g)
if existing != nil {
updatedNames, needsUpdate = existing.names.Update(list...)
} else {
updatedNames = NewNames(list...)
needsUpdate = true // this is a new group
}
l.lock.RUnlock()
if needsUpdate {
// compute the new hash ring outside any lock
newNode := locatorNode{
group: g,
names: updatedNames,
ring: l.newRing(updatedNames),
}
l.lock.Lock()
// another goroutine may have barged and updated the same group.
// so, we need to search again for that group's node, which is
// what insert does.
l.nodes = l.nodes.insert(newNode)
l.lock.Unlock()
}
}
// NewLocator creates a hash-based service locator from a set of options.
func NewLocator(opts ...LocatorOption) (Locator, error) {
l := new(locator)
for _, o := range opts {
if err := o.apply(l); err != nil {
return nil, err
}
}
l.initialize()
return l, nil
}
// Find uses the given locator to find services by a given string
// value. No additional memory allocation is performed, making this
// a better option that using []byte(value).
func Find(l Locator, v string) []Service {
return l.Find(
unsafe.Slice(unsafe.StringData(v), len(v)),
)
}
// FindGroup uses the given Locator to find a service only within
// a certain group. No additional memory allocation is performed.
func FindGroup(l Locator, g Group, v string) []Service {
return l.FindGroup(
g,
unsafe.Slice(unsafe.StringData(v), len(v)),
)
}