@@ -449,7 +449,8 @@ private static void addShard(
449449 .addShard (shardRoutingEntry );
450450 }
451451
452- public Builder updateNumberOfShards (final int numberOfShards , final String index ) {
452+ /* Update the number of shards for an existing index in serverless */
453+ public Builder updateNumberOfShards (final int newShardCount , final String index ) {
453454 if (indicesRouting == null ) {
454455 throw new IllegalStateException ("once build is called the builder cannot be reused" );
455456 }
@@ -458,15 +459,33 @@ public Builder updateNumberOfShards(final int numberOfShards, final String index
458459 // ignore index missing failure, its closed...
459460 return this ;
460461 }
462+ // Replica count (should be 0 for serverless)
461463 int currentNumberOfReplicas = indexRoutingTable .shard (0 ).size () - 1 ; // remove the required primary
462- int numberOfOldShards = indexRoutingTable .size ();
464+ int oldShardCount = indexRoutingTable .size ();
465+ assert (newShardCount % oldShardCount == 0 ) : "New shard count must be multiple of old shard count" ;
463466 IndexRoutingTable .Builder builder = new IndexRoutingTable .Builder (shardRoutingRoleStrategy , indexRoutingTable .getIndex ());
467+ builder .ensureShardArray (newShardCount );
468+
464469 // re-add existing shards
465- builder .ensureShardArray (numberOfOldShards );
466- for (int i = 0 ; i < numberOfOldShards ; i ++) {
470+ for (int i = 0 ; i < oldShardCount ; i ++) {
467471 builder .addIndexShard (new IndexShardRoutingTable .Builder (indexRoutingTable .shard (i )));
468472 }
469- // See addReplica and addIndexShard to create the new shards
473+
474+ int numNewShards = newShardCount - oldShardCount ;
475+ // Add new shards
476+ for (int i = 0 ; i < numNewShards ; i ++) {
477+ ShardId shardId = new ShardId (indexRoutingTable .getIndex (), oldShardCount + i );
478+ ShardRouting shardRouting = ShardRouting .newUnassigned (
479+ shardId ,
480+ true ,
481+ RecoverySource .EmptyStoreRecoverySource .INSTANCE ,
482+ new UnassignedInfo (UnassignedInfo .Reason .SHARD_ADDED , null ), // A new Reason needed in UnassignedInfo
483+ ShardRouting .Role .INDEX_ONLY ); // A new role API similar to newReplicaRole() needed in shardRoutingRoleStrategy ?
484+ IndexShardRoutingTable .Builder indexShardRoutingBuilder = IndexShardRoutingTable .builder (shardId );
485+ indexShardRoutingBuilder .addShard (shardRouting );
486+ builder .addIndexShard (indexShardRoutingBuilder );
487+ }
488+ // No need to add replicas because no replicas on serverless.
470489 indicesRouting .put (index , builder .build ());
471490 return this ;
472491 }
0 commit comments