From 4713ae2eb7023d0eeef9ed87a3c37e4cd2cdd07f Mon Sep 17 00:00:00 2001 From: Yu Sun <11154022+yu-sun-77@users.noreply.github.com> Date: Fri, 20 Nov 2020 16:40:40 -0800 Subject: [PATCH] Improving Test coverage (#239) * add jacoco for code coverage * update codecov report uploader action * clean code * clean code * change jacoco limit threshold to 0.0 to * integrate Codecov with Github Actions pipeline * upload jacoco report to codecov * update README.md to show codecov badge * add UT for ShardStateCollectorTests * add UT for ShardStateCollectorTests * add UT for ShardStateCollectorTests * use paranamer module to avoid creating default constructor * pass code style check * update sha file for jackson-module-paranamer-2.11.3 * revert NodeStatsFixedShardsMetricsCollectorTests change * invoke tearDown after testing * fix accessControllerException * make NodeStatsMetricsAllShardsPerCollectionStatus static * move readEvent method to TestUtil * clean code * remove unsued test case * ppopulate diff value for NodeStatsMetricsAllShardsPerCollectionStatus * refactor based on code review * make valueCalculators as ImmutableMap * fix paranamer lib dependency issue * iignore sha file * grant permission to fix AccessControlException * update byte-buddy version to 1.9.7 * update byte-buddy version to 1.9.7 * set tests.security.manager to false * change minimum code coverage verification to 0.19 --- .gitignore | 3 +- build.gradle | 9 +- licenses/paranamer-LICENSE.txt | 8 ++ licenses/paranamer-NOTICE.txt | 0 .../performanceanalyzer-rca-1.11.jar.sha1 | 2 +- .../NodeStatsAllShardsMetricsCollector.java | 26 +++-- .../ThreadPoolMetricsCollector.java | 27 ++--- src/main/resources/plugin-security.policy | 4 + ...deStatsAllShardsMetricsCollectorTests.java | 101 +++++++++------- ...StatsFixedShardsMetricsCollectorTests.java | 6 +- .../collectors/ShardStateCollectorTests.java | 108 +++++++++++++----- .../ThreadPoolMetricsCollectorTests.java | 15 +-- .../reader/AbstractReaderTests.java | 6 +- .../PerformanceAnalyzerSqliteTests.java | 10 +- .../performanceanalyzer/util/TestUtil.java | 14 +++ 15 files changed, 210 insertions(+), 129 deletions(-) create mode 100644 licenses/paranamer-LICENSE.txt create mode 100644 licenses/paranamer-NOTICE.txt create mode 100644 src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/util/TestUtil.java diff --git a/.gitignore b/.gitignore index 73de4101..8125a5d9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /.idea/ /build/ *.iml -licenses/performanceanalyzer-rca-1.3.jar.sha1s \ No newline at end of file +licenses/performanceanalyzer-rca-1.3.jar.sha1s +licenses/*.sha1 \ No newline at end of file diff --git a/build.gradle b/build.gradle index f26d82b1..e47100e0 100644 --- a/build.gradle +++ b/build.gradle @@ -73,6 +73,7 @@ ext { test { enabled = true + systemProperty 'tests.security.manager', 'false' } licenseHeaders.enabled = false @@ -162,7 +163,7 @@ jacocoTestCoverageVerification { violationRules { rule { limit { - minimum = 0.0 + minimum = 0.19 } } } @@ -194,6 +195,7 @@ dependencies { compile 'com.amazon.opendistro.elasticsearch:performanceanalyzer-rca:1.11' compile 'com.fasterxml.jackson.core:jackson-annotations:2.10.4' compile 'com.fasterxml.jackson.core:jackson-databind:2.10.4' + compile 'com.fasterxml.jackson.module:jackson-module-paranamer:2.10.4' compile(group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.1') { force = 'true' } @@ -214,10 +216,11 @@ dependencies { } testCompile group: 'org.javassist', name: 'javassist', version: '3.24.0-GA' testCompile group: 'org.powermock', name: 'powermock-reflect', version: '2.0.0' - testCompile(group: 'net.bytebuddy', name: 'byte-buddy', version: '1.9.3') { + //minimum byte-buddy version to be compatible with mockito-core 2.23.0 is 1.9.7+. https://github.com/mockito/mockito/issues/1606 + testCompile(group: 'net.bytebuddy', name: 'byte-buddy', version: '1.9.7') { force = 'true' } - testCompile(group: 'net.bytebuddy', name: 'byte-buddy-agent', version: '1.9.3') { + testCompile(group: 'net.bytebuddy', name: 'byte-buddy-agent', version: '1.9.7') { force = 'true' } testCompile(group: 'org.objenesis', name: 'objenesis', version: '3.0.1') { diff --git a/licenses/paranamer-LICENSE.txt b/licenses/paranamer-LICENSE.txt new file mode 100644 index 00000000..97c8034a --- /dev/null +++ b/licenses/paranamer-LICENSE.txt @@ -0,0 +1,8 @@ +This copy of Jackson JSON processor databind module is licensed under the +Apache (Software) License, version 2.0 ("the License"). +See the License for details about distribution rights, and the +specific rights regarding derivate works. + +You may obtain a copy of the License at: + +http://www.apache.org/licenses/LICENSE-2.0 \ No newline at end of file diff --git a/licenses/paranamer-NOTICE.txt b/licenses/paranamer-NOTICE.txt new file mode 100644 index 00000000..e69de29b diff --git a/licenses/performanceanalyzer-rca-1.11.jar.sha1 b/licenses/performanceanalyzer-rca-1.11.jar.sha1 index 78938653..72a09528 100644 --- a/licenses/performanceanalyzer-rca-1.11.jar.sha1 +++ b/licenses/performanceanalyzer-rca-1.11.jar.sha1 @@ -1 +1 @@ -650b643b53a2697e6480913eae8ac602f35a4f51 \ No newline at end of file +2721b7d9d73641be5bbde80399c386dd40364e51 \ No newline at end of file diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsAllShardsMetricsCollector.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsAllShardsMetricsCollector.java index 3f06f249..9df91e19 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsAllShardsMetricsCollector.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsAllShardsMetricsCollector.java @@ -15,11 +15,19 @@ package com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.ShardStatsValue; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsProcessor; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.Utils; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.collect.ImmutableMap; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.Utils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags; @@ -29,13 +37,6 @@ import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.IndicesService; import org.elasticsearch.indices.NodeIndicesStats; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.ShardStatsValue; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsProcessor; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; -import com.fasterxml.jackson.annotation.JsonIgnore; -import com.fasterxml.jackson.annotation.JsonProperty; /** * This collector collects metrics for all shards on a node in a single run. @@ -85,7 +86,7 @@ private void populateCurrentShards() { currentShards = Utils.getShards(); } - private Map valueCalculators = new HashMap() { { + private static final Map maps = new HashMap() { { put(ShardStatsValue.INDEXING_THROTTLE_TIME.toString(), (shardStats) -> shardStats.getStats().getIndexing().getTotal().getThrottleTime().millis()); @@ -103,6 +104,8 @@ private void populateCurrentShards() { } }; + private static final ImmutableMap valueCalculators = ImmutableMap.copyOf(maps); + @Override public String getMetricsPath(long startTime, String... keysPath) { // throw exception if keysPath.length is not equal to 2 (Keys should be Index Name, and ShardId) @@ -207,7 +210,7 @@ public void populateDiffMetricValue(NodeStatsMetricsAllShardsPerCollectionStatus String.valueOf(ShardId)); } - public class NodeStatsMetricsAllShardsPerCollectionStatus extends MetricStatus { + public static class NodeStatsMetricsAllShardsPerCollectionStatus extends MetricStatus { @JsonIgnore private ShardStats shardStats; @@ -265,7 +268,6 @@ public NodeStatsMetricsAllShardsPerCollectionStatus(long queryCacheHitCount, lon this.requestCacheInBytes = requestCacheInBytes; } - private long calculate(ShardStatsValue nodeMetric) { return valueCalculators.get(nodeMetric.toString()).calculateValue(shardStats); } diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ThreadPoolMetricsCollector.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ThreadPoolMetricsCollector.java index b0235c39..cf72dd2c 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ThreadPoolMetricsCollector.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ThreadPoolMetricsCollector.java @@ -22,13 +22,6 @@ import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsProcessor; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; import com.fasterxml.jackson.annotation.JsonProperty; -import com.google.common.annotations.VisibleForTesting; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.ThreadPoolExecutor; import org.apache.commons.lang3.reflect.FieldUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -36,6 +29,13 @@ import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPoolStats.Stats; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ThreadPoolExecutor; + public class ThreadPoolMetricsCollector extends PerformanceAnalyzerMetricsCollector implements MetricsProcessor { private static final Logger LOG = LogManager.getLogger(ThreadPoolMetricsCollector.class); public static final int SAMPLING_TIME_INTERVAL = MetricsConfiguration.CONFIG_MAP.get(ThreadPoolMetricsCollector.class).samplingInterval; @@ -159,19 +159,6 @@ public ThreadPoolStatus(String type, this.queueCapacity = queueCapacity; } - // default constructor for jackson to de-serialize this class - // from json string in unit test - @VisibleForTesting - public ThreadPoolStatus() { - this.type = "testing"; - this.queueSize = -1; - this.rejected = -1; - this.threadsCount = -1; - this.threadsActive = -1; - this.queueLatency = -1.0; - this.queueCapacity = -1; - } - @JsonProperty(ThreadPoolDimension.Constants.TYPE_VALUE) public String getType() { return type; diff --git a/src/main/resources/plugin-security.policy b/src/main/resources/plugin-security.policy index 8e7432d8..2d7cf289 100644 --- a/src/main/resources/plugin-security.policy +++ b/src/main/resources/plugin-security.policy @@ -21,12 +21,16 @@ grant { //permission java.io.FilePermission "/dev/shm/-","read,readlink,write,delete,execute"; //permission java.io.FilePermission "/proc/-","read,readlink,execute,write,delete"; //permission java.io.FilePermission "/sys/block/-","read,readlink,execute,write,delete"; + permission java.io.FilePermission "build/tmp/junit_metrics", "read"; permission com.sun.tools.attach.AttachPermission "attachVirtualMachine"; permission com.sun.tools.attach.AttachPermission "createAttachProvider"; permission java.lang.RuntimePermission "manageProcess"; permission java.lang.RuntimePermission "loadLibrary.attach"; permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.misc"; permission java.lang.RuntimePermission "accessClassInPackage.sun.tools.attach"; + permission java.lang.RuntimePermission "createClassLoader"; + permission java.lang.RuntimePermission "defineClass"; + }; diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsAllShardsMetricsCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsAllShardsMetricsCollectorTests.java index 0778ad7f..421562cd 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsAllShardsMetricsCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsAllShardsMetricsCollectorTests.java @@ -15,62 +15,75 @@ 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.NodeStatsAllShardsMetricsCollector.NodeStatsMetricsAllShardsPerCollectionStatus; +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.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; -import static org.junit.Assert.assertTrue; +public class NodeStatsAllShardsMetricsCollectorTests extends ESSingleNodeTestCase { + private static final String TEST_INDEX = "test"; + private NodeStatsAllShardsMetricsCollector nodeStatsAllShardsMetricsCollector; -@Ignore -public class NodeStatsAllShardsMetricsCollectorTests extends CustomMetricsLocationTestBase { + @Before + public void init() { + IndicesService indicesService = getInstanceFromNode(IndicesService.class); + ESResources.INSTANCE.setIndicesService(indicesService); - @Test - public void testNodeStatsMetrics() { - System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); - long startTimeInMills = 1253722339; - MetricsConfiguration.CONFIG_MAP.put(NodeStatsAllShardsMetricsCollector.class, MetricsConfiguration.cdefault); + nodeStatsAllShardsMetricsCollector = new NodeStatsAllShardsMetricsCollector(null); + } - NodeStatsAllShardsMetricsCollector nodeStatsAllShardsMetricsCollector = new NodeStatsAllShardsMetricsCollector(null); - nodeStatsAllShardsMetricsCollector.saveMetricValues("89123.23", startTimeInMills, "NodesStatsIndex", "55"); + @After + public void tearDown() throws Exception { + super.tearDown(); + } + @Test + public void testCollectMetrics() throws IOException { + long startTimeInMills = 1153721339; + createIndex(TEST_INDEX); - String fetchedValue = PerformanceAnalyzerMetrics.getMetric( - PluginSettings.instance().getMetricsLocation() - + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+"/indices/NodesStatsIndex/55/"); - PerformanceAnalyzerMetrics.removeMetrics(PluginSettings.instance().getMetricsLocation() - + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)); - assertEquals("89123.23", fetchedValue); + nodeStatsAllShardsMetricsCollector.collectMetrics(startTimeInMills); + startTimeInMills += 500; + nodeStatsAllShardsMetricsCollector.collectMetrics(startTimeInMills); - try { - nodeStatsAllShardsMetricsCollector.saveMetricValues("89123.23", startTimeInMills, "NodesStatsIndex"); - assertTrue("Negative scenario test: Should have been a RuntimeException", true); - } catch (RuntimeException ex) { - //- expecting exception...only 1 values passed; 2 expected - } + List metrics = readMetrics(); + assertEquals(2, metrics.size()); - try { - nodeStatsAllShardsMetricsCollector.saveMetricValues("89123.23", startTimeInMills); - assertTrue("Negative scenario test: Should have been a RuntimeException", true); - } catch (RuntimeException ex) { - //- expecting exception...only 0 values passed; 2 expected - } + NodeStatsMetricsAllShardsPerCollectionStatus diffMetricValue = metrics.get(1); + assertEquals(0, diffMetricValue.getFieldDataEvictions()); + assertEquals(0, diffMetricValue.getFieldDataInBytes()); + assertEquals(0, diffMetricValue.getQueryCacheHitCount()); + assertEquals(0, diffMetricValue.getQueryCacheInBytes()); + assertEquals(0, diffMetricValue.getQueryCacheMissCount()); + assertEquals(0, diffMetricValue.getRequestCacheEvictions()); + assertEquals(0, diffMetricValue.getRequestCacheHitCount()); + assertEquals(0, diffMetricValue.getRequestCacheInBytes()); + assertEquals(0, diffMetricValue.getRequestCacheMissCount()); + } - try { - nodeStatsAllShardsMetricsCollector.saveMetricValues("89123.23", startTimeInMills, "NodesStatsIndex", "55", "123"); - assertTrue("Negative scenario test: Should have been a RuntimeException", true); - } catch (RuntimeException ex) { - //- expecting exception...only 3 values passed; 2 expected - } + private List readMetrics() throws IOException { + List metrics = TestUtil.readEvents(); + assert metrics.size() == 2; + ObjectMapper objectMapper = new ObjectMapper().registerModule(new ParanamerModule()); - try { - nodeStatsAllShardsMetricsCollector.getNodeIndicesStatsByShardField(); - } catch (Exception exception) { - assertTrue("There shouldn't be any exception in the code; Please check the reflection code for any changes", true); + List list = new ArrayList<>(); + for (int i = 0; i < 2; i++) { + String[] jsonStrs = metrics.get(0).value.split("\n"); + assert jsonStrs.length == 2; + list.add(objectMapper.readValue(jsonStrs[1], NodeStatsMetricsAllShardsPerCollectionStatus.class)); } + return list; } } diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsFixedShardsMetricsCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsFixedShardsMetricsCollectorTests.java index ceed7f10..926e6f23 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsFixedShardsMetricsCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/NodeStatsFixedShardsMetricsCollectorTests.java @@ -40,10 +40,10 @@ public void testNodeStatsMetrics() { String fetchedValue = PerformanceAnalyzerMetrics.getMetric( - PluginSettings.instance().getMetricsLocation() - + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+"/indices/NodesStatsIndex/55/"); + PluginSettings.instance().getMetricsLocation() + + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+"/indices/NodesStatsIndex/55/"); PerformanceAnalyzerMetrics.removeMetrics(PluginSettings.instance().getMetricsLocation() - + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)); + + PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)); assertEquals("89123.23", fetchedValue); diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ShardStateCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ShardStateCollectorTests.java index 26307d73..87cc754f 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ShardStateCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ShardStateCollectorTests.java @@ -15,46 +15,98 @@ package com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.CustomMetricsLocationTestBase; +import static com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.ShardType.SHARD_PRIMARY; +import static com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.ShardType.SHARD_REPLICA; +import static org.elasticsearch.test.ESTestCase.settings; +import static org.junit.Assert.assertEquals; + +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.MetricsConfiguration; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.TestUtil; +import com.carrotsearch.randomizedtesting.RandomizedRunner; +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.Version; +import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.routing.IndexRoutingTable; +import org.elasticsearch.cluster.routing.RoutingTable; +import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.settings.Settings; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Mockito; -import java.util.ArrayList; -import java.util.List; +@RunWith(RandomizedRunner.class) +public class ShardStateCollectorTests { + private static final String TEST_INDEX = "test"; + private static final int NUMBER_OF_PRIMARY_SHARDS = 1; + private static final int NUMBER_OF_REPLICAS = 1; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; + private ShardStateCollector shardStateCollector; + private ClusterService clusterService; + private PerformanceAnalyzerController controller; + private ConfigOverridesWrapper configOverrides; -public class ShardStateCollectorTests extends CustomMetricsLocationTestBase { + @Before + public void init() { + clusterService = Mockito.mock(ClusterService.class); + ESResources.INSTANCE.setClusterService(clusterService); - @Test - public void testShardsStateMetrics() { - MetricsConfiguration.CONFIG_MAP.put(ShardStateCollector.class, MetricsConfiguration.cdefault); System.setProperty("performanceanalyzer.metrics.log.enabled", "False"); + MetricsConfiguration.CONFIG_MAP.put(ShardStateCollector.class, MetricsConfiguration.cdefault); + controller = Mockito.mock(PerformanceAnalyzerController.class); + configOverrides = Mockito.mock(ConfigOverridesWrapper.class); + shardStateCollector = new ShardStateCollector(controller, configOverrides); + } + + @Test + public void testCollectMetrics() throws IOException { long startTimeInMills = 1153721339; - PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class); - ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class); - Mockito.when(controller.isCollectorEnabled(configOverrides, "ShardStateCollector")) + Mockito.when(controller.isCollectorEnabled(configOverrides, "ShardsStateCollector")) .thenReturn(true); - ShardStateCollector shardsStateCollector = new ShardStateCollector(controller, configOverrides); - shardsStateCollector.saveMetricValues("shard_state_metrics", startTimeInMills); - List metrics = new ArrayList<>(); - PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); - - assertEquals(1, metrics.size()); - assertEquals("shard_state_metrics", metrics.get(0).value); - - try { - shardsStateCollector.saveMetricValues("shard_state_metrics", startTimeInMills, "123"); - assertTrue("Negative scenario test: Should have been a RuntimeException", true); - } catch (RuntimeException ex) { - //- expecting exception...1 values passed; 0 expected - } + Mockito.when(clusterService.state()).thenReturn(generateClusterState()); + shardStateCollector.collectMetrics(startTimeInMills); + List metrics = readMetrics(); + assertEquals(NUMBER_OF_PRIMARY_SHARDS + NUMBER_OF_REPLICAS, metrics.size()); + assertEquals(SHARD_PRIMARY.toString(), metrics.get(0).getShardType()); + assertEquals(SHARD_REPLICA.toString(), metrics.get(1).getShardType()); } -} + private ClusterState generateClusterState() { + Metadata metaData = Metadata.builder() + .put(IndexMetadata.builder(TEST_INDEX) + .settings(settings(Version.CURRENT)) + .numberOfShards(NUMBER_OF_PRIMARY_SHARDS) + .numberOfReplicas(NUMBER_OF_REPLICAS)) + .build(); + + RoutingTable testRoutingTable = new RoutingTable.Builder() + .add(new IndexRoutingTable.Builder(metaData.index(TEST_INDEX). + getIndex()).initializeAsNew(metaData.index(TEST_INDEX)).build()) + .build(); + + return ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING + .getDefault(Settings.EMPTY)).metadata(metaData).routingTable(testRoutingTable).build(); + } + + 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 == 4; + List list = new ArrayList<>(); + list.add(objectMapper.readValue(jsonStrs[2], ShardStateCollector.ShardStateMetrics.class)); + list.add(objectMapper.readValue(jsonStrs[3], ShardStateCollector.ShardStateMetrics.class)); + return list; + } +} \ No newline at end of file diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ThreadPoolMetricsCollectorTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ThreadPoolMetricsCollectorTests.java index 01d8fe7f..851821d3 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ThreadPoolMetricsCollectorTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/collectors/ThreadPoolMetricsCollectorTests.java @@ -21,9 +21,10 @@ import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.ThreadPoolMetricsCollector.ThreadPoolStatus; 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 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; @@ -56,7 +57,7 @@ public void init() { public void testThreadPoolMetrics() { long startTimeInMills = 1453724339; threadPoolMetricsCollector.saveMetricValues("12321.5464", startTimeInMills); - List metrics = readEvents(); + List metrics = TestUtil.readEvents(); assertEquals(1, metrics.size()); assertEquals("12321.5464", metrics.get(0).value); @@ -120,16 +121,10 @@ private ThreadPoolStats generateThreadPoolStat(long rejected) { return new ThreadPoolStats(stats); } - private List readEvents() { - List metrics = new ArrayList<>(); - PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); - return metrics; - } - private ThreadPoolStatus readMetrics() throws IOException { - List metrics = readEvents(); + List metrics = TestUtil.readEvents(); assert metrics.size() == 1; - ObjectMapper objectMapper = new ObjectMapper(); + ObjectMapper objectMapper = new ObjectMapper().registerModule(new ParanamerModule()); String[] jsonStrs = metrics.get(0).value.split("\n"); assert jsonStrs.length == 2; return objectMapper.readValue(jsonStrs[1], ThreadPoolStatus.class); diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/reader/AbstractReaderTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/reader/AbstractReaderTests.java index 64131bf7..50f0d112 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/reader/AbstractReaderTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/reader/AbstractReaderTests.java @@ -21,7 +21,7 @@ import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.HeapMetricsCollector.HeapStatus; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.MasterServiceMetrics.MasterPendingStatus; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.NodeDetailsCollector.NodeDetailsStatus; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.NodeStatsAllShardsMetricsCollector; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.NodeStatsAllShardsMetricsCollector.NodeStatsMetricsAllShardsPerCollectionStatus; import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.NodeStatsFixedShardsMetricsCollector; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.GCType; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.NodeRole; @@ -38,7 +38,6 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; - import org.jooq.Condition; import org.jooq.impl.DSL; import org.junit.Ignore; @@ -148,8 +147,7 @@ protected String createShardStatMetrics(long indexingThrottleTime, versionMapMemory, bitsetMemory, shardSizeInBytes)).serialize(); - NodeStatsAllShardsMetricsCollector dummyCollectorAllShards = new NodeStatsAllShardsMetricsCollector(null); - str += (dummyCollectorAllShards.new NodeStatsMetricsAllShardsPerCollectionStatus( + str += (new NodeStatsMetricsAllShardsPerCollectionStatus( queryCacheHitCount, queryCacheMissCount, queryCacheInBytes, diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/sqlite/PerformanceAnalyzerSqliteTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/sqlite/PerformanceAnalyzerSqliteTests.java index 0299d177..138a7aa8 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/sqlite/PerformanceAnalyzerSqliteTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/sqlite/PerformanceAnalyzerSqliteTests.java @@ -15,17 +15,21 @@ package com.amazon.opendistro.elasticsearch.performanceanalyzer.sqlite; -import org.junit.Test; import static org.junit.Assert.assertEquals; + +import com.amazon.opendistro.elasticsearch.performanceanalyzer.PerformanceAnalyzerPlugin; import java.sql.Connection; +import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; -import java.sql.DriverManager; +import org.junit.Test; public class PerformanceAnalyzerSqliteTests { public PerformanceAnalyzerSqliteTests() { - System.setProperty("java.io.tmpdir", "/tmp"); + PerformanceAnalyzerPlugin.invokePrivileged(() -> { + System.setProperty("java.io.tmpdir", "/tmp"); + }); } diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/util/TestUtil.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/util/TestUtil.java new file mode 100644 index 00000000..8922bdef --- /dev/null +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/util/TestUtil.java @@ -0,0 +1,14 @@ +package com.amazon.opendistro.elasticsearch.performanceanalyzer.util; + +import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event; +import java.util.ArrayList; +import java.util.List; + +public class TestUtil { + public static List readEvents() { + List metrics = new ArrayList<>(); + PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics); + return metrics; + } +}