zookeeper-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [zookeeper] maoling commented on a change in pull request #984: ZOOKEEPER-3427: Introduce SnapshotComparer that assists debugging with snapshots.
Date Thu, 13 Jun 2019 11:17:47 GMT
maoling commented on a change in pull request #984: ZOOKEEPER-3427: Introduce SnapshotComparer
that assists debugging with snapshots.
URL: https://github.com/apache/zookeeper/pull/984#discussion_r293326650
 
 

 ##########
 File path: zookeeper-server/src/main/java/org/apache/zookeeper/server/SnapshotComparer.java
 ##########
 @@ -0,0 +1,455 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Scanner;
+import java.util.zip.Adler32;
+import java.util.zip.CheckedInputStream;
+
+import org.apache.commons.cli.BasicParser;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.commons.cli.ParseException;
+import org.apache.jute.BinaryInputArchive;
+import org.apache.jute.InputArchive;
+import org.apache.zookeeper.server.persistence.FileSnap;
+
+/**
+ * Compare two data tree snapshots, output information about the delta.
+ * Only outputs information about permanent nodes, ignoring both sessions
+ * and ephemeral nodes.
+ */
+public class SnapshotComparer {
+  private final Options options;
+  private static final String leftOption = "left";
+  private static final String rightOption = "right";
+  private static final String byteThresholdOption = "bytes";
+  private static final String nodeThresholdOption = "nodes";
+  private static final String debugOption = "debug";
+  private static final String interactiveOption = "interactive";
+
+  @SuppressWarnings("static")
+  private SnapshotComparer() {
+    options = new Options();
+    options.addOption(
+        OptionBuilder
+            .hasArg()
+            .isRequired(true)
+            .withLongOpt(leftOption)
+            .withDescription("the left snapshot file")
+            .withArgName("LEFT")
+            .withType(File.class)
+            .create("l"));
+    options.addOption(
+        OptionBuilder
+            .hasArg()
+            .isRequired(true)
+            .withLongOpt(rightOption)
+            .withDescription("the right snapshot file")
+            .withArgName("RIGHT")
+            .withType(File.class)
+            .create("r"));
+    options.addOption(
+        OptionBuilder
+            .hasArg()
+            .isRequired(true)
+            .withLongOpt(byteThresholdOption)
+            .withDescription("the node data delta size threshold, in bytes, for printing
the node")
+            .withArgName("BYTETHRESHOLD")
+            .withType(File.class)
+            .create("b"));
+    options.addOption(
+        OptionBuilder
+            .hasArg()
+            .isRequired(true)
+            .withLongOpt(nodeThresholdOption)
+            .withDescription("the descendant node delta size threshold, in nodes, for printing
the node")
+            .withArgName("NODETHRESHOLD")
+            .withType(String.class)
+            .create("n"));
+    options.addOption(
+        OptionBuilder
+            .hasArg()
+            .withLongOpt(debugOption)
+            .withDescription("use debug output")
+            .withArgName("DEBUG")
+            .withType(String.class)
+            .create("d"));
+    options.addOption(
+        OptionBuilder
+            .hasArg()
+            .withLongOpt(interactiveOption)
+            .withDescription("interactive mode")
+            .withArgName("INTERACTIVE")
+            .withType(String.class)
+            .create("i"));
+  }
+
+  private void usage() {
+    HelpFormatter help = new HelpFormatter();
+
+    help.printHelp(
+        120,
+        "java -cp <classPath> " + SnapshotFormatter.class.getName(),
+        "",
+        options,
+        "");
+  }
+
+  public static void main(String[] args) throws Exception {
+    SnapshotComparer app = new SnapshotComparer();
+    app.compareSnapshots(args);
+  }
+
+  private void compareSnapshots(String[] args) throws Exception {
+    CommandLine parsedOptions;
+    try {
+      parsedOptions = new BasicParser().parse(options, args);
+    } catch (ParseException e) {
+      usage();
+      return;
+    }
+    File left = (File)parsedOptions.getParsedOptionValue(leftOption);
+    File right = (File)parsedOptions.getParsedOptionValue(rightOption);
+    int byteThreshold = Integer.parseInt((String)parsedOptions.getParsedOptionValue(byteThresholdOption));
+    int nodeThreshold = Integer.parseInt((String)parsedOptions.getParsedOptionValue(nodeThresholdOption));
+    boolean debug = parsedOptions.hasOption(debugOption);
+    boolean interactive = parsedOptions.hasOption(interactiveOption);
+    System.out.println("Successfully parsed options!");
+    TreeInfo leftTree = new TreeInfo(left);
+    TreeInfo rightTree = new TreeInfo(right);
+
+    System.out.println(leftTree.toString());
+    System.out.println(rightTree.toString());
+
+    compareTrees(leftTree, rightTree, byteThreshold, nodeThreshold, debug, interactive);
+  }
+
+  private static class TreeInfo {
+    public static class TreeNode {
+      final String label;
+      final long size;
+      final List<TreeNode> children;
+      long descendantSize;
+      long descendantCount;
+
+      private TreeNode() {
+        label = null;
+        size = -1;
+        children = null;
+        descendantSize = -1;
+        descendantCount = -1;
+      }
+
+      public static class AlphabeticComparator implements Comparator<TreeNode> {
+        public int compare(TreeNode left, TreeNode right) {
+          if (left == right) {
+            return 0;
+          }
+          if (left == null) {
+            return -1;
+          }
+          if (right == null) {
+            return 1;
+          }
+          return left.label.compareTo(right.label);
+        }
+      }
+
+      public TreeNode(String label, long size) {
+        this.label = label;
+        this.size = size;
+        this.children = new ArrayList<TreeNode>();
+      }
+
+      void AddChild(TreeNode child) {
+        children.add(child);
+      }
+
+      void populateChildren(String path, DataTree dataTree, TreeInfo treeInfo) throws Exception
{
+        populateChildren(path, dataTree, treeInfo, 1);
+      }
+
+      void populateChildren(String path, DataTree dataTree, TreeInfo treeInfo, int currentDepth)
throws Exception {
+        List<String> childLabels = null;
+        childLabels = dataTree.getChildren(path, null, null);
+
+        if (childLabels != null && !childLabels.isEmpty()) {
+          for (String childName : childLabels){
+            String childPath = path + "/" + childName;
+            DataNode childNode = dataTree.getNode(childPath);
+            long size = childNode.data == null ? 0 : childNode.data.length;
+            TreeNode childTreeNode = new TreeNode(childPath, size);
 
 Review comment:
   quick comment: 
   Finbugs complians about this and the  `ICAST_IDIV_CAST_TO_DOUBLE`, `VA_FORMAT_STRING_USES_NEWLINE`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message