Refine search
if (!isExternalClient) { try { zkClient.start(); } catch (Exception e) { throw new IOException("Could not start Curator Framework", e); CuratorFramework nullNsFw = zkClient.usingNamespace(null); EnsurePath ensureNs = nullNsFw.newNamespaceAwareEnsurePath("/" + zkClient.getNamespace()); try { ensureNs.ensure(nullNsFw.getZookeeperClient()); } catch (Exception e) { throw new IOException("Could not create namespace", e);
@Override protected void run(Bootstrap<EmoConfiguration> bootstrap, Namespace namespace, EmoConfiguration configuration) throws Exception { String hostString = namespace.getString("host"); String serviceName = namespace.getString("service"); CuratorFramework curator = configuration.getZooKeeperConfiguration().newCurator(); curator.start(); HostAndPort host = HostAndPort.fromString(hostString).withDefaultPort(9160); ServiceEndPoint endPoint = new ServiceEndPointBuilder() .withServiceName(serviceName) .withId(host.toString()) .build(); String dir = ZKPaths.makePath("ostrich", endPoint.getServiceName()); String path = ZKPaths.makePath(dir, endPoint.getId()); curator.newNamespaceAwareEnsurePath(dir).ensure(curator.getZookeeperClient()); try { curator.delete().forPath(path); System.out.println("Deleted."); } catch (KeeperException.NoNodeException e) { System.out.println("Not found."); } curator.close(); } }
void refresh(final RefreshMode mode) throws Exception { ensurePath.ensure(client.getZookeeperClient()); List<String> children = client.getChildren().usingWatcher(childrenWatcher).forPath(path); Collections.sort(children, new Comparator<String>() { @Override public int compare(String left, String right) { return left.compareTo(right); } }); processChildren(children, mode); }
public void createCurrentTSOZNode() throws Exception { EnsurePath path = zkClient.newNamespaceAwareEnsurePath(CURRENT_TSO_PATH); path.ensure(zkClient.getZookeeperClient()); Stat stat = zkClient.checkExists().forPath(CURRENT_TSO_PATH); assert (stat != null); LOG.info("Path {} ensured", path.getPath()); }
public static void registerForChanges(CuratorFramework curatorFramework, String... basePaths) { if (curatorFramework.getState() != CuratorFrameworkState.STARTED) { curatorFramework.start(); } try { curatorFramework.getZookeeperClient().blockUntilConnectedOrTimedOut(); } catch (InterruptedException e) { log.error(e.getMessage(), e); Throwables.propagate(e); } for (String basePath : basePaths) { log.debug("Adding watched path: " + basePath); try { new EnsurePath(basePath).ensure(curatorFramework.getZookeeperClient()); } catch (Exception e) { log.error(e.getMessage(), e); Throwables.propagate(e); } } log.debug("exiting registerForChanges"); } }
@Override public void ensure(String path) throws Exception { curator.newNamespaceAwareEnsurePath(path).ensure(curator.getZookeeperClient()); }
public static Long getOffsetFromZooKeeper(CuratorFramework curatorClient, String groupId, String topic, int partition) throws Exception { ZKGroupTopicDirs topicDirs = new ZKGroupTopicDirs(groupId, topic); String path = topicDirs.consumerOffsetDir() + "/" + partition; curatorClient.newNamespaceAwareEnsurePath(path).ensure(curatorClient.getZookeeperClient()); byte[] data = curatorClient.getData().forPath(path); if (data == null) { return null; } else { String asString = new String(data, ConfigConstants.DEFAULT_CHARSET); if (asString.length() == 0) { return null; } else { try { return Long.valueOf(asString); } catch (NumberFormatException e) { LOG.error( "The offset in ZooKeeper for group '{}', topic '{}', partition {} is a malformed string: {}", groupId, topic, partition, asString); return null; } } } } }
public static void setOffsetInZooKeeper(CuratorFramework curatorClient, String groupId, String topic, int partition, long offset) throws Exception { ZKGroupTopicDirs topicDirs = new ZKGroupTopicDirs(groupId, topic); String path = topicDirs.consumerOffsetDir() + "/" + partition; curatorClient.newNamespaceAwareEnsurePath(path).ensure(curatorClient.getZookeeperClient()); byte[] data = Long.toString(offset).getBytes(ConfigConstants.DEFAULT_CHARSET); curatorClient.setData().forPath(path, data); }
private void ensureZookeeperPath() throws Exception { new EnsurePath(GONDOLA_HOSTS).ensure(client.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().startsWith(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 TreeRefreshOperation(this, path, RefreshMode.FORCE_GET_DATA_AND_STAT)); }
curator = getCuratorFramework(curator); curator.getConnectionStateListenable().addListener(listener); setStatus(Status.CONNECTED); curatorConsumerCache = new PathChildrenCache(curator, fBaseZkPath, true); EnsurePath ensurePath = new EnsurePath(fBaseZkPath); ensurePath.ensure(curator.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(); }
@Override /** {@inheritDoc} */ public void ensurePath(final String path, final boolean excludingLast) throws KeeperException { assertClusterIdFlagTrue(); EnsurePath ensurePath = client.newNamespaceAwareEnsurePath(path); if (excludingLast) { ensurePath = ensurePath.excludingLast(); } try { ensurePath.ensure(client.getZookeeperClient()); } catch (Exception e) { propagateIfInstanceOf(e, KeeperException.class); throw propagate(e); } }
/** * Make directories if necessary * @param dir the dir * @return return true if mkdirs successfully, or throw ZkException */ public Boolean mkdirs(String dir){ try { EnsurePath clientAppPathExist = new EnsurePath("/" + client.getNamespace() + slash(dir)); clientAppPathExist.ensure(client.getZookeeperClient()); return Boolean.TRUE; } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } }
/** * @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 NodeCacheExtended(CuratorFramework client, String path, boolean dataIsCompressed) { this.client = client; this.path = path; this.dataIsCompressed = dataIsCompressed; ensurePath = client.newNamespaceAwareEnsurePath(path).excludingLast(); }
EnsurePath newNamespaceAwareEnsurePath(String path) { return new EnsurePath(fixForNamespace(path, false), client.getAclProvider()); } }
void refresh(final String path, final RefreshMode mode) throws Exception { ensurePath.ensure(client.getZookeeperClient()); Stat stat = new Stat(); try { List<String> children = client.getChildren().storingStatIn(stat).usingWatcher(watcher).forPath(path); processChildren(path, children, mode); updateIfNeeded(path, stat, children); } catch (KeeperException.NoNodeException e) { // node no longer exists - ignore log.debug("Node {} is no longer available.", path); } }
private void validateZKPath(String zkPath) throws Exception { EnsurePath path = zkClient.newNamespaceAwareEnsurePath(zkPath); path.ensure(zkClient.getZookeeperClient()); Stat stat = zkClient.checkExists().forPath(zkPath); Preconditions.checkNotNull(stat); LOG.info("Path {} ensured", path.getPath()); }
@Override public void ensure(String path) throws Exception { curator.newNamespaceAwareEnsurePath(path).ensure(curator.getZookeeperClient()); }
/** * Updates the table layout ID in ZooKeeper. This will notify all watchers of the table layout. * * <p> * The caller must ensure proper locking of table layout update operations. * </p> * * @param zkClient connection to ZooKeeper. * @param tableURI Notify the users of the table with this URI. * @param layoutID Encoded layout update for the table with the specified URI. * @throws IOException on unrecoverable ZooKeeper error. */ public static void setTableLayout( final CuratorFramework zkClient, final FijiURI tableURI, final String layoutID) throws IOException { final String path = ZooKeeperUtils.getTableLayoutFile(tableURI).getPath(); try { zkClient.newNamespaceAwareEnsurePath(path).ensure(zkClient.getZookeeperClient()); zkClient.setData().forPath(path, Bytes.toBytes(layoutID)); LOG.debug("Updated layout ID for table {} to {}.", tableURI, layoutID); } catch (Exception e) { wrapAndRethrow(e); } }