This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Common changes needed to support dynamic en/disabling of config overr…
…ides (#294) * Common changes needed to support dynamic en/disabling of config overrides * Separate serialize/deserialize methods into own helper * Add licence header to new files * Update licence year to 2020 * Add javadoc explaining one element array * Synchronize serialize and deserialize methods
- Loading branch information
Showing
13 changed files
with
354 additions
and
8 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
...amazon/opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverrides.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* POJO for config overrides. The class contains two sets of overrides, one for enabling and one | ||
* for disabling. | ||
*/ | ||
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; | ||
} | ||
|
||
/** | ||
* 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<String> rcas; | ||
private List<String> deciders; | ||
private List<String> actions; | ||
|
||
public Overrides() { | ||
this.rcas = new ArrayList<>(); | ||
this.deciders = new ArrayList<>(); | ||
this.actions = new ArrayList<>(); | ||
} | ||
|
||
public List<String> getRcas() { | ||
return rcas; | ||
} | ||
|
||
public void setRcas(List<String> rcas) { | ||
this.rcas = rcas; | ||
} | ||
|
||
public List<String> getDeciders() { | ||
return deciders; | ||
} | ||
|
||
public void setDeciders(List<String> deciders) { | ||
this.deciders = deciders; | ||
} | ||
|
||
public List<String> getActions() { | ||
return actions; | ||
} | ||
|
||
public void setActions(List<String> actions) { | ||
this.actions = actions; | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
.../opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides; | ||
|
||
import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.JsonConverter; | ||
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 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 | ||
// there instead for the caller to get the value. | ||
final IOException[] exception = new IOException[1]; | ||
final String serializedOverrides = AccessController.doPrivileged((PrivilegedAction<String>) () -> { | ||
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 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 | ||
// there instead for the caller to get the value. | ||
final IOException[] exception = new IOException[1]; | ||
final ConfigOverrides configOverrides = AccessController.doPrivileged((PrivilegedAction<ConfigOverrides>) () -> { | ||
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; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...opendistro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides; | ||
|
||
/** | ||
* Class responsible for holding the latest config overrides across the cluster. | ||
*/ | ||
public class ConfigOverridesWrapper { | ||
|
||
private volatile ConfigOverrides currentClusterConfigOverrides; | ||
private volatile long lastUpdatedTimestamp; | ||
|
||
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; | ||
} | ||
|
||
public long getLastUpdatedTimestamp() { | ||
return lastUpdatedTimestamp; | ||
} | ||
|
||
public void setLastUpdatedTimestamp(long lastUpdatedTimestamp) { | ||
this.lastUpdatedTimestamp = lastUpdatedTimestamp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...distro/elasticsearch/performanceanalyzer/config/overrides/ConfigOverridesHelperTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistro.elasticsearch.performanceanalyzer.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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.