@Override public boolean isConnected() { return client.getZookeeperClient().isConnected(); }
@Override public boolean isConnected() { return client.getZookeeperClient().isConnected(); }
@Override public String getServers() { return curator.getZookeeperClient().getCurrentConnectionString(); }
@Override public boolean isConnected() { return client.getZookeeperClient().isConnected(); }
@Override public boolean isConnected() { return client.getZookeeperClient().isConnected(); }
/** * Checks if is connected. * * @return true, if is connected */ public boolean isConnected() { return zkClient.getZookeeperClient().isConnected(); }
public ZooKeeperHealthChecker(final ZooKeeperClient zooKeeperClient) { this.client = zooKeeperClient.getCuratorFramework().getZookeeperClient(); }
@Override public ZooKeeper.States getZookeeperState() { try { return curator.getZookeeperClient().getZooKeeper().getState(); } catch ( Exception e ) { throw new RuntimeException(e); } }
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; } }
private void waitForBothWorkersToHaveUnackedTasks() throws Exception { while (remoteTaskRunner.getWorkersWithUnacknowledgedTask().size() < 2) { Thread.sleep(5); } ZooKeeper zk = rtrTestUtils.getCuratorFramework().getZookeeperClient().getZooKeeper(); while (zk.getChildren(rtrTestUtils.tasksPath + "/worker0", false).size() < 1 || zk.getChildren(rtrTestUtils.tasksPath + "/worker1", false).size() < 1) { Thread.sleep(5); } } }
private void waitForOneWorkerToHaveUnackedTasks() throws Exception { while (remoteTaskRunner.getWorkersWithUnacknowledgedTask().size() < 1) { Thread.sleep(5); } ZooKeeper zk = rtrTestUtils.getCuratorFramework().getZookeeperClient().getZooKeeper(); while (zk.getChildren(rtrTestUtils.tasksPath + "/worker0", false).size() < 1 && zk.getChildren(rtrTestUtils.tasksPath + "/worker1", false).size() < 1) { Thread.sleep(5); } }
@Override public void processResult(CuratorFramework client, CuratorEvent event) throws Exception { WatchedEvent fakeEvent = new WatchedEvent(Watcher.Event.EventType.None, curator.getZookeeperClient().isConnected() ? Watcher.Event.KeeperState.SyncConnected : Watcher.Event.KeeperState.Disconnected, null); watcher.process(fakeEvent); } };
@Override public ZooKeeper.States getState() throws KeeperException { assertClusterIdFlagTrue(); try { return client.getZookeeperClient().getZooKeeper().getState(); } catch (Exception e) { throwIfInstanceOf(e, KeeperException.class); throw new RuntimeException(e); } }
@Override public void delete(final String path, final int version) throws KeeperException { assertClusterIdFlagTrue(); final String namespace = emptyToNull(client.getNamespace()); final String namespacedPath = ZKPaths.fixForNamespace(namespace, path); try { client.getZookeeperClient().getZooKeeper().delete(namespacedPath, version); } catch (Exception e) { throwIfInstanceOf(e, KeeperException.class); throw new RuntimeException(e); } }
@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 } }
KillSession.kill(curator.getZookeeperClient().getZooKeeper(), server.getConnectString());