diff --git a/licenses/bcpkix-jdk15on-1.66.jar.sha1 b/licenses/bcpkix-jdk15on-1.66.jar.sha1 deleted file mode 100644 index 7a5b700b..00000000 --- a/licenses/bcpkix-jdk15on-1.66.jar.sha1 +++ /dev/null @@ -1 +0,0 @@ -86cca2c1a32775abe92316d9628b7ee50b6f19ad \ No newline at end of file diff --git a/licenses/bcprov-jdk15on-1.66.jar.sha1 b/licenses/bcprov-jdk15on-1.66.jar.sha1 deleted file mode 100644 index 009cae20..00000000 --- a/licenses/bcprov-jdk15on-1.66.jar.sha1 +++ /dev/null @@ -1 +0,0 @@ -ed564ade61defca27e26fb1378a70b22831fc5c1 \ No newline at end of file diff --git a/licenses/jackson-annotations-2.10.4.jar.sha1 b/licenses/jackson-annotations-2.10.4.jar.sha1 deleted file mode 100644 index 0c548bb0..00000000 --- a/licenses/jackson-annotations-2.10.4.jar.sha1 +++ /dev/null @@ -1 +0,0 @@ -6ae6028aff033f194c9710ad87c224ccaadeed6c \ No newline at end of file diff --git a/licenses/jackson-databind-2.10.4.jar.sha1 b/licenses/jackson-databind-2.10.4.jar.sha1 deleted file mode 100644 index 27d5a72c..00000000 --- a/licenses/jackson-databind-2.10.4.jar.sha1 +++ /dev/null @@ -1 +0,0 @@ -76e9152e93d4cf052f93a64596f633ba5b1c8ed9 \ No newline at end of file diff --git a/licenses/performanceanalyzer-rca-1.13.jar.sha1 b/licenses/performanceanalyzer-rca-1.13.jar.sha1 index 1959cbc9..0c4379d5 100644 --- a/licenses/performanceanalyzer-rca-1.13.jar.sha1 +++ b/licenses/performanceanalyzer-rca-1.13.jar.sha1 @@ -1 +1 @@ -0ca252df95b9c0743d7b29452c7637504d5ba50f \ No newline at end of file +0d0d568122db55b2a11cf5d0f562d754e2bcf898 \ No newline at end of file diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPlugin.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPlugin.java index ada7d7a8..76a63c28 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPlugin.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2019-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. @@ -26,7 +26,6 @@ import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.FaultDetectionMetricsCollector; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.GCInfoCollector; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.HeapMetricsCollector; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.ShardIndexingPressureMetricsCollector; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.MasterServiceEventMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.MasterServiceMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.MasterThrottlingMetricsCollector; @@ -214,13 +213,7 @@ public PerformanceAnalyzerPlugin(final Settings settings, final java.nio.file.Pa scheduledMetricCollectorsExecutor.addScheduledMetricCollector(new ClusterApplierServiceStatsCollector( performanceAnalyzerController,configOverridesWrapper)); scheduledMetricCollectorsExecutor.addScheduledMetricCollector(new AdmissionControlMetricsCollector()); - try { - Class.forName(ShardIndexingPressureMetricsCollector.SHARD_INDEXING_PRESSURE_CLASS_NAME); - scheduledMetricCollectorsExecutor.addScheduledMetricCollector(new ShardIndexingPressureMetricsCollector( - performanceAnalyzerController,configOverridesWrapper)); - } catch (ClassNotFoundException e) { - LOG.info("Shard IndexingPressure not present in this ES version. Skipping ShardIndexingPressureMetricsCollector"); - } + try { Class.forName(MasterClusterStateUpdateStatsCollector.MASTER_CLUSTER_UPDATE_STATS_CLASS_NAME); scheduledMetricCollectorsExecutor.addScheduledMetricCollector(new MasterClusterStateUpdateStatsCollector( diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/AdmissionControlMetricsCollector.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/AdmissionControlMetricsCollector.java index 116d08c5..70a24728 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/AdmissionControlMetricsCollector.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/AdmissionControlMetricsCollector.java @@ -1,3 +1,18 @@ +/* + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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 com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors; import com.amazon.opendistro.elasticsearch.performanceanalyzer.PerformanceAnalyzerApp; @@ -6,6 +21,7 @@ import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsProcessor; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.rca.framework.metrics.ExceptionsAndErrors; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.rca.framework.metrics.WriterMetrics; import com.fasterxml.jackson.annotation.JsonProperty; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -14,7 +30,7 @@ import java.util.Objects; /** - * AdmissionControlMetricsCollector collects `UsedQuota`, `TotalQuota`, RejectionCount from ElasticSearch-Jetty - AdmissionController + * AdmissionControlMetricsCollector collects `UsedQuota`, `TotalQuota`, RejectionCount */ public class AdmissionControlMetricsCollector extends PerformanceAnalyzerMetricsCollector implements MetricsProcessor { @@ -41,7 +57,15 @@ public AdmissionControlMetricsCollector() { } @Override + @SuppressWarnings("unchecked") public void collectMetrics(long startTime) { + if(!isAdmissionControlFeatureAvailable()) { + LOG.debug("AdmissionControl is not available for this domain"); + PerformanceAnalyzerApp.WRITER_METRICS_AGGREGATOR.updateStat( + WriterMetrics.ADMISSION_CONTROL_COLLECTOR_NOT_AVAILABLE, "", 1); + return; + } + long startTimeMillis = System.currentTimeMillis(); try { Class admissionController = Class.forName(ADMISSION_CONTROLLER); @@ -87,11 +111,17 @@ public void collectMetrics(long startTime) { } saveMetricValues(value.toString(), startTime); + + PerformanceAnalyzerApp.WRITER_METRICS_AGGREGATOR.updateStat( + WriterMetrics.ADMISSION_CONTROL_COLLECTOR_EXECUTION_TIME, "", + System.currentTimeMillis() - startTimeMillis); + } catch(Exception ex) { PerformanceAnalyzerApp.ERRORS_AND_EXCEPTIONS_AGGREGATOR.updateStat( ExceptionsAndErrors.ADMISSION_CONTROL_COLLECTOR_ERROR, getCollectorName(), System.currentTimeMillis() - startTimeMillis); - LOG.debug("Exception in collecting AdmissionControl Metrics: {} for startTime {}", ex::toString, () -> startTime); + LOG.debug("Exception in collecting AdmissionControl Metrics: {} for startTime {}", + ex::toString, () -> startTime); } } @@ -144,4 +174,13 @@ public long getRejectionCount() { } } + private boolean isAdmissionControlFeatureAvailable() { + try { + Class.forName(ADMISSION_CONTROLLER); + Class.forName(ADMISSION_CONTROL_SERVICE); + } catch (ClassNotFoundException e) { + return false; + } + return true; + } } diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ClusterApplierServiceStatsCollector.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ClusterApplierServiceStatsCollector.java index fc92feb4..aca0bfcd 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ClusterApplierServiceStatsCollector.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ClusterApplierServiceStatsCollector.java @@ -194,4 +194,4 @@ public double getClusterApplierServiceFailed() { return clusterStateAppliedFailedCount; } } -} \ No newline at end of file +} diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ElectionTermCollector.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ElectionTermCollector.java index 650558ce..334e8c6b 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ElectionTermCollector.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ElectionTermCollector.java @@ -103,4 +103,4 @@ public long getElectionTerm() { return electionTerm; } } -} \ No newline at end of file +} diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/MasterServiceMetrics.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/MasterServiceMetrics.java index 95d555a7..bb2067a8 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/MasterServiceMetrics.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/MasterServiceMetrics.java @@ -23,6 +23,7 @@ import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.MasterPendingTaskDimension; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.MasterPendingValue; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.MasterPendingTaskDimension; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsProcessor; import com.fasterxml.jackson.annotation.JsonProperty; @@ -30,6 +31,7 @@ import java.util.HashMap; import java.util.List; + @SuppressWarnings("unchecked") public class MasterServiceMetrics extends PerformanceAnalyzerMetricsCollector implements MetricsProcessor { public static final int SAMPLING_TIME_INTERVAL = MetricsConfiguration.CONFIG_MAP.get(MasterServiceMetrics.class).samplingInterval; diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/util/Utils.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/util/Utils.java index b62d3f97..426c9a5a 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/util/Utils.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/util/Utils.java @@ -64,9 +64,10 @@ public static void configureMetrics() { MetricsConfiguration.CONFIG_MAP.put(FaultDetectionMetricsCollector.class, cdefault); MetricsConfiguration.CONFIG_MAP.put(ShardStateCollector.class, cdefault); MetricsConfiguration.CONFIG_MAP.put(MasterThrottlingMetricsCollector.class, cdefault); - MetricsConfiguration.CONFIG_MAP.put(ShardIndexingPressureMetricsCollector.class, cdefault); MetricsConfiguration.CONFIG_MAP.put(MasterClusterStateUpdateStatsCollector.class, cdefault); MetricsConfiguration.CONFIG_MAP.put(ClusterApplierServiceStatsCollector.class, cdefault); + MetricsConfiguration.CONFIG_MAP.put(ElectionTermCollector.class, cdefault); + MetricsConfiguration.CONFIG_MAP.put(ShardIndexingPressureMetricsCollector.class, cdefault); } // These methods are utility functions for the Node Stat Metrics Collectors. These methods are used by both the all diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/AdmissionControlMetricsCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/AdmissionControlMetricsCollectorTests.java index 8edd8210..979ab6a6 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/AdmissionControlMetricsCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/AdmissionControlMetricsCollectorTests.java @@ -1,28 +1,52 @@ +/* + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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 com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors; +import static org.junit.Assert.assertEquals; + import com.amazon.opendistro.elasticsearch.performanceanalyzer.CustomMetricsLocationTestBase; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event; -import org.junit.Test; - -import java.util.ArrayList; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.TestUtil; import java.util.List; - -import static org.junit.Assert.assertEquals; +import org.junit.Before; +import org.junit.Test; public class AdmissionControlMetricsCollectorTests extends CustomMetricsLocationTestBase { + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + // clean metricQueue before running every test + TestUtil.readEvents(); + System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); + } + @Test public void admissionControlMetricsCollector() { - MetricsConfiguration.CONFIG_MAP.put(AdmissionControlMetricsCollector.class, MetricsConfiguration.cdefault); - AdmissionControlMetricsCollector admissionControlMetricsCollector = new AdmissionControlMetricsCollector(); + MetricsConfiguration.CONFIG_MAP.put( + AdmissionControlMetricsCollector.class, MetricsConfiguration.cdefault); + AdmissionControlMetricsCollector admissionControlMetricsCollector = + new AdmissionControlMetricsCollector(); long startTimeInMills = System.currentTimeMillis(); admissionControlMetricsCollector.saveMetricValues("testMetric", startTimeInMills); - List metrics = new ArrayList<>(); - PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); + List metrics = TestUtil.readEvents(); assertEquals(1, metrics.size()); assertEquals("testMetric", metrics.get(0).value); } diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ClusterApplierServiceStatsCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ClusterApplierServiceStatsCollectorTests.java index 7a40cf54..88d47a1d 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ClusterApplierServiceStatsCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ClusterApplierServiceStatsCollectorTests.java @@ -1,61 +1,173 @@ /* - * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * or in the "license" file accompanying this file. This file 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. + * or in the "license" file accompanying this file. This file 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 com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import com.amazon.opendistro.elasticsearch.performanceanalyzer.CustomMetricsLocationTestBase; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources; import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController; import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event; -import org.junit.Test; -import org.mockito.Mockito; - +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import org.elasticsearch.cluster.service.ClusterApplierService; +import org.elasticsearch.cluster.service.ClusterService; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - +@RunWith(PowerMockRunner.class) +@PrepareForTest({ESResources.class}) public class ClusterApplierServiceStatsCollectorTests extends CustomMetricsLocationTestBase { + ObjectMapper mapper = new ObjectMapper(); @Test - public void testClusterApplierServiceStatsMetrics() { - MetricsConfiguration.CONFIG_MAP.put(ClusterApplierServiceStatsCollector.class, MetricsConfiguration.cdefault); + public void testClusterApplierServiceStats_saveMetricValues() { + MetricsConfiguration.CONFIG_MAP.put( + ClusterApplierServiceStatsCollector.class, MetricsConfiguration.cdefault); System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); - long startTimeInMills = 1153721339; - PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class); + PerformanceAnalyzerController controller = + Mockito.mock(PerformanceAnalyzerController.class); ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class); - Mockito.when(controller.isCollectorEnabled(configOverrides, "ClusterApplierServiceStatsCollector")) + ClusterApplierServiceStatsCollector clusterApplierServiceStatsCollector = + new ClusterApplierServiceStatsCollector(controller, configOverrides); + Mockito.when( + controller.isCollectorEnabled( + configOverrides, "ClusterApplierServiceStatsCollector")) .thenReturn(true); - ClusterApplierServiceStatsCollector clusterApplierServiceStatsCollector = new ClusterApplierServiceStatsCollector( - controller, configOverrides); - clusterApplierServiceStatsCollector.saveMetricValues("testMetric", startTimeInMills); - - List metrics = new ArrayList<>(); + clusterApplierServiceStatsCollector.saveMetricValues( + "cluster_applier_service", startTimeInMills); + List metrics = new ArrayList<>(); PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); + assertEquals(1, metrics.size()); - assertEquals("testMetric", metrics.get(0).value); + assertEquals("cluster_applier_service", metrics.get(0).value); try { - clusterApplierServiceStatsCollector.saveMetricValues("Cluster_applier", startTimeInMills, "123"); + clusterApplierServiceStatsCollector.saveMetricValues( + "cluster_applier_service", startTimeInMills, "dummy"); assertTrue("Negative scenario test: Should have been a RuntimeException", true); } catch (RuntimeException ex) { - //- expecting exception...1 values passed; 0 expected + // - expecting exception...1 values passed; 0 expected } } + + @SuppressWarnings("unchecked") + @Ignore + @Test + public void testClusterApplierServiceStats_collectMetrics() + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, + JsonProcessingException { + System.out.println("test 1"); + MetricsConfiguration.CONFIG_MAP.put( + ClusterApplierServiceStatsCollector.class, MetricsConfiguration.cdefault); + System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); + long startTimeInMills = 1153721339; + PerformanceAnalyzerController controller = + Mockito.mock(PerformanceAnalyzerController.class); + ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class); + ClusterApplierServiceStatsCollector clusterApplierServiceStatsCollector = + new ClusterApplierServiceStatsCollector(controller, configOverrides); + ClusterApplierServiceStatsCollector spyCollector = + Mockito.spy(clusterApplierServiceStatsCollector); + Mockito.doReturn( + new ClusterApplierServiceStatsCollector.ClusterApplierServiceStats( + 23L, 15L, 2L, -1L)) + .when(spyCollector) + .getClusterApplierServiceStats(); + Mockito.when( + controller.isCollectorEnabled( + configOverrides, + ClusterApplierServiceStatsCollector.class.getSimpleName())) + .thenReturn(true); + + ESResources esResources = Mockito.mock(ESResources.class); + ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterApplierService clusterApplierService = Mockito.mock(ClusterApplierService.class); + Whitebox.setInternalState(ESResources.class, "INSTANCE", esResources); + Mockito.when(esResources.getClusterService()).thenReturn(clusterService); + Mockito.when(clusterService.getClusterApplierService()).thenReturn(clusterApplierService); + + spyCollector.collectMetrics(startTimeInMills); + + List metrics = new ArrayList<>(); + PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); + + assertEquals(1, metrics.size()); + String[] lines = metrics.get(0).value.split(System.lineSeparator()); + Map map = mapper.readValue(lines[1], Map.class); + assertEquals( + 0.6521739130434783, + map.get( + AllMetrics.ClusterApplierServiceStatsValue.CLUSTER_APPLIER_SERVICE_LATENCY + .toString())); + assertEquals( + 2.0, + map.get( + AllMetrics.ClusterApplierServiceStatsValue.CLUSTER_APPLIER_SERVICE_FAILURE + .toString())); + } + + @SuppressWarnings("unchecked") + @Test + public void testClusterApplierServiceStats_collectMetrics_ClassNotFoundException() { + MetricsConfiguration.CONFIG_MAP.put( + ClusterApplierServiceStatsCollector.class, MetricsConfiguration.cdefault); + System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); + long startTimeInMills = 1153721339; + PerformanceAnalyzerController controller = + Mockito.mock(PerformanceAnalyzerController.class); + ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class); + ClusterApplierServiceStatsCollector clusterApplierServiceStatsCollector = + new ClusterApplierServiceStatsCollector(controller, configOverrides); + ClusterApplierServiceStatsCollector spyCollector = + Mockito.spy(clusterApplierServiceStatsCollector); + Mockito.when( + controller.isCollectorEnabled( + configOverrides, + ClusterApplierServiceStatsCollector.class.getSimpleName())) + .thenReturn(true); + + ESResources esResources = Mockito.mock(ESResources.class); + ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterApplierService clusterApplierService = Mockito.mock(ClusterApplierService.class); + Whitebox.setInternalState(ESResources.class, "INSTANCE", esResources); + Mockito.when(esResources.getClusterService()).thenReturn(clusterService); + Mockito.when(clusterService.getClusterApplierService()).thenReturn(clusterApplierService); + + spyCollector.collectMetrics(startTimeInMills); + + List metrics = new ArrayList<>(); + PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); + // No method found to get cluster state applier thread stats. Skipping + // ClusterApplierServiceStatsCollector. + assertEquals(0, metrics.size()); + } } diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ElectionTermCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ElectionTermCollectorTests.java index f085c13e..ec0ccc96 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ElectionTermCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ElectionTermCollectorTests.java @@ -1,5 +1,5 @@ /* - * Copyright <2021> Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License.