You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@zookeeper.apache.org by GitBox <gi...@apache.org> on 2022/01/03 16:41:00 UTC

[GitHub] [zookeeper] sonatype-lift[bot] commented on a change in pull request #1791: ZOOKEEPER-4434 : Backport ZOOKEEPER-3142 for branch-3.5

sonatype-lift[bot] commented on a change in pull request #1791:
URL: https://github.com/apache/zookeeper/pull/1791#discussion_r777584987



##########
File path: zookeeper-server/src/main/java/org/apache/zookeeper/server/SnapshotFormatter.java
##########
@@ -34,79 +36,139 @@
 import org.apache.yetus.audience.InterfaceAudience;
 import org.apache.zookeeper.data.StatPersisted;
 import org.apache.zookeeper.server.persistence.FileSnap;
+import org.apache.zookeeper.server.persistence.Util;
+import org.json.simple.JSONValue;
+
+import static org.apache.zookeeper.server.persistence.FileSnap.SNAPSHOT_FILE_PREFIX;
 
 /**
  * Dump a snapshot file to stdout.
+ *
+ * For JSON format, followed https://dev.yorhel.nl/ncdu/jsonfmt
  */
 @InterfaceAudience.Public
 public class SnapshotFormatter {
 
+    // per-znode counter so ncdu treats each as a unique object
+    private static Integer INODE_IDX = 1000;
+
     /**
      * USAGE: SnapshotFormatter snapshot_file
      */
     public static void main(String[] args) throws Exception {
-        if (args.length != 1) {
-            System.err.println("USAGE: SnapshotFormatter snapshot_file");
+        String snapshotFile = null;
+        boolean dumpData = false;
+        boolean dumpJson = false;
+
+        int i;
+        for (i = 0; i < args.length; i++) {
+            if (args[i].equals("-d")) {
+                dumpData = true;
+            } else if (args[i].equals("-json")) {
+                dumpJson = true;
+            } else {
+                snapshotFile = args[i];
+                i++;
+                break;
+            }
+        }
+        if (args.length != i || snapshotFile == null) {
+            System.err.println("USAGE: SnapshotFormatter [-d|-json] snapshot_file");
+            System.err.println("       -d dump the data for each znode");
+            System.err.println("       -json dump znode info in json format");
             System.exit(2);
         }
 
-        new SnapshotFormatter().run(args[0]);
+        if (dumpData && dumpJson) {
+            System.err.println("Cannot specify both data dump (-d) and json mode (-json) in same call");
+            System.exit(ExitCode.INVALID_INVOCATION.getValue());
+        }
+
+        new SnapshotFormatter().run(snapshotFile, dumpData, dumpJson);
     }
-    
-    public void run(String snapshotFileName) throws IOException {
-        InputStream is = new CheckedInputStream(
+
+    public void run(String snapshotFileName, boolean dumpData, boolean dumpJson)
+        throws IOException {
+        File snapshotFile = new File(snapshotFileName);
+        try (InputStream is = new CheckedInputStream(
                 new BufferedInputStream(new FileInputStream(snapshotFileName)),
-                new Adler32());
-        InputArchive ia = BinaryInputArchive.getArchive(is);
-        
-        FileSnap fileSnap = new FileSnap(null);
+                new Adler32())) {
+            InputArchive ia = BinaryInputArchive.getArchive(is);
+
+            FileSnap fileSnap = new FileSnap(null);
+
+            DataTree dataTree = new DataTree();
+            Map<Long, Integer> sessions = new HashMap<Long, Integer>();
 
-        DataTree dataTree = new DataTree();
-        Map<Long, Integer> sessions = new HashMap<Long, Integer>();
-        
-        fileSnap.deserialize(dataTree, sessions, ia);
+            fileSnap.deserialize(dataTree, sessions, ia);

Review comment:
       *RESOURCE_LEAK:*  resource of type `java.io.DataInputStream` acquired by call to `getArchive(...)` at line 96 is not released after line 103.
   (at-me [in a reply](https://help.sonatype.com/lift/talking-to-lift) with `help` or `ignore`)

##########
File path: zookeeper-server/src/main/java/org/apache/zookeeper/server/SnapshotFormatter.java
##########
@@ -34,79 +36,139 @@
 import org.apache.yetus.audience.InterfaceAudience;
 import org.apache.zookeeper.data.StatPersisted;
 import org.apache.zookeeper.server.persistence.FileSnap;
+import org.apache.zookeeper.server.persistence.Util;
+import org.json.simple.JSONValue;
+
+import static org.apache.zookeeper.server.persistence.FileSnap.SNAPSHOT_FILE_PREFIX;
 
 /**
  * Dump a snapshot file to stdout.
+ *
+ * For JSON format, followed https://dev.yorhel.nl/ncdu/jsonfmt
  */
 @InterfaceAudience.Public
 public class SnapshotFormatter {
 
+    // per-znode counter so ncdu treats each as a unique object
+    private static Integer INODE_IDX = 1000;
+
     /**
      * USAGE: SnapshotFormatter snapshot_file
      */
     public static void main(String[] args) throws Exception {
-        if (args.length != 1) {
-            System.err.println("USAGE: SnapshotFormatter snapshot_file");
+        String snapshotFile = null;
+        boolean dumpData = false;
+        boolean dumpJson = false;
+
+        int i;
+        for (i = 0; i < args.length; i++) {
+            if (args[i].equals("-d")) {
+                dumpData = true;
+            } else if (args[i].equals("-json")) {
+                dumpJson = true;
+            } else {
+                snapshotFile = args[i];
+                i++;
+                break;
+            }
+        }
+        if (args.length != i || snapshotFile == null) {
+            System.err.println("USAGE: SnapshotFormatter [-d|-json] snapshot_file");
+            System.err.println("       -d dump the data for each znode");
+            System.err.println("       -json dump znode info in json format");
             System.exit(2);
         }
 
-        new SnapshotFormatter().run(args[0]);
+        if (dumpData && dumpJson) {
+            System.err.println("Cannot specify both data dump (-d) and json mode (-json) in same call");
+            System.exit(ExitCode.INVALID_INVOCATION.getValue());
+        }
+
+        new SnapshotFormatter().run(snapshotFile, dumpData, dumpJson);
     }
-    
-    public void run(String snapshotFileName) throws IOException {
-        InputStream is = new CheckedInputStream(
+
+    public void run(String snapshotFileName, boolean dumpData, boolean dumpJson)
+        throws IOException {
+        File snapshotFile = new File(snapshotFileName);
+        try (InputStream is = new CheckedInputStream(
                 new BufferedInputStream(new FileInputStream(snapshotFileName)),
-                new Adler32());
-        InputArchive ia = BinaryInputArchive.getArchive(is);
-        
-        FileSnap fileSnap = new FileSnap(null);
+                new Adler32())) {
+            InputArchive ia = BinaryInputArchive.getArchive(is);
+
+            FileSnap fileSnap = new FileSnap(null);
+
+            DataTree dataTree = new DataTree();
+            Map<Long, Integer> sessions = new HashMap<Long, Integer>();
 
-        DataTree dataTree = new DataTree();
-        Map<Long, Integer> sessions = new HashMap<Long, Integer>();
-        
-        fileSnap.deserialize(dataTree, sessions, ia);
+            fileSnap.deserialize(dataTree, sessions, ia);
+            long fileNameZxid = Util.getZxidFromName(snapshotFile.getName(), SNAPSHOT_FILE_PREFIX);
 
-        printDetails(dataTree, sessions);
+            if (dumpJson) {
+                printSnapshotJson(dataTree);
+            } else {
+                printDetails(dataTree, sessions, dumpData, fileNameZxid);
+            }
+        }

Review comment:
       *RESOURCE_LEAK:*  resource of type `java.util.zip.CheckedInputStream` acquired by call to `new()` at line 93 is not released after line 111.
   **Note**: potential exception at line 103
   (at-me [in a reply](https://help.sonatype.com/lift/talking-to-lift) with `help` or `ignore`)




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@zookeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org