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

Fix flakes in ./topdown/cache #7188

Merged
merged 1 commit into from
Dec 4, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion topdown/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,14 @@ func NewInterQueryCache(config *Config) InterQueryCache {
func NewInterQueryCacheWithContext(ctx context.Context, config *Config) InterQueryCache {
iqCache := newCache(config)
if iqCache.staleEntryEvictionTimePeriodSeconds() > 0 {
cleanupTicker := time.NewTicker(time.Duration(iqCache.staleEntryEvictionTimePeriodSeconds()) * time.Second)
go func() {
cleanupTicker := time.NewTicker(time.Duration(iqCache.staleEntryEvictionTimePeriodSeconds()) * time.Second)
for {
select {
case <-cleanupTicker.C:
// NOTE: We stop the ticker and create a new one here to ensure that applications
// get _at least_ staleEntryEvictionTimePeriodSeconds with the cache unlocked;
// see https://github.com/open-policy-agent/opa/pull/7188/files#r1855342998
Copy link
Member

Choose a reason for hiding this comment

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

Instead of putting a link here, it would be helpful to add some text explaining the situation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The link is to the discussion in this PR; I'm happy to add more detail here, but I thought that the link to the original was probably also useful background. Would the following work?

Suggested change
// see https://github.com/open-policy-agent/opa/pull/7188/files#r1855342998
// for more background, see https://github.com/open-policy-agent/opa/pull/7188/files#r1855342998

Copy link
Member

Choose a reason for hiding this comment

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

A single line summary of the convo with the link would be great!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that's what the two lines above the link are -- a summary of the discussion, and then a link for more information.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, can you please update the commit message by adding a description in the body and we can get this in.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the commit message to cover this discussion as well as the fixing of the flakes in the test (which was the primary motivation).

cleanupTicker.Stop()
iqCache.cleanStaleValues()
cleanupTicker = time.NewTicker(time.Duration(iqCache.staleEntryEvictionTimePeriodSeconds()) * time.Second)
evankanderson marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
32 changes: 22 additions & 10 deletions topdown/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,17 @@ func TestInsertWithExpiryAndEviction(t *testing.T) {
t.Fatalf("Unexpected error %v", err)
}

cache := NewInterQueryCacheWithContext(context.Background(), config)
// This starts a background ticker at stale_entry_eviction_period_seconds to clean up items.
ctx, cancel := context.WithCancel(context.Background())
cache := NewInterQueryCacheWithContext(ctx, config)
t.Cleanup(cancel)

cacheValue := newInterQueryCacheValue(ast.StringTerm("bar").Value, 20)
cache.InsertWithExpiry(ast.StringTerm("force_evicted_foo").Value, cacheValue, time.Now().Add(100*time.Second))
if fetchedCacheValue, found := cache.Get(ast.StringTerm("force_evicted_foo").Value); !found {
t.Fatalf("Expected cache entry with value %v, found %v", cacheValue, fetchedCacheValue)
}
cache.InsertWithExpiry(ast.StringTerm("expired_foo").Value, cacheValue, time.Now().Add(1*time.Second))
cache.InsertWithExpiry(ast.StringTerm("expired_foo").Value, cacheValue, time.Now().Add(900*time.Millisecond))
if fetchedCacheValue, found := cache.Get(ast.StringTerm("expired_foo").Value); !found {
t.Fatalf("Expected cache entry with value %v, found %v", cacheValue, fetchedCacheValue)
}
Expand All @@ -425,7 +428,7 @@ func TestInsertWithExpiryAndEviction(t *testing.T) {
}

// Ensure stale entries clean up routine runs at least once
time.Sleep(2 * time.Second)
time.Sleep(1100 * time.Millisecond)

// Entry deleted even though not expired because force evicted when foo is inserted
if fetchedCacheValue, found := cache.Get(ast.StringTerm("force_evicted_foo").Value); found {
Expand Down Expand Up @@ -454,20 +457,23 @@ func TestInsertHighTTLWithStaleEntryCleanup(t *testing.T) {
t.Fatalf("Unexpected error %v", err)
}

cache := NewInterQueryCacheWithContext(context.Background(), config)
// This starts a background ticker at stale_entry_eviction_period_seconds to clean up items.
ctx, cancel := context.WithCancel(context.Background())
cache := NewInterQueryCacheWithContext(ctx, config)
t.Cleanup(cancel)

cacheValue := newInterQueryCacheValue(ast.StringTerm("bar").Value, 20)
cache.InsertWithExpiry(ast.StringTerm("high_ttl_foo").Value, cacheValue, time.Now().Add(100*time.Second))
if fetchedCacheValue, found := cache.Get(ast.StringTerm("high_ttl_foo").Value); !found {
t.Fatalf("Expected cache entry with value %v, found %v", cacheValue, fetchedCacheValue)
}
cache.InsertWithExpiry(ast.StringTerm("expired_foo").Value, cacheValue, time.Now().Add(1*time.Second))
cache.InsertWithExpiry(ast.StringTerm("expired_foo").Value, cacheValue, time.Now().Add(900*time.Millisecond))
if fetchedCacheValue, found := cache.Get(ast.StringTerm("expired_foo").Value); !found {
t.Fatalf("Expected cache entry with value %v, found no entry", fetchedCacheValue)
}

// Ensure stale entries clean up routine runs at least once
time.Sleep(2 * time.Second)
time.Sleep(1100 * time.Millisecond)

cache.InsertWithExpiry(ast.StringTerm("foo").Value, cacheValue, time.Now().Add(10*time.Second))
if fetchedCacheValue, found := cache.Get(ast.StringTerm("foo").Value); !found {
Expand Down Expand Up @@ -497,7 +503,10 @@ func TestInsertHighTTLWithoutStaleEntryCleanup(t *testing.T) {
t.Fatalf("Unexpected error %v", err)
}

cache := NewInterQueryCacheWithContext(context.Background(), config)
// This starts a background ticker at stale_entry_eviction_period_seconds to clean up items.
ctx, cancel := context.WithCancel(context.Background())
cache := NewInterQueryCacheWithContext(ctx, config)
t.Cleanup(cancel)

cacheValue := newInterQueryCacheValue(ast.StringTerm("bar").Value, 20)
cache.InsertWithExpiry(ast.StringTerm("high_ttl_foo").Value, cacheValue, time.Now().Add(100*time.Second))
Expand Down Expand Up @@ -537,14 +546,17 @@ func TestZeroExpiryTime(t *testing.T) {
t.Fatalf("Unexpected error %v", err)
}

cache := NewInterQueryCacheWithContext(context.Background(), config)
// This starts a background ticker at stale_entry_eviction_period_seconds to clean up items.
ctx, cancel := context.WithCancel(context.Background())
cache := NewInterQueryCacheWithContext(ctx, config)
t.Cleanup(cancel)
cacheValue := newInterQueryCacheValue(ast.StringTerm("bar").Value, 20)
cache.InsertWithExpiry(ast.StringTerm("foo").Value, cacheValue, time.Time{})
if fetchedCacheValue, found := cache.Get(ast.StringTerm("foo").Value); !found {
t.Fatalf("Expected cache entry with value %v for foo, found %v", cacheValue, fetchedCacheValue)
}

time.Sleep(2 * time.Second)
time.Sleep(1100 * time.Millisecond)

// Stale entry cleanup routine skips zero time cache entries
if fetchedCacheValue, found := cache.Get(ast.StringTerm("foo").Value); !found {
Expand Down Expand Up @@ -574,7 +586,7 @@ func TestCancelNewInterQueryCacheWithContext(t *testing.T) {
}

cancel()
time.Sleep(2 * time.Second)
time.Sleep(1100 * time.Millisecond)

// Stale entry cleanup routine stopped as context was cancelled
if fetchedCacheValue, found := cache.Get(ast.StringTerm("foo").Value); !found {
Expand Down