diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexReshardingMetadata.java b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexReshardingMetadata.java index 2c00c7d976a8e..4d0c2bcb43912 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexReshardingMetadata.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexReshardingMetadata.java @@ -205,10 +205,27 @@ public static IndexReshardingMetadata newSplitByMultiple(int shardCount, int mul return new IndexReshardingMetadata(IndexReshardingState.Split.newSplitByMultiple(shardCount, multiple)); } + /** + * @return the split state of this metadata block, or throw IllegalArgumentException if this metadata doesn't represent a split + */ public IndexReshardingState.Split getSplit() { return switch (state) { case IndexReshardingState.Noop ignored -> throw new IllegalArgumentException("resharding metadata is not a split"); case IndexReshardingState.Split s -> s; }; } + + /** + * @return the number of shards the index has at the start of this operation + */ + public int shardCountBefore() { + return state.shardCountBefore(); + } + + /** + * @return the number of shards that the index will have when resharding completes + */ + public int shardCountAfter() { + return state.shardCountAfter(); + } } diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexReshardingState.java b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexReshardingState.java index c89417ccdcc0f..ce71a4ed6a417 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexReshardingState.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexReshardingState.java @@ -29,6 +29,16 @@ * concrete subclasses for the operations that are currently defined (which is only split for now). */ public abstract sealed class IndexReshardingState implements Writeable, ToXContentFragment { + /** + * @return the number of shards the index has at the start of this operation + */ + public abstract int shardCountBefore(); + + /** + * @return the number of shards that the index will have when resharding completes + */ + public abstract int shardCountAfter(); + // This class exists only so that tests can check that IndexReshardingMetadata can support more than one kind of operation. // When we have another real operation such as Shrink this can be removed. public static final class Noop extends IndexReshardingState { @@ -64,6 +74,16 @@ public boolean equals(Object other) { public int hashCode() { return 0; } + + @Override + public int shardCountBefore() { + return 1; + } + + @Override + public int shardCountAfter() { + return 1; + } } public static final class Split extends IndexReshardingState { @@ -210,13 +230,13 @@ public int hashCode() { return Objects.hash(Arrays.hashCode(sourceShards), Arrays.hashCode(targetShards)); } - // visible for testing - int oldShardCount() { + @Override + public int shardCountBefore() { return oldShardCount; } - // visible for testing - int newShardCount() { + @Override + public int shardCountAfter() { return newShardCount; } diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/IndexReshardingMetadataTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/IndexReshardingMetadataTests.java index 3f3acb0b9aa2c..8ec3b6e67860d 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/IndexReshardingMetadataTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/IndexReshardingMetadataTests.java @@ -21,8 +21,8 @@ public void testSplit() { var split = metadata.getSplit(); // starting state is as expected - assert split.oldShardCount() == numShards; - assert split.newShardCount() == numShards * multiple; + assert metadata.shardCountBefore() == numShards; + assert metadata.shardCountAfter() == numShards * multiple; for (int i = 0; i < numShards; i++) { assert split.getSourceShardState(i) == IndexReshardingState.Split.SourceShardState.SOURCE; }