From 73b29e900571ef74ce74f0c8ce060d3f72835aeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Fri, 14 Oct 2022 11:30:42 +0000 Subject: [PATCH] Save executor stats into cache --- app/executor.php | 4 +-- app/functionsProxy.php | 10 +++--- composer.lock | 2 +- src/FunctionsProxy/Adapter/Random.php | 3 ++ src/FunctionsProxy/Adapter/RoundRobin.php | 2 +- src/FunctionsProxy/Adapter/UsageBased.php | 43 +++++++++++++++++++++++ 6 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 src/FunctionsProxy/Adapter/UsageBased.php diff --git a/app/executor.php b/app/executor.php index 971c23ae9d..edb8fbb909 100644 --- a/app/executor.php +++ b/app/executor.php @@ -665,7 +665,7 @@ App::get('/v1/health') $functionsUsage[$containerUsage['name']] = $containerUsage['cpu']; } } catch(\Exception $err) { - // TODO: Handle better + // TODO: @Meldiron Handle better \var_dump($err); } finally { $orchestrationPool->put($orchestration); @@ -805,7 +805,7 @@ $http->on('start', function ($http) { foreach ($orphans as $runtime) { go(function () use ($runtime, $orchestrationPool) { - // TODO: Only remove containers prefixed with this executor name + // TODO: @Meldiron Only remove containers prefixed with this executor name try { $orchestration = $orchestrationPool->get(); diff --git a/app/functionsProxy.php b/app/functionsProxy.php index 011803bfb9..8a94ad5c69 100644 --- a/app/functionsProxy.php +++ b/app/functionsProxy.php @@ -47,7 +47,7 @@ function markOffline(Cache $cache, string $executorId, string $error, bool $forc { $data = $cache->load('executors-' . $executorId, 60 * 60 * 24 * 30 * 3); // 3 months - $cache->save('executors-' . $executorId, ['status' => 'offline']); + $cache->save('executors-' . $executorId, ['status' => 'offline', 'stats' => []]); if (!$data || $data['status'] === 'online' || $forceShowError) { Console::warning('Executor "' . $executorId . '" went down! Message:'); @@ -55,11 +55,11 @@ function markOffline(Cache $cache, string $executorId, string $error, bool $forc } } -function markOnline(cache $cache, string $executorId, bool $forceShowError = false) +function markOnline(cache $cache, string $executorId, bool $forceShowError = false, mixed $stats = []) { $data = $cache->load('executors-' . $executorId, 60 * 60 * 24 * 30 * 3); // 3 months - $cache->save('executors-' . $executorId, ['status' => 'online']); + $cache->save('executors-' . $executorId, ['status' => 'online', 'stats' => $stats]); if (!$data || $data['status'] === 'offline' || $forceShowError) { Console::success('Executor "' . $executorId . '" went online.'); @@ -92,14 +92,14 @@ function fetchExecutorsState(RedisPool $redisPool, bool $forceShowError = false) 'x-appwrite-executor-key: ' . App::getEnv('_APP_EXECUTOR_SECRET', '') ]); - $executorResponse = \curl_exec($ch); // TODO: Use to save usage stats + $executorResponse = \curl_exec($ch); $statusCode = \curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = \curl_error($ch); \curl_close($ch); if ($statusCode === 200) { - markOnline($cache, $id, $forceShowError); + markOnline($cache, $id, $forceShowError, \json_decode($executorResponse, true)); } else { $message = 'Code: ' . $statusCode . ' with response "' . $executorResponse . '" and error error: ' . $error; markOffline($cache, $id, $message, $forceShowError); diff --git a/composer.lock b/composer.lock index c0f0ece5f7..4292b84e3d 100644 --- a/composer.lock +++ b/composer.lock @@ -5401,5 +5401,5 @@ "platform-overrides": { "php": "8.0" }, - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.3.0" } diff --git a/src/FunctionsProxy/Adapter/Random.php b/src/FunctionsProxy/Adapter/Random.php index 8b8496f619..cada99ed35 100644 --- a/src/FunctionsProxy/Adapter/Random.php +++ b/src/FunctionsProxy/Adapter/Random.php @@ -10,6 +10,9 @@ class Random extends Adapter { $executors = $this->getExecutors(); $executor = $executors[\array_rand($executors)] ?? null; + + \var_dump($executor); + return $executor ?? null; } } diff --git a/src/FunctionsProxy/Adapter/RoundRobin.php b/src/FunctionsProxy/Adapter/RoundRobin.php index 25bea722dd..f106f1950e 100644 --- a/src/FunctionsProxy/Adapter/RoundRobin.php +++ b/src/FunctionsProxy/Adapter/RoundRobin.php @@ -6,7 +6,7 @@ use FunctionsProxy\Adapter; class RoundRobin extends Adapter { - private $currentIndex = 0; // TODO: Put into redis to share across proxies + private $currentIndex = 0; // TODO: @Meldiron Put into redis to share across proxies public function getNextExecutor(): array { diff --git a/src/FunctionsProxy/Adapter/UsageBased.php b/src/FunctionsProxy/Adapter/UsageBased.php new file mode 100644 index 0000000000..ccc28beebb --- /dev/null +++ b/src/FunctionsProxy/Adapter/UsageBased.php @@ -0,0 +1,43 @@ +getExecutors(); + + /* + appwrite-functions-proxy | array(3) { + appwrite-functions-proxy | ["id"]=> + appwrite-functions-proxy | string(4) "exc2" + appwrite-functions-proxy | ["hostname"]=> + appwrite-functions-proxy | string(18) "appwrite-executor2" + appwrite-functions-proxy | ["state"]=> + appwrite-functions-proxy | array(2) { + appwrite-functions-proxy | ["status"]=> + appwrite-functions-proxy | string(6) "online" + appwrite-functions-proxy | ["stats"]=> + appwrite-functions-proxy | array(3) { + appwrite-functions-proxy | ["status"]=> + appwrite-functions-proxy | string(4) "pass" + appwrite-functions-proxy | ["hostUsage"]=> + appwrite-functions-proxy | float(0.348125) + appwrite-functions-proxy | ["functionsUsage"]=> + appwrite-functions-proxy | array(0) { + appwrite-functions-proxy | } + appwrite-functions-proxy | } + appwrite-functions-proxy | } + appwrite-functions-proxy | } + */ + + // TODO: @Meldiron Proper functionality + // TODO: @Meldiron Use this adapter and test it + + $executor = $executors[\array_rand($executors)] ?? null; + return $executor ?? null; + } +}