Index: oak-core/src/main/java/org/apache/jackrabbit/oak/cache/AbstractCacheStats.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- oak-core/src/main/java/org/apache/jackrabbit/oak/cache/AbstractCacheStats.java (date 1487956431000) +++ oak-core/src/main/java/org/apache/jackrabbit/oak/cache/AbstractCacheStats.java (date 1487956431000) @@ -0,0 +1,166 @@ +/* + * 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.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.concurrent.TimeUnit; + +import javax.annotation.Nonnull; + +import com.google.common.base.Objects; +import com.google.common.cache.CacheStats; +import org.apache.jackrabbit.oak.api.jmx.CacheStatsMBean; +import org.apache.jackrabbit.oak.commons.jmx.AnnotatedStandardMBean; + +/** + * Abstract base class for providing cache statistic via the {@link CacheStatsMBean}. + */ +public abstract class AbstractCacheStats extends AnnotatedStandardMBean implements CacheStatsMBean { + + @Nonnull + private final String name; + + private CacheStats lastSnapshot = + new CacheStats(0, 0, 0, 0, 0, 0); + + /** + * Create a new {@code CacheStatsMBean} for a cache with the given {@code name}. + * @param name + */ + protected AbstractCacheStats(@Nonnull String name) { + super(CacheStatsMBean.class); + 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 + 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(); + } + + @Override + public String cacheInfoAsString() { + return Objects.toStringHelper("CacheStats") + .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", timeInWords(getTotalLoadTime())) + .add("averageLoadPenalty", format("%1.2f ns", getAverageLoadPenalty())) + .add("evictionCount", getEvictionCount()) + .add("elementCount", getElementCount()) + .add("totalWeight", humanReadableByteCount(estimateCurrentWeight())) + .add("maxWeight", humanReadableByteCount(getMaxTotalWeight())) + .toString(); + } + + 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)) + ); + } + +} Index: oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheStats.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheStats.java (date 1488193417000) +++ oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheStats.java (date 1487956431000) @@ -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 cache; private final Weigher 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 @@ * @param maxWeight the maximum weight */ @SuppressWarnings("unchecked") - public CacheStats(Cache cache, String name, - Weigher weigher, long maxWeight) { - super(CacheStatsMBean.class); - this.cache = (Cache) cache; - this.name = name; + public CacheStats( + @Nonnull Cache cache, + @Nonnull String name, + @Nullable Weigher weigher, + long maxWeight) { + super(name); + this.cache = (Cache) checkNotNull(cache); this.weigher = (Weigher) 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 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)) - ); - } } Index: oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordCacheStats.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordCacheStats.java (date 1488193417000) +++ oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordCacheStats.java (date 1487956431000) @@ -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 stats; @@ -50,92 +41,20 @@ @Nonnull private final Supplier weight; - private CacheStats lastSnapshot; - - public RecordCacheStats(@Nonnull String name, + public RecordCacheStats( + @Nonnull String name, @Nonnull Supplier stats, - @Nonnull Supplier elementCount, @Nonnull Supplier weight) { - super(CacheStatsMBean.class); - this.name = checkNotNull(name); + @Nonnull Supplier elementCount, + @Nonnull Supplier 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 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(); - } - }