com.spotify.helios.servicescommon.coordination.ZooKeeperClient.createAndSetData()方法的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(6.7k)|赞(0)|评价(0)|浏览(88)

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

ZooKeeperClient.createAndSetData介绍

暂无

代码示例

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

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

代码示例来源: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

try {
 if (client.stat(path) == null) {
  client.createAndSetData(path, write.data);
 } else {
  client.setData(path, write.data);

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

if (remoteData == null) {
 log.debug("sync: creating node {}", nodePath);
 client.createAndSetData(nodePath, localData);
 remote.put(node, localData);
} else if (!Arrays.equals(remoteData, localData)) {

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

@Override
 public boolean update(final byte[] bytes) {
  final String parent = ZKPaths.getPathAndNode(path).getPath();
  try {
   if (zooKeeperClient.stat(parent) == null) {
    return false;
   }
   if (zooKeeperClient.stat(path) == null) {
    zooKeeperClient.createAndSetData(path, bytes);
   } else {
    zooKeeperClient.setData(path, bytes);
   }
   return true;
  } catch (KeeperException.NodeExistsException ignore) {
   // Conflict due to curator retry or losing a race. We're done here.
   return true;
  } catch (KeeperException.ConnectionLossException e) {
   log.warn("ZooKeeper connection lost while updating node: {}", path);
   return false;
  } catch (KeeperException e) {
   log.error("failed to update node: {}", path, e);
   return false;
  }
 }
}

代码示例来源: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

client.createAndSetData(historyPath, item.getStatus().toJsonBytes());

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

@Override
public void createAndSetData(String path, byte[] data) throws KeeperException {
 try {
  client.createAndSetData(path, data);
 } catch (KeeperException e) {
  reporter.checkException(e, tag, "createAndSetData");
  throw e;
 }
}

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

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: at.molindo/helios-services

try {
 if (client.stat(path) == null) {
  client.createAndSetData(path, write.data);
 } else {
  client.setData(path, write.data);

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

if (remoteData == null) {
 log.debug("sync: creating node {}", nodePath);
 client.createAndSetData(nodePath, localData);
 remote.put(node, localData);
} else if (!Arrays.equals(remoteData, localData)) {

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

@Override
 public boolean update(final byte[] bytes) {
  final String parent = ZKPaths.getPathAndNode(path).getPath();
  try {
   if (zooKeeperClient.stat(parent) == null) {
    return false;
   }
   if (zooKeeperClient.stat(path) == null) {
    zooKeeperClient.createAndSetData(path, bytes);
   } else {
    zooKeeperClient.setData(path, bytes);
   }
   return true;
  } catch (KeeperException.NodeExistsException ignore) {
   // Conflict due to curator retry or losing a race. We're done here.
   return true;
  } catch (KeeperException.ConnectionLossException e) {
   log.warn("ZooKeeper connection lost while updating node: {}", path);
   return false;
  } catch (KeeperException e) {
   log.error("failed to update node: {}", path, e);
   return false;
  }
 }
}

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

private boolean tryWriteToZooKeeper(TEvent event) {
 final String eventsPath = getZkEventsPath(event);
 try {
  log.debug("writing queued event to zookeeper {} {}", getKey(event),
       getTimestamp(event));
  client.ensurePath(eventsPath);
  client.createAndSetData(getZkEventPath(eventsPath, getTimestamp(event)), toBytes(event));
  // See if too many
  final List<String> events = client.getChildren(eventsPath);
  if (events.size() > getMaxEventsPerPath()) {
   trimStatusEvents(events, eventsPath);
  }
 } catch (NodeExistsException e) {
  // Ahh, the two generals problem...  We handle by doing nothing since the thing
  // we wanted in, is in.
  log.debug("event we wanted in is already there");
 } catch (ConnectionLossException e) {
  log.warn("Connection lost while putting event into zookeeper, will retry");
  return false;
 } catch (KeeperException e) {
  log.error("Error putting event into zookeeper, will retry", e);
  return false;
 }
 return true;
}

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

client.createAndSetData(idPath, id.getBytes(UTF_8));
} else {
 final byte[] bytes = client.getData(idPath);

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

/**
 * Registers a host into ZooKeeper.  The {@code id} is initially generated randomly by the Agent
 * and persisted on disk.  This way, in the event that you have two agents attempting to register
 * with the same value of @{code host}, the first one will win.
 */
@Override
public void registerHost(final String host, final String id) {
 log.info("registering host: {}", host);
 final ZooKeeperClient client = provider.get("registerHost");
 try {
  // TODO (dano): this code is replicated in AgentZooKeeperRegistrar
  // 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(host));
  client.ensurePath(Paths.configHostJobs(host));
  client.ensurePath(Paths.configHostPorts(host));
  client.ensurePath(Paths.statusHost(host));
  client.ensurePath(Paths.statusHostJobs(host));
  // Finish registration by creating the id node last
  client.createAndSetData(Paths.configHostId(host), id.getBytes(UTF_8));
 } catch (Exception e) {
  throw new HeliosRuntimeException("registering host " + host + " failed", e);
 }
}

相关文章