Skip to content

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

Merged
sipsma merged 8 commits into
mainfrom
sipsma/containerd-metadata-in-memory
Nov 2, 2026
Merged

sipsma merged 8 commits into
mainfrom
sipsma/containerd-metadata-in-memory

Conversation

@sipsma

@sipsma sipsma commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

This is #14965's change, unchanged, now on its own PR on today's main. #14965 was folded into #15007, the engine performance batch, and closed. It's split back out because replacing containerd's metadata store needs validation and review of its own.

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 through the path it takes today when a bolt database fails to open. A crash already resets the local cache today, through the dagql cache's unclean-shutdown check, so this adds no reset. A damaged nonempty file is refused the same way. This covers the engine process crashing or being killed, not the host: like the rest of the engine's state, the store isn't synced to disk, so a host crash or power loss can leave an older or empty file, which the next start accepts.
  • 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.

Also in this PR

  • fuse-overlayfs support is removed. Engines without overlayfs use the native snapshotter, as they already did where fuse-overlayfs was missing. A config that names fuse-overlayfs now gets the engine's unknown-snapshotter error and the engine doesn't start.
  • The native snapshotter works with the store. engine: run containerd's metadata on an in-memory store #14965's version opened the snapshotter's store at start for every snapshotter, so native, which opens its own store on the same file, found it marked in use and every snapshot operation failed. The engine now hands the store only to the built-in overlayfs snapshotter, and a graceful stop closes the snapshotter so native's store is saved too.
  • A start that fails for a reason a reset can't fix, such as an unknown snapshotter name, closes the stores it opened, so the next start keeps the local cache instead of resetting it.

On a fresh engine taking in the remote cache's results

Measured by Remote Cache's work on cheap installs (Nov 1), one profiled run per arm: a fresh engine installing dagger/dagger's test-base results from the remote cache. These are times summed across concurrent operations, not wall time, and there is no wall-time pair.

Summed time Without the store With it
Metadata writes 225 s 156 s
Snapshot prepare 59 s 16 s
Snapshot commit 34 s 13 s
Snapshot view 33 s 16 s
Snapshot usage 34 s 8.5 s
The install's commit 397 s 296 s

With the store in place, containerd's metadata database is still the largest lock in a fresh engine's first 15 s: 9.8–12.1 s of contention summed, from two serialized transactions per fetched blob (two diagnostic profiles, Nov 1). So the store cuts the time spent in metadata work, but its single writer still serializes it.

Validation

Run for this PR, on spot machines:

  • A new CI check, test-split/test-fastbolt, passes. CI never ran the store's own tests before, since it's a separate Go module. The check runs:
    • the store's tests with the race detector, including a save that fills a 1 MiB filesystem: the save fails, leaves no partial file, and the next open refuses the file;
    • a differential test against the real bbolt v1.4.3, 300 random sequences of 40 transactions each, requiring the same data after every commit and rollback;
    • a concurrent-reader test with the race detector, which requires readers to read during 5,000 writes and checks the final values;
    • containerd's own core/metadata and core/snapshots/storage tests on the store, at the engine's containerd version, the metadata tests also with the race detector at 1, 4 and 16 CPUs.
  • Two new engine tests pass: an engine on the native snapshotter keeps its cache across a clean restart and resets after a kill; and a start that fails on an unknown snapshotter leaves the cache for the next start.
  • Upgrade and downgrade: main's engine, then this PR's, then this PR's again, then main's, on one state volume. Each engine worked. This PR's first start logged one reset, refusing main's bolt file, and main's start after this PR reset too. The harness ends each engine with a kill rather than a graceful stop, so it can't show a graceful restart keeping the cache; the restart tests above cover that.
  • Scale: 100,000 snapshots and 100,000 leases through containerd's metadata on the store held 193 MiB of heap for both stores. Saving them at stop took 244 ms and 170 ms, into files of 18.4 and 15.8 MiB. Loading them at start took 215 ms, and all came back.
  • Race detector on the engine's integration suites: a race-built engine stops itself on a lock checker inside containerd's CNI library, on main as well, so those suites couldn't run that way. The race-detector tests above stand in for them.
  • Review: two independent reviews. They found the native snapshotter problem, a proxy snapshotter named overlayfs keeping the store open, a concurrency test that could pass without reading, and the failed-start gap, all fixed here, and no concurrency defects in the store.

Earlier validation (from #14965)

  • internal/fastbolt's own tests: persistence round trips, refusal of unclean and damaged files, several handles to one bucket across writes, shared read handles. They pass under the race detector, and they're added to CI's lists of nested modules to test and lint.
  • Run outside CI, before engine: run containerd's metadata on an in-memory store #14965:
    • 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.
  • The engine's own restart tests in CI's cache-persistence suite run on the store and pass: the cache survives a graceful restart, and an unclean shutdown resets it.
  • On a spot machine with engine: run containerd's metadata on an in-memory store #14965's branch: 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, and lint passes.
  • engine: run containerd's metadata on an in-memory store #14965's CI was green on its last head (91 checks), and every engine integration test in CI runs on the store.

Not validated yet

  • Long-lived engines. No run has kept an engine up for days.
  • Crashes at other points. The restart tests kill the engine once, between builds. A crash during the save at graceful stop, or a stop that times out before the save finishes, isn't tested. Both should leave a file the next start refuses, and so reset the local cache.

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]>
The engine opens the snapshotter's store at start, so a database its
last engine never closed resets the local cache. Only overlayfs uses
that handle. The native snapshotter opens its own store on the same
file, and found it marked in use by the engine's handle, so every
snapshot operation failed.

After choosing the snapshotter, the engine now closes its handle unless
the snapshotter is overlayfs. The check at start still happens first,
so a crashed file still resets the local cache. A graceful stop now
closes the snapshotter, which closes the store it uses, overlayfs the
engine's and native its own, so native's store is saved too.

The new test runs an engine on the native snapshotter: a build, a clean
restart that keeps the cache, and a kill whose next start resets it.

Signed-off-by: Erik Sipsma <[email protected]>
The engine never supported fuse-overlayfs in practice. It only picked it
when overlayfs was unavailable and fuse-overlayfs happened to be. An
engine without overlayfs now uses the native snapshotter, as it already
did where fuse-overlayfs was missing. A config that names
fuse-overlayfs gets the engine's error for an unknown snapshotter, and
the engine doesn't start.

The export path's and layercopy's special cases for fuse-overlayfs
mounts go with it, and so does the module.

Signed-off-by: Erik Sipsma <[email protected]>
…containerd's metadata tests

internal/fastbolt is its own Go module, so the package-test shard, which
lists the root module's packages, never ran its tests, and nothing else
in CI did. The new testFastbolt check runs:

- its own tests with the race detector, including a save that fills a
  small filesystem: the save fails, leaves no partial file, and the next
  open refuses the file instead of reading part of a save;
- a differential test that drives it and the real bbolt through the
  same random operations and requires the same data after every
  transaction, and a concurrent-reader test with the race detector;
- containerd's own core/metadata and core/snapshots/storage tests at the
  engine's containerd version, on the store, the metadata tests also with
  the race detector at 1, 4 and 16 CPUs.

Signed-off-by: Erik Sipsma <[email protected]>
A start that failed for a reason a local cache reset can't fix, such as
an unknown snapshotter name, returned without closing the metadata
stores it had opened. With the in-memory store they stay marked in use,
so the next start, with the config fixed, reset an otherwise clean
local cache.

Such a failure now closes the local cache state the way the reset path
does, so the unchanged stores are saved clean.

The new test starts an engine, fails a start with an unknown
snapshotter, then requires the next start to keep the first engine's
cache.

Signed-off-by: Erik Sipsma <[email protected]>
The differential test's go.mod replaces realbolt with ./realbolt, which
the testFastbolt check fills with a copy of the real bbolt. Without the
directory, the Go scanners that read every module in the workspace,
such as golangci-lint's, fail on the replace. A placeholder module there
keeps them working, and the check replaces it whole.

Signed-off-by: Erik Sipsma <[email protected]>
…shotter

The graceful stop now closes the snapshotter, which closes the store it
uses. A server stopped before it had a snapshotter, as the graceful stop
tests build one, has none, and the stop dereferenced it. Without a
snapshotter, the stop closes the engine's store directly, as it did
before.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the sipsma/containerd-metadata-in-memory branch from 1ee2f99 to a832069 Compare November 2, 2026 21:51
@sipsma
sipsma merged commit 52ad3ff into main Nov 2, 2026
94 checks passed
@sipsma
sipsma deleted the sipsma/containerd-metadata-in-memory branch November 2, 2026 22:14
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.

2 participants