Refine search
/** * A helper method that sets a field value. * @throws java.lang.Exception */ private void setFieldData(Type type, String id, String name, String val) throws Exception { try { zk.create().withMode(CreateMode.PERSISTENT).withACL(Ids.OPEN_ACL_UNSAFE) .forPath(makeFieldZnode(type, id, name), val.getBytes(ENCODING)); } catch (KeeperException.NodeExistsException e) { zk.setData().forPath(makeFieldZnode(type, id, name), val.getBytes(ENCODING)); } }
zkClient = BlobStoreUtils.createZKClient(conf); if(zkClient.checkExists().forPath(BLOBSTORE_SUBTREE + "/" + key) == null) { zkClient.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT) .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE).forPath(BLOBSTORE_MAX_KEY_SEQUENCE_SUBTREE + "/" + key); zkClient.setData().forPath(BLOBSTORE_MAX_KEY_SEQUENCE_SUBTREE + "/" + key, ByteBuffer.allocate(INT_CAPACITY).putInt(INITIAL_SEQUENCE_NUMBER).array()); return INITIAL_SEQUENCE_NUMBER;
try Stat stat = client.setData().withVersion((int)compareVersion).forPath(ZKPaths.makePath(configPath, CONFIG_NODE_NAME), bytes); newVersion = stat.getVersion(); client.create().creatingParentsIfNeeded().forPath(ZKPaths.makePath(configPath, CONFIG_NODE_NAME), bytes); newVersion = 0;
boolean wasCreated = false; try { zk.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).withACL(Ids.OPEN_ACL_UNSAFE).forPath(makeZnode(type, id)); wasCreated = true; zk.delete().forPath(makeZnode(type, id));//default version is -1 if (zk.checkExists().forPath(makeZnode(type, id)) == null) { throw new IOException("Unable to create " + makeZnode(type, id));
private void advertiseTSOServerInfoThroughZK(long epoch) throws Exception { Stat previousTSOZNodeStat = new Stat(); byte[] previousTSOInfoAsBytes = zkClient.getData().storingStatIn(previousTSOZNodeStat).forPath(currentTSOPath); if (previousTSOInfoAsBytes != null && !new String(previousTSOInfoAsBytes, Charsets.UTF_8).isEmpty()) { String previousTSOInfo = new String(previousTSOInfoAsBytes, Charsets.UTF_8); String[] previousTSOAndEpochArray = previousTSOInfo.split("#"); Preconditions.checkArgument(previousTSOAndEpochArray.length == 2, "Incorrect TSO Info found: ", previousTSOInfo); long oldEpoch = Long.parseLong(previousTSOAndEpochArray[1]); if (oldEpoch > epoch) { throw new LeaseManagementException("Another TSO replica was found " + previousTSOInfo); } } String tsoInfoAsString = tsoHostAndPort + "#" + Long.toString(epoch); byte[] tsoInfoAsBytes = tsoInfoAsString.getBytes(Charsets.UTF_8); zkClient.setData().withVersion(previousTSOZNodeStat.getVersion()).forPath(currentTSOPath, tsoInfoAsBytes); LOG.info("TSO instance {} (Epoch {}) advertised through ZK", tsoHostAndPort, epoch); }
/** * Set data into a ZNode. * @param path Path of the ZNode. * @param data Data to set. * @param version Version of the data to store. * @throws Exception If it cannot contact Zookeeper. */ public void setData(String path, byte[] data, int version) throws Exception { curator.setData().withVersion(version).forPath(path, data); }
private static void copyChildren(CuratorFramework curatorClient, String oldPath, String newPath, int depth) throws Exception { for (String childNode : curatorClient.getChildren().forPath(oldPath)) { String oldChildPath = oldPath + File.separator + childNode; Stat stat = curatorClient.checkExists().forPath(oldChildPath); // no need to check exists String newChildPath = newPath + File.separator + childNode; log.info("Creating new znode: " + newChildPath); curatorClient.create().withMode(CreateMode.PERSISTENT).withACL(OPEN_ACL_UNSAFE) .forPath(newChildPath, curatorClient.getData().storingStatIn(stat).forPath(oldChildPath)); if (--depth > 0) { copyChildren(curatorClient, oldChildPath, newChildPath, depth); } } }
/** * Create a path if it does not already exist ("mkdir -p") * @param path string with '/' separator * @param acl list of ACL entries * @throws TokenStoreException */ public void ensurePath(String path, List<ACL> acl) throws TokenStoreException { try { CuratorFramework zk = getSession(); String node = zk.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT) .withACL(acl).forPath(path); LOGGER.info("Created path: {} ", node); } catch (KeeperException.NodeExistsException e) { // node already exists } catch (Exception e) { throw new TokenStoreException("Error creating path " + path, e); } }
(fencingUsername + ":" + fencingPassword).getBytes()) .build(); client.start(); client.create() .creatingParentsIfNeeded() .withMode(CreateMode.PERSISTENT) .withACL(conf.getZkAcls()) .forPath(fencingPath, "dummy-data".getBytes()); } catch (KeeperException.NodeExistsException nee) {
/** * Create the parent znode for this job state. */ public void create() throws IOException { try { zk.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT) .withACL(Ids.OPEN_ACL_UNSAFE).forPath(job_trackingroot); } catch (KeeperException.NodeExistsException e) { //root must exist already } catch (Exception e) { throw new IOException("Unable to create parent nodes"); } try { trackingnode = zk.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL) .withACL(Ids.OPEN_ACL_UNSAFE).forPath(makeTrackingZnode(), jobid.getBytes()); } catch (Exception e) { throw new IOException("Unable to create " + makeTrackingZnode()); } } public void delete() throws IOException {
private void updateDeliveryTag(CuratorFramework curatorClient, String gfacServerName, ProcessSubmitEvent event, MessageContext messageContext) throws Exception { String experimentId = event.getExperimentId(); String processId = event.getProcessId(); long deliveryTag = messageContext.getDeliveryTag(); String processNodePath = ZKPaths.makePath(GFacUtils.getExperimentNodePath(experimentId), processId); Stat stat = curatorClient.checkExists().forPath(processNodePath); if (stat != null) { // create /experiments/{processId}/deliveryTag node and set data - deliveryTag String deliveryTagPath = ZKPaths.makePath(processNodePath, ZkConstants.ZOOKEEPER_DELIVERYTAG_NODE); curatorClient.setData().withVersion(-1).forPath(deliveryTagPath, GFacUtils.longToBytes(deliveryTag)); } }
private void createProcessZKNode(CuratorFramework curatorClient, String gfacServerName,ProcessSubmitEvent event ,MessageContext messageContext) throws Exception { String processId = event.getProcessId(); String token = event.getTokenId(); String experimentId = event.getExperimentId(); long deliveryTag = messageContext.getDeliveryTag(); // create /experiments//{experimentId}{processId} node and set data - serverName, add redelivery listener String experimentNodePath = GFacUtils.getExperimentNodePath(experimentId); String zkProcessNodePath = ZKPaths.makePath(experimentNodePath, processId); ZKPaths.mkdirs(curatorClient.getZookeeperClient().getZooKeeper(), zkProcessNodePath); curatorClient.setData().withVersion(-1).forPath(zkProcessNodePath, gfacServerName.getBytes()); curatorClient.getData().usingWatcher(Factory.getRedeliveryReqeustWatcher(experimentId, processId)).forPath(zkProcessNodePath); // create /experiments//{experimentId}{processId}/cancelListener String zkProcessCancelPath = ZKPaths.makePath(zkProcessNodePath, ZkConstants.ZOOKEEPER_CANCEL_LISTENER_NODE); ZKPaths.mkdirs(curatorClient.getZookeeperClient().getZooKeeper(), zkProcessCancelPath); // create /experiments/{experimentId}/{processId}/deliveryTag node and set data - deliveryTag String deliveryTagPath = ZKPaths.makePath(zkProcessNodePath, ZkConstants.ZOOKEEPER_DELIVERYTAG_NODE); ZKPaths.mkdirs(curatorClient.getZookeeperClient().getZooKeeper(), deliveryTagPath); curatorClient.setData().withVersion(-1).forPath(deliveryTagPath, GFacUtils.longToBytes(deliveryTag)); // create /experiments/{experimentId}/{processId}/token node and set data - token String tokenNodePath = ZKPaths.makePath(zkProcessNodePath, ZkConstants.ZOOKEEPER_TOKEN_NODE); ZKPaths.mkdirs(curatorClient.getZookeeperClient().getZooKeeper(), tokenNodePath); curatorClient.setData().withVersion(-1).forPath(tokenNodePath, token.getBytes()); }
if ( createIt ) client.create().creatingParentContainersIfNeeded().forPath(path, newValue); client.setData().withVersion(stat.getVersion()).forPath(path, newValue);
private void log(SentMessageTrace messageTrace) { try { String undeliveredPath = paths.subscriptionPath(messageTrace.getTopicName(), messageTrace.getSubscription(), NODE_NAME); BackgroundPathAndBytesable<?> builder = exists(undeliveredPath) ? curator.setData() : curator.create(); builder.forPath(undeliveredPath, mapper.writeValueAsBytes(messageTrace)); } catch (Exception exception) { LOGGER.warn( format("Could not log undelivered message for topic: %s and subscription: %s", messageTrace.getQualifiedTopicName(), messageTrace.getSubscription() ), exception ); } }
/** * createNodeWithACL * Create a node under ACL mode * @param path * @param payload * @throws Exception */ public void createNodeWithACL(String path, byte[] payload) throws Exception { ACL acl = new ACL(ZooDefs.Perms.ALL, ZooDefs.Ids.AUTH_IDS); List<ACL> aclList = Lists.newArrayList(acl); try { client.create().withACL(aclList).forPath(path, payload); } catch (Exception e) { logger.error("Create security file failed."); e.printStackTrace(); } }
CompletableFuture<Integer> setData(final String path, final Data data) { final CompletableFuture<Integer> result = new CompletableFuture<>(); try { if (data.getVersion() == null) { client.setData().inBackground( callback(event -> result.complete(event.getStat().getVersion()), result::completeExceptionally, path), executor) .forPath(path, data.getData()); } else { client.setData().withVersion(data.getVersion().asIntVersion().getIntValue()).inBackground( callback(event -> result.complete(event.getStat().getVersion()), result::completeExceptionally, path), executor) .forPath(path, data.getData()); } } catch (Exception e) { result.completeExceptionally(StoreException.create(StoreException.Type.UNKNOWN, e, path)); } return result; }
@Override public T forPath(String path, byte[] data) throws Exception { return inner.forPath(path, data); }
@Override public ErrorListenerPathAndBytesable<T> inBackground(Object context) { return new ErrorListenerPathAndBytesableDecorator<>(inner.inBackground(context)); }
public void set(String path, Object value, int version) throws Exception { byte[] bytes = (value == null ? new byte[0] : value.toString().getBytes(CHARSET)); if (exists(path, false)) { zookeeperClient.setData().withVersion(0).forPath(path, bytes); LOGGER.debug("set value of node " + path + " to " + value); } else { zookeeperClient.create().creatingParentsIfNeeded().forPath(path, bytes); LOGGER.debug("create node " + path + " value " + value); } }