Skip to content

Commit e02b778

Browse files
authored
Refactor TranslogStats and RequestCacheStats with Builder pattern (#19961)
1 parent f0a1dba commit e02b778

File tree

8 files changed

+264
-36
lines changed

8 files changed

+264
-36
lines changed

CHANGELOG.md

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

5657
### Fixed
5758
- Fix Allocation and Rebalance Constraints of WeightFunction are incorrectly reset ([#19012](https://github.com/opensearch-project/OpenSearch/pull/19012))
@@ -108,6 +109,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
108109
- Deprecated existing constructors in RemoteTranslogTransferTracker.Stats and RemoteSegmentTransferTracker.Stats in favor of the new Builder ([#19837](https://github.com/opensearch-project/OpenSearch/pull/19837))
109110
- Deprecated existing constructors in GetStats, FlushStats and QueryCacheStats in favor of the new Builder ([#19935](https://github.com/opensearch-project/OpenSearch/pull/19935))
110111
- Deprecated existing constructors in FieldDataStats and CompletionStats in favor of the new Builder ([#19936](https://github.com/opensearch-project/OpenSearch/pull/19936))
112+
- Deprecated existing constructors in TranslogStats and RequestCacheStats in favor of the new Builder ([#19961](https://github.com/opensearch-project/OpenSearch/pull/19961))
111113

112114
### Removed
113115

server/src/main/java/org/opensearch/index/cache/request/RequestCacheStats.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,30 @@ public class RequestCacheStats implements Writeable, ToXContentFragment {
5757

5858
public RequestCacheStats() {}
5959

60+
/**
61+
* Private constructor that takes a builder.
62+
* This is the sole entry point for creating a new RequestCacheStats object.
63+
* @param builder The builder instance containing all the values.
64+
*/
65+
private RequestCacheStats(Builder builder) {
66+
this.memorySize = builder.memorySize;
67+
this.evictions = builder.evictions;
68+
this.hitCount = builder.hitCount;
69+
this.missCount = builder.missCount;
70+
}
71+
6072
public RequestCacheStats(StreamInput in) throws IOException {
6173
memorySize = in.readVLong();
6274
evictions = in.readVLong();
6375
hitCount = in.readVLong();
6476
missCount = in.readVLong();
6577
}
6678

79+
/**
80+
* This constructor will be deprecated starting in version 3.4.0.
81+
* Use {@link Builder} instead.
82+
*/
83+
@Deprecated
6784
public RequestCacheStats(long memorySize, long evictions, long hitCount, long missCount) {
6885
this.memorySize = memorySize;
6986
this.evictions = evictions;
@@ -98,6 +115,47 @@ public long getMissCount() {
98115
return this.missCount;
99116
}
100117

118+
/**
119+
* Builder for the {@link RequestCacheStats} class.
120+
* Provides a fluent API for constructing a RequestCacheStats object.
121+
*/
122+
public static class Builder {
123+
private long memorySize = 0;
124+
private long evictions = 0;
125+
private long hitCount = 0;
126+
private long missCount = 0;
127+
128+
public Builder() {}
129+
130+
public Builder memorySize(long count) {
131+
this.memorySize = count;
132+
return this;
133+
}
134+
135+
public Builder evictions(long count) {
136+
this.evictions = count;
137+
return this;
138+
}
139+
140+
public Builder hitCount(long count) {
141+
this.hitCount = count;
142+
return this;
143+
}
144+
145+
public Builder missCount(long count) {
146+
this.missCount = count;
147+
return this;
148+
}
149+
150+
/**
151+
* Creates a {@link RequestCacheStats} object from the builder's current state.
152+
* @return A new RequestCacheStats instance.
153+
*/
154+
public RequestCacheStats build() {
155+
return new RequestCacheStats(this);
156+
}
157+
}
158+
101159
@Override
102160
public void writeTo(StreamOutput out) throws IOException {
103161
out.writeVLong(memorySize);

server/src/main/java/org/opensearch/index/cache/request/ShardRequestCache.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ public final class ShardRequestCache {
5454
final CounterMetric missCount = new CounterMetric();
5555

5656
public RequestCacheStats stats() {
57-
return new RequestCacheStats(Math.max(0, totalMetric.count()), evictionsMetric.count(), hitCount.count(), missCount.count());
57+
return new RequestCacheStats.Builder().memorySize(Math.max(0, totalMetric.count()))
58+
.evictions(evictionsMetric.count())
59+
.hitCount(hitCount.count())
60+
.missCount(missCount.count())
61+
.build();
5862
}
5963

6064
public void onHit() {

server/src/main/java/org/opensearch/index/translog/LocalTranslog.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,12 @@ public TranslogStats stats() {
166166
// acquire lock to make the two numbers roughly consistent (no file change half way)
167167
try (ReleasableLock lock = readLock.acquire()) {
168168
long uncommittedGen = getMinGenerationForSeqNo(deletionPolicy.getLocalCheckpointOfSafeCommit() + 1).translogFileGeneration;
169-
return new TranslogStats(
170-
totalOperations(),
171-
sizeInBytes(),
172-
totalOperationsByMinGen(uncommittedGen),
173-
sizeInBytesByMinGen(uncommittedGen),
174-
earliestLastModifiedAge()
175-
);
169+
return new TranslogStats.Builder().numberOfOperations(totalOperations())
170+
.translogSizeInBytes(sizeInBytes())
171+
.uncommittedOperations(totalOperationsByMinGen(uncommittedGen))
172+
.uncommittedSizeInBytes(sizeInBytesByMinGen(uncommittedGen))
173+
.earliestLastModifiedAge(earliestLastModifiedAge())
174+
.build();
176175
}
177176
}
178177

server/src/main/java/org/opensearch/index/translog/Translog.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -937,13 +937,12 @@ public TranslogStats stats() {
937937
// acquire lock to make the two numbers roughly consistent (no file change half way)
938938
try (ReleasableLock lock = readLock.acquire()) {
939939
long uncommittedGen = getMinGenerationForSeqNo(deletionPolicy.getLocalCheckpointOfSafeCommit() + 1).translogFileGeneration;
940-
return new TranslogStats(
941-
totalOperations(),
942-
sizeInBytes(),
943-
totalOperationsByMinGen(uncommittedGen),
944-
sizeInBytesByMinGen(uncommittedGen),
945-
earliestLastModifiedAge()
946-
);
940+
return new TranslogStats.Builder().numberOfOperations(totalOperations())
941+
.translogSizeInBytes(sizeInBytes())
942+
.uncommittedOperations(totalOperationsByMinGen(uncommittedGen))
943+
.uncommittedSizeInBytes(sizeInBytesByMinGen(uncommittedGen))
944+
.earliestLastModifiedAge(earliestLastModifiedAge())
945+
.build();
947946
}
948947
}
949948

server/src/main/java/org/opensearch/index/translog/TranslogStats.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ public TranslogStats() {
6767
remoteTranslogStats = new RemoteTranslogStats();
6868
}
6969

70+
/**
71+
* Private constructor that takes a builder.
72+
* This is the sole entry point for creating a new TranslogStats object.
73+
* @param builder The builder instance containing all the values.
74+
*/
75+
private TranslogStats(Builder builder) {
76+
this.translogSizeInBytes = builder.translogSizeInBytes;
77+
this.numberOfOperations = builder.numberOfOperations;
78+
this.uncommittedSizeInBytes = builder.uncommittedSizeInBytes;
79+
this.uncommittedOperations = builder.uncommittedOperations;
80+
this.earliestLastModifiedAge = builder.earliestLastModifiedAge;
81+
this.remoteTranslogStats = builder.remoteTranslogStats;
82+
}
83+
7084
public TranslogStats(StreamInput in) throws IOException {
7185
numberOfOperations = in.readVInt();
7286
translogSizeInBytes = in.readVLong();
@@ -78,6 +92,11 @@ public TranslogStats(StreamInput in) throws IOException {
7892
: new RemoteTranslogStats();
7993
}
8094

95+
/**
96+
* This constructor will be deprecated starting in version 3.4.0.
97+
* Use {@link TranslogStats.Builder} instead.
98+
*/
99+
@Deprecated
81100
public TranslogStats(
82101
int numberOfOperations,
83102
long translogSizeInBytes,
@@ -159,6 +178,69 @@ public RemoteTranslogStats getRemoteTranslogStats() {
159178
return remoteTranslogStats;
160179
}
161180

181+
/**
182+
* Builder for the {@link TranslogStats} class.
183+
* Provides a fluent API for constructing a TranslogStats object.
184+
*/
185+
public static class Builder {
186+
private int numberOfOperations = 0;
187+
private long translogSizeInBytes = 0;
188+
private int uncommittedOperations = 0;
189+
private long uncommittedSizeInBytes = 0;
190+
private long earliestLastModifiedAge = 0;
191+
private final RemoteTranslogStats remoteTranslogStats = new RemoteTranslogStats();
192+
193+
public Builder() {}
194+
195+
public Builder numberOfOperations(int operations) {
196+
if (operations < 0) {
197+
throw new IllegalArgumentException("numberOfOperations must be >= 0");
198+
}
199+
this.numberOfOperations = operations;
200+
return this;
201+
}
202+
203+
public Builder translogSizeInBytes(long size) {
204+
if (size < 0) {
205+
throw new IllegalArgumentException("translogSizeInBytes must be >= 0");
206+
}
207+
this.translogSizeInBytes = size;
208+
return this;
209+
}
210+
211+
public Builder uncommittedOperations(int operations) {
212+
if (operations < 0) {
213+
throw new IllegalArgumentException("uncommittedOperations must be >= 0");
214+
}
215+
this.uncommittedOperations = operations;
216+
return this;
217+
}
218+
219+
public Builder uncommittedSizeInBytes(long bytes) {
220+
if (bytes < 0) {
221+
throw new IllegalArgumentException("uncommittedSizeInBytes must be >= 0");
222+
}
223+
this.uncommittedSizeInBytes = bytes;
224+
return this;
225+
}
226+
227+
public Builder earliestLastModifiedAge(long age) {
228+
if (age < 0) {
229+
throw new IllegalArgumentException("earliestLastModifiedAge must be >= 0");
230+
}
231+
this.earliestLastModifiedAge = age;
232+
return this;
233+
}
234+
235+
/**
236+
* Creates a {@link TranslogStats} object from the builder's current state.
237+
* @return A new TranslogStats instance.
238+
*/
239+
public TranslogStats build() {
240+
return new TranslogStats(this);
241+
}
242+
}
243+
162244
@Override
163245
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
164246
builder.startObject(TRANSLOG);

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

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,12 @@ public void testShardValidatorWithValidShard() {
398398
getNodeWithRoles(DATA_NODE_1, Set.of(DiscoveryNodeRole.DATA_ROLE))
399399
);
400400
autoForceMergeManager.start();
401-
TranslogStats translogStats = new TranslogStats(0, 0, 0, 0, TimeValue.timeValueSeconds(6).getMillis());
401+
TranslogStats translogStats = new TranslogStats.Builder().numberOfOperations(0)
402+
.translogSizeInBytes(0)
403+
.uncommittedOperations(0)
404+
.uncommittedSizeInBytes(0)
405+
.earliestLastModifiedAge(TimeValue.timeValueSeconds(6).getMillis())
406+
.build();
402407
IndexShard shard = getShard(TEST_INDEX_1, translogStats, 2);
403408
assertTrue(autoForceMergeManager.getShardValidator().validate(shard).isAllowed());
404409
autoForceMergeManager.close();
@@ -410,7 +415,12 @@ public void testShardValidatorWithShardNotInStartedState() {
410415
getNodeWithRoles(DATA_NODE_1, Set.of(DiscoveryNodeRole.DATA_ROLE))
411416
);
412417
autoForceMergeManager.start();
413-
TranslogStats translogStats = new TranslogStats(0, 0, 0, 0, TimeValue.timeValueSeconds(6).getMillis());
418+
TranslogStats translogStats = new TranslogStats.Builder().numberOfOperations(0)
419+
.translogSizeInBytes(0)
420+
.uncommittedOperations(0)
421+
.uncommittedSizeInBytes(0)
422+
.earliestLastModifiedAge(TimeValue.timeValueSeconds(6).getMillis())
423+
.build();
414424
IndexShard shard = getShard(TEST_INDEX_1, translogStats, 2);
415425
when(shard.state()).thenReturn(IndexShardState.RECOVERING);
416426
assertFalse(autoForceMergeManager.getShardValidator().validate(shard).isAllowed());
@@ -437,7 +447,12 @@ public void testShardValidatorWithLowSegmentCount() {
437447
getNodeWithRoles(DATA_NODE_1, Set.of(DiscoveryNodeRole.DATA_ROLE))
438448
);
439449
autoForceMergeManager.start();
440-
TranslogStats translogStats = new TranslogStats(0, 0, 0, 0, TimeValue.timeValueSeconds(5).getMillis());
450+
TranslogStats translogStats = new TranslogStats.Builder().numberOfOperations(0)
451+
.translogSizeInBytes(0)
452+
.uncommittedOperations(0)
453+
.uncommittedSizeInBytes(0)
454+
.earliestLastModifiedAge(TimeValue.timeValueSeconds(5).getMillis())
455+
.build();
441456
IndexShard shard = getShard(TEST_INDEX_1, translogStats, 1);
442457
assertFalse(autoForceMergeManager.getShardValidator().validate(shard).isAllowed());
443458
autoForceMergeManager.close();
@@ -451,7 +466,12 @@ public void testShardValidatorWithRecentTranslog() {
451466
getNodeWithRoles(DATA_NODE_1, Set.of(DiscoveryNodeRole.DATA_ROLE))
452467
);
453468
autoForceMergeManager.start();
454-
TranslogStats translogStats = new TranslogStats(0, 0, 0, 0, TimeValue.timeValueSeconds(1).getMillis());
469+
TranslogStats translogStats = new TranslogStats.Builder().numberOfOperations(0)
470+
.translogSizeInBytes(0)
471+
.uncommittedOperations(0)
472+
.uncommittedSizeInBytes(0)
473+
.earliestLastModifiedAge(TimeValue.timeValueSeconds(1).getMillis())
474+
.build();
455475
IndexShard shard = getShard(TEST_INDEX_1, translogStats, 2);
456476
assertFalse(autoForceMergeManager.getShardValidator().validate(shard).isAllowed());
457477
autoForceMergeManager.close();
@@ -532,7 +552,12 @@ public void testForceMergeOperationOnDataNodeWithFailingMerges() throws IOExcept
532552
when(threadPool.stats()).thenReturn(stats);
533553

534554
IndexService indexService1 = mock(IndexService.class);
535-
TranslogStats translogStats = new TranslogStats(0, 0, 0, 0, TimeValue.timeValueSeconds(6).getMillis());
555+
TranslogStats translogStats = new TranslogStats.Builder().numberOfOperations(0)
556+
.translogSizeInBytes(0)
557+
.uncommittedOperations(0)
558+
.uncommittedSizeInBytes(0)
559+
.earliestLastModifiedAge(TimeValue.timeValueSeconds(6).getMillis())
560+
.build();
536561
IndexShard shard1 = getShard(TEST_INDEX_1, translogStats, 2);
537562
List<IndexShard> indexShards1 = List.of(shard1);
538563
when(indexService1.spliterator()).thenReturn(indexShards1.spliterator());
@@ -592,7 +617,12 @@ public void testForceMergeOperationOnDataNodeOfWarmEnabledCluster() throws IOExc
592617
);
593618
when(threadPool.stats()).thenReturn(stats);
594619
IndexService indexService1 = mock(IndexService.class);
595-
TranslogStats translogStats = new TranslogStats(0, 0, 0, 0, TimeValue.timeValueSeconds(1).getMillis());
620+
TranslogStats translogStats = new TranslogStats.Builder().numberOfOperations(0)
621+
.translogSizeInBytes(0)
622+
.uncommittedOperations(0)
623+
.uncommittedSizeInBytes(0)
624+
.earliestLastModifiedAge(TimeValue.timeValueSeconds(1).getMillis())
625+
.build();
596626
IndexShard shard1 = getShard(TEST_INDEX_1, translogStats, 2);
597627
List<IndexShard> indexShards1 = List.of(shard1);
598628
when(indexService1.spliterator()).thenReturn(indexShards1.spliterator());
@@ -660,7 +690,12 @@ public void testForceMergeOperationOnDataNodeWithThreadInterruption() throws Int
660690
when(threadPool.stats()).thenReturn(stats);
661691

662692
IndexService indexService1 = mock(IndexService.class);
663-
TranslogStats translogStats = new TranslogStats(0, 0, 0, 0, TimeValue.timeValueSeconds(1).getMillis());
693+
TranslogStats translogStats = new TranslogStats.Builder().numberOfOperations(0)
694+
.translogSizeInBytes(0)
695+
.uncommittedOperations(0)
696+
.uncommittedSizeInBytes(0)
697+
.earliestLastModifiedAge(TimeValue.timeValueSeconds(1).getMillis())
698+
.build();
664699
List<IndexShard> indexShards1 = List.of(getShard(TEST_INDEX_1, translogStats, 2));
665700
when(indexService1.spliterator()).thenReturn(indexShards1.spliterator());
666701
List<IndexService> indexServices = List.of(indexService1);
@@ -727,7 +762,12 @@ public void testMergesFailedMetric() throws IOException {
727762
setupIndexServiceWithShard(1024L, 2);
728763

729764
IndexService indexService = mock(IndexService.class);
730-
TranslogStats translogStats = new TranslogStats(0, 0, 0, 0, TimeValue.timeValueSeconds(6).getMillis());
765+
TranslogStats translogStats = new TranslogStats.Builder().numberOfOperations(0)
766+
.translogSizeInBytes(0)
767+
.uncommittedOperations(0)
768+
.uncommittedSizeInBytes(0)
769+
.earliestLastModifiedAge(TimeValue.timeValueSeconds(6).getMillis())
770+
.build();
731771
IndexShard shard = getShard(TEST_INDEX_1, translogStats, 2);
732772
doThrow(new IOException("Test exception")).when(shard).forceMerge(any());
733773

@@ -851,7 +891,12 @@ private ExecutorService setupCompleteEnvironmentForMerge() {
851891

852892
private void setupIndexServiceWithShard(long shardSize, int segmentCount) throws IOException {
853893
IndexService indexService = mock(IndexService.class);
854-
TranslogStats translogStats = new TranslogStats(0, 0, 0, 0, TimeValue.timeValueSeconds(6).getMillis());
894+
TranslogStats translogStats = new TranslogStats.Builder().numberOfOperations(0)
895+
.translogSizeInBytes(0)
896+
.uncommittedOperations(0)
897+
.uncommittedSizeInBytes(0)
898+
.earliestLastModifiedAge(TimeValue.timeValueSeconds(6).getMillis())
899+
.build();
855900
IndexShard shard = getShard(TEST_INDEX_1, translogStats, segmentCount);
856901

857902
if (shardSize != 1024L) {

0 commit comments

Comments
 (0)