Skip to content

re: buck2-casd, a machine-local CAS daemon every buck2 daemon clones blobs from, on bes-stack - #52

Draft
jun-mercury wants to merge 4 commits into
bes-stackfrom
casd/shared-cas-daemon
Draft

jun-mercury wants to merge 4 commits into
bes-stackfrom
casd/shared-cas-daemon

Conversation

@jun-mercury

Copy link
Copy Markdown

Arian's buck2-casd, arianvp#2, ported onto bes-stack. Each buck2 daemon on a devbox keeps its outputs under buck-out/<isolation-dir>, so every lab worktree and isolation dir downloads and stores its own copy of every blob. With [buck2_re_client] cas_shared_cache = <dir>, one machine-local daemon owns the blob store, and every buck2 daemon clones out of it.

For the consumer. Set cas_shared_cache to a directory on the same filesystem as buck-out. The first buck2 daemon that needs it starts buck2-casd, the binary next to buck2 or cas_shared_cache_binary, on a Unix socket in that directory. It passes the daemon its own cas_address, TLS settings, instance name and digest function. CAS and ByteStream traffic then goes to the daemon, which fetches each blob from the remote once and keeps it as a raw file. buck2 clones those files into buck-out. The engine and the action cache are still reached directly. buck2 killall now stops the daemon too. Its filtered forms should leave it alone, because the daemon's command line has no --isolation-dir. That comes from reading the code, not from a run, since the unfiltered form would stop every session's daemons on this machine. A buck2 daemon whose buck2-casd disappears starts a new one on its next CAS request.

What was ported and what was rewritten. The daemon crate, shared_cache.rs, casd_autostart.rs, the configuration keys, the docs and the tests are Arian's code. The four commits keep him as author. His PR's seven commits include an action cache that was added and then removed, so the net change is split by area instead of replayed. His client integration edits upstream's pool.rs. bes-stack has no such file since 70c6a3996d replaced it with sluongng's sharded client, so the integration is re-implemented on GrpcChannelConnector and GRPCClients:

  • The Unix socket connector and a plaintext override live in GrpcChannelConnector. The override is needed because on bes-stack the tls key wins over the address scheme (e8ba7b2835), so a loopback daemon would otherwise be dialled over TLS.
  • bes-stack connects its channels eagerly, so the daemon starts before the connectors dial.
  • Arian's per-call socket check and five-second probe are gone. A dead daemon now shows up as a broken connection in the existing retry loop. GRPCClients::reconnect starts a successor before it redials the CAS or ByteStream channel, and the request is retried rather than failed.
  • Batch uploads to the daemon go uncompressed. bes-stack picks a batch compressor from the engine's capabilities, and the daemon rejects compressed batch updates. The first live build hit exactly that. The daemon's own upstream client still compresses toward the remote; against Namespace it selected zstd.
  • remote_cache_chunking with cas_shared_cache is refused at connect time, because the daemon implements neither SplitBlob nor SpliceBlob.
  • On the daemon side, ByteStream resource names may now carry the REAPI 2.3 digest function segment (blobs/blake3/...) that bes-stack sends for BLAKE3. The daemon's upstream client is also given the daemon's digest function.

Base. This targets bes-stack, not #51 (credential helper). The two touch the same files, client.rs, buck2_re_configuration and docs/users/remote_execution.md, but in different places. Whichever lands second needs a textual rebase, not a redesign. One interaction to settle then: an auto-started buck2-casd gets http_headers but not credential_helper, so with a short-lived token its upstream connection would go stale while buck2's own connections renew.

Evidence, 2026-09-26. These binaries were built with cargo build --release from this branch, not with buck2.

  • cargo test: remote_execution 110 passed, buck2_casd 9 unit, 3 end-to-end and 1 autostart, buck2_re_configuration 7, buck2_wrapper_common 6. The autostart test starts the real binary from the client and replaces it after SIGTERM and after SIGKILL under a live client. Clippy reports nothing in the touched crates.

  • Against Namespace RE, a scratch project with one remote action writes three incompressible 8 MB files. Two buck2 daemons, --isolation-dir casdA and casdB, shared one cas_shared_cache. A third daemon, casdC in a copy of the project without the key, is the control.

    casdA casdB casdC, control
    build bc62b92c c0a450d1 b6ee7a59
    remote actions 1 1 (different digest: the output path holds the isolation dir) 1
    buck2 "RE downloaded" 50 KiB 50 KiB 23 MiB
    bytes buck2-casd received from Namespace (ss) 24,104,579 +512 n/a

    After casdB the store still held the same three 8 MB files: same inodes, same mtimes, and outputs with identical sha256 in both buck-out trees. So the 24 MB crossed the network once, for two daemons.

  • A SIGKILLed buck2-casd with its socket file left behind was replaced by casdB's next build (build 083ae77b), which succeeded.

Not proven.

  • No disk sharing. This devbox is ext4, so the clone fell back to a copy, logged once. Each buck-out has its own copy next to the store's. The network saving is proven; the disk saving needs btrfs or XFS and was not tested.
  • Only the scratch target. No lab build and no Haskell or Go tree has run through the daemon.
  • No buck2-built binary. Autostart also depends on buck2-casd sitting next to buck2. Nothing packages it there yet, and lab's tools/buck2/pin is unchanged.
  • BLAKE3 end to end, TLS-less remotes, macOS and Windows.

Seen during the port, not changed here.

  • The one-time reflink fallback warning reaches the build console, not only the daemon log.

Posted by Claude Code on behalf of Jun Matsushita.

…ed client

Every buck2 daemon keeps its outputs under buck-out/<isolation-dir>, so
daemons with different isolation dirs, or different checkouts, each
download and store their own copy of every blob. With
`[buck2_re_client] cas_shared_cache = <dir>` the OSS client sends CAS
and ByteStream traffic to buck2-casd, a daemon that owns one blob store
for the machine, and clones blobs out of that directory into buck-out
instead of receiving them over gRPC. For a blob the directory lacks,
the client first reads one byte through the daemon, which makes it
fetch and store the whole blob, and then clones that too. Anything
still missing falls back to gRPC. The client starts the daemon on demand
when nothing answers at its socket, unless
`cas_shared_cache_autostart = false`.

Ported from arianvp#2. bes-stack replaced upstream's pool.rs with
sluongng's sharded client (70c6a39), so the integration is
re-implemented there rather than cherry-picked:

- The Unix socket connector and the plaintext override move from the
  pool into GrpcChannelConnector. bes-stack needs the override because
  its `tls` key wins over the address scheme (e8ba7b2).
- bes-stack connects channels eagerly, so the daemon starts before the
  connectors dial.
- Arian's per-call socket check and five-second probe are gone. A dead
  daemon now surfaces as a broken connection in the existing retry
  loop, and GRPCClients::reconnect starts a successor before redialing
  the CAS or ByteStream channel. The failing request is retried, not
  lost.
- Warm-up reads use bes-stack's resource names, digest function segment
  included.
- The auto-started daemon takes its digest function from the effective
  `digest_algorithms` bes-stack already passes in, not from a second
  parse of `[buck2] digest_algorithms`.
- Batch uploads to the daemon go uncompressed. bes-stack picks a batch
  compressor from the engine's capabilities, and the daemon rejects
  compressed batch updates; its own upstream client compresses for the
  remote. Found in the live test against Namespace.
- The client refuses `remote_cache_chunking` together with
  `cas_shared_cache`, because the daemon implements neither SplitBlob
  nor SpliceBlob.

shared_cache.rs, casd_autostart.rs, the configuration keys and the
download tests are Arian's as written, adjusted for bes-stack's
`Option<bool>` `tls` and its test helpers.

Assisted-by: Claude:claude-opus-5-5
The daemon serves the remote execution API's CAS, ByteStream and
Capabilities services on a Unix socket in its directory, or on a
loopback port, and never listens off the machine. It stores each blob
once as a raw read-only file at <dir>/blobs/<xx>/<hash>-<size>. Raw
files are what lets a buck2 daemon clone a blob whole, as a reflink
where the filesystem supports one. Misses, uploads and FindMissingBlobs
pass through to the upstream CAS via buck2's own OSS client. Concurrent
misses for one digest fetch once, and eviction is LRU by mtime under an
optional size cap.

Ported from arianvp#2; the crate is Arian's. Three changes make it
fit bes-stack. The upstream client leaves TLS to the address scheme
unless `--upstream-tls` is given, matching bes-stack's `Option<bool>`
`tls`. The upstream client gets the daemon's digest function, so it
names blobs the way buck2 does. And ByteStream resource names may carry
the REAPI 2.3 digest function segment (`blobs/blake3/<hash>/<size>`),
which bes-stack sends for BLAKE3. The autostart test now expects the
client's retry, not a timed probe, to replace a killed daemon.

Assisted-by: Claude:claude-opus-5-5
buck2-casd outlives the buck2 daemons that start it, so without this it
runs until reboot, and an upgraded buck2 keeps talking to the old
binary. Recognising it in is_buck2_exe makes killall stop it. The
filtered forms of killall, by isolation dir or repository, still leave
it alone. A live client whose daemon disappears starts a new one on its
next CAS request.

From arianvp#2.

Assisted-by: Claude:claude-opus-5-5
Documents the `cas_shared_cache` keys, the daemon's lifecycle, and the
limit a reader would otherwise rediscover: action digests include the
isolation dir, so two isolation dirs share blob content but never
action cache hits.

From arianvp#2.

Assisted-by: Claude:claude-opus-5-5
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