mirror of
https://github.com/rustfs/rustfs.git
synced 2026-12-02 22:26:21 +00:00
perf(quota): defer published v1 quota settlements (#9867)
Hand off the terminal v1 ledger update after successful object publication when its bytes are already charged. Bound the detached work per instance and keep its retry on the shutdown tracker. Preserve synchronous settlement for shrinking writes and saturation. Co-authored-by: heihutu <heihutu@gmail.com> Co-authored-by: zhi22915 <qiuzgang@gmail.com> Co-authored-by: qunjiazhu <qunjiazhu@gmail.com>
This commit is contained in:
@@ -782,12 +782,36 @@ impl QuotaReservation {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Complete the reservation after object publication. V1 growth is
|
||||
/// already charged, so its terminal record can drain in the background.
|
||||
/// Shrinking writes settle inline so they can signal the recount promptly.
|
||||
pub(crate) async fn commit(mut self) {
|
||||
self.state = ReservationState::Committed;
|
||||
let Some(ledger) = self.ledger.as_ref() else {
|
||||
return;
|
||||
};
|
||||
crate::store::list_objects::observe_scanner_namespace_mutations(ledger.bucket(), 1);
|
||||
if let ReservationLedgerData::Single(data) = ledger
|
||||
&& data.reservation.new_size >= data.reservation.old_size
|
||||
&& let Ok(slot) = Arc::clone(&data.store.ctx.v1_quota_settlement_slots).try_acquire_owned()
|
||||
{
|
||||
let ctx = Arc::clone(&data.store.ctx);
|
||||
drop(ctx.spawn_detached_mutation(async move {
|
||||
let _slot = slot;
|
||||
self.finish_commit().await;
|
||||
if let Some(ledger) = self.ledger.take() {
|
||||
retry_settlement(&ledger, true, self.metadata_guard.as_ref()).await;
|
||||
}
|
||||
}));
|
||||
return;
|
||||
}
|
||||
self.finish_commit().await;
|
||||
}
|
||||
|
||||
async fn finish_commit(&mut self) {
|
||||
let Some(ledger) = self.ledger.as_ref() else {
|
||||
return;
|
||||
};
|
||||
match settle(ledger, true, self.metadata_guard.as_ref()).await {
|
||||
Ok(()) => self.ledger = None,
|
||||
Err(err) => log_deferred_settlement(ledger, "commit_deferred", &err),
|
||||
@@ -829,13 +853,17 @@ impl Drop for QuotaReservation {
|
||||
};
|
||||
runtime.spawn(async move {
|
||||
let _shard_operation_guard = shard_operation_guard;
|
||||
if let Err(err) = settle(&ledger, committed, metadata_guard.as_ref()).await {
|
||||
log_deferred_settlement(&ledger, "background_retry_failed", &err);
|
||||
}
|
||||
retry_settlement(&ledger, committed, metadata_guard.as_ref()).await;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async fn retry_settlement(ledger: &ReservationLedgerData, committed: bool, metadata_guard: Option<&Arc<NamespaceLockGuard>>) {
|
||||
if let Err(err) = settle(ledger, committed, metadata_guard).await {
|
||||
log_deferred_settlement(ledger, "background_retry_failed", &err);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn begin(
|
||||
ctx: &crate::runtime::instance::InstanceContext,
|
||||
bucket: &str,
|
||||
@@ -2992,6 +3020,79 @@ mod tests {
|
||||
.expect("owner guard");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn v1_published_growth_settles_in_a_bounded_tracked_task() {
|
||||
let (_directories, job) = reconciliation_fixture(100).await;
|
||||
job.run().await.expect("bootstrap");
|
||||
let object = ledger_object(&job.bucket);
|
||||
let slots = &job.store.ctx.v1_quota_settlement_slots;
|
||||
let capacity = slots.available_permits();
|
||||
let mut reservation = quota_reservation(&job, 0, 1).await.expect("durable reservation");
|
||||
reservation.mark_commit_started().await.expect("durable marker");
|
||||
let Some(ReservationLedgerData::Single(data)) = reservation.ledger.as_ref() else {
|
||||
panic!("expected a v1 reservation");
|
||||
};
|
||||
let operation_id = data.operation_id;
|
||||
let operation = job
|
||||
.store
|
||||
.new_ns_lock(RUSTFS_META_BUCKET, &operation_lock_object(&object, operation_id))
|
||||
.await
|
||||
.expect("operation lock");
|
||||
let guard = lock_v1_ledger(&job.store, &object, "quota_ledger_test_hold", get_lock_acquire_timeout())
|
||||
.await
|
||||
.expect("block settlement after the marker");
|
||||
tokio::time::timeout(Duration::from_secs(1), reservation.commit())
|
||||
.await
|
||||
.expect("publication must not wait for the blocked v1 settlement");
|
||||
assert_eq!(slots.available_permits(), capacity - 1);
|
||||
assert_eq!(job.store.ctx.detached_mutation_count(), 1);
|
||||
assert!(operation.get_write_lock_quiet(Duration::from_millis(10)).await.is_err());
|
||||
let pending = read_test_ledger(&job).await;
|
||||
assert_eq!(pending.accounted_usage, 11);
|
||||
assert!(pending.reservations[&operation_id].commit_started);
|
||||
|
||||
fail_next_quota_ledger_save_for_test();
|
||||
drop(guard);
|
||||
assert!(job.store.ctx.wait_for_detached_mutations(Duration::from_secs(10)).await);
|
||||
let settled = read_test_ledger(&job).await;
|
||||
assert_eq!(settled.accounted_usage, 11);
|
||||
assert!(settled.reservations.is_empty(), "the tracked task owns the retry");
|
||||
assert_eq!(slots.available_permits(), capacity);
|
||||
|
||||
let _full = Arc::clone(slots)
|
||||
.try_acquire_many_owned(capacity.try_into().expect("slot count fits"))
|
||||
.expect("fill the background budget");
|
||||
let mut reservation = quota_reservation(&job, 0, 0).await.expect("second reservation");
|
||||
reservation.mark_commit_started().await.expect("second marker");
|
||||
let guard = lock_v1_ledger(&job.store, &object, "quota_ledger_test_hold", get_lock_acquire_timeout())
|
||||
.await
|
||||
.expect("block the inline fallback");
|
||||
let mut fallback = Box::pin(reservation.commit());
|
||||
assert!(futures::poll!(fallback.as_mut()).is_pending());
|
||||
assert_eq!(job.store.ctx.detached_mutation_count(), 0);
|
||||
drop(guard);
|
||||
tokio::time::timeout(Duration::from_secs(10), fallback)
|
||||
.await
|
||||
.expect("inline settlement completes");
|
||||
assert!(read_test_ledger(&job).await.reservations.is_empty());
|
||||
drop(_full);
|
||||
|
||||
let mut reservation = quota_reservation(&job, 1, 0).await.expect("shrinking reservation");
|
||||
reservation.mark_commit_started().await.expect("shrinking marker");
|
||||
let guard = lock_v1_ledger(&job.store, &object, "quota_ledger_test_hold", get_lock_acquire_timeout())
|
||||
.await
|
||||
.expect("block the shrinking settlement");
|
||||
let mut shrink = Box::pin(reservation.commit());
|
||||
assert!(futures::poll!(shrink.as_mut()).is_pending());
|
||||
assert_eq!(slots.available_permits(), capacity);
|
||||
drop(guard);
|
||||
tokio::time::timeout(Duration::from_secs(10), shrink)
|
||||
.await
|
||||
.expect("shrinking settlement completes inline");
|
||||
assert!(read_test_ledger(&job).await.reconcile_required);
|
||||
}
|
||||
|
||||
async fn queue_v1_test_admissions(
|
||||
job: &ReconcileJob,
|
||||
names: &[&str],
|
||||
@@ -3523,6 +3624,7 @@ mod tests {
|
||||
"shrink" | "grow" => {
|
||||
reservation.mark_commit_started().await.expect("commit marker");
|
||||
reservation.commit().await;
|
||||
assert!(job.store.ctx.wait_for_detached_mutations(Duration::from_secs(10)).await);
|
||||
None
|
||||
}
|
||||
"reap" => {
|
||||
|
||||
@@ -423,6 +423,8 @@ pub struct InstanceContext {
|
||||
/// caller (for example, an S3 client disconnect) cannot stop them between
|
||||
/// their on-disk steps. Shutdown waits for them after the HTTP drain.
|
||||
detached_mutations: TaskTracker,
|
||||
/// Limits v1 settlements that may outlive a successful object response.
|
||||
pub(crate) v1_quota_settlement_slots: Arc<Semaphore>,
|
||||
}
|
||||
|
||||
impl InstanceContext {
|
||||
@@ -562,6 +564,7 @@ impl InstanceContext {
|
||||
transition_transaction_recovery_stores: std::sync::Mutex::new(HashSet::new()),
|
||||
tier_delete_journal_recovery_wakeup: tokio::sync::Notify::new(),
|
||||
detached_mutations: TaskTracker::new(),
|
||||
v1_quota_settlement_slots: Arc::new(Semaphore::new(64)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -585,6 +588,14 @@ impl InstanceContext {
|
||||
/// the mutation, together with the locks and guards it owns, never stops
|
||||
/// between two of its on-disk steps because its caller went away.
|
||||
pub(crate) async fn run_detached_mutation<F>(&self, mutation: F) -> Result<F::Output, tokio::task::JoinError>
|
||||
where
|
||||
F: std::future::Future + Send + 'static,
|
||||
F::Output: Send + 'static,
|
||||
{
|
||||
self.spawn_detached_mutation(mutation).await
|
||||
}
|
||||
|
||||
pub(crate) fn spawn_detached_mutation<F>(&self, mutation: F) -> tokio::task::JoinHandle<F::Output>
|
||||
where
|
||||
F: std::future::Future + Send + 'static,
|
||||
F::Output: Send + 'static,
|
||||
@@ -599,7 +610,7 @@ impl InstanceContext {
|
||||
let context = rustfs_io_metrics::s3_http_metrics::current_s3_http_operation_context();
|
||||
#[cfg(not(feature = "diagnostic-read-version-caller"))]
|
||||
let mutation = rustfs_io_metrics::s3_http_metrics::with_s3_http_operation_context(context, mutation);
|
||||
self.detached_mutations.spawn(mutation.in_current_span()).await
|
||||
self.detached_mutations.spawn(mutation.in_current_span())
|
||||
}
|
||||
|
||||
/// Wait up to `timeout` for every detached mutation to finish. Returns
|
||||
|
||||
Reference in New Issue
Block a user