Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -46,6 +46,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Pass registry of headers from ActionPlugin.getRestHeaders to ActionPlugin.getRestHandlerWrapper ([#19875](https://github.com/opensearch-project/OpenSearch/pull/19875))
- Refactor the Condition.Stats and DirectoryFileTransferTracker.Stats class to use the Builder pattern instead of constructors ([#19862](https://github.com/opensearch-project/OpenSearch/pull/19862))
- 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))

### Fixed
- Fix Allocation and Rebalance Constraints of WeightFunction are incorrectly reset ([#19012](https://github.com/opensearch-project/OpenSearch/pull/19012))
Expand Down Expand Up @@ -96,6 +97,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Deprecated existing constructors in DocStats and StoreStats in favor of the new Builder ([#19863](https://github.com/opensearch-project/OpenSearch/pull/19863))
- 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))

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ public class QueryCacheStats implements Writeable, ToXContentFragment {

public QueryCacheStats() {}

/**
* Private constructor that takes a builder.
* This is the sole entry point for creating a new QueryCacheStats object.
* @param builder The builder instance containing all the values.
*/
private QueryCacheStats(Builder builder) {
this.ramBytesUsed = builder.ramBytesUsed;
this.hitCount = builder.hitCount;
this.missCount = builder.missCount;
this.cacheCount = builder.cacheCount;
this.cacheSize = builder.cacheSize;
}

public QueryCacheStats(StreamInput in) throws IOException {
ramBytesUsed = in.readLong();
hitCount = in.readLong();
Expand All @@ -68,6 +81,11 @@ public QueryCacheStats(StreamInput in) throws IOException {
cacheSize = in.readLong();
}

/**
* This constructor will be deprecated starting in version 3.4.0.
* Use {@link Builder} instead.
*/
@Deprecated
public QueryCacheStats(long ramBytesUsed, long hitCount, long missCount, long cacheCount, long cacheSize) {
this.ramBytesUsed = ramBytesUsed;
this.hitCount = hitCount;
Expand Down Expand Up @@ -137,6 +155,53 @@ public long getEvictions() {
return cacheCount - cacheSize;
}

/**
* Builder for the {@link QueryCacheStats} class.
* Provides a fluent API for constructing a QueryCacheStats object.
*/
public static class Builder {
private long ramBytesUsed = 0;
private long hitCount = 0;
private long missCount = 0;
private long cacheCount = 0;
private long cacheSize = 0;

public Builder() {}

public Builder ramBytesUsed(long used) {
this.ramBytesUsed = used;
return this;
}

public Builder hitCount(long count) {
this.hitCount = count;
return this;
}

public Builder missCount(long count) {
this.missCount = count;
return this;
}

public Builder cacheCount(long count) {
this.cacheCount = count;
return this;
}

public Builder cacheSize(long size) {
this.cacheSize = size;
return this;
}

/**
* Creates a {@link QueryCacheStats} object from the builder's current state.
* @return A new QueryCacheStats instance.
*/
public QueryCacheStats build() {
return new QueryCacheStats(this);
}
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeLong(ramBytesUsed);
Expand Down
52 changes: 52 additions & 0 deletions server/src/main/java/org/opensearch/index/flush/FlushStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,28 @@ public FlushStats() {

}

/**
* Private constructor that takes a builder.
* This is the sole entry point for creating a new FlushStats object.
* @param builder The builder instance containing all the values.
*/
private FlushStats(Builder builder) {
this.total = builder.total;
this.periodic = builder.periodic;
this.totalTimeInMillis = builder.totalTimeInMillis;
}

public FlushStats(StreamInput in) throws IOException {
total = in.readVLong();
totalTimeInMillis = in.readVLong();
periodic = in.readVLong();
}

/**
* This constructor will be deprecated starting in version 3.4.0.
* Use {@link Builder} instead.
*/
@Deprecated
public FlushStats(long total, long periodic, long totalTimeInMillis) {
this.total = total;
this.periodic = periodic;
Expand Down Expand Up @@ -117,6 +133,42 @@ public TimeValue getTotalTime() {
return new TimeValue(totalTimeInMillis);
}

/**
* Builder for the {@link FlushStats} class.
* Provides a fluent API for constructing a FlushStats object.
*/
public static class Builder {
private long total = 0;
private long periodic = 0;
private long totalTimeInMillis = 0;

public Builder() {}

public Builder total(long total) {
this.total = total;
return this;
}

public Builder periodic(long periodic) {
this.periodic = periodic;
return this;
}

public Builder totalTimeInMillis(long time) {
this.totalTimeInMillis = time;
return this;
}

/**
* Creates a {@link FlushStats} object from the builder's current state.
*
* @return A new FlushStats instance.
*/
public FlushStats build() {
return new FlushStats(this);
}
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(Fields.FLUSH);
Expand Down
66 changes: 66 additions & 0 deletions server/src/main/java/org/opensearch/index/get/GetStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ public class GetStats implements Writeable, ToXContentFragment {

public GetStats() {}

/**
* Private constructor that takes a builder.
* This is the sole entry point for creating a new GetStats object.
* @param builder The builder instance containing all the values.
*/
private GetStats(Builder builder) {
this.existsCount = builder.existsCount;
this.existsTimeInMillis = builder.existsTimeInMillis;
this.missingCount = builder.missingCount;
this.missingTimeInMillis = builder.missingTimeInMillis;
this.current = builder.current;
}

public GetStats(StreamInput in) throws IOException {
existsCount = in.readVLong();
existsTimeInMillis = in.readVLong();
Expand All @@ -67,6 +80,11 @@ public GetStats(StreamInput in) throws IOException {
current = in.readVLong();
}

/**
* This constructor will be deprecated starting in version 3.4.0.
* Use {@link Builder} instead.
*/
@Deprecated
public GetStats(long existsCount, long existsTimeInMillis, long missingCount, long missingTimeInMillis, long current) {
this.existsCount = existsCount;
this.existsTimeInMillis = existsTimeInMillis;
Expand Down Expand Up @@ -134,6 +152,54 @@ public long current() {
return this.current;
}

/**
* Builder for the {@link GetStats} class.
* Provides a fluent API for constructing a GetStats object.
*/
public static class Builder {
private long existsCount = 0;
private long existsTimeInMillis = 0;
private long missingCount = 0;
private long missingTimeInMillis = 0;
private long current = 0;

public Builder() {}

public Builder existsCount(long count) {
this.existsCount = count;
return this;
}

public Builder existsTimeInMillis(long time) {
this.existsTimeInMillis = time;
return this;
}

public Builder missingCount(long count) {
this.missingCount = count;
return this;
}

public Builder missingTimeInMillis(long time) {
this.missingTimeInMillis = time;
return this;
}

public Builder current(long current) {
this.current = current;
return this;
}

/**
* Creates a {@link GetStats} object from the builder's current state.
*
* @return A new GetStats instance.
*/
public GetStats build() {
return new GetStats(this);
}
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(Fields.GET);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,12 @@ public ShardGetService(IndexSettings indexSettings, IndexShard indexShard, Mappe
}

public GetStats stats() {
return new GetStats(
existsMetric.count(),
TimeUnit.NANOSECONDS.toMillis(existsMetric.sum()),
missingMetric.count(),
TimeUnit.NANOSECONDS.toMillis(missingMetric.sum()),
currentMetric.count()
);
return new GetStats.Builder().existsCount(existsMetric.count())
.existsTimeInMillis(TimeUnit.NANOSECONDS.toMillis(existsMetric.sum()))
.missingCount(missingMetric.count())
.missingTimeInMillis(TimeUnit.NANOSECONDS.toMillis(missingMetric.sum()))
.current(currentMetric.count())
.build();
}

public GetResult get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,10 @@ public RefreshStats refreshStats() {
}

public FlushStats flushStats() {
return new FlushStats(flushMetric.count(), periodicFlushMetric.count(), TimeUnit.NANOSECONDS.toMillis(flushMetric.sum()));
return new FlushStats.Builder().total(flushMetric.count())
.periodic(periodicFlushMetric.count())
.totalTimeInMillis(TimeUnit.NANOSECONDS.toMillis(flushMetric.sum()))
.build();
}

public DocsStats docStats() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,24 @@ public QueryCacheStats getStats(ShardId shard) {
// We also have some shared ram usage that we try to distribute to
// proportionally to their number of cache entries of each shard
if (stats.isEmpty()) {
shardStats.add(new QueryCacheStats(sharedRamBytesUsed, 0, 0, 0, 0));
shardStats.add(
new QueryCacheStats.Builder().ramBytesUsed(sharedRamBytesUsed).hitCount(0).missCount(0).cacheCount(0).cacheSize(0).build()
);
} else {
long totalSize = 0;
for (QueryCacheStats s : stats.values()) {
totalSize += s.getCacheSize();
}
final double weight = totalSize == 0 ? 1d / stats.size() : ((double) shardStats.getCacheSize()) / totalSize;
final long additionalRamBytesUsed = Math.round(weight * sharedRamBytesUsed);
shardStats.add(new QueryCacheStats(additionalRamBytesUsed, 0, 0, 0, 0));
shardStats.add(
new QueryCacheStats.Builder().ramBytesUsed(additionalRamBytesUsed)
.hitCount(0)
.missCount(0)
.cacheCount(0)
.cacheSize(0)
.build()
);
}
return shardStats;
}
Expand Down Expand Up @@ -287,7 +296,12 @@ private static class Stats implements Cloneable {
}

QueryCacheStats toQueryCacheStats() {
return new QueryCacheStats(ramBytesUsed, hitCount, missCount, cacheCount, cacheSize);
return new QueryCacheStats.Builder().ramBytesUsed(ramBytesUsed)
.hitCount(hitCount)
.missCount(missCount)
.cacheCount(cacheCount)
.cacheSize(cacheSize)
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1372,15 +1372,17 @@ private CommonStats createRandomCommonStats() {
.build();
commonStats.indexing = new IndexingStats();
commonStats.completion = new CompletionStats();
commonStats.flush = new FlushStats(randomLongBetween(0, 100), randomLongBetween(0, 100), randomLongBetween(0, 100));
commonStats.flush = new FlushStats.Builder().total(randomLongBetween(0, 100))
.periodic(randomLongBetween(0, 100))
.totalTimeInMillis(randomLongBetween(0, 100))
.build();
commonStats.fieldData = new FieldDataStats(randomLongBetween(0, 100), randomLongBetween(0, 100), null);
commonStats.queryCache = new QueryCacheStats(
randomLongBetween(0, 100),
randomLongBetween(0, 100),
randomLongBetween(0, 100),
randomLongBetween(0, 100),
randomLongBetween(0, 100)
);
commonStats.queryCache = new QueryCacheStats.Builder().ramBytesUsed(randomLongBetween(0, 100))
.hitCount(randomLongBetween(0, 100))
.missCount(randomLongBetween(0, 100))
.cacheCount(randomLongBetween(0, 100))
.cacheSize(randomLongBetween(0, 100))
.build();
commonStats.segments = new SegmentsStats();

return commonStats;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,15 +371,17 @@ private CommonStats createRandomCommonStats() {
.build();
commonStats.indexing = new IndexingStats();
commonStats.completion = new CompletionStats();
commonStats.flush = new FlushStats(randomLongBetween(0, 100), randomLongBetween(0, 100), randomLongBetween(0, 100));
commonStats.flush = new FlushStats.Builder().total(randomLongBetween(0, 100))
.periodic(randomLongBetween(0, 100))
.totalTimeInMillis(randomLongBetween(0, 100))
.build();
commonStats.fieldData = new FieldDataStats(randomLongBetween(0, 100), randomLongBetween(0, 100), null);
commonStats.queryCache = new QueryCacheStats(
randomLongBetween(0, 100),
randomLongBetween(0, 100),
randomLongBetween(0, 100),
randomLongBetween(0, 100),
randomLongBetween(0, 100)
);
commonStats.queryCache = new QueryCacheStats.Builder().ramBytesUsed(randomLongBetween(0, 100))
.hitCount(randomLongBetween(0, 100))
.missCount(randomLongBetween(0, 100))
.cacheCount(randomLongBetween(0, 100))
.cacheSize(randomLongBetween(0, 100))
.build();
commonStats.segments = new SegmentsStats();

return commonStats;
Expand Down
Loading
Loading