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.
This commit is contained in:
acamilo 2026-10-02 10:59:47 +00:00
parent da8fa2a927
commit 9a4542de5f
6 changed files with 24 additions and 14 deletions

View file

@ -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 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 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 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 fresh allocation in content as well: the router overwrites the staging file with zeros before
before the reply, so it reads as zeros and no artifact sealed from it -- a prefix, or the whole 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, 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 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`). one writer named by two attachments of the same command is refused (`INVALID_ENVELOPE`).

View file

@ -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). `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 - **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 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 every note was treated as live-path correctness. A recycled writer's staging file is zero-filled
and reserved again when the router reissues it, so no artifact sealed from it -- by prefix or 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 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 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 with an unsealed writer (N2). A refused sealing reply is answered as the handler's own seal

View file

@ -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 `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 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. 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 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 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. `rpc.call` and `publish` too; the SDK exposes them for replies.

View file

@ -596,7 +596,7 @@ pub struct SealedReply {
/// As [`Responder::reply`]: routed to the caller, or the caller had detached. /// As [`Responder::reply`]: routed to the caller, or the caller had detached.
pub routed: bool, pub routed: bool,
/// With `recycle`, a fresh writer of the same allocation for each artifact sealed. Its /// 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. /// allocation does.
pub writers: Vec<ArtifactWriter>, pub writers: Vec<ArtifactWriter>,
} }

View file

@ -336,7 +336,7 @@ async fn read_loop<R: AsyncRead + Unpin>(
// (bus-v1 section 12 amendment 2026-10-02): the copies run here, outside the // (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 // 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, // 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. // is (BUS-02 review N3); otherwise it goes with the copy.
let total: u64 = job.seals.iter().map(|s| s.len).sum(); let total: u64 = job.seals.iter().map(|s| s.len).sum();
let plan: Vec<(u64, u64, u64, Option<u64>)> = job let plan: Vec<(u64, u64, u64, Option<u64>)> = job

View file

@ -115,18 +115,28 @@ impl Store {
result result
} }
/// Makes a recycled writer's staging file what a fresh allocation is: `len` bytes, all zero, /// 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 /// (BUS-02 review N3). The file still holds the artifact it was sealed from; a producer that
/// producer that wrote fewer bytes into its next life and sealed the whole allocation would /// wrote fewer bytes into its next life and sealed the whole allocation would otherwise
/// otherwise publish the previous artifact's tail. Truncating frees those blocks, and the /// publish the previous artifact's tail. The zeros are written over the blocks already
/// reservation that follows is the one `create_staging` makes. /// 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<()> { pub(crate) fn reset_staging(&self, serial: u64, len: u64) -> io::Result<()> {
use std::os::unix::fs::FileExt;
let file = OpenOptions::new() let file = OpenOptions::new()
.write(true) .write(true)
.custom_flags(libc::O_NOFOLLOW) .custom_flags(libc::O_NOFOLLOW)
.open(self.path(&staging_rel(serial)))?; .open(self.path(&staging_rel(serial)))?;
file.set_len(0)?; let zeros = vec![0u8; len.clamp(1, 256 * 1024) as usize];
reserve(&file, len) 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, /// Copies exactly `len` staging bytes into a fresh sealed file, checking the length and,