From 37be24362522aae4ffe3c15daef4bb361269c156 Mon Sep 17 00:00:00 2001 From: Karthik Kumarguru Date: Sun, 19 Jul 2020 15:55:05 -0700 Subject: [PATCH 1/6] Common changes needed to support dynamic en/disabling of config overrides --- .../config/overrides/ConfigOverrides.java | 69 ++++++++++++ .../overrides/ConfigOverridesWrapper.java | 101 ++++++++++++++++++ .../reader/ClusterDetailsEventProcessor.java | 16 ++- .../ConfigOverridesWrapperTests.java | 80 ++++++++++++++ .../rca/RcaControllerTest.java | 15 ++- .../rca/RcaTestHelper.java | 14 ++- ...lusterDetailsEventProcessorTestHelper.java | 10 +- src/test/resources/reader/1566413960000 | 2 + src/test/resources/reader/1566413965000 | 2 + src/test/resources/reader/1566413970000 | 2 + .../resources/tmp/file_rotate/rca.test.file | Bin 0 -> 4096 bytes .../rca.test.file.2020-07-20-11-21-23 | Bin 0 -> 4096 bytes 12 files changed, 303 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverrides.java create mode 100644 src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java create mode 100644 src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapperTests.java create mode 100644 src/test/resources/tmp/file_rotate/rca.test.file create mode 100644 src/test/resources/tmp/file_rotate/rca.test.file.2020-07-20-11-21-23 diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverrides.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverrides.java new file mode 100644 index 000000000..20ee3d8a9 --- /dev/null +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverrides.java @@ -0,0 +1,69 @@ +package com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides; + +import java.util.ArrayList; +import java.util.List; + +/** + * POJO for config overrides. + */ +public class ConfigOverrides { + private Overrides enable; + private Overrides disable; + + public ConfigOverrides() { + this.enable = new Overrides(); + this.disable = new Overrides(); + } + + public Overrides getEnable() { + return enable; + } + + public void setEnable(Overrides enable) { + this.enable = enable; + } + + public Overrides getDisable() { + return disable; + } + + public void setDisable(Overrides disable) { + this.disable = disable; + } + + public static class Overrides { + private List rcas; + private List deciders; + private List actions; + + public Overrides() { + this.rcas = new ArrayList<>(); + this.deciders = new ArrayList<>(); + this.actions = new ArrayList<>(); + } + + public List getRcas() { + return rcas; + } + + public void setRcas(List rcas) { + this.rcas = rcas; + } + + public List getDeciders() { + return deciders; + } + + public void setDeciders(List deciders) { + this.deciders = deciders; + } + + public List getActions() { + return actions; + } + + public void setActions(List actions) { + this.actions = actions; + } + } +} diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java new file mode 100644 index 000000000..93e8ee756 --- /dev/null +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java @@ -0,0 +1,101 @@ +package com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.security.AccessController; +import java.security.PrivilegedAction; + +/** + * Class responsible for holding the latest config overrides across the cluster. + */ +public class ConfigOverridesWrapper { + + private volatile ConfigOverrides currentClusterConfigOverrides; + private volatile long lastUpdatedTimestamp; + private final ObjectMapper mapper; + + public ConfigOverridesWrapper() { + this(new ObjectMapper()); + } + + /** + * Ctor used only for unit test purposes. + * @param mapper The object mapper instance. + */ + public ConfigOverridesWrapper(final ObjectMapper mapper) { + this.currentClusterConfigOverrides = new ConfigOverrides(); + this.mapper = mapper; + } + + public ConfigOverrides getCurrentClusterConfigOverrides() { + return currentClusterConfigOverrides; + } + + /** + * Sets a new ConfigOverrides instance as the current cluster config overrides instance. + * + * @param configOverrides the ConfigOverrides instance. + */ + public void setCurrentClusterConfigOverrides(final ConfigOverrides configOverrides) { + this.currentClusterConfigOverrides = configOverrides; + } + + /** + * Deserializes a JSON representation of the config overrides into a {@link ConfigOverrides} instance. + * + * @param overrides The JSON string representing config overrides. + * @return A {@link ConfigOverrides} instance if the JSON is valid. + * @throws IOException if conversion runs into an IOException. + */ + public ConfigOverrides deserialize(final String overrides) throws IOException { + final IOException[] exception = new IOException[1]; + final ConfigOverrides configOverrides = AccessController.doPrivileged((PrivilegedAction) () -> { + try { + return mapper.readValue(overrides, ConfigOverrides.class); + } catch (IOException ioe) { + exception[0] = ioe; + } + return null; + }); + + if (configOverrides == null && exception[0] != null) { + // re throw the exception that was consumed while deserializing. + throw exception[0]; + } + + return configOverrides; + } + + /** + * Serializes a {@link ConfigOverrides} instance to its JSON representation. + * + * @param overrides The {@link ConfigOverrides} instance. + * @return String in JSON format representing the serialized equivalent. + * @throws IOException if conversion runs into an IOException. + */ + public String serialize(final ConfigOverrides overrides) throws IOException { + final IOException[] exception = new IOException[1]; + final String serializedOverrides = AccessController.doPrivileged((PrivilegedAction) () -> { + try { + return mapper.writeValueAsString(overrides); + } catch (IOException e) { + exception[0] = e; + } + return ""; + }); + + if (serializedOverrides.isEmpty() && exception[0] != null) { + throw exception[0]; + } + + return serializedOverrides; + } + + public long getLastUpdatedTimestamp() { + return lastUpdatedTimestamp; + } + + public void setLastUpdatedTimestamp(long lastUpdatedTimestamp) { + this.lastUpdatedTimestamp = lastUpdatedTimestamp; + } +} diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/reader/ClusterDetailsEventProcessor.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/reader/ClusterDetailsEventProcessor.java index 4cd1b41a4..b11160abd 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/reader/ClusterDetailsEventProcessor.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/reader/ClusterDetailsEventProcessor.java @@ -49,27 +49,35 @@ public void finalizeProcessing() {} @Override public void processEvent(Event event) { String[] lines = event.value.split(System.lineSeparator()); - if (lines.length < 2) { - // We expect at-least 2 lines as the first line is always timestamp + if (lines.length < 4) { + // We expect at-least 4 lines as the first line is always timestamp, + // the second line is the list of overridden rca conf values, + // the third line is the timestamp of when the last override was set, // and there must be at least one ElasticSearch node in a cluster. LOG.error( - "ClusterDetails contain less items than expected. " + "Expected 2, found: {}", + "ClusterDetails contain less items than expected. " + "Expected 4, found: {}", event.value); return; } + // An example node_metrics data is something like this for a two node cluster: // {"current_time":1566414001749} + // {"overrides": {"enabled": {}, "disabled": {}} + // {"lastOverrideTimestamp":1566414001749} // {"ID":"4sqG_APMQuaQwEW17_6zwg","HOST_ADDRESS":"10.212.73.121"} // {"ID":"OVH94mKXT5ibeqvDoAyTeg","HOST_ADDRESS":"10.212.78.83"} // // The line 0 is timestamp that can be skipped. So we allocated size of // the array is one less than the list. + + + final List tmpNodesDetails = new ArrayList<>(); // Just to keep track of duplicate node ids. Set ids = new HashSet<>(); - for (int i = 1; i < lines.length; ++i) { + for (int i = 3; i < lines.length; ++i) { NodeDetails nodeDetails = new NodeDetails(lines[i]); // Include nodeIds we haven't seen so far. diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapperTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapperTests.java new file mode 100644 index 000000000..3a0395af6 --- /dev/null +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapperTests.java @@ -0,0 +1,80 @@ +package com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.JsonConverter; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + +public class ConfigOverridesWrapperTests { + private ConfigOverridesWrapper testConfigOverridesWrapper; + private final ConfigOverrides validTestOverrides = buildValidConfigOverrides(); + private final String validTestOverrideJson = JsonConverter + .writeValueAsString(validTestOverrides); + + @Mock + private ObjectMapper mockMapper; + + public ConfigOverridesWrapperTests() throws JsonProcessingException { + } + + @Before + public void setUp() throws Exception { + initMocks(this); + testConfigOverridesWrapper = new ConfigOverridesWrapper(); + testConfigOverridesWrapper.setCurrentClusterConfigOverrides(validTestOverrides); + } + + @Test + public void testSerializeSuccess() throws IOException { + String serializedOverrides = testConfigOverridesWrapper.serialize(validTestOverrides); + + assertEquals(validTestOverrideJson, serializedOverrides); + } + + @Test + public void testDeserializeSuccess() throws IOException { + ConfigOverrides deserializedOverrides = testConfigOverridesWrapper.deserialize(validTestOverrideJson); + + assertEquals(validTestOverrides.getEnable().getRcas(), deserializedOverrides.getEnable().getRcas()); + assertEquals(validTestOverrides.getEnable().getDeciders(), deserializedOverrides.getEnable().getDeciders()); + assertEquals(validTestOverrides.getEnable().getActions(), deserializedOverrides.getEnable().getActions()); + + assertEquals(validTestOverrides.getDisable().getRcas(), deserializedOverrides.getDisable().getRcas()); + assertEquals(validTestOverrides.getDisable().getDeciders(), deserializedOverrides.getDisable().getDeciders()); + assertEquals(validTestOverrides.getDisable().getActions(), deserializedOverrides.getDisable().getActions()); + } + + @Test(expected = IOException.class) + public void testSerializeInvalidOverrides() throws IOException { + when(mockMapper.writeValueAsString(any())).thenThrow(JsonParseException.class); + + testConfigOverridesWrapper = new ConfigOverridesWrapper(mockMapper); + testConfigOverridesWrapper.serialize(new ConfigOverrides()); + } + + @Test(expected = IOException.class) + public void testDeserializeNullString() throws IOException { + testConfigOverridesWrapper.deserialize("invalid json"); + } + + private ConfigOverrides buildValidConfigOverrides() { + ConfigOverrides overrides = new ConfigOverrides(); + overrides.getDisable().setRcas(Arrays.asList("rca1", "rca2")); + overrides.getDisable().setActions(Arrays.asList("action1", "action2")); + overrides.getEnable().setRcas(Arrays.asList("rca3", "rca4")); + overrides.getEnable().setDeciders(Collections.singletonList("decider1")); + + return overrides; + } +} \ No newline at end of file diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/rca/RcaControllerTest.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/rca/RcaControllerTest.java index cb8ef36a0..9d922428b 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/rca/RcaControllerTest.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/rca/RcaControllerTest.java @@ -39,6 +39,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import javax.swing.JSeparator; import org.jooq.tools.json.JSONObject; import org.junit.After; import org.junit.Assert; @@ -359,17 +360,29 @@ public void testHandlers() throws IOException { } private void setMyIp(String ip, AllMetrics.NodeRole nodeRole) { + final String separator = System.lineSeparator(); JSONObject jtime = new JSONObject(); jtime.put("current_time", 1566414001749L); + JSONObject jOverrides = new JSONObject(); + JSONObject jOverridesTimeStamp = new JSONObject(); + JSONObject jNode = new JSONObject(); jNode.put(AllMetrics.NodeDetailColumns.ID.toString(), "4sqG_APMQuaQwEW17_6zwg"); jNode.put(AllMetrics.NodeDetailColumns.HOST_ADDRESS.toString(), ip); jNode.put(AllMetrics.NodeDetailColumns.ROLE.toString(), nodeRole); ClusterDetailsEventProcessor eventProcessor = new ClusterDetailsEventProcessor(); + StringBuilder nodeDetails = new StringBuilder(); + nodeDetails.append(jtime); + nodeDetails.append(separator); + nodeDetails.append(jOverrides); + nodeDetails.append(separator); + nodeDetails.append(jOverridesTimeStamp); + nodeDetails.append(separator); + nodeDetails.append(jNode.toString()); eventProcessor.processEvent( - new Event("", jtime.toString() + System.lineSeparator() + jNode.toString(), 0)); + new Event("", nodeDetails.toString(), 0)); rcaController.getAppContext().setClusterDetailsEventProcessor(eventProcessor); } diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/rca/RcaTestHelper.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/rca/RcaTestHelper.java index 60c32e797..58c0eafb1 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/rca/RcaTestHelper.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/rca/RcaTestHelper.java @@ -131,9 +131,13 @@ public static void setEvaluationTimeForAllNodes(List connect } public static AppContext setMyIp(String ip, AllMetrics.NodeRole nodeRole) { + final String separator = System.lineSeparator(); JSONObject jtime = new JSONObject(); jtime.put("current_time", 1566414001749L); + JSONObject jOverrides = new JSONObject(); + JSONObject jOverridesTimeStamp = new JSONObject(); + JSONObject jNode = new JSONObject(); jNode.put(AllMetrics.NodeDetailColumns.ID.toString(), "4sqG_APMQuaQwEW17_6zwg"); jNode.put(AllMetrics.NodeDetailColumns.HOST_ADDRESS.toString(), ip); @@ -142,8 +146,16 @@ public static AppContext setMyIp(String ip, AllMetrics.NodeRole nodeRole) { nodeRole == AllMetrics.NodeRole.ELECTED_MASTER ? true : false); ClusterDetailsEventProcessor eventProcessor = new ClusterDetailsEventProcessor(); + StringBuilder nodeDetails = new StringBuilder(); + nodeDetails.append(jtime); + nodeDetails.append(separator); + nodeDetails.append(jOverrides); + nodeDetails.append(separator); + nodeDetails.append(jOverridesTimeStamp); + nodeDetails.append(separator); + nodeDetails.append(jNode.toString()); eventProcessor.processEvent( - new Event("", jtime.toString() + System.lineSeparator() + jNode.toString(), 0)); + new Event("", nodeDetails.toString(), 0)); AppContext appContext = new AppContext(); appContext.setClusterDetailsEventProcessor(eventProcessor); return appContext; diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/reader/ClusterDetailsEventProcessorTestHelper.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/reader/ClusterDetailsEventProcessorTestHelper.java index aaa12f3ef..61fc637e5 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/reader/ClusterDetailsEventProcessorTestHelper.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/reader/ClusterDetailsEventProcessorTestHelper.java @@ -15,7 +15,6 @@ package com.amazon.opendistro.elasticsearch.performanceanalyzer.reader; -import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics.NodeRole; import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics; import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event; @@ -24,6 +23,8 @@ import java.util.List; public class ClusterDetailsEventProcessorTestHelper extends AbstractReaderTests { + + private static final String SEPARATOR = System.getProperty("line.separator"); List nodeDetails; public ClusterDetailsEventProcessorTestHelper() throws SQLException, ClassNotFoundException { @@ -49,9 +50,14 @@ public ClusterDetailsEventProcessor generateClusterDetailsEvent() { return new ClusterDetailsEventProcessor(); } StringBuilder stringBuilder = new StringBuilder().append(PerformanceAnalyzerMetrics.getJsonCurrentMilliSeconds()); + stringBuilder.append(SEPARATOR); + // TODO: @ktkrg - Change this in the PR for #291 + stringBuilder.append("RESERVED FOR CONFIG OVERRIDES"); + stringBuilder.append(SEPARATOR); + stringBuilder.append("RESERVED FOR CONFIG OVERRIDES"); nodeDetails.stream().forEach( node -> { - stringBuilder.append(System.getProperty("line.separator")) + stringBuilder.append(SEPARATOR) .append(node); } ); diff --git a/src/test/resources/reader/1566413960000 b/src/test/resources/reader/1566413960000 index 70cfa59ac..e502443f1 100644 --- a/src/test/resources/reader/1566413960000 +++ b/src/test/resources/reader/1566413960000 @@ -35,6 +35,8 @@ $ {"CBType":"parent","CB_EstimatedSize":14142616,"CB_TrippedEvents":0,"CB_ConfiguredSize":12001502822}$ ^node_metrics {"current_time":1566413936488} +{"RESERVED FOR CONFIG OVERRIDES"} +{"RESERVED FOR CONFIG OVERRIDES"} {"ID":"4sqG_APMQuaQwEW17_6zwg","HOST_ADDRESS":"10.212.73.121"} {"ID":"OVH94mKXT5ibeqvDoAyTeg","HOST_ADDRESS":"10.212.78.83"} $ diff --git a/src/test/resources/reader/1566413965000 b/src/test/resources/reader/1566413965000 index 11d23440d..d3a9234ed 100644 --- a/src/test/resources/reader/1566413965000 +++ b/src/test/resources/reader/1566413965000 @@ -34,6 +34,8 @@ $ {"CBType":"parent","CB_EstimatedSize":1193,"CB_TrippedEvents":0,"CB_ConfiguredSize":12001502822}$ ^node_metrics {"current_time":1566413966493} +{"RESERVED FOR CONFIG OVERRIDES"} +{"RESERVED FOR CONFIG OVERRIDES"} {"ID":"4sqG_APMQuaQwEW17_6zwg","HOST_ADDRESS":"10.212.73.121"} {"ID":"OVH94mKXT5ibeqvDoAyTeg","HOST_ADDRESS":"10.212.78.83"} $ diff --git a/src/test/resources/reader/1566413970000 b/src/test/resources/reader/1566413970000 index 5f60f8c09..5cb0f229e 100644 --- a/src/test/resources/reader/1566413970000 +++ b/src/test/resources/reader/1566413970000 @@ -35,6 +35,8 @@ $ {"CBType":"parent","CB_EstimatedSize":87619025,"CB_TrippedEvents":0,"CB_ConfiguredSize":12001502822}$ ^node_metrics {"current_time":1566413996662} +{"RESERVED FOR CONFIG OVERRIDES"} +{"RESERVED FOR CONFIG OVERRIDES"} {"ID":"4sqG_APMQuaQwEW17_6zwg","HOST_ADDRESS":"10.212.73.121"} {"ID":"OVH94mKXT5ibeqvDoAyTeg","HOST_ADDRESS":"10.212.78.83"} $ diff --git a/src/test/resources/tmp/file_rotate/rca.test.file b/src/test/resources/tmp/file_rotate/rca.test.file new file mode 100644 index 0000000000000000000000000000000000000000..b2aa6b7be0f2d7dba01300b76782b74a78ae8b14 GIT binary patch literal 4096 zcmeHK&u`N(6t?5;M+FmyGWD#yWxFPY^ z@J}HmuDf#NIc=e7BqXlIPmb+;e$RgIdvW4iy*-Z=r{hctrsy-&Kp3N!lp+M(`2|fW z7StMB3SM^p(l*f39}o6Gfnbivm^_B&|C7%>veWMO@fV`l`-H#Fl*{w1kO99cgkW-3 zM;gbjV|xzu?4xss*2g<_$RE6-gQ4e~Ixf9*2N$+GqG!&C+V8wKgCWd!;S4>R7D-Z8 zJt`MY_-Z!cG)~nzF1V6$uri_|u!*uHq+wP-BnyKnOA20v*z~#zh&WvX*rzI>V!{0h zmvI*QIS;Zl%vY8MUzi^<#psmDAYyW{wtH;r!2fs;hPjeV1M@Ne;JMcPjdw4t?e^gz9+k<;ZxYxb;LuG`s#b4FB~5F(os#yWxFPY^ z@J}HmuDf#NIc=e7BqXlIPmb+;e$RgIdvW4iy*-Z=r{hctrsy-&Kp3N!lp+M(`2|fW z7StMB3SM^p(l*f39}o6Gfnbivm^_B&|C7%>veWMO@fV`l`-H#Fl*{w1kO99cgkW-3 zM;gbjV|xzu?4xss*2g<_$RE6-gQ4e~Ixf9*2N$+GqG!&C+V8wKgCWd!;S4>R7D-Z8 zJt`MY_-Z!cG)~nzF1V6$uri_|u!*uHq+wP-BnyKnOA20v*z~#zh&WvX*rzI>V!{0h zmvI*QIS;Zl%vY8MUzi^<#psmDAYyW{wtH;r!2fs;hPjeV1M@Ne;JMcPjdw4t?e^gz9+k<;ZxYxb;LuG`s#b4FB~5F(os Date: Wed, 22 Jul 2020 15:55:54 -0700 Subject: [PATCH 2/6] Separate serialize/deserialize methods into own helper --- .../overrides/ConfigOverridesHelper.java | 65 +++++++++++++++ .../overrides/ConfigOverridesWrapper.java | 70 ---------------- .../overrides/ConfigOverridesHelperTests.java | 61 ++++++++++++++ .../ConfigOverridesWrapperTests.java | 80 ------------------- 4 files changed, 126 insertions(+), 150 deletions(-) create mode 100644 src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java create mode 100644 src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelperTests.java delete mode 100644 src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapperTests.java diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java new file mode 100644 index 000000000..66e480252 --- /dev/null +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java @@ -0,0 +1,65 @@ +package com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.security.AccessController; +import java.security.PrivilegedAction; + +/** + * Class that helps with operations concerning {@link ConfigOverrides}s + */ +public class ConfigOverridesHelper { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + /** + * Serializes a {@link ConfigOverrides} instance to its JSON representation. + * + * @param overrides The {@link ConfigOverrides} instance. + * @return String in JSON format representing the serialized equivalent. + * @throws IOException if conversion runs into an IOException. + */ + public static String serialize(final ConfigOverrides overrides) throws IOException { + final IOException[] exception = new IOException[1]; + final String serializedOverrides = AccessController.doPrivileged((PrivilegedAction) () -> { + try { + return MAPPER.writeValueAsString(overrides); + } catch (IOException e) { + exception[0] = e; + } + return ""; + }); + + if (serializedOverrides.isEmpty() && exception[0] != null) { + throw exception[0]; + } + + return serializedOverrides; + } + + /** + * Deserializes a JSON representation of the config overrides into a {@link ConfigOverrides} instance. + * + * @param overrides The JSON string representing config overrides. + * @return A {@link ConfigOverrides} instance if the JSON is valid. + * @throws IOException if conversion runs into an IOException. + */ + public static ConfigOverrides deserialize(final String overrides) throws IOException { + final IOException[] exception = new IOException[1]; + final ConfigOverrides configOverrides = AccessController.doPrivileged((PrivilegedAction) () -> { + try { + return MAPPER.readValue(overrides, ConfigOverrides.class); + } catch (IOException ioe) { + exception[0] = ioe; + } + return null; + }); + + if (configOverrides == null && exception[0] != null) { + // re throw the exception that was consumed while deserializing. + throw exception[0]; + } + + return configOverrides; + } +} diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java index 93e8ee756..30f1487c0 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java @@ -1,10 +1,5 @@ package com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.IOException; -import java.security.AccessController; -import java.security.PrivilegedAction; - /** * Class responsible for holding the latest config overrides across the cluster. */ @@ -12,20 +7,6 @@ public class ConfigOverridesWrapper { private volatile ConfigOverrides currentClusterConfigOverrides; private volatile long lastUpdatedTimestamp; - private final ObjectMapper mapper; - - public ConfigOverridesWrapper() { - this(new ObjectMapper()); - } - - /** - * Ctor used only for unit test purposes. - * @param mapper The object mapper instance. - */ - public ConfigOverridesWrapper(final ObjectMapper mapper) { - this.currentClusterConfigOverrides = new ConfigOverrides(); - this.mapper = mapper; - } public ConfigOverrides getCurrentClusterConfigOverrides() { return currentClusterConfigOverrides; @@ -40,57 +21,6 @@ public void setCurrentClusterConfigOverrides(final ConfigOverrides configOverrid this.currentClusterConfigOverrides = configOverrides; } - /** - * Deserializes a JSON representation of the config overrides into a {@link ConfigOverrides} instance. - * - * @param overrides The JSON string representing config overrides. - * @return A {@link ConfigOverrides} instance if the JSON is valid. - * @throws IOException if conversion runs into an IOException. - */ - public ConfigOverrides deserialize(final String overrides) throws IOException { - final IOException[] exception = new IOException[1]; - final ConfigOverrides configOverrides = AccessController.doPrivileged((PrivilegedAction) () -> { - try { - return mapper.readValue(overrides, ConfigOverrides.class); - } catch (IOException ioe) { - exception[0] = ioe; - } - return null; - }); - - if (configOverrides == null && exception[0] != null) { - // re throw the exception that was consumed while deserializing. - throw exception[0]; - } - - return configOverrides; - } - - /** - * Serializes a {@link ConfigOverrides} instance to its JSON representation. - * - * @param overrides The {@link ConfigOverrides} instance. - * @return String in JSON format representing the serialized equivalent. - * @throws IOException if conversion runs into an IOException. - */ - public String serialize(final ConfigOverrides overrides) throws IOException { - final IOException[] exception = new IOException[1]; - final String serializedOverrides = AccessController.doPrivileged((PrivilegedAction) () -> { - try { - return mapper.writeValueAsString(overrides); - } catch (IOException e) { - exception[0] = e; - } - return ""; - }); - - if (serializedOverrides.isEmpty() && exception[0] != null) { - throw exception[0]; - } - - return serializedOverrides; - } - public long getLastUpdatedTimestamp() { return lastUpdatedTimestamp; } diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelperTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelperTests.java new file mode 100644 index 000000000..9cf9eab91 --- /dev/null +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelperTests.java @@ -0,0 +1,61 @@ +package com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides; + +import static org.junit.Assert.assertEquals; + +import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.JsonConverter; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import org.junit.Before; +import org.junit.Test; + +public class ConfigOverridesHelperTests { + private ConfigOverridesWrapper testConfigOverridesWrapper; + private final ConfigOverrides validTestOverrides = buildValidConfigOverrides(); + private final String validTestOverrideJson = JsonConverter + .writeValueAsString(validTestOverrides); + + @Before + public void setUp() { + testConfigOverridesWrapper = new ConfigOverridesWrapper(); + testConfigOverridesWrapper.setCurrentClusterConfigOverrides(validTestOverrides); + } + + @Test + public void testSerializeSuccess() throws IOException { + String serializedOverrides = ConfigOverridesHelper.serialize(validTestOverrides); + + assertEquals(validTestOverrideJson, serializedOverrides); + } + + @Test + public void testDeserializeSuccess() throws IOException { + ConfigOverrides deserializedOverrides = + ConfigOverridesHelper.deserialize(validTestOverrideJson); + + assertEquals(validTestOverrides.getEnable().getRcas(), deserializedOverrides.getEnable().getRcas()); + assertEquals(validTestOverrides.getEnable().getDeciders(), deserializedOverrides.getEnable().getDeciders()); + assertEquals(validTestOverrides.getEnable().getActions(), deserializedOverrides.getEnable().getActions()); + + assertEquals(validTestOverrides.getDisable().getRcas(), deserializedOverrides.getDisable().getRcas()); + assertEquals(validTestOverrides.getDisable().getDeciders(), deserializedOverrides.getDisable().getDeciders()); + assertEquals(validTestOverrides.getDisable().getActions(), deserializedOverrides.getDisable().getActions()); + } + + @Test(expected = IOException.class) + public void testDeserializeIOException() throws IOException { + String nonJsonString = "Not a JSON string."; + ConfigOverridesHelper.deserialize(nonJsonString); + } + + private ConfigOverrides buildValidConfigOverrides() { + ConfigOverrides overrides = new ConfigOverrides(); + overrides.getDisable().setRcas(Arrays.asList("rca1", "rca2")); + overrides.getDisable().setActions(Arrays.asList("action1", "action2")); + overrides.getEnable().setRcas(Arrays.asList("rca3", "rca4")); + overrides.getEnable().setDeciders(Collections.singletonList("decider1")); + + return overrides; + } + +} \ No newline at end of file diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapperTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapperTests.java deleted file mode 100644 index 3a0395af6..000000000 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapperTests.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides; - -import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; -import static org.mockito.MockitoAnnotations.initMocks; - -import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.JsonConverter; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.IOException; -import java.util.Arrays; -import java.util.Collections; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; - -public class ConfigOverridesWrapperTests { - private ConfigOverridesWrapper testConfigOverridesWrapper; - private final ConfigOverrides validTestOverrides = buildValidConfigOverrides(); - private final String validTestOverrideJson = JsonConverter - .writeValueAsString(validTestOverrides); - - @Mock - private ObjectMapper mockMapper; - - public ConfigOverridesWrapperTests() throws JsonProcessingException { - } - - @Before - public void setUp() throws Exception { - initMocks(this); - testConfigOverridesWrapper = new ConfigOverridesWrapper(); - testConfigOverridesWrapper.setCurrentClusterConfigOverrides(validTestOverrides); - } - - @Test - public void testSerializeSuccess() throws IOException { - String serializedOverrides = testConfigOverridesWrapper.serialize(validTestOverrides); - - assertEquals(validTestOverrideJson, serializedOverrides); - } - - @Test - public void testDeserializeSuccess() throws IOException { - ConfigOverrides deserializedOverrides = testConfigOverridesWrapper.deserialize(validTestOverrideJson); - - assertEquals(validTestOverrides.getEnable().getRcas(), deserializedOverrides.getEnable().getRcas()); - assertEquals(validTestOverrides.getEnable().getDeciders(), deserializedOverrides.getEnable().getDeciders()); - assertEquals(validTestOverrides.getEnable().getActions(), deserializedOverrides.getEnable().getActions()); - - assertEquals(validTestOverrides.getDisable().getRcas(), deserializedOverrides.getDisable().getRcas()); - assertEquals(validTestOverrides.getDisable().getDeciders(), deserializedOverrides.getDisable().getDeciders()); - assertEquals(validTestOverrides.getDisable().getActions(), deserializedOverrides.getDisable().getActions()); - } - - @Test(expected = IOException.class) - public void testSerializeInvalidOverrides() throws IOException { - when(mockMapper.writeValueAsString(any())).thenThrow(JsonParseException.class); - - testConfigOverridesWrapper = new ConfigOverridesWrapper(mockMapper); - testConfigOverridesWrapper.serialize(new ConfigOverrides()); - } - - @Test(expected = IOException.class) - public void testDeserializeNullString() throws IOException { - testConfigOverridesWrapper.deserialize("invalid json"); - } - - private ConfigOverrides buildValidConfigOverrides() { - ConfigOverrides overrides = new ConfigOverrides(); - overrides.getDisable().setRcas(Arrays.asList("rca1", "rca2")); - overrides.getDisable().setActions(Arrays.asList("action1", "action2")); - overrides.getEnable().setRcas(Arrays.asList("rca3", "rca4")); - overrides.getEnable().setDeciders(Collections.singletonList("decider1")); - - return overrides; - } -} \ No newline at end of file From a795e6d8ac1063942153193341c725830956da86 Mon Sep 17 00:00:00 2001 From: Karthik Kumarguru Date: Mon, 27 Jul 2020 09:28:11 -0700 Subject: [PATCH 3/6] Add licence header to new files --- .../config/overrides/ConfigOverrides.java | 23 ++++++++++++++++++- .../overrides/ConfigOverridesHelper.java | 15 ++++++++++++ .../overrides/ConfigOverridesWrapper.java | 15 ++++++++++++ .../overrides/ConfigOverridesHelperTests.java | 15 ++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverrides.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverrides.java index 20ee3d8a9..0e3895791 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverrides.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverrides.java @@ -1,10 +1,26 @@ +/* + * Copyright 2019 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.config.overrides; import java.util.ArrayList; import java.util.List; /** - * POJO for config overrides. + * POJO for config overrides. The class contains two sets of overrides, one for enabling and one + * for disabling. */ public class ConfigOverrides { private Overrides enable; @@ -31,6 +47,11 @@ public void setDisable(Overrides disable) { this.disable = disable; } + /** + * Class containing the overridable attributes of the system. Currently, overriding the + * enabled/disabled state for RCAs, deciders, and actions are supported. More attributes can + * be added as needed. + */ public static class Overrides { private List rcas; private List deciders; diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java index 66e480252..b823eff38 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java @@ -1,3 +1,18 @@ +/* + * Copyright 2019 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.config.overrides; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java index 30f1487c0..f021aa7e5 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java @@ -1,3 +1,18 @@ +/* + * Copyright 2019 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.config.overrides; /** diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelperTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelperTests.java index 9cf9eab91..12227774a 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelperTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelperTests.java @@ -1,3 +1,18 @@ +/* + * Copyright 2019 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.config.overrides; import static org.junit.Assert.assertEquals; From b0adabd76d6e3f6c50316191ce918af36c9fc9d5 Mon Sep 17 00:00:00 2001 From: Karthik Kumarguru Date: Mon, 27 Jul 2020 11:08:09 -0700 Subject: [PATCH 4/6] Update licence year to 2020 --- .../performanceanalyzer/config/overrides/ConfigOverrides.java | 2 +- .../config/overrides/ConfigOverridesHelper.java | 2 +- .../config/overrides/ConfigOverridesWrapper.java | 2 +- .../config/overrides/ConfigOverridesHelperTests.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverrides.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverrides.java index 0e3895791..e77b2097d 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverrides.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverrides.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * 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. diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java index b823eff38..016b93fc9 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * 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. diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java index f021aa7e5..812e89b1e 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * 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. diff --git a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelperTests.java b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelperTests.java index 12227774a..663707487 100644 --- a/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelperTests.java +++ b/src/test/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelperTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * 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. From 6efceb31aa660d3c22b6c13e09701d42251a3a26 Mon Sep 17 00:00:00 2001 From: Karthik Kumarguru Date: Mon, 27 Jul 2020 12:01:41 -0700 Subject: [PATCH 5/6] Add javadoc explaining one element array --- .../config/overrides/ConfigOverridesHelper.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java index 016b93fc9..391d78c33 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java @@ -15,6 +15,7 @@ package com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides; +import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.JsonConverter; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.security.AccessController; @@ -35,6 +36,10 @@ public class ConfigOverridesHelper { * @throws IOException if conversion runs into an IOException. */ public static String serialize(final ConfigOverrides overrides) throws IOException { + // We can't use a local variable to set the exception generated inside the lambda as the + // local variable is not effectively final(because we'll end up mutating the reference). + // In order to fish the exception out, we need to create a wrapper and set the exception + // there instead for the caller to get the value. final IOException[] exception = new IOException[1]; final String serializedOverrides = AccessController.doPrivileged((PrivilegedAction) () -> { try { @@ -60,6 +65,10 @@ public static String serialize(final ConfigOverrides overrides) throws IOExcepti * @throws IOException if conversion runs into an IOException. */ public static ConfigOverrides deserialize(final String overrides) throws IOException { + // We can't use a local variable to set the exception generated inside the lambda as the + // local variable is not effectively final(because we'll end up mutating the reference). + // In order to fish the exception out, we need to create a wrapper and set the exception + // there instead for the caller to get the value. final IOException[] exception = new IOException[1]; final ConfigOverrides configOverrides = AccessController.doPrivileged((PrivilegedAction) () -> { try { From fdbf2c95de96f9d6bdb46508f829e14a656bd4a6 Mon Sep 17 00:00:00 2001 From: Karthik Kumarguru Date: Mon, 27 Jul 2020 23:13:16 -0700 Subject: [PATCH 6/6] Synchronize serialize and deserialize methods --- .../config/overrides/ConfigOverridesHelper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java index 391d78c33..00a28f5f1 100644 --- a/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java +++ b/src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java @@ -35,7 +35,7 @@ public class ConfigOverridesHelper { * @return String in JSON format representing the serialized equivalent. * @throws IOException if conversion runs into an IOException. */ - public static String serialize(final ConfigOverrides overrides) throws IOException { + public static synchronized String serialize(final ConfigOverrides overrides) throws IOException { // We can't use a local variable to set the exception generated inside the lambda as the // local variable is not effectively final(because we'll end up mutating the reference). // In order to fish the exception out, we need to create a wrapper and set the exception @@ -64,7 +64,7 @@ public static String serialize(final ConfigOverrides overrides) throws IOExcepti * @return A {@link ConfigOverrides} instance if the JSON is valid. * @throws IOException if conversion runs into an IOException. */ - public static ConfigOverrides deserialize(final String overrides) throws IOException { + public static synchronized ConfigOverrides deserialize(final String overrides) throws IOException { // We can't use a local variable to set the exception generated inside the lambda as the // local variable is not effectively final(because we'll end up mutating the reference). // In order to fish the exception out, we need to create a wrapper and set the exception