feat(registry): add the remote worker lease and result protocol
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 8s
VoidBuildSystem CI / sanitizers-gcc16 (push) Failing after 5s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 10s
VoidBuildSystem CI / wine-windows-gcc16 (push) Failing after 52s

This commit is contained in:
faumaray committed 2026-12-05 11:08:23 +03:00
1 parent c2b80416a5
commit 1ae72e4d07
7 files changed
+336 -1

No files matched your search

+202
View File
@@ -961,6 +961,208 @@ HttpResponse RegistryService::handle_request(const HttpRequest &req) {
return HttpResponse::json(201, "{\"stored\":true,\"created\":true}\n");
}
// Remote execution workers: registration and heartbeats (in-memory, ephemeral).
if (segs.size() >= 3 && segs[0] == "api" && segs[1] == "v1" && segs[2] == "workers") {
if (!principal) {
return HttpResponse::error_json(401, "unauthorized", "authentication required");
}
if (segs.size() == 3 && req.method == "POST") {
std::string platform;
if (!req.body.empty()) {
auto parsed = Void::Json::Strict::parse(req.body);
const auto *obj = parsed ? std::get_if<JsonVal::Object>(&parsed->value) : nullptr;
if (!obj) {
return HttpResponse::error_json(400, "invalid_json",
"worker registration must be a JSON object");
}
platform = get_str(*obj, "platform");
}
WorkerRecord worker;
worker.worker_id = std::format("wrk_{}_{}", ++worker_sequence_, random_hex(6));
worker.username = principal->username;
worker.platform = platform.substr(0, 128);
worker.registered_epoch = now_epoch();
worker.last_seen_epoch = worker.registered_epoch;
workers_[worker.worker_id] = worker;
append_audit_locked(principal->username, "worker.register", worker.worker_id, "ok", "");
return HttpResponse::json(201, "{\"worker_id\":" + json_quote(worker.worker_id) +
",\"registered_epoch\":" +
std::to_string(worker.registered_epoch) + "}\n");
}
if (segs.size() == 5 && segs[4] == "heartbeat" && req.method == "POST") {
const auto it = workers_.find(segs[3]);
if (it == workers_.end()) {
return HttpResponse::error_json(404, "not_found", "worker not registered");
}
if (principal->role != Role::Admin && it->second.username != principal->username) {
return HttpResponse::error_json(403, "forbidden",
"cannot heartbeat another user's worker");
}
it->second.last_seen_epoch = now_epoch();
return HttpResponse::json(200, "{\"worker_id\":" + json_quote(it->second.worker_id) +
",\"last_seen_epoch\":" +
std::to_string(it->second.last_seen_epoch) + "}\n");
}
if (segs.size() == 3 && req.method == "GET") {
std::vector<const WorkerRecord *> visible;
for (const auto &[id, worker] : workers_) {
if (principal->role == Role::Admin || worker.username == principal->username) {
visible.push_back(&worker);
}
}
return HttpResponse::json(
200, "{\"workers\":" + json_array(visible, [](const WorkerRecord *w) {
return "{\"worker_id\":" + json_quote(w->worker_id) +
",\"username\":" + json_quote(w->username) +
",\"platform\":" + json_quote(w->platform) +
",\"last_seen_epoch\":" + std::to_string(w->last_seen_epoch) + '}';
}) + "}\n");
}
}
// Action leases and result upload for remote workers.
if (segs.size() >= 4 && segs[0] == "api" && segs[1] == "v1" && segs[2] == "actions") {
if (!principal) {
return HttpResponse::error_json(401, "unauthorized", "authentication required");
}
const auto prune_expired_leases = [&] {
const auto now = now_epoch();
for (auto it = action_leases_.begin(); it != action_leases_.end();) {
it = it->second.expires_epoch <= now ? action_leases_.erase(it) : std::next(it);
}
};
if (segs.size() == 4 && segs[3] == "lease" && req.method == "POST") {
auto parsed = Void::Json::Strict::parse(req.body);
const auto *obj = parsed ? std::get_if<JsonVal::Object>(&parsed->value) : nullptr;
if (!obj) {
return HttpResponse::error_json(400, "invalid_json", "lease body must be JSON");
}
const std::string action_key = get_str(*obj, "action_key");
const std::string worker_id = get_str(*obj, "worker_id");
std::int64_t lease_seconds = 60;
if (const auto it = obj->find("lease_seconds"); it != obj->end()) {
const auto *value = std::get_if<std::int64_t>(&it->second.value);
if (!value || *value <= 0 || *value > 3600) {
return HttpResponse::error_json(400, "invalid_lease",
"lease_seconds must be 1..3600");
}
lease_seconds = *value;
}
if (!is_sha256_hex(action_key)) {
return HttpResponse::error_json(400, "invalid_key",
"action_key must be a SHA-256 hex digest");
}
if (!worker_id.empty() && !workers_.contains(worker_id)) {
return HttpResponse::error_json(404, "not_found", "worker not registered");
}
const auto record_path =
action_records_dir_ / action_key.substr(0, 2) / (action_key + ".json");
std::error_code lease_ec;
if (!std::filesystem::is_regular_file(record_path, lease_ec)) {
return HttpResponse::error_json(404, "not_found", "action record not found");
}
prune_expired_leases();
if (const auto it = action_leases_.find(action_key); it != action_leases_.end()) {
return HttpResponse::error_json(409, "already_leased",
"action is leased by " + it->second.worker_id +
" until " +
std::to_string(it->second.expires_epoch));
}
ActionLeaseRecord lease;
lease.action_key = action_key;
lease.worker_id = worker_id;
lease.lease_token = random_hex(16);
lease.expires_epoch = now_epoch() + lease_seconds;
action_leases_[action_key] = lease;
auto record_text = Void::FileSystem::read_file(record_path);
if (!record_text) {
return HttpResponse::error_json(500, "storage_error", "cannot read action record");
}
append_audit_locked(principal->username, "action.lease", action_key, "ok", worker_id);
return HttpResponse::json(
200, "{\"action_key\":" + json_quote(action_key) +
",\"worker_id\":" + json_quote(worker_id) +
",\"lease_token\":" + json_quote(lease.lease_token) +
",\"expires_epoch\":" + std::to_string(lease.expires_epoch) +
",\"record\":" + *record_text + "}\n");
}
if (segs.size() == 5 && segs[4] == "result" && req.method == "POST") {
const std::string &action_key = segs[3];
if (!is_sha256_hex(action_key)) {
return HttpResponse::error_json(400, "invalid_key",
"action_key must be a SHA-256 hex digest");
}
prune_expired_leases();
const auto lease_it = action_leases_.find(action_key);
if (lease_it == action_leases_.end()) {
return HttpResponse::error_json(409, "not_leased", "action has no active lease");
}
auto parsed = Void::Json::Strict::parse(req.body);
const auto *obj = parsed ? std::get_if<JsonVal::Object>(&parsed->value) : nullptr;
if (!obj) {
return HttpResponse::error_json(400, "invalid_json", "result body must be JSON");
}
if (get_str(*obj, "lease_token") != lease_it->second.lease_token) {
return HttpResponse::error_json(403, "invalid_lease_token",
"lease token does not match");
}
const auto record_it = obj->find("record");
if (record_it == obj->end() ||
!std::get_if<JsonVal::Object>(&record_it->second.value)) {
return HttpResponse::error_json(400, "invalid_record",
"record must be a JSON object");
}
const auto *record_obj = std::get_if<JsonVal::Object>(&record_it->second.value);
if (get_str(*record_obj, "action_key") != action_key) {
return HttpResponse::error_json(400, "invalid_record",
"record action_key does not match the lease");
}
// Every referenced output blob must already exist in the CAS.
std::vector<std::string> missing;
if (const auto outputs_it = record_obj->find("outputs");
outputs_it != record_obj->end()) {
if (const auto *outputs = std::get_if<JsonVal::Object>(&outputs_it->second.value)) {
for (const auto &[path, ref] : *outputs) {
const auto *ref_obj = std::get_if<JsonVal::Object>(&ref.value);
const std::string sha = ref_obj ? get_str(*ref_obj, "sha256") : "";
const auto blob = action_blobs_dir_ / sha.substr(0, 2) / sha;
std::error_code blob_ec;
if (sha.empty() || !std::filesystem::is_regular_file(blob, blob_ec)) {
missing.push_back(path);
}
}
}
}
if (!missing.empty()) {
return HttpResponse::error_json(400, "missing_blobs",
"output blobs missing for: " +
json_array(missing, json_quote));
}
const auto record_path =
action_records_dir_ / action_key.substr(0, 2) / (action_key + ".json");
std::error_code result_ec;
const bool existed = std::filesystem::is_regular_file(record_path, result_ec);
std::filesystem::create_directories(record_path.parent_path(), result_ec);
auto serialized_record = Void::Json::Strict::serialize(record_it->second);
if (!serialized_record) {
return HttpResponse::error_json(500, "storage_error",
"cannot serialize result record");
}
const std::string &record_text = *serialized_record;
auto wrote = Platform::atomic_write_file(record_path, record_text, true, 0644u);
if (!wrote) {
return HttpResponse::error_json(500, "storage_error", "cannot store result record");
}
action_leases_.erase(lease_it);
append_audit_locked(principal->username, "action.result", action_key, "ok",
existed ? "replaced" : "stored");
return HttpResponse::json(200, "{\"stored\":true}\n");
}
}
auto can_read_pkg = [&](const PackageMeta &p) {
if (!p.is_private)
return true;