Modified: zookeeper/trunk/src/contrib/zooinspector/src/java/org/apache/zookeeper/retry/ZooKeeperRetry.java URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/contrib/zooinspector/src/java/org/apache/zookeeper/retry/ZooKeeperRetry.java?rev=1584497&r1=1584496&r2=1584497&view=diff ============================================================================== --- zookeeper/trunk/src/contrib/zooinspector/src/java/org/apache/zookeeper/retry/ZooKeeperRetry.java (original) +++ zookeeper/trunk/src/contrib/zooinspector/src/java/org/apache/zookeeper/retry/ZooKeeperRetry.java Fri Apr 4 01:24:37 2014 @@ -1,288 +1,288 @@ -/** - * 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.retry; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.Watcher; -import org.apache.zookeeper.ZooKeeper; -import org.apache.zookeeper.data.ACL; -import org.apache.zookeeper.data.Stat; -import org.apache.zookeeper.inspector.logger.LoggerFactory; - -/** - * A Class which extends {@link ZooKeeper} and will automatically retry calls to - * zookeeper if a {@link KeeperException.ConnectionLossException} occurs - */ -public class ZooKeeperRetry extends ZooKeeper { - - private boolean closed = false; - private final Watcher watcher; - private int limit = -1; - - /** - * @param connectString - * @param sessionTimeout - * @param watcher - * @throws IOException - */ - public ZooKeeperRetry(String connectString, int sessionTimeout, - Watcher watcher) throws IOException { - super(connectString, sessionTimeout, watcher); - this.watcher = watcher; - } - - /** - * @param connectString - * @param sessionTimeout - * @param watcher - * @param sessionId - * @param sessionPasswd - * @throws IOException - */ - public ZooKeeperRetry(String connectString, int sessionTimeout, - Watcher watcher, long sessionId, byte[] sessionPasswd) - throws IOException { - super(connectString, sessionTimeout, watcher, sessionId, sessionPasswd); - this.watcher = watcher; - } - - @Override - public synchronized void close() throws InterruptedException { - this.closed = true; - super.close(); - } - - @Override - public String create(String path, byte[] data, List<ACL> acl, - CreateMode createMode) throws KeeperException, InterruptedException { - int count = 0; - do { - try { - return super.create(path, data, acl, createMode); - } catch (KeeperException.ConnectionLossException e) { - LoggerFactory.getLogger().warn( - "ZooKeeper connection lost. Trying to reconnect."); - if (exists(path, false) != null) { - return path; - } - } catch (KeeperException.NodeExistsException e) { - return path; - } - } while (!closed && (limit == -1 || count++ < limit)); - return null; - } - - @Override - public void delete(String path, int version) throws InterruptedException, - KeeperException { - int count = 0; - do { - try { - super.delete(path, version); - } catch (KeeperException.ConnectionLossException e) { - LoggerFactory.getLogger().warn( - "ZooKeeper connection lost. Trying to reconnect."); - if (exists(path, false) == null) { - return; - } - } catch (KeeperException.NoNodeException e) { - break; - } - } while (!closed && (limit == -1 || count++ < limit)); - } - - @Override - public Stat exists(String path, boolean watch) throws KeeperException, - InterruptedException { - int count = 0; - do { - try { - return super.exists(path, watch ? watcher : null); - } catch (KeeperException.ConnectionLossException e) { - LoggerFactory.getLogger().warn( - "ZooKeeper connection lost. Trying to reconnect."); - } - } while (!closed && (limit == -1 || count++ < limit)); - return null; - } - - @Override - public Stat exists(String path, Watcher watcher) throws KeeperException, - InterruptedException { - int count = 0; - do { - try { - return super.exists(path, watcher); - } catch (KeeperException.ConnectionLossException e) { - LoggerFactory.getLogger().warn( - "ZooKeeper connection lost. Trying to reconnect."); - } - } while (!closed && (limit == -1 || count++ < limit)); - return null; - } - - @Override - public List<ACL> getACL(String path, Stat stat) throws KeeperException, - InterruptedException { - int count = 0; - do { - try { - return super.getACL(path, stat); - } catch (KeeperException.ConnectionLossException e) { - LoggerFactory.getLogger().warn( - "ZooKeeper connection lost. Trying to reconnect."); - } - } while (!closed && (limit == -1 || count++ < limit)); - return null; - } - - @Override - public List<String> getChildren(String path, boolean watch) - throws KeeperException, InterruptedException { - int count = 0; - do { - try { - return super.getChildren(path, watch ? watcher : null); - } catch (KeeperException.ConnectionLossException e) { - LoggerFactory.getLogger().warn( - "ZooKeeper connection lost. Trying to reconnect."); - } - } while (!closed && (limit == -1 || count++ < limit)); - return new ArrayList<String>(); - } - - @Override - public List<String> getChildren(String path, Watcher watcher) - throws KeeperException, InterruptedException { - int count = 0; - do { - try { - return super.getChildren(path, watcher); - } catch (KeeperException.ConnectionLossException e) { - LoggerFactory.getLogger().warn( - "ZooKeeper connection lost. Trying to reconnect."); - } - } while (!closed && (limit == -1 || count++ < limit)); - return new ArrayList<String>(); - } - - @Override - public byte[] getData(String path, boolean watch, Stat stat) - throws KeeperException, InterruptedException { - int count = 0; - do { - try { - return super.getData(path, watch ? watcher : null, stat); - } catch (KeeperException.ConnectionLossException e) { - LoggerFactory.getLogger().warn( - "ZooKeeper connection lost. Trying to reconnect."); - } - } while (!closed && (limit == -1 || count++ < limit)); - return null; - } - - @Override - public byte[] getData(String path, Watcher watcher, Stat stat) - throws KeeperException, InterruptedException { - int count = 0; - do { - try { - return super.getData(path, watcher, stat); - } catch (KeeperException.ConnectionLossException e) { - LoggerFactory.getLogger().warn( - "ZooKeeper connection lost. Trying to reconnect."); - } - } while (!closed && (limit == -1 || count++ < limit)); - return null; - } - - @Override - public Stat setACL(String path, List<ACL> acl, int version) - throws KeeperException, InterruptedException { - int count = 0; - do { - try { - return super.setACL(path, acl, version); - } catch (KeeperException.ConnectionLossException e) { - LoggerFactory.getLogger().warn( - "ZooKeeper connection lost. Trying to reconnect."); - Stat s = exists(path, false); - if (s != null) { - if (getACL(path, s).equals(acl)) { - return s; - } - } else { - return null; - } - } - } while (!closed && (limit == -1 || count++ < limit)); - return null; - } - - @Override - public Stat setData(String path, byte[] data, int version) - throws KeeperException, InterruptedException { - int count = 0; - do { - try { - return super.setData(path, data, version); - } catch (KeeperException.ConnectionLossException e) { - LoggerFactory.getLogger().warn( - "ZooKeeper connection lost. Trying to reconnect."); - Stat s = exists(path, false); - if (s != null) { - if (getData(path, false, s) == data) { - return s; - } - } else { - return null; - } - } - } while (!closed && (limit == -1 || count++ < limit)); - return null; - } - - /** - * @param limit - */ - public void setRetryLimit(int limit) { - this.limit = limit; - } - - /** - * @return true if successfully connected to zookeeper - */ - public boolean testConnection() { - int count = 0; - do { - try { - return super.exists("/", null) != null; - } catch (Exception e) { - LoggerFactory.getLogger().warn( - "ZooKeeper connection lost. Trying to reconnect."); - } - } while (count++ < 5); - return false; - } - -} +/** + * 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.retry; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.inspector.logger.LoggerFactory; + +/** + * A Class which extends {@link ZooKeeper} and will automatically retry calls to + * zookeeper if a {@link KeeperException.ConnectionLossException} occurs + */ +public class ZooKeeperRetry extends ZooKeeper { + + private boolean closed = false; + private final Watcher watcher; + private int limit = -1; + + /** + * @param connectString + * @param sessionTimeout + * @param watcher + * @throws IOException + */ + public ZooKeeperRetry(String connectString, int sessionTimeout, + Watcher watcher) throws IOException { + super(connectString, sessionTimeout, watcher); + this.watcher = watcher; + } + + /** + * @param connectString + * @param sessionTimeout + * @param watcher + * @param sessionId + * @param sessionPasswd + * @throws IOException + */ + public ZooKeeperRetry(String connectString, int sessionTimeout, + Watcher watcher, long sessionId, byte[] sessionPasswd) + throws IOException { + super(connectString, sessionTimeout, watcher, sessionId, sessionPasswd); + this.watcher = watcher; + } + + @Override + public synchronized void close() throws InterruptedException { + this.closed = true; + super.close(); + } + + @Override + public String create(String path, byte[] data, List<ACL> acl, + CreateMode createMode) throws KeeperException, InterruptedException { + int count = 0; + do { + try { + return super.create(path, data, acl, createMode); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + if (exists(path, false) != null) { + return path; + } + } catch (KeeperException.NodeExistsException e) { + return path; + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + @Override + public void delete(String path, int version) throws InterruptedException, + KeeperException { + int count = 0; + do { + try { + super.delete(path, version); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + if (exists(path, false) == null) { + return; + } + } catch (KeeperException.NoNodeException e) { + break; + } + } while (!closed && (limit == -1 || count++ < limit)); + } + + @Override + public Stat exists(String path, boolean watch) throws KeeperException, + InterruptedException { + int count = 0; + do { + try { + return super.exists(path, watch ? watcher : null); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + @Override + public Stat exists(String path, Watcher watcher) throws KeeperException, + InterruptedException { + int count = 0; + do { + try { + return super.exists(path, watcher); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + @Override + public List<ACL> getACL(String path, Stat stat) throws KeeperException, + InterruptedException { + int count = 0; + do { + try { + return super.getACL(path, stat); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + @Override + public List<String> getChildren(String path, boolean watch) + throws KeeperException, InterruptedException { + int count = 0; + do { + try { + return super.getChildren(path, watch ? watcher : null); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (!closed && (limit == -1 || count++ < limit)); + return new ArrayList<String>(); + } + + @Override + public List<String> getChildren(String path, Watcher watcher) + throws KeeperException, InterruptedException { + int count = 0; + do { + try { + return super.getChildren(path, watcher); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (!closed && (limit == -1 || count++ < limit)); + return new ArrayList<String>(); + } + + @Override + public byte[] getData(String path, boolean watch, Stat stat) + throws KeeperException, InterruptedException { + int count = 0; + do { + try { + return super.getData(path, watch ? watcher : null, stat); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + @Override + public byte[] getData(String path, Watcher watcher, Stat stat) + throws KeeperException, InterruptedException { + int count = 0; + do { + try { + return super.getData(path, watcher, stat); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + @Override + public Stat setACL(String path, List<ACL> acl, int version) + throws KeeperException, InterruptedException { + int count = 0; + do { + try { + return super.setACL(path, acl, version); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + Stat s = exists(path, false); + if (s != null) { + if (getACL(path, s).equals(acl)) { + return s; + } + } else { + return null; + } + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + @Override + public Stat setData(String path, byte[] data, int version) + throws KeeperException, InterruptedException { + int count = 0; + do { + try { + return super.setData(path, data, version); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + Stat s = exists(path, false); + if (s != null) { + if (getData(path, false, s) == data) { + return s; + } + } else { + return null; + } + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + /** + * @param limit + */ + public void setRetryLimit(int limit) { + this.limit = limit; + } + + /** + * @return true if successfully connected to zookeeper + */ + public boolean testConnection() { + int count = 0; + do { + try { + return super.exists("/", null) != null; + } catch (Exception e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (count++ < 5); + return false; + } + +}
Propchange: zookeeper/trunk/src/contrib/zooinspector/src/java/org/apache/zookeeper/retry/ZooKeeperRetry.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: zookeeper/trunk/src/contrib/zooinspector/zooInspector.cmd URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/contrib/zooinspector/zooInspector.cmd?rev=1584497&r1=1584496&r2=1584497&view=diff ============================================================================== --- zookeeper/trunk/src/contrib/zooinspector/zooInspector.cmd (original) +++ zookeeper/trunk/src/contrib/zooinspector/zooInspector.cmd Fri Apr 4 01:24:37 2014 @@ -1,18 +1,18 @@ -#!/bin/sh - -# 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. - -java -cp "zookeeper-dev-ZooInspector.jar;lib\*;lib" org.apache.zookeeper.inspector.ZooInspector +#!/bin/sh + +# 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. + +java -cp "zookeeper-dev-ZooInspector.jar;lib\*;lib" org.apache.zookeeper.inspector.ZooInspector Propchange: zookeeper/trunk/src/contrib/zooinspector/zooInspector.cmd ------------------------------------------------------------------------------ svn:eol-style = native Modified: 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=1584497&r1=1584496&r2=1584497&view=diff ============================================================================== --- zookeeper/trunk/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java (original) +++ zookeeper/trunk/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java Fri Apr 4 01:24:37 2014 @@ -1,128 +1,128 @@ -/** - * 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); - if (n.data != null) { - System.out.println(" dataLength = " + n.data.length); - } else { - System.out.println(" no data"); - } - 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)); - } -} +/** + * 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); + if (n.data != null) { + System.out.println(" dataLength = " + n.data.length); + } else { + System.out.println(" no data"); + } + 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:eol-style = native Modified: zookeeper/trunk/src/java/test/org/apache/zookeeper/server/PrepRequestProcessorTest.java URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/test/org/apache/zookeeper/server/PrepRequestProcessorTest.java?rev=1584497&r1=1584496&r2=1584497&view=diff ============================================================================== --- zookeeper/trunk/src/java/test/org/apache/zookeeper/server/PrepRequestProcessorTest.java (original) +++ zookeeper/trunk/src/java/test/org/apache/zookeeper/server/PrepRequestProcessorTest.java Fri Apr 4 01:24:37 2014 @@ -1,149 +1,149 @@ -/** - * 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 static org.junit.Assert.*; - -import java.io.File; -import java.io.PrintWriter; -import java.nio.ByteBuffer; -import java.util.concurrent.CountDownLatch; - -import org.apache.zookeeper.PortAssignment; -import org.apache.zookeeper.KeeperException.Code; -import org.apache.zookeeper.KeeperException.SessionExpiredException; -import org.apache.zookeeper.KeeperException.SessionMovedException; -import org.apache.zookeeper.ZooDefs.OpCode; -import org.apache.zookeeper.server.PrepRequestProcessor; -import org.apache.zookeeper.server.Request; -import org.apache.zookeeper.server.RequestProcessor; -import org.apache.zookeeper.server.ServerCnxnFactory; -import org.apache.zookeeper.server.SyncRequestProcessor; -import org.apache.zookeeper.server.ZooKeeperServer; -import org.apache.zookeeper.test.ClientBase; -import org.apache.zookeeper.txn.ErrorTxn; -import org.junit.Assert; -import org.junit.Test; - -public class PrepRequestProcessorTest extends ClientBase { - private static String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); - private static final int CONNECTION_TIMEOUT = 3000; - private final CountDownLatch testEnd = new CountDownLatch(1); - - @Test - public void testPRequest() throws Exception { - File tmpDir = ClientBase.createTmpDir(); - ClientBase.setupTestEnv(); - ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); - SyncRequestProcessor.setSnapCount(100); - final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); - ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); - f.startup(zks); - Assert.assertTrue("waiting for server being up ", - ClientBase.waitForServerUp(HOSTPORT,CONNECTION_TIMEOUT)); - zks.sessionTracker = new MySessionTracker(); - PrepRequestProcessor processor = new PrepRequestProcessor(zks, new MyRequestProcessor()); - Request foo = new Request(null, 1l, 1, OpCode.create, ByteBuffer.allocate(3), null); - processor.pRequest(foo); - testEnd.await(5, java.util.concurrent.TimeUnit.SECONDS); - f.shutdown(); - zks.shutdown(); - } - - - private class MyRequestProcessor implements RequestProcessor { - @Override - public void processRequest(Request request) { - Assert.assertEquals("Request should have marshalling error", new ErrorTxn(Code.MARSHALLINGERROR.intValue()), request.getTxn()); - testEnd.countDown(); - } - @Override - public void shutdown() { - // TODO Auto-generated method stub - - } - } - - private class MySessionTracker implements SessionTracker { - @Override - public boolean addGlobalSession(long id, int to) { - // TODO Auto-generated method stub - return false; - } - @Override - public boolean addSession(long id, int to) { - // TODO Auto-generated method stub - return false; - } - @Override - public void checkSession(long sessionId, Object owner) - throws SessionExpiredException, SessionMovedException { - // TODO Auto-generated method stub - } - @Override - public long createSession(int sessionTimeout) { - // TODO Auto-generated method stub - return 0; - } - @Override - public void dumpSessions(PrintWriter pwriter) { - // TODO Auto-generated method stub - - } - @Override - public void removeSession(long sessionId) { - // TODO Auto-generated method stub - - } - public int upgradeSession(long sessionId) { - // TODO Auto-generated method stub - return 0; - } - @Override - public void setOwner(long id, Object owner) - throws SessionExpiredException { - // TODO Auto-generated method stub - - } - @Override - public void shutdown() { - // TODO Auto-generated method stub - - } - @Override - public boolean touchSession(long sessionId, int sessionTimeout) { - // TODO Auto-generated method stub - return false; - } - @Override - public void setSessionClosing(long sessionId) { - // TODO Auto-generated method stub - } - @Override - public boolean isTrackingSession(long sessionId) { - // TODO Auto-generated method stub - return false; - } - @Override - public void checkGlobalSession(long sessionId, Object owner) - throws SessionExpiredException, SessionMovedException { - // TODO Auto-generated method stub - } - } -} +/** + * 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 static org.junit.Assert.*; + +import java.io.File; +import java.io.PrintWriter; +import java.nio.ByteBuffer; +import java.util.concurrent.CountDownLatch; + +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.KeeperException.SessionExpiredException; +import org.apache.zookeeper.KeeperException.SessionMovedException; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.server.PrepRequestProcessor; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.txn.ErrorTxn; +import org.junit.Assert; +import org.junit.Test; + +public class PrepRequestProcessorTest extends ClientBase { + private static String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + private static final int CONNECTION_TIMEOUT = 3000; + private final CountDownLatch testEnd = new CountDownLatch(1); + + @Test + public void testPRequest() throws Exception { + File tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + SyncRequestProcessor.setSnapCount(100); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + Assert.assertTrue("waiting for server being up ", + ClientBase.waitForServerUp(HOSTPORT,CONNECTION_TIMEOUT)); + zks.sessionTracker = new MySessionTracker(); + PrepRequestProcessor processor = new PrepRequestProcessor(zks, new MyRequestProcessor()); + Request foo = new Request(null, 1l, 1, OpCode.create, ByteBuffer.allocate(3), null); + processor.pRequest(foo); + testEnd.await(5, java.util.concurrent.TimeUnit.SECONDS); + f.shutdown(); + zks.shutdown(); + } + + + private class MyRequestProcessor implements RequestProcessor { + @Override + public void processRequest(Request request) { + Assert.assertEquals("Request should have marshalling error", new ErrorTxn(Code.MARSHALLINGERROR.intValue()), request.getTxn()); + testEnd.countDown(); + } + @Override + public void shutdown() { + // TODO Auto-generated method stub + + } + } + + private class MySessionTracker implements SessionTracker { + @Override + public boolean addGlobalSession(long id, int to) { + // TODO Auto-generated method stub + return false; + } + @Override + public boolean addSession(long id, int to) { + // TODO Auto-generated method stub + return false; + } + @Override + public void checkSession(long sessionId, Object owner) + throws SessionExpiredException, SessionMovedException { + // TODO Auto-generated method stub + } + @Override + public long createSession(int sessionTimeout) { + // TODO Auto-generated method stub + return 0; + } + @Override + public void dumpSessions(PrintWriter pwriter) { + // TODO Auto-generated method stub + + } + @Override + public void removeSession(long sessionId) { + // TODO Auto-generated method stub + + } + public int upgradeSession(long sessionId) { + // TODO Auto-generated method stub + return 0; + } + @Override + public void setOwner(long id, Object owner) + throws SessionExpiredException { + // TODO Auto-generated method stub + + } + @Override + public void shutdown() { + // TODO Auto-generated method stub + + } + @Override + public boolean touchSession(long sessionId, int sessionTimeout) { + // TODO Auto-generated method stub + return false; + } + @Override + public void setSessionClosing(long sessionId) { + // TODO Auto-generated method stub + } + @Override + public boolean isTrackingSession(long sessionId) { + // TODO Auto-generated method stub + return false; + } + @Override + public void checkGlobalSession(long sessionId, Object owner) + throws SessionExpiredException, SessionMovedException { + // TODO Auto-generated method stub + } + } +} Propchange: zookeeper/trunk/src/java/test/org/apache/zookeeper/server/PrepRequestProcessorTest.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: zookeeper/trunk/src/java/test/org/apache/zookeeper/test/FLEPredicateTest.java URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/test/org/apache/zookeeper/test/FLEPredicateTest.java?rev=1584497&r1=1584496&r2=1584497&view=diff ============================================================================== --- zookeeper/trunk/src/java/test/org/apache/zookeeper/test/FLEPredicateTest.java (original) +++ zookeeper/trunk/src/java/test/org/apache/zookeeper/test/FLEPredicateTest.java Fri Apr 4 01:24:37 2014 @@ -1,107 +1,107 @@ -/** - * 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.test; - -import java.io.File; -import java.io.IOException; -import java.net.InetSocketAddress; -import java.util.HashMap; - -import org.apache.zookeeper.server.quorum.FastLeaderElection; -import org.apache.zookeeper.server.quorum.QuorumCnxManager; -import org.apache.zookeeper.server.quorum.QuorumPeer; -import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; -import org.apache.zookeeper.PortAssignment; -import org.apache.zookeeper.ZKTestCase; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.junit.Assert; -import org.junit.Test; - -public class FLEPredicateTest extends ZKTestCase { - - protected static final Logger LOG = LoggerFactory.getLogger(FLEPredicateTest.class); - - class MockFLE extends FastLeaderElection { - MockFLE(QuorumPeer peer){ - super(peer, new QuorumCnxManager(peer)); - } - - boolean predicate(long newId, long newZxid, long newEpoch, long curId, long curZxid, long curEpoch){ - return this.totalOrderPredicate(newId, newZxid, newEpoch, curId, curZxid, curEpoch); - } - } - - - HashMap<Long,QuorumServer> peers; - - @Test - public void testPredicate() throws IOException { - - peers = new HashMap<Long,QuorumServer>(3); - - /* - * Creates list of peers. - */ - for(int i = 0; i < 3; i++) { - peers.put(Long.valueOf(i), - new QuorumServer(i, - new InetSocketAddress( - "127.0.0.1", PortAssignment.unique()), - new InetSocketAddress( - "127.0.0.1", PortAssignment.unique()))); - } - - /* - * Creating peer. - */ - try{ - File tmpDir = ClientBase.createTmpDir(); - QuorumPeer peer = new QuorumPeer(peers, tmpDir, tmpDir, - PortAssignment.unique(), 3, 0, 1000, 2, 2); - - MockFLE mock = new MockFLE(peer); - - /* - * Lower epoch must return false - */ - - Assert.assertFalse (mock.predicate(4L, 0L, 0L, 3L, 0L, 2L)); - - /* - * Later epoch - */ - Assert.assertTrue (mock.predicate(0L, 0L, 1L, 1L, 0L, 0L)); - - /* - * Higher zxid - */ - Assert.assertTrue(mock.predicate(0L, 1L, 0L, 1L, 0L, 0L)); - - /* - * Higher id - */ - Assert.assertTrue(mock.predicate(1L, 1L, 0L, 0L, 1L, 0L)); - } catch (IOException e) { - LOG.error("Exception while creating quorum peer", e); - Assert.fail("Exception while creating quorum peer"); - } - } -} +/** + * 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.test; + +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.HashMap; + +import org.apache.zookeeper.server.quorum.FastLeaderElection; +import org.apache.zookeeper.server.quorum.QuorumCnxManager; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.junit.Assert; +import org.junit.Test; + +public class FLEPredicateTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(FLEPredicateTest.class); + + class MockFLE extends FastLeaderElection { + MockFLE(QuorumPeer peer){ + super(peer, new QuorumCnxManager(peer)); + } + + boolean predicate(long newId, long newZxid, long newEpoch, long curId, long curZxid, long curEpoch){ + return this.totalOrderPredicate(newId, newZxid, newEpoch, curId, curZxid, curEpoch); + } + } + + + HashMap<Long,QuorumServer> peers; + + @Test + public void testPredicate() throws IOException { + + peers = new HashMap<Long,QuorumServer>(3); + + /* + * Creates list of peers. + */ + for(int i = 0; i < 3; i++) { + peers.put(Long.valueOf(i), + new QuorumServer(i, + new InetSocketAddress( + "127.0.0.1", PortAssignment.unique()), + new InetSocketAddress( + "127.0.0.1", PortAssignment.unique()))); + } + + /* + * Creating peer. + */ + try{ + File tmpDir = ClientBase.createTmpDir(); + QuorumPeer peer = new QuorumPeer(peers, tmpDir, tmpDir, + PortAssignment.unique(), 3, 0, 1000, 2, 2); + + MockFLE mock = new MockFLE(peer); + + /* + * Lower epoch must return false + */ + + Assert.assertFalse (mock.predicate(4L, 0L, 0L, 3L, 0L, 2L)); + + /* + * Later epoch + */ + Assert.assertTrue (mock.predicate(0L, 0L, 1L, 1L, 0L, 0L)); + + /* + * Higher zxid + */ + Assert.assertTrue(mock.predicate(0L, 1L, 0L, 1L, 0L, 0L)); + + /* + * Higher id + */ + Assert.assertTrue(mock.predicate(1L, 1L, 0L, 0L, 1L, 0L)); + } catch (IOException e) { + LOG.error("Exception while creating quorum peer", e); + Assert.fail("Exception while creating quorum peer"); + } + } +} Propchange: zookeeper/trunk/src/java/test/org/apache/zookeeper/test/FLEPredicateTest.java ------------------------------------------------------------------------------ svn:eol-style = native