From bf1eb3982969c7bb58b862362cc415ee623afeba Mon Sep 17 00:00:00 2001 From: xzy Date: Wed, 11 Jun 2025 17:19:27 +0800 Subject: [PATCH] Fix the incorrect deductions caused by incomplete objectstorage-traffic data (#5647) --- controllers/pkg/go.mod | 4 +-- .../pkg/objectstorage/objectstorage.go | 28 +++++++++++++++++++ .../controllers/monitor_controller.go | 11 ++++++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/controllers/pkg/go.mod b/controllers/pkg/go.mod index 62e8adaeb..a125d6ae0 100644 --- a/controllers/pkg/go.mod +++ b/controllers/pkg/go.mod @@ -39,14 +39,12 @@ require ( github.com/wechatpay-apiv3/wechatpay-go v0.2.17 go.mongodb.org/mongo-driver v1.12.1 go.uber.org/zap v1.26.0 - golang.org/x/time v0.5.0 gopkg.in/natefinch/lumberjack.v2 v2.2.1 gopkg.in/yaml.v3 v3.0.1 gorm.io/driver/postgres v1.5.4 gorm.io/gorm v1.25.5 k8s.io/api v0.29.0 k8s.io/apimachinery v0.29.0 - k8s.io/client-go v12.0.0+incompatible sigs.k8s.io/controller-runtime v0.17.2 ) @@ -106,6 +104,7 @@ require ( golang.org/x/sys v0.20.0 // indirect golang.org/x/term v0.20.0 // indirect golang.org/x/text v0.15.0 // indirect + golang.org/x/time v0.5.0 // indirect google.golang.org/appengine v1.6.8 // indirect google.golang.org/protobuf v1.34.1 // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect @@ -113,6 +112,7 @@ require ( gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + k8s.io/client-go v12.0.0+incompatible // indirect k8s.io/klog/v2 v2.110.1 // indirect k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect k8s.io/utils v0.0.0-20231127182322-b307cd553661 // indirect diff --git a/controllers/pkg/objectstorage/objectstorage.go b/controllers/pkg/objectstorage/objectstorage.go index ccc2985e3..1ccfbf941 100644 --- a/controllers/pkg/objectstorage/objectstorage.go +++ b/controllers/pkg/objectstorage/objectstorage.go @@ -247,6 +247,11 @@ func QueryUserUsageAndTraffic(client *MetricsClient) (Metrics, error) { if err != nil { return nil, fmt.Errorf("failed to get bucket traffic metrics: %w", err) } + + // Initialize a map to count the number of metrics per bucket for sent traffic + bucketSentCounts := make(map[string]int) + // Seed the random number generator + for _, bucketMetric := range bucketMetrics { if !isUsageAndTrafficBytesTargetMetric(bucketMetric.Name) { continue @@ -279,7 +284,14 @@ func QueryUserUsageAndTraffic(client *MetricsClient) (Metrics, error) { metricData.Usage[bucket] += intValue } if bucketMetric.Name == "minio_bucket_traffic_sent_bytes" { + // Skip this data point to simulate loss + // if strings.HasPrefix(bucket, "961a5c2b") && rand.Intn(10) == 0 { + // fmt.Printf("Simulated data loss for bucket %s, value %d\n", bucket, intValue) + // continue + // } metricData.Sent[bucket] += intValue + // Increment the counter for this bucket + bucketSentCounts[bucket]++ } if bucketMetric.Name == "minio_bucket_traffic_received_bytes" { metricData.Received[bucket] += intValue @@ -289,6 +301,22 @@ func QueryUserUsageAndTraffic(client *MetricsClient) (Metrics, error) { } } + // After processing all metrics, check the counts for sent traffic + for user, metricData := range obMetrics { + for bucket, count := range bucketSentCounts { + if _, ok := metricData.Sent[bucket]; ok { + if count < 4 { + // Zero out if less than 4 metrics were added + metricData.Sent[bucket] = -1 + } + // if strings.HasPrefix(bucket, "961a5c2b") { + // fmt.Println("bucket:", bucket, ", sent:", sent) + // } + } + } + obMetrics[user] = metricData + } + return obMetrics, err } diff --git a/controllers/resources/controllers/monitor_controller.go b/controllers/resources/controllers/monitor_controller.go index 1682d5208..9f6c915b3 100644 --- a/controllers/resources/controllers/monitor_controller.go +++ b/controllers/resources/controllers/monitor_controller.go @@ -602,9 +602,14 @@ func (r *MonitorReconciler) monitorObjectStorageTraffic() error { sent := int64(0) if r.lastObjectMetrics != nil && r.lastObjectMetrics[user].Sent != nil { if _, ok := r.lastObjectMetrics[user].Sent[bucket]; ok { - ss := m - r.lastObjectMetrics[user].Sent[bucket] - if ss > 0 { - sent = ss + if m == -1 { + r.currentObjectMetrics[user].Sent[bucket] = r.lastObjectMetrics[user].Sent[bucket] + m = r.lastObjectMetrics[user].Sent[bucket] + } else { + ss := m - r.lastObjectMetrics[user].Sent[bucket] + if ss > 0 { + sent = ss + } } } }