You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by ca...@apache.org on 2012/03/18 21:53:40 UTC

svn commit: r1302222 - in /zookeeper/branches/branch-3.4: CHANGES.txt build.xml src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java src/java/test/org/apache/zookeeper/test/InvalidSnapshotTest.java

Author: camille
Date: Sun Mar 18 20:53:40 2012
New Revision: 1302222

URL: http://svn.apache.org/viewvc?rev=1302222&view=rev
Log:
ZOOKEEPER-1377. add support for dumping a snapshot file content (similar to LogFormatter) (phunt via camille)

Added:
    zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java   (with props)
Modified:
    zookeeper/branches/branch-3.4/CHANGES.txt
    zookeeper/branches/branch-3.4/build.xml
    zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/InvalidSnapshotTest.java

Modified: zookeeper/branches/branch-3.4/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/CHANGES.txt?rev=1302222&r1=1302221&r2=1302222&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/CHANGES.txt (original)
+++ zookeeper/branches/branch-3.4/CHANGES.txt Sun Mar 18 20:53:40 2012
@@ -37,6 +37,8 @@ IMPROVEMENTS:
   in order to get rid of the intermediate shell process
   (Roman Shaposhnik via phunt)
 
+  ZOOKEEPER-1377. add support for dumping a snapshot file content (similar to LogFormatter). (phunt via camille)
+
 
 Release 3.4.3 - 2012-02-06
 

Modified: zookeeper/branches/branch-3.4/build.xml
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/build.xml?rev=1302222&r1=1302221&r2=1302222&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/build.xml (original)
+++ zookeeper/branches/branch-3.4/build.xml Sun Mar 18 20:53:40 2012
@@ -469,6 +469,7 @@
           <include name="org/apache/zookeeper/ZooDefs.java"/>
           <include name="org/apache/zookeeper/ZooKeeper.java"/>
           <include name="org/apache/zookeeper/server/LogFormatter.java"/>
+          <include name="org/apache/zookeeper/server/SnapshotFormatter.java"/>
           <include name="org/apache/zookeeper/server/PurgeTxnLog.java"/>
           <exclude name="org/apache/zookeeper/server/quorum/QuorumPacket"/>
     	</fileset>

Added: zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java?rev=1302222&view=auto
==============================================================================
--- zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java (added)
+++ zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java Sun Mar 18 20:53:40 2012
@@ -0,0 +1,124 @@
+/**
+ * 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.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.zip.Adler32;
+import java.util.zip.CheckedInputStream;
+
+import org.apache.jute.BinaryInputArchive;
+import org.apache.jute.InputArchive;
+import org.apache.zookeeper.data.StatPersisted;
+import org.apache.zookeeper.server.persistence.FileSnap;
+
+/**
+ * Dump a snapshot file to stdout.
+ */
+public class SnapshotFormatter {
+
+    /**
+     * USAGE: SnapshotFormatter snapshot_file
+     */
+    public static void main(String[] args) throws Exception {
+        if (args.length != 1) {
+            System.err.println("USAGE: SnapshotFormatter snapshot_file");
+            System.exit(2);
+        }
+
+        new SnapshotFormatter().run(args[0]);
+    }
+    
+    public void run(String snapshotFileName) throws IOException {
+        InputStream is = new CheckedInputStream(
+                new BufferedInputStream(new FileInputStream(snapshotFileName)),
+                new Adler32());
+        InputArchive ia = BinaryInputArchive.getArchive(is);
+        
+        FileSnap fileSnap = new FileSnap(null);
+
+        DataTree dataTree = new DataTree();
+        Map<Long, Integer> sessions = new HashMap<Long, Integer>();
+        
+        fileSnap.deserialize(dataTree, sessions, ia);
+
+        printDetails(dataTree, sessions);
+    }
+
+    private void printDetails(DataTree dataTree, Map<Long, Integer> sessions) {
+        printZnodeDetails(dataTree);
+        printSessionDetails(dataTree, sessions);
+    }
+
+    private void printZnodeDetails(DataTree dataTree) {
+        System.out.println(String.format("ZNode Details (count=%d):",
+                dataTree.getNodeCount()));
+        
+        printZnode(dataTree, "/");
+        System.out.println("----");
+    }
+
+    private void printZnode(DataTree dataTree, String name) {
+        System.out.println("----");
+        DataNode n = dataTree.getNode(name);
+        Set<String> children;
+        synchronized(n) { // keep findbugs happy
+            System.out.println(name);
+            printStat(n.stat);
+            System.out.println("  dataLength = " + n.data.length);
+            children = n.getChildren();
+        }
+        if (children != null) {
+            for (String child : children) {
+                printZnode(dataTree, name + (name.equals("/") ? "" : "/") + child);
+            }
+        }
+    }
+
+    private void printSessionDetails(DataTree dataTree, Map<Long, Integer> sessions) {
+        System.out.println("Session Details (sid, timeout, ephemeralCount):");
+        for (Map.Entry<Long, Integer> e : sessions.entrySet()) {
+            long sid = e.getKey();
+            System.out.println(String.format("%#016x, %d, %d",
+                    sid, e.getValue(), dataTree.getEphemerals(sid).size()));
+        }
+    }
+
+    private void printStat(StatPersisted stat) {
+        printHex("cZxid", stat.getCzxid());
+        System.out.println("  ctime = " + new Date(stat.getCtime()).toString());
+        printHex("mZxid", stat.getMzxid());
+        System.out.println("  mtime = " + new Date(stat.getMtime()).toString());
+        printHex("pZxid", stat.getPzxid());
+        System.out.println("  cversion = " + stat.getCversion());
+        System.out.println("  dataVersion = " + stat.getVersion());
+        System.out.println("  aclVersion = " + stat.getAversion());
+        printHex("ephemeralOwner", stat.getEphemeralOwner());
+    }
+
+    private void printHex(String prefix, long value) {
+        System.out.println(String.format("  %s = %#016x", prefix, value));
+    }
+}

Propchange: zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/InvalidSnapshotTest.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/InvalidSnapshotTest.java?rev=1302222&r1=1302221&r2=1302222&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/InvalidSnapshotTest.java (original)
+++ zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/InvalidSnapshotTest.java Sun Mar 18 20:53:40 2012
@@ -33,6 +33,7 @@ import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.Watcher.Event.KeeperState;
 import org.apache.zookeeper.server.LogFormatter;
 import org.apache.zookeeper.server.ServerCnxnFactory;
+import org.apache.zookeeper.server.SnapshotFormatter;
 import org.apache.zookeeper.server.SyncRequestProcessor;
 import org.apache.zookeeper.server.ZooKeeperServer;
 import org.junit.Assert;
@@ -58,6 +59,18 @@ public class InvalidSnapshotTest extends
         LogFormatter.main(args);
     }
     
+
+    /**
+     * Verify the SnapshotFormatter by running it on a known file.
+     */
+    @Test
+    public void testSnapshotFormatter() throws Exception {
+        File snapDir = new File(testData, "invalidsnap");
+        File snapfile = new File(new File(snapDir, "version-2"), "snapshot.272");
+        String[] args = {snapfile.getCanonicalFile().toString()};
+        SnapshotFormatter.main(args);
+    }
+    
     /**
      * test the snapshot
      * @throws Exception an exception could be expected