Repository: zookeeper Updated Branches: refs/heads/master fb7ddacfc -> 54c7f6b47
This PR adds support for TTL nodes to the CLI Author: randgalt <[email protected]> Reviewers: Michael Han <[email protected]>, Camille Fournier <[email protected]> Closes #193 from Randgalt/ZOOKEEPER-2608 and squashes the following commits: bd57197 [randgalt] fixed spacing nits f241102 [randgalt] Merge branch 'master' into ZOOKEEPER-2608 72fdda9 [randgalt] Added ttl option to CLI create command 02fd557 [randgalt] Removed bogus import 9cb973c [randgalt] This patch takes advantage of 3.5's container support. Most of the work needed to support TTLs is there already. In order not to break on-disk and protocol compatibility the ephemeralOwner is yet-again overloaded to have special meaning. New opcodes and transaction records had to be added in a similar manner to Containers Project: http://git-wip-us.apache.org/repos/asf/zookeeper/repo Commit: http://git-wip-us.apache.org/repos/asf/zookeeper/commit/54c7f6b4 Tree: http://git-wip-us.apache.org/repos/asf/zookeeper/tree/54c7f6b4 Diff: http://git-wip-us.apache.org/repos/asf/zookeeper/diff/54c7f6b4 Branch: refs/heads/master Commit: 54c7f6b472ed7bb10fd86cb3c28262fa4f7351d9 Parents: fb7ddac Author: randgalt <[email protected]> Authored: Thu Mar 23 10:46:42 2017 -0700 Committer: Michael Han <[email protected]> Committed: Thu Mar 23 10:46:42 2017 -0700 ---------------------------------------------------------------------- .../org/apache/zookeeper/cli/CreateCommand.java | 37 +++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zookeeper/blob/54c7f6b4/src/java/main/org/apache/zookeeper/cli/CreateCommand.java ---------------------------------------------------------------------- diff --git a/src/java/main/org/apache/zookeeper/cli/CreateCommand.java b/src/java/main/org/apache/zookeeper/cli/CreateCommand.java index 2c37784..ee6f58a 100644 --- a/src/java/main/org/apache/zookeeper/cli/CreateCommand.java +++ b/src/java/main/org/apache/zookeeper/cli/CreateCommand.java @@ -23,6 +23,8 @@ import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.EphemeralType; /** * create command for cli @@ -33,14 +35,15 @@ public class CreateCommand extends CliCommand { private String[] args; private CommandLine cl; - { + static { options.addOption(new Option("e", false, "ephemeral")); options.addOption(new Option("s", false, "sequential")); options.addOption(new Option("c", false, "container")); + options.addOption(new Option("t", true, "ttl")); } public CreateCommand() { - super("create", "[-s] [-e] [-c] path [data] [acl]"); + super("create", "[-s] [-e] [-c] [-t ttl] path [data] [acl]"); } @@ -62,23 +65,47 @@ public class CreateCommand extends CliCommand { @Override public boolean exec() throws CliException { - CreateMode flags = CreateMode.PERSISTENT; boolean hasE = cl.hasOption("e"); boolean hasS = cl.hasOption("s"); boolean hasC = cl.hasOption("c"); + boolean hasT = cl.hasOption("t"); if (hasC && (hasE || hasS)) { throw new MalformedCommandException("-c cannot be combined with -s or -e. Containers cannot be ephemeral or sequential."); } + long ttl; + try { + ttl = hasT ? Long.parseLong(cl.getOptionValue("t")) : 0; + } catch (NumberFormatException e) { + throw new MalformedCommandException("-t argument must be a long value"); + } + if (hasT && hasE) { + throw new MalformedCommandException("TTLs cannot be used with Ephemeral znodes"); + } + if (hasT && hasC) { + throw new MalformedCommandException("TTLs cannot be used with Container znodes"); + } + + CreateMode flags; if(hasE && hasS) { flags = CreateMode.EPHEMERAL_SEQUENTIAL; } else if (hasE) { flags = CreateMode.EPHEMERAL; } else if (hasS) { - flags = CreateMode.PERSISTENT_SEQUENTIAL; + flags = hasT ? CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL : CreateMode.PERSISTENT_SEQUENTIAL; } else if (hasC) { flags = CreateMode.CONTAINER; + } else { + flags = hasT ? CreateMode.PERSISTENT_WITH_TTL : CreateMode.PERSISTENT; + } + if (hasT) { + try { + EphemeralType.ttlToEphemeralOwner(ttl); + } catch (IllegalArgumentException e) { + throw new MalformedCommandException(e.getMessage()); + } } + String path = args[1]; byte[] data = null; if (args.length > 2) { @@ -89,7 +116,7 @@ public class CreateCommand extends CliCommand { acl = AclParser.parse(args[3]); } try { - String newPath = zk.create(path, data, acl, flags); + String newPath = hasT ? zk.create(path, data, acl, flags, new Stat(), ttl) : zk.create(path, data, acl, flags); err.println("Created " + newPath); } catch(KeeperException.EphemeralOnLocalSessionException e) { err.println("Unable to create ephemeral node on a local session");
