Skip to content

Commit 5969a77

Browse files
authored
Refactor IndexPressutreStats, DeviceStats and TransportStats with Builder pattern (opensearch-project#19991)
1 parent 4cb04c3 commit 5969a77

File tree

9 files changed

+386
-110
lines changed

9 files changed

+386
-110
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5555
- Thread Context Preservation by gRPC Interceptor ([#19776](https://github.com/opensearch-project/OpenSearch/pull/19776))
5656
- Update NoOpResult constructors in the Engine to be public ([#19950](https://github.com/opensearch-project/OpenSearch/pull/19950))
5757
- Refactor the TranslogStats and RequestCacheStats class to use the Builder pattern instead of constructors ([#19961](https://github.com/opensearch-project/OpenSearch/pull/19961))
58+
- Refactor the IndexPressutreStats, DeviceStats and TransportStats class to use the Builder pattern instead of constructors ([#19991](https://github.com/opensearch-project/OpenSearch/pull/19991))
5859

5960
### Fixed
6061
- Fix Allocation and Rebalance Constraints of WeightFunction are incorrectly reset ([#19012](https://github.com/opensearch-project/OpenSearch/pull/19012))
@@ -112,6 +113,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
112113
- Deprecated existing constructors in GetStats, FlushStats and QueryCacheStats in favor of the new Builder ([#19935](https://github.com/opensearch-project/OpenSearch/pull/19935))
113114
- Deprecated existing constructors in FieldDataStats and CompletionStats in favor of the new Builder ([#19936](https://github.com/opensearch-project/OpenSearch/pull/19936))
114115
- Deprecated existing constructors in TranslogStats and RequestCacheStats in favor of the new Builder ([#19961](https://github.com/opensearch-project/OpenSearch/pull/19961))
116+
- Deprecated existing constructors in IndexPressutreStats, DeviceStats and TransportStats in favor of the new Builder ([#19991](https://github.com/opensearch-project/OpenSearch/pull/19991))
115117

116118
### Removed
117119

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -401,14 +401,13 @@ ShardIndexingPressureStats shardStats() {
401401
IndexingPressurePerShardStats shardStats = new IndexingPressurePerShardStats(shardEntry.getValue(), isEnforcedMode);
402402
statsPerShard.put(shardEntry.getKey(), shardStats);
403403
}
404-
return new ShardIndexingPressureStats(
405-
statsPerShard,
406-
memoryManager.getTotalNodeLimitsBreachedRejections(),
407-
memoryManager.getTotalLastSuccessfulRequestLimitsBreachedRejections(),
408-
memoryManager.getTotalThroughputDegradationLimitsBreachedRejections(),
409-
shardIndexingPressureSettings.isShardIndexingPressureEnabled(),
410-
isEnforcedMode
411-
);
404+
return new ShardIndexingPressureStats.Builder().shardIndexingPressureStore(statsPerShard)
405+
.totalNodeLimitsBreachedRejections(memoryManager.getTotalNodeLimitsBreachedRejections())
406+
.totalLastSuccessfulRequestLimitsBreachedRejections(memoryManager.getTotalLastSuccessfulRequestLimitsBreachedRejections())
407+
.totalThroughputDegradationLimitsBreachedRejections(memoryManager.getTotalThroughputDegradationLimitsBreachedRejections())
408+
.shardIndexingPressureEnabled(shardIndexingPressureSettings.isShardIndexingPressureEnabled())
409+
.shardIndexingPressureEnforced(isEnforcedMode)
410+
.build();
412411
}
413412

414413
ShardIndexingPressureStats coldStats() {
@@ -419,25 +418,23 @@ ShardIndexingPressureStats coldStats() {
419418
IndexingPressurePerShardStats shardStats = new IndexingPressurePerShardStats(shardEntry.getValue(), isEnforcedMode);
420419
statsPerShard.put(shardEntry.getKey(), shardStats);
421420
}
422-
return new ShardIndexingPressureStats(
423-
statsPerShard,
424-
memoryManager.getTotalNodeLimitsBreachedRejections(),
425-
memoryManager.getTotalLastSuccessfulRequestLimitsBreachedRejections(),
426-
memoryManager.getTotalThroughputDegradationLimitsBreachedRejections(),
427-
shardIndexingPressureSettings.isShardIndexingPressureEnabled(),
428-
isEnforcedMode
429-
);
421+
return new ShardIndexingPressureStats.Builder().shardIndexingPressureStore(statsPerShard)
422+
.totalNodeLimitsBreachedRejections(memoryManager.getTotalNodeLimitsBreachedRejections())
423+
.totalLastSuccessfulRequestLimitsBreachedRejections(memoryManager.getTotalLastSuccessfulRequestLimitsBreachedRejections())
424+
.totalThroughputDegradationLimitsBreachedRejections(memoryManager.getTotalThroughputDegradationLimitsBreachedRejections())
425+
.shardIndexingPressureEnabled(shardIndexingPressureSettings.isShardIndexingPressureEnabled())
426+
.shardIndexingPressureEnforced(isEnforcedMode)
427+
.build();
430428
}
431429

432430
ShardIndexingPressureStats topStats() {
433-
return new ShardIndexingPressureStats(
434-
Collections.emptyMap(),
435-
memoryManager.getTotalNodeLimitsBreachedRejections(),
436-
memoryManager.getTotalLastSuccessfulRequestLimitsBreachedRejections(),
437-
memoryManager.getTotalThroughputDegradationLimitsBreachedRejections(),
438-
shardIndexingPressureSettings.isShardIndexingPressureEnabled(),
439-
shardIndexingPressureSettings.isShardIndexingPressureEnforced()
440-
);
431+
return new ShardIndexingPressureStats.Builder().shardIndexingPressureStore(Collections.emptyMap())
432+
.totalNodeLimitsBreachedRejections(memoryManager.getTotalNodeLimitsBreachedRejections())
433+
.totalLastSuccessfulRequestLimitsBreachedRejections(memoryManager.getTotalLastSuccessfulRequestLimitsBreachedRejections())
434+
.totalThroughputDegradationLimitsBreachedRejections(memoryManager.getTotalThroughputDegradationLimitsBreachedRejections())
435+
.shardIndexingPressureEnabled(shardIndexingPressureSettings.isShardIndexingPressureEnabled())
436+
.shardIndexingPressureEnforced(shardIndexingPressureSettings.isShardIndexingPressureEnforced())
437+
.build();
441438
}
442439

443440
ShardIndexingPressureTracker getShardIndexingPressureTracker(ShardId shardId) {

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ public class ShardIndexingPressureStats implements Writeable, ToXContentFragment
3636
private final boolean shardIndexingPressureEnabled;
3737
private final boolean shardIndexingPressureEnforced;
3838

39+
/**
40+
* Private constructor that takes a builder.
41+
* This is the sole entry point for creating a new ShardIndexingPressureStats object.
42+
* @param builder The builder instance containing all the values.
43+
*/
44+
private ShardIndexingPressureStats(Builder builder) {
45+
this.shardIndexingPressureStore = builder.shardIndexingPressureStore;
46+
this.totalNodeLimitsBreachedRejections = builder.totalNodeLimitsBreachedRejections;
47+
this.totalLastSuccessfulRequestLimitsBreachedRejections = builder.totalLastSuccessfulRequestLimitsBreachedRejections;
48+
this.totalThroughputDegradationLimitsBreachedRejections = builder.totalThroughputDegradationLimitsBreachedRejections;
49+
this.shardIndexingPressureEnabled = builder.shardIndexingPressureEnabled;
50+
this.shardIndexingPressureEnforced = builder.shardIndexingPressureEnforced;
51+
}
52+
3953
public ShardIndexingPressureStats(StreamInput in) throws IOException {
4054
int shardEntries = in.readInt();
4155
shardIndexingPressureStore = new HashMap<>();
@@ -51,6 +65,11 @@ public ShardIndexingPressureStats(StreamInput in) throws IOException {
5165
shardIndexingPressureEnforced = in.readBoolean();
5266
}
5367

68+
/**
69+
* This constructor will be deprecated starting in version 3.4.0.
70+
* Use {@link Builder} instead.
71+
*/
72+
@Deprecated
5473
public ShardIndexingPressureStats(
5574
Map<ShardId, IndexingPressurePerShardStats> shardIndexingPressureStore,
5675
long totalNodeLimitsBreachedRejections,
@@ -85,6 +104,59 @@ public IndexingPressurePerShardStats getIndexingPressureShardStats(ShardId shard
85104
return shardIndexingPressureStore.get(shardId);
86105
}
87106

107+
/**
108+
* Builder for the {@link ShardIndexingPressureStats} class.
109+
* Provides a fluent API for constructing a ShardIndexingPressureStats object.
110+
*/
111+
public static class Builder {
112+
private Map<ShardId, IndexingPressurePerShardStats> shardIndexingPressureStore = null;
113+
private long totalNodeLimitsBreachedRejections = 0;
114+
private long totalLastSuccessfulRequestLimitsBreachedRejections = 0;
115+
private long totalThroughputDegradationLimitsBreachedRejections = 0;
116+
private boolean shardIndexingPressureEnabled = false;
117+
private boolean shardIndexingPressureEnforced = false;
118+
119+
public Builder() {}
120+
121+
public Builder shardIndexingPressureStore(Map<ShardId, IndexingPressurePerShardStats> shardIndexingPressureStore) {
122+
this.shardIndexingPressureStore = shardIndexingPressureStore;
123+
return this;
124+
}
125+
126+
public Builder totalNodeLimitsBreachedRejections(long total) {
127+
this.totalNodeLimitsBreachedRejections = total;
128+
return this;
129+
}
130+
131+
public Builder totalLastSuccessfulRequestLimitsBreachedRejections(long total) {
132+
this.totalLastSuccessfulRequestLimitsBreachedRejections = total;
133+
return this;
134+
}
135+
136+
public Builder totalThroughputDegradationLimitsBreachedRejections(long total) {
137+
this.totalThroughputDegradationLimitsBreachedRejections = total;
138+
return this;
139+
}
140+
141+
public Builder shardIndexingPressureEnabled(boolean enabled) {
142+
this.shardIndexingPressureEnabled = enabled;
143+
return this;
144+
}
145+
146+
public Builder shardIndexingPressureEnforced(boolean enforced) {
147+
this.shardIndexingPressureEnforced = enforced;
148+
return this;
149+
}
150+
151+
/**
152+
* Creates a {@link ShardIndexingPressureStats} object from the builder's current state.
153+
* @return A new ShardIndexingPressureStats instance.
154+
*/
155+
public ShardIndexingPressureStats build() {
156+
return new ShardIndexingPressureStats(this);
157+
}
158+
}
159+
88160
@Override
89161
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
90162
builder.startObject("shard_indexing_pressure");

server/src/main/java/org/opensearch/monitor/fs/FsInfo.java

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,38 @@ public static class DeviceStats implements Writeable, ToXContentFragment {
248248
final long currentIOTime;
249249
final long previousIOTime;
250250

251+
/**
252+
* Private constructor that takes a builder.
253+
* This is the sole entry point for creating a new DeviceStats object.
254+
* @param builder The builder instance containing all the values.
255+
*/
256+
private DeviceStats(Builder builder) {
257+
this.majorDeviceNumber = builder.majorDeviceNumber;
258+
this.minorDeviceNumber = builder.minorDeviceNumber;
259+
this.deviceName = builder.deviceName;
260+
this.currentReadsCompleted = builder.currentReadsCompleted;
261+
this.previousReadsCompleted = builder.previousReadsCompleted;
262+
this.currentSectorsRead = builder.currentSectorsRead;
263+
this.previousSectorsRead = builder.previousSectorsRead;
264+
this.currentWritesCompleted = builder.currentWritesCompleted;
265+
this.previousWritesCompleted = builder.previousWritesCompleted;
266+
this.currentSectorsWritten = builder.currentSectorsWritten;
267+
this.previousSectorsWritten = builder.previousSectorsWritten;
268+
this.currentReadTime = builder.currentReadTime;
269+
this.previousReadTime = builder.previousReadTime;
270+
this.currentWriteTime = builder.currentWriteTime;
271+
this.previousWriteTime = builder.previousWriteTime;
272+
this.currentQueueSize = builder.currentQueueSize;
273+
this.previousQueueSize = builder.previousQueueSize;
274+
this.currentIOTime = builder.currentIOTime;
275+
this.previousIOTime = builder.previousIOTime;
276+
}
277+
278+
/**
279+
* This constructor will be deprecated starting in version 3.4.0.
280+
* Use {@link Builder} instead.
281+
*/
282+
@Deprecated
251283
public DeviceStats(
252284
final int majorDeviceNumber,
253285
final int minorDeviceNumber,
@@ -285,6 +317,11 @@ public DeviceStats(
285317
);
286318
}
287319

320+
/**
321+
* This constructor will be deprecated starting in version 3.4.0.
322+
* Use {@link Builder} instead.
323+
*/
324+
@Deprecated
288325
private DeviceStats(
289326
final int majorDeviceNumber,
290327
final int minorDeviceNumber,
@@ -452,6 +489,109 @@ public String getDeviceName() {
452489
return deviceName;
453490
}
454491

492+
/**
493+
* Builder for the {@link DeviceStats} class.
494+
* Provides a fluent API for constructing a DeviceStats object.
495+
*/
496+
public static class Builder {
497+
private int majorDeviceNumber = 0;
498+
private int minorDeviceNumber = 0;
499+
private String deviceName = "";
500+
private long currentReadsCompleted = 0;
501+
private long previousReadsCompleted = 0;
502+
private long currentSectorsWritten = 0;
503+
private long previousSectorsWritten = 0;
504+
private long currentSectorsRead = 0;
505+
private long previousSectorsRead = 0;
506+
private long currentWritesCompleted = 0;
507+
private long previousWritesCompleted = 0;
508+
private long currentReadTime = 0;
509+
private long previousReadTime = 0;
510+
private long currentWriteTime = 0;
511+
private long previousWriteTime = 0;
512+
private long currentQueueSize = 0;
513+
private long previousQueueSize = 0;
514+
private long currentIOTime = 0;
515+
private long previousIOTime = 0;
516+
517+
public Builder() {}
518+
519+
public Builder majorDeviceNumber(int number) {
520+
this.majorDeviceNumber = number;
521+
return this;
522+
}
523+
524+
public Builder minorDeviceNumber(int number) {
525+
this.minorDeviceNumber = number;
526+
return this;
527+
}
528+
529+
public Builder deviceName(String name) {
530+
this.deviceName = name;
531+
return this;
532+
}
533+
534+
public Builder currentReadsCompleted(long completed) {
535+
this.currentReadsCompleted = completed;
536+
return this;
537+
}
538+
539+
public Builder currentSectorsRead(long read) {
540+
this.currentSectorsRead = read;
541+
return this;
542+
}
543+
544+
public Builder currentWritesCompleted(long completed) {
545+
this.currentWritesCompleted = completed;
546+
return this;
547+
}
548+
549+
public Builder currentSectorsWritten(long written) {
550+
this.currentSectorsWritten = written;
551+
return this;
552+
}
553+
554+
public Builder currentReadTime(long time) {
555+
this.currentReadTime = time;
556+
return this;
557+
}
558+
559+
public Builder currentWriteTime(long time) {
560+
this.currentWriteTime = time;
561+
return this;
562+
}
563+
564+
public Builder currentQueueSize(long size) {
565+
this.currentQueueSize = size;
566+
return this;
567+
}
568+
569+
public Builder currentIOTime(long time) {
570+
this.currentIOTime = time;
571+
return this;
572+
}
573+
574+
public Builder previousDeviceStats(DeviceStats deviceStats) {
575+
this.previousReadsCompleted = (deviceStats != null) ? deviceStats.currentReadsCompleted : -1;
576+
this.previousWritesCompleted = (deviceStats != null) ? deviceStats.currentWritesCompleted : -1;
577+
this.previousSectorsRead = (deviceStats != null) ? deviceStats.currentSectorsRead : -1;
578+
this.previousSectorsWritten = (deviceStats != null) ? deviceStats.currentSectorsWritten : -1;
579+
this.previousReadTime = (deviceStats != null) ? deviceStats.currentReadTime : -1;
580+
this.previousWriteTime = (deviceStats != null) ? deviceStats.currentWriteTime : -1;
581+
this.previousQueueSize = (deviceStats != null) ? deviceStats.currentQueueSize : -1;
582+
this.previousIOTime = (deviceStats != null) ? deviceStats.currentIOTime : -1;
583+
return this;
584+
}
585+
586+
/**
587+
* Creates a {@link DeviceStats} object from the builder's current state.
588+
* @return A new DeviceStats instance.
589+
*/
590+
public DeviceStats build() {
591+
return new DeviceStats(this);
592+
}
593+
}
594+
455595
@Override
456596
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
457597
builder.field("device_name", getDeviceName());

server/src/main/java/org/opensearch/monitor/fs/FsProbe.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,19 @@ final FsInfo.IoStats ioStats(final Set<Tuple<Integer, Integer>> devicesNumbers,
156156
final long writeTime = Long.parseLong(fields[10]);
157157
final long ioTime = fields.length > 12 ? Long.parseLong(fields[12]) : 0;
158158
final long queueSize = fields.length > 13 ? Long.parseLong(fields[13]) : 0;
159-
final FsInfo.DeviceStats deviceStats = new FsInfo.DeviceStats(
160-
majorDeviceNumber,
161-
minorDeviceNumber,
162-
deviceName,
163-
readsCompleted,
164-
sectorsRead,
165-
writesCompleted,
166-
sectorsWritten,
167-
readTime,
168-
writeTime,
169-
queueSize,
170-
ioTime,
171-
deviceMap.get(Tuple.tuple(majorDeviceNumber, minorDeviceNumber))
172-
);
159+
final FsInfo.DeviceStats deviceStats = new FsInfo.DeviceStats.Builder().majorDeviceNumber(majorDeviceNumber)
160+
.minorDeviceNumber(minorDeviceNumber)
161+
.deviceName(deviceName)
162+
.currentReadsCompleted(readsCompleted)
163+
.currentSectorsRead(sectorsRead)
164+
.currentWritesCompleted(writesCompleted)
165+
.currentSectorsWritten(sectorsWritten)
166+
.currentReadTime(readTime)
167+
.currentWriteTime(writeTime)
168+
.currentQueueSize(queueSize)
169+
.currentIOTime(ioTime)
170+
.previousDeviceStats(deviceMap.get(Tuple.tuple(majorDeviceNumber, minorDeviceNumber)))
171+
.build();
173172
devicesStats.add(deviceStats);
174173
}
175174
}

server/src/main/java/org/opensearch/transport/TcpTransport.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -970,14 +970,13 @@ public final TransportStats getStats() {
970970
final long messagesSent = statsTracker.getMessagesSent();
971971
final long messagesReceived = statsTracker.getMessagesReceived();
972972
final long bytesRead = statsTracker.getBytesRead();
973-
return new TransportStats(
974-
acceptedChannels.size(),
975-
outboundConnectionCount.get(),
976-
messagesReceived,
977-
bytesRead,
978-
messagesSent,
979-
bytesWritten
980-
);
973+
return new TransportStats.Builder().serverOpen(acceptedChannels.size())
974+
.totalOutboundConnections(outboundConnectionCount.get())
975+
.rxCount(messagesReceived)
976+
.rxSize(bytesRead)
977+
.txCount(messagesSent)
978+
.txSize(bytesWritten)
979+
.build();
981980
}
982981

983982
/**

0 commit comments

Comments
 (0)