From 9a4542de5f0970f028bd58b0c8912ce7975e2d42 Mon Sep 17 00:00:00 2001 From: acamilo Date: Fri, 2 Oct 2026 10:59:47 +0000 Subject: [PATCH] flybus: zero-fill a recycled staging file in place instead of truncating and reserving it again Freeing the blocks and faulting them back in on the producer's next write cost about 36 us a 92,160-byte reply on tmpfs; writing zeros over the blocks already there costs about 10 (rtt, process mode, interleaved against the branch before the review round). Same guarantee: a reissued writer reads as zeros, as a fresh allocation does. --- docs/design/session-framework/bus-v1.md | 4 ++-- .../session-framework/implementation.md | 4 ++-- services/flysim/crates/flybus/README.md | 2 +- .../crates/flybus/src/client/handles.rs | 2 +- .../flysim/crates/flybus/src/router/mod.rs | 2 +- services/flysim/crates/flybus/src/store.rs | 24 +++++++++++++------ 6 files changed, 24 insertions(+), 14 deletions(-) diff --git a/docs/design/session-framework/bus-v1.md b/docs/design/session-framework/bus-v1.md index 9e9f58d..d355997 100644 --- a/docs/design/session-framework/bus-v1.md +++ b/docs/design/session-framework/bus-v1.md @@ -593,8 +593,8 @@ critical path, for answers the router could give as it admits the reply. attachment it sealed, named by attachment. A writer it cannot reissue (owner budget) is simply left out. Section 8.1's immutability is unchanged: the sealed bytes are a copy, so a producer writing into the recycled staging file reaches only the staging inode. A reissued writer is a - fresh allocation in content as well: the router empties the staging file and reserves it again - before the reply, so it reads as zeros and no artifact sealed from it -- a prefix, or the whole + fresh allocation in content as well: the router overwrites the staging file with zeros before + the reply, so it reads as zeros and no artifact sealed from it -- a prefix, or the whole allocation through `artifact.seal` -- can carry bytes of the artifact it was before (review N3, 2026-10-02). `recycle` in a body with no sealing attachment is refused, `false` included, and one writer named by two attachments of the same command is refused (`INVALID_ENVELOPE`). diff --git a/docs/design/session-framework/implementation.md b/docs/design/session-framework/implementation.md index a0c2e18..b4353be 100644 --- a/docs/design/session-framework/implementation.md +++ b/docs/design/session-framework/implementation.md @@ -573,8 +573,8 @@ section 12 (amendment 2026-10-02); [step-v1](step-v1.md)'s ordering is untouched `7b940584`) in `local`, `bus/thread` and `bus/process` (the FAFB parity suites, all arms). - **Review notes (2026-10-02).** The review approved with notes; since the feed and the control API already run over flybus in production, the router and SDK changes carry live traffic, so - every note was treated as live-path correctness. A recycled writer's staging file is emptied - and reserved again when the router reissues it, so no artifact sealed from it -- by prefix or + every note was treated as live-path correctness. A recycled writer's staging file is zero-filled + when the router reissues it (about 10 us a 92,160-byte reply on tmpfs), so no artifact sealed from it -- by prefix or whole -- carries the previous artifact's bytes (N3). A sealing reply is sent with the worker's result cache held, so a duplicate replays the sealed artifacts rather than racing the reply with an unsealed writer (N2). A refused sealing reply is answered as the handler's own seal diff --git a/services/flysim/crates/flybus/README.md b/services/flysim/crates/flybus/README.md index 074e290..93822df 100644 --- a/services/flysim/crates/flybus/README.md +++ b/services/flysim/crates/flybus/README.md @@ -192,7 +192,7 @@ let kept = frame.retain().await?; // an independent explicit hold `responder.reply_sealing(outcome, attachments, unsealed, recycle)`: the router seals it as it admits the reply, with no `artifact.seal` round trip, and with `recycle` hands each staging allocation back as a fresh writer (`SealedReply::writers`), with no `artifact.allocate` either. - The router empties a reissued staging file first, so a recycled writer reads as zeros like a + The router zero-fills a reissued staging file first, so a recycled writer reads as zeros like a new allocation and never carries the previous artifact's bytes into the next one, however it is sealed. The handle is valid once the reply is admitted. The router accepts sealing attachments on `rpc.call` and `publish` too; the SDK exposes them for replies. diff --git a/services/flysim/crates/flybus/src/client/handles.rs b/services/flysim/crates/flybus/src/client/handles.rs index 78f5e49..f04003e 100644 --- a/services/flysim/crates/flybus/src/client/handles.rs +++ b/services/flysim/crates/flybus/src/client/handles.rs @@ -596,7 +596,7 @@ pub struct SealedReply { /// As [`Responder::reply`]: routed to the caller, or the caller had detached. pub routed: bool, /// With `recycle`, a fresh writer of the same allocation for each artifact sealed. Its - /// staging file is the sealed artifact's, emptied: it reads as zeros, as a new + /// staging file is the sealed artifact's, zero-filled: it reads as zeros, as a new /// allocation does. pub writers: Vec, } diff --git a/services/flysim/crates/flybus/src/router/mod.rs b/services/flysim/crates/flybus/src/router/mod.rs index 024ae0f..3e91f84 100644 --- a/services/flysim/crates/flybus/src/router/mod.rs +++ b/services/flysim/crates/flybus/src/router/mod.rs @@ -336,7 +336,7 @@ async fn read_loop( // (bus-v1 section 12 amendment 2026-10-02): the copies run here, outside the // state lock, and this connection's next command waits for the admission, so // its commands stay in order. A recycled writer's staging file is kept, - // renamed for the artifact it becomes and reset to zeros, as a fresh allocation + // renamed for the artifact it becomes and zero-filled, as a fresh allocation // is (BUS-02 review N3); otherwise it goes with the copy. let total: u64 = job.seals.iter().map(|s| s.len).sum(); let plan: Vec<(u64, u64, u64, Option)> = job diff --git a/services/flysim/crates/flybus/src/store.rs b/services/flysim/crates/flybus/src/store.rs index da970c5..a027e14 100644 --- a/services/flysim/crates/flybus/src/store.rs +++ b/services/flysim/crates/flybus/src/store.rs @@ -115,18 +115,28 @@ impl Store { result } - /// Makes a recycled writer's staging file what a fresh allocation is: `len` bytes, all zero, - /// reserved (BUS-02 review N3). The file still holds the artifact it was sealed from; a - /// producer that wrote fewer bytes into its next life and sealed the whole allocation would - /// otherwise publish the previous artifact's tail. Truncating frees those blocks, and the - /// reservation that follows is the one `create_staging` makes. + /// Makes a recycled writer's staging file what a fresh allocation is: `len` bytes, all zero + /// (BUS-02 review N3). The file still holds the artifact it was sealed from; a producer that + /// wrote fewer bytes into its next life and sealed the whole allocation would otherwise + /// publish the previous artifact's tail. The zeros are written over the blocks already + /// there, which stay allocated: freeing them (truncating, or punching a hole) and faulting + /// them back in on the next write cost four times as much on tmpfs (a 92,160-byte frame: + /// about 36 us a reply against 9). The length is set last, in case the producer resized + /// the file. pub(crate) fn reset_staging(&self, serial: u64, len: u64) -> io::Result<()> { + use std::os::unix::fs::FileExt; let file = OpenOptions::new() .write(true) .custom_flags(libc::O_NOFOLLOW) .open(self.path(&staging_rel(serial)))?; - file.set_len(0)?; - reserve(&file, len) + let zeros = vec![0u8; len.clamp(1, 256 * 1024) as usize]; + let mut at = 0; + while at < len { + let n = (len - at).min(zeros.len() as u64) as usize; + file.write_all_at(&zeros[..n], at)?; + at += n as u64; + } + file.set_len(len) } /// Copies exactly `len` staging bytes into a fresh sealed file, checking the length and,