Skip to content

Move S3 buckets and keys onto the capsule journal - #130

Merged
saworbit merged 5 commits into
mainfrom
format/names
Nov 12, 2026
Merged

saworbit merged 5 commits into
mainfrom
format/names

Conversation

@saworbit

Copy link
Copy Markdown
Owner

Description

Part of #97.

Stacked on #129, which is stacked on #128 and #127. Merge those first. Until then this diff also shows their commits; this PR's change is aef538b.

This implements the names half of on-disk format 1 (spec §5.5–§5.7 and §7; PR 3 in §14). Two problems motivate it:

  • S3 buckets and keys lived only in sled, so losing space.db lost them, even though Commit capsules to a journal and make sled its index #129 made the capsule list recoverable.

  • An S3 overwrite bound the new key and deleted the old capsule in separate steps. A crash in between could leave the old capsule alive with no key pointing at it, or the new one without its key.

  • Name record codecs (crates/common/src/format.rs):

    • NAMESPACE, BIND and UNBIND use the layouts the spec drafted. A namespace can't be empty or contain NUL; both encode and decode check this.
    • BATCH sub-records no longer carry a frame. They are now kind, version, a reserved field, header length and header (spec §5.7). See the first reviewer note for why.
  • One commit path: CapsuleCatalog::commit(records) commits several records as one BATCH and returns the capsules its DROPs removed. create_capsule and delete_capsule are now provided methods built on it.

  • Registry (crates/capsule-registry):

    • commit checks CAPSULE and DROP records against the index while holding the journal lock (Journal::lock / JournalWriter::append). A DROP of a capsule that isn't live fails the whole commit, and nothing is written.
    • sled indexes names in new namespaces and names trees. The s3_buckets and s3_objects trees, and the MetadataLog that wrote them, are gone.
    • New read API: create_namespace, namespace_created_at, name, list_names.
  • Pipeline (crates/pipeline):

    • finish_write_with commits a write together with other records.
    • commit commits records on their own.
    • release frees a dropped capsule's segments. Until it's called, those segments stay readable, which is how a GET in flight pins them.
    • stage_from_reader streams a write and leaves committing it to the caller.
  • S3 view (crates/protocol-s3):

    • Buckets are namespaces s3:<bucket> and keys are names in them.
    • A PUT commits the capsule, its key, the bucket if it's new, and the drop of the replaced capsule as one BATCH. A DELETE commits the unbind and the drop as one.
    • Overwrites and DELETEs commit at once. A GET still reading the old object holds its segments until it ends, where before the delete itself was put off.

Type

  • Bug fix
  • New feature
  • Breaking change
  • Refactor / cleanup
  • Docs / comments only

Checklist

Quality gates

  • cargo fmt --all
  • cargo clippy --all-targets -- -D warnings (run with --workspace)
  • Tests pass (cargo test --workspace): 326 passed, 0 failed
  • cargo xtask audit (if touching crates, deps, or features): not available in this tree; no dependency changes
  • Docs compile: RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps

Documentation

  • I updated relevant docs (or no user-facing changes)
  • CHANGELOG.md updated (for features, fixes, breaking changes)

Security

  • No hardcoded secrets, tokens, or keys introduced
  • External inputs are validated (if applicable): bucket names and keys are capped at what a journal string can hold (65,535 bytes, less the s3: prefix for buckets), so an oversized one gets InvalidBucketName or InvalidArgument instead of failing after the body is stored
  • No new unsafe blocks (or they are justified and documented)

Dependency changes (skip if none)

None.

What a reviewer should know

  • The BATCH encoding from Commit capsules to a journal and make sled its index #129 had a bug. Sub-records were complete framed records. When a crash cuts off a BATCH, the open scan searches forward for the next valid record. It found one of those embedded sub-records, reported damage in the middle of the journal, and the journal refused to open. Nothing wrote a BATCH until this PR, so it never showed up. A new torn-overwrite test caught it. Format 1 hasn't shipped, so I changed the encoding rather than working around it. A unit test now cuts a BATCH at every byte and checks the journal opens with only the earlier record.
  • Index version 2. An index written by Commit capsules to a journal and make sled its index #129 is rebuilt on open. Any S3 names it held in the old sled trees aren't in the journal, so the rebuild drops them, with a warning. A space.db that holds S3 buckets but has no journal position (one written by main) is refused rather than cleared, so going back to main still finds them.
  • Pins are the data log's in-memory reference counts. Pipeline::garbage_collect doesn't know about them and would remove a pinned segment, but nothing calls it while serve-s3 runs. The spec (§14) notes this.
  • Behaviour changes the tests now encode:
    • A failed PUT no longer creates its bucket, because the bucket commits with the object (a_failed_upload_stores_nothing).
    • The GET-during-overwrite test now asserts that the old capsule leaves the index immediately and its segments stay in the data log until the GET ends.
    • The dropped-upload test waited on content_entries(), which has been trivially empty since Commit capsules to a journal and make sled its index #129 moved uncommitted hashes to the pending index. It now waits on the data log.
  • API changes:
    • CapsuleRegistry::{create_bucket, bucket_created_at, object, bind_object, unbind_object, list_objects} are replaced by the generic name API.
    • Journal::commit is replaced by lock / append.
    • metadata_log.rs and metadata_ops.rs are removed.
  • Still open:
    • A sled apply that fails after a durable append still leaves the index behind until restart, as in Commit capsules to a journal and make sled its index #129.
    • A name crafted to contain a whole record could still fool the forward scan if its own record is torn (§8.1 limitation, documented).
    • spacectl delete of a named capsule leaves the name, which reads as missing (open question 6, kept as is).
  • Checked beyond the tests:
    • Mutation checks: releasing segments under a reader fails the GET-during-overwrite test, and skipping the live-capsule check on DROP fails the refused-commit test.
    • A real spacectl serve-s3 run: put, overwrite, delete and create an empty bucket, then delete space.db and restart. The keys, the empty bucket and the deletion all came back, and spacectl list showed only the live capsules.

Add docs/on-disk-format.md, a draft spec (not implemented) for making
every durable fact rebuildable from append-only files:

- A 48-byte checksummed frame on every record.
- SEGMENT records that carry their write-once descriptor next to the
  stored bytes, replacing the JSON sidecar.
- A separate capsule journal (CAPSULE manifests with a logical digest,
  DROP, NAMESPACE, BIND, UNBIND, atomic BATCH), with sled demoted to a
  rebuildable index and ref counts derived from live manifests.
- Torn-tail truncation, read-only open on mid-file corruption, and
  compaction by generation swap.

Targets #96, #97 and #108. Link it from docs/README.md as a draft.
Implement the data-log half of on-disk format 1. The NVRAM log no longer
needs a sidecar: losing space.nvram.segments made every capsule
unreadable (#96), and the file was rewritten whole on every append and
ref-count change without an fsync.

- common::format: codec for the 48-byte record frame (BLAKE3 head and
  body checksums), the FILE record and the SEGMENT descriptor, with
  byte layouts defined field by field.
- nvram-sim: the log starts with a FILE record and every append writes
  one framed record per segment. open() scans the log to rebuild the
  segment map, truncates a record cut short at the end, and opens
  read-only when invalid bytes are followed by valid records. Stores
  from before format 1 are refused with a message saying so.
- Ref counts are no longer stored. Opening a registry pipeline recounts
  them from the capsules in space.db, so an interrupted dedup write
  can't leave an inflated count (part of #97). Compaction refuses to
  run until counts have been recounted.
- Deletes and GC now change only the in-memory map; a removed segment's
  record comes back with no references on reopen until compaction
  drops it. integration_test is updated for that.
- Tests: codec layouts and checksums, truncation of the last record at
  every byte offset, mid-log damage, compaction after a recount, and
  registry-level reopen, dedup-crash and torn-append cases.
- Docs: capsule.md, the S3 quick start, dedup and encryption notes,
  SECURITY.md, nvram-sim.md and CHANGELOG describe the new log. The spec
  now records that only the last body is checksummed on open, that an
  all-zero content hash means none, a forward-search limitation, and
  how this stands in until the journal exists.

Breaking: existing space.nvram stores can't be opened.

Closes #96.
Implement the capsule-journal half of on-disk format 1. Capsule records
lived only in sled, so losing space.db lost the capsule list, and a
crashed write could leave a content-index entry for a segment that
compaction later removed.

- common::format: codecs for CAPSULE manifests (segment entries with
  plain length and content hash, BLAKE3 digest, dedupe and
  requires-encryption flags, write-time policy), DROP and BATCH.
  Capsule gains a digest field.
- nvram_sim::record_file: the open, scan and torn-tail logic from the
  data log, now shared with the journal.
- capsule-registry: space.journal next to space.db. A write commits by
  appending and fsyncing a CAPSULE record, a delete by a DROP. Each
  record is applied to sled in one transaction with the journal
  position. On open, records past that position are replayed; a missing
  or mismatched index is rebuilt; a space.db from before the journal is
  refused. sled keeps per-segment reference counts and which segment
  each indexed hash belongs to.
- New segments' hashes wait in an in-memory pending index and reach the
  durable content index only when their capsule commits.
- The pipeline builds the manifest and digest as it writes, commits a
  delete before releasing segments, and read_capsule checks the digest.
- Pipeline opens pair the journal with the data log's store id (a new
  journal adopts it; a mismatch is refused), keep the segment allocator
  above every id in the log, and set the log's ref counts from sled.
- Tests: journal codecs; index rebuilt after deleting space.db; records
  an older index missed replayed; torn journal tail; crashed write
  leaves no index entry; store id mismatch and adoption; digest. Two
  integration tests move from fixed paths in the crate folder to unique
  temp directories.
- Docs: capsule.md, the S3 quick start, dedup notes and CHANGELOG
  describe the journal; the spec records PR 2 as done and where the code
  differs from it.

S3 buckets and keys still live only in sled.

Part of #97.
Implement the names half of on-disk format 1. S3 buckets and keys lived
only in sled, so losing space.db lost them, and an S3 overwrite bound
the new key and deleted the old capsule in separate steps, so a crash in
between could leave the old capsule alive with no key pointing at it.

- common::format: codecs for NAMESPACE, BIND and UNBIND. A namespace
  can't be empty or contain NUL, checked on encode and decode. BATCH
  sub-records drop their frame (kind, version, reserved, header_len,
  header): embedded whole records made a torn batch look like damage in
  the middle of the journal, which then refused to open.
- CapsuleCatalog has one commit(records) that commits several records
  as a BATCH and returns the capsules its DROPs removed;
  create_capsule and delete_capsule are built on it.
- capsule-registry: commit checks CAPSULE and DROP records against the
  index under the journal lock, so a DROP of a capsule that isn't live
  fails the whole commit and nothing is written. sled indexes names in
  namespaces and names trees; the s3_buckets and s3_objects trees and
  the MetadataLog that wrote them are gone. A pre-journal space.db
  holding S3 buckets is refused like one holding capsules, and a
  rebuild drops the old trees. The index version goes to 2.
- pipeline: finish_write_with commits a write together with other
  records, commit commits records alone, and release frees a dropped
  capsule's segments, so a caller can hold them while a reader
  finishes. stage_from_reader leaves a streamed write for the caller to
  commit.
- protocol-s3: buckets are namespaces "s3:<bucket>" and keys are names.
  PUT commits the capsule, its key, the bucket if new and the drop of
  the replaced capsule as one batch; DELETE commits the unbind and the
  drop as one. Overwrites and DELETEs commit at once; a GET in flight
  holds the dropped capsule's segments until it ends, instead of
  delaying the delete. A failed PUT no longer creates its bucket.
- Tests: name codecs and namespace rules; a batch cut at every byte is
  dropped as a torn tail; names rebuilt after deleting space.db; a
  refused commit writes nothing; a torn overwrite batch leaves the old
  key; pre-journal S3 names refused or dropped; S3 keys survive a lost
  index; the GET-during-overwrite test now checks segments stay pinned.
- Docs: the spec records PR 3 as done, the new BATCH layout and where
  the code differs; capsule.md, the S3 quick start, streaming reads,
  dedup notes and CHANGELOG describe names in the journal.

Part of #97.
@saworbit
saworbit merged commit 06070c6 into main Nov 12, 2026
2 checks passed
@saworbit
saworbit deleted the format/names branch November 12, 2026 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant