Files
seaweedfs/weed/s3api/bucket_size_metrics_test.go
T
41f1c25d01 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>
2026-11-26 08:31:16 -08:00

69 lines
2.2 KiB
Go

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
// garbage and never goes negative. Quota enforcement runs on this value so a
// bucket full of tombstones is not flipped read-only while its live data is
// under quota.
func TestCollectionInfoLogicalSize(t *testing.T) {
cases := []struct {
name string
size float64
deleted float64
expected float64
}{
{"no garbage", 1000, 0, 1000},
{"some garbage", 1000, 300, 700},
{"all garbage", 1000, 1000, 0},
{"deleted exceeds size clamps to zero", 300, 1000, 0},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
info := &CollectionInfo{Size: tc.size, DeletedByteCount: tc.deleted}
if got := info.LogicalSize(); got != tc.expected {
t.Errorf("LogicalSize(): got %.0f, want %.0f", got, tc.expected)
}
})
}
}
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)
}
}
}