perf(quota): Skip expensive usage checks when no quota configured (#1749)

Co-authored-by: houseme <housemecn@gmail.com>
This commit is contained in:
thorntonmc
2026-02-08 09:37:53 -05:00
committed by GitHub
parent a574285ab2
commit 927f3a57d7
5 changed files with 52 additions and 17 deletions
+2 -2
View File
@@ -422,7 +422,7 @@ impl Operation for CheckBucketQuotaHandler {
};
let result = quota_checker
.check_quota(&bucket, operation, request.operation_size)
.check_quota_with_usage_reporting(&bucket, operation, request.operation_size, true)
.await
.map_err(|e| s3_error!(InternalError, "Failed to check quota: {}", e))?;
@@ -431,7 +431,7 @@ impl Operation for CheckBucketQuotaHandler {
operation_type: request.operation_type,
operation_size: request.operation_size,
allowed: result.allowed,
current_usage: result.current_usage,
current_usage: result.current_usage.unwrap_or(0),
quota_limit: result.quota_limit,
remaining_quota: result.remaining,
};
+19 -8
View File
@@ -667,6 +667,7 @@ impl S3 for FS {
.map_err(ApiError::from)?;
// check quota after completing multipart upload
let mut quota_usage_calculated = false;
if let Some(metadata_sys) = rustfs_ecstore::bucket::metadata_sys::GLOBAL_BucketMetadataSys.get() {
let quota_checker = QuotaChecker::new(metadata_sys.clone());
@@ -682,15 +683,13 @@ impl S3 for FS {
S3ErrorCode::InvalidRequest,
format!(
"Bucket quota exceeded. Current usage: {} bytes, limit: {} bytes",
check_result.current_usage,
check_result.current_usage.unwrap_or(0),
check_result.quota_limit.unwrap_or(0)
),
));
}
// Update quota tracking after successful multipart upload
if rustfs_ecstore::bucket::metadata_sys::GLOBAL_BucketMetadataSys.get().is_some() {
rustfs_ecstore::data_usage::increment_bucket_usage_memory(&bucket, obj_info.size as u64).await;
}
// Track if usage was actually calculated (not just returned as None/0)
quota_usage_calculated = check_result.current_usage.is_some();
}
Err(e) => {
warn!("Quota check failed for bucket {}: {}, allowing operation", bucket, e);
@@ -698,6 +697,13 @@ impl S3 for FS {
}
}
// Only increment usage cache when quota checking actually calculated real usage.
// This prevents cache corruption: when quotas are disabled, the cache remains unset.
// When quotas are later enabled, the cache will miss and recalculate from backend.
if quota_usage_calculated {
rustfs_ecstore::data_usage::increment_bucket_usage_memory(&bucket, obj_info.size as u64).await;
}
// Invalidate cache for the completed multipart object
let manager = get_concurrency_manager();
let mpu_bucket = bucket.clone();
@@ -1036,6 +1042,7 @@ impl S3 for FS {
}
// check quota for copy operation
let mut quota_usage_calculated = false;
if let Some(metadata_sys) = rustfs_ecstore::bucket::metadata_sys::GLOBAL_BucketMetadataSys.get() {
let quota_checker = QuotaChecker::new(metadata_sys.clone());
@@ -1049,11 +1056,13 @@ impl S3 for FS {
S3ErrorCode::InvalidRequest,
format!(
"Bucket quota exceeded. Current usage: {} bytes, limit: {} bytes",
check_result.current_usage,
check_result.current_usage.unwrap_or(0),
check_result.quota_limit.unwrap_or(0)
),
));
}
// Track if usage was actually calculated (not just returned as None/0)
quota_usage_calculated = check_result.current_usage.is_some();
}
Err(e) => {
warn!("Quota check failed for bucket {}: {}, allowing operation", bucket, e);
@@ -1066,8 +1075,10 @@ impl S3 for FS {
.await
.map_err(ApiError::from)?;
// Update quota tracking after successful copy
if rustfs_ecstore::bucket::metadata_sys::GLOBAL_BucketMetadataSys.get().is_some() {
// Only increment usage cache when quota checking actually calculated real usage.
// This prevents cache corruption: when quotas are disabled, the cache remains unset.
// When quotas are later enabled, the cache will miss and recalculate from backend.
if quota_usage_calculated {
rustfs_ecstore::data_usage::increment_bucket_usage_memory(&bucket, oi.size as u64).await;
}
+10 -3
View File
@@ -133,6 +133,7 @@ impl Objects {
}
// check quota for put operation
let mut quota_usage_calculated = false;
if let Some(size) = content_length
&& let Some(metadata_sys) = rustfs_ecstore::bucket::metadata_sys::GLOBAL_BucketMetadataSys.get()
{
@@ -148,11 +149,13 @@ impl Objects {
S3ErrorCode::InvalidRequest,
format!(
"Bucket quota exceeded. Current usage: {} bytes, limit: {} bytes",
check_result.current_usage,
check_result.current_usage.unwrap_or(0),
check_result.quota_limit.unwrap_or(0)
),
));
}
// Track if usage was actually calculated (not just returned as None/0)
quota_usage_calculated = check_result.current_usage.is_some();
}
Err(e) => {
warn!("Quota check failed for bucket {}: {}, allowing operation", bucket, e);
@@ -332,8 +335,12 @@ impl Objects {
.await
.map_err(ApiError::from)?;
// Fast in-memory update for immediate quota consistency
rustfs_ecstore::data_usage::increment_bucket_usage_memory(&bucket, obj_info.size as u64).await;
// Only increment usage cache when quota checking actually calculated real usage.
// This prevents cache corruption: when quotas are disabled, the cache remains unset.
// When quotas are later enabled, the cache will miss and recalculate from backend.
if quota_usage_calculated {
rustfs_ecstore::data_usage::increment_bucket_usage_memory(&bucket, obj_info.size as u64).await;
}
// Invalidate cache for the written object to prevent stale data
let manager = get_concurrency_manager();