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 <noreply@anthropic.com>

* s3api: shorten the bucket size metrics lock-loss comments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-11-26 08:31:16 -08:00
1 parent d2276f531e
commit 41f1c25d01
3 files changed
+57 -4

No files matched your search

+11 -4
View File
@@ -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) {
+37
View File
@@ -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)
}
}
}
+9
View File
@@ -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.