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
@@ -3,7 +3,9 @@
|
||||
#include <Void/Async/ThreadPool.hpp>
|
||||
#include <Void/Build/Platform/FileOps.hpp>
|
||||
#include <Void/Build/Platform/ProcessSupervisor.hpp>
|
||||
#include <Void/Build/Util/Json.hpp>
|
||||
#include <Void/Build/Util/Sha256.hpp>
|
||||
#include <Void/Data/StrictJson.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/System/Path.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
@@ -126,6 +128,256 @@ bool restore_outputs_from_cache(const Graph::ActionNode &action,
|
||||
return true;
|
||||
}
|
||||
|
||||
// Build-path-independent path mapping used for shared remote cache keys and records.
|
||||
// Longest prefix wins so nested build/root prefixes are replaced before their parents.
|
||||
struct PathMapper {
|
||||
std::vector<std::pair<std::string, std::string>> pairs;
|
||||
|
||||
[[nodiscard]] std::string normalize(std::string_view input) const {
|
||||
std::string out{input};
|
||||
for (const auto &[prefix, token] : pairs) {
|
||||
std::size_t pos = 0;
|
||||
while ((pos = out.find(prefix, pos)) != std::string::npos) {
|
||||
out.replace(pos, prefix.size(), token);
|
||||
pos += token.size();
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string denormalize(std::string_view input) const {
|
||||
std::string out{input};
|
||||
for (const auto &[prefix, token] : pairs) {
|
||||
std::size_t pos = 0;
|
||||
while ((pos = out.find(token, pos)) != std::string::npos) {
|
||||
out.replace(pos, token.size(), prefix);
|
||||
pos += prefix.size();
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
PathMapper make_path_mapper(const Graph::BuildGraph &graph) {
|
||||
std::vector<std::pair<std::string, std::string>> pairs;
|
||||
auto add = [&](const std::filesystem::path &path, std::string token) {
|
||||
if (!path.empty()) {
|
||||
pairs.emplace_back(path.lexically_normal().string(), std::move(token));
|
||||
}
|
||||
};
|
||||
add(graph.snapshot_materialized_root, "${SNAPSHOT}");
|
||||
add(graph.profile_build_dir, "${PROFILE}");
|
||||
if (!graph.profile_build_dir.empty()) {
|
||||
// The whole build root (probes, .vbs state, other profiles) must stay portable too.
|
||||
add(graph.profile_build_dir.parent_path().parent_path(), "${BUILD}");
|
||||
}
|
||||
add(graph.project_root, "${PROJECT}");
|
||||
std::ranges::sort(pairs,
|
||||
[](const auto &a, const auto &b) { return a.first.size() > b.first.size(); });
|
||||
return PathMapper{std::move(pairs)};
|
||||
}
|
||||
|
||||
std::string portable_executable(const std::filesystem::path &exe, const PathMapper &mapper) {
|
||||
const std::string raw = exe.string();
|
||||
const std::string normalized = mapper.normalize(raw);
|
||||
if (normalized == raw && exe.has_filename()) {
|
||||
// Toolchain identity already covers the compiler; keep external tool paths portable.
|
||||
return exe.filename().string();
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
// Portable action identity: logical action id + flags + input content digests, with all
|
||||
// build-root/project-root prefixes normalized away. Absolute output paths are excluded.
|
||||
std::string compute_remote_action_key(const Graph::ActionNode &action,
|
||||
const Graph::BuildGraph &graph, RunDigestCache &digest_cache,
|
||||
const PathMapper &mapper) {
|
||||
Util::Sha256Hasher hasher;
|
||||
hasher.update_length_prefixed("vbs-remote-action-v1");
|
||||
hasher.update_length_prefixed(graph.profile_identity);
|
||||
hasher.update_length_prefixed(graph.toolchain.identity_hash);
|
||||
hasher.update_length_prefixed(action.id);
|
||||
hasher.update_u64(static_cast<std::uint64_t>(action.kind));
|
||||
hasher.update_length_prefixed(portable_executable(action.executable, mapper));
|
||||
hasher.update_u64(action.arguments.size());
|
||||
for (const auto &arg : action.arguments) {
|
||||
hasher.update_length_prefixed(mapper.normalize(arg));
|
||||
}
|
||||
hasher.update_length_prefixed(mapper.normalize(action.response_file_content));
|
||||
hasher.update_u64(action.environment.size());
|
||||
for (const auto &[k, v] : action.environment) {
|
||||
hasher.update_length_prefixed(k);
|
||||
hasher.update_length_prefixed(mapper.normalize(v));
|
||||
}
|
||||
hasher.update_u64(action.declared_inputs.size());
|
||||
for (const auto &in : action.declared_inputs) {
|
||||
hasher.update_length_prefixed(mapper.normalize(in.string()));
|
||||
if (auto digest = digest_cache.hash_file(in); digest) {
|
||||
hasher.update_length_prefixed(*digest);
|
||||
} else {
|
||||
hasher.update_length_prefixed("<missing>");
|
||||
}
|
||||
}
|
||||
hasher.update_u64(action.nondeterministic ? 1u : 0u);
|
||||
return hasher.finalize_hex();
|
||||
}
|
||||
|
||||
std::string serialize_remote_record(const State::ActionSuccessRecord &record,
|
||||
const Graph::ActionNode &action, const PathMapper &mapper,
|
||||
std::string_view remote_key) {
|
||||
using Util::json_array;
|
||||
using Util::json_quote;
|
||||
|
||||
std::vector<State::BlobRef> ordered_outputs;
|
||||
ordered_outputs.reserve(action.declared_outputs.size());
|
||||
for (const auto &out : action.declared_outputs) {
|
||||
const auto it = record.outputs.find(out.string());
|
||||
if (it == record.outputs.end()) {
|
||||
return {};
|
||||
}
|
||||
ordered_outputs.push_back(it->second);
|
||||
}
|
||||
|
||||
std::string json = "{\"schema_version\":1,\"action_key\":" + json_quote(remote_key) +
|
||||
",\"profile_identity\":" + json_quote(record.profile_identity) +
|
||||
",\"target_name\":" + json_quote(record.target_name) +
|
||||
",\"recorded_epoch\":" + std::to_string(record.recorded_epoch) +
|
||||
",\"outputs\":";
|
||||
json += json_array(ordered_outputs, [](const State::BlobRef &ref) {
|
||||
return "{\"sha256\":" + json_quote(ref.sha256) + ",\"size\":" + std::to_string(ref.size) +
|
||||
",\"mode\":" + std::to_string(ref.mode) + '}';
|
||||
});
|
||||
json += ",\"discovered_inputs\":[";
|
||||
bool first = true;
|
||||
for (const auto &[path, digest] : record.discovered_inputs) {
|
||||
if (!first) {
|
||||
json += ',';
|
||||
}
|
||||
first = false;
|
||||
json += "{\"path\":" + json_quote(mapper.normalize(path)) +
|
||||
",\"sha256\":" + json_quote(digest) + '}';
|
||||
}
|
||||
json += "],\"lookup_guards\":";
|
||||
json += json_array(record.lookup_guards, [&](const Snapshot::LookupGuard &guard) {
|
||||
std::vector<std::string> roots;
|
||||
roots.reserve(guard.search_roots.size());
|
||||
for (const auto &root : guard.search_roots) {
|
||||
roots.push_back(mapper.normalize(root));
|
||||
}
|
||||
return "{\"search_roots\":" + json_array(roots, json_quote) +
|
||||
",\"lookup_name\":" + json_quote(guard.lookup_name) +
|
||||
",\"matched_root_index\":" + std::to_string(guard.matched_root_index) +
|
||||
",\"matched_sha256\":" + json_quote(guard.matched_sha256) + '}';
|
||||
});
|
||||
json += '}';
|
||||
return json;
|
||||
}
|
||||
|
||||
using RemoteJson = Void::Json::Strict::Value;
|
||||
|
||||
const RemoteJson *remote_find(const RemoteJson::Object &obj, const std::string &key) {
|
||||
const auto it = obj.find(key);
|
||||
return it == obj.end() ? nullptr : &it->second;
|
||||
}
|
||||
|
||||
std::string remote_str(const RemoteJson::Object &obj, const std::string &key) {
|
||||
const auto *value = remote_find(obj, key);
|
||||
if (!value) {
|
||||
return {};
|
||||
}
|
||||
const auto *text = std::get_if<std::string>(&value->value);
|
||||
return text ? *text : std::string{};
|
||||
}
|
||||
|
||||
std::int64_t remote_i64(const RemoteJson::Object &obj, const std::string &key) {
|
||||
const auto *value = remote_find(obj, key);
|
||||
if (!value) {
|
||||
return 0;
|
||||
}
|
||||
const auto *number = std::get_if<std::int64_t>(&value->value);
|
||||
return number ? *number : 0;
|
||||
}
|
||||
|
||||
std::optional<State::ActionSuccessRecord> parse_remote_record(std::string_view json_text,
|
||||
const Graph::ActionNode &action,
|
||||
const PathMapper &mapper) {
|
||||
auto parsed = Void::Json::Strict::parse(json_text);
|
||||
if (!parsed) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto *root = std::get_if<RemoteJson::Object>(&parsed->value);
|
||||
if (!root) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
State::ActionSuccessRecord record;
|
||||
record.profile_identity = remote_str(*root, "profile_identity");
|
||||
record.target_name = remote_str(*root, "target_name");
|
||||
record.recorded_epoch = remote_i64(*root, "recorded_epoch");
|
||||
|
||||
const auto *outputs = remote_find(*root, "outputs");
|
||||
const auto *output_array = outputs ? std::get_if<RemoteJson::Array>(&outputs->value) : nullptr;
|
||||
if (!output_array || output_array->size() != action.declared_outputs.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
for (std::size_t i = 0; i < output_array->size(); ++i) {
|
||||
const auto *obj = std::get_if<RemoteJson::Object>(&(*output_array)[i].value);
|
||||
if (!obj) {
|
||||
return std::nullopt;
|
||||
}
|
||||
State::BlobRef ref;
|
||||
ref.sha256 = remote_str(*obj, "sha256");
|
||||
ref.size = static_cast<std::uint64_t>(remote_i64(*obj, "size"));
|
||||
ref.mode = static_cast<std::uint32_t>(remote_i64(*obj, "mode"));
|
||||
if (ref.sha256.size() != 64) {
|
||||
return std::nullopt;
|
||||
}
|
||||
record.outputs[action.declared_outputs[i].string()] = std::move(ref);
|
||||
}
|
||||
|
||||
if (const auto *discovered = remote_find(*root, "discovered_inputs")) {
|
||||
if (const auto *array = std::get_if<RemoteJson::Array>(&discovered->value)) {
|
||||
for (const auto &item : *array) {
|
||||
const auto *obj = std::get_if<RemoteJson::Object>(&item.value);
|
||||
if (!obj) {
|
||||
continue;
|
||||
}
|
||||
const auto path = remote_str(*obj, "path");
|
||||
const auto digest = remote_str(*obj, "sha256");
|
||||
if (!path.empty() && !digest.empty()) {
|
||||
record.discovered_inputs[mapper.denormalize(path)] = digest;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (const auto *guards = remote_find(*root, "lookup_guards")) {
|
||||
if (const auto *array = std::get_if<RemoteJson::Array>(&guards->value)) {
|
||||
for (const auto &item : *array) {
|
||||
const auto *obj = std::get_if<RemoteJson::Object>(&item.value);
|
||||
if (!obj) {
|
||||
continue;
|
||||
}
|
||||
Snapshot::LookupGuard guard;
|
||||
guard.lookup_name = remote_str(*obj, "lookup_name");
|
||||
guard.matched_root_index = static_cast<int>(remote_i64(*obj, "matched_root_index"));
|
||||
guard.matched_sha256 = remote_str(*obj, "matched_sha256");
|
||||
if (const auto *roots = remote_find(*obj, "search_roots")) {
|
||||
if (const auto *root_array = std::get_if<RemoteJson::Array>(&roots->value)) {
|
||||
for (const auto &root_value : *root_array) {
|
||||
if (const auto *text = std::get_if<std::string>(&root_value.value)) {
|
||||
guard.search_roots.push_back(mapper.denormalize(*text));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
record.lookup_guards.push_back(std::move(guard));
|
||||
}
|
||||
}
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
std::set<std::string> collect_ancestors(const Graph::BuildGraph &graph, const std::string &id) {
|
||||
std::set<std::string> ancestors;
|
||||
std::queue<std::string> q;
|
||||
@@ -309,6 +561,7 @@ execute_graph(const Graph::BuildGraph &graph, State::StateStore &state,
|
||||
std::atomic<std::size_t> started_exec_count{0};
|
||||
bool abort_scheduling = false;
|
||||
RunDigestCache digest_cache;
|
||||
const PathMapper path_mapper = make_path_mapper(graph);
|
||||
|
||||
auto action_label = [&](const Graph::ActionNode &act) -> std::string {
|
||||
auto rel_name = [&](const std::filesystem::path &p) -> std::string {
|
||||
@@ -385,6 +638,40 @@ execute_graph(const Graph::BuildGraph &graph, State::StateStore &state,
|
||||
}
|
||||
}
|
||||
|
||||
// 1b. Shared remote action cache lookup (Track D1) on local miss.
|
||||
if (act.cacheable && !act.nondeterministic && options.use_cache &&
|
||||
state.has_remote_cache()) {
|
||||
const std::string remote_key =
|
||||
compute_remote_action_key(act, graph, digest_cache, path_mapper);
|
||||
auto remote_json = state.fetch_remote_action(remote_key);
|
||||
if (remote_json && remote_json->has_value()) {
|
||||
auto remote_record = parse_remote_record(**remote_json, act, path_mapper);
|
||||
bool usable = remote_record.has_value();
|
||||
if (usable) {
|
||||
for (const auto &entry : remote_record->outputs) {
|
||||
auto have = state.ensure_remote_blob(entry.second.sha256);
|
||||
if (!have || !*have) {
|
||||
usable = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (usable &&
|
||||
verify_discovered_inputs_unchanged(remote_record->discovered_inputs,
|
||||
digest_cache) &&
|
||||
(verify_outputs_match_record(act, *remote_record, digest_cache) ||
|
||||
restore_outputs_from_cache(act, *remote_record, state, digest_cache))) {
|
||||
remote_record->action_key = remote_key;
|
||||
(void)state.record_action_success(*remote_record);
|
||||
res.succeeded = true;
|
||||
res.from_cache = true;
|
||||
res.exit_code = 0;
|
||||
res.elapsed = timer.elapsed_as<std::chrono::milliseconds>();
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const std::size_t seq = started_exec_count.fetch_add(1, std::memory_order_relaxed) + 1;
|
||||
Void::Log::info("Build", "[{:>2}/{}] {}", seq, selected_indices.size(), label);
|
||||
emit_event(ExecutionEvent::Kind::ActionStarted, act, label, seq);
|
||||
@@ -529,6 +816,38 @@ execute_graph(const Graph::BuildGraph &graph, State::StateStore &state,
|
||||
committed.error().message());
|
||||
return res;
|
||||
}
|
||||
|
||||
// Best-effort publication to the shared remote cache (Track D1): blobs first,
|
||||
// then the action record. Cache upload failures never fail the build.
|
||||
if (state.has_remote_cache()) {
|
||||
const std::string remote_key =
|
||||
compute_remote_action_key(act, graph, digest_cache, path_mapper);
|
||||
bool uploaded = true;
|
||||
for (const auto &out : act.declared_outputs) {
|
||||
const auto it = rec.outputs.find(out.string());
|
||||
if (it == rec.outputs.end()) {
|
||||
uploaded = false;
|
||||
break;
|
||||
}
|
||||
auto pushed = state.publish_remote_blob(it->second.sha256);
|
||||
if (!pushed) {
|
||||
Void::Log::warn("Remote", "Skipping remote cache upload for '{}': {}",
|
||||
act.id, pushed.error().message());
|
||||
uploaded = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (uploaded) {
|
||||
const auto json = serialize_remote_record(rec, act, path_mapper, remote_key);
|
||||
if (!json.empty()) {
|
||||
auto pushed = state.publish_remote_action(remote_key, json);
|
||||
if (!pushed) {
|
||||
Void::Log::warn("Remote", "Skipping remote action record for '{}': {}",
|
||||
act.id, pushed.error().message());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.succeeded = true;
|
||||
|
||||
Reference in new issue
Block a user