|
20 | 20 | import org.elasticsearch.xpack.core.ml.job.config.Job; |
21 | 21 | import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex; |
22 | 22 | import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndexFields; |
| 23 | +import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.TimingStats; |
23 | 24 | import org.elasticsearch.xpack.ml.MachineLearning; |
24 | 25 | import org.junit.After; |
25 | 26 |
|
|
35 | 36 | import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; |
36 | 37 | import static org.hamcrest.Matchers.containsString; |
37 | 38 | import static org.hamcrest.Matchers.equalTo; |
| 39 | +import static org.hamcrest.Matchers.hasEntry; |
38 | 40 | import static org.hamcrest.Matchers.not; |
39 | 41 |
|
40 | 42 | public class MlJobIT extends ESRestTestCase { |
@@ -413,6 +415,55 @@ public void testDeleteJob() throws Exception { |
413 | 415 | client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats"))); |
414 | 416 | } |
415 | 417 |
|
| 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 | + |
416 | 467 | public void testDeleteJobAsync() throws Exception { |
417 | 468 | String jobId = "delete-job-async-job"; |
418 | 469 | String indexName = AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT; |
|
0 commit comments