Alerts : Alert Transport Layer

This commit fixes the test and tests to make sure the alert we get back from
the alertsstore isn't null in execute alert since it may have been deleted.

Original commit: elastic/x-pack-elasticsearch@7a57dd090a
This commit is contained in:
Brian Murphy 2014-11-04 17:03:03 +00:00
parent a05fc88f0d
commit 85655ac16d
3 changed files with 20 additions and 7 deletions

View File

@ -118,6 +118,10 @@ public class AlertManager extends AbstractComponent {
public void executeAlert(String alertName, DateTime scheduledFireTime, DateTime fireTime){
ensureStarted();
Alert alert = alertsStore.getAlert(alertName);
if (alert == null) {
logger.warn("Unable to find [{}] in the alert store, perhaps it has been deleted", alertName);
return;
}
if (!alert.enabled()) {
logger.debug("Alert [{}] is not enabled", alert.alertName());
return;

View File

@ -106,10 +106,6 @@ public class AlertsStore extends AbstractComponent {
* Creates an alert with the specified and fails if an alert with the name already exists.
*/
public Alert createAlert(Alert alert) throws IOException {
if (!client.admin().indices().prepareExists(ALERT_INDEX).execute().actionGet().isExists()) {
createAlertsIndex();
}
if (alertMap.putIfAbsent(alert.alertName(), alert) == null) {
XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
alert.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS);

View File

@ -6,6 +6,7 @@
package org.elasticsearch.alerts.actions;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.ShardSearchFailure;
@ -18,6 +19,8 @@ import org.elasticsearch.alerts.client.AlertsClientInterface;
import org.elasticsearch.alerts.plugin.AlertsPlugin;
import org.elasticsearch.alerts.transport.actions.create.CreateAlertRequest;
import org.elasticsearch.alerts.transport.actions.create.CreateAlertResponse;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse;
import org.elasticsearch.alerts.transport.actions.get.GetAlertRequest;
import org.elasticsearch.alerts.transport.actions.get.GetAlertResponse;
import org.elasticsearch.alerts.transport.actions.update.UpdateAlertRequest;
@ -79,8 +82,8 @@ public class AlertActionsTest extends ElasticsearchIntegrationTest {
@Test
public void testAlertActionParser() throws Exception {
DateTime fireTime = new DateTime(DateTimeZone.UTC);
DateTime scheduledFireTime = new DateTime(DateTimeZone.UTC);
DateTime fireTime = new DateTime(DateTimeZone.UTC);
DateTime scheduledFireTime = new DateTime(DateTimeZone.UTC);
Map<String, Object> triggerMap = new HashMap<>();
@ -129,10 +132,11 @@ public class AlertActionsTest extends ElasticsearchIntegrationTest {
}
@Test
public void testCreateAlert() throws Exception {
public void testAlertActions() throws Exception {
createIndex("my-index");
createIndex(AlertsStore.ALERT_INDEX);
createIndex(AlertActionManager.ALERT_HISTORY_INDEX);
ensureGreen("my-index", AlertsStore.ALERT_INDEX, AlertActionManager.ALERT_HISTORY_INDEX);
client().preparePutIndexedScript()
@ -224,6 +228,15 @@ public class AlertActionsTest extends ElasticsearchIntegrationTest {
UpdateAlertResponse updateAlertResponse = alertsClient.updateAlert(updateAlertRequest).actionGet();
assertTrue(updateAlertResponse.success());
DeleteAlertRequest deleteAlertRequest = new DeleteAlertRequest(alert.alertName());
DeleteAlertResponse deleteAlertResponse = alertsClient.deleteAlert(deleteAlertRequest).actionGet();
assertTrue(deleteAlertResponse.success());
getAlertResponse = alertsClient.getAlert(getAlertRequest).actionGet();
assertFalse(getAlertResponse.found());
updateAlertResponse = alertsClient.updateAlert(updateAlertRequest).actionGet();
assertFalse(updateAlertResponse.success());
}