diff --git a/crates/ecstore/src/disk/os.rs b/crates/ecstore/src/disk/os.rs index b96ad8eee2..78e5da64f0 100644 --- a/crates/ecstore/src/disk/os.rs +++ b/crates/ecstore/src/disk/os.rs @@ -1008,6 +1008,7 @@ impl OpenedDstDirFsyncGroup { struct DstDirFsyncWaiter { owner: DstDirFsyncOwner, + enqueued_at: Option, result_tx: oneshot::Sender, } @@ -1079,6 +1080,8 @@ impl DstDirFsyncGroupCommit { let (result_tx, result_rx) = oneshot::channel(); let mut registry = self.inner.lock(); if registry.total_waiters >= MAX_DST_DIR_FSYNC_WAITERS { + drop(registry); + rustfs_io_metrics::record_put_rename_dst_dir_fsync_group_enqueue("waiter_limit"); return Err(io::Error::new( io::ErrorKind::WouldBlock, "dst dir fsync group commit waiter limit reached", @@ -1088,6 +1091,8 @@ impl DstDirFsyncGroupCommit { group.clone() } else { if registry.groups.len() >= MAX_DST_DIR_FSYNC_GROUPS { + drop(registry); + rustfs_io_metrics::record_put_rename_dst_dir_fsync_group_enqueue("group_limit"); return Err(io::Error::new( io::ErrorKind::WouldBlock, "dst dir fsync group commit active group limit reached", @@ -1107,6 +1112,7 @@ impl DstDirFsyncGroupCommit { let mut group_state = group.inner.lock(); group_state.pending.push_back(DstDirFsyncWaiter { result_tx, + enqueued_at: rustfs_io_metrics::put_stage_timer(), owner: DstDirFsyncOwner { _namespace: namespace_owner, _resource: super::io_admission::current_resource_unit(), @@ -1120,6 +1126,7 @@ impl DstDirFsyncGroupCommit { registry.total_waiters += 1; drop(group_state); drop(registry); + rustfs_io_metrics::record_put_rename_dst_dir_fsync_group_enqueue("accepted"); #[cfg(test)] fsync_dir_recorder::run_after_group_enqueue(&group.dir); @@ -1184,7 +1191,9 @@ async fn fsync_open_dst_dir_group(group: &DstDirFsyncGroup, namespace_owners: Ve } fsync_dir_recorder::record(&dir); } - dir_file.sync_all() + let result = dir_file.sync_all(); + rustfs_io_metrics::record_put_rename_dst_dir_fsync_group_physical_sync(result.is_ok()); + result }) .await .map_err(|err| io::Error::other(format!("blocking dst dir group fsync failed: {err}")))? @@ -1245,9 +1254,10 @@ fn run_dst_dir_fsync_group_worker( #[cfg(test)] fsync_dir_recorder::run_before_group_batch(&group.dir); tokio::task::yield_now().await; - let mut batch: Vec = { + let (mut batch, drained_at) = { let mut group_state = group.inner.lock(); - group_state.pending.drain(..).collect() + let drained_at = rustfs_io_metrics::put_stage_timer(); + (group_state.pending.drain(..).collect::>(), drained_at) }; if batch.is_empty() { let mut group_state = group.inner.lock(); @@ -1258,6 +1268,16 @@ fn run_dst_dir_fsync_group_worker( return; } worker_guard.in_flight = batch.len(); + rustfs_io_metrics::record_put_rename_dst_dir_fsync_group_batch(batch.len()); + if let Some(drained_at) = drained_at { + for waiter in &batch { + if let Some(enqueued_at) = waiter.enqueued_at { + rustfs_io_metrics::record_put_rename_dst_dir_fsync_group_queue_wait( + drained_at.saturating_duration_since(enqueued_at), + ); + } + } + } #[cfg(test)] fsync_dir_recorder::record_grouped(&group.dir, batch.len()); diff --git a/crates/io-metrics/src/lib.rs b/crates/io-metrics/src/lib.rs index de5bf46ea3..2c82b38482 100644 --- a/crates/io-metrics/src/lib.rs +++ b/crates/io-metrics/src/lib.rs @@ -2910,6 +2910,41 @@ fn put_stage_count_value(value: usize) -> f64 { } } +/// Record an accepted destination-directory waiter or the capacity limit it reached. +#[inline(always)] +pub fn record_put_rename_dst_dir_fsync_group_enqueue(outcome: &'static str) { + if put_stage_metrics_enabled() { + counter!("rustfs_s3_put_object_rename_dst_dir_fsync_group_enqueue_total", "outcome" => outcome).increment(1); + } +} + +/// Record one nonempty drained batch before waiting for its physical sync. +#[inline(always)] +pub fn record_put_rename_dst_dir_fsync_group_batch(waiters: usize) { + if put_stage_metrics_enabled() { + let kind = if waiters == 1 { "single" } else { "multi" }; + histogram!("rustfs_s3_put_object_rename_dst_dir_fsync_group_batch_waiters", "kind" => kind) + .record(put_stage_count_value(waiters)); + } +} + +/// Record one waiter's time from enqueue until its batch was drained. +#[inline(always)] +pub fn record_put_rename_dst_dir_fsync_group_queue_wait(wait: std::time::Duration) { + if put_stage_metrics_enabled() { + histogram!("rustfs_s3_put_object_rename_dst_dir_fsync_group_queue_wait_ms").record(wait.as_secs_f64() * 1000.0); + } +} + +/// Record one completed native directory sync, including a returned error. +#[inline(always)] +pub fn record_put_rename_dst_dir_fsync_group_physical_sync(succeeded: bool) { + if put_stage_metrics_enabled() { + let outcome = if succeeded { "success" } else { "error" }; + counter!("rustfs_s3_put_object_rename_dst_dir_fsync_group_physical_sync_total", "outcome" => outcome).increment(1); + } +} + #[inline(always)] pub fn record_put_rename_fdatasync_batch(mode: &'static str, files: usize) { if !put_stage_metrics_enabled() { @@ -4975,6 +5010,68 @@ mod tests { assert_eq!(durations, HashSet::from([duration_stage.to_string(), pending_stage.to_string()])); } + #[test] + fn put_rename_dst_dir_fsync_group_metrics_are_static_and_gated() { + let _guard = METRICS_FLAG_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let recorder = DebuggingRecorder::new(); + let snapshotter = recorder.snapshotter(); + let wait = std::time::Duration::from_millis(2); + metrics::with_local_recorder(&recorder, || { + set_put_stage_metrics_enabled(false); + record_put_rename_dst_dir_fsync_group_enqueue("accepted"); + record_put_rename_dst_dir_fsync_group_batch(3); + record_put_rename_dst_dir_fsync_group_queue_wait(wait); + record_put_rename_dst_dir_fsync_group_physical_sync(true); + + assert!(snapshotter.snapshot().into_vec().is_empty()); + set_put_stage_metrics_enabled(true); + record_put_rename_dst_dir_fsync_group_enqueue("accepted"); + record_put_rename_dst_dir_fsync_group_enqueue("group_limit"); + record_put_rename_dst_dir_fsync_group_enqueue("group_limit"); + record_put_rename_dst_dir_fsync_group_enqueue("waiter_limit"); + record_put_rename_dst_dir_fsync_group_batch(1); + record_put_rename_dst_dir_fsync_group_batch(2); + record_put_rename_dst_dir_fsync_group_queue_wait(wait); + record_put_rename_dst_dir_fsync_group_physical_sync(true); + record_put_rename_dst_dir_fsync_group_physical_sync(true); + record_put_rename_dst_dir_fsync_group_physical_sync(false); + set_put_stage_metrics_enabled(false); + }); + + let rows = snapshotter.snapshot().into_vec(); + assert_eq!(rows.len(), 8); + let enqueue = "rustfs_s3_put_object_rename_dst_dir_fsync_group_enqueue_total"; + let batch = "rustfs_s3_put_object_rename_dst_dir_fsync_group_batch_waiters"; + let queue_wait = "rustfs_s3_put_object_rename_dst_dir_fsync_group_queue_wait_ms"; + let physical = "rustfs_s3_put_object_rename_dst_dir_fsync_group_physical_sync_total"; + let value = |name: &str, label: Option<(&str, &str)>| { + rows.iter() + .find(|(key, _, _, _)| { + key.key().name() == name && key.key().labels().map(|item| (item.key(), item.value())).eq(label) + }) + .map(|(_, _, _, value)| value) + .expect("metric with the expected label set") + }; + + assert!(matches!(value(enqueue, Some(("outcome", "accepted"))), DebugValue::Counter(1))); + assert!(matches!(value(enqueue, Some(("outcome", "group_limit"))), DebugValue::Counter(2))); + assert!(matches!(value(enqueue, Some(("outcome", "waiter_limit"))), DebugValue::Counter(1))); + assert!(matches!(value(physical, Some(("outcome", "success"))), DebugValue::Counter(2))); + assert!(matches!(value(physical, Some(("outcome", "error"))), DebugValue::Counter(1))); + assert!(matches!( + value(batch, Some(("kind", "single"))), + DebugValue::Histogram(samples) if samples.len() == 1 && samples[0].0 == 1.0 + )); + assert!(matches!( + value(batch, Some(("kind", "multi"))), + DebugValue::Histogram(samples) if samples.len() == 1 && samples[0].0 == 2.0 + )); + assert!(matches!( + value(queue_wait, None), + DebugValue::Histogram(samples) if samples.len() == 1 && samples[0].0 == 2.0 + )); + } + #[test] fn put_rename_code_level_metrics_are_static_and_gated() { let _guard = METRICS_FLAG_LOCK.lock().unwrap_or_else(|e| e.into_inner()); diff --git a/docs/operations/durability-modes.md b/docs/operations/durability-modes.md index bd13f8382f..a5f7cb695c 100644 --- a/docs/operations/durability-modes.md +++ b/docs/operations/durability-modes.md @@ -118,6 +118,19 @@ On Linux, an instrumented run can use `strace -f -c -p -e trace=openat,ope Server-side packing of multiple S3 keys into shared containers requires a persistent index, atomic publication, per-key version/delete semantics, garbage collection, and heal/recovery support. These requirements also apply when compacting historical objects. Current inline storage keeps each object's directory and metadata entry independently addressable. +## Destination-directory group commit metrics + +The experimental destination-directory group commit path emits these metrics when `RUSTFS_OBS_PUT_STAGE_METRICS_ENABLED` and metrics export are enabled. + +| Metric | Meaning | +| --- | --- | +| `rustfs_s3_put_object_rename_dst_dir_fsync_group_enqueue_total` | Waiters accepted into the group queue, or rejected at its active-group or waiter limit. The fixed `outcome` labels are `accepted`, `group_limit`, and `waiter_limit`. | +| `rustfs_s3_put_object_rename_dst_dir_fsync_group_batch_waiters` | Waiters drained per batch, split by `kind=single` or `kind=multi`. Histogram count is the number of drained batches and sum is the number of drained waiters. | +| `rustfs_s3_put_object_rename_dst_dir_fsync_group_queue_wait_ms` | Time from enqueue to drain, one sample per drained waiter. It excludes directory opening, the subsequent global permit wait, and the sync itself. | +| `rustfs_s3_put_object_rename_dst_dir_fsync_group_physical_sync_total` | Returned grouped directory `sync_all()` calls on Unix, labeled `outcome=success` or `outcome=error`. | + +Batch and queue metrics describe the coordinator's work. Canceling a caller does not remove its waiter from the queue. A worker that shuts down before draining can discard queued waiters, while a drained batch can end before reaching the native sync. Use the physical counter for completed sync calls. These metrics cover the grouped destination-directory path, so comparisons with grouping disabled still require scoped syscall counts. + ## Recovery from corrupt object metadata A zero-length or unreadable `xl.meta` is corruption, not an absent object.