Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: balancing bugs #44

Merged
merged 6 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,56 +137,67 @@ function createState(string $dsn): State
$runtimeId = $request->getHeader('x-opr-runtime-id', '');
$method = $request->getHeader('x-opr-addressing-method', ADDRESSING_METHOD_ANYCAST_EFFICIENT);

$group = new Group();

if ($method === ADDRESSING_METHOD_ANYCAST_FAST) {
$algorithm = new Random();
}

$balancers = [];

// Cold-started-only options
$balancer1 = new Balancer($algorithm);

// Only online executors
$balancer1->addFilter(fn ($option) => $option->getState('status', 'offline') === 'online');

if ($method === ADDRESSING_METHOD_ANYCAST_EFFICIENT) {
// Only low host-cpu usage
$balancer1->addFilter(function ($option) {
return ($option->getState('usage', 100)) < 80;
});

// Only low runtime-cpu usage
// Executors with runtime cold-started
if (!empty($runtimeId)) {
$balancer1->addFilter(function ($option) use ($runtimeId) {
$balancer = new Balancer($algorithm);
$balancer->addFilter(function ($option) use ($runtimeId) {
$runtimes = $option->getState('runtimes', []);
$runtime = $runtimes[$runtimeId] ?? [];
return ($runtime['usage'] ?? 100) < 80;
});
$balancers[] = $balancer;
}

// Any options
$balancer2 = new Balancer($algorithm);
$balancer2->addFilter(fn ($option) => $option->getState('status', 'offline') === 'online');
// Executors with low host-cpu usage
$balancer = new Balancer($algorithm);
$balancer->addFilter(fn ($option) => \intval($option->getState('usage', '100')) < 80);
$balancers[] = $balancer;

// Online executors
$balancer = new Balancer($algorithm);
$balancer->addFilter(fn ($option) => $option->getState('status', 'offline') === 'online');
$balancers[] = $balancer;

// Any executors
$balancer = new Balancer($algorithm);
$balancer->addFilter(fn () => true);
$balancers[] = $balancer;
} else {
// Any executors
$balancer = new Balancer($algorithm);
$balancer->addFilter(fn () => true);
$balancers[] = $balancer;
}

foreach ($state->list(RESOURCE_EXECUTORS) as $hostname => $executor) {
$executor['runtimes'] = $state->list(RESOURCE_RUNTIMES . $hostname);
$executor['runtimes'] = $state->list(RESOURCE_RUNTIMES . $hostname); // TODO: Avoid those extra Redis calls
$executor['hostname'] = $hostname;

if (Http::isDevelopment()) {
Console::log("Updated balancing option '" . $hostname . "' with ". \count($executor['runtimes'])." runtimes: " . \json_encode($executor));
}

$executor['hostname'] = $hostname;

$balancer1->addOption(new Option($executor));
if (isset($balancer2)) {
$balancer2->addOption(new Option($executor));
foreach ($balancers as $balancer) {
$balancer->addOption(new Option($executor));
}
}

$group->add($balancer1);

if (isset($balancer2)) {
$group->add($balancer2);
$group = new Group();
foreach ($balancers as $balancer) {
$group->add($balancer);
}

return $group;
Expand All @@ -209,10 +220,10 @@ function createState(string $dsn): State

if ($firstCheck || Http::isDevelopment() || $executor['status'] !== $newStatus) {
if ($newStatus === 'online') {
Console::info('Executor "' . $hostname . '" is online');
Console::info('Executor "' . $hostname . '" went online');
} else {
$message = $node->getState()['message'] ?? 'Unexpected error.';
$error = new Exception('Executor "' . $hostname . '" is offline: ' . $message, 500);
$error = new Exception('Executor "' . $hostname . '" went offline: ' . $message, 500);
logError($error, "healthCheckError", $logger, null);
}
}
Expand Down
6 changes: 3 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading