Save executor stats into cache

This commit is contained in:
Matej Bačo
2022-10-14 11:30:42 +00:00
parent 5807523488
commit 73b29e9005
6 changed files with 55 additions and 9 deletions
+2 -2
View File
@@ -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();
+5 -5
View File
@@ -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);
Generated
+1 -1
View File
@@ -5401,5 +5401,5 @@
"platform-overrides": {
"php": "8.0"
},
"plugin-api-version": "2.2.0"
"plugin-api-version": "2.3.0"
}
+3
View File
@@ -10,6 +10,9 @@ class Random extends Adapter
{
$executors = $this->getExecutors();
$executor = $executors[\array_rand($executors)] ?? null;
\var_dump($executor);
return $executor ?? null;
}
}
+1 -1
View File
@@ -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
{
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace FunctionsProxy\Adapter;
use FunctionsProxy\Adapter;
class UsageBased extends Adapter
{
public function getNextExecutor(): array
{
$executors = $this->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;
}
}