From 41f1c25d0174a4ff2fdf5f0035a52ff3f0fed13a Mon Sep 17 00:00:00 2001 From: Yip Leo Date: Fri, 27 Nov 2026 00:31:16 +0800 Subject: [PATCH] s3api: drop bucket size gauges on an instance that lost the metrics lock (#13019) * s3api: drop bucket size gauges on an instance that lost the metrics lock Only the S3 instance holding the s3.leader lock collects bucket sizes and quota state, once a minute. An instance that loses the lock and keeps running never clears what it set. bucketMetricTTLControl drops a bucket's gauges only after 10 minutes without activity, and every request the instance serves for the bucket counts as activity. So for the buckets clients keep using through it, its bucket_size_bytes, bucket_physical_size_bytes, bucket_object_count, bucket_quota_bytes and bucket_read_only series stay at the values it collected last, next to the new holder's current ones. Summing a gauge over instances counts those buckets twice, and taking the max shows a bucket that has shrunk since at its old size. On every tick without the lock, reset these five gauges. Only the holder's collection sets them, so an instance that never held the lock loses nothing, and one that lost it stops reporting within a minute. In a cluster with seven S3 instances, one kept 10 to 14 buckets at their old values for four days after losing the lock; at one point the max over instances put a bucket 37 TB above its size. TestBucketSizeMetricsTickWithoutLockDropsGauges sets the gauges for two buckets, checks that a tick with the lock leaves them, and that a tick without it removes all five. Without the reset it fails for each gauge. Co-Authored-By: Claude Opus 5.5 * s3api: shorten the bucket size metrics lock-loss comments Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Chris Lu --- weed/s3api/bucket_size_metrics.go | 15 ++++++++--- weed/s3api/bucket_size_metrics_test.go | 37 ++++++++++++++++++++++++++ weed/stats/metrics.go | 9 +++++++ 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/weed/s3api/bucket_size_metrics.go b/weed/s3api/bucket_size_metrics.go index 928a4d85f1..0846923ff4 100644 --- a/weed/s3api/bucket_size_metrics.go +++ b/weed/s3api/bucket_size_metrics.go @@ -80,14 +80,21 @@ func (s3a *S3ApiServer) startBucketSizeMetricsLoop(ctx context.Context) { glog.V(1).Infof("Stopping bucket size metrics collection") return case <-ticker.C: - // Only collect metrics if we hold the lock - if lock.IsLocked() { - s3a.collectAndUpdateBucketSizeMetrics(ctx) - } + s3a.bucketSizeMetricsTick(ctx, lock.IsLocked()) } } } +// bucketSizeMetricsTick drops the bucket gauges while this instance does not +// hold the lock; otherwise it would keep exporting the values it last collected. +func (s3a *S3ApiServer) bucketSizeMetricsTick(ctx context.Context, locked bool) { + if !locked { + stats.ResetBucketSizeMetrics() + return + } + s3a.collectAndUpdateBucketSizeMetrics(ctx) +} + // collectAndUpdateBucketSizeMetrics collects bucket sizes from master topology // and updates Prometheus metrics. Uses the same approach as quota enforcement. func (s3a *S3ApiServer) collectAndUpdateBucketSizeMetrics(ctx context.Context) { diff --git a/weed/s3api/bucket_size_metrics_test.go b/weed/s3api/bucket_size_metrics_test.go index 39eaaebdd1..5a382b1da5 100644 --- a/weed/s3api/bucket_size_metrics_test.go +++ b/weed/s3api/bucket_size_metrics_test.go @@ -1,7 +1,13 @@ package s3api import ( + "context" "testing" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/testutil" + + "github.com/seaweedfs/seaweedfs/weed/stats" ) // TestCollectionInfoLogicalSize verifies logical size excludes un-vacuumed @@ -29,3 +35,34 @@ func TestCollectionInfoLogicalSize(t *testing.T) { }) } } + +func TestBucketSizeMetricsTickWithoutLockDropsGauges(t *testing.T) { + gauges := map[string]*prometheus.GaugeVec{ + "bucket_size_bytes": stats.S3BucketSizeBytesGauge, + "bucket_physical_size_bytes": stats.S3BucketPhysicalSizeBytesGauge, + "bucket_object_count": stats.S3BucketObjectCountGauge, + "bucket_quota_bytes": stats.S3BucketQuotaBytesGauge, + "bucket_read_only": stats.S3BucketReadOnlyGauge, + } + t.Cleanup(stats.ResetBucketSizeMetrics) + for _, bucket := range []string{"tick-a", "tick-b"} { + stats.UpdateBucketSizeMetrics(bucket, 100, 200, 3) + stats.UpdateBucketQuotaMetrics(bucket, 1000, true) + } + // No masters configured: the collection gives up before it touches a gauge. + s3a := &S3ApiServer{option: &S3ApiServerOption{}} + + s3a.bucketSizeMetricsTick(context.Background(), true) + for name, g := range gauges { + if n := testutil.CollectAndCount(g); n != 2 { + t.Fatalf("after a tick with the lock, %s has %d series, want 2", name, n) + } + } + + s3a.bucketSizeMetricsTick(context.Background(), false) + for name, g := range gauges { + if n := testutil.CollectAndCount(g); n != 0 { + t.Errorf("after a tick without the lock, %s has %d series, want 0", name, n) + } + } +} diff --git a/weed/stats/metrics.go b/weed/stats/metrics.go index 264c7cfacc..7db6ed4f78 100644 --- a/weed/stats/metrics.go +++ b/weed/stats/metrics.go @@ -1408,6 +1408,15 @@ func UpdateBucketSizeMetrics(bucket string, logicalSize, physicalSize float64, o RecordBucketActiveTime(bucket) } +// ResetBucketSizeMetrics drops the size, object count, quota and read-only series of every bucket. +func ResetBucketSizeMetrics() { + S3BucketSizeBytesGauge.Reset() + S3BucketPhysicalSizeBytesGauge.Reset() + S3BucketObjectCountGauge.Reset() + S3BucketQuotaBytesGauge.Reset() + S3BucketReadOnlyGauge.Reset() +} + // UpdateBucketQuotaMetrics updates the per-bucket quota gauges. A non-positive // quota removes the quota series so utilization queries like // bucket_size_bytes / bucket_quota_bytes only see enforced quotas.