/** * Returns a view of this EnsurePath instance that does not make the last node. * i.e. if the path is "/a/b/c" only "/a/b" will be ensured * * @return view */ public EnsurePath excludingLast() { return new EnsurePath(path, helper, false); }
private byte[] internalPoll(long timeout, TimeUnit unit) throws Exception ensurePath.ensure(client.getZookeeperClient());
/** * @param client curztor client * @param path the full path to the node to cache * @param dataIsCompressed if true, data in the path is compressed */ public NodeCache(CuratorFramework client, String path, boolean dataIsCompressed) { this.client = client; this.path = path; this.dataIsCompressed = dataIsCompressed; ensurePath = client.newNamespaceAwareEnsurePath(path).excludingLast(); }
namespaceCurator.start(); EnsurePath namespaceEnsurePath = new EnsurePath(namespace); namespaceEnsurePath.ensure(namespaceCurator.getZookeeperClient()); namespaceCurator.close(); EnsurePath ensurePath = new EnsurePath(NODE_STATES_PATH); ensurePath.ensure(this.curator.getZookeeperClient()); ensurePath = new EnsurePath(CLUSTER_PATH); ensurePath.ensure(this.curator.getZookeeperClient());
/** * NOTE: this is a BLOCKING method. Rebuild the internal cache for the given node by querying * for all needed data WITHOUT generating any events to send to listeners. * * @param fullPath full path of the node to rebuild * @throws Exception errors */ public void rebuildNode(String fullPath) throws Exception { Preconditions.checkArgument(ZKPaths.getPathAndNode(fullPath).getPath().equals(path), "Node is not part of this cache: " + fullPath); Preconditions.checkState(!executorService.isShutdown(), "cache has been closed"); ensurePath.ensure(client.getZookeeperClient()); internalRebuildNode(fullPath); // this is necessary so that any updates that occurred while rebuilding are taken // have to rebuild entire tree in case this node got deleted in the interim offerOperation(new RefreshOperation(this, RefreshMode.FORCE_GET_DATA_AND_STAT)); }
EnsurePath newNamespaceAwareEnsurePath(String path) { return new EnsurePath(fixForNamespace(path)); } }
/** * Creates in mutex promotion mode. The optimistic lock will be tried first using * the given retry policy. If the increment does not succeed, a {@link InterProcessMutex} will be tried * with its own retry policy * * @param client the client * @param path path to hold the value * @param retryPolicy the retry policy to use * @param promotedToLock the arguments for the mutex promotion */ public DistributedAtomicValue(CuratorFramework client, String path, RetryPolicy retryPolicy, PromotedToLock promotedToLock) { this.client = client; this.path = path; this.retryPolicy = retryPolicy; this.promotedToLock = promotedToLock; mutex = (promotedToLock != null) ? new InterProcessMutex(client, promotedToLock.getPath()) : null; ensurePath = client.newNamespaceAwareEnsurePath(path).excludingLast(); }
String fixForNamespace(String path) { if ( ensurePath != null ) { try { ensurePath.ensure(client.getZookeeperClient()); } catch ( Exception e ) { client.logError("Ensure path threw exception", e); } } return ZKPaths.fixForNamespace(namespace, path); }
NamespaceImpl(CuratorFrameworkImpl client, String namespace) { if ( namespace != null ) { try { PathUtils.validatePath("/" + namespace); } catch ( IllegalArgumentException e ) { throw new IllegalArgumentException("Invalid namespace: " + namespace); } } this.client = client; this.namespace = namespace; ensurePath = (namespace != null) ? new EnsurePath(ZKPaths.makePath("/", namespace)) : null; }
client.newNamespaceAwareEnsurePath(queuePath).ensure(client.getZookeeperClient());
/** * Same as {@link #start()} but gives the option of doing an initial build * * @param buildInitial if true, {@link #rebuild()} will be called before this method * returns in order to get an initial view of the node * @throws Exception errors */ public void start(boolean buildInitial) throws Exception { Preconditions.checkState(state.compareAndSet(State.LATENT, State.STARTED), "Cannot be started more than once"); ensurePath.ensure(client.getZookeeperClient()); client.getConnectionStateListenable().addListener(connectionStateListener); if ( buildInitial ) { internalRebuild(); } reset(); }
private boolean getCurrentValue(MutableAtomicValue<byte[]> result, Stat stat) throws Exception { boolean createIt = false; try { ensurePath.ensure(client.getZookeeperClient()); result.preValue = client.getData().storingStatIn(stat).forPath(path); } catch ( KeeperException.NoNodeException e ) { result.preValue = null; createIt = true; } return createIt; }
void refresh(final RefreshMode mode) throws Exception { ensurePath.ensure(client.getZookeeperClient()); final BackgroundCallback callback = new BackgroundCallback() { @Override public void processResult(CuratorFramework client, CuratorEvent event) throws Exception { processChildren(event.getChildren(), mode); } }; client.getChildren().usingWatcher(childrenWatcher).inBackground(callback).forPath(path); }
/** * Inserts data into queue. * * @param data the data * @return true if data was successfully added * @throws Exception errors */ public boolean offer(byte[] data) throws Exception { ensurePath.ensure(client.getZookeeperClient()); String thisPath = ZKPaths.makePath(path, PREFIX); client.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath(thisPath, data); return true; }
/** * NOTE: this is a BLOCKING method. Completely rebuild the internal cache by querying * for all needed data WITHOUT generating any events to send to listeners. * * @throws Exception errors */ public void rebuild() throws Exception { Preconditions.checkState(!executorService.isShutdown(), "cache has been closed"); ensurePath.ensure(client.getZookeeperClient()); clear(); List<String> children = client.getChildren().forPath(path); for ( String child : children ) { String fullPath = ZKPaths.makePath(path, child); internalRebuildNode(fullPath); if ( rebuildTestExchanger != null ) { rebuildTestExchanger.exchange(new Object()); } } // this is necessary so that any updates that occurred while rebuilding are taken offerOperation(new RefreshOperation(this, RefreshMode.FORCE_GET_DATA_AND_STAT)); }
private byte[] internalElement(boolean removeIt, Watcher watcher) throws Exception ensurePath.ensure(client.getZookeeperClient());
/** * Forcibly sets the value any guarantees of atomicity. * * @param newValue the new value * @throws Exception ZooKeeper errors */ public void forceSet(byte[] newValue) throws Exception { try { ensurePath.ensure(client.getZookeeperClient()); client.setData().forPath(path, newValue); } catch ( KeeperException.NoNodeException dummy ) { try { client.create().forPath(path, newValue); } catch ( KeeperException.NodeExistsException dummy2 ) { client.setData().forPath(path, newValue); } } }