function capacity() { try { $childData = $this->zookeeper->getChildren($this->path); return count($childData->children); } catch (Scalr_Service_Zookeeper_Exception $e) { if ($e->getCode() == Scalr_Service_Zookeeper_Exception::NOT_FOUND) { return 0; } throw $e; } }
function initiate($quorum = null) { if ($quorum != null) { $this->originalQuorum = $this->quorum; $this->quorum = $quorum; } try { $this->zookeeper->create($this->path); } catch (Scalr_Service_Zookeeper_Exception $ignore) { } if (!$this->lock->tryAcquire()) { throw new Scalr_Service_Zookeeper_InterruptedException("Cannot get exclusive lock on election node. " . "Another election is already initiated"); } try { // Delete previous votes $childInfo = $this->zookeeper->getChildren($this->path); if ($childInfo->children) { foreach ($childInfo->children as $childName) { if (strpos($childName, "n") === 0) { $this->zookeeper->delete("{$this->path}/{$childName}"); } } } $this->isInitiator = true; $this->setStatus(self::STATUS_READY); if ($this->timeout) { $this->timeout->reset(); } $this->logger->info(sprintf("Election initiated (quorum: %d, path: %s)", $this->quorum, $this->path)); } catch (Exception $e) { // Finally release lock $this->lock->release(); throw $e; } }
function peek() { $this->init(); do { // Fetch path child nodes if (!$this->children) { //$this->logger->debug("Getting queue path child nodes"); if ($this->blocking) { $start = microtime(true); try { do { $childData = $this->zookeeper->getChildren($this->path); $loop = !$childData->children && ($this->blockingTimeout && !Scalr_Util_Timeout::reached($this->blockingTimeout, $start) || !$this->blockingTimeout); } while ($loop); } catch (Scalr_Util_TimeoutException $e) { return null; } } else { $childData = $this->zookeeper->getChildren($this->path); } if ($childData->children) { sort($childData->children); $this->children = $childData->children; } else { $this->logger->debug("Queue is empty"); if (!$this->blocking) { return null; } } } $this->logger->debug("Children capacity: " . count($this->children)); while ($childName = array_shift($this->children)) { try { $statData = $this->zookeeper->get("{$this->path}/{$childName}"); $this->zookeeper->delete("{$this->path}/{$childName}"); $this->logger->info("Peeked {$this->path}/{$childName} from queue"); return base64_decode($statData->data64); } catch (Exception $wasDeleted) { // Continue loop $this->logger->debug(sprintf("Got error while delete node %s. " . "I think it was processed by another client", "{$this->path}/{$childName}")); } } } while (!$this->children); }
private function itemNames() { $ret = array(); $childData = $this->zookeeper->getChildren($this->path); if ($childData->children) { foreach ($childData->children as $childName) { if ($this->matchElementName($childName)) { $ret[] = $childName; } } } return $ret; }