Skip to content

Commit 2e7b7a8

Browse files
Deprecate types in rollover index API (#38389)
Backport for #38039 Relates to #35190
1 parent d0fb8dc commit 2e7b7a8

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
@@ -42,8 +42,6 @@
4242
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
4343
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
4444
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
45-
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
46-
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
4745
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
4846
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
4947
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
@@ -65,6 +63,8 @@
6563
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
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;
@@ -1234,17 +1234,54 @@ public RolloverResponse rollover(RolloverRequest rolloverRequest, RequestOptions
12341234
RolloverResponse::fromXContent, emptySet());
12351235
}
12361236

1237+
/**
1238+
* Asynchronously rolls over an index using the Rollover Index API.
1239+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
1240+
* Rollover Index API on elastic.co</a>
1241+
* @param rolloverRequest the request
1242+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1243+
* @param listener the listener to be notified upon request completion
1244+
*/
1245+
public void rolloverAsync(RolloverRequest rolloverRequest, RequestOptions options, ActionListener<RolloverResponse> listener) {
1246+
restHighLevelClient.performRequestAsyncAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover, options,
1247+
RolloverResponse::fromXContent, listener, emptySet());
1248+
}
1249+
1250+
/**
1251+
* Rolls over an index using the Rollover Index API.
1252+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
1253+
* Rollover Index API on elastic.co</a>
1254+
* @param rolloverRequest the request
1255+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1256+
* @return the response
1257+
* @throws IOException in case there is a problem sending the request or parsing back the response
1258+
*
1259+
* @deprecated This method uses deprecated request and response objects.
1260+
* The method {@link #rollover(RolloverRequest, RequestOptions)} should be used instead, which accepts a new request object.
1261+
*/
1262+
@Deprecated
1263+
public org.elasticsearch.action.admin.indices.rollover.RolloverResponse rollover(
1264+
org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest,
1265+
RequestOptions options) throws IOException {
1266+
return restHighLevelClient.performRequestAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover, options,
1267+
org.elasticsearch.action.admin.indices.rollover.RolloverResponse::fromXContent, emptySet());
1268+
}
1269+
12371270
/**
12381271
* Rolls over an index using the Rollover Index API.
12391272
* <p>
12401273
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
12411274
* Rollover Index API on elastic.co</a>
1242-
* @deprecated Prefer {@link #rollover(RolloverRequest, RequestOptions)}
1275+
*
1276+
* @deprecated This method uses deprecated request and response objects.
1277+
* The method {@link #rollover(RolloverRequest, RequestOptions)} should be used instead, which accepts a new request object.
12431278
*/
12441279
@Deprecated
1245-
public RolloverResponse rollover(RolloverRequest rolloverRequest, Header... headers) throws IOException {
1280+
public org.elasticsearch.action.admin.indices.rollover.RolloverResponse rollover(
1281+
org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest,
1282+
Header... headers) throws IOException {
12461283
return restHighLevelClient.performRequestAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover,
1247-
RolloverResponse::fromXContent, emptySet(), headers);
1284+
org.elasticsearch.action.admin.indices.rollover.RolloverResponse::fromXContent, emptySet(), headers);
12481285
}
12491286

12501287
/**
@@ -1254,23 +1291,33 @@ public RolloverResponse rollover(RolloverRequest rolloverRequest, Header... head
12541291
* @param rolloverRequest the request
12551292
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
12561293
* @param listener the listener to be notified upon request completion
1294+
*
1295+
* @deprecated This method uses deprecated request and response objects.
1296+
* The method {@link #rolloverAsync(RolloverRequest, RequestOptions, ActionListener)} should be used instead, which
1297+
* accepts a new request object.
12571298
*/
1258-
public void rolloverAsync(RolloverRequest rolloverRequest, RequestOptions options, ActionListener<RolloverResponse> listener) {
1299+
@Deprecated
1300+
public void rolloverAsync(org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest,
1301+
RequestOptions options, ActionListener<org.elasticsearch.action.admin.indices.rollover.RolloverResponse> listener) {
12591302
restHighLevelClient.performRequestAsyncAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover, options,
1260-
RolloverResponse::fromXContent, listener, emptySet());
1303+
org.elasticsearch.action.admin.indices.rollover.RolloverResponse::fromXContent, listener, emptySet());
12611304
}
12621305

12631306
/**
12641307
* Asynchronously rolls over an index using the Rollover Index API.
12651308
* <p>
12661309
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
12671310
* Rollover Index API on elastic.co</a>
1268-
* @deprecated Prefer {@link #rolloverAsync(RolloverRequest, RequestOptions, ActionListener)}
1311+
*
1312+
* @deprecated This method uses deprecated request and response objects.
1313+
* The method {@link #rolloverAsync(RolloverRequest, RequestOptions, ActionListener)} should be used instead, which
1314+
* accepts a new request object.
12691315
*/
12701316
@Deprecated
1271-
public void rolloverAsync(RolloverRequest rolloverRequest, ActionListener<RolloverResponse> listener, Header... headers) {
1317+
public void rolloverAsync(org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest,
1318+
ActionListener<org.elasticsearch.action.admin.indices.rollover.RolloverResponse> listener, Header... headers) {
12721319
restHighLevelClient.performRequestAsyncAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover,
1273-
RolloverResponse::fromXContent, listener, emptySet(), headers);
1320+
org.elasticsearch.action.admin.indices.rollover.RolloverResponse::fromXContent, listener, emptySet(), headers);
12741321
}
12751322

12761323
/**

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
@@ -37,7 +37,6 @@
3737
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
3838
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
3939
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
40-
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
4140
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
4241
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
4342
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
@@ -53,6 +52,7 @@
5352
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
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
import org.elasticsearch.rest.BaseRestHandler;
5858

@@ -345,7 +345,7 @@ private static Request resize(ResizeRequest resizeRequest) throws IOException {
345345

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

351351
RequestConverters.Params params = new RequestConverters.Params(request);
@@ -355,11 +355,31 @@ static Request rollover(RolloverRequest rolloverRequest) throws IOException {
355355
if (rolloverRequest.isDryRun()) {
356356
params.putParam("dry_run", Boolean.TRUE.toString());
357357
}
358+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, "false");
358359

359360
request.setEntity(RequestConverters.createEntity(rolloverRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
360361
return request;
361362
}
362363

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