From 5b813344abdaa925a899a43dac3d47661eefbd03 Mon Sep 17 00:00:00 2001 From: Hauser Zhi Date: Tue, 1 Dec 2026 20:52:01 +0800 Subject: [PATCH] perf(heal): build MRF checkpoint records in one pass Co-Authored-By: heihutu Co-Authored-By: zhi22915 Co-Authored-By: qunjiazhu --- crates/heal/src/heal/mrf_queue.rs | 138 +++++++++++------- .../heal/src/heal/mrf_queue/partial_write.rs | 2 +- .../tests/unknown_source_tests.rs | 2 +- .../mrf_queue/tests/empty_events_tests.rs | 39 +++++ 4 files changed, 129 insertions(+), 52 deletions(-) diff --git a/crates/heal/src/heal/mrf_queue.rs b/crates/heal/src/heal/mrf_queue.rs index 4f777f482e..038a98f31d 100644 --- a/crates/heal/src/heal/mrf_queue.rs +++ b/crates/heal/src/heal/mrf_queue.rs @@ -1038,6 +1038,29 @@ async fn submit_mrf_heal_request( Ok(receipt.result) } +#[derive(Default)] +#[cfg_attr(test, derive(Debug, PartialEq, Eq))] +struct MrfCheckpointRecords { + authoritative: Vec, + legacy: Vec, + lifecycle: Vec, +} + +impl MrfCheckpointRecords { + fn append(&mut self, intent: &MrfIntent) -> Option<&[u8]> { + let start = self.authoritative.len(); + if !encode_intent(intent, &mut self.authoritative) { + counter!("rustfs_heal_mrf_dropped_total", "reason" => "journal_identity_oversized").increment(1); + return None; + } + let encoded = &self.authoritative[start..]; + if intent.kind == MrfKind::MetadataCorruption || intent.scope.is_none() { + self.legacy.extend_from_slice(encoded); + } + Some(encoded) + } +} + struct MrfRuntime { queue: MrfQueue, partial_writes: PartialWrites, @@ -1413,20 +1436,17 @@ impl MrfRuntime { self.publish_metrics(); } - fn snapshot(&self) -> (Vec, Vec) { - let mut authoritative = Vec::new(); - let mut legacy = Vec::new(); - for intent in self.queue.intents().chain(self.partial_writes.intents()) { - let scoped_identity = - !matches!(intent.kind, rustfs_common::mrf_channel::MrfKind::MetadataCorruption) && intent.scope.is_some(); - if !encode_intent(intent, &mut authoritative) { - counter!("rustfs_heal_mrf_dropped_total", "reason" => "journal_identity_oversized").increment(1); - } - if !scoped_identity && !encode_intent(intent, &mut legacy) { - counter!("rustfs_heal_mrf_dropped_total", "reason" => "journal_identity_oversized").increment(1); - } + fn snapshot(&self) -> MrfCheckpointRecords { + let mut snapshot = MrfCheckpointRecords::default(); + for intent in self.queue.intents() { + let _ = snapshot.append(intent); } - (authoritative, legacy) + // Bind the lifecycle record to the exact published bytes, including + // the record CRC, without running the codec a second time. + snapshot.lifecycle = self + .partial_writes + .checkpoint_records(|intent| snapshot.append(intent).map(|bytes| sha2::Sha256::digest(bytes).into())); + snapshot } async fn flush(&mut self) -> bool { @@ -1438,29 +1458,31 @@ impl MrfRuntime { if self.partial_writes.pending_lifecycle_transition.is_some() { return self.flush_pending_lifecycle(None).await; } - let (authoritative, legacy) = self.snapshot(); + let MrfCheckpointRecords { + authoritative, + legacy, + lifecycle, + } = self.snapshot(); + if lifecycle.len() != self.partial_writes.depth() { + return false; + } let lifecycle_byte_limit = self.config.journal_max_bytes.saturating_mul(4); - let lifecycle_checkpoint = if self.partial_writes.depth() == 0 { + let lifecycle_checkpoint = if lifecycle.is_empty() { None } else { - let records = self.partial_writes.checkpoint_records(intent_digest); - (records.len() == self.partial_writes.depth()) - .then(|| { - encode_mrf_lifecycle_checkpoint( - self.lifecycle_format_version, - self.checkpoint_owner, - self.next_checkpoint_sequence, - records, - lifecycle_byte_limit, - ) - }) - .flatten() + let Some(checkpoint) = encode_mrf_lifecycle_checkpoint( + self.lifecycle_format_version, + self.checkpoint_owner, + self.next_checkpoint_sequence, + lifecycle, + lifecycle_byte_limit, + ) else { + return false; + }; + Some(checkpoint) }; - let lifecycle_ready = self.partial_writes.depth() == 0 || lifecycle_checkpoint.is_some(); let (committed_persisted, committed_on_disk) = if authoritative.is_empty() { (true, false) - } else if !lifecycle_ready { - (false, false) } else { match snapshot::publish_committed_snapshot_with_companion( &journal_disks().await, @@ -1490,14 +1512,8 @@ impl MrfRuntime { } } }; - self.finish_flush( - &authoritative, - &legacy, - committed_persisted, - committed_on_disk, - lifecycle_checkpoint.is_some(), - ) - .await + self.finish_flush(&authoritative, &legacy, committed_persisted, committed_on_disk) + .await } async fn pending_lifecycle_commit_matches( @@ -1544,7 +1560,14 @@ impl MrfRuntime { .clamp(Duration::from_secs(5), Duration::from_secs(60)); let intent = pending.intent.clone(); let record = pending.record.clone(); - let mut records = self.partial_writes.checkpoint_records(intent_digest); + let MrfCheckpointRecords { + authoritative, + legacy, + lifecycle: mut records, + } = self.snapshot(); + if records.len() != self.partial_writes.depth() { + return false; + } let Some(current) = records.iter_mut().find(|current| { current.responsibility_id == record.responsibility_id && current.intent_digest == record.intent_digest }) else { @@ -1566,15 +1589,14 @@ impl MrfRuntime { ) else { return false; }; - let (authoritative, legacy) = self.snapshot(); // A lost publication result must not regenerate the body or downgrade // the owner. An exact committed readback resolves it without new writes. let already_committed = match self.pending_lifecycle_commit_matches(&authoritative, &lifecycle).await { Ok(committed) => committed, Err(_) => return false, }; - let (authoritative, lifecycle) = if already_committed { - (authoritative, lifecycle) + let authoritative = if already_committed { + authoritative } else { let proof = match proof { Some(proof) => proof, @@ -1639,7 +1661,7 @@ impl MrfRuntime { { return false; } - (authoritative, lifecycle) + authoritative }; if !self.partial_writes.restore_state(&intent, &record) { return false; @@ -1650,8 +1672,7 @@ impl MrfRuntime { self.next_checkpoint_sequence = self.next_checkpoint_sequence.saturating_add(1); self.journal_on_disk = true; self.dirty = true; - self.finish_flush(&authoritative, &legacy, true, true, !lifecycle.is_empty()) - .await + self.finish_flush(&authoritative, &legacy, true, true).await } async fn finish_flush( @@ -1660,7 +1681,6 @@ impl MrfRuntime { legacy: &[u8], committed_persisted: bool, committed_on_disk: bool, - lifecycle_ready: bool, ) -> bool { let authoritative_persisted = write_journal(MRF_SCOPED_JOURNAL_PATH, authoritative).await; if !authoritative.is_empty() { @@ -1679,7 +1699,7 @@ impl MrfRuntime { } all_deleted } else { - lifecycle_ready && committed_persisted + committed_persisted }; // Keep dirty until the committed checkpoint, authoritative snapshot, // and compatibility mirror have all been accepted; otherwise a @@ -3220,7 +3240,11 @@ mod tests { backoff_until: None, }; assert_eq!(runtime.queue.try_push_typed(scoped.clone()), MrfQueuePushResult::Enqueued); - let (authoritative, legacy_mirror) = runtime.snapshot(); + let MrfCheckpointRecords { + authoritative, + legacy: legacy_mirror, + .. + } = runtime.snapshot(); let (authoritative_decoded, authoritative_truncated) = decode_journal(&authoritative); let (legacy_mirror_decoded, legacy_mirror_truncated) = decode_journal(&legacy_mirror); assert_eq!(authoritative_truncated, 0, "W13 authoritative scoped mirror must decode cleanly"); @@ -4342,10 +4366,24 @@ mod tests { runtime_checkpoint: None, backoff_until: None, }; - assert_eq!(runtime.queue.try_push_typed(scoped.clone()), MrfQueuePushResult::Enqueued); + runtime + .admit_partial_write(scoped.clone(), Some(Uuid::new_v4())) + .expect("admit scoped responsibility"); assert_eq!(runtime.queue.try_push_typed(compat.clone()), MrfQueuePushResult::Enqueued); - let (authoritative, legacy) = runtime.snapshot(); + let MrfCheckpointRecords { + authoritative, + legacy, + lifecycle, + } = runtime.snapshot(); + let plain_bytes = encoded_payload(&compat); + let scoped_bytes = encoded_payload(&scoped); + assert_eq!(legacy, plain_bytes, "mirror reuses the exact unscoped bytes"); + assert_eq!(authoritative, [plain_bytes, scoped_bytes].concat()); + let (_, consumed) = decode_one(&authoritative).expect("decode first record"); + let durable_digest: [u8; 32] = sha2::Sha256::digest(&authoritative[consumed..]).into(); + assert_eq!(lifecycle.len(), 1); + assert_eq!(lifecycle[0].intent_digest, durable_digest); let (authoritative_decoded, authoritative_truncated) = decode_journal(&authoritative); assert_eq!(authoritative_truncated, 0); assert_eq!(authoritative_decoded.len(), 2); diff --git a/crates/heal/src/heal/mrf_queue/partial_write.rs b/crates/heal/src/heal/mrf_queue/partial_write.rs index 89c45e7829..ec44a52220 100644 --- a/crates/heal/src/heal/mrf_queue/partial_write.rs +++ b/crates/heal/src/heal/mrf_queue/partial_write.rs @@ -590,7 +590,7 @@ impl PartialWrites { pub(super) fn checkpoint_records( &self, - digest_for: impl Fn(&MrfIntent) -> Option<[u8; 32]>, + mut digest_for: impl FnMut(&MrfIntent) -> Option<[u8; 32]>, ) -> Vec { self.entries .values() diff --git a/crates/heal/src/heal/mrf_queue/partial_write/tests/unknown_source_tests.rs b/crates/heal/src/heal/mrf_queue/partial_write/tests/unknown_source_tests.rs index eea5c9240a..737add2d1e 100644 --- a/crates/heal/src/heal/mrf_queue/partial_write/tests/unknown_source_tests.rs +++ b/crates/heal/src/heal/mrf_queue/partial_write/tests/unknown_source_tests.rs @@ -879,7 +879,7 @@ fn unknown_source_deleted_bucket_writer_lost_result_is_resolved_by_exact_commit_ let record = pending.record.clone(); runtime.partial_writes.pending_lifecycle_transition = Some(pending); let sequence = runtime.next_checkpoint_sequence; - let (payload, _) = runtime.snapshot(); + let payload = runtime.snapshot().authoritative; let lifecycle = encode_mrf_lifecycle_checkpoint( 2, runtime.checkpoint_owner, diff --git a/crates/heal/src/heal/mrf_queue/tests/empty_events_tests.rs b/crates/heal/src/heal/mrf_queue/tests/empty_events_tests.rs index 83a11eaa95..8e5ef052c9 100644 --- a/crates/heal/src/heal/mrf_queue/tests/empty_events_tests.rs +++ b/crates/heal/src/heal/mrf_queue/tests/empty_events_tests.rs @@ -537,3 +537,42 @@ fn empty_events_real_consumer_stop_retains_unresolved_journal() { ); }); } + +#[test] +fn lifecycle_encoding_failure_preserves_the_previous_checkpoint_and_mirrors() { + if isolated("lifecycle_encoding_failure_preserves_the_previous_checkpoint_and_mirrors") { + return; + } + tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .expect("runtime") + .block_on(async { + let (_env, _manager, mut runtime, anchor) = bound_runtime().await; + let previous = runtime.runtime_checkpoint; + let authoritative = read_journal(MRF_SCOPED_JOURNAL_PATH).await; + let legacy = read_journal(MRF_JOURNAL_PATH).await; + assert!(authoritative.is_some(), "fixture has a published authoritative mirror"); + + let mut hint = partial_intent("next-bucket"); + hint.kind = MrfKind::DecodeFailure; + hint.scope = None; + runtime.admit_best_effort(hint); + runtime + .admit_partial_write(partial_intent("next-bucket"), Some(anchor.bucket_incarnation_id)) + .expect("admit a new durable responsibility"); + let original_limit = runtime.config.journal_max_bytes; + runtime.config.journal_max_bytes = 1; + assert!(!runtime.flush().await, "an oversized companion cannot acknowledge publication"); + assert_eq!(runtime.runtime_checkpoint, previous); + assert_eq!(read_journal(MRF_SCOPED_JOURNAL_PATH).await, authoritative); + assert_eq!(read_journal(MRF_JOURNAL_PATH).await, legacy); + assert!(runtime.dirty, "the next flush must retain pending work"); + + runtime.config.journal_max_bytes = original_limit; + assert!(runtime.flush().await, "the prior state remains retryable"); + assert_ne!(runtime.runtime_checkpoint, previous); + assert_ne!(read_journal(MRF_SCOPED_JOURNAL_PATH).await, authoritative); + assert_ne!(read_journal(MRF_JOURNAL_PATH).await, legacy); + }); +}