Skip to content

Commit 0b24d84

Browse files
committed
HLRC: Add get rollup job (elastic#33921)
Adds support for the get rollup job to the High Level REST Client. I had to do three interesting and unexpected things: 1. I ported the rollup state wiping code into the high level client tests. I'll move this into the test framework in a followup and remove the x-pack version. 2. The `timeout` in the rollup config was serialized using the `toString` representation of `TimeValue` which produces fractional time values which are more human readable but aren't supported by parsing. So I switched it to `getStringRep`. 3. Refactor the xcontent round trip testing utilities so we can test parsing of classes that don't implements `ToXContent`.
1 parent 0288322 commit 0b24d84

File tree

15 files changed

+1093
-51
lines changed

15 files changed

+1093
-51
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/RollupClient.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package org.elasticsearch.client;
2121

2222
import org.elasticsearch.action.ActionListener;
23+
import org.elasticsearch.client.rollup.GetRollupJobRequest;
24+
import org.elasticsearch.client.rollup.GetRollupJobResponse;
2325
import org.elasticsearch.client.rollup.PutRollupJobRequest;
2426
import org.elasticsearch.client.rollup.PutRollupJobResponse;
2527

@@ -73,4 +75,37 @@ public void putRollupJobAsync(PutRollupJobRequest request, RequestOptions option
7375
PutRollupJobResponse::fromXContent,
7476
listener, Collections.emptySet());
7577
}
78+
79+
/**
80+
* Get a rollup job from the cluster.
81+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-put-job.html">
82+
* the docs</a> for more.
83+
* @param request the request
84+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
85+
* @return the response
86+
* @throws IOException in case there is a problem sending the request or parsing back the response
87+
*/
88+
public GetRollupJobResponse getRollupJob(GetRollupJobRequest request, RequestOptions options) throws IOException {
89+
return restHighLevelClient.performRequestAndParseEntity(request,
90+
RollupRequestConverters::getJob,
91+
options,
92+
GetRollupJobResponse::fromXContent,
93+
Collections.emptySet());
94+
}
95+
96+
/**
97+
* Asynchronously get a rollup job from the cluster.
98+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-put-job.html">
99+
* the docs</a> for more.
100+
* @param request the request
101+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
102+
* @param listener the listener to be notified upon request completion
103+
*/
104+
public void getRollupJobAsync(GetRollupJobRequest request, RequestOptions options, ActionListener<GetRollupJobResponse> listener) {
105+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
106+
RollupRequestConverters::getJob,
107+
options,
108+
GetRollupJobResponse::fromXContent,
109+
listener, Collections.emptySet());
110+
}
76111
}

client/rest-high-level/src/main/java/org/elasticsearch/client/RollupRequestConverters.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
*/
1919
package org.elasticsearch.client;
2020

21+
import org.apache.http.client.methods.HttpGet;
2122
import org.apache.http.client.methods.HttpPut;
23+
import org.elasticsearch.client.rollup.GetRollupJobRequest;
2224
import org.elasticsearch.client.rollup.PutRollupJobRequest;
2325

2426
import java.io.IOException;
@@ -42,4 +44,14 @@ static Request putJob(final PutRollupJobRequest putRollupJobRequest) throws IOEx
4244
request.setEntity(createEntity(putRollupJobRequest, REQUEST_BODY_CONTENT_TYPE));
4345
return request;
4446
}
47+
48+
static Request getJob(final GetRollupJobRequest getRollupJobRequest) {
49+
String endpoint = new RequestConverters.EndpointBuilder()
50+
.addPathPartAsIs("_xpack")
51+
.addPathPartAsIs("rollup")
52+
.addPathPartAsIs("job")
53+
.addPathPart(getRollupJobRequest.getJobId())
54+
.build();
55+
return new Request(HttpGet.METHOD_NAME, endpoint);
56+
}
4557
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.rollup;
21+
22+
import org.elasticsearch.client.Validatable;
23+
import org.elasticsearch.client.ValidationException;
24+
25+
import java.util.Objects;
26+
import java.util.Optional;
27+
28+
/**
29+
* Request to fetch rollup jobs.
30+
*/
31+
public class GetRollupJobRequest implements Validatable {
32+
private final String jobId;
33+
34+
/**
35+
* Create a requets .
36+
* @param jobId id of the job to return or {@code _all} to return all jobs
37+
*/
38+
public GetRollupJobRequest(final String jobId) {
39+
Objects.requireNonNull(jobId, "jobId is required");
40+
if ("_all".equals(jobId)) {
41+
throw new IllegalArgumentException("use the default ctor to ask for all jobs");
42+
}
43+
this.jobId = jobId;
44+
}
45+
46+
/**
47+
* Create a request to load all rollup jobs.
48+
*/
49+
public GetRollupJobRequest() {
50+
this.jobId = "_all";
51+
}
52+
53+
/**
54+
* ID of the job to return.
55+
*/
56+
public String getJobId() {
57+
return jobId;
58+
}
59+
60+
@Override
61+
public Optional<ValidationException> validate() {
62+
return Optional.empty();
63+
}
64+
65+
@Override
66+
public boolean equals(Object o) {
67+
if (this == o) return true;
68+
if (o == null || getClass() != o.getClass()) return false;
69+
final GetRollupJobRequest that = (GetRollupJobRequest) o;
70+
return jobId.equals(that.jobId);
71+
}
72+
73+
@Override
74+
public int hashCode() {
75+
return Objects.hash(jobId);
76+
}
77+
}

0 commit comments

Comments
 (0)