public void writeBytes(String path, byte[] bytes) { try { if (_curator.checkExists().forPath(path) == null) { CreateBuilder builder = _curator.create(); ProtectACLCreateModePathAndBytesable<String> createAble = (ProtectACLCreateModePathAndBytesable<String>) builder .creatingParentsIfNeeded(); createAble.withMode(CreateMode.PERSISTENT).forPath(path, bytes); } else { _curator.setData().forPath(path, bytes); } } catch (Exception e) { throw new RuntimeException(e); } }
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;
private void startCreateCurrentNode() { if (!isActive()) return; String createPath = null; try { createPath = getSlotPath(currentSlot); LOG.info("Attempting to create " + createPath); client.create().withMode(CreateMode.EPHEMERAL).inBackground(backgroundCallback) .forPath(createPath, data); } catch (Exception e) { LOG.error("Creating node. Path: " + createPath, e); throw new RuntimeException(e); } }
if (curatorFramework.checkExists().forPath(path) == null) { try { curatorFramework.create() .creatingParentsIfNeeded() .withMode(mode) .forPath(path, rawBytes);
@Override public String persistSequential(final String key, final String value) { try { return client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath(key, value.getBytes(Charsets.UTF_8)); //CHECKSTYLE:OFF } catch (final Exception ex) { //CHECKSTYLE:ON RegExceptionHandler.handleException(ex); } return null; }
public static void main(String[] args) throws Exception { final String remoteAddress = "localhost:2181"; final String groupId = "Sentinel-Demo"; final String dataId = "SYSTEM-CODE-DEMO-FLOW"; final String rule = "[\n" + " {\n" + " \"resource\": \"TestResource\",\n" + " \"controlBehavior\": 0,\n" + " \"count\": 10.0,\n" + " \"grade\": 1,\n" + " \"limitApp\": \"default\",\n" + " \"strategy\": 0\n" + " }\n" + "]"; CuratorFramework zkClient = CuratorFrameworkFactory.newClient(remoteAddress, new ExponentialBackoffRetry(SLEEP_TIME, RETRY_TIMES)); zkClient.start(); String path = getPath(groupId, dataId); Stat stat = zkClient.checkExists().forPath(path); if (stat == null) { zkClient.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, null); } zkClient.setData().forPath(path, rule.getBytes()); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } zkClient.close(); }
public ZkBasedSnowFlakeIdGenerator(String zooKeeperUrl, String applicationName) { CuratorFramework client = CuratorFrameworkFactory.newClient(zooKeeperUrl, new RetryForever(1000)); client.start(); try { String path = "/EasyTransIdGen/" + applicationName + "/P"; String nodeName = client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path); String sequenceStr = nodeName.replaceAll(path, ""); hostSeq = Long.parseLong(sequenceStr) % (2^SnowFlake.MACHINE_BIT); client.close();//do not need to keep connection, hostSeq will not change } catch (Exception e) { throw new RuntimeException("create Id generator failed",e); } }
private void createInstanceNodeForInitialization() throws Exception { String parent = String.format("%s/%s/instances", CONSTANTS_ROOT_PATH, CommonUtils.getApplication(this.endpoint)); String path = String.format("%s/%s", parent, this.endpoint); byte[] versionByteArray = ByteUtils.longToByteArray(this.instanceVersion); try { this.curatorFramework.create().withMode(CreateMode.EPHEMERAL).forPath(path, versionByteArray); } catch (NodeExistsException error) { try { this.instanceLock.lock(); this.instanceInited = false; this.curatorFramework.checkExists().usingWatcher(this).inBackground(this).forPath(path); this.instanceCond.await(this.initializeWaitingSeconds, TimeUnit.SECONDS); if (this.instanceInited == false) { throw error; } } catch (InterruptedException ignore) { // ignore } finally { this.instanceLock.unlock(); } } }
this.zkClient.start(); Stat stat = this.zkClient.checkExists().forPath(this.path); if (stat == null) { this.zkClient.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(this.path, null);
if (zkClient.checkExists().forPath(nodeCreatePath) != null) { zkClient.setData().forPath(nodeCreatePath, os.toByteArray()) .setVersion(-1); if (!isUpdate) { zkClient.create().withMode(CreateMode.PERSISTENT) .forPath(nodeCreatePath, os.toByteArray()); if (isUpdate) { LOG.debug("Updating non existent Key path [" + nodeCreatePath
public void updateBrokerStatus(String clusterName, Set<String> brokers) { try { //add cluster node if (zkClient.checkExists().forPath(getClusterPath(clusterName)) == null) { zkClient.create().forPath(getClusterPath(clusterName)); zkClient.create().forPath(getIdsPath(clusterName)); } if (zkClient.checkExists().forPath(getNSPath(clusterName)) != null) { Set brokerSetInZk = fromJsonBytes(zkClient.getData().forPath(getNSPath(clusterName)), Set.class); if (!brokers.equals(brokerSetInZk)) { zkClient.setData().forPath(getNSPath(clusterName), toJsonString(brokers).getBytes()); aliveBrokers.put(clusterName, brokers); } } else { zkClient.create().withMode(CreateMode.EPHEMERAL).forPath(getNSPath(clusterName), toJsonString(brokers).getBytes()); aliveBrokers.put(clusterName, brokers); } } catch (Exception ex) { log.error("update broker status failed, clusterName:" + clusterName + ", brokers:" + brokers, ex); } }
public void save(SingularityHostState hostState) throws InterruptedException { final String path = ZKPaths.makePath(ROOT_PATH, hostState.getHostname()); final byte[] data = hostStateTranscoder.toBytes(hostState); if (curator.getState() == CuratorFrameworkState.STARTED) { try { if (exists(path)) { curator.setData().forPath(path, data); } else { curator.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path, data); } } catch (Throwable t) { throw Throwables.propagate(t); } } }
if (configClient.checkExists().forPath(directory) == null) { configClient.create().creatingParentsIfNeeded().forPath(directory); configClient.create().withMode(CreateMode.EPHEMERAL).inBackground(new BackgroundCallback() {
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 ensureZkPaths(CuratorFramework curatorFramework, String[] paths) { for (String s : paths) { try { Stat stat = curatorFramework.checkExists().forPath(s); if (stat == null) { curatorFramework.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT) .forPath(s); } } catch (Exception e) { LOG.info(e.getMessage()); } } }
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); } } }
public synchronized void off(String destination) { if (mode == Mode.LOCAL) { BooleanMutex mutex = LOCAL_LOCK.get(destination); if (mutex != null && mutex.state()) { mutex.set(false); } } else { try { String path = SYN_SWITCH_ZK_NODE + destination; try { curatorClient.getCurator() .create() .creatingParentContainersIfNeeded() .withMode(CreateMode.PERSISTENT) .forPath(path, "off".getBytes(StandardCharsets.UTF_8)); } catch (Exception e) { curatorClient.getCurator().setData().forPath(path, "off".getBytes(StandardCharsets.UTF_8)); } } catch (Exception e) { throw new RuntimeException(e); } } }
private void initZkDataStructure() throws Exception { /* *|/servers * - /gfac * - /gfac-node0 (localhost:2181) *|/experiments */ airavataServerHostPort = ServerSettings.getGfacServerHost() + ":" + ServerSettings.getGFacServerPort(); // create PERSISTENT nodes ZKPaths.mkdirs(curatorClient.getZookeeperClient().getZooKeeper(), GFacUtils.getZKGfacServersParentPath()); ZKPaths.mkdirs(curatorClient.getZookeeperClient().getZooKeeper(), ZkConstants.ZOOKEEPER_EXPERIMENT_NODE); // create EPHEMERAL server name node String gfacName = ServerSettings.getGFacServerName(); if (curatorClient.checkExists().forPath(ZKPaths.makePath(GFacUtils.getZKGfacServersParentPath() ,gfacName)) == null) { curatorClient.create().withMode(CreateMode.EPHEMERAL).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE) .forPath(ZKPaths.makePath(GFacUtils.getZKGfacServersParentPath(), gfacName)); } curatorClient.setData().withVersion(-1).forPath(ZKPaths.makePath(GFacUtils.getZKGfacServersParentPath(), gfacName), airavataServerHostPort.getBytes()); }
/** * 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 {