Пример #1
0
 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;
     }
 }
Пример #2
0
 function clear()
 {
     $this->init();
     $this->lock->acquire();
     try {
         foreach ($this->itemNames() as $name) {
             $this->zookeeper->delete("{$this->path}/{$name}");
         }
         $this->lock->release();
     } catch (Exception $e) {
         $this->lock->release();
         throw $e;
     }
 }