diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d76dcf89fffe..2a3ab5f180f51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Change the default value of doc_values in WildcardFieldMapper to true. ([#19796](https://github.com/opensearch-project/OpenSearch/pull/19796)) - Make Engine#loadHistoryUUID() protected and Origin#isFromTranslog() public ([#19753](https://github.com/opensearch-project/OpenSearch/pull/19752)) - Bump opensearch-protobufs dependency to 0.23.0 and update transport-grpc module compatibility ([#19831](https://github.com/opensearch-project/OpenSearch/pull/19831)) +- Refactor the RefreshStats class to use the Builder pattern instead of constructors ([#19835](https://github.com/opensearch-project/OpenSearch/pull/19835)) - Refactor the DocStats and StoreStats class to use the Builder pattern instead of constructors ([#19863](https://github.com/opensearch-project/OpenSearch/pull/19863)) ### Fixed @@ -78,6 +79,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Deprecated - Deprecated existing constructors in ThreadPoolStats.Stats in favor of the new Builder ([#19317](https://github.com/opensearch-project/OpenSearch/pull/19317)) - Deprecated existing constructors in IndexingStats.Stats in favor of the new Builder ([#19306](https://github.com/opensearch-project/OpenSearch/pull/19306)) +- Deprecated existing constructors in RefreshStats in favor of the new Builder ([#19835](https://github.com/opensearch-project/OpenSearch/pull/19835)) - Deprecated existing constructors in DocStats and StoreStats in favor of the new Builder ([#19863](https://github.com/opensearch-project/OpenSearch/pull/19863)) ### Removed diff --git a/server/src/main/java/org/opensearch/index/refresh/RefreshStats.java b/server/src/main/java/org/opensearch/index/refresh/RefreshStats.java index 489ac386f72a0..a05bf3fd4593f 100644 --- a/server/src/main/java/org/opensearch/index/refresh/RefreshStats.java +++ b/server/src/main/java/org/opensearch/index/refresh/RefreshStats.java @@ -83,6 +83,11 @@ public void writeTo(StreamOutput out) throws IOException { out.writeVInt(listeners); } + /** + * This constructor will be deprecated starting in version 3.4.0. + * Use {@link Builder} instead. + */ + @Deprecated public RefreshStats(long total, long totalTimeInMillis, long externalTotal, long externalTotalTimeInMillis, int listeners) { this.total = total; this.totalTimeInMillis = totalTimeInMillis; @@ -167,6 +172,66 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } + /** + * Private constructor that takes a builder. + * This is the sole entry point for creating a new RefreshStats object. + * @param builder The builder instance containing all the values. + */ + private RefreshStats(Builder builder) { + this.total = builder.total; + this.totalTimeInMillis = builder.totalTimeInMillis; + this.externalTotal = builder.externalTotal; + this.externalTotalTimeInMillis = builder.externalTotalTimeInMillis; + this.listeners = builder.listeners; + } + + /** + * Builder for the {@link RefreshStats} class. + * Provides a fluent API for constructing instances. + */ + public static class Builder { + private long total = 0; + private long totalTimeInMillis = 0; + private long externalTotal = 0; + private long externalTotalTimeInMillis = 0; + private int listeners = 0; + + public Builder() {} + + public Builder total(long total) { + this.total = total; + return this; + } + + public Builder totalTimeInMillis(long time) { + this.totalTimeInMillis = time; + return this; + } + + public Builder externalTotal(long total) { + this.externalTotal = total; + return this; + } + + public Builder externalTotalTimeInMillis(long time) { + this.externalTotalTimeInMillis = time; + return this; + } + + public Builder listeners(int listeners) { + this.listeners = listeners; + return this; + } + + /** + * Creates a {@link RefreshStats} object from the builder's current state. + * @return A new RefreshStats instance. + */ + public RefreshStats build() { + return new RefreshStats(this); + } + } + @Override public boolean equals(Object obj) { if (obj == null || obj.getClass() != RefreshStats.class) { diff --git a/server/src/main/java/org/opensearch/index/shard/IndexShard.java b/server/src/main/java/org/opensearch/index/shard/IndexShard.java index 824cb68fb79b5..2c2cc01587131 100644 --- a/server/src/main/java/org/opensearch/index/shard/IndexShard.java +++ b/server/src/main/java/org/opensearch/index/shard/IndexShard.java @@ -1503,13 +1503,12 @@ public long getWritingBytes() { public RefreshStats refreshStats() { int listeners = refreshListeners.pendingCount(); - return new RefreshStats( - refreshMetric.count(), - TimeUnit.NANOSECONDS.toMillis(refreshMetric.sum()), - externalRefreshMetric.count(), - TimeUnit.NANOSECONDS.toMillis(externalRefreshMetric.sum()), - listeners - ); + return new RefreshStats.Builder().total(refreshMetric.count()) + .totalTimeInMillis(TimeUnit.NANOSECONDS.toMillis(refreshMetric.sum())) + .externalTotal(externalRefreshMetric.count()) + .externalTotalTimeInMillis(TimeUnit.NANOSECONDS.toMillis(externalRefreshMetric.sum())) + .listeners(listeners) + .build(); } public FlushStats flushStats() { diff --git a/server/src/main/java/org/opensearch/index/shard/IndexingStats.java b/server/src/main/java/org/opensearch/index/shard/IndexingStats.java index ebad62b69199f..599e0a9fa8e73 100644 --- a/server/src/main/java/org/opensearch/index/shard/IndexingStats.java +++ b/server/src/main/java/org/opensearch/index/shard/IndexingStats.java @@ -209,7 +209,7 @@ public Stats(StreamInput in) throws IOException { } /** - * This constructor will be deprecated starting in version 3.3.0. + * This constructor will be deprecated starting in version 3.4.0. * Use {@link Builder} instead. */ @Deprecated @@ -243,7 +243,7 @@ public Stats( } /** - * This constructor will be deprecated starting in version 3.3.0. + * This constructor will be deprecated starting in version 3.4.0. * Use {@link Builder} instead. */ @Deprecated diff --git a/server/src/main/java/org/opensearch/threadpool/ThreadPool.java b/server/src/main/java/org/opensearch/threadpool/ThreadPool.java index 20426d09b6207..0c9adfe109008 100644 --- a/server/src/main/java/org/opensearch/threadpool/ThreadPool.java +++ b/server/src/main/java/org/opensearch/threadpool/ThreadPool.java @@ -541,7 +541,18 @@ public ThreadPoolStats stats() { continue; } if (holder.info.type == ThreadPoolType.FORK_JOIN) { - stats.add(new ThreadPoolStats.Stats(name, 0, 0, 0, 0, 0, 0, -1, holder.info.getMax())); + stats.add( + new ThreadPoolStats.Stats.Builder().name(name) + .threads(0) + .queue(0) + .active(0) + .rejected(0) + .largest(0) + .completed(0) + .waitTimeNanos(-1) + .parallelism(holder.info.getMax()) + .build() + ); continue; } int threads = -1; diff --git a/server/src/main/java/org/opensearch/threadpool/ThreadPoolStats.java b/server/src/main/java/org/opensearch/threadpool/ThreadPoolStats.java index 9ee058bed9d16..495df2a28cd7d 100644 --- a/server/src/main/java/org/opensearch/threadpool/ThreadPoolStats.java +++ b/server/src/main/java/org/opensearch/threadpool/ThreadPoolStats.java @@ -91,7 +91,7 @@ private Stats(Builder builder) { } /** - * This constructor will be deprecated starting in version 3.3.0. + * This constructor will be deprecated starting in version 3.4.0. * Use {@link Builder} instead. */ @Deprecated @@ -107,6 +107,11 @@ public Stats(String name, int threads, int queue, int active, long rejected, int this.parallelism = -1; } + /** + * This constructor will be deprecated starting in version 3.4.0. + * Use {@link Builder} instead. + */ + @Deprecated public Stats( String name, int threads, diff --git a/server/src/test/java/org/opensearch/index/autoforcemerge/AutoForceMergeManagerTests.java b/server/src/test/java/org/opensearch/index/autoforcemerge/AutoForceMergeManagerTests.java index 34019fb42a802..5ab4ebd19f86f 100644 --- a/server/src/test/java/org/opensearch/index/autoforcemerge/AutoForceMergeManagerTests.java +++ b/server/src/test/java/org/opensearch/index/autoforcemerge/AutoForceMergeManagerTests.java @@ -815,7 +815,18 @@ private ExecutorService setupForceMergeThreadPool(int threadCount) { when(threadPool.executor(ThreadPool.Names.FORCE_MERGE)).thenReturn(executorService); ThreadPoolStats stats = new ThreadPoolStats( - Arrays.asList(new ThreadPoolStats.Stats(ThreadPool.Names.FORCE_MERGE, threadCount, 0, 0, 0, threadCount, 0, -1, -1)) + Arrays.asList( + new ThreadPoolStats.Stats.Builder().name(ThreadPool.Names.FORCE_MERGE) + .threads(threadCount) + .queue(0) + .active(0) + .rejected(0) + .largest(threadCount) + .completed(0) + .waitTimeNanos(-1) + .parallelism(-1) + .build() + ) ); when(threadPool.stats()).thenReturn(stats); diff --git a/server/src/test/java/org/opensearch/index/refresh/RefreshStatsTests.java b/server/src/test/java/org/opensearch/index/refresh/RefreshStatsTests.java index 3a1239a21c889..eeff66e791285 100644 --- a/server/src/test/java/org/opensearch/index/refresh/RefreshStatsTests.java +++ b/server/src/test/java/org/opensearch/index/refresh/RefreshStatsTests.java @@ -41,13 +41,13 @@ public class RefreshStatsTests extends OpenSearchTestCase { public void testSerialize() throws IOException { - RefreshStats stats = new RefreshStats( - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - between(0, Integer.MAX_VALUE) - ); + RefreshStats stats = new RefreshStats.Builder().total(randomNonNegativeLong()) + .totalTimeInMillis(randomNonNegativeLong()) + .externalTotal(randomNonNegativeLong()) + .externalTotalTimeInMillis(randomNonNegativeLong()) + .listeners(between(0, Integer.MAX_VALUE)) + .build(); + BytesStreamOutput out = new BytesStreamOutput(); stats.writeTo(out); StreamInput input = out.bytes().streamInput(); diff --git a/server/src/test/java/org/opensearch/rest/action/cat/RestThreadPoolActionRowTests.java b/server/src/test/java/org/opensearch/rest/action/cat/RestThreadPoolActionRowTests.java index dc79e73f050f8..9355fa0931b29 100644 --- a/server/src/test/java/org/opensearch/rest/action/cat/RestThreadPoolActionRowTests.java +++ b/server/src/test/java/org/opensearch/rest/action/cat/RestThreadPoolActionRowTests.java @@ -56,7 +56,16 @@ public void testForkJoinRow() { final int parallelism = 7; ThreadPool.Info fjInfo = new ThreadPool.Info(poolName, ThreadPool.ThreadPoolType.FORK_JOIN, parallelism, parallelism, null, null); - ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats(poolName, 0, 0, 0, 0, 0, 0, -1, parallelism); + ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats.Builder().name(poolName) + .threads(0) + .queue(0) + .active(0) + .rejected(0) + .largest(0) + .completed(0) + .waitTimeNanos(-1) + .parallelism(parallelism) + .build(); Table table = action.getTableWithHeader(new FakeRestRequest.Builder(xContentRegistry()).build()); action.writeRow(table, nodeName, nodeId, eid, pid, host, ip, port, poolName, fjInfo, dummyStats); @@ -93,7 +102,16 @@ public void testNonForkJoinRowScaling() { final String poolName = "generic"; ThreadPool.Info scalingInfo = new ThreadPool.Info(poolName, ThreadPool.ThreadPoolType.SCALING, 1, 4, null, null); - ThreadPoolStats.Stats stats = new ThreadPoolStats.Stats(poolName, 3, 2, 1, 5L, 3, 10L, 111L, -1); + ThreadPoolStats.Stats stats = new ThreadPoolStats.Stats.Builder().name(poolName) + .threads(3) + .queue(2) + .active(1) + .rejected(5L) + .largest(3) + .completed(10L) + .waitTimeNanos(111L) + .parallelism(-1) + .build(); Table table = action.getTableWithHeader(new FakeRestRequest.Builder(xContentRegistry()).build()); action.writeRow(table, nodeName, nodeId, eid, pid, host, ip, port, poolName, scalingInfo, stats); @@ -120,7 +138,17 @@ public void testForkJoinRowParallelismZero() { final String poolName = "fj_zero"; final int parallelism = 0; ThreadPool.Info fjInfo = new ThreadPool.Info(poolName, ThreadPool.ThreadPoolType.FORK_JOIN, parallelism, parallelism, null, null); - ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats(poolName, 0, 0, 0, 0, 0, 0, -1, parallelism); + ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats.Builder().name(poolName) + .threads(0) + .queue(0) + .active(0) + .rejected(0) + .largest(0) + .completed(0) + .waitTimeNanos(-1) + .parallelism(parallelism) + .build(); + Table table = action.getTableWithHeader(new FakeRestRequest.Builder(xContentRegistry()).build()); action.writeRow(table, "n", "id", "eid", 1L, "h", "ip", 9300, poolName, fjInfo, dummyStats); assertEquals(parallelism, table.getRows().get(0).get(indexOf(table).get("parallelism")).value); @@ -130,7 +158,16 @@ public void testForkJoinRowParallelismNegative() { final String poolName = "fj_negative"; final int parallelism = -5; ThreadPool.Info fjInfo = new ThreadPool.Info(poolName, ThreadPool.ThreadPoolType.FORK_JOIN, parallelism, parallelism, null, null); - ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats(poolName, 0, 0, 0, 0, 0, 0, -1, parallelism); + ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats.Builder().name(poolName) + .threads(0) + .queue(0) + .active(0) + .rejected(0) + .largest(0) + .completed(0) + .waitTimeNanos(-1) + .parallelism(parallelism) + .build(); Table table = action.getTableWithHeader(new FakeRestRequest.Builder(xContentRegistry()).build()); action.writeRow(table, "n", "id", "eid", 1L, "h", "ip", 9300, poolName, fjInfo, dummyStats); @@ -141,7 +178,16 @@ public void testForkJoinRowNullInfo() { final String poolName = "fj_nullinfo"; final int parallelism = 3; ThreadPool.Info fjInfo = null; // null info - ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats(poolName, 0, 0, 0, 0, 0, 0, -1, parallelism); + ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats.Builder().name(poolName) + .threads(0) + .queue(0) + .active(0) + .rejected(0) + .largest(0) + .completed(0) + .waitTimeNanos(-1) + .parallelism(parallelism) + .build(); Table table = action.getTableWithHeader(new FakeRestRequest.Builder(xContentRegistry()).build()); action.writeRow(table, "n", "id", "eid", 1L, "h", "ip", 9300, poolName, fjInfo, dummyStats); @@ -177,7 +223,16 @@ public void testMultipleForkJoinRows() { null, null ); - ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats(poolNames[i], 0, 0, 0, 0, 0, 0, -1, parallelisms[i]); + ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats.Builder().name(poolNames[i]) + .threads(0) + .queue(0) + .active(0) + .rejected(0) + .largest(0) + .completed(0) + .waitTimeNanos(-1) + .parallelism(parallelisms[i]) + .build(); action.writeRow(table, "n" + i, "id" + i, "eid" + i, 1L, "h" + i, "ip" + i, 9300 + i, poolNames[i], fjInfo, dummyStats); } assertEquals(2, table.getRows().size()); @@ -190,7 +245,16 @@ public void testForkJoinRowLargeParallelism() { final String poolName = "fj_large"; final int parallelism = Integer.MAX_VALUE; ThreadPool.Info fjInfo = new ThreadPool.Info(poolName, ThreadPool.ThreadPoolType.FORK_JOIN, parallelism, parallelism, null, null); - ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats(poolName, 0, 0, 0, 0, 0, 0, -1, parallelism); + ThreadPoolStats.Stats dummyStats = new ThreadPoolStats.Stats.Builder().name(poolName) + .threads(0) + .queue(0) + .active(0) + .rejected(0) + .largest(0) + .completed(0) + .waitTimeNanos(-1) + .parallelism(parallelism) + .build(); Table table = action.getTableWithHeader(new FakeRestRequest.Builder(xContentRegistry()).build()); action.writeRow(table, "n", "id", "eid", 1L, "h", "ip", 9300, poolName, fjInfo, dummyStats); @@ -322,7 +386,16 @@ public void testBuildTableWithForkJoinPool() throws Exception { when(nodesInfoResponse.getNodesMap()).thenReturn(nodeInfoMap); // 5. ThreadPoolStats.Stats for ForkJoin - ThreadPoolStats.Stats fjStats = new ThreadPoolStats.Stats(poolName, 0, 0, 0, 0, 0, 0, -1, parallelism); + ThreadPoolStats.Stats fjStats = new ThreadPoolStats.Stats.Builder().name(poolName) + .threads(0) + .queue(0) + .active(0) + .rejected(0) + .largest(0) + .completed(0) + .waitTimeNanos(-1) + .parallelism(parallelism) + .build(); ThreadPoolStats threadPoolStats = new ThreadPoolStats(new ArrayList<>(List.of(fjStats))); NodeStats nodeStats = mock(NodeStats.class); when(nodeStats.getThreadPool()).thenReturn(threadPoolStats); diff --git a/server/src/test/java/org/opensearch/rest/action/cat/RestThreadPoolActionTests.java b/server/src/test/java/org/opensearch/rest/action/cat/RestThreadPoolActionTests.java index d6f92012067a6..52dacfdc6f00f 100644 --- a/server/src/test/java/org/opensearch/rest/action/cat/RestThreadPoolActionTests.java +++ b/server/src/test/java/org/opensearch/rest/action/cat/RestThreadPoolActionTests.java @@ -26,17 +26,16 @@ public class RestThreadPoolActionTests extends OpenSearchTestCase { public void testForkJoinPoolTypeStatsAreReported() { // Setup for ForkJoinPool stats - ThreadPoolStats.Stats fjStats = new ThreadPoolStats.Stats( - "fork_join", // name - 42, // active - 84, // rejected - 21, // largest - 64, // completed - -1, // queue (should be -1 for FJ) - 1, // threads - 0, // taskTimeNanos (or whatever the last arg is) - 8 // parallelism (for example: 8, or whatever is appropriate for your test) - ); + ThreadPoolStats.Stats fjStats = new ThreadPoolStats.Stats.Builder().name("fork_join") + .threads(1) + .queue(-1) // should be -1 for FJ + .active(42) + .rejected(84) + .largest(21) + .completed(64) + .waitTimeNanos(0) // or whatever the last arg is + .parallelism(8) // for example: 8, or whatever is appropriate for your test + .build(); List statsList = Collections.singletonList(fjStats); diff --git a/server/src/test/java/org/opensearch/threadpool/ThreadPoolStatsTests.java b/server/src/test/java/org/opensearch/threadpool/ThreadPoolStatsTests.java index f5d32a58f3797..cfce6fb3cf68b 100644 --- a/server/src/test/java/org/opensearch/threadpool/ThreadPoolStatsTests.java +++ b/server/src/test/java/org/opensearch/threadpool/ThreadPoolStatsTests.java @@ -87,7 +87,16 @@ public void testThreadPoolStatsSort() throws IOException { public void testStatsParallelismConstructorAndToXContent() throws IOException { // Test constructor and toXContent with parallelism set - ThreadPoolStats.Stats stats = new ThreadPoolStats.Stats("test", 1, 2, 3, 4L, 5, 6L, 7L, 8); + ThreadPoolStats.Stats stats = new ThreadPoolStats.Stats.Builder().name("test") + .threads(1) + .queue(2) + .active(3) + .rejected(4L) + .largest(5) + .completed(6L) + .waitTimeNanos(7L) + .parallelism(8) + .build(); XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); stats.toXContent(builder, ToXContent.EMPTY_PARAMS); @@ -96,7 +105,16 @@ public void testStatsParallelismConstructorAndToXContent() throws IOException { assertTrue(json.contains("\"parallelism\":8")); // Test with parallelism = -1 (should not output the field) - stats = new ThreadPoolStats.Stats("test", 1, 2, 3, 4L, 5, 6L, 7L, -1); + stats = new ThreadPoolStats.Stats.Builder().name("test") + .threads(1) + .queue(2) + .active(3) + .rejected(4L) + .largest(5) + .completed(6L) + .waitTimeNanos(7L) + .parallelism(-1) + .build(); builder = XContentFactory.jsonBuilder(); builder.startObject(); stats.toXContent(builder, ToXContent.EMPTY_PARAMS); @@ -107,7 +125,16 @@ public void testStatsParallelismConstructorAndToXContent() throws IOException { public void testStatsSerializationParallelismVersion() throws IOException { // Serialization for version >= 3.4.0 (parallelism is written and read) - ThreadPoolStats.Stats statsOut = new ThreadPoolStats.Stats("test", 1, 2, 3, 4L, 5, 6L, 7L, 9); + ThreadPoolStats.Stats statsOut = new ThreadPoolStats.Stats.Builder().name("test") + .threads(1) + .queue(2) + .active(3) + .rejected(4L) + .largest(5) + .completed(6L) + .waitTimeNanos(7L) + .parallelism(9) + .build(); BytesStreamOutput out = new BytesStreamOutput(); out.setVersion(Version.V_3_4_0); statsOut.writeTo(out); @@ -127,10 +154,19 @@ public void testStatsSerializationParallelismVersion() throws IOException { } public void testStatsCompareToWithParallelism() { - ThreadPoolStats.Stats s1 = new ThreadPoolStats.Stats("a", 1, 2, 3, 4L, 5, 6L, 7L, 8); - ThreadPoolStats.Stats s2 = new ThreadPoolStats.Stats("a", 1, 2, 3, 4L, 5, 6L, 7L, 8); - ThreadPoolStats.Stats s3 = new ThreadPoolStats.Stats("a", 2, 2, 3, 4L, 5, 6L, 7L, 8); - ThreadPoolStats.Stats s4 = new ThreadPoolStats.Stats("b", 1, 2, 3, 4L, 5, 6L, 7L, 8); + ThreadPoolStats.Stats.Builder builder = new ThreadPoolStats.Stats.Builder().name("a") + .threads(1) + .queue(2) + .active(3) + .rejected(4L) + .largest(5) + .completed(6L) + .waitTimeNanos(7L) + .parallelism(8); + ThreadPoolStats.Stats s1 = builder.build(); + ThreadPoolStats.Stats s2 = builder.build(); + ThreadPoolStats.Stats s3 = builder.threads(2).build(); + ThreadPoolStats.Stats s4 = builder.name("b").build(); assertEquals(0, s1.compareTo(s2)); assertTrue(s1.compareTo(s3) < 0); diff --git a/server/src/test/java/org/opensearch/threadpool/ThreadPoolTests.java b/server/src/test/java/org/opensearch/threadpool/ThreadPoolTests.java index cdc5561fcec38..1cbede9ab11ad 100644 --- a/server/src/test/java/org/opensearch/threadpool/ThreadPoolTests.java +++ b/server/src/test/java/org/opensearch/threadpool/ThreadPoolTests.java @@ -412,7 +412,16 @@ public void close() throws IOException {} // required by abstract base class public void testStatsParallelismConstructorAndToXContent() throws IOException { // 1. Test the full constructor and toXContent with parallelism set - ThreadPoolStats.Stats stats = new ThreadPoolStats.Stats("test", 1, 2, 3, 4L, 5, 6L, 7L, 8); + ThreadPoolStats.Stats stats = new ThreadPoolStats.Stats.Builder().name("test") + .threads(1) + .queue(2) + .active(3) + .rejected(4L) + .largest(5) + .completed(6L) + .waitTimeNanos(7L) + .parallelism(8) + .build(); XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); stats.toXContent(builder, ToXContent.EMPTY_PARAMS); @@ -421,7 +430,16 @@ public void testStatsParallelismConstructorAndToXContent() throws IOException { assertThat(json, containsString("\"parallelism\":8")); // 2. Test with parallelism = -1 (should not output the field) - stats = new ThreadPoolStats.Stats("test", 1, 2, 3, 4L, 5, 6L, 7L, -1); + stats = new ThreadPoolStats.Stats.Builder().name("test") + .threads(1) + .queue(2) + .active(3) + .rejected(4L) + .largest(5) + .completed(6L) + .waitTimeNanos(7L) + .parallelism(-1) + .build(); builder = XContentFactory.jsonBuilder(); builder.startObject(); stats.toXContent(builder, ToXContent.EMPTY_PARAMS); @@ -432,7 +450,16 @@ public void testStatsParallelismConstructorAndToXContent() throws IOException { public void testStatsSerializationParallelismVersion() throws IOException { // 3. Test serialization for version >= 3.4.0 (parallelism is written and read) - ThreadPoolStats.Stats statsOut = new ThreadPoolStats.Stats("test", 1, 2, 3, 4L, 5, 6L, 7L, 9); + ThreadPoolStats.Stats statsOut = new ThreadPoolStats.Stats.Builder().name("test") + .threads(1) + .queue(2) + .active(3) + .rejected(4L) + .largest(5) + .completed(6L) + .waitTimeNanos(7L) + .parallelism(9) + .build(); BytesStreamOutput out = new BytesStreamOutput(); out.setVersion(Version.V_3_4_0); statsOut.writeTo(out); @@ -540,7 +567,16 @@ public void testInfoStreamInputThrowsOnUnknownTypeAndNewVersion() throws IOExcep } public void testStatsSerializationParallelismNegativeValue() throws IOException { - ThreadPoolStats.Stats statsOut = new ThreadPoolStats.Stats("test", 1, 2, 3, 4L, 5, 6L, 7L, -1); + ThreadPoolStats.Stats statsOut = new ThreadPoolStats.Stats.Builder().name("test") + .threads(1) + .queue(2) + .active(3) + .rejected(4L) + .largest(5) + .completed(6L) + .waitTimeNanos(7L) + .parallelism(-1) + .build(); BytesStreamOutput out = new BytesStreamOutput(); out.setVersion(Version.V_3_4_0); statsOut.writeTo(out);