diff --git a/CHANGELOG.md b/CHANGELOG.md index 5171b9289b81b..398cf360c2ce8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Change the default value of doc_values in WildcardFieldMapper to true. ([#19796](https://github.com/opensearch-project/OpenSearch/pull/19796)) - Make Engine#loadHistoryUUID() protected and Origin#isFromTranslog() public ([#19753](https://github.com/opensearch-project/OpenSearch/pull/19752)) - Bump opensearch-protobufs dependency to 0.23.0 and update transport-grpc module compatibility ([#19831](https://github.com/opensearch-project/OpenSearch/pull/19831)) +- Refactor the DocStats and StoreStats class to use the Builder pattern instead of constructors ([#19863](https://github.com/opensearch-project/OpenSearch/pull/19863)) ### Fixed - Fix Allocation and Rebalance Constraints of WeightFunction are incorrectly reset ([#19012](https://github.com/opensearch-project/OpenSearch/pull/19012)) @@ -74,6 +75,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Deprecated - Deprecated existing constructors in ThreadPoolStats.Stats in favor of the new Builder ([#19317](https://github.com/opensearch-project/OpenSearch/pull/19317)) - Deprecated existing constructors in IndexingStats.Stats in favor of the new Builder ([#19306](https://github.com/opensearch-project/OpenSearch/pull/19306)) +- Deprecated existing constructors in DocStats and StoreStats in favor of the new Builder ([#19863](https://github.com/opensearch-project/OpenSearch/pull/19863)) ### Removed diff --git a/modules/store-subdirectory/src/test/java/org/opensearch/plugin/store/subdirectory/SubdirectoryStorePluginTests.java b/modules/store-subdirectory/src/test/java/org/opensearch/plugin/store/subdirectory/SubdirectoryStorePluginTests.java index 51fe5dfa019fd..b7f2f54a09ff2 100644 --- a/modules/store-subdirectory/src/test/java/org/opensearch/plugin/store/subdirectory/SubdirectoryStorePluginTests.java +++ b/modules/store-subdirectory/src/test/java/org/opensearch/plugin/store/subdirectory/SubdirectoryStorePluginTests.java @@ -87,7 +87,7 @@ public void testStats() throws IOException { final long otherStatsBytes = randomLongBetween(0L, Integer.MAX_VALUE); final long otherStatsReservedBytes = randomBoolean() ? StoreStats.UNKNOWN_RESERVED_BYTES : randomLongBetween(0L, Integer.MAX_VALUE); - stats.add(new StoreStats(otherStatsBytes, otherStatsReservedBytes)); + stats.add(new StoreStats.Builder().sizeInBytes(otherStatsBytes).reservedSize(otherStatsReservedBytes).build()); assertEquals(initialStoreSize + otherStatsBytes, stats.getSize().getBytes()); assertEquals(Math.max(reservedBytes, 0L) + Math.max(otherStatsReservedBytes, 0L), stats.getReservedSize().getBytes()); diff --git a/server/src/main/java/org/opensearch/index/engine/Engine.java b/server/src/main/java/org/opensearch/index/engine/Engine.java index 0acd2a5cd9d47..10e1c98b43858 100644 --- a/server/src/main/java/org/opensearch/index/engine/Engine.java +++ b/server/src/main/java/org/opensearch/index/engine/Engine.java @@ -275,7 +275,7 @@ protected final DocsStats docsStats(IndexReader indexReader) { logger.trace(() -> new ParameterizedMessage("failed to get size for [{}]", info.info.name), e); } } - return new DocsStats(numDocs, numDeletedDocs, sizeInBytes); + return new DocsStats.Builder().count(numDocs).deleted(numDeletedDocs).totalSizeInBytes(sizeInBytes).build(); } /** diff --git a/server/src/main/java/org/opensearch/index/shard/DocsStats.java b/server/src/main/java/org/opensearch/index/shard/DocsStats.java index 4ca475a45c04b..50e7299f4f7fa 100644 --- a/server/src/main/java/org/opensearch/index/shard/DocsStats.java +++ b/server/src/main/java/org/opensearch/index/shard/DocsStats.java @@ -58,12 +58,28 @@ public DocsStats() { } + /** + * Private constructor that takes a builder. + * This is the sole entry point for creating a new DocsStats object. + * @param builder The builder instance containing all the values. + */ + private DocsStats(Builder builder) { + this.count = builder.count; + this.deleted = builder.deleted; + this.totalSizeInBytes = builder.totalSizeInBytes; + } + public DocsStats(StreamInput in) throws IOException { count = in.readVLong(); deleted = in.readVLong(); totalSizeInBytes = in.readVLong(); } + /** + * This constructor will be deprecated starting in version 3.4.0. + * Use {@link Builder} instead. + */ + @Deprecated public DocsStats(long count, long deleted, long totalSizeInBytes) { this.count = count; this.deleted = deleted; @@ -107,6 +123,41 @@ public long getAverageSizeInBytes() { return totalDocs == 0 ? 0 : totalSizeInBytes / totalDocs; } + /** + * Builder for the {@link DocsStats} class. + * Provides a fluent API for constructing a DocsStats object. + */ + public static class Builder { + private long count = 0; + private long deleted = 0; + private long totalSizeInBytes = 0; + + public Builder() {} + + public Builder count(long count) { + this.count = count; + return this; + } + + public Builder deleted(long deleted) { + this.deleted = deleted; + return this; + } + + public Builder totalSizeInBytes(long totalSizeInBytes) { + this.totalSizeInBytes = totalSizeInBytes; + return this; + } + + /** + * Creates a {@link DocsStats} object from the builder's current state. + * @return A new DocsStats instance. + */ + public DocsStats build() { + return new DocsStats(this); + } + } + @Override public void writeTo(StreamOutput out) throws IOException { out.writeVLong(count); diff --git a/server/src/main/java/org/opensearch/index/store/Store.java b/server/src/main/java/org/opensearch/index/store/Store.java index 0173d2faa46ca..4f8f69da44bd4 100644 --- a/server/src/main/java/org/opensearch/index/store/Store.java +++ b/server/src/main/java/org/opensearch/index/store/Store.java @@ -469,7 +469,7 @@ public CheckIndex.Status checkIndex(PrintStream out) throws IOException { */ public StoreStats stats(long reservedBytes) throws IOException { ensureOpen(); - return new StoreStats(directory.estimateSize(), reservedBytes); + return new StoreStats.Builder().sizeInBytes(directory.estimateSize()).reservedSize(reservedBytes).build(); } /** diff --git a/server/src/main/java/org/opensearch/index/store/StoreStats.java b/server/src/main/java/org/opensearch/index/store/StoreStats.java index 4763b5e5e8a21..16d7468965422 100644 --- a/server/src/main/java/org/opensearch/index/store/StoreStats.java +++ b/server/src/main/java/org/opensearch/index/store/StoreStats.java @@ -62,15 +62,26 @@ public StoreStats() { } + /** + * Private constructor that takes a builder. + * This is the sole entry point for creating a new StoreStats object. + * @param builder The builder instance containing all the values. + */ + private StoreStats(Builder builder) { + this.sizeInBytes = builder.sizeInBytes; + this.reservedSize = builder.reservedSize; + } + public StoreStats(StreamInput in) throws IOException { sizeInBytes = in.readVLong(); reservedSize = in.readZLong(); } /** - * @param sizeInBytes the size of the store in bytes - * @param reservedSize a prediction of how much larger the store is expected to grow, or {@link StoreStats#UNKNOWN_RESERVED_BYTES}. - */ + * This constructor will be deprecated starting in version 3.4.0. + * Use {@link Builder} instead. + */ + @Deprecated public StoreStats(long sizeInBytes, long reservedSize) { assert reservedSize == UNKNOWN_RESERVED_BYTES || reservedSize >= 0 : reservedSize; this.sizeInBytes = sizeInBytes; @@ -114,6 +125,42 @@ public ByteSizeValue getReservedSize() { return new ByteSizeValue(reservedSize); } + /** + * Builder for the {@link StoreStats} class. + * Provides a fluent API for constructing a StoreStats object. + */ + public static class Builder { + private long sizeInBytes = 0; + private long reservedSize = 0; + + public Builder() {} + + /** + * @param bytes the size of the store in bytes + */ + public Builder sizeInBytes(long bytes) { + this.sizeInBytes = bytes; + return this; + } + + /** + * @param size a prediction of how much larger the store is expected to grow, or {@link StoreStats#UNKNOWN_RESERVED_BYTES}. + */ + public Builder reservedSize(long size) { + assert size == UNKNOWN_RESERVED_BYTES || size >= 0 : size; + this.reservedSize = size; + return this; + } + + /** + * Creates a {@link StoreStats} object from the builder's current state. + * @return A new StoreStats instance. + */ + public StoreStats build() { + return new StoreStats(this); + } + } + @Override public void writeTo(StreamOutput out) throws IOException { out.writeVLong(sizeInBytes); diff --git a/server/src/test/java/org/opensearch/action/admin/cluster/node/stats/NodeStatsTests.java b/server/src/test/java/org/opensearch/action/admin/cluster/node/stats/NodeStatsTests.java index 58b21a1e1b270..c21c015c5bea0 100644 --- a/server/src/test/java/org/opensearch/action/admin/cluster/node/stats/NodeStatsTests.java +++ b/server/src/test/java/org/opensearch/action/admin/cluster/node/stats/NodeStatsTests.java @@ -1147,10 +1147,10 @@ public void testOldVersionNodes() throws IOException { long numDeletedDocs = randomLongBetween(0, 100); CommonStats commonStats = new CommonStats(CommonStatsFlags.NONE); - commonStats.docs = new DocsStats(numDocs, numDeletedDocs, 0); - commonStats.store = new StoreStats(100, 0L); + commonStats.docs = new DocsStats.Builder().count(numDocs).deleted(numDeletedDocs).totalSizeInBytes(0).build(); + commonStats.store = new StoreStats.Builder().sizeInBytes(100).reservedSize(0L).build(); commonStats.indexing = new IndexingStats(); - DocsStats hostDocStats = new DocsStats(numDocs, numDeletedDocs, 0); + DocsStats hostDocStats = new DocsStats.Builder().count(numDocs).deleted(numDeletedDocs).totalSizeInBytes(0).build(); CommonStatsFlags commonStatsFlags = new CommonStatsFlags(); commonStatsFlags.clear(); @@ -1193,8 +1193,8 @@ public void testNodeIndicesStatsSerialization() throws IOException { levelParams.add(NodeIndicesStats.StatsLevel.NODE); CommonStats commonStats = new CommonStats(CommonStatsFlags.NONE); - commonStats.docs = new DocsStats(numDocs, numDeletedDocs, 0); - commonStats.store = new StoreStats(100, 0L); + commonStats.docs = new DocsStats.Builder().count(numDocs).deleted(numDeletedDocs).totalSizeInBytes(0).build(); + commonStats.store = new StoreStats.Builder().sizeInBytes(100).reservedSize(0L).build(); commonStats.indexing = new IndexingStats(); CommonStatsFlags commonStatsFlags = new CommonStatsFlags(); @@ -1244,8 +1244,8 @@ public void testNodeIndicesStatsToXContent() { levelParams.add(NodeIndicesStats.StatsLevel.NODE); CommonStats commonStats = new CommonStats(CommonStatsFlags.NONE); - commonStats.docs = new DocsStats(numDocs, numDeletedDocs, 0); - commonStats.store = new StoreStats(100, 0L); + commonStats.docs = new DocsStats.Builder().count(numDocs).deleted(numDeletedDocs).totalSizeInBytes(0).build(); + commonStats.store = new StoreStats.Builder().sizeInBytes(100).reservedSize(0L).build(); commonStats.indexing = new IndexingStats(); CommonStatsFlags commonStatsFlags = new CommonStatsFlags(); @@ -1364,8 +1364,13 @@ public void testNodeIndicesStatsWithAndWithoutAggregations() throws IOException private CommonStats createRandomCommonStats() { CommonStats commonStats = new CommonStats(CommonStatsFlags.NONE); - commonStats.docs = new DocsStats(randomLongBetween(0, 10000), randomLongBetween(0, 100), randomLongBetween(0, 1000)); - commonStats.store = new StoreStats(randomLongBetween(0, 100), randomLongBetween(0, 1000)); + commonStats.docs = new DocsStats.Builder().count(randomLongBetween(0, 10000)) + .deleted(randomLongBetween(0, 100)) + .totalSizeInBytes(randomLongBetween(0, 1000)) + .build(); + commonStats.store = new StoreStats.Builder().sizeInBytes(randomLongBetween(0, 100)) + .reservedSize(randomLongBetween(0, 1000)) + .build(); commonStats.indexing = new IndexingStats(); commonStats.completion = new CompletionStats(); commonStats.flush = new FlushStats(randomLongBetween(0, 100), randomLongBetween(0, 100), randomLongBetween(0, 100)); diff --git a/server/src/test/java/org/opensearch/action/admin/cluster/stats/ClusterStatsNodesTests.java b/server/src/test/java/org/opensearch/action/admin/cluster/stats/ClusterStatsNodesTests.java index 8661bd89b2ac0..4498d4c73912c 100644 --- a/server/src/test/java/org/opensearch/action/admin/cluster/stats/ClusterStatsNodesTests.java +++ b/server/src/test/java/org/opensearch/action/admin/cluster/stats/ClusterStatsNodesTests.java @@ -362,8 +362,13 @@ private ClusterStatsNodeResponse createClusterStatsNodeResponse( private CommonStats createRandomCommonStats() { CommonStats commonStats = new CommonStats(CommonStatsFlags.NONE); - commonStats.docs = new DocsStats(randomLongBetween(0, 10000), randomLongBetween(0, 100), randomLongBetween(0, 1000)); - commonStats.store = new StoreStats(randomLongBetween(0, 100), randomLongBetween(0, 1000)); + commonStats.docs = new DocsStats.Builder().count(randomLongBetween(0, 10000)) + .deleted(randomLongBetween(0, 100)) + .totalSizeInBytes(randomLongBetween(0, 1000)) + .build(); + commonStats.store = new StoreStats.Builder().sizeInBytes(randomLongBetween(0, 100)) + .reservedSize(randomLongBetween(0, 1000)) + .build(); commonStats.indexing = new IndexingStats(); commonStats.completion = new CompletionStats(); commonStats.flush = new FlushStats(randomLongBetween(0, 100), randomLongBetween(0, 100), randomLongBetween(0, 100)); diff --git a/server/src/test/java/org/opensearch/action/admin/cluster/stats/ClusterStatsResponseTests.java b/server/src/test/java/org/opensearch/action/admin/cluster/stats/ClusterStatsResponseTests.java index 193c9cc471f7b..c8ddd5c6b4eef 100644 --- a/server/src/test/java/org/opensearch/action/admin/cluster/stats/ClusterStatsResponseTests.java +++ b/server/src/test/java/org/opensearch/action/admin/cluster/stats/ClusterStatsResponseTests.java @@ -230,8 +230,13 @@ private ClusterStatsNodeResponse createClusterStatsNodeResponse(DiscoveryNode no private CommonStats createRandomCommonStats() { CommonStats commonStats = new CommonStats(CommonStatsFlags.NONE); - commonStats.docs = new DocsStats(randomLongBetween(0, 10000), randomLongBetween(0, 100), randomLongBetween(0, 1000)); - commonStats.store = new StoreStats(randomLongBetween(0, 100), randomLongBetween(0, 1000)); + commonStats.docs = new DocsStats.Builder().count(randomLongBetween(0, 10000)) + .deleted(randomLongBetween(0, 100)) + .totalSizeInBytes(randomLongBetween(0, 1000)) + .build(); + commonStats.store = new StoreStats.Builder().sizeInBytes(randomLongBetween(0, 100)) + .reservedSize(randomLongBetween(0, 1000)) + .build(); commonStats.indexing = new IndexingStats(); commonStats.completion = new CompletionStats(); commonStats.flush = new FlushStats(randomLongBetween(0, 100), randomLongBetween(0, 100), randomLongBetween(0, 100)); diff --git a/server/src/test/java/org/opensearch/action/admin/indices/rollover/TransportRolloverActionTests.java b/server/src/test/java/org/opensearch/action/admin/indices/rollover/TransportRolloverActionTests.java index 6ff7c3b722056..ce2fd26fe8c2e 100644 --- a/server/src/test/java/org/opensearch/action/admin/indices/rollover/TransportRolloverActionTests.java +++ b/server/src/test/java/org/opensearch/action/admin/indices/rollover/TransportRolloverActionTests.java @@ -148,7 +148,7 @@ public void testEvaluateConditions() { final Set> conditions = Sets.newHashSet(maxDocsCondition, maxAgeCondition, maxSizeCondition); Map results = evaluateConditions( conditions, - new DocsStats(matchMaxDocs, 0L, ByteSizeUnit.MB.toBytes(120)), + new DocsStats.Builder().count(matchMaxDocs).deleted(0L).totalSizeInBytes(ByteSizeUnit.MB.toBytes(120)).build(), metadata ); assertThat(results.size(), equalTo(3)); @@ -156,7 +156,11 @@ public void testEvaluateConditions() { assertThat(matched, equalTo(true)); } - results = evaluateConditions(conditions, new DocsStats(notMatchMaxDocs, 0, notMatchMaxSize.getBytes()), metadata); + results = evaluateConditions( + conditions, + new DocsStats.Builder().count(notMatchMaxDocs).deleted(0).totalSizeInBytes(notMatchMaxSize.getBytes()).build(), + metadata + ); assertThat(results.size(), equalTo(3)); for (Map.Entry entry : results.entrySet()) { if (entry.getKey().equals(maxAgeCondition.toString())) { @@ -211,7 +215,12 @@ public void testEvaluateWithoutMetadata() { long matchMaxDocs = randomIntBetween(100, 1000); final Set> conditions = Sets.newHashSet(maxDocsCondition, maxAgeCondition, maxSizeCondition); - Map results = evaluateConditions(conditions, new DocsStats(matchMaxDocs, 0L, ByteSizeUnit.MB.toBytes(120)), null); + + Map results = evaluateConditions( + conditions, + new DocsStats.Builder().count(matchMaxDocs).deleted(0L).totalSizeInBytes(ByteSizeUnit.MB.toBytes(120)).build(), + null + ); assertThat(results.size(), equalTo(3)); results.forEach((k, v) -> assertFalse(v)); @@ -390,10 +399,14 @@ public void testResolveIndices() { private IndicesStatsResponse createIndicesStatResponse(String indexName, long totalDocs, long primariesDocs) { final CommonStats primaryStats = mock(CommonStats.class); - when(primaryStats.getDocs()).thenReturn(new DocsStats(primariesDocs, 0, between(1, 10000))); + when(primaryStats.getDocs()).thenReturn( + new DocsStats.Builder().count(primariesDocs).deleted(0).totalSizeInBytes(between(1, 10000)).build() + ); final CommonStats totalStats = mock(CommonStats.class); - when(totalStats.getDocs()).thenReturn(new DocsStats(totalDocs, 0, between(1, 10000))); + when(totalStats.getDocs()).thenReturn( + new DocsStats.Builder().count(totalDocs).deleted(0).totalSizeInBytes(between(1, 10000)).build() + ); final IndicesStatsResponse response = mock(IndicesStatsResponse.class); when(response.getPrimaries()).thenReturn(primaryStats); @@ -422,10 +435,14 @@ private IndicesStatsResponse createAliasToMultipleIndicesStatsResponse(Map TransportResizeAction.prepareCreateIndexRequest( new ResizeRequest("target", "source"), state, - (i) -> new DocsStats(Integer.MAX_VALUE, between(1, 1000), between(1, 100)), - new StoreStats(between(1, 10000), between(1, 10000)), + (i) -> new DocsStats.Builder().count(Integer.MAX_VALUE) + .deleted(between(1, 1000)) + .totalSizeInBytes(between(1, 100)) + .build(), + new StoreStats.Builder().sizeInBytes(between(1, 10000)).reservedSize(between(1, 10000)).build(), clusterSettings, "source", "target" @@ -157,8 +160,13 @@ public void testErrorCondition() { TransportResizeAction.prepareCreateIndexRequest( req, clusterState, - (i) -> i == 2 || i == 3 ? new DocsStats(Integer.MAX_VALUE / 2, between(1, 1000), between(1, 10000)) : null, - new StoreStats(between(1, 10000), between(1, 10000)), + (i) -> i == 2 || i == 3 + ? new DocsStats.Builder().count(Integer.MAX_VALUE / 2) + .deleted(between(1, 1000)) + .totalSizeInBytes(between(1, 10000)) + .build() + : null, + new StoreStats.Builder().sizeInBytes(between(1, 10000)).reservedSize(between(1, 10000)).build(), clusterSettings, "source", "target" @@ -177,8 +185,8 @@ public void testErrorCondition() { TransportResizeAction.prepareCreateIndexRequest( req, clusterState, - (i) -> new DocsStats(between(10, 1000), between(1, 10), between(1, 10000)), - new StoreStats(between(1, 10000), between(1, 10000)), + (i) -> new DocsStats.Builder().count(between(10, 1000)).deleted(between(1, 10)).totalSizeInBytes(between(1, 10000)).build(), + new StoreStats.Builder().sizeInBytes(between(1, 10000)).reservedSize(between(1, 10000)).build(), clusterSettings, "source", "target" @@ -207,8 +215,8 @@ public void testErrorCondition() { TransportResizeAction.prepareCreateIndexRequest( new ResizeRequest("target", "source"), clusterState, - (i) -> new DocsStats(between(1, 1000), between(1, 1000), between(0, 10000)), - new StoreStats(between(1, 10000), between(1, 10000)), + (i) -> new DocsStats.Builder().count(between(1, 1000)).deleted(between(1, 1000)).totalSizeInBytes(between(0, 10000)).build(), + new StoreStats.Builder().sizeInBytes(between(1, 10000)).reservedSize(between(1, 10000)).build(), clusterSettings, "source", "target" @@ -240,7 +248,7 @@ public void testPassNumRoutingShards() { resizeRequest, clusterState, null, - new StoreStats(between(1, 10000), between(1, 10000)), + new StoreStats.Builder().sizeInBytes(between(1, 10000)).reservedSize(between(1, 10000)).build(), clusterSettings, "source", "target" @@ -254,7 +262,7 @@ public void testPassNumRoutingShards() { resizeRequest, clusterState, null, - new StoreStats(between(1, 10000), between(1, 10000)), + new StoreStats.Builder().sizeInBytes(between(1, 10000)).reservedSize(between(1, 10000)).build(), clusterSettings, "source", "target" @@ -288,7 +296,7 @@ public void testPassNumRoutingShardsAndFail() { resizeRequest, clusterState, null, - new StoreStats(between(1, 10000), between(1, 10000)), + new StoreStats.Builder().sizeInBytes(between(1, 10000)).reservedSize(between(1, 10000)).build(), clusterSettings, "source", "target" @@ -305,7 +313,7 @@ public void testPassNumRoutingShardsAndFail() { resizeRequest, finalState, null, - new StoreStats(between(1, 10000), between(1, 10000)), + new StoreStats.Builder().sizeInBytes(between(1, 10000)).reservedSize(between(1, 10000)).build(), clusterSettings, "source", "target" @@ -335,7 +343,10 @@ public void testShrinkIndexSettings() { routingTable = OpenSearchAllocationTestCase.startInitializingShardsAndReroute(service, clusterState, indexName).routingTable(); clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); int numSourceShards = clusterState.metadata().index(indexName).getNumberOfShards(); - DocsStats stats = new DocsStats(between(0, (IndexWriter.MAX_DOCS) / numSourceShards), between(1, 1000), between(1, 10000)); + DocsStats stats = new DocsStats.Builder().count(between(0, (IndexWriter.MAX_DOCS) / numSourceShards)) + .deleted(between(1, 1000)) + .totalSizeInBytes(between(1, 10000)) + .build(); ResizeRequest target = new ResizeRequest("target", indexName); final ActiveShardCount activeShardCount = randomBoolean() ? ActiveShardCount.ALL : ActiveShardCount.ONE; target.setWaitForActiveShards(activeShardCount); @@ -343,7 +354,7 @@ public void testShrinkIndexSettings() { target, clusterState, (i) -> stats, - new StoreStats(between(1, 10000), between(1, 10000)), + new StoreStats.Builder().sizeInBytes(between(1, 10000)).reservedSize(between(1, 10000)).build(), clusterSettings, indexName, "target" @@ -377,7 +388,10 @@ public void testShrinkWithMaxShardSize() { routingTable = OpenSearchAllocationTestCase.startInitializingShardsAndReroute(service, clusterState, indexName).routingTable(); clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); int numSourceShards = clusterState.metadata().index(indexName).getNumberOfShards(); - DocsStats stats = new DocsStats(between(0, (IndexWriter.MAX_DOCS) / numSourceShards), between(1, 1000), between(1, 10000)); + DocsStats stats = new DocsStats.Builder().count(between(0, (IndexWriter.MAX_DOCS) / numSourceShards)) + .deleted(between(1, 1000)) + .totalSizeInBytes(between(1, 10000)) + .build(); // target index's shards number must be the lowest factor of the source index's shards number int expectedShardsNum = 5; @@ -390,7 +404,7 @@ public void testShrinkWithMaxShardSize() { resizeRequest, clusterState, (i) -> stats, - new StoreStats(100, between(1, 10000)), + new StoreStats.Builder().sizeInBytes(100).reservedSize(between(1, 10000)).build(), clusterSettings, indexName, "target" @@ -412,7 +426,7 @@ public void testShrinkWithMaxShardSize() { resizeRequest, clusterState, (i) -> stats, - new StoreStats(100, between(1, 10000)), + new StoreStats.Builder().sizeInBytes(100).reservedSize(between(1, 10000)).build(), clusterSettings, indexName, "target" @@ -434,7 +448,7 @@ public void testShrinkWithMaxShardSize() { resizeRequest, clusterState, (i) -> stats, - new StoreStats(100, between(1, 10000)), + new StoreStats.Builder().sizeInBytes(100).reservedSize(between(1, 10000)).build(), clusterSettings, indexName, "target" @@ -453,21 +467,35 @@ public void testCalculateTargetIndexShardsNum() { ).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); IndexMetadata indexMetadata = clusterState.metadata().index(indexName); - assertEquals(TransportResizeAction.calculateTargetIndexShardsNum(null, new StoreStats(100, between(1, 10000)), indexMetadata), 1); + assertEquals( + TransportResizeAction.calculateTargetIndexShardsNum( + null, + new StoreStats.Builder().sizeInBytes(100).reservedSize(between(1, 10000)).build(), + indexMetadata + ), + 1 + ); assertEquals( TransportResizeAction.calculateTargetIndexShardsNum( new ByteSizeValue(0), - new StoreStats(100, between(1, 10000)), + new StoreStats.Builder().sizeInBytes(100).reservedSize(between(1, 10000)).build(), indexMetadata ), 1 ); assertEquals(TransportResizeAction.calculateTargetIndexShardsNum(new ByteSizeValue(1), null, indexMetadata), 1); - assertEquals(TransportResizeAction.calculateTargetIndexShardsNum(new ByteSizeValue(1), new StoreStats(0, 0), indexMetadata), 1); + assertEquals( + TransportResizeAction.calculateTargetIndexShardsNum( + new ByteSizeValue(1), + new StoreStats.Builder().sizeInBytes(0).reservedSize(0).build(), + indexMetadata + ), + 1 + ); assertEquals( TransportResizeAction.calculateTargetIndexShardsNum( new ByteSizeValue(1000), - new StoreStats(100, between(1, 10000)), + new StoreStats.Builder().sizeInBytes(100).reservedSize(between(1, 10000)).build(), indexMetadata ), 1 @@ -475,7 +503,7 @@ public void testCalculateTargetIndexShardsNum() { assertEquals( TransportResizeAction.calculateTargetIndexShardsNum( new ByteSizeValue(1), - new StoreStats(100, between(1, 10000)), + new StoreStats.Builder().sizeInBytes(100).reservedSize(between(1, 10000)).build(), indexMetadata ), indexMetadata.getNumberOfShards() @@ -488,7 +516,7 @@ public void testCalculateTargetIndexShardsNum() { assertEquals( TransportResizeAction.calculateTargetIndexShardsNum( new ByteSizeValue(10), - new StoreStats(100, between(1, 10000)), + new StoreStats.Builder().sizeInBytes(100).reservedSize(between(1, 10000)).build(), indexMetadata ), 10 @@ -496,7 +524,7 @@ public void testCalculateTargetIndexShardsNum() { assertEquals( TransportResizeAction.calculateTargetIndexShardsNum( new ByteSizeValue(12), - new StoreStats(100, between(1, 10000)), + new StoreStats.Builder().sizeInBytes(100).reservedSize(between(1, 10000)).build(), indexMetadata ), indexMetadata.getNumberOfShards() @@ -504,7 +532,7 @@ public void testCalculateTargetIndexShardsNum() { assertEquals( TransportResizeAction.calculateTargetIndexShardsNum( new ByteSizeValue(20), - new StoreStats(100, between(1, 10000)), + new StoreStats.Builder().sizeInBytes(100).reservedSize(between(1, 10000)).build(), indexMetadata ), 5 @@ -512,7 +540,7 @@ public void testCalculateTargetIndexShardsNum() { assertEquals( TransportResizeAction.calculateTargetIndexShardsNum( new ByteSizeValue(50), - new StoreStats(100, between(1, 10000)), + new StoreStats.Builder().sizeInBytes(100).reservedSize(between(1, 10000)).build(), indexMetadata ), 2 @@ -549,7 +577,7 @@ public void testIndexBlocks() { resizeRequest, finalState, null, - new StoreStats(between(1, 10000), between(1, 10000)), + new StoreStats.Builder().sizeInBytes(between(1, 10000)).reservedSize(between(1, 10000)).build(), clusterSettings, indexName, "target" @@ -576,7 +604,10 @@ public void testIndexBlocks() { routingTable = OpenSearchAllocationTestCase.startInitializingShardsAndReroute(service, clusterState, indexName).routingTable(); clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); int numSourceShards = clusterState.metadata().index(indexName).getNumberOfShards(); - DocsStats stats = new DocsStats(between(0, (IndexWriter.MAX_DOCS) / numSourceShards), between(1, 1000), between(1, 10000)); + DocsStats stats = new DocsStats.Builder().count(between(0, (IndexWriter.MAX_DOCS) / numSourceShards)) + .deleted(between(1, 1000)) + .totalSizeInBytes(between(1, 10000)) + .build(); int expectedShardsNum; String cause; @@ -601,7 +632,7 @@ public void testIndexBlocks() { resizeRequest, clusterState, (i) -> stats, - new StoreStats(100, between(1, 10000)), + new StoreStats.Builder().sizeInBytes(100).reservedSize(between(1, 10000)).build(), clusterSettings, indexName, "target" @@ -648,7 +679,10 @@ public void testResizeFailuresDuringMigration() { // now we start the shard routingTable = OpenSearchAllocationTestCase.startInitializingShardsAndReroute(service, clusterState, "source").routingTable(); clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - DocsStats stats = new DocsStats(between(0, (IndexWriter.MAX_DOCS) / 10), between(1, 1000), between(1, 10000)); + DocsStats stats = new DocsStats.Builder().count(between(0, (IndexWriter.MAX_DOCS) / 10)) + .deleted(between(1, 1000)) + .totalSizeInBytes(between(1, 10000)) + .build(); ResizeRequest resizeRequest = new ResizeRequest("target", "source"); ResizeType resizeType; int expectedShardsNum; @@ -685,7 +719,7 @@ public void testResizeFailuresDuringMigration() { resizeRequest, finalState, (i) -> stats, - new StoreStats(between(1, 10000), between(1, 10000)), + new StoreStats.Builder().sizeInBytes(between(1, 10000)).reservedSize(between(1, 10000)).build(), clusterSettings, "source", "target" @@ -704,7 +738,7 @@ public void testResizeFailuresDuringMigration() { resizeRequest, clusterState, (i) -> stats, - new StoreStats(100, between(1, 10000)), + new StoreStats.Builder().sizeInBytes(100).reservedSize(between(1, 10000)).build(), clusterSettings, "source", "target" @@ -720,7 +754,7 @@ public void testResizeFailuresDuringMigration() { resizeRequest, clusterState, (i) -> stats, - new StoreStats(100, between(1, 10000)), + new StoreStats.Builder().sizeInBytes(100).reservedSize(between(1, 10000)).build(), clusterSettings, "source", "target" diff --git a/server/src/test/java/org/opensearch/cluster/DiskUsageTests.java b/server/src/test/java/org/opensearch/cluster/DiskUsageTests.java index d790d95757b02..247659f29c34d 100644 --- a/server/src/test/java/org/opensearch/cluster/DiskUsageTests.java +++ b/server/src/test/java/org/opensearch/cluster/DiskUsageTests.java @@ -122,7 +122,7 @@ public void testFillShardLevelInfo() { test_0 = ShardRoutingHelper.moveToStarted(test_0); Path test0Path = createTempDir().resolve("indices").resolve(index.getUUID()).resolve("0"); CommonStats commonStats0 = new CommonStats(); - commonStats0.store = new StoreStats(100, 0L); + commonStats0.store = new StoreStats.Builder().sizeInBytes(100).reservedSize(0L).build(); ShardRouting test_1 = ShardRouting.newUnassigned( new ShardId(index, 1), false, @@ -133,7 +133,7 @@ public void testFillShardLevelInfo() { test_1 = ShardRoutingHelper.moveToStarted(test_1); Path test1Path = createTempDir().resolve("indices").resolve(index.getUUID()).resolve("1"); CommonStats commonStats1 = new CommonStats(); - commonStats1.store = new StoreStats(1000, 0L); + commonStats1.store = new StoreStats.Builder().sizeInBytes(1000).reservedSize(0L).build(); ShardStats[] stats = new ShardStats[] { new ShardStats(test_0, new ShardPath(false, test0Path, test0Path, test_0.shardId()), commonStats0, null, null, null, null), new ShardStats(test_1, new ShardPath(false, test1Path, test1Path, test_1.shardId()), commonStats1, null, null, null, null) }; diff --git a/server/src/test/java/org/opensearch/index/shard/DocsStatsTests.java b/server/src/test/java/org/opensearch/index/shard/DocsStatsTests.java index c534f8e0a00be..d6b12c86d6fef 100644 --- a/server/src/test/java/org/opensearch/index/shard/DocsStatsTests.java +++ b/server/src/test/java/org/opensearch/index/shard/DocsStatsTests.java @@ -42,13 +42,13 @@ public class DocsStatsTests extends OpenSearchTestCase { public void testCalculateAverageDocSize() throws Exception { - DocsStats stats = new DocsStats(10, 2, 120); + DocsStats stats = new DocsStats.Builder().count(10).deleted(2).totalSizeInBytes(120).build(); assertThat(stats.getAverageSizeInBytes(), equalTo(10L)); - stats.add(new DocsStats(0, 0, 0)); + stats.add(new DocsStats.Builder().count(0).deleted(0).totalSizeInBytes(0).build()); assertThat(stats.getAverageSizeInBytes(), equalTo(10L)); - stats.add(new DocsStats(8, 30, 480)); + stats.add(new DocsStats.Builder().count(8).deleted(30).totalSizeInBytes(480).build()); assertThat(stats.getCount(), equalTo(18L)); assertThat(stats.getDeleted(), equalTo(32L)); assertThat(stats.getTotalSizeInBytes(), equalTo(600L)); @@ -56,25 +56,28 @@ public void testCalculateAverageDocSize() throws Exception { } public void testUninitialisedShards() { - DocsStats stats = new DocsStats(0, 0, -1); + DocsStats stats = new DocsStats.Builder().count(0).deleted(0).totalSizeInBytes(-1).build(); assertThat(stats.getTotalSizeInBytes(), equalTo(-1L)); assertThat(stats.getAverageSizeInBytes(), equalTo(0L)); - stats.add(new DocsStats(0, 0, -1)); + stats.add(new DocsStats.Builder().count(0).deleted(0).totalSizeInBytes(-1).build()); assertThat(stats.getTotalSizeInBytes(), equalTo(-1L)); assertThat(stats.getAverageSizeInBytes(), equalTo(0L)); - stats.add(new DocsStats(1, 0, 10)); + stats.add(new DocsStats.Builder().count(1).deleted(0).totalSizeInBytes(10).build()); assertThat(stats.getTotalSizeInBytes(), equalTo(10L)); assertThat(stats.getAverageSizeInBytes(), equalTo(10L)); - stats.add(new DocsStats(0, 0, -1)); + stats.add(new DocsStats.Builder().count(0).deleted(0).totalSizeInBytes(-1).build()); assertThat(stats.getTotalSizeInBytes(), equalTo(10L)); assertThat(stats.getAverageSizeInBytes(), equalTo(10L)); - stats.add(new DocsStats(1, 0, 20)); + stats.add(new DocsStats.Builder().count(1).deleted(0).totalSizeInBytes(20).build()); assertThat(stats.getTotalSizeInBytes(), equalTo(30L)); assertThat(stats.getAverageSizeInBytes(), equalTo(15L)); } public void testSerialize() throws Exception { - DocsStats originalStats = new DocsStats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong()); + DocsStats originalStats = new DocsStats.Builder().count(randomNonNegativeLong()) + .deleted(randomNonNegativeLong()) + .totalSizeInBytes(randomNonNegativeLong()) + .build(); try (BytesStreamOutput out = new BytesStreamOutput()) { originalStats.writeTo(out); BytesReference bytes = out.bytes(); diff --git a/server/src/test/java/org/opensearch/index/store/StoreTests.java b/server/src/test/java/org/opensearch/index/store/StoreTests.java index 7957a3b0d45e0..ae9d179b291f0 100644 --- a/server/src/test/java/org/opensearch/index/store/StoreTests.java +++ b/server/src/test/java/org/opensearch/index/store/StoreTests.java @@ -855,7 +855,7 @@ public void testStoreStats() throws IOException { final long otherStatsBytes = randomLongBetween(0L, Integer.MAX_VALUE); final long otherStatsReservedBytes = randomBoolean() ? StoreStats.UNKNOWN_RESERVED_BYTES : randomLongBetween(0L, Integer.MAX_VALUE); - stats.add(new StoreStats(otherStatsBytes, otherStatsReservedBytes)); + stats.add(new StoreStats.Builder().sizeInBytes(otherStatsBytes).reservedSize(otherStatsReservedBytes).build()); assertEquals(initialStoreSize + otherStatsBytes, stats.getSize().getBytes()); assertEquals(Math.max(reservedBytes, 0L) + Math.max(otherStatsReservedBytes, 0L), stats.getReservedSize().getBytes()); diff --git a/server/src/test/java/org/opensearch/rest/action/cat/RestShardsActionTests.java b/server/src/test/java/org/opensearch/rest/action/cat/RestShardsActionTests.java index 708e0c7d3da77..173069bf07d22 100644 --- a/server/src/test/java/org/opensearch/rest/action/cat/RestShardsActionTests.java +++ b/server/src/test/java/org/opensearch/rest/action/cat/RestShardsActionTests.java @@ -86,7 +86,7 @@ public void setup() { .resolve(shardRouting.shardId().getIndex().getUUID()) .resolve(String.valueOf(shardRouting.shardId().id())); CommonStats commonStats = new CommonStats(); - commonStats.docs = new DocsStats(numDocs, numDeletedDocs, 0); + commonStats.docs = new DocsStats.Builder().count(numDocs).deleted(numDeletedDocs).totalSizeInBytes(0).build(); ShardStats shardStats = new ShardStats( shardRouting, new ShardPath(false, path, path, shardRouting.shardId()),