* 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; package org.elasticsearch.alerts.actions;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.alerts.Alert; import org.elasticsearch.alerts.Alert;
import org.elasticsearch.alerts.AlertManager; import org.elasticsearch.alerts.AlertManager;
import org.elasticsearch.alerts.AlertResult; import org.elasticsearch.alerts.AlertResult;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class AlertActionManager extends AbstractComponent { public class AlertActionManager extends AbstractComponent {
private final AlertManager alertManager; private final AlertManager alertManager;
private final Map<String, AlertActionFactory> actionImplemented; private volatile ImmutableOpenMap<String, AlertActionFactory> actionImplemented;
@Inject @Inject
public AlertActionManager(Settings settings, AlertManager alertManager, Client client) { public AlertActionManager(Settings settings, AlertManager alertManager, Client client) {
super(settings); super(settings);
this.alertManager = alertManager; this.alertManager = alertManager;
this.actionImplemented = new HashMap<>(); this.actionImplemented = ImmutableOpenMap.<String, AlertActionFactory>builder()
registerAction("email", new EmailAlertActionFactory()); .fPut("email", new EmailAlertActionFactory())
registerAction("index", new IndexAlertActionFactory(client)); .fPut("index", new IndexAlertActionFactory(client))
.build();
alertManager.setActionManager(this); alertManager.setActionManager(this);
} }
public void registerAction(String name, AlertActionFactory actionFactory){ public void registerAction(String name, AlertActionFactory actionFactory){
synchronized (actionImplemented) { actionImplemented = ImmutableOpenMap.builder(actionImplemented)
actionImplemented.put(name, actionFactory); .fPut(name, actionFactory)
} .build();
} }
public List<AlertAction> parseActionsFromMap(Map<String,Object> actionMap) { public List<AlertAction> parseActionsFromMap(Map<String,Object> actionMap) {
ImmutableOpenMap<String, AlertActionFactory> actionImplemented = this.actionImplemented;
List<AlertAction> actions = new ArrayList<>(); List<AlertAction> actions = new ArrayList<>();
synchronized (actionImplemented) {
for (Map.Entry<String, Object> actionEntry : actionMap.entrySet()) { for (Map.Entry<String, Object> actionEntry : actionMap.entrySet()) {
actions.add(actionImplemented.get(actionEntry.getKey()).createAction(actionEntry.getValue())); 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; 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 { public class BasicAlertingTest extends ElasticsearchIntegrationTest {
@Override @Override