ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agoncha...@apache.org
Subject [45/50] [abbrv] ignite git commit: IGNITE-5288: removing test from suite: Inconsistency of committed and the max memory numbers should not cause stopping node - Fixes #2006.
Date Thu, 01 Jun 2017 13:24:00 GMT
IGNITE-5288: removing test from suite: Inconsistency of committed and the max memory numbers
should not cause stopping node - Fixes #2006.

Signed-off-by: Alexey Goncharuk <alexey.goncharuk@gmail.com>


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

Branch: refs/heads/ignite-5267
Commit: 82ccc9a7bbfc5907959d6f9832f69dfe9adf2da5
Parents: dda56bf
Author: dspavlov <dpavlov@gridgain.com>
Authored: Thu Jun 1 12:23:34 2017 +0300
Committer: Alexey Goncharuk <alexey.goncharuk@gmail.com>
Committed: Thu Jun 1 12:23:34 2017 +0300

----------------------------------------------------------------------
 .../discovery/GridDiscoveryManager.java         |  26 +++-
 ...dManagerMxBeanIllegalArgumentHandleTest.java | 125 +++++++++++++++++++
 2 files changed, 146 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/82ccc9a7/modules/core/src/main/java/org/apache/ignite/internal/managers/discovery/GridDiscoveryManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/managers/discovery/GridDiscoveryManager.java
b/modules/core/src/main/java/org/apache/ignite/internal/managers/discovery/GridDiscoveryManager.java
index b47f4fa..7c702c2 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/managers/discovery/GridDiscoveryManager.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/managers/discovery/GridDiscoveryManager.java
@@ -149,7 +149,7 @@ public class GridDiscoveryManager extends GridManagerAdapter<DiscoverySpi>
{
     /** Metrics update frequency. */
     private static final long METRICS_UPDATE_FREQ = 3000;
 
-    /** */
+    /** JVM interface to memory consumption info */
     private static final MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
 
     /** */
@@ -297,6 +297,22 @@ public class GridDiscoveryManager extends GridManagerAdapter<DiscoverySpi>
{
         }
     }
 
+    /**
+     * Returns the current memory usage of the heap
+     * @return memory usage or fake value with zero in case there was exception during take
of metrics
+     */
+    private MemoryUsage getHeapMemoryUsage() {
+        // Catch exception here to allow discovery proceed even if metrics are not available
+        // java.lang.IllegalArgumentException: committed = 5274103808 should be < max
= 5274095616
+        // at java.lang.management.MemoryUsage.<init>(Unknown Source)
+        try {
+            return mem.getHeapMemoryUsage();
+        }
+        catch (IllegalArgumentException ignored) {
+            return new MemoryUsage(0, 0, 0, 0);
+        }
+    }
+
     /** {@inheritDoc} */
     @Override public void onBeforeSpiStart() {
         DiscoverySpi spi = getSpi();
@@ -806,19 +822,19 @@ public class GridDiscoveryManager extends GridManagerAdapter<DiscoverySpi>
{
             }
 
             @Override public long getHeapMemoryInitialized() {
-                return mem.getHeapMemoryUsage().getInit();
+                return getHeapMemoryUsage().getInit();
             }
 
             @Override public long getHeapMemoryUsed() {
-                return mem.getHeapMemoryUsage().getUsed();
+                return getHeapMemoryUsage().getUsed();
             }
 
             @Override public long getHeapMemoryCommitted() {
-                return mem.getHeapMemoryUsage().getCommitted();
+                return getHeapMemoryUsage().getCommitted();
             }
 
             @Override public long getHeapMemoryMaximum() {
-                return mem.getHeapMemoryUsage().getMax();
+                return getHeapMemoryUsage().getMax();
             }
 
             @Override public long getNonHeapMemoryInitialized() {

http://git-wip-us.apache.org/repos/asf/ignite/blob/82ccc9a7/modules/core/src/test/java/org/apache/ignite/internal/managers/GridManagerMxBeanIllegalArgumentHandleTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/managers/GridManagerMxBeanIllegalArgumentHandleTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/managers/GridManagerMxBeanIllegalArgumentHandleTest.java
new file mode 100644
index 0000000..0276abd
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/managers/GridManagerMxBeanIllegalArgumentHandleTest.java
@@ -0,0 +1,125 @@
+/*
+ * 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.managers;
+
+import java.lang.management.MemoryMXBean;
+import java.lang.management.MemoryUsage;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import junit.framework.TestCase;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.managers.discovery.GridDiscoveryManager;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.jetbrains.annotations.NotNull;
+import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import static org.mockito.Mockito.when;
+
+/**
+ * Inconsistent metrics from JVM reported: grid manager handle test
+ *
+ * Test modifies static final field, used only for development
+ */
+public class GridManagerMxBeanIllegalArgumentHandleTest extends TestCase {
+    /** Original value of {@link GridDiscoveryManager#mem} to be restored after test */
+    private Object mxBeanToRestore;
+
+    /** Mem mx bean field in {@link GridDiscoveryManager#mem}, already set accessible */
+    private Field memMxBeanField;
+
+    /** If we succeeded to set final field this flag is true, otherwise test assertions will
not be performed */
+    private boolean correctSetupOfTestPerformed;
+
+    /** {@inheritDoc} Changes field to always failing mock */
+    @Override public void setUp() throws Exception {
+        super.setUp();
+        try {
+            final MemoryMXBean memoryMXBean = createAlwaysFailingMxBean();
+            memMxBeanField = createAccessibleMemField();
+            mxBeanToRestore = memMxBeanField.get(null);
+            memMxBeanField.set(null, memoryMXBean);
+
+            correctSetupOfTestPerformed = memMxBeanField.get(null) == memoryMXBean;
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /** MX bean which is always failing to respond with metrics */
+    @NotNull private MemoryMXBean createAlwaysFailingMxBean() {
+        final Answer<MemoryUsage> failingAnswer = new Answer<MemoryUsage>() {
+            @Override public MemoryUsage answer(InvocationOnMock invocationOnMock) throws
Throwable {
+                throw new IllegalArgumentException("java.lang.IllegalArgumentException: committed
= 5274103808 should be < max = 5274095616");
+            }
+        };
+        final MemoryMXBean memoryMXBean = Mockito.mock(MemoryMXBean.class);
+        when(memoryMXBean.getHeapMemoryUsage()).thenAnswer(failingAnswer);
+        when(memoryMXBean.getNonHeapMemoryUsage()).thenAnswer(failingAnswer);
+        return memoryMXBean;
+    }
+
+
+    /** Reflections {@link GridDiscoveryManager#mem} field which was made accessible and
mutable */
+    @NotNull private Field createAccessibleMemField() throws NoSuchFieldException, IllegalAccessException
{
+        final Field memField = GridDiscoveryManager.class.getDeclaredField("mem");
+        memField.setAccessible(true);
+
+        final Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(memField, memField.getModifiers() & ~Modifier.FINAL);
+        return memField;
+    }
+
+    /**
+     * Restores static field in {@link GridDiscoveryManager#mem}
+     *
+     * @throws Exception if field set failed
+     */
+    @Override public void tearDown() throws Exception {
+        super.tearDown();
+        if (correctSetupOfTestPerformed)
+            memMxBeanField.set(null, mxBeanToRestore);
+    }
+
+    /** Creates minimal disco manager mock, checks illegal state is not propagated */
+    public void testIllegalStateIsCatch() {
+        final IgniteConfiguration cfg = new IgniteConfiguration();
+        cfg.setDiscoverySpi(new TcpDiscoverySpi());
+
+        final IgniteLogger log = Mockito.mock(IgniteLogger.class);
+
+        final GridKernalContext ctx = Mockito.mock(GridKernalContext.class);
+        when(ctx.config()).thenReturn(cfg);
+        when(ctx.log(Mockito.anyString())).thenReturn(log);
+        when(ctx.log(Mockito.any(Class.class))).thenReturn(log);
+
+        final GridDiscoveryManager mgr = new GridDiscoveryManager(ctx);
+        final long nHeapMax = mgr.metrics().getNonHeapMemoryMaximum();
+        if (correctSetupOfTestPerformed)
+            assertEquals(0, nHeapMax);
+
+        final long heapMax = mgr.metrics().getHeapMemoryMaximum();
+        if (correctSetupOfTestPerformed)
+            assertEquals(0, heapMax);
+    }
+}


Mime
View raw message