Skip to content

Commit

Permalink
Move MultiFingerprintBounds into pkg/storage/blooms/v1 package
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Haudum <[email protected]>
  • Loading branch information
chaudum committed Feb 20, 2024
1 parent 6d4c259 commit 9da070e
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 164 deletions.
35 changes: 0 additions & 35 deletions pkg/bloomgateway/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,38 +184,3 @@ func partitionRequest(req *logproto.FilterChunkRefRequest) []seriesWithBounds {

return result
}

type MultiFingerprintBounds []v1.FingerprintBounds

func (mb MultiFingerprintBounds) Union(target v1.FingerprintBounds) MultiFingerprintBounds {
if len(mb) == 0 {
return MultiFingerprintBounds{target}
}
if len(mb) == 1 {
return mb[0].Union(target)
}

mb = append(mb, target)
slices.SortFunc(mb, func(a, b v1.FingerprintBounds) int {
if a.Less(b) {
return -1
} else if a.Equal(b) {
return 0
} else {
return 1
}
})

var union MultiFingerprintBounds
for i := 0; i < len(mb); i++ {
j := len(union) - 1 // index of last item of union
if j >= 0 && union[j].Max >= mb[i].Min-1 {
union[j] = v1.NewBounds(union[j].Min, max(mb[i].Max, union[j].Max))
} else {
union = append(union, mb[i])
}
}

mb = union
return mb
}
129 changes: 0 additions & 129 deletions pkg/bloomgateway/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,132 +385,3 @@ func createBlockRefsFromBlockData(t *testing.T, tenant string, data []*bloomship
}
return res
}

func Test_MultiFingerprintBounds(t *testing.T) {
for _, tc := range []struct {
desc string
mb MultiFingerprintBounds
target v1.FingerprintBounds
exp MultiFingerprintBounds
}{
{
desc: "no elements",
mb: MultiFingerprintBounds{},
target: v1.NewBounds(0, 9),
exp: MultiFingerprintBounds{
v1.NewBounds(0, 9),
},
},
{
desc: "single element before",
mb: MultiFingerprintBounds{
v1.NewBounds(5, 9),
},
target: v1.NewBounds(15, 19),
exp: MultiFingerprintBounds{
v1.NewBounds(5, 9),
v1.NewBounds(15, 19),
},
},
{
desc: "single element after",
mb: MultiFingerprintBounds{
v1.NewBounds(5, 9),
},
target: v1.NewBounds(0, 3),
exp: MultiFingerprintBounds{
v1.NewBounds(0, 3),
v1.NewBounds(5, 9),
},
},
{
desc: "single element overlapping",
mb: MultiFingerprintBounds{
v1.NewBounds(5, 9),
},
target: v1.NewBounds(0, 14),
exp: MultiFingerprintBounds{
v1.NewBounds(0, 14),
},
},
{
desc: "multiple elements single overlapping",
mb: MultiFingerprintBounds{
v1.NewBounds(5, 9),
v1.NewBounds(15, 19),
},
target: v1.NewBounds(0, 6),
exp: MultiFingerprintBounds{
v1.NewBounds(0, 9),
v1.NewBounds(15, 19),
},
},
{
desc: "multiple elements single overlapping",
mb: MultiFingerprintBounds{
v1.NewBounds(5, 9),
v1.NewBounds(15, 19),
},
target: v1.NewBounds(11, 25),
exp: MultiFingerprintBounds{
v1.NewBounds(5, 9),
v1.NewBounds(11, 25),
},
},
{
desc: "multiple elements combining overlapping",
mb: MultiFingerprintBounds{
v1.NewBounds(5, 9),
v1.NewBounds(15, 19),
},
target: v1.NewBounds(9, 15),
exp: MultiFingerprintBounds{
v1.NewBounds(5, 19),
},
},
{
desc: "combination",
mb: MultiFingerprintBounds{
v1.NewBounds(0, 2),
v1.NewBounds(5, 9),
v1.NewBounds(15, 19),
v1.NewBounds(25, 29),
},
target: v1.NewBounds(9, 15),
exp: MultiFingerprintBounds{
v1.NewBounds(0, 2),
v1.NewBounds(5, 19),
v1.NewBounds(25, 29),
},
},
{
desc: "overlapping ranges",
mb: MultiFingerprintBounds{
v1.NewBounds(0, 6),
v1.NewBounds(5, 15),
},
target: v1.NewBounds(8, 10),
exp: MultiFingerprintBounds{
v1.NewBounds(0, 15),
},
},
{
desc: "disjoint ranges and target is inbetween",
mb: MultiFingerprintBounds{
v1.NewBounds(0, 9),
v1.NewBounds(30, 39),
},
target: v1.NewBounds(15, 19),
exp: MultiFingerprintBounds{
v1.NewBounds(0, 9),
v1.NewBounds(15, 19),
v1.NewBounds(30, 39),
},
},
} {
t.Run(tc.desc, func(t *testing.T) {
res := tc.mb.Union(tc.target)
require.Equal(t, tc.exp, res)
})
}
}
36 changes: 36 additions & 0 deletions pkg/storage/bloom/v1/bounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/pkg/errors"
"github.com/prometheus/common/model"
"golang.org/x/exp/slices"

"github.com/grafana/loki/pkg/util/encoding"
)
Expand Down Expand Up @@ -158,6 +159,41 @@ func (b FingerprintBounds) Unless(target FingerprintBounds) (res []FingerprintBo
return res
}

type MultiFingerprintBounds []FingerprintBounds

func (mb MultiFingerprintBounds) Union(target FingerprintBounds) MultiFingerprintBounds {
if len(mb) == 0 {
return MultiFingerprintBounds{target}
}
if len(mb) == 1 {
return mb[0].Union(target)
}

mb = append(mb, target)
slices.SortFunc(mb, func(a, b FingerprintBounds) int {
if a.Less(b) {
return -1
} else if a.Equal(b) {
return 0
} else {

Check warning on line 178 in pkg/storage/bloom/v1/bounds.go

View workflow job for this annotation

GitHub Actions / checks

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
return 1
}
})

var union MultiFingerprintBounds
for i := 0; i < len(mb); i++ {
j := len(union) - 1 // index of last item of union
if j >= 0 && union[j].Max >= mb[i].Min-1 {
union[j] = NewBounds(union[j].Min, max(mb[i].Max, union[j].Max))
} else {
union = append(union, mb[i])
}
}

mb = union
return mb
}

// unused, but illustrative
type BoundedIter[V any] struct {
Iterator[V]
Expand Down
129 changes: 129 additions & 0 deletions pkg/storage/bloom/v1/bounds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,132 @@ func Test_FingerprintBounds_Unless(t *testing.T) {
}, NewBounds(5, 25).Unless(target))
assert.Nil(t, NewBounds(14, 15).Unless(target))
}

func Test_MultiFingerprintBounds(t *testing.T) {
for _, tc := range []struct {
desc string
mb MultiFingerprintBounds
target FingerprintBounds
exp MultiFingerprintBounds
}{
{
desc: "no elements",
mb: MultiFingerprintBounds{},
target: NewBounds(0, 9),
exp: MultiFingerprintBounds{
NewBounds(0, 9),
},
},
{
desc: "single element before",
mb: MultiFingerprintBounds{
NewBounds(5, 9),
},
target: NewBounds(15, 19),
exp: MultiFingerprintBounds{
NewBounds(5, 9),
NewBounds(15, 19),
},
},
{
desc: "single element after",
mb: MultiFingerprintBounds{
NewBounds(5, 9),
},
target: NewBounds(0, 3),
exp: MultiFingerprintBounds{
NewBounds(0, 3),
NewBounds(5, 9),
},
},
{
desc: "single element overlapping",
mb: MultiFingerprintBounds{
NewBounds(5, 9),
},
target: NewBounds(0, 14),
exp: MultiFingerprintBounds{
NewBounds(0, 14),
},
},
{
desc: "multiple elements single overlapping",
mb: MultiFingerprintBounds{
NewBounds(5, 9),
NewBounds(15, 19),
},
target: NewBounds(0, 6),
exp: MultiFingerprintBounds{
NewBounds(0, 9),
NewBounds(15, 19),
},
},
{
desc: "multiple elements single overlapping",
mb: MultiFingerprintBounds{
NewBounds(5, 9),
NewBounds(15, 19),
},
target: NewBounds(11, 25),
exp: MultiFingerprintBounds{
NewBounds(5, 9),
NewBounds(11, 25),
},
},
{
desc: "multiple elements combining overlapping",
mb: MultiFingerprintBounds{
NewBounds(5, 9),
NewBounds(15, 19),
},
target: NewBounds(9, 15),
exp: MultiFingerprintBounds{
NewBounds(5, 19),
},
},
{
desc: "combination",
mb: MultiFingerprintBounds{
NewBounds(0, 2),
NewBounds(5, 9),
NewBounds(15, 19),
NewBounds(25, 29),
},
target: NewBounds(9, 15),
exp: MultiFingerprintBounds{
NewBounds(0, 2),
NewBounds(5, 19),
NewBounds(25, 29),
},
},
{
desc: "overlapping ranges",
mb: MultiFingerprintBounds{
NewBounds(0, 6),
NewBounds(5, 15),
},
target: NewBounds(8, 10),
exp: MultiFingerprintBounds{
NewBounds(0, 15),
},
},
{
desc: "disjoint ranges and target is inbetween",
mb: MultiFingerprintBounds{
NewBounds(0, 9),
NewBounds(30, 39),
},
target: NewBounds(15, 19),
exp: MultiFingerprintBounds{
NewBounds(0, 9),
NewBounds(15, 19),
NewBounds(30, 39),
},
},
} {
t.Run(tc.desc, func(t *testing.T) {
res := tc.mb.Union(tc.target)
assert.Equal(t, tc.exp, res)
})
}
}

0 comments on commit 9da070e

Please sign in to comment.