-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
topdown: Adding cap to caches for regex and glob built-in functions #6846
Merged
johanfylling
merged 7 commits into
open-policy-agent:main
from
johanfylling:fix/regex_mem_leak
Jul 5, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c42020f
Adding bench tests for regex built-in
johanfylling 2f98658
Capping built-in regex cache to 100 entries
johanfylling 3fd3fd5
Capping built-in glob cache to 100 entries
johanfylling 6cacf17
Capping Wasm built-in regex and glob caches to 100 entries
johanfylling f8075c8
Making linter happy
johanfylling 528d32a
Adding tests
johanfylling c794780
Merge branch 'main' into fix/regex_mem_leak
johanfylling 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
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
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,100 @@ | ||
// Copyright 2024 The OPA Authors. All rights reserved. | ||
// Use of this source code is governed by an Apache2 | ||
// license that can be found in the LICENSE file. | ||
|
||
package topdown | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/gobwas/glob" | ||
"github.com/open-policy-agent/opa/ast" | ||
) | ||
|
||
func BenchmarkBuiltinGlobMatch(b *testing.B) { | ||
iter := func(*ast.Term) error { return nil } | ||
ctx := BuiltinContext{} | ||
|
||
for _, reusePattern := range []bool{true, false} { | ||
for _, patternCount := range []int{10, 100, 1000} { | ||
b.Run(fmt.Sprintf("reuse-pattern=%v, pattern-count=%d", reusePattern, patternCount), func(b *testing.B) { | ||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
// Clearing the cache | ||
globCache = make(map[string]glob.Glob) | ||
|
||
for i := 0; i < patternCount; i++ { | ||
var operands []*ast.Term | ||
if reusePattern { | ||
operands = []*ast.Term{ | ||
ast.NewTerm(ast.String("foo/*")), | ||
ast.NullTerm(), | ||
ast.NewTerm(ast.String("foo/bar")), | ||
} | ||
} else { | ||
operands = []*ast.Term{ | ||
ast.NewTerm(ast.String(fmt.Sprintf("foo/*/%d", i))), | ||
ast.NullTerm(), | ||
ast.NewTerm(ast.String(fmt.Sprintf("foo/bar/%d", i))), | ||
} | ||
} | ||
if err := builtinGlobMatch(ctx, operands, iter); err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkBuiltinGlobMatchAsync(b *testing.B) { | ||
iter := func(*ast.Term) error { return nil } | ||
ctx := BuiltinContext{} | ||
|
||
for _, reusePattern := range []bool{true, false} { | ||
for _, clientCount := range []int{100, 200} { | ||
for _, patternCount := range []int{10, 100, 1000} { | ||
b.Run(fmt.Sprintf("reuse-pattern=%v, clients=%d, pattern-count=%d", reusePattern, clientCount, patternCount), func(b *testing.B) { | ||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
// Clearing the cache | ||
globCache = make(map[string]glob.Glob) | ||
|
||
wg := sync.WaitGroup{} | ||
for i := 0; i < clientCount; i++ { | ||
clientID := i | ||
wg.Add(1) | ||
go func() { | ||
for j := 0; j < patternCount; j++ { | ||
var operands []*ast.Term | ||
if reusePattern { | ||
operands = []*ast.Term{ | ||
ast.NewTerm(ast.String("foo/*")), | ||
ast.NullTerm(), | ||
ast.NewTerm(ast.String("foo/bar")), | ||
} | ||
} else { | ||
operands = []*ast.Term{ | ||
ast.NewTerm(ast.String(fmt.Sprintf("foo/*/%d/%d", clientID, j))), | ||
ast.NullTerm(), | ||
ast.NewTerm(ast.String(fmt.Sprintf("foo/bar/%d/%d", clientID, j))), | ||
} | ||
} | ||
if err := builtinGlobMatch(ctx, operands, iter); err != nil { | ||
b.Error(err) | ||
return | ||
} | ||
} | ||
wg.Done() | ||
}() | ||
} | ||
wg.Wait() | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} |
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,71 @@ | ||
// Copyright 2024 The OPA Authors. All rights reserved. | ||
// Use of this source code is governed by an Apache2 | ||
// license that can be found in the LICENSE file. | ||
|
||
package topdown | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/open-policy-agent/opa/ast" | ||
) | ||
|
||
func TestGlobBuiltinCache(t *testing.T) { | ||
ctx := BuiltinContext{} | ||
iter := func(*ast.Term) error { return nil } | ||
|
||
// A novel glob pattern is cached. | ||
glob1 := "foo/*" | ||
operands := []*ast.Term{ | ||
ast.NewTerm(ast.String(glob1)), | ||
ast.NullTerm(), | ||
ast.NewTerm(ast.String("foo/bar")), | ||
} | ||
err := builtinGlobMatch(ctx, operands, iter) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
|
||
// the glob id will have a trailing '-' rune. | ||
if _, ok := globCache[fmt.Sprintf("%s-", glob1)]; !ok { | ||
t.Fatalf("Expected glob to be cached: %v", glob1) | ||
} | ||
|
||
// Fill up the cache. | ||
for i := 0; i < regexCacheMaxSize-1; i++ { | ||
operands := []*ast.Term{ | ||
ast.NewTerm(ast.String(fmt.Sprintf("foo/%d/*", i))), | ||
ast.NullTerm(), | ||
ast.NewTerm(ast.String(fmt.Sprintf("foo/%d/bar", i))), | ||
} | ||
err := builtinGlobMatch(ctx, operands, iter) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
} | ||
|
||
if len(globCache) != regexCacheMaxSize { | ||
t.Fatalf("Expected cache to be full") | ||
} | ||
|
||
// A new glob pattern is cached and a random pattern is evicted. | ||
glob2 := "bar/*" | ||
operands = []*ast.Term{ | ||
ast.NewTerm(ast.String(glob2)), | ||
ast.NullTerm(), | ||
ast.NewTerm(ast.String("bar/baz")), | ||
} | ||
err = builtinGlobMatch(ctx, operands, iter) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
|
||
if len(globCache) != regexCacheMaxSize { | ||
t.Fatalf("Expected cache be capped at %d, was %d", regexCacheMaxSize, len(globCache)) | ||
} | ||
|
||
if _, ok := globCache[fmt.Sprintf("%s-", glob2)]; !ok { | ||
t.Fatalf("Expected glob to be cached: %v", glob2) | ||
} | ||
} |
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,96 @@ | ||
// Copyright 2024 The OPA Authors. All rights reserved. | ||
// Use of this source code is governed by an Apache2 | ||
// license that can be found in the LICENSE file. | ||
|
||
package topdown | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/open-policy-agent/opa/ast" | ||
) | ||
|
||
func BenchmarkBuiltinRegexMatch(b *testing.B) { | ||
iter := func(*ast.Term) error { return nil } | ||
ctx := BuiltinContext{} | ||
|
||
for _, reusePattern := range []bool{true, false} { | ||
for _, patternCount := range []int{10, 100, 1000} { | ||
b.Run(fmt.Sprintf("reuse-pattern=%v, pattern-count=%d", reusePattern, patternCount), func(b *testing.B) { | ||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
// Clearing the cache | ||
regexpCache = make(map[string]*regexp.Regexp) | ||
|
||
for i := 0; i < patternCount; i++ { | ||
var operands []*ast.Term | ||
if reusePattern { | ||
operands = []*ast.Term{ | ||
ast.NewTerm(ast.String("foo.*")), | ||
ast.NewTerm(ast.String("foobar")), | ||
} | ||
} else { | ||
operands = []*ast.Term{ | ||
ast.NewTerm(ast.String(fmt.Sprintf("foo%d.*", i))), | ||
ast.NewTerm(ast.String(fmt.Sprintf("foo%dbar", i))), | ||
} | ||
} | ||
if err := builtinRegexMatch(ctx, operands, iter); err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkBuiltinRegexMatchAsync(b *testing.B) { | ||
iter := func(*ast.Term) error { return nil } | ||
ctx := BuiltinContext{} | ||
|
||
for _, reusePattern := range []bool{true, false} { | ||
for _, clientCount := range []int{100, 200} { | ||
for _, patternCount := range []int{10, 100, 1000} { | ||
b.Run(fmt.Sprintf("reuse-pattern=%v, clients=%d, pattern-count=%d", reusePattern, clientCount, patternCount), func(b *testing.B) { | ||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
// Clearing the cache | ||
regexpCache = make(map[string]*regexp.Regexp) | ||
|
||
wg := sync.WaitGroup{} | ||
for i := 0; i < clientCount; i++ { | ||
clientID := i | ||
wg.Add(1) | ||
go func() { | ||
for j := 0; j < patternCount; j++ { | ||
var operands []*ast.Term | ||
if reusePattern { | ||
operands = []*ast.Term{ | ||
ast.NewTerm(ast.String("foo.*")), | ||
ast.NewTerm(ast.String("foobar")), | ||
} | ||
} else { | ||
operands = []*ast.Term{ | ||
ast.NewTerm(ast.String(fmt.Sprintf("foo%d_%d.*", clientID, j))), | ||
ast.NewTerm(ast.String(fmt.Sprintf("foo%d_%dbar", clientID, j))), | ||
} | ||
} | ||
if err := builtinRegexMatch(ctx, operands, iter); err != nil { | ||
b.Error(err) | ||
return | ||
} | ||
} | ||
wg.Done() | ||
}() | ||
} | ||
wg.Wait() | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} |
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,67 @@ | ||
// Copyright 2024 The OPA Authors. All rights reserved. | ||
// Use of this source code is governed by an Apache2 | ||
// license that can be found in the LICENSE file. | ||
|
||
package topdown | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/open-policy-agent/opa/ast" | ||
) | ||
|
||
func TestRegexBuiltinCache(t *testing.T) { | ||
ctx := BuiltinContext{} | ||
iter := func(*ast.Term) error { return nil } | ||
|
||
// A novel regex pattern is cached. | ||
regex1 := "foo.*" | ||
operands := []*ast.Term{ | ||
ast.NewTerm(ast.String(regex1)), | ||
ast.NewTerm(ast.String("foobar")), | ||
} | ||
err := builtinRegexMatch(ctx, operands, iter) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
|
||
if _, ok := regexpCache[regex1]; !ok { | ||
t.Fatalf("Expected regex to be cached: %v", regex1) | ||
} | ||
|
||
// Fill up the cache. | ||
for i := 0; i < regexCacheMaxSize-1; i++ { | ||
operands := []*ast.Term{ | ||
ast.NewTerm(ast.String(fmt.Sprintf("foo%d.*", i))), | ||
ast.NewTerm(ast.String(fmt.Sprintf("foo%dbar", i))), | ||
} | ||
err := builtinRegexMatch(ctx, operands, iter) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
} | ||
|
||
if len(regexpCache) != regexCacheMaxSize { | ||
t.Fatalf("Expected cache to be full") | ||
} | ||
|
||
// A new regex pattern is cached and a random pattern is evicted. | ||
regex2 := "bar.*" | ||
operands = []*ast.Term{ | ||
ast.NewTerm(ast.String(regex2)), | ||
ast.NewTerm(ast.String("barbaz")), | ||
} | ||
err = builtinRegexMatch(ctx, operands, iter) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
|
||
if len(regexpCache) != regexCacheMaxSize { | ||
t.Fatalf("Expected cache be capped at %d, was %d", regexCacheMaxSize, len(regexpCache)) | ||
} | ||
|
||
if _, ok := regexpCache[regex2]; !ok { | ||
t.Fatalf("Expected regex to be cached: %v", regex2) | ||
} | ||
} |
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.
why semi? I think it's pretty random 😅
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.
It's not explicitly random, as the randomness is an implementation detail of the map.
I also think that it'd be deterministic over multiple runs with the same map entries.
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.
So we're checking
len(globCache) >= globCacheMaxSize
but we only remove one. This is the only code path where cached patterns are inserted, right? So it cannot happen that through some other code path, the cache would still continue to grow without bounds because we're only removing one element here.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.
Correct, discounting tests, this is the only place we touch the cache.