gpt4 book ai didi

com.spotify.helios.servicescommon.coordination.ZooKeeperClient类的使用及代码示例

转载 作者:知者 更新时间:2024-03-19 02:53:31 30 4
gpt4 key购买 nike

本文整理了Java中com.spotify.helios.servicescommon.coordination.ZooKeeperClient类的一些代码示例,展示了ZooKeeperClient类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZooKeeperClient类的具体详情如下:
包路径:com.spotify.helios.servicescommon.coordination.ZooKeeperClient
类名称:ZooKeeperClient

ZooKeeperClient介绍

[英]Exists because the Curator library makes things ununit-testable without this. Also it avoids having to catch Exception at call-sites.
[中]之所以存在,是因为策展人图书馆让没有这个东西就无法进行测试。此外,它还避免了在调用站点捕获异常。

代码示例

代码示例来源:origin: spotify/helios

@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
 }
}

代码示例来源:origin: spotify/helios

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);
 }
}

代码示例来源:origin: spotify/helios

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();
 }
}

代码示例来源:origin: spotify/helios

private void delete(final String node) throws KeeperException {
 final ZooKeeperClient client = client("delete");
 final String nodePath = ZKPaths.makePath(path, node);
 if (client.stat(nodePath) != null) {
  log.debug("deleting node: {}", nodePath);
  client.delete(nodePath);
 }
}

代码示例来源:origin: spotify/helios

@Override
public void start() {
 client.start();
 client.getConnectionStateListenable().addListener(
   (client, newState) -> reporter.connectionStateChanged(newState));
}

代码示例来源:origin: spotify/helios

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);

代码示例来源:origin: spotify/helios

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);

代码示例来源:origin: spotify/helios

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();

代码示例来源:origin: spotify/helios

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);

代码示例来源:origin: at.molindo/helios-services

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();

代码示例来源:origin: spotify/helios

client.ensurePath(Paths.configDeploymentGroups());
client.ensurePath(Paths.statusDeploymentGroups());
client.transaction(
  create(Paths.configDeploymentGroup(deploymentGroup.getName()), deploymentGroup),
  create(Paths.statusDeploymentGroup(deploymentGroup.getName())),

代码示例来源:origin: spotify/helios

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);

代码示例来源:origin: at.molindo/helios-services

@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);
 }
}

代码示例来源:origin: spotify/helios

@Override
public void delete(String path) throws KeeperException {
 reporter.time(tag, "delete", () -> {
  client.delete(path);
  return null;
 });
}

代码示例来源:origin: spotify/helios

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));
}

代码示例来源:origin: spotify/helios

@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;
 }
}

代码示例来源:origin: spotify/helios

@Override
public Stat stat(String path) throws KeeperException {
 return reporter.time(tag, "stat", () -> client.stat(path));
}

代码示例来源:origin: spotify/helios

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("/");

代码示例来源:origin: spotify/helios

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);
 }
}

代码示例来源:origin: spotify/helios

@Override
public void ensurePath(String path) throws KeeperException {
 reporter.time(tag, "ensurePath", () -> {
  client.ensurePath(path);
  return null;
 });
}

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com