Fix missing index issue by explicitly creating the index if implicit creation fails

This really shouldn't be happening.

Original commit: elastic/x-pack-elasticsearch@7d334ec76f
This commit is contained in:
Brian Murphy 2014-11-26 18:10:52 +00:00
parent 193865925c
commit 87e056d762
1 changed files with 23 additions and 7 deletions

View File

@ -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();