Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ rc du local/photos --incomplete
rc du local --snapshot
```

During sustained writes or lifecycle transitions, `rc du local/photos --snapshot` can show the latest completed scanner traversal as an estimate until the bucket has certified usage. The output includes the original observation time. JSON reports mark the result as partial and include the bucket in `data.observed_statistics`. Cluster totals exclude these estimates.

For large buckets, a live scan may take many S3 requests. Existing scripts using `--fallback` still try the snapshot first. See the [operational utilities reference](docs/reference/rc/ops.md) for permissions, version counts, and partial results.

## Command Overview
Expand Down
88 changes: 77 additions & 11 deletions crates/cli/src/commands/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ struct UsageData {
#[serde(skip_serializing_if = "std::collections::BTreeMap::is_empty")]
bucket_snapshot_at: std::collections::BTreeMap<String, Timestamp>,
#[serde(skip_serializing_if = "std::collections::BTreeMap::is_empty")]
observed_statistics: std::collections::BTreeMap<String, Timestamp>,
#[serde(skip_serializing_if = "std::collections::BTreeMap::is_empty")]
initial_statistics: std::collections::BTreeMap<String, Timestamp>,
#[serde(skip_serializing_if = "Vec::is_empty")]
unmeasured_buckets: Vec<String>,
Expand Down Expand Up @@ -282,13 +284,20 @@ fn emit_report(

if report.partial {
if !formatter.is_json() {
formatter.warning(if !report.initial_statistics.is_empty() {
"Initial statistics are incomplete."
} else if !report.unmeasured_buckets.is_empty() {
"Server statistics are unavailable for one or more buckets."
} else {
"Usage scan is partial; one or more buckets could not be read."
});
if !report.observed_statistics.is_empty() {
formatter.warning(
"Observed bucket statistics are estimates from a completed scanner traversal.",
);
}
if !report.initial_statistics.is_empty() {
formatter.warning("Initial statistics are incomplete.");
}
if !report.unmeasured_buckets.is_empty() {
formatter.warning("Server statistics are unavailable for one or more buckets.");
}
if !report.failures.is_empty() {
formatter.warning("Usage scan is partial; one or more buckets could not be read.");
}
}
ExitCode::NetworkError
} else {
Expand Down Expand Up @@ -353,9 +362,16 @@ fn scope_snapshot(
report
.initial_statistics
.retain(|bucket, _| bucket == bucket_name);
report
.observed_statistics
.retain(|bucket, _| bucket == bucket_name);
report.unmeasured_buckets.clear();
report.partial = !report.initial_statistics.is_empty();
if let Some(timestamp) = report.initial_statistics.get(bucket_name) {
let estimate_at = report
.observed_statistics
.get(bucket_name)
.or_else(|| report.initial_statistics.get(bucket_name));
report.partial = estimate_at.is_some();
if let Some(timestamp) = estimate_at {
report.snapshot_at = Some(*timestamp);
report.version_count = None;
report.delete_marker_count = None;
Expand All @@ -378,6 +394,7 @@ fn success_output(
status: "success",
data: UsageData {
bucket_snapshot_at: report.bucket_snapshot_at.clone(),
observed_statistics: report.observed_statistics.clone(),
initial_statistics: report.initial_statistics.clone(),
unmeasured_buckets: report.unmeasured_buckets.clone(),
scope: report.scope.as_str(),
Expand Down Expand Up @@ -449,17 +466,31 @@ fn print_human(report: &UsageReport, now: Timestamp, formatter: &Formatter) {
"BUCKET BYTES OBJECTS VERSIONS DELETE MARKERS",
);
for bucket in &report.buckets {
let observed = report
.observed_statistics
.get(&bucket.name)
.map(|time| format!(" (observed {time}, estimate)"))
.unwrap_or_default();
formatter.println(&format!(
"{:<31} {:>11} {:>11} {:>12} {:>17}",
"{:<31} {:>11} {:>11} {:>12} {:>17}{observed}",
formatter.sanitize_text(&bucket.name),
humansize::format_size(bucket.total_bytes, humansize::BINARY),
bucket.object_count,
optional_count(bucket.version_count),
optional_count(bucket.delete_marker_count),
));
}
let total_label = if report.scope == rc_core::ops::UsageScope::Cluster
&& !report.observed_statistics.is_empty()
{
"Total (excludes observed rows)"
} else if !report.observed_statistics.is_empty() {
"Estimated total"
} else {
"Total"
};
formatter.println(&format!(
"Total: {}, {} object(s), {} version(s), {} delete marker(s)",
"{total_label}: {}, {} object(s), {} version(s), {} delete marker(s)",
humansize::format_size(report.total_bytes, humansize::BINARY),
report.object_count,
optional_count(report.version_count),
Expand Down Expand Up @@ -615,6 +646,7 @@ mod tests {
fn snapshot(timestamp: i64) -> UsageReport {
UsageReport {
bucket_snapshot_at: Default::default(),
observed_statistics: Default::default(),
initial_statistics: Default::default(),
unmeasured_buckets: Vec::new(),
source: UsageSource::ServerSnapshot,
Expand Down Expand Up @@ -658,6 +690,18 @@ mod tests {
incomplete_upload_count: None,
incomplete_upload_bytes: None,
});
report
.observed_statistics
.insert("hot".into(), Timestamp::from_second(150).unwrap());
report.push_bucket(UsageBucket {
name: "hot".into(),
total_bytes: 131,
object_count: 4,
version_count: None,
delete_marker_count: None,
incomplete_upload_count: None,
incomplete_upload_bytes: None,
});
let request = |bucket| UsageScanRequest {
bucket: Some(bucket),
prefix: None,
Expand All @@ -668,6 +712,28 @@ mod tests {
scope_snapshot(report.clone(), "alias", &request(first)).expect("full bucket");
assert!(!complete.partial);
assert!(complete.initial_statistics.is_empty());
assert!(complete.observed_statistics.is_empty());
let observed =
scope_snapshot(report.clone(), "alias", &request("hot".into())).expect("hot bucket");
assert!(observed.partial);
assert_eq!(
observed.snapshot_at,
Some(Timestamp::from_second(150).unwrap())
);
assert_eq!(
(
observed.object_count,
observed.total_bytes,
observed.version_count
),
(4, 131, None)
);
let output = success_output(&observed, None, Timestamp::from_second(300).unwrap());
assert_eq!(output.data.snapshot_age_seconds, Some(150));
assert_eq!(
output.data.observed_statistics,
observed.observed_statistics
);
assert_eq!(
complete.snapshot_at,
Some(Timestamp::from_second(100).expect("full time"))
Expand Down
35 changes: 19 additions & 16 deletions crates/core/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ pub struct UsageReport {
/// Original complete measurement times, independent of owner fencing.
#[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
pub bucket_snapshot_at: std::collections::BTreeMap<String, Timestamp>,
/// Full scanner traversals that lack a bucket certificate, keyed by their original observation time.
#[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
pub observed_statistics: std::collections::BTreeMap<String, Timestamp>,
/// Initial measurements, keyed by bucket and original checkpoint time.
/// These rows are partial and never replace complete server measurements.
#[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
Expand Down Expand Up @@ -182,6 +185,7 @@ impl UsageReport {
/// Create an empty report that can be populated with bucket rows.
pub fn empty(source: UsageSource, scope: UsageScope, path: Option<String>) -> Self {
Self {
observed_statistics: Default::default(),
initial_statistics: Default::default(),
bucket_snapshot_at: Default::default(),
unmeasured_buckets: Vec::new(),
Expand Down Expand Up @@ -222,29 +226,28 @@ impl UsageReport {
.sort_by(|left, right| left.name.cmp(&right.name));
self.failures
.sort_by(|left, right| left.bucket.cmp(&right.bucket));
self.total_bytes = self.buckets.iter().fold(0_u64, |total, bucket| {
// A bucket-scoped estimate describes that bucket alone. A cluster total
// excludes observations because concurrent changes prevented certification.
let counted = self.buckets.iter().filter(|row| {
self.scope != UsageScope::Cluster || !self.observed_statistics.contains_key(&row.name)
});
self.total_bytes = counted.clone().fold(0_u64, |total, bucket| {
total.saturating_add(bucket.total_bytes)
});
self.object_count = self.buckets.iter().fold(0_u64, |total, bucket| {
self.object_count = counted.clone().fold(0_u64, |total, bucket| {
total.saturating_add(bucket.object_count)
});
self.version_count = sum_optional(self.buckets.iter().map(|bucket| bucket.version_count))
self.version_count = sum_optional(counted.clone().map(|bucket| bucket.version_count))
.or_else(|| versions_were_requested.then_some(0));
self.delete_marker_count =
sum_optional(self.buckets.iter().map(|bucket| bucket.delete_marker_count))
sum_optional(counted.clone().map(|bucket| bucket.delete_marker_count))
.or_else(|| delete_markers_were_requested.then_some(0));
self.incomplete_upload_count = sum_optional(
self.buckets
.iter()
.map(|bucket| bucket.incomplete_upload_count),
)
.or_else(|| incomplete_uploads_were_requested.then_some(0));
self.incomplete_upload_bytes = sum_optional(
self.buckets
.iter()
.map(|bucket| bucket.incomplete_upload_bytes),
)
.or_else(|| incomplete_bytes_were_requested.then_some(0));
self.incomplete_upload_count =
sum_optional(counted.clone().map(|bucket| bucket.incomplete_upload_count))
.or_else(|| incomplete_uploads_were_requested.then_some(0));
self.incomplete_upload_bytes =
sum_optional(counted.map(|bucket| bucket.incomplete_upload_bytes))
.or_else(|| incomplete_bytes_were_requested.then_some(0));
}
}

Expand Down
Loading