Let the xcontent type of alert history documents be based on the xcontent type of alert documents.

Closes elastic/elasticsearch#54

Original commit: elastic/x-pack-elasticsearch@e03bf5d3bf
This commit is contained in:
Martijn van Groningen 2014-11-28 15:54:32 +01:00
parent c094430584
commit 8910a1f284
5 changed files with 56 additions and 18 deletions

View File

@ -12,6 +12,7 @@ import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
import java.util.ArrayList;
@ -26,12 +27,14 @@ public class Alert implements ToXContent {
private List<AlertAction> actions;
private String schedule;
private DateTime lastExecuteTime;
private long version;
private TimeValue throttlePeriod = new TimeValue(0);
private DateTime timeLastActionExecuted = null;
private AlertAckState ackState = AlertAckState.NOT_ACKABLE;
private Map<String,Object> metadata = null;
private transient long version;
private transient XContentType contentType;
public Alert() {
actions = new ArrayList<>();
}
@ -110,6 +113,17 @@ public class Alert implements ToXContent {
this.version = version;
}
void setContentType(XContentType contentType) {
this.contentType = contentType;
}
/**
* @return xcontext type of the _source of this action entry.
*/
public XContentType getContentType() {
return contentType;
}
/**
* @return The unique name of this alert.
*/

View File

@ -31,6 +31,7 @@ import org.elasticsearch.common.lucene.uid.Versions;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.SearchHit;
@ -40,8 +41,6 @@ import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
/**
*/
public class AlertsStore extends AbstractComponent {
@ -110,8 +109,7 @@ public class AlertsStore extends AbstractComponent {
*/
public void updateAlert(Alert alert) throws IOException {
ensureStarted();
// TODO: the content type should be based on the provided content type when the alert was initially added.
BytesReference source = jsonBuilder().value(alert).bytes();
BytesReference source = XContentFactory.contentBuilder(alert.getContentType()).value(alert).bytes();
IndexResponse response = client.index(createIndexRequest(alert.alertName(), source)).actionGet();
alert.version(response.getVersion());
// Don't need to update the alertMap, since we are working on an instance from it.
@ -241,6 +239,8 @@ public class AlertsStore extends AbstractComponent {
Alert alert = new Alert();
alert.alertName(alertName);
try (XContentParser parser = XContentHelper.createParser(source)) {
alert.setContentType(parser.contentType());
String currentFieldName = null;
XContentParser.Token token = parser.nextToken();
assert token == XContentParser.Token.START_OBJECT;

View File

@ -12,6 +12,7 @@ import org.elasticsearch.alerts.triggers.AlertTrigger;
import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
import java.util.List;
@ -23,28 +24,29 @@ import java.util.Map;
public class AlertActionEntry implements ToXContent {
private String id;
private long version;
private String alertName;
private DateTime fireTime;
private DateTime scheduledTime;
private AlertTrigger trigger;
private List<AlertAction> actions;
private AlertActionState state;
private SearchRequest searchRequest;
/*Optional*/
private Map<String, Object> searchResponse;
private boolean triggered;
private String errorMsg;
private Map<String,Object> metadata;
private transient long version;
private transient XContentType contentType;
AlertActionEntry() {
}
public AlertActionEntry(Alert alert, DateTime scheduledTime, DateTime fireTime, AlertActionState state) throws IOException {
this.id = alert.alertName() + "#" + scheduledTime.toDateTimeISO();
this.version = 1;
this.alertName = alert.alertName();
this.fireTime = fireTime;
this.scheduledTime = scheduledTime;
@ -52,6 +54,10 @@ public class AlertActionEntry implements ToXContent {
this.actions = alert.actions();
this.state = state;
this.searchRequest = alert.getSearchRequest();
this.metadata = alert.getMetadata();
this.version = 1;
this.contentType = alert.getContentType();
}
/**
@ -172,6 +178,17 @@ public class AlertActionEntry implements ToXContent {
this.version = version;
}
/**
* @return xcontext type of the _source of this action entry.
*/
public XContentType getContentType() {
return contentType;
}
void setContentType(XContentType contentType) {
this.contentType = contentType;
}
/**
* @return The error if an error occured otherwise null
*/
@ -194,8 +211,6 @@ public class AlertActionEntry implements ToXContent {
this.metadata = metadata;
}
@Override
public XContentBuilder toXContent(XContentBuilder historyEntry, Params params) throws IOException {
historyEntry.startObject();

View File

@ -206,6 +206,8 @@ public class AlertActionManager extends AbstractComponent {
entry.setVersion(version);
try (XContentParser parser = XContentHelper.createParser(source)) {
entry.setContentType(parser.contentType());
String currentFieldName = null;
XContentParser.Token token = parser.nextToken();
assert token == XContentParser.Token.START_OBJECT;
@ -269,11 +271,8 @@ public class AlertActionManager extends AbstractComponent {
logger.debug("Adding alert action for alert [{}]", alert.alertName());
String alertHistoryIndex = getAlertHistoryIndexNameForTime(scheduledFireTime);
AlertActionEntry entry = new AlertActionEntry(alert, scheduledFireTime, fireTime, AlertActionState.SEARCH_NEEDED);
if (alert.getMetadata() != null) {
entry.setMetadata(alert.getMetadata());
}
IndexResponse response = client.prepareIndex(alertHistoryIndex, ALERT_HISTORY_TYPE, entry.getId())
.setSource(XContentFactory.jsonBuilder().value(entry))
.setSource(XContentFactory.contentBuilder(alert.getContentType()).value(entry))
.setOpType(IndexRequest.OpType.CREATE)
.get();
entry.setVersion(response.getVersion());
@ -298,7 +297,7 @@ public class AlertActionManager extends AbstractComponent {
ensureStarted();
logger.debug("Updating alert action [{}]", entry.getId());
IndexResponse response = client.prepareIndex(getAlertHistoryIndexNameForTime(entry.getScheduledTime()), ALERT_HISTORY_TYPE, entry.getId())
.setSource(XContentFactory.jsonBuilder().value(entry))
.setSource(XContentFactory.contentBuilder(entry.getContentType()).value(entry))
.get();
logger.debug("Updated alert action [{}]", entry.getId());
entry.setVersion(response.getVersion());

View File

@ -34,7 +34,7 @@ import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.*;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.common.xcontent.XContentFactory.*;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
import static org.hamcrest.Matchers.*;
@ -94,7 +94,17 @@ public abstract class AbstractAlertingTests extends ElasticsearchIntegrationTest
}
protected BytesReference createAlertSource(String cron, SearchRequest request, String scriptTrigger, Map<String,Object> metadata) throws IOException {
XContentBuilder builder = jsonBuilder().startObject();
XContentBuilder builder;
if (randomBoolean()) {
builder = jsonBuilder();
} else if (randomBoolean()) {
builder = yamlBuilder();
} else if (randomBoolean()) {
builder = cborBuilder();
} else {
builder = smileBuilder();
}
builder.startObject();
builder.field("schedule", cron);
builder.field("request");
AlertUtils.writeSearchRequest(request, builder, ToXContent.EMPTY_PARAMS);