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.