You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by md...@apache.org on 2017/03/01 18:06:41 UTC

svn commit: r1785013 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/cache/ oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/

Author: mduerig
Date: Wed Mar  1 18:06:41 2017
New Revision: 1785013

URL: http://svn.apache.org/viewvc?rev=1785013&view=rev
Log:
OAK-4619: Unify RecordCacheStats and CacheStats
Factor common functionality into abstract base class

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/AbstractCacheStats.java
      - copied, changed from r1785012, jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheStats.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheStats.java
    jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordCacheStats.java

Copied: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/AbstractCacheStats.java (from r1785012, jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheStats.java)
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/AbstractCacheStats.java?p2=jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/AbstractCacheStats.java&p1=jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheStats.java&r1=1785012&r2=1785013&rev=1785013&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheStats.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/AbstractCacheStats.java Wed Mar  1 18:06:41 2017
@@ -1,62 +1,77 @@
 /*
- * 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
+ * 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
+ *      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.
  *
- * 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.jackrabbit.oak.cache;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+import static java.lang.String.format;
 import static org.apache.jackrabbit.oak.commons.IOUtils.humanReadableByteCount;
 
-import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
+import javax.annotation.Nonnull;
+
 import com.google.common.base.Objects;
-import com.google.common.cache.Cache;
-import com.google.common.cache.Weigher;
+import com.google.common.cache.CacheStats;
 import org.apache.jackrabbit.oak.api.jmx.CacheStatsMBean;
 import org.apache.jackrabbit.oak.commons.jmx.AnnotatedStandardMBean;
 
 /**
- * Cache statistics.
+ * Abstract base class for providing cache statistic via the {@link CacheStatsMBean}.
  */
-public class CacheStats extends AnnotatedStandardMBean implements CacheStatsMBean {
-    private final Cache<Object, Object> cache;
-    private final Weigher<Object, Object> weigher;
-    private final long maxWeight;
+public abstract class AbstractCacheStats extends AnnotatedStandardMBean implements CacheStatsMBean {
+
+    @Nonnull
     private final String name;
-    private com.google.common.cache.CacheStats lastSnapshot = 
-            new com.google.common.cache.CacheStats(
-            0, 0, 0, 0, 0, 0);
+
+    private CacheStats lastSnapshot =
+            new CacheStats(0, 0, 0, 0, 0, 0);
 
     /**
-     * Construct the cache stats object.
-     * 
-     * @param cache the cache
-     * @param name the name of the cache
-     * @param weigher the weigher used to estimate the current weight
-     * @param maxWeight the maximum weight
+     * Create a new {@code CacheStatsMBean} for a cache with the given {@code name}.
+     * @param name
      */
-    @SuppressWarnings("unchecked")
-    public CacheStats(Cache<?, ?> cache, String name, 
-            Weigher<?, ?> weigher, long maxWeight) {
+    protected AbstractCacheStats(@Nonnull String name) {
         super(CacheStatsMBean.class);
-        this.cache = (Cache<Object, Object>) cache;
-        this.name = name;
-        this.weigher = (Weigher<Object, Object>) weigher;
-        this.maxWeight = maxWeight;
+        this.name = checkNotNull(name);
+    }
+
+    /**
+     * Call back invoked to retrieve the most recent {@code CacheStats} instance of the
+     * underlying cache.
+     */
+    protected abstract CacheStats getCurrentStats();
+
+    private CacheStats stats() {
+        return getCurrentStats().minus(lastSnapshot);
+    }
+
+    @Override
+    public synchronized void resetStats() {
+        // Cache stats cannot be rest at Guava level. Instead we
+        // take a snapshot and then subtract it from future stats calls
+        lastSnapshot = getCurrentStats();
+    }
+
+    @Nonnull
+    @Override
+    public String getName() {
+        return name;
     }
 
     @Override
@@ -120,49 +135,18 @@ public class CacheStats extends Annotate
     }
 
     @Override
-    public long getElementCount() {
-        return cache.size();
-    }
-
-    @Override
-    public long estimateCurrentWeight() {
-        if (weigher == null) {
-            return -1;
-        }
-        long size = 0;
-        for (Map.Entry<?, ?> e : cache.asMap().entrySet()) {
-            Object k = e.getKey();
-            Object v = e.getValue();
-            size += weigher.weigh(k, v);
-        }
-        return size;
-    }
-
-    @Override
-    public long getMaxTotalWeight() {
-        return maxWeight;
-    }
-
-    @Override
-    public synchronized void resetStats() {
-        //Cache stats cannot be rest at Guava level. Instead we
-        //take a snapshot and then subtract it from future stats calls
-        lastSnapshot = cache.stats();
-    }
-
-    @Override
     public String cacheInfoAsString() {
         return Objects.toStringHelper("CacheStats")
                 .add("hitCount", getHitCount())
-                .add("hitRate", String.format("%1.2f", getHitRate()))
+                .add("hitRate", format("%1.2f", getHitRate()))
                 .add("missCount", getMissCount())
-                .add("missRate", String.format("%1.2f", getMissRate()))
+                .add("missRate", format("%1.2f", getMissRate()))
                 .add("requestCount", getRequestCount())
                 .add("loadCount", getLoadCount())
                 .add("loadSuccessCount", getLoadSuccessCount())
                 .add("loadExceptionCount", getLoadExceptionCount())
                 .add("totalLoadTime", timeInWords(getTotalLoadTime()))
-                .add("averageLoadPenalty (nanos)", String.format("%1.2f", getAverageLoadPenalty()))
+                .add("averageLoadPenalty", format("%1.2f ns", getAverageLoadPenalty()))
                 .add("evictionCount", getEvictionCount())
                 .add("elementCount", getElementCount())
                 .add("totalWeight", humanReadableByteCount(estimateCurrentWeight()))
@@ -170,15 +154,6 @@ public class CacheStats extends Annotate
                 .toString();
     }
 
-    @Override
-    public String getName() {
-        return name;
-    }
-
-    private com.google.common.cache.CacheStats stats() {
-        return cache.stats().minus(lastSnapshot);
-    }
-
     static String timeInWords(long nanos) {
         long millis = TimeUnit.NANOSECONDS.toMillis(nanos);
         return String.format("%d min, %d sec",
@@ -187,4 +162,5 @@ public class CacheStats extends Annotate
                         TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
         );
     }
+
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheStats.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheStats.java?rev=1785013&r1=1785012&r2=1785013&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheStats.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheStats.java Wed Mar  1 18:06:41 2017
@@ -18,28 +18,23 @@
  */
 package org.apache.jackrabbit.oak.cache;
 
-import static org.apache.jackrabbit.oak.commons.IOUtils.humanReadableByteCount;
+import static com.google.common.base.Preconditions.checkNotNull;
 
 import java.util.Map;
-import java.util.concurrent.TimeUnit;
 
-import com.google.common.base.Objects;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
 import com.google.common.cache.Cache;
 import com.google.common.cache.Weigher;
-import org.apache.jackrabbit.oak.api.jmx.CacheStatsMBean;
-import org.apache.jackrabbit.oak.commons.jmx.AnnotatedStandardMBean;
 
 /**
  * Cache statistics.
  */
-public class CacheStats extends AnnotatedStandardMBean implements CacheStatsMBean {
+public class CacheStats extends AbstractCacheStats {
     private final Cache<Object, Object> cache;
     private final Weigher<Object, Object> weigher;
     private final long maxWeight;
-    private final String name;
-    private com.google.common.cache.CacheStats lastSnapshot = 
-            new com.google.common.cache.CacheStats(
-            0, 0, 0, 0, 0, 0);
 
     /**
      * Construct the cache stats object.
@@ -50,73 +45,20 @@ public class CacheStats extends Annotate
      * @param maxWeight the maximum weight
      */
     @SuppressWarnings("unchecked")
-    public CacheStats(Cache<?, ?> cache, String name, 
-            Weigher<?, ?> weigher, long maxWeight) {
-        super(CacheStatsMBean.class);
-        this.cache = (Cache<Object, Object>) cache;
-        this.name = name;
+    public CacheStats(
+            @Nonnull Cache<?, ?> cache,
+            @Nonnull String name,
+            @Nullable Weigher<?, ?> weigher,
+            long maxWeight) {
+        super(name);
+        this.cache = (Cache<Object, Object>) checkNotNull(cache);
         this.weigher = (Weigher<Object, Object>) weigher;
         this.maxWeight = maxWeight;
     }
 
     @Override
-    public long getRequestCount() {
-        return stats().requestCount();
-    }
-
-    @Override
-    public long getHitCount() {
-        return stats().hitCount();
-    }
-
-    @Override
-    public double getHitRate() {
-        return stats().hitRate();
-    }
-
-    @Override
-    public long getMissCount() {
-        return stats().missCount();
-    }
-
-    @Override
-    public double getMissRate() {
-        return stats().missRate();
-    }
-
-    @Override
-    public long getLoadCount() {
-        return stats().loadCount();
-    }
-
-    @Override
-    public long getLoadSuccessCount() {
-        return stats().loadSuccessCount();
-    }
-
-    @Override
-    public long getLoadExceptionCount() {
-        return stats().loadExceptionCount();
-    }
-
-    @Override
-    public double getLoadExceptionRate() {
-        return stats().loadExceptionRate();
-    }
-
-    @Override
-    public long getTotalLoadTime() {
-        return stats().totalLoadTime();
-    }
-
-    @Override
-    public double getAverageLoadPenalty() {
-        return stats().averageLoadPenalty();
-    }
-
-    @Override
-    public long getEvictionCount() {
-        return stats().evictionCount();
+    protected com.google.common.cache.CacheStats getCurrentStats() {
+        return cache.stats();
     }
 
     @Override
@@ -142,49 +84,4 @@ public class CacheStats extends Annotate
     public long getMaxTotalWeight() {
         return maxWeight;
     }
-
-    @Override
-    public synchronized void resetStats() {
-        //Cache stats cannot be rest at Guava level. Instead we
-        //take a snapshot and then subtract it from future stats calls
-        lastSnapshot = cache.stats();
-    }
-
-    @Override
-    public String cacheInfoAsString() {
-        return Objects.toStringHelper("CacheStats")
-                .add("hitCount", getHitCount())
-                .add("hitRate", String.format("%1.2f", getHitRate()))
-                .add("missCount", getMissCount())
-                .add("missRate", String.format("%1.2f", getMissRate()))
-                .add("requestCount", getRequestCount())
-                .add("loadCount", getLoadCount())
-                .add("loadSuccessCount", getLoadSuccessCount())
-                .add("loadExceptionCount", getLoadExceptionCount())
-                .add("totalLoadTime", timeInWords(getTotalLoadTime()))
-                .add("averageLoadPenalty (nanos)", String.format("%1.2f", getAverageLoadPenalty()))
-                .add("evictionCount", getEvictionCount())
-                .add("elementCount", getElementCount())
-                .add("totalWeight", humanReadableByteCount(estimateCurrentWeight()))
-                .add("maxWeight", humanReadableByteCount(getMaxTotalWeight()))
-                .toString();
-    }
-
-    @Override
-    public String getName() {
-        return name;
-    }
-
-    private com.google.common.cache.CacheStats stats() {
-        return cache.stats().minus(lastSnapshot);
-    }
-
-    static String timeInWords(long nanos) {
-        long millis = TimeUnit.NANOSECONDS.toMillis(nanos);
-        return String.format("%d min, %d sec",
-                TimeUnit.MILLISECONDS.toMinutes(millis),
-                TimeUnit.MILLISECONDS.toSeconds(millis) -
-                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
-        );
-    }
 }

Modified: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordCacheStats.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordCacheStats.java?rev=1785013&r1=1785012&r2=1785013&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordCacheStats.java (original)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordCacheStats.java Wed Mar  1 18:06:41 2017
@@ -20,26 +20,17 @@
 package org.apache.jackrabbit.oak.segment;
 
 import static com.google.common.base.Preconditions.checkNotNull;
-import static java.lang.String.format;
-import static java.util.concurrent.TimeUnit.NANOSECONDS;
-import static org.apache.jackrabbit.oak.commons.IOUtils.humanReadableByteCount;
 
 import javax.annotation.Nonnull;
 
-import com.google.common.base.Objects;
 import com.google.common.base.Supplier;
 import com.google.common.cache.CacheStats;
-import org.apache.jackrabbit.oak.api.jmx.CacheStatsMBean;
-import org.apache.jackrabbit.oak.commons.jmx.AnnotatedStandardMBean;
+import org.apache.jackrabbit.oak.cache.AbstractCacheStats;
 
-// FIXME OAK-4619: Unify RecordCacheStats and CacheStats
 /**
  * Statistics for {@link RecordCache}.
  */
-public class RecordCacheStats extends AnnotatedStandardMBean implements CacheStatsMBean {
-
-    @Nonnull
-    private final String name;
+public class RecordCacheStats extends AbstractCacheStats {
 
     @Nonnull
     private final Supplier<CacheStats> stats;
@@ -50,92 +41,20 @@ public class RecordCacheStats extends An
     @Nonnull
     private final Supplier<Long> weight;
 
-    private CacheStats lastSnapshot;
-
-    public RecordCacheStats(@Nonnull String name,
+    public RecordCacheStats(
+            @Nonnull String name,
             @Nonnull Supplier<CacheStats> stats,
-            @Nonnull Supplier<Long> elementCount, @Nonnull Supplier<Long> weight) {
-        super(CacheStatsMBean.class);
-        this.name = checkNotNull(name);
+            @Nonnull Supplier<Long> elementCount,
+            @Nonnull Supplier<Long> weight) {
+        super(name);
         this.stats = checkNotNull(stats);
         this.elementCount = checkNotNull(elementCount);
         this.weight = checkNotNull(weight);
-        this.lastSnapshot = stats.get();
-    }
-
-    private CacheStats stats() {
-        return stats.get().minus(lastSnapshot);
     }
 
     @Override
-    public synchronized void resetStats() {
-        lastSnapshot = stats.get();
-    }
-
-    @Nonnull
-    @Override
-    public String getName() {
-        return name;
-    }
-
-    @Override
-    public long getRequestCount() {
-        return stats().requestCount();
-    }
-
-    @Override
-    public long getHitCount() {
-        return stats().hitCount();
-    }
-
-    @Override
-    public double getHitRate() {
-        return stats().hitRate();
-    }
-
-    @Override
-    public long getMissCount() {
-        return stats().missCount();
-    }
-
-    @Override
-    public double getMissRate() {
-        return stats().missRate();
-    }
-
-    @Override
-    public long getLoadCount() {
-        return stats().loadCount();
-    }
-
-    @Override
-    public long getLoadSuccessCount() {
-        return stats().loadSuccessCount();
-    }
-
-    @Override
-    public long getLoadExceptionCount() {
-        return stats().loadExceptionCount();
-    }
-
-    @Override
-    public double getLoadExceptionRate() {
-        return stats().loadExceptionRate();
-    }
-
-    @Override
-    public long getTotalLoadTime() {
-        return stats().totalLoadTime();
-    }
-
-    @Override
-    public double getAverageLoadPenalty() {
-        return stats().averageLoadPenalty();
-    }
-
-    @Override
-    public long getEvictionCount() {
-        return stats().evictionCount();
+    protected CacheStats getCurrentStats() {
+        return stats.get();
     }
 
     @Override
@@ -152,26 +71,4 @@ public class RecordCacheStats extends An
     public long estimateCurrentWeight() {
         return weight.get();
     }
-
-    @Override
-    public String cacheInfoAsString() {
-        return Objects.toStringHelper("CacheStats(" + name + ")")
-            .add("hitCount", getHitCount())
-            .add("hitRate", format("%1.2f", getHitRate()))
-            .add("missCount", getMissCount())
-            .add("missRate", format("%1.2f", getMissRate()))
-            .add("requestCount", getRequestCount())
-            .add("loadCount", getLoadCount())
-            .add("loadSuccessCount", getLoadSuccessCount())
-            .add("loadExceptionCount", getLoadExceptionCount())
-            .add("totalLoadTime", format("%d s", NANOSECONDS.toSeconds(getTotalLoadTime())))
-            .add("averageLoadPenalty ", format("%1.2f ns", getAverageLoadPenalty()))
-            .add("evictionCount", getEvictionCount())
-            .add("elementCount", getElementCount())
-            .add("totalWeight", humanReadableByteCount(estimateCurrentWeight()))
-            .add("maxWeight", humanReadableByteCount(getMaxTotalWeight()))
-            .add("currentWeights", humanReadableByteCount(estimateCurrentWeight()))
-            .toString();
-    }
-
 }