Skip to content

Commit 8bbb3c9

Browse files
authored
REST high-level client: add support for Rollover Index API (#28698)
Relates to #27205
1 parent 94594f1 commit 8bbb3c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1026
-370
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
3636
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
3737
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
38+
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
39+
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
3840
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
3941
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
4042

@@ -272,7 +274,7 @@ public void shrinkAsync(ResizeRequest resizeRequest, ActionListener<ResizeRespon
272274
* Splits an index using the Split Index API
273275
* <p>
274276
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-split-index.html">
275-
* Shrink Index API on elastic.co</a>
277+
* Split Index API on elastic.co</a>
276278
*/
277279
public ResizeResponse split(ResizeRequest resizeRequest, Header... headers) throws IOException {
278280
return restHighLevelClient.performRequestAndParseEntity(resizeRequest, Request::split, ResizeResponse::fromXContent,
@@ -289,4 +291,26 @@ public void splitAsync(ResizeRequest resizeRequest, ActionListener<ResizeRespons
289291
restHighLevelClient.performRequestAsyncAndParseEntity(resizeRequest, Request::split, ResizeResponse::fromXContent,
290292
listener, emptySet(), headers);
291293
}
294+
295+
/**
296+
* Rolls over an index using the Rollover Index API
297+
* <p>
298+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
299+
* Rollover Index API on elastic.co</a>
300+
*/
301+
public RolloverResponse rollover(RolloverRequest rolloverRequest, Header... headers) throws IOException {
302+
return restHighLevelClient.performRequestAndParseEntity(rolloverRequest, Request::rollover, RolloverResponse::fromXContent,
303+
emptySet(), headers);
304+
}
305+
306+
/**
307+
* Asynchronously rolls over an index using the Rollover Index API
308+
* <p>
309+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
310+
* Rollover Index API on elastic.co</a>
311+
*/
312+
public void rolloverAsync(RolloverRequest rolloverRequest, ActionListener<RolloverResponse> listener, Header... headers) {
313+
restHighLevelClient.performRequestAsyncAndParseEntity(rolloverRequest, Request::rollover, RolloverResponse::fromXContent,
314+
listener, emptySet(), headers);
315+
}
292316
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
3939
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
4040
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
41+
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
4142
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
4243
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
4344
import org.elasticsearch.action.bulk.BulkRequest;
@@ -498,11 +499,10 @@ static Request existsAlias(GetAliasesRequest getAliasesRequest) {
498499
}
499500

500501
static Request rankEval(RankEvalRequest rankEvalRequest) throws IOException {
501-
// TODO maybe indices should be propery of RankEvalRequest and not of the spec
502+
// TODO maybe indices should be property of RankEvalRequest and not of the spec
502503
List<String> indices = rankEvalRequest.getRankEvalSpec().getIndices();
503504
String endpoint = endpoint(indices.toArray(new String[indices.size()]), Strings.EMPTY_ARRAY, "_rank_eval");
504-
HttpEntity entity = null;
505-
entity = createEntity(rankEvalRequest.getRankEvalSpec(), REQUEST_BODY_CONTENT_TYPE);
505+
HttpEntity entity = createEntity(rankEvalRequest.getRankEvalSpec(), REQUEST_BODY_CONTENT_TYPE);
506506
return new Request(HttpGet.METHOD_NAME, endpoint, Collections.emptyMap(), entity);
507507
}
508508

@@ -542,6 +542,19 @@ static Request clusterPutSettings(ClusterUpdateSettingsRequest clusterUpdateSett
542542
return new Request(HttpPut.METHOD_NAME, endpoint, parameters.getParams(), entity);
543543
}
544544

545+
static Request rollover(RolloverRequest rolloverRequest) throws IOException {
546+
Params params = Params.builder();
547+
params.withTimeout(rolloverRequest.timeout());
548+
params.withMasterTimeout(rolloverRequest.masterNodeTimeout());
549+
params.withWaitForActiveShards(rolloverRequest.getCreateIndexRequest().waitForActiveShards());
550+
if (rolloverRequest.isDryRun()) {
551+
params.putParam("dry_run", Boolean.TRUE.toString());
552+
}
553+
String endpoint = buildEndpoint(rolloverRequest.getAlias(), "_rollover", rolloverRequest.getNewIndexName());
554+
HttpEntity entity = createEntity(rolloverRequest, REQUEST_BODY_CONTENT_TYPE);
555+
return new Request(HttpPost.METHOD_NAME, endpoint, params.getParams(), entity);
556+
}
557+
545558
private static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) throws IOException {
546559
BytesRef source = XContentHelper.toXContent(toXContent, xContentType, false).toBytesRef();
547560
return new ByteArrayEntity(source.bytes, source.offset, source.length, createContentType(xContentType));

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,18 @@
3939
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
4040
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
4141
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
42+
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
43+
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
4244
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
4345
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
4446
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
47+
import org.elasticsearch.action.index.IndexRequest;
4548
import org.elasticsearch.action.support.IndicesOptions;
49+
import org.elasticsearch.action.support.WriteRequest;
4650
import org.elasticsearch.common.settings.Settings;
51+
import org.elasticsearch.common.unit.ByteSizeUnit;
52+
import org.elasticsearch.common.unit.ByteSizeValue;
53+
import org.elasticsearch.common.unit.TimeValue;
4754
import org.elasticsearch.common.xcontent.XContentBuilder;
4855
import org.elasticsearch.common.xcontent.json.JsonXContent;
4956
import org.elasticsearch.common.xcontent.support.XContentMapValues;
@@ -435,4 +442,57 @@ public void testSplit() throws IOException {
435442
Map<String, Object> aliasData = (Map<String, Object>)XContentMapValues.extractValue("target.aliases.alias", getIndexResponse);
436443
assertNotNull(aliasData);
437444
}
445+
446+
public void testRollover() throws IOException {
447+
highLevelClient().indices().create(new CreateIndexRequest("test").alias(new Alias("alias")));
448+
RolloverRequest rolloverRequest = new RolloverRequest("alias", "test_new");
449+
rolloverRequest.addMaxIndexDocsCondition(1);
450+
451+
{
452+
RolloverResponse rolloverResponse = execute(rolloverRequest, highLevelClient().indices()::rollover,
453+
highLevelClient().indices()::rolloverAsync);
454+
assertFalse(rolloverResponse.isRolledOver());
455+
assertFalse(rolloverResponse.isDryRun());
456+
Map<String, Boolean> conditionStatus = rolloverResponse.getConditionStatus();
457+
assertEquals(1, conditionStatus.size());
458+
assertFalse(conditionStatus.get("[max_docs: 1]"));
459+
assertEquals("test", rolloverResponse.getOldIndex());
460+
assertEquals("test_new", rolloverResponse.getNewIndex());
461+
}
462+
463+
highLevelClient().index(new IndexRequest("test", "type", "1").source("field", "value"));
464+
highLevelClient().index(new IndexRequest("test", "type", "2").source("field", "value")
465+
.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL));
466+
//without the refresh the rollover may not happen as the number of docs seen may be off
467+
468+
{
469+
rolloverRequest.addMaxIndexAgeCondition(new TimeValue(1));
470+
rolloverRequest.dryRun(true);
471+
RolloverResponse rolloverResponse = execute(rolloverRequest, highLevelClient().indices()::rollover,
472+
highLevelClient().indices()::rolloverAsync);
473+
assertFalse(rolloverResponse.isRolledOver());
474+
assertTrue(rolloverResponse.isDryRun());
475+
Map<String, Boolean> conditionStatus = rolloverResponse.getConditionStatus();
476+
assertEquals(2, conditionStatus.size());
477+
assertTrue(conditionStatus.get("[max_docs: 1]"));
478+
assertTrue(conditionStatus.get("[max_age: 1ms]"));
479+
assertEquals("test", rolloverResponse.getOldIndex());
480+
assertEquals("test_new", rolloverResponse.getNewIndex());
481+
}
482+
{
483+
rolloverRequest.dryRun(false);
484+
rolloverRequest.addMaxIndexSizeCondition(new ByteSizeValue(1, ByteSizeUnit.MB));
485+
RolloverResponse rolloverResponse = execute(rolloverRequest, highLevelClient().indices()::rollover,
486+
highLevelClient().indices()::rolloverAsync);
487+
assertTrue(rolloverResponse.isRolledOver());
488+
assertFalse(rolloverResponse.isDryRun());
489+
Map<String, Boolean> conditionStatus = rolloverResponse.getConditionStatus();
490+
assertEquals(3, conditionStatus.size());
491+
assertTrue(conditionStatus.get("[max_docs: 1]"));
492+
assertTrue(conditionStatus.get("[max_age: 1ms]"));
493+
assertFalse(conditionStatus.get("[max_size: 1mb]"));
494+
assertEquals("test", rolloverResponse.getOldIndex());
495+
assertEquals("test_new", rolloverResponse.getNewIndex());
496+
}
497+
}
438498
}

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
4141
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
4242
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
43+
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
4344
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
4445
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
4546
import org.elasticsearch.action.bulk.BulkRequest;
@@ -74,6 +75,7 @@
7475
import org.elasticsearch.common.xcontent.XContentHelper;
7576
import org.elasticsearch.common.xcontent.XContentParser;
7677
import org.elasticsearch.common.xcontent.XContentType;
78+
import org.elasticsearch.index.RandomCreateIndexGenerator;
7779
import org.elasticsearch.index.VersionType;
7880
import org.elasticsearch.index.query.TermQueryBuilder;
7981
import org.elasticsearch.index.rankeval.PrecisionAtK;
@@ -1116,11 +1118,9 @@ private static void resizeTest(ResizeType resizeType, CheckedFunction<ResizeRequ
11161118
if (randomBoolean()) {
11171119
randomAliases(createIndexRequest);
11181120
}
1119-
setRandomWaitForActiveShards(createIndexRequest::waitForActiveShards, expectedParams);
11201121
resizeRequest.setTargetIndex(createIndexRequest);
1121-
} else {
1122-
setRandomWaitForActiveShards(resizeRequest::setWaitForActiveShards, expectedParams);
11231122
}
1123+
setRandomWaitForActiveShards(resizeRequest::setWaitForActiveShards, expectedParams);
11241124

11251125
Request request = function.apply(resizeRequest);
11261126
assertEquals(HttpPut.METHOD_NAME, request.getMethod());
@@ -1144,6 +1144,44 @@ public void testClusterPutSettings() throws IOException {
11441144
assertEquals(expectedParams, expectedRequest.getParameters());
11451145
}
11461146

1147+
public void testRollover() throws IOException {
1148+
RolloverRequest rolloverRequest = new RolloverRequest(randomAlphaOfLengthBetween(3, 10),
1149+
randomBoolean() ? null : randomAlphaOfLengthBetween(3, 10));
1150+
Map<String, String> expectedParams = new HashMap<>();
1151+
setRandomTimeout(rolloverRequest::timeout, rolloverRequest.timeout(), expectedParams);
1152+
setRandomMasterTimeout(rolloverRequest, expectedParams);
1153+
if (randomBoolean()) {
1154+
rolloverRequest.dryRun(randomBoolean());
1155+
if (rolloverRequest.isDryRun()) {
1156+
expectedParams.put("dry_run", "true");
1157+
}
1158+
}
1159+
if (randomBoolean()) {
1160+
rolloverRequest.addMaxIndexAgeCondition(new TimeValue(randomNonNegativeLong()));
1161+
}
1162+
if (randomBoolean()) {
1163+
String type = randomAlphaOfLengthBetween(3, 10);
1164+
rolloverRequest.getCreateIndexRequest().mapping(type, RandomCreateIndexGenerator.randomMapping(type));
1165+
}
1166+
if (randomBoolean()) {
1167+
RandomCreateIndexGenerator.randomAliases(rolloverRequest.getCreateIndexRequest());
1168+
}
1169+
if (randomBoolean()) {
1170+
rolloverRequest.getCreateIndexRequest().settings(RandomCreateIndexGenerator.randomIndexSettings());
1171+
}
1172+
setRandomWaitForActiveShards(rolloverRequest.getCreateIndexRequest()::waitForActiveShards, expectedParams);
1173+
1174+
Request request = Request.rollover(rolloverRequest);
1175+
if (rolloverRequest.getNewIndexName() == null) {
1176+
assertEquals("/" + rolloverRequest.getAlias() + "/_rollover", request.getEndpoint());
1177+
} else {
1178+
assertEquals("/" + rolloverRequest.getAlias() + "/_rollover/" + rolloverRequest.getNewIndexName(), request.getEndpoint());
1179+
}
1180+
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
1181+
assertToXContentBody(rolloverRequest, request.getEntity());
1182+
assertEquals(expectedParams, request.getParameters());
1183+
}
1184+
11471185
private static void assertToXContentBody(ToXContent expectedBody, HttpEntity actualEntity) throws IOException {
11481186
BytesReference expectedBytes = XContentHelper.toXContent(expectedBody, REQUEST_BODY_CONTENT_TYPE, false);
11491187
assertEquals(XContentType.JSON.mediaTypeWithoutParameters(), actualEntity.getContentType().getValue());

0 commit comments

Comments
 (0)