Return-Path: X-Original-To: apmail-zookeeper-commits-archive@www.apache.org Delivered-To: apmail-zookeeper-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 953EC98CB for ; Sun, 18 Mar 2012 20:54:06 +0000 (UTC) Received: (qmail 33538 invoked by uid 500); 18 Mar 2012 20:54:06 -0000 Delivered-To: apmail-zookeeper-commits-archive@zookeeper.apache.org Received: (qmail 33513 invoked by uid 500); 18 Mar 2012 20:54:06 -0000 Mailing-List: contact commits-help@zookeeper.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ Delivered-To: mailing list commits@zookeeper.apache.org Received: (qmail 33498 invoked by uid 99); 18 Mar 2012 20:54:06 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 18 Mar 2012 20:54:06 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 18 Mar 2012 20:54:02 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 385F72388860 for ; Sun, 18 Mar 2012 20:53:41 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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 Date: Sun, 18 Mar 2012 20:53:40 -0000 To: commits@zookeeper.apache.org From: camille@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120318205341.385F72388860@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 @@ + 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 sessions = new HashMap(); + + fileSnap.deserialize(dataTree, sessions, ia); + + printDetails(dataTree, sessions); + } + + private void printDetails(DataTree dataTree, Map 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 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 sessions) { + System.out.println("Session Details (sid, timeout, ephemeralCount):"); + for (Map.Entry 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