Implement cpu usage stats

This commit is contained in:
Matej Bačo
2022-10-16 14:28:36 +00:00
parent 2337da5d6c
commit b79bdc56a1
7 changed files with 91 additions and 74 deletions
+1 -1
View File
@@ -60,5 +60,5 @@ abstract class Adapter
return $responseExecutors;
}
abstract public function getNextExecutor(): array;
abstract public function getNextExecutor(?string $contaierId): array;
}
+1 -3
View File
@@ -6,13 +6,11 @@ use FunctionsProxy\Adapter;
class Random extends Adapter
{
public function getNextExecutor(): array
public function getNextExecutor(?string $contaierId): array
{
$executors = $this->getExecutors();
$executor = $executors[\array_rand($executors)] ?? null;
\var_dump($executor);
return $executor ?? null;
}
}
+1 -1
View File
@@ -8,7 +8,7 @@ class RoundRobin extends Adapter
{
private $currentIndex = 0; // TODO: @Meldiron Put into utopia app resource or/and utopia registry
public function getNextExecutor(): array
public function getNextExecutor(?string $contaierId): array
{
$executors = $this->getExecutors();
$executor = $executors[$this->currentIndex] ?? null;
+60 -48
View File
@@ -6,62 +6,74 @@ use FunctionsProxy\Adapter;
class UsageBased extends Adapter
{
public function getNextExecutor($contaierId): array
public function getNextExecutor(?string $contaierId): 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 | ["health"]=>
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) {
// container1: 0.5
// container1: 0.2
appwrite-functions-proxy | }
appwrite-functions-proxy | }
appwrite-functions-proxy | }
appwrite-functions-proxy | }
*/
// Remove offline and unknown-status executors
$executors = \array_filter($executors, fn($executor) => (($executor['state'] ?? [])['status']) ?? 'offline' === 'online');
// low, mid, high
// Ideal executor is one that is already running a contianer for this function deployment
$idealExecutors = [];
// 10% on host, 90% on chontainer
// 90% on host, 10% on chontainer
// TODO: @Meldiron Proper functionality
// TODO: @Meldiron Use this adapter and test it
/*
// If 2+ running, do this to sort:
$hostUsage = 20;
$containerUsage = 40;
$hostWeight = 0.33;
$contiainerWeight = 0.66;
$totalUsage = $hostUsage * $hostWeight + $containerUsage * $contiainerWeight;
// If all existing are above 80%, we should start on new executor ideally
if(host < 0.8 & container < 0.8) {
// For whatever reason we don't know container yet. We consider all executors ideal
if(!(isset($contaierId))) {
$idealExecutors = \array_map(fn($executor) => $executor['id'], $executors);
} else {
foreach($executors as $executor) {
$executorId = $executor['id'] ?? '';
$executorState = $executor['state']['health'] ?? [];
$hostUsage = intval($executorState['hostUsage'] ?? 100);
$containerUsage = intval(($executorState['functionsUsage'] ?? [])[$executorId . '-' . $contaierId] ?? 100);
// If host or contianer usage above 80, executor is not ideal
if($hostUsage < 80 && $containerUsage < 80) {
$idealExecutors[] = $executorId;
}
}
}
*/
$executor = $executors[\array_rand($executors)] ?? null;
if(\count($idealExecutors) <= 0) {
// If no ideal, let's consider all of them ideal. Since there is no prefference.
$idealExecutors = \array_map(fn($executor) => $executor['id'], $executors);
}
// 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));
$executor = $executors[$executorIndex];
if(!isset($executor)) {
continue;
}
$executorState = $executor['state']['health'] ?? [];
$hostUsage = intval($executorState['hostUsage'] ?? 10);
$containerUsage = intval(($executorState['functionsUsage'] ?? [])[$executorId . '-' . $contaierId] ?? 100);
$usageIndex = ($hostUsage * $hostWeight) + ($containerUsage * $containerWeight);
$sortedExecutors[$executorId] = $usageIndex;
}
\asort($sortedExecutors);
// Pick the least used executor
$idealExecutorId = $idealExecutors[0] ?? null;
// Null if no executor found
if($idealExecutorId === null) {
return null;
}
$executorIndex = \array_search($idealExecutorId, \array_map(fn($executor) => $executor['id'], $executors));
$executor = $executors[$executorIndex];
// Null if can't match executor to ID
return $executor ?? null;
}
}