ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject [3/7] incubator-ignite git commit: IGNITE-49 Implemented MXBean
Date Fri, 16 Jan 2015 11:29:18 GMT
IGNITE-49 Implemented MXBean


Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/31733531
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/31733531
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/31733531

Branch: refs/heads/ignite-49
Commit: 31733531e1324b4419e072bdfc6567aa13992b6c
Parents: d8ecf41
Author: nikolay_tikhonov <ntikhonov@gridgain.com>
Authored: Mon Jan 12 12:05:06 2015 +0300
Committer: nikolay_tikhonov <ntikhonov@gridgain.com>
Committed: Fri Jan 16 14:13:34 2015 +0300

----------------------------------------------------------------------
 .../org/apache/ignite/IgniteCacheManager.java   | 119 ++++++++++++++++++-
 .../ignite/cache/CacheConfigurationMXBean.java  |  33 ++---
 .../apache/ignite/cache/CacheMetricsMXBean.java |  94 +++++++++++++++
 .../cache/GridCacheMetricsAdapter.java          |   1 -
 .../cache/GridCacheAbstractMetricsSelfTest.java |  80 +++++++++++--
 5 files changed, 301 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/31733531/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java b/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
index fa4530b..dbe6838 100644
--- a/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
+++ b/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
@@ -316,14 +316,127 @@ public class IgniteCacheManager implements CacheManager {
         }
     }
 
+
     /** {@inheritDoc} */
     @Override public void enableStatistics(String cacheName, boolean enabled) {
-        ensureNotClosed();
+        Ignite ignite = findIgnite(cacheName);
+
+        GridCache cache = ignite.cache(cacheName);
+
+        GridCacheConfiguration configuration = ignite.jcache(cacheName).getConfiguration(GridCacheConfiguration.class);
+
+        MBeanServer mBeanSrv = ignite.configuration().getMBeanServer();
 
         if (cacheName == null)
-            throw new NullPointerException();
+            cacheName = "null";
+
+        if (enabled) {
+            CacheMetricsMXBean mxBean = new CacheMetricsMXBean(cache);
+
+            registerCacheObject(mBeanSrv, mxBean, uri.toString(), cacheName, true);
+
+            configuration.setStatisticsEnabled(true);
+        }
+        else {
+            unregisterCacheObject(mBeanSrv, uri.toString(), cacheName, true);
+
+            configuration.setStatisticsEnabled(false);
+        }
+    }
+
+    /**
+     *
+     */
+    private static String mbeanSafe(String string) {
+        return string == null ? "" : string.replaceAll(",|:|=|\n", ".");
+    }
+
+    /**
+     * Finds node which has the cache.
+     *
+     * @param cacheName Cache name.
+     * @return Ignite node.
+     */
+    private Ignite findIgnite(String cacheName) {
+        for (Ignite ignite : Ignition.allGrids())
+            if (ignite.jcache(cacheName) != null)
+                return ignite;
+
+        return null;
+    }
+
+    /**
+     * @param mxbean MXBean.
+     * @param cacheManagerName name generated by URI and classloader.
+     * @param name cache name.
+     */
+    public static void registerCacheObject(MBeanServer mBeanServer, Object mxbean, String
cacheManagerName, String name,
+                                           boolean stats) {
+        ObjectName registeredObjectName = calculateObjectName(cacheManagerName, name, stats);
 
-        throw new UnsupportedOperationException();
+        try {
+            if (!isRegistered(mBeanServer, registeredObjectName)) {
+                mBeanServer.registerMBean(mxbean, registeredObjectName);
+
+                System.out.println("Registered mbean with name " + registeredObjectName.toString());
+            }
+        }
+        catch (Exception e) {
+            throw new CacheException("Error registering cache MXBeans for CacheManager "
+ registeredObjectName
+                    + " . Error was " + e.getMessage(), e);
+        }
+    }
+
+    /**
+     * @return {@code True} if MBean registered.
+     */
+    private static boolean isRegistered(MBeanServer mBeanServer, ObjectName objectName) {
+        return !mBeanServer.queryNames(objectName, null).isEmpty();
+    }
+
+    /**
+     * UnRegisters the mxbean if registered already.
+     *
+     * @param mBeanSrv MBean server
+     * @param cacheManagerName name generated by URI and classloader.
+     * @param name cache name.
+     * @param stats is mxbean, a statistics mxbean.
+     */
+    public static void unregisterCacheObject(MBeanServer mBeanSrv, String cacheManagerName,
String name, boolean stats) {
+        Set<ObjectName> registeredObjectNames = null;
+
+        ObjectName objectName = calculateObjectName(cacheManagerName, name, stats);
+        registeredObjectNames = mBeanSrv.queryNames(objectName, null);
+
+        System.out.println("Try UnRegistered mbean with name " + objectName.toString());
+
+        //should just be one
+        for (ObjectName registeredObjectName : registeredObjectNames) {
+            try {
+                mBeanSrv.unregisterMBean(registeredObjectName);
+            } catch (Exception e) {
+                throw new CacheException("Error unregistering object instance " + registeredObjectName
+                        + " . Error was " + e.getMessage(), e);
+            }
+        }
+    }
+
+    /**
+     * Creates an object name.
+     */
+    private static ObjectName calculateObjectName(String cacheManagerName, String name, boolean
stats) {
+        String cacheManagerNameSafe = mbeanSafe(cacheManagerName);
+        String cacheName = mbeanSafe(name);
+
+        try {
+            String objectNameType = stats ? "Statistics" : "Configuration";
+            return new ObjectName(
+                    "javax.cache:type=Cache" + objectNameType + ",CacheManager=" + cacheManagerNameSafe
+ ",Cache=" + cacheName);
+        } catch (MalformedObjectNameException e) {
+            throw new CacheException(
+                    "Illegal ObjectName for Management Bean. " + "CacheManager=[" + cacheManagerNameSafe
+ "], Cache=["
+                            + cacheName + "]", e);
+        }
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/31733531/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java
b/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java
index 2f04cc8..070780a 100644
--- a/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java
+++ b/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java
@@ -9,60 +9,63 @@
 
 package org.apache.ignite.cache;
 
-import javax.cache.*;
-import javax.cache.configuration.*;
+import org.gridgain.grid.cache.GridCacheConfiguration;
+
 import javax.cache.management.*;
 
 /**
- * TODO: Add class description.
+ * Implementation of {@link CacheMXBean}.
+ *
+ * It's a simple wrapper around {@link GridCacheConfiguration} for readonly
+ * access to cache configuration.
  */
-public class CacheConfigurationMXBean<K, V> implements CacheMXBean {
+public class CacheConfigurationMXBean implements CacheMXBean {
     /**
      *
      */
-    private final Cache<K, V> cache;
+    private final GridCacheConfiguration cacheCfg;
 
     /**
      * Constructor.
      *
-     * @param cache The cache.
+     * @param cacheCfg The cache configuration.
      */
-    public CacheConfigurationMXBean(Cache<K, V> cache) {
-        this.cache = cache;
+    public CacheConfigurationMXBean(GridCacheConfiguration cacheCfg) {
+        this.cacheCfg = cacheCfg;
     }
 
     /** {@inheritDoc} */
     @Override public String getKeyType() {
-        return cache.getConfiguration(CompleteConfiguration.class).getKeyType().getName();
+        return cacheCfg.getKeyType().getName();
     }
 
     /** {@inheritDoc} */
     @Override public String getValueType() {
-        return cache.getConfiguration(CompleteConfiguration.class).getValueType().getName();
+        return cacheCfg.getValueType().getName();
     }
 
     /** {@inheritDoc} */
     @Override public boolean isReadThrough() {
-        return cache.getConfiguration(CompleteConfiguration.class).isReadThrough();
+        return cacheCfg.isReadThrough();
     }
 
     /** {@inheritDoc} */
     @Override public boolean isWriteThrough() {
-        return cache.getConfiguration(CompleteConfiguration.class).isWriteThrough();
+        return cacheCfg.isWriteThrough();
     }
 
     /** {@inheritDoc} */
     @Override public boolean isStoreByValue() {
-        return cache.getConfiguration(Configuration.class).isStoreByValue();
+        return cacheCfg.isStoreByValue();
     }
 
     /** {@inheritDoc} */
     @Override public boolean isStatisticsEnabled() {
-        return cache.getConfiguration(CompleteConfiguration.class).isStatisticsEnabled();
+        return cacheCfg.isStatisticsEnabled();
     }
 
     /** {@inheritDoc} */
     @Override public boolean isManagementEnabled() {
-        return cache.getConfiguration(CompleteConfiguration.class).isManagementEnabled();
+        return cacheCfg.isManagementEnabled();
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/31733531/modules/core/src/main/java/org/apache/ignite/cache/CacheMetricsMXBean.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/CacheMetricsMXBean.java b/modules/core/src/main/java/org/apache/ignite/cache/CacheMetricsMXBean.java
new file mode 100644
index 0000000..92fa80e
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/cache/CacheMetricsMXBean.java
@@ -0,0 +1,94 @@
+/* @java.file.header */
+
+/*  _________        _____ __________________        _____
+ *  __  ____/___________(_)______  /__  ____/______ ____(_)_______
+ *  _  / __  __  ___/__  / _  __  / _  / __  _  __ `/__  / __  __ \
+ *  / /_/ /  _  /    _  /  / /_/ /  / /_/ /  / /_/ / _  /  _  / / /
+ *  \____/   /_/     /_/   \_,__/   \____/   \__,_/  /_/   /_/ /_/
+ */
+
+package org.apache.ignite.cache;
+
+import org.gridgain.grid.cache.GridCache;
+
+import javax.cache.management.*;
+
+/**
+ * Implementation of {@link CacheStatisticsMXBean}.
+ */
+public class CacheMetricsMXBean implements CacheStatisticsMXBean {
+
+    /**
+     * Grid cache.
+     */
+    private final GridCache cache;
+
+    /**
+     * Constructor.
+     *
+     * @param cache GridCache
+     */
+    public CacheMetricsMXBean(GridCache cache) {
+        this.cache = cache;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void clear() {
+        cache.resetMetrics();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCacheHits() {
+        return cache.metrics().hits();
+    }
+
+    /** {@inheritDoc} */
+    @Override public float getCacheHitPercentage() {
+        return cache.metrics().getCacheHitPercentage();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCacheMisses() {
+        return cache.metrics().getCacheMisses();
+    }
+
+    /** {@inheritDoc} */
+    @Override public float getCacheMissPercentage() {
+        return cache.metrics().getCacheMissPercentage();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCacheGets() {
+        return cache.metrics().getCacheGets();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCachePuts() {
+        return cache.metrics().getCachePuts();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCacheRemovals() {
+        return cache.metrics().getCacheRemovals();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCacheEvictions() {
+        return cache.metrics().getCacheEvictions();
+    }
+
+    /** {@inheritDoc} */
+    @Override public float getAverageGetTime() {
+        return cache.metrics().getAverageGetTime();
+    }
+
+    /** {@inheritDoc} */
+    @Override public float getAveragePutTime() {
+        return cache.metrics().getAveragePutTime();
+    }
+
+    /** {@inheritDoc} */
+    @Override public float getAverageRemoveTime() {
+        return cache.metrics().getAverageRemoveTime();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/31733531/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
index a878972..ae539d5 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
@@ -20,7 +20,6 @@ package org.gridgain.grid.kernal.processors.cache;
 import org.gridgain.grid.cache.*;
 import org.gridgain.grid.util.tostring.*;
 import org.gridgain.grid.util.typedef.internal.*;
-import org.jdk8.backport.*;
 import org.jetbrains.annotations.*;
 
 import java.io.*;

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/31733531/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
index 82aa122..74e6068 100644
--- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
+++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
@@ -25,6 +25,8 @@ import org.gridgain.grid.util.typedef.internal.*;
 import org.gridgain.testframework.*;
 
 import javax.cache.expiry.*;
+import javax.management.*;
+import java.net.*;
 import java.util.*;
 
 import static java.util.concurrent.TimeUnit.*;
@@ -114,11 +116,11 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
 
         float times = (System.nanoTime() - start) * 1.f / 1000;
 
-        float averageRemoveTime = cache.metrics().getAverageRemoveTime();
+        float avgRmvTime = cache.metrics().getAverageRemoveTime();
 
-        assert averageRemoveTime > 0;
+        assert avgRmvTime > 0;
 
-        assertEquals(times, averageRemoveTime, times / 10);
+        assertEquals(times, avgRmvTime, times / 10);
 
         jcache.remove(2);
 
@@ -188,6 +190,71 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
     /**
      * @throws Exception If failed.
      */
+    public void testStatisticMXBean() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+
+        URI uri = URI.create("ignite://default");
+
+        IgniteCacheManager cacheManager = new IgniteCacheManager(uri);
+
+        cacheManager.enableStatistics(null, true);
+
+        ObjectName objectName =
+            new ObjectName("javax.cache:type=CacheStatistics,CacheManager=ignite.//default,Cache=null");
+
+        long cachePuts = (long)getConfiguration().getMBeanServer().getAttribute(objectName,
"CachePuts");
+
+        assertEquals(0L, cachePuts);
+
+        jcache.put(1, 1);
+
+        cachePuts = (long)getConfiguration().getMBeanServer().getAttribute(objectName, "CachePuts");
+
+        assertEquals(1, cachePuts);
+
+        cacheManager.enableStatistics(null, false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testCacheMxBean() throws Exception {
+        URI uri = URI.create("ignite://default");
+
+        IgniteCacheManager cacheManager = new IgniteCacheManager(uri);
+
+        cacheManager.enableManagement(null, true);
+
+        ObjectName objectName =
+            new ObjectName("javax.cache:type=CacheConfiguration,CacheManager=ignite.//default,Cache=null");
+
+        String keyType = (String)getConfiguration().getMBeanServer().getAttribute(objectName,
"KeyType");
+        assertEquals("java.lang.Object", keyType);
+
+        String valueType = (String)getConfiguration().getMBeanServer().getAttribute(objectName,
"ValueType");
+        assertEquals("java.lang.Object", valueType);
+
+        boolean readThrough = (boolean)getConfiguration().getMBeanServer().getAttribute(objectName,
"ReadThrough");
+        assertEquals(false, readThrough);
+
+        boolean writeThrough = (boolean)getConfiguration().getMBeanServer().getAttribute(objectName,
"WriteThrough");
+        assertEquals(false, writeThrough);
+
+        boolean storeByValue = (boolean)getConfiguration().getMBeanServer().getAttribute(objectName,
"StoreByValue");
+        assertEquals(true, storeByValue);
+
+        boolean isStat = (boolean)getConfiguration().getMBeanServer().getAttribute(objectName,
"StatisticsEnabled");
+        assertEquals(true, isStat);
+
+        boolean isMng = (boolean)getConfiguration().getMBeanServer().getAttribute(objectName,
"ManagementEnabled");
+        assertEquals(true, isMng);
+
+        cacheManager.enableManagement(null, false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
     public void testGetAllAvgTime() throws Exception {
         IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
         GridCache<Object, Object> cache = grid(0).cache(null);
@@ -233,12 +300,12 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
 
         float times = (System.nanoTime() - start) * 1.f / 1000;
 
-        float averagePutTime = cache.metrics().getAveragePutTime();
+        float avgPutTime = cache.metrics().getAveragePutTime();
 
-        assert averagePutTime > 0;
+        assert avgPutTime > 0;
 
         assertEquals(1, cache.metrics().writes());
-        assertEquals(times, averagePutTime, times / 3);
+        assertEquals(times, avgPutTime, times / 3);
 
         jcache.put(2, 2);
 
@@ -333,7 +400,6 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
         assertEquals(expMisses, misses);
     }
 
-
     /**
      * @throws Exception If failed.
      */


Mime
View raw message