feat(cache): add shared HTTP action cache and parallel snapshot hashing
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 4s
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 4s
- D1: RemoteCasClient + registry /api/v1/cas endpoints with bearer auth and digest verification, StateStore remote action/blob APIs, build-path-independent action keys, --remote-cache CLI - D2: parallel snapshot revalidation hashing via Void::Concurrency::ThreadPool - Verified in Docker GCC 16: 8/8 CTest suites, cross-build-dir 100% cache hit, 5001-file warm no-op 374 ms - Fix self-host recipe source list for RemoteCache.cpp and accept GCC 16 -lgcc_s_asneeded
This commit is contained in:
1 parent
6c225c2dff
commit
09c0a91d1d
19 files changed
+1213
-18
No files matched your search
@@ -0,0 +1,355 @@
|
||||
#include <Void/Build/State/RemoteCache.hpp>
|
||||
|
||||
#include <Void/Build/Platform/ProcessSupervisor.hpp>
|
||||
#include <Void/Build/Util/Sha256.hpp>
|
||||
#include <Void/Core/Scope.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
#include <Void/Text/Str.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <chrono>
|
||||
#include <format>
|
||||
#include <map>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace Void::Build::State {
|
||||
namespace {
|
||||
|
||||
Void::Result::Error make_error(Void::System::SystemErrorCode code, std::string msg) {
|
||||
return Void::System::make_system_error(code, std::move(msg));
|
||||
}
|
||||
|
||||
bool is_valid_sha256_hex(std::string_view hex) {
|
||||
if (hex.size() != 64) {
|
||||
return false;
|
||||
}
|
||||
return std::ranges::all_of(
|
||||
hex, [](char c) { return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); });
|
||||
}
|
||||
|
||||
struct ParsedUrl {
|
||||
std::string scheme;
|
||||
std::string host;
|
||||
std::uint16_t port{80};
|
||||
std::string base_path;
|
||||
};
|
||||
|
||||
Void::Result::Result<ParsedUrl, Void::Result::Error> parse_remote_url(std::string_view url) {
|
||||
url = Void::Str::trim_ascii(url);
|
||||
const auto sep = url.find("://");
|
||||
if (sep == std::string_view::npos) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"remote cache URL must include http:// or https://")};
|
||||
}
|
||||
ParsedUrl out;
|
||||
out.scheme = Void::Str::to_lower_ascii(url.substr(0, sep));
|
||||
if (out.scheme != "http" && out.scheme != "https") {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"unsupported remote cache URL scheme")};
|
||||
}
|
||||
const auto rest = url.substr(sep + 3);
|
||||
const auto slash = rest.find('/');
|
||||
const auto authority = slash == std::string_view::npos ? rest : rest.substr(0, slash);
|
||||
out.base_path =
|
||||
slash == std::string_view::npos ? std::string{} : std::string{rest.substr(slash)};
|
||||
while (!out.base_path.empty() && out.base_path.back() == '/') {
|
||||
out.base_path.pop_back();
|
||||
}
|
||||
const auto colon = authority.rfind(':');
|
||||
if (colon != std::string_view::npos) {
|
||||
out.host = std::string{authority.substr(0, colon)};
|
||||
int p = 0;
|
||||
if (!Void::Str::to_int(authority.substr(colon + 1), p) || p <= 0 || p > 65535) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"invalid port in remote cache URL")};
|
||||
}
|
||||
out.port = static_cast<std::uint16_t>(p);
|
||||
} else {
|
||||
out.host = std::string{authority};
|
||||
out.port = out.scheme == "https" ? 443 : 80;
|
||||
}
|
||||
if (out.host.empty()) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"empty host in remote cache URL")};
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
struct HttpReply {
|
||||
std::uint16_t status{0};
|
||||
std::string body;
|
||||
};
|
||||
|
||||
bool send_all(int fd, std::string_view data) {
|
||||
std::size_t off = 0;
|
||||
while (off < data.size()) {
|
||||
const ssize_t n = ::send(fd, data.data() + off, data.size() - off, MSG_NOSIGNAL);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
off += static_cast<std::size_t>(n);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Void::Result::Result<HttpReply, Void::Result::Error>
|
||||
http_exchange(const ParsedUrl &parsed, std::string_view method, std::string_view path,
|
||||
const std::map<std::string, std::string, std::less<>> &headers,
|
||||
std::string_view body) {
|
||||
const std::string full_path = parsed.base_path + std::string{path};
|
||||
|
||||
if (parsed.scheme == "http") {
|
||||
struct addrinfo hints{};
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
struct addrinfo *res = nullptr;
|
||||
const std::string port_s = std::to_string(parsed.port);
|
||||
if (::getaddrinfo(parsed.host.c_str(), port_s.c_str(), &hints, &res) != 0 || !res) {
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error,
|
||||
std::format("remote cache: failed resolving host '{}'", parsed.host))};
|
||||
}
|
||||
auto free_ai = Void::Scope::on_exit([&] { ::freeaddrinfo(res); });
|
||||
|
||||
const int fd = ::socket(res->ai_family, res->ai_socktype | SOCK_CLOEXEC, res->ai_protocol);
|
||||
if (fd < 0) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
|
||||
"remote cache: socket() failed")};
|
||||
}
|
||||
Void::System::OwnedFd guard{fd};
|
||||
|
||||
struct timeval tv{60, 0};
|
||||
(void)::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||
(void)::setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
||||
|
||||
if (::connect(fd, res->ai_addr, res->ai_addrlen) != 0) {
|
||||
return std::unexpected{make_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
std::format("remote cache: connect to {}:{} failed", parsed.host, parsed.port))};
|
||||
}
|
||||
|
||||
std::string req = std::format("{} {} HTTP/1.1\r\nHost: {}:{}\r\nConnection: close\r\n"
|
||||
"Content-Length: {}\r\n",
|
||||
method, full_path, parsed.host, parsed.port, body.size());
|
||||
for (const auto &[k, v] : headers) {
|
||||
req += k + ": " + v + "\r\n";
|
||||
}
|
||||
req += "\r\n";
|
||||
req += body;
|
||||
if (!send_all(fd, req)) {
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error, "remote cache: send() failed")};
|
||||
}
|
||||
|
||||
std::string raw;
|
||||
std::array<char, 16384> buf{};
|
||||
for (;;) {
|
||||
const ssize_t n = ::recv(fd, buf.data(), buf.size(), 0);
|
||||
if (n > 0) {
|
||||
raw.append(buf.data(), static_cast<std::size_t>(n));
|
||||
} else if (n == 0) {
|
||||
break;
|
||||
} else if (errno != EINTR) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
|
||||
"remote cache: recv() failed")};
|
||||
}
|
||||
}
|
||||
|
||||
const auto hdr_end = raw.find("\r\n\r\n");
|
||||
if (hdr_end == std::string::npos) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
|
||||
"remote cache: malformed HTTP response")};
|
||||
}
|
||||
HttpReply reply;
|
||||
const auto first_crlf = raw.find("\r\n");
|
||||
const std::string_view status_line{
|
||||
raw.data(), first_crlf == std::string::npos ? raw.size() : first_crlf};
|
||||
const auto sp1 = status_line.find(' ');
|
||||
if (sp1 != std::string_view::npos) {
|
||||
int code = 0;
|
||||
if (Void::Str::to_int(status_line.substr(sp1 + 1, 3), code)) {
|
||||
reply.status = static_cast<std::uint16_t>(code);
|
||||
}
|
||||
}
|
||||
reply.body = raw.substr(hdr_end + 4);
|
||||
return reply;
|
||||
}
|
||||
|
||||
// HTTPS via curl preserves TLS certificate verification (never disables TLS).
|
||||
auto curl = Platform::resolve_executable("curl");
|
||||
if (!curl) {
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::not_found,
|
||||
"HTTPS remote cache requires curl (Void::Net not linked)")};
|
||||
}
|
||||
const std::string full_url =
|
||||
std::format("https://{}:{}{}", parsed.host, parsed.port, full_path);
|
||||
Platform::ProcessRequest req;
|
||||
req.executable = *curl;
|
||||
req.arguments = {"--silent", "--show-error", "--proto", "=https",
|
||||
"-X", std::string{method}, "-w", "\n%{http_code}"};
|
||||
for (const auto &[k, v] : headers) {
|
||||
req.arguments.push_back("-H");
|
||||
req.arguments.push_back(k + ": " + v);
|
||||
}
|
||||
if (!body.empty()) {
|
||||
req.arguments.push_back("--data-binary");
|
||||
req.arguments.push_back("@-");
|
||||
req.stdin_data = std::string{body};
|
||||
}
|
||||
req.arguments.push_back(full_url);
|
||||
req.timeout = std::chrono::seconds{120};
|
||||
auto proc = Platform::run_supervised(req);
|
||||
if (!proc || !proc->succeeded()) {
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error,
|
||||
std::format("remote cache HTTPS request failed: {}",
|
||||
proc ? proc->stderr_data : proc.error().message()))};
|
||||
}
|
||||
HttpReply reply;
|
||||
const auto nl = proc->stdout_data.rfind('\n');
|
||||
if (nl != std::string::npos) {
|
||||
int code = 0;
|
||||
if (Void::Str::to_int(Void::Str::trim_ascii(proc->stdout_data.substr(nl + 1)), code)) {
|
||||
reply.status = static_cast<std::uint16_t>(code);
|
||||
}
|
||||
reply.body = proc->stdout_data.substr(0, nl);
|
||||
}
|
||||
return reply;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string, std::less<>> auth_headers(const RemoteCacheConfig &config) {
|
||||
std::map<std::string, std::string, std::less<>> headers;
|
||||
if (!config.bearer_token.empty()) {
|
||||
headers["Authorization"] = "Bearer " + config.bearer_token;
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
Void::Result::Result<ParsedUrl, Void::Result::Error> require_url(const RemoteCacheConfig &config) {
|
||||
if (config.url.empty()) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"remote cache is not configured")};
|
||||
}
|
||||
return parse_remote_url(config.url);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
RemoteCasClient::RemoteCasClient(RemoteCacheConfig config) : config_{std::move(config)} {}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
RemoteCasClient::put_blob(std::string_view sha256, std::string_view bytes) const {
|
||||
if (!is_valid_sha256_hex(sha256)) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"remote cache: invalid blob SHA-256")};
|
||||
}
|
||||
auto parsed = require_url(config_);
|
||||
if (!parsed) {
|
||||
return std::unexpected{parsed.error()};
|
||||
}
|
||||
auto headers = auth_headers(config_);
|
||||
headers["Content-Type"] = "application/octet-stream";
|
||||
const std::string path = "/api/v1/cas/blobs/" + std::string{sha256};
|
||||
auto reply = http_exchange(*parsed, "PUT", path, headers, bytes);
|
||||
if (!reply) {
|
||||
return std::unexpected{reply.error()};
|
||||
}
|
||||
if (reply->status != 200 && reply->status != 201) {
|
||||
return std::unexpected{make_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
std::format("remote cache: blob upload failed with HTTP {}", reply->status))};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::optional<std::string>, Void::Result::Error>
|
||||
RemoteCasClient::get_blob(std::string_view sha256) const {
|
||||
if (!is_valid_sha256_hex(sha256)) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"remote cache: invalid blob SHA-256")};
|
||||
}
|
||||
auto parsed = require_url(config_);
|
||||
if (!parsed) {
|
||||
return std::unexpected{parsed.error()};
|
||||
}
|
||||
const std::string path = "/api/v1/cas/blobs/" + std::string{sha256};
|
||||
auto reply = http_exchange(*parsed, "GET", path, auth_headers(config_), {});
|
||||
if (!reply) {
|
||||
return std::unexpected{reply.error()};
|
||||
}
|
||||
if (reply->status == 404) {
|
||||
return std::optional<std::string>{};
|
||||
}
|
||||
if (reply->status != 200) {
|
||||
return std::unexpected{make_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
std::format("remote cache: blob download failed with HTTP {}", reply->status))};
|
||||
}
|
||||
if (Util::sha256_hex(reply->body) != sha256) {
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"remote cache: downloaded blob failed SHA-256 verification (discarded)")};
|
||||
}
|
||||
return std::optional<std::string>{std::move(reply->body)};
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
RemoteCasClient::put_action(std::string_view action_key, std::string_view json) const {
|
||||
if (!is_valid_sha256_hex(action_key)) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"remote cache: invalid action key")};
|
||||
}
|
||||
auto parsed = require_url(config_);
|
||||
if (!parsed) {
|
||||
return std::unexpected{parsed.error()};
|
||||
}
|
||||
auto headers = auth_headers(config_);
|
||||
headers["Content-Type"] = "application/json";
|
||||
const std::string path = "/api/v1/cas/actions/" + std::string{action_key};
|
||||
auto reply = http_exchange(*parsed, "PUT", path, headers, json);
|
||||
if (!reply) {
|
||||
return std::unexpected{reply.error()};
|
||||
}
|
||||
if (reply->status != 200 && reply->status != 201) {
|
||||
return std::unexpected{make_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
std::format("remote cache: action upload failed with HTTP {}", reply->status))};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::optional<std::string>, Void::Result::Error>
|
||||
RemoteCasClient::get_action(std::string_view action_key) const {
|
||||
if (!is_valid_sha256_hex(action_key)) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"remote cache: invalid action key")};
|
||||
}
|
||||
auto parsed = require_url(config_);
|
||||
if (!parsed) {
|
||||
return std::unexpected{parsed.error()};
|
||||
}
|
||||
const std::string path = "/api/v1/cas/actions/" + std::string{action_key};
|
||||
auto reply = http_exchange(*parsed, "GET", path, auth_headers(config_), {});
|
||||
if (!reply) {
|
||||
return std::unexpected{reply.error()};
|
||||
}
|
||||
if (reply->status == 404) {
|
||||
return std::optional<std::string>{};
|
||||
}
|
||||
if (reply->status != 200) {
|
||||
return std::unexpected{make_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
std::format("remote cache: action download failed with HTTP {}", reply->status))};
|
||||
}
|
||||
return std::optional<std::string>{std::move(reply->body)};
|
||||
}
|
||||
|
||||
} // namespace Void::Build::State
|
||||
@@ -512,6 +512,102 @@ StateStore::materialize_blob(const BlobRef &ref, const std::filesystem::path &de
|
||||
return {};
|
||||
}
|
||||
|
||||
void StateStore::configure_remote_cache(std::optional<RemoteCacheConfig> config) {
|
||||
if (config && !config->url.empty()) {
|
||||
remote_.emplace(std::move(*config));
|
||||
} else {
|
||||
remote_.reset();
|
||||
}
|
||||
}
|
||||
|
||||
Void::Result::Result<std::optional<std::string>, Void::Result::Error>
|
||||
StateStore::fetch_remote_action(std::string_view action_key) const {
|
||||
if (!remote_) {
|
||||
return std::optional<std::string>{};
|
||||
}
|
||||
return remote_->get_action(action_key);
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
StateStore::publish_remote_action(std::string_view action_key, std::string_view json) const {
|
||||
if (!remote_) {
|
||||
return {};
|
||||
}
|
||||
return remote_->put_action(action_key, json);
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
StateStore::publish_remote_blob(std::string_view sha256) const {
|
||||
if (!remote_) {
|
||||
return {};
|
||||
}
|
||||
if (!is_valid_sha256_hex(sha256)) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"StateStore: invalid CAS blob digest")};
|
||||
}
|
||||
auto bytes = Void::FileSystem::read_file(cas_blob_path(sha256));
|
||||
if (!bytes) {
|
||||
return std::unexpected{bytes.error()};
|
||||
}
|
||||
if (Util::sha256_hex(*bytes) != sha256) {
|
||||
return std::unexpected{make_error(
|
||||
Void::System::SystemErrorCode::invalid_argument,
|
||||
std::format("StateStore: refusing to upload corrupted CAS blob '{}'", sha256))};
|
||||
}
|
||||
return remote_->put_blob(sha256, *bytes);
|
||||
}
|
||||
|
||||
Void::Result::Result<bool, Void::Result::Error>
|
||||
StateStore::ensure_remote_blob(std::string_view sha256) {
|
||||
if (!is_valid_sha256_hex(sha256)) {
|
||||
return false;
|
||||
}
|
||||
auto local = verify_blob(sha256);
|
||||
if (!local) {
|
||||
return std::unexpected{local.error()};
|
||||
}
|
||||
if (*local) {
|
||||
return true;
|
||||
}
|
||||
if (!remote_) {
|
||||
return false;
|
||||
}
|
||||
auto fetched = remote_->get_blob(sha256);
|
||||
if (!fetched) {
|
||||
return std::unexpected{fetched.error()};
|
||||
}
|
||||
if (!fetched->has_value()) {
|
||||
return false;
|
||||
}
|
||||
const std::string &bytes = **fetched;
|
||||
if (Util::sha256_hex(bytes) != sha256) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto final_path = cas_blob_path(sha256);
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(final_path.parent_path(), ec);
|
||||
const auto stage_path =
|
||||
staging_dir_ / std::format("remote_{}_{}.stage", sha256,
|
||||
g_lease_seq.fetch_add(1, std::memory_order_relaxed));
|
||||
auto wrote = Platform::atomic_write_file(stage_path, bytes, true, 0644);
|
||||
if (!wrote) {
|
||||
return std::unexpected{wrote.error()};
|
||||
}
|
||||
(void)::chmod(stage_path.c_str(), 0444);
|
||||
std::filesystem::rename(stage_path, final_path, ec);
|
||||
if (ec) {
|
||||
std::filesystem::remove(stage_path, ec);
|
||||
if (!std::filesystem::exists(final_path)) {
|
||||
return std::unexpected{make_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
std::format("StateStore: failed publishing remote CAS blob '{}'", sha256))};
|
||||
}
|
||||
}
|
||||
(void)Platform::fsync_directory(final_path.parent_path());
|
||||
return true;
|
||||
}
|
||||
|
||||
Void::Result::Result<std::optional<ActionSuccessRecord>, Void::Result::Error>
|
||||
StateStore::lookup_action(std::string_view action_key) const {
|
||||
std::optional<ActionSuccessRecord> found;
|
||||
|
||||
Reference in new issue
Block a user