Skip to content

Commit 85d55e3

Browse files
authored
Add test that proves _timing_stats document is deleted when the job is deleted (#45840) (#45854)
1 parent 2ed19b2 commit 85d55e3

File tree

1 file changed

+51
-0
lines changed
  • x-pack/plugin/ml/qa/native-multi-node-tests/src/test/java/org/elasticsearch/xpack/ml/integration

1 file changed

+51
-0
lines changed

x-pack/plugin/ml/qa/native-multi-node-tests/src/test/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.elasticsearch.xpack.core.ml.job.config.Job;
2121
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex;
2222
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndexFields;
23+
import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.TimingStats;
2324
import org.elasticsearch.xpack.ml.MachineLearning;
2425
import org.junit.After;
2526

@@ -35,6 +36,7 @@
3536
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
3637
import static org.hamcrest.Matchers.containsString;
3738
import static org.hamcrest.Matchers.equalTo;
39+
import static org.hamcrest.Matchers.hasEntry;
3840
import static org.hamcrest.Matchers.not;
3941

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

418+
public void testDeleteJob_TimingStatsDocumentIsDeleted() throws Exception {
419+
String jobId = "delete-job-with-timing-stats-document-job";
420+
String indexName = AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT;
421+
createFarequoteJob(jobId);
422+
423+
assertThat(
424+
EntityUtils.toString(client().performRequest(new Request("GET", indexName + "/_count")).getEntity()),
425+
containsString("\"count\":0")); // documents related to the job do not exist yet
426+
427+
Response openResponse =
428+
client().performRequest(new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_open"));
429+
assertThat(entityAsMap(openResponse), hasEntry("opened", true));
430+
431+
Request postDataRequest = new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_data");
432+
postDataRequest.setJsonEntity("{ \"airline\":\"LOT\", \"response_time\":100, \"time\":\"2019-07-01 00:00:00Z\" }");
433+
client().performRequest(postDataRequest);
434+
postDataRequest.setJsonEntity("{ \"airline\":\"LOT\", \"response_time\":100, \"time\":\"2019-07-01 02:00:00Z\" }");
435+
client().performRequest(postDataRequest);
436+
437+
Response flushResponse =
438+
client().performRequest(new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_flush"));
439+
assertThat(entityAsMap(flushResponse), hasEntry("flushed", true));
440+
441+
Response closeResponse =
442+
client().performRequest(new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_close"));
443+
assertThat(entityAsMap(closeResponse), hasEntry("closed", true));
444+
445+
String timingStatsDoc =
446+
EntityUtils.toString(
447+
client().performRequest(new Request("GET", indexName + "/_doc/" + TimingStats.documentId(jobId))).getEntity());
448+
assertThat(timingStatsDoc, containsString("\"bucket_count\":2")); // TimingStats doc exists, 2 buckets have been processed
449+
450+
client().performRequest(new Request("DELETE", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId));
451+
452+
waitUntilIndexIsEmpty(indexName); // when job is being deleted, it also deletes all related documents from the shared index
453+
454+
// check that the TimingStats documents got deleted
455+
ResponseException exception = expectThrows(
456+
ResponseException.class,
457+
() -> client().performRequest(new Request("GET", indexName + "/_doc/" + TimingStats.documentId(jobId))));
458+
assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(404));
459+
460+
// check that the job itself is gone
461+
exception = expectThrows(
462+
ResponseException.class,
463+
() -> client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")));
464+
assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(404));
465+
}
466+
416467
public void testDeleteJobAsync() throws Exception {
417468
String jobId = "delete-job-async-job";
418469
String indexName = AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT;

0 commit comments

Comments
 (0)