Skip to content

Commit 9efa888

Browse files
author
WyRainBow
committed
Fix counter initialization errors and mesh change detection logic
1 parent 5e17b0a commit 9efa888

2 files changed

Lines changed: 18 additions & 53 deletions

File tree

pkg/console/counter/component.go

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"math"
2323

2424
"github.com/apache/dubbo-admin/pkg/core/events"
25+
"github.com/apache/dubbo-admin/pkg/core/logger"
2526
meshresource "github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1"
2627
resmodel "github.com/apache/dubbo-admin/pkg/core/resource/model"
2728
"github.com/apache/dubbo-admin/pkg/core/runtime"
@@ -76,7 +77,9 @@ func (c *managerComponent) Start(rt runtime.Runtime, _ <-chan struct{}) error {
7677
return fmt.Errorf("component %s does not implement store.Router", runtime.ResourceStore)
7778
}
7879

79-
c.initializeCountsFromStore(storeRouter)
80+
if err := c.initializeCountsFromStore(storeRouter); err != nil {
81+
logger.Warnf("Failed to initialize counter manager from store: %v", err)
82+
}
8083

8184
component, err := rt.GetComponent(runtime.EventBus)
8285
if err != nil {
@@ -112,9 +115,7 @@ func (c *managerComponent) initializeResourceCount(storeRouter store.Router, kin
112115
}
113116

114117
allResources := resourceStore.List()
115-
116-
meshCounts := make(map[string]int64)
117-
meshDistributions := make(map[string]map[string]int64)
118+
cm := c.manager.(*counterManager)
118119

119120
for _, obj := range allResources {
120121
resource, ok := obj.(resmodel.Resource)
@@ -127,66 +128,29 @@ func (c *managerComponent) initializeResourceCount(storeRouter store.Router, kin
127128
mesh = "default"
128129
}
129130

130-
meshCounts[mesh]++
131+
if counter, exists := cm.simpleCounters[kind]; exists {
132+
counter.Increment(mesh)
133+
}
131134

132135
if kind == meshresource.InstanceKind {
133136
instance, ok := resource.(*meshresource.InstanceResource)
134137
if ok && instance.Spec != nil {
135138
protocol := instance.Spec.GetProtocol()
136139
if protocol != "" {
137-
if meshDistributions[mesh] == nil {
138-
meshDistributions[mesh] = make(map[string]int64)
140+
if cfg := cm.getDistributionConfig(kind, ProtocolCounter); cfg != nil {
141+
cfg.counter.Increment(mesh, protocol)
139142
}
140-
meshDistributions[mesh]["protocol:"+protocol]++
141143
}
142144

143145
releaseVersion := instance.Spec.GetReleaseVersion()
144146
if releaseVersion != "" {
145-
if meshDistributions[mesh] == nil {
146-
meshDistributions[mesh] = make(map[string]int64)
147+
if cfg := cm.getDistributionConfig(kind, ReleaseCounter); cfg != nil {
148+
cfg.counter.Increment(mesh, releaseVersion)
147149
}
148-
meshDistributions[mesh]["release:"+releaseVersion]++
149150
}
150151

151-
if meshDistributions[mesh] == nil {
152-
meshDistributions[mesh] = make(map[string]int64)
153-
}
154-
meshDistributions[mesh]["discovery:"+mesh]++
155-
}
156-
}
157-
}
158-
159-
cm := c.manager.(*counterManager)
160-
161-
if counter, exists := cm.simpleCounters[kind]; exists {
162-
for mesh, count := range meshCounts {
163-
for i := int64(0); i < count; i++ {
164-
counter.Increment(mesh)
165-
}
166-
}
167-
}
168-
169-
if kind == meshresource.InstanceKind {
170-
for mesh, distributions := range meshDistributions {
171-
for key, count := range distributions {
172-
if len(key) > 9 && key[:9] == "protocol:" {
173-
if cfg := cm.getDistributionConfig(kind, ProtocolCounter); cfg != nil {
174-
for i := int64(0); i < count; i++ {
175-
cfg.counter.Increment(mesh, key[9:])
176-
}
177-
}
178-
} else if len(key) > 8 && key[:8] == "release:" {
179-
if cfg := cm.getDistributionConfig(kind, ReleaseCounter); cfg != nil {
180-
for i := int64(0); i < count; i++ {
181-
cfg.counter.Increment(mesh, key[8:])
182-
}
183-
}
184-
} else if len(key) > 11 && key[:11] == "discovery:" {
185-
if cfg := cm.getDistributionConfig(kind, DiscoveryCounter); cfg != nil {
186-
for i := int64(0); i < count; i++ {
187-
cfg.counter.Increment(mesh, key[11:])
188-
}
189-
}
152+
if cfg := cm.getDistributionConfig(kind, DiscoveryCounter); cfg != nil {
153+
cfg.counter.Increment(mesh, mesh)
190154
}
191155
}
192156
}
@@ -198,3 +162,4 @@ func (c *managerComponent) initializeResourceCount(storeRouter store.Router, kin
198162
func (c *managerComponent) CounterManager() CounterManager {
199163
return c.manager
200164
}
165+

pkg/console/counter/manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,11 @@ func (cfg *distributionCounterConfig) extractFrom(res resmodel.Resource) string
285285
func (cfg *distributionCounterConfig) update(oldObj, newObj resmodel.Resource) {
286286
oldKey := normalizeDistributionKey(cfg.extractFrom(oldObj))
287287
newKey := normalizeDistributionKey(cfg.extractFrom(newObj))
288-
if oldKey == newKey {
289-
return
290-
}
291288
oldMesh := extractMeshName(oldObj)
292289
newMesh := extractMeshName(newObj)
290+
if oldKey == newKey && oldMesh == newMesh {
291+
return
292+
}
293293
if oldObj != nil {
294294
cfg.counter.Decrement(oldMesh, oldKey)
295295
}

0 commit comments

Comments
 (0)