diff --git a/CHANGELOG.md b/CHANGELOG.md index 51817010ccfcb..b460bb8e344ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Refactor the RemoteTranslogTransferTracker.Stats and RemoteSegmentTransferTracker.Stats class to use the Builder pattern instead of constructors ([#19837](https://github.com/opensearch-project/OpenSearch/pull/19837)) - Refactor the GetStats, FlushStats and QueryCacheStats class to use the Builder pattern instead of constructors ([#19935](https://github.com/opensearch-project/OpenSearch/pull/19935)) - Add RangeSemver for `dependencies` in `plugin-descriptor.properties` ([#19939](https://github.com/opensearch-project/OpenSearch/pull/19939)) +- Refactor the FieldDataStats and CompletionStats class to use the Builder pattern instead of constructors ([#19936](https://github.com/opensearch-project/OpenSearch/pull/19936)) ### Fixed - Fix Allocation and Rebalance Constraints of WeightFunction are incorrectly reset ([#19012](https://github.com/opensearch-project/OpenSearch/pull/19012)) @@ -101,6 +102,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Deprecated existing constructors in Condition.Stats and DirectoryFileTransferTracker.Stats in favor of the new Builder ([#19862](https://github.com/opensearch-project/OpenSearch/pull/19862)) - Deprecated existing constructors in RemoteTranslogTransferTracker.Stats and RemoteSegmentTransferTracker.Stats in favor of the new Builder ([#19837](https://github.com/opensearch-project/OpenSearch/pull/19837)) - Deprecated existing constructors in GetStats, FlushStats and QueryCacheStats in favor of the new Builder ([#19935](https://github.com/opensearch-project/OpenSearch/pull/19935)) +- Deprecated existing constructors in FieldDataStats and CompletionStats in favor of the new Builder ([#19936](https://github.com/opensearch-project/OpenSearch/pull/19936)) ### Removed diff --git a/server/src/main/java/org/opensearch/index/engine/CompletionStatsCache.java b/server/src/main/java/org/opensearch/index/engine/CompletionStatsCache.java index 07f988d65cf76..9358325690ab4 100644 --- a/server/src/main/java/org/opensearch/index/engine/CompletionStatsCache.java +++ b/server/src/main/java/org/opensearch/index/engine/CompletionStatsCache.java @@ -116,7 +116,7 @@ CompletionStats get(String... fieldNamePatterns) { } } - return new CompletionStats(sizeInBytes, new FieldMemoryStats(completionFields)); + return new CompletionStats.Builder().sizeInBytes(sizeInBytes).fieldMemoryStats(new FieldMemoryStats(completionFields)).build(); }); boolean success = false; @@ -153,7 +153,7 @@ private static CompletionStats filterCompletionStatsByFieldName(String[] fieldNa } else { fieldMemoryStats = null; } - return new CompletionStats(fullCompletionStats.getSizeInBytes(), fieldMemoryStats); + return new CompletionStats.Builder().sizeInBytes(fullCompletionStats.getSizeInBytes()).fieldMemoryStats(fieldMemoryStats).build(); } @Override diff --git a/server/src/main/java/org/opensearch/index/fielddata/FieldDataStats.java b/server/src/main/java/org/opensearch/index/fielddata/FieldDataStats.java index 1bd81c840a3d4..1251f6e2bda6f 100644 --- a/server/src/main/java/org/opensearch/index/fielddata/FieldDataStats.java +++ b/server/src/main/java/org/opensearch/index/fielddata/FieldDataStats.java @@ -67,12 +67,28 @@ public FieldDataStats() { } + /** + * Private constructor that takes a builder. + * This is the sole entry point for creating a new FieldDataStats object. + * @param builder The builder instance containing all the values. + */ + private FieldDataStats(Builder builder) { + this.memorySize = builder.memorySize; + this.evictions = builder.evictions; + this.fields = builder.fields; + } + public FieldDataStats(StreamInput in) throws IOException { memorySize = in.readVLong(); evictions = in.readVLong(); fields = in.readOptionalWriteable(FieldMemoryStats::new); } + /** + * This constructor will be deprecated starting in version 3.4.0. + * Use {@link FieldDataStats.Builder} instead. + */ + @Deprecated public FieldDataStats(long memorySize, long evictions, @Nullable FieldMemoryStats fields) { this.memorySize = memorySize; this.evictions = evictions; @@ -111,6 +127,41 @@ public FieldMemoryStats getFields() { return fields; } + /** + * Builder for the {@link FieldDataStats} class. + * Provides a fluent API for constructing a FieldDataStats object. + */ + public static class Builder { + private long memorySize = 0; + private long evictions = 0; + private FieldMemoryStats fields = null; + + public Builder() {} + + public Builder memorySize(long memorySize) { + this.memorySize = memorySize; + return this; + } + + public Builder evictions(long evictions) { + this.evictions = evictions; + return this; + } + + public Builder fieldMemoryStats(@Nullable FieldMemoryStats fields) { + this.fields = fields; + return this; + } + + /** + * Creates a {@link FieldDataStats} object from the builder's current state. + * @return A new FieldDataStats instance. + */ + public FieldDataStats build() { + return new FieldDataStats(this); + } + } + @Override public void writeTo(StreamOutput out) throws IOException { out.writeVLong(memorySize); diff --git a/server/src/main/java/org/opensearch/index/fielddata/ShardFieldData.java b/server/src/main/java/org/opensearch/index/fielddata/ShardFieldData.java index d8f49ba3d9c60..2155fda870b92 100644 --- a/server/src/main/java/org/opensearch/index/fielddata/ShardFieldData.java +++ b/server/src/main/java/org/opensearch/index/fielddata/ShardFieldData.java @@ -67,11 +67,10 @@ public FieldDataStats stats(String... fields) { } } } - return new FieldDataStats( - totalMetric.count(), - evictionsMetric.count(), - fieldTotals == null ? null : new FieldMemoryStats(fieldTotals) - ); + return new FieldDataStats.Builder().memorySize(totalMetric.count()) + .evictions(evictionsMetric.count()) + .fieldMemoryStats(fieldTotals == null ? null : new FieldMemoryStats(fieldTotals)) + .build(); } @Override diff --git a/server/src/main/java/org/opensearch/search/suggest/completion/CompletionStats.java b/server/src/main/java/org/opensearch/search/suggest/completion/CompletionStats.java index ad91e75c591f3..993e374a82b85 100644 --- a/server/src/main/java/org/opensearch/search/suggest/completion/CompletionStats.java +++ b/server/src/main/java/org/opensearch/search/suggest/completion/CompletionStats.java @@ -62,11 +62,26 @@ public class CompletionStats implements Writeable, ToXContentFragment { public CompletionStats() {} + /** + * Private constructor that takes a builder. + * This is the sole entry point for creating a new CompletionStats object. + * @param builder The builder instance containing all the values. + */ + private CompletionStats(Builder builder) { + this.sizeInBytes = builder.sizeInBytes; + this.fields = builder.fields; + } + public CompletionStats(StreamInput in) throws IOException { sizeInBytes = in.readVLong(); fields = in.readOptionalWriteable(FieldMemoryStats::new); } + /** + * This constructor will be deprecated starting in version 3.4.0. + * Use {@link CompletionStats.Builder} instead. + */ + @Deprecated public CompletionStats(long size, @Nullable FieldMemoryStats fields) { this.sizeInBytes = size; this.fields = fields; @@ -84,6 +99,35 @@ public FieldMemoryStats getFields() { return fields; } + /** + * Builder for the {@link CompletionStats} class. + * Provides a fluent API for constructing a CompletionStats object. + */ + public static class Builder { + private long sizeInBytes = 0; + private FieldMemoryStats fields = null; + + public Builder() {} + + public Builder sizeInBytes(long bytes) { + this.sizeInBytes = bytes; + return this; + } + + public Builder fieldMemoryStats(FieldMemoryStats fields) { + this.fields = fields; + return this; + } + + /** + * Creates a {@link CompletionStats} object from the builder's current state. + * @return A new CompletionStats instance. + */ + public CompletionStats build() { + return new CompletionStats(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 34a041c3620db..c31c0e0349de0 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 @@ -1376,7 +1376,10 @@ private CommonStats createRandomCommonStats() { .periodic(randomLongBetween(0, 100)) .totalTimeInMillis(randomLongBetween(0, 100)) .build(); - commonStats.fieldData = new FieldDataStats(randomLongBetween(0, 100), randomLongBetween(0, 100), null); + commonStats.fieldData = new FieldDataStats.Builder().memorySize(randomLongBetween(0, 100)) + .evictions(randomLongBetween(0, 100)) + .fieldMemoryStats(null) + .build(); commonStats.queryCache = new QueryCacheStats.Builder().ramBytesUsed(randomLongBetween(0, 100)) .hitCount(randomLongBetween(0, 100)) .missCount(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 0bbcad0407c4d..6b83fc544cbf2 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 @@ -375,7 +375,10 @@ private CommonStats createRandomCommonStats() { .periodic(randomLongBetween(0, 100)) .totalTimeInMillis(randomLongBetween(0, 100)) .build(); - commonStats.fieldData = new FieldDataStats(randomLongBetween(0, 100), randomLongBetween(0, 100), null); + commonStats.fieldData = new FieldDataStats.Builder().memorySize(randomLongBetween(0, 100)) + .evictions(randomLongBetween(0, 100)) + .fieldMemoryStats(null) + .build(); commonStats.queryCache = new QueryCacheStats.Builder().ramBytesUsed(randomLongBetween(0, 100)) .hitCount(randomLongBetween(0, 100)) .missCount(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 f673dcf56f7cd..e57ec8640b07c 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 @@ -243,7 +243,10 @@ private CommonStats createRandomCommonStats() { .periodic(randomLongBetween(0, 100)) .totalTimeInMillis(randomLongBetween(0, 100)) .build(); - commonStats.fieldData = new FieldDataStats(randomLongBetween(0, 100), randomLongBetween(0, 100), null); + commonStats.fieldData = new FieldDataStats.Builder().memorySize(randomLongBetween(0, 100)) + .evictions(randomLongBetween(0, 100)) + .fieldMemoryStats(null) + .build(); commonStats.queryCache = new QueryCacheStats.Builder().ramBytesUsed(randomLongBetween(0, 100)) .hitCount(randomLongBetween(0, 100)) .missCount(randomLongBetween(0, 100)) diff --git a/server/src/test/java/org/opensearch/index/fielddata/FieldDataStatsTests.java b/server/src/test/java/org/opensearch/index/fielddata/FieldDataStatsTests.java index 296f760095ed1..3b16381c8189b 100644 --- a/server/src/test/java/org/opensearch/index/fielddata/FieldDataStatsTests.java +++ b/server/src/test/java/org/opensearch/index/fielddata/FieldDataStatsTests.java @@ -43,7 +43,10 @@ public class FieldDataStatsTests extends OpenSearchTestCase { public void testSerialize() throws IOException { FieldMemoryStats map = randomBoolean() ? null : FieldMemoryStatsTests.randomFieldMemoryStats(); - FieldDataStats stats = new FieldDataStats(randomNonNegativeLong(), randomNonNegativeLong(), map); + FieldDataStats stats = new FieldDataStats.Builder().memorySize(randomNonNegativeLong()) + .evictions(randomNonNegativeLong()) + .fieldMemoryStats(map) + .build(); BytesStreamOutput out = new BytesStreamOutput(); stats.writeTo(out); StreamInput input = out.bytes().streamInput(); diff --git a/server/src/test/java/org/opensearch/index/suggest/stats/CompletionsStatsTests.java b/server/src/test/java/org/opensearch/index/suggest/stats/CompletionsStatsTests.java index 104f132fb47ce..aa9137088d8e1 100644 --- a/server/src/test/java/org/opensearch/index/suggest/stats/CompletionsStatsTests.java +++ b/server/src/test/java/org/opensearch/index/suggest/stats/CompletionsStatsTests.java @@ -44,7 +44,7 @@ public class CompletionsStatsTests extends OpenSearchTestCase { public void testSerialize() throws IOException { FieldMemoryStats map = randomBoolean() ? null : FieldMemoryStatsTests.randomFieldMemoryStats(); - CompletionStats stats = new CompletionStats(randomNonNegativeLong(), map); + CompletionStats stats = new CompletionStats.Builder().sizeInBytes(randomNonNegativeLong()).fieldMemoryStats(map).build(); BytesStreamOutput out = new BytesStreamOutput(); stats.writeTo(out); StreamInput input = out.bytes().streamInput();