Permalink
Browse files

ISPN-2120 When calculating stats, only count local operations

  • Loading branch information...
1 parent bc7a5c9 commit 2d82f5f774f5f0fa808275fbbd38ed0a54497a39 @galderz galderz committed with Sanne Jun 21, 2012
@@ -94,12 +94,14 @@ public Object visitGetKeyValueCommand(InvocationContext ctx, GetKeyValueCommand
Object retval = invokeNextInterceptor(ctx, command);
long t2 = System.nanoTime();
long intervalMilliseconds = nanosecondsIntervalToMilliseconds(t1, t2);
- if (retval == null) {
- missTimes.getAndAdd(intervalMilliseconds);
- misses.incrementAndGet();
- } else {
- hitTimes.getAndAdd(intervalMilliseconds);
- hits.incrementAndGet();
+ if (ctx.isOriginLocal()) {
+ if (retval == null) {
+ missTimes.getAndAdd(intervalMilliseconds);
+ misses.incrementAndGet();
+ } else {
+ hitTimes.getAndAdd(intervalMilliseconds);
+ hits.incrementAndGet();
+ }
}
return retval;
}
@@ -111,7 +113,7 @@ public Object visitPutMapCommand(InvocationContext ctx, PutMapCommand command) t
Object retval = invokeNextInterceptor(ctx, command);
long t2 = System.nanoTime();
long intervalMilliseconds = nanosecondsIntervalToMilliseconds(t1, t2);
- if (data != null && !data.isEmpty()) {
+ if (ctx.isOriginLocal() && data != null && !data.isEmpty()) {
storeTimes.getAndAdd(intervalMilliseconds);
stores.getAndAdd(data.size());
}
@@ -123,7 +125,7 @@ public Object visitPutMapCommand(InvocationContext ctx, PutMapCommand command) t
public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable {
long t1 = System.nanoTime();
Object retval = invokeNextInterceptor(ctx, command);
- if (command.isSuccessful()) {
+ if (ctx.isOriginLocal() && command.isSuccessful()) {
long t2 = System.nanoTime();
long intervalMilliseconds = nanosecondsIntervalToMilliseconds(t1, t2);
storeTimes.getAndAdd(intervalMilliseconds);
@@ -135,10 +137,12 @@ public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand
@Override
public Object visitRemoveCommand(InvocationContext ctx, RemoveCommand command) throws Throwable {
Object retval = invokeNextInterceptor(ctx, command);
- if (retval == null) {
- removeMisses.incrementAndGet();
- } else {
- removeHits.incrementAndGet();
+ if (ctx.isOriginLocal()) {
+ if (retval == null) {
+ removeMisses.incrementAndGet();
+ } else {
+ removeHits.incrementAndGet();
+ }
}
return retval;
}
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2012 Red Hat, Inc. and/or its affiliates.
+ *
+ * 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+package org.infinispan.jmx;
+
+import org.infinispan.Cache;
+import org.infinispan.configuration.cache.CacheMode;
+import org.infinispan.configuration.cache.ConfigurationBuilder;
+import org.infinispan.manager.EmbeddedCacheManager;
+import org.infinispan.test.MultipleCacheManagersTest;
+import org.infinispan.test.fwk.TestCacheManagerFactory;
+import org.testng.annotations.Test;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.infinispan.test.TestingUtil.*;
+import static org.testng.AssertJUnit.assertEquals;
+
+/**
+ * // TODO: Document this
+ *
+ * @author Galder Zamarreño
+ * @since 5.2
+ */
+@Test(groups = "functional", testName = "jmx.ClusteredCacheMgmtInterceptorMBeanTest")
+public class ClusteredCacheMgmtInterceptorMBeanTest extends MultipleCacheManagersTest {
+
+ private static final String JMX_1 = ClusteredCacheMgmtInterceptorMBeanTest.class.getSimpleName() + "-1";
+ private static final String JMX_2 = ClusteredCacheMgmtInterceptorMBeanTest.class.getSimpleName() + "-2";
+
+ @Override
+ protected void createCacheManagers() throws Throwable {
+ ConfigurationBuilder builder = getDefaultClusteredCacheConfig(
+ CacheMode.REPL_SYNC, false);
+ builder.jmxStatistics().enable();
+
+ EmbeddedCacheManager cm1 = TestCacheManagerFactory
+ .createClusteredCacheManagerEnforceJmxDomain(JMX_1, builder);
+ EmbeddedCacheManager cm2 = TestCacheManagerFactory
+ .createClusteredCacheManagerEnforceJmxDomain(JMX_2, builder);
+ registerCacheManager(cm1, cm2);
+ }
+
+ public void testCorrectStatsInCluster() throws Exception {
+ Cache<String, String> cache1 = cache(0);
+ Cache<String, String> cache2 = cache(1);
+ cache1.put("k", "v");
+ assertEquals("v", cache2.get("k"));
+ ObjectName stats1 = getCacheObjectName(JMX_1, "___defaultcache(repl_sync)", "Statistics");
+ ObjectName stats2 = getCacheObjectName(JMX_2, "___defaultcache(repl_sync)", "Statistics");
+
+ MBeanServer mBeanServer = PerThreadMBeanServerLookup.getThreadMBeanServer();
+ assertEquals((long) 1, mBeanServer.getAttribute(stats1, "Stores"));
+ assertEquals((long) 0, mBeanServer.getAttribute(stats2, "Stores"));
+
+ Map<String, String> values = new HashMap<String, String>();
+ values.put("k1", "v1");
+ values.put("k2", "v2");
+ values.put("k3", "v3");
+ cache2.putAll(values);
+
+ assertEquals((long) 1, mBeanServer.getAttribute(stats1, "Stores"));
+ assertEquals((long) 3, mBeanServer.getAttribute(stats2, "Stores"));
+
+ cache1.remove("k");
+
+ assertEquals((long) 1, mBeanServer.getAttribute(stats1, "RemoveHits"));
+ assertEquals((long) 0, mBeanServer.getAttribute(stats2, "RemoveHits"));
+ }
+
+}
@@ -369,6 +369,7 @@ protected Transaction tx(int i) {
return caches;
}
+ @Deprecated
protected <K, V> List<Cache<K, V>> createClusteredCaches(int numMembersInCluster, Configuration defaultConfig) {
List<Cache<K, V>> caches = new ArrayList<Cache<K, V>>(numMembersInCluster);
for (int i = 0; i < numMembersInCluster; i++) {
@@ -381,6 +382,18 @@ protected Transaction tx(int i) {
return caches;
}
+ protected <K, V> List<Cache<K, V>> createClusteredCaches(int numMembersInCluster, ConfigurationBuilder builder) {
+ List<Cache<K, V>> caches = new ArrayList<Cache<K, V>>(numMembersInCluster);
+ for (int i = 0; i < numMembersInCluster; i++) {
+ EmbeddedCacheManager cm = addClusterEnabledCacheManager(builder);
+ Cache<K, V> cache = cm.getCache();
+ caches.add(cache);
+
+ }
+ waitForClusterToForm();
+ return caches;
+ }
+
protected ReplListener replListener(Cache cache) {
ReplListener listener = listeners.get(cache);
if (listener == null) {
@@ -244,6 +244,10 @@ public static EmbeddedCacheManager createCacheManager(GlobalConfigurationBuilder
return newDefaultCacheManager(true, globalBuilder, builder, false);
}
+ public static EmbeddedCacheManager createCacheManager(GlobalConfigurationBuilder globalBuilder, ConfigurationBuilder builder, boolean keepJmxDomain) {
+ return newDefaultCacheManager(true, globalBuilder, builder, keepJmxDomain);
+ }
+
/**
* Creates a cache manager and amends the supplied configuration in order to avoid conflicts (e.g. jmx, jgroups)
* during running tests in parallel.
@@ -337,6 +341,20 @@ public static EmbeddedCacheManager createCacheManagerEnforceJmxDomain(String jmx
return createCacheManagerEnforceJmxDomain(jmxDomain, true, true);
}
+ public static EmbeddedCacheManager createClusteredCacheManagerEnforceJmxDomain(String jmxDomain, ConfigurationBuilder builder) {
+ return createClusteredCacheManagerEnforceJmxDomain(jmxDomain, true, builder);
+ }
+
+ public static EmbeddedCacheManager createClusteredCacheManagerEnforceJmxDomain(String jmxDomain, boolean exposeGlobalJmx, ConfigurationBuilder builder) {
+ GlobalConfigurationBuilder globalBuilder = GlobalConfigurationBuilder.defaultClusteredBuilder();
+ amendGlobalConfiguration(globalBuilder, new TransportFlags());
+ globalBuilder.globalJmxStatistics()
+ .jmxDomain(jmxDomain)
+ .mBeanServerLookup(new PerThreadMBeanServerLookup())
+ .enabled(exposeGlobalJmx);
+ return createCacheManager(globalBuilder, builder, true);
+ }
+
/**
* @see #createCacheManagerEnforceJmxDomain(String)
*/

0 comments on commit 2d82f5f

Please sign in to comment.