Skip to content

Commit 1ee30dc

Browse files
authored
Refactor ShardStats, WarmerStats and IndexingPressureStats with Builder pattern (#19966)
1 parent 8bf2a18 commit 1ee30dc

File tree

18 files changed

+393
-110
lines changed

18 files changed

+393
-110
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6868
- Refactor the HttpStats, ScriptStats, AdaptiveSelectionStats and OsStats class to use the Builder pattern instead of constructors ([#20014](https://github.com/opensearch-project/OpenSearch/pull/20014))
6969
- Bump opensearch-protobufs dependency to 0.24.0 and update transport-grpc module compatibility ([#20059](https://github.com/opensearch-project/OpenSearch/pull/20059))
7070

71+
- Refactor the ShardStats, WarmerStats and IndexingPressureStats class to use the Builder pattern instead of constructors ([#19966](https://github.com/opensearch-project/OpenSearch/pull/19966))
7172

7273
### Fixed
7374
- Fix Allocation and Rebalance Constraints of WeightFunction are incorrectly reset ([#19012](https://github.com/opensearch-project/OpenSearch/pull/19012))
@@ -142,6 +143,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
142143
- Deprecated existing constructors in IndexPressutreStats, DeviceStats and TransportStats in favor of the new Builder ([#19991](https://github.com/opensearch-project/OpenSearch/pull/19991))
143144
- Deprecated existing constructors in Cache.CacheStats in favor of the new Builder ([#20015](https://github.com/opensearch-project/OpenSearch/pull/20015))
144145
- Deprecated existing constructors in HttpStats, ScriptStats, AdaptiveSelectionStats and OsStats in favor of the new Builder ([#20014](https://github.com/opensearch-project/OpenSearch/pull/20014))
146+
- Deprecated existing constructors in ShardStats, WarmerStats and IndexingPressureStats in favor of the new Builder ([#19966](https://github.com/opensearch-project/OpenSearch/pull/19966))
145147

146148
### Removed
147149

server/src/main/java/org/opensearch/action/admin/cluster/stats/TransportClusterStatsAction.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,14 @@ protected ClusterStatsNodeResponse nodeOperation(ClusterStatsNodeRequest nodeReq
225225
pollingIngestStats = null;
226226
}
227227
shardsStats.add(
228-
new ShardStats(
229-
indexShard.routingEntry(),
230-
indexShard.shardPath(),
231-
new CommonStats(indicesService.getIndicesQueryCache(), indexShard, commonStatsFlags),
232-
commitStats,
233-
seqNoStats,
234-
retentionLeaseStats,
235-
pollingIngestStats
236-
)
228+
new ShardStats.Builder().shardRouting(indexShard.routingEntry())
229+
.shardPath(indexShard.shardPath())
230+
.commonStats(new CommonStats(indicesService.getIndicesQueryCache(), indexShard, commonStatsFlags))
231+
.commitStats(commitStats)
232+
.seqNoStats(seqNoStats)
233+
.retentionLeaseStats(retentionLeaseStats)
234+
.pollingIngestStats(pollingIngestStats)
235+
.build()
237236
);
238237
}
239238
}
@@ -257,6 +256,7 @@ protected ClusterStatsNodeResponse nodeOperation(ClusterStatsNodeRequest nodeReq
257256

258257
/**
259258
* A metric is required when: all cluster stats are required (OR) if the metric is requested
259+
*
260260
* @param metric
261261
* @param clusterStatsRequest
262262
* @return

server/src/main/java/org/opensearch/action/admin/indices/stats/ShardStats.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,23 @@ public RetentionLeaseStats getRetentionLeaseStats() {
8383
private String statePath;
8484
private boolean isCustomDataPath;
8585

86+
/**
87+
* Private constructor that takes a builder.
88+
* This is the sole entry point for creating a new ShardStats object.
89+
* @param builder The builder instance containing all the values.
90+
*/
91+
private ShardStats(Builder builder) {
92+
this.shardRouting = builder.shardRouting;
93+
this.dataPath = builder.shardPath.getRootDataPath().toString();
94+
this.statePath = builder.shardPath.getRootStatePath().toString();
95+
this.isCustomDataPath = builder.shardPath.isCustomDataPath();
96+
this.commitStats = builder.commitStats;
97+
this.commonStats = builder.commonStats;
98+
this.seqNoStats = builder.seqNoStats;
99+
this.retentionLeaseStats = builder.retentionLeaseStats;
100+
this.pollingIngestStats = builder.pollingIngestStats;
101+
}
102+
86103
public ShardStats(StreamInput in) throws IOException {
87104
shardRouting = new ShardRouting(in);
88105
commonStats = new CommonStats(in);
@@ -97,6 +114,11 @@ public ShardStats(StreamInput in) throws IOException {
97114
}
98115
}
99116

117+
/**
118+
* This constructor will be deprecated starting in version 3.4.0.
119+
* Use {@link Builder} instead.
120+
*/
121+
@Deprecated
100122
public ShardStats(
101123
final ShardRouting routing,
102124
final ShardPath shardPath,
@@ -155,6 +177,65 @@ public boolean isCustomDataPath() {
155177
return isCustomDataPath;
156178
}
157179

180+
/**
181+
* Builder for the {@link ShardStats} class.
182+
* Provides a fluent API for constructing a ShardStats object.
183+
*/
184+
public static class Builder {
185+
private ShardRouting shardRouting = null;
186+
private ShardPath shardPath = null;
187+
private CommonStats commonStats = null;
188+
private CommitStats commitStats = null;
189+
private SeqNoStats seqNoStats = null;
190+
private RetentionLeaseStats retentionLeaseStats = null;
191+
private PollingIngestStats pollingIngestStats = null;
192+
193+
public Builder() {}
194+
195+
public Builder shardRouting(ShardRouting shardRouting) {
196+
this.shardRouting = shardRouting;
197+
return this;
198+
}
199+
200+
public Builder shardPath(ShardPath shardPath) {
201+
this.shardPath = shardPath;
202+
return this;
203+
}
204+
205+
public Builder commonStats(CommonStats commonStats) {
206+
this.commonStats = commonStats;
207+
return this;
208+
}
209+
210+
public Builder commitStats(CommitStats commitStats) {
211+
this.commitStats = commitStats;
212+
return this;
213+
}
214+
215+
public Builder seqNoStats(SeqNoStats seqNoStats) {
216+
this.seqNoStats = seqNoStats;
217+
return this;
218+
}
219+
220+
public Builder retentionLeaseStats(RetentionLeaseStats retentionLeaseStats) {
221+
this.retentionLeaseStats = retentionLeaseStats;
222+
return this;
223+
}
224+
225+
public Builder pollingIngestStats(PollingIngestStats pollingIngestStats) {
226+
this.pollingIngestStats = pollingIngestStats;
227+
return this;
228+
}
229+
230+
/**
231+
* Creates a {@link ShardStats} object from the builder's current state.
232+
* @return A new ShardStats instance.
233+
*/
234+
public ShardStats build() {
235+
return new ShardStats(this);
236+
}
237+
}
238+
158239
@Override
159240
public void writeTo(StreamOutput out) throws IOException {
160241
shardRouting.writeTo(out);

server/src/main/java/org/opensearch/action/admin/indices/stats/TransportIndicesStatsAction.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,14 @@ protected ShardStats shardOperation(IndicesStatsRequest request, ShardRouting sh
155155
retentionLeaseStats = null;
156156
pollingIngestStats = null;
157157
}
158-
return new ShardStats(
159-
indexShard.routingEntry(),
160-
indexShard.shardPath(),
161-
commonStats,
162-
commitStats,
163-
seqNoStats,
164-
retentionLeaseStats,
165-
pollingIngestStats
166-
);
158+
return new ShardStats.Builder().shardRouting(indexShard.routingEntry())
159+
.shardPath(indexShard.shardPath())
160+
.commonStats(commonStats)
161+
.commitStats(commitStats)
162+
.seqNoStats(seqNoStats)
163+
.retentionLeaseStats(retentionLeaseStats)
164+
.pollingIngestStats(pollingIngestStats)
165+
.build();
166+
167167
}
168168
}

server/src/main/java/org/opensearch/index/IndexingPressure.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -218,19 +218,18 @@ public long getCurrentReplicaBytes() {
218218
}
219219

220220
public IndexingPressureStats stats() {
221-
return new IndexingPressureStats(
222-
totalCombinedCoordinatingAndPrimaryBytes.get(),
223-
totalCoordinatingBytes.get(),
224-
totalPrimaryBytes.get(),
225-
totalReplicaBytes.get(),
226-
currentCombinedCoordinatingAndPrimaryBytes.get(),
227-
currentCoordinatingBytes.get(),
228-
currentPrimaryBytes.get(),
229-
currentReplicaBytes.get(),
230-
coordinatingRejections.get(),
231-
primaryRejections.get(),
232-
replicaRejections.get(),
233-
primaryAndCoordinatingLimits
234-
);
221+
return new IndexingPressureStats.Builder().totalCombinedCoordinatingAndPrimaryBytes(totalCombinedCoordinatingAndPrimaryBytes.get())
222+
.totalCoordinatingBytes(totalCoordinatingBytes.get())
223+
.totalPrimaryBytes(totalPrimaryBytes.get())
224+
.totalReplicaBytes(totalReplicaBytes.get())
225+
.currentCombinedCoordinatingAndPrimaryBytes(currentCombinedCoordinatingAndPrimaryBytes.get())
226+
.currentCoordinatingBytes(currentCoordinatingBytes.get())
227+
.currentPrimaryBytes(currentPrimaryBytes.get())
228+
.currentReplicaBytes(currentReplicaBytes.get())
229+
.coordinatingRejections(coordinatingRejections.get())
230+
.primaryRejections(primaryRejections.get())
231+
.replicaRejections(replicaRejections.get())
232+
.memoryLimit(primaryAndCoordinatingLimits)
233+
.build();
235234
}
236235
}

server/src/main/java/org/opensearch/index/stats/IndexingPressureStats.java

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,26 @@ public class IndexingPressureStats implements Writeable, ToXContentFragment {
6464
private final long replicaRejections;
6565
private final long memoryLimit;
6666

67+
/**
68+
* Private constructor that takes a builder.
69+
* This is the sole entry point for creating a new IndexingPressureStats object.
70+
* @param builder The builder instance containing all the values.
71+
*/
72+
private IndexingPressureStats(Builder builder) {
73+
this.totalCombinedCoordinatingAndPrimaryBytes = builder.totalCombinedCoordinatingAndPrimaryBytes;
74+
this.totalCoordinatingBytes = builder.totalCoordinatingBytes;
75+
this.totalPrimaryBytes = builder.totalPrimaryBytes;
76+
this.totalReplicaBytes = builder.totalReplicaBytes;
77+
this.currentCombinedCoordinatingAndPrimaryBytes = builder.currentCombinedCoordinatingAndPrimaryBytes;
78+
this.currentCoordinatingBytes = builder.currentCoordinatingBytes;
79+
this.currentPrimaryBytes = builder.currentPrimaryBytes;
80+
this.currentReplicaBytes = builder.currentReplicaBytes;
81+
this.coordinatingRejections = builder.coordinatingRejections;
82+
this.primaryRejections = builder.primaryRejections;
83+
this.replicaRejections = builder.replicaRejections;
84+
this.memoryLimit = builder.memoryLimit;
85+
}
86+
6787
public IndexingPressureStats(StreamInput in) throws IOException {
6888
totalCombinedCoordinatingAndPrimaryBytes = in.readVLong();
6989
totalCoordinatingBytes = in.readVLong();
@@ -81,6 +101,11 @@ public IndexingPressureStats(StreamInput in) throws IOException {
81101
memoryLimit = in.readVLong();
82102
}
83103

104+
/**
105+
* This constructor will be deprecated starting in version 3.4.0.
106+
* Use {@link Builder} instead.
107+
*/
108+
@Deprecated
84109
public IndexingPressureStats(
85110
long totalCombinedCoordinatingAndPrimaryBytes,
86111
long totalCoordinatingBytes,
@@ -109,6 +134,95 @@ public IndexingPressureStats(
109134
this.memoryLimit = memoryLimit;
110135
}
111136

137+
/**
138+
* Builder for the {@link IndexingPressureStats} class.
139+
* Provides a fluent API for constructing a IndexingPressureStats object.
140+
*/
141+
public static class Builder {
142+
private long totalCombinedCoordinatingAndPrimaryBytes = 0;
143+
private long totalCoordinatingBytes = 0;
144+
private long totalPrimaryBytes = 0;
145+
private long totalReplicaBytes = 0;
146+
private long currentCombinedCoordinatingAndPrimaryBytes = 0;
147+
private long currentCoordinatingBytes = 0;
148+
private long currentPrimaryBytes = 0;
149+
private long currentReplicaBytes = 0;
150+
private long coordinatingRejections = 0;
151+
private long primaryRejections = 0;
152+
private long replicaRejections = 0;
153+
private long memoryLimit = 0;
154+
155+
public Builder() {}
156+
157+
public Builder totalCombinedCoordinatingAndPrimaryBytes(long bytes) {
158+
this.totalCombinedCoordinatingAndPrimaryBytes = bytes;
159+
return this;
160+
}
161+
162+
public Builder totalCoordinatingBytes(long bytes) {
163+
this.totalCoordinatingBytes = bytes;
164+
return this;
165+
}
166+
167+
public Builder totalPrimaryBytes(long bytes) {
168+
this.totalPrimaryBytes = bytes;
169+
return this;
170+
}
171+
172+
public Builder totalReplicaBytes(long bytes) {
173+
this.totalReplicaBytes = bytes;
174+
return this;
175+
}
176+
177+
public Builder currentCombinedCoordinatingAndPrimaryBytes(long bytes) {
178+
this.currentCombinedCoordinatingAndPrimaryBytes = bytes;
179+
return this;
180+
}
181+
182+
public Builder currentCoordinatingBytes(long bytes) {
183+
this.currentCoordinatingBytes = bytes;
184+
return this;
185+
}
186+
187+
public Builder currentPrimaryBytes(long bytes) {
188+
this.currentPrimaryBytes = bytes;
189+
return this;
190+
}
191+
192+
public Builder currentReplicaBytes(long bytes) {
193+
this.currentReplicaBytes = bytes;
194+
return this;
195+
}
196+
197+
public Builder coordinatingRejections(long rejections) {
198+
this.coordinatingRejections = rejections;
199+
return this;
200+
}
201+
202+
public Builder primaryRejections(long rejections) {
203+
this.primaryRejections = rejections;
204+
return this;
205+
}
206+
207+
public Builder replicaRejections(long rejections) {
208+
this.replicaRejections = rejections;
209+
return this;
210+
}
211+
212+
public Builder memoryLimit(long limit) {
213+
this.memoryLimit = limit;
214+
return this;
215+
}
216+
217+
/**
218+
* Creates a {@link IndexingPressureStats} object from the builder's current state.
219+
* @return A new IndexingPressureStats instance.
220+
*/
221+
public IndexingPressureStats build() {
222+
return new IndexingPressureStats(this);
223+
}
224+
}
225+
112226
@Override
113227
public void writeTo(StreamOutput out) throws IOException {
114228
out.writeVLong(totalCombinedCoordinatingAndPrimaryBytes);

server/src/main/java/org/opensearch/index/warmer/ShardIndexWarmerService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public void onPostWarm(long tookInNanos) {
7171
}
7272

7373
public WarmerStats stats() {
74-
return new WarmerStats(current.count(), warmerMetric.count(), TimeUnit.NANOSECONDS.toMillis(warmerMetric.sum()));
74+
return new WarmerStats.Builder().current(current.count())
75+
.total(warmerMetric.count())
76+
.totalTimeInMillis(TimeUnit.NANOSECONDS.toMillis(warmerMetric.sum()))
77+
.build();
7578
}
7679
}

0 commit comments

Comments
 (0)