mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-18 02:44:49 +00:00
Core: fix trigger search for templates
Original commit: elastic/x-pack-elasticsearch@11cdc8a396
This commit is contained in:
parent
c070e932c3
commit
f58b36b70a
@ -297,6 +297,14 @@ public class AlertsStore extends AbstractComponent {
|
||||
default:
|
||||
throw new ElasticsearchIllegalArgumentException("Unexpected field [" + searchRequestFieldName + "]");
|
||||
}
|
||||
} else if (token.isValue()) {
|
||||
switch (searchRequestFieldName) {
|
||||
case "template_name":
|
||||
searchRequest.templateName(parser.textOrNull());
|
||||
break;
|
||||
default:
|
||||
throw new ElasticsearchIllegalArgumentException("Unexpected field [" + searchRequestFieldName + "]");
|
||||
}
|
||||
} else {
|
||||
throw new ElasticsearchIllegalArgumentException("Unexpected field [" + searchRequestFieldName + "]");
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.cluster.ClusterState;
|
||||
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
|
||||
import org.elasticsearch.cluster.routing.IndexRoutingTable;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.hppc.cursors.ObjectObjectCursor;
|
||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
@ -39,7 +40,6 @@ import java.util.Random;
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
@ -79,7 +79,12 @@ public abstract class AbstractAlertingTests extends ElasticsearchIntegrationTest
|
||||
builder.field("enable", true);
|
||||
|
||||
builder.startObject("request");
|
||||
XContentHelper.writeRawField("body", request.source(), builder, ToXContent.EMPTY_PARAMS);
|
||||
if (Strings.hasLength(request.source())) {
|
||||
XContentHelper.writeRawField("body", request.source(), builder, ToXContent.EMPTY_PARAMS);
|
||||
}
|
||||
if (request.templateName() != null) {
|
||||
builder.field("template_name", request.templateName());
|
||||
}
|
||||
builder.startArray("indices");
|
||||
for (String index : request.indices()) {
|
||||
builder.value(index);
|
||||
@ -107,7 +112,7 @@ public abstract class AbstractAlertingTests extends ElasticsearchIntegrationTest
|
||||
return internalTestCluster().getInstance(AlertsClient.class);
|
||||
}
|
||||
|
||||
protected void assertAlertTriggered(final String alertName) throws Exception {
|
||||
protected void assertAlertTriggered(final String alertName, final long expectedAlertActionsWithActionPerformed) throws Exception {
|
||||
assertBusy(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@ -123,15 +128,14 @@ public abstract class AbstractAlertingTests extends ElasticsearchIntegrationTest
|
||||
.setIndicesOptions(IndicesOptions.lenientExpandOpen())
|
||||
.setQuery(boolQuery().must(matchQuery("alert_name", alertName)).must(matchQuery("state", AlertActionState.ACTION_PERFORMED.toString())))
|
||||
.addField("response.hits.total")
|
||||
.setSize(1)
|
||||
.get();
|
||||
assertThat(searchResponse.getHits().getHits().length, equalTo(1));
|
||||
assertThat(searchResponse.getHits().getTotalHits(), greaterThanOrEqualTo(expectedAlertActionsWithActionPerformed));
|
||||
assertThat((Integer) searchResponse.getHits().getAt(0).field("response.hits.total").getValue(), greaterThanOrEqualTo(1));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void assertNoAlertTrigger(final String alertName) throws Exception {
|
||||
protected void assertNoAlertTrigger(final String alertName, final long expectedAlertActionsWithNoActionNeeded) throws Exception {
|
||||
assertBusy(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@ -143,12 +147,20 @@ public abstract class AbstractAlertingTests extends ElasticsearchIntegrationTest
|
||||
assertThat(routingTable, notNullValue());
|
||||
assertThat(routingTable.allPrimaryShardsActive(), is(true));
|
||||
|
||||
// SearchResponse k = client().prepareSearch(AlertActionManager.ALERT_HISTORY_INDEX)
|
||||
// .setIndicesOptions(IndicesOptions.lenientExpandOpen())
|
||||
// .setQuery(boolQuery().must(matchQuery("alert_name", alertName)))
|
||||
// .get();
|
||||
// System.out.println("KK: " + k.getHits().getTotalHits());
|
||||
// for (SearchHit hit : k.getHits()) {
|
||||
// System.out.println("Hit " + XContentHelper.toString(hit));
|
||||
// }
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch(AlertActionManager.ALERT_HISTORY_INDEX)
|
||||
.setIndicesOptions(IndicesOptions.lenientExpandOpen())
|
||||
.setQuery(boolQuery().must(matchQuery("alert_name", alertName)).must(matchQuery("state", AlertActionState.NO_ACTION_NEEDED.toString())))
|
||||
.setSize(1)
|
||||
.get();
|
||||
assertThat(searchResponse.getHits().getHits().length, equalTo(1));
|
||||
assertThat(searchResponse.getHits().getTotalHits(), greaterThanOrEqualTo(expectedAlertActionsWithNoActionNeeded));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -13,12 +13,10 @@ import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest;
|
||||
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse;
|
||||
import org.elasticsearch.alerts.transport.actions.index.IndexAlertResponse;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.elasticsearch.index.query.FilterBuilders.rangeFilter;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
@ -43,7 +41,7 @@ public class BasicAlertingTest extends AbstractAlertingTests {
|
||||
alertsClient.prepareIndexAlert("my-first-alert")
|
||||
.setAlertSource(alertSource)
|
||||
.get();
|
||||
assertAlertTriggered("my-first-alert");
|
||||
assertAlertTriggered("my-first-alert", 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -56,11 +54,11 @@ public class BasicAlertingTest extends AbstractAlertingTests {
|
||||
.get();
|
||||
|
||||
// The alert can't trigger because there is no data that matches with the query
|
||||
assertNoAlertTrigger("my-first-alert");
|
||||
assertNoAlertTrigger("my-first-alert", 1);
|
||||
|
||||
// Index sample doc after we register the alert and the alert should get triggered
|
||||
client().prepareIndex("my-index", "my-type").setSource("field", "value").get();
|
||||
assertAlertTriggered("my-first-alert");
|
||||
assertAlertTriggered("my-first-alert", 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -123,22 +121,31 @@ public class BasicAlertingTest extends AbstractAlertingTests {
|
||||
|
||||
@Test
|
||||
public void testTriggerSearch() throws Exception {
|
||||
assertAcked(prepareCreate("my-index")
|
||||
.addMapping("my-type", "_timestamp", "enabled=true", "event_type", "type=string"));
|
||||
assertAcked(prepareCreate("my-index").addMapping("my-type", "_timestamp", "enabled=true", "event_type", "type=string"));
|
||||
client().preparePutIndexedScript()
|
||||
.setScriptLang("mustache")
|
||||
.setId("my-template")
|
||||
.setSource(jsonBuilder().startObject().field("template").value(SearchSourceBuilder.searchSource().query(
|
||||
filteredQuery(matchQuery("event_type", "a"), rangeFilter("_timestamp").from("{{SCHEDULED_FIRE_TIME}}||-30s").to("{{SCHEDULED_FIRE_TIME}}"))
|
||||
)).endObject())
|
||||
.get();
|
||||
|
||||
String alertName = "red-alert";
|
||||
long scheduleTimeInMs = 5000;
|
||||
List<SearchRequest> searchRequests = new ArrayList<>();
|
||||
searchRequests.add(
|
||||
new SearchRequest("my-index")
|
||||
.source(searchSource().query(
|
||||
filteredQuery(matchQuery("event_type", "a"), rangeFilter("_timestamp").from("<<<SCHEDULED_FIRE_TIME>>>||-30s").to("<<<SCHEDULED_FIRE_TIME>>>"))
|
||||
)
|
||||
SearchRequest[] searchRequests = new SearchRequest[]{
|
||||
new SearchRequest("my-index").source(searchSource().query(
|
||||
filteredQuery(matchQuery("event_type", "a"), rangeFilter("_timestamp").from("<<<SCHEDULED_FIRE_TIME>>>||-30s").to("<<<SCHEDULED_FIRE_TIME>>>"))
|
||||
)
|
||||
)
|
||||
// TODO: add template based search requests
|
||||
);
|
||||
// client().prepareSearch("my-index").setTemplateName("my-template").request()
|
||||
// TODO: add template source based search requests
|
||||
};
|
||||
|
||||
for (SearchRequest request : searchRequests) {
|
||||
logger.info("Running: {}", request);
|
||||
// A clean start. no data to trigger on and alert actions
|
||||
cluster().wipeIndices(AlertActionManager.ALERT_HISTORY_INDEX, "my-index");
|
||||
|
||||
alertClient().prepareDeleteAlert(alertName).get();
|
||||
alertClient().prepareIndexAlert(alertName)
|
||||
.setAlertSource(createAlertSource(String.format("0/%s * * * * ? *", (scheduleTimeInMs / 1000)), request, "return hits.total >= 3"))
|
||||
@ -155,8 +162,7 @@ public class BasicAlertingTest extends AbstractAlertingTests {
|
||||
.get();
|
||||
long timeLeft = scheduleTimeInMs - (System.currentTimeMillis() - time1);
|
||||
Thread.sleep(timeLeft);
|
||||
assertNoAlertTrigger(alertName);
|
||||
cluster().wipeIndices(AlertActionManager.ALERT_HISTORY_INDEX);
|
||||
assertNoAlertTrigger(alertName, 1);
|
||||
|
||||
time1 = System.currentTimeMillis();
|
||||
client().prepareIndex("my-index", "my-type")
|
||||
@ -165,7 +171,7 @@ public class BasicAlertingTest extends AbstractAlertingTests {
|
||||
.get();
|
||||
timeLeft = scheduleTimeInMs - (System.currentTimeMillis() - time1);
|
||||
Thread.sleep(timeLeft);
|
||||
assertNoAlertTrigger(alertName);
|
||||
assertNoAlertTrigger(alertName, 2);
|
||||
|
||||
time1 = System.currentTimeMillis();
|
||||
client().prepareIndex("my-index", "my-type")
|
||||
@ -174,7 +180,7 @@ public class BasicAlertingTest extends AbstractAlertingTests {
|
||||
.get();
|
||||
timeLeft = scheduleTimeInMs - (System.currentTimeMillis() - time1);
|
||||
Thread.sleep(timeLeft);
|
||||
assertAlertTriggered(alertName);
|
||||
assertAlertTriggered(alertName, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ public class NoMasterNodeTests extends AbstractAlertingTests {
|
||||
alertsClient.prepareIndexAlert("my-first-alert")
|
||||
.setAlertSource(alertSource)
|
||||
.get();
|
||||
assertAlertTriggered("my-first-alert");
|
||||
assertAlertTriggered("my-first-alert", 1);
|
||||
|
||||
// Stop the elected master, no new master will be elected b/c of m_m_n is set to 2
|
||||
internalTestCluster().stopCurrentMasterNode();
|
||||
@ -92,7 +92,7 @@ public class NoMasterNodeTests extends AbstractAlertingTests {
|
||||
alertsClient.prepareIndexAlert("my-second-alert")
|
||||
.setAlertSource(alertSource)
|
||||
.get();
|
||||
assertAlertTriggered("my-second-alert");
|
||||
assertAlertTriggered("my-second-alert", 2);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user