Skip to content

Commit

Permalink
Add test that proves _timing_stats document is deleted when the job i…
Browse files Browse the repository at this point in the history
…s deleted (elastic#45840)
  • Loading branch information
przemekwitek committed Aug 22, 2019
1 parent bfddaaa commit 20dfcc2
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.elasticsearch.xpack.core.ml.job.config.Job;
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex;
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndexFields;
import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.TimingStats;
import org.elasticsearch.xpack.ml.MachineLearning;
import org.junit.After;

Expand All @@ -35,6 +36,7 @@
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.not;

public class MlJobIT extends ESRestTestCase {
Expand Down Expand Up @@ -413,6 +415,55 @@ public void testDeleteJob() throws Exception {
client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")));
}

public void testDeleteJob_TimingStatsDocumentIsDeleted() throws Exception {
String jobId = "delete-job-with-timing-stats-document-job";
String indexName = AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT;
createFarequoteJob(jobId);

assertThat(
EntityUtils.toString(client().performRequest(new Request("GET", indexName + "/_count")).getEntity()),
containsString("\"count\":0")); // documents related to the job do not exist yet

Response openResponse =
client().performRequest(new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_open"));
assertThat(entityAsMap(openResponse), hasEntry("opened", true));

Request postDataRequest = new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_data");
postDataRequest.setJsonEntity("{ \"airline\":\"LOT\", \"response_time\":100, \"time\":\"2019-07-01 00:00:00Z\" }");
client().performRequest(postDataRequest);
postDataRequest.setJsonEntity("{ \"airline\":\"LOT\", \"response_time\":100, \"time\":\"2019-07-01 02:00:00Z\" }");
client().performRequest(postDataRequest);

Response flushResponse =
client().performRequest(new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_flush"));
assertThat(entityAsMap(flushResponse), hasEntry("flushed", true));

Response closeResponse =
client().performRequest(new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_close"));
assertThat(entityAsMap(closeResponse), hasEntry("closed", true));

String timingStatsDoc =
EntityUtils.toString(
client().performRequest(new Request("GET", indexName + "/_doc/" + TimingStats.documentId(jobId))).getEntity());
assertThat(timingStatsDoc, containsString("\"bucket_count\":2")); // TimingStats doc exists, 2 buckets have been processed

client().performRequest(new Request("DELETE", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId));

waitUntilIndexIsEmpty(indexName); // when job is being deleted, it also deletes all related documents from the shared index

// check that the TimingStats documents got deleted
ResponseException exception = expectThrows(
ResponseException.class,
() -> client().performRequest(new Request("GET", indexName + "/_doc/" + TimingStats.documentId(jobId))));
assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(404));

// check that the job itself is gone
exception = expectThrows(
ResponseException.class,
() -> client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")));
assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(404));
}

public void testDeleteJobAsync() throws Exception {
String jobId = "delete-job-async-job";
String indexName = AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT;
Expand Down

0 comments on commit 20dfcc2

Please sign in to comment.