add body support for create index request
This commit is contained in:
parent
dec0dcc30b
commit
0c6d19c40c
|
@ -21,6 +21,7 @@ package org.elasticsearch.action.admin.indices.rollover;
|
|||
import org.elasticsearch.ElasticsearchParseException;
|
||||
import org.elasticsearch.action.ActionRequestValidationException;
|
||||
import org.elasticsearch.action.IndicesRequest;
|
||||
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
import org.elasticsearch.action.support.master.AcknowledgedRequest;
|
||||
import org.elasticsearch.common.ParseField;
|
||||
|
@ -37,6 +38,8 @@ import org.elasticsearch.common.xcontent.XContentType;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.elasticsearch.action.ValidateActions.addValidationError;
|
||||
|
@ -50,13 +53,26 @@ public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implem
|
|||
private String sourceAlias;
|
||||
private boolean simulate;
|
||||
private Set<Condition> conditions = new HashSet<>(2);
|
||||
private CreateIndexRequest createIndexRequest = new CreateIndexRequest("_na_");
|
||||
|
||||
public static ObjectParser<Set<Condition>, ParseFieldMatcherSupplier> TLP_PARSER =
|
||||
public static ObjectParser<RolloverRequest, ParseFieldMatcherSupplier> PARSER =
|
||||
new ObjectParser<>("conditions", null);
|
||||
static {
|
||||
TLP_PARSER.declareField((parser, conditions, parseFieldMatcherSupplier) ->
|
||||
Condition.PARSER.parse(parser, conditions, () -> ParseFieldMatcher.EMPTY),
|
||||
PARSER.declareField((parser, request, parseFieldMatcherSupplier) ->
|
||||
Condition.PARSER.parse(parser, request.conditions, parseFieldMatcherSupplier),
|
||||
new ParseField("conditions"), ObjectParser.ValueType.OBJECT);
|
||||
PARSER.declareField((parser, request, parseFieldMatcherSupplier) ->
|
||||
request.createIndexRequest.settings(parser.map()),
|
||||
new ParseField("settings"), ObjectParser.ValueType.OBJECT);
|
||||
PARSER.declareField((parser, request, parseFieldMatcherSupplier) -> {
|
||||
for (Map.Entry<String, Object> mappingsEntry : parser.map().entrySet()) {
|
||||
request.createIndexRequest.mapping(mappingsEntry.getKey(),
|
||||
(Map<String, Object>) mappingsEntry.getValue());
|
||||
}
|
||||
}, new ParseField("mappings"), ObjectParser.ValueType.OBJECT);
|
||||
PARSER.declareField((parser, request, parseFieldMatcherSupplier) ->
|
||||
request.createIndexRequest.aliases(parser.map()),
|
||||
new ParseField("aliases"), ObjectParser.ValueType.OBJECT);
|
||||
}
|
||||
|
||||
RolloverRequest() {}
|
||||
|
@ -67,10 +83,13 @@ public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implem
|
|||
|
||||
@Override
|
||||
public ActionRequestValidationException validate() {
|
||||
ActionRequestValidationException validationException = null;
|
||||
ActionRequestValidationException validationException = createIndexRequest == null ? null : createIndexRequest.validate();
|
||||
if (sourceAlias == null) {
|
||||
validationException = addValidationError("source alias is missing", validationException);
|
||||
}
|
||||
if (createIndexRequest == null) {
|
||||
validationException = addValidationError("create index request is missing", validationException);
|
||||
}
|
||||
return validationException;
|
||||
}
|
||||
|
||||
|
@ -83,6 +102,8 @@ public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implem
|
|||
for (int i = 0; i < size; i++) {
|
||||
this.conditions.add(in.readNamedWriteable(Condition.class));
|
||||
}
|
||||
createIndexRequest = new CreateIndexRequest();
|
||||
createIndexRequest.readFrom(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -94,6 +115,7 @@ public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implem
|
|||
for (Condition condition : conditions) {
|
||||
out.writeNamedWriteable(condition);
|
||||
}
|
||||
createIndexRequest.writeTo(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -134,11 +156,19 @@ public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implem
|
|||
return sourceAlias;
|
||||
}
|
||||
|
||||
public CreateIndexRequest getCreateIndexRequest() {
|
||||
return createIndexRequest;
|
||||
}
|
||||
|
||||
public void setCreateIndexRequest(CreateIndexRequest createIndexRequest) {
|
||||
this.createIndexRequest = Objects.requireNonNull(createIndexRequest, "create index request must not be null");;
|
||||
}
|
||||
|
||||
public void source(BytesReference source) {
|
||||
XContentType xContentType = XContentFactory.xContentType(source);
|
||||
if (xContentType != null) {
|
||||
try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(source)) {
|
||||
TLP_PARSER.parse(parser, this.conditions, () -> ParseFieldMatcher.EMPTY);
|
||||
PARSER.parse(parser, this, () -> ParseFieldMatcher.EMPTY);
|
||||
} catch (IOException e) {
|
||||
throw new ElasticsearchParseException("failed to parse source for rollover index", e);
|
||||
}
|
||||
|
|
|
@ -18,8 +18,10 @@
|
|||
*/
|
||||
package org.elasticsearch.action.admin.indices.rollover;
|
||||
|
||||
import org.elasticsearch.action.admin.indices.alias.Alias;
|
||||
import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder;
|
||||
import org.elasticsearch.client.ElasticsearchClient;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
|
||||
|
||||
|
@ -51,4 +53,19 @@ public class RolloverRequestBuilder extends MasterNodeOperationRequestBuilder<Ro
|
|||
this.request.simulate(simulate);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RolloverRequestBuilder settings(Settings settings) {
|
||||
this.request.getCreateIndexRequest().settings(settings);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RolloverRequestBuilder alias(Alias alias) {
|
||||
this.request.getCreateIndexRequest().alias(alias);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RolloverRequestBuilder mapping(String type, String source) {
|
||||
this.request.getCreateIndexRequest().mapping(type, source);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ package org.elasticsearch.action.admin.indices.rollover;
|
|||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesClusterStateUpdateRequest;
|
||||
import org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest;
|
||||
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
|
||||
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
|
||||
import org.elasticsearch.action.support.ActionFilters;
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
|
@ -117,9 +118,7 @@ public class TransportRolloverAction extends TransportMasterNodeAction<RolloverR
|
|||
new RolloverResponse(sourceIndexName, rolloverIndexName, conditionResults, false, true,
|
||||
createRolloverIndex);
|
||||
if (createRolloverIndex) {
|
||||
CreateIndexClusterStateUpdateRequest updateRequest =
|
||||
prepareCreateIndexRequest(rolloverIndexName, rolloverRequest);
|
||||
createIndexService.createIndex(updateRequest,
|
||||
createIndexService.createIndex(prepareCreateIndexRequest(rolloverIndexName, rolloverRequest),
|
||||
new ActionListener<ClusterStateUpdateResponse>() {
|
||||
@Override
|
||||
public void onResponse(ClusterStateUpdateResponse response) {
|
||||
|
@ -216,10 +215,18 @@ public class TransportRolloverAction extends TransportMasterNodeAction<RolloverR
|
|||
|
||||
static CreateIndexClusterStateUpdateRequest prepareCreateIndexRequest(final String targetIndexName,
|
||||
final RolloverRequest rolloverRequest) {
|
||||
return new CreateIndexClusterStateUpdateRequest(rolloverRequest,
|
||||
|
||||
final CreateIndexRequest createIndexRequest = rolloverRequest.getCreateIndexRequest();
|
||||
createIndexRequest.cause("rollover_index");
|
||||
createIndexRequest.index(targetIndexName);
|
||||
return new CreateIndexClusterStateUpdateRequest(createIndexRequest,
|
||||
"rollover_index", targetIndexName, true)
|
||||
.ackTimeout(rolloverRequest.timeout())
|
||||
.masterNodeTimeout(rolloverRequest.masterNodeTimeout());
|
||||
.ackTimeout(createIndexRequest.timeout())
|
||||
.masterNodeTimeout(createIndexRequest.masterNodeTimeout())
|
||||
.settings(createIndexRequest.settings())
|
||||
.aliases(createIndexRequest.aliases())
|
||||
.customs(createIndexRequest.customs())
|
||||
.mappings(createIndexRequest.mappings());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ package org.elasticsearch.action.admin.indices.rollover;
|
|||
import org.elasticsearch.action.admin.indices.alias.Alias;
|
||||
import org.elasticsearch.cluster.ClusterState;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
|
||||
|
@ -67,6 +68,32 @@ public class RolloverIT extends ESIntegTestCase {
|
|||
assertTrue(newIndex.getAliases().containsKey("test_alias"));
|
||||
}
|
||||
|
||||
public void testRolloverWithIndexSettings() throws Exception {
|
||||
assertAcked(prepareCreate("test_index-2").addAlias(new Alias("test_alias")).get());
|
||||
index("test_index-2", "type1", "1", "field", "value");
|
||||
flush("test_index-2");
|
||||
final Settings settings = Settings.builder()
|
||||
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
|
||||
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
|
||||
.build();
|
||||
final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias")
|
||||
.settings(settings).alias(new Alias("extra_alias")).get();
|
||||
assertThat(response.getOldIndex(), equalTo("test_index-2"));
|
||||
assertThat(response.getNewIndex(), equalTo("test_index-3"));
|
||||
assertThat(response.isSimulate(), equalTo(false));
|
||||
assertThat(response.isRolledOver(), equalTo(true));
|
||||
assertThat(response.isRolloverIndexCreated(), equalTo(true));
|
||||
assertThat(response.getConditionStatus().size(), equalTo(0));
|
||||
final ClusterState state = client().admin().cluster().prepareState().get().getState();
|
||||
final IndexMetaData oldIndex = state.metaData().index("test_index-2");
|
||||
assertFalse(oldIndex.getAliases().containsKey("test_alias"));
|
||||
final IndexMetaData newIndex = state.metaData().index("test_index-3");
|
||||
assertThat(newIndex.getNumberOfShards(), equalTo(1));
|
||||
assertThat(newIndex.getNumberOfReplicas(), equalTo(0));
|
||||
assertTrue(newIndex.getAliases().containsKey("test_alias"));
|
||||
assertTrue(newIndex.getAliases().containsKey("extra_alias"));
|
||||
}
|
||||
|
||||
public void testRolloverSimulate() throws Exception {
|
||||
assertAcked(prepareCreate("test_index-1").addAlias(new Alias("test_alias")).get());
|
||||
index("test_index-1", "type1", "1", "field", "value");
|
||||
|
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.action.admin.indices.rollover;
|
||||
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
public class RolloverRequestTests extends ESTestCase {
|
||||
|
||||
public void testConditionsParsing() throws Exception {
|
||||
final RolloverRequest request = new RolloverRequest(randomAsciiOfLength(10));
|
||||
final XContentBuilder builder = XContentFactory.jsonBuilder()
|
||||
.startObject()
|
||||
.startObject("conditions")
|
||||
.field("max_age", "10d")
|
||||
.field("max_docs", 100)
|
||||
.endObject()
|
||||
.endObject();
|
||||
request.source(builder.bytes());
|
||||
Set<Condition> conditions = request.getConditions();
|
||||
assertThat(conditions.size(), equalTo(2));
|
||||
for (Condition condition : conditions) {
|
||||
if (condition instanceof MaxAgeCondition) {
|
||||
MaxAgeCondition maxAgeCondition = (MaxAgeCondition) condition;
|
||||
assertThat(maxAgeCondition.value.getMillis(), equalTo(TimeValue.timeValueHours(24 * 10).getMillis()));
|
||||
} else if (condition instanceof MaxDocsCondition) {
|
||||
MaxDocsCondition maxDocsCondition = (MaxDocsCondition) condition;
|
||||
assertThat(maxDocsCondition.value, equalTo(100L));
|
||||
} else {
|
||||
fail("unexpected condition " + condition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testParsingWithIndexSettings() throws Exception {
|
||||
final RolloverRequest request = new RolloverRequest(randomAsciiOfLength(10));
|
||||
final XContentBuilder builder = XContentFactory.jsonBuilder()
|
||||
.startObject()
|
||||
.startObject("conditions")
|
||||
.field("max_age", "10d")
|
||||
.field("max_docs", 100)
|
||||
.endObject()
|
||||
.startObject("mappings")
|
||||
.startObject("type1")
|
||||
.startObject("properties")
|
||||
.startObject("field1")
|
||||
.field("type", "string")
|
||||
.field("index", "not_analyzed")
|
||||
.endObject()
|
||||
.endObject()
|
||||
.endObject()
|
||||
.endObject()
|
||||
.startObject("settings")
|
||||
.field("number_of_shards", 10)
|
||||
.endObject()
|
||||
.startObject("aliases")
|
||||
.startObject("alias1").endObject()
|
||||
.endObject()
|
||||
.endObject();
|
||||
request.source(builder.bytes());
|
||||
Set<Condition> conditions = request.getConditions();
|
||||
assertThat(conditions.size(), equalTo(2));
|
||||
assertThat(request.getCreateIndexRequest().mappings().size(), equalTo(1));
|
||||
assertThat(request.getCreateIndexRequest().aliases().size(), equalTo(1));
|
||||
assertThat(request.getCreateIndexRequest().settings().getAsInt("number_of_shards", 0), equalTo(10));
|
||||
}
|
||||
}
|
|
@ -21,6 +21,7 @@ package org.elasticsearch.action.admin.indices.rollover;
|
|||
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesClusterStateUpdateRequest;
|
||||
import org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest;
|
||||
import org.elasticsearch.cluster.metadata.AliasAction;
|
||||
import org.elasticsearch.cluster.metadata.AliasMetaData;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
|
@ -86,7 +87,6 @@ public class TransportRolloverActionTests extends ESTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public void testCreateUpdateAliasRequest() throws Exception {
|
||||
String sourceAlias = randomAsciiOfLength(10);
|
||||
String sourceIndex = randomAsciiOfLength(10);
|
||||
|
@ -158,4 +158,22 @@ public class TransportRolloverActionTests extends ESTestCase {
|
|||
assertThat(TransportRolloverAction.generateRolloverIndexName("index-name-1"), equalTo("index-name-2"));
|
||||
assertThat(TransportRolloverAction.generateRolloverIndexName("index-name-2"), equalTo("index-name-3"));
|
||||
}
|
||||
|
||||
public void testCreateIndexRequest() throws Exception {
|
||||
String alias = randomAsciiOfLength(10);
|
||||
String rolloverIndex = randomAsciiOfLength(10);
|
||||
final RolloverRequest rolloverRequest = new RolloverRequest(alias);
|
||||
final Settings settings = Settings.builder()
|
||||
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
|
||||
.put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID())
|
||||
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
|
||||
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
|
||||
.build();
|
||||
rolloverRequest.getCreateIndexRequest().settings(settings);
|
||||
final CreateIndexClusterStateUpdateRequest createIndexRequest =
|
||||
TransportRolloverAction.prepareCreateIndexRequest(rolloverIndex, rolloverRequest);
|
||||
assertThat(createIndexRequest.settings(), equalTo(settings));
|
||||
assertThat(createIndexRequest.index(), equalTo(rolloverIndex));
|
||||
assertThat(createIndexRequest.cause(), equalTo("rollover_index"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue