Skip to content

Commit ad95677

Browse files
kkewweiharshavamsi
authored andcommitted
Fix the computed max shards of cluster to avoid int overflow (opensearch-project#14155)
Signed-off-by: kkewwei <kkewwei@163.com>
1 parent e57b73b commit ad95677

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5555
### Fixed
5656
- Fix handling of Short and Byte data types in ScriptProcessor ingest pipeline ([#14379](https://github.com/opensearch-project/OpenSearch/issues/14379))
5757
- Switch to iterative version of WKT format parser ([#14086](https://github.com/opensearch-project/OpenSearch/pull/14086))
58+
- Fix the computed max shards of cluster to avoid int overflow ([#14155](https://github.com/opensearch-project/OpenSearch/pull/14155))
5859

5960
### Security
6061

server/src/main/java/org/opensearch/indices/ShardLimitValidator.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,15 @@ static Optional<String> checkShardLimit(
261261
return Optional.empty();
262262
}
263263

264+
int computedMaxShards = (int) Math.min(Integer.MAX_VALUE, (long) maxShardsPerNodeSetting * nodeCount);
264265
int maxShardsInCluster = maxShardsPerClusterSetting;
265266
if (maxShardsInCluster == -1) {
266-
maxShardsInCluster = maxShardsPerNodeSetting * nodeCount;
267+
maxShardsInCluster = computedMaxShards;
267268
} else {
268-
maxShardsInCluster = Math.min(maxShardsInCluster, maxShardsPerNodeSetting * nodeCount);
269+
maxShardsInCluster = Math.min(maxShardsInCluster, computedMaxShards);
269270
}
270271

271-
int currentOpenShards = state.getMetadata().getTotalOpenIndexShards();
272+
long currentOpenShards = state.getMetadata().getTotalOpenIndexShards();
272273
if ((currentOpenShards + newShards) > maxShardsInCluster) {
273274
String errorMessage = "this action would add ["
274275
+ newShards

server/src/test/java/org/opensearch/indices/ShardLimitValidatorTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,25 @@ public void testNonSystemIndexCreationFailsWithMaxShardLimitOnCluster() {
214214
);
215215
}
216216

217+
public void testComputedMaxShardsOfClusterIntOverFlow() {
218+
final int maxShardLimitPerNode = 500_000_000;
219+
ClusterState state = createClusterForShardLimitTest(15, 1, 1);
220+
Optional<String> errorMessage = ShardLimitValidator.checkShardLimit(2, state, maxShardLimitPerNode, -1);
221+
assertFalse(errorMessage.isPresent());
222+
223+
errorMessage = ShardLimitValidator.checkShardLimit(Integer.MAX_VALUE - 1, state, maxShardLimitPerNode, -1);
224+
assertEquals(
225+
"this action would add ["
226+
+ (Integer.MAX_VALUE - 1)
227+
+ "] total shards, but this cluster currently has ["
228+
+ 2
229+
+ "]/["
230+
+ Integer.MAX_VALUE
231+
+ "] maximum shards open",
232+
errorMessage.get()
233+
);
234+
}
235+
217236
public void testNonSystemIndexCreationPassesWithMaxShardLimitOnCluster() {
218237
final int maxShardLimitOnCluster = 5;
219238
Settings limitOnlySettings = Settings.builder()

0 commit comments

Comments
 (0)