Repository: ignite
Updated Branches:
  refs/heads/master 6dc473d79 -> 31e88fc32


IGNITE-8776 Eviction policy MBeans are never registered if 
evictionPolicyFactory is used - Fixes #4300.

Signed-off-by: Dmitriy Pavlov <[email protected]>


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

Branch: refs/heads/master
Commit: 31e88fc32725aebfc52e705c0b7c085fec510f66
Parents: 6dc473d
Author: kcmvp <[email protected]>
Authored: Thu Jul 12 18:59:41 2018 +0300
Committer: Dmitriy Pavlov <[email protected]>
Committed: Thu Jul 12 18:59:41 2018 +0300

----------------------------------------------------------------------
 .../cache/GridCacheEvictionManager.java         | 128 +++++++++++++++++++
 .../processors/cache/GridCacheProcessor.java    |  18 ---
 .../cache/GridEvictionPolicyMBeansTest.java     | 117 +++++++++++++++++
 .../IgniteCacheMetricsSelfTestSuite.java        |   3 +
 4 files changed, 248 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/31e88fc3/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheEvictionManager.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheEvictionManager.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheEvictionManager.java
index 2c9dec7..6caf3e4 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheEvictionManager.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheEvictionManager.java
@@ -18,6 +18,7 @@
 package org.apache.ignite.internal.processors.cache;
 
 import java.util.Collection;
+import javax.management.MBeanServer;
 import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.cache.eviction.EvictionFilter;
 import org.apache.ignite.cache.eviction.EvictionPolicy;
@@ -30,6 +31,7 @@ import org.apache.ignite.internal.util.GridBusyLock;
 import org.apache.ignite.internal.util.typedef.X;
 import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.lang.IgniteUuid;
+import org.apache.ignite.mxbean.IgniteMBeanAware;
 import org.jetbrains.annotations.Nullable;
 
 import static org.apache.ignite.events.EventType.EVT_CACHE_ENTRY_EVICTED;
@@ -72,6 +74,9 @@ public class GridCacheEvictionManager extends 
GridCacheManagerAdapter implements
 
         plcEnabled = plc != null;
 
+        if (plcEnabled)
+            prepare(cfg, plc, cctx.isNear());
+
         filter = cfg.getEvictionFilter();
 
         if (log.isDebugEnabled())
@@ -311,4 +316,127 @@ public class GridCacheEvictionManager extends 
GridCacheManagerAdapter implements
     public EvictionPolicy getEvictionPolicy() {
         return plc;
     }
+
+    /**
+     * Performs injections and MBean registration.
+     *
+     * @param cfg Cache configuration.
+     * @param rsrc Resource.
+     * @param near Near flag.
+     * @throws IgniteCheckedException If failed.
+     */
+    private void prepare(CacheConfiguration cfg, @Nullable Object rsrc, 
boolean near) throws IgniteCheckedException {
+        cctx.kernalContext().resource().injectGeneric(rsrc);
+
+        cctx.kernalContext().resource().injectCacheName(rsrc, cfg.getName());
+
+        registerMbean(rsrc, cfg.getName(), near);
+    }
+
+    /**
+     * Registers MBean for cache components.
+     *
+     * @param obj Cache component.
+     * @param cacheName Cache name.
+     * @param near Near flag.
+     * @throws IgniteCheckedException If registration failed.
+     */
+    @SuppressWarnings("unchecked")
+    private void registerMbean(Object obj, @Nullable String cacheName, boolean 
near)
+        throws IgniteCheckedException {
+        if (U.IGNITE_MBEANS_DISABLED)
+            return;
+
+        assert obj != null;
+
+        MBeanServer srvr = cctx.kernalContext().config().getMBeanServer();
+
+        assert srvr != null;
+
+        cacheName = U.maskName(cacheName);
+
+        cacheName = near ? cacheName + "-near" : cacheName;
+
+        final Object mbeanImpl = (obj instanceof IgniteMBeanAware) ? 
((IgniteMBeanAware)obj).getMBean() : obj;
+
+        for (Class<?> itf : mbeanImpl.getClass().getInterfaces()) {
+            if (itf.getName().endsWith("MBean") || 
itf.getName().endsWith("MXBean")) {
+                try {
+                    U.registerMBean(srvr, 
cctx.kernalContext().igniteInstanceName(), cacheName, obj.getClass().getName(),
+                        mbeanImpl, (Class<Object>)itf);
+                }
+                catch (Throwable e) {
+                    throw new IgniteCheckedException("Failed to register MBean 
for component: " + obj, e);
+                }
+
+                break;
+            }
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void stop0(boolean cancel, boolean destroy) {
+        cleanup(cctx.config(), plc, cctx.isNear());
+    }
+
+    /**
+     * @param cfg Cache configuration.
+     * @param rsrc Resource.
+     * @param near Near flag.
+     */
+    void cleanup(CacheConfiguration cfg, @Nullable Object rsrc, boolean near) {
+        if (rsrc != null) {
+            unregisterMbean(rsrc, cfg.getName(), near);
+
+            try {
+                cctx.kernalContext().resource().cleanupGeneric(rsrc);
+            }
+            catch (IgniteCheckedException e) {
+                U.error(log, "Failed to cleanup resource: " + rsrc, e);
+            }
+        }
+    }
+
+    /**
+     * Unregisters MBean for cache components.
+     *
+     * @param o Cache component.
+     * @param cacheName Cache name.
+     * @param near Near flag.
+     */
+    private void unregisterMbean(Object o, @Nullable String cacheName, boolean 
near) {
+        if (U.IGNITE_MBEANS_DISABLED)
+            return;
+
+        assert o != null;
+
+        MBeanServer srvr = cctx.kernalContext().config().getMBeanServer();
+
+        assert srvr != null;
+
+        cacheName = U.maskName(cacheName);
+
+        cacheName = near ? cacheName + "-near" : cacheName;
+
+        boolean needToUnregister = o instanceof IgniteMBeanAware;
+
+        if (!needToUnregister) {
+            for (Class<?> itf : o.getClass().getInterfaces()) {
+                if (itf.getName().endsWith("MBean") || 
itf.getName().endsWith("MXBean")) {
+                    needToUnregister = true;
+
+                    break;
+                }
+            }
+        }
+
+        if (needToUnregister) {
+            try {
+                
srvr.unregisterMBean(U.makeMBeanName(cctx.kernalContext().igniteInstanceName(), 
cacheName, o.getClass().getName()));
+            }
+            catch (Throwable e) {
+                U.error(log, "Failed to unregister MBean for component: " + o, 
e);
+            }
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/31e88fc3/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
index 99bb4cb..26f1887 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
@@ -572,20 +572,11 @@ public class GridCacheProcessor extends 
GridProcessorAdapter {
      * @throws IgniteCheckedException If failed to inject.
      */
     private void prepare(CacheConfiguration cfg, Collection<Object> objs) 
throws IgniteCheckedException {
-        prepare(cfg, cfg.getEvictionPolicyFactory(), false);
-        prepare(cfg, cfg.getEvictionPolicy(), false);
         prepare(cfg, cfg.getAffinity(), false);
         prepare(cfg, cfg.getAffinityMapper(), false);
         prepare(cfg, cfg.getEvictionFilter(), false);
         prepare(cfg, cfg.getInterceptor(), false);
 
-        NearCacheConfiguration nearCfg = cfg.getNearConfiguration();
-
-        if (nearCfg != null) {
-            prepare(cfg, nearCfg.getNearEvictionPolicyFactory(), true);
-            prepare(cfg, nearCfg.getNearEvictionPolicy(), true);
-        }
-
         for (Object obj : objs)
             prepare(cfg, obj, false);
     }
@@ -612,8 +603,6 @@ public class GridCacheProcessor extends 
GridProcessorAdapter {
     private void cleanup(GridCacheContext cctx) {
         CacheConfiguration cfg = cctx.config();
 
-        cleanup(cfg, cfg.getEvictionPolicyFactory(), false);
-        cleanup(cfg, cfg.getEvictionPolicy(), false);
         cleanup(cfg, cfg.getAffinity(), false);
         cleanup(cfg, cfg.getAffinityMapper(), false);
         cleanup(cfg, cfg.getEvictionFilter(), false);
@@ -625,13 +614,6 @@ public class GridCacheProcessor extends 
GridProcessorAdapter {
             unregisterMbean(cctx.cache().clusterMxBean(), cfg.getName(), 
false);
         }
 
-        NearCacheConfiguration nearCfg = cfg.getNearConfiguration();
-
-        if (nearCfg != null) {
-            cleanup(cfg, nearCfg.getNearEvictionPolicyFactory(), true);
-            cleanup(cfg, nearCfg.getNearEvictionPolicy(), true);
-        }
-
         cctx.cleanup();
     }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/31e88fc3/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridEvictionPolicyMBeansTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridEvictionPolicyMBeansTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridEvictionPolicyMBeansTest.java
new file mode 100644
index 0000000..b093bb2
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridEvictionPolicyMBeansTest.java
@@ -0,0 +1,117 @@
+/*
+ * 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.ignite.internal.processors.cache;
+
+import javax.management.ObjectName;
+import org.apache.ignite.cache.eviction.fifo.FifoEvictionPolicyFactory;
+import org.apache.ignite.cache.eviction.lru.LruEvictionPolicy;
+import org.apache.ignite.cache.eviction.lru.LruEvictionPolicyFactory;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.configuration.NearCacheConfiguration;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+
+/**
+ * Tests for the eviction policy JMX beans registered by the kernal.
+ */
+public class GridEvictionPolicyMBeansTest extends GridCommonAbstractTest {
+    /** Create test and auto-start the grid */
+    public GridEvictionPolicyMBeansTest() {
+        super(true);
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * This implementation  adds eviction policies.
+     */
+    @Override protected IgniteConfiguration getConfiguration(String 
igniteInstanceName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        FifoEvictionPolicyFactory<String, String> plc = new 
FifoEvictionPolicyFactory<>();
+
+        plc.setMaxSize(100);
+        plc.setBatchSize(10);
+        plc.setMaxMemorySize(20);
+
+        CacheConfiguration cache1 = defaultCacheConfiguration();
+
+        cache1.setName("cache1");
+        cache1.setOnheapCacheEnabled(true);
+        cache1.setEvictionPolicyFactory(plc);
+
+        NearCacheConfiguration ncf = new NearCacheConfiguration<>();
+
+        ncf.setNearEvictionPolicyFactory(new LruEvictionPolicyFactory<>(40, 
10, 500));
+
+        cache1.setNearConfiguration(ncf);
+
+        CacheConfiguration cache2 = defaultCacheConfiguration();
+
+        cache2.setName("cache2");
+        cache2.setOnheapCacheEnabled(true);
+
+        LruEvictionPolicy lep = new LruEvictionPolicy();
+
+        lep.setBatchSize(10);
+        lep.setMaxMemorySize(125);
+        lep.setMaxSize(30);
+        cache2.setEvictionPolicy(lep);
+
+        ncf = new NearCacheConfiguration<>();
+        lep = new LruEvictionPolicy();
+
+        lep.setBatchSize(10);
+        lep.setMaxMemorySize(500);
+        lep.setMaxSize(40);
+        ncf.setNearEvictionPolicy(lep);
+        cache2.setNearConfiguration(ncf);
+
+        cfg.setCacheConfiguration(cache1, cache2);
+
+        return cfg;
+    }
+
+    /** Check that eviction bean is available */
+    public void testEvictionPolicyBeans() throws Exception{
+        checkBean("cache1", 
"org.apache.ignite.cache.eviction.fifo.FifoEvictionPolicy", "MaxSize", 100);
+        checkBean("cache1", 
"org.apache.ignite.cache.eviction.fifo.FifoEvictionPolicy", "BatchSize", 10);
+        checkBean("cache1", 
"org.apache.ignite.cache.eviction.fifo.FifoEvictionPolicy", "MaxMemorySize", 
20L);
+
+        checkBean("cache1-near", 
"org.apache.ignite.cache.eviction.lru.LruEvictionPolicy", "MaxSize", 40);
+        checkBean("cache1-near", 
"org.apache.ignite.cache.eviction.lru.LruEvictionPolicy", "BatchSize", 10);
+        checkBean("cache1-near", 
"org.apache.ignite.cache.eviction.lru.LruEvictionPolicy", "MaxMemorySize", 
500L);
+
+        checkBean("cache2", 
"org.apache.ignite.cache.eviction.lru.LruEvictionPolicy", "MaxSize", 30);
+        checkBean("cache2", 
"org.apache.ignite.cache.eviction.lru.LruEvictionPolicy", "BatchSize", 10);
+        checkBean("cache2", 
"org.apache.ignite.cache.eviction.lru.LruEvictionPolicy", "MaxMemorySize", 
125L);
+
+        checkBean("cache2-near", 
"org.apache.ignite.cache.eviction.lru.LruEvictionPolicy", "MaxSize", 40);
+        checkBean("cache2-near", 
"org.apache.ignite.cache.eviction.lru.LruEvictionPolicy", "BatchSize", 10);
+        checkBean("cache2-near", 
"org.apache.ignite.cache.eviction.lru.LruEvictionPolicy", "MaxMemorySize", 
500L);
+    }
+
+    /** Checks that a bean with the specified group and name is available and 
has the expected attribute */
+    private void checkBean(String grp, String name, String attributeName, 
Object expAttributeVal) throws Exception {
+        ObjectName mBeanName = IgniteUtils.makeMBeanName(grid().name(), grp, 
name);
+        Object attributeVal = 
grid().configuration().getMBeanServer().getAttribute(mBeanName, attributeName);
+
+        assertEquals(expAttributeVal, attributeVal);
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/31e88fc3/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheMetricsSelfTestSuite.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheMetricsSelfTestSuite.java
 
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheMetricsSelfTestSuite.java
index 237f7e1..dc79123 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheMetricsSelfTestSuite.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheMetricsSelfTestSuite.java
@@ -25,6 +25,7 @@ import 
org.apache.ignite.internal.processors.cache.CacheMetricsEnableRuntimeTest
 import 
org.apache.ignite.internal.processors.cache.CacheMetricsEntitiesCountTest;
 import 
org.apache.ignite.internal.processors.cache.CacheMetricsForClusterGroupSelfTest;
 import org.apache.ignite.internal.processors.cache.CacheValidatorMetricsTest;
+import 
org.apache.ignite.internal.processors.cache.GridEvictionPolicyMBeansTest;
 import 
org.apache.ignite.internal.processors.cache.OffheapCacheMetricsForClusterGroupSelfTest;
 import 
org.apache.ignite.internal.processors.cache.distributed.near.GridCacheAtomicPartitionedMetricsSelfTest;
 import 
org.apache.ignite.internal.processors.cache.distributed.near.GridCacheAtomicPartitionedTckMetricsSelfTestImpl;
@@ -77,6 +78,8 @@ public class IgniteCacheMetricsSelfTestSuite extends 
TestSuite {
 
         suite.addTestSuite(TransactionMetricsMxBeanImplTest.class);
 
+        suite.addTestSuite(GridEvictionPolicyMBeansTest.class);
+
         return suite;
     }
 }

Reply via email to