Skip to content

Commit 1f62a7a

Browse files
authored
[ML] Data Frame HLRC start & stop APIs (#40154)
1 parent ac2a5c6 commit 1f62a7a

18 files changed

+1067
-4
lines changed

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

+85
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
import org.elasticsearch.client.core.AcknowledgedResponse;
2424
import org.elasticsearch.client.dataframe.DeleteDataFrameTransformRequest;
2525
import org.elasticsearch.client.dataframe.PutDataFrameTransformRequest;
26+
import org.elasticsearch.client.dataframe.StartDataFrameTransformRequest;
27+
import org.elasticsearch.client.dataframe.StartDataFrameTransformResponse;
28+
import org.elasticsearch.client.dataframe.StopDataFrameTransformRequest;
29+
import org.elasticsearch.client.dataframe.StopDataFrameTransformResponse;
2630

2731
import java.io.IOException;
2832
import java.util.Collections;
@@ -115,4 +119,85 @@ public void deleteDataFrameTransformAsync(DeleteDataFrameTransformRequest reques
115119
listener,
116120
Collections.emptySet());
117121
}
122+
123+
124+
/**
125+
* Start a data frame transform
126+
* <p>
127+
* For additional info
128+
* see <a href="https://www.TODO.com">Start Data Frame transform documentation</a>
129+
*
130+
* @param request The start data frame transform request
131+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
132+
* @return A response object indicating request success
133+
* @throws IOException when there is a serialization issue sending the request or receiving the response
134+
*/
135+
public StartDataFrameTransformResponse startDataFrameTransform(StartDataFrameTransformRequest request, RequestOptions options)
136+
throws IOException {
137+
return restHighLevelClient.performRequestAndParseEntity(request,
138+
DataFrameRequestConverters::startDataFrameTransform,
139+
options,
140+
StartDataFrameTransformResponse::fromXContent,
141+
Collections.emptySet());
142+
}
143+
144+
/**
145+
* Start a data frame transform asynchronously and notifies listener on completion
146+
* <p>
147+
* For additional info
148+
* see <a href="https://www.TODO.com">Start Data Frame transform documentation</a>
149+
*
150+
* @param request The start data frame transform request
151+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
152+
* @param listener Listener to be notified upon request completion
153+
*/
154+
public void startDataFrameTransformAsync(StartDataFrameTransformRequest request, RequestOptions options,
155+
ActionListener<StartDataFrameTransformResponse> listener) {
156+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
157+
DataFrameRequestConverters::startDataFrameTransform,
158+
options,
159+
StartDataFrameTransformResponse::fromXContent,
160+
listener,
161+
Collections.emptySet());
162+
}
163+
164+
/**
165+
* Stop a data frame transform
166+
* <p>
167+
* For additional info
168+
* see <a href="https://www.TODO.com">Stop Data Frame transform documentation</a>
169+
*
170+
* @param request The stop data frame transform request
171+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
172+
* @return A response object indicating request success
173+
* @throws IOException when there is a serialization issue sending the request or receiving the response
174+
*/
175+
public StopDataFrameTransformResponse stopDataFrameTransform(StopDataFrameTransformRequest request, RequestOptions options)
176+
throws IOException {
177+
return restHighLevelClient.performRequestAndParseEntity(request,
178+
DataFrameRequestConverters::stopDataFrameTransform,
179+
options,
180+
StopDataFrameTransformResponse::fromXContent,
181+
Collections.emptySet());
182+
}
183+
184+
/**
185+
* Stop a data frame transform asynchronously and notifies listener on completion
186+
* <p>
187+
* For additional info
188+
* see <a href="https://www.TODO.com">Stop Data Frame transform documentation</a>
189+
*
190+
* @param request The stop data frame transform request
191+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
192+
* @param listener Listener to be notified upon request completion
193+
*/
194+
public void stopDataFrameTransformAsync(StopDataFrameTransformRequest request, RequestOptions options,
195+
ActionListener<StopDataFrameTransformResponse> listener) {
196+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
197+
DataFrameRequestConverters::stopDataFrameTransform,
198+
options,
199+
StopDataFrameTransformResponse::fromXContent,
200+
listener,
201+
Collections.emptySet());
202+
}
118203
}

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

+34
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
package org.elasticsearch.client;
2121

2222
import org.apache.http.client.methods.HttpDelete;
23+
import org.apache.http.client.methods.HttpPost;
2324
import org.apache.http.client.methods.HttpPut;
2425
import org.elasticsearch.client.dataframe.DeleteDataFrameTransformRequest;
2526
import org.elasticsearch.client.dataframe.PutDataFrameTransformRequest;
27+
import org.elasticsearch.client.dataframe.StartDataFrameTransformRequest;
28+
import org.elasticsearch.client.dataframe.StopDataFrameTransformRequest;
2629

2730
import java.io.IOException;
2831

@@ -50,4 +53,35 @@ static Request deleteDataFrameTransform(DeleteDataFrameTransformRequest request)
5053
.build();
5154
return new Request(HttpDelete.METHOD_NAME, endpoint);
5255
}
56+
57+
static Request startDataFrameTransform(StartDataFrameTransformRequest startRequest) {
58+
String endpoint = new RequestConverters.EndpointBuilder()
59+
.addPathPartAsIs("_data_frame", "transforms")
60+
.addPathPart(startRequest.getId())
61+
.addPathPartAsIs("_start")
62+
.build();
63+
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
64+
RequestConverters.Params params = new RequestConverters.Params(request);
65+
if (startRequest.getTimeout() != null) {
66+
params.withTimeout(startRequest.getTimeout());
67+
}
68+
return request;
69+
}
70+
71+
static Request stopDataFrameTransform(StopDataFrameTransformRequest stopRequest) {
72+
String endpoint = new RequestConverters.EndpointBuilder()
73+
.addPathPartAsIs("_data_frame", "transforms")
74+
.addPathPart(stopRequest.getId())
75+
.addPathPartAsIs("_stop")
76+
.build();
77+
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
78+
RequestConverters.Params params = new RequestConverters.Params(request);
79+
if (stopRequest.getWaitForCompletion() != null) {
80+
params.withWaitForCompletion(stopRequest.getWaitForCompletion());
81+
}
82+
if (stopRequest.getTimeout() != null) {
83+
params.withTimeout(stopRequest.getTimeout());
84+
}
85+
return request;
86+
}
5387
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
20+
package org.elasticsearch.client.core;
21+
22+
import org.elasticsearch.ElasticsearchException;
23+
import org.elasticsearch.action.TaskOperationFailure;
24+
import org.elasticsearch.common.Nullable;
25+
import org.elasticsearch.common.ParseField;
26+
import org.elasticsearch.common.TriFunction;
27+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
28+
29+
import java.util.ArrayList;
30+
import java.util.Collections;
31+
import java.util.List;
32+
import java.util.Objects;
33+
34+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
35+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
36+
37+
public class AcknowledgedTasksResponse {
38+
39+
protected static final ParseField TASK_FAILURES = new ParseField("task_failures");
40+
protected static final ParseField NODE_FAILURES = new ParseField("node_failures");
41+
42+
@SuppressWarnings("unchecked")
43+
protected static <T extends AcknowledgedTasksResponse> ConstructingObjectParser<T, Void> generateParser(
44+
String name,
45+
TriFunction<Boolean, List<TaskOperationFailure>, List<? extends ElasticsearchException>, T> ctor,
46+
String ackFieldName) {
47+
48+
ConstructingObjectParser<T, Void> parser = new ConstructingObjectParser<>(name, true,
49+
args -> ctor.apply((boolean) args[0], (List<TaskOperationFailure>) args[1], (List<ElasticsearchException>) args[2]));
50+
parser.declareBoolean(constructorArg(), new ParseField(ackFieldName));
51+
parser.declareObjectArray(optionalConstructorArg(), (p, c) -> TaskOperationFailure.fromXContent(p), TASK_FAILURES);
52+
parser.declareObjectArray(optionalConstructorArg(), (p, c) -> ElasticsearchException.fromXContent(p), NODE_FAILURES);
53+
return parser;
54+
}
55+
56+
private boolean acknowledged;
57+
private List<TaskOperationFailure> taskFailures;
58+
private List<ElasticsearchException> nodeFailures;
59+
60+
public AcknowledgedTasksResponse(boolean acknowledged, @Nullable List<TaskOperationFailure> taskFailures,
61+
@Nullable List<? extends ElasticsearchException> nodeFailures) {
62+
this.acknowledged = acknowledged;
63+
this.taskFailures = taskFailures == null ? Collections.emptyList() : Collections.unmodifiableList(new ArrayList<>(taskFailures));
64+
this.nodeFailures = nodeFailures == null ? Collections.emptyList() : Collections.unmodifiableList(new ArrayList<>(nodeFailures));
65+
}
66+
67+
public boolean isAcknowledged() {
68+
return acknowledged;
69+
}
70+
71+
public List<TaskOperationFailure> getTaskFailures() {
72+
return taskFailures;
73+
}
74+
75+
public List<ElasticsearchException> getNodeFailures() {
76+
return nodeFailures;
77+
}
78+
79+
@Override
80+
public boolean equals(Object obj) {
81+
if (this == obj) {
82+
return true;
83+
}
84+
85+
if (obj == null || getClass() != obj.getClass()) {
86+
return false;
87+
}
88+
89+
AcknowledgedTasksResponse other = (AcknowledgedTasksResponse) obj;
90+
return acknowledged == other.acknowledged
91+
&& taskFailures.equals(other.taskFailures)
92+
&& nodeFailures.equals(other.nodeFailures);
93+
}
94+
95+
@Override
96+
public int hashCode() {
97+
return Objects.hash(acknowledged, taskFailures, nodeFailures);
98+
}
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
20+
package org.elasticsearch.client.dataframe;
21+
22+
import org.elasticsearch.client.Validatable;
23+
import org.elasticsearch.client.ValidationException;
24+
import org.elasticsearch.common.unit.TimeValue;
25+
26+
import java.util.Objects;
27+
import java.util.Optional;
28+
29+
public class StartDataFrameTransformRequest implements Validatable {
30+
31+
private final String id;
32+
private TimeValue timeout;
33+
34+
public StartDataFrameTransformRequest(String id) {
35+
this.id = id;
36+
}
37+
38+
public StartDataFrameTransformRequest(String id, TimeValue timeout) {
39+
this.id = id;
40+
this.timeout = timeout;
41+
}
42+
43+
public String getId() {
44+
return id;
45+
}
46+
47+
public TimeValue getTimeout() {
48+
return timeout;
49+
}
50+
51+
public void setTimeout(TimeValue timeout) {
52+
this.timeout = timeout;
53+
}
54+
55+
@Override
56+
public Optional<ValidationException> validate() {
57+
if (id == null) {
58+
ValidationException validationException = new ValidationException();
59+
validationException.addValidationError("data frame transform id must not be null");
60+
return Optional.of(validationException);
61+
} else {
62+
return Optional.empty();
63+
}
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return Objects.hash(id, timeout);
69+
}
70+
71+
@Override
72+
public boolean equals(Object obj) {
73+
if (this == obj) {
74+
return true;
75+
}
76+
77+
if (obj == null || getClass() != obj.getClass()) {
78+
return false;
79+
}
80+
StartDataFrameTransformRequest other = (StartDataFrameTransformRequest) obj;
81+
return Objects.equals(this.id, other.id)
82+
&& Objects.equals(this.timeout, other.timeout);
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
20+
package org.elasticsearch.client.dataframe;
21+
22+
import org.elasticsearch.ElasticsearchException;
23+
import org.elasticsearch.action.TaskOperationFailure;
24+
import org.elasticsearch.client.core.AcknowledgedTasksResponse;
25+
import org.elasticsearch.common.Nullable;
26+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
27+
import org.elasticsearch.common.xcontent.XContentParser;
28+
29+
import java.io.IOException;
30+
import java.util.List;
31+
32+
public class StartDataFrameTransformResponse extends AcknowledgedTasksResponse {
33+
34+
private static final String STARTED = "started";
35+
36+
private static final ConstructingObjectParser<StartDataFrameTransformResponse, Void> PARSER =
37+
AcknowledgedTasksResponse.generateParser("start_data_frame_transform_response", StartDataFrameTransformResponse::new, STARTED);
38+
39+
public static StartDataFrameTransformResponse fromXContent(final XContentParser parser) throws IOException {
40+
return PARSER.parse(parser, null);
41+
}
42+
43+
public StartDataFrameTransformResponse(boolean started, @Nullable List<TaskOperationFailure> taskFailures,
44+
@Nullable List<? extends ElasticsearchException> nodeFailures) {
45+
super(started, taskFailures, nodeFailures);
46+
}
47+
48+
public boolean isStarted() {
49+
return isAcknowledged();
50+
}
51+
}

0 commit comments

Comments
 (0)