feat(registry): add the distributed action queue
This commit is contained in:
1 parent
0674b7f9fe
commit
2776e459e3
6 files changed
+181
No files matched your search
@@ -178,6 +178,7 @@ class RegistryService {
|
||||
std::map<std::string, PackageMeta, std::less<>> packages_;
|
||||
std::map<std::string, WorkerRecord, std::less<>> workers_;
|
||||
std::map<std::string, ActionLeaseRecord, std::less<>> action_leases_; // action_key -> lease
|
||||
std::vector<std::string> action_queue_; // enqueued action keys (in-memory)
|
||||
std::uint64_t worker_sequence_{0};
|
||||
std::vector<AuditEntry> audits_;
|
||||
HttpServer server_;
|
||||
|
||||
@@ -1020,6 +1020,109 @@ HttpResponse RegistryService::handle_request(const HttpRequest &req) {
|
||||
}
|
||||
}
|
||||
|
||||
// Distributed action queue: enqueue keys and lease them to workers.
|
||||
if (segs.size() >= 3 && segs[0] == "api" && segs[1] == "v1" && segs[2] == "queue") {
|
||||
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() == 3 && req.method == "GET") {
|
||||
prune_expired_leases();
|
||||
std::vector<std::string> pending;
|
||||
for (const auto &key : action_queue_) {
|
||||
if (!action_leases_.contains(key)) {
|
||||
pending.push_back(key);
|
||||
}
|
||||
}
|
||||
std::vector<std::string> leased_keys;
|
||||
for (const auto &[key, lease] : action_leases_) {
|
||||
if (std::ranges::find(action_queue_, key) != action_queue_.end()) {
|
||||
leased_keys.push_back(key);
|
||||
}
|
||||
}
|
||||
return HttpResponse::json(
|
||||
200, "{\"pending\":" + json_array(pending, json_quote) +
|
||||
",\"leased\":" + json_array(leased_keys, json_quote) + "}\n");
|
||||
}
|
||||
if (segs.size() == 3 && req.method == "POST") {
|
||||
if (!principal->scopes.contains("publish") && principal->role != Role::Admin) {
|
||||
return HttpResponse::error_json(403, "forbidden", "publish scope required");
|
||||
}
|
||||
auto parsed = Void::Json::Strict::parse(req.body);
|
||||
const auto *obj = parsed ? std::get_if<JsonVal::Object>(&parsed->value) : nullptr;
|
||||
const JsonVal::Array *keys = nullptr;
|
||||
if (obj != nullptr) {
|
||||
if (const auto keys_it = obj->find("action_keys"); keys_it != obj->end()) {
|
||||
keys = std::get_if<JsonVal::Array>(&keys_it->second.value);
|
||||
}
|
||||
}
|
||||
if (keys == nullptr) {
|
||||
return HttpResponse::error_json(400, "invalid_json",
|
||||
"action_keys must be a JSON array");
|
||||
}
|
||||
std::size_t queued = 0;
|
||||
for (const auto &entry : *keys) {
|
||||
const auto *key = std::get_if<std::string>(&entry.value);
|
||||
if (key == nullptr || !is_sha256_hex(*key)) {
|
||||
return HttpResponse::error_json(400, "invalid_key",
|
||||
"action keys must be SHA-256 hex digests");
|
||||
}
|
||||
if (std::ranges::find(action_queue_, *key) == action_queue_.end()) {
|
||||
action_queue_.push_back(*key);
|
||||
++queued;
|
||||
}
|
||||
}
|
||||
append_audit_locked(principal->username, "queue.enqueue", std::to_string(queued), "ok",
|
||||
"");
|
||||
return HttpResponse::json(
|
||||
200, "{\"queued\":" + std::to_string(queued) +
|
||||
",\"pending\":" + std::to_string(action_queue_.size()) + "}\n");
|
||||
}
|
||||
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;
|
||||
const std::string worker_id = obj ? get_str(*obj, "worker_id") : std::string{};
|
||||
std::int64_t lease_seconds = 60;
|
||||
if (obj) {
|
||||
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 (!worker_id.empty() && !workers_.contains(worker_id)) {
|
||||
return HttpResponse::error_json(404, "not_found", "worker not registered");
|
||||
}
|
||||
prune_expired_leases();
|
||||
const auto next = std::ranges::find_if(action_queue_, [&](const std::string &key) {
|
||||
return !action_leases_.contains(key);
|
||||
});
|
||||
if (next == action_queue_.end()) {
|
||||
return HttpResponse::error_json(404, "empty", "no queued action available");
|
||||
}
|
||||
ActionLeaseRecord lease;
|
||||
lease.action_key = *next;
|
||||
lease.worker_id = worker_id;
|
||||
lease.lease_token = random_hex(16);
|
||||
lease.expires_epoch = now_epoch() + lease_seconds;
|
||||
action_leases_[*next] = lease;
|
||||
append_audit_locked(principal->username, "queue.lease", *next, "ok", worker_id);
|
||||
return HttpResponse::json(
|
||||
200, "{\"action_key\":" + json_quote(*next) +
|
||||
",\"worker_id\":" + json_quote(worker_id) +
|
||||
",\"lease_token\":" + json_quote(lease.lease_token) +
|
||||
",\"expires_epoch\":" + std::to_string(lease.expires_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) {
|
||||
@@ -1157,6 +1260,8 @@ HttpResponse RegistryService::handle_request(const HttpRequest &req) {
|
||||
return HttpResponse::error_json(500, "storage_error", "cannot store result record");
|
||||
}
|
||||
action_leases_.erase(lease_it);
|
||||
action_queue_.erase(std::remove(action_queue_.begin(), action_queue_.end(), action_key),
|
||||
action_queue_.end());
|
||||
append_audit_locked(principal->username, "action.result", action_key, "ok",
|
||||
existed ? "replaced" : "stored");
|
||||
return HttpResponse::json(200, "{\"stored\":true}\n");
|
||||
|
||||
Reference in new issue
Block a user