-
Notifications
You must be signed in to change notification settings - Fork 2.2k
bugfix: fix leaking of Silences matcherCache entries #3930
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3daa7b8
fix leaking of matcher cache entries
Spaceman1701 bab0b9f
improve clock logic in TestSilenceGCOverTime
Spaceman1701 969f274
remove TestSilencesGc
Spaceman1701 4f0e5ea
make table driven test more idiomatic
Spaceman1701 82731f2
replace test with one suggested by grobinson-grafana
Spaceman1701 f345aab
replace require.Len with require.Empty where needed
Spaceman1701 f02cfc1
Merge branch 'main' into fix-mem-leak
gotjosh 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,29 +84,135 @@ func TestOptionsValidate(t *testing.T) { | |
} | ||
} | ||
|
||
func TestSilencesGC(t *testing.T) { | ||
s, err := New(Options{}) | ||
require.NoError(t, err) | ||
func TestSilenceGCOverTime(t *testing.T) { | ||
t.Run("GC does not remove active silences", func(t *testing.T) { | ||
s, err := New(Options{}) | ||
require.NoError(t, err) | ||
s.clock = clock.NewMock() | ||
now := s.nowUTC() | ||
s.st = state{ | ||
"1": &pb.MeshSilence{Silence: &pb.Silence{Id: "1"}, ExpiresAt: now}, | ||
"2": &pb.MeshSilence{Silence: &pb.Silence{Id: "2"}, ExpiresAt: now.Add(-time.Second)}, | ||
"3": &pb.MeshSilence{Silence: &pb.Silence{Id: "3"}, ExpiresAt: now.Add(time.Second)}, | ||
} | ||
want := state{ | ||
"3": &pb.MeshSilence{Silence: &pb.Silence{Id: "3"}, ExpiresAt: now.Add(time.Second)}, | ||
} | ||
n, err := s.GC() | ||
require.NoError(t, err) | ||
require.Equal(t, 2, n) | ||
require.Equal(t, want, s.st) | ||
}) | ||
|
||
s.clock = clock.NewMock() | ||
now := s.nowUTC() | ||
t.Run("GC does not leak cache entries", func(t *testing.T) { | ||
s, err := New(Options{}) | ||
require.NoError(t, err) | ||
clock := clock.NewMock() | ||
s.clock = clock | ||
sil1 := &pb.Silence{ | ||
Matchers: []*pb.Matcher{{ | ||
Type: pb.Matcher_EQUAL, | ||
Name: "foo", | ||
Pattern: "bar", | ||
}}, | ||
StartsAt: clock.Now(), | ||
EndsAt: clock.Now().Add(time.Minute), | ||
} | ||
require.NoError(t, s.Set(sil1)) | ||
// Need to query the silence to populate the matcher cache. | ||
s.Query(QMatches(model.LabelSet{"foo": "bar"})) | ||
require.Len(t, s.st, 1) | ||
require.Len(t, s.mc, 1) | ||
// Move time forward and both silence and cache entry should be garbage | ||
// collected. | ||
clock.Add(time.Minute) | ||
n, err := s.GC() | ||
require.NoError(t, err) | ||
require.Equal(t, 1, n) | ||
require.Empty(t, s.st) | ||
require.Empty(t, s.mc) | ||
}) | ||
|
||
newSilence := func(exp time.Time) *pb.MeshSilence { | ||
return &pb.MeshSilence{ExpiresAt: exp} | ||
} | ||
s.st = state{ | ||
"1": newSilence(now), | ||
"2": newSilence(now.Add(-time.Second)), | ||
"3": newSilence(now.Add(time.Second)), | ||
} | ||
want := state{ | ||
"3": newSilence(now.Add(time.Second)), | ||
} | ||
t.Run("replacing a silences does not leak cache entries", func(t *testing.T) { | ||
s, err := New(Options{}) | ||
require.NoError(t, err) | ||
clock := clock.NewMock() | ||
s.clock = clock | ||
sil1 := &pb.Silence{ | ||
Matchers: []*pb.Matcher{{ | ||
Type: pb.Matcher_EQUAL, | ||
Name: "foo", | ||
Pattern: "bar", | ||
}}, | ||
StartsAt: clock.Now(), | ||
EndsAt: clock.Now().Add(time.Minute), | ||
} | ||
require.NoError(t, s.Set(sil1)) | ||
// Need to query the silence to populate the matcher cache. | ||
s.Query(QMatches(model.LabelSet{"foo": "bar"})) | ||
require.Len(t, s.st, 1) | ||
require.Len(t, s.mc, 1) | ||
// must clone sil1 before replacing it. | ||
sil2 := cloneSilence(sil1) | ||
sil2.Matchers = []*pb.Matcher{{ | ||
Type: pb.Matcher_EQUAL, | ||
Name: "bar", | ||
Pattern: "baz", | ||
}} | ||
require.NoError(t, s.Set(sil2)) | ||
// Need to query the silence to populate the matcher cache. | ||
s.Query(QMatches(model.LabelSet{"bar": "baz"})) | ||
require.Len(t, s.st, 2) | ||
require.Len(t, s.mc, 2) | ||
// Move time forward and both silence and cache entry should be garbage | ||
// collected. | ||
clock.Add(time.Minute) | ||
n, err := s.GC() | ||
require.NoError(t, err) | ||
require.Equal(t, 2, n) | ||
require.Empty(t, s.st) | ||
require.Empty(t, s.mc) | ||
}) | ||
|
||
n, err := s.GC() | ||
require.NoError(t, err) | ||
require.Equal(t, 2, n) | ||
require.Equal(t, want, s.st) | ||
// This test checks for a memory leak that occurred in the matcher cache when | ||
// updating an existing silence. | ||
t.Run("updating a silences does not leak cache entries", func(t *testing.T) { | ||
s, err := New(Options{}) | ||
require.NoError(t, err) | ||
clock := clock.NewMock() | ||
s.clock = clock | ||
sil1 := &pb.Silence{ | ||
Id: "1", | ||
Matchers: []*pb.Matcher{{ | ||
Type: pb.Matcher_EQUAL, | ||
Name: "foo", | ||
Pattern: "bar", | ||
}}, | ||
StartsAt: clock.Now(), | ||
EndsAt: clock.Now().Add(time.Minute), | ||
} | ||
s.st["1"] = &pb.MeshSilence{Silence: sil1, ExpiresAt: clock.Now().Add(time.Minute)} | ||
// Need to query the silence to populate the matcher cache. | ||
s.Query(QMatches(model.LabelSet{"foo": "bar"})) | ||
require.Len(t, s.mc, 1) | ||
// must clone sil1 before updating it. | ||
sil2 := cloneSilence(sil1) | ||
require.NoError(t, s.Set(sil2)) | ||
// The memory leak occurred because updating a silence would add a new | ||
// entry in the matcher cache even though no new silence was created. | ||
// This check asserts that this no longer happens. | ||
s.Query(QMatches(model.LabelSet{"foo": "bar"})) | ||
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. I added this line because the memory leak only occurs if the matcher cache is populated because of a query. |
||
require.Len(t, s.st, 1) | ||
require.Len(t, s.mc, 1) | ||
// Move time forward and both silence and cache entry should be garbage | ||
// collected. | ||
clock.Add(time.Minute) | ||
n, err := s.GC() | ||
require.NoError(t, err) | ||
require.Equal(t, 1, n) | ||
require.Empty(t, s.st) | ||
require.Empty(t, s.mc) | ||
}) | ||
} | ||
|
||
func TestSilencesSnapshot(t *testing.T) { | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.