示例#1
0
文件: Barrier.php 项目: mheydt/scalr
 function enter($data = null)
 {
     // who will be first?
     $iAmFirst = false;
     try {
         $this->zookeeper->create($this->path);
         $iAmFirst = true;
     } catch (Scalr_Service_Zookeeper_Exception $e) {
         if ($e->getCode() != Scalr_Service_Zookeeper_Exception::CONFLICT) {
             throw $e;
         }
     }
     // create child node
     $this->zookeeper->create("{$this->path}/n", $data ? serialize($data) : null, array(Scalr_Service_Zookeeper::OPT_SEQUENCE => true));
     // wait while all nodes will enter barrier
     try {
         while ($this->timeout && !$this->timeout->reached() || !$this->timeout) {
             if ($iAmFirst && $this->capacity() >= $this->quorum || !$iAmFirst && !$this->zookeeper->exists($this->path)) {
                 break;
             } else {
                 Scalr_Util_Timeout::sleep(100);
             }
         }
     } catch (Exception $e) {
         // Finally delete barrier znode
         if ($this->autoDelete) {
             $this->delete();
         }
         throw $e;
     }
     if ($this->autoDelete) {
         $this->delete();
     }
 }
示例#2
0
 function tryAcquire()
 {
     try {
         $this->zookeeper->create($this->path);
         return true;
     } catch (Scalr_Service_Zookeeper_Exception $e) {
         if ($e->getCode() == Scalr_Service_Zookeeper_Exception::CONFLICT) {
             return false;
         }
         throw $e;
     }
 }
示例#3
0
文件: Set.php 项目: mheydt/scalr
 function add($item)
 {
     $this->init();
     $this->lock->acquire();
     try {
         $ret = false;
         if (!$this->contains0($item)) {
             $ret = true;
             $this->zookeeper->create("{$this->path}/element", "{$item}", array(Scalr_Service_Zookeeper::OPT_SEQUENCE => true));
         }
         $this->lock->release();
         return $ret;
     } catch (Exception $e) {
         $this->lock->release();
         throw $e;
     }
 }
示例#4
0
 private function setStatus($status)
 {
     if ($status != self::STATUS_NOTSET) {
         try {
             $this->zookeeper->set("{$this->path}/status", serialize($status));
         } catch (Scalr_Service_Zookeeper_Exception $e) {
             if ($e->getCode() == Scalr_Service_Zookeeper_Exception::NOT_FOUND) {
                 $this->zookeeper->create("{$this->path}/status", serialize($status));
                 return;
             }
             throw $e;
         }
     } else {
         try {
             $this->zookeeper->delete("{$this->path}/status");
         } catch (Scalr_Service_Zookeeper_Exception $ignore) {
         }
     }
 }
示例#5
0
 function put($data)
 {
     $this->init();
     $pathData = $this->zookeeper->create("{$this->path}/element", $data, array("sequence" => true));
 }