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:06:46 UTC
svn commit: r1302213 - in /zookeeper/trunk: 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:06:46 2012
New Revision: 1302213
URL: http://svn.apache.org/viewvc?rev=1302213&view=rev
Log:
ZOOKEEPER-1377. add support for dumping a snapshot file content (similar to LogFormatter) (phunt via camille)
Added:
zookeeper/trunk/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java (with props)
Modified:
zookeeper/trunk/CHANGES.txt
zookeeper/trunk/build.xml
zookeeper/trunk/src/java/test/org/apache/zookeeper/test/InvalidSnapshotTest.java
Modified: zookeeper/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/trunk/CHANGES.txt?rev=1302213&r1=1302212&r2=1302213&view=diff
==============================================================================
--- zookeeper/trunk/CHANGES.txt (original)
+++ zookeeper/trunk/CHANGES.txt Sun Mar 18 20:06:46 2012
@@ -256,6 +256,8 @@ IMPROVEMENTS:
data directory (phunt via henry)
ZOOKEEPER-1397. Remove BookKeeper documentation links. (flavio via camille)
+
+ ZOOKEEPER-1377. add support for dumping a snapshot file content (similar to LogFormatter). (phunt via camille)
ZOOKEEPER-271. Better command line parsing in ZookeeperMain.
(Hartmut Lang via phunt)
Modified: zookeeper/trunk/build.xml
URL: http://svn.apache.org/viewvc/zookeeper/trunk/build.xml?rev=1302213&r1=1302212&r2=1302213&view=diff
==============================================================================
--- zookeeper/trunk/build.xml (original)
+++ zookeeper/trunk/build.xml Sun Mar 18 20:06:46 2012
@@ -488,6 +488,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/trunk/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java?rev=1302213&view=auto
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java (added)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java Sun Mar 18 20:06:46 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/trunk/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: zookeeper/trunk/src/java/test/org/apache/zookeeper/test/InvalidSnapshotTest.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/test/org/apache/zookeeper/test/InvalidSnapshotTest.java?rev=1302213&r1=1302212&r2=1302213&view=diff
==============================================================================
--- zookeeper/trunk/src/java/test/org/apache/zookeeper/test/InvalidSnapshotTest.java (original)
+++ zookeeper/trunk/src/java/test/org/apache/zookeeper/test/InvalidSnapshotTest.java Sun Mar 18 20:06:46 2012
@@ -23,20 +23,21 @@ import static org.apache.zookeeper.test.
import java.io.File;
import java.util.concurrent.CountDownLatch;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.apache.zookeeper.PortAssignment;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZKTestCase;
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;
import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class InvalidSnapshotTest extends ZKTestCase implements Watcher {
private final static Logger LOG = LoggerFactory.getLogger(InvalidSnapshotTest.class);
@@ -59,6 +60,17 @@ public class InvalidSnapshotTest extends
}
/**
+ * 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
*/