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); }
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; } } } } }
@Override public void ensure(String path) throws Exception { curator.newNamespaceAwareEnsurePath(path).ensure(curator.getZookeeperClient()); }
@Override public void ensure(String path) throws Exception { curator.newNamespaceAwareEnsurePath(path).ensure(curator.getZookeeperClient()); }
@Override public CuratorFramework newClient(final String connectString, final int sessionTimeoutMs, final int connectionTimeoutMs, final RetryPolicy retryPolicy, final ACLProvider aclProvider, final List<AuthInfo> authorization) { final CuratorFramework curator = mock(CuratorFramework.class); final RetryLoop retryLoop = mock(RetryLoop.class); when(retryLoop.shouldContinue()).thenReturn(false); final CuratorZookeeperClient czkClient = mock(CuratorZookeeperClient.class); when(czkClient.newRetryLoop()).thenReturn(retryLoop); when(curator.getZookeeperClient()).thenReturn(czkClient); @SuppressWarnings("unchecked") final Listenable<ConnectionStateListener> mockListener = (Listenable<ConnectionStateListener>) mock(Listenable.class); when(curator.getConnectionStateListenable()).thenReturn(mockListener); final GetChildrenBuilder builder = mock(GetChildrenBuilder.class); when(curator.getChildren()).thenReturn(builder); try { when(builder.forPath(anyString())).thenThrow( new KeeperException.ConnectionLossException()); } catch (Exception ignored) { // never throws } when(curator.newNamespaceAwareEnsurePath(anyString())).thenReturn(mock(EnsurePath.class)); return curator; } }
protected List<String> setupDefaultMaster(final int offset, String... args) throws Exception { curator.newNamespaceAwareEnsurePath(Paths.configHosts()).ensure(curator.getZookeeperClient()); curator.newNamespaceAwareEnsurePath(Paths.configJobs()).ensure(curator.getZookeeperClient()); curator.newNamespaceAwareEnsurePath(Paths.configJobRefs()).ensure(curator.getZookeeperClient()); curator.newNamespaceAwareEnsurePath(Paths.statusHosts()).ensure(curator.getZookeeperClient()); curator.newNamespaceAwareEnsurePath(Paths.statusMasters()).ensure(curator.getZookeeperClient()); curator.newNamespaceAwareEnsurePath(Paths.historyJobs()).ensure(curator.getZookeeperClient()); curator.newNamespaceAwareEnsurePath(Paths.configId(zkClusterId)) .ensure(curator.getZookeeperClient());
@Test public void testZooKeeperClient() throws Exception { // Create the cluster ID node zk().curatorWithSuperAuth().newNamespaceAwareEnsurePath(Paths.configId(zkClusterId)) .ensure(zk().curatorWithSuperAuth().getZookeeperClient()); // We need to create a new curator because ZooKeeperClient will try to start it, // and zk().curator() has already been started. final ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3); final CuratorFramework curator = CuratorFrameworkFactory.builder() .retryPolicy(retryPolicy) .connectString(zk().connectString()) .build(); final ZooKeeperClient client = new DefaultZooKeeperClient(curator, zkClusterId); client.start(); // This should work since the cluster ID exists client.create("/test"); // Now let's remove the cluster ID client.delete(Paths.configId(zkClusterId)); // Sleep so the watcher thread in ZooKeeperClient has a chance to update state Thread.sleep(500); // Try the same operation again, and it should fail this time try { client.ensurePath(Paths.configJobs()); fail("ZooKeeper operation should have failed because cluster ID was removed"); } catch (IllegalStateException ignore) { // ignored } }
curator.newNamespaceAwareEnsurePath(loadQueueLocation).ensure(curator.getZookeeperClient()); curator.newNamespaceAwareEnsurePath(servedSegmentsLocation).ensure(curator.getZookeeperClient()); curator.newNamespaceAwareEnsurePath(liveSegmentsLocation).ensure(curator.getZookeeperClient());
nullNsFw.newNamespaceAwareEnsurePath("/" + zkClient.getNamespace()); try { ensureNs.ensure(nullNsFw.getZookeeperClient());
/** * @param client the client * @param path path to watch * @param executorService ExecutorService to use for the ZooKeeperGroup's background thread */ public ZooKeeperGroup(CuratorFramework client, String path, Class<T> clazz, final ExecutorService executorService) { this.client = client; this.path = path; this.clazz = clazz; this.executorService = executorService; ensurePath = client.newNamespaceAwareEnsurePath(path); }
@Override @Deprecated public EnsurePath newNamespaceAwareEnsurePath(String path) { return inner.newNamespaceAwareEnsurePath(path); }
public DistributedTableSerializer(TableSerializer tableSerializer, CuratorFramework curator, String basePath) { _source = checkNotNull(tableSerializer, "tableSerializer"); _curator = checkNotNull(curator, "curator"); _basePath = checkNotNull(basePath, "basePath"); _ensurePath = curator.newNamespaceAwareEnsurePath(basePath); }
public DistributedTableSerializer(TableSerializer tableSerializer, CuratorFramework curator, String basePath) { _source = checkNotNull(tableSerializer, "tableSerializer"); _curator = checkNotNull(curator, "curator"); _basePath = checkNotNull(basePath, "basePath"); _ensurePath = curator.newNamespaceAwareEnsurePath(basePath); }
/** * @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(); }
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()); }
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()); }
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()); }
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()); }
@Override public void ensure(String path) throws Exception { curator.newNamespaceAwareEnsurePath(path).ensure(curator.getZookeeperClient()); }
/** * Starts the client. This call ensures the creation of the root path. * * @throws Exception if cache fails to start or root path creation fails. * @see #close() */ public void start() throws Exception { curator.newNamespaceAwareEnsurePath(root).ensure(curator.getZookeeperClient()); // ensure root is created getCache().start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE); //build cache at start up, to ensure we get correct results right away }