Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading