From 64d9b002cfab10609cecbb24a343011b75cc128f Mon Sep 17 00:00:00 2001 From: Klim Todrik Date: Thu, 20 Oct 2022 18:42:56 +0400 Subject: [PATCH] Dev (#4) * Added skip self-switcher in getting the oldest node --- src/K8s/Resources.php | 11 +++++------ src/Manticore/ManticoreJson.php | 27 ++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/K8s/Resources.php b/src/K8s/Resources.php index 9370845..a6fcfa5 100644 --- a/src/K8s/Resources.php +++ b/src/K8s/Resources.php @@ -44,7 +44,6 @@ public function getPods(): array foreach ($pods['items'] as $pod) { if ($pod['status']['phase'] === 'Running' || $pod['status']['phase'] === 'Pending') { - if (!empty($pod['status']['conditions'])) { $readyCondition = false; foreach ($pod['status']['conditions'] as $condition) { @@ -53,7 +52,7 @@ public function getPods(): array } } - if (!$readyCondition){ + if (!$readyCondition) { continue; } } @@ -82,13 +81,13 @@ public function getActivePodsCount(): int /** * @throws \JsonException */ - public function getOldestActivePodName() + public function getOldestActivePodName($skipSelf = true) { $currentPodHostname = gethostname(); $pods = []; foreach ($this->getPods() as $pod) { - if ($pod['metadata']['name'] === $currentPodHostname) { + if ($skipSelf && $pod['metadata']['name'] === $currentPodHostname) { continue; } $pods[$pod['status']['startTime']] = $pod['metadata']['name']; @@ -144,7 +143,7 @@ public function getPodsHostnames(): array } - public function getMinAvailableReplica() + public function getMinAvailableReplica($skipSelf = true) { $podsList = $this->getPodsHostnames(); if ($podsList === []) { @@ -156,7 +155,7 @@ public function getMinAvailableReplica() $min = array_shift($podsList); - if ($min === gethostname()) { + if ($skipSelf && $min === gethostname()) { // skip itself $min = array_shift($podsList); } diff --git a/src/Manticore/ManticoreJson.php b/src/Manticore/ManticoreJson.php index f0acde3..8623033 100644 --- a/src/Manticore/ManticoreJson.php +++ b/src/Manticore/ManticoreJson.php @@ -47,7 +47,7 @@ public function __construct($clusterName, $binaryPort = 9312) if (file_exists($this->path)) { try { $manticoreJson = file_get_contents($this->path); - Analog::log("Manticore json content: ".$manticoreJson); + Analog::debug("Manticore json content: ".$manticoreJson); $this->conf = json_decode($manticoreJson, true); } catch (\Exception $exception) { $this->conf = []; @@ -134,6 +134,31 @@ public function checkNodesAvailability(Resources $resources, $port, $shortCluste $this->updateNodesList($availableNodes); } + public function isAllNodesNonPrimary(Resources $resources, $qlPort): bool + { + $nodes = $resources->getPodsIp(); + + $nonPrimaryNodesCount = 0; + foreach ($nodes as $hostname => $ip) { + if ($hostname === gethostname()) { + continue; + } + + + try { + $connection = new ManticoreConnector($ip, $qlPort, $this->clusterName, 60); + if (!$connection->isClusterPrimary()) { + $nonPrimaryNodesCount++; + } + + } catch (\RuntimeException $exception) { + Analog::log("Node at $ip no more available\n".$exception->getMessage()); + } + } + + return (count($nodes)-1 === $nonPrimaryNodesCount); + } + /** * @throws \JsonException */