Track health check ListObjects as Class A operations

This commit is contained in:
DullJZ
2025-11-06 01:41:51 +08:00
parent c7e984aac2
commit 1402d40c05
6 changed files with 99 additions and 2 deletions
+52
View File
@@ -1,6 +1,8 @@
package bucket
import (
"log"
"github.com/DullJZ/s3-balance/internal/health"
"github.com/DullJZ/s3-balance/internal/metrics"
)
@@ -64,3 +66,53 @@ func (r *MetricsReporter) ReportStats(stats *health.Stats) {
r.metrics.SetBucketUsage(stats.TargetID, stats.UsedSize, bucket.Config.MaxSizeBytes)
}
}
// RecordOperation 实现 health.OperationRecorder 接口
func (r *MetricsReporter) RecordOperation(targetID string, category health.OperationCategory) {
r.manager.mu.RLock()
bucket, exists := r.manager.buckets[targetID]
storage := r.manager.storage
r.manager.mu.RUnlock()
if !exists {
return
}
// 转换 health.OperationCategory 到 bucket.OperationCategory
var bucketCategory OperationCategory
switch category {
case health.OperationTypeA:
bucketCategory = OperationTypeA
case health.OperationTypeB:
bucketCategory = OperationTypeB
default:
return
}
// 更新 Prometheus 指标
if r.metrics != nil {
r.metrics.RecordBackendOperation(targetID, string(bucketCategory))
}
// 持久化操作计数到数据库并更新内存计数
var disabled bool
if storage != nil {
// 先持久化到数据库
newCount, err := storage.IncrementBucketOperation(targetID, string(bucketCategory))
if err != nil {
log.Printf("Failed to persist health check operation count for bucket %s: %v", targetID, err)
// 如果数据库更新失败,仍然更新内存计数
disabled = bucket.RecordOperation(bucketCategory)
} else {
// 使用数据库返回的最新计数更新内存
disabled = bucket.SetOperationCount(bucketCategory, newCount)
}
} else {
// 没有 storage service,只更新内存
disabled = bucket.RecordOperation(bucketCategory)
}
if disabled {
log.Printf("Bucket %s disabled after exceeding %s-type operation limit (detected by health check)", targetID, bucketCategory)
}
}