/** * Switch to the given node and map the cluster from its HELLO * * @param Node $node */ private function switchToNode(Node $node) { $nodeId = $node->getId(); // Return early if we're trying to switch to the current node. if ($this->nodeId === $nodeId) { // But return early only if the current node is connected to Disque. // If it is disconnected, we want to overwrite it with the node // from the method argument, because that one is connected. if ($this->getCurrentNode()->isConnected()) { return; } // Copy the stats from the now-disconnected node object $this->copyNodeStats($this->getCurrentNode(), $node); } $this->resetNodeCounters(); $this->nodeId = $nodeId; $this->nodes[$nodeId] = $node; $this->revealClusterFromHello($node); }
/** * Calculate the node priority from its job count, stick to the current node * * As the priority is based on the number of jobs, higher is better. * * @param Node $node * @param string $currentNodeId * * @return float Node priority */ private function calculateNodePriority(Node $node, $currentNodeId) { $priority = $node->getJobCount(); if ($node->getId() === $currentNodeId) { $margin = 1 + $this->marginToSwitch; $priority = $priority * $margin; } // Apply a weight determined by the node priority as assigned by Disque. // Priority 1 means the node is healthy. // Priority 10 to 100 means the node is probably failing, or has failed $disquePriority = $node->getPriority(); // Disque node priority should never be lower than 1, but let's be sure if ($disquePriority < Node::PRIORITY_OK) { $disquePriorityWeight = 1; } elseif (Node::PRIORITY_OK <= $disquePriority && $disquePriority < Node::PRIORITY_POSSIBLE_FAILURE) { // Node is OK, but Disque may have assigned a lower priority to it // We use the base-10 logarithm in the formula, so priorities // 1 to 10 transform into a weight of 1 to 0.5. When Disque starts // using more priority steps, priority 9 will discount about a half // of the job count. $disquePriorityWeight = 1 / (1 + log10($disquePriority)); } else { // Node is failing, or it has failed $disquePriorityWeight = 0; } $priority = $priority * $disquePriorityWeight; return (double) $priority; }
/** * Switch to the given node and map the cluster from its HELLO * * @param Node $node */ private function switchToNode(Node $node) { $nodeId = $node->getId(); if ($this->nodeId === $nodeId) { return; } $this->resetNodeCounters(); $this->nodeId = $nodeId; $this->nodes[$nodeId] = $node; $this->revealClusterFromHello($node); }