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
Add listeners for publisher actions #295
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cf547f6
Code changes for action listener plugin
vigyasharma aaf56f0
Remove action execute method
vigyasharma 389a335
Add tests for framework plugins controller
vigyasharma d0de8f8
Fix styling issues
vigyasharma e169ed7
Throw exceptions if unable to instantiate plugin
vigyasharma b8b5487
Remove execute from cache capacity action
vigyasharma f149144
Checkstyle fixes
vigyasharma 5d1cbfa
Remove static from PluginControllerConfig
vigyasharma 8b5388f
Fix broken UT after rebase
vigyasharma d207d65
Spotbugs fix
vigyasharma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
28 changes: 28 additions & 0 deletions
28
...on/opendistro/elasticsearch/performanceanalyzer/decisionmaker/actions/ActionListener.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,28 @@ | ||
/* | ||
* 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.decisionmaker.actions; | ||
|
||
/** | ||
* This listener is notified whenever an action suggestion is | ||
* published by the decision maker Publisher | ||
*/ | ||
public interface ActionListener { | ||
|
||
/** | ||
* Called when Publisher emits an action | ||
*/ | ||
void actionPublished(Action action); | ||
} |
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
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
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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/amazon/opendistro/elasticsearch/performanceanalyzer/plugins/Plugin.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,30 @@ | ||
/* | ||
* 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.plugins; | ||
|
||
/** | ||
* Allows adding custom extensions to the analysis graph. | ||
* | ||
* <p>RCA framework plugins can be installed to extend the analysis graph through custom | ||
* metric nodes, rca nodes, deciders or action listeners. These can subscribe to flow | ||
* units from existing nodes to add new functionality, or override existing graph nodes to | ||
* customize for specific use cases. | ||
*/ | ||
public abstract class Plugin { | ||
|
||
public abstract String name(); | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
...ava/com/amazon/opendistro/elasticsearch/performanceanalyzer/plugins/PluginController.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,84 @@ | ||
/* | ||
* 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.plugins; | ||
|
||
import com.amazon.opendistro.elasticsearch.performanceanalyzer.decisionmaker.actions.ActionListener; | ||
import com.amazon.opendistro.elasticsearch.performanceanalyzer.decisionmaker.deciders.Publisher; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class PluginController { | ||
|
||
private static final Logger LOG = LogManager.getLogger(PluginController.class); | ||
private final Publisher publisher; | ||
private List<Plugin> plugins; | ||
private PluginControllerConfig pluginControllerConfig; | ||
|
||
public PluginController(PluginControllerConfig pluginConfig, Publisher publisher) { | ||
this.pluginControllerConfig = pluginConfig; | ||
this.publisher = publisher; | ||
this.plugins = new ArrayList<>(); | ||
} | ||
|
||
public void initPlugins() { | ||
loadFrameworkPlugins(); | ||
vigyasharma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
registerActionListeners(); | ||
} | ||
|
||
private void loadFrameworkPlugins() { | ||
for (Class<?> pluginClass : pluginControllerConfig.getFrameworkPlugins()) { | ||
final Constructor<?>[] constructors = pluginClass.getConstructors(); | ||
if (constructors.length == 0) { | ||
throw new IllegalStateException( | ||
"no public constructor found for plugin class: [" + pluginClass.getName() + "]"); | ||
} | ||
if (constructors.length > 1) { | ||
throw new IllegalStateException( | ||
"unique constructor expected for plugin class: [" + pluginClass.getName() + "]"); | ||
} | ||
if (constructors[0].getParameterCount() != 0) { | ||
throw new IllegalStateException( | ||
"default constructor expected for plugin class: [" + pluginClass.getName() + "]"); | ||
} | ||
|
||
try { | ||
plugins.add((Plugin) constructors[0].newInstance()); | ||
LOG.info("loaded plugin: [{}]", plugins.get(plugins.size() - 1).name()); | ||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { | ||
LOG.error("Failed to instantiate plugin", e); | ||
throw new IllegalStateException("Failed to instantiate plugin: [" + pluginClass.getName() + "]", e); | ||
} | ||
} | ||
} | ||
|
||
private void registerActionListeners() { | ||
for (Plugin plugin: plugins) { | ||
if (ActionListener.class.isAssignableFrom(plugin.getClass())) { | ||
publisher.addActionListener((ActionListener)plugin); | ||
} | ||
} | ||
} | ||
|
||
@VisibleForTesting | ||
List<Plugin> getPlugins() { | ||
return plugins; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...m/amazon/opendistro/elasticsearch/performanceanalyzer/plugins/PluginControllerConfig.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,36 @@ | ||
/* | ||
* 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.plugins; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class PluginControllerConfig { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use a singleton pattern here instead of static? Then we can avoid the PowerMock stuff in the test class and have multiple configs in one JVM context |
||
|
||
private List<Class<? extends Plugin>> frameworkPlugins; | ||
|
||
public PluginControllerConfig() { | ||
frameworkPlugins = new ArrayList<>(); | ||
frameworkPlugins.add(PublisherEventsLogger.class); | ||
} | ||
|
||
/** | ||
* Returns a list of entry point classes for internal framework plugins | ||
*/ | ||
public List<Class<? extends Plugin>> getFrameworkPlugins() { | ||
return frameworkPlugins; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...om/amazon/opendistro/elasticsearch/performanceanalyzer/plugins/PublisherEventsLogger.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,40 @@ | ||
/* | ||
* 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.plugins; | ||
|
||
import com.amazon.opendistro.elasticsearch.performanceanalyzer.decisionmaker.actions.Action; | ||
import com.amazon.opendistro.elasticsearch.performanceanalyzer.decisionmaker.actions.ActionListener; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
/** | ||
* A simple listener that logs all actions published by the publisher | ||
*/ | ||
public class PublisherEventsLogger extends Plugin implements ActionListener { | ||
|
||
private static final Logger LOG = LogManager.getLogger(PublisherEventsLogger.class); | ||
public static final String NAME = "publisher_events_logger_plugin"; | ||
|
||
@Override | ||
public void actionPublished(Action action) { | ||
LOG.info("Action: [{}] published by decision maker publisher.", action.name()); | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return NAME; | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we make this list thread safe ? it looks to me that addActionListener() is called from a different thread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should only be called during instantiation of the graph, not during regular scheduling.