Skip to content

Commit 006c38d

Browse files
author
Christoph Büscher
committed
HLRC: Add parameters to stopRollupJob API (#35545)
With #34811 the API for stopping rollup jobs got two new url parameters "wait_for_completion" and "timeout". This change adds these to the HLRC APIs as well. Relates to #34811
1 parent 4edd706 commit 006c38d

File tree

7 files changed

+66
-6
lines changed

7 files changed

+66
-6
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.elasticsearch.action.support.WriteRequest;
5151
import org.elasticsearch.action.update.UpdateRequest;
5252
import org.elasticsearch.client.core.CountRequest;
53+
import org.elasticsearch.client.core.TermVectorsRequest;
5354
import org.elasticsearch.client.security.RefreshPolicy;
5455
import org.elasticsearch.cluster.health.ClusterHealthStatus;
5556
import org.elasticsearch.common.Nullable;
@@ -78,7 +79,6 @@
7879
import org.elasticsearch.script.mustache.SearchTemplateRequest;
7980
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
8081
import org.elasticsearch.tasks.TaskId;
81-
import org.elasticsearch.client.core.TermVectorsRequest;
8282

8383
import java.io.ByteArrayOutputStream;
8484
import java.io.IOException;
@@ -268,7 +268,7 @@ private static Request getStyleRequest(String method, GetRequest getRequest) {
268268

269269
return request;
270270
}
271-
271+
272272
static Request sourceExists(GetRequest getRequest) {
273273
Request request = new Request(HttpHead.METHOD_NAME, endpoint(getRequest.index(), getRequest.type(), getRequest.id(), "_source"));
274274

@@ -279,7 +279,7 @@ static Request sourceExists(GetRequest getRequest) {
279279
parameters.withRealtime(getRequest.realtime());
280280
// Version params are not currently supported by the source exists API so are not passed
281281
return request;
282-
}
282+
}
283283

284284
static Request multiGet(MultiGetRequest multiGetRequest) throws IOException {
285285
Request request = new Request(HttpPost.METHOD_NAME, "/_mget");

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,14 @@ static Request stopJob(final StopRollupJobRequest stopRollupJobRequest) throws I
6565
.addPathPart(stopRollupJobRequest.getJobId())
6666
.addPathPartAsIs("_stop")
6767
.build();
68-
return new Request(HttpPost.METHOD_NAME, endpoint);
68+
69+
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
70+
RequestConverters.Params parameters = new RequestConverters.Params(request);
71+
parameters.withTimeout(stopRollupJobRequest.timeout());
72+
if (stopRollupJobRequest.waitForCompletion() != null) {
73+
parameters.withWaitForCompletion(stopRollupJobRequest.waitForCompletion());
74+
}
75+
return request;
6976
}
7077

7178
static Request getJob(final GetRollupJobRequest getRollupJobRequest) {

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
package org.elasticsearch.client.rollup;
2020

2121
import org.elasticsearch.client.Validatable;
22+
import org.elasticsearch.common.unit.TimeValue;
2223

2324
import java.util.Objects;
2425

2526
public class StopRollupJobRequest implements Validatable {
2627

2728
private final String jobId;
29+
private TimeValue timeout;
30+
private Boolean waitForCompletion;
2831

2932
public StopRollupJobRequest(final String jobId) {
3033
this.jobId = Objects.requireNonNull(jobId, "id parameter must not be null");
@@ -46,4 +49,26 @@ public boolean equals(Object o) {
4649
public int hashCode() {
4750
return Objects.hash(jobId);
4851
}
52+
53+
/**
54+
* Sets the requests optional "timeout" parameter.
55+
*/
56+
public void timeout(TimeValue timeout) {
57+
this.timeout = timeout;
58+
}
59+
60+
public TimeValue timeout() {
61+
return this.timeout;
62+
}
63+
64+
/**
65+
* Sets the requests optional "wait_for_completion".
66+
*/
67+
public void waitForCompletion(boolean waitForCompletion) {
68+
this.waitForCompletion = waitForCompletion;
69+
}
70+
71+
public Boolean waitForCompletion() {
72+
return this.waitForCompletion;
73+
}
4974
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,14 @@ public void testPutAndGetRollupJob() throws Exception {
236236

237237
// stop the job
238238
StopRollupJobRequest stopRequest = new StopRollupJobRequest(id);
239+
stopRequest.waitForCompletion(randomBoolean());
239240
StopRollupJobResponse stopResponse = execute(stopRequest, rollupClient::stopRollupJob, rollupClient::stopRollupJobAsync);
240241
assertTrue(stopResponse.isAcknowledged());
242+
if (stopRequest.waitForCompletion()) {
243+
getResponse = execute(new GetRollupJobRequest(id), rollupClient::getRollupJob, rollupClient::getRollupJobAsync);
244+
assertThat(getResponse.getJobs(), hasSize(1));
245+
assertThat(getResponse.getJobs().get(0).getStatus().getState(), equalTo(IndexerState.STOPPED));
246+
}
241247
}
242248

243249
public void testGetMissingRollupJob() throws Exception {

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.client.rollup.StopRollupJobRequest;
2929
import org.elasticsearch.client.rollup.job.config.RollupJobConfig;
3030
import org.elasticsearch.client.rollup.job.config.RollupJobConfigTests;
31+
import org.elasticsearch.common.unit.TimeValue;
3132
import org.elasticsearch.test.ESTestCase;
3233

3334
import java.io.IOException;
@@ -64,13 +65,27 @@ public void testStartJob() throws IOException {
6465

6566
public void testStopJob() throws IOException {
6667
String jobId = randomAlphaOfLength(5);
67-
6868
StopRollupJobRequest stopJob = new StopRollupJobRequest(jobId);
69+
String expectedTimeOutString = null;
70+
String expectedWaitForCompletion = null;
71+
int expectedParameters = 0;
72+
if (randomBoolean()) {
73+
stopJob.timeout(TimeValue.parseTimeValue(randomPositiveTimeValue(), "timeout"));
74+
expectedTimeOutString = stopJob.timeout().getStringRep();
75+
expectedParameters++;
76+
}
77+
if (randomBoolean()) {
78+
stopJob.waitForCompletion(randomBoolean());
79+
expectedWaitForCompletion = stopJob.waitForCompletion().toString();
80+
expectedParameters++;
81+
}
6982

7083
Request request = RollupRequestConverters.stopJob(stopJob);
7184
assertThat(request.getEndpoint(), equalTo("/_xpack/rollup/job/" + jobId + "/_stop"));
7285
assertThat(HttpPost.METHOD_NAME, equalTo(request.getMethod()));
73-
assertThat(request.getParameters().keySet(), empty());
86+
assertThat(request.getParameters().keySet().size(), equalTo(expectedParameters));
87+
assertThat(request.getParameters().get("timeout"), equalTo(expectedTimeOutString));
88+
assertThat(request.getParameters().get("wait_for_completion"), equalTo(expectedWaitForCompletion));
7489
assertNull(request.getEntity());
7590
}
7691

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ public void testStopRollupJob() throws Exception {
288288
String id = "job_1";
289289
// tag::rollup-stop-job-request
290290
StopRollupJobRequest request = new StopRollupJobRequest(id); // <1>
291+
request.waitForCompletion(true); // <2>
292+
request.timeout(TimeValue.timeValueSeconds(10)); // <3>
291293
// end::rollup-stop-job-request
292294

293295

docs/java-rest/high-level/rollup/stop_job.asciidoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ The Stop Rollup Job API allows you to stop a job by ID.
1717
include-tagged::{doc-tests-file}[{api}-request]
1818
--------------------------------------------------
1919
<1> The ID of the job to stop.
20+
<2> Whether the request should wait that the stop operation has completed
21+
before returning (optional, defaults to `false`)
22+
<3> If `wait_for_completion=true`, this parameter controls how long to wait
23+
before giving up and throwing an error (optional, defaults to 30 seconds).
24+
2025

2126
[id="{upid}-{api}-response"]
2227
==== Response

0 commit comments

Comments
 (0)