Java Code Examples for org.apache.curator.framework.CuratorFramework#usingNamespace()

The following examples show how to use org.apache.curator.framework.CuratorFramework#usingNamespace() . These examples are extracted from open source projects. 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 Project: xian   File: TestNamespaceFacade.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void     testGetNamespace() throws Exception
{
    CuratorFramework    client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    CuratorFramework    client2 = CuratorFrameworkFactory.builder().namespace("snafu").retryPolicy(new RetryOneTime(1)).connectString("").build();
    try
    {
        client.start();

        CuratorFramework fooClient = client.usingNamespace("foo");
        CuratorFramework barClient = client.usingNamespace("bar");

        Assert.assertEquals(client.getNamespace(), "");
        Assert.assertEquals(client2.getNamespace(), "snafu");
        Assert.assertEquals(fooClient.getNamespace(), "foo");
        Assert.assertEquals(barClient.getNamespace(), "bar");
    }
    finally
    {
        CloseableUtils.closeQuietly(client2);
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 2
Source Project: xian   File: TestNamespaceFacade.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void     testSimultaneous() throws Exception
{
    CuratorFramework    client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try
    {
        client.start();

        CuratorFramework fooClient = client.usingNamespace("foo");
        CuratorFramework barClient = client.usingNamespace("bar");

        fooClient.create().forPath("/one");
        barClient.create().forPath("/one");

        Assert.assertNotNull(client.getZookeeperClient().getZooKeeper().exists("/foo/one", false));
        Assert.assertNotNull(client.getZookeeperClient().getZooKeeper().exists("/bar/one", false));
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 3
Source Project: curator   File: TestFramework.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateContainersUsingNamespace() throws Exception
{
    final String namespace = "container2";
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
    CuratorFramework client = builder.connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).build();
    try
    {
        client.start();
        CuratorFramework nsClient = client.usingNamespace(namespace);
        String path = "/path1/path2";
        nsClient.createContainers(path);
        Assert.assertNotNull(nsClient.checkExists().forPath(path));
        Assert.assertNotNull(nsClient.getZookeeperClient().getZooKeeper().exists("/" + namespace + path, false));
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 4
Source Project: Flink-CEPplus   File: ZooKeeperUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link ZooKeeperSubmittedJobGraphStore} instance.
 *
 * @param client        The {@link CuratorFramework} ZooKeeper client to use
 * @param configuration {@link Configuration} object
 * @return {@link ZooKeeperSubmittedJobGraphStore} instance
 * @throws Exception if the submitted job graph store cannot be created
 */
public static ZooKeeperSubmittedJobGraphStore createSubmittedJobGraphs(
		CuratorFramework client,
		Configuration configuration) throws Exception {

	checkNotNull(configuration, "Configuration");

	RetrievableStateStorageHelper<SubmittedJobGraph> stateStorage = createFileSystemStateStorage(configuration, "submittedJobGraph");

	// ZooKeeper submitted jobs root dir
	String zooKeeperSubmittedJobsPath = configuration.getString(HighAvailabilityOptions.HA_ZOOKEEPER_JOBGRAPHS_PATH);

	// Ensure that the job graphs path exists
	client.newNamespaceAwareEnsurePath(zooKeeperSubmittedJobsPath)
		.ensure(client.getZookeeperClient());

	// All operations will have the path as root
	CuratorFramework facade = client.usingNamespace(client.getNamespace() + zooKeeperSubmittedJobsPath);

	final String zooKeeperFullSubmittedJobsPath = client.getNamespace() + zooKeeperSubmittedJobsPath;

	final ZooKeeperStateHandleStore<SubmittedJobGraph> zooKeeperStateHandleStore = new ZooKeeperStateHandleStore<>(facade, stateStorage);

	final PathChildrenCache pathCache = new PathChildrenCache(facade, "/", false);

	return new ZooKeeperSubmittedJobGraphStore(
		zooKeeperFullSubmittedJobsPath,
		zooKeeperStateHandleStore,
		pathCache);
}
 
Example 5
Source Project: Flink-CEPplus   File: ZooKeeperUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns a facade of the client that uses the specified namespace, and ensures that all nodes
 * in the path exist.
 *
 * @param client ZK client
 * @param path the new namespace
 * @return ZK Client that uses the new namespace
 * @throws Exception ZK errors
 */
public static CuratorFramework useNamespaceAndEnsurePath(final CuratorFramework client, final String path) throws Exception {
	Preconditions.checkNotNull(client, "client must not be null");
	Preconditions.checkNotNull(path, "path must not be null");

	// Ensure that the checkpoints path exists
	client.newNamespaceAwareEnsurePath(path)
		.ensure(client.getZookeeperClient());

	// All operations will have the path as root
	return client.usingNamespace(generateZookeeperPath(client.getNamespace(), path));
}
 
Example 6
@Nonnull
private ZooKeeperSubmittedJobGraphStore createZooKeeperSubmittedJobGraphStore(String fullPath) throws Exception {
	final CuratorFramework client = ZooKeeper.getClient();
	// Ensure that the job graphs path exists
	client.newNamespaceAwareEnsurePath(fullPath).ensure(client.getZookeeperClient());

	// All operations will have the path as root
	CuratorFramework facade = client.usingNamespace(client.getNamespace() + fullPath);
	return new ZooKeeperSubmittedJobGraphStore(
		fullPath,
		new ZooKeeperStateHandleStore<>(
			facade,
			localStateStorage),
		new PathChildrenCache(facade, "/", false));
}
 
Example 7
Source Project: flink   File: ZooKeeperUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link ZooKeeperSubmittedJobGraphStore} instance.
 *
 * @param client        The {@link CuratorFramework} ZooKeeper client to use
 * @param configuration {@link Configuration} object
 * @return {@link ZooKeeperSubmittedJobGraphStore} instance
 * @throws Exception if the submitted job graph store cannot be created
 */
public static ZooKeeperSubmittedJobGraphStore createSubmittedJobGraphs(
		CuratorFramework client,
		Configuration configuration) throws Exception {

	checkNotNull(configuration, "Configuration");

	RetrievableStateStorageHelper<SubmittedJobGraph> stateStorage = createFileSystemStateStorage(configuration, "submittedJobGraph");

	// ZooKeeper submitted jobs root dir
	String zooKeeperSubmittedJobsPath = configuration.getString(HighAvailabilityOptions.HA_ZOOKEEPER_JOBGRAPHS_PATH);

	// Ensure that the job graphs path exists
	client.newNamespaceAwareEnsurePath(zooKeeperSubmittedJobsPath)
		.ensure(client.getZookeeperClient());

	// All operations will have the path as root
	CuratorFramework facade = client.usingNamespace(client.getNamespace() + zooKeeperSubmittedJobsPath);

	final String zooKeeperFullSubmittedJobsPath = client.getNamespace() + zooKeeperSubmittedJobsPath;

	final ZooKeeperStateHandleStore<SubmittedJobGraph> zooKeeperStateHandleStore = new ZooKeeperStateHandleStore<>(facade, stateStorage);

	final PathChildrenCache pathCache = new PathChildrenCache(facade, "/", false);

	return new ZooKeeperSubmittedJobGraphStore(
		zooKeeperFullSubmittedJobsPath,
		zooKeeperStateHandleStore,
		pathCache);
}
 
Example 8
Source Project: flink   File: ZooKeeperUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns a facade of the client that uses the specified namespace, and ensures that all nodes
 * in the path exist.
 *
 * @param client ZK client
 * @param path the new namespace
 * @return ZK Client that uses the new namespace
 * @throws Exception ZK errors
 */
public static CuratorFramework useNamespaceAndEnsurePath(final CuratorFramework client, final String path) throws Exception {
	Preconditions.checkNotNull(client, "client must not be null");
	Preconditions.checkNotNull(path, "path must not be null");

	// Ensure that the checkpoints path exists
	client.newNamespaceAwareEnsurePath(path)
		.ensure(client.getZookeeperClient());

	// All operations will have the path as root
	return client.usingNamespace(generateZookeeperPath(client.getNamespace(), path));
}
 
Example 9
@Nonnull
private ZooKeeperSubmittedJobGraphStore createZooKeeperSubmittedJobGraphStore(String fullPath) throws Exception {
	final CuratorFramework client = ZooKeeper.getClient();
	// Ensure that the job graphs path exists
	client.newNamespaceAwareEnsurePath(fullPath).ensure(client.getZookeeperClient());

	// All operations will have the path as root
	CuratorFramework facade = client.usingNamespace(client.getNamespace() + fullPath);
	return new ZooKeeperSubmittedJobGraphStore(
		fullPath,
		new ZooKeeperStateHandleStore<>(
			facade,
			localStateStorage),
		new PathChildrenCache(facade, "/", false));
}
 
Example 10
Source Project: xian   File: TestNamespaceFacade.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void     testIsStarted() throws Exception
{
    CuratorFramework    client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    CuratorFramework    namespaced = client.usingNamespace(null);

    Assert.assertEquals(client.getState(), namespaced.getState(), "Namespaced state did not match true state after call to start.");

    client.close();
    Assert.assertEquals(client.getState(), namespaced.getState(), "Namespaced state did not match true state after call to close.");
}
 
Example 11
Source Project: xian   File: TestNamespaceFacade.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Test that ACLs work on a NamespaceFacade. See CURATOR-132
 * @throws Exception
 */
@Test
public void testACL() throws Exception
{
    CuratorFramework    client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    client.getZookeeperClient().blockUntilConnectedOrTimedOut();

    client.create().creatingParentsIfNeeded().forPath("/parent/child", "A string".getBytes());
    CuratorFramework client2 = client.usingNamespace("parent");

    Assert.assertNotNull(client2.getData().forPath("/child"));  
    client.setACL().withACL(Collections.singletonList(
        new ACL(ZooDefs.Perms.WRITE, ZooDefs.Ids.ANYONE_ID_UNSAFE))).
            forPath("/parent/child");
    // This will attempt to setACL on /parent/child, Previously this failed because /child
    // isn't present. Using "child" would case a failure because the path didn't start with
    // a slash
    try
    {
        List<ACL> acls = client2.getACL().forPath("/child");
        Assert.assertNotNull(acls);
        Assert.assertEquals(acls.size(), 1);
        Assert.assertEquals(acls.get(0).getId(), ZooDefs.Ids.ANYONE_ID_UNSAFE);
        Assert.assertEquals(acls.get(0).getPerms(), ZooDefs.Perms.WRITE);
        client2.setACL().withACL(Collections.singletonList(
            new ACL(ZooDefs.Perms.DELETE, ZooDefs.Ids.ANYONE_ID_UNSAFE))).
                forPath("/child");
        Assert.fail("Expected auth exception was not thrown");
    }
    catch(NoAuthException e)
    {
        //Expected
    }
}
 
Example 12
@Singleton
@Provides
public CuratorFramework provideCurator(ZooKeeperConfiguration config, BaragonConnectionStateListener connectionStateListener) {
  CuratorFramework client = CuratorFrameworkFactory.newClient(
    config.getQuorum(),
    config.getSessionTimeoutMillis(),
    config.getConnectTimeoutMillis(),
    new ExponentialBackoffRetry(config.getRetryBaseSleepTimeMilliseconds(), config.getRetryMaxTries()));

  client.getConnectionStateListenable().addListener(connectionStateListener);

  client.start();

  return client.usingNamespace(config.getZkNamespace());
}
 
Example 13
Source Project: xian   File: TestFrameworkEdges.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testProtectedCreateNodeDeletion() throws Exception
{
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), 1, new RetryNTimes(0, 0));
    try
    {
        client.start();

        for ( int i = 0; i < 2; ++i )
        {
            CuratorFramework localClient = (i == 0) ? client : client.usingNamespace("nm");
            localClient.create().forPath("/parent");
            Assert.assertEquals(localClient.getChildren().forPath("/parent").size(), 0);

            CreateBuilderImpl createBuilder = (CreateBuilderImpl)localClient.create();
            createBuilder.failNextCreateForTesting = true;
            FindAndDeleteProtectedNodeInBackground.debugInsertError.set(true);
            try
            {
                createBuilder.withProtection().forPath("/parent/test");
                Assert.fail("failNextCreateForTesting should have caused a ConnectionLossException");
            }
            catch ( KeeperException.ConnectionLossException e )
            {
                // ignore, correct
            }

            timing.sleepABit();
            List<String> children = localClient.getChildren().forPath("/parent");
            Assert.assertEquals(children.size(), 0, children.toString()); // protected mode should have deleted the node

            localClient.delete().forPath("/parent");
        }
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 14
Source Project: emodb   File: ZKNamespaces.java    License: Apache License 2.0 5 votes vote down vote up
public static CuratorFramework usingChildNamespace(CuratorFramework curator, String... children) {
    String path = curator.getNamespace();
    for (String child : children) {
        path = path.length() > 0 ? ZKPaths.makePath(path, child) : child;
    }
    // Curator does not allow namespaces to start with a leading '/'
    if (path.startsWith("/")) {
        path = path.substring(1);
    }
    return curator.usingNamespace(path);
}
 
Example 15
Source Project: cloud-config   File: CloudConfigSample.java    License: MIT License 5 votes vote down vote up
private static void prepare(TestingServer server) throws Exception {
        CuratorFramework zkRootClient = null;
        try {
            zkRootClient = CuratorFrameworkFactory.builder()
                    .connectString(server.getConnectString())
                    .retryPolicy(new BoundedExponentialBackoffRetry(10, 100, 7))
                    .build();
            zkRootClient.start();

            ZooKeeper zk = zkRootClient.getZookeeperClient().getZooKeeper();
            ZKPaths.mkdirs(zk, "/" + CloudConfigCommon.CONFIG_ROOT);
            ZKPaths.mkdirs(zk, "/"+CloudConfigCommon.PROPERTY_ROOT);

            CuratorFramework zkConfigClient = zkRootClient.usingNamespace(CloudConfigCommon.CONFIG_ROOT);
//        CuratorFramework zkPropsClient  = zkRootClient.usingNamespace(CloudConfigCommon.PROPERTY_ROOT);

            String config = "{\n" +
                    "    \"driverClassName\" : \"com.mysql.jdbc.Driver\",\n" +
                    "    \"userName\" : \"root\",\n" +
                    "    \"password\" : \"1111\", \n"+
                    "    \"jdbcUrl\" : \"jdbc:mysql://127.0.0.1:3306/a?characterEncoding=utf8&createDatabaseIfNotExist=true\"\n"+
                    "}";

            zkConfigClient.create().creatingParentsIfNeeded().forPath("/database/mydb", config.getBytes());
        } finally {
            if(zkRootClient!=null) {
                zkRootClient.close();
            }
        }
    }
 
Example 16
Source Project: curator   File: TestFrameworkEdges.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testProtectedCreateNodeDeletion() throws Exception
{
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), 1, new RetryNTimes(0, 0));
    try
    {
        client.start();

        for ( int i = 0; i < 2; ++i )
        {
            CuratorFramework localClient = (i == 0) ? client : client.usingNamespace("nm");
            localClient.create().forPath("/parent");
            Assert.assertEquals(localClient.getChildren().forPath("/parent").size(), 0);

            CreateBuilderImpl createBuilder = (CreateBuilderImpl)localClient.create();
            createBuilder.failNextCreateForTesting = true;
            FindAndDeleteProtectedNodeInBackground.debugInsertError.set(true);
            try
            {
                createBuilder.withProtection().forPath("/parent/test");
                Assert.fail("failNextCreateForTesting should have caused a ConnectionLossException");
            }
            catch ( KeeperException.ConnectionLossException e )
            {
                // ignore, correct
            }

            timing.sleepABit();
            List<String> children = localClient.getChildren().forPath("/parent");
            Assert.assertEquals(children.size(), 0, children.toString()); // protected mode should have deleted the node

            localClient.delete().forPath("/parent");
        }
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 17
@Required
public void setClient(CuratorFramework client) {
    this.client = client.usingNamespace(CloudConfigCommon.CONFIG_ROOT);
}
 
Example 18
@Autowired
public void setClient(CuratorFramework client) {
    this.client = client.usingNamespace(CloudConfigCommon.CONFIG_ROOT);
}
 
Example 19
public void setClient(CuratorFramework client) {
    this.client = client.usingNamespace(CloudConfigCommon.PROPERTY_ROOT);
}
 
Example 20
Source Project: workflow   File: WorkflowManagerBuilder.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * <strong>required</strong><br>
 * Set the Curator instance to use. In addition
 * to the Curator instance, specify a namespace for the workflow and a version. The namespace
 * and version combine to create a unique workflow. All instances using the same namespace and version
 * are logically part of the same workflow.
 *
 * @param curator Curator instance
 * @param namespace workflow namespace
 * @param version workflow version
 * @return this (for chaining)
 */
public WorkflowManagerBuilder withCurator(CuratorFramework curator, String namespace, String version)
{
    curator = Preconditions.checkNotNull(curator, "curator cannot be null");
    namespace = Preconditions.checkNotNull(namespace, "namespace cannot be null");
    version = Preconditions.checkNotNull(version, "version cannot be null");
    this.curator = curator.usingNamespace(namespace + "-" + version);
    return this;
}