From 85655ac16d175962e3cc6c2e045a04801cdb1303 Mon Sep 17 00:00:00 2001 From: Brian Murphy Date: Tue, 4 Nov 2014 17:03:03 +0000 Subject: [PATCH] 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@7a57dd090ad9561ebaee107c02ac360223e3febd --- .../elasticsearch/alerts/AlertManager.java | 4 ++++ .../org/elasticsearch/alerts/AlertsStore.java | 4 ---- .../alerts/actions/AlertActionsTest.java | 19 ++++++++++++++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/elasticsearch/alerts/AlertManager.java b/src/main/java/org/elasticsearch/alerts/AlertManager.java index ba71de065f5..f7f76f17f1a 100644 --- a/src/main/java/org/elasticsearch/alerts/AlertManager.java +++ b/src/main/java/org/elasticsearch/alerts/AlertManager.java @@ -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; diff --git a/src/main/java/org/elasticsearch/alerts/AlertsStore.java b/src/main/java/org/elasticsearch/alerts/AlertsStore.java index 40a90b6cb36..472f741ac47 100644 --- a/src/main/java/org/elasticsearch/alerts/AlertsStore.java +++ b/src/main/java/org/elasticsearch/alerts/AlertsStore.java @@ -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); diff --git a/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java b/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java index 14348af8356..44dc8e10c63 100644 --- a/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java +++ b/src/test/java/org/elasticsearch/alerts/actions/AlertActionsTest.java @@ -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 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()); }