Skip to content

Commit 77f9218

Browse files
committed
refactor of remove & clean invalid nodes in cache & metrics
1 parent 3582022 commit 77f9218

File tree

4 files changed

+36
-48
lines changed

4 files changed

+36
-48
lines changed

cluster/calcium/node.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ func (c *Calcium) RemoveNode(ctx context.Context, nodename string) error {
9090
},
9191
// then: remove node resource metadata
9292
func(ctx context.Context) error {
93-
if err = c.rmgr.RemoveNode(ctx, nodename); err != nil {
93+
if err := c.rmgr.RemoveNode(ctx, nodename); err != nil {
9494
return err
9595
}
9696
enginefactory.RemoveEngineFromCache(ctx, node.Endpoint, node.Ca, node.Cert, node.Key)
97-
metrics.Client.RemoveInvalidNodes([]string{nodename})
97+
metrics.Client.RemoveInvalidNodes(nodename)
9898
return nil
9999
},
100100
// rollback: do nothing

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ require (
2626
github.com/pkg/errors v0.9.1
2727
github.com/projecteru2/libyavirt v0.0.0-20230921032447-a617cf0c746c
2828
github.com/prometheus/client_golang v1.15.0
29+
github.com/prometheus/client_model v0.3.0
2930
github.com/rs/zerolog v1.29.1
3031
github.com/sanity-io/litter v1.5.5
3132
github.com/stretchr/testify v1.8.2
@@ -105,7 +106,6 @@ require (
105106
github.com/opencontainers/runc v1.1.6 // indirect
106107
github.com/pjbgf/sha1cd v0.3.0 // indirect
107108
github.com/pmezard/go-difflib v1.0.0 // indirect
108-
github.com/prometheus/client_model v0.3.0 // indirect
109109
github.com/prometheus/common v0.42.0 // indirect
110110
github.com/prometheus/procfs v0.9.0 // indirect
111111
github.com/rogpeppe/go-internal v1.10.0 // indirect

metrics/handler.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import (
55
"net/http"
66

77
"github.com/projecteru2/core/cluster"
8+
enginefactory "github.com/projecteru2/core/engine/factory"
89
"github.com/projecteru2/core/log"
910
"github.com/projecteru2/core/types"
11+
"github.com/prometheus/client_golang/prometheus"
12+
promClient "github.com/prometheus/client_model/go"
13+
"golang.org/x/exp/slices"
1014
)
1115

1216
// ResourceMiddleware to make sure update resource correct
@@ -20,7 +24,8 @@ func (m *Metrics) ResourceMiddleware(cluster cluster.Cluster) func(http.Handler)
2024
if err != nil {
2125
logger.Error(ctx, err, "Get all nodes err")
2226
}
23-
activeNodes := make(map[string]*types.Node, 0)
27+
existNodenames := getExistNodenames()
28+
activeNodes := map[string]*types.Node{}
2429
for node := range nodes {
2530
metrics, err := m.rmgr.GetNodeMetrics(ctx, node)
2631
if err != nil {
@@ -30,8 +35,31 @@ func (m *Metrics) ResourceMiddleware(cluster cluster.Cluster) func(http.Handler)
3035
activeNodes[node.Name] = node
3136
m.SendMetrics(ctx, metrics...)
3237
}
33-
m.DeleteInactiveNodesWithCache(ctx, activeNodes)
38+
// refresh nodes
39+
invalidNodenames := []string{}
40+
for _, nodename := range existNodenames {
41+
if node := activeNodes[nodename]; node != nil {
42+
invalidNodenames = append(invalidNodenames, nodename)
43+
enginefactory.RemoveEngineFromCache(ctx, node.Endpoint, node.Ca, node.Cert, node.Key)
44+
}
45+
}
46+
m.RemoveInvalidNodes(invalidNodenames...)
3447
h.ServeHTTP(w, r)
3548
})
3649
}
3750
}
51+
52+
func getExistNodenames() []string {
53+
metrics, _ := prometheus.DefaultGatherer.Gather()
54+
nodenames := []string{}
55+
for _, metric := range metrics {
56+
for _, mf := range metric.GetMetric() {
57+
if i := slices.IndexFunc(mf.Label, func(label *promClient.LabelPair) bool {
58+
return label.GetName() == "nodename"
59+
}); i != -1 {
60+
nodenames = append(nodenames, mf.Label[i].GetValue())
61+
}
62+
}
63+
}
64+
return nodenames
65+
}

metrics/metrics.go

+3-43
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ import (
66
"strconv"
77
"sync"
88

9-
enginefactory "github.com/projecteru2/core/engine/factory"
109
"github.com/projecteru2/core/log"
1110
"github.com/projecteru2/core/resource"
1211
"github.com/projecteru2/core/resource/cobalt"
1312
plugintypes "github.com/projecteru2/core/resource/plugins/types"
1413
"github.com/projecteru2/core/types"
1514
"github.com/projecteru2/core/utils"
16-
io_prometheus_client "github.com/prometheus/client_model/go"
15+
promClient "github.com/prometheus/client_model/go"
1716
"golang.org/x/exp/slices"
1817

1918
statsdlib "github.com/CMGS/statsd"
@@ -88,55 +87,16 @@ func (m *Metrics) SendMetrics(ctx context.Context, metrics ...*plugintypes.Metri
8887
}
8988
}
9089

91-
func (m *Metrics) DeleteInactiveNodesWithCache(ctx context.Context, activeNodesMap map[string]*types.Node) {
92-
metricNodeNameMap := m.getNodeNameMapFromMetrics()
93-
// 计算差集
94-
invalidNodes := make([]string, 0)
95-
for nodeName := range metricNodeNameMap {
96-
if node, exists := activeNodesMap[nodeName]; !exists {
97-
invalidNodes = append(invalidNodes, nodeName)
98-
enginefactory.RemoveEngineFromCache(ctx, node.Endpoint, node.Ca, node.Cert, node.Key)
99-
}
100-
}
101-
m.RemoveInvalidNodes(invalidNodes)
102-
}
103-
104-
func (m *Metrics) getNodeNameMapFromMetrics() map[string]bool {
105-
metrics, _ := prometheus.DefaultGatherer.Gather()
106-
nodeNameMap := make(map[string]bool, 0)
107-
for _, metric := range metrics {
108-
for _, mf := range metric.GetMetric() {
109-
if len(mf.Label) == 0 {
110-
continue
111-
}
112-
for _, label := range mf.Label {
113-
if label.GetName() == "nodename" {
114-
nodeNameMap[label.GetValue()] = true
115-
break
116-
}
117-
}
118-
}
119-
}
120-
return nodeNameMap
121-
}
122-
12390
// RemoveInvalidNodes 清除多余的metric标签值
124-
func (m *Metrics) RemoveInvalidNodes(invalidNodes []string) {
91+
func (m *Metrics) RemoveInvalidNodes(invalidNodes ...string) {
12592
if len(invalidNodes) == 0 {
12693
return
12794
}
12895
for _, collector := range m.Collectors {
129-
if collector == nil {
130-
return
131-
}
13296
metrics, _ := prometheus.DefaultGatherer.Gather()
13397
for _, metric := range metrics {
13498
for _, mf := range metric.GetMetric() {
135-
if len(mf.Label) == 0 {
136-
continue
137-
}
138-
139-
if !slices.ContainsFunc(mf.Label, func(label *io_prometheus_client.LabelPair) bool {
99+
if !slices.ContainsFunc(mf.Label, func(label *promClient.LabelPair) bool {
140100
return label.GetName() == "nodename" && slices.ContainsFunc(invalidNodes, func(nodename string) bool {
141101
return label.GetValue() == nodename
142102
})

0 commit comments

Comments
 (0)