Skip to content
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: Update cache interface with Clone method #5997

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions topdown/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ package cache

import (
"container/list"
"sync"

"github.com/open-policy-agent/opa/ast"

"sync"

"github.com/open-policy-agent/opa/util"
)

Expand Down Expand Up @@ -62,6 +61,7 @@ func (c *Config) validateAndInjectDefaults() error {
// InterQueryCacheValue defines the interface for the data that the inter-query cache holds.
type InterQueryCacheValue interface {
SizeInBytes() int64
Clone() (InterQueryCacheValue, error)
}

// InterQueryCache defines the interface for the inter-query cache.
Expand All @@ -70,6 +70,7 @@ type InterQueryCache interface {
Insert(key ast.Value, value InterQueryCacheValue) int
Delete(key ast.Value)
UpdateConfig(config *Config)
Clone(value InterQueryCacheValue) (InterQueryCacheValue, error)
}

// NewInterQueryCache returns a new inter-query cache.
Expand Down Expand Up @@ -130,6 +131,12 @@ func (c *cache) UpdateConfig(config *Config) {
c.config = config
}

func (c *cache) Clone(value InterQueryCacheValue) (InterQueryCacheValue, error) {
c.mtx.Lock()
defer c.mtx.Unlock()
return c.unsafeClone(value)
}

func (c *cache) unsafeInsert(k ast.Value, v InterQueryCacheValue) (dropped int) {
size := v.SizeInBytes()
limit := c.maxSizeBytes()
Expand Down Expand Up @@ -174,6 +181,10 @@ func (c *cache) unsafeDelete(k ast.Value) {
c.l.Remove(cacheItem.keyElement)
}

func (c *cache) unsafeClone(value InterQueryCacheValue) (InterQueryCacheValue, error) {
return value.Clone()
}

func (c *cache) maxSizeBytes() int64 {
if c.config == nil {
return defaultMaxSizeBytes
Expand Down
45 changes: 45 additions & 0 deletions topdown/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,47 @@ func TestConcurrentInsert(t *testing.T) {
}
}

func TestClone(t *testing.T) {
in := `{"inter_query_builtin_cache": {"max_size_bytes": 40},}`

config, err := ParseCachingConfig([]byte(in))
if err != nil {
t.Fatalf("Unexpected error %v", err)
}

cache := NewInterQueryCache(config)

cacheValue := newInterQueryCacheValue(ast.StringTerm("bar").Value, 20)
dropped := cache.Insert(ast.StringTerm("foo").Value, cacheValue)
if dropped != 0 {
t.Fatal("Expected dropped to be zero")
}

val, found := cache.Get(ast.StringTerm("foo").Value)
if !found {
t.Fatal("Expected key \"foo\" in cache")
}

dup, err := cache.Clone(val)
if err != nil {
t.Fatal(err)
}

original, ok := val.(*testInterQueryCacheValue)
if !ok {
t.Fatal("unexpected type")
}

cloned, ok := dup.(*testInterQueryCacheValue)
if !ok {
t.Fatal("unexpected type")
}

if !reflect.DeepEqual(*original, *cloned) {
t.Fatalf("Expected to get %v, but got %v", *original, *cloned)
}
}

func TestDelete(t *testing.T) {
config, err := ParseCachingConfig(nil)
if err != nil {
Expand Down Expand Up @@ -299,3 +340,7 @@ func newInterQueryCacheValue(value ast.Value, size int) *testInterQueryCacheValu
func (p testInterQueryCacheValue) SizeInBytes() int64 {
return int64(p.size)
}

func (p testInterQueryCacheValue) Clone() (InterQueryCacheValue, error) {
return &testInterQueryCacheValue{value: p.value, size: p.size}, nil
}
26 changes: 25 additions & 1 deletion topdown/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,10 +803,16 @@ func insertErrorIntoHTTPSendCache(bctx BuiltinContext, key ast.Object, err error
func (c *interQueryCache) checkHTTPSendInterQueryCache() (ast.Value, error) {
requestCache := c.bctx.InterQueryBuiltinCache

value, found := requestCache.Get(c.key)
cachedValue, found := requestCache.Get(c.key)
if !found {
return nil, nil
}

value, cerr := requestCache.Clone(cachedValue)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the benefit of this over cachedValues.Clone() here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

requestCache.Clone(cachedValue) uses the cache's mutex.

My thinking here is that each routine will have it's own copy of the cached item. Inside the clone implementation we make a clone of the header as well. So multiple routines can perform operations on the header map w/o any race issues. What do you think of this approach?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

requestCache.Clone(cachedValue) uses the cache's mutex.

Ah, of course. I had missed that. 👍

if cerr != nil {
return nil, handleHTTPSendErr(c.bctx, cerr)
}

c.bctx.Metrics.Counter(httpSendInterQueryCacheHits).Incr()
var cachedRespData *interQueryCacheData

Expand Down Expand Up @@ -1035,6 +1041,12 @@ func newInterQueryCacheValue(bctx BuiltinContext, resp *http.Response, respBody
return &interQueryCacheValue{Data: b}, nil
}

func (cb interQueryCacheValue) Clone() (cache.InterQueryCacheValue, error) {
dup := make([]byte, len(cb.Data))
copy(dup, cb.Data)
return &interQueryCacheValue{Data: dup}, nil
}

func (cb interQueryCacheValue) SizeInBytes() int64 {
return int64(len(cb.Data))
}
Expand Down Expand Up @@ -1118,6 +1130,18 @@ func (c *interQueryCacheData) SizeInBytes() int64 {
return 0
}

func (c *interQueryCacheData) Clone() (cache.InterQueryCacheValue, error) {
dup := make([]byte, len(c.RespBody))
copy(dup, c.RespBody)

return &interQueryCacheData{
ExpiresAt: c.ExpiresAt,
RespBody: dup,
Status: c.Status,
StatusCode: c.StatusCode,
Headers: c.Headers.Clone()}, nil
}

type responseHeaders struct {
date time.Time // origination date and time of response
cacheControl map[string]string // response cache-control header
Expand Down
64 changes: 64 additions & 0 deletions topdown/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2851,6 +2851,10 @@ func (c *onlyOnceInterQueryCache) Delete(_ ast.Value) {}

func (c *onlyOnceInterQueryCache) UpdateConfig(_ *iCache.Config) {}

func (c *onlyOnceInterQueryCache) Clone(val iCache.InterQueryCacheValue) (iCache.InterQueryCacheValue, error) {
return val, nil
}

func TestInterQueryCacheConcurrentModification(t *testing.T) {

// create an inter-query cache that'll return a value on first access, but none at subsequent accesses.
Expand Down Expand Up @@ -2899,6 +2903,66 @@ func TestInterQueryCacheConcurrentModification(t *testing.T) {
}
}

func TestInterQueryCacheDataClone(t *testing.T) {
data := interQueryCacheData{
Headers: map[string][]string{
"Date": {"Thu, 01 Jan 1970 00:00:00 GMT"},
},
ExpiresAt: time.Now().Add(time.Hour),
StatusCode: 200,
Status: "200 OK",
RespBody: []byte("foo"),
}

dup, err := data.Clone()
if err != nil {
t.Fatal(err)
}

cloned, ok := dup.(*interQueryCacheData)
if !ok {
t.Fatal("unexpected type")
}

if !reflect.DeepEqual(data, *cloned) {
t.Fatalf("Expected to get %v, but got %v", data, *cloned)
}
}

func TestInterQueryCacheValueClone(t *testing.T) {

cacheData := interQueryCacheData{
Headers: map[string][]string{
"Date": {"Thu, 01 Jan 1970 00:00:00 GMT"},
},
ExpiresAt: time.Now().Add(time.Hour),
StatusCode: 200,
Status: "200 OK",
RespBody: []byte("foo"),
}

b, err := json.Marshal(cacheData)
if err != nil {
t.Fatal(err)
}

cacheVal := interQueryCacheValue{Data: b}

dup, err := cacheVal.Clone()
if err != nil {
t.Fatal(err)
}

cloned, ok := dup.(*interQueryCacheValue)
if !ok {
t.Fatal("unexpected type")
}

if !reflect.DeepEqual(cacheVal, *cloned) {
t.Fatal("inter-query cache element and its clone are not equal")
}
}

func TestIntraQueryCache_ClientError(t *testing.T) {
data := loadSmallTestData()

Expand Down