Skip to content

Commit 91d747d

Browse files
committed
Update Routing table builder
1 parent 8e2efee commit 91d747d

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,23 @@ public ClusterState applyAutoshardIndexRequest(
146146
final String indexName = request.index();
147147
final IndexMetadata sourceMetadata = currentState.metadata().index(indexName);
148148
int sourceNumShards = sourceMetadata.getNumberOfShards();
149+
int targetNumShards = sourceNumShards * 2;
149150

150151
Settings.Builder settingsBuilder = Settings.builder().put(sourceMetadata.getSettings());
151152
settingsBuilder.remove(IndexMetadata.SETTING_NUMBER_OF_SHARDS);
152-
settingsBuilder.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, sourceNumShards * 2);
153+
settingsBuilder.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, targetNumShards);
153154

154155
final Map<Index, Settings> updates = Maps.newHashMapWithExpectedSize(1);
155156
updates.put(sourceMetadata.getIndex(), settingsBuilder.build());
156157
final Metadata newMetadata = currentState.metadata().withIndexSettingsUpdates(updates);
157158

159+
/*
158160
var routingTableBuilder = RoutingTable.builder(allocationService.getShardRoutingRoleStrategy(), currentState.routingTable())
159161
.remove(indexName)
160162
.addAsNew(newMetadata.index(indexName));
163+
*/
164+
165+
var routingTableBuilder = RoutingTable.builder().updateNumberOfShards(targetNumShards, indexName);
161166

162167
ClusterState updated = ClusterState.builder(currentState)
163168
.incrementVersion()

server/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

server/src/main/java/org/elasticsearch/cluster/routing/UnassignedInfo.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,11 @@ public enum Reason {
175175
/**
176176
* Replica is unpromotable and the primary failed.
177177
*/
178-
UNPROMOTABLE_REPLICA
178+
UNPROMOTABLE_REPLICA,
179+
/**
180+
* New shard added as part of index resize (auto sharding)
181+
*/
182+
SHARD_ADDED
179183
}
180184

181185
/**

0 commit comments

Comments
 (0)