-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
[ML] Delete forecast API (#31134) #33218
Merged
benwtrent
merged 12 commits into
elastic:master
from
benwtrent:feature/delete-forecast-api
Sep 4, 2018
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d40b201
Delete forecast API (#31134)
benwtrent ec318a3
Merge branch 'master' into feature/delete-forecast-api
benwtrent c6d3869
Adding ability to delete more than one forecast
benwtrent 283f3fc
minor changes and adding yaml tests
benwtrent 70cdfaf
Fixing yaml tests, adjusting search and delete
benwtrent 96c53e1
Merge branch 'master' into feature/delete-forecast-api
benwtrent cd7b37c
Merge branch 'master' into feature/delete-forecast-api
benwtrent 08a32bf
making stream and parser try-with-resource
benwtrent d170da4
Merge branch 'master' into feature/delete-forecast-api
benwtrent de0bead
Merge branch 'master' into feature/delete-forecast-api
benwtrent eccab09
blacklisting tests that are meant to raise errors
benwtrent 8643d1a
Removing forbidden API utilization
benwtrent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...lugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteForecastAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.action; | ||
|
||
import org.elasticsearch.action.Action; | ||
import org.elasticsearch.action.ActionRequestBuilder; | ||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.action.support.master.AcknowledgedRequest; | ||
import org.elasticsearch.action.support.master.AcknowledgedResponse; | ||
import org.elasticsearch.client.ElasticsearchClient; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.xpack.core.ml.job.config.Job; | ||
import org.elasticsearch.xpack.core.ml.job.results.ForecastRequestStats; | ||
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; | ||
|
||
import java.io.IOException; | ||
|
||
public class DeleteForecastAction extends Action<AcknowledgedResponse> { | ||
|
||
public static final DeleteForecastAction INSTANCE = new DeleteForecastAction(); | ||
public static final String NAME = "cluster:admin/xpack/ml/job/forecast/delete"; | ||
|
||
private DeleteForecastAction() { | ||
super(NAME); | ||
} | ||
|
||
@Override | ||
public AcknowledgedResponse newResponse() { | ||
return new AcknowledgedResponse(); | ||
} | ||
|
||
public static class Request extends AcknowledgedRequest<Request> { | ||
|
||
private String jobId; | ||
private String forecastId; | ||
private boolean allowNoForecasts = true; | ||
|
||
public Request() { | ||
} | ||
|
||
public Request(String jobId, String forecastId) { | ||
this.jobId = ExceptionsHelper.requireNonNull(jobId, Job.ID.getPreferredName()); | ||
this.forecastId = ExceptionsHelper.requireNonNull(forecastId, ForecastRequestStats.FORECAST_ID.getPreferredName()); | ||
} | ||
|
||
public String getJobId() { | ||
return jobId; | ||
} | ||
|
||
public String getForecastId() { | ||
return forecastId; | ||
} | ||
|
||
public boolean isAllowNoForecasts() { | ||
return allowNoForecasts; | ||
} | ||
|
||
public void setAllowNoForecasts(boolean allowNoForecasts) { | ||
this.allowNoForecasts = allowNoForecasts; | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void readFrom(StreamInput in) throws IOException { | ||
super.readFrom(in); | ||
jobId = in.readString(); | ||
forecastId = in.readString(); | ||
allowNoForecasts = in.readBoolean(); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeString(jobId); | ||
out.writeString(forecastId); | ||
out.writeBoolean(allowNoForecasts); | ||
} | ||
} | ||
|
||
public static class RequestBuilder extends ActionRequestBuilder<Request, AcknowledgedResponse> { | ||
|
||
public RequestBuilder(ElasticsearchClient client, DeleteForecastAction action) { | ||
super(client, action, new Request()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,10 @@ | |
|
||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.ElasticsearchStatusException; | ||
import org.elasticsearch.action.support.master.AcknowledgedResponse; | ||
import org.elasticsearch.cluster.metadata.MetaData; | ||
import org.elasticsearch.common.unit.TimeValue; | ||
import org.elasticsearch.xpack.core.ml.action.DeleteForecastAction; | ||
import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; | ||
import org.elasticsearch.xpack.core.ml.job.config.AnalysisLimits; | ||
import org.elasticsearch.xpack.core.ml.job.config.DataDescription; | ||
|
@@ -276,6 +279,104 @@ public void testOverflowToDisk() throws Exception { | |
|
||
} | ||
|
||
public void testDelete() throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also add a YML test in |
||
Detector.Builder detector = new Detector.Builder("mean", "value"); | ||
|
||
TimeValue bucketSpan = TimeValue.timeValueHours(1); | ||
AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder(Collections.singletonList(detector.build())); | ||
analysisConfig.setBucketSpan(bucketSpan); | ||
DataDescription.Builder dataDescription = new DataDescription.Builder(); | ||
dataDescription.setTimeFormat("epoch"); | ||
|
||
Job.Builder job = new Job.Builder("forecast-it-test-delete"); | ||
job.setAnalysisConfig(analysisConfig); | ||
job.setDataDescription(dataDescription); | ||
|
||
registerJob(job); | ||
putJob(job); | ||
openJob(job.getId()); | ||
|
||
long now = Instant.now().getEpochSecond(); | ||
long timestamp = now - 50 * bucketSpan.seconds(); | ||
List<String> data = new ArrayList<>(); | ||
while (timestamp < now) { | ||
data.add(createJsonRecord(createRecord(timestamp, 10.0))); | ||
data.add(createJsonRecord(createRecord(timestamp, 30.0))); | ||
timestamp += bucketSpan.seconds(); | ||
} | ||
|
||
postData(job.getId(), data.stream().collect(Collectors.joining())); | ||
flushJob(job.getId(), false); | ||
String forecastIdDefaultDurationDefaultExpiry = forecast(job.getId(), null, null); | ||
String forecastIdDuration1HourNoExpiry = forecast(job.getId(), TimeValue.timeValueHours(1), TimeValue.ZERO); | ||
waitForecastToFinish(job.getId(), forecastIdDefaultDurationDefaultExpiry); | ||
waitForecastToFinish(job.getId(), forecastIdDuration1HourNoExpiry); | ||
closeJob(job.getId()); | ||
|
||
{ | ||
ForecastRequestStats forecastStats = getForecastStats(job.getId(), forecastIdDefaultDurationDefaultExpiry); | ||
assertNotNull(forecastStats); | ||
ForecastRequestStats otherStats = getForecastStats(job.getId(), forecastIdDuration1HourNoExpiry); | ||
assertNotNull(otherStats); | ||
} | ||
|
||
{ | ||
DeleteForecastAction.Request request = new DeleteForecastAction.Request(job.getId(), | ||
forecastIdDefaultDurationDefaultExpiry + "," + forecastIdDuration1HourNoExpiry); | ||
AcknowledgedResponse response = client().execute(DeleteForecastAction.INSTANCE, request).actionGet(); | ||
assertTrue(response.isAcknowledged()); | ||
} | ||
|
||
{ | ||
ForecastRequestStats forecastStats = getForecastStats(job.getId(), forecastIdDefaultDurationDefaultExpiry); | ||
assertNull(forecastStats); | ||
ForecastRequestStats otherStats = getForecastStats(job.getId(), forecastIdDuration1HourNoExpiry); | ||
assertNull(otherStats); | ||
} | ||
|
||
{ | ||
DeleteForecastAction.Request request = new DeleteForecastAction.Request(job.getId(), "forecast-does-not-exist"); | ||
ElasticsearchException e = expectThrows(ElasticsearchException.class, | ||
() -> client().execute(DeleteForecastAction.INSTANCE, request).actionGet()); | ||
assertThat(e.getMessage(), | ||
equalTo(String.format("No forecast(s) [forecast-does-not-exist] exists for job [%s]", job.getId()))); | ||
} | ||
|
||
{ | ||
DeleteForecastAction.Request request = new DeleteForecastAction.Request(job.getId(), MetaData.ALL); | ||
AcknowledgedResponse response = client().execute(DeleteForecastAction.INSTANCE, request).actionGet(); | ||
assertTrue(response.isAcknowledged()); | ||
} | ||
|
||
{ | ||
Job.Builder otherJob = new Job.Builder("forecasts-delete-with-all-and-allow-no-forecasts"); | ||
otherJob.setAnalysisConfig(analysisConfig); | ||
otherJob.setDataDescription(dataDescription); | ||
|
||
registerJob(otherJob); | ||
putJob(otherJob); | ||
DeleteForecastAction.Request request = new DeleteForecastAction.Request(otherJob.getId(), MetaData.ALL); | ||
AcknowledgedResponse response = client().execute(DeleteForecastAction.INSTANCE, request).actionGet(); | ||
assertTrue(response.isAcknowledged()); | ||
} | ||
|
||
{ | ||
Job.Builder otherJob = new Job.Builder("forecasts-delete-with-all-and-not-allow-no-forecasts"); | ||
otherJob.setAnalysisConfig(analysisConfig); | ||
otherJob.setDataDescription(dataDescription); | ||
|
||
registerJob(otherJob); | ||
putJob(otherJob); | ||
|
||
DeleteForecastAction.Request request = new DeleteForecastAction.Request(otherJob.getId(), MetaData.ALL); | ||
request.setAllowNoForecasts(false); | ||
ElasticsearchException e = expectThrows(ElasticsearchException.class, | ||
() -> client().execute(DeleteForecastAction.INSTANCE, request).actionGet()); | ||
assertThat(e.getMessage(), | ||
equalTo(String.format("No forecast(s) [_all] exists for job [forecasts-delete-with-all-and-not-allow-no-forecasts]"))); | ||
} | ||
} | ||
|
||
private void createDataWithLotsOfClientIps(TimeValue bucketSpan, Job.Builder job) throws IOException { | ||
long now = Instant.now().getEpochSecond(); | ||
long timestamp = now - 15 * bucketSpan.seconds(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Contrary to the other delete requests we currently have, this one deletes results instead of resources. I can imagine the UI allowing users to select multiple forecasts and then delete them in bulk. It would be a pain for the UI to have to perform a request per forecast. I think we should consider allowing this to handle deletion of multiple forecasts. Users can pass a comma separated list of forecast IDs.