Skip to content
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 @@ -49,6 +49,7 @@
import org.elasticsearch.client.ml.PutDatafeedRequest;
import org.elasticsearch.client.ml.PutJobRequest;
import org.elasticsearch.client.ml.StartDatafeedRequest;
import org.elasticsearch.client.ml.StopDatafeedRequest;
import org.elasticsearch.client.ml.UpdateJobRequest;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
Expand Down Expand Up @@ -245,6 +246,19 @@ static Request startDatafeed(StartDatafeedRequest startDatafeedRequest) throws I
return request;
}

static Request stopDatafeed(StopDatafeedRequest stopDatafeedRequest) throws IOException {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_xpack")
.addPathPartAsIs("ml")
.addPathPartAsIs("datafeeds")
.addPathPart(Strings.collectionToCommaDelimitedString(stopDatafeedRequest.getDatafeedIds()))
.addPathPartAsIs("_stop")
.build();
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
request.setEntity(createEntity(stopDatafeedRequest, REQUEST_BODY_CONTENT_TYPE));
return request;
}

static Request deleteForecast(DeleteForecastRequest deleteForecastRequest) {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_xpack")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
import org.elasticsearch.client.ml.PutJobResponse;
import org.elasticsearch.client.ml.StartDatafeedRequest;
import org.elasticsearch.client.ml.StartDatafeedResponse;
import org.elasticsearch.client.ml.StopDatafeedRequest;
import org.elasticsearch.client.ml.StopDatafeedResponse;
import org.elasticsearch.client.ml.UpdateJobRequest;
import org.elasticsearch.client.ml.job.stats.JobStats;

Expand Down Expand Up @@ -607,6 +609,46 @@ public void startDatafeedAsync(StartDatafeedRequest request, RequestOptions opti
Collections.emptySet());
}

/**
* Stops the given Machine Learning Datafeed
* <p>
* For additional info
* see <a href="http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-stop-datafeed.html">
* ML Stop Datafeed documentation</a>
*
* @param request The request to stop the datafeed
* @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 StopDatafeedResponse stopDatafeed(StopDatafeedRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request,
MLRequestConverters::stopDatafeed,
options,
StopDatafeedResponse::fromXContent,
Collections.emptySet());
}

/**
* Stops the given Machine Learning Datafeed asynchronously and notifies the listener on completion
* <p>
* For additional info
* see <a href="http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-stop-datafeed.html">
* ML Stop Datafeed documentation</a>
*
* @param request The request to stop the datafeed
* @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 stopDatafeedAsync(StopDatafeedRequest request, RequestOptions options, ActionListener<StopDatafeedResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request,
MLRequestConverters::stopDatafeed,
options,
StopDatafeedResponse::fromXContent,
listener,
Collections.emptySet());
}

/**
* Updates a Machine Learning {@link org.elasticsearch.client.ml.job.config.Job}
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* 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.datafeed.DatafeedConfig;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
* Request to stop Machine Learning Datafeeds
*/
public class StopDatafeedRequest extends ActionRequest implements ToXContentObject {

public static final ParseField TIMEOUT = new ParseField("timeout");
public static final ParseField FORCE = new ParseField("force");
public static final ParseField ALLOW_NO_DATAFEEDS = new ParseField("allow_no_datafeeds");

@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<StopDatafeedRequest, Void> PARSER = new ConstructingObjectParser<>(
"stop_datafeed_request",
a -> new StopDatafeedRequest((List<String>) a[0]));

static {
PARSER.declareField(ConstructingObjectParser.constructorArg(),
p -> Arrays.asList(Strings.commaDelimitedListToStringArray(p.text())),
DatafeedConfig.ID, ObjectParser.ValueType.STRING_ARRAY);
PARSER.declareString((obj, val) -> obj.setTimeout(TimeValue.parseTimeValue(val, TIMEOUT.getPreferredName())), TIMEOUT);
PARSER.declareBoolean(StopDatafeedRequest::setForce, FORCE);
PARSER.declareBoolean(StopDatafeedRequest::setAllowNoDatafeeds, ALLOW_NO_DATAFEEDS);
}

private static final String ALL_DATAFEEDS = "_all";

private final List<String> datafeedIds;
private TimeValue timeout;
private Boolean force;
private Boolean allowNoDatafeeds;

/**
* Explicitly stop all datafeeds
*
* @return a {@link StopDatafeedRequest} for all existing datafeeds
*/
public static StopDatafeedRequest stopAllDatafeedsRequest(){
return new StopDatafeedRequest(ALL_DATAFEEDS);
}

StopDatafeedRequest(List<String> datafeedIds) {
if (datafeedIds.isEmpty()) {
throw new InvalidParameterException("datafeedIds must not be empty");
}
if (datafeedIds.stream().anyMatch(Objects::isNull)) {
throw new NullPointerException("datafeedIds must not contain null values");
}
this.datafeedIds = new ArrayList<>(datafeedIds);
}

/**
* Close the specified Datafeeds via their unique datafeedIds
*
* @param datafeedIds must be non-null and non-empty and each datafeedId must be non-null
*/
public StopDatafeedRequest(String... datafeedIds) {
this(Arrays.asList(datafeedIds));
}

/**
* All the datafeedIds to be stopped
*/
public List<String> getDatafeedIds() {
return datafeedIds;
}

public TimeValue getTimeout() {
return timeout;
}

/**
* How long to wait for the stop request to complete before timing out.
*
* @param timeout Default value: 30 minutes
*/
public void setTimeout(TimeValue timeout) {
this.timeout = timeout;
}

public Boolean isForce() {
return force;
}

/**
* Should the stopping be forced.
*
* Use to forcefully stop a datafeed
*
* @param force When {@code true} forcefully stop the datafeed. Defaults to {@code false}
*/
public void setForce(boolean force) {
this.force = force;
}

public Boolean isAllowNoDatafeeds() {
return this.allowNoDatafeeds;
}

/**
* Whether to ignore if a wildcard expression matches no datafeeds.
*
* This includes {@code _all} string.
*
* @param allowNoDatafeeds When {@code true} ignore if wildcard or {@code _all} matches no datafeeds. Defaults to {@code true}
*/
public void setAllowNoDatafeeds(boolean allowNoDatafeeds) {
this.allowNoDatafeeds = allowNoDatafeeds;
}

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

@Override
public int hashCode() {
return Objects.hash(datafeedIds, timeout, force, allowNoDatafeeds);
}

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

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

StopDatafeedRequest that = (StopDatafeedRequest) other;
return Objects.equals(datafeedIds, that.datafeedIds) &&
Objects.equals(timeout, that.timeout) &&
Objects.equals(force, that.force) &&
Objects.equals(allowNoDatafeeds, that.allowNoDatafeeds);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(DatafeedConfig.ID.getPreferredName(), Strings.collectionToCommaDelimitedString(datafeedIds));
if (timeout != null) {
builder.field(TIMEOUT.getPreferredName(), timeout.getStringRep());
}
if (force != null) {
builder.field(FORCE.getPreferredName(), force);
}
if (allowNoDatafeeds != null) {
builder.field(ALLOW_NO_DATAFEEDS.getPreferredName(), allowNoDatafeeds);
}
builder.endObject();
return builder;
}

@Override
public String toString() {
return Strings.toString(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.ActionResponse;
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;

/**
* Response indicating if the Machine Learning Datafeed is now stopped or not
*/
public class StopDatafeedResponse extends ActionResponse implements ToXContentObject {

private static final ParseField STOPPED = new ParseField("stopped");

public static final ConstructingObjectParser<StopDatafeedResponse, Void> PARSER =
new ConstructingObjectParser<>(
"stop_datafeed_response",
true,
(a) -> new StopDatafeedResponse((Boolean)a[0]));

static {
PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), STOPPED);
}

private final boolean stopped;

public StopDatafeedResponse(boolean stopped) {
this.stopped = stopped;
}

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

/**
* Has the Datafeed stopped or not
*
* @return boolean value indicating the Datafeed stopped status
*/
public boolean isStopped() {
return stopped;
}

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

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

StopDatafeedResponse that = (StopDatafeedResponse) other;
return isStopped() == that.isStopped();
}

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

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(STOPPED.getPreferredName(), stopped);
builder.endObject();
return builder;
}
}
Loading