Skip to content

Commit 3d71bf6

Browse files
committed
[HLRC][ML] Add delete expired data API
Relates to elastic#29827
1 parent cd822b7 commit 3d71bf6

File tree

12 files changed

+586
-0
lines changed

12 files changed

+586
-0
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/MLRequestConverters.java

+12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.client.ml.DeleteCalendarJobRequest;
3333
import org.elasticsearch.client.ml.DeleteCalendarRequest;
3434
import org.elasticsearch.client.ml.DeleteDatafeedRequest;
35+
import org.elasticsearch.client.ml.DeleteExpiredDataRequest;
3536
import org.elasticsearch.client.ml.DeleteFilterRequest;
3637
import org.elasticsearch.client.ml.DeleteForecastRequest;
3738
import org.elasticsearch.client.ml.DeleteJobRequest;
@@ -155,6 +156,17 @@ static Request closeJob(CloseJobRequest closeJobRequest) throws IOException {
155156
return request;
156157
}
157158

159+
static Request deleteExpiredData(DeleteExpiredDataRequest deleteExpiredDataRequest) {
160+
String endpoint = new EndpointBuilder()
161+
.addPathPartAsIs("_xpack")
162+
.addPathPartAsIs("ml")
163+
.addPathPartAsIs("_delete_expired_data")
164+
.build();
165+
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
166+
167+
return request;
168+
}
169+
158170
static Request deleteJob(DeleteJobRequest deleteJobRequest) {
159171
String endpoint = new EndpointBuilder()
160172
.addPathPartAsIs("_xpack")

client/rest-high-level/src/main/java/org/elasticsearch/client/MachineLearningClient.java

+44
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.elasticsearch.client.ml.DeleteCalendarJobRequest;
2727
import org.elasticsearch.client.ml.DeleteCalendarRequest;
2828
import org.elasticsearch.client.ml.DeleteDatafeedRequest;
29+
import org.elasticsearch.client.ml.DeleteExpiredDataRequest;
30+
import org.elasticsearch.client.ml.DeleteExpiredDataResponse;
2931
import org.elasticsearch.client.ml.DeleteFilterRequest;
3032
import org.elasticsearch.client.ml.DeleteForecastRequest;
3133
import org.elasticsearch.client.ml.DeleteJobRequest;
@@ -227,6 +229,48 @@ public void getJobStatsAsync(GetJobStatsRequest request, RequestOptions options,
227229
Collections.emptySet());
228230
}
229231

232+
/**
233+
* Deletes expired data from Machine Learning Jobs
234+
* <p>
235+
* For additional info
236+
* see <a href="http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-delete-expired-data.html">ML Delete Expired Data
237+
* documentation</a>
238+
*
239+
* @param request The request to delete expired ML data
240+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
241+
* @return The action response which contains the acknowledgement or the task id depending on whether the action was set to wait for
242+
* completion
243+
* @throws IOException when there is a serialization issue sending the request or receiving the response
244+
*/
245+
public DeleteExpiredDataResponse deleteExpiredData(DeleteExpiredDataRequest request, RequestOptions options) throws IOException {
246+
return restHighLevelClient.performRequestAndParseEntity(request,
247+
MLRequestConverters::deleteExpiredData,
248+
options,
249+
DeleteExpiredDataResponse::fromXContent,
250+
Collections.emptySet());
251+
}
252+
253+
/**
254+
* Deletes expired data from Machine Learning Jobs asynchronously and notifies the listener on completion
255+
* <p>
256+
* For additional info
257+
* see <a href="http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-delete-expired-data.html">ML Delete Expired Data
258+
* documentation</a>
259+
*
260+
* @param request The request to delete expired ML data
261+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
262+
* @param listener Listener to be notified upon request completion
263+
*/
264+
public void deleteExpiredDataAsync(DeleteExpiredDataRequest request, RequestOptions options,
265+
ActionListener<DeleteExpiredDataResponse> listener) {
266+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
267+
MLRequestConverters::deleteExpiredData,
268+
options,
269+
DeleteExpiredDataResponse::fromXContent,
270+
listener,
271+
Collections.emptySet());
272+
}
273+
230274
/**
231275
* Deletes the given Machine Learning Job
232276
* <p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client.ml;
20+
21+
import org.elasticsearch.action.ActionRequest;
22+
import org.elasticsearch.action.ActionRequestValidationException;
23+
24+
/**
25+
* Request to delete expired model snapshots and forecasts
26+
*/
27+
public class DeleteExpiredDataRequest extends ActionRequest {
28+
29+
/**
30+
* Create a new request to delete expired data
31+
*/
32+
public DeleteExpiredDataRequest() {
33+
}
34+
35+
@Override
36+
public ActionRequestValidationException validate() {
37+
return null;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client.ml;
20+
21+
import org.elasticsearch.action.ActionResponse;
22+
import org.elasticsearch.common.ParseField;
23+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
24+
import org.elasticsearch.common.xcontent.ToXContent;
25+
import org.elasticsearch.common.xcontent.ToXContentObject;
26+
import org.elasticsearch.common.xcontent.XContentBuilder;
27+
import org.elasticsearch.common.xcontent.XContentParser;
28+
29+
import java.io.IOException;
30+
import java.util.Objects;
31+
32+
33+
/**
34+
* A response acknowledging the deletion of expired data
35+
*/
36+
public class DeleteExpiredDataResponse extends ActionResponse implements ToXContentObject {
37+
38+
private static final ParseField DELETED = new ParseField("deleted");
39+
40+
public DeleteExpiredDataResponse(boolean deleted) {
41+
this.deleted = deleted;
42+
}
43+
44+
public static final ConstructingObjectParser<DeleteExpiredDataResponse, Void> PARSER =
45+
new ConstructingObjectParser<>("delete_expired_data_response", true,
46+
a -> new DeleteExpiredDataResponse((Boolean) a[0]));
47+
48+
static {
49+
PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), DELETED);
50+
}
51+
52+
public static DeleteExpiredDataResponse fromXContent(XContentParser parser) throws IOException {
53+
return PARSER.parse(parser, null);
54+
}
55+
56+
private final Boolean deleted;
57+
58+
public Boolean getDeleted() {
59+
return deleted;
60+
}
61+
62+
@Override
63+
public int hashCode() {
64+
return Objects.hash(deleted);
65+
}
66+
67+
@Override
68+
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
69+
builder.startObject();
70+
if (deleted != null) {
71+
builder.field(DELETED.getPreferredName(), deleted);
72+
}
73+
builder.endObject();
74+
return builder;
75+
}
76+
77+
@Override
78+
public boolean equals(Object obj) {
79+
if (obj == null) {
80+
return false;
81+
}
82+
if (getClass() != obj.getClass()) {
83+
return false;
84+
}
85+
DeleteExpiredDataResponse response = (DeleteExpiredDataResponse) obj;
86+
return Objects.equals(deleted, response.deleted);
87+
}
88+
}

client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.client.ml.DeleteCalendarJobRequest;
2929
import org.elasticsearch.client.ml.DeleteCalendarRequest;
3030
import org.elasticsearch.client.ml.DeleteDatafeedRequest;
31+
import org.elasticsearch.client.ml.DeleteExpiredDataRequest;
3132
import org.elasticsearch.client.ml.DeleteFilterRequest;
3233
import org.elasticsearch.client.ml.DeleteForecastRequest;
3334
import org.elasticsearch.client.ml.DeleteJobRequest;
@@ -183,6 +184,14 @@ public void testCloseJob() throws Exception {
183184
requestEntityToString(request));
184185
}
185186

187+
public void testDeleteExpiredData() {
188+
DeleteExpiredDataRequest deleteExpiredDataRequest = new DeleteExpiredDataRequest();
189+
190+
Request request = MLRequestConverters.deleteExpiredData(deleteExpiredDataRequest);
191+
assertEquals(HttpDelete.METHOD_NAME, request.getMethod());
192+
assertEquals("/_xpack/ml/_delete_expired_data", request.getEndpoint());
193+
}
194+
186195
public void testDeleteJob() {
187196
String jobId = randomAlphaOfLength(10);
188197
DeleteJobRequest deleteJobRequest = new DeleteJobRequest(jobId);

0 commit comments

Comments
 (0)