Skip to content

Commit 6417044

Browse files
Deprecate types in rollover index API (#38039)
Relates to #35190
1 parent 578fd14 commit 6417044

File tree

23 files changed

+758
-45
lines changed

23 files changed

+758
-45
lines changed

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
4242
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
4343
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
44-
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
45-
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
4644
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
4745
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
4846
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
@@ -64,6 +62,8 @@
6462
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
6563
import org.elasticsearch.client.indices.PutMappingRequest;
6664
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
65+
import org.elasticsearch.client.indices.rollover.RolloverRequest;
66+
import org.elasticsearch.client.indices.rollover.RolloverResponse;
6767
import org.elasticsearch.rest.RestStatus;
6868

6969
import java.io.IOException;
@@ -853,6 +853,46 @@ public void rolloverAsync(RolloverRequest rolloverRequest, RequestOptions option
853853
RolloverResponse::fromXContent, listener, emptySet());
854854
}
855855

856+
857+
/**
858+
* Rolls over an index using the Rollover Index API.
859+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
860+
* Rollover Index API on elastic.co</a>
861+
* @param rolloverRequest the request
862+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
863+
* @return the response
864+
* @throws IOException in case there is a problem sending the request or parsing back the response
865+
*
866+
* @deprecated This method uses deprecated request and response objects.
867+
* The method {@link #rollover(RolloverRequest, RequestOptions)} should be used instead, which accepts a new request object.
868+
*/
869+
@Deprecated
870+
public org.elasticsearch.action.admin.indices.rollover.RolloverResponse rollover(
871+
org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest,
872+
RequestOptions options) throws IOException {
873+
return restHighLevelClient.performRequestAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover, options,
874+
org.elasticsearch.action.admin.indices.rollover.RolloverResponse::fromXContent, emptySet());
875+
}
876+
877+
/**
878+
* Asynchronously rolls over an index using the Rollover Index API.
879+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
880+
* Rollover Index API on elastic.co</a>
881+
* @param rolloverRequest the request
882+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
883+
* @param listener the listener to be notified upon request completion
884+
*
885+
* @deprecated This method uses deprecated request and response objects.
886+
* The method {@link #rolloverAsync(RolloverRequest, RequestOptions, ActionListener)} should be used instead, which
887+
* accepts a new request object.
888+
*/
889+
@Deprecated
890+
public void rolloverAsync(org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest,
891+
RequestOptions options, ActionListener<org.elasticsearch.action.admin.indices.rollover.RolloverResponse> listener) {
892+
restHighLevelClient.performRequestAsyncAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover, options,
893+
org.elasticsearch.action.admin.indices.rollover.RolloverResponse::fromXContent, listener, emptySet());
894+
}
895+
856896
/**
857897
* Gets one or more aliases using the Get Index Aliases API.
858898
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html"> Indices Aliases API on

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

Lines changed: 21 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;
@@ -52,6 +51,7 @@
5251
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
5352
import org.elasticsearch.client.indices.PutMappingRequest;
5453
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
54+
import org.elasticsearch.client.indices.rollover.RolloverRequest;
5555
import org.elasticsearch.common.Strings;
5656

5757
import java.io.IOException;
@@ -339,7 +339,7 @@ private static Request resize(ResizeRequest resizeRequest) throws IOException {
339339

340340
static Request rollover(RolloverRequest rolloverRequest) throws IOException {
341341
String endpoint = new RequestConverters.EndpointBuilder().addPathPart(rolloverRequest.getAlias()).addPathPartAsIs("_rollover")
342-
.addPathPart(rolloverRequest.getNewIndexName()).build();
342+
.addPathPart(rolloverRequest.getNewIndexName()).build();
343343
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
344344

345345
RequestConverters.Params params = new RequestConverters.Params(request);
@@ -354,6 +354,25 @@ static Request rollover(RolloverRequest rolloverRequest) throws IOException {
354354
return request;
355355
}
356356

357+
@Deprecated
358+
static Request rollover(org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest) throws IOException {
359+
String endpoint = new RequestConverters.EndpointBuilder().addPathPart(rolloverRequest.getAlias()).addPathPartAsIs("_rollover")
360+
.addPathPart(rolloverRequest.getNewIndexName()).build();
361+
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
362+
363+
RequestConverters.Params params = new RequestConverters.Params(request);
364+
params.withTimeout(rolloverRequest.timeout());
365+
params.withMasterTimeout(rolloverRequest.masterNodeTimeout());
366+
params.withWaitForActiveShards(rolloverRequest.getCreateIndexRequest().waitForActiveShards());
367+
if (rolloverRequest.isDryRun()) {
368+
params.putParam("dry_run", Boolean.TRUE.toString());
369+
}
370+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, "true");
371+
request.setEntity(RequestConverters.createEntity(rolloverRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
372+
373+
return request;
374+
}
375+
357376
static Request getSettings(GetSettingsRequest getSettingsRequest) {
358377
String[] indices = getSettingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getSettingsRequest.indices();
359378
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,10 +36,18 @@ 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

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

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)