Skip to content

Commit aa9816c

Browse files
Deprecate types in rollover index API (#38389) (#38458)
Backport for #38039 Relates to #35190
1 parent 1f2cd9f commit aa9816c

File tree

23 files changed

+784
-56
lines changed

23 files changed

+784
-56
lines changed

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

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
3939
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
4040
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
41-
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
42-
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
4341
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
4442
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
4543
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
@@ -65,6 +63,8 @@
6563
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
6664
import org.elasticsearch.client.indices.PutMappingRequest;
6765
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
66+
import org.elasticsearch.client.indices.rollover.RolloverRequest;
67+
import org.elasticsearch.client.indices.rollover.RolloverResponse;
6868
import org.elasticsearch.rest.RestStatus;
6969

7070
import java.io.IOException;
@@ -1317,17 +1317,54 @@ public RolloverResponse rollover(RolloverRequest rolloverRequest, RequestOptions
13171317
RolloverResponse::fromXContent, emptySet());
13181318
}
13191319

1320+
/**
1321+
* Asynchronously rolls over an index using the Rollover Index API.
1322+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
1323+
* Rollover Index API on elastic.co</a>
1324+
* @param rolloverRequest the request
1325+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1326+
* @param listener the listener to be notified upon request completion
1327+
*/
1328+
public void rolloverAsync(RolloverRequest rolloverRequest, RequestOptions options, ActionListener<RolloverResponse> listener) {
1329+
restHighLevelClient.performRequestAsyncAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover, options,
1330+
RolloverResponse::fromXContent, listener, emptySet());
1331+
}
1332+
1333+
/**
1334+
* Rolls over an index using the Rollover Index API.
1335+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
1336+
* Rollover Index API on elastic.co</a>
1337+
* @param rolloverRequest the request
1338+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1339+
* @return the response
1340+
* @throws IOException in case there is a problem sending the request or parsing back the response
1341+
*
1342+
* @deprecated This method uses deprecated request and response objects.
1343+
* The method {@link #rollover(RolloverRequest, RequestOptions)} should be used instead, which accepts a new request object.
1344+
*/
1345+
@Deprecated
1346+
public org.elasticsearch.action.admin.indices.rollover.RolloverResponse rollover(
1347+
org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest,
1348+
RequestOptions options) throws IOException {
1349+
return restHighLevelClient.performRequestAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover, options,
1350+
org.elasticsearch.action.admin.indices.rollover.RolloverResponse::fromXContent, emptySet());
1351+
}
1352+
13201353
/**
13211354
* Rolls over an index using the Rollover Index API.
13221355
* <p>
13231356
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
13241357
* Rollover Index API on elastic.co</a>
1325-
* @deprecated Prefer {@link #rollover(RolloverRequest, RequestOptions)}
1358+
*
1359+
* @deprecated This method uses deprecated request and response objects.
1360+
* The method {@link #rollover(RolloverRequest, RequestOptions)} should be used instead, which accepts a new request object.
13261361
*/
13271362
@Deprecated
1328-
public RolloverResponse rollover(RolloverRequest rolloverRequest, Header... headers) throws IOException {
1363+
public org.elasticsearch.action.admin.indices.rollover.RolloverResponse rollover(
1364+
org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest,
1365+
Header... headers) throws IOException {
13291366
return restHighLevelClient.performRequestAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover,
1330-
RolloverResponse::fromXContent, emptySet(), headers);
1367+
org.elasticsearch.action.admin.indices.rollover.RolloverResponse::fromXContent, emptySet(), headers);
13311368
}
13321369

13331370
/**
@@ -1337,23 +1374,33 @@ public RolloverResponse rollover(RolloverRequest rolloverRequest, Header... head
13371374
* @param rolloverRequest the request
13381375
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
13391376
* @param listener the listener to be notified upon request completion
1377+
*
1378+
* @deprecated This method uses deprecated request and response objects.
1379+
* The method {@link #rolloverAsync(RolloverRequest, RequestOptions, ActionListener)} should be used instead, which
1380+
* accepts a new request object.
13401381
*/
1341-
public void rolloverAsync(RolloverRequest rolloverRequest, RequestOptions options, ActionListener<RolloverResponse> listener) {
1382+
@Deprecated
1383+
public void rolloverAsync(org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest,
1384+
RequestOptions options, ActionListener<org.elasticsearch.action.admin.indices.rollover.RolloverResponse> listener) {
13421385
restHighLevelClient.performRequestAsyncAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover, options,
1343-
RolloverResponse::fromXContent, listener, emptySet());
1386+
org.elasticsearch.action.admin.indices.rollover.RolloverResponse::fromXContent, listener, emptySet());
13441387
}
13451388

13461389
/**
13471390
* Asynchronously rolls over an index using the Rollover Index API.
13481391
* <p>
13491392
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
13501393
* Rollover Index API on elastic.co</a>
1351-
* @deprecated Prefer {@link #rolloverAsync(RolloverRequest, RequestOptions, ActionListener)}
1394+
*
1395+
* @deprecated This method uses deprecated request and response objects.
1396+
* The method {@link #rolloverAsync(RolloverRequest, RequestOptions, ActionListener)} should be used instead, which
1397+
* accepts a new request object.
13521398
*/
13531399
@Deprecated
1354-
public void rolloverAsync(RolloverRequest rolloverRequest, ActionListener<RolloverResponse> listener, Header... headers) {
1400+
public void rolloverAsync(org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest,
1401+
ActionListener<org.elasticsearch.action.admin.indices.rollover.RolloverResponse> listener, Header... headers) {
13551402
restHighLevelClient.performRequestAsyncAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover,
1356-
RolloverResponse::fromXContent, listener, emptySet(), headers);
1403+
org.elasticsearch.action.admin.indices.rollover.RolloverResponse::fromXContent, listener, emptySet(), headers);
13571404
}
13581405

13591406
/**

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
3636
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
3737
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
38-
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
3938
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
4039
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
4140
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
@@ -53,6 +52,7 @@
5352
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
5453
import org.elasticsearch.client.indices.PutMappingRequest;
5554
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
55+
import org.elasticsearch.client.indices.rollover.RolloverRequest;
5656
import org.elasticsearch.common.Strings;
5757

5858
import java.io.IOException;
@@ -348,7 +348,7 @@ private static Request resize(ResizeRequest resizeRequest) throws IOException {
348348

349349
static Request rollover(RolloverRequest rolloverRequest) throws IOException {
350350
String endpoint = new RequestConverters.EndpointBuilder().addPathPart(rolloverRequest.getAlias()).addPathPartAsIs("_rollover")
351-
.addPathPart(rolloverRequest.getNewIndexName()).build();
351+
.addPathPart(rolloverRequest.getNewIndexName()).build();
352352
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
353353

354354
RequestConverters.Params params = new RequestConverters.Params(request);
@@ -358,11 +358,31 @@ static Request rollover(RolloverRequest rolloverRequest) throws IOException {
358358
if (rolloverRequest.isDryRun()) {
359359
params.putParam("dry_run", Boolean.TRUE.toString());
360360
}
361+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, "false");
361362

362363
request.setEntity(RequestConverters.createEntity(rolloverRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
363364
return request;
364365
}
365366

367+
@Deprecated
368+
static Request rollover(org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest) throws IOException {
369+
String endpoint = new RequestConverters.EndpointBuilder().addPathPart(rolloverRequest.getAlias()).addPathPartAsIs("_rollover")
370+
.addPathPart(rolloverRequest.getNewIndexName()).build();
371+
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
372+
373+
RequestConverters.Params params = new RequestConverters.Params(request);
374+
params.withTimeout(rolloverRequest.timeout());
375+
params.withMasterTimeout(rolloverRequest.masterNodeTimeout());
376+
params.withWaitForActiveShards(rolloverRequest.getCreateIndexRequest().waitForActiveShards());
377+
if (rolloverRequest.isDryRun()) {
378+
params.putParam("dry_run", Boolean.TRUE.toString());
379+
}
380+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, "true");
381+
request.setEntity(RequestConverters.createEntity(rolloverRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
382+
383+
return request;
384+
}
385+
366386
static Request getSettings(GetSettingsRequest getSettingsRequest) {
367387
String[] indices = getSettingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getSettingsRequest.indices();
368388
String[] names = getSettingsRequest.names() == null ? Strings.EMPTY_ARRAY : getSettingsRequest.names();

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,19 @@ public abstract class TimedRequest implements Validatable {
3636
private TimeValue timeout = DEFAULT_ACK_TIMEOUT;
3737
private TimeValue masterTimeout = DEFAULT_MASTER_NODE_TIMEOUT;
3838

39+
/**
40+
* Sets the timeout to wait for the all the nodes to acknowledge
41+
* @param timeout timeout as a {@link TimeValue}
42+
*/
3943
public void setTimeout(TimeValue timeout) {
4044
this.timeout = timeout;
4145

4246
}
4347

48+
/**
49+
* Sets the timeout to connect to the master node
50+
* @param masterTimeout timeout as a {@link TimeValue}
51+
*/
4452
public void setMasterTimeout(TimeValue masterTimeout) {
4553
this.masterTimeout = masterTimeout;
4654
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,14 @@ public CreateIndexRequest waitForActiveShards(ActiveShardCount waitForActiveShar
338338
return this;
339339
}
340340

341-
@Override
342341
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
343342
builder.startObject();
343+
innerToXContent(builder, params);
344+
builder.endObject();
345+
return builder;
346+
}
344347

348+
public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
345349
builder.startObject(SETTINGS.getPreferredName());
346350
settings.toXContent(builder, params);
347351
builder.endObject();
@@ -356,8 +360,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
356360
for (Alias alias : aliases) {
357361
alias.toXContent(builder, params);
358362
}
359-
builder.endObject();
360-
361363
builder.endObject();
362364
return builder;
363365
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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.indices.rollover;
20+
21+
import org.elasticsearch.action.admin.indices.rollover.Condition;
22+
import org.elasticsearch.action.admin.indices.rollover.MaxAgeCondition;
23+
import org.elasticsearch.action.admin.indices.rollover.MaxDocsCondition;
24+
import org.elasticsearch.action.admin.indices.rollover.MaxSizeCondition;
25+
import org.elasticsearch.client.TimedRequest;
26+
import org.elasticsearch.client.indices.CreateIndexRequest;
27+
import org.elasticsearch.common.unit.ByteSizeValue;
28+
import org.elasticsearch.common.unit.TimeValue;
29+
import org.elasticsearch.common.xcontent.ToXContentObject;
30+
import org.elasticsearch.common.xcontent.XContentBuilder;
31+
32+
import java.io.IOException;
33+
import java.util.HashMap;
34+
import java.util.Map;
35+
36+
/**
37+
* Request class to swap index under an alias upon satisfying conditions
38+
*/
39+
public class RolloverRequest extends TimedRequest implements ToXContentObject {
40+
41+
private final String alias;
42+
private final String newIndexName;
43+
private boolean dryRun;
44+
private final Map<String, Condition<?>> conditions = new HashMap<>(2);
45+
//the index name "_na_" is never read back, what matters are settings, mappings and aliases
46+
private final CreateIndexRequest createIndexRequest = new CreateIndexRequest("_na_");
47+
48+
public RolloverRequest(String alias, String newIndexName) {
49+
if (alias == null) {
50+
throw new IllegalArgumentException("The index alias cannot be null!");
51+
}
52+
this.alias = alias;
53+
this.newIndexName = newIndexName;
54+
}
55+
56+
/**
57+
* Returns the alias of the rollover operation
58+
*/
59+
public String getAlias() {
60+
return alias;
61+
}
62+
63+
/**
64+
* Returns the new index name for the rollover
65+
*/
66+
public String getNewIndexName() {
67+
return newIndexName;
68+
}
69+
70+
71+
/**
72+
* Sets if the rollover should not be executed when conditions are met
73+
*/
74+
public RolloverRequest dryRun(boolean dryRun) {
75+
this.dryRun = dryRun;
76+
return this;
77+
}
78+
/**
79+
* Returns if the rollover should not be executed when conditions are met
80+
*/
81+
public boolean isDryRun() {
82+
return dryRun;
83+
}
84+
85+
/**
86+
* Adds condition to check if the index is at least <code>age</code> old
87+
*/
88+
public RolloverRequest addMaxIndexAgeCondition(TimeValue age) {
89+
MaxAgeCondition maxAgeCondition = new MaxAgeCondition(age);
90+
if (this.conditions.containsKey(maxAgeCondition.name())) {
91+
throw new IllegalArgumentException(maxAgeCondition.name() + " condition is already set");
92+
}
93+
this.conditions.put(maxAgeCondition.name(), maxAgeCondition);
94+
return this;
95+
}
96+
97+
/**
98+
* Adds condition to check if the index has at least <code>numDocs</code>
99+
*/
100+
public RolloverRequest addMaxIndexDocsCondition(long numDocs) {
101+
MaxDocsCondition maxDocsCondition = new MaxDocsCondition(numDocs);
102+
if (this.conditions.containsKey(maxDocsCondition.name())) {
103+
throw new IllegalArgumentException(maxDocsCondition.name() + " condition is already set");
104+
}
105+
this.conditions.put(maxDocsCondition.name(), maxDocsCondition);
106+
return this;
107+
}
108+
/**
109+
* Adds a size-based condition to check if the index size is at least <code>size</code>.
110+
*/
111+
public RolloverRequest addMaxIndexSizeCondition(ByteSizeValue size) {
112+
MaxSizeCondition maxSizeCondition = new MaxSizeCondition(size);
113+
if (this.conditions.containsKey(maxSizeCondition.name())) {
114+
throw new IllegalArgumentException(maxSizeCondition + " condition is already set");
115+
}
116+
this.conditions.put(maxSizeCondition.name(), maxSizeCondition);
117+
return this;
118+
}
119+
/**
120+
* Returns all set conditions
121+
*/
122+
public Map<String, Condition<?>> getConditions() {
123+
return conditions;
124+
}
125+
126+
/**
127+
* Returns the inner {@link CreateIndexRequest}. Allows to configure mappings, settings and aliases for the new index.
128+
*/
129+
public CreateIndexRequest getCreateIndexRequest() {
130+
return createIndexRequest;
131+
}
132+
133+
@Override
134+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
135+
builder.startObject();
136+
createIndexRequest.innerToXContent(builder, params);
137+
138+
builder.startObject("conditions");
139+
for (Condition<?> condition : conditions.values()) {
140+
condition.toXContent(builder, params);
141+
}
142+
builder.endObject();
143+
144+
builder.endObject();
145+
return builder;
146+
}
147+
148+
}

0 commit comments

Comments
 (0)