@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 } }
private void write(final String node, final byte[] data) throws KeeperException { final ZooKeeperClient client = client("write"); final String nodePath = ZKPaths.makePath(path, node); if (client.stat(nodePath) != null) { log.debug("setting node: {}", nodePath); client.setData(nodePath, data); } else { log.debug("creating node: {}", nodePath); client.createAndSetData(nodePath, data); } }
static void initializeAcl(final String zooKeeperConnectionString, final String zooKeeperClusterId, final String masterUser, final String masterPassword, final String agentUser, final String agentPassword) throws KeeperException { final ACLProvider aclProvider = heliosAclProvider( masterUser, digest(masterUser, masterPassword), agentUser, digest(agentUser, agentPassword)); final List<AuthInfo> authorization = Lists.newArrayList(new AuthInfo( "digest", String.format("%s:%s", masterUser, masterPassword).getBytes())); final RetryPolicy zooKeeperRetryPolicy = new ExponentialBackoffRetry(1000, 3); final CuratorFramework curator = new CuratorClientFactoryImpl().newClient( zooKeeperConnectionString, (int) TimeUnit.SECONDS.toMillis(60), (int) TimeUnit.SECONDS.toMillis(15), zooKeeperRetryPolicy, aclProvider, authorization); final ZooKeeperClient client = new DefaultZooKeeperClient(curator, zooKeeperClusterId); try { client.start(); initializeAclRecursive(client, "/", aclProvider); } finally { client.close(); } }
@Override public void start() { client.start(); client.getConnectionStateListenable().addListener( (client, newState) -> reporter.connectionStateChanged(newState)); }
private void syncChecked() throws KeeperException { final ZooKeeperClient client = client("sync"); final List<String> nodes = client.getChildren(path); final Map<String, byte[]> snapshot = entries.get(); for (final String node : nodes) { final String nodePath = ZKPaths.makePath(path, node); final byte[] data = client.getData(nodePath); remote.put(node, data); if (remoteData == null) { log.debug("sync: creating node {}", nodePath); client.createAndSetData(nodePath, localData); remote.put(node, localData); } else if (!Arrays.equals(remoteData, localData)) { log.debug("sync: updating node {}", nodePath); client.setData(nodePath, localData); remote.put(node, localData); final String nodePath = ZKPaths.makePath(path, node); log.debug("sync: deleting node {}", nodePath); client.delete(nodePath); remote.remove(node);
client.getNode(taskPath); client.transaction(operations); log.info("deployed {}: {} (retry={})", deployment, host, count); } catch (NoNodeException e) { if (client.exists(taskCreationPath) != null) { if (client.stat(taskPath) != null) { throw new JobAlreadyDeployedException(host, id);
final String hostInfoPath = Paths.statusHostInfo(name); final Stat stat = client.exists(idPath); if (stat == null) { log.debug("Agent id node not present, registering agent {}: {}", id, name); ZooKeeperRegistrarUtil.registerHost(client, idPath, name, id); } else { final byte[] bytes = client.getData(idPath); final String existingId = bytes == null ? "" : new String(bytes, UTF_8); if (!id.equals(existingId)) { final Stat hostInfoStat = client.stat(hostInfoPath); if (hostInfoStat != null) { final long mtime = hostInfoStat.getMtime(); final String upPath = Paths.statusHostUp(name); log.debug("Creating up node: {}", upPath); client.ensurePath(upPath, true); upNode = client.persistentEphemeralNode(upPath, EPHEMERAL, EMPTY_BYTES); upNode.start();
final ZooKeeperClient client = provider.get("removeDeploymentGroup"); try { client.ensurePath(Paths.configDeploymentGroups()); client.ensurePath(Paths.statusDeploymentGroups()); client.ensurePath(Paths.statusDeploymentGroupTasks()); if (client.exists(path) == null) { operations.add(create(path)); client.transaction(operations); } catch (final NoNodeException e) { throw new DeploymentGroupDoesNotExistException(name);
final String idPath = Paths.configHostId(name); final Stat stat = client.exists(idPath); if (stat == null) { log.debug("Agent id node not present, registering agent {}: {}", id, name); client.ensurePath(Paths.configHost(name)); client.ensurePath(Paths.configHost(name)); client.ensurePath(Paths.configHostJobs(name)); client.ensurePath(Paths.configHostPorts(name)); client.ensurePath(Paths.statusHost(name)); client.ensurePath(Paths.statusHostJobs(name)); client.createAndSetData(idPath, id.getBytes(UTF_8)); } else { final byte[] bytes = client.getData(idPath); final String existingId = bytes == null ? "" : new String(bytes, UTF_8); if (!id.equals(existingId)) { final String upPath = Paths.statusHostUp(name); log.debug("Creating up node: {}", upPath); upNode = client.persistentEphemeralNode(upPath, EPHEMERAL, EMPTY_BYTES); upNode.start();
client.ensurePath(Paths.configDeploymentGroups()); client.ensurePath(Paths.statusDeploymentGroups()); client.transaction( create(Paths.configDeploymentGroup(deploymentGroup.getName()), deploymentGroup), create(Paths.statusDeploymentGroup(deploymentGroup.getName())),
item.getTimestamp()); client.ensurePath(historyPath, true); client.createAndSetData(historyPath, item.getStatus().toJsonBytes()); final List<String> events = client.getChildren(Paths.historyJobHostEvents(jobId, hostname)); if (events.size() > MAX_NUMBER_STATUS_EVENTS_TO_RETAIN) { trimStatusEvents(events, jobId);
@Override public void removeDeploymentGroup(final String name) throws DeploymentGroupDoesNotExistException { log.info("removing deployment-group: name={}", name); final ZooKeeperClient client = provider.get("removeDeploymentGroup"); try { client.ensurePath(Paths.configDeploymentGroups()); client.delete(Paths.configDeploymentGroup(name)); if (client.exists(Paths.statusDeploymentGroupHosts(name)) != null) { client.delete(Paths.statusDeploymentGroupHosts(name)); } if (client.exists(Paths.statusDeploymentGroup(name)) != null) { client.delete(Paths.statusDeploymentGroup(name)); } } catch (final NoNodeException e) { throw new DeploymentGroupDoesNotExistException(name); } catch (final KeeperException e) { throw new HeliosRuntimeException("removing deployment-group " + name + " failed", e); } }
@Override public void delete(String path) throws KeeperException { reporter.time(tag, "delete", () -> { client.delete(path); return null; }); }
public static void registerHost(final ZooKeeperClient client, final String idPath, final String hostname, final String hostId) throws KeeperException { log.info("registering host: {}", hostname); // This would've been nice to do in a transaction but PathChildrenCache ensures paths // so we can't know what paths already exist so assembling a suitable transaction is too // painful. client.ensurePath(Paths.configHost(hostname)); client.ensurePath(Paths.configHostJobs(hostname)); client.ensurePath(Paths.configHostPorts(hostname)); client.ensurePath(Paths.statusHost(hostname)); client.ensurePath(Paths.statusHostJobs(hostname)); // Finish registration by creating the id node last client.createAndSetData(idPath, hostId.getBytes(UTF_8)); }
@Override public boolean tryToRegister(final ZooKeeperClient client) throws KeeperException { client.ensurePath(Paths.configHosts()); client.ensurePath(Paths.configJobs()); client.ensurePath(Paths.configJobRefs()); client.ensurePath(Paths.statusHosts()); client.ensurePath(Paths.statusMasters()); client.ensurePath(Paths.historyJobs()); client.ensurePath(Paths.configDeploymentGroups()); client.ensurePath(Paths.statusDeploymentGroups()); if (upNode == null) { final String upPath = Paths.statusMasterUp(name); client.ensurePath(upPath, true); upNode = client.persistentEphemeralNode(upPath, Mode.EPHEMERAL, new byte[]{}); upNode.start(); } log.info("ZooKeeper registration complete"); return true; } }
@Override public Stat stat(String path) throws KeeperException { return reporter.time(tag, "stat", () -> client.stat(path)); }
final ZooKeeperClient client = new DefaultZooKeeperClient(curator, config.getZooKeeperClusterId()); client.start(); zkRegistrar = ZooKeeperRegistrarService.newBuilder() .setZooKeeperClient(client) final List<ACL> curAcls = client.getAcl("/"); final List<ACL> wantedAcls = aclProvider.getAclForPath("/"); if (!Sets.newHashSet(curAcls).equals(Sets.newHashSet(wantedAcls))) { "Current ACL's on the zookeeper root node differ from desired, updating: {} -> {}", curAcls, wantedAcls); client.getCuratorFramework().setACL().withACL(wantedAcls).forPath("/");
private static void checkForPortConflicts(final ZooKeeperClient client, final String host, final int port, final JobId jobId) throws JobPortAllocationConflictException { try { final String path = Paths.configHostPort(host, port); if (client.stat(path) == null) { return; } final byte[] b = client.getData(path); final JobId existingJobId = parse(b, JobId.class); throw new JobPortAllocationConflictException(jobId, existingJobId, host, port); } catch (KeeperException | IOException ex) { throw new HeliosRuntimeException("checking port allocations failed", ex); } }
@Override public void ensurePath(String path) throws KeeperException { reporter.time(tag, "ensurePath", () -> { client.ensurePath(path); return null; }); }