You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by ni...@apache.org on 2020/03/18 10:57:50 UTC

[ignite] branch ignite-2.8.1 updated: IGNITE-12768: MetricRegistryMBean doesn't show histogram values in case when histogram name contains underscore character (#7542)

This is an automated email from the ASF dual-hosted git repository.

nizhikov pushed a commit to branch ignite-2.8.1
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/ignite-2.8.1 by this push:
     new 6a81a7d  IGNITE-12768: MetricRegistryMBean doesn't show histogram values in case when histogram name contains underscore character (#7542)
6a81a7d is described below

commit 6a81a7d6db420f3c01bb26363337077c3d4abbde
Author: Nikolay <ni...@apache.org>
AuthorDate: Wed Mar 18 13:31:38 2020 +0300

    IGNITE-12768: MetricRegistryMBean doesn't show histogram values in case when histogram name contains underscore character (#7542)
---
 .../processors/metric/impl/MetricUtils.java        |  9 ++--
 .../ignite/spi/metric/jmx/MetricRegistryMBean.java | 45 ++++++++++++++-----
 .../ignite/internal/metric/JmxExporterSpiTest.java | 51 +++++++++++++++++++++-
 3 files changed, 90 insertions(+), 15 deletions(-)

diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/metric/impl/MetricUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/metric/impl/MetricUtils.java
index 9a4c0db..8d2c9ba 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/metric/impl/MetricUtils.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/metric/impl/MetricUtils.java
@@ -36,7 +36,10 @@ public class MetricUtils {
     public static final String SEPARATOR = ".";
 
     /** Histogram metric last interval high bound. */
-    public static final String INF = "inf";
+    public static final String INF = "_inf";
+
+    /** Histogram name divider. */
+    public static final char HISTOGRAM_NAME_DIVIDER = '_';
 
     /**
      * Builds metric name. Each parameter will separated by '.' char.
@@ -158,12 +161,12 @@ public class MetricUtils {
         long min = 0;
 
         for (int i = 0; i < bounds.length; i++) {
-            names[i] = name + '_' + min + '_' + bounds[i];
+            names[i] = name + HISTOGRAM_NAME_DIVIDER + min + HISTOGRAM_NAME_DIVIDER + bounds[i];
 
             min = bounds[i];
         }
 
-        names[bounds.length] = name + '_' + min + '_' + INF;
+        names[bounds.length] = name + HISTOGRAM_NAME_DIVIDER + min + INF;
 
         cache.put(name, new T2<>(bounds, names));
 
diff --git a/modules/core/src/main/java/org/apache/ignite/spi/metric/jmx/MetricRegistryMBean.java b/modules/core/src/main/java/org/apache/ignite/spi/metric/jmx/MetricRegistryMBean.java
index 05d039f..9d10290 100644
--- a/modules/core/src/main/java/org/apache/ignite/spi/metric/jmx/MetricRegistryMBean.java
+++ b/modules/core/src/main/java/org/apache/ignite/spi/metric/jmx/MetricRegistryMBean.java
@@ -22,7 +22,6 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Scanner;
 import javax.management.MBeanAttributeInfo;
 import javax.management.MBeanInfo;
 import org.apache.ignite.internal.processors.metric.impl.MetricUtils;
@@ -39,6 +38,7 @@ import org.apache.ignite.spi.metric.ReadOnlyMetricManager;
 import org.apache.ignite.spi.metric.ReadOnlyMetricRegistry;
 
 import static java.util.Arrays.binarySearch;
+import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.HISTOGRAM_NAME_DIVIDER;
 import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.INF;
 import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.histogramBucketNames;
 
@@ -160,12 +160,25 @@ public class MetricRegistryMBean extends ReadOnlyDynamicMBean {
      * @see MetricUtils#histogramBucketNames(HistogramMetric, Map)
      */
     public static Long searchHistogram(String name, ReadOnlyMetricRegistry mreg) {
-        Scanner sc = new Scanner(name).useDelimiter("_");
+        int highBoundIdx;
 
-        if (!sc.hasNext())
+        boolean isInf = name.endsWith(INF);
+
+        if (isInf)
+            highBoundIdx = name.length() - 4;
+        else {
+            highBoundIdx = name.lastIndexOf(HISTOGRAM_NAME_DIVIDER);
+
+            if (highBoundIdx == -1)
+                return null;
+        }
+
+        int lowBoundIdx = name.lastIndexOf(HISTOGRAM_NAME_DIVIDER, highBoundIdx - 1);
+
+        if (lowBoundIdx == -1)
             return null;
 
-        Metric m = mreg.findMetric(sc.next());
+        Metric m = mreg.findMetric(name.substring(0, lowBoundIdx));
 
         if (!(m instanceof HistogramMetric))
             return null;
@@ -175,20 +188,30 @@ public class MetricRegistryMBean extends ReadOnlyDynamicMBean {
         long[] bounds = h.bounds();
         long[] values = h.value();
 
-        if (!sc.hasNextLong())
-            return null;
+        long lowBound;
 
-        long lowBound = sc.nextLong();
+        try {
+            lowBound = Long.parseLong(name.substring(lowBoundIdx + 1, highBoundIdx));
+        }
+        catch (NumberFormatException e) {
+            return null;
+        }
 
-        //If `highBound` not presented this can be last interval `[max]_inf`.
-        if (!sc.hasNextLong()) {
-            if (sc.hasNext() && INF.equals(sc.next()) && bounds[bounds.length - 1] == lowBound)
+        if (isInf) {
+            if (bounds[bounds.length - 1] == lowBound)
                 return values[values.length - 1];
 
             return null;
         }
 
-        long highBound = sc.nextLong();
+        long highBound;
+
+        try {
+            highBound = Long.parseLong(name.substring(highBoundIdx + 1));
+        }
+        catch (NumberFormatException e) {
+            return null;
+        }
 
         int idx = binarySearch(bounds, highBound);
 
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/metric/JmxExporterSpiTest.java b/modules/core/src/test/java/org/apache/ignite/internal/metric/JmxExporterSpiTest.java
index 20429a8..1b1a6fb 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/metric/JmxExporterSpiTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/metric/JmxExporterSpiTest.java
@@ -114,6 +114,9 @@ public class JmxExporterSpiTest extends AbstractExporterSpiTest {
     /** */
     private static IgniteEx ignite;
 
+    /** */
+    private static final String REGISTRY_NAME = "test_registry";
+
     /** {@inheritDoc} */
     @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
         IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
@@ -486,6 +489,10 @@ public class JmxExporterSpiTest extends AbstractExporterSpiTest {
         assertEquals(Long.valueOf(2), searchHistogram("histogram_50_500", mreg));
         assertEquals(Long.valueOf(3), searchHistogram("histogram_500_inf", mreg));
 
+        assertEquals(Long.valueOf(1), searchHistogram("histogram_with_underscore_0_50", mreg));
+        assertEquals(Long.valueOf(2), searchHistogram("histogram_with_underscore_50_500", mreg));
+        assertEquals(Long.valueOf(3), searchHistogram("histogram_with_underscore_500_inf", mreg));
+
         assertNull(searchHistogram("unknown", mreg));
         assertNull(searchHistogram("unknown_0", mreg));
         assertNull(searchHistogram("unknown_0_50", mreg));
@@ -498,6 +505,12 @@ public class JmxExporterSpiTest extends AbstractExporterSpiTest {
         assertNull(searchHistogram("histogram_0_100", mreg));
         assertNull(searchHistogram("histogram_0_inf", mreg));
         assertNull(searchHistogram("histogram_0_500", mreg));
+
+        assertNull(searchHistogram("histogram_with_underscore", mreg));
+        assertNull(searchHistogram("histogram_with_underscore_0", mreg));
+        assertNull(searchHistogram("histogram_with_underscore_0_100", mreg));
+        assertNull(searchHistogram("histogram_with_underscore_0_inf", mreg));
+        assertNull(searchHistogram("histogram_with_underscore_0_500", mreg));
     }
 
     /** */
@@ -511,11 +524,38 @@ public class JmxExporterSpiTest extends AbstractExporterSpiTest {
 
         MBeanAttributeInfo[] attrs = bean.getMBeanInfo().getAttributes();
 
-        assertEquals(3, attrs.length);
+        assertEquals(6, attrs.length);
 
         assertEquals(1L, bean.getAttribute("histogram_0_50"));
         assertEquals(2L, bean.getAttribute("histogram_50_500"));
         assertEquals(3L, bean.getAttribute("histogram_500_inf"));
+
+        assertEquals(1L, bean.getAttribute("histogram_with_underscore_0_50"));
+        assertEquals(2L, bean.getAttribute("histogram_with_underscore_50_500"));
+        assertEquals(3L, bean.getAttribute("histogram_with_underscore_500_inf"));
+    }
+
+    /** */
+    @Test
+    public void testJmxHistogramNamesExport() throws Exception {
+        MetricRegistry reg = ignite.context().metric().registry(REGISTRY_NAME);
+
+        String simpleName = "testhist";
+        String nameWithUnderscore = "test_hist";
+
+        reg.histogram(simpleName, new long[] {10, 100}, null);
+        reg.histogram(nameWithUnderscore, new long[] {10, 100}, null);
+
+        DynamicMBean mbn = metricRegistry(ignite.name(), null, REGISTRY_NAME);
+
+        assertNotNull(mbn.getAttribute(simpleName + '_' + 0 + '_' + 10));
+        assertEquals(0L, mbn.getAttribute(simpleName + '_' + 0 + '_' + 10));
+        assertNotNull(mbn.getAttribute(simpleName + '_' + 10 + '_' + 100));
+        assertEquals(0L, mbn.getAttribute(simpleName + '_' + 10 + '_' + 100));
+        assertNotNull(mbn.getAttribute(nameWithUnderscore + '_' + 10 + '_' + 100));
+        assertEquals(0L, mbn.getAttribute(nameWithUnderscore + '_' + 10 + '_' + 100));
+        assertNotNull(mbn.getAttribute(simpleName + '_' + 100 + "_inf"));
+        assertEquals(0L, mbn.getAttribute(simpleName + '_' + 100 + "_inf"));
     }
 
     /** */
@@ -868,5 +908,14 @@ public class JmxExporterSpiTest extends AbstractExporterSpiTest {
         histogram.value(600);
         histogram.value(600);
         histogram.value(600);
+
+        histogram = mreg.histogram("histogram_with_underscore", bounds, null);
+
+        histogram.value(10);
+        histogram.value(51);
+        histogram.value(60);
+        histogram.value(600);
+        histogram.value(600);
+        histogram.value(600);
     }
 }