Skip to content

Commit 362ab15

Browse files
author
Paul Sanwald
authored
backport 936faba to 6.x (#35045)
* backport 936faba to 6.x * address code review comments
1 parent 62f8da2 commit 362ab15

File tree

12 files changed

+425
-22
lines changed

12 files changed

+425
-22
lines changed

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

Lines changed: 36 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.DeleteRollupJobRequest;
24+
import org.elasticsearch.client.rollup.DeleteRollupJobResponse;
2325
import org.elasticsearch.client.rollup.GetRollupCapsRequest;
2426
import org.elasticsearch.client.rollup.GetRollupCapsResponse;
2527
import org.elasticsearch.client.rollup.GetRollupJobRequest;
@@ -114,6 +116,40 @@ public void startRollupJobAsync(StartRollupJobRequest request, RequestOptions op
114116
listener, Collections.emptySet());
115117
}
116118

119+
/**
120+
* Delete a rollup job from the cluster
121+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-delete-job.html">
122+
* the docs</a> for more.
123+
* @param request the request
124+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
125+
* @return the response
126+
* @throws IOException in case there is a problem sending the request or parsing back the response
127+
*/
128+
public DeleteRollupJobResponse deleteRollupJob(DeleteRollupJobRequest request, RequestOptions options) throws IOException {
129+
return restHighLevelClient.performRequestAndParseEntity(request,
130+
RollupRequestConverters::deleteJob,
131+
options,
132+
DeleteRollupJobResponse::fromXContent,
133+
Collections.emptySet());
134+
}
135+
/**
136+
* Asynchronously delete a rollup job from the cluster
137+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-delete-job.html">
138+
* The docs</a> for details.
139+
* @param request the request
140+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
141+
* @param listener the listener to be notified upon request completion
142+
*/
143+
public void deleteRollupJobAsync(DeleteRollupJobRequest request,
144+
RequestOptions options,
145+
ActionListener<DeleteRollupJobResponse> listener) {
146+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
147+
RollupRequestConverters::deleteJob,
148+
options,
149+
DeleteRollupJobResponse::fromXContent,
150+
listener, Collections.emptySet());
151+
}
152+
117153
/**
118154
* Get a rollup job from the cluster.
119155
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-put-job.html">

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

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

21+
import org.apache.http.client.methods.HttpDelete;
2122
import org.apache.http.client.methods.HttpGet;
2223
import org.apache.http.client.methods.HttpPost;
2324
import org.apache.http.client.methods.HttpPut;
2425
import org.elasticsearch.client.rollup.GetRollupCapsRequest;
26+
import org.elasticsearch.client.rollup.DeleteRollupJobRequest;
2527
import org.elasticsearch.client.rollup.GetRollupJobRequest;
2628
import org.elasticsearch.client.rollup.PutRollupJobRequest;
2729
import org.elasticsearch.client.rollup.StartRollupJobRequest;
@@ -74,4 +76,16 @@ static Request getRollupCaps(final GetRollupCapsRequest getRollupCapsRequest) th
7476
request.setEntity(createEntity(getRollupCapsRequest, REQUEST_BODY_CONTENT_TYPE));
7577
return request;
7678
}
79+
80+
static Request deleteJob(final DeleteRollupJobRequest deleteRollupJobRequest) throws IOException {
81+
String endpoint = new RequestConverters.EndpointBuilder()
82+
.addPathPartAsIs("_xpack")
83+
.addPathPartAsIs("rollup")
84+
.addPathPartAsIs("job")
85+
.addPathPart(deleteRollupJobRequest.getId())
86+
.build();
87+
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
88+
request.setEntity(createEntity(deleteRollupJobRequest, REQUEST_BODY_CONTENT_TYPE));
89+
return request;
90+
}
7791
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
3333

3434
public abstract class AcknowledgedResponse implements ToXContentObject {
35+
private final boolean acknowledged;
3536

3637
protected static final String PARSE_FIELD_NAME = "acknowledged";
37-
private final boolean acknowledged;
3838

3939
public AcknowledgedResponse(final boolean acknowledged) {
4040
this.acknowledged = acknowledged;
@@ -83,4 +83,5 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par
8383
protected String getFieldName() {
8484
return PARSE_FIELD_NAME;
8585
}
86+
8687
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.common.ParseField;
23+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
24+
import org.elasticsearch.common.xcontent.ToXContentObject;
25+
import org.elasticsearch.common.xcontent.XContentBuilder;
26+
import org.elasticsearch.common.xcontent.XContentParser;
27+
28+
import java.io.IOException;
29+
import java.util.Objects;
30+
31+
32+
public class DeleteRollupJobRequest implements Validatable, ToXContentObject {
33+
34+
private static final ParseField ID_FIELD = new ParseField("id");
35+
private final String id;
36+
37+
38+
public DeleteRollupJobRequest(String id) {
39+
this.id = Objects.requireNonNull(id, "id parameter must not be null");
40+
}
41+
42+
public String getId() {
43+
return id;
44+
}
45+
46+
private static final ConstructingObjectParser<DeleteRollupJobRequest, Void> PARSER =
47+
new ConstructingObjectParser<>("request", a -> {
48+
return new DeleteRollupJobRequest((String) a[0]);
49+
});
50+
51+
static {
52+
PARSER.declareString(ConstructingObjectParser.constructorArg(), ID_FIELD);
53+
}
54+
55+
public static DeleteRollupJobRequest fromXContent(XContentParser parser) {
56+
return PARSER.apply(parser, null);
57+
}
58+
59+
@Override
60+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
61+
builder.startObject();
62+
builder.field(ID_FIELD.getPreferredName(), this.id);
63+
builder.endObject();
64+
return builder;
65+
}
66+
67+
@Override
68+
public boolean equals(Object o) {
69+
if (this == o) return true;
70+
if (o == null || getClass() != o.getClass()) return false;
71+
DeleteRollupJobRequest that = (DeleteRollupJobRequest) o;
72+
return Objects.equals(id, that.id);
73+
}
74+
75+
@Override
76+
public int hashCode() {
77+
return Objects.hash(id);
78+
}
79+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.common.xcontent.ConstructingObjectParser;
23+
import org.elasticsearch.common.xcontent.XContentParser;
24+
25+
import java.io.IOException;
26+
27+
public class DeleteRollupJobResponse extends AcknowledgedResponse {
28+
29+
public DeleteRollupJobResponse(boolean acknowledged) {
30+
super(acknowledged);
31+
}
32+
33+
private static final ConstructingObjectParser<DeleteRollupJobResponse, Void> PARSER = AcknowledgedResponse
34+
.generateParser("delete_rollup_job_response", DeleteRollupJobResponse::new, AcknowledgedResponse.PARSE_FIELD_NAME);
35+
36+
public static DeleteRollupJobResponse fromXContent(final XContentParser parser) throws IOException {
37+
return PARSER.parse(parser, null);
38+
}
39+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@
2323

2424
import java.io.IOException;
2525

26-
2726
public class PutRollupJobResponse extends AcknowledgedResponse {
2827

28+
2929
public PutRollupJobResponse(boolean acknowledged) {
3030
super(acknowledged);
3131
}
3232

3333
private static final ConstructingObjectParser<PutRollupJobResponse, Void> PARSER = AcknowledgedResponse
34-
.generateParser("delete_rollup_job_response", PutRollupJobResponse::new, AcknowledgedResponse.PARSE_FIELD_NAME);
34+
.generateParser("delete_rollup_job_response", PutRollupJobResponse::new, AcknowledgedResponse.PARSE_FIELD_NAME);
3535

3636
public static PutRollupJobResponse fromXContent(final XContentParser parser) throws IOException {
3737
return PARSER.parse(parser, null);
3838
}
39+
3940
}

client/rest-high-level/src/test/java/org/elasticsearch/client/RollupIT.java

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.elasticsearch.client;
2020

2121
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
22+
import org.elasticsearch.ElasticsearchStatusException;
2223
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
2324
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
2425
import org.elasticsearch.action.bulk.BulkItemResponse;
@@ -34,6 +35,8 @@
3435
import org.elasticsearch.client.rollup.GetRollupJobResponse;
3536
import org.elasticsearch.client.rollup.GetRollupJobResponse.IndexerState;
3637
import org.elasticsearch.client.rollup.GetRollupJobResponse.JobWrapper;
38+
import org.elasticsearch.client.rollup.DeleteRollupJobRequest;
39+
import org.elasticsearch.client.rollup.DeleteRollupJobResponse;
3740
import org.elasticsearch.client.rollup.PutRollupJobRequest;
3841
import org.elasticsearch.client.rollup.PutRollupJobResponse;
3942
import org.elasticsearch.client.rollup.RollableIndexCaps;
@@ -53,6 +56,7 @@
5356
import org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder;
5457
import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
5558
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregationBuilder;
59+
import org.junit.Before;
5660

5761
import java.util.Arrays;
5862
import java.util.Collections;
@@ -69,18 +73,35 @@
6973
import static org.hamcrest.Matchers.greaterThan;
7074
import static org.hamcrest.Matchers.hasKey;
7175
import static org.hamcrest.Matchers.hasSize;
76+
import static org.hamcrest.Matchers.is;
7277
import static org.hamcrest.Matchers.lessThan;
7378

7479
public class RollupIT extends ESRestHighLevelClientTestCase {
7580

81+
double sum = 0.0d;
82+
int max = Integer.MIN_VALUE;
83+
int min = Integer.MAX_VALUE;
7684
private static final List<String> SUPPORTED_METRICS = Arrays.asList(MaxAggregationBuilder.NAME, MinAggregationBuilder.NAME,
77-
SumAggregationBuilder.NAME, AvgAggregationBuilder.NAME, ValueCountAggregationBuilder.NAME);
78-
79-
public void testPutStartAndGetRollupJob() throws Exception {
80-
double sum = 0.0d;
81-
int max = Integer.MIN_VALUE;
82-
int min = Integer.MAX_VALUE;
85+
SumAggregationBuilder.NAME, AvgAggregationBuilder.NAME, ValueCountAggregationBuilder.NAME);
86+
87+
private String id;
88+
private String indexPattern;
89+
private String rollupIndex;
90+
private String cron;
91+
private int pageSize;
92+
private int numDocs;
93+
94+
@Before
95+
public void init() throws Exception {
96+
id = randomAlphaOfLength(10);
97+
indexPattern = randomFrom("docs", "d*", "doc*");
98+
rollupIndex = randomFrom("rollup", "test");
99+
cron = "*/1 * * * * ?";
100+
numDocs = indexDocs();
101+
pageSize = randomIntBetween(numDocs, numDocs * 10);
102+
}
83103

104+
public int indexDocs() throws Exception {
84105
final BulkRequest bulkRequest = new BulkRequest();
85106
bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
86107
for (int minute = 0; minute < 60; minute++) {
@@ -120,12 +141,33 @@ public void testPutStartAndGetRollupJob() throws Exception {
120141

121142
RefreshResponse refreshResponse = highLevelClient().indices().refresh(new RefreshRequest("docs"), RequestOptions.DEFAULT);
122143
assertEquals(0, refreshResponse.getFailedShards());
144+
return numDocs;
145+
}
123146

124-
final String id = randomAlphaOfLength(10);
125-
final String indexPattern = randomFrom("docs", "d*", "doc*");
126-
final String rollupIndex = randomFrom("rollup", "test");
127-
final String cron = "*/1 * * * * ?";
128-
final int pageSize = randomIntBetween(numDocs, numDocs * 10);
147+
148+
public void testDeleteRollupJob() throws Exception {
149+
final GroupConfig groups = new GroupConfig(new DateHistogramGroupConfig("date", DateHistogramInterval.DAY));
150+
final List<MetricConfig> metrics = Collections.singletonList(new MetricConfig("value", SUPPORTED_METRICS));
151+
final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(30, 600));
152+
PutRollupJobRequest putRollupJobRequest =
153+
new PutRollupJobRequest(new RollupJobConfig(id, indexPattern, rollupIndex, cron, pageSize, groups, metrics, timeout));
154+
final RollupClient rollupClient = highLevelClient().rollup();
155+
PutRollupJobResponse response = execute(putRollupJobRequest, rollupClient::putRollupJob, rollupClient::putRollupJobAsync);
156+
DeleteRollupJobRequest deleteRollupJobRequest = new DeleteRollupJobRequest(id);
157+
DeleteRollupJobResponse deleteRollupJobResponse = highLevelClient().rollup()
158+
.deleteRollupJob(deleteRollupJobRequest, RequestOptions.DEFAULT);
159+
assertTrue(deleteRollupJobResponse.isAcknowledged());
160+
}
161+
162+
public void testDeleteMissingRollupJob() {
163+
DeleteRollupJobRequest deleteRollupJobRequest = new DeleteRollupJobRequest(randomAlphaOfLength(10));
164+
ElasticsearchStatusException responseException = expectThrows(ElasticsearchStatusException.class,() -> highLevelClient().rollup()
165+
.deleteRollupJob(deleteRollupJobRequest, RequestOptions.DEFAULT));
166+
assertThat(responseException.status().getStatus(), is(404));
167+
}
168+
169+
@SuppressWarnings("unchecked")
170+
public void testPutAndGetRollupJob() throws Exception {
129171
// TODO expand this to also test with histogram and terms?
130172
final GroupConfig groups = new GroupConfig(new DateHistogramGroupConfig("date", DateHistogramInterval.DAY));
131173
final List<MetricConfig> metrics = Collections.singletonList(new MetricConfig("value", SUPPORTED_METRICS));
@@ -142,9 +184,6 @@ public void testPutStartAndGetRollupJob() throws Exception {
142184
StartRollupJobResponse startResponse = execute(startRequest, rollupClient::startRollupJob, rollupClient::startRollupJobAsync);
143185
assertTrue(startResponse.isAcknowledged());
144186

145-
int finalMin = min;
146-
int finalMax = max;
147-
double finalSum = sum;
148187
assertBusy(() -> {
149188
SearchResponse searchResponse = highLevelClient().search(new SearchRequest(rollupIndex), RequestOptions.DEFAULT);
150189
assertEquals(0, searchResponse.getFailedShards());
@@ -162,13 +201,13 @@ public void testPutStartAndGetRollupJob() throws Exception {
162201
for (String name : metric.getMetrics()) {
163202
Number value = (Number) source.get(metric.getField() + "." + name + ".value");
164203
if ("min".equals(name)) {
165-
assertEquals(finalMin, value.intValue());
204+
assertEquals(min, value.intValue());
166205
} else if ("max".equals(name)) {
167-
assertEquals(finalMax, value.intValue());
206+
assertEquals(max, value.intValue());
168207
} else if ("sum".equals(name)) {
169-
assertEquals(finalSum, value.doubleValue(), 0.0d);
208+
assertEquals(sum, value.doubleValue(), 0.0d);
170209
} else if ("avg".equals(name)) {
171-
assertEquals(finalSum, value.doubleValue(), 0.0d);
210+
assertEquals(sum, value.doubleValue(), 0.0d);
172211
Number avgCount = (Number) source.get(metric.getField() + "." + name + "._count");
173212
assertEquals(numDocs, avgCount.intValue());
174213
} else if ("value_count".equals(name)) {

0 commit comments

Comments
 (0)