org.apache.zookeeper.CreateMode Java Examples

The following examples show how to use org.apache.zookeeper.CreateMode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BasicTests.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void     testSimple() throws Exception
{
    CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), 10000, 10000, null, new RetryOneTime(1));
    client.start();
    try
    {
        client.blockUntilConnectedOrTimedOut();
        String              path = client.getZooKeeper().create("/test", new byte[]{1,2,3}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        Assert.assertEquals(path, "/test");
    }
    finally
    {
        client.close();
    }
}
 
Example #2
Source File: LedgerAllocatorPool.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private void initializePool() throws IOException {
    try {
        List<String> allocators;
        try {
            allocators = zkc.get().getChildren(poolPath, false);
        } catch (KeeperException.NoNodeException e) {
            logger.info("Allocator Pool {} doesn't exist. Creating it.", poolPath);
            ZkUtils.createFullPathOptimistic(zkc.get(), poolPath, new byte[0], zkc.getDefaultACL(),
                    CreateMode.PERSISTENT);
            allocators = zkc.get().getChildren(poolPath, false);
        }
        if (null == allocators) {
            allocators = new ArrayList<String>();
        }
        if (allocators.size() < corePoolSize) {
            createAllocators(corePoolSize - allocators.size());
            allocators = zkc.get().getChildren(poolPath, false);
        }
        initializeAllocators(allocators);
    } catch (InterruptedException ie) {
        throw new DLInterruptedException("Interrupted when ensuring " + poolPath + " created : ", ie);
    } catch (KeeperException ke) {
        throw new IOException("Encountered zookeeper exception when initializing pool " + poolPath + " : ", ke);
    }
}
 
Example #3
Source File: JobHistoryZKStateManager.java    From eagle with Apache License 2.0 6 votes vote down vote up
public void addProcessedJob(String date, String jobId) {
    String path = zkRoot + "/" + ZNODE_JOBS + "/" + date + "/" + jobId;
    try {
        if (curator.checkExists().forPath(path) == null) {
            curator.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .forPath(path);
        } else {
            curator.setData().forPath(path);
        }
    } catch (Exception e) {
        LOG.error("fail adding processed jobs", e);
        throw new RuntimeException(e);
    }
}
 
Example #4
Source File: JobZNodeManager.java    From twister2 with Apache License 2.0 6 votes vote down vote up
/**
 * Create job submission time znode
 */
public static void createJstZNode(CuratorFramework client,
                                  String rootPath,
                                  String jobID,
                                  long jsTime) {

  String jstPath = ZKUtils.jobSubmisionTimePath(rootPath, jobID);

  try {
    client
        .create()
        .creatingParentsIfNeeded()
        .withMode(CreateMode.PERSISTENT)
        .forPath(jstPath, Longs.toByteArray(jsTime));

    LOG.info("Job Submission Time ZNode created: " + jstPath);

  } catch (Exception e) {
    throw new Twister2RuntimeException("Can not create job submission time znode: " + jstPath, e);
  }
}
 
Example #5
Source File: Utils.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Synchronously create zookeeper path recursively and optimistically.
 *
 * @see #zkAsyncCreateFullPathOptimistic(ZooKeeperClient, String, byte[], List, CreateMode)
 * @param zkc Zookeeper client
 * @param path Zookeeper full path
 * @param data Zookeeper data
 * @param acl Acl of the zk path
 * @param createMode Create mode of zk path
 * @throws ZooKeeperClient.ZooKeeperConnectionException
 * @throws KeeperException
 * @throws InterruptedException
 */
public static void zkCreateFullPathOptimistic(
    ZooKeeperClient zkc,
    String path,
    byte[] data,
    final List<ACL> acl,
    final CreateMode createMode) throws IOException, KeeperException {
    try {
        FutureUtils.result(zkAsyncCreateFullPathOptimistic(zkc, path, data, acl, createMode));
    } catch (ZooKeeperClient.ZooKeeperConnectionException zkce) {
        throw zkce;
    } catch (KeeperException ke) {
        throw ke;
    } catch (InterruptedException ie) {
        throw new DLInterruptedException("Interrupted on create zookeeper path " + path, ie);
    } catch (RuntimeException rte) {
        throw rte;
    } catch (Exception exc) {
        throw new RuntimeException("Unexpected Exception", exc);
    }
}
 
Example #6
Source File: TestPersistentEphemeralNode.java    From curator with Apache License 2.0 6 votes vote down vote up
/**
 * Test that if a persistent ephemeral node is created and the node already exists
 * that if data is present in the PersistentEphermalNode that it is still set.
 * @throws Exception
 */
@Test
public void testSetDataWhenNodeExists() throws Exception
{
    CuratorFramework curator = newCurator();
    curator.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(PATH, "InitialData".getBytes());

    byte[] data = "Hello World".getBytes();

    PersistentEphemeralNode node = new PersistentEphemeralNode(curator, PersistentEphemeralNode.Mode.EPHEMERAL, PATH, data);
    node.debugWaitMsForBackgroundBeforeClose.set(timing.forSleepingABit().milliseconds());
    try
    {
        node.start();
        node.waitForInitialCreate(timing.forWaiting().seconds(), TimeUnit.SECONDS);
        assertTrue(Arrays.equals(curator.getData().forPath(node.getActualPath()), data));
    }
    finally
    {
        CloseableUtils.closeQuietly(node);
    }
}
 
Example #7
Source File: ServiceProvider.java    From ranger with Apache License 2.0 6 votes vote down vote up
private void createPath() throws Exception {
    Retryer<Void> retryer = RetryerBuilder.<Void>newBuilder()
            .retryIfExceptionOfType(KeeperException.NodeExistsException.class) //Ephemeral node still exists
            .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
            .withBlockStrategy(BlockStrategies.threadSleepStrategy())
            .withStopStrategy(StopStrategies.neverStop())
            .build();
    try {
        retryer.call(() -> {
            curatorFramework.create().withMode(CreateMode.EPHEMERAL).forPath(
                    String.format("/%s/%s", serviceName, serviceNode.representation()),
                    serializer.serialize(serviceNode));
            return null;
        });
    } catch (Exception e) {
        final String message = String.format("Could not create node for %s after 60 retries (1 min). " +
                        "This service will not be discoverable. Retry after some time.", serviceName);
        logger.error(message, e);
        throw new Exception(message, e);
    }

}
 
Example #8
Source File: DistributedClusterStat.java    From leaf-snowflake with Apache License 2.0 6 votes vote down vote up
@Override
public void set_data(String path,byte[] data) throws Exception
{
	if (data.length > Utils.SIZE_1_K * 800)
	{
		throw new Exception("Writing 800k+ data into ZK is not allowed!, data size is " + data.length);
	}
	if (zkobj.exists(zk,path,false))
	{
		zkobj.setData(zk,path,data);
	}
	else
	{
		zkobj.mkdirs(zk, PathUtils.parent_path(path));
		zkobj.createNode(zk,path,data,CreateMode.PERSISTENT);
	}
	if (zkCache != null)
		zkCache.put(path,data);
}
 
Example #9
Source File: ZKUtil.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Convert from ZKUtilOp to ZKOp
 */
private static Op toZooKeeperOp(ZKWatcher zkw, ZKUtilOp op) throws UnsupportedOperationException {
  if(op == null) {
    return null;
  }

  if (op instanceof CreateAndFailSilent) {
    CreateAndFailSilent cafs = (CreateAndFailSilent)op;
    return Op.create(cafs.getPath(), cafs.getData(), createACL(zkw, cafs.getPath()),
      CreateMode.PERSISTENT);
  } else if (op instanceof DeleteNodeFailSilent) {
    DeleteNodeFailSilent dnfs = (DeleteNodeFailSilent)op;
    return Op.delete(dnfs.getPath(), -1);
  } else if (op instanceof SetData) {
    SetData sd = (SetData) op;
    return Op.setData(sd.getPath(), sd.getData(), sd.getVersion());
  } else {
    throw new UnsupportedOperationException("Unexpected ZKUtilOp type: "
      + op.getClass().getName());
  }
}
 
Example #10
Source File: TaskManager.java    From Taroco-Scheduler with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 分配指定任务给指定server
 *
 * @param taskName 任务名称
 * @param serverId server节点
 */
private void assignTask2Server(final String taskName, final String serverId) {
    final String taskPath = zkClient.getTaskPath() + "/" + taskName;
    try {
        final List<String> taskServerIds = zkClient.getClient().getChildren().forPath(taskPath);
        if (!CollectionUtils.isEmpty(taskServerIds)) {
            // 任务已分配, 删除分配信息
            for (String taskServerId : taskServerIds) {
                zkClient.getClient().delete().deletingChildrenIfNeeded()
                        .forPath(taskPath + "/" + taskServerId);
            }
        }
        final String runningInfo = "0:" + System.currentTimeMillis();
        final String path = taskPath + "/" + serverId;
        final Stat stat = zkClient.getClient().checkExists().forPath(path);
        if (stat == null) {
            zkClient.getClient()
                    .create()
                    .withMode(CreateMode.EPHEMERAL)
                    .forPath(path, runningInfo.getBytes());
        }
        log.info("成功分配任务 [" + taskPath + "]" + " 给 server [" + serverId + "]");
    } catch (Exception e) {
        log.error("assignTask2Server failed: taskName={}, serverId={}", taskName, serverId, e);
    }
}
 
Example #11
Source File: ZkRpcServer.java    From hasting with MIT License 6 votes vote down vote up
private void addProviderServer(){
	RpcHostAndPort hostAndPort = new RpcHostAndPort(network.getHost(),network.getPort());
	hostAndPort.setTime(time);
	hostAndPort.setToken(this.getToken());
	hostAndPort.setApplication(this.getApplication());

	String serverKey = ZKUtils.genServerKey(this.serverMd5);
	String hostAndPortJson = JSONUtils.toJSON(hostAndPort);
	logger.info("create rpc provider:"+hostAndPortJson);
	try{
		byte[] data = hostAndPortJson.getBytes(defaultEncoding);
		this.zkclient.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(serverKey, data);
		logger.info("add rpc provider success "+serverKey);
	}catch(Exception e){
		logger.error("add provider error",e);
		throw new RpcException(e);
	}
}
 
Example #12
Source File: TestActiveStandbyElector.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Test for a bug encountered during development of HADOOP-8163:
 * ensureBaseNode() should throw an exception if it has to retry
 * more than 3 times to create any part of the path.
 */
@Test
public void testEnsureBaseNodeFails() throws Exception {
  Mockito.doThrow(new KeeperException.ConnectionLossException())
    .when(mockZK).create(
        Mockito.eq(ZK_PARENT_NAME), Mockito.<byte[]>any(),
        Mockito.eq(Ids.OPEN_ACL_UNSAFE), Mockito.eq(CreateMode.PERSISTENT));
  try {
    elector.ensureParentZNode();
    Assert.fail("Did not throw!");
  } catch (IOException ioe) {
    if (!(ioe.getCause() instanceof KeeperException.ConnectionLossException)) {
      throw ioe;
    }
  }
  // Should have tried three times
  Mockito.verify(mockZK, Mockito.times(3)).create(
      Mockito.eq(ZK_PARENT_NAME), Mockito.<byte[]>any(),
      Mockito.eq(Ids.OPEN_ACL_UNSAFE), Mockito.eq(CreateMode.PERSISTENT));
}
 
Example #13
Source File: BackupManager.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void uploadCollectionProperties(URI backupLoc, String backupId, String collectionName) throws IOException {
  URI sourceDir = repository.resolve(backupLoc, backupId, ZK_STATE_DIR);
  URI source = repository.resolve(sourceDir, ZkStateReader.COLLECTION_PROPS_ZKNODE);
  if (!repository.exists(source)) {
    // No collection properties to restore
    return;
  }
  String zkPath = ZkStateReader.COLLECTIONS_ZKNODE + '/' + collectionName + '/' + ZkStateReader.COLLECTION_PROPS_ZKNODE;

  try (IndexInput is = repository.openInput(sourceDir, ZkStateReader.COLLECTION_PROPS_ZKNODE, IOContext.DEFAULT)) {
    byte[] arr = new byte[(int) is.length()];
    is.readBytes(arr, 0, (int) is.length());
    zkStateReader.getZkClient().create(zkPath, arr, CreateMode.PERSISTENT, true);
  } catch (KeeperException | InterruptedException e) {
    throw new IOException("Error uploading file to zookeeper path " + source.toString() + " to " + zkPath,
        SolrZkClient.checkInterrupted(e));
  }
}
 
Example #14
Source File: ZkClient.java    From Taroco-Scheduler with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 初始化root节点
 */
private void initRootPath() {
    //当zk状态正常后才能调用
    final String rootPath = this.schedulerProperties.getZk().getRootPath();
    try {
        if (client.checkExists().forPath(rootPath) == null) {
            client.create()
                    .creatingParentsIfNeeded()
                    .withMode(CreateMode.PERSISTENT).forPath(rootPath, Version.getVersion().getBytes());
        } else {
            //先校验父亲节点,本身是否已经是schedule的目录
            byte[] value = this.client.getData().forPath(rootPath);
            if (value == null) {
                client.setData().forPath(rootPath, Version.getVersion().getBytes());
            } else {
                String dataVersion = new String(value);
                if (!Version.isCompatible(dataVersion)) {
                    log.warn("TarocoScheduler程序版本:" + Version.getVersion() + " ,不匹配Zookeeper中的数据版本:" + dataVersion);
                }
                log.info("当前TarocoScheduler的程序版本:" + Version.getVersion() + ", Zookeeper中的数据版本: " + dataVersion);
            }
        }
    } catch (Exception e) {
        log.error("初始化 rootPath 失败.", e);
    }
}
 
Example #15
Source File: ScheduleTaskZk.java    From Taroco-Scheduler with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void addTask(Task task) {
    try {
        String zkPath = this.pathTask;
        zkPath = zkPath + "/" + task.stringKey();
        if (StringUtils.isBlank(task.getType())) {
            task.setType(DefaultConstants.TYPE_TAROCO_TASK);
        }
        String json = JsonUtil.object2Json(task);
        if (this.client.checkExists().forPath(zkPath) == null) {
            this.client.create().withMode(CreateMode.PERSISTENT).forPath(zkPath, json.getBytes());
        }
    } catch (Exception e) {
        log.error("addTask failed:", e);
    }
}
 
Example #16
Source File: DLMetadata.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
public void create(URI uri) throws IOException {
    DistributedLogConfiguration conf = new DistributedLogConfiguration();
    ZooKeeperClient zkc = ZooKeeperClientBuilder.newBuilder()
            .sessionTimeoutMs(conf.getZKSessionTimeoutMilliseconds())
            .retryThreadCount(conf.getZKClientNumberRetryThreads())
            .requestRateLimit(conf.getZKRequestRateLimit())
            .zkAclId(conf.getZkAclId())
            .uri(uri)
            .build();
    byte[] data = serialize();
    try {
        Utils.zkCreateFullPathOptimistic(zkc, uri.getPath(), data,
            zkc.getDefaultACL(), CreateMode.PERSISTENT);
    } catch (KeeperException ke) {
        throw new ZKException("Encountered zookeeper exception on creating dl metadata", ke);
    } finally {
        zkc.close();
    }
}
 
Example #17
Source File: PersistentEphemeralNode.java    From curator-extensions with Apache License 2.0 6 votes vote down vote up
/**
 * Create the ephemeral node in ZooKeeper.  If the node cannot be created in a timely fashion then an exception will
 * be thrown.
 *
 * @param curator Client to manage ZooKeeper nodes with.
 * @param basePath Path to parent node this node should be created in.
 * @param data Data to store in the node.
 * @param mode Node creation mode.
 */
public PersistentEphemeralNode(CuratorFramework curator, String basePath, byte[] data, CreateMode mode) {
    Objects.requireNonNull(curator);
    checkArgument(curator.getState() == CuratorFrameworkState.STARTED);
    Objects.requireNonNull(basePath);
    Objects.requireNonNull(data);
    Objects.requireNonNull(mode);
    checkArgument(mode == CreateMode.EPHEMERAL || mode == CreateMode.EPHEMERAL_SEQUENTIAL);

    // TODO: Share this executor across multiple persistent ephemeral nodes in a way that guarantees that it is a
    // TODO: single thread executor.
    _executor = Executors.newSingleThreadScheduledExecutor(THREAD_FACTORY);
    _async = new Async(_executor, new Sync(curator, basePath, data, mode));

    CountDownLatch latch = new CountDownLatch(1);
    _async.createNode(latch);
    await(latch, CREATION_WAIT_IN_SECONDS, TimeUnit.SECONDS);
}
 
Example #18
Source File: OverseerCollectionMessageHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * This doesn't validate the config (path) itself and is just responsible for creating the confNode.
 * That check should be done before the config node is created.
 */
public static void createConfNode(DistribStateManager stateManager, String configName, String coll) throws IOException, AlreadyExistsException, BadVersionException, KeeperException, InterruptedException {

  if (configName != null) {
    String collDir = ZkStateReader.COLLECTIONS_ZKNODE + "/" + coll;
    log.debug("creating collections conf node {} ", collDir);
    byte[] data = Utils.toJSON(makeMap(ZkController.CONFIGNAME_PROP, configName));
    if (stateManager.hasData(collDir)) {
      stateManager.setData(collDir, data, -1);
    } else {
      stateManager.makePath(collDir, data, CreateMode.PERSISTENT, false);
    }
  } else {
    throw new SolrException(ErrorCode.BAD_REQUEST,"Unable to get config name");
  }
}
 
Example #19
Source File: ZkDistributeLock.java    From azeroth with Apache License 2.0 6 votes vote down vote up
/**
 * @param zkServers
 * @param lockName
 * @param sessionTimeout
 */
public ZkDistributeLock(String zkServers, String lockName, int sessionTimeout) {
    if (lockName.contains(LOCK_KEY_SUFFIX)) {
        throw new LockException("lockName 不能包含[" + LOCK_KEY_SUFFIX + "]");
    }
    this.lockName = lockName;
    this.sessionTimeout = sessionTimeout;
    try {
        zk = new ZooKeeper(zkServers, sessionTimeout, this);
        Stat stat = zk.exists(ROOT_PATH, false);
        if (stat == null) {
            // 创建根节点
            zk.create(ROOT_PATH, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
    } catch (Exception e) {
        throw new LockException(e);
    }
}
 
Example #20
Source File: ZookeeperTest.java    From uncode-schedule with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testCreateTask() throws Exception {
	ZooKeeper zk = new ZooKeeper("localhost:2181", 3000, null);
	List<ACL> acls = new ArrayList<ACL>();
	zk.addAuthInfo("digest", "ScheduleAdmin:password".getBytes());
	acls.add(new ACL(ZooDefs.Perms.ALL, new Id("digest",
			DigestAuthenticationProvider.generateDigest("ScheduleAdmin:password"))));
	acls.add(new ACL(ZooDefs.Perms.READ, Ids.ANYONE_ID_UNSAFE));
	zk.create("/uncode/schedule/task/taskObj#print", new byte[0], acls, CreateMode.PERSISTENT);
	zk.getData("/uncode/schedule/task/taskObj#print", false, null);
}
 
Example #21
Source File: ZookeeperRegistryExecutor.java    From Thunder with Apache License 2.0 5 votes vote down vote up
@Override
public void registerService(String interfaze, ApplicationEntity applicationEntity) throws Exception {
    String application = applicationEntity.getApplication();
    String group = applicationEntity.getGroup();

    StringBuilder builder = createServiceInterfacePath(interfaze, application, group);
    String path = builder.toString();

    if (!invoker.pathExist(path)) {
        invoker.createPath(path, CreateMode.PERSISTENT);
    }

    List<String> childPathList = invoker.getChildPathList(path);
    for (String childPath : childPathList) {
        String applicationJson = childPath.substring(childPath.lastIndexOf("/") + 1);
        ApplicationEntity entity = ZookeeperApplicationEntityFactory.fromJson(applicationJson);
        if (entity.equals(applicationEntity)) {
            LOG.info("Delete expired service [{}]", childPath);
            if (invoker.pathExist(childPath)) {
                invoker.deletePath(childPath);
            }
        }
    }

    builder.append("/");
    builder.append(ZookeeperApplicationEntityFactory.toJson(applicationEntity));
    path = builder.toString();

    LOG.info("Register service [{}]", path);
    invoker.createPath(path, CreateMode.EPHEMERAL);
}
 
Example #22
Source File: ZKStoreHelper.java    From pravega with Apache License 2.0 5 votes vote down vote up
CompletableFuture<String> createEphemeralSequentialZNode(final String path) {
    final CompletableFuture<String> result = new CompletableFuture<>();

    try {
        CreateBuilder createBuilder = client.create();
        BackgroundCallback callback = callback(x -> result.complete(x.getName()),
                result::completeExceptionally, path);
        createBuilder.creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
                     .inBackground(callback, executor).forPath(path);
    } catch (Exception e) {
        result.completeExceptionally(StoreException.create(StoreException.Type.UNKNOWN, e, path));
    }

    return result;
}
 
Example #23
Source File: PersistentNode.java    From curator with Apache License 2.0 5 votes vote down vote up
/**
 * @param givenClient        client instance
 * @param mode          creation mode
 * @param useProtection if true, call {@link CreateBuilder#withProtection()}
 * @param basePath the base path for the node
 * @param initData data for the node
 * @param ttl for ttl modes, the ttl to use
 */
public PersistentNode(CuratorFramework givenClient, final CreateMode mode, boolean useProtection, final String basePath, byte[] initData, long ttl)
{
    this.useProtection = useProtection;
    this.client = Preconditions.checkNotNull(givenClient, "client cannot be null").newWatcherRemoveCuratorFramework();
    this.basePath = PathUtils.validatePath(basePath);
    this.mode = Preconditions.checkNotNull(mode, "mode cannot be null");
    this.ttl = ttl;
    final byte[] data = Preconditions.checkNotNull(initData, "data cannot be null");

    backgroundCallback = new BackgroundCallback()
    {
        @Override
        public void processResult(CuratorFramework dummy, CuratorEvent event) throws Exception
        {
            if ( isActive() )
            {
                processBackgroundCallback(event);
            }
            else
            {
                processBackgroundCallbackClosedState(event);
            }
        }
    };

    this.data.set(Arrays.copyOf(data, data.length));
}
 
Example #24
Source File: ZookeeperRegistryCenter.java    From idworker with Apache License 2.0 5 votes vote down vote up
@Override
public String persistSequential(final String key, final String value) {
    try {
        return client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath(key,
                value.getBytes(StandardCharsets.UTF_8));
    } catch (final Exception ex) {
        RegExceptionHandler.handleException(ex);
    }
    return null;
}
 
Example #25
Source File: BaseClientTest.java    From opensharding-spi-impl with Apache License 2.0 5 votes vote down vote up
protected final void delAllChildren(final IZookeeperClient client) throws KeeperException, InterruptedException {
    String key = "a/b/bb";
    client.createAllNeedPath(key, "bb", CreateMode.PERSISTENT);
    key = "a/c/cc";
    client.createAllNeedPath(key, "cc", CreateMode.PERSISTENT);
    assertNotNull(getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false));
    client.deleteAllChildren("a");
    assertNull(getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false));
    assertNotNull(getZooKeeper(client).exists("/" + TestSupport.ROOT, false));
    ((BaseClient) client).deleteNamespace();
}
 
Example #26
Source File: Curator4ZookeeperRegistry.java    From spring-cloud-sofastack-samples with Apache License 2.0 5 votes vote down vote up
/***
 * 订阅
 * @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).forPath(consumerPath + CONTEXT_SEP + encodeUrl);

        } catch (KeeperException.NodeExistsException nodeExistsException) {
            if (LOGGER.isWarnEnabled()) {
                LOGGER.warn("consumer has exists in zookeeper, consumer=" + url);
            }
        } catch (Throwable t) {
            throw new SofaRpcRuntimeException(
                "Failed to register consumer to zookeeperRegistry!", t);
        }
    }
}
 
Example #27
Source File: TestPersistentNode.java    From xian with Apache License 2.0 5 votes vote down vote up
@Test
public void testQuickSetData() throws Exception
{
    final byte[] TEST_DATA = "hey".getBytes();
    final byte[] ALT_TEST_DATA = "there".getBytes();

    Timing timing = new Timing();
    PersistentNode pen = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try
    {
        client.start();
        pen = new PersistentNode(client, CreateMode.PERSISTENT, false, "/test", TEST_DATA);
        pen.start();
        try
        {
            pen.setData(ALT_TEST_DATA);
            Assert.fail("IllegalStateException should have been thrown");
        }
        catch ( IllegalStateException dummy )
        {
            // expected
        }
    }
    finally
    {
        CloseableUtils.closeQuietly(pen);
        CloseableUtils.closeQuietly(client);
    }
}
 
Example #28
Source File: ZKUtil.java    From codes-scratch-zookeeper-netty with Apache License 2.0 5 votes vote down vote up
public static void setPath(CuratorFramework client, String path, String content, CreateMode mode) {
    try {
        if (client.checkExists().forPath(path) == null) {
            ((ACLBackgroundPathAndBytesable) client.create().creatingParentsIfNeeded().withMode(mode))
                    .forPath(path, List2StringUtil.toBytes(content));
        } else { client.setData().forPath(path, List2StringUtil.toBytes(content)); }
    } catch (Exception e) {
        LOGGER.error("ZKUtil-->>setPath(CuratorFramework client, String path, String content, CreateMode mode) error,", e);
    }
}
 
Example #29
Source File: ZKSubscriptionStateStore.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
/**
 * Advances the position associated with the subscriber.
 *
 * @param newPosition - new commit position
 */
@Override
public CompletableFuture<Void> advanceCommitPosition(DLSN newPosition) {
    if (null == lastCommittedPosition.get()
            || (newPosition.compareTo(lastCommittedPosition.get()) > 0)) {
        lastCommittedPosition.set(newPosition);
        return Utils.zkAsyncCreateFullPathOptimisticAndSetData(zooKeeperClient,
            zkPath, newPosition.serialize().getBytes(Charsets.UTF_8),
            zooKeeperClient.getDefaultACL(),
            CreateMode.PERSISTENT);
    } else {
        return FutureUtils.Void();
    }
}
 
Example #30
Source File: TestAuthorizationFramework.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void distribSetUp() throws Exception {
  super.distribSetUp();
  try (ZkStateReader zkStateReader = new ZkStateReader(zkServer.getZkAddress(),
      TIMEOUT, TIMEOUT)) {
    zkStateReader.getZkClient().create(ZkStateReader.SOLR_SECURITY_CONF_PATH,
        "{\"authorization\":{\"class\":\"org.apache.solr.security.MockAuthorizationPlugin\"}}".getBytes(StandardCharsets.UTF_8),
        CreateMode.PERSISTENT, true);
  }
}