Skip to content

Commit eb9cf54

Browse files
committed
just some refactoring
Signed-off-by: Alvin Lin <[email protected]>
1 parent 98af28d commit eb9cf54

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

pkg/querier/queryrange/query_range.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,15 @@ func matrixMerge(resps []*PrometheusResponse) []SampleStream {
349349
// We need to make sure we don't repeat samples. This causes some visualisations to be broken in Grafana.
350350
// The prometheus API is inclusive of start and end timestamps.
351351
if len(existing.Samples) > 0 && len(stream.Samples) > 0 {
352-
if existing.Samples[len(existing.Samples)-1].TimestampMs == stream.Samples[0].TimestampMs {
353-
// Typically this the cases, so optimize by not doing full blown search.
352+
existingEndTs := existing.Samples[len(existing.Samples)-1].TimestampMs
353+
if existingEndTs == stream.Samples[0].TimestampMs {
354+
// Typically this the cases where only 1 sample point overlap,
355+
// so optimize with simple code.
354356
stream.Samples = stream.Samples[1:]
355-
} else {
356-
existingEndTs := existing.Samples[len(existing.Samples)-1].TimestampMs
357-
stream.Samples = searchFirstBiggerTimestamp(existingEndTs, stream.Samples)
358-
}
357+
} else if existingEndTs > stream.Samples[0].TimestampMs {
358+
// Overlap might be big, use heavier algorithm to remove overlap.
359+
stream.Samples = chopOffOverlapPortion(stream.Samples, existingEndTs)
360+
} // else there is no overlap, yay!
359361
}
360362
existing.Samples = append(existing.Samples, stream.Samples...)
361363
output[metric] = existing
@@ -376,11 +378,11 @@ func matrixMerge(resps []*PrometheusResponse) []SampleStream {
376378
return result
377379
}
378380

379-
func searchFirstBiggerTimestamp(targetTs int64, samples []cortexpb.Sample) []cortexpb.Sample {
381+
func chopOffOverlapPortion(samples []cortexpb.Sample, choppingPointTs int64) []cortexpb.Sample {
380382

381383
// assuming stream.Samples is sorted by by timestamp in ascending order.
382384
searchResult := sort.Search(len(samples), func(i int) bool {
383-
return samples[i].TimestampMs > targetTs
385+
return samples[i].TimestampMs > choppingPointTs
384386
})
385387

386388
if searchResult < len(samples) {

0 commit comments

Comments
 (0)