private void createIfNotExists(String path) throws Exception { if (curatorFramework.checkExists().forPath(path) == null) { curatorFramework.create().creatingParentContainersIfNeeded().forPath(path, new byte[0]); } }
private void initializeClusterInstancesDirectory() throws Exception { String parent = String.format("%s/%s/instances", CONSTANTS_ROOT_PATH, CommonUtils.getApplication(this.endpoint)); try { this.curatorFramework.create() // .creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(parent); } catch (NodeExistsException nex) { logger.debug("Path exists(path= {})!", parent); // ignore } }
private void initializeSubsystemRollbackDirectory() throws Exception { String parent = String.format("%s/%s/rollback", CONSTANTS_ROOT_PATH, CommonUtils.getApplication(this.endpoint)); try { this.curatorFramework.create() // .creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(parent); } catch (NodeExistsException nex) { logger.debug("Path exists(path= {})!", parent); // ignore } }
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 synchronized void on(String destination) { if (mode == Mode.LOCAL) { BooleanMutex mutex = LOCAL_LOCK.get(destination); if (mutex != null && !mutex.state()) { mutex.set(true); } } else { try { String path = SYN_SWITCH_ZK_NODE + destination; try { curatorClient.getCurator() .create() .creatingParentContainersIfNeeded() .withMode(CreateMode.PERSISTENT) .forPath(path, "on".getBytes(StandardCharsets.UTF_8)); } catch (Exception e) { curatorClient.getCurator().setData().forPath(path, "on".getBytes(StandardCharsets.UTF_8)); } } catch (Exception e) { throw new RuntimeException(e); } } }
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); } } }
Stat stat = this.zkClient.checkExists().forPath(this.path); if (stat == null) { this.zkClient.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(this.path, null);
@Test public void createNonExisting2() throws Exception { CuratorFramework client = mock(CuratorFramework.class); ExistsBuilder builder = mock(ExistsBuilder.class); CreateBuilder createBuilder = mock(CreateBuilder.class); ProtectACLCreateModeStatPathAndBytesable<String> protector = mock(ProtectACLCreateModeStatPathAndBytesable.class); when(builder.forPath(anyString())).thenReturn(new Stat()); when(client.checkExists()).thenReturn(builder); when(client.create()).thenReturn(createBuilder); when(createBuilder.creatingParentContainersIfNeeded()).thenReturn(protector); new DistributedLockServiceCuratorImpl(client, "/", 0); verify(protector, times(0)).forPath(anyString(), anyObject()); }
@Test public void createNonExisting() throws Exception { CuratorFramework client = mock(CuratorFramework.class); ExistsBuilder builder = mock(ExistsBuilder.class); CreateBuilder createBuilder = mock(CreateBuilder.class); ProtectACLCreateModeStatPathAndBytesable<String> protector = mock(ProtectACLCreateModeStatPathAndBytesable.class); when(builder.forPath(anyString())).thenReturn(null); when(client.checkExists()).thenReturn(builder); when(client.create()).thenReturn(createBuilder); when(createBuilder.creatingParentContainersIfNeeded()).thenReturn((ProtectACLCreateModeStatPathAndBytesable<String>)protector); new DistributedLockServiceCuratorImpl(client, "/", 0); verify(protector).forPath(anyString(), anyObject()); }
/*** * 订阅 * @param config */ protected void subscribeConsumerUrls(ConsumerConfig config) { // 注册Consumer节点 String url = null; if (config.isRegister()) { try { String consumerPath = buildConsumerPath(rootPath, config); if (consumerUrls.containsKey(config)) { url = consumerUrls.get(config); } else { url = ZookeeperRegistryHelper.convertConsumerToUrl(config); consumerUrls.put(config, url); } String encodeUrl = URLEncoder.encode(url, "UTF-8"); getAndCheckZkClient().create().creatingParentContainersIfNeeded() .withMode(CreateMode.EPHEMERAL) // Consumer临时节点 .forPath(consumerPath + CONTEXT_SEP + encodeUrl); } catch (KeeperException.NodeExistsException nodeExistsException) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("consumer has exists in zookeeper, consumer=" + url); } } catch (Exception e) { throw new SofaRpcRuntimeException("Failed to register consumer to zookeeperRegistry!", e); } } }
/*** * 订阅 * @param config */ protected void subscribeConsumerUrls(ConsumerConfig config) { // 注册Consumer节点 String url = null; if (config.isRegister()) { try { String consumerPath = buildConsumerPath(rootPath, config); if (consumerUrls.containsKey(config)) { url = consumerUrls.get(config); } else { url = ZookeeperRegistryHelper.convertConsumerToUrl(config); consumerUrls.put(config, url); } String encodeUrl = URLEncoder.encode(url, "UTF-8"); getAndCheckZkClient().create().creatingParentContainersIfNeeded() .withMode(CreateMode.EPHEMERAL) // Consumer临时节点 .forPath(consumerPath + CONTEXT_SEP + encodeUrl); } catch (KeeperException.NodeExistsException nodeExistsException) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("consumer has exists in zookeeper, consumer=" + url); } } catch (Exception e) { throw new SofaRpcRuntimeException("Failed to register consumer to zookeeperRegistry!", e); } } }
getAndCheckZkClient().create().creatingParentContainersIfNeeded()
getAndCheckZkClient().create().creatingParentContainersIfNeeded()
private void createPersistentPathIfNecessary(String path) throws Exception { try { this.curatorFramework.create() // .creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path); } catch (NodeExistsException nex) { logger.debug("Path exists(path= {})!", path); } }
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()); } } }
@Override public ProtectACLCreateModePathAndBytesable<String> creatingParentContainersIfNeeded() { return new ProtectACLCreateModePathAndBytesableDecorator<>(inner.creatingParentContainersIfNeeded()); }
public static long getLastCachedTid(BigInteger machineId) throws Exception { long oldTid = 0; String timestampPath = String.format("/snowflake/hosts/%s/timestamp", machineId); Stat stat = curator.checkExists().forPath(timestampPath); if (stat == null) { curator.create().creatingParentContainersIfNeeded().forPath(timestampPath); } else { byte[] data = curator.getData().forPath(timestampPath); oldTid = LongCombiner.FIXED_LEN_ENCODER.decode(data); } return oldTid; }
private void writeOrCreate(String path, byte[] serializedData) throws Exception { try { curator.setData().forPath(path, serializedData); } catch (KeeperException.NoNodeException e) { try { curator.create().creatingParentContainersIfNeeded().forPath(path, serializedData); } catch (KeeperException.NodeExistsException ex) { // ignore } } }
private void writeOrCreate(String path, byte[] serializedData) throws Exception { try { curator.setData().forPath(path, serializedData); } catch (KeeperException.NoNodeException e) { try { curator.create().creatingParentContainersIfNeeded().forPath(path, serializedData); } catch (KeeperException.NodeExistsException ex) { // ignore } } }
public void save(EventSetMeta eventSetMeta) throws Exception { curator.create().creatingParentContainersIfNeeded() .forPath(txnPath(eventSetMeta.getTransactionId()), mapper.writeValueAsBytes(eventSetMeta)); //This is okay, as this object is accessed always from only a single thread. //So this is equivalent to saving lastTansactionId saveLastTransactionId(eventSetMeta.getTransactionId()); }