Removed checks for creating alert & history index from code and use index templates instead

Original commit: elastic/x-pack-elasticsearch@c81134701f
This commit is contained in:
Martijn van Groningen 2014-11-04 17:23:05 +01:00
parent c8c30f9b28
commit 669203b2a1
4 changed files with 6 additions and 54 deletions

View File

@ -7,9 +7,6 @@ package org.elasticsearch.alerts;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
@ -21,11 +18,9 @@ import org.elasticsearch.alerts.actions.AlertAction;
import org.elasticsearch.alerts.actions.AlertActionRegistry;
import org.elasticsearch.alerts.triggers.TriggerManager;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
@ -95,10 +90,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(String name, BytesReference alertSource) {
if (!client.admin().indices().prepareExists(ALERT_INDEX).execute().actionGet().isExists()) {
createAlertsIndex();
}
Alert alert = parseAlert(name, alertSource, 1);
if (alertMap.putIfAbsent(name, alert) == null) {
persistAlert(name, alertSource, IndexRequest.OpType.CREATE);
@ -222,10 +213,6 @@ public class AlertsStore extends AbstractComponent {
}
private void loadAlerts() {
if (!client.admin().indices().prepareExists(ALERT_INDEX).execute().actionGet().isExists()) {
createAlertsIndex();
}
SearchResponse response = client.prepareSearch()
.setSearchType(SearchType.SCAN)
.setScroll(scrollTimeout)
@ -299,13 +286,6 @@ public class AlertsStore extends AbstractComponent {
return alert;
}
private ClusterHealthStatus createAlertsIndex() {
CreateIndexResponse cir = client.admin().indices().prepareCreate(ALERT_INDEX).addMapping(ALERT_TYPE).execute().actionGet(); //TODO FIX MAPPINGS
ClusterHealthResponse actionGet = client.admin().cluster()
.health(Requests.clusterHealthRequest(ALERT_INDEX).waitForGreenStatus().waitForEvents(Priority.LANGUID).waitForRelocatingShards(0)).actionGet();
return actionGet.getStatus();
}
private enum State {
STOPPED,

View File

@ -175,9 +175,9 @@ public class AlertActionEntry implements ToXContent{
@Override
public XContentBuilder toXContent(XContentBuilder historyEntry, Params params) throws IOException {
historyEntry.startObject();
historyEntry.field("alertName", alertName);
historyEntry.field("alert_name", alertName);
historyEntry.field("triggered", triggered);
historyEntry.field("fireTime", fireTime.toDateTimeISO());
historyEntry.field("fire_time", fireTime.toDateTimeISO());
historyEntry.field(AlertActionManager.SCHEDULED_FIRE_TIME_FIELD, scheduledTime.toDateTimeISO());
historyEntry.field("trigger", trigger, params);

View File

@ -7,9 +7,6 @@ package org.elasticsearch.alerts.actions;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
@ -25,7 +22,6 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
@ -45,10 +41,10 @@ import java.util.concurrent.atomic.AtomicReference;
*/
public class AlertActionManager extends AbstractComponent {
public static final String ALERT_NAME_FIELD = "alertName";
public static final String ALERT_NAME_FIELD = "alert_name";
public static final String TRIGGERED_FIELD = "triggered";
public static final String FIRE_TIME_FIELD = "fireTime";
public static final String SCHEDULED_FIRE_TIME_FIELD = "scheduledFireTime";
public static final String FIRE_TIME_FIELD = "fire_time";
public static final String SCHEDULED_FIRE_TIME_FIELD = "scheduled_fire_time";
public static final String TRIGGER_FIELD = "trigger";
public static final String REQUEST = "request_binary";
public static final String RESPONSE = "response_binary";
@ -194,10 +190,6 @@ public class AlertActionManager extends AbstractComponent {
}
public boolean loadQueue() {
if (!client.admin().indices().prepareExists(ALERT_HISTORY_INDEX).execute().actionGet().isExists()) {
createAlertHistoryIndex();
}
//@TODO: change to scan/scroll if we get back over 100
SearchResponse searchResponse = client.prepareSearch().setSource(
"{ \"query\" : " +
@ -217,8 +209,6 @@ public class AlertActionManager extends AbstractComponent {
return true;
}
protected AlertActionEntry parseHistory(String historyId, SearchHit sh, long version) {
return parseHistory(historyId, sh.getSourceRef(), version);
}
@ -294,12 +284,7 @@ public class AlertActionManager extends AbstractComponent {
return entry;
}
public void addAlertAction(Alert alert, TriggerResult result, DateTime scheduledFireTime, DateTime fireTime) throws IOException {
if (!client.admin().indices().prepareExists(ALERT_HISTORY_INDEX).get().isExists()) {
createAlertHistoryIndex();
}
AlertActionState state = AlertActionState.NO_ACTION_NEEDED;
if (result.isTriggered() && !alert.actions().isEmpty()) {
state = AlertActionState.ACTION_NEEDED;
@ -322,19 +307,6 @@ public class AlertActionManager extends AbstractComponent {
}
}
private ClusterHealthStatus createAlertHistoryIndex() {
CreateIndexResponse cir = client.admin().indices().prepareCreate(ALERT_HISTORY_INDEX).addMapping(ALERT_HISTORY_TYPE).execute().actionGet(); //TODO FIX MAPPINGS
if (!cir.isAcknowledged()) {
logger.error("Create [{}] was not acknowledged", ALERT_HISTORY_INDEX);
}
ClusterHealthResponse actionGet = client.admin().cluster()
.health(Requests.clusterHealthRequest(ALERT_HISTORY_INDEX).waitForGreenStatus().waitForEvents(Priority.LANGUID).waitForRelocatingShards(0)).actionGet();
return actionGet.getStatus();
}
private AlertActionEntry getHistoryEntryFromIndex(String entryId) {
GetRequest getRequest = Requests.getRequest(ALERT_HISTORY_INDEX);
getRequest.type(ALERT_HISTORY_TYPE);

View File

@ -19,7 +19,7 @@ public enum AlertActionState implements ToXContent {
ACTION_UNDERWAY,
ACTION_PERFORMED;
public static final String FIELD_NAME = "AlertHistoryState";
public static final String FIELD_NAME = "state";
@Override