Files
traces/modules/frontend/cache_keys_test.go
Joe ElliottandGitHub 50db194080 BugFix: Blocks whose end second matches a query range throw an erroneous error (#4783)
* make block metas for search take a time.Time to force the caller to interpret their time correctly

Signed-off-by: Joe Elliott <[email protected]>

* Applied the same fix to cache key

Signed-off-by: Joe Elliott <[email protected]>

* linter i love you, but you you're bringing me down

Signed-off-by: Joe Elliott <[email protected]>

* maybe

Signed-off-by: Joe Elliott <[email protected]>

* only calculate start/end time once in tag sharder

Signed-off-by: Joe Elliott <[email protected]>

* push time.Time out one level

Signed-off-by: Joe Elliott <[email protected]>

---------

Signed-off-by: Joe Elliott <[email protected]>
2025-03-04 10:38:02 -05:00

171 lines
3.9 KiB
Go

package frontend
import (
"testing"
"time"
"github.com/grafana/tempo/pkg/tempopb"
"github.com/grafana/tempo/tempodb/backend"
"github.com/stretchr/testify/require"
)
func TestCacheKeyForJob(t *testing.T) {
tcs := []struct {
name string
tenant string
queryHash uint64
req *tempopb.SearchRequest
meta *backend.BlockMeta
searchPage int
pagesToSearch int
expected string
}{
{
name: "valid!",
tenant: "foo",
queryHash: 42,
req: &tempopb.SearchRequest{
Start: 10,
End: 20,
},
meta: &backend.BlockMeta{
BlockID: backend.MustParse("00000000-0000-0000-0000-000000000123"),
StartTime: time.Unix(15, 0),
EndTime: time.Unix(16, 0),
},
searchPage: 1,
pagesToSearch: 2,
expected: "sj:foo:42:00000000-0000-0000-0000-000000000123:1:2",
},
{
name: "no query hash means no query cache",
queryHash: 0,
req: &tempopb.SearchRequest{
Start: 10,
End: 20,
},
meta: &backend.BlockMeta{
BlockID: backend.MustParse("00000000-0000-0000-0000-000000000123"),
StartTime: time.Unix(15, 0),
EndTime: time.Unix(16, 0),
},
searchPage: 1,
pagesToSearch: 2,
expected: "",
},
{
name: "meta before start time",
queryHash: 42,
req: &tempopb.SearchRequest{
Start: 10,
End: 20,
},
meta: &backend.BlockMeta{
BlockID: backend.MustParse("00000000-0000-0000-0000-000000000123"),
StartTime: time.Unix(5, 0),
EndTime: time.Unix(6, 0),
},
searchPage: 1,
pagesToSearch: 2,
expected: "",
},
{
name: "meta overlaps search start",
queryHash: 42,
req: &tempopb.SearchRequest{
Start: 10,
End: 20,
},
meta: &backend.BlockMeta{
BlockID: backend.MustParse("00000000-0000-0000-0000-000000000123"),
StartTime: time.Unix(5, 0),
EndTime: time.Unix(15, 0),
},
searchPage: 1,
pagesToSearch: 2,
expected: "",
},
{
name: "meta overlaps search end",
queryHash: 42,
req: &tempopb.SearchRequest{
Start: 10,
End: 20,
},
meta: &backend.BlockMeta{
BlockID: backend.MustParse("00000000-0000-0000-0000-000000000123"),
StartTime: time.Unix(15, 0),
EndTime: time.Unix(25, 0),
},
searchPage: 1,
pagesToSearch: 2,
expected: "",
},
{
name: "meta after search range",
queryHash: 42,
req: &tempopb.SearchRequest{
Start: 10,
End: 20,
},
meta: &backend.BlockMeta{
BlockID: backend.MustParse("00000000-0000-0000-0000-000000000123"),
StartTime: time.Unix(25, 0),
EndTime: time.Unix(30, 0),
},
searchPage: 1,
pagesToSearch: 2,
expected: "",
},
{
name: "meta encapsulates search range",
queryHash: 42,
req: &tempopb.SearchRequest{
Start: 10,
End: 20,
},
meta: &backend.BlockMeta{
BlockID: backend.MustParse("00000000-0000-0000-0000-000000000123"),
StartTime: time.Unix(5, 0),
EndTime: time.Unix(30, 0),
},
searchPage: 1,
pagesToSearch: 2,
expected: "",
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
startTime := time.Unix(int64(tc.req.Start), 0)
endTime := time.Unix(int64(tc.req.End), 0)
actual := searchJobCacheKey(tc.tenant, tc.queryHash, startTime, endTime, tc.meta, tc.searchPage, tc.pagesToSearch)
require.Equal(t, tc.expected, actual)
})
}
}
func BenchmarkCacheKeyForJob(b *testing.B) {
req := &tempopb.SearchRequest{
Start: 10,
End: 20,
}
meta := &backend.BlockMeta{
BlockID: backend.MustParse("00000000-0000-0000-0000-000000000123"),
StartTime: time.Unix(15, 0),
EndTime: time.Unix(16, 0),
}
startTime := time.Unix(int64(req.Start), 0)
endTime := time.Unix(int64(req.End), 0)
for i := 0; i < b.N; i++ {
s := searchJobCacheKey("foo", 10, startTime, endTime, meta, 1, 2)
if len(s) == 0 {
b.Fatalf("expected non-empty string")
}
}
}