@Override public void createAndSetData(String path, byte[] data) throws KeeperException { reporter.time(tag, "createAndSetData", () -> { client.createAndSetData(path, data); return null; }); }
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); } }
try { if (client.stat(path) == null) { client.createAndSetData(path, write.data); } else { client.setData(path, write.data);
if (remoteData == null) { log.debug("sync: creating node {}", nodePath); client.createAndSetData(nodePath, localData); remote.put(node, localData); } else if (!Arrays.equals(remoteData, localData)) {
@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; } } }
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)); }
client.createAndSetData(historyPath, item.getStatus().toJsonBytes());
@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; } }
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); } }
try { if (client.stat(path) == null) { client.createAndSetData(path, write.data); } else { client.setData(path, write.data);
if (remoteData == null) { log.debug("sync: creating node {}", nodePath); client.createAndSetData(nodePath, localData); remote.put(node, localData); } else if (!Arrays.equals(remoteData, localData)) {
@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; } } }
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; }
client.createAndSetData(idPath, id.getBytes(UTF_8)); } else { final byte[] bytes = client.getData(idPath);
/** * 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); } }