Skip to content

Commit 5dc0417

Browse files
pado0sandeshkr419
andauthored
Refactor GetStats, FlushStats and QueryCacheStats with Builder pattern (#19935)
Co-authored-by: Sandesh Kumar <sandeshkr419@gmail.com>
1 parent 0ff0db6 commit 5dc0417

File tree

10 files changed

+246
-38
lines changed

10 files changed

+246
-38
lines changed

CHANGELOG.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
4646
- Pass registry of headers from ActionPlugin.getRestHeaders to ActionPlugin.getRestHandlerWrapper ([#19875](https://github.com/opensearch-project/OpenSearch/pull/19875))
4747
- 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))
4848
- 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))
49+
- Refactor the GetStats, FlushStats and QueryCacheStats class to use the Builder pattern instead of constructors ([#19935](https://github.com/opensearch-project/OpenSearch/pull/19935))
4950
- Add RangeSemver for `dependencies` in `plugin-descriptor.properties` ([#19939](https://github.com/opensearch-project/OpenSearch/pull/19939))
5051

5152
### Fixed
@@ -89,15 +90,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
8990
- Bump `com.google.api.grpc:proto-google-iam-v1` from 1.55.0 to 1.57.0 ([#19872](https://github.com/opensearch-project/OpenSearch/pull/19872))
9091
- Bump `org.apache.commons:commons-text` from 1.13.1 to 1.14.0 ([#19871](https://github.com/opensearch-project/OpenSearch/pull/19871))
9192
- Exclude group com.microsoft.sqlserver from hadoop-minicluster ([#19889](https://github.com/opensearch-project/OpenSearch/pull/19889))
92-
- ### Deprecated
93+
- Bump `actions/github-script` from 7 to 8 ([#19946](https://github.com/opensearch-project/OpenSearch/pull/19946))
94+
- Bump `com.google.api:gax-httpjson` from 2.69.0 to 2.72.1 ([#19943](https://github.com/opensearch-project/OpenSearch/pull/19943))
95+
96+
### Deprecated
9397
- Deprecated existing constructors in ThreadPoolStats.Stats in favor of the new Builder ([#19317](https://github.com/opensearch-project/OpenSearch/pull/19317))
9498
- Deprecated existing constructors in IndexingStats.Stats in favor of the new Builder ([#19306](https://github.com/opensearch-project/OpenSearch/pull/19306))
9599
- Deprecated existing constructors in RefreshStats in favor of the new Builder ([#19835](https://github.com/opensearch-project/OpenSearch/pull/19835))
96100
- Deprecated existing constructors in DocStats and StoreStats in favor of the new Builder ([#19863](https://github.com/opensearch-project/OpenSearch/pull/19863))
97101
- Deprecated existing constructors in Condition.Stats and DirectoryFileTransferTracker.Stats in favor of the new Builder ([#19862](https://github.com/opensearch-project/OpenSearch/pull/19862))
98102
- Deprecated existing constructors in RemoteTranslogTransferTracker.Stats and RemoteSegmentTransferTracker.Stats in favor of the new Builder ([#19837](https://github.com/opensearch-project/OpenSearch/pull/19837))
99-
- Bump `actions/github-script` from 7 to 8 ([#19946](https://github.com/opensearch-project/OpenSearch/pull/19946))
100-
- Bump `com.google.api:gax-httpjson` from 2.69.0 to 2.72.1 ([#19943](https://github.com/opensearch-project/OpenSearch/pull/19943))
103+
- Deprecated existing constructors in GetStats, FlushStats and QueryCacheStats in favor of the new Builder ([#19935](https://github.com/opensearch-project/OpenSearch/pull/19935))
101104

102105
### Removed
103106

server/src/main/java/org/opensearch/index/cache/query/QueryCacheStats.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ public class QueryCacheStats implements Writeable, ToXContentFragment {
6060

6161
public QueryCacheStats() {}
6262

63+
/**
64+
* Private constructor that takes a builder.
65+
* This is the sole entry point for creating a new QueryCacheStats object.
66+
* @param builder The builder instance containing all the values.
67+
*/
68+
private QueryCacheStats(Builder builder) {
69+
this.ramBytesUsed = builder.ramBytesUsed;
70+
this.hitCount = builder.hitCount;
71+
this.missCount = builder.missCount;
72+
this.cacheCount = builder.cacheCount;
73+
this.cacheSize = builder.cacheSize;
74+
}
75+
6376
public QueryCacheStats(StreamInput in) throws IOException {
6477
ramBytesUsed = in.readLong();
6578
hitCount = in.readLong();
@@ -68,6 +81,11 @@ public QueryCacheStats(StreamInput in) throws IOException {
6881
cacheSize = in.readLong();
6982
}
7083

84+
/**
85+
* This constructor will be deprecated starting in version 3.4.0.
86+
* Use {@link Builder} instead.
87+
*/
88+
@Deprecated
7189
public QueryCacheStats(long ramBytesUsed, long hitCount, long missCount, long cacheCount, long cacheSize) {
7290
this.ramBytesUsed = ramBytesUsed;
7391
this.hitCount = hitCount;
@@ -137,6 +155,53 @@ public long getEvictions() {
137155
return cacheCount - cacheSize;
138156
}
139157

158+
/**
159+
* Builder for the {@link QueryCacheStats} class.
160+
* Provides a fluent API for constructing a QueryCacheStats object.
161+
*/
162+
public static class Builder {
163+
private long ramBytesUsed = 0;
164+
private long hitCount = 0;
165+
private long missCount = 0;
166+
private long cacheCount = 0;
167+
private long cacheSize = 0;
168+
169+
public Builder() {}
170+
171+
public Builder ramBytesUsed(long used) {
172+
this.ramBytesUsed = used;
173+
return this;
174+
}
175+
176+
public Builder hitCount(long count) {
177+
this.hitCount = count;
178+
return this;
179+
}
180+
181+
public Builder missCount(long count) {
182+
this.missCount = count;
183+
return this;
184+
}
185+
186+
public Builder cacheCount(long count) {
187+
this.cacheCount = count;
188+
return this;
189+
}
190+
191+
public Builder cacheSize(long size) {
192+
this.cacheSize = size;
193+
return this;
194+
}
195+
196+
/**
197+
* Creates a {@link QueryCacheStats} object from the builder's current state.
198+
* @return A new QueryCacheStats instance.
199+
*/
200+
public QueryCacheStats build() {
201+
return new QueryCacheStats(this);
202+
}
203+
}
204+
140205
@Override
141206
public void writeTo(StreamOutput out) throws IOException {
142207
out.writeLong(ramBytesUsed);

server/src/main/java/org/opensearch/index/flush/FlushStats.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,28 @@ public FlushStats() {
5858

5959
}
6060

61+
/**
62+
* Private constructor that takes a builder.
63+
* This is the sole entry point for creating a new FlushStats object.
64+
* @param builder The builder instance containing all the values.
65+
*/
66+
private FlushStats(Builder builder) {
67+
this.total = builder.total;
68+
this.periodic = builder.periodic;
69+
this.totalTimeInMillis = builder.totalTimeInMillis;
70+
}
71+
6172
public FlushStats(StreamInput in) throws IOException {
6273
total = in.readVLong();
6374
totalTimeInMillis = in.readVLong();
6475
periodic = in.readVLong();
6576
}
6677

78+
/**
79+
* This constructor will be deprecated starting in version 3.4.0.
80+
* Use {@link Builder} instead.
81+
*/
82+
@Deprecated
6783
public FlushStats(long total, long periodic, long totalTimeInMillis) {
6884
this.total = total;
6985
this.periodic = periodic;
@@ -117,6 +133,42 @@ public TimeValue getTotalTime() {
117133
return new TimeValue(totalTimeInMillis);
118134
}
119135

136+
/**
137+
* Builder for the {@link FlushStats} class.
138+
* Provides a fluent API for constructing a FlushStats object.
139+
*/
140+
public static class Builder {
141+
private long total = 0;
142+
private long periodic = 0;
143+
private long totalTimeInMillis = 0;
144+
145+
public Builder() {}
146+
147+
public Builder total(long total) {
148+
this.total = total;
149+
return this;
150+
}
151+
152+
public Builder periodic(long periodic) {
153+
this.periodic = periodic;
154+
return this;
155+
}
156+
157+
public Builder totalTimeInMillis(long time) {
158+
this.totalTimeInMillis = time;
159+
return this;
160+
}
161+
162+
/**
163+
* Creates a {@link FlushStats} object from the builder's current state.
164+
*
165+
* @return A new FlushStats instance.
166+
*/
167+
public FlushStats build() {
168+
return new FlushStats(this);
169+
}
170+
}
171+
120172
@Override
121173
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
122174
builder.startObject(Fields.FLUSH);

server/src/main/java/org/opensearch/index/get/GetStats.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ public class GetStats implements Writeable, ToXContentFragment {
5959

6060
public GetStats() {}
6161

62+
/**
63+
* Private constructor that takes a builder.
64+
* This is the sole entry point for creating a new GetStats object.
65+
* @param builder The builder instance containing all the values.
66+
*/
67+
private GetStats(Builder builder) {
68+
this.existsCount = builder.existsCount;
69+
this.existsTimeInMillis = builder.existsTimeInMillis;
70+
this.missingCount = builder.missingCount;
71+
this.missingTimeInMillis = builder.missingTimeInMillis;
72+
this.current = builder.current;
73+
}
74+
6275
public GetStats(StreamInput in) throws IOException {
6376
existsCount = in.readVLong();
6477
existsTimeInMillis = in.readVLong();
@@ -67,6 +80,11 @@ public GetStats(StreamInput in) throws IOException {
6780
current = in.readVLong();
6881
}
6982

83+
/**
84+
* This constructor will be deprecated starting in version 3.4.0.
85+
* Use {@link Builder} instead.
86+
*/
87+
@Deprecated
7088
public GetStats(long existsCount, long existsTimeInMillis, long missingCount, long missingTimeInMillis, long current) {
7189
this.existsCount = existsCount;
7290
this.existsTimeInMillis = existsTimeInMillis;
@@ -134,6 +152,54 @@ public long current() {
134152
return this.current;
135153
}
136154

155+
/**
156+
* Builder for the {@link GetStats} class.
157+
* Provides a fluent API for constructing a GetStats object.
158+
*/
159+
public static class Builder {
160+
private long existsCount = 0;
161+
private long existsTimeInMillis = 0;
162+
private long missingCount = 0;
163+
private long missingTimeInMillis = 0;
164+
private long current = 0;
165+
166+
public Builder() {}
167+
168+
public Builder existsCount(long count) {
169+
this.existsCount = count;
170+
return this;
171+
}
172+
173+
public Builder existsTimeInMillis(long time) {
174+
this.existsTimeInMillis = time;
175+
return this;
176+
}
177+
178+
public Builder missingCount(long count) {
179+
this.missingCount = count;
180+
return this;
181+
}
182+
183+
public Builder missingTimeInMillis(long time) {
184+
this.missingTimeInMillis = time;
185+
return this;
186+
}
187+
188+
public Builder current(long current) {
189+
this.current = current;
190+
return this;
191+
}
192+
193+
/**
194+
* Creates a {@link GetStats} object from the builder's current state.
195+
*
196+
* @return A new GetStats instance.
197+
*/
198+
public GetStats build() {
199+
return new GetStats(this);
200+
}
201+
}
202+
137203
@Override
138204
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
139205
builder.startObject(Fields.GET);

server/src/main/java/org/opensearch/index/get/ShardGetService.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,12 @@ public ShardGetService(IndexSettings indexSettings, IndexShard indexShard, Mappe
107107
}
108108

109109
public GetStats stats() {
110-
return new GetStats(
111-
existsMetric.count(),
112-
TimeUnit.NANOSECONDS.toMillis(existsMetric.sum()),
113-
missingMetric.count(),
114-
TimeUnit.NANOSECONDS.toMillis(missingMetric.sum()),
115-
currentMetric.count()
116-
);
110+
return new GetStats.Builder().existsCount(existsMetric.count())
111+
.existsTimeInMillis(TimeUnit.NANOSECONDS.toMillis(existsMetric.sum()))
112+
.missingCount(missingMetric.count())
113+
.missingTimeInMillis(TimeUnit.NANOSECONDS.toMillis(missingMetric.sum()))
114+
.current(currentMetric.count())
115+
.build();
117116
}
118117

119118
public GetResult get(

server/src/main/java/org/opensearch/index/shard/IndexShard.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,10 @@ public RefreshStats refreshStats() {
15131513
}
15141514

15151515
public FlushStats flushStats() {
1516-
return new FlushStats(flushMetric.count(), periodicFlushMetric.count(), TimeUnit.NANOSECONDS.toMillis(flushMetric.sum()));
1516+
return new FlushStats.Builder().total(flushMetric.count())
1517+
.periodic(periodicFlushMetric.count())
1518+
.totalTimeInMillis(TimeUnit.NANOSECONDS.toMillis(flushMetric.sum()))
1519+
.build();
15171520
}
15181521

15191522
public DocsStats docStats() {

server/src/main/java/org/opensearch/indices/IndicesQueryCache.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,24 @@ public QueryCacheStats getStats(ShardId shard) {
183183
// We also have some shared ram usage that we try to distribute to
184184
// proportionally to their number of cache entries of each shard
185185
if (stats.isEmpty()) {
186-
shardStats.add(new QueryCacheStats(sharedRamBytesUsed, 0, 0, 0, 0));
186+
shardStats.add(
187+
new QueryCacheStats.Builder().ramBytesUsed(sharedRamBytesUsed).hitCount(0).missCount(0).cacheCount(0).cacheSize(0).build()
188+
);
187189
} else {
188190
long totalSize = 0;
189191
for (QueryCacheStats s : stats.values()) {
190192
totalSize += s.getCacheSize();
191193
}
192194
final double weight = totalSize == 0 ? 1d / stats.size() : ((double) shardStats.getCacheSize()) / totalSize;
193195
final long additionalRamBytesUsed = Math.round(weight * sharedRamBytesUsed);
194-
shardStats.add(new QueryCacheStats(additionalRamBytesUsed, 0, 0, 0, 0));
196+
shardStats.add(
197+
new QueryCacheStats.Builder().ramBytesUsed(additionalRamBytesUsed)
198+
.hitCount(0)
199+
.missCount(0)
200+
.cacheCount(0)
201+
.cacheSize(0)
202+
.build()
203+
);
195204
}
196205
return shardStats;
197206
}
@@ -287,7 +296,12 @@ private static class Stats implements Cloneable {
287296
}
288297

289298
QueryCacheStats toQueryCacheStats() {
290-
return new QueryCacheStats(ramBytesUsed, hitCount, missCount, cacheCount, cacheSize);
299+
return new QueryCacheStats.Builder().ramBytesUsed(ramBytesUsed)
300+
.hitCount(hitCount)
301+
.missCount(missCount)
302+
.cacheCount(cacheCount)
303+
.cacheSize(cacheSize)
304+
.build();
291305
}
292306

293307
@Override

server/src/test/java/org/opensearch/action/admin/cluster/node/stats/NodeStatsTests.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,15 +1372,17 @@ private CommonStats createRandomCommonStats() {
13721372
.build();
13731373
commonStats.indexing = new IndexingStats();
13741374
commonStats.completion = new CompletionStats();
1375-
commonStats.flush = new FlushStats(randomLongBetween(0, 100), randomLongBetween(0, 100), randomLongBetween(0, 100));
1375+
commonStats.flush = new FlushStats.Builder().total(randomLongBetween(0, 100))
1376+
.periodic(randomLongBetween(0, 100))
1377+
.totalTimeInMillis(randomLongBetween(0, 100))
1378+
.build();
13761379
commonStats.fieldData = new FieldDataStats(randomLongBetween(0, 100), randomLongBetween(0, 100), null);
1377-
commonStats.queryCache = new QueryCacheStats(
1378-
randomLongBetween(0, 100),
1379-
randomLongBetween(0, 100),
1380-
randomLongBetween(0, 100),
1381-
randomLongBetween(0, 100),
1382-
randomLongBetween(0, 100)
1383-
);
1380+
commonStats.queryCache = new QueryCacheStats.Builder().ramBytesUsed(randomLongBetween(0, 100))
1381+
.hitCount(randomLongBetween(0, 100))
1382+
.missCount(randomLongBetween(0, 100))
1383+
.cacheCount(randomLongBetween(0, 100))
1384+
.cacheSize(randomLongBetween(0, 100))
1385+
.build();
13841386
commonStats.segments = new SegmentsStats();
13851387

13861388
return commonStats;

server/src/test/java/org/opensearch/action/admin/cluster/stats/ClusterStatsNodesTests.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -371,15 +371,17 @@ private CommonStats createRandomCommonStats() {
371371
.build();
372372
commonStats.indexing = new IndexingStats();
373373
commonStats.completion = new CompletionStats();
374-
commonStats.flush = new FlushStats(randomLongBetween(0, 100), randomLongBetween(0, 100), randomLongBetween(0, 100));
374+
commonStats.flush = new FlushStats.Builder().total(randomLongBetween(0, 100))
375+
.periodic(randomLongBetween(0, 100))
376+
.totalTimeInMillis(randomLongBetween(0, 100))
377+
.build();
375378
commonStats.fieldData = new FieldDataStats(randomLongBetween(0, 100), randomLongBetween(0, 100), null);
376-
commonStats.queryCache = new QueryCacheStats(
377-
randomLongBetween(0, 100),
378-
randomLongBetween(0, 100),
379-
randomLongBetween(0, 100),
380-
randomLongBetween(0, 100),
381-
randomLongBetween(0, 100)
382-
);
379+
commonStats.queryCache = new QueryCacheStats.Builder().ramBytesUsed(randomLongBetween(0, 100))
380+
.hitCount(randomLongBetween(0, 100))
381+
.missCount(randomLongBetween(0, 100))
382+
.cacheCount(randomLongBetween(0, 100))
383+
.cacheSize(randomLongBetween(0, 100))
384+
.build();
383385
commonStats.segments = new SegmentsStats();
384386

385387
return commonStats;

0 commit comments

Comments
 (0)