Skip to content

Add start rollup job support to HL REST Client #34623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -28,6 +28,8 @@
import org.elasticsearch.client.rollup.GetRollupCapsResponse;
import org.elasticsearch.client.rollup.PutRollupJobRequest;
import org.elasticsearch.client.rollup.PutRollupJobResponse;
import org.elasticsearch.client.rollup.StartRollupJobRequest;
import org.elasticsearch.client.rollup.StartRollupJobResponse;

import java.io.IOException;
import java.util.Collections;
Expand Down Expand Up @@ -80,6 +82,40 @@ public void putRollupJobAsync(PutRollupJobRequest request, RequestOptions option
listener, Collections.emptySet());
}

/**
* Start a rollup job
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-start-job.html">
* the docs</a> for more.
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response
* @throws IOException in case there is a problem sending the request or parsing back the response
*/
public StartRollupJobResponse startRollupJob(StartRollupJobRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request,
RollupRequestConverters::startJob,
options,
StartRollupJobResponse::fromXContent,
Collections.emptySet());
}

/**
* Asynchronously start a rollup job
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-start-job.html">
* the docs</a> for more.
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion
*/
public void startRollupJobAsync(StartRollupJobRequest request, RequestOptions options,
ActionListener<StartRollupJobResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request,
RollupRequestConverters::startJob,
options,
StartRollupJobResponse::fromXContent,
listener, Collections.emptySet());
}

/**
* Delete a rollup job from the cluster
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-delete-job.html">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.elasticsearch.client.rollup.DeleteRollupJobRequest;
import org.elasticsearch.client.rollup.GetRollupJobRequest;
import org.elasticsearch.client.rollup.GetRollupCapsRequest;
import org.elasticsearch.client.rollup.PutRollupJobRequest;
import org.elasticsearch.client.rollup.StartRollupJobRequest;

import java.io.IOException;

Expand All @@ -48,6 +50,18 @@ static Request putJob(final PutRollupJobRequest putRollupJobRequest) throws IOEx
return request;
}

static Request startJob(final StartRollupJobRequest startRollupJobRequest) throws IOException {
String endpoint = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_xpack")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is now a variadic, so u can .addPathPartAsIs("_xpack", "rollup", "job")....

.addPathPartAsIs("rollup")
.addPathPartAsIs("job")
.addPathPart(startRollupJobRequest.getJobId())
.addPathPart("_start")
.build();
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
return request;
}

static Request getJob(final GetRollupJobRequest getRollupJobRequest) {
String endpoint = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_xpack")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@

public abstract class AcknowledgedResponse implements ToXContentObject {
private final boolean acknowledged;
private final String field;

public AcknowledgedResponse(final boolean acknowledged) {
public AcknowledgedResponse(final boolean acknowledged, final String field) {
this.acknowledged = acknowledged;
this.field = field;
}

public boolean isAcknowledged() {
Expand Down Expand Up @@ -58,7 +60,7 @@ public int hashCode() {
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject();
{
builder.field("acknowledged", isAcknowledged());
builder.field(field, isAcknowledged());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not sure i like changing this, because it would be nice if we had a more generic AcknowledgedResponse. I understand that yall need to override the "acknowledged" field, but maybe its best to have builder.field(getField(), isAcknowledged()); and override that in the child classes that are in this review. I wonder if the ConstructingObjectParser can also be put in here with the same abstraction.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np, will update

}
builder.endObject();
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@

public class DeleteRollupJobResponse extends AcknowledgedResponse {

private static final String PARSE_FIELD_NAME = "acknowledged";

public DeleteRollupJobResponse(boolean acknowledged) {
super(acknowledged);
super(acknowledged, PARSE_FIELD_NAME);
}

public static DeleteRollupJobResponse fromXContent(final XContentParser parser) throws IOException {
Expand All @@ -41,6 +43,6 @@ public static DeleteRollupJobResponse fromXContent(final XContentParser parser)
= new ConstructingObjectParser<>("delete_rollup_job_response", true,
args -> new DeleteRollupJobResponse((boolean) args[0]));
static {
PARSER.declareBoolean(constructorArg(), new ParseField("acknowledged"));
PARSER.declareBoolean(constructorArg(), new ParseField(PARSE_FIELD_NAME));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@

public class PutRollupJobResponse extends AcknowledgedResponse {

private static final String PARSE_FIELD_NAME = "acknowledged";

public PutRollupJobResponse(boolean acknowledged) {
super(acknowledged);
super(acknowledged, PARSE_FIELD_NAME);
}

public static PutRollupJobResponse fromXContent(final XContentParser parser) throws IOException {
Expand All @@ -40,6 +41,6 @@ public static PutRollupJobResponse fromXContent(final XContentParser parser) thr
private static final ConstructingObjectParser<PutRollupJobResponse, Void> PARSER
= new ConstructingObjectParser<>("put_rollup_job_response", true, args -> new PutRollupJobResponse((boolean) args[0]));
static {
PARSER.declareBoolean(constructorArg(), new ParseField("acknowledged"));
PARSER.declareBoolean(constructorArg(), new ParseField(PARSE_FIELD_NAME));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.rollup;

import org.elasticsearch.client.Validatable;

import java.util.Objects;

public class StartRollupJobRequest implements Validatable {

private final String jobId;

public StartRollupJobRequest(final String jobId) {
this.jobId = Objects.requireNonNull(jobId, "id parameter must not be null");
}

public String getJobId() {
return jobId;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final StartRollupJobRequest that = (StartRollupJobRequest) o;
return Objects.equals(jobId, that.jobId);
}

@Override
public int hashCode() {
return Objects.hash(jobId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.client.rollup;

import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;

public class StartRollupJobResponse extends AcknowledgedResponse {

private static final String PARSE_FIELD_NAME = "started";

public StartRollupJobResponse(boolean acknowledged) {
super(acknowledged, PARSE_FIELD_NAME);
}

public static StartRollupJobResponse fromXContent(final XContentParser parser) throws IOException {
return PARSER.parse(parser, null);
}

private static final ConstructingObjectParser<StartRollupJobResponse, Void> PARSER
= new ConstructingObjectParser<>("start_rollup_job_response", true,
args -> new StartRollupJobResponse((boolean) args[0]));
static {
PARSER.declareBoolean(constructorArg(), new ParseField(PARSE_FIELD_NAME));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.elasticsearch.client.rollup.GetRollupJobResponse.JobWrapper;
import org.elasticsearch.client.rollup.PutRollupJobRequest;
import org.elasticsearch.client.rollup.PutRollupJobResponse;
import org.elasticsearch.client.rollup.StartRollupJobRequest;
import org.elasticsearch.client.rollup.StartRollupJobResponse;
import org.elasticsearch.client.rollup.RollableIndexCaps;
import org.elasticsearch.client.rollup.RollupJobCaps;
import org.elasticsearch.client.rollup.job.config.DateHistogramGroupConfig;
Expand Down Expand Up @@ -151,7 +153,7 @@ public void testDeleteRollupJob() throws Exception {
PutRollupJobRequest putRollupJobRequest =
new PutRollupJobRequest(new RollupJobConfig(id, indexPattern, rollupIndex, cron, pageSize, groups, metrics, timeout));
final RollupClient rollupClient = highLevelClient().rollup();
PutRollupJobResponse response = execute(putRollupJobRequest, rollupClient::putRollupJob, rollupClient::putRollupJobAsync);
execute(putRollupJobRequest, rollupClient::putRollupJob, rollupClient::putRollupJobAsync);
DeleteRollupJobRequest deleteRollupJobRequest = new DeleteRollupJobRequest(id);
DeleteRollupJobResponse deleteRollupJobResponse = highLevelClient().rollup()
.deleteRollupJob(deleteRollupJobRequest, RequestOptions.DEFAULT);
Expand All @@ -165,8 +167,7 @@ public void testDeleteMissingRollupJob() {
assertThat(responseException.status().getStatus(), is(404));
}

@SuppressWarnings("unchecked")
public void testPutAndGetRollupJob() throws Exception {
public void testPutStartAndGetRollupJob() throws Exception {
// TODO expand this to also test with histogram and terms?
final GroupConfig groups = new GroupConfig(new DateHistogramGroupConfig("date", DateHistogramInterval.DAY));
final List<MetricConfig> metrics = Arrays.asList(
Expand All @@ -181,9 +182,9 @@ public void testPutAndGetRollupJob() throws Exception {
PutRollupJobResponse response = execute(putRollupJobRequest, rollupClient::putRollupJob, rollupClient::putRollupJobAsync);
assertTrue(response.isAcknowledged());

// TODO Replace this with the Rollup Start Job API
Response startResponse = client().performRequest(new Request("POST", "/_xpack/rollup/job/" + id + "/_start"));
assertEquals(RestStatus.OK.getStatus(), startResponse.getHttpResponse().getStatusLine().getStatusCode());
StartRollupJobRequest startRequest = new StartRollupJobRequest(id);
StartRollupJobResponse startResponse = execute(startRequest, rollupClient::startRollupJob, rollupClient::startRollupJobAsync);
assertTrue(startResponse.isAcknowledged());

assertBusy(() -> {
SearchResponse searchResponse = highLevelClient().search(new SearchRequest(rollupIndex), RequestOptions.DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@
package org.elasticsearch.client;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.elasticsearch.client.rollup.GetRollupJobRequest;
import org.elasticsearch.client.rollup.PutRollupJobRequest;
import org.elasticsearch.client.rollup.StartRollupJobRequest;
import org.elasticsearch.client.rollup.job.config.RollupJobConfig;
import org.elasticsearch.client.rollup.job.config.RollupJobConfigTests;
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

public class RollupRequestConvertersTests extends ESTestCase {
Expand All @@ -47,6 +49,18 @@ public void testPutJob() throws IOException {
RequestConvertersTests.assertToXContentBody(put, request.getEntity());
}

public void testStartJob() throws IOException {
String jobId = randomAlphaOfLength(5);

StartRollupJobRequest startJob = new StartRollupJobRequest(jobId);

Request request = RollupRequestConverters.startJob(startJob);
assertThat(request.getEndpoint(), equalTo("/_xpack/rollup/job/" + jobId + "/_start"));
assertThat(HttpPost.METHOD_NAME, equalTo(request.getMethod()));
assertThat(request.getParameters().keySet(), empty());
assertThat(request.getEntity(), nullValue());
}

public void testGetJob() {
boolean getAll = randomBoolean();
String job = getAll ? "_all" : RequestConvertersTests.randomIndicesNames(1, 1)[0];
Expand Down
Loading