Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ jobs:
run: ctest --test-dir build --output-on-failure

# The same remote cache test, against a real server instead of the test one: over http,
# without and with compressed uploads, then over https with Basic authentication (the certificate of tests/integration/tls and an
# without and with compressed uploads, and over its gRPC interface; then over https and
# grpcs with Basic authentication (the certificate of tests/integration/tls and an
# htpasswd file). The image is pinned by digest.
- name: Test the remote cache against bazel-remote
if: matrix.llvm == '21'
Expand All @@ -182,26 +183,31 @@ jobs:
done
curl -sf "$@" > /dev/null
}
docker run -d --rm --name bazel-remote -p 127.0.0.1:19090:8080 "$BAZEL_REMOTE_IMAGE" --max_size=1 --dir=/data
docker run -d --rm --name bazel-remote -p 127.0.0.1:19090:8080 -p 127.0.0.1:19092:9092 "$BAZEL_REMOTE_IMAGE" --max_size=1 --dir=/data
wait_for http://127.0.0.1:19090/status
CPPMCACHE_TEST_REMOTE=http://127.0.0.1:19090 ctest --test-dir build -R 'integration\.remote\.plain' --output-on-failure
# Again with zstd-compressed uploads, which bazel-remote decodes (and downloads are asked for
# compressed in both runs).
CPPMCACHE_REMOTE_COMPRESSION=zstd CPPMCACHE_TEST_REMOTE=http://127.0.0.1:19090 \
ctest --test-dir build -R 'integration\.remote\.plain' --output-on-failure
CPPMCACHE_TEST_REMOTE=grpc://127.0.0.1:19092 ctest --test-dir build -R 'integration\.remote\.plain' --output-on-failure
docker logs bazel-remote 2>&1 | grep -c ' GRPC ' | xargs echo "gRPC calls served by bazel-remote:"
docker logs bazel-remote 2>&1 | grep -cE ' (GET|PUT|HEAD) ' | xargs echo "requests served by bazel-remote:"
docker rm -f bazel-remote

secrets="${RUNNER_TEMP}/bazel-remote-secrets"
mkdir -p "$secrets"
printf '%s:%s\n' "$TEST_USER" "$(openssl passwd -apr1 -salt cppmcache "$TEST_PASSWORD")" > "${secrets}/htpasswd"
docker run -d --rm --name bazel-remote-tls -p 127.0.0.1:19091:8080 \
docker run -d --rm --name bazel-remote-tls -p 127.0.0.1:19091:8080 -p 127.0.0.1:19093:9092 \
-v "${PWD}/tests/integration/tls:/tls:ro" -v "${secrets}:/secrets:ro" "$BAZEL_REMOTE_IMAGE" --max_size=1 --dir=/data \
--tls_cert_file=/tls/server.pem --tls_key_file=/tls/server.key --htpasswd_file=/secrets/htpasswd
wait_for --cacert tests/integration/tls/ca.pem -u "${TEST_USER}:${TEST_PASSWORD}" https://127.0.0.1:19091/status
CPPMCACHE_TEST_REMOTE=https://127.0.0.1:19091 CPPMCACHE_TEST_CA="${PWD}/tests/integration/tls/ca.pem" \
CPPMCACHE_TEST_USER="$TEST_USER" CPPMCACHE_TEST_PASSWORD="$TEST_PASSWORD" \
ctest --test-dir build -R 'integration\.remote\.https-basic' --output-on-failure
CPPMCACHE_TEST_REMOTE=grpcs://127.0.0.1:19093 CPPMCACHE_TEST_CA="${PWD}/tests/integration/tls/ca.pem" \
CPPMCACHE_TEST_USER="$TEST_USER" CPPMCACHE_TEST_PASSWORD="$TEST_PASSWORD" \
ctest --test-dir build -R 'integration\.remote\.https-basic' --output-on-failure
docker logs bazel-remote-tls 2>&1 | grep -cE ' (GET|PUT|HEAD) ' | xargs echo "requests served by bazel-remote over https:"
docker rm -f bazel-remote-tls

Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ target_sources(cppmcache_core PUBLIC FILE_SET CXX_MODULES BASE_DIRS src FILES
src/cppmcache/Driver.cppm
src/cppmcache/Process.cppm
src/cppmcache/Http.cppm
src/cppmcache/Hpack.cppm
src/cppmcache/Grpc.cppm
src/cppmcache/Remote.cppm
src/cppmcache/Cache.cppm)
cppmcache_configure_target(cppmcache_core)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ PDB shared by the compilations of a target. Configure with
| To share between… | Set |
|-----------------------------|------------------------------------------------------------------|
| checkouts, or CI runners | `CPPMCACHE_BASEDIR` to the root of the project: paths under it become relative |
| machines | `CPPMCACHE_REMOTE=https://host[:port]` (or `http://`), a [bazel-remote](https://github.com/buchgr/bazel-remote) or any server of Bazel's HTTP caching protocol |
| machines | `CPPMCACHE_REMOTE=https://host[:port]` (or `http://`), a [bazel-remote](https://github.com/buchgr/bazel-remote) or any server of Bazel's HTTP caching protocol; `grpcs://host[:port][/instance]` (or `grpc://`) for a server of the Remote Execution API's gRPC interface |
| machines with different compiler dates | `CPPMCACHE_COMPILERCHECK=content` |

Over `https://` the server's certificate is checked against the system's roots, and credentials
Expand All @@ -127,7 +127,7 @@ come from the environment or from files, never from the URL. Details are in
| `CPPMCACHE_MAXSIZE` | size of the local cache beyond which the entries used least recently are evicted: `500M`, `5G`, `0` for no limit (default: `5G`, powers of 1024) |
| `CPPMCACHE_BASEDIR` | absolute directory under which paths are made relative |
| `CPPMCACHE_COMPILERCHECK` | `content`: identify the compiler by the digest of its executable |
| `CPPMCACHE_REMOTE` | URL of a remote cache, `http://` or `https://` |
| `CPPMCACHE_REMOTE` | URL of a remote cache: `http://`, `https://`, `grpc://` or `grpcs://` |
| `CPPMCACHE_REMOTE_TOKEN` | bearer token sent to the remote cache (or `CPPMCACHE_REMOTE_TOKEN_FILE`, a file that holds it) |
| `CPPMCACHE_REMOTE_USER`, `CPPMCACHE_REMOTE_PASSWORD` | Basic authentication with the remote cache (or `CPPMCACHE_REMOTE_PASSWORD_FILE`) |
| `CPPMCACHE_REMOTE_CA_CERT` | PEM file of the only roots trusted over `https://` (default: the system's) |
Expand Down
26 changes: 24 additions & 2 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,28 @@ export CPPMCACHE_REMOTE=http://localhost:8080
understand that encoding: it would store the compressed bytes, which every client would then
refuse as not matching their digest.

### The gRPC interface

A `grpc://` or `grpcs://` URL makes cppmcache speak the gRPC interface of the
[Remote Execution API](https://github.com/bazelbuild/remote-apis) instead, as servers such as
BuildBuddy, Buildbarn or bazel-remote (on its gRPC port, 9092 by default) offer it:

```sh
export CPPMCACHE_REMOTE=grpcs://cache.example.com/my-instance
```

- `grpcs://` is gRPC over TLS (ALPN `h2`), with the same server authentication and credentials as
`https://` below; `grpc://` is HTTP/2 without TLS. The default port is 443 for `grpcs://`, 80 for
`grpc://`: a server on 9092 needs it written.
- The path, if any, is the instance name (`my-instance` above).
- Results and blobs go through ActionCache.GetActionResult and UpdateActionResult,
ContentAddressableStorage.FindMissingBlobs, BatchReadBlobs and BatchUpdateBlobs, and
ByteStream.Read and Write for blobs above 3 MiB. A manifest is stored as an ActionResult, as over
HTTP, under a digest whose size is a nominal 1.
- Blobs travel uncompressed, and the client is written in this project (HTTP/2 and HPACK
included) rather than taken from the gRPC libraries, which would weigh on every project that
imports cppmcache.

### TLS and credentials

An `https://` URL makes every connection TLS 1.3, and the server is authenticated with the Web PKI:
Expand Down Expand Up @@ -588,8 +610,8 @@ only, and the configure step says so. `openssl` or `schannel` require one, and `
prints for `--version`, which is part of the key. A program of that name that only wraps a
compiler and adds options of its own, chosen by its environment, would not be seen: the key
would not follow those options. A script is always passed through.
- The remote cache speaks HTTP, not the gRPC interface of the Remote Execution API. A miss waits
for its uploads, which a parallel build hides ([measurements](measurements.md#remote-cache-across-a-network)).
- Over gRPC, blobs are not compressed, and calls go one after the other on the connection. A miss
waits for its uploads, which a parallel build hides ([measurements](measurements.md#remote-cache-across-a-network)).
Each compilation opens its own connection, so over `https://` each one also makes a TLS
handshake; revocation of the server's certificate is not checked.
- A restored dependency file is rewritten, not copied: the same dependencies, laid out differently.
16 changes: 10 additions & 6 deletions src/cppmcache/Cache.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,10 @@ struct Context {
// Name, in an ActionResult stored under a manifest key, of the output that holds the manifest.
constexpr std::string_view manifestOutput = "manifest";

// A manifest key names no action, so its digest has no size of its own. The gRPC interface wants
// one, and servers refuse zero for a hash that is not that of the empty blob: it gets a nominal 1.
constexpr std::int64_t manifestKeySize = 1;

// Content of a blob, from the local CAS or else from the remote cache. A blob that came from the
// remote cache is checked against its digest and kept locally.
std::optional<std::string> fetchBlob(Context& context, const Digest& digest) {
Expand Down Expand Up @@ -575,7 +579,7 @@ std::optional<std::string> remoteManifest(Context& context) {
if (context.remote == nullptr) {
return std::nullopt;
}
const auto encoded = context.remote->get("ac", context.manifestKey);
const auto encoded = context.remote->get("ac", context.manifestKey, manifestKeySize);
const auto result = encoded ? ActionResult::decode(*encoded) : std::nullopt;
if (!result) {
return std::nullopt;
Expand Down Expand Up @@ -996,7 +1000,7 @@ std::optional<int> serve(Context& context, const ManifestEntry& entry) {
auto resultBytes = store.actionGet(actionDigest);
bool fromRemote = false;
if (!resultBytes && context.remote != nullptr) {
resultBytes = context.remote->get("ac", actionDigest.hash);
resultBytes = context.remote->get("ac", actionDigest.hash, actionDigest.size);
fromRemote = resultBytes.has_value();
}
const auto result = resultBytes ? ActionResult::decode(*resultBytes) : std::nullopt;
Expand Down Expand Up @@ -1108,9 +1112,9 @@ void upload(Context& context, const std::vector<ManifestEntry>& localEntries, co

std::vector<Operation> probe;
for (const auto& file : result.outputFiles) {
probe.push_back(RemoteCache::checking("cas", file.digest.hash));
probe.push_back(RemoteCache::checking("cas", file.digest.hash, file.digest.size));
}
probe.push_back(RemoteCache::getting("ac", context.manifestKey));
probe.push_back(RemoteCache::getting("ac", context.manifestKey, manifestKeySize));
remote.run(probe);

// The server's manifest, from the local CAS when this machine has it.
Expand Down Expand Up @@ -1189,14 +1193,14 @@ void upload(Context& context, const std::vector<ManifestEntry>& localEntries, co
holder.outputFiles.push_back({std::string{manifestOutput}, digestOf(manifest)});
const std::string encodedResult = result.encode();
const std::string encodedHolder = holder.encode();
std::array records{RemoteCache::putting("ac", actionDigest.hash, encodedResult),
std::array records{RemoteCache::putting("ac", actionDigest.hash, encodedResult, actionDigest.size),
RemoteCache::putting("cas", holder.outputFiles.front().digest.hash, manifest)};
remote.run(records);
if (!std::ranges::all_of(records, &Operation::found)) {
return;
}
// Only now: an entry pointing to a manifest the server lacks would hide the entries it had.
if (remote.put("ac", context.manifestKey, encodedHolder)) {
if (remote.put("ac", context.manifestKey, encodedHolder, manifestKeySize)) {
context.store.count("remote_store");
}
}
Expand Down
Loading
Loading