From 15e18b52447648e924abc55a9915c0aa176b34e0 Mon Sep 17 00:00:00 2001 From: Vladimir Steshin Date: Tue, 7 Nov 2023 14:49:17 +0300 Subject: [PATCH] IGNITE-18434 Fixed histogram metric configuration restore on node restart (#11028) --- .../processors/metric/MetricRegistry.java | 42 ++++++++++--------- .../metric/MetricsConfigurationTest.java | 40 ++++++++++++++++++ 2 files changed, 62 insertions(+), 20 deletions(-) diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/metric/MetricRegistry.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/metric/MetricRegistry.java index 8602d594039fd..801cdfb4bec9a 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/metric/MetricRegistry.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/metric/MetricRegistry.java @@ -269,16 +269,7 @@ public LongAdderWithDelegateMetric longAdderMetric( * @see HitRateMetric */ public HitRateMetric hitRateMetric(String name, @Nullable String desc, long rateTimeInterval, int size) { - String fullName = metricName(regName, name); - - HitRateMetric metric = addMetric(name, new HitRateMetric(fullName, desc, rateTimeInterval, size)); - - Long cfgRateTimeInterval = hitRateCfgProvider.apply(fullName); - - if (cfgRateTimeInterval != null) - metric.reset(cfgRateTimeInterval, DFLT_SIZE); - - return metric; + return addMetric(name, new HitRateMetric(metricName(regName, name), desc, rateTimeInterval, size)); } /** @@ -302,16 +293,7 @@ public BooleanMetricImpl booleanMetric(String name, @Nullable String desc) { * @return {@link HistogramMetricImpl} */ public HistogramMetricImpl histogram(String name, long[] bounds, @Nullable String desc) { - String fullName = metricName(regName, name); - - HistogramMetricImpl metric = addMetric(name, new HistogramMetricImpl(fullName, desc, bounds)); - - long[] cfgBounds = histogramCfgProvider.apply(fullName); - - if (cfgBounds != null) - metric.reset(cfgBounds); - - return metric; + return addMetric(name, new HistogramMetricImpl(metricName(regName, name), desc, bounds)); } /** @@ -330,9 +312,29 @@ private T addMetric(String name, T metric) { if (old != null) return old; + configureMetrics(metric); + return metric; } + /** + * Assigns metric settings if {@code metric} is configurable. + */ + private void configureMetrics(Metric metric) { + if (metric instanceof HistogramMetricImpl) { + long[] cfgBounds = histogramCfgProvider.apply(metric.name()); + + if (cfgBounds != null) + ((HistogramMetricImpl)metric).reset(cfgBounds); + } + else if (metric instanceof HitRateMetric) { + Long cfgRateTimeInterval = hitRateCfgProvider.apply(metric.name()); + + if (cfgRateTimeInterval != null) + ((HitRateMetric)metric).reset(cfgRateTimeInterval, DFLT_SIZE); + } + } + /** {@inheritDoc} */ @Override public String name() { return regName; diff --git a/modules/core/src/test/java/org/apache/ignite/internal/metric/MetricsConfigurationTest.java b/modules/core/src/test/java/org/apache/ignite/internal/metric/MetricsConfigurationTest.java index 622bea647c3a8..29e318659260b 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/metric/MetricsConfigurationTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/metric/MetricsConfigurationTest.java @@ -27,6 +27,7 @@ import org.apache.ignite.internal.processors.metric.impl.HistogramMetricImpl; import org.apache.ignite.internal.processors.metric.impl.HitRateMetric; import org.apache.ignite.internal.processors.metric.impl.PeriodicHistogramMetricImpl; +import org.apache.ignite.internal.util.typedef.F; import org.apache.ignite.mxbean.MetricsMxBean; import org.apache.ignite.spi.metric.HistogramMetric; import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; @@ -39,6 +40,8 @@ import static org.apache.ignite.internal.processors.metric.GridMetricManager.TX_METRICS; import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.cacheMetricsRegistryName; import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.metricName; +import static org.apache.ignite.internal.processors.pool.PoolProcessor.TASK_EXEC_TIME; +import static org.apache.ignite.internal.processors.pool.PoolProcessor.THREAD_POOLS; import static org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertNotEquals; @@ -59,9 +62,18 @@ public class MetricsConfigurationTest extends GridCommonAbstractTest { /** {@inheritDoc} */ @Override protected void beforeTest() throws Exception { + super.beforeTest(); + cleanPersistenceDir(); } + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + super.afterTest(); + + stopAllGrids(); + } + /** {@inheritDoc} */ @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); @@ -306,6 +318,34 @@ public void testConfigRemovedOnCacheRemove() throws Exception { }); } + /** + * Tests that histogram configuration is read again after node restart. + */ + @Test + public void testHistogramConfigIsKeptAfterNodeRestart() throws Exception { + IgniteEx ignite = startGrid("persistent-0"); + + ignite.cluster().state(ClusterState.ACTIVE); + + String regName = metricName(THREAD_POOLS, "StripedExecutor"); + + HistogramMetric hist = ignite.context().metric().registry(regName).findMetric(TASK_EXEC_TIME); + + assertFalse(F.arrayEq(BOUNDS, hist.bounds())); + + ignite.context().metric().configureHistogram(metricName(regName, TASK_EXEC_TIME), BOUNDS); + + assertTrue(F.arrayEq(BOUNDS, hist.bounds())); + + stopGrid("persistent-0", false); + + ignite = startGrid("persistent-0"); + + hist = ignite.context().metric().registry(regName).findMetric(TASK_EXEC_TIME); + + assertTrue(F.arrayEq(BOUNDS, hist.bounds())); + } + /** Tests metric configuration removed on registry remove. */ @Test public void testConfigRemovedOnCacheGroupRemove() throws Exception {