Skip to content

Commit f229797

Browse files
pado0sandeshkr419
andauthored
Refactor RefreshStats with Builder pattern (#19835)
Co-authored-by: Sandesh Kumar <[email protected]>
1 parent db72ca2 commit f229797

File tree

12 files changed

+286
-49
lines changed

12 files changed

+286
-49
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
3434
- Change the default value of doc_values in WildcardFieldMapper to true. ([#19796](https://github.com/opensearch-project/OpenSearch/pull/19796))
3535
- Make Engine#loadHistoryUUID() protected and Origin#isFromTranslog() public ([#19753](https://github.com/opensearch-project/OpenSearch/pull/19752))
3636
- Bump opensearch-protobufs dependency to 0.23.0 and update transport-grpc module compatibility ([#19831](https://github.com/opensearch-project/OpenSearch/pull/19831))
37+
- Refactor the RefreshStats class to use the Builder pattern instead of constructors ([#19835](https://github.com/opensearch-project/OpenSearch/pull/19835))
3738
- Refactor the DocStats and StoreStats class to use the Builder pattern instead of constructors ([#19863](https://github.com/opensearch-project/OpenSearch/pull/19863))
3839
- Pass registry of headers from ActionPlugin.getRestHeaders to ActionPlugin.getRestHandlerWrapper ([#19875](https://github.com/opensearch-project/OpenSearch/pull/19875))
3940

@@ -79,6 +80,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
7980
### Deprecated
8081
- Deprecated existing constructors in ThreadPoolStats.Stats in favor of the new Builder ([#19317](https://github.com/opensearch-project/OpenSearch/pull/19317))
8182
- Deprecated existing constructors in IndexingStats.Stats in favor of the new Builder ([#19306](https://github.com/opensearch-project/OpenSearch/pull/19306))
83+
- Deprecated existing constructors in RefreshStats in favor of the new Builder ([#19835](https://github.com/opensearch-project/OpenSearch/pull/19835))
8284
- Deprecated existing constructors in DocStats and StoreStats in favor of the new Builder ([#19863](https://github.com/opensearch-project/OpenSearch/pull/19863))
8385

8486
### Removed

server/src/main/java/org/opensearch/index/refresh/RefreshStats.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public void writeTo(StreamOutput out) throws IOException {
8383
out.writeVInt(listeners);
8484
}
8585

86+
/**
87+
* This constructor will be deprecated starting in version 3.4.0.
88+
* Use {@link Builder} instead.
89+
*/
90+
@Deprecated
8691
public RefreshStats(long total, long totalTimeInMillis, long externalTotal, long externalTotalTimeInMillis, int listeners) {
8792
this.total = total;
8893
this.totalTimeInMillis = totalTimeInMillis;
@@ -167,6 +172,66 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
167172
return builder;
168173
}
169174

175+
/**
176+
* Private constructor that takes a builder.
177+
* This is the sole entry point for creating a new RefreshStats object.
178+
* @param builder The builder instance containing all the values.
179+
*/
180+
private RefreshStats(Builder builder) {
181+
this.total = builder.total;
182+
this.totalTimeInMillis = builder.totalTimeInMillis;
183+
this.externalTotal = builder.externalTotal;
184+
this.externalTotalTimeInMillis = builder.externalTotalTimeInMillis;
185+
this.listeners = builder.listeners;
186+
}
187+
188+
/**
189+
* Builder for the {@link RefreshStats} class.
190+
* Provides a fluent API for constructing instances.
191+
*/
192+
public static class Builder {
193+
private long total = 0;
194+
private long totalTimeInMillis = 0;
195+
private long externalTotal = 0;
196+
private long externalTotalTimeInMillis = 0;
197+
private int listeners = 0;
198+
199+
public Builder() {}
200+
201+
public Builder total(long total) {
202+
this.total = total;
203+
return this;
204+
}
205+
206+
public Builder totalTimeInMillis(long time) {
207+
this.totalTimeInMillis = time;
208+
return this;
209+
}
210+
211+
public Builder externalTotal(long total) {
212+
this.externalTotal = total;
213+
return this;
214+
}
215+
216+
public Builder externalTotalTimeInMillis(long time) {
217+
this.externalTotalTimeInMillis = time;
218+
return this;
219+
}
220+
221+
public Builder listeners(int listeners) {
222+
this.listeners = listeners;
223+
return this;
224+
}
225+
226+
/**
227+
* Creates a {@link RefreshStats} object from the builder's current state.
228+
* @return A new RefreshStats instance.
229+
*/
230+
public RefreshStats build() {
231+
return new RefreshStats(this);
232+
}
233+
}
234+
170235
@Override
171236
public boolean equals(Object obj) {
172237
if (obj == null || obj.getClass() != RefreshStats.class) {

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,13 +1503,12 @@ public long getWritingBytes() {
15031503

15041504
public RefreshStats refreshStats() {
15051505
int listeners = refreshListeners.pendingCount();
1506-
return new RefreshStats(
1507-
refreshMetric.count(),
1508-
TimeUnit.NANOSECONDS.toMillis(refreshMetric.sum()),
1509-
externalRefreshMetric.count(),
1510-
TimeUnit.NANOSECONDS.toMillis(externalRefreshMetric.sum()),
1511-
listeners
1512-
);
1506+
return new RefreshStats.Builder().total(refreshMetric.count())
1507+
.totalTimeInMillis(TimeUnit.NANOSECONDS.toMillis(refreshMetric.sum()))
1508+
.externalTotal(externalRefreshMetric.count())
1509+
.externalTotalTimeInMillis(TimeUnit.NANOSECONDS.toMillis(externalRefreshMetric.sum()))
1510+
.listeners(listeners)
1511+
.build();
15131512
}
15141513

15151514
public FlushStats flushStats() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public Stats(StreamInput in) throws IOException {
209209
}
210210

211211
/**
212-
* This constructor will be deprecated starting in version 3.3.0.
212+
* This constructor will be deprecated starting in version 3.4.0.
213213
* Use {@link Builder} instead.
214214
*/
215215
@Deprecated
@@ -243,7 +243,7 @@ public Stats(
243243
}
244244

245245
/**
246-
* This constructor will be deprecated starting in version 3.3.0.
246+
* This constructor will be deprecated starting in version 3.4.0.
247247
* Use {@link Builder} instead.
248248
*/
249249
@Deprecated

server/src/main/java/org/opensearch/threadpool/ThreadPool.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,18 @@ public ThreadPoolStats stats() {
541541
continue;
542542
}
543543
if (holder.info.type == ThreadPoolType.FORK_JOIN) {
544-
stats.add(new ThreadPoolStats.Stats(name, 0, 0, 0, 0, 0, 0, -1, holder.info.getMax()));
544+
stats.add(
545+
new ThreadPoolStats.Stats.Builder().name(name)
546+
.threads(0)
547+
.queue(0)
548+
.active(0)
549+
.rejected(0)
550+
.largest(0)
551+
.completed(0)
552+
.waitTimeNanos(-1)
553+
.parallelism(holder.info.getMax())
554+
.build()
555+
);
545556
continue;
546557
}
547558
int threads = -1;

server/src/main/java/org/opensearch/threadpool/ThreadPoolStats.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private Stats(Builder builder) {
9191
}
9292

9393
/**
94-
* This constructor will be deprecated starting in version 3.3.0.
94+
* This constructor will be deprecated starting in version 3.4.0.
9595
* Use {@link Builder} instead.
9696
*/
9797
@Deprecated
@@ -107,6 +107,11 @@ public Stats(String name, int threads, int queue, int active, long rejected, int
107107
this.parallelism = -1;
108108
}
109109

110+
/**
111+
* This constructor will be deprecated starting in version 3.4.0.
112+
* Use {@link Builder} instead.
113+
*/
114+
@Deprecated
110115
public Stats(
111116
String name,
112117
int threads,

server/src/test/java/org/opensearch/index/autoforcemerge/AutoForceMergeManagerTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,18 @@ private ExecutorService setupForceMergeThreadPool(int threadCount) {
815815
when(threadPool.executor(ThreadPool.Names.FORCE_MERGE)).thenReturn(executorService);
816816

817817
ThreadPoolStats stats = new ThreadPoolStats(
818-
Arrays.asList(new ThreadPoolStats.Stats(ThreadPool.Names.FORCE_MERGE, threadCount, 0, 0, 0, threadCount, 0, -1, -1))
818+
Arrays.asList(
819+
new ThreadPoolStats.Stats.Builder().name(ThreadPool.Names.FORCE_MERGE)
820+
.threads(threadCount)
821+
.queue(0)
822+
.active(0)
823+
.rejected(0)
824+
.largest(threadCount)
825+
.completed(0)
826+
.waitTimeNanos(-1)
827+
.parallelism(-1)
828+
.build()
829+
)
819830
);
820831
when(threadPool.stats()).thenReturn(stats);
821832

server/src/test/java/org/opensearch/index/refresh/RefreshStatsTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@
4141
public class RefreshStatsTests extends OpenSearchTestCase {
4242

4343
public void testSerialize() throws IOException {
44-
RefreshStats stats = new RefreshStats(
45-
randomNonNegativeLong(),
46-
randomNonNegativeLong(),
47-
randomNonNegativeLong(),
48-
randomNonNegativeLong(),
49-
between(0, Integer.MAX_VALUE)
50-
);
44+
RefreshStats stats = new RefreshStats.Builder().total(randomNonNegativeLong())
45+
.totalTimeInMillis(randomNonNegativeLong())
46+
.externalTotal(randomNonNegativeLong())
47+
.externalTotalTimeInMillis(randomNonNegativeLong())
48+
.listeners(between(0, Integer.MAX_VALUE))
49+
.build();
50+
5151
BytesStreamOutput out = new BytesStreamOutput();
5252
stats.writeTo(out);
5353
StreamInput input = out.bytes().streamInput();

server/src/test/java/org/opensearch/rest/action/cat/RestThreadPoolActionRowTests.java

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,16 @@ public void testForkJoinRow() {
5656
final int parallelism = 7;
5757

5858
ThreadPool.Info fjInfo = new ThreadPool.Info(poolName, ThreadPool.ThreadPoolType.FORK_JOIN, parallelism, parallelism, null, null);
59-
ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats(poolName, 0, 0, 0, 0, 0, 0, -1, parallelism);
59+
ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats.Builder().name(poolName)
60+
.threads(0)
61+
.queue(0)
62+
.active(0)
63+
.rejected(0)
64+
.largest(0)
65+
.completed(0)
66+
.waitTimeNanos(-1)
67+
.parallelism(parallelism)
68+
.build();
6069

6170
Table table = action.getTableWithHeader(new FakeRestRequest.Builder(xContentRegistry()).build());
6271
action.writeRow(table, nodeName, nodeId, eid, pid, host, ip, port, poolName, fjInfo, dummyStats);
@@ -93,7 +102,16 @@ public void testNonForkJoinRowScaling() {
93102
final String poolName = "generic";
94103

95104
ThreadPool.Info scalingInfo = new ThreadPool.Info(poolName, ThreadPool.ThreadPoolType.SCALING, 1, 4, null, null);
96-
ThreadPoolStats.Stats stats = new ThreadPoolStats.Stats(poolName, 3, 2, 1, 5L, 3, 10L, 111L, -1);
105+
ThreadPoolStats.Stats stats = new ThreadPoolStats.Stats.Builder().name(poolName)
106+
.threads(3)
107+
.queue(2)
108+
.active(1)
109+
.rejected(5L)
110+
.largest(3)
111+
.completed(10L)
112+
.waitTimeNanos(111L)
113+
.parallelism(-1)
114+
.build();
97115

98116
Table table = action.getTableWithHeader(new FakeRestRequest.Builder(xContentRegistry()).build());
99117
action.writeRow(table, nodeName, nodeId, eid, pid, host, ip, port, poolName, scalingInfo, stats);
@@ -120,7 +138,17 @@ public void testForkJoinRowParallelismZero() {
120138
final String poolName = "fj_zero";
121139
final int parallelism = 0;
122140
ThreadPool.Info fjInfo = new ThreadPool.Info(poolName, ThreadPool.ThreadPoolType.FORK_JOIN, parallelism, parallelism, null, null);
123-
ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats(poolName, 0, 0, 0, 0, 0, 0, -1, parallelism);
141+
ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats.Builder().name(poolName)
142+
.threads(0)
143+
.queue(0)
144+
.active(0)
145+
.rejected(0)
146+
.largest(0)
147+
.completed(0)
148+
.waitTimeNanos(-1)
149+
.parallelism(parallelism)
150+
.build();
151+
124152
Table table = action.getTableWithHeader(new FakeRestRequest.Builder(xContentRegistry()).build());
125153
action.writeRow(table, "n", "id", "eid", 1L, "h", "ip", 9300, poolName, fjInfo, dummyStats);
126154
assertEquals(parallelism, table.getRows().get(0).get(indexOf(table).get("parallelism")).value);
@@ -130,7 +158,16 @@ public void testForkJoinRowParallelismNegative() {
130158
final String poolName = "fj_negative";
131159
final int parallelism = -5;
132160
ThreadPool.Info fjInfo = new ThreadPool.Info(poolName, ThreadPool.ThreadPoolType.FORK_JOIN, parallelism, parallelism, null, null);
133-
ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats(poolName, 0, 0, 0, 0, 0, 0, -1, parallelism);
161+
ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats.Builder().name(poolName)
162+
.threads(0)
163+
.queue(0)
164+
.active(0)
165+
.rejected(0)
166+
.largest(0)
167+
.completed(0)
168+
.waitTimeNanos(-1)
169+
.parallelism(parallelism)
170+
.build();
134171

135172
Table table = action.getTableWithHeader(new FakeRestRequest.Builder(xContentRegistry()).build());
136173
action.writeRow(table, "n", "id", "eid", 1L, "h", "ip", 9300, poolName, fjInfo, dummyStats);
@@ -141,7 +178,16 @@ public void testForkJoinRowNullInfo() {
141178
final String poolName = "fj_nullinfo";
142179
final int parallelism = 3;
143180
ThreadPool.Info fjInfo = null; // null info
144-
ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats(poolName, 0, 0, 0, 0, 0, 0, -1, parallelism);
181+
ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats.Builder().name(poolName)
182+
.threads(0)
183+
.queue(0)
184+
.active(0)
185+
.rejected(0)
186+
.largest(0)
187+
.completed(0)
188+
.waitTimeNanos(-1)
189+
.parallelism(parallelism)
190+
.build();
145191

146192
Table table = action.getTableWithHeader(new FakeRestRequest.Builder(xContentRegistry()).build());
147193
action.writeRow(table, "n", "id", "eid", 1L, "h", "ip", 9300, poolName, fjInfo, dummyStats);
@@ -177,7 +223,16 @@ public void testMultipleForkJoinRows() {
177223
null,
178224
null
179225
);
180-
ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats(poolNames[i], 0, 0, 0, 0, 0, 0, -1, parallelisms[i]);
226+
ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats.Builder().name(poolNames[i])
227+
.threads(0)
228+
.queue(0)
229+
.active(0)
230+
.rejected(0)
231+
.largest(0)
232+
.completed(0)
233+
.waitTimeNanos(-1)
234+
.parallelism(parallelisms[i])
235+
.build();
181236
action.writeRow(table, "n" + i, "id" + i, "eid" + i, 1L, "h" + i, "ip" + i, 9300 + i, poolNames[i], fjInfo, dummyStats);
182237
}
183238
assertEquals(2, table.getRows().size());
@@ -190,7 +245,16 @@ public void testForkJoinRowLargeParallelism() {
190245
final String poolName = "fj_large";
191246
final int parallelism = Integer.MAX_VALUE;
192247
ThreadPool.Info fjInfo = new ThreadPool.Info(poolName, ThreadPool.ThreadPoolType.FORK_JOIN, parallelism, parallelism, null, null);
193-
ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats(poolName, 0, 0, 0, 0, 0, 0, -1, parallelism);
248+
ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats.Builder().name(poolName)
249+
.threads(0)
250+
.queue(0)
251+
.active(0)
252+
.rejected(0)
253+
.largest(0)
254+
.completed(0)
255+
.waitTimeNanos(-1)
256+
.parallelism(parallelism)
257+
.build();
194258

195259
Table table = action.getTableWithHeader(new FakeRestRequest.Builder(xContentRegistry()).build());
196260
action.writeRow(table, "n", "id", "eid", 1L, "h", "ip", 9300, poolName, fjInfo, dummyStats);
@@ -322,7 +386,16 @@ public void testBuildTableWithForkJoinPool() throws Exception {
322386
when(nodesInfoResponse.getNodesMap()).thenReturn(nodeInfoMap);
323387

324388
// 5. ThreadPoolStats.Stats for ForkJoin
325-
ThreadPoolStats.Stats fjStats = new ThreadPoolStats.Stats(poolName, 0, 0, 0, 0, 0, 0, -1, parallelism);
389+
ThreadPoolStats.Stats fjStats = new ThreadPoolStats.Stats.Builder().name(poolName)
390+
.threads(0)
391+
.queue(0)
392+
.active(0)
393+
.rejected(0)
394+
.largest(0)
395+
.completed(0)
396+
.waitTimeNanos(-1)
397+
.parallelism(parallelism)
398+
.build();
326399
ThreadPoolStats threadPoolStats = new ThreadPoolStats(new ArrayList<>(List.of(fjStats)));
327400
NodeStats nodeStats = mock(NodeStats.class);
328401
when(nodeStats.getThreadPool()).thenReturn(threadPoolStats);

server/src/test/java/org/opensearch/rest/action/cat/RestThreadPoolActionTests.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,16 @@ public class RestThreadPoolActionTests extends OpenSearchTestCase {
2626

2727
public void testForkJoinPoolTypeStatsAreReported() {
2828
// Setup for ForkJoinPool stats
29-
ThreadPoolStats.Stats fjStats = new ThreadPoolStats.Stats(
30-
"fork_join", // name
31-
42, // active
32-
84, // rejected
33-
21, // largest
34-
64, // completed
35-
-1, // queue (should be -1 for FJ)
36-
1, // threads
37-
0, // taskTimeNanos (or whatever the last arg is)
38-
8 // parallelism (for example: 8, or whatever is appropriate for your test)
39-
);
29+
ThreadPoolStats.Stats fjStats = new ThreadPoolStats.Stats.Builder().name("fork_join")
30+
.threads(1)
31+
.queue(-1) // should be -1 for FJ
32+
.active(42)
33+
.rejected(84)
34+
.largest(21)
35+
.completed(64)
36+
.waitTimeNanos(0) // or whatever the last arg is
37+
.parallelism(8) // for example: 8, or whatever is appropriate for your test
38+
.build();
4039

4140
List<ThreadPoolStats.Stats> statsList = Collections.singletonList(fjStats);
4241

0 commit comments

Comments
 (0)