Skip to content

engine: run containerd's metadata on an in-memory store - #14965

Closed
sipsma wants to merge 2 commits into
mainfrom
sipsma/inmem-metadata-store
Closed

sipsma wants to merge 2 commits into
mainfrom
sipsma/inmem-metadata-store

Conversation

@sipsma

@sipsma sipsma commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

containerd's metadata database and the overlay snapshotter's store run on bbolt. The engine already runs both without fsync, so what bolt costs is its page work: decoding and rewriting B+tree pages, copying them for each write transaction, and holding one write lock while it does. Every snapshot prepare, view and commit, and every lease change, pays it.

This replaces bbolt, for containerd and for the engine's own bolt code, with internal/fastbolt: a store with the part of bbolt's API they use, built for speed instead of durability. One go.mod replace swaps it in; containerd isn't forked.

What it does for users

Measured on a cold first build of dagger's CLI with the dagger/go module, on c8gd.4xlarge machines, each round running the engine with and without the store on one machine, 12 rounds:

Cold first build Without With Change (median of the 12 paired rounds)
Wall time 26.7 s 26.4 s −0.25 s; faster in all 12 rounds (−0.04 to −0.85 s)
Engine CPU 44.6 CPU-s 41.5 −2.9 CPU-s
GC CPU 14.0 CPU-s 12.6 −1.3 CPU-s
Allocated 5.9 GiB, 66 M objects 5.2 GiB, 60 M −0.67 GiB, −6.4 M objects
Peak memory 762 MB 737 MB −26 MB
  • Edits (one file changed, rebuilt) and unchanged rebuilds: no change in wall time or allocations. They make few snapshot calls.
  • Why it's faster: in a profiled build, the engine spends about 40% less time inside containerd's snapshot and lease calls (summed over goroutines); snapshot prepare, view and commit each take about half as long at the median (for example, prepare 424 → 203 µs). The store's own code allocates about 83% less than bbolt did.
  • Context: these runs used an engine carrying other pending allocation changes, with the garbage collection target at 100. An earlier series on an older line, without those changes, measured −0.37 s (12 of 12 rounds faster) and −2.9 CPU-s.
  • Memory: after a cold build of dagger's CLI the store holds about 13 MiB. bbolt kept the same data in a memory-mapped file instead.

How it works

  • Each bucket is a persistent tree. A write transaction copies only the path to what it changes and publishes the new root with one atomic store. Readers take no lock, never wait for the writer and see the state as of their start, as in bbolt. One writer at a time.
  • Reads allocate almost nothing: read transactions share one handle per bucket version.
  • One difference from bbolt: Bucket.Tx() returns nil on a bucket opened in a read transaction. Nothing in containerd or the engine calls it.

How it persists and recovers

  • Graceful stop: Close writes the whole store to its file, as a temporary file renamed over it, with a checksum. The engine already closes both databases at the end of its graceful stop.
  • Start: Open loads the file and marks it in use until Close. A missing or empty file starts an empty store, as bbolt does.
  • Crash or kill: the file is still marked in use, so Open refuses it, and the engine resets its local cache: the same path it takes today when a bolt database fails to open. A damaged file is refused the same way.
  • The overlay snapshotter opens its store on first use, so the engine now opens it at start (making its directory first), to send a refused file through that reset rather than failing the first snapshot call.
  • Upgrading and downgrading: the store's file format isn't bolt's, so the first start after an upgrade, or after a downgrade to an engine that uses bolt, resets the local cache once.
  • Nothing is durable while the engine runs; it doesn't need to be. After a crash the dagql cache is unclean, and the local cache is reset already.

Testing

  • internal/fastbolt's tests, now in CI with the other nested modules (and linted like them): persistence round trips, refusal of unclean and damaged files, several handles to one bucket across writes, shared read handles. They also pass under the race detector.
  • Run outside CI, before this PR:
    • a differential test against real bbolt: 300 random seeds of nested buckets, puts, deletes, cursors, sequences and rollbacks, with handles held across writes and a read transaction open across each write;
    • containerd v2.2.5's own core/metadata (also under the race detector) and core/snapshots/storage test suites, run on the store.
  • An engine smoke test: start and build; a graceful stop saves the store marked clean; a restart reuses it with the build cached and no reset; after a kill, the next start resets the local cache and works.
  • With this branch, on a spot machine: the engine's snapshot (99) and server (487) unit tests, and the cache-persistence (31), local-cache (40), engine (37) and cache (9) integration suites all pass; lint passes.
  • Every engine integration test in CI now runs on the store.

containerd's metadata database and the overlay snapshotter's store both
run on go.etcd.io/bbolt. The engine runs them without fsync, so bolt's
cost is its page work: rereading and rewriting B+tree pages, copying
them for every write transaction, and holding a single write lock while
it does. This package is a store with the subset of bbolt's API that
containerd and the engine use, built for speed instead of durability.

Each bucket is a persistent treap. A write transaction copies only the
path to what it changes and publishes the new root with one atomic
store, so readers take no lock and see the state as of their start, as
in bbolt. Read transactions share one handle per bucket version; a key
and its value are one allocation.

Nothing is durable while it runs. Close writes the whole store to its
file (a temporary file renamed over it, with a checksum), and Open reads
it back and marks the file in use, so a file whose process never closed
it, or a damaged one, is refused at the next Open.

Signed-off-by: Erik Sipsma <[email protected]>
containerd's metadata database, the overlay snapshotter's store and the
engine's own bolt code reach bolt only through go.etcd.io/bbolt, so one
replace moves them all onto the in-memory store. No containerd fork is
needed.

On a cold first build of dagger's CLI the engine spends about half as
long in containerd's snapshot and lease calls, uses about 3 CPU-s less,
allocates about 0.7 GiB and 6 M objects less, and the build finishes
0.2-0.4 s sooner. Edits and unchanged rebuilds don't change.

The store is saved when the engine stops gracefully and loaded when it
starts. After a crash or kill its file is still marked in use, so it is
refused, and the engine resets its local cache, as it does today when a
bolt database fails to open. The overlay snapshotter opens its store on
first use, so the engine now opens it at start (making its directory
first), to send a refused file through that same reset.

The store's own tests run in CI with the other nested modules, and it
is linted like them.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma

sipsma commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Its commits moved into #15007, which now combines the engine's pending performance changes, to save CI runs.

@sipsma

sipsma commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Split back out of #15007 into #15036, unchanged, for its own validation and review.

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