Skip to content

Commit

Permalink
Dev (#4)
Browse files Browse the repository at this point in the history
* Added skip self-switcher in getting the oldest node
  • Loading branch information
djklim87 authored Oct 20, 2022
1 parent 1aecdf2 commit 64d9b00
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
11 changes: 5 additions & 6 deletions src/K8s/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -53,7 +52,7 @@ public function getPods(): array
}
}

if (!$readyCondition){
if (!$readyCondition) {
continue;
}
}
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -144,7 +143,7 @@ public function getPodsHostnames(): array
}


public function getMinAvailableReplica()
public function getMinAvailableReplica($skipSelf = true)
{
$podsList = $this->getPodsHostnames();
if ($podsList === []) {
Expand All @@ -156,7 +155,7 @@ public function getMinAvailableReplica()

$min = array_shift($podsList);

if ($min === gethostname()) {
if ($skipSelf && $min === gethostname()) {
// skip itself
$min = array_shift($podsList);
}
Expand Down
27 changes: 26 additions & 1 deletion src/Manticore/ManticoreJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 64d9b00

Please sign in to comment.