Skip to content

Commit fc3c3b6

Browse files
committed
Rewrite IndexMetadata update
1 parent e125670 commit fc3c3b6

File tree

7 files changed

+41
-9
lines changed

7 files changed

+41
-9
lines changed

server/src/main/java/org/elasticsearch/TransportVersions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ static TransportVersion def(int id) {
202202
public static final TransportVersion RERANKER_FAILURES_ALLOWED = def(9_013_0_00);
203203
public static final TransportVersion VOYAGE_AI_INTEGRATION_ADDED = def(9_014_0_00);
204204
// making this large for now to avoid merge conflicts
205-
public static final TransportVersion UNASSIGENEDINFO_SHARD_ADDED = def(9_020_0_00);
205+
public static final TransportVersion UNASSIGENEDINFO_RESHARD_ADDED = def(9_020_0_00);
206206

207207
/*
208208
* STOP! READ THIS FIRST! No, really,

server/src/main/java/org/elasticsearch/action/admin/indices/autoshard/AutoshardIndexClusterStateUpdateRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import org.elasticsearch.action.support.ActiveShardCount;
1313

1414
/**
15-
* Cluster state update request that allows to re-shard an index
15+
* Cluster state update request that allows re-sharding an index
1616
* At the moment, we only have the ability to increment the number of shards
1717
* of an index (by a multiplicative factor).
1818
* We do not support removing shards from an index.

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,7 @@ public IndexMetadata withIncrementedPrimaryTerm(int shardId) {
949949
* Q: Is it ok to set routingNumShards to shardCount ?
950950
* Q: Should we increment this.version here ?
951951
*/
952+
/*
952953
public IndexMetadata withIncrementedPrimaryShards(int shardCount) {
953954
if (this.primaryTerms.length == shardCount) return this;
954955
@@ -1016,6 +1017,7 @@ public IndexMetadata withIncrementedPrimaryShards(int shardCount) {
10161017
this.shardSizeInBytesForecast
10171018
);
10181019
}
1020+
*/
10191021

10201022
/**
10211023
* @param timestampRange new @timestamp range
@@ -2031,6 +2033,34 @@ public Builder numberOfShards(int numberOfShards) {
20312033
return this;
20322034
}
20332035

2036+
/** Builder to create IndexMetadata that has an increased shard count (used for re-shard).
2037+
* The new shard count must be a multiple of the original shardcount.
2038+
* We do not support shrinking the shard count.
2039+
* @param shardCount updated shardCount
2040+
*/
2041+
public Builder reshardAddShards (int shardCount) {
2042+
// Assert routingNumShards is null ?
2043+
// Assert numberOfShards > 0
2044+
if (shardCount % numberOfShards() != 0) {
2045+
throw new IllegalArgumentException(
2046+
"New shard count ["
2047+
+ shardCount
2048+
+ "] should be a multiple"
2049+
+ " of current shard count ["
2050+
+ numberOfShards()
2051+
+ "] for ["
2052+
+ index
2053+
+ "]"
2054+
);
2055+
}
2056+
settings = Settings.builder().put(settings).put(SETTING_NUMBER_OF_SHARDS, shardCount).build();
2057+
var newPrimaryTerms = new long[shardCount];
2058+
System.arraycopy(primaryTerms, 0, newPrimaryTerms, 0, this.primaryTerms.length);
2059+
primaryTerms = newPrimaryTerms;
2060+
routingNumShards = shardCount;
2061+
return this;
2062+
}
2063+
20342064
/**
20352065
* Sets the number of shards that should be used for routing. This should only be used if the number of shards in
20362066
* an index has changed ie if the index is shrunk.

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,9 +2177,11 @@ public Builder updateNumberOfShards(final int numberOfShards, final String index
21772177
if (indexMetadata == null) {
21782178
throw new IndexNotFoundException(index);
21792179
}
2180-
IndexMetadata newIndexMetadata = indexMetadata.withIncrementedPrimaryShards(numberOfShards);
2181-
put(IndexMetadata.builder(newIndexMetadata).numberOfShards(numberOfShards));
2182-
2180+
// IndexMetadata newIndexMetadata = indexMetadata.withIncrementedPrimaryShards(numberOfShards);
2181+
// put(IndexMetadata.builder(newIndexMetadata).numberOfShards(numberOfShards));
2182+
put(IndexMetadata.builder(indexMetadata)
2183+
.settingsVersion(indexMetadata.getSettingsVersion() + 1)
2184+
.reshardAddShards(numberOfShards));
21832185
return this;
21842186
}
21852187

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ public Builder updateNumberOfShards(final int newShardCount, final String index)
482482
shardId,
483483
true,
484484
RecoverySource.EmptyStoreRecoverySource.INSTANCE,
485-
new UnassignedInfo(UnassignedInfo.Reason.SHARD_ADDED, null), // A new Reason needed in UnassignedInfo
485+
new UnassignedInfo(UnassignedInfo.Reason.RESHARD_ADDED, null), // A new Reason needed in UnassignedInfo
486486
ShardRouting.Role.INDEX_ONLY
487487
); // A new role API similar to newReplicaRole() needed in shardRoutingRoleStrategy ?
488488
IndexShardRoutingTable.Builder indexShardRoutingBuilder = IndexShardRoutingTable.builder(shardId);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public enum Reason {
179179
/**
180180
* New shard added as part of index re-sharding operation
181181
*/
182-
SHARD_ADDED
182+
RESHARD_ADDED
183183
}
184184

185185
/**
@@ -339,7 +339,7 @@ public void writeTo(StreamOutput out) throws IOException {
339339
out.writeByte((byte) Reason.NODE_LEFT.ordinal());
340340
} else if (reason.equals(Reason.UNPROMOTABLE_REPLICA) && out.getTransportVersion().before(VERSION_UNPROMOTABLE_REPLICA_ADDED)) {
341341
out.writeByte((byte) Reason.PRIMARY_FAILED.ordinal());
342-
} else if (reason.equals(Reason.SHARD_ADDED) && out.getTransportVersion().before(TransportVersions.UNASSIGENEDINFO_SHARD_ADDED)) {
342+
} else if (reason.equals(Reason.RESHARD_ADDED) && out.getTransportVersion().before(TransportVersions.UNASSIGENEDINFO_RESHARD_ADDED)) {
343343
out.writeByte((byte) Reason.FORCED_EMPTY_PRIMARY.ordinal());
344344
} else {
345345
out.writeByte((byte) reason.ordinal());

server/src/test/java/org/elasticsearch/cluster/routing/UnassignedInfoTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void testReasonOrdinalOrder() {
8686
UnassignedInfo.Reason.INDEX_CLOSED,
8787
UnassignedInfo.Reason.NODE_RESTARTING,
8888
UnassignedInfo.Reason.UNPROMOTABLE_REPLICA,
89-
UnassignedInfo.Reason.SHARD_ADDED };
89+
UnassignedInfo.Reason.RESHARD_ADDED };
9090
for (int i = 0; i < order.length; i++) {
9191
assertThat(order[i].ordinal(), equalTo(i));
9292
}

0 commit comments

Comments
 (0)