-
Notifications
You must be signed in to change notification settings - Fork 3
/
location.go
81 lines (65 loc) · 1.84 KB
/
location.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
package gosnowth
import (
"context"
"fmt"
"path"
)
// LocateMetric returns a list of nodes owning the specified metric.
func (sc *SnowthClient) LocateMetric(uuid string, metric string,
node ...*SnowthNode,
) ([]TopologyNode, error) {
if len(node) > 0 {
return sc.LocateMetricRemote(uuid, metric, node[0])
}
topo, err := sc.Topology()
if err != nil {
return nil, err
}
return topo.FindMetric(uuid, metric)
}
// LocateMetricContext is the context aware version of LocateMetric.
func (sc *SnowthClient) LocateMetricContext(ctx context.Context, uuid string,
metric string, node ...*SnowthNode,
) ([]TopologyNode, error) {
if len(node) > 0 {
return sc.LocateMetricRemoteContext(ctx, uuid, metric, node[0])
}
topo, err := sc.Topology()
if err != nil {
return nil, err
}
return topo.FindMetric(uuid, metric)
}
// LocateMetricRemote locates which nodes contain specified metric data.
func (sc *SnowthClient) LocateMetricRemote(uuid string, metric string,
node *SnowthNode,
) ([]TopologyNode, error) {
return sc.LocateMetricRemoteContext(context.Background(),
uuid, metric, node)
}
// LocateMetricRemoteContext is the context aware version of LocateMetricRemote.
func (sc *SnowthClient) LocateMetricRemoteContext(ctx context.Context,
uuid string, metric string, node *SnowthNode,
) ([]TopologyNode, error) {
r := &Topology{}
if node == nil {
nodes := sc.ListActiveNodes()
if len(nodes) == 0 {
return nil, fmt.Errorf("no active nodes")
}
node = nodes[0]
}
body, _, err := sc.DoRequestContext(ctx, node, "GET",
path.Join("/locate/xml", uuid, metric), 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 r.WriteCopies == 0 {
r.WriteCopies = r.OldWriteCopies
}
r.OldWriteCopies = r.WriteCopies
return r.Nodes, nil
}