* Throw a understandle error if an alert action doesn't exist

* Moved over to a copy on write map instead of a hash map that is protected by synchronized blocks

Original commit: elastic/x-pack-elasticsearch@285515d585
This commit is contained in:
Martijn van Groningen 2014-10-24 18:50:47 +02:00
parent 39182616c7
commit 449edcda1d
2 changed files with 19 additions and 13 deletions

View File

@ -5,45 +5,51 @@
*/
package org.elasticsearch.alerts.actions;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.alerts.Alert;
import org.elasticsearch.alerts.AlertManager;
import org.elasticsearch.alerts.AlertResult;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AlertActionManager extends AbstractComponent {
private final AlertManager alertManager;
private final Map<String, AlertActionFactory> actionImplemented;
private volatile ImmutableOpenMap<String, AlertActionFactory> actionImplemented;
@Inject
public AlertActionManager(Settings settings, AlertManager alertManager, Client client) {
super(settings);
this.alertManager = alertManager;
this.actionImplemented = new HashMap<>();
registerAction("email", new EmailAlertActionFactory());
registerAction("index", new IndexAlertActionFactory(client));
this.actionImplemented = ImmutableOpenMap.<String, AlertActionFactory>builder()
.fPut("email", new EmailAlertActionFactory())
.fPut("index", new IndexAlertActionFactory(client))
.build();
alertManager.setActionManager(this);
}
public void registerAction(String name, AlertActionFactory actionFactory){
synchronized (actionImplemented) {
actionImplemented.put(name, actionFactory);
}
actionImplemented = ImmutableOpenMap.builder(actionImplemented)
.fPut(name, actionFactory)
.build();
}
public List<AlertAction> parseActionsFromMap(Map<String,Object> actionMap) {
ImmutableOpenMap<String, AlertActionFactory> actionImplemented = this.actionImplemented;
List<AlertAction> actions = new ArrayList<>();
synchronized (actionImplemented) {
for (Map.Entry<String, Object> actionEntry : actionMap.entrySet()) {
actions.add(actionImplemented.get(actionEntry.getKey()).createAction(actionEntry.getValue()));
for (Map.Entry<String, Object> actionEntry : actionMap.entrySet()) {
AlertActionFactory factory = actionImplemented.get(actionEntry.getKey());
if (factory != null) {
actions.add(factory.createAction(actionEntry.getValue()));
} else {
throw new ElasticsearchIllegalArgumentException("No action exists with the name [" + actionEntry.getKey() + "]");
}
}
return actions;

View File

@ -31,7 +31,7 @@ import static org.hamcrest.core.Is.is;
/**
*/
@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE, numClientNodes = 0, transportClientRatio = 0, numDataNodes = 1)
@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE, numClientNodes = 0, transportClientRatio = 0, maxNumDataNodes = 3)
public class BasicAlertingTest extends ElasticsearchIntegrationTest {
@Override
@ -111,7 +111,7 @@ public class BasicAlertingTest extends ElasticsearchIntegrationTest {
1,
true,
true
);
);
alertManager.addAlert("my-first-alert", alert, true);
assertBusy(new Runnable() {
@Override