-
Notifications
You must be signed in to change notification settings - Fork 692
Speedup DistinctValue collector and exit early for ingesters #4104
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
electron0zero
merged 8 commits into
grafana:main
from
electron0zero:fast_collect_and_early_stop
Sep 20, 2024
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f6dad82
make the collector go fast...
electron0zero 23b34ff
fixup usage and log lines
electron0zero e2edd76
exit early when we hit the limits of collector
electron0zero 7fab352
cleanup
electron0zero 7e74b04
CHANGELOG.md
electron0zero c49a064
fix lint
electron0zero fbb28d7
break with goto
electron0zero 9db274c
locked and loaded
electron0zero 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
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 |
|---|---|---|
|
|
@@ -5,66 +5,83 @@ import ( | |
| ) | ||
|
|
||
| type DistinctValue[T comparable] struct { | ||
| values map[T]struct{} | ||
| new map[T]struct{} | ||
| len func(T) int | ||
| maxLen int | ||
| currLen int | ||
| totalLen int | ||
| mtx sync.RWMutex | ||
| values map[T]struct{} | ||
| new map[T]struct{} | ||
| len func(T) int | ||
| maxLen int | ||
| currLen int | ||
| limExceeded bool | ||
| diffEnabled bool | ||
| mtx sync.Mutex | ||
| } | ||
|
|
||
| // NewDistinctValue with the given maximum data size. This is calculated | ||
| // as the total length of the recorded strings. For ease of use, maximum=0 | ||
| // is interpreted as unlimited. | ||
| // Use NewDistinctValueWithDiff to enable diff support, but that one is slightly slower. | ||
| func NewDistinctValue[T comparable](maxDataSize int, len func(T) int) *DistinctValue[T] { | ||
| return &DistinctValue[T]{ | ||
| values: make(map[T]struct{}), | ||
| new: make(map[T]struct{}), | ||
| maxLen: maxDataSize, | ||
| len: len, | ||
| values: make(map[T]struct{}), | ||
| new: make(map[T]struct{}), | ||
| maxLen: maxDataSize, | ||
| diffEnabled: false, // disable diff to make it faster | ||
| len: len, | ||
| } | ||
| } | ||
|
|
||
| // NewDistinctValueWithDiff is like NewDistinctValue but with diff support enabled. | ||
| func NewDistinctValueWithDiff[T comparable](maxDataSize int, len func(T) int) *DistinctValue[T] { | ||
| return &DistinctValue[T]{ | ||
| values: make(map[T]struct{}), | ||
| new: make(map[T]struct{}), | ||
| maxLen: maxDataSize, | ||
| diffEnabled: true, | ||
| len: len, | ||
| } | ||
| } | ||
|
|
||
| // Collect adds a new value to the distinct value collector. | ||
| // return true when it reaches the limits and can't fit more values. | ||
| // callers of return of Collect or call Exceeded to stop early. | ||
| func (d *DistinctValue[T]) Collect(v T) (exceeded bool) { | ||
| d.mtx.RLock() | ||
| if _, ok := d.values[v]; ok { | ||
| d.mtx.RUnlock() | ||
| return // Already present | ||
| d.mtx.Lock() | ||
| defer d.mtx.Unlock() | ||
|
|
||
| if d.limExceeded { | ||
| return true | ||
| } | ||
| d.mtx.RUnlock() | ||
|
|
||
| // Calculate length | ||
| valueLen := d.len(v) | ||
|
|
||
| d.mtx.Lock() | ||
| defer d.mtx.Unlock() | ||
| // Can it fit? | ||
| // note: we will stop adding values slightly before the limit is reached | ||
| if d.maxLen > 0 && d.currLen+valueLen >= d.maxLen { | ||
| // No, it can't fit | ||
| d.limExceeded = true | ||
| return true | ||
| } | ||
|
|
||
| if _, ok := d.values[v]; ok { | ||
| return // Already present | ||
| } | ||
|
|
||
| // Record total inspected length regardless | ||
| d.totalLen += valueLen | ||
|
|
||
| // Can it fit? | ||
| if d.maxLen > 0 && d.currLen+valueLen > d.maxLen { | ||
| // No | ||
| return true | ||
| if d.diffEnabled { | ||
| d.new[v] = struct{}{} | ||
| } | ||
|
|
||
| d.new[v] = struct{}{} | ||
| d.values[v] = struct{}{} | ||
| d.currLen += valueLen | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| // Values returns the final list of distinct values collected and sorted. | ||
| func (d *DistinctValue[T]) Values() []T { | ||
| ss := make([]T, 0, len(d.values)) | ||
|
Member
Author
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. @joe-elliott I think we have the same subtle bug here as well, that we had in Diff. we are checking the len of a member var without locking. I think we got lucky here because we usualy call Values after we are done collecting. locking this as well. |
||
|
|
||
| d.mtx.RLock() | ||
| defer d.mtx.RUnlock() | ||
| d.mtx.Lock() | ||
| defer d.mtx.Unlock() | ||
|
|
||
| for k := range d.values { | ||
| ss = append(ss, k) | ||
|
|
@@ -73,26 +90,32 @@ func (d *DistinctValue[T]) Values() []T { | |
| return ss | ||
| } | ||
|
|
||
| // Exceeded indicates if some values were lost because the maximum size limit was met. | ||
| // Exceeded indicates that we have exceeded the limit | ||
| // can be used to stop early and to avoid collecting further values | ||
| func (d *DistinctValue[T]) Exceeded() bool { | ||
| d.mtx.RLock() | ||
| defer d.mtx.RUnlock() | ||
| return d.totalLen > d.currLen | ||
| d.mtx.Lock() | ||
| defer d.mtx.Unlock() | ||
| return d.limExceeded | ||
| } | ||
|
|
||
| // TotalDataSize is the total size of all distinct strings encountered. | ||
| func (d *DistinctValue[T]) TotalDataSize() int { | ||
| d.mtx.RLock() | ||
| defer d.mtx.RUnlock() | ||
| return d.totalLen | ||
| // Size is the total size of all distinct items collected | ||
| func (d *DistinctValue[T]) Size() int { | ||
| d.mtx.Lock() | ||
| defer d.mtx.Unlock() | ||
| return d.currLen | ||
| } | ||
|
|
||
| // Diff returns all new strings collected since the last time diff was called | ||
| // returns nil if diff is not enabled | ||
| func (d *DistinctValue[T]) Diff() []T { | ||
| if !d.diffEnabled { | ||
|
joe-elliott marked this conversation as resolved.
|
||
| return nil | ||
| } | ||
|
|
||
| ss := make([]T, 0, len(d.new)) | ||
|
joe-elliott marked this conversation as resolved.
|
||
|
|
||
| d.mtx.RLock() | ||
| defer d.mtx.RUnlock() | ||
| d.mtx.Lock() | ||
| defer d.mtx.Unlock() | ||
|
|
||
| for k := range d.new { | ||
| ss = append(ss, k) | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.