Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit bf6a4cc

Browse files
authored
Added Election Term Metric (#297)
* Changed master total PendingQueueSize to PendingQueueSize per task type * Added Election Term Metric * Formmated files * Merge conflict resolved * Addressed comments Co-authored-by: Meet Shah <[email protected]>
1 parent 130a494 commit bf6a4cc

File tree

4 files changed

+238
-0
lines changed

4 files changed

+238
-0
lines changed

src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/PerformanceAnalyzerPlugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.CircuitBreakerCollector;
2424
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.ClusterApplierServiceStatsCollector;
2525
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.DisksCollector;
26+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.ElectionTermCollector;
2627
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.FaultDetectionMetricsCollector;
2728
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.GCInfoCollector;
2829
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.HeapMetricsCollector;
@@ -238,6 +239,9 @@ public PerformanceAnalyzerPlugin(final Settings settings, final java.nio.file.Pa
238239
scheduledMetricCollectorsExecutor.addScheduledMetricCollector(
239240
new ClusterApplierServiceStatsCollector(
240241
performanceAnalyzerController, configOverridesWrapper));
242+
scheduledMetricCollectorsExecutor.addScheduledMetricCollector(
243+
new ElectionTermCollector(
244+
performanceAnalyzerController,configOverridesWrapper));
241245
scheduledMetricCollectorsExecutor.addScheduledMetricCollector(
242246
new AdmissionControlMetricsCollector());
243247
try {
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors;
17+
18+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources;
19+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.PerformanceAnalyzerApp;
20+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController;
21+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper;
22+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics;
23+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration;
24+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsProcessor;
25+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics;
26+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.rca.framework.metrics.ExceptionsAndErrors;
27+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.rca.framework.metrics.WriterMetrics;
28+
import com.fasterxml.jackson.annotation.JsonProperty;
29+
import org.apache.logging.log4j.LogManager;
30+
import org.apache.logging.log4j.Logger;
31+
32+
/**
33+
* This class starts publishing election term metric. These metric is emitted from cluster state.
34+
*/
35+
public class ElectionTermCollector extends PerformanceAnalyzerMetricsCollector implements MetricsProcessor {
36+
public static final int SAMPLING_TIME_INTERVAL = MetricsConfiguration.CONFIG_MAP.get(ElectionTermCollector.class).samplingInterval;
37+
private static final Logger LOG = LogManager.getLogger(ElectionTermCollector.class);
38+
private static final int KEYS_PATH_LENGTH = 0;
39+
private final ConfigOverridesWrapper configOverridesWrapper;
40+
private final PerformanceAnalyzerController controller;
41+
private StringBuilder value;
42+
43+
public ElectionTermCollector(PerformanceAnalyzerController controller,
44+
ConfigOverridesWrapper configOverridesWrapper) {
45+
super(SAMPLING_TIME_INTERVAL, "ElectionTermCollector");
46+
value = new StringBuilder();
47+
this.controller = controller;
48+
this.configOverridesWrapper = configOverridesWrapper;
49+
}
50+
51+
@Override
52+
public String getMetricsPath(long startTime, String... keysPath) {
53+
// throw exception if keysPath.length is not equal to 0
54+
if (keysPath.length != KEYS_PATH_LENGTH) {
55+
throw new RuntimeException("keys length should be " + KEYS_PATH_LENGTH);
56+
}
57+
58+
return PerformanceAnalyzerMetrics.generatePath(startTime, PerformanceAnalyzerMetrics.sElectionTermPath);
59+
}
60+
61+
@Override
62+
public void collectMetrics(long startTime) {
63+
if (!controller.isCollectorEnabled(configOverridesWrapper, getCollectorName())) {
64+
return;
65+
}
66+
long mCurrT = System.currentTimeMillis();
67+
try {
68+
if (ESResources.INSTANCE.getClusterService() == null
69+
|| ESResources.INSTANCE.getClusterService().state() == null) {
70+
return;
71+
}
72+
73+
value.setLength(0);
74+
value.append(PerformanceAnalyzerMetrics.getJsonCurrentMilliSeconds())
75+
.append(PerformanceAnalyzerMetrics.sMetricNewLineDelimitor);
76+
value.append(new ElectionTermMetrics(
77+
ESResources.INSTANCE.getClusterService().state()
78+
.term()).serialize());
79+
saveMetricValues(value.toString(), startTime);
80+
81+
PerformanceAnalyzerApp.WRITER_METRICS_AGGREGATOR.updateStat(
82+
WriterMetrics.ELECTION_TERM_COLLECTOR_EXECUTION_TIME, "",
83+
System.currentTimeMillis() - mCurrT);
84+
85+
} catch (Exception ex) {
86+
PerformanceAnalyzerApp.ERRORS_AND_EXCEPTIONS_AGGREGATOR.updateStat(
87+
ExceptionsAndErrors.ELECTION_TERM_COLLECTOR_ERROR, "",
88+
System.currentTimeMillis() - mCurrT);
89+
LOG.debug("Exception in Collecting Election term Metrics: {} for startTime {}",
90+
() -> ex.toString(), () -> startTime);
91+
}
92+
}
93+
94+
public static class ElectionTermMetrics extends MetricStatus {
95+
private final long electionTerm;
96+
97+
public ElectionTermMetrics(long electionTerm) {
98+
this.electionTerm = electionTerm;
99+
}
100+
101+
@JsonProperty(AllMetrics.ElectionTermValue.Constants.ELECTION_TERM_VALUE)
102+
public long getElectionTerm() {
103+
return electionTerm;
104+
}
105+
}
106+
}

src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/util/Utils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources;
1919
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.AdmissionControlMetricsCollector;
2020
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.CacheConfigMetricsCollector;
21+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.ElectionTermCollector;
2122
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.FaultDetectionMetricsCollector;
2223
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.ShardIndexingPressureMetricsCollector;
2324
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.MasterThrottlingMetricsCollector;
@@ -62,6 +63,7 @@ public static void configureMetrics() {
6263
MetricsConfiguration.CONFIG_MAP.put(ShardStateCollector.class, cdefault);
6364
MetricsConfiguration.CONFIG_MAP.put(MasterThrottlingMetricsCollector.class, cdefault);
6465
MetricsConfiguration.CONFIG_MAP.put(ClusterApplierServiceStatsCollector.class, cdefault);
66+
MetricsConfiguration.CONFIG_MAP.put(ElectionTermCollector.class, cdefault);
6567
MetricsConfiguration.CONFIG_MAP.put(ShardIndexingPressureMetricsCollector.class, cdefault);
6668
}
6769

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors;
17+
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertNull;
20+
import static org.junit.Assert.assertTrue;
21+
import static org.junit.Assert.fail;
22+
import static org.mockito.MockitoAnnotations.initMocks;
23+
24+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources;
25+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController;
26+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PluginSettings;
27+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper;
28+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.ElectionTermValue;
29+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration;
30+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics;
31+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event;
32+
import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.TestUtil;
33+
import java.util.List;
34+
import org.elasticsearch.cluster.service.ClusterService;
35+
import org.elasticsearch.test.ClusterServiceUtils;
36+
import org.elasticsearch.threadpool.TestThreadPool;
37+
import org.elasticsearch.threadpool.ThreadPool;
38+
import org.junit.After;
39+
import org.junit.Before;
40+
import org.junit.Test;
41+
import org.mockito.Mock;
42+
import org.mockito.Mockito;
43+
44+
public class ElectionTermCollectorTests {
45+
private ElectionTermCollector electionTermCollector;
46+
private long startTimeInMills = 1153721339;
47+
private ThreadPool threadPool;
48+
private PerformanceAnalyzerController controller;
49+
private ConfigOverridesWrapper configOverrides;
50+
51+
@Mock
52+
private ClusterService mockedClusterService;
53+
54+
@Before
55+
public void init() {
56+
initMocks(this);
57+
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
58+
threadPool = new TestThreadPool("test");
59+
ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool);
60+
ESResources.INSTANCE.setClusterService(clusterService);
61+
controller = Mockito.mock(PerformanceAnalyzerController.class);
62+
configOverrides = Mockito.mock(ConfigOverridesWrapper.class);
63+
64+
MetricsConfiguration.CONFIG_MAP.put(ElectionTermCollector.class, MetricsConfiguration.cdefault);
65+
electionTermCollector = new ElectionTermCollector(controller,configOverrides);
66+
67+
//clean metricQueue before running every test
68+
TestUtil.readEvents();
69+
}
70+
71+
@After
72+
public void tearDown() {
73+
threadPool.shutdownNow();
74+
}
75+
76+
@Test
77+
public void testGetMetricPath() {
78+
String expectedPath = PluginSettings.instance().getMetricsLocation()
79+
+ PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills) + "/"
80+
+ PerformanceAnalyzerMetrics.sElectionTermPath;
81+
String actualPath = electionTermCollector.getMetricsPath(startTimeInMills);
82+
assertEquals(expectedPath,actualPath);
83+
84+
try {
85+
electionTermCollector.getMetricsPath(startTimeInMills,"current");
86+
fail("Negative scenario test: Should have been a RuntimeException");
87+
} catch (RuntimeException ex){
88+
//- expecting exception...1 value passed; 0 expected
89+
}
90+
}
91+
92+
@Test
93+
public void testCollectMetrics() {
94+
electionTermCollector.collectMetrics(startTimeInMills);
95+
String jsonStr = readMetricsInJsonString(1);
96+
String[] jsonStrArray = jsonStr.split(":",2);
97+
assertTrue(jsonStrArray[0].contains(ElectionTermValue.Constants.ELECTION_TERM_VALUE));
98+
assertTrue(jsonStrArray[1].contains("0"));
99+
}
100+
101+
@Test
102+
public void testWithMockClusterService() {
103+
ESResources.INSTANCE.setClusterService(mockedClusterService);
104+
electionTermCollector.collectMetrics(startTimeInMills);
105+
String jsonStr = readMetricsInJsonString(0);
106+
assertNull(jsonStr);
107+
108+
ESResources.INSTANCE.setClusterService(null);
109+
electionTermCollector.collectMetrics(startTimeInMills);
110+
jsonStr = readMetricsInJsonString(0);
111+
assertNull(jsonStr);
112+
}
113+
114+
private String readMetricsInJsonString(int size) {
115+
List<Event> metrics = TestUtil.readEvents();
116+
assert metrics.size() == size;
117+
if (size != 0) {
118+
String[] jsonStrs = metrics.get(0).value.split("\n");
119+
assert jsonStrs.length == 2;
120+
return jsonStrs[1];
121+
} else {
122+
return null;
123+
}
124+
}
125+
}
126+

0 commit comments

Comments
 (0)