zookeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From f..@apache.org
Subject svn commit: r1365171 - in /zookeeper/bookkeeper/branches/branch-4.1: ./ bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/ bookkeeper-server/src/main/java/org/apache/bookkeeper/client/ bookkeeper-server/src/main/java/org/apache/bookkeeper/pr...
Date Tue, 24 Jul 2012 17:02:38 GMT
Author: fpj
Date: Tue Jul 24 17:02:38 2012
New Revision: 1365171

URL: http://svn.apache.org/viewvc?rev=1365171&view=rev
Log:
BOOKKEEPER-327: System.currentTimeMillis usage in BookKeeper (uma via fpj)


Added:
    zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/test/java/org/apache/bookkeeper/proto/TestBKStats.java
Modified:
    zookeeper/bookkeeper/branches/branch-4.1/CHANGES.txt
    zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/Bookie.java
    zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/GarbageCollectorThread.java
    zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/Journal.java
    zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeper.java
    zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BKStats.java
    zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieServer.java
    zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java
    zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/ServerStats.java
    zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
    zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/MathUtils.java

Modified: zookeeper/bookkeeper/branches/branch-4.1/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/branches/branch-4.1/CHANGES.txt?rev=1365171&r1=1365170&r2=1365171&view=diff
==============================================================================
--- zookeeper/bookkeeper/branches/branch-4.1/CHANGES.txt (original)
+++ zookeeper/bookkeeper/branches/branch-4.1/CHANGES.txt Tue Jul 24 17:02:38 2012
@@ -152,6 +152,8 @@ Release 4.1.0 - 2012-06-07
 
         BOOKKEEPER-288: NOTICE files don't have the correct year (ivank via sijie)
 
+	BOOKKEEPER-327: System.currentTimeMillis usage in BookKeeper (uma via fpj)
+
       hedwig-client/
 
         BOOKKEEPER-217: NPE in hedwig client when enable DEBUG (sijie via ivank)

Modified: zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/Bookie.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/Bookie.java?rev=1365171&r1=1365170&r2=1365171&view=diff
==============================================================================
--- zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/Bookie.java
(original)
+++ zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/Bookie.java
Tue Jul 24 17:02:38 2012
@@ -1,4 +1,4 @@
-/*
+/**
  *
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -46,6 +46,7 @@ import org.apache.bookkeeper.conf.Server
 import org.apache.bookkeeper.jmx.BKMBeanInfo;
 import org.apache.bookkeeper.jmx.BKMBeanRegistry;
 import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.WriteCallback;
+import org.apache.bookkeeper.util.MathUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.apache.zookeeper.KeeperException;
@@ -792,7 +793,7 @@ public class Bookie extends Thread {
         Bookie b = new Bookie(new ServerConfiguration());
         b.start();
         CounterCallback cb = new CounterCallback();
-        long start = System.currentTimeMillis();
+        long start = MathUtils.now();
         for (int i = 0; i < 100000; i++) {
             ByteBuffer buff = ByteBuffer.allocate(1024);
             buff.putLong(1);
@@ -803,7 +804,7 @@ public class Bookie extends Thread {
             b.addEntry(buff, cb, null, new byte[0]);
         }
         cb.waitZero();
-        long end = System.currentTimeMillis();
+        long end = MathUtils.now();
         System.out.println("Took " + (end-start) + "ms");
     }
 }

Modified: zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/GarbageCollectorThread.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/GarbageCollectorThread.java?rev=1365171&r1=1365170&r2=1365171&view=diff
==============================================================================
--- zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/GarbageCollectorThread.java
(original)
+++ zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/GarbageCollectorThread.java
Tue Jul 24 17:02:38 2012
@@ -1,4 +1,4 @@
-/*
+/**
  *
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -23,21 +23,21 @@ package org.apache.bookkeeper.bookie;
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
-import java.util.Comparator;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Map;
+import java.util.Comparator;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicBoolean;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import org.apache.bookkeeper.bookie.EntryLogger.EntryLogScanner;
 import org.apache.bookkeeper.conf.ServerConfiguration;
 import org.apache.bookkeeper.meta.LedgerManager;
 import org.apache.zookeeper.ZooKeeper;
+import org.apache.bookkeeper.util.MathUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * This is the garbage collector thread that runs in the background to
@@ -172,7 +172,7 @@ public class GarbageCollectorThread exte
         LOG.info("Major Compaction : enabled=" + enableMajorCompaction + ", threshold="
                + majorCompactionThreshold + ", interval=" + majorCompactionInterval);
 
-        lastMinorCompactionTime = lastMajorCompactionTime = System.currentTimeMillis();
+        lastMinorCompactionTime = lastMajorCompactionTime = MathUtils.now();
     }
 
     @Override
@@ -197,13 +197,13 @@ public class GarbageCollectorThread exte
             // gc entry logs
             doGcEntryLogs();
 
-            long curTime = System.currentTimeMillis();
+            long curTime = MathUtils.now();
             if (enableMajorCompaction &&
                 curTime - lastMajorCompactionTime > majorCompactionInterval) {
                 // enter major compaction
                 LOG.info("Enter major compaction");
                 doCompactEntryLogs(majorCompactionThreshold);
-                lastMajorCompactionTime = System.currentTimeMillis();
+                lastMajorCompactionTime = MathUtils.now();
                 // also move minor compaction time
                 lastMinorCompactionTime = lastMajorCompactionTime;
                 continue;
@@ -214,7 +214,7 @@ public class GarbageCollectorThread exte
                 // enter minor compaction
                 LOG.info("Enter minor compaction");
                 doCompactEntryLogs(minorCompactionThreshold);
-                lastMinorCompactionTime = System.currentTimeMillis();
+                lastMinorCompactionTime = MathUtils.now();
             }
         }
     }
@@ -284,7 +284,7 @@ public class GarbageCollectorThread exte
                 }
             }
         };
-        List<EntryLogMetadata> logsToCompact = new ArrayList();
+        List<EntryLogMetadata> logsToCompact = new ArrayList<EntryLogMetadata>();
         logsToCompact.addAll(entryLogMetaMap.values());
         Collections.sort(logsToCompact, sizeComparator);
         for (EntryLogMetadata meta : logsToCompact) {

Modified: zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/Journal.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/Journal.java?rev=1365171&r1=1365170&r2=1365171&view=diff
==============================================================================
--- zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/Journal.java
(original)
+++ zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/Journal.java
Tue Jul 24 17:02:38 2012
@@ -1,4 +1,4 @@
-/*
+/**
  *
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -35,6 +35,7 @@ import java.util.concurrent.LinkedBlocki
 import org.apache.bookkeeper.conf.ServerConfiguration;
 import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.WriteCallback;
 import org.apache.bookkeeper.util.IOUtils;
+import org.apache.bookkeeper.util.MathUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -480,7 +481,7 @@ class Journal extends Thread {
             while (true) {
                 // new journal file to write
                 if (null == logFile) {
-                    logId = System.currentTimeMillis();
+                    logId = MathUtils.now();
                     logFile = new JournalChannel(journalDirectory, logId);
                     bc = logFile.getBufferedChannel();
 

Modified: zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeper.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeper.java?rev=1365171&r1=1365170&r2=1365171&view=diff
==============================================================================
--- zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeper.java
(original)
+++ zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeper.java
Tue Jul 24 17:02:38 2012
@@ -1,6 +1,4 @@
-package org.apache.bookkeeper.client;
-
-/*
+/**
  *
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -20,31 +18,30 @@ package org.apache.bookkeeper.client;
  * under the License.
  *
  */
+package org.apache.bookkeeper.client;
 
 import java.io.IOException;
-import java.util.concurrent.Executors;
-
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 
-import org.apache.bookkeeper.meta.LedgerManager;
-import org.apache.bookkeeper.meta.LedgerManagerFactory;
-import org.apache.bookkeeper.conf.ClientConfiguration;
 import org.apache.bookkeeper.client.AsyncCallback.CreateCallback;
 import org.apache.bookkeeper.client.AsyncCallback.DeleteCallback;
 import org.apache.bookkeeper.client.AsyncCallback.OpenCallback;
 import org.apache.bookkeeper.client.BKException.Code;
+import org.apache.bookkeeper.conf.ClientConfiguration;
+import org.apache.bookkeeper.meta.LedgerManager;
+import org.apache.bookkeeper.meta.LedgerManagerFactory;
 import org.apache.bookkeeper.proto.BookieClient;
 import org.apache.bookkeeper.util.OrderedSafeExecutor;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.apache.zookeeper.KeeperException;
 import org.apache.zookeeper.WatchedEvent;
 import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.ZooKeeper;
 import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
 import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
-import org.apache.bookkeeper.util.SafeRunnable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * BookKeeper client. We assume there is one single writer to a ledger at any

Modified: zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BKStats.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BKStats.java?rev=1365171&r1=1365170&r2=1365171&view=diff
==============================================================================
--- zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BKStats.java
(original)
+++ zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BKStats.java
Tue Jul 24 17:02:38 2012
@@ -1,4 +1,4 @@
-/*
+/**
  *
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -23,10 +23,14 @@ package org.apache.bookkeeper.proto;
 
 import java.beans.ConstructorProperties;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  * Bookie Server Stats
  */
 public class BKStats {
+    private static final Logger LOG = LoggerFactory.getLogger(BKStats.class);
     private static BKStats instance = new BKStats();
 
     public static BKStats getInstance() {
@@ -105,6 +109,14 @@ public class BKStats {
          * Update Latency
          */
         synchronized public void updateLatency(long latency) {
+            if (latency < 0) {
+                // less than 0ms . Ideally this should not happen.
+                // We have seen this latency negative in some cases due to the
+                // behaviors of JVM. Ignoring the statistics updation for such
+                // cases.
+                LOG.warn("Latency time coming negative");
+                return;
+            }
             totalLatency += latency;
             ++numSuccessOps;
             if (latency < minLatency) {

Modified: zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieServer.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieServer.java?rev=1365171&r1=1365170&r2=1365171&view=diff
==============================================================================
--- zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieServer.java
(original)
+++ zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieServer.java
Tue Jul 24 17:02:38 2012
@@ -1,6 +1,4 @@
-package org.apache.bookkeeper.proto;
-
-/*
+/**
  *
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -20,6 +18,7 @@ package org.apache.bookkeeper.proto;
  * under the License.
  *
  */
+package org.apache.bookkeeper.proto;
 
 import java.io.File;
 import java.io.IOException;
@@ -37,6 +36,8 @@ import org.apache.bookkeeper.bookie.Exit
 import org.apache.bookkeeper.conf.ServerConfiguration;
 import org.apache.bookkeeper.jmx.BKMBeanRegistry;
 import org.apache.bookkeeper.proto.NIOServerFactory.Cnxn;
+import org.apache.bookkeeper.util.MathUtils;
+
 import static org.apache.bookkeeper.proto.BookieProtocol.PacketHeader;
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.cli.BasicParser;
@@ -322,7 +323,7 @@ public class BookieServer implements NIO
         int statType = BKStats.STATS_UNKNOWN;
         long startTime = 0;
         if (isStatsEnabled) {
-            startTime = System.currentTimeMillis();
+            startTime = MathUtils.now();
         }
 
         // packet format is different between ADDENTRY and READENTRY
@@ -442,7 +443,7 @@ public class BookieServer implements NIO
             if (success) {
                 // for add operations, we compute latency in writeComplete callbacks.
                 if (statType != BKStats.STATS_ADD) {
-                    long elapsedTime = System.currentTimeMillis() - startTime;
+                    long elapsedTime = MathUtils.now() - startTime;
                     bkStats.getOpStats(statType).updateLatency(elapsedTime);
                 }
             } else {
@@ -482,7 +483,7 @@ public class BookieServer implements NIO
             // compute the latency
             if (0 == rc) {
                 // for add operations, we compute latency in writeComplete callbacks.
-                long elapsedTime = System.currentTimeMillis() - startTime;
+                long elapsedTime = MathUtils.now() - startTime;
                 bkStats.getOpStats(BKStats.STATS_ADD).updateLatency(elapsedTime);
             } else {
                 bkStats.getOpStats(BKStats.STATS_ADD).incrementFailedOps();             
  

Modified: zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java?rev=1365171&r1=1365170&r2=1365171&view=diff
==============================================================================
--- zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java
(original)
+++ zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java
Tue Jul 24 17:02:38 2012
@@ -1,5 +1,3 @@
-package org.apache.bookkeeper.proto;
-
 /**
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -17,6 +15,7 @@ package org.apache.bookkeeper.proto;
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+package org.apache.bookkeeper.proto;
 
 import java.io.IOException;
 import java.net.InetSocketAddress;
@@ -26,23 +25,19 @@ import java.util.concurrent.ConcurrentHa
 import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicLong;
 
-import org.apache.bookkeeper.conf.ClientConfiguration;
 import org.apache.bookkeeper.client.BKException;
+import org.apache.bookkeeper.conf.ClientConfiguration;
+import org.apache.bookkeeper.proto.BookieProtocol.PacketHeader;
 import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback;
-import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.WriteCallback;
 import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback;
-import static org.apache.bookkeeper.proto.BookieProtocol.PacketHeader;
+import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.WriteCallback;
+import org.apache.bookkeeper.util.MathUtils;
 import org.apache.bookkeeper.util.OrderedSafeExecutor;
 import org.apache.bookkeeper.util.SafeRunnable;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.jboss.netty.util.Timer;
-import org.jboss.netty.util.HashedWheelTimer;
 import org.jboss.netty.bootstrap.ClientBootstrap;
 import org.jboss.netty.buffer.ChannelBuffer;
 import org.jboss.netty.buffer.ChannelBuffers;
 import org.jboss.netty.channel.Channel;
-import org.jboss.netty.channel.ChannelFactory;
 import org.jboss.netty.channel.ChannelFuture;
 import org.jboss.netty.channel.ChannelFutureListener;
 import org.jboss.netty.channel.ChannelHandlerContext;
@@ -58,8 +53,12 @@ import org.jboss.netty.channel.socket.Cl
 import org.jboss.netty.handler.codec.frame.CorruptedFrameException;
 import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
 import org.jboss.netty.handler.codec.frame.TooLongFrameException;
-import org.jboss.netty.handler.timeout.ReadTimeoutHandler;
 import org.jboss.netty.handler.timeout.ReadTimeoutException;
+import org.jboss.netty.handler.timeout.ReadTimeoutHandler;
+import org.jboss.netty.util.HashedWheelTimer;
+import org.jboss.netty.util.Timer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * This class manages all details of connection to a particular bookie. It also
@@ -503,7 +502,7 @@ public class PerChannelBookieClient exte
         }
 
         final ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
-        final int type, rc;
+        final int rc;
         final long ledgerId, entryId;
         final PacketHeader header;
 
@@ -665,7 +664,7 @@ public class PerChannelBookieClient exte
         CompletionKey(long ledgerId, long entryId) {
             this.ledgerId = ledgerId;
             this.entryId = entryId;
-            this.timeoutAt = System.currentTimeMillis() + (conf.getReadTimeout()*1000);
+            this.timeoutAt = MathUtils.now() + (conf.getReadTimeout()*1000);
         }
 
         @Override
@@ -687,7 +686,7 @@ public class PerChannelBookieClient exte
         }
 
         public boolean shouldTimeout() {
-            return this.timeoutAt <= System.currentTimeMillis();
+            return this.timeoutAt <= MathUtils.now();
         }
     }
 

Modified: zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/ServerStats.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/ServerStats.java?rev=1365171&r1=1365170&r2=1365171&view=diff
==============================================================================
--- zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/ServerStats.java
(original)
+++ zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/ServerStats.java
Tue Jul 24 17:02:38 2012
@@ -1,4 +1,4 @@
-/*
+/**
  * Licensed 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
@@ -14,6 +14,8 @@
 
 package org.apache.bookkeeper.proto;
 
+import org.apache.bookkeeper.util.MathUtils;
+
 public class ServerStats {
     private static ServerStats instance = new ServerStats();
     private long packetsSent;
@@ -113,7 +115,7 @@ public class ServerStats {
     }
 
     synchronized void updateLatency(long requestCreateTime) {
-        long latency = System.currentTimeMillis() - requestCreateTime;
+        long latency = MathUtils.now() - requestCreateTime;
         totalLatency += latency;
         count++;
         if (latency < minLatency) {

Modified: zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java?rev=1365171&r1=1365170&r2=1365171&view=diff
==============================================================================
--- zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
(original)
+++ zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
Tue Jul 24 17:02:38 2012
@@ -1,5 +1,3 @@
-package org.apache.bookkeeper.util;
-
 /**
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -17,6 +15,7 @@ package org.apache.bookkeeper.util;
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+package org.apache.bookkeeper.util;
 
 import java.io.BufferedReader;
 import java.io.File;
@@ -209,7 +208,7 @@ public class LocalBookKeeper {
     }
 
     public static boolean waitForServerUp(String hp, long timeout) {
-        long start = System.currentTimeMillis();
+        long start = MathUtils.now();
         String split[] = hp.split(":");
         String host = split[0];
         int port = Integer.parseInt(split[1]);
@@ -241,7 +240,7 @@ public class LocalBookKeeper {
                 LOG.info("server " + hp + " not up " + e);
             }
 
-            if (System.currentTimeMillis() > start + timeout) {
+            if (MathUtils.now() > start + timeout) {
                 break;
             }
             try {

Modified: zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/MathUtils.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/MathUtils.java?rev=1365171&r1=1365170&r2=1365171&view=diff
==============================================================================
--- zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/MathUtils.java
(original)
+++ zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/MathUtils.java
Tue Jul 24 17:02:38 2012
@@ -1,6 +1,3 @@
-package org.apache.bookkeeper.util;
-
-
 /**
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -18,12 +15,13 @@ package org.apache.bookkeeper.util;
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+package org.apache.bookkeeper.util;
 
 /**
- * Provides misc math functions that dont come standard
+ * Provides misc math functions that don't come standard
  */
 public class MathUtils {
-
+    private static final long NANOSECONDS_PER_MILLISECOND = 1000000;
     public static int signSafeMod(long dividend, int divisor) {
         int mod = (int) (dividend % divisor);
 
@@ -34,5 +32,17 @@ public class MathUtils {
         return mod;
 
     }
+    
+    /**
+     * Current time from some arbitrary time base in the past, counting in
+     * milliseconds, and not affected by settimeofday or similar system clock
+     * changes. This is appropriate to use when computing how much longer to
+     * wait for an interval to expire.
+     * 
+     * @return current time in milliseconds.
+     */
+    public static long now() {
+        return System.nanoTime() / NANOSECONDS_PER_MILLISECOND;
+    }
 
 }

Added: zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/test/java/org/apache/bookkeeper/proto/TestBKStats.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/test/java/org/apache/bookkeeper/proto/TestBKStats.java?rev=1365171&view=auto
==============================================================================
--- zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/test/java/org/apache/bookkeeper/proto/TestBKStats.java
(added)
+++ zookeeper/bookkeeper/branches/branch-4.1/bookkeeper-server/src/test/java/org/apache/bookkeeper/proto/TestBKStats.java
Tue Jul 24 17:02:38 2012
@@ -0,0 +1,45 @@
+/**
+ *
+ * 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.bookkeeper.proto;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.bookkeeper.proto.BKStats.OpStats;
+import org.junit.Test;
+
+/** Tests that Statistics updation in Bookie Server */
+public class TestBKStats {
+
+    /**
+     * Tests that updatLatency should not fail with
+     * ArrayIndexOutOfBoundException when latency time coming as negative.
+     */
+    @Test
+    public void testUpdateLatencyShouldNotFailWithAIOBEWithNegativeLatency()
+            throws Exception {
+        BKStats stats = BKStats.getInstance();
+        OpStats opStat = stats.getOpStats(0);
+        opStat.updateLatency(-10);
+        assertEquals("Should not update any latency metrics", 0,
+                opStat.numSuccessOps);
+
+    }
+}



Mime
View raw message