-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathconsistent.go
More file actions
383 lines (328 loc) · 9.62 KB
/
consistent.go
File metadata and controls
383 lines (328 loc) · 9.62 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright 2018 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package consistent provides a consistent hashing function.
//
// Consistent hashing is often used to distribute requests to a changing set of servers. For example,
// say you have some cache servers cacheA, cacheB, and cacheC. You want to decide which cache server
// to use to look up information on a user.
//
// You could use a typical hash table and hash the user id
// to one of cacheA, cacheB, or cacheC. But with a typical hash table, if you add or remove a server,
// almost all keys will get remapped to different results, which basically could bring your service
// to a grinding halt while the caches get rebuilt.
//
// With a consistent hash, adding or removing a server drastically reduces the number of keys that
// get remapped.
//
// Read more about consistent hashing on wikipedia: http://en.wikipedia.org/wiki/Consistent_hashing
//
package consistent
import (
"errors"
"expvar"
"sort"
"strconv"
"sync"
mw "github.com/zserge/metric"
"github.com/CovenantSQL/CovenantSQL/conf"
"github.com/CovenantSQL/CovenantSQL/crypto/hash"
"github.com/CovenantSQL/CovenantSQL/proto"
"github.com/CovenantSQL/CovenantSQL/utils/log"
)
// NodeKeys is NodeKey array.
type NodeKeys []proto.NodeKey
// Len returns the length of the uints array.
func (x NodeKeys) Len() int { return len(x) }
// Less returns true if node i is less than node j.
func (x NodeKeys) Less(i, j int) bool { return x[i].Less(&x[j]) }
// Swap exchanges nodes i and j.
func (x NodeKeys) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
var (
// ErrEmptyCircle is the error returned when trying to get an node when nothing has been added to hash.
ErrEmptyCircle = errors.New("empty circle")
// ErrKeyNotFound is the error returned when no key in circle
ErrKeyNotFound = errors.New("node key not found")
)
const (
mwKeyDHTNodeCount = "service:DHT:NodeCount"
)
func init() {
expvar.Publish(mwKeyDHTNodeCount, mw.NewGauge("1M1h"))
}
// Consistent holds the information about the members of the consistent hash circle.
type Consistent struct {
// TODO(auxten): do not store node info on circle, just put node id as value
// and put node info into kms
circle map[proto.NodeKey]*proto.Node
//members map[proto.NodeID]proto.Node
sortedHashes NodeKeys
NumberOfReplicas int
persist Persistence
cacheLock sync.RWMutex
sync.RWMutex
}
// InitConsistent creates a new Consistent object with a default setting of 20 replicas for each entry.
func InitConsistent(storePath string, persistImpl Persistence, initBP bool) (c *Consistent, err error) {
var BPNodes []proto.Node
if initBP {
// Load BlockProducer public key, set it in public key store
// as all kms.BP stuff is initialized on kms init()
if conf.GConf == nil {
log.Fatal("must call conf.LoadConfig first")
}
BPNodes = conf.GConf.SeedBPNodes
}
c = &Consistent{
//TODO(auxten): reduce NumberOfReplicas
NumberOfReplicas: 20,
circle: make(map[proto.NodeKey]*proto.Node),
persist: persistImpl,
}
err = c.persist.Init(storePath, BPNodes)
if err != nil {
log.WithError(err).Error("init persist BP nodes failed")
return
}
nodes, err := c.persist.GetAllNodeInfo()
if err != nil {
log.WithError(err).Error("get all node id failed")
return
}
log.Debugf("c.persist.GetAllNodeInfo: %#v", nodes)
_, isKMSStorage := c.persist.(*KMSStorage)
if isKMSStorage {
for _, n := range nodes[:] {
c.Add(n)
}
} else {
// currently just for KayakKVServer
for _, n := range nodes[:] {
c.AddCache(n)
}
}
return
}
// nodeKey generates a string key for an node with an index.
func (c *Consistent) nodeKey(nodeID proto.NodeID, idx int) string {
// return node + "|" + strconv.Itoa(idx)
return strconv.Itoa(idx) + string(nodeID)
}
// Add inserts a string node in the consistent hash.
func (c *Consistent) Add(node proto.Node) (err error) {
log.WithField("node", node).Debug("add node to consistent ring")
c.Lock()
defer c.Unlock()
return c.add(node)
}
// Remove removes an node from the hash.
func (c *Consistent) Remove(nodeID proto.NodeID) (err error) {
c.Lock()
defer c.Unlock()
err = c.persist.DelNode(nodeID)
if err != nil {
log.WithField("node", nodeID).WithError(err).Error("del node failed")
return
}
c.RemoveCache(nodeID)
return
}
// Set sets all the nodes in the hash. If there are existing nodes not
// present in nodes, they will be removed.
func (c *Consistent) Set(nodes []proto.Node) (err error) {
c.Lock()
defer c.Unlock()
err = c.persist.Reset()
if err != nil {
log.WithError(err).Error("reset bucket failed")
return
}
c.ResetCache()
for _, v := range nodes {
c.add(v)
}
return
}
// need c.Lock() before calling.
func (c *Consistent) add(node proto.Node) (err error) {
err = c.persist.SetNode(&node)
if err != nil {
log.WithField("node", node).WithError(err).Error("set node info failed")
return
}
c.AddCache(node)
return
}
// AddCache only adds c.circle skips persist.
func (c *Consistent) AddCache(node proto.Node) {
c.cacheLock.Lock()
defer c.cacheLock.Unlock()
for i := 0; i < c.NumberOfReplicas; i++ {
c.circle[hashKey(c.nodeKey(node.ID, i))] = &node
}
c.updateSortedHashes()
expvar.Get(mwKeyDHTNodeCount).(mw.Metric).Add(float64(len(c.circle) / c.NumberOfReplicas))
return
}
// RemoveCache removes an node from the hash cache.
func (c *Consistent) RemoveCache(nodeID proto.NodeID) {
c.cacheLock.Lock()
defer c.cacheLock.Unlock()
for i := 0; i < c.NumberOfReplicas; i++ {
delete(c.circle, hashKey(c.nodeKey(nodeID, i)))
}
c.updateSortedHashes()
}
// ResetCache removes all node from the hash cache.
func (c *Consistent) ResetCache() {
c.cacheLock.Lock()
defer c.cacheLock.Unlock()
c.circle = make(map[proto.NodeKey]*proto.Node)
c.sortedHashes = NodeKeys{}
}
// GetNeighbor returns an node close to where name hashes to in the circle.
func (c *Consistent) GetNeighbor(name string) (proto.Node, error) {
c.RLock()
defer c.RUnlock()
c.cacheLock.RLock()
defer c.cacheLock.RUnlock()
if len(c.circle) == 0 {
return proto.Node{}, ErrEmptyCircle
}
key := hashKey(name)
i := c.search(key)
return *c.circle[c.sortedHashes[i]], nil
}
// GetNode returns an node by its node id.
func (c *Consistent) GetNode(name string) (*proto.Node, error) {
c.RLock()
defer c.RUnlock()
c.cacheLock.RLock()
defer c.cacheLock.RUnlock()
n, ok := c.circle[hashKey(c.nodeKey(proto.NodeID(name), 0))]
if ok {
return n, nil
}
return nil, ErrKeyNotFound
}
func (c *Consistent) search(key proto.NodeKey) (i int) {
f := func(x int) bool {
//return c.sortedHashes[x] > key
return key.Less(&c.sortedHashes[x])
}
i = sort.Search(len(c.sortedHashes), f)
if i >= len(c.sortedHashes) {
i = 0
}
return
}
// GetTwoNeighbors returns the two closest distinct nodes to the name input in the circle.
func (c *Consistent) GetTwoNeighbors(name string) (proto.Node, proto.Node, error) {
c.RLock()
defer c.RUnlock()
c.cacheLock.RLock()
defer c.cacheLock.RUnlock()
if len(c.circle) == 0 {
return proto.Node{}, proto.Node{}, ErrEmptyCircle
}
key := hashKey(name)
i := c.search(key)
a := *c.circle[c.sortedHashes[i]]
if len(c.sortedHashes)/c.NumberOfReplicas == 1 {
return a, proto.Node{}, nil
}
start := i
var b proto.Node
for i = start + 1; i != start; i++ {
if i >= len(c.sortedHashes) {
i = 0
}
b = *c.circle[c.sortedHashes[i]]
if b.ID != a.ID {
break
}
}
return a, b, nil
}
// GetNeighborsEx returns the N closest distinct nodes to the name input in the circle.
func (c *Consistent) GetNeighborsEx(name string, n int, roles proto.ServerRoles) ([]proto.Node, error) {
c.RLock()
defer c.RUnlock()
c.cacheLock.RLock()
defer c.cacheLock.RUnlock()
if len(c.circle) == 0 {
return nil, ErrEmptyCircle
}
count := len(c.sortedHashes) / c.NumberOfReplicas
if count < n {
n = count
}
var (
key = hashKey(name)
i = c.search(key)
start = i
res = make([]proto.Node, 0, n)
elem = *c.circle[c.sortedHashes[i]]
)
var noFilter = roles == nil || len(roles) == 0
if noFilter || roles.Contains(elem.Role) {
res = append(res, elem)
}
if noFilter && len(res) == n {
return res, nil
}
for i = start + 1; i != start; i++ {
if i >= len(c.sortedHashes) {
i = 0
}
elem = *c.circle[c.sortedHashes[i]]
if noFilter || roles.Contains(elem.Role) {
if !sliceContainsMember(res, elem) {
res = append(res, elem)
}
}
if len(res) == n {
break
}
}
return res, nil
}
// GetNeighbors returns the N closest distinct nodes to the name input in the circle.
func (c *Consistent) GetNeighbors(name string, n int) ([]proto.Node, error) {
return c.GetNeighborsEx(name, n, nil)
}
func hashKey(key string) proto.NodeKey {
return proto.NodeKey{Hash: hash.HashH([]byte(key))}
}
func (c *Consistent) updateSortedHashes() {
hashes := c.sortedHashes[:0]
//reallocate if we're holding on to too much (1/4th)
if cap(c.sortedHashes)/(c.NumberOfReplicas*4) > len(c.circle) {
hashes = nil
}
for k := range c.circle {
hashes = append(hashes, k)
}
sort.Sort(hashes)
c.sortedHashes = hashes
}
func sliceContainsMember(set []proto.Node, member proto.Node) bool {
for _, m := range set {
if m.ID == member.ID {
return true
}
}
return false
}