From 55dee530462cb077395033564b06dd36ac091f05 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Mon, 15 Oct 2018 19:49:58 -0400 Subject: [PATCH] Do not update number of replicas on no indices (#34481) Today when submitting an update settings request to update the number of replicas with a wildcard that does not match any indices and allow no indices is set to true, the request ends up being interpreted as updating the number of replicas for all indices. That is, consider the following sequence: PUT /test-index { "settings": { "index.number_of_replicas": 0 } } PUT /non-existent-*/_settings?expand_wildcards=open&allow_no_indices=true { "settings": { "index.number_of_replicas": 1 } } GET /test-index/_settings The latter will show that the number of replicas on test-index is now one. This is surprising, and should be considered a bug. The underlying problem here is treating no indices in the underlying methods used to update the routing table and the metadata as meaning all indices. This commit takes away this assumption. Tests that relied on this behavior have been changed to no longer rely on this. A test for this situation is added in UpdateNumberOfReplicasIT. --- .../cluster/metadata/MetaData.java | 12 ++++++++---- .../cluster/routing/RoutingTable.java | 12 ++++++++---- .../cluster/routing/RoutingTableTests.java | 2 +- .../allocation/InSyncAllocationIdTests.java | 4 ++-- .../PreferPrimaryAllocationTests.java | 6 ++++-- .../UpdateNumberOfReplicasTests.java | 10 ++++++---- .../settings/UpdateNumberOfReplicasIT.java | 19 +++++++++++++++++++ 7 files changed, 48 insertions(+), 17 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java index 75869b54850..f4eafd05e15 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -1034,10 +1034,14 @@ public class MetaData implements Iterable, Diffable, To return this; } - public Builder updateNumberOfReplicas(int numberOfReplicas, String... indices) { - if (indices == null || indices.length == 0) { - indices = this.indices.keys().toArray(String.class); - } + /** + * Update the number of replicas for the specified indices. + * + * @param numberOfReplicas the number of replicas + * @param indices the indices to update the number of replicas for + * @return the builder + */ + public Builder updateNumberOfReplicas(final int numberOfReplicas, final String[] indices) { for (String index : indices) { IndexMetaData indexMetaData = this.indices.get(index); if (indexMetaData == null) { diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java b/server/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java index 36c512c17aa..bab150fff12 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java @@ -457,13 +457,17 @@ public class RoutingTable implements Iterable, Diffable= 0", e.getMessage()); } } + + public void testUpdateNumberOfReplicasAllowNoIndices() { + createIndex("test-index", Settings.builder().put("index.number_of_replicas", 0).build()); + final IndicesOptions options = + new IndicesOptions(EnumSet.of(IndicesOptions.Option.ALLOW_NO_INDICES), EnumSet.of(IndicesOptions.WildcardStates.OPEN)); + assertAcked(client() + .admin() + .indices() + .prepareUpdateSettings("non-existent-*") + .setSettings(Settings.builder().put("index.number_of_replicas", 1)) + .setIndicesOptions(options) + .get()); + final int numberOfReplicas = Integer.parseInt( + client().admin().indices().prepareGetSettings("test-index").get().getSetting("test-index", "index.number_of_replicas")); + assertThat(numberOfReplicas, equalTo(0)); + } + }