From bc5b1afb55c3b9a75c8d3f69c7832765918b439c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Thu, 15 Nov 2018 10:50:48 +0100 Subject: [PATCH] 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 --- .../client/RequestConverters.java | 6 ++--- .../client/RollupRequestConverters.java | 9 ++++++- .../client/rollup/StopRollupJobRequest.java | 25 +++++++++++++++++++ .../org/elasticsearch/client/RollupIT.java | 6 +++++ .../client/RollupRequestConvertersTests.java | 19 ++++++++++++-- .../documentation/RollupDocumentationIT.java | 2 ++ .../high-level/rollup/stop_job.asciidoc | 5 ++++ 7 files changed, 66 insertions(+), 6 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java index d448275d358..7f6b422d866 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java @@ -50,6 +50,7 @@ import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.client.core.CountRequest; +import org.elasticsearch.client.core.TermVectorsRequest; import org.elasticsearch.client.security.RefreshPolicy; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.common.Nullable; @@ -78,7 +79,6 @@ import org.elasticsearch.script.mustache.MultiSearchTemplateRequest; import org.elasticsearch.script.mustache.SearchTemplateRequest; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.tasks.TaskId; -import org.elasticsearch.client.core.TermVectorsRequest; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -264,7 +264,7 @@ final class RequestConverters { return request; } - + static Request sourceExists(GetRequest getRequest) { Request request = new Request(HttpHead.METHOD_NAME, endpoint(getRequest.index(), getRequest.type(), getRequest.id(), "_source")); @@ -275,7 +275,7 @@ final class RequestConverters { parameters.withRealtime(getRequest.realtime()); // Version params are not currently supported by the source exists API so are not passed return request; - } + } static Request multiGet(MultiGetRequest multiGetRequest) throws IOException { Request request = new Request(HttpPost.METHOD_NAME, "/_mget"); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RollupRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RollupRequestConverters.java index 175c74ee76b..7fca7c89b54 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RollupRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RollupRequestConverters.java @@ -65,7 +65,14 @@ final class RollupRequestConverters { .addPathPart(stopRollupJobRequest.getJobId()) .addPathPartAsIs("_stop") .build(); - return new Request(HttpPost.METHOD_NAME, endpoint); + + Request request = new Request(HttpPost.METHOD_NAME, endpoint); + RequestConverters.Params parameters = new RequestConverters.Params(request); + parameters.withTimeout(stopRollupJobRequest.timeout()); + if (stopRollupJobRequest.waitForCompletion() != null) { + parameters.withWaitForCompletion(stopRollupJobRequest.waitForCompletion()); + } + return request; } static Request getJob(final GetRollupJobRequest getRollupJobRequest) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/StopRollupJobRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/StopRollupJobRequest.java index 948dc5deac2..05c8836e2bf 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/StopRollupJobRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/StopRollupJobRequest.java @@ -19,12 +19,15 @@ package org.elasticsearch.client.rollup; import org.elasticsearch.client.Validatable; +import org.elasticsearch.common.unit.TimeValue; import java.util.Objects; public class StopRollupJobRequest implements Validatable { private final String jobId; + private TimeValue timeout; + private Boolean waitForCompletion; public StopRollupJobRequest(final String jobId) { this.jobId = Objects.requireNonNull(jobId, "id parameter must not be null"); @@ -46,4 +49,26 @@ public class StopRollupJobRequest implements Validatable { public int hashCode() { return Objects.hash(jobId); } + + /** + * Sets the requests optional "timeout" parameter. + */ + public void timeout(TimeValue timeout) { + this.timeout = timeout; + } + + public TimeValue timeout() { + return this.timeout; + } + + /** + * Sets the requests optional "wait_for_completion". + */ + public void waitForCompletion(boolean waitForCompletion) { + this.waitForCompletion = waitForCompletion; + } + + public Boolean waitForCompletion() { + return this.waitForCompletion; + } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RollupIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RollupIT.java index cc69abb3bbf..671baee9377 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RollupIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RollupIT.java @@ -235,8 +235,14 @@ public class RollupIT extends ESRestHighLevelClientTestCase { // stop the job StopRollupJobRequest stopRequest = new StopRollupJobRequest(id); + stopRequest.waitForCompletion(randomBoolean()); StopRollupJobResponse stopResponse = execute(stopRequest, rollupClient::stopRollupJob, rollupClient::stopRollupJobAsync); assertTrue(stopResponse.isAcknowledged()); + if (stopRequest.waitForCompletion()) { + getResponse = execute(new GetRollupJobRequest(id), rollupClient::getRollupJob, rollupClient::getRollupJobAsync); + assertThat(getResponse.getJobs(), hasSize(1)); + assertThat(getResponse.getJobs().get(0).getStatus().getState(), equalTo(IndexerState.STOPPED)); + } } public void testGetMissingRollupJob() throws Exception { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RollupRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RollupRequestConvertersTests.java index 12907f0f3b6..b4edeb46422 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RollupRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RollupRequestConvertersTests.java @@ -28,6 +28,7 @@ import org.elasticsearch.client.rollup.StartRollupJobRequest; import org.elasticsearch.client.rollup.StopRollupJobRequest; import org.elasticsearch.client.rollup.job.config.RollupJobConfig; import org.elasticsearch.client.rollup.job.config.RollupJobConfigTests; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -64,13 +65,27 @@ public class RollupRequestConvertersTests extends ESTestCase { public void testStopJob() throws IOException { String jobId = randomAlphaOfLength(5); - StopRollupJobRequest stopJob = new StopRollupJobRequest(jobId); + String expectedTimeOutString = null; + String expectedWaitForCompletion = null; + int expectedParameters = 0; + if (randomBoolean()) { + stopJob.timeout(TimeValue.parseTimeValue(randomPositiveTimeValue(), "timeout")); + expectedTimeOutString = stopJob.timeout().getStringRep(); + expectedParameters++; + } + if (randomBoolean()) { + stopJob.waitForCompletion(randomBoolean()); + expectedWaitForCompletion = stopJob.waitForCompletion().toString(); + expectedParameters++; + } Request request = RollupRequestConverters.stopJob(stopJob); assertThat(request.getEndpoint(), equalTo("/_xpack/rollup/job/" + jobId + "/_stop")); assertThat(HttpPost.METHOD_NAME, equalTo(request.getMethod())); - assertThat(request.getParameters().keySet(), empty()); + assertThat(request.getParameters().keySet().size(), equalTo(expectedParameters)); + assertThat(request.getParameters().get("timeout"), equalTo(expectedTimeOutString)); + assertThat(request.getParameters().get("wait_for_completion"), equalTo(expectedWaitForCompletion)); assertNull(request.getEntity()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/RollupDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/RollupDocumentationIT.java index 6ace4309930..2689f9663a7 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/RollupDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/RollupDocumentationIT.java @@ -288,6 +288,8 @@ public class RollupDocumentationIT extends ESRestHighLevelClientTestCase { String id = "job_1"; // tag::rollup-stop-job-request StopRollupJobRequest request = new StopRollupJobRequest(id); // <1> + request.waitForCompletion(true); // <2> + request.timeout(TimeValue.timeValueSeconds(10)); // <3> // end::rollup-stop-job-request diff --git a/docs/java-rest/high-level/rollup/stop_job.asciidoc b/docs/java-rest/high-level/rollup/stop_job.asciidoc index 41ec965bb7c..cba1dcdd2d3 100644 --- a/docs/java-rest/high-level/rollup/stop_job.asciidoc +++ b/docs/java-rest/high-level/rollup/stop_job.asciidoc @@ -17,6 +17,11 @@ The Stop Rollup Job API allows you to stop a job by ID. include-tagged::{doc-tests-file}[{api}-request] -------------------------------------------------- <1> The ID of the job to stop. +<2> Whether the request should wait that the stop operation has completed +before returning (optional, defaults to `false`) +<3> If `wait_for_completion=true`, this parameter controls how long to wait +before giving up and throwing an error (optional, defaults to 30 seconds). + [id="{upid}-{api}-response"] ==== Response