-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhypercache_dist.go
More file actions
106 lines (87 loc) · 2.59 KB
/
hypercache_dist.go
File metadata and controls
106 lines (87 loc) · 2.59 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
package hypercache
import (
"github.com/hyp3rd/hypercache/pkg/backend"
)
// DistMetrics returns distributed backend metrics if the underlying backend is DistMemory.
// Returns nil if unsupported.
func (hyperCache *HyperCache[T]) DistMetrics() any { // generic any to avoid exporting type into core interface
if dm, ok := any(hyperCache.backend).(*backend.DistMemory); ok {
m := dm.Metrics()
return m
}
return nil
}
// ClusterOwners returns the owners for a key if the distributed backend supports it; otherwise empty slice.
func (hyperCache *HyperCache[T]) ClusterOwners(key string) []string {
if dm, ok := any(hyperCache.backend).(*backend.DistMemory); ok {
owners := dm.DebugOwners(key)
out := make([]string, 0, len(owners))
for _, o := range owners {
out = append(out, string(o))
}
return out
}
return nil
}
// DistMembershipSnapshot returns a snapshot of membership if distributed backend; otherwise nil slice.
//
//nolint:nonamedreturns
func (hyperCache *HyperCache[T]) DistMembershipSnapshot() (members []struct {
ID string
Address string
State string
Incarnation uint64
}, replication, vnodes int,
) {
if dm, ok := any(hyperCache.backend).(*backend.DistMemory); ok {
membership := dm.Membership()
ring := dm.Ring()
if membership == nil || ring == nil {
return nil, 0, 0
}
nodes := membership.List()
out := make([]struct {
ID string
Address string
State string
Incarnation uint64
}, 0, len(nodes))
for _, node := range nodes {
out = append(out, struct {
ID string
Address string
State string
Incarnation uint64
}{
ID: string(node.ID),
Address: node.Address,
State: node.State.String(),
Incarnation: node.Incarnation,
})
}
return out, ring.Replication(), ring.VirtualNodesPerNode()
}
return nil, 0, 0
}
// DistRingHashSpots returns vnode hashes as hex strings if available (debug).
func (hyperCache *HyperCache[T]) DistRingHashSpots() []string {
if dm, ok := any(hyperCache.backend).(*backend.DistMemory); ok {
if ring := dm.Ring(); ring != nil {
return ring.VNodeHashes()
}
}
return nil
}
// DistHeartbeatMetrics returns distributed heartbeat metrics if supported.
func (hyperCache *HyperCache[T]) DistHeartbeatMetrics() any {
if dm, ok := any(hyperCache.backend).(*backend.DistMemory); ok {
m := dm.Metrics()
return map[string]any{
"heartbeatSuccess": m.HeartbeatSuccess,
"heartbeatFailure": m.HeartbeatFailure,
"nodesRemoved": m.NodesRemoved,
"readPrimaryPromote": m.ReadPrimaryPromote,
}
}
return nil
}