-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lazyquerier: deep copy hints (#10228)
* lazyquerier: deep copy hints The promQL engine can modify the grouping that it passes to us after calling. First the LazyQuerier is called in [`populateSeries`](https://github.com/grafana/mimir/blob/76ab843e3bb3d6517fcd1ee47e919bd9d107ef0d/vendor/github.com/prometheus/prometheus/promql/engine.go#L953-L976), and then the grouping is changed (via sorting) when evaluating the [aggregate expression](https://github.com/grafana/mimir/blob/76ab843e3bb3d6517fcd1ee47e919bd9d107ef0d/vendor/github.com/prometheus/prometheus/promql/engine.go#L1571-L1573) Signed-off-by: Dimitar Dimitrov <[email protected]> * Don't copy nil params Signed-off-by: Dimitar Dimitrov <[email protected]> * Fix filename typo Signed-off-by: Dimitar Dimitrov <[email protected]> * Clean up code a bit Signed-off-by: Dimitar Dimitrov <[email protected]> --------- Signed-off-by: Dimitar Dimitrov <[email protected]>
- Loading branch information
1 parent
a53f79d
commit aeff3be
Showing
2 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package lazyquery | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/prometheus/prometheus/storage" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
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 | ||
assert.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 | ||
assert.Equal(t, originalField.Interface(), copiedField.Interface(), "Field %s has different values", typ.Field(i).Name) | ||
|
||
switch originalField.Kind() { | ||
// For reference types, ensure they point to different memory | ||
case reflect.Slice, reflect.Map, reflect.Ptr: | ||
if !originalField.IsNil() { | ||
assert.NotEqual(t, originalField.UnsafePointer(), copiedField.UnsafePointer(), "Field %s shares memory between original and copy", typ.Field(i).Name) | ||
} | ||
default: | ||
// Any other types are copied by value, so the assert.Equal above is enough. | ||
} | ||
} | ||
} |