Skip to content

Commit ce3bc14

Browse files
author
Christoph Büscher
committed
Add stop rollup job support to HL REST Client
This change adds support for stoping a rollup job to the High Level REST Client. Relates to #29827
1 parent 92e3473 commit ce3bc14

File tree

14 files changed

+368
-10
lines changed

14 files changed

+368
-10
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.elasticsearch.client.rollup.GetRollupCapsResponse;
2929
import org.elasticsearch.client.rollup.PutRollupJobRequest;
3030
import org.elasticsearch.client.rollup.PutRollupJobResponse;
31+
import org.elasticsearch.client.rollup.StopRollupJobRequest;
32+
import org.elasticsearch.client.rollup.StopRollupJobResponse;
3133

3234
import java.io.IOException;
3335
import java.util.Collections;
@@ -80,6 +82,40 @@ public void putRollupJobAsync(PutRollupJobRequest request, RequestOptions option
8082
listener, Collections.emptySet());
8183
}
8284

85+
/**
86+
* Stop a rollup job
87+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-stop-job.html">
88+
* the docs</a> for more.
89+
* @param request the request
90+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
91+
* @return the response
92+
* @throws IOException in case there is a problem sending the request or parsing back the response
93+
*/
94+
public StopRollupJobResponse stopRollupJob(StopRollupJobRequest request, RequestOptions options) throws IOException {
95+
return restHighLevelClient.performRequestAndParseEntity(request,
96+
RollupRequestConverters::stopJob,
97+
options,
98+
StopRollupJobResponse::fromXContent,
99+
Collections.emptySet());
100+
}
101+
102+
/**
103+
* Asynchronously stop a rollup job
104+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-stop-job.html">
105+
* the docs</a> for more.
106+
* @param request the request
107+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
108+
* @param listener the listener to be notified upon request completion
109+
*/
110+
public void stopRollupJobAsync(StopRollupJobRequest request, RequestOptions options,
111+
ActionListener<StopRollupJobResponse> listener) {
112+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
113+
RollupRequestConverters::stopJob,
114+
options,
115+
StopRollupJobResponse::fromXContent,
116+
listener, Collections.emptySet());
117+
}
118+
83119
/**
84120
* Delete a rollup job from the cluster
85121
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-delete-job.html">

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020

2121
import org.apache.http.client.methods.HttpDelete;
2222
import org.apache.http.client.methods.HttpGet;
23+
import org.apache.http.client.methods.HttpPost;
2324
import org.apache.http.client.methods.HttpPut;
2425
import org.elasticsearch.client.rollup.DeleteRollupJobRequest;
2526
import org.elasticsearch.client.rollup.GetRollupJobRequest;
2627
import org.elasticsearch.client.rollup.GetRollupCapsRequest;
2728
import org.elasticsearch.client.rollup.PutRollupJobRequest;
29+
import org.elasticsearch.client.rollup.StopRollupJobRequest;
2830

2931
import java.io.IOException;
3032

@@ -48,6 +50,18 @@ static Request putJob(final PutRollupJobRequest putRollupJobRequest) throws IOEx
4850
return request;
4951
}
5052

53+
static Request stopJob(final StopRollupJobRequest stopRollupJobRequest) throws IOException {
54+
String endpoint = new RequestConverters.EndpointBuilder()
55+
.addPathPartAsIs("_xpack")
56+
.addPathPartAsIs("rollup")
57+
.addPathPartAsIs("job")
58+
.addPathPart(stopRollupJobRequest.getJobId())
59+
.addPathPart("_stop")
60+
.build();
61+
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
62+
return request;
63+
}
64+
5165
static Request getJob(final GetRollupJobRequest getRollupJobRequest) {
5266
String endpoint = new RequestConverters.EndpointBuilder()
5367
.addPathPartAsIs("_xpack")

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828

2929
public abstract class AcknowledgedResponse implements ToXContentObject {
3030
private final boolean acknowledged;
31+
private final String field;
3132

32-
public AcknowledgedResponse(final boolean acknowledged) {
33+
public AcknowledgedResponse(final boolean acknowledged, final String field) {
3334
this.acknowledged = acknowledged;
35+
this.field = field;
3436
}
3537

3638
public boolean isAcknowledged() {
@@ -58,7 +60,7 @@ public int hashCode() {
5860
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
5961
builder.startObject();
6062
{
61-
builder.field("acknowledged", isAcknowledged());
63+
builder.field(field, isAcknowledged());
6264
}
6365
builder.endObject();
6466
return builder;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929

3030
public class DeleteRollupJobResponse extends AcknowledgedResponse {
3131

32+
private static final String PARSE_FIELD_NAME = "acknowledged";
33+
3234
public DeleteRollupJobResponse(boolean acknowledged) {
33-
super(acknowledged);
35+
super(acknowledged, PARSE_FIELD_NAME);
3436
}
3537

3638
public static DeleteRollupJobResponse fromXContent(final XContentParser parser) throws IOException {
@@ -41,6 +43,6 @@ public static DeleteRollupJobResponse fromXContent(final XContentParser parser)
4143
= new ConstructingObjectParser<>("delete_rollup_job_response", true,
4244
args -> new DeleteRollupJobResponse((boolean) args[0]));
4345
static {
44-
PARSER.declareBoolean(constructorArg(), new ParseField("acknowledged"));
46+
PARSER.declareBoolean(constructorArg(), new ParseField(PARSE_FIELD_NAME));
4547
}
4648
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828

2929
public class PutRollupJobResponse extends AcknowledgedResponse {
3030

31+
private static final String PARSE_FIELD_NAME = "acknowledged";
3132

3233
public PutRollupJobResponse(boolean acknowledged) {
33-
super(acknowledged);
34+
super(acknowledged, PARSE_FIELD_NAME);
3435
}
3536

3637
public static PutRollupJobResponse fromXContent(final XContentParser parser) throws IOException {
@@ -40,6 +41,6 @@ public static PutRollupJobResponse fromXContent(final XContentParser parser) thr
4041
private static final ConstructingObjectParser<PutRollupJobResponse, Void> PARSER
4142
= new ConstructingObjectParser<>("put_rollup_job_response", true, args -> new PutRollupJobResponse((boolean) args[0]));
4243
static {
43-
PARSER.declareBoolean(constructorArg(), new ParseField("acknowledged"));
44+
PARSER.declareBoolean(constructorArg(), new ParseField(PARSE_FIELD_NAME));
4445
}
4546
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
package org.elasticsearch.client.rollup;
20+
21+
import org.elasticsearch.client.Validatable;
22+
23+
import java.util.Objects;
24+
25+
public class StopRollupJobRequest implements Validatable {
26+
27+
private final String jobId;
28+
29+
public StopRollupJobRequest(final String jobId) {
30+
this.jobId = Objects.requireNonNull(jobId, "id parameter must not be null");
31+
}
32+
33+
public String getJobId() {
34+
return jobId;
35+
}
36+
37+
@Override
38+
public boolean equals(Object o) {
39+
if (this == o) return true;
40+
if (o == null || getClass() != o.getClass()) return false;
41+
final StopRollupJobRequest that = (StopRollupJobRequest) o;
42+
return Objects.equals(jobId, that.jobId);
43+
}
44+
45+
@Override
46+
public int hashCode() {
47+
return Objects.hash(jobId);
48+
}
49+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.rollup;
21+
22+
import org.elasticsearch.common.ParseField;
23+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
24+
import org.elasticsearch.common.xcontent.XContentParser;
25+
26+
import java.io.IOException;
27+
28+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
29+
30+
public class StopRollupJobResponse extends AcknowledgedResponse {
31+
32+
private static final String PARSE_FIELD_NAME = "stopped";
33+
34+
public StopRollupJobResponse(boolean acknowledged) {
35+
super(acknowledged, PARSE_FIELD_NAME);
36+
}
37+
38+
public static StopRollupJobResponse fromXContent(final XContentParser parser) throws IOException {
39+
return PARSER.parse(parser, null);
40+
}
41+
42+
private static final ConstructingObjectParser<StopRollupJobResponse, Void> PARSER
43+
= new ConstructingObjectParser<>("stop_rollup_job_response", true,
44+
args -> new StopRollupJobResponse((boolean) args[0]));
45+
static {
46+
PARSER.declareBoolean(constructorArg(), new ParseField(PARSE_FIELD_NAME));
47+
}
48+
}

client/rest-high-level/src/test/java/org/elasticsearch/client/RollupIT.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import org.elasticsearch.client.rollup.GetRollupJobResponse.JobWrapper;
4040
import org.elasticsearch.client.rollup.PutRollupJobRequest;
4141
import org.elasticsearch.client.rollup.PutRollupJobResponse;
42+
import org.elasticsearch.client.rollup.StopRollupJobRequest;
43+
import org.elasticsearch.client.rollup.StopRollupJobResponse;
4244
import org.elasticsearch.client.rollup.RollableIndexCaps;
4345
import org.elasticsearch.client.rollup.RollupJobCaps;
4446
import org.elasticsearch.client.rollup.job.config.DateHistogramGroupConfig;
@@ -151,7 +153,7 @@ public void testDeleteRollupJob() throws Exception {
151153
PutRollupJobRequest putRollupJobRequest =
152154
new PutRollupJobRequest(new RollupJobConfig(id, indexPattern, rollupIndex, cron, pageSize, groups, metrics, timeout));
153155
final RollupClient rollupClient = highLevelClient().rollup();
154-
PutRollupJobResponse response = execute(putRollupJobRequest, rollupClient::putRollupJob, rollupClient::putRollupJobAsync);
156+
execute(putRollupJobRequest, rollupClient::putRollupJob, rollupClient::putRollupJobAsync);
155157
DeleteRollupJobRequest deleteRollupJobRequest = new DeleteRollupJobRequest(id);
156158
DeleteRollupJobResponse deleteRollupJobResponse = highLevelClient().rollup()
157159
.deleteRollupJob(deleteRollupJobRequest, RequestOptions.DEFAULT);
@@ -165,7 +167,6 @@ public void testDeleteMissingRollupJob() {
165167
assertThat(responseException.status().getStatus(), is(404));
166168
}
167169

168-
@SuppressWarnings("unchecked")
169170
public void testPutAndGetRollupJob() throws Exception {
170171
// TODO expand this to also test with histogram and terms?
171172
final GroupConfig groups = new GroupConfig(new DateHistogramGroupConfig("date", DateHistogramInterval.DAY));
@@ -237,6 +238,11 @@ public void testPutAndGetRollupJob() throws Exception {
237238
assertThat(job.getStatus().getState(), either(equalTo(IndexerState.STARTED)).or(equalTo(IndexerState.INDEXING)));
238239
assertThat(job.getStatus().getCurrentPosition(), hasKey("date.date_histogram"));
239240
assertEquals(true, job.getStatus().getUpgradedDocumentId());
241+
242+
// stop the job
243+
StopRollupJobRequest startRequest = new StopRollupJobRequest(id);
244+
StopRollupJobResponse stopResponse = execute(startRequest, rollupClient::stopRollupJob, rollupClient::stopRollupJobAsync);
245+
assertTrue(stopResponse.isAcknowledged());
240246
}
241247

242248
public void testGetMissingRollupJob() throws Exception {

client/rest-high-level/src/test/java/org/elasticsearch/client/RollupRequestConvertersTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@
2020
package org.elasticsearch.client;
2121

2222
import org.apache.http.client.methods.HttpGet;
23+
import org.apache.http.client.methods.HttpPost;
2324
import org.apache.http.client.methods.HttpPut;
2425
import org.elasticsearch.client.rollup.GetRollupJobRequest;
2526
import org.elasticsearch.client.rollup.PutRollupJobRequest;
27+
import org.elasticsearch.client.rollup.StopRollupJobRequest;
2628
import org.elasticsearch.client.rollup.job.config.RollupJobConfig;
2729
import org.elasticsearch.client.rollup.job.config.RollupJobConfigTests;
2830
import org.elasticsearch.test.ESTestCase;
2931

3032
import java.io.IOException;
3133

32-
import static org.hamcrest.Matchers.equalTo;
3334
import static org.hamcrest.Matchers.empty;
35+
import static org.hamcrest.Matchers.equalTo;
3436
import static org.hamcrest.Matchers.nullValue;
3537

3638
public class RollupRequestConvertersTests extends ESTestCase {
@@ -47,6 +49,18 @@ public void testPutJob() throws IOException {
4749
RequestConvertersTests.assertToXContentBody(put, request.getEntity());
4850
}
4951

52+
public void testStopJob() throws IOException {
53+
String jobId = randomAlphaOfLength(5);
54+
55+
StopRollupJobRequest stopJob = new StopRollupJobRequest(jobId);
56+
57+
Request request = RollupRequestConverters.stopJob(stopJob);
58+
assertThat(request.getEndpoint(), equalTo("/_xpack/rollup/job/" + jobId + "/_stop"));
59+
assertThat(HttpPost.METHOD_NAME, equalTo(request.getMethod()));
60+
assertThat(request.getParameters().keySet(), empty());
61+
assertThat(request.getEntity(), nullValue());
62+
}
63+
5064
public void testGetJob() {
5165
boolean getAll = randomBoolean();
5266
String job = getAll ? "_all" : RequestConvertersTests.randomIndicesNames(1, 1)[0];

0 commit comments

Comments
 (0)