private void checkClusterIdExists(final String id, final String checker) { try { final Stat stat = client.checkExists().usingWatcher(watcher).forPath(Paths.configId(id)); final boolean exists = stat != null; clusterIdExists.set(exists); log.info("Cluster ID {} {} when checked by {}", id, exists ? "exists" : "does not exist", checker); } catch (Exception e) { clusterIdExists.set(false); log.error("Exception while checking ZooKeeper cluster ID {}", clusterId, e); } } }
@Test public void testMaster() throws Exception { startDefaultMaster("--zk-cluster-id=" + zkClusterId); final HeliosClient client = defaultClient(); // This should succeed since the cluster ID was created by SystemTestBase client.jobs().get(); // Delete the cluster ID zk().curatorWithSuperAuth().delete().forPath(Paths.configId(zkClusterId)); // Call jobs again, and this time it should throw an exception because the cluster ID is gone try { client.jobs().get(); } catch (ExecutionException e) { assertThat(e.getMessage(), containsString("500")); } }
@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(Paths.statusMasters()).ensure(curator.getZookeeperClient()); curator.newNamespaceAwareEnsurePath(Paths.historyJobs()).ensure(curator.getZookeeperClient()); curator.newNamespaceAwareEnsurePath(Paths.configId(zkClusterId)) .ensure(curator.getZookeeperClient());
private List<String> getChildren() throws Exception { final Stat childrenStat = new Stat(); while (true) { final List<String> possibleChildren = curator.getChildren() .storingStatIn(childrenStat) .usingWatcher(childrenWatcher) .forPath(path); if (clusterId == null) { // Do not do any checks if the clusterId is not specified on the command line. return possibleChildren; } try { curator.inTransaction() .check().forPath(Paths.configId(clusterId)).and() .check().withVersion(childrenStat.getVersion()).forPath(path).and() .commit(); } catch (KeeperException.BadVersionException e) { // Jobs have somehow changed while we were creating the transaction, retry. continue; } return possibleChildren; } }
private void checkClusterIdExists(final String id, final String checker) { try { final Stat stat = client.checkExists().usingWatcher(watcher).forPath(Paths.configId(id)); final boolean exists = stat != null; clusterIdExists.set(exists); log.info("Cluster ID {} {} when checked by {}", id, exists ? "exists" : "does not exist", checker); } catch (Exception e) { clusterIdExists.set(false); log.error("Exception while checking ZooKeeper cluster ID {}", clusterId, e); } } }
private List<String> getChildren() throws Exception { Stat childrenStat = new Stat(); while (true) { final List<String> possibleChildren = curator.getChildren() .storingStatIn(childrenStat) .usingWatcher(childrenWatcher) .forPath(path); if (clusterId == null) { // Do not do any checks if the clusterId is not specified on the command line. return possibleChildren; } try { curator.inTransaction() .check().forPath(Paths.configId(clusterId)).and() .check().withVersion(childrenStat.getVersion()).forPath(path).and() .commit(); } catch (KeeperException.BadVersionException e) { // Jobs have somehow changed while we were creating the transaction, retry. continue; } return possibleChildren; } }