private boolean watchForData() throws InterruptedException, KeeperException { if (LOG.isTraceEnabled()) { LOG.trace(String.format("Getting value for %s", nodePath)); } try { synchronized (this) { value = decode(zk.getData(nodePath, watcher, stat)); return true; } } catch (KeeperException.NoNodeException e) { watchForCreation(); return false; } }
public void ensureCreated(CreateMode createMode) throws InterruptedException, KeeperException { zk.ensureCreated(nodePath, encode(initialValue), createMode); } }
private void watchForCreation() throws InterruptedException, KeeperException { synchronized (this) { value = null; stat = new Stat(); } if (zk.exists(nodePath, watcher) != null) { watchForData(); } }
protected WatchedNode(final ZooKeeperPlus zk, final String nodePath, final boolean waitForCreation, final CreateMode createMode, final T initialValue, final T emptyValue) throws KeeperException, InterruptedException { this.zk = zk; this.nodePath = nodePath; this.initialValue = initialValue; this.emptyValue = emptyValue; // Immediately try to load the data, if it fails, then optionally create and wait if (!watchForData()) { // Optionally create the node if (createMode != null) { zk.create(nodePath, encode(initialValue), createMode); } // Optionally wait for the node's creation if (waitForCreation) { // Cannot wait for creation of sequential nodes (path is not yet known) if (createMode == CreateMode.EPHEMERAL_SEQUENTIAL || createMode == CreateMode.PERSISTENT_SEQUENTIAL) { throw new RuntimeException("Cannot wait for creation of sequential nodes"); } NodeCreationBarrier.block(zk, nodePath); watchForData(); } } }
public void set(T v) throws KeeperException, InterruptedException { zk.setData(nodePath, encode(v), -1); }
public void update(WatchedNodeUpdater<T> updater) throws InterruptedException, KeeperException { ExponentialBackoff backoff = new ExponentialBackoff(); while (true) { try { synchronized (this) { zk.setData(nodePath, encode(updater.update(value)), stat.getVersion()); } } catch (KeeperException.BadVersionException e) { // If we did not update from the latest version, backoff and retry. if (LOG.isDebugEnabled()) { LOG.debug("Did not have latest version to update node " + nodePath + ". Backing off for " + backoff.getBackoffMs() + " ms"); } backoff.backoff(); continue; } break; } }