Fix `getFromThrough()` function for ShortRefs (#11386)

**What this PR does / why we need it**:

Since chunks don't necessarily have the same duration, we cannot assume
that the last item of a slice sorted by `From` time also has the
greatest `Through` time.

Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
pull/11320/head
Christian Haudum 2 years ago committed by GitHub
parent abb63b21b9
commit 79693d79ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      pkg/bloomgateway/util.go
  2. 18
      pkg/bloomgateway/util_test.go

@ -5,6 +5,7 @@ import (
"time"
"github.com/prometheus/common/model"
"golang.org/x/exp/slices"
"github.com/grafana/loki/pkg/logproto"
v1 "github.com/grafana/loki/pkg/storage/bloom/v1"
@ -76,15 +77,22 @@ func getDayTime(ts model.Time) time.Time {
return time.Date(ts.Time().Year(), ts.Time().Month(), ts.Time().Day(), 0, 0, 0, 0, time.UTC)
}
// TODO(chaudum): Fix Through time calculation
// getFromThrough assumes a list of ShortRefs sorted by From time
// However, it does also assume that the last item has the highest
// Through time, which might not be the case!
func getFromThrough(refs []*logproto.ShortRef) (model.Time, model.Time) {
if len(refs) == 0 {
return model.Earliest, model.Latest
}
return refs[0].From, refs[len(refs)-1].Through
maxItem := slices.MaxFunc(refs, func(a, b *logproto.ShortRef) int {
if a.Through > b.Through {
return 1
} else if a.Through < b.Through {
return -1
}
return 0
})
return refs[0].From, maxItem.Through
}
// convertToSearches converts a list of line filter expressions to a list of

@ -3,6 +3,7 @@ package bloomgateway
import (
"testing"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
"github.com/grafana/loki/pkg/logproto"
@ -37,6 +38,23 @@ func TestSliceIterWithIndex(t *testing.T) {
})
}
func TestGetFromThrough(t *testing.T) {
chunks := []*logproto.ShortRef{
{From: 0, Through: 6},
{From: 1, Through: 5},
{From: 2, Through: 9},
{From: 3, Through: 8},
{From: 4, Through: 7},
}
from, through := getFromThrough(chunks)
require.Equal(t, model.Time(0), from)
require.Equal(t, model.Time(9), through)
// assert that slice order did not change
require.Equal(t, model.Time(0), chunks[0].From)
require.Equal(t, model.Time(4), chunks[len(chunks)-1].From)
}
func mkBlockRef(minFp, maxFp uint64) bloomshipper.BlockRef {
return bloomshipper.BlockRef{
Ref: bloomshipper.Ref{

Loading…
Cancel
Save