-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a deprecation info API check for too many shards (#85967)
This commit makes sure that there is enough room in a cluster to add a small number of shards during an upgrade. The information is exposed in the deprecation info API as a cluster configuration check. Closes #85702
- Loading branch information
Showing
5 changed files
with
178 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...precation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.deprecation; | ||
|
||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.indices.ShardLimitValidator; | ||
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue; | ||
|
||
import java.util.Locale; | ||
|
||
public class ClusterDeprecationChecks { | ||
/** | ||
* Upgrading can require the addition of one ore more small indices. This method checks that based on configuration we have the room | ||
* to add a small number of additional shards to the cluster. The goal is to prevent a failure during upgrade. | ||
* @param clusterState The cluster state, used to get settings and information about nodes | ||
* @return A deprecation issue if there is not enough room in this cluster to add a few more shards, or null otherwise | ||
*/ | ||
static DeprecationIssue checkShards(ClusterState clusterState) { | ||
// Make sure we have room to add a small non-frozen index if needed | ||
final int shardsInFutureNewSmallIndex = 5; | ||
final int replicasForFutureIndex = 1; | ||
if (ShardLimitValidator.canAddShardsToCluster(shardsInFutureNewSmallIndex, replicasForFutureIndex, clusterState, false)) { | ||
return null; | ||
} else { | ||
final int totalShardsToAdd = shardsInFutureNewSmallIndex * (1 + replicasForFutureIndex); | ||
return new DeprecationIssue( | ||
DeprecationIssue.Level.WARNING, | ||
"The cluster has too many shards to be able to upgrade", | ||
"https://ela.st/es-deprecation-8-shard-limit", | ||
String.format( | ||
Locale.ROOT, | ||
"Upgrading requires adding a small number of new shards. There is not enough room for %d more " | ||
+ "shards. Increase the cluster.max_shards_per_node setting, or remove indices " | ||
+ "to clear up resources.", | ||
totalShardsToAdd | ||
), | ||
false, | ||
null | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...tion/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.deprecation; | ||
|
||
import org.elasticsearch.Version; | ||
import org.elasticsearch.cluster.ClusterName; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.metadata.IndexMetadata; | ||
import org.elasticsearch.cluster.metadata.Metadata; | ||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.cluster.node.DiscoveryNodes; | ||
import org.elasticsearch.cluster.routing.allocation.DataTier; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.indices.ShardLimitValidator; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static org.elasticsearch.xpack.deprecation.DeprecationChecks.CLUSTER_SETTINGS_CHECKS; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class ClusterDeprecationChecksTests extends ESTestCase { | ||
public void testCheckShards() { | ||
/* | ||
* This test sets the number of allowed shards per node to 5 and creates 2 nodes. So we have room for 10 shards, which is the | ||
* number of shards that checkShards() is making sure we can add. The first time there are no indices, so the check passes. The | ||
* next time there is an index with one shard and one replica, leaving room for 8 shards. So the check fails. | ||
*/ | ||
final ClusterState state = ClusterState.builder(new ClusterName(randomAlphaOfLength(5))) | ||
.metadata( | ||
Metadata.builder() | ||
.persistentSettings(Settings.builder().put(ShardLimitValidator.SETTING_CLUSTER_MAX_SHARDS_PER_NODE.getKey(), 5).build()) | ||
.build() | ||
) | ||
.nodes( | ||
DiscoveryNodes.builder() | ||
.add(new DiscoveryNode(UUID.randomUUID().toString(), buildNewFakeTransportAddress(), Version.CURRENT)) | ||
.add(new DiscoveryNode(UUID.randomUUID().toString(), buildNewFakeTransportAddress(), Version.CURRENT)) | ||
) | ||
.build(); | ||
List<DeprecationIssue> issues = DeprecationChecks.filterChecks(CLUSTER_SETTINGS_CHECKS, c -> c.apply(state)); | ||
assertThat(0, equalTo(issues.size())); | ||
|
||
final ClusterState stateWithProblems = ClusterState.builder(new ClusterName(randomAlphaOfLength(5))) | ||
.metadata( | ||
Metadata.builder() | ||
.persistentSettings(Settings.builder().put(ShardLimitValidator.SETTING_CLUSTER_MAX_SHARDS_PER_NODE.getKey(), 4).build()) | ||
.put( | ||
IndexMetadata.builder(randomAlphaOfLength(10)) | ||
.settings(settings(Version.CURRENT).put(DataTier.TIER_PREFERENCE_SETTING.getKey(), " ")) | ||
.numberOfShards(1) | ||
.numberOfReplicas(1) | ||
.build(), | ||
false | ||
) | ||
.build() | ||
) | ||
.nodes( | ||
DiscoveryNodes.builder() | ||
.add(new DiscoveryNode(UUID.randomUUID().toString(), buildNewFakeTransportAddress(), Version.CURRENT)) | ||
.add(new DiscoveryNode(UUID.randomUUID().toString(), buildNewFakeTransportAddress(), Version.CURRENT)) | ||
) | ||
.build(); | ||
|
||
issues = DeprecationChecks.filterChecks(CLUSTER_SETTINGS_CHECKS, c -> c.apply(stateWithProblems)); | ||
|
||
DeprecationIssue expected = new DeprecationIssue( | ||
DeprecationIssue.Level.WARNING, | ||
"The cluster has too many shards to be able to upgrade", | ||
"https://ela.st/es-deprecation-8-shard-limit", | ||
"Upgrading requires adding a small number of new shards. There is not enough room for 10 more shards. Increase the cluster" | ||
+ ".max_shards_per_node setting, or remove indices to clear up resources.", | ||
false, | ||
null | ||
); | ||
assertEquals(singletonList(expected), issues); | ||
} | ||
} |