Skip to content

[7.x] [ML] add new snapshot upgrader API for upgrading older snapshots (#64665) #65010

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 9 commits into from
Nov 17, 2020
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 @@ -86,6 +86,7 @@
import org.elasticsearch.client.ml.UpdateFilterRequest;
import org.elasticsearch.client.ml.UpdateJobRequest;
import org.elasticsearch.client.ml.UpdateModelSnapshotRequest;
import org.elasticsearch.client.ml.UpgradeJobModelSnapshotRequest;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentType;
Expand Down Expand Up @@ -429,6 +430,29 @@ static Request updateModelSnapshot(UpdateModelSnapshotRequest updateModelSnapsho
return request;
}

static Request upgradeJobSnapshot(UpgradeJobModelSnapshotRequest upgradeJobModelSnapshotRequest) {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_ml")
.addPathPartAsIs("anomaly_detectors")
.addPathPart(upgradeJobModelSnapshotRequest.getJobId())
.addPathPartAsIs("model_snapshots")
.addPathPart(upgradeJobModelSnapshotRequest.getSnapshotId())
.addPathPartAsIs("_upgrade")
.build();
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
RequestConverters.Params params = new RequestConverters.Params();
if (upgradeJobModelSnapshotRequest.getTimeout() != null) {
params.putParam(UpgradeJobModelSnapshotRequest.TIMEOUT.getPreferredName(),
upgradeJobModelSnapshotRequest.getTimeout().getStringRep());
}
if (upgradeJobModelSnapshotRequest.getWaitForCompletion() != null) {
params.putParam(UpgradeJobModelSnapshotRequest.WAIT_FOR_COMPLETION.getPreferredName(),
upgradeJobModelSnapshotRequest.getWaitForCompletion().toString());
}
request.addParameters(params.asMap());
return request;
}

static Request revertModelSnapshot(RevertModelSnapshotRequest revertModelSnapshotsRequest) throws IOException {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_ml")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
import org.elasticsearch.client.ml.UpdateJobRequest;
import org.elasticsearch.client.ml.UpdateModelSnapshotRequest;
import org.elasticsearch.client.ml.UpdateModelSnapshotResponse;
import org.elasticsearch.client.ml.UpgradeJobModelSnapshotRequest;
import org.elasticsearch.client.ml.UpgradeJobModelSnapshotResponse;
import org.elasticsearch.client.ml.job.stats.JobStats;

import java.io.IOException;
Expand Down Expand Up @@ -1178,6 +1180,50 @@ public Cancellable updateModelSnapshotAsync(UpdateModelSnapshotRequest request,
Collections.emptySet());
}

/**
* Upgrades a snapshot for a Machine Learning Job to the current major version.
* <p>
* For additional info
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-upgrade-job-model-snapshot.html">
* ML Upgrade job snapshots documentation</a>
*
* @param request The request
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @throws IOException when there is a serialization issue sending the request or receiving the response
*/
public UpgradeJobModelSnapshotResponse upgradeJobSnapshot(UpgradeJobModelSnapshotRequest request,
RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request,
MLRequestConverters::upgradeJobSnapshot,
options,
UpgradeJobModelSnapshotResponse::fromXContent,
Collections.emptySet());
}

/**
* Upgrades a snapshot for a Machine Learning Job to the current major version,
* notifies listener once the upgrade has started.
* <p>
* For additional info
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-upgrade-job-model-snapshot.html">
* ML Upgrade job snapshots documentation</a>
*
* @param request The request
* @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
* @return cancellable that may be used to cancel the request
*/
public Cancellable upgradeJobSnapshotAsync(UpgradeJobModelSnapshotRequest request,
RequestOptions options,
ActionListener<UpgradeJobModelSnapshotResponse> listener) {
return restHighLevelClient.performRequestAsyncAndParseEntity(request,
MLRequestConverters::upgradeJobSnapshot,
options,
UpgradeJobModelSnapshotResponse::fromXContent,
listener,
Collections.emptySet());
}

/**
* Gets overall buckets for a set of Machine Learning Jobs.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* 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.client.Validatable;
import org.elasticsearch.client.ml.job.config.Job;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;

public class UpgradeJobModelSnapshotRequest implements Validatable, ToXContentObject {

public static final ParseField SNAPSHOT_ID = new ParseField("snapshot_id");
public static final ParseField TIMEOUT = new ParseField("timeout");
public static final ParseField WAIT_FOR_COMPLETION = new ParseField("wait_for_completion");

private static final ConstructingObjectParser<UpgradeJobModelSnapshotRequest, Void> PARSER = new ConstructingObjectParser<>(
"upgrade_job_snapshot_request",
true,
a -> new UpgradeJobModelSnapshotRequest((String) a[0], (String) a[1], (String) a[2], (Boolean) a[3]));

static {
PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID);
PARSER.declareString(ConstructingObjectParser.constructorArg(), SNAPSHOT_ID);
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), TIMEOUT);
PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), WAIT_FOR_COMPLETION);
}

private final String jobId;
private final String snapshotId;
private final TimeValue timeout;
private final Boolean waitForCompletion;

UpgradeJobModelSnapshotRequest(String jobId, String snapshotId, String timeout, Boolean waitForCompletion) {
this(jobId,
snapshotId,
timeout == null ? null : TimeValue.parseTimeValue(timeout, TIMEOUT.getPreferredName()),
waitForCompletion);
}

public UpgradeJobModelSnapshotRequest(String jobId, String snapshotId, TimeValue timeValue, Boolean waitForCompletion) {
this.jobId = Objects.requireNonNull(jobId, Job.ID.getPreferredName());
this.snapshotId = Objects.requireNonNull(snapshotId, SNAPSHOT_ID.getPreferredName());
this.timeout = timeValue;
this.waitForCompletion = waitForCompletion;
}

public static UpgradeJobModelSnapshotRequest fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}

public String getJobId() {
return jobId;
}

public String getSnapshotId() {
return snapshotId;
}

public TimeValue getTimeout() {
return timeout;
}

public Boolean getWaitForCompletion() {
return waitForCompletion;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UpgradeJobModelSnapshotRequest request = (UpgradeJobModelSnapshotRequest) o;
return Objects.equals(jobId, request.jobId) &&
Objects.equals(timeout, request.timeout) &&
Objects.equals(waitForCompletion, request.waitForCompletion) &&
Objects.equals(snapshotId, request.snapshotId);
}

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

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(Job.ID.getPreferredName(), jobId);
builder.field(SNAPSHOT_ID.getPreferredName(), snapshotId);
if (timeout != null) {
builder.field(TIMEOUT.getPreferredName(), timeout.getStringRep());
}
if (waitForCompletion != null) {
builder.field(WAIT_FOR_COMPLETION.getPreferredName(), waitForCompletion);
}
builder.endObject();
return builder;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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.common.ParseField;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;

public class UpgradeJobModelSnapshotResponse implements ToXContentObject {

private static final ParseField COMPLETED = new ParseField("completed");
private static final ParseField NODE = new ParseField("node");

public static final ConstructingObjectParser<UpgradeJobModelSnapshotResponse, Void> PARSER =
new ConstructingObjectParser<>("upgrade_job_snapshot_response", true,
(a) -> new UpgradeJobModelSnapshotResponse((Boolean) a[0], (String) a[1]));

static {
PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), COMPLETED);
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), NODE);
}

private final boolean completed;
private final String node;

public UpgradeJobModelSnapshotResponse(Boolean opened, String node) {
this.completed = opened != null && opened;
this.node = node;
}

public static UpgradeJobModelSnapshotResponse fromXContent(XContentParser parser) throws IOException {
return PARSER.parse(parser, null);
}

public boolean isCompleted() {
return completed;
}

/**
* The node that the job was assigned to
*
* @return The ID of a node if the job was assigned to a node.
*/
public String getNode() {
return node;
}

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

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

UpgradeJobModelSnapshotResponse that = (UpgradeJobModelSnapshotResponse) other;
return completed == that.completed
&& Objects.equals(node, that.node);
}

@Override
public int hashCode() {
return Objects.hash(completed, node);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(COMPLETED.getPreferredName(), completed);
if (node != null) {
builder.field(NODE.getPreferredName(), node);
}
builder.endObject();
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import org.elasticsearch.client.ml.UpdateFilterRequest;
import org.elasticsearch.client.ml.UpdateJobRequest;
import org.elasticsearch.client.ml.UpdateModelSnapshotRequest;
import org.elasticsearch.client.ml.UpgradeJobModelSnapshotRequest;
import org.elasticsearch.client.ml.calendars.Calendar;
import org.elasticsearch.client.ml.calendars.CalendarTests;
import org.elasticsearch.client.ml.calendars.ScheduledEvent;
Expand Down Expand Up @@ -507,6 +508,30 @@ public void testUpdateModelSnapshot() throws IOException {
}
}

public void testUpgradeJobModelSnapshot() {
String jobId = randomAlphaOfLength(10);
String snapshotId = randomAlphaOfLength(10);
TimeValue timeout = TimeValue.parseTimeValue(randomTimeValue(), "test");
boolean waitForCompletion = randomBoolean();
boolean includeTimeout = randomBoolean();
boolean includeWaitForCompletion = randomBoolean();
UpgradeJobModelSnapshotRequest upgradeJobModelSnapshotRequest = new UpgradeJobModelSnapshotRequest(jobId,
snapshotId,
includeTimeout ? timeout : null,
includeWaitForCompletion ? waitForCompletion : null);

Request request = MLRequestConverters.upgradeJobSnapshot(upgradeJobModelSnapshotRequest);
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
assertEquals("/_ml/anomaly_detectors/" + jobId + "/model_snapshots/" + snapshotId + "/_upgrade", request.getEndpoint());
assertThat(request.getParameters().isEmpty(), equalTo(includeTimeout == false && includeWaitForCompletion == false));
if (includeTimeout) {
assertThat(request.getParameters().get("timeout"), equalTo(timeout.getStringRep()));
}
if (includeWaitForCompletion) {
assertThat(request.getParameters().get("wait_for_completion"), equalTo(Boolean.toString(waitForCompletion)));
}
}

public void testRevertModelSnapshot() throws IOException {
String jobId = randomAlphaOfLength(10);
String snapshotId = randomAlphaOfLength(10);
Expand Down
Loading