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(); } }
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; } }
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; } }
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) { } } }
function put($data) { $this->init(); $pathData = $this->zookeeper->create("{$this->path}/element", $data, array("sequence" => true)); }