Skip to content

Commit 7444b2d

Browse files
ankikumaywangd
authored andcommitted
Calculate routing num shards correctly during reshard (elastic#125601)
1 parent c27d233 commit 7444b2d

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/IndexMetadata.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,34 +1998,31 @@ public Builder numberOfShards(int numberOfShards) {
19981998

19991999
/**
20002000
* Builder to create IndexMetadata that has an increased shard count (used for re-shard).
2001-
* The new shard count must be a multiple of the original shardcount.
2001+
* The new shard count must be a multiple of the original shardcount as well as a factor
2002+
* of routingNumShards.
20022003
* We do not support shrinking the shard count.
2003-
* @param shardCount updated shardCount
2004-
*
2005-
* TODO: Check if this.version needs to be incremented
2004+
* @param targetShardCount target shard count after resharding
20062005
*/
2007-
public Builder reshardAddShards(int shardCount) {
2008-
// Assert routingNumShards is null ?
2009-
// Assert numberOfShards > 0
2010-
if (shardCount % numberOfShards() != 0) {
2006+
public Builder reshardAddShards(int targetShardCount) {
2007+
final int sourceNumShards = numberOfShards();
2008+
if (targetShardCount % sourceNumShards != 0) {
20112009
throw new IllegalArgumentException(
20122010
"New shard count ["
2013-
+ shardCount
2011+
+ targetShardCount
20142012
+ "] should be a multiple"
20152013
+ " of current shard count ["
2016-
+ numberOfShards()
2014+
+ sourceNumShards
20172015
+ "] for ["
20182016
+ index
20192017
+ "]"
20202018
);
20212019
}
2022-
IndexVersion indexVersionCreated = indexCreatedVersion(settings);
2023-
settings = Settings.builder().put(settings).put(SETTING_NUMBER_OF_SHARDS, shardCount).build();
2024-
var newPrimaryTerms = new long[shardCount];
2020+
settings = Settings.builder().put(settings).put(SETTING_NUMBER_OF_SHARDS, targetShardCount).build();
2021+
var newPrimaryTerms = new long[targetShardCount];
20252022
Arrays.fill(newPrimaryTerms, this.primaryTerms.length, newPrimaryTerms.length, SequenceNumbers.UNASSIGNED_PRIMARY_TERM);
20262023
System.arraycopy(primaryTerms, 0, newPrimaryTerms, 0, this.primaryTerms.length);
20272024
primaryTerms = newPrimaryTerms;
2028-
routingNumShards = MetadataCreateIndexService.calculateNumRoutingShards(shardCount, indexVersionCreated);
2025+
routingNumShards = MetadataCreateIndexService.getIndexNumberOfRoutingShards(settings, sourceNumShards, this.routingNumShards);
20292026
return this;
20302027
}
20312028

@@ -3034,7 +3031,7 @@ public static ShardId selectCloneShard(int shardId, IndexMetadata sourceIndexMet
30343031
return new ShardId(sourceIndexMetadata.getIndex(), shardId);
30353032
}
30363033

3037-
private static void assertSplitMetadata(int numSourceShards, int numTargetShards, IndexMetadata sourceIndexMetadata) {
3034+
public static void assertSplitMetadata(int numSourceShards, int numTargetShards, IndexMetadata sourceIndexMetadata) {
30383035
if (numSourceShards > numTargetShards) {
30393036
throw new IllegalArgumentException(
30403037
"the number of source shards ["

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,10 +1282,23 @@ private static void validateSoftDeleteSettings(Settings indexSettings) {
12821282
* it will return the value configured for that index.
12831283
*/
12841284
static int getIndexNumberOfRoutingShards(Settings indexSettings, @Nullable IndexMetadata sourceMetadata) {
1285+
final int routingNumShards = getIndexNumberOfRoutingShards(
1286+
indexSettings,
1287+
sourceMetadata == null ? 1 : sourceMetadata.getNumberOfShards(),
1288+
sourceMetadata == null ? 0 : sourceMetadata.getRoutingNumShards()
1289+
);
1290+
return routingNumShards;
1291+
}
1292+
1293+
/**
1294+
* Calculates the number of routing shards based on the configured value in indexSettings or if recovering from another index
1295+
* it will return the value configured for that index.
1296+
*/
1297+
static int getIndexNumberOfRoutingShards(Settings indexSettings, final int sourceNumShards, final int sourceRoutingNumShards) {
12851298
final int numTargetShards = INDEX_NUMBER_OF_SHARDS_SETTING.get(indexSettings);
12861299
final IndexVersion indexVersionCreated = IndexMetadata.SETTING_INDEX_VERSION_CREATED.get(indexSettings);
12871300
final int routingNumShards;
1288-
if (sourceMetadata == null || sourceMetadata.getNumberOfShards() == 1) {
1301+
if (sourceNumShards == 1) {
12891302
// in this case we either have no index to recover from or
12901303
// we have a source index with 1 shard and without an explicit split factor
12911304
// or one that is valid in that case we can split into whatever and auto-generate a new factor.
@@ -1299,7 +1312,7 @@ static int getIndexNumberOfRoutingShards(Settings indexSettings, @Nullable Index
12991312
} else {
13001313
assert IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.exists(indexSettings) == false
13011314
: "index.number_of_routing_shards should not be present on the target index on resize";
1302-
routingNumShards = sourceMetadata.getRoutingNumShards();
1315+
routingNumShards = sourceRoutingNumShards;
13031316
}
13041317
return routingNumShards;
13051318
}

0 commit comments

Comments
 (0)