How to create a ttl node using apache curator? I have tried the following
ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3);
String connectionString = "127.0.0.1:2181";
CuratorFramework client = CuratorFrameworkFactory.newClient(connectionString, retryPolicy);
client.getUnhandledErrorListenable().addListener((message, e) -> {
System.err.println("error=" + message);
e.printStackTrace();
});
client.getConnectionStateListenable().addListener((c, newState) -> {
System.out.println("state=" + newState);
});
client.start();
Code 1:
PersistentTtlNode persistentTtlNode = new PersistentTtlNode(client, "/stores/abc.com", 10000, "".getBytes());
persistentTtlNode.start();
boolean flag = persistentTtlNode.waitForInitialCreate(1000, TimeUnit.MICROSECONDS);
System.out.println(flag);
persistentTtlNode.close();
Result:
Node is not getting created and the flag
is false
Code 2:
client.create().withTtl(1000).creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT_WITH_TTL).forPath("/stores/india.com");
Result: Getting the following exception:
Exception in thread "main" org.apache.zookeeper.KeeperException$UnimplementedException: KeeperErrorCode = Unimplemented for /stores/india.com
at org.apache.zookeeper.KeeperException.create(KeeperException.java:106)
at org.apache.zookeeper.KeeperException.create(KeeperException.java:54)
at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:1837)
at org.apache.curator.framework.imps.CreateBuilderImpl$16.call(CreateBuilderImpl.java:1131)
at org.apache.curator.framework.imps.CreateBuilderImpl$16.call(CreateBuilderImpl.java:1113)
at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:93)
at org.apache.curator.framework.imps.CreateBuilderImpl.pathInForeground(CreateBuilderImpl.java:1110)
at org.apache.curator.framework.imps.CreateBuilderImpl.protectedPathInForeground(CreateBuilderImpl.java:593)
at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:583)
at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:561)
at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:48)
at Main.main(Main.java:67)
What's the correct way to create a node with ttl in apache curator?
Finally found the answer, apparently we need to enable certain configurations while starting zookeeper.
conf
directoryzoo.cfg
tickTime=2000
dataDir=./data/zookeeper
clientPort=2181
maxClientCnxns=60
extendedTypesEnabled=true
emulate353TTLNodes=true
My mistake was I added zookeeper.extendedTypesEnabled
in the config file. Don't add the word zookeeper
. It's needed only if you are using command line shell i.e zkCli.sh
.
Now restart the cluster using bin/zkServer.sh stop
and bin/zkServer.sh start
.
The above commands will work fine without throwing any more exceptions.
Note: If the znode is not modified within the TTL and has no children it will become a candidate to be deleted by the server at some point in the future.
Keep in mind, zookeeper version should be > 3.5.3
and the java client version should be the same as zookeeper version