Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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"));

Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
5 changes: 5 additions & 0 deletions docs/java-rest/high-level/rollup/stop_job.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down