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 b446e498f77fa..5c6d63f04b3fe 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -993,10 +993,14 @@ public Builder updateSettings(Settings settings, String... indices) { 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 36c512c17aaac..bab150fff12bd 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 Builder updateNodes(long version, RoutingNodes routingNodes) { return this; } - public Builder updateNumberOfReplicas(int numberOfReplicas, String... indices) { + /** + * 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) { if (indicesRouting == null) { throw new IllegalStateException("once build is called the builder cannot be reused"); } - if (indices == null || indices.length == 0) { - indices = indicesRouting.keys().toArray(String.class); - } for (String index : indices) { IndexRoutingTable indexRoutingTable = indicesRouting.get(index); if (indexRoutingTable == null) { diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java index 349997d7793eb..0f55270354301 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java @@ -275,7 +275,7 @@ public void testRoutingTableBuiltMoreThanOnce() { assertThat(e.getMessage(), containsString("cannot be reused")); } try { - b.updateNumberOfReplicas(1, "foo"); + b.updateNumberOfReplicas(1, new String[]{"foo"}); fail("expected exception"); } catch (IllegalStateException e) { assertThat(e.getMessage(), containsString("cannot be reused")); diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/InSyncAllocationIdTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/InSyncAllocationIdTests.java index 5f39336569fd9..8630d2cc5b6d9 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/InSyncAllocationIdTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/InSyncAllocationIdTests.java @@ -292,8 +292,8 @@ public void testInSyncIdsNotTrimmedWhenNotGrowing() throws Exception { logger.info("decrease number of replicas to 0"); clusterState = ClusterState.builder(clusterState) - .routingTable(RoutingTable.builder(clusterState.routingTable()).updateNumberOfReplicas(0, "test").build()) - .metaData(MetaData.builder(clusterState.metaData()).updateNumberOfReplicas(0, "test")).build(); + .routingTable(RoutingTable.builder(clusterState.routingTable()).updateNumberOfReplicas(0, new String[]{"test"}).build()) + .metaData(MetaData.builder(clusterState.metaData()).updateNumberOfReplicas(0, new String[]{"test"})).build(); logger.info("add back node 1"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add( diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferPrimaryAllocationTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferPrimaryAllocationTests.java index d4e032f47614a..35a9be017d5d4 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferPrimaryAllocationTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferPrimaryAllocationTests.java @@ -68,8 +68,10 @@ public void testPreferPrimaryAllocationOverReplicas() { } logger.info("increasing the number of replicas to 1, and perform a reroute (to get the replicas allocation going)"); - RoutingTable updatedRoutingTable = RoutingTable.builder(clusterState.routingTable()).updateNumberOfReplicas(1).build(); - metaData = MetaData.builder(clusterState.metaData()).updateNumberOfReplicas(1).build(); + final String[] indices = {"test1", "test2"}; + RoutingTable updatedRoutingTable = + RoutingTable.builder(clusterState.routingTable()).updateNumberOfReplicas(1, indices).build(); + metaData = MetaData.builder(clusterState.metaData()).updateNumberOfReplicas(1, indices).build(); clusterState = ClusterState.builder(clusterState).routingTable(updatedRoutingTable).metaData(metaData).build(); clusterState = strategy.reroute(clusterState, "reroute"); diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/UpdateNumberOfReplicasTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/UpdateNumberOfReplicasTests.java index 167172ec9bd15..3001a4ba9e423 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/UpdateNumberOfReplicasTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/UpdateNumberOfReplicasTests.java @@ -96,8 +96,10 @@ public void testUpdateNumberOfReplicas() { logger.info("add another replica"); routingNodes = clusterState.getRoutingNodes(); - RoutingTable updatedRoutingTable = RoutingTable.builder(clusterState.routingTable()).updateNumberOfReplicas(2).build(); - metaData = MetaData.builder(clusterState.metaData()).updateNumberOfReplicas(2).build(); + final String[] indices = {"test"}; + RoutingTable updatedRoutingTable = + RoutingTable.builder(clusterState.routingTable()).updateNumberOfReplicas(2, indices).build(); + metaData = MetaData.builder(clusterState.metaData()).updateNumberOfReplicas(2, indices).build(); clusterState = ClusterState.builder(clusterState).routingTable(updatedRoutingTable).metaData(metaData).build(); assertThat(clusterState.metaData().index("test").getNumberOfReplicas(), equalTo(2)); @@ -143,8 +145,8 @@ public void testUpdateNumberOfReplicas() { logger.info("now remove a replica"); routingNodes = clusterState.getRoutingNodes(); - updatedRoutingTable = RoutingTable.builder(clusterState.routingTable()).updateNumberOfReplicas(1).build(); - metaData = MetaData.builder(clusterState.metaData()).updateNumberOfReplicas(1).build(); + updatedRoutingTable = RoutingTable.builder(clusterState.routingTable()).updateNumberOfReplicas(1, indices).build(); + metaData = MetaData.builder(clusterState.metaData()).updateNumberOfReplicas(1, indices).build(); clusterState = ClusterState.builder(clusterState).routingTable(updatedRoutingTable).metaData(metaData).build(); assertThat(clusterState.metaData().index("test").getNumberOfReplicas(), equalTo(1)); diff --git a/server/src/test/java/org/elasticsearch/indices/settings/UpdateNumberOfReplicasIT.java b/server/src/test/java/org/elasticsearch/indices/settings/UpdateNumberOfReplicasIT.java index 9b759bff56928..100cda1c03cef 100644 --- a/server/src/test/java/org/elasticsearch/indices/settings/UpdateNumberOfReplicasIT.java +++ b/server/src/test/java/org/elasticsearch/indices/settings/UpdateNumberOfReplicasIT.java @@ -21,6 +21,7 @@ import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.Priority; @@ -28,6 +29,7 @@ import org.elasticsearch.test.ESIntegTestCase; import java.io.IOException; +import java.util.EnumSet; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; @@ -274,4 +276,21 @@ public void testUpdateWithInvalidNumberOfReplicas() { assertEquals("Failed to parse value [" + value + "] for setting [index.number_of_replicas] must be >= 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)); + } + }