Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test that proves TimingStats document is deleted when the job is deleted #45840

Merged
merged 1 commit into from
Aug 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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