Skip to content

Commit 9866a67

Browse files
Add shardCountBefore/After operations to IndexReshardingMetadata (#125058)
* Add shardCountBefore/After operations to IndexReshardingMetadata Any reshard operation will change shard count, so it makes sense to make this information available here. * [CI] Auto commit changes from spotless --------- Co-authored-by: elasticsearchmachine <[email protected]>
1 parent 0e42914 commit 9866a67

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,27 @@ public static IndexReshardingMetadata newSplitByMultiple(int shardCount, int mul
205205
return new IndexReshardingMetadata(IndexReshardingState.Split.newSplitByMultiple(shardCount, multiple));
206206
}
207207

208+
/**
209+
* @return the split state of this metadata block, or throw IllegalArgumentException if this metadata doesn't represent a split
210+
*/
208211
public IndexReshardingState.Split getSplit() {
209212
return switch (state) {
210213
case IndexReshardingState.Noop ignored -> throw new IllegalArgumentException("resharding metadata is not a split");
211214
case IndexReshardingState.Split s -> s;
212215
};
213216
}
217+
218+
/**
219+
* @return the number of shards the index has at the start of this operation
220+
*/
221+
public int shardCountBefore() {
222+
return state.shardCountBefore();
223+
}
224+
225+
/**
226+
* @return the number of shards that the index will have when resharding completes
227+
*/
228+
public int shardCountAfter() {
229+
return state.shardCountAfter();
230+
}
214231
}

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
* concrete subclasses for the operations that are currently defined (which is only split for now).
3030
*/
3131
public abstract sealed class IndexReshardingState implements Writeable, ToXContentFragment {
32+
/**
33+
* @return the number of shards the index has at the start of this operation
34+
*/
35+
public abstract int shardCountBefore();
36+
37+
/**
38+
* @return the number of shards that the index will have when resharding completes
39+
*/
40+
public abstract int shardCountAfter();
41+
3242
// This class exists only so that tests can check that IndexReshardingMetadata can support more than one kind of operation.
3343
// When we have another real operation such as Shrink this can be removed.
3444
public static final class Noop extends IndexReshardingState {
@@ -64,6 +74,16 @@ public boolean equals(Object other) {
6474
public int hashCode() {
6575
return 0;
6676
}
77+
78+
@Override
79+
public int shardCountBefore() {
80+
return 1;
81+
}
82+
83+
@Override
84+
public int shardCountAfter() {
85+
return 1;
86+
}
6787
}
6888

6989
public static final class Split extends IndexReshardingState {
@@ -210,13 +230,13 @@ public int hashCode() {
210230
return Objects.hash(Arrays.hashCode(sourceShards), Arrays.hashCode(targetShards));
211231
}
212232

213-
// visible for testing
214-
int oldShardCount() {
233+
@Override
234+
public int shardCountBefore() {
215235
return oldShardCount;
216236
}
217237

218-
// visible for testing
219-
int newShardCount() {
238+
@Override
239+
public int shardCountAfter() {
220240
return newShardCount;
221241
}
222242

server/src/test/java/org/elasticsearch/cluster/metadata/IndexReshardingMetadataTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public void testSplit() {
2121
var split = metadata.getSplit();
2222

2323
// starting state is as expected
24-
assert split.oldShardCount() == numShards;
25-
assert split.newShardCount() == numShards * multiple;
24+
assert metadata.shardCountBefore() == numShards;
25+
assert metadata.shardCountAfter() == numShards * multiple;
2626
for (int i = 0; i < numShards; i++) {
2727
assert split.getSourceShardState(i) == IndexReshardingState.Split.SourceShardState.SOURCE;
2828
}

0 commit comments

Comments
 (0)