Skip to content

Commit

Permalink
[ML][Data Frame] Add update transform api endpoint (elastic#45154)
Browse files Browse the repository at this point in the history
This adds the ability to `_update` stored data frame transforms. All mutable fields are applied when the next checkpoint starts. The exception being `description`. 

This PR contains all that is necessary for this addition:
* HLRC
* Docs
* Server side
  • Loading branch information
benwtrent authored Aug 7, 2019
1 parent c592d24 commit 1da7c59
Show file tree
Hide file tree
Showing 36 changed files with 2,903 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.elasticsearch.client.dataframe.StartDataFrameTransformResponse;
import org.elasticsearch.client.dataframe.StopDataFrameTransformRequest;
import org.elasticsearch.client.dataframe.StopDataFrameTransformResponse;
import org.elasticsearch.client.dataframe.UpdateDataFrameTransformRequest;
import org.elasticsearch.client.dataframe.UpdateDataFrameTransformResponse;

import java.io.IOException;
import java.util.Collections;
Expand Down Expand Up @@ -88,6 +90,51 @@ public void putDataFrameTransformAsync(PutDataFrameTransformRequest request, Req
Collections.emptySet());
}

/**
* Updates an existing Data Frame Transform
* <p>
* For additional info
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/update-data-frame-transform.html">
* Create data frame transform documentation</a>
*
* @param request The UpdateDataFrameTransformRequest containing the
* {@link org.elasticsearch.client.dataframe.transforms.DataFrameTransformConfigUpdate}.
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return An UpdateDataFrameTransformResponse object containing the updated configuration
* @throws IOException when there is a serialization issue sending the request or receiving the response
*/
public UpdateDataFrameTransformResponse updateDataFrameTransform(UpdateDataFrameTransformRequest request,
RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request,
DataFrameRequestConverters::updateDataFrameTransform,
options,
UpdateDataFrameTransformResponse::fromXContent,
Collections.emptySet());
}

/**
* Updates an existing Data Frame Transform asynchronously and notifies listener on completion
* <p>
* For additional info
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/update-data-frame-transform.html">
* Create data frame transform documentation</a>
*
* @param request The UpdateDataFrameTransformRequest containing the
* {@link org.elasticsearch.client.dataframe.transforms.DataFrameTransformConfigUpdate}.
* @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 updateDataFrameTransformAsync(UpdateDataFrameTransformRequest request,
RequestOptions options,
ActionListener<UpdateDataFrameTransformResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request,
DataFrameRequestConverters::updateDataFrameTransform,
options,
UpdateDataFrameTransformResponse::fromXContent,
listener,
Collections.emptySet());
}

/**
* Get the running statistics of a Data Frame Transform
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.client.dataframe.PutDataFrameTransformRequest;
import org.elasticsearch.client.dataframe.StartDataFrameTransformRequest;
import org.elasticsearch.client.dataframe.StopDataFrameTransformRequest;
import org.elasticsearch.client.dataframe.UpdateDataFrameTransformRequest;
import org.elasticsearch.common.Strings;

import java.io.IOException;
Expand Down Expand Up @@ -58,6 +59,20 @@ static Request putDataFrameTransform(PutDataFrameTransformRequest putRequest) th
return request;
}

static Request updateDataFrameTransform(UpdateDataFrameTransformRequest updateDataFrameTransformRequest) throws IOException {
String endpoint = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_data_frame", "transforms")
.addPathPart(updateDataFrameTransformRequest.getId())
.addPathPart("_update")
.build();
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
request.setEntity(createEntity(updateDataFrameTransformRequest, REQUEST_BODY_CONTENT_TYPE));
if (updateDataFrameTransformRequest.getDeferValidation() != null) {
request.addParameter(DEFER_VALIDATION, Boolean.toString(updateDataFrameTransformRequest.getDeferValidation()));
}
return request;
}

static Request getDataFrameTransform(GetDataFrameTransformRequest getRequest) {
String endpoint = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_data_frame", "transforms")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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.dataframe;

import org.elasticsearch.client.Validatable;
import org.elasticsearch.client.ValidationException;
import org.elasticsearch.client.dataframe.transforms.DataFrameTransformConfigUpdate;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;

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

public class UpdateDataFrameTransformRequest implements ToXContentObject, Validatable {

private final DataFrameTransformConfigUpdate update;
private final String id;
private Boolean deferValidation;

public UpdateDataFrameTransformRequest(DataFrameTransformConfigUpdate update, String id) {
this.update = update;
this.id = id;
}

public DataFrameTransformConfigUpdate getUpdate() {
return update;
}

public Boolean getDeferValidation() {
return deferValidation;
}

public String getId() {
return id;
}

/**
* Indicates if deferrable validations should be skipped until the transform starts
*
* @param deferValidation {@code true} will cause validations to be deferred
*/
public void setDeferValidation(boolean deferValidation) {
this.deferValidation = deferValidation;
}

@Override
public Optional<ValidationException> validate() {
ValidationException validationException = new ValidationException();
if (update == null) {
validationException.addValidationError("put requires a non-null data frame config update object");
}
if (id == null) {
validationException.addValidationError("data frame transform id cannot be null");
}
if (validationException.validationErrors().isEmpty()) {
return Optional.empty();
} else {
return Optional.of(validationException);
}
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return update.toXContent(builder, params);
}

@Override
public int hashCode() {
return Objects.hash(update, deferValidation, id);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
UpdateDataFrameTransformRequest other = (UpdateDataFrameTransformRequest) obj;
return Objects.equals(update, other.update)
&& Objects.equals(id, other.id)
&& Objects.equals(deferValidation, other.deferValidation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.dataframe;

import org.elasticsearch.client.dataframe.transforms.DataFrameTransformConfig;
import org.elasticsearch.common.xcontent.XContentParser;

import java.util.Objects;

public class UpdateDataFrameTransformResponse {

public static UpdateDataFrameTransformResponse fromXContent(final XContentParser parser) {
return new UpdateDataFrameTransformResponse(DataFrameTransformConfig.PARSER.apply(parser, null));
}

private DataFrameTransformConfig transformConfiguration;

public UpdateDataFrameTransformResponse(DataFrameTransformConfig transformConfiguration) {
this.transformConfiguration = transformConfiguration;
}

public DataFrameTransformConfig getTransformConfiguration() {
return transformConfiguration;
}

@Override
public int hashCode() {
return Objects.hash(transformConfiguration);
}

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

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

final UpdateDataFrameTransformResponse that = (UpdateDataFrameTransformResponse) other;
return Objects.equals(this.transformConfiguration, that.transformConfiguration);
}
}
Loading

0 comments on commit 1da7c59

Please sign in to comment.