Skip to content

Commit

Permalink
Fix the computed max shards of cluster to avoid int overflow (opensea…
Browse files Browse the repository at this point in the history
…rch-project#14155)

Signed-off-by: kkewwei <[email protected]>
  • Loading branch information
kkewwei authored and harshavamsi committed Jul 12, 2024
1 parent e57b73b commit ad95677
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Fixed
- Fix handling of Short and Byte data types in ScriptProcessor ingest pipeline ([#14379](https://github.com/opensearch-project/OpenSearch/issues/14379))
- Switch to iterative version of WKT format parser ([#14086](https://github.com/opensearch-project/OpenSearch/pull/14086))
- Fix the computed max shards of cluster to avoid int overflow ([#14155](https://github.com/opensearch-project/OpenSearch/pull/14155))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,15 @@ static Optional<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,25 @@ public void testNonSystemIndexCreationFailsWithMaxShardLimitOnCluster() {
);
}

public void testComputedMaxShardsOfClusterIntOverFlow() {
final int maxShardLimitPerNode = 500_000_000;
ClusterState state = createClusterForShardLimitTest(15, 1, 1);
Optional<String> 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()
Expand Down

0 comments on commit ad95677

Please sign in to comment.