Skip to content

fix(dataobj): Fixes timerange matching for dataobj files #16222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/dataobj/metastore/metastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func objectOverlapsRange(lbs labels.Labels, start, end time.Time) (bool, string)
if objStart.IsZero() || objEnd.IsZero() {
return false, ""
}
if objStart.Before(start) || objEnd.After(end) {
if objEnd.Before(start) || objStart.After(end) {
return false, ""
}
return true, objPath
Expand Down
117 changes: 117 additions & 0 deletions pkg/dataobj/metastore/metastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package metastore

import (
"context"
"strconv"
"testing"
"time"

"github.com/go-kit/log"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/require"

"github.com/grafana/dskit/backoff"
Expand Down Expand Up @@ -302,3 +304,118 @@ func TestDataObjectsPaths(t *testing.T) {
require.Contains(t, paths, "path5")
})
}

func TestObjectOverlapsRange(t *testing.T) {
testPath := "test/path"

tests := []struct {
name string
objStart time.Time
objEnd time.Time
queryStart time.Time
queryEnd time.Time
wantMatch bool
desc string
}{
{
name: "object fully within query range",
objStart: time.Unix(11, 0),
objEnd: time.Unix(12, 0),
queryStart: time.Unix(10, 0),
queryEnd: time.Unix(13, 0),
wantMatch: true,
desc: "query: [10,13], obj: [11,12]",
},
{
name: "object and query equal",
objStart: time.Unix(11, 0),
objEnd: time.Unix(12, 0),
queryStart: time.Unix(11, 0),
queryEnd: time.Unix(122, 0),
wantMatch: true,
desc: "query: [11,12], obj: [11,12]",
},
{
name: "object fully contains query range",
objStart: time.Unix(10, 0),
objEnd: time.Unix(13, 0),
queryStart: time.Unix(11, 0),
queryEnd: time.Unix(12, 0),
wantMatch: true,
desc: "query: [11,12], obj: [10,13]",
},
{
name: "object overlaps start of query range",
objStart: time.Unix(9, 0),
objEnd: time.Unix(11, 0),
queryStart: time.Unix(10, 0),
queryEnd: time.Unix(12, 0),
wantMatch: true,
desc: "query: [10,12], obj: [9,11]",
},
{
name: "object overlaps end of query range",
objStart: time.Unix(11, 0),
objEnd: time.Unix(13, 0),
queryStart: time.Unix(10, 0),
queryEnd: time.Unix(12, 0),
wantMatch: true,
desc: "query: [10,12], obj: [11,13]",
},
{
name: "object ends before query range",
objStart: time.Unix(8, 0),
objEnd: time.Unix(9, 0),
queryStart: time.Unix(10, 0),
queryEnd: time.Unix(11, 0),
wantMatch: false,
desc: "query: [10,11], obj: [8,9]",
},
{
name: "object starts after query range",
objStart: time.Unix(12, 0),
objEnd: time.Unix(13, 0),
queryStart: time.Unix(10, 0),
queryEnd: time.Unix(11, 0),
wantMatch: false,
desc: "query: [10,11], obj: [12,13]",
},
{
name: "object touches start of query range",
objStart: time.Unix(9, 0),
objEnd: time.Unix(10, 0),
queryStart: time.Unix(10, 0),
queryEnd: time.Unix(11, 0),
wantMatch: true,
desc: "query: [10,11], obj: [9,10]",
},
{
name: "object touches end of query range",
objStart: time.Unix(11, 0),
objEnd: time.Unix(12, 0),
queryStart: time.Unix(10, 0),
queryEnd: time.Unix(11, 0),
wantMatch: true,
desc: "query: [10,11], obj: [11,12]",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create labels with timestamps in nanoseconds
lbs := labels.Labels{
{Name: "__start__", Value: strconv.FormatInt(tt.objStart.UnixNano(), 10)},
{Name: "__end__", Value: strconv.FormatInt(tt.objEnd.UnixNano(), 10)},
{Name: "__path__", Value: testPath},
}

gotMatch, gotPath := objectOverlapsRange(lbs, tt.queryStart, tt.queryEnd)
require.Equal(t, tt.wantMatch, gotMatch, "overlap match failed for %s", tt.desc)
if tt.wantMatch {
require.Equal(t, testPath, gotPath, "path should match when ranges overlap")
} else {
require.Empty(t, gotPath, "path should be empty when ranges don't overlap")
}
})
}
}