From b2cf7521c5a38a1df37706eb071af77c04afc90d Mon Sep 17 00:00:00 2001 From: kkewwei Date: Wed, 12 Jun 2024 13:49:39 +0800 Subject: [PATCH] Fix the computed max shards of cluster to avoid int overflow Signed-off-by: kkewwei --- CHANGELOG.md | 1 + .../indices/ShardLimitValidator.java | 7 ++++--- .../indices/ShardLimitValidatorTests.java | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e46ab0077314..30f93d6490673 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Fix NPE on restore searchable snapshot ([#13911](https://github.com/opensearch-project/OpenSearch/pull/13911)) - Fix double invocation of postCollection when MultiBucketCollector is present ([#14015](https://github.com/opensearch-project/OpenSearch/pull/14015)) - Java high-level REST client bulk() is not respecting the bulkRequest.requireAlias(true) method call ([#14146](https://github.com/opensearch-project/OpenSearch/pull/14146)) +- Fix the computed max shards of cluster to avoid int overflow ([#14155](https://github.com/opensearch-project/OpenSearch/pull/14155)) ### Security diff --git a/server/src/main/java/org/opensearch/indices/ShardLimitValidator.java b/server/src/main/java/org/opensearch/indices/ShardLimitValidator.java index e345b613eebbd..94e91e2d4c3ac 100644 --- a/server/src/main/java/org/opensearch/indices/ShardLimitValidator.java +++ b/server/src/main/java/org/opensearch/indices/ShardLimitValidator.java @@ -261,14 +261,15 @@ static Optional checkShardLimit( return Optional.empty(); } + int computedMaxShards = (int) Math.min(Integer.MAX_VALUE, (long) maxShardsPerNodeSetting * nodeCount); int maxShardsInCluster = maxShardsPerClusterSetting; if (maxShardsInCluster == -1) { - maxShardsInCluster = maxShardsPerNodeSetting * nodeCount; + maxShardsInCluster = computedMaxShards; } else { - maxShardsInCluster = Math.min(maxShardsInCluster, maxShardsPerNodeSetting * nodeCount); + maxShardsInCluster = Math.min(maxShardsInCluster, computedMaxShards); } - int currentOpenShards = state.getMetadata().getTotalOpenIndexShards(); + long currentOpenShards = state.getMetadata().getTotalOpenIndexShards(); if ((currentOpenShards + newShards) > maxShardsInCluster) { String errorMessage = "this action would add [" + newShards diff --git a/server/src/test/java/org/opensearch/indices/ShardLimitValidatorTests.java b/server/src/test/java/org/opensearch/indices/ShardLimitValidatorTests.java index 040632ea3ed8d..0b1ec8fd85ae5 100644 --- a/server/src/test/java/org/opensearch/indices/ShardLimitValidatorTests.java +++ b/server/src/test/java/org/opensearch/indices/ShardLimitValidatorTests.java @@ -214,6 +214,25 @@ public void testNonSystemIndexCreationFailsWithMaxShardLimitOnCluster() { ); } + public void testComputedMaxShardsOfClusterIntOverFlow() { + final int maxShardLimitPerNode = 500_000_000; + ClusterState state = createClusterForShardLimitTest(15, 1, 1); + Optional errorMessage = ShardLimitValidator.checkShardLimit(2, state, maxShardLimitPerNode, -1); + assertFalse(errorMessage.isPresent()); + + errorMessage = ShardLimitValidator.checkShardLimit(Integer.MAX_VALUE - 1, state, maxShardLimitPerNode, -1); + assertEquals( + "this action would add [" + + (Integer.MAX_VALUE - 1) + + "] total shards, but this cluster currently has [" + + 2 + + "]/[" + + Integer.MAX_VALUE + + "] maximum shards open", + errorMessage.get() + ); + } + public void testNonSystemIndexCreationPassesWithMaxShardLimitOnCluster() { final int maxShardLimitOnCluster = 5; Settings limitOnlySettings = Settings.builder()