Skip to content

[HLRC][ML] Add ML delete model snapshot API #35537

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.elasticsearch.client.ml.DeleteDatafeedRequest;
import org.elasticsearch.client.ml.DeleteForecastRequest;
import org.elasticsearch.client.ml.DeleteJobRequest;
import org.elasticsearch.client.ml.DeleteModelSnapshotRequest;
import org.elasticsearch.client.ml.FlushJobRequest;
import org.elasticsearch.client.ml.ForecastJobRequest;
import org.elasticsearch.client.ml.GetBucketsRequest;
Expand Down Expand Up @@ -335,6 +336,18 @@ static Request deleteForecast(DeleteForecastRequest deleteForecastRequest) {
return request;
}

static Request deleteModelSnapshot(DeleteModelSnapshotRequest deleteModelSnapshotRequest) {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_xpack")
.addPathPartAsIs("ml")
.addPathPartAsIs("anomaly_detectors")
.addPathPart(deleteModelSnapshotRequest.getJobId())
.addPathPartAsIs("model_snapshots")
.addPathPart(deleteModelSnapshotRequest.getSnapshotId())
.build();
return new Request(HttpDelete.METHOD_NAME, endpoint);
}

static Request getBuckets(GetBucketsRequest getBucketsRequest) throws IOException {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_xpack")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.client.ml.DeleteForecastRequest;
import org.elasticsearch.client.ml.DeleteJobRequest;
import org.elasticsearch.client.ml.DeleteJobResponse;
import org.elasticsearch.client.ml.DeleteModelSnapshotRequest;
import org.elasticsearch.client.ml.FlushJobRequest;
import org.elasticsearch.client.ml.FlushJobResponse;
import org.elasticsearch.client.ml.ForecastJobRequest;
Expand Down Expand Up @@ -464,6 +465,47 @@ public void deleteForecastAsync(DeleteForecastRequest request, RequestOptions op
Collections.emptySet());
}

/**
* Deletes Machine Learning Model Snapshots
* <p>
* For additional info
* see <a href="http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-delete-snapshot.html">
* ML Delete Model Snapshot documentation</a>
*
* @param request The request to delete the model snapshot
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return action acknowledgement
* @throws IOException when there is a serialization issue sending the request or receiving the response
*/
public AcknowledgedResponse deleteModelSnapshot(DeleteModelSnapshotRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request,
MLRequestConverters::deleteModelSnapshot,
options,
AcknowledgedResponse::fromXContent,
Collections.emptySet());
}

/**
* Deletes Machine Learning Model Snapshots asynchronously and notifies the listener on completion
* <p>
* For additional info
* see <a href="http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-delete-snapshot.html">
* ML Delete Model Snapshot documentation</a>
*
* @param request The request to delete the model snapshot
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener Listener to be notified upon request completion
*/
public void deleteModelSnapshotAsync(DeleteModelSnapshotRequest request, RequestOptions options,
ActionListener<AcknowledgedResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request,
MLRequestConverters::deleteModelSnapshot,
options,
AcknowledgedResponse::fromXContent,
listener,
Collections.emptySet());
}

/**
* Creates a new Machine Learning Datafeed
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.ml;

import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.client.ml.job.config.Job;
import org.elasticsearch.client.ml.job.process.ModelSnapshot;


import java.util.Objects;

/**
* Request to delete a Machine Learning Model Snapshot Job via its Job and Snapshot IDs
*/
public class DeleteModelSnapshotRequest extends ActionRequest {

private final String jobId;
private final String snapshotId;

public DeleteModelSnapshotRequest(String jobId, String snapshotId) {
this.jobId = Objects.requireNonNull(jobId, "[" + Job.ID + "] must not be null");
this.snapshotId = Objects.requireNonNull(snapshotId, "[" + ModelSnapshot.SNAPSHOT_ID + "] must not be null");
}

public String getJobId() {
return jobId;
}

public String getSnapshotId() {
return snapshotId;
}

@Override
public ActionRequestValidationException validate() {
return null;
}

@Override
public int hashCode() {
return Objects.hash(jobId, snapshotId);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}

if (obj == null || obj.getClass() != getClass()) {
return false;
}

DeleteModelSnapshotRequest other = (DeleteModelSnapshotRequest) obj;
return Objects.equals(jobId, other.jobId) && Objects.equals(snapshotId, other.snapshotId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.client.ml.DeleteDatafeedRequest;
import org.elasticsearch.client.ml.DeleteForecastRequest;
import org.elasticsearch.client.ml.DeleteJobRequest;
import org.elasticsearch.client.ml.DeleteModelSnapshotRequest;
import org.elasticsearch.client.ml.FlushJobRequest;
import org.elasticsearch.client.ml.ForecastJobRequest;
import org.elasticsearch.client.ml.GetBucketsRequest;
Expand Down Expand Up @@ -362,6 +363,16 @@ public void testDeleteForecast() {
request.getParameters().get(DeleteForecastRequest.ALLOW_NO_FORECASTS.getPreferredName()));
}

public void testDeleteModelSnapshot() {
String jobId = randomAlphaOfLength(10);
String snapshotId = randomAlphaOfLength(10);
DeleteModelSnapshotRequest deleteModelSnapshotRequest = new DeleteModelSnapshotRequest(jobId, snapshotId);

Request request = MLRequestConverters.deleteModelSnapshot(deleteModelSnapshotRequest);
assertEquals(HttpDelete.METHOD_NAME, request.getMethod());
assertEquals("/_xpack/ml/anomaly_detectors/" + jobId + "/model_snapshots/" + snapshotId, request.getEndpoint());
}

public void testGetBuckets() throws IOException {
String jobId = randomAlphaOfLength(10);
GetBucketsRequest getBucketsRequest = new GetBucketsRequest(jobId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.elasticsearch.client.ml.DeleteForecastRequest;
import org.elasticsearch.client.ml.DeleteJobRequest;
import org.elasticsearch.client.ml.DeleteJobResponse;
import org.elasticsearch.client.ml.DeleteModelSnapshotRequest;
import org.elasticsearch.client.ml.FlushJobRequest;
import org.elasticsearch.client.ml.FlushJobResponse;
import org.elasticsearch.client.ml.ForecastJobRequest;
Expand Down Expand Up @@ -996,4 +997,38 @@ private String createAndPutDatafeed(String jobId, String indexName) throws IOExc
highLevelClient().machineLearning().putDatafeed(new PutDatafeedRequest(datafeed), RequestOptions.DEFAULT);
return datafeedId;
}

public void createModelSnapshot(String jobId, String snapshotId) throws IOException {
Job job = MachineLearningIT.buildJob(jobId);
highLevelClient().machineLearning().putJob(new PutJobRequest(job), RequestOptions.DEFAULT);

IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared", "doc");
indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
indexRequest.source("{\"job_id\":\"" + jobId + "\", \"timestamp\":1541587919000, " +
"\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " +
"\"snapshot_id\":\"" + snapshotId + "\", \"snapshot_doc_count\":1, \"model_size_stats\":{" +
"\"job_id\":\"" + jobId + "\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " +
"\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," +
"\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " +
"\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," +
"\"latest_result_time_stamp\":1519930800000, \"retain\":false}", XContentType.JSON);

highLevelClient().index(indexRequest, RequestOptions.DEFAULT);
}

public void testDeleteModelSnapshot() throws IOException {
String jobId = "test-delete-model-snapshot";
String snapshotId = "1541587919";

createModelSnapshot(jobId, snapshotId);

MachineLearningClient machineLearningClient = highLevelClient().machineLearning();

DeleteModelSnapshotRequest request = new DeleteModelSnapshotRequest(jobId, snapshotId);

AcknowledgedResponse response = execute(request, machineLearningClient::deleteModelSnapshot,
machineLearningClient::deleteModelSnapshotAsync);

assertTrue(response.isAcknowledged());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.elasticsearch.client.ml.DeleteForecastRequest;
import org.elasticsearch.client.ml.DeleteJobRequest;
import org.elasticsearch.client.ml.DeleteJobResponse;
import org.elasticsearch.client.ml.DeleteModelSnapshotRequest;
import org.elasticsearch.client.ml.FlushJobRequest;
import org.elasticsearch.client.ml.FlushJobResponse;
import org.elasticsearch.client.ml.ForecastJobRequest;
Expand Down Expand Up @@ -1867,6 +1868,73 @@ public void onFailure(Exception e) {
}
}

public void testDeleteModelSnapshot() throws IOException, InterruptedException {
RestHighLevelClient client = highLevelClient();

String jobId = "test-delete-model-snapshot";
String snapshotId = "1541587919";
Job job = MachineLearningIT.buildJob(jobId);
client.machineLearning().putJob(new PutJobRequest(job), RequestOptions.DEFAULT);

// Let us index a snapshot
IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared", "doc");
indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
indexRequest.source("{\"job_id\":\"" + jobId + "\", \"timestamp\":1541587919000, " +
"\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " +
"\"snapshot_id\":\"" + snapshotId + "\", \"snapshot_doc_count\":1, \"model_size_stats\":{" +
"\"job_id\":\"" + jobId + "\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " +
"\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," +
"\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " +
"\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," +
"\"latest_result_time_stamp\":1519930800000, \"retain\":false}", XContentType.JSON);
{
client.index(indexRequest, RequestOptions.DEFAULT);

// tag::delete-model-snapshot-request
DeleteModelSnapshotRequest request = new DeleteModelSnapshotRequest(jobId, snapshotId); // <1>
// end::delete-model-snapshot-request

// tag::delete-model-snapshot-execute
AcknowledgedResponse response = client.machineLearning().deleteModelSnapshot(request, RequestOptions.DEFAULT);
// end::delete-model-snapshot-execute

// tag::delete-model-snapshot-response
boolean isAcknowledged = response.isAcknowledged(); // <1>
// end::delete-model-snapshot-response

assertTrue(isAcknowledged);
}
{
client.index(indexRequest, RequestOptions.DEFAULT);

// tag::delete-model-snapshot-execute-listener
ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
// <1>
}

@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::delete-model-snapshot-execute-listener

// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);

DeleteModelSnapshotRequest deleteModelSnapshotRequest = new DeleteModelSnapshotRequest(jobId, "1541587919");

// tag::delete-model-snapshot-execute-async
client.machineLearning().deleteModelSnapshotAsync(deleteModelSnapshotRequest, RequestOptions.DEFAULT, listener); // <1>
// end::delete-model-snapshot-execute-async

assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}

public void testGetModelSnapshots() throws IOException, InterruptedException {
RestHighLevelClient client = highLevelClient();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.ml;

import org.elasticsearch.test.ESTestCase;

public class DeleteModelSnapshotRequestTests extends ESTestCase {

public void test_WithNullJobId() {
NullPointerException ex = expectThrows(NullPointerException.class, () ->
new DeleteModelSnapshotRequest(null, randomAlphaOfLength(10)));
assertEquals("[job_id] must not be null", ex.getMessage());
}

public void test_WithNullSnapshotId() {
NullPointerException ex = expectThrows(NullPointerException.class, ()
-> new DeleteModelSnapshotRequest(randomAlphaOfLength(10), null));
assertEquals("[snapshot_id] must not be null", ex.getMessage());
}

private DeleteModelSnapshotRequest createTestInstance() {
return new DeleteModelSnapshotRequest(randomAlphaOfLength(10), randomAlphaOfLength(10));
}
}
30 changes: 30 additions & 0 deletions docs/java-rest/high-level/ml/delete-model-snapshot.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--
:api: delete-model-snapshot
:request: DeleteModelSnapshotRequest
:response: AcknowledgedResponse
--
[id="{upid}-{api}"]
=== Delete Model Snapshot API

[id="{upid}-{api}-request"]
==== Delete Model Snapshot Request

A +{request}+ object requires both a non-null `jobId` and a non-null `snapshotId`.

["source","java",subs="attributes,callouts,macros"]
---------------------------------------------------
include-tagged::{doc-tests-file}[{api}-request]
---------------------------------------------------
<1> Constructing a new request referencing existing `jobId` and `snapshotId`.

include::../execution.asciidoc[]

[id="{upid}-{api}-response"]
==== Delete Model Snapshot Response

The returned +{response}+ object indicates the acknowledgement of the request:
["source","java",subs="attributes,callouts,macros"]
---------------------------------------------------
include-tagged::{doc-tests-file}[{api}-response]
---------------------------------------------------
<1> `isAcknowledged` was the deletion request acknowledged or not
2 changes: 2 additions & 0 deletions docs/java-rest/high-level/supported-apis.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ The Java High Level REST Client supports the following Machine Learning APIs:
* <<{upid}-put-filter>>
* <<{upid}-get-model-snapshots>>
* <<{upid}-get-filters>>
* <<{upid}-delete-model-snapshot>>
* <<{upid}-update-filter>>

include::ml/put-job.asciidoc[]
Expand Down Expand Up @@ -299,6 +300,7 @@ include::ml/delete-calendar.asciidoc[]
include::ml/put-filter.asciidoc[]
include::ml/get-model-snapshots.asciidoc[]
include::ml/get-filters.asciidoc[]
include::ml/delete-model-snapshot.asciidoc[]
include::ml/update-filter.asciidoc[]

== Migration APIs
Expand Down