Prechádzať zdrojové kódy

ZOOKEEPER-1423: 4lw and jmx should expose the size of the datadir/datalogdir
(Edward Ribeiro via rgs)

git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1690207 13f79535-47bb-0310-9956-ffa450edef68

Raúl Gutiérrez Segalés 10 rokov pred
rodič
commit
359fa97e58

+ 3 - 0
CHANGES.txt

@@ -229,6 +229,9 @@ IMPROVEMENTS:
   ZOOKEEPER-2140: NettyServerCnxn and NIOServerCnxn code should be improved
   (Arshad Mohammad via rgs)
 
+  ZOOKEEPER-1423: 4lw and jmx should expose the size of the datadir/datalogdir
+  (Edward Ribeiro via rgs)
+
 Release 3.5.0 - 8/4/2014
 
 NEW FEATURES:

+ 10 - 0
src/docs/src/documentation/content/xdocs/zookeeperAdmin.xml

@@ -1638,6 +1638,16 @@ server.3=zoo3:2888:3888</programlisting>
             </listitem>
           </varlistentry>
 
+          <varlistentry>
+            <term>dirs</term>
+
+            <listitem>
+              <para><emphasis role="bold">New in 3.5.1:</emphasis>
+                Shows the total size of snapshot and log files in bytes
+              </para>
+            </listitem>
+          </varlistentry>
+
           <varlistentry>
             <term>wchp</term>
 

+ 10 - 0
src/java/main/org/apache/zookeeper/server/ServerStats.java

@@ -40,6 +40,8 @@ public class ServerStats {
         public long getLastProcessedZxid();
         public String getState();
         public int getNumAliveConnections();
+        public long getDataDirSize();
+        public long getLogDirSize();
     }
     
     public ServerStats(Provider provider) {
@@ -69,6 +71,14 @@ public class ServerStats {
     public long getLastProcessedZxid(){
         return provider.getLastProcessedZxid();
     }
+
+    public long getDataDirSize() {
+        return provider.getDataDirSize();
+    }
+
+    public long getLogDirSize() {
+        return provider.getLogDirSize();
+    }
     
     synchronized public long getPacketsReceived() {
         return packetsReceived;

+ 39 - 6
src/java/main/org/apache/zookeeper/server/ZooKeeperServer.java

@@ -164,7 +164,6 @@ public class ZooKeeperServer implements SessionExpirer, ServerStats.Provider {
      * creates a zookeeperserver instance.
      * @param txnLogFactory the file transaction snapshot logging class
      * @param tickTime the ticktime for the server
-     * @param treeBuilder the datatree builder
      * @throws IOException
      */
     public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime) throws IOException {
@@ -181,9 +180,13 @@ public class ZooKeeperServer implements SessionExpirer, ServerStats.Provider {
         pwriter.print("secureClientPort=");
         pwriter.println(getSecureClientPort());
         pwriter.print("dataDir=");
-        pwriter.println(zkDb.snapLog.getSnapDir().getAbsolutePath());
-        pwriter.print("dataLogDir=");
         pwriter.println(zkDb.snapLog.getDataDir().getAbsolutePath());
+        pwriter.print("dataDirSize=");
+        pwriter.println(getDataDirSize());
+        pwriter.print("dataLogDir=");
+        pwriter.println(zkDb.snapLog.getSnapDir().getAbsolutePath());
+        pwriter.print("dataLogSize=");
+        pwriter.println(getLogDirSize());
         pwriter.print("tickTime=");
         pwriter.println(getTickTime());
         pwriter.print("maxClientCnxns=");
@@ -304,6 +307,36 @@ public class ZooKeeperServer implements SessionExpirer, ServerStats.Provider {
         }
     }
 
+    @Override
+    public long getDataDirSize() {
+        if (zkDb == null) {
+            return 0L;
+        }
+        File path = zkDb.snapLog.getDataDir();
+        return getDirSize(path);
+    }
+
+    @Override
+    public long getLogDirSize() {
+        if (zkDb == null) {
+            return 0L;
+        }
+        File path = zkDb.snapLog.getSnapDir();
+        return getDirSize(path);
+    }
+
+    private long getDirSize(File file) {
+        long size = 0L;
+        if (file.isDirectory()) {
+            for (File f: file.listFiles()) {
+                size += getDirSize(f);
+            }
+        } else {
+            size = file.length();
+        }
+        return size;
+    }
+
     public long getZxid() {
         return hzxid.get();
     }
@@ -591,9 +624,9 @@ public class ZooKeeperServer implements SessionExpirer, ServerStats.Provider {
             int sessionTimeout) throws IOException {
         boolean rc = sessionTracker.touchSession(sessionId, sessionTimeout);
         if (LOG.isTraceEnabled()) {
-            ZooTrace.logTraceMessage(LOG,ZooTrace.SESSION_TRACE_MASK,
-                                     "Session 0x" + Long.toHexString(sessionId) +
-                    " is valid: " + rc);
+            ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK,
+                    "Session 0x" + Long.toHexString(sessionId) +
+                            " is valid: " + rc);
         }
         finishSessionInit(cnxn, rc);
     }

+ 7 - 0
src/java/main/org/apache/zookeeper/server/ZooKeeperServerBean.java

@@ -118,6 +118,13 @@ public class ZooKeeperServerBean implements ZooKeeperServerMXBean, ZKMBeanInfo {
         zks.setMaxSessionTimeout(max);
     }
 
+    public long getDataDirSize() {
+        return zks.getDataDirSize();
+    }
+
+    public long getLogDirSize() {
+        return zks.getLogDirSize();
+    }
     
     public long getPacketsReceived() {
         return zks.serverStats().getPacketsReceived();

+ 9 - 0
src/java/main/org/apache/zookeeper/server/ZooKeeperServerMXBean.java

@@ -107,4 +107,13 @@ public interface ZooKeeperServerMXBean {
      * @return number of alive client connections
      */
     public long getNumAliveConnections();
+
+    /**
+     * @return estimated size of data directory in bytes
+    */
+    public long getDataDirSize();
+    /**
+     * @return estimated size of log directory in bytes
+     */
+    public long getLogDirSize();
 }

+ 18 - 0
src/java/main/org/apache/zookeeper/server/admin/Commands.java

@@ -111,6 +111,7 @@ public class Commands {
         registerCommand(new CnxnStatResetCommand());
         registerCommand(new ConfCommand());
         registerCommand(new ConsCommand());
+        registerCommand(new DirsCommand());
         registerCommand(new DumpCommand());
         registerCommand(new EnvCommand());
         registerCommand(new GetTraceMaskCommand());
@@ -178,6 +179,23 @@ public class Commands {
         }
     }
 
+    /**
+     * Information on ZK datadir and snapdir size in bytes
+     */
+    public static class DirsCommand extends CommandBase {
+        public DirsCommand() {
+            super(Arrays.asList("dirs"));
+        }
+
+        @Override
+        public CommandResponse run(ZooKeeperServer zkServer, Map<String, String> kwargs) {
+            CommandResponse response = initializeResponse();
+            response.put("datadir_size", zkServer.getDataDirSize());
+            response.put("logdir_size", zkServer.getLogDirSize());
+            return response;
+        }
+    }
+
     /**
      * Information on session expirations and ephemerals. Returned map contains:
      *   - "expiry_time_to_session_ids": Map<Long, Set<Long>>

+ 2 - 0
src/java/main/org/apache/zookeeper/server/command/CommandExecutor.java

@@ -57,6 +57,8 @@ public class CommandExecutor {
             command = new StatResetCommand(pwriter, serverCnxn);
         } else if (commandCode == FourLetterCommands.crstCmd) {
             command = new CnxnStatResetCommand(pwriter, serverCnxn);
+        } else if (commandCode == FourLetterCommands.dirsCmd) {
+            command = new DirsCommand(pwriter, serverCnxn);
         } else if (commandCode == FourLetterCommands.dumpCmd) {
             command = new DumpCommand(pwriter, serverCnxn);
         } else if (commandCode == FourLetterCommands.statCmd

+ 41 - 0
src/java/main/org/apache/zookeeper/server/command/DirsCommand.java

@@ -0,0 +1,41 @@
+/**
+ * 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.command;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import org.apache.zookeeper.server.ServerCnxn;
+
+public class DirsCommand extends AbstractFourLetterCommand {
+
+    public DirsCommand(PrintWriter pw, ServerCnxn serverCnxn) {
+        super(pw, serverCnxn);
+    }
+
+    @Override
+    public void commandRun() throws IOException {
+        if (zkServer == null) {
+            pw.println(ZK_NOT_SERVING);
+            return;
+        }
+        pw.println("datadir_size: " + zkServer.getDataDirSize());
+        pw.println("logdir_size: " + zkServer.getLogDirSize());
+    }
+}

+ 8 - 0
src/java/main/org/apache/zookeeper/server/command/FourLetterCommands.java

@@ -46,6 +46,13 @@ public class FourLetterCommands {
     public final static int crstCmd =
         ByteBuffer.wrap("crst".getBytes()).getInt();
 
+    /*
+     * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands">
+     * Zk Admin</a>. this link is for all the commands.
+     */
+    public final static int dirsCmd =
+        ByteBuffer.wrap("dirs".getBytes()).getInt();
+
     /*
      * See <a href="{@docRoot}/../../../docs/zookeeperAdmin.html#sc_zkCommands">
      * Zk Admin</a>. this link is for all the commands.
@@ -152,6 +159,7 @@ public class FourLetterCommands {
         cmd2String.put(confCmd, "conf");
         cmd2String.put(consCmd, "cons");
         cmd2String.put(crstCmd, "crst");
+        cmd2String.put(dirsCmd, "dirs");
         cmd2String.put(dumpCmd, "dump");
         cmd2String.put(enviCmd, "envi");
         cmd2String.put(getTraceMaskCmd, "gtmk");

+ 3 - 0
src/java/test/org/apache/zookeeper/test/FourLetterWordsQuorumTest.java

@@ -64,6 +64,7 @@ public class FourLetterWordsQuorumTest extends QuorumBase {
             verify(hp, "srvr", "Outstanding");
             verify(hp, "cons", sid);
             verify(hp, "dump", sid);
+            verify(hp, "dirs", "size");
 
             zk.getData("/", true, null);
 
@@ -74,6 +75,7 @@ public class FourLetterWordsQuorumTest extends QuorumBase {
             verify(hp, "wchs", "watching 1");
             verify(hp, "wchp", sid);
             verify(hp, "wchc", sid);
+            verify(hp, "dirs", "size");
 
             zk.close();
 
@@ -87,6 +89,7 @@ public class FourLetterWordsQuorumTest extends QuorumBase {
             verify(hp, "wchs", "watch");
             verify(hp, "wchp", "");
             verify(hp, "wchc", "");
+            verify(hp, "dirs", "size");
 
             verify(hp, "srst", "reset");
             verify(hp, "crst", "reset");

+ 3 - 0
src/java/test/org/apache/zookeeper/test/FourLetterWordsTest.java

@@ -68,6 +68,7 @@ public class FourLetterWordsTest extends ClientBase {
         verify("srvr", "Outstanding");
         verify("cons", sid);
         verify("dump", sid);
+        verify("dirs", "size");
 
         zk.getData("/", true, null);
 
@@ -79,6 +80,7 @@ public class FourLetterWordsTest extends ClientBase {
         verify("wchs", "watching 1");
         verify("wchp", sid);
         verify("wchc", sid);
+        verify("dirs", "size");
         zk.close();
 
         verify("ruok", "imok");
@@ -102,6 +104,7 @@ public class FourLetterWordsTest extends ClientBase {
         verify("mntr", "num_alive_connections");
         verify("stat", "Connections");
         verify("srvr", "Connections");
+        verify("dirs", "size");
     }
 
     private String sendRequest(String cmd) throws IOException, SSLContextException {