Skip to content
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

lazyquerier: deep copy hints #10228

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion pkg/storage/lazyquery/lazyquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package lazyquery

import (
"context"
"slices"

"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/storage"
Expand Down Expand Up @@ -49,15 +50,26 @@ func (l LazyQuerier) Select(ctx context.Context, selectSorted bool, params *stor
// make sure there is space in the buffer, to unblock the goroutine and let it die even if nobody is
// waiting for the result yet (or anymore).
future := make(chan storage.SeriesSet, 1)
copiedParams := copyParams(params)
go func() {
future <- l.next.Select(ctx, selectSorted, params, matchers...)
future <- l.next.Select(ctx, selectSorted, copiedParams, matchers...)
}()

return &lazySeriesSet{
future: future,
}
}

func copyParams(params *storage.SelectHints) *storage.SelectHints {
if params == nil {
return nil
}
copiedParams := *params
copiedParams.Grouping = slices.Clone(params.Grouping)

return &copiedParams
}

// LabelValues implements Storage.Querier
func (l LazyQuerier) LabelValues(ctx context.Context, name string, hints *storage.LabelHints, matchers ...*labels.Matcher) ([]string, annotations.Annotations, error) {
return l.next.LabelValues(ctx, name, hints, matchers...)
Expand Down
48 changes: 48 additions & 0 deletions pkg/storage/lazyquery/lazyquery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: AGPL-3.0-only

package lazyquery

import (
"reflect"
"testing"

"github.com/prometheus/prometheus/storage"
"github.com/stretchr/testify/require"
)

func TestCopyParamsDeepCopy(t *testing.T) {
original := &storage.SelectHints{
Start: 1000,
End: 2000,
Step: 10,
Range: 3600,
Func: "rate",
Grouping: []string{"label1", "label2"},
}

copied := copyParams(original)

// First verify the structs themselves are different
require.NotSame(t, original, copied)

// Then check each field is a different pointer
originalVal := reflect.ValueOf(original).Elem()
copiedVal := reflect.ValueOf(copied).Elem()
typ := originalVal.Type()
for i := 0; i < typ.NumField(); i++ {
originalField := originalVal.Field(i)
copiedField := copiedVal.Field(i)

// Check if values are equal
require.Equal(t, originalField.Interface(), copiedField.Interface(), "Field %s has different values", typ.Field(i).Name)

// For reference types, ensure they point to different memory
if originalField.Kind() == reflect.Slice ||
dimitarvdimitrov marked this conversation as resolved.
Show resolved Hide resolved
originalField.Kind() == reflect.Map ||
originalField.Kind() == reflect.Ptr {
if !originalField.IsNil() {
require.NotEqual(t, originalField.UnsafePointer(), copiedField.UnsafePointer(), "Field %s shares memory between original and copy", typ.Field(i).Name)
}
}
}
}
Loading