Skip to content

Commit c6cd502

Browse files
authored
Add GetRollupCaps API to high level rest client (#32880) (#34647)
Adds GetRollupCaps API to the HLRC, and tweaks some of the Caps objects to be immutable. Also various style tweaks
1 parent fa97fd4 commit c6cd502

File tree

24 files changed

+1279
-109
lines changed

24 files changed

+1279
-109
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -1056,3 +1056,4 @@ private static String encodePart(String pathPart) {
10561056
}
10571057
}
10581058
}
1059+

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

+39
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.elasticsearch.action.ActionListener;
2323
import org.elasticsearch.client.rollup.GetRollupJobRequest;
2424
import org.elasticsearch.client.rollup.GetRollupJobResponse;
25+
import org.elasticsearch.client.rollup.GetRollupCapsRequest;
26+
import org.elasticsearch.client.rollup.GetRollupCapsResponse;
2527
import org.elasticsearch.client.rollup.PutRollupJobRequest;
2628
import org.elasticsearch.client.rollup.PutRollupJobResponse;
2729

@@ -101,11 +103,48 @@ public GetRollupJobResponse getRollupJob(GetRollupJobRequest request, RequestOpt
101103
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
102104
* @param listener the listener to be notified upon request completion
103105
*/
106+
107+
104108
public void getRollupJobAsync(GetRollupJobRequest request, RequestOptions options, ActionListener<GetRollupJobResponse> listener) {
105109
restHighLevelClient.performRequestAsyncAndParseEntity(request,
106110
RollupRequestConverters::getJob,
107111
options,
108112
GetRollupJobResponse::fromXContent,
109113
listener, Collections.emptySet());
110114
}
115+
116+
/**
117+
* Get the Rollup Capabilities of a target (non-rollup) index or pattern
118+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/master/rollup-get-rollup-caps.html">
119+
* the docs</a> for more.
120+
* @param request the request
121+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
122+
* @return the response
123+
* @throws IOException in case there is a problem sending the request or parsing back the response
124+
*/
125+
public GetRollupCapsResponse getRollupCapabilities(GetRollupCapsRequest request, RequestOptions options) throws IOException {
126+
return restHighLevelClient.performRequestAndParseEntity(request,
127+
RollupRequestConverters::getRollupCaps,
128+
options,
129+
GetRollupCapsResponse::fromXContent,
130+
Collections.emptySet());
131+
}
132+
133+
/**
134+
* Asynchronously Get the Rollup Capabilities of a target (non-rollup) index or pattern
135+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-put-job.html">
136+
* the docs</a> for more.
137+
* @param request the request
138+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
139+
* @param listener the listener to be notified upon request completion
140+
*/
141+
public void getRollupCapabilitiesAsync(GetRollupCapsRequest request, RequestOptions options,
142+
ActionListener<GetRollupCapsResponse> listener) {
143+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
144+
RollupRequestConverters::getRollupCaps,
145+
options,
146+
GetRollupCapsResponse::fromXContent,
147+
listener,
148+
Collections.emptySet());
149+
}
111150
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.http.client.methods.HttpGet;
2222
import org.apache.http.client.methods.HttpPut;
2323
import org.elasticsearch.client.rollup.GetRollupJobRequest;
24+
import org.elasticsearch.client.rollup.GetRollupCapsRequest;
2425
import org.elasticsearch.client.rollup.PutRollupJobRequest;
2526

2627
import java.io.IOException;
@@ -54,4 +55,16 @@ static Request getJob(final GetRollupJobRequest getRollupJobRequest) {
5455
.build();
5556
return new Request(HttpGet.METHOD_NAME, endpoint);
5657
}
58+
59+
static Request getRollupCaps(final GetRollupCapsRequest getRollupCapsRequest) throws IOException {
60+
String endpoint = new RequestConverters.EndpointBuilder()
61+
.addPathPartAsIs("_xpack")
62+
.addPathPartAsIs("rollup")
63+
.addPathPartAsIs("data")
64+
.addPathPart(getRollupCapsRequest.getIndexPattern())
65+
.build();
66+
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
67+
request.setEntity(createEntity(getRollupCapsRequest, REQUEST_BODY_CONTENT_TYPE));
68+
return request;
69+
}
5770
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
package org.elasticsearch.client.rollup;
20+
21+
import org.elasticsearch.client.Validatable;
22+
import org.elasticsearch.cluster.metadata.MetaData;
23+
import org.elasticsearch.common.Strings;
24+
import org.elasticsearch.common.xcontent.ToXContentObject;
25+
import org.elasticsearch.common.xcontent.XContentBuilder;
26+
27+
import java.io.IOException;
28+
import java.util.Objects;
29+
30+
public class GetRollupCapsRequest implements Validatable, ToXContentObject {
31+
private static final String ID = "id";
32+
private final String indexPattern;
33+
34+
public GetRollupCapsRequest(final String indexPattern) {
35+
if (Strings.isNullOrEmpty(indexPattern) || indexPattern.equals("*")) {
36+
this.indexPattern = MetaData.ALL;
37+
} else {
38+
this.indexPattern = indexPattern;
39+
}
40+
}
41+
42+
public String getIndexPattern() {
43+
return indexPattern;
44+
}
45+
46+
@Override
47+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
48+
builder.startObject();
49+
builder.field(ID, indexPattern);
50+
builder.endObject();
51+
return builder;
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
return Objects.hash(indexPattern);
57+
}
58+
59+
@Override
60+
public boolean equals(Object obj) {
61+
if (obj == null) {
62+
return false;
63+
}
64+
if (getClass() != obj.getClass()) {
65+
return false;
66+
}
67+
GetRollupCapsRequest other = (GetRollupCapsRequest) obj;
68+
return Objects.equals(indexPattern, other.indexPattern);
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
package org.elasticsearch.client.rollup;
20+
21+
import org.elasticsearch.common.Strings;
22+
import org.elasticsearch.common.xcontent.ToXContent;
23+
import org.elasticsearch.common.xcontent.ToXContentObject;
24+
import org.elasticsearch.common.xcontent.XContentBuilder;
25+
import org.elasticsearch.common.xcontent.XContentParser;
26+
27+
import java.io.IOException;
28+
import java.util.Collections;
29+
import java.util.HashMap;
30+
import java.util.Map;
31+
import java.util.Objects;
32+
33+
public class GetRollupCapsResponse implements ToXContentObject {
34+
35+
private final Map<String, RollableIndexCaps> jobs;
36+
37+
public GetRollupCapsResponse(final Map<String, RollableIndexCaps> jobs) {
38+
this.jobs = Collections.unmodifiableMap(Objects.requireNonNull(jobs));
39+
}
40+
41+
public Map<String, RollableIndexCaps> getJobs() {
42+
return jobs;
43+
}
44+
45+
@Override
46+
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
47+
builder.startObject();
48+
for (Map.Entry<String, RollableIndexCaps> entry : jobs.entrySet()) {
49+
entry.getValue().toXContent(builder, params);
50+
}
51+
builder.endObject();
52+
return builder;
53+
}
54+
55+
public static GetRollupCapsResponse fromXContent(final XContentParser parser) throws IOException {
56+
Map<String, RollableIndexCaps> jobs = new HashMap<>();
57+
XContentParser.Token token = parser.nextToken();
58+
if (token.equals(XContentParser.Token.START_OBJECT)) {
59+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
60+
if (token.equals(XContentParser.Token.FIELD_NAME)) {
61+
String pattern = parser.currentName();
62+
63+
RollableIndexCaps cap = RollableIndexCaps.PARSER.apply(pattern).apply(parser, null);
64+
jobs.put(pattern, cap);
65+
}
66+
}
67+
}
68+
return new GetRollupCapsResponse(jobs);
69+
}
70+
71+
@Override
72+
public int hashCode() {
73+
return Objects.hash(jobs);
74+
}
75+
76+
@Override
77+
public boolean equals(Object obj) {
78+
if (obj == null) {
79+
return false;
80+
}
81+
if (getClass() != obj.getClass()) {
82+
return false;
83+
}
84+
GetRollupCapsResponse other = (GetRollupCapsResponse) obj;
85+
return Objects.equals(jobs, other.jobs);
86+
}
87+
88+
@Override
89+
public final String toString() {
90+
return Strings.toString(this);
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
package org.elasticsearch.client.rollup;
20+
21+
import org.elasticsearch.common.ParseField;
22+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
23+
import org.elasticsearch.common.xcontent.ToXContentFragment;
24+
import org.elasticsearch.common.xcontent.XContentBuilder;
25+
26+
import java.io.IOException;
27+
import java.util.Collections;
28+
import java.util.Comparator;
29+
import java.util.List;
30+
import java.util.Objects;
31+
import java.util.function.Function;
32+
import java.util.stream.Collectors;
33+
34+
/**
35+
* Represents the rollup capabilities of a non-rollup index. E.g. what values/aggregations
36+
* were rolled up for this index, in what rollup jobs that data is stored and where those
37+
* concrete rollup indices exist
38+
*
39+
* The index name can either be a single index, or an index pattern (logstash-*)
40+
*/
41+
public class RollableIndexCaps implements ToXContentFragment {
42+
private static final ParseField ROLLUP_JOBS = new ParseField("rollup_jobs");
43+
44+
public static final Function<String, ConstructingObjectParser<RollableIndexCaps, Void>> PARSER = indexName -> {
45+
@SuppressWarnings("unchecked")
46+
ConstructingObjectParser<RollableIndexCaps, Void> p
47+
= new ConstructingObjectParser<>(indexName,
48+
a -> new RollableIndexCaps(indexName, (List<RollupJobCaps>) a[0]));
49+
50+
p.declareObjectArray(ConstructingObjectParser.constructorArg(), RollupJobCaps.PARSER::apply,
51+
ROLLUP_JOBS);
52+
return p;
53+
};
54+
55+
private final String indexName;
56+
private final List<RollupJobCaps> jobCaps;
57+
58+
RollableIndexCaps(final String indexName, final List<RollupJobCaps> caps) {
59+
this.indexName = indexName;
60+
this.jobCaps = Collections.unmodifiableList(Objects.requireNonNull(caps)
61+
.stream()
62+
.sorted(Comparator.comparing(RollupJobCaps::getJobID))
63+
.collect(Collectors.toList()));
64+
}
65+
66+
public String getIndexName() {
67+
return indexName;
68+
}
69+
70+
public List<RollupJobCaps> getJobCaps() {
71+
return jobCaps;
72+
}
73+
74+
@Override
75+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
76+
builder.startObject(indexName);
77+
{
78+
builder.field(ROLLUP_JOBS.getPreferredName(), jobCaps);
79+
}
80+
builder.endObject();
81+
return builder;
82+
}
83+
84+
@Override
85+
public boolean equals(Object other) {
86+
if (this == other) {
87+
return true;
88+
}
89+
90+
if (other == null || getClass() != other.getClass()) {
91+
return false;
92+
}
93+
94+
RollableIndexCaps that = (RollableIndexCaps) other;
95+
return Objects.equals(this.jobCaps, that.jobCaps)
96+
&& Objects.equals(this.indexName, that.indexName);
97+
}
98+
99+
@Override
100+
public int hashCode() {
101+
return Objects.hash(jobCaps, indexName);
102+
}
103+
}

0 commit comments

Comments
 (0)