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
This commit is contained in:
Christoph Büscher 2018-11-15 10:50:48 +01:00 committed by GitHub
parent 8e2c84ad8b
commit bc5b1afb55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 66 additions and 6 deletions

View File

@ -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");

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -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 {

View File

@ -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());
}

View File

@ -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

View File

@ -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