-
Notifications
You must be signed in to change notification settings - Fork 9.8k
fix inconsistent histogram chunk iterators #13659
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
107bca1
fix: clarify histogram chunk iterator interface
krajorama 5ed612c
fix: update some histogram chunk iterators to fullfil the interface
krajorama bdf8e3f
Add unit test to verify chunkenc.Iterator interface
krajorama fe7fc65
Refactor test into its own package
krajorama e64784d
Split TestListSeriesIteratorCopiesHistograms along type to be simpler
krajorama 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
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -131,18 +131,18 @@ type Iterator interface { | |
// At returns the current timestamp/value pair if the value is a float. | ||
// Before the iterator has advanced, the behaviour is unspecified. | ||
At() (int64, float64) | ||
// AtHistogram returns the current timestamp/value pair if the value is a | ||
// AtHistogram returns a copy(!) of the current timestamp/value pair if the value is a | ||
// histogram with integer counts. Before the iterator has advanced, the behaviour | ||
// is unspecified. | ||
// The method accepts an optional Histogram object which will be | ||
// The method accepts an optional Histogram object which may be | ||
// reused when not nil. Otherwise, a new Histogram object will be allocated. | ||
AtHistogram(*histogram.Histogram) (int64, *histogram.Histogram) | ||
// AtFloatHistogram returns the current timestamp/value pair if the | ||
// AtFloatHistogram returns a copy(!) of the current timestamp/value pair if the | ||
// value is a histogram with floating-point counts. It also works if the | ||
// value is a histogram with integer counts, in which case a | ||
// FloatHistogram copy of the histogram is returned. Before the iterator | ||
// has advanced, the behaviour is unspecified. | ||
// The method accepts an optional FloatHistogram object which will be | ||
// The method accepts an optional FloatHistogram object which may be | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn’t your PR enforce that the optional FloatHistogram object will be reused? |
||
// reused when not nil. Otherwise, a new FloatHistogram object will be allocated. | ||
AtFloatHistogram(*histogram.FloatHistogram) (int64, *histogram.FloatHistogram) | ||
// AtT returns the current timestamp. | ||
|
This file contains hidden or 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 hidden or 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,73 @@ | ||
// Copyright 2024 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package testutil | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/prometheus/prometheus/model/histogram" | ||
"github.com/prometheus/prometheus/tsdb/chunkenc" | ||
) | ||
|
||
// Test helper to ensure that the iterator copies histograms when iterating. | ||
// The helper will call it.Next() and check if the next value is a histogram. | ||
func VerifyChunkIteratorCopiesHistograms(h *histogram.Histogram, it chunkenc.Iterator) error { | ||
originalCount := h.Count | ||
if it.Next() != chunkenc.ValHistogram { | ||
return fmt.Errorf("expected histogram value type") | ||
} | ||
// Get a copy. | ||
_, hCopy := it.AtHistogram(nil) | ||
// Copy into a new histogram. | ||
hCopy2 := &histogram.Histogram{} | ||
_, hCopy2 = it.AtHistogram(hCopy2) | ||
|
||
// Change the original | ||
h.Count = originalCount + 1 | ||
|
||
// Ensure that the copy is not affected. | ||
if hCopy.Count != originalCount { | ||
return fmt.Errorf("copied histogram count should not change when original is modified") | ||
} | ||
if hCopy2.Count != originalCount { | ||
return fmt.Errorf("copied to histogram count should not change when original is modified") | ||
} | ||
return nil | ||
} | ||
|
||
// Test helper to ensure that the iterator copies float histograms when iterating. | ||
// The helper will call it.Next() and check if the next value is a float histogram. | ||
func VerifyChunkIteratorCopiesFloatHistograms(fh *histogram.FloatHistogram, it chunkenc.Iterator) error { | ||
originalCount := fh.Count | ||
if it.Next() != chunkenc.ValFloatHistogram { | ||
return fmt.Errorf("expected float histogram value type") | ||
} | ||
// Get a copy. | ||
_, hCopy := it.AtFloatHistogram(nil) | ||
// Copy into a new histogram. | ||
hCopy2 := &histogram.FloatHistogram{} | ||
_, hCopy2 = it.AtFloatHistogram(hCopy2) | ||
|
||
// Change the original | ||
fh.Count = originalCount + 1 | ||
|
||
// Ensure that the copy is not affected. | ||
if hCopy.Count != originalCount { | ||
return fmt.Errorf("copied histogram count should not change when original is modified") | ||
} | ||
if hCopy2.Count != originalCount { | ||
return fmt.Errorf("copied to histogram count should not change when original is modified") | ||
} | ||
return nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn’t your PR enforce that the optional Histogram object will be reused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not checking if the value is reused and also I see some examples of especially mock iterators that always return new objects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation is then ambiguous. As a caller of
it.AtHistogram(h)
, I want to know whether the object corresponding to the pointer I passed has actually been reused or not, without having to go through all the implementations of the interface.I think that we should use will as before, even because this way we kind of force new implementations to respect the contract: if they don't reuse the given object, and if something bad happens, then it is their fault.
Related to the mocked iterators, I don't know if they are so relevant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds reasonable. I can also just update the mocks as well to be sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although there's one non mock that needs rework then:
func (c *concreteSeriesIterator) AtFloatHistogram(*histogram.FloatHistogram) (int64, *histogram.FloatHistogram) {