From 5f559810ce1c0e48826ad933d560d26bc360ddfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Wed, 14 Nov 2018 15:02:05 +0100 Subject: [PATCH 1/2] HLRC: Add parameters to stopRollupJon API 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 ++++++++++++-- 5 files changed, 59 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 d448275d35845..7f6b422d866c0 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.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.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 @@ private static Request getStyleRequest(String method, GetRequest getRequest) { 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 @@ static Request sourceExists(GetRequest getRequest) { 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 175c74ee76bdb..7fca7c89b541d 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 @@ static Request stopJob(final StopRollupJobRequest stopRollupJobRequest) throws I .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 948dc5deac2a5..05c8836e2bf19 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 boolean equals(Object o) { 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 cc69abb3bbf3f..671baee9377c2 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 void testPutStartAndGetRollupJob() throws Exception { // 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 12907f0f3b655..b4edeb46422ff 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.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 void testStartJob() throws IOException { 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()); } From 6ab274d2a9d908bc3800f3595f9335548ef33e53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Wed, 14 Nov 2018 17:51:29 +0100 Subject: [PATCH 2/2] Add parameters to HL client docs --- .../client/documentation/RollupDocumentationIT.java | 2 ++ docs/java-rest/high-level/rollup/stop_job.asciidoc | 5 +++++ 2 files changed, 7 insertions(+) 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 6ace43099309b..2689f9663a735 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 void testStopRollupJob() throws Exception { 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 41ec965bb7c28..cba1dcdd2d374 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