Read the index.number_of_replicas from template so that wait_for_active_shards is interpreted correctly (#54231) (#54413)

This commit takes into account the index.number_of_replicas (defaults to
0 - no replicas- ) value when setting an index template. This change
enables the index.wait_for_active_shards value to be interpreted
correctly

(cherry picked from commit 07026ac3d56dc9fae69467adfda7eaed7ea3ca00)
Signed-off-by: Andrei Dan <andrei.dan@elastic.co>

Co-authored-by: tninokehoe <62655306+tninokehoe@users.noreply.github.com>
This commit is contained in:
Andrei Dan 2020-03-30 14:34:49 +01:00 committed by GitHub
parent 3c604da7f6
commit d5320d9d29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 1 deletions

View File

@ -490,13 +490,15 @@ public class MetaDataIndexTemplateService {
int dummyPartitionSize = IndexMetaData.INDEX_ROUTING_PARTITION_SIZE_SETTING.get(settings); int dummyPartitionSize = IndexMetaData.INDEX_ROUTING_PARTITION_SIZE_SETTING.get(settings);
int dummyShards = settings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, int dummyShards = settings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS,
dummyPartitionSize == 1 ? 1 : dummyPartitionSize + 1); dummyPartitionSize == 1 ? 1 : dummyPartitionSize + 1);
int shardReplicas = settings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0);
//create index service for parsing and validating "mappings" //create index service for parsing and validating "mappings"
Settings dummySettings = Settings.builder() Settings dummySettings = Settings.builder()
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
.put(settings) .put(settings)
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, dummyShards) .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, dummyShards)
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0) .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, shardReplicas)
.put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()) .put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID())
.build(); .build();

View File

@ -76,6 +76,53 @@ public class MetaDataIndexTemplateServiceTests extends ESSingleNodeTestCase {
"must be one of [true, false, checksum] but was: blargh")); "must be one of [true, false, checksum] but was: blargh"));
} }
public void testIndexTemplateValidationWithSpecifiedReplicas() throws Exception {
PutRequest request = new PutRequest("test", "test_replicas");
request.patterns(singletonList("test_shards_wait*"));
Settings.Builder settingsBuilder = builder()
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, "1")
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, "1")
.put(IndexMetaData.SETTING_WAIT_FOR_ACTIVE_SHARDS.getKey(), "2");
request.settings(settingsBuilder.build());
List<Throwable> throwables = putTemplateDetail(request);
assertThat(throwables, is(empty()));
}
public void testIndexTemplateValidationErrorsWithSpecifiedReplicas() throws Exception {
PutRequest request = new PutRequest("test", "test_specified_replicas");
request.patterns(singletonList("test_shards_wait*"));
Settings.Builder settingsBuilder = builder()
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, "1")
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, "1")
.put(IndexMetaData.SETTING_WAIT_FOR_ACTIVE_SHARDS.getKey(), "3");
request.settings(settingsBuilder.build());
List<Throwable> throwables = putTemplateDetail(request);
assertThat(throwables.get(0), instanceOf(IllegalArgumentException.class));
assertThat(throwables.get(0).getMessage(), containsString("[3]: cannot be greater than number of shard copies [2]"));
}
public void testIndexTemplateValidationWithDefaultReplicas() throws Exception {
PutRequest request = new PutRequest("test", "test_default_replicas");
request.patterns(singletonList("test_wait_shards_default_replica*"));
Settings.Builder settingsBuilder = builder()
.put(IndexMetaData.SETTING_WAIT_FOR_ACTIVE_SHARDS.getKey(), "2");
request.settings(settingsBuilder.build());
List<Throwable> throwables = putTemplateDetail(request);
assertThat(throwables.get(0), instanceOf(IllegalArgumentException.class));
assertThat(throwables.get(0).getMessage(), containsString("[2]: cannot be greater than number of shard copies [1]"));
}
public void testIndexTemplateValidationAccumulatesValidationErrors() { public void testIndexTemplateValidationAccumulatesValidationErrors() {
PutRequest request = new PutRequest("test", "putTemplate shards"); PutRequest request = new PutRequest("test", "putTemplate shards");
request.patterns(singletonList("_test_shards*")); request.patterns(singletonList("_test_shards*"));