Arun
Arun

Reputation: 1286

Could not create zookeeper node (which does not already exist) from the root using java

I intend to create a zookeeper node from java. I create the curator client initially with which I retrieve the zookeeper object and create the nodes which fail.

What I do?

        //client is the CuratorFramework object
        String MUTEX_LEADER_PATH = "/leader/jobadmin";
        client.getZookeeperClient().getZooKeeper().create(MUTEX_LEADER_PATH, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

What I get?

    [2015-04-20 10:53:17,992] [ERROR] [org.apache.curator.framework.recipes.leader.LeaderSelector] [LeaderSelector-0] mutex.acquire() threw an exception
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /leader
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:111)
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
    at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:783)
    at org.apache.curator.utils.ZKPaths.mkdirs(ZKPaths.java:199)
    at org.apache.curator.framework.imps.CreateBuilderImpl$11.call(CreateBuilderImpl.java:682)
    at org.apache.curator.framework.imps.CreateBuilderImpl$11.call(CreateBuilderImpl.java:660)
    at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:107)
    at org.apache.curator.framework.imps.CreateBuilderImpl.pathInForeground(CreateBuilderImpl.java:656)
    at org.apache.curator.framework.imps.CreateBuilderImpl.protectedPathInForeground(CreateBuilderImpl.java:441)
    at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:431)
    at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:411)
    at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:44)
    at org.apache.curator.framework.recipes.locks.LockInternals.attemptLock(LockInternals.java:224)
    at org.apache.curator.framework.recipes.locks.InterProcessMutex.internalLock(InterProcessMutex.java:221)
    at org.apache.curator.framework.recipes.locks.InterProcessMutex.acquire(InterProcessMutex.java:77)
    at org.apache.curator.framework.recipes.leader.LeaderSelector.doWork(LeaderSelector.java:378)
    at org.apache.curator.framework.recipes.leader.LeaderSelector.doWorkLoop(LeaderSelector.java:436)
    at org.apache.curator.framework.recipes.leader.LeaderSelector.access$100(LeaderSelector.java:64)
    at org.apache.curator.framework.recipes.leader.LeaderSelector$2.call(LeaderSelector.java:241)
    at org.apache.curator.framework.recipes.leader.LeaderSelector$2.call(LeaderSelector.java:235)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

Tried with different create modes but the result is the same.

Creating the client using the following code:

private synchronized CuratorFramework createClient() {
    if (client != null) {
        return client;
    }

    RetryPolicy retryPolicy = new RetryUntilElapsed(SESSION_TIMEOUT, 1000);
    client = CuratorFrameworkFactory.newClient(MUTEX_LEADER_PATH, SESSION_TIMEOUT, 100, retryPolicy);
    client.start();

    return client;
}

How can I create the zookeeper node which doesn't exist from the root?

Upvotes: 1

Views: 3817

Answers (1)

Arun
Arun

Reputation: 1286

I solved the problem with the following piece of code.

 client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).withACL(Ids.OPEN_ACL_UNSAFE).forPath(MUTEX_LEADER_PATH, new byte[0]);

It worked perfect!

Upvotes: 2

Related Questions