[infinispan-commits] Infinispan SVN: r1315 - in trunk/core/src: main/java/org/infinispan/interceptors and 2 other directories.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Mon Dec 21 14:10:55 EST 2009
Author: galder.zamarreno at jboss.com
Date: 2009-12-21 14:10:54 -0500 (Mon, 21 Dec 2009)
New Revision: 1315
Added:
trunk/core/src/main/java/org/infinispan/stats/
trunk/core/src/main/java/org/infinispan/stats/Stats.java
trunk/core/src/main/java/org/infinispan/stats/StatsImpl.java
Modified:
trunk/core/src/main/java/org/infinispan/AdvancedCache.java
trunk/core/src/main/java/org/infinispan/CacheDelegate.java
trunk/core/src/main/java/org/infinispan/interceptors/CacheMgmtInterceptor.java
trunk/core/src/test/java/org/infinispan/jmx/CacheMgmtInterceptorMBeanTest.java
Log:
[ISPN-319] (Provide Stats via AdvancedCache interface) Done.
Modified: trunk/core/src/main/java/org/infinispan/AdvancedCache.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/AdvancedCache.java 2009-12-21 16:08:35 UTC (rev 1314)
+++ trunk/core/src/main/java/org/infinispan/AdvancedCache.java 2009-12-21 19:10:54 UTC (rev 1315)
@@ -8,6 +8,7 @@
import org.infinispan.factories.ComponentRegistry;
import org.infinispan.interceptors.base.CommandInterceptor;
import org.infinispan.remoting.rpc.RpcManager;
+import org.infinispan.stats.Stats;
import javax.transaction.TransactionManager;
import java.util.Collection;
@@ -121,4 +122,6 @@
DataContainer getDataContainer();
TransactionManager getTransactionManager();
+
+ Stats getStats();
}
Modified: trunk/core/src/main/java/org/infinispan/CacheDelegate.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/CacheDelegate.java 2009-12-21 16:08:35 UTC (rev 1314)
+++ trunk/core/src/main/java/org/infinispan/CacheDelegate.java 2009-12-21 19:10:54 UTC (rev 1315)
@@ -60,6 +60,8 @@
import org.infinispan.remoting.responses.ResponseGenerator;
import org.infinispan.remoting.rpc.RpcManager;
import org.infinispan.statetransfer.StateTransferManager;
+import org.infinispan.stats.Stats;
+import org.infinispan.stats.StatsImpl;
import org.infinispan.util.concurrent.FutureListener;
import org.infinispan.util.concurrent.NotifyingFuture;
import org.infinispan.util.logging.Log;
@@ -412,6 +414,10 @@
return cacheManager;
}
+ public Stats getStats() {
+ return new StatsImpl(invoker);
+ }
+
@SuppressWarnings("unchecked")
public final V put(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdleTime, TimeUnit idleTimeUnit) {
InvocationContext ctx = getInvocationContext();
Modified: trunk/core/src/main/java/org/infinispan/interceptors/CacheMgmtInterceptor.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/interceptors/CacheMgmtInterceptor.java 2009-12-21 16:08:35 UTC (rev 1314)
+++ trunk/core/src/main/java/org/infinispan/interceptors/CacheMgmtInterceptor.java 2009-12-21 19:10:54 UTC (rev 1315)
@@ -25,6 +25,7 @@
import org.infinispan.commands.write.EvictCommand;
import org.infinispan.commands.write.PutKeyValueCommand;
import org.infinispan.commands.write.PutMapCommand;
+import org.infinispan.commands.write.RemoveCommand;
import org.infinispan.container.DataContainer;
import org.infinispan.context.InvocationContext;
import org.infinispan.factories.annotations.Inject;
@@ -59,6 +60,8 @@
private AtomicLong evictions = new AtomicLong(0);
private AtomicLong start = new AtomicLong(System.currentTimeMillis());
private AtomicLong reset = new AtomicLong(start.get());
+ private AtomicLong removeHits = new AtomicLong(0);
+ private AtomicLong removeMisses = new AtomicLong(0);
private DataContainer dataContainer;
@@ -114,6 +117,17 @@
return retval;
}
+ @Override
+ public Object visitRemoveCommand(InvocationContext ctx, RemoveCommand command) throws Throwable {
+ Object retval = invokeNextInterceptor(ctx, command);
+ if (retval == null) {
+ removeMisses.incrementAndGet();
+ } else {
+ removeHits.incrementAndGet();
+ }
+ return retval;
+ }
+
@ManagedAttribute(description = "Number of cache attribute hits")
@Metric(displayName = "Number of cache hits", measurementType = MeasurementType.TRENDSUP, displayType = DisplayType.SUMMARY)
public long getHits() {
@@ -126,6 +140,18 @@
return misses.get();
}
+ @ManagedAttribute(description = "Number of cache removal hits")
+ @Metric(displayName = "Number of cache removal hits", measurementType = MeasurementType.TRENDSUP, displayType = DisplayType.SUMMARY)
+ public long getRemoveHits() {
+ return removeHits.get();
+ }
+
+ @ManagedAttribute(description = "Number of cache removals where keys were not found")
+ @Metric(displayName = "Number of cache removal misses", measurementType = MeasurementType.TRENDSUP, displayType = DisplayType.SUMMARY)
+ public long getRemoveMisses() {
+ return removeMisses.get();
+ }
+
@ManagedAttribute(description = "number of cache attribute put operations")
@Metric(displayName = "Number of cache puts" , measurementType = MeasurementType.TRENDSUP, displayType = DisplayType.SUMMARY)
public long getStores() {
@@ -172,8 +198,8 @@
return (storeTimes.get()) / stores.get();
}
- @ManagedAttribute(description = "Number of entries in the cache")
- @Metric(displayName = "Number of cache entries", displayType = DisplayType.SUMMARY)
+ @ManagedAttribute(description = "Number of entries currently in the cache")
+ @Metric(displayName = "Number of current cache entries", displayType = DisplayType.SUMMARY)
public int getNumberOfEntries() {
return dataContainer.size();
}
@@ -200,6 +226,8 @@
hitTimes.set(0);
missTimes.set(0);
storeTimes.set(0);
+ removeHits.set(0);
+ removeMisses.set(0);
reset.set(System.currentTimeMillis());
}
}
Added: trunk/core/src/main/java/org/infinispan/stats/Stats.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/stats/Stats.java (rev 0)
+++ trunk/core/src/main/java/org/infinispan/stats/Stats.java 2009-12-21 19:10:54 UTC (rev 1315)
@@ -0,0 +1,82 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and
+ * individual contributors as indicated by the @author tags. See the
+ * copyright.txt file in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.infinispan.stats;
+
+/**
+ * Stats.
+ *
+ * @author Galder Zamarreño
+ * @since 4.0
+ */
+public interface Stats {
+
+ /**
+ * @return Number of seconds since cache started.
+ */
+ long getTimeSinceStart();
+
+ /**
+ * @return Number of entries currently in the cache.
+ */
+ int getCurrentNumberOfEntries();
+
+ /**
+ * @return Number of entries stored in cache .
+ */
+ long getTotalNumberOfEntries();
+
+ /**
+ * @return Number of put operations on the cache.
+ */
+ long getStores();
+
+ /**
+ * @return Number of get operations.
+ */
+ long getRetrievals();
+
+ /**
+ * @return Number of cache get hits.
+ */
+ long getHits();
+
+ /**
+ * @return Number of cache get misses.
+ */
+ long getMisses();
+
+ /**
+ * @return Number of cache removal hits.
+ */
+ long getRemoveHits();
+
+ /**
+ * @return Number of cache removal misses.
+ */
+ long getRemoveMisses();
+
+ /**
+ * @return Number of cache eviction.
+ */
+ long getEvictions();
+}
Added: trunk/core/src/main/java/org/infinispan/stats/StatsImpl.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/stats/StatsImpl.java (rev 0)
+++ trunk/core/src/main/java/org/infinispan/stats/StatsImpl.java 2009-12-21 19:10:54 UTC (rev 1315)
@@ -0,0 +1,120 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and
+ * individual contributors as indicated by the @author tags. See the
+ * copyright.txt file in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.infinispan.stats;
+
+import java.util.List;
+
+import net.jcip.annotations.Immutable;
+
+import org.infinispan.interceptors.CacheMgmtInterceptor;
+import org.infinispan.interceptors.InterceptorChain;
+import org.infinispan.interceptors.base.CommandInterceptor;
+
+/**
+ * StatsImpl.
+ *
+ * @author Galder Zamarreño
+ * @since 4.0
+ */
+ at Immutable
+public class StatsImpl implements Stats {
+ final long timeSinceStart;
+ final int currentNumberOfEntries;
+ final long totalNumberOfEntries;
+ final long retrievals;
+ final long stores;
+ final long hits;
+ final long misses;
+ final long removeHits;
+ final long removeMisses;
+ final long evictions;
+
+ public StatsImpl(InterceptorChain chain) {
+ List<CommandInterceptor> interceptors = chain.getInterceptorsWhichExtend(CacheMgmtInterceptor.class);
+ if (!interceptors.isEmpty()) {
+ CacheMgmtInterceptor mgmtInterceptor = (CacheMgmtInterceptor) interceptors.get(0);
+ timeSinceStart = mgmtInterceptor.getElapsedTime();
+ currentNumberOfEntries = mgmtInterceptor.getNumberOfEntries();
+ totalNumberOfEntries = mgmtInterceptor.getStores();
+ retrievals = mgmtInterceptor.getHits() + mgmtInterceptor.getMisses();
+ stores = mgmtInterceptor.getStores();
+ hits = mgmtInterceptor.getHits();
+ misses = mgmtInterceptor.getMisses();
+ removeHits = mgmtInterceptor.getRemoveHits();
+ removeMisses = mgmtInterceptor.getRemoveMisses();
+ evictions = mgmtInterceptor.getEvictions();
+ } else {
+ timeSinceStart = -1;
+ currentNumberOfEntries = -1;
+ totalNumberOfEntries = -1;
+ retrievals = -1;
+ stores = -1;
+ hits = -1;
+ misses = -1;
+ removeHits = -1;
+ removeMisses = -1;
+ evictions = -1;
+ }
+ }
+
+ public long getTimeSinceStart() {
+ return timeSinceStart;
+ }
+
+ public int getCurrentNumberOfEntries() {
+ return currentNumberOfEntries;
+ }
+
+ public long getTotalNumberOfEntries() {
+ return totalNumberOfEntries;
+ }
+
+ public long getRetrievals() {
+ return retrievals;
+ }
+
+ public long getStores() {
+ return stores;
+ }
+
+ public long getHits() {
+ return hits;
+ }
+
+ public long getMisses() {
+ return misses;
+ }
+
+ public long getRemoveHits() {
+ return removeHits;
+ }
+
+ public long getRemoveMisses() {
+ return removeMisses;
+ }
+
+ public long getEvictions() {
+ return evictions;
+ }
+
+}
Modified: trunk/core/src/test/java/org/infinispan/jmx/CacheMgmtInterceptorMBeanTest.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/jmx/CacheMgmtInterceptorMBeanTest.java 2009-12-21 16:08:35 UTC (rev 1314)
+++ trunk/core/src/test/java/org/infinispan/jmx/CacheMgmtInterceptorMBeanTest.java 2009-12-21 19:10:54 UTC (rev 1315)
@@ -1,5 +1,6 @@
package org.infinispan.jmx;
+import org.infinispan.AdvancedCache;
import org.infinispan.config.Configuration;
import org.infinispan.config.GlobalConfiguration;
import org.infinispan.manager.CacheManager;
@@ -18,10 +19,11 @@
*
* @author Mircea.Markus at jboss.com
*/
- at Test(groups = "jmx.CacheMgmtInterceptorMBeanTest", testName = "jmx.CacheMgmtInterceptorMBeanTest")
+ at Test(groups = "functional", testName = "jmx.CacheMgmtInterceptorMBeanTest")
public class CacheMgmtInterceptorMBeanTest extends SingleCacheManagerTest {
private ObjectName mgmtInterceptor;
private MBeanServer threadMBeanServer;
+ AdvancedCache advanced;
protected CacheManager createCacheManager() throws Exception {
GlobalConfiguration globalConfiguration = GlobalConfiguration.getNonClusteredDefault();
@@ -34,6 +36,7 @@
configuration.setExposeJmxStatistics(true);
cacheManager.defineConfiguration("test", configuration);
cache = cacheManager.getCache("test");
+ advanced = cache.getAdvancedCache();
mgmtInterceptor = new ObjectName("CacheMgmtInterceptorMBeanTest:cache-name=test(local),jmx-resource=Statistics");
threadMBeanServer = PerThreadMBeanServerLookup.getThreadMBeanServer();
@@ -46,56 +49,126 @@
}
public void testEviction() throws Exception {
- assertAttributeValue("Evictions", 0);
+ assertEvictions(0);
cache.put("key", "value");
- assertAttributeValue("Evictions", 0);
+ assertEvictions(0);
cache.evict("key");
- assertAttributeValue("Evictions", 1);
+ assertEvictions(1);
cache.evict("does_not_exist");
- assertAttributeValue("Evictions", 2);
+ assertEvictions(2);
}
public void testGetKeyValue() throws Exception {
- assertAttributeValue("Misses", 0);
- assertAttributeValue("Hits", 0);
+ assertMisses(0);
+ assertHits(0);
+ assert 0 == advanced.getStats().getHits();
assertAttributeValue("HitRatio", 0);
cache.put("key", "value");
- assertAttributeValue("Misses", 0);
- assertAttributeValue("Hits", 0);
+ assertMisses(0);
+ assertHits(0);
assertAttributeValue("HitRatio", 0);
assert cache.get("key").equals("value");
- assertAttributeValue("Misses", 0);
- assertAttributeValue("Hits", 1);
+ assertMisses(0);
+ assertHits(1);
assertAttributeValue("HitRatio", 1);
assert cache.get("key_ne") == null;
assert cache.get("key_ne") == null;
assert cache.get("key_ne") == null;
- assertAttributeValue("Misses", 3);
- assertAttributeValue("Hits", 1);
+ assertMisses(3);
+ assertHits(1);
assertAttributeValue("HitRatio", 0.25f);
}
public void testStores() throws Exception {
- assertAttributeValue("Evictions", 0);
- assertAttributeValue("Stores", 0);
+ assertEvictions(0);
+ assertStores(0);
cache.put("key", "value");
- assertAttributeValue("Stores", 1);
+ assertStores(1);
cache.put("key", "value");
- assertAttributeValue("Stores", 2);
+ assertStores(2);
+ assertCurrentNumberOfEntries(1);
Map toAdd = new HashMap();
toAdd.put("key", "value");
toAdd.put("key2", "value2");
cache.putAll(toAdd);
- assertAttributeValue("Stores", 4);
+ assertStores(4);
+ assertCurrentNumberOfEntries(2);
+
+ resetStats();
+
+ toAdd = new HashMap();
+ toAdd.put("key3", "value3");
+ toAdd.put("key4", "value4");
+ cache.putAll(toAdd);
+ assertStores(2);
+ assertCurrentNumberOfEntries(4);
}
+ public void testRemoves() throws Exception {
+ assertStores(0);
+ assertRemoveHits(0);
+ assertRemoveMisses(0);
+ cache.put("key", "value");
+ cache.put("key2", "value2");
+ cache.put("key3", "value3");
+ assertStores(3);
+ assertRemoveHits(0);
+ assertRemoveMisses(0);
+
+ cache.remove("key");
+ cache.remove("key3");
+ cache.remove("key4");
+ assertRemoveHits(2);
+ assertRemoveMisses(1);
+
+ cache.remove("key2");
+ assertRemoveHits(3);
+ assertRemoveMisses(1);
+ }
+
private void assertAttributeValue(String attrName, float expectedValue) throws Exception {
String receivedVal = threadMBeanServer.getAttribute(mgmtInterceptor, attrName).toString();
assert Float.parseFloat(receivedVal) == expectedValue : "expecting " + expectedValue + " for " + attrName + ", but received " + receivedVal;
}
+
+ private void assertEvictions(float expectedValue) throws Exception {
+ assertAttributeValue("Evictions", expectedValue);
+ assert expectedValue == advanced.getStats().getEvictions();
+ }
+
+ private void assertMisses(float expectedValue) throws Exception {
+ assertAttributeValue("Misses", expectedValue);
+ assert expectedValue == advanced.getStats().getMisses();
+ }
+
+ private void assertHits(float expectedValue) throws Exception {
+ assertAttributeValue("Hits", expectedValue);
+ assert expectedValue == advanced.getStats().getHits();
+ }
+
+ private void assertStores(float expectedValue) throws Exception {
+ assertAttributeValue("Stores", expectedValue);
+ assert expectedValue == advanced.getStats().getStores();
+ }
+
+ private void assertRemoveHits(float expectedValue) throws Exception {
+ assertAttributeValue("RemoveHits", expectedValue);
+ assert expectedValue == advanced.getStats().getRemoveHits();
+ }
+
+ private void assertRemoveMisses(float expectedValue) throws Exception {
+ assertAttributeValue("RemoveMisses", expectedValue);
+ assert expectedValue == advanced.getStats().getRemoveMisses();
+ }
+
+ private void assertCurrentNumberOfEntries(float expectedValue) throws Exception {
+ assertAttributeValue("NumberOfEntries", expectedValue);
+ assert expectedValue == advanced.getStats().getCurrentNumberOfEntries();
+ }
+
}
More information about the infinispan-commits
mailing list