Skip to content

Commit 0f70d99

Browse files
committed
iter
1 parent 87f4d83 commit 0f70d99

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

x-pack/plugin/downsample/src/main/java/org/elasticsearch/xpack/downsample/DownsampleShardIndexer.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,9 @@ public LeafBucketCollector getLeafCollector(final AggregationExecutionContext ag
374374
}
375375

376376
void bulkCollection() throws IOException {
377-
for (LeafDownsampleCollector leafBucketCollector : leafBucketCollectors.reversed()) {
377+
// The leaf bucked collectors newer timestamp go first, other we capture the incorrect last value for counters and labels.
378+
leafBucketCollectors.sort((o1, o2) -> -Long.compare(o1.firstTimeStampForBulkCollection, o2.firstTimeStampForBulkCollection));
379+
for (LeafDownsampleCollector leafBucketCollector : leafBucketCollectors) {
378380
leafBucketCollector.leafBulkCollection();
379381
}
380382
}
@@ -386,6 +388,8 @@ class LeafDownsampleCollector extends LeafBucketCollector {
386388
final FormattedDocValues[] formattedDocValues;
387389
final AbstractDownsampleFieldProducer[] fieldProducers;
388390

391+
// Capture the first timestamp in order to determine which leaf collector's leafBulkCollection() is invoked first.
392+
long firstTimeStampForBulkCollection;
389393
final IntArrayList buffer = new IntArrayList(DOCID_BUFFER_SIZE);
390394
final long timestampBoundStartTime = searchExecutionContext.getIndexSettings().getTimestampBounds().startTime();
391395

@@ -462,6 +466,11 @@ public void collect(int docId, long owningBucketOrd) throws IOException {
462466
}
463467
bucketsCreated++;
464468
}
469+
470+
if (buffer.isEmpty()) {
471+
firstTimeStampForBulkCollection = aggCtx.getTimestamp();
472+
}
473+
// buffer.add() always delegates to system.arraycopy() and checks buffer size for resizing purposes:
465474
buffer.buffer[buffer.elementsCount++] = docId;
466475
if (buffer.size() == DOCID_BUFFER_SIZE) {
467476
bulkCollection();

0 commit comments

Comments
 (0)