From 6d91c95eebfb110544b977d57d223af94e38b8e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 17 Oct 2022 08:35:13 +0000 Subject: [PATCH] Fix bugs around CPU based proxy adapter --- app/functionsProxy.php | 4 +++- src/FunctionsProxy/Adapter/UsageBased.php | 17 ++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/app/functionsProxy.php b/app/functionsProxy.php index ae84169d1a..a8edffd3d6 100644 --- a/app/functionsProxy.php +++ b/app/functionsProxy.php @@ -222,7 +222,7 @@ $run = function (SwooleRequest $request, SwooleResponse $response) use ($adapter $runtimeId = $body['runtimeId'] ?? null; $executor = $adapter->getNextExecutor($runtimeId); - Console::success("Executing on " . $executor['hostname']); + Console::success("Executing on " . $executor['id'] . ' (' . $executor['hostname'] . ')'); $client = new Client($executor['hostname'], 80); $client->setMethod($request->server['request_method'] ?? 'GET'); @@ -237,6 +237,8 @@ $run = function (SwooleRequest $request, SwooleResponse $response) use ($adapter $body['runtimeId'] = $executor['id'] . '-' . $body['runtimeId']; } + // TODO: @Meldiron Add variable regarding executor ID + $client->requestBody = \json_encode($body); $status = $client->execute($request->server['request_uri'] ?? '/'); diff --git a/src/FunctionsProxy/Adapter/UsageBased.php b/src/FunctionsProxy/Adapter/UsageBased.php index affe41382b..1319f9134c 100644 --- a/src/FunctionsProxy/Adapter/UsageBased.php +++ b/src/FunctionsProxy/Adapter/UsageBased.php @@ -8,6 +8,11 @@ class UsageBased extends Adapter { public function getNextExecutor(?string $contaierId): array { + // Adapter configuration + $idealMaxUsage = 80; // Means 80% + $hostWeight = 0.3; + $containerWeight = 0.7; //Weights should add up to 1. For maintanance reasons + $executors = $this->getExecutors(); // Remove offline and unknown-status executors @@ -24,10 +29,10 @@ class UsageBased extends Adapter $executorId = $executor['id'] ?? ''; $executorState = $executor['state']['health'] ?? []; $hostUsage = intval($executorState['hostUsage'] ?? 100); - $containerUsage = intval(($executorState['functionsUsage'] ?? [])[$executorId . '-' . $contaierId] ?? 100); + $containerUsage = intval(($executorState['functionsUsage'] ?? [])[$executorId . '-' . $contaierId] ?? 100); // Forcing 100 to mark that starting runtime is the least ideal. - // If host or contianer usage above 80, executor is not ideal - if ($hostUsage < 80 && $containerUsage < 80) { + // If host or contianer usage above idealMaxUsage, executor is not ideal + if ($hostUsage < $idealMaxUsage && $containerUsage < $idealMaxUsage) { $idealExecutors[] = $executorId; } } @@ -40,8 +45,6 @@ class UsageBased extends Adapter // Sort containers based on usage $sortedExecutors = []; - $hostWeight = 0.3; - $containerWeight = 0.7; foreach ($idealExecutors as $executorId) { $executorIndex = \array_search($executorId, \array_map(fn($executor) => $executor['id'], $executors)); @@ -53,7 +56,7 @@ class UsageBased extends Adapter $executorState = $executor['state']['health'] ?? []; $hostUsage = intval($executorState['hostUsage'] ?? 10); - $containerUsage = intval(($executorState['functionsUsage'] ?? [])[$executorId . '-' . $contaierId] ?? 100); + $containerUsage = intval(($executorState['functionsUsage'] ?? [])[$executorId . '-' . $contaierId] ?? 0); $usageIndex = ($hostUsage * $hostWeight) + ($containerUsage * $containerWeight); @@ -63,7 +66,7 @@ class UsageBased extends Adapter \asort($sortedExecutors); // Pick the least used executor - $idealExecutorId = $idealExecutors[0] ?? null; + $idealExecutorId = \array_keys($sortedExecutors)[0] ?? null; // Null if no executor found if ($idealExecutorId === null) {