|
7 | 7 |
|
8 | 8 | import org.elasticsearch.ElasticsearchException;
|
9 | 9 | import org.elasticsearch.ElasticsearchStatusException;
|
| 10 | +import org.elasticsearch.action.support.master.AcknowledgedResponse; |
| 11 | +import org.elasticsearch.cluster.metadata.MetaData; |
10 | 12 | import org.elasticsearch.common.unit.TimeValue;
|
| 13 | +import org.elasticsearch.xpack.core.ml.action.DeleteForecastAction; |
11 | 14 | import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig;
|
12 | 15 | import org.elasticsearch.xpack.core.ml.job.config.AnalysisLimits;
|
13 | 16 | import org.elasticsearch.xpack.core.ml.job.config.DataDescription;
|
@@ -276,6 +279,104 @@ public void testOverflowToDisk() throws Exception {
|
276 | 279 |
|
277 | 280 | }
|
278 | 281 |
|
| 282 | + public void testDelete() throws Exception { |
| 283 | + Detector.Builder detector = new Detector.Builder("mean", "value"); |
| 284 | + |
| 285 | + TimeValue bucketSpan = TimeValue.timeValueHours(1); |
| 286 | + AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder(Collections.singletonList(detector.build())); |
| 287 | + analysisConfig.setBucketSpan(bucketSpan); |
| 288 | + DataDescription.Builder dataDescription = new DataDescription.Builder(); |
| 289 | + dataDescription.setTimeFormat("epoch"); |
| 290 | + |
| 291 | + Job.Builder job = new Job.Builder("forecast-it-test-delete"); |
| 292 | + job.setAnalysisConfig(analysisConfig); |
| 293 | + job.setDataDescription(dataDescription); |
| 294 | + |
| 295 | + registerJob(job); |
| 296 | + putJob(job); |
| 297 | + openJob(job.getId()); |
| 298 | + |
| 299 | + long now = Instant.now().getEpochSecond(); |
| 300 | + long timestamp = now - 50 * bucketSpan.seconds(); |
| 301 | + List<String> data = new ArrayList<>(); |
| 302 | + while (timestamp < now) { |
| 303 | + data.add(createJsonRecord(createRecord(timestamp, 10.0))); |
| 304 | + data.add(createJsonRecord(createRecord(timestamp, 30.0))); |
| 305 | + timestamp += bucketSpan.seconds(); |
| 306 | + } |
| 307 | + |
| 308 | + postData(job.getId(), data.stream().collect(Collectors.joining())); |
| 309 | + flushJob(job.getId(), false); |
| 310 | + String forecastIdDefaultDurationDefaultExpiry = forecast(job.getId(), null, null); |
| 311 | + String forecastIdDuration1HourNoExpiry = forecast(job.getId(), TimeValue.timeValueHours(1), TimeValue.ZERO); |
| 312 | + waitForecastToFinish(job.getId(), forecastIdDefaultDurationDefaultExpiry); |
| 313 | + waitForecastToFinish(job.getId(), forecastIdDuration1HourNoExpiry); |
| 314 | + closeJob(job.getId()); |
| 315 | + |
| 316 | + { |
| 317 | + ForecastRequestStats forecastStats = getForecastStats(job.getId(), forecastIdDefaultDurationDefaultExpiry); |
| 318 | + assertNotNull(forecastStats); |
| 319 | + ForecastRequestStats otherStats = getForecastStats(job.getId(), forecastIdDuration1HourNoExpiry); |
| 320 | + assertNotNull(otherStats); |
| 321 | + } |
| 322 | + |
| 323 | + { |
| 324 | + DeleteForecastAction.Request request = new DeleteForecastAction.Request(job.getId(), |
| 325 | + forecastIdDefaultDurationDefaultExpiry + "," + forecastIdDuration1HourNoExpiry); |
| 326 | + AcknowledgedResponse response = client().execute(DeleteForecastAction.INSTANCE, request).actionGet(); |
| 327 | + assertTrue(response.isAcknowledged()); |
| 328 | + } |
| 329 | + |
| 330 | + { |
| 331 | + ForecastRequestStats forecastStats = getForecastStats(job.getId(), forecastIdDefaultDurationDefaultExpiry); |
| 332 | + assertNull(forecastStats); |
| 333 | + ForecastRequestStats otherStats = getForecastStats(job.getId(), forecastIdDuration1HourNoExpiry); |
| 334 | + assertNull(otherStats); |
| 335 | + } |
| 336 | + |
| 337 | + { |
| 338 | + DeleteForecastAction.Request request = new DeleteForecastAction.Request(job.getId(), "forecast-does-not-exist"); |
| 339 | + ElasticsearchException e = expectThrows(ElasticsearchException.class, |
| 340 | + () -> client().execute(DeleteForecastAction.INSTANCE, request).actionGet()); |
| 341 | + assertThat(e.getMessage(), |
| 342 | + equalTo("No forecast(s) [forecast-does-not-exist] exists for job [forecast-it-test-delete]")); |
| 343 | + } |
| 344 | + |
| 345 | + { |
| 346 | + DeleteForecastAction.Request request = new DeleteForecastAction.Request(job.getId(), MetaData.ALL); |
| 347 | + AcknowledgedResponse response = client().execute(DeleteForecastAction.INSTANCE, request).actionGet(); |
| 348 | + assertTrue(response.isAcknowledged()); |
| 349 | + } |
| 350 | + |
| 351 | + { |
| 352 | + Job.Builder otherJob = new Job.Builder("forecasts-delete-with-all-and-allow-no-forecasts"); |
| 353 | + otherJob.setAnalysisConfig(analysisConfig); |
| 354 | + otherJob.setDataDescription(dataDescription); |
| 355 | + |
| 356 | + registerJob(otherJob); |
| 357 | + putJob(otherJob); |
| 358 | + DeleteForecastAction.Request request = new DeleteForecastAction.Request(otherJob.getId(), MetaData.ALL); |
| 359 | + AcknowledgedResponse response = client().execute(DeleteForecastAction.INSTANCE, request).actionGet(); |
| 360 | + assertTrue(response.isAcknowledged()); |
| 361 | + } |
| 362 | + |
| 363 | + { |
| 364 | + Job.Builder otherJob = new Job.Builder("forecasts-delete-with-all-and-not-allow-no-forecasts"); |
| 365 | + otherJob.setAnalysisConfig(analysisConfig); |
| 366 | + otherJob.setDataDescription(dataDescription); |
| 367 | + |
| 368 | + registerJob(otherJob); |
| 369 | + putJob(otherJob); |
| 370 | + |
| 371 | + DeleteForecastAction.Request request = new DeleteForecastAction.Request(otherJob.getId(), MetaData.ALL); |
| 372 | + request.setAllowNoForecasts(false); |
| 373 | + ElasticsearchException e = expectThrows(ElasticsearchException.class, |
| 374 | + () -> client().execute(DeleteForecastAction.INSTANCE, request).actionGet()); |
| 375 | + assertThat(e.getMessage(), |
| 376 | + equalTo("No forecast(s) [_all] exists for job [forecasts-delete-with-all-and-not-allow-no-forecasts]")); |
| 377 | + } |
| 378 | + } |
| 379 | + |
279 | 380 | private void createDataWithLotsOfClientIps(TimeValue bucketSpan, Job.Builder job) throws IOException {
|
280 | 381 | long now = Instant.now().getEpochSecond();
|
281 | 382 | long timestamp = now - 15 * bucketSpan.seconds();
|
|
0 commit comments