-
Notifications
You must be signed in to change notification settings - Fork 3
/
topology.go
455 lines (363 loc) · 10.5 KB
/
topology.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
package gosnowth
import (
"bytes"
"context"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"encoding/xml"
"fmt"
"path"
"sort"
"strings"
)
type topologyNodeSlot struct {
Location [sha256.Size]byte
Node *TopologyNode
Idx uint16
}
// Topology values represent IRONdb topology structure.
type Topology struct {
XMLName xml.Name `json:"-" xml:"nodes"`
OldWriteCopies uint8 `json:"-" xml:"n,attr"`
WriteCopies uint8 `json:"-" xml:"write_copies,attr"`
useSide bool `json:"-" xml:"-"`
Hash string `json:"-" xml:"-"`
Nodes []TopologyNode `json:"-" xml:"node"`
ring []topologyNodeSlot `json:"-" xml:"-"`
}
func (topo *Topology) Len() int { return len(topo.ring) }
func (topo *Topology) Swap(i, j int) {
topo.ring[i], topo.ring[j] = topo.ring[j], topo.ring[i]
}
func (topo *Topology) Less(i, j int) bool {
return bytes.Compare(topo.ring[i].Location[:], topo.ring[j].Location[:]) < 0
}
// TopoSide represent the (both = 0, a = 1, b = 2) side of a snowth ring.
type TopoSide uint8
// UnmarshalXMLAttr handle side enum to integral transformation.
func (i *TopoSide) UnmarshalXMLAttr(attr xml.Attr) error {
switch strings.ToLower(attr.Value) {
default:
*i = 0
case "a":
*i = 1
case "b":
*i = 2
}
return nil
}
// MarshalXMLAttr handle side integral to enum transformation.
func (i TopoSide) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
var s string
switch i {
default:
s = "both"
case 1:
s = "a"
case 2:
s = "b"
}
return xml.Attr{Name: name, Value: s}, nil
}
// TopologyNode represent a node in the IRONdb topology structure.
type TopologyNode struct {
XMLName xml.Name `json:"-" xml:"node"`
ID string `json:"id" xml:"id,attr"`
Address string `json:"address" xml:"address,attr"`
Port uint16 `json:"port" xml:"port,attr"`
APIPort uint16 `json:"apiport" xml:"apiport,attr"`
Weight uint16 `json:"weight " xml:"weight,attr"`
Side TopoSide `json:"side" xml:"side,attr"`
WriteCopies uint8 `json:"n" xml:"-"`
}
func (topo *Topology) compile() error {
nslots := 0
if topo.WriteCopies == 0 {
topo.WriteCopies = topo.OldWriteCopies
}
topo.OldWriteCopies = topo.WriteCopies
for _, node := range topo.Nodes {
node.ID = strings.ToLower(node.ID)
if node.Side != 0 {
topo.useSide = true
}
nslots += int(node.Weight)
}
hash := sha256.New()
for _, node := range topo.Nodes {
if _, err := hash.Write([]byte(node.ID)); err != nil {
return fmt.Errorf("unable to write hash: %w", err)
}
if _, err := hash.Write([]byte{0, 0}); err != nil {
return fmt.Errorf("unable to write hash: %w", err)
}
netshort := make([]byte, 2)
binary.BigEndian.PutUint16(netshort, node.Weight)
if _, err := hash.Write(netshort); err != nil {
return fmt.Errorf("unable to write hash: %w", err)
}
if topo.useSide {
binary.BigEndian.PutUint16(netshort, uint16(node.Side))
if _, err := hash.Write(netshort); err != nil {
return fmt.Errorf("unable to write hash: %w", err)
}
}
}
// This matches the horrible backware compatibility requirements in the
// C version.
if topo.WriteCopies != 2 { //nolint:nestif
if _, err := hash.Write(bytes.Repeat([]byte{0}, 38)); err != nil {
return fmt.Errorf("unable to write hash: %w", err)
}
netshort := make([]byte, 2)
binary.BigEndian.PutUint16(netshort, uint16(topo.WriteCopies))
if _, err := hash.Write(netshort); err != nil {
return fmt.Errorf("unable to write hash: %w", err)
}
if topo.useSide {
binary.BigEndian.PutUint16(netshort, 0)
if _, err := hash.Write(netshort); err != nil {
return fmt.Errorf("unable to write hash: %w", err)
}
}
}
sum := hex.EncodeToString(hash.Sum(nil))
if topo.Hash == "" {
topo.Hash = sum
}
if topo.Hash != sum {
return fmt.Errorf("bad topology hash")
}
topo.ring = make([]topologyNodeSlot, nslots)
i := 0
for nodeIdx, node := range topo.Nodes {
for w := uint16(0); w < node.Weight; w++ {
location := make([]byte, 38)
copy(location, node.ID)
binary.BigEndian.PutUint16(location[36:], w)
sum := sha256.Sum256(location)
copy(topo.ring[i].Location[:], sum[:])
topo.ring[i].Node = &topo.Nodes[nodeIdx]
topo.ring[i].Idx = w
if node.Side == 1 {
topo.ring[i].Location[0] &= 0x7f
} else if node.Side == 2 {
topo.ring[i].Location[0] |= 0x80
}
i++
}
}
sort.Sort(topo)
return nil
}
func (topo *Topology) binSearchNext(location [sha256.Size]byte) (int, int) {
start := 0
end := len(topo.ring) - 1
mid := len(topo.ring) / 2
var cmp int
for start <= end {
cmp = bytes.Compare(location[:], topo.ring[mid].Location[:])
if cmp == 0 {
return mid, mid
}
if cmp < 0 {
end = mid - 1
mid = (start + end) / 2
continue
}
start = mid + 1
mid = (start + end) / 2
}
cmp = bytes.Compare(location[:], topo.ring[mid].Location[:])
if cmp > 0 {
mid++
}
if mid >= len(topo.ring) {
mid = 0
}
return -1, mid
}
func nodeListContains(list []TopologyNode, item TopologyNode) bool {
for _, node := range list {
if node.ID == item.ID {
return true
}
}
return false
}
func (topo *Topology) findNext(location [sha256.Size]byte, found []TopologyNode) *TopologyNode {
side := 0
if topo.useSide {
side = 1
if (location[0] & 0x80) != 0 {
side = 2
}
}
idx, next := topo.binSearchNext(location)
if idx < 0 {
idx = next
}
for attempts := 0; attempts < 2; attempts++ {
for idx < len(topo.ring) {
slot := &topo.ring[idx]
if side == 0 ||
((slot.Location[0]&0x80) == 0 && side == 1) ||
((slot.Location[0]&0x80) != 0 && side == 2) {
// We're unsided or this side matches
if len(found) == 0 || !nodeListContains(found, *slot.Node) {
return slot.Node
}
}
idx++
}
idx = 0
}
return nil
}
// FindMetricNodeIDs finds topo.WriteCopies nodes on which uuid-metric lives.
func (topo *Topology) FindMetricNodeIDs(uuid, metric string) ([]string, error) {
nodes, err := topo.FindN(strings.ToLower(uuid)+"-"+metric,
int(topo.WriteCopies))
if nodes == nil {
if err == nil {
err = fmt.Errorf("unable to find metric node: FindN failed")
}
return nil, err
}
results := make([]string, len(nodes))
for i, node := range nodes {
results[i] = node.ID
}
return results, nil
}
// FindMetric finds topo.WriteCopies nodes on which uuid-metric lives.
func (topo *Topology) FindMetric(uuid, metric string) ([]TopologyNode, error) {
return topo.FindN(strings.ToLower(uuid)+"-"+metric, int(topo.WriteCopies))
}
// FindMetricN finds n nodes on which uuid-metric lives.
func (topo *Topology) FindMetricN(uuid, metric string, n int) ([]TopologyNode, error) {
return topo.FindN(strings.ToLower(uuid)+"-"+metric, n)
}
// Find finds topo.WriteCopies nodes on which s lives.
func (topo *Topology) Find(s string) ([]TopologyNode, error) {
return topo.FindN(s, int(topo.WriteCopies))
}
// FindN finds n nodes on which s lives.
func (topo *Topology) FindN(s string, n int) ([]TopologyNode, error) {
if topo.ring == nil || len(topo.ring) < 1 {
return nil, fmt.Errorf("unable to find n nodes: empty topology")
}
location := sha256.Sum256([]byte(s))
nodes := make([]TopologyNode, 0)
for i := 0; i < n; i++ {
node := topo.findNext(location, nodes)
if node == nil {
break
}
nodes = append(nodes, *node)
location[0] ^= 0x80
}
return nodes, nil
}
// GetTopologyInfo retrieves topology information from a node.
func (sc *SnowthClient) GetTopologyInfo(nodes ...*SnowthNode) (*Topology, error) {
var node *SnowthNode
if len(nodes) > 0 && nodes[0] != nil {
node = nodes[0]
} else {
node = sc.GetActiveNode()
}
if node == nil {
return nil, fmt.Errorf("unable to get active node")
}
return sc.GetTopologyInfoContext(context.Background(), node)
}
// TopologyLoadXML creates a new Topology directly from an XML buffer.
func TopologyLoadXML(xml string) (*Topology, error) {
r := &Topology{}
if err := decodeXML(strings.NewReader(xml), &r); err != nil {
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}
if err := r.compile(); err != nil {
return nil, err
}
return r, nil
}
// GetTopologyInfoContext is the context aware version of GetTopologyInfo.
func (sc *SnowthClient) GetTopologyInfoContext(ctx context.Context,
nodes ...*SnowthNode,
) (*Topology, error) {
r := &Topology{}
var node *SnowthNode
if len(nodes) > 0 && nodes[0] != nil {
node = nodes[0]
} else {
node = sc.GetActiveNode()
}
if node == nil {
return nil, fmt.Errorf("unable to get active node")
}
topologyID := node.GetCurrentTopology()
if topologyID == "" {
return nil, fmt.Errorf("no active topology")
}
if topologyID == sc.currentTopology && sc.currentTopologyCompiled != nil {
return sc.currentTopologyCompiled, nil
}
body, _, err := sc.DoRequestContext(ctx, node, "GET",
path.Join("/topology/xml", node.GetCurrentTopology()), nil, nil)
if err != nil {
return nil, err
}
if err := decodeXML(body, &r); err != nil {
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}
if err = r.compile(); err != nil {
return nil, err
}
sc.currentTopology = topologyID
sc.currentTopologyCompiled = r
return r, nil
}
// LoadTopology loads a new topology on a node without activating it.
func (sc *SnowthClient) LoadTopology(hash string, t *Topology,
nodes ...*SnowthNode,
) error {
var node *SnowthNode
if len(nodes) > 0 && nodes[0] != nil {
node = nodes[0]
} else {
node = sc.GetActiveNode()
}
if node == nil {
return fmt.Errorf("unable to get active node")
}
return sc.LoadTopologyContext(context.Background(), hash, t, node)
}
// LoadTopologyContext is the context aware version of LoadTopology.
func (sc *SnowthClient) LoadTopologyContext(ctx context.Context, hash string,
t *Topology, node *SnowthNode,
) error {
b, err := encodeXML(t)
if err != nil {
return fmt.Errorf("failed to encode request data: %w", err)
}
_, _, err = sc.DoRequestContext(ctx, node, "POST",
path.Join("/topology", hash), b, nil)
return err
}
// ActivateTopology activates a new topology on the node.
// WARNING THIS IS DANGEROUS.
func (sc *SnowthClient) ActivateTopology(hash string, node *SnowthNode) error {
return sc.ActivateTopologyContext(context.Background(), hash, node)
}
// ActivateTopologyContext is the context aware version of ActivateTopology.
// WARNING THIS IS DANGEROUS.
func (sc *SnowthClient) ActivateTopologyContext(ctx context.Context,
hash string, node *SnowthNode,
) error {
_, _, err := sc.DoRequestContext(ctx, node, "GET",
path.Join("/activate", hash), nil, nil)
return err
}