Skip to content

Commit e7609b4

Browse files
committed
refactor usages to the new Builder with Test
Signed-off-by: Jean Kim <bgshhd95@gmail.com>
1 parent 20661f2 commit e7609b4

File tree

2 files changed

+62
-36
lines changed

2 files changed

+62
-36
lines changed

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343

4444
/**
4545
* Internal class that maintains relevant indexing statistics / metrics.
46-
* @see IndexShard
4746
*
4847
* @opensearch.internal
48+
* @see IndexShard
4949
*/
5050
final class InternalIndexingStats implements IndexingOperationListener {
5151
private final StatsHolder totalStats = new StatsHolder();
@@ -154,20 +154,20 @@ static class StatsHolder {
154154
private final MaxMetric maxLastIndexRequestTimestamp = new MaxMetric();
155155

156156
IndexingStats.Stats stats(boolean isThrottled, long currentThrottleMillis) {
157-
return new IndexingStats.Stats(
158-
indexMetric.count(),
159-
TimeUnit.NANOSECONDS.toMillis(indexMetric.sum()),
160-
indexCurrent.count(),
161-
indexFailed.count(),
162-
deleteMetric.count(),
163-
TimeUnit.NANOSECONDS.toMillis(deleteMetric.sum()),
164-
deleteCurrent.count(),
165-
noopUpdates.count(),
166-
isThrottled,
167-
TimeUnit.MILLISECONDS.toMillis(currentThrottleMillis),
168-
new IndexingStats.Stats.DocStatusStats(),
169-
maxLastIndexRequestTimestamp.get()
170-
);
157+
return new IndexingStats.Stats.Builder()
158+
.indexCount(indexMetric.count())
159+
.indexTimeInMillis(TimeUnit.NANOSECONDS.toMillis(indexMetric.sum()))
160+
.indexCurrent(indexCurrent.count())
161+
.indexFailedCount(indexFailed.count())
162+
.deleteCount(deleteMetric.count())
163+
.deleteTimeInMillis(TimeUnit.NANOSECONDS.toMillis(deleteMetric.sum()))
164+
.deleteCurrent(deleteCurrent.count())
165+
.noopUpdateCount(noopUpdates.count())
166+
.isThrottled(isThrottled)
167+
.throttleTimeInMillis(TimeUnit.MILLISECONDS.toMillis(currentThrottleMillis))
168+
.docStatusStats(new IndexingStats.Stats.DocStatusStats())
169+
.maxLastIndexRequestTimestamp(maxLastIndexRequestTimestamp.get())
170+
.build();
171171
}
172172
}
173173
}

server/src/test/java/org/opensearch/index/shard/IndexingStatsTests.java

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,21 @@ public void testMaxLastIndexRequestTimestampAggregation() throws Exception {
128128
long ts1 = randomLongBetween(0, 1000000);
129129
long ts2 = randomLongBetween(0, 1000000);
130130
long ts3 = randomLongBetween(0, 1000000);
131-
IndexingStats.Stats stats1 = new IndexingStats.Stats(1, 2, 3, 4, 5, 6, 7, 8, false, 9, docStatusStats, ts1);
132-
IndexingStats.Stats stats2 = new IndexingStats.Stats(1, 2, 3, 4, 5, 6, 7, 8, false, 9, docStatusStats, ts2);
133-
IndexingStats.Stats stats3 = new IndexingStats.Stats(1, 2, 3, 4, 5, 6, 7, 8, false, 9, docStatusStats, ts3);
131+
IndexingStats.Stats.Builder defaultStats = new IndexingStats.Stats.Builder()
132+
.indexCount(1)
133+
.indexTimeInMillis(2)
134+
.indexCurrent(3)
135+
.indexFailedCount(4)
136+
.deleteCount(5)
137+
.deleteTimeInMillis(6)
138+
.deleteCurrent(7)
139+
.noopUpdateCount(8)
140+
.isThrottled(false)
141+
.throttleTimeInMillis(9)
142+
.docStatusStats(docStatusStats);
143+
IndexingStats.Stats stats1 = defaultStats.maxLastIndexRequestTimestamp(ts1).build();
144+
IndexingStats.Stats stats2 = defaultStats.maxLastIndexRequestTimestamp(ts2).build();
145+
IndexingStats.Stats stats3 = defaultStats.maxLastIndexRequestTimestamp(ts3).build();
134146

135147
// Aggregate stats1 + stats2
136148
stats1.add(stats2);
@@ -141,20 +153,34 @@ public void testMaxLastIndexRequestTimestampAggregation() throws Exception {
141153
assertEquals(Math.max(Math.max(ts1, ts2), ts3), stats1.getMaxLastIndexRequestTimestamp());
142154

143155
// Test with zero and negative values
144-
IndexingStats.Stats statsZero = new IndexingStats.Stats(1, 2, 3, 4, 5, 6, 7, 8, false, 9, docStatusStats, 0L);
145-
IndexingStats.Stats statsNeg = new IndexingStats.Stats(1, 2, 3, 4, 5, 6, 7, 8, false, 9, docStatusStats, -100L);
156+
IndexingStats.Stats statsZero = defaultStats.maxLastIndexRequestTimestamp(0L).build();
157+
IndexingStats.Stats statsNeg = defaultStats.maxLastIndexRequestTimestamp(-100L).build();
158+
146159
statsZero.add(statsNeg);
147160
assertEquals(0L, statsZero.getMaxLastIndexRequestTimestamp());
148161

149-
IndexingStats.Stats statsNeg2 = new IndexingStats.Stats(1, 2, 3, 4, 5, 6, 7, 8, false, 9, docStatusStats, -50L);
162+
IndexingStats.Stats statsNeg2 = defaultStats.maxLastIndexRequestTimestamp(-50L).build();
150163
statsNeg.add(statsNeg2);
151164
assertEquals(-50L, statsNeg.getMaxLastIndexRequestTimestamp());
152165
}
153166

154167
public void testMaxLastIndexRequestTimestampBackwardCompatibility() throws IOException {
155168
IndexingStats.Stats.DocStatusStats docStatusStats = new IndexingStats.Stats.DocStatusStats();
156169
long ts = randomLongBetween(0, 1000000);
157-
IndexingStats.Stats stats = new IndexingStats.Stats(1, 2, 3, 4, 5, 6, 7, 8, false, 9, docStatusStats, ts);
170+
IndexingStats.Stats stats = new IndexingStats.Stats.Builder()
171+
.indexCount(1)
172+
.indexTimeInMillis(2)
173+
.indexCurrent(3)
174+
.indexFailedCount(4)
175+
.deleteCount(5)
176+
.deleteTimeInMillis(6)
177+
.deleteCurrent(7)
178+
.noopUpdateCount(8)
179+
.isThrottled(false)
180+
.throttleTimeInMillis(9)
181+
.docStatusStats(docStatusStats)
182+
.maxLastIndexRequestTimestamp(ts)
183+
.build();
158184

159185
// Serialize with V_3_1_0 (should include the field)
160186
BytesStreamOutput outNew = new BytesStreamOutput();
@@ -181,20 +207,20 @@ private IndexingStats createTestInstance() {
181207
docStatusStats.add(RestStatus.fromCode(i * 100), randomNonNegativeLong());
182208
}
183209

184-
IndexingStats.Stats stats = new IndexingStats.Stats(
185-
randomNonNegativeLong(),
186-
randomNonNegativeLong(),
187-
randomNonNegativeLong(),
188-
randomNonNegativeLong(),
189-
randomNonNegativeLong(),
190-
randomNonNegativeLong(),
191-
randomNonNegativeLong(),
192-
randomNonNegativeLong(),
193-
randomBoolean(),
194-
randomNonNegativeLong(),
195-
docStatusStats,
196-
randomLong()
197-
);
210+
IndexingStats.Stats stats = new IndexingStats.Stats.Builder()
211+
.indexCount(randomNonNegativeLong())
212+
.indexTimeInMillis(randomNonNegativeLong())
213+
.indexCurrent(randomNonNegativeLong())
214+
.indexFailedCount(randomNonNegativeLong())
215+
.deleteCount(randomNonNegativeLong())
216+
.deleteTimeInMillis(randomNonNegativeLong())
217+
.deleteCurrent(randomNonNegativeLong())
218+
.noopUpdateCount(randomNonNegativeLong())
219+
.isThrottled(randomBoolean())
220+
.throttleTimeInMillis(randomNonNegativeLong())
221+
.docStatusStats(docStatusStats)
222+
.maxLastIndexRequestTimestamp(randomLong())
223+
.build();
198224

199225
return new IndexingStats(stats);
200226
}

0 commit comments

Comments
 (0)