diff --git a/src/main/java/org/elasticsearch/alerts/actions/AlertActionManager.java b/src/main/java/org/elasticsearch/alerts/actions/AlertActionManager.java index 7d003ac54f2..d0e2faa08fa 100644 --- a/src/main/java/org/elasticsearch/alerts/actions/AlertActionManager.java +++ b/src/main/java/org/elasticsearch/alerts/actions/AlertActionManager.java @@ -33,6 +33,7 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.indices.IndexMissingException; import org.elasticsearch.search.SearchHit; import org.elasticsearch.threadpool.ThreadPool; @@ -125,6 +126,7 @@ public class AlertActionManager extends AbstractComponent { logger.error("Unable to check index availability", e); return false; } + try { loadQueue(); } catch (Exception e) { @@ -261,17 +263,31 @@ public class AlertActionManager extends AbstractComponent { } public void addAlertAction(Alert alert, DateTime scheduledFireTime, DateTime fireTime) throws IOException { + addAlertAction(alert, scheduledFireTime, fireTime, true); + } + + public void addAlertAction(Alert alert, DateTime scheduledFireTime, DateTime fireTime, boolean retry) throws IOException { ensureStarted(); logger.debug("Adding alert action for alert [{}]", alert.alertName()); - - AlertActionEntry entry = new AlertActionEntry(alert, scheduledFireTime, fireTime, AlertActionState.SEARCH_NEEDED); String alertHistoryIndex = getAlertHistoryIndexNameForTime(scheduledFireTime); - IndexResponse response = client.prepareIndex(alertHistoryIndex, ALERT_HISTORY_TYPE, entry.getId()) - .setSource(XContentFactory.jsonBuilder().value(entry)) - .setOpType(IndexRequest.OpType.CREATE) - .get(); + AlertActionEntry entry = new AlertActionEntry(alert, scheduledFireTime, fireTime, AlertActionState.SEARCH_NEEDED); + try { + IndexResponse response = client.prepareIndex(alertHistoryIndex, ALERT_HISTORY_TYPE, entry.getId()) + .setSource(XContentFactory.jsonBuilder().value(entry)) + .setOpType(IndexRequest.OpType.CREATE) + .get(); + entry.setVersion(response.getVersion()); + } catch (IndexMissingException ime) { + ///@TODO This really shouldn't be happening + if (retry) { + logger.error("Unable to dynamically implicitly create alert history index [" + alertHistoryIndex + "] creating explicitly"); + client.admin().indices().prepareCreate(alertHistoryIndex).get(); + addAlertAction(alert, scheduledFireTime, fireTime, false); + } else { + throw new ElasticsearchException("Unable to create alert history index [" + alertHistoryIndex + "]", ime); + } + } - entry.setVersion(response.getVersion()); long currentSize = actionsToBeProcessed.size() + 1; actionsToBeProcessed.add(entry); long currentLargestQueueSize = largestQueueSize.get();