-
Notifications
You must be signed in to change notification settings - Fork 1
/
dist.go
477 lines (437 loc) · 10.7 KB
/
dist.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package h3geodist
import (
"errors"
"fmt"
"math"
"sort"
"strconv"
"sync"
"github.com/uber/h3-go/v3"
)
var (
// ErrNoSlots means that the number of virtual nodes is distributed by 100%.
// It is necessary to change the configuration of virtual nodes.
ErrNoSlots = errors.New("h3geodist: no distribute slots")
// ErrVNodes returns when there are no virtual nodes.
ErrVNodes = errors.New("h3geodist: vnodes not found")
)
// Distributed holds information about nodes,
// and scheduler of virtual nodes with replicas.
// Thread-safe.
type Distributed struct {
mu sync.RWMutex
replFactor int
loadFactor float64
vnodes uint64
ring map[uint64]*node
index map[int]*node
hashes []uint64
nodes []*node
level int
stats map[string]float64
}
// Cell is a type to represent a distributed cell
// with specifying the hostname and H3 Index.
type Cell struct {
H3ID h3.H3Index
Host string
}
func (c Cell) String() string {
return fmt.Sprintf("Cell{Host: %s, ID: %s}", c.Host, h3.ToString(c.H3ID))
}
func (c Cell) HexID() string {
return h3.ToString(c.H3ID)
}
// NodeInfo is a type to represent a node load statistic.
type NodeInfo struct {
Host string
Load float64
}
type node struct {
addr string
}
// Default creates and returns a new Distributed instance with level - Level5.
func Default() *Distributed {
dist, _ := New(Level5)
return dist
}
// New creates and returns a new Distributed instance
// with specified cell level and options.
func New(cellLevel int, opts ...Option) (*Distributed, error) {
if ok := validateLevel(cellLevel); !ok {
return nil, fmt.Errorf("h3geodist: unsupported level - got %d, expected [%d-%d]",
cellLevel, Level0, Level6)
}
h3dist := &Distributed{
loadFactor: DefaultLoadFactor,
replFactor: DefaultReplicationFactor,
vnodes: DefaultVNodes,
level: cellLevel,
ring: make(map[uint64]*node),
index: make(map[int]*node),
stats: make(map[string]float64),
}
for _, f := range opts {
f(h3dist)
}
return h3dist, nil
}
// IsEmpty returns TRUE if the nodes list are empty, otherwise FALSE.
func (d *Distributed) IsEmpty() bool {
d.mu.RLock()
defer d.mu.RUnlock()
return len(d.nodes) == 0
}
// NumReplica returns number of replicas.
func (d *Distributed) NumReplica() int {
return d.replFactor
}
// Stats returns load distribution by nodes.
func (d *Distributed) Stats() []NodeInfo {
d.mu.RLock()
defer d.mu.RUnlock()
stats := make([]NodeInfo, 0, len(d.stats))
for host, load := range d.stats {
stats = append(stats, NodeInfo{
Host: host,
Load: load,
})
}
return stats
}
// VNodes returns number of virtual nodes.
func (d *Distributed) VNodes() uint64 {
return d.vnodes
}
// Nodes returns a list of nodes.
func (d *Distributed) Nodes() []string {
d.mu.RLock()
defer d.mu.RUnlock()
nodes := make([]string, 0, len(d.nodes))
for i := 0; i < len(d.nodes); i++ {
nodes = append(nodes, d.nodes[i].addr)
}
return nodes
}
// Lookup returns distributed cell.
func (d *Distributed) Lookup(cell h3.H3Index) (Cell, bool) {
d.mu.RLock()
defer d.mu.RUnlock()
if len(d.nodes) == 0 {
return Cell{}, false
}
addr, ok := d.lookup(cell)
if !ok {
return Cell{}, false
}
return Cell{H3ID: cell, Host: addr}, true
}
// IsOwned сhecks if the host for a distributed cell has changed.
func (d *Distributed) IsOwned(c Cell) bool {
d.mu.RLock()
defer d.mu.RUnlock()
addr, ok := d.lookup(c.H3ID)
if !ok {
return false
}
return addr == c.Host
}
// WhereIsMyParent finds and returns parent distributed cell.
// The child object must be less resolution than the parent's parent.
func (d *Distributed) WhereIsMyParent(child h3.H3Index) (c Cell, err error) {
d.mu.RLock()
defer d.mu.RUnlock()
curLevel := h3.Resolution(child)
if curLevel < d.level {
return c, fmt.Errorf("h3geodist: child resolution got %d, expected > %d",
curLevel, d.level)
}
cell := h3.ToParent(child, d.level)
addr, ok := d.lookup(cell)
if !ok {
return c, ErrVNodes
}
c.H3ID = cell
c.Host = addr
return
}
// LookupFromLatLon returns distributed cell.
func (d *Distributed) LookupFromLatLon(lat float64, lon float64) (c Cell, err error) {
d.mu.RLock()
defer d.mu.RUnlock()
cell := h3.FromGeo(h3.GeoCoord{Latitude: lat, Longitude: lon}, d.level)
addr, ok := d.lookup(cell)
if !ok {
return c, ErrVNodes
}
return Cell{H3ID: cell, Host: addr}, nil
}
// Neighbor is a type for represent a neighbor distributed cell,
// with the distance from a target point to the center of each neighbor.
type Neighbor struct {
Cell Cell
DistanceM float64
}
// NeighborsFromLatLon returns the current distributed cell
// for a geographic coordinate and neighbors sorted by distance in descending order.
// Distance is measured from geographic coordinates to the center of each neighbor.
func (d *Distributed) NeighborsFromLatLon(lat float64, lon float64) (target Cell, neighbors []Neighbor, err error) {
d.mu.RLock()
defer d.mu.RUnlock()
src := h3.GeoCoord{Latitude: lat, Longitude: lon}
cell := h3.FromGeo(src, d.level)
addr, ok := d.lookup(cell)
if !ok {
return target, nil, ErrVNodes
}
target.Host = addr
target.H3ID = cell
ring := h3.KRing(cell, 1)
neighbors = make([]Neighbor, 0, len(ring))
for i := 0; i < len(ring); i++ {
if !h3.AreNeighbors(cell, ring[i]) {
continue
}
addr, ok := d.lookup(ring[i])
if !ok {
continue
}
dest := h3.ToGeo(ring[i])
neighbors = append(neighbors, Neighbor{
Cell: Cell{Host: addr, H3ID: ring[i]},
DistanceM: h3.PointDistM(src, dest),
})
}
sort.Slice(neighbors, func(i, j int) bool {
return neighbors[i].DistanceM < neighbors[j].DistanceM
})
return
}
// ReplicaFor returns a list of hosts for replication.
func (d *Distributed) ReplicaFor(cell h3.H3Index, n int) ([]string, error) {
d.mu.RLock()
defer d.mu.RUnlock()
if n > len(d.nodes) {
return nil, fmt.Errorf("h3geodist: insufficient number of nodes want %d, have %d",
n, len(d.nodes))
}
var mykey uint64
var next int
myaddr, ok := d.lookup(cell)
if !ok {
return nil, ErrVNodes
}
keys := make([]uint64, 0, 4)
hosts := make(map[uint64]*node)
for i := 0; i < len(d.nodes); i++ {
hk := str2hash(d.nodes[i].addr)
if d.nodes[i].addr == myaddr {
mykey = hk
}
hosts[hk] = d.nodes[i]
keys = append(keys, hk)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
res := make([]string, 0, 4)
for next < len(keys) {
if keys[next] == mykey {
res = append(res, hosts[keys[next]].addr)
break
}
next++
}
for len(res) < n {
next++
if next >= len(keys) {
next = 0
}
res = append(res, hosts[keys[next]].addr)
}
return res, nil
}
// LookupMany returns a list of distributed cell.
func (d *Distributed) LookupMany(cell []h3.H3Index, iter func(c Cell) bool) bool {
d.mu.RLock()
defer d.mu.RUnlock()
if len(cell) == 0 || len(d.nodes) == 0 {
return false
}
for i := 0; i < len(cell); i++ {
addr, ok := d.lookup(cell[i])
if !ok {
continue
}
if ok := iter(Cell{H3ID: cell[i], Host: addr}); !ok {
return false
}
}
return true
}
// VNodeIndex returns the Index of the virtual node by H3Index.
func (d *Distributed) VNodeIndex(cell h3.H3Index) int {
hashKey := uint2hash(uint64(cell))
return int(hashKey % d.vnodes)
}
// EachVNode iterate each vnode, calling fn for each vnode.
func (d *Distributed) EachVNode(fn func(vnode uint64, addr string) bool) {
for i := uint64(0); i < d.vnodes; i++ {
addr, ok := d.Addr(i)
if !ok {
continue
}
if !fn(i, addr) {
break
}
}
}
// Addr returns the addr of the node by vnode id.
func (d *Distributed) Addr(vnode uint64) (addr string, ok bool) {
hashKey := uint2hash(vnode)
idx := int(hashKey % d.vnodes)
d.mu.RLock()
node, found := d.index[idx]
d.mu.RUnlock()
if !found {
return
}
addr = node.addr
ok = true
return
}
// EachCell iterate each distributed cell, calling fn for each cell.
func (d *Distributed) EachCell(iter func(c Cell)) {
d.mu.RLock()
defer d.mu.RUnlock()
if len(d.nodes) == 0 {
return
}
Iter(d.level, func(_ uint, cell h3.H3Index) {
addr, ok := d.lookup(cell)
if !ok {
return
}
iter(Cell{H3ID: cell, Host: addr})
})
}
// Add adds a new node.
func (d *Distributed) Add(addr string) error {
d.mu.Lock()
defer d.mu.Unlock()
if d.exist(addr) {
return nil
}
newNode := &node{addr: addr}
d.nodes = append(d.nodes, newNode)
d.add(newNode)
return d.distribute()
}
// Remove removes a node.
func (d *Distributed) Remove(addr string) {
d.mu.Lock()
defer d.mu.Unlock()
if !d.exist(addr) {
return
}
d.remove(addr)
_ = d.distribute()
}
func (d *Distributed) lookup(cell h3.H3Index) (addr string, ok bool) {
hashKey := uint2hash(uint64(cell))
idx := int(hashKey % d.vnodes)
node, found := d.index[idx]
if !found {
return
}
addr = node.addr
ok = true
return
}
func (d *Distributed) exist(addr string) (ok bool) {
for i := 0; i < len(d.nodes); i++ {
if addr == d.nodes[i].addr {
ok = true
break
}
}
return
}
func (d *Distributed) distribute() error {
stats := make(map[string]float64)
index := make(map[int]*node)
for vnode := uint64(0); vnode < d.vnodes; vnode++ {
nodeIndex := d.findNodeIndex(uint2hash(vnode))
avgload := d.AvgLoad()
var next int
for {
next++
if next >= len(d.hashes) {
return ErrNoSlots
}
node := d.ring[d.hashes[nodeIndex]]
load := stats[node.addr]
if load+1 <= avgload {
index[int(vnode)] = node
stats[node.addr]++
break
}
nodeIndex++
if nodeIndex >= len(d.hashes) {
nodeIndex = 0
}
}
}
d.index = index
d.stats = stats
return nil
}
// AvgLoad returns the average load.
func (d *Distributed) AvgLoad() float64 {
if len(d.nodes) == 0 {
return 0
}
return math.Ceil(float64(d.vnodes/uint64(len(d.nodes))) * d.loadFactor)
}
func (d *Distributed) findNodeIndex(hashKey uint64) int {
nodeIndex := sort.Search(len(d.hashes), func(n int) bool {
return d.hashes[n] >= hashKey
})
if nodeIndex >= len(d.hashes) {
nodeIndex = 0
}
return nodeIndex
}
func (d *Distributed) add(n *node) {
for i := 0; i < d.replFactor; i++ {
hashKey := str2hash(n.addr + strconv.Itoa(i))
d.ring[hashKey] = n
d.hashes = append(d.hashes, hashKey)
}
sort.Slice(d.hashes, func(i int, j int) bool {
return d.hashes[i] < d.hashes[j]
})
}
func (d *Distributed) remove(addr string) {
for i := 0; i < d.replFactor; i++ {
hashKey := str2hash(addr + strconv.Itoa(i))
delete(d.ring, hashKey)
for i := 0; i < len(d.hashes); i++ {
if d.hashes[i] == hashKey {
d.hashes = append(d.hashes[:i], d.hashes[i+1:]...)
break
}
}
}
for i := 0; i < len(d.nodes); i++ {
if d.nodes[i].addr == addr {
d.nodes = append(d.nodes[:i], d.nodes[i+1:]...)
}
}
delete(d.stats, addr)
if len(d.nodes) == 0 {
d.stats = make(map[string]float64)
d.index = make(map[int]*node)
d.hashes = make([]uint64, 0, d.vnodes)
}
}