This is an automated email from the ASF dual-hosted git repository. mwalch pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/accumulo.git
The following commit(s) were added to refs/heads/master by this push: new 22b0bf5 Updates to DumpZookeeper utility (#857) 22b0bf5 is described below commit 22b0bf59bc8186f7ca29f769f459c3ecd8c46d78 Author: Mike Walch <mwa...@apache.org> AuthorDate: Thu Dec 27 13:40:44 2018 -0500 Updates to DumpZookeeper utility (#857) * Created human readable option which is now default * Xml can still be output by passing --xml argument * Utility can now be called using 'accumulo-util dump-zoo' --- assemble/bin/accumulo-util | 4 ++ .../apache/accumulo/server/util/DumpZookeeper.java | 73 +++++++++++++++++----- 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/assemble/bin/accumulo-util b/assemble/bin/accumulo-util index 4a8b9ba..b5827da 100755 --- a/assemble/bin/accumulo-util +++ b/assemble/bin/accumulo-util @@ -21,6 +21,7 @@ Usage: accumulo-util <command> (<argument> ...) Commands: build-native Builds Accumulo native libraries + dump-zoo Dumps data in ZooKeeper hadoop-jar Runs 'hadoop jar' command with Accumulo jars gen-monitor-cert Generates Accumulo monitor certficate load-jars-hdfs Loads Accumulo jars in lib/ to HDFS for VFS classloader @@ -263,6 +264,9 @@ function main() { build-native) build_native "${@:2}" ;; + dump-zoo) + "$bin"/accumulo org.apache.accumulo.server.util.DumpZookeeper "${@:2}" + ;; hadoop-jar) hadoop_jar "${@:2}" ;; diff --git a/server/base/src/main/java/org/apache/accumulo/server/util/DumpZookeeper.java b/server/base/src/main/java/org/apache/accumulo/server/util/DumpZookeeper.java index 24329ff..7fe7a8c 100644 --- a/server/base/src/main/java/org/apache/accumulo/server/util/DumpZookeeper.java +++ b/server/base/src/main/java/org/apache/accumulo/server/util/DumpZookeeper.java @@ -21,8 +21,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import java.io.PrintStream; import java.util.Base64; -import org.apache.accumulo.core.cli.Help; -import org.apache.accumulo.core.conf.SiteConfiguration; +import org.apache.accumulo.core.cli.ConfigOpts; import org.apache.accumulo.fate.zookeeper.IZooReaderWriter; import org.apache.accumulo.fate.zookeeper.ZooReaderWriter; import org.apache.log4j.Level; @@ -34,7 +33,7 @@ import com.beust.jcommander.Parameter; public class DumpZookeeper { - static IZooReaderWriter zk = null; + private static IZooReaderWriter zk = null; private static final Logger log = Logger.getLogger(DumpZookeeper.class); @@ -48,9 +47,13 @@ public class DumpZookeeper { } } - static class Opts extends Help { - @Parameter(names = "--root", description = "the root of the znode tree to dump") + static class Opts extends ConfigOpts { + @Parameter(names = {"-r", "-root", "--root"}, + description = "Root ZooKeeper directory to start dump at") String root = "/"; + @Parameter(names = {"-x", "-xml", "--xml"}, + description = "Output dump as XML (instead of human readable") + boolean xml = false; } public static void main(String[] args) { @@ -60,19 +63,27 @@ public class DumpZookeeper { Logger.getRootLogger().setLevel(Level.WARN); PrintStream out = System.out; try { - zk = new ZooReaderWriter(new SiteConfiguration()); - - write(out, 0, "<dump root='%s'>", opts.root); - for (String child : zk.getChildren(opts.root, null)) - if (!child.equals("zookeeper")) - dump(out, opts.root, child, 1); - write(out, 0, "</dump>"); + zk = new ZooReaderWriter(opts.getSiteConfiguration()); + if (opts.xml) { + writeXml(out, opts.root); + } else { + writeHumanReadable(out, opts.root); + } } catch (Exception ex) { log.error(ex, ex); } } - private static void dump(PrintStream out, String root, String child, int indent) + private static void writeXml(PrintStream out, String root) + throws KeeperException, InterruptedException { + write(out, 0, "<dump root='%s'>", root); + for (String child : zk.getChildren(root, null)) + if (!child.equals("zookeeper")) + childXml(out, root, child, 1); + write(out, 0, "</dump>"); + } + + private static void childXml(PrintStream out, String root, String child, int indent) throws KeeperException, InterruptedException { String path = root + "/" + child; if (root.endsWith("/")) @@ -101,7 +112,7 @@ public class DumpZookeeper { value.value); } for (String c : zk.getChildren(path, null)) { - dump(out, path, c, indent + 1); + childXml(out, path, c, indent + 1); } write(out, indent, "</node>"); } @@ -119,7 +130,39 @@ public class DumpZookeeper { private static void write(PrintStream out, int indent, String fmt, Object... args) { for (int i = 0; i < indent; i++) - out.print(" "); + out.print(" "); out.println(String.format(fmt, args)); } + + private static void writeHumanReadable(PrintStream out, String root) + throws KeeperException, InterruptedException { + write(out, 0, "%s:", root); + for (String child : zk.getChildren(root, null)) + if (!child.equals("zookeeper")) + childHumanReadable(out, root, child, 1); + } + + private static void childHumanReadable(PrintStream out, String root, String child, int indent) + throws KeeperException, InterruptedException { + String path = root + "/" + child; + if (root.endsWith("/")) + path = root + child; + Stat stat = zk.getStatus(path); + if (stat == null) + return; + String node = child; + if (stat.getEphemeralOwner() != 0) { + node = "*" + child + "*"; + } + if (stat.getDataLength() == 0) { + write(out, indent, "%s:", node); + } else { + write(out, indent, "%s: %s", node, value(path).value); + } + if (stat.getNumChildren() > 0) { + for (String c : zk.getChildren(path, null)) { + childHumanReadable(out, path, c, indent + 1); + } + } + } }