diff --git a/build.gradle b/build.gradle index 1ec2963a..a826082c 100644 --- a/build.gradle +++ b/build.gradle @@ -165,7 +165,7 @@ jacocoTestCoverageVerification { violationRules { rule { limit { - minimum = 0.31 + minimum = 0.48 } } } 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 cb7db549..a4356170 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPlugin.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPlugin.java @@ -15,16 +15,49 @@ package com.amazon.opendistro.elasticsearch.performanceanalyzer; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.GCInfoCollector; +import static java.util.Collections.singletonList; + +import com.amazon.opendistro.elasticsearch.performanceanalyzer.action.PerformanceAnalyzerActionFilter; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.CacheConfigMetricsCollector; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.CircuitBreakerCollector; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.DisksCollector; 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.MasterServiceEventMetrics; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.MasterServiceMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.MasterThrottlingMetricsCollector; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.MetricsPurgeActivity; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.NetworkInterfaceCollector; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.NodeDetailsCollector; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.NodeStatsAllShardsMetricsCollector; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.NodeStatsFixedShardsMetricsCollector; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.OSMetricsCollector; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.ScheduledMetricCollectorsExecutor; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.ShardStateCollector; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.StatsCollector; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.ThreadPoolMetricsCollector; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PluginSettings; import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.setting.ClusterSettingsManager; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.setting.PerformanceAnalyzerClusterSettings; import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.setting.handler.ConfigOverridesClusterSettingHandler; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.CacheConfigMetricsCollector; import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.setting.handler.NodeStatsSettingHandler; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.setting.handler.PerformanceAnalyzerClusterSettingHandler; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.config.PerformanceAnalyzerClusterConfigAction; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.config.PerformanceAnalyzerConfigAction; import com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.config.PerformanceAnalyzerOverridesClusterConfigAction; import com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.config.PerformanceAnalyzerResourceProvider; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.whoami.TransportWhoAmIAction; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.whoami.WhoAmIAction; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.listener.PerformanceAnalyzerSearchListener; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.EventLog; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.EventLogFileHandler; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.transport.PerformanceAnalyzerTransportInterceptor; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.Utils; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.writer.EventLogQueueProcessor; import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -37,7 +70,6 @@ import java.util.List; import java.util.Map; import java.util.function.Supplier; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; @@ -76,40 +108,6 @@ import org.elasticsearch.transport.TransportInterceptor; import org.elasticsearch.watcher.ResourceWatcherService; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.action.PerformanceAnalyzerActionFilter; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.CircuitBreakerCollector; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.DisksCollector; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.HeapMetricsCollector; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.MasterServiceEventMetrics; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.MasterServiceMetrics; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.MetricsPurgeActivity; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.NetworkInterfaceCollector; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.NodeDetailsCollector; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.OSMetricsCollector; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.ScheduledMetricCollectorsExecutor; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.StatsCollector; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.ThreadPoolMetricsCollector; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.NodeStatsAllShardsMetricsCollector; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.NodeStatsFixedShardsMetricsCollector; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.setting.ClusterSettingsManager; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.setting.handler.PerformanceAnalyzerClusterSettingHandler; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.setting.PerformanceAnalyzerClusterSettings; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PluginSettings; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.config.PerformanceAnalyzerClusterConfigAction; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.config.PerformanceAnalyzerConfigAction; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.whoami.TransportWhoAmIAction; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.whoami.WhoAmIAction; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.listener.PerformanceAnalyzerSearchListener; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.EventLog; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.EventLogFileHandler; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.transport.PerformanceAnalyzerTransportInterceptor; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.Utils; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.writer.EventLogQueueProcessor; - -import static java.util.Collections.singletonList; - public final class PerformanceAnalyzerPlugin extends Plugin implements ActionPlugin, NetworkPlugin, SearchPlugin { private static final Logger LOG = LogManager.getLogger(PerformanceAnalyzerPlugin.class); public static final String PLUGIN_NAME = "opendistro_performance_analyzer"; @@ -134,8 +132,6 @@ public final class PerformanceAnalyzerPlugin extends Plugin implements ActionPlu } } - - public static void invokePrivileged(Runnable runner) { AccessController.doPrivileged((PrivilegedAction) () -> { try { @@ -148,18 +144,6 @@ public static void invokePrivileged(Runnable runner) { } ); } - public static void invokePrivilegedAndLogError(Runnable runner) { - AccessController.doPrivileged((PrivilegedAction) () -> { - try { - runner.run(); - } catch(Exception ex) { - LOG.error((Supplier) () -> new ParameterizedMessage("Privileged Invocation failed {}", - ex.toString()), ex); - } - return null; - } ); - } - private final ScheduledMetricCollectorsExecutor scheduledMetricCollectorsExecutor; public PerformanceAnalyzerPlugin(final Settings settings, final java.nio.file.Path configPath) { diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPluginTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPluginTests.java new file mode 100644 index 00000000..0bd3381d --- /dev/null +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPluginTests.java @@ -0,0 +1,161 @@ +/* + * Copyright <2020> 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; + +import static org.mockito.MockitoAnnotations.initMocks; + +import com.amazon.opendistro.elasticsearch.performanceanalyzer.action.PerformanceAnalyzerActionFilter; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.setting.PerformanceAnalyzerClusterSettings; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.config.PerformanceAnalyzerClusterConfigAction; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.config.PerformanceAnalyzerConfigAction; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.config.PerformanceAnalyzerOverridesClusterConfigAction; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.http_action.config.PerformanceAnalyzerResourceProvider; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.transport.PerformanceAnalyzerTransportInterceptor; +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope.Scope; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.function.Supplier; +import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.support.ActionFilter; +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.settings.ClusterSettings; +import org.elasticsearch.common.settings.Setting; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.discovery.Discovery; +import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; +import org.elasticsearch.indices.breaker.BreakerSettings; +import org.elasticsearch.indices.breaker.CircuitBreakerService; +import org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService; +import org.elasticsearch.plugins.ActionPlugin.ActionHandler; +import org.elasticsearch.rest.RestController; +import org.elasticsearch.rest.RestHandler; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.threadpool.TestThreadPool; +import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.transport.Transport; +import org.elasticsearch.transport.TransportInterceptor; +import org.elasticsearch.usage.UsageService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + +@ThreadLeakScope(Scope.NONE) +public class PerformanceAnalyzerPluginTests extends ESTestCase { + private PerformanceAnalyzerPlugin plugin; + private Settings settings; + private RestController restController; + private ThreadPool threadPool; + private NodeClient nodeClient; + private Environment environment; + private CircuitBreakerService circuitBreakerService; + private ClusterService clusterService; + private ClusterSettings clusterSettings; + + @Mock + private Discovery discovery; + + @Before + public void setup() { + initMocks(this); + + settings = Settings.builder().put("path.home", "./").build(); + plugin = new PerformanceAnalyzerPlugin(settings, Paths.get("build/tmp/junit_metrics")); + clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); + circuitBreakerService = new HierarchyCircuitBreakerService(settings, new ArrayList(), clusterSettings); + UsageService usageService = new UsageService(); + threadPool = new TestThreadPool("test"); + nodeClient = new NodeClient(settings, threadPool); + environment = TestEnvironment.newEnvironment(settings); + clusterService = new ClusterService(settings, clusterSettings, threadPool); + restController = new RestController(Collections.emptySet(), null, nodeClient, circuitBreakerService, usageService); + } + + @After + public void tearDown() throws Exception { + threadPool.shutdownNow(); + super.tearDown(); + } + + @Test + public void testGetActionFilters() { + List list = plugin.getActionFilters(); + assertEquals(1, list.size()); + assertEquals(PerformanceAnalyzerActionFilter.class, list.get(0).getClass()); + } + + @Test + public void testGetActions() { + List> list = plugin.getActions(); + assertEquals(1, list.size()); + assertEquals(ActionHandler.class, list.get(0).getClass()); + } + + @Test + public void testGetTransportInterceptors() { + List list = plugin.getTransportInterceptors(null, null); + assertEquals(1, list.size()); + assertEquals(PerformanceAnalyzerTransportInterceptor.class, list.get(0).getClass()); + } + + @Test + public void testGetRestHandlers() { + List handlers = plugin.getRestHandlers(settings, restController, null, + null, null, null, null); + assertEquals(4, handlers.size()); + assertEquals(PerformanceAnalyzerConfigAction.class, handlers.get(0).getClass()); + assertEquals(PerformanceAnalyzerClusterConfigAction.class, handlers.get(1).getClass()); + assertEquals(PerformanceAnalyzerResourceProvider.class, handlers.get(2).getClass()); + assertEquals(PerformanceAnalyzerOverridesClusterConfigAction.class, handlers.get(3).getClass()); + } + + @Test + public void testCreateComponents() { + Collection components = plugin.createComponents( + nodeClient, clusterService, threadPool, null,null, + null, environment, null,null, null,null); + assertEquals(1, components.size()); + assertEquals(settings, ESResources.INSTANCE.getSettings()); + assertEquals(threadPool, ESResources.INSTANCE.getThreadPool()); + assertEquals(environment, ESResources.INSTANCE.getEnvironment()); + assertEquals(nodeClient, ESResources.INSTANCE.getClient()); + } + + @Test + public void testGetTransports() { + Map> map = plugin.getTransports(settings, threadPool, null, circuitBreakerService, null, null); + assertEquals(0, map.size()); + assertEquals(settings, ESResources.INSTANCE.getSettings()); + assertEquals(circuitBreakerService, ESResources.INSTANCE.getCircuitBreakerService()); + } + + @Test + public void testGetSettings() { + List> list = plugin.getSettings(); + assertEquals(3, list.size()); + assertEquals(PerformanceAnalyzerClusterSettings.COMPOSITE_PA_SETTING, list.get(0)); + assertEquals(PerformanceAnalyzerClusterSettings.PA_NODE_STATS_SETTING, list.get(1)); + assertEquals(PerformanceAnalyzerClusterSettings.CONFIG_OVERRIDES_SETTING, list.get(2)); + } +} diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/action/PerformanceAnalyzerActionFilterTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/action/PerformanceAnalyzerActionFilterTests.java new file mode 100644 index 00000000..091b96a3 --- /dev/null +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/action/PerformanceAnalyzerActionFilterTests.java @@ -0,0 +1,71 @@ +/* + * Copyright <2020> 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.action; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.MockitoAnnotations.initMocks; + +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController; +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.bulk.BulkRequest; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.support.ActionFilterChain; +import org.elasticsearch.tasks.Task; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; + +public class PerformanceAnalyzerActionFilterTests { + private static final String[] testIndices = new String[]{"testIndex"}; + private PerformanceAnalyzerActionFilter filter; + + @Mock private PerformanceAnalyzerController controller; + @Mock private SearchRequest searchRequest; + @Mock private BulkRequest bulkRequest; + @Mock private ActionListener listener; + @Mock private ActionFilterChain chain; + @Mock private Task task; + + @Before + public void init() { + initMocks(this); + + Mockito.when(controller.isPerformanceAnalyzerEnabled()).thenReturn(true); + filter = new PerformanceAnalyzerActionFilter((controller)); + } + + @Test + public void testApplyWithSearchRequest() { + Mockito.when(searchRequest.indices()).thenReturn(testIndices); + testApply(searchRequest); + } + + @Test + public void testApplyWithBulkRequest() { + testApply(bulkRequest); + } + + private void testApply(ActionRequest request) { + filter.apply(task, "_action", request, listener, chain); + verify(chain).proceed(eq(task), eq("_action"), eq(request), any()); + } +} + diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CacheConfigMetricsCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CacheConfigMetricsCollectorTests.java index 74cafe3e..313773a5 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CacheConfigMetricsCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CacheConfigMetricsCollectorTests.java @@ -1,3 +1,18 @@ +/* + * Copyright <2020> 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.ESResources; diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CircuitBreakerCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CircuitBreakerCollectorTests.java index ed656418..9f0186a6 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CircuitBreakerCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/CircuitBreakerCollectorTests.java @@ -15,42 +15,66 @@ package com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors; -import org.junit.Ignore; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.CircuitBreakerCollector.CircuitBreakerStatus; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.TestUtil; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.module.paranamer.ParanamerModule; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.elasticsearch.common.breaker.CircuitBreaker; +import org.elasticsearch.indices.IndicesService; +import org.elasticsearch.test.ESSingleNodeTestCase; +import org.junit.After; +import org.junit.Before; import org.junit.Test; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.CustomMetricsLocationTestBase; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PluginSettings; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; -import static org.junit.Assert.assertEquals; -@Ignore -public class CircuitBreakerCollectorTests extends CustomMetricsLocationTestBase { +public class CircuitBreakerCollectorTests extends ESSingleNodeTestCase { + private static final String TEST_INDEX = "test"; + private CircuitBreakerCollector collector; + private long startTimeInMills = 1153721339; + + @Before + public void init() { + IndicesService indicesService = getInstanceFromNode(IndicesService.class); + ESResources.INSTANCE.setIndicesService(indicesService); + ESResources.INSTANCE.setCircuitBreakerService(indicesService.getCircuitBreakerService()); - @Test - public void testCircuitBreakerMetrics() { MetricsConfiguration.CONFIG_MAP.put(CircuitBreakerCollector.class, MetricsConfiguration.cdefault); - System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); - long startTimeInMills = 1153721339; - CircuitBreakerCollector circuitBreakerCollector = new CircuitBreakerCollector(); - circuitBreakerCollector.saveMetricValues("werjbdsiviewur", startTimeInMills); - String fetchedValue = PerformanceAnalyzerMetrics.getMetric(PluginSettings.instance().getMetricsLocation() - + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+"/circuit_breaker/"); - PerformanceAnalyzerMetrics.removeMetrics(PluginSettings.instance().getMetricsLocation() - + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)); - assertEquals("werjbdsiviewur", fetchedValue); - - try { - circuitBreakerCollector.saveMetricValues("12321.5464", startTimeInMills, "123"); - assertEquals(true, true); - } catch (RuntimeException ex) { - //- expecting exception...1 values passed; 0 expected - } + collector = new CircuitBreakerCollector(); + } + + @After + public void tearDown() throws Exception { + super.tearDown(); + } + + @Test + public void testCollectMetrics() throws IOException { + createIndex(TEST_INDEX); + collector.collectMetrics(startTimeInMills); + List metrics = readMetrics(); + assertEquals(5, metrics.size()); + assertEquals(CircuitBreaker.REQUEST, metrics.get(0).getType()); + assertEquals(CircuitBreaker.FIELDDATA, metrics.get(1).getType()); + assertEquals(CircuitBreaker.IN_FLIGHT_REQUESTS, metrics.get(2).getType()); + assertEquals(CircuitBreaker.ACCOUNTING, metrics.get(3).getType()); + assertEquals(CircuitBreaker.PARENT, metrics.get(4).getType()); + } - try { - circuitBreakerCollector.getMetricsPath(startTimeInMills, "123", "x"); - assertEquals(true, true); - } catch (RuntimeException ex) { - //- expecting exception...2 values passed; 0 expected + private List readMetrics() throws IOException { + List metrics = TestUtil.readEvents(); + assert metrics.size() == 1; + ObjectMapper objectMapper = new ObjectMapper().registerModule(new ParanamerModule()); + String[] jsonStrs = metrics.get(0).value.split("\n"); + assert jsonStrs.length == 6; + List list = new ArrayList<>(); + for (int i = 1; i < 6; i++) { + list.add(objectMapper.readValue(jsonStrs[i], CircuitBreakerStatus.class)); } + return list; } } diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeDetailsCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeDetailsCollectorTests.java index 9dab133d..43d95afa 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeDetailsCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeDetailsCollectorTests.java @@ -1,3 +1,18 @@ +/* + * Copyright <2020> 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.mockito.MockitoAnnotations.initMocks;