Compare commits

...

1 Commits

Author SHA1 Message Date
overtrue cbe2aa3ae7 fix(ecstore): preserve bucket usage observations across cleanup 2026-12-02 20:20:47 +08:00
2 changed files with 69 additions and 0 deletions
+11
View File
@@ -957,11 +957,22 @@ fn data_usage_contains_bucket(data_usage_info: &DataUsageInfo, bucket: &str) ->
}
fn advance_data_usage_last_update(data_usage_info: &mut DataUsageInfo) {
let previous = data_usage_info.snapshot_identity();
let now = SystemTime::now();
data_usage_info.last_update = Some(match data_usage_info.last_update {
Some(last_update) if last_update >= now => last_update.checked_add(Duration::from_nanos(1)).unwrap_or(now),
_ => now,
});
let current = data_usage_info.snapshot_identity();
// Namespace cleanup advances the root revision without rescanning the surviving buckets.
// Carry only observations bound to the root we read. Their sources and completion times stay intact.
data_usage_info.usage_snapshot_bucket_observations.retain(|_, observation| {
if observation.snapshot_identity != previous {
return false;
}
observation.snapshot_identity = current;
true
});
}
#[cfg(test)]
@@ -119,6 +119,64 @@ fn companion_only_observed_snapshot_does_not_create_a_legacy_quota_baseline() {
);
}
#[tokio::test]
async fn namespace_cleanup_preserves_the_observation_of_an_unrelated_bucket() {
let bucket = "pending-bucket";
let incarnation = uuid::Uuid::from_u128(31);
let original = companion_snapshot(bucket, incarnation);
let observation = &original.usage_snapshot_bucket_observations[bucket];
let store = UsageCasStore::default();
store.state.lock().await.object = Some((serde_json::to_vec(&original).expect("encode usage root"), 1));
remove_bucket_usage_from_object_with_retries(&store, DATA_USAGE_OBJ_NAME_PATH.as_str(), "new-bucket", 0, None, None)
.await
.expect("fence the new bucket's namespace");
let (mut saved, _) = load_data_usage_for_bucket_removal(&store, DATA_USAGE_OBJ_NAME_PATH.as_str())
.await
.expect("read updated usage root")
.expect("usage root remains present");
assert!(saved.last_update > original.last_update);
assert_eq!(saved.scanner_cycle, original.scanner_cycle);
assert_eq!(saved.scanner_epoch, original.scanner_epoch);
assert_eq!(saved.buckets_usage[bucket], original.buckets_usage[bucket]);
let retained = &saved.usage_snapshot_bucket_observations[bucket];
assert_eq!(retained.completed_at, observation.completed_at);
assert_eq!(retained.sources, observation.sources);
assert_eq!(
saved.project_bucket_observations_for_admin(&HashMap::from([(bucket.to_string(), incarnation.to_string())])),
1
);
assert_eq!(saved.buckets_usage[bucket].size, observation.usage.size);
assert!(!saved.buckets_usage.contains_key("new-bucket"));
}
#[test]
fn namespace_cleanup_cannot_make_a_mismatched_observation_current() {
let bucket = "pending-bucket";
let incarnation = uuid::Uuid::from_u128(31);
let mut snapshot = companion_snapshot(bucket, incarnation);
let future = SystemTime::now() + Duration::from_secs(3600);
snapshot.last_update = Some(future);
// With a future root timestamp, the namespace fence advances it by one nanosecond.
let next = future + Duration::from_nanos(1);
snapshot
.usage_snapshot_bucket_observations
.get_mut(bucket)
.expect("test observation")
.snapshot_identity
.last_update = Some(next);
advance_data_usage_last_update(&mut snapshot);
assert_eq!(snapshot.last_update, Some(next));
assert_eq!(
snapshot.project_bucket_observations_for_admin(&HashMap::from([(bucket.to_string(), incarnation.to_string())])),
0
);
assert_eq!(snapshot.buckets_usage[bucket].size, 100);
}
#[test]
fn bucket_removal_discards_companions_even_without_a_canonical_entry() {
let mut snapshot = companion_snapshot("bucket", uuid::Uuid::from_u128(31));