feat(worker): drain the queue with parallel workers
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 9s
VoidBuildSystem CI / sanitizers-gcc16 (push) Failing after 7s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 16s
VoidBuildSystem CI / fuzz-clang (push) Failing after 6s
VoidBuildSystem CI / wine-windows-gcc16 (push) Failing after 1m8s
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 9s
VoidBuildSystem CI / sanitizers-gcc16 (push) Failing after 7s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 16s
VoidBuildSystem CI / fuzz-clang (push) Failing after 6s
VoidBuildSystem CI / wine-windows-gcc16 (push) Failing after 1m8s
This commit is contained in:
1 parent
af2be817a5
commit
ac08887d8f
6 files changed
+176
-30
No files matched your search
+65
-29
@@ -52,6 +52,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cctype>
|
||||
#include <charconv>
|
||||
#include <chrono>
|
||||
@@ -1695,6 +1696,7 @@ struct WorkerCmdOptions {
|
||||
std::string token_env{"VBS_REGISTRY_TOKEN"};
|
||||
std::string lease_seconds_arg{"60"};
|
||||
std::string exec_timeout_arg{"600"};
|
||||
std::string parallel_arg{"1"};
|
||||
};
|
||||
|
||||
int execute_worker_command(const WorkerCmdOptions &opts) {
|
||||
@@ -1975,7 +1977,9 @@ int execute_worker_command(const WorkerCmdOptions &opts) {
|
||||
|
||||
if (opts.queue_mode && !opts.action_key_arg) {
|
||||
// Continuous loop: lease, process, repeat; exits when the queue is empty (after the
|
||||
// optional idle timeout) or on --once.
|
||||
// optional idle timeout) or on --once. --parallel runs several lease/execute loops so
|
||||
// multiple workers drain the queue concurrently (the registry leases each action to
|
||||
// exactly one worker).
|
||||
int idle_timeout_seconds = 0;
|
||||
if (!Void::Str::to_int(opts.idle_timeout_arg, idle_timeout_seconds) ||
|
||||
idle_timeout_seconds < 0 || idle_timeout_seconds > 3600) {
|
||||
@@ -1983,41 +1987,71 @@ int execute_worker_command(const WorkerCmdOptions &opts) {
|
||||
<< "'\n";
|
||||
return 2;
|
||||
}
|
||||
const auto idle_deadline =
|
||||
std::chrono::steady_clock::now() + std::chrono::seconds{idle_timeout_seconds};
|
||||
int lease_failures = 0;
|
||||
while (true) {
|
||||
auto leased = Package::lease_queued_action(registry, registration->worker_id,
|
||||
lease_seconds, auth);
|
||||
if (!leased) {
|
||||
if (leased.error().kind() == Void::Result::ErrorKind::not_found) {
|
||||
if (idle_timeout_seconds == 0 ||
|
||||
std::chrono::steady_clock::now() >= idle_deadline) {
|
||||
std::cout << "queue empty\n";
|
||||
return 0;
|
||||
int parallel_workers = 1;
|
||||
if (!Void::Str::to_int(opts.parallel_arg, parallel_workers) || parallel_workers < 1 ||
|
||||
parallel_workers > 32) {
|
||||
std::cerr << "vbs worker: invalid --parallel value '" << opts.parallel_arg
|
||||
<< "' (expected 1..32)\n";
|
||||
return 2;
|
||||
}
|
||||
std::atomic<bool> empty_reported{false};
|
||||
std::atomic<bool> worker_failed{false};
|
||||
const auto drain_queue = [&] {
|
||||
const auto idle_deadline =
|
||||
std::chrono::steady_clock::now() + std::chrono::seconds{idle_timeout_seconds};
|
||||
int lease_failures = 0;
|
||||
for (;;) {
|
||||
if (worker_failed.load(std::memory_order_relaxed)) {
|
||||
return;
|
||||
}
|
||||
auto leased = Package::lease_queued_action(registry, registration->worker_id,
|
||||
lease_seconds, auth);
|
||||
if (!leased) {
|
||||
if (leased.error().kind() == Void::Result::ErrorKind::not_found) {
|
||||
if (idle_timeout_seconds == 0 ||
|
||||
std::chrono::steady_clock::now() >= idle_deadline) {
|
||||
if (!empty_reported.exchange(true)) {
|
||||
std::cout << "queue empty\n";
|
||||
}
|
||||
return;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{250});
|
||||
continue;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{250});
|
||||
if (++lease_failures > 3) {
|
||||
std::cerr << "vbs worker: giving up after repeated lease failures: "
|
||||
<< leased.error().message() << '\n';
|
||||
worker_failed.store(true);
|
||||
return;
|
||||
}
|
||||
Void::Log::warn("Worker", "lease attempt {} failed: {}", lease_failures,
|
||||
leased.error().message());
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{100});
|
||||
continue;
|
||||
}
|
||||
if (++lease_failures > 3) {
|
||||
std::cerr << "vbs worker: giving up after repeated lease failures: "
|
||||
<< leased.error().message() << '\n';
|
||||
return 1;
|
||||
lease_failures = 0;
|
||||
if (const int code = process_leased(*leased); code != 0) {
|
||||
worker_failed.store(true);
|
||||
return;
|
||||
}
|
||||
if (opts.once) {
|
||||
return;
|
||||
}
|
||||
Void::Log::warn("Worker", "lease attempt {} failed: {}", lease_failures,
|
||||
leased.error().message());
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{100});
|
||||
continue;
|
||||
}
|
||||
lease_failures = 0;
|
||||
const Package::LeasedAction &queued = *leased;
|
||||
if (const int code = process_leased(queued); code != 0) {
|
||||
return code;
|
||||
};
|
||||
if (parallel_workers == 1) {
|
||||
drain_queue();
|
||||
} else {
|
||||
std::vector<std::thread> pool;
|
||||
pool.reserve(static_cast<std::size_t>(parallel_workers));
|
||||
for (int i = 0; i < parallel_workers; ++i) {
|
||||
pool.emplace_back(drain_queue);
|
||||
}
|
||||
if (opts.once) {
|
||||
return 0;
|
||||
for (auto &thread : pool) {
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
return worker_failed.load() ? 1 : 0;
|
||||
}
|
||||
|
||||
auto direct = Package::lease_action(registry, *opts.action_key_arg, registration->worker_id,
|
||||
@@ -3774,7 +3808,9 @@ int main(int argc, char **argv) {
|
||||
.option_default("--lease-seconds", "", "Lease TTL in seconds (1..3600)",
|
||||
worker_opts.lease_seconds_arg, "60")
|
||||
.option_default("--exec-timeout", "", "Remote action timeout in seconds (1..86400)",
|
||||
worker_opts.exec_timeout_arg, "600");
|
||||
worker_opts.exec_timeout_arg, "600")
|
||||
.option_default("--parallel", "", "Concurrent lease/execute loops in queue mode (1..32)",
|
||||
worker_opts.parallel_arg, "1");
|
||||
add_log_flags(worker_cmd);
|
||||
|
||||
auto &migrate_cmd = root.subcommand(
|
||||
|
||||
Reference in new issue
Block a user