@@ -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 ["
0 commit comments