Skip to content

Commit ecc9605

Browse files
authored
[TSDB] Cache rollup bucket timestamp to reduce rounding cost (#88420)
1 parent a4ec9c6 commit ecc9605

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/v2/RollupShardIndexer.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ private class TimeSeriesBucketCollector extends BucketCollector {
209209
private long bucketsCreated;
210210
private final RollupBucketBuilder rollupBucketBuilder = new RollupBucketBuilder();
211211
long lastTimestamp = Long.MAX_VALUE;
212+
long lastHistoTimestamp = Long.MAX_VALUE;
212213
BytesRef lastTsid = null;
213214

214215
TimeSeriesBucketCollector(BulkProcessor bulkProcessor) {
@@ -232,14 +233,18 @@ public void collect(int docId, long owningBucketOrd) throws IOException {
232233
final BytesRef tsid = aggCtx.getTsid();
233234
assert tsid != null : "Document without [" + TimeSeriesIdFieldMapper.NAME + "] field was found.";
234235
final long timestamp = aggCtx.getTimestamp();
235-
final long histoTimestamp = rounding.round(timestamp);
236+
237+
boolean tsidChanged = tsid.equals(rollupBucketBuilder.tsid()) == false;
238+
if (tsidChanged || timestamp < lastHistoTimestamp) {
239+
lastHistoTimestamp = rounding.round(timestamp);
240+
}
236241

237242
logger.trace(
238243
"Doc: [{}] - _tsid: [{}], @timestamp: [{}}] -> rollup bucket ts: [{}]",
239244
docId,
240245
DocValueFormat.TIME_SERIES_ID.format(tsid),
241246
timestampFormat.format(timestamp),
242-
timestampFormat.format(histoTimestamp)
247+
timestampFormat.format(lastHistoTimestamp)
243248
);
244249

245250
/*
@@ -262,15 +267,15 @@ public void collect(int docId, long owningBucketOrd) throws IOException {
262267
lastTsid = BytesRef.deepCopyOf(tsid);
263268
lastTimestamp = timestamp;
264269

265-
if (tsid.equals(rollupBucketBuilder.tsid()) == false || rollupBucketBuilder.timestamp() != histoTimestamp) {
270+
if (tsidChanged || rollupBucketBuilder.timestamp() != lastHistoTimestamp) {
266271
// Flush rollup doc if not empty
267272
if (rollupBucketBuilder.isEmpty() == false) {
268273
Map<String, Object> doc = rollupBucketBuilder.buildRollupDocument();
269274
indexBucket(doc);
270275
}
271276

272277
// Create new rollup bucket
273-
rollupBucketBuilder.init(tsid, histoTimestamp);
278+
rollupBucketBuilder.init(tsid, lastHistoTimestamp);
274279
bucketsCreated++;
275280
}
276281

0 commit comments

Comments
 (0)