-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
feat: Pattern ingesters add a limiter for high eviction rate #13464
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b2531db
testing limiting eviction
cyriltovena edabc88
Merge remote-tracking branch 'upstream/main' into lockdown-eviction
cyriltovena 80b9850
feat: Pattern ingesters add a limiter for high eviction rate
cyriltovena 1e927d5
improve pruning tick
cyriltovena 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 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
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,51 @@ | ||
package drain | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type limiter struct { | ||
added int64 | ||
evicted int64 | ||
maxPercentage float64 | ||
blockedUntil time.Time | ||
} | ||
|
||
func newLimiter(maxPercentage float64) *limiter { | ||
return &limiter{ | ||
maxPercentage: maxPercentage, | ||
} | ||
} | ||
|
||
func (l *limiter) Allow() bool { | ||
if !l.blockedUntil.IsZero() { | ||
if time.Now().Before(l.blockedUntil) { | ||
return false | ||
} | ||
l.reset() | ||
} | ||
if l.added == 0 { | ||
l.added++ | ||
return true | ||
} | ||
if float64(l.evicted)/float64(l.added) > l.maxPercentage { | ||
l.block() | ||
return false | ||
} | ||
l.added++ | ||
return true | ||
} | ||
|
||
func (l *limiter) Evict() { | ||
l.evicted++ | ||
} | ||
|
||
func (l *limiter) reset() { | ||
l.added = 0 | ||
l.evicted = 0 | ||
l.blockedUntil = time.Time{} | ||
} | ||
|
||
func (l *limiter) block() { | ||
l.blockedUntil = time.Now().Add(10 * time.Minute) | ||
} |
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,70 @@ | ||
package drain | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewLimiter(t *testing.T) { | ||
maxPercentage := 0.5 | ||
l := newLimiter(maxPercentage) | ||
require.NotNil(t, l, "expected non-nil limiter") | ||
require.Equal(t, maxPercentage, l.maxPercentage, "expected maxPercentage to match") | ||
require.Equal(t, int64(0), l.added, "expected added to be 0") | ||
require.Equal(t, int64(0), l.evicted, "expected evicted to be 0") | ||
require.True(t, l.blockedUntil.IsZero(), "expected blockedUntil to be zero") | ||
} | ||
|
||
func TestLimiterAllow(t *testing.T) { | ||
maxPercentage := 0.5 | ||
l := newLimiter(maxPercentage) | ||
|
||
// Test allowing when no evictions | ||
require.True(t, l.Allow(), "expected Allow to return true initially") | ||
|
||
// Test allowing until evictions exceed maxPercentage | ||
for i := 0; i < 2; i++ { | ||
require.True(t, l.Allow(), "expected Allow to return true %d", i) | ||
l.Evict() | ||
} | ||
|
||
// Evict to exceed maxPercentage | ||
l.Evict() | ||
require.False(t, l.Allow(), "expected Allow to return false after evictions exceed maxPercentage") | ||
|
||
// Test blocking time | ||
require.False(t, l.blockedUntil.IsZero(), "expected blockedUntil to be set") | ||
|
||
// Fast forward time to simulate block duration passing | ||
l.blockedUntil = time.Now().Add(-1 * time.Minute) | ||
require.True(t, l.Allow(), "expected Allow to return true after block duration") | ||
} | ||
|
||
func TestLimiterEvict(t *testing.T) { | ||
l := newLimiter(0.5) | ||
l.Evict() | ||
require.Equal(t, int64(1), l.evicted, "expected evicted to be 1") | ||
l.Evict() | ||
require.Equal(t, int64(2), l.evicted, "expected evicted to be 2") | ||
} | ||
|
||
func TestLimiterReset(t *testing.T) { | ||
l := newLimiter(0.5) | ||
l.added = 10 | ||
l.evicted = 5 | ||
l.blockedUntil = time.Now().Add(10 * time.Minute) | ||
l.reset() | ||
require.Equal(t, int64(0), l.added, "expected added to be 0") | ||
require.Equal(t, int64(0), l.evicted, "expected evicted to be 0") | ||
require.True(t, l.blockedUntil.IsZero(), "expected blockedUntil to be zero") | ||
} | ||
|
||
func TestLimiterBlock(t *testing.T) { | ||
l := newLimiter(0.5) | ||
l.block() | ||
require.False(t, l.blockedUntil.IsZero(), "expected blockedUntil to be set") | ||
require.False(t, l.Allow()) | ||
require.True(t, l.blockedUntil.After(time.Now()), "expected blockedUntil to be in the future") | ||
} |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
What do you think about sampling instead of blocking completely for some time?
We could accept a small %age of the writes to keep building patterns. It might make a nicer experience for the user?
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.
Yeah that's good suggestion. I would like to keep it that way for now until we have all clusters under control and then improve this 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.
Bare in mind that it will still grow the tree if we do sampling. So I think it's a bit more risky.
In this case it will constantly lock the streams if it keeps evicting.