-
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
Issue-5320 clean expired cache entries periodically #6392
Issue-5320 clean expired cache entries periodically #6392
Conversation
✅ Deploy Preview for openpolicyagent ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
9689d07
to
3a9499d
Compare
This is an awesome contribution! Thanks, and keep up the great work! 😃 👏 |
500bb33
to
cb43b1e
Compare
cb43b1e
to
ea52d5f
Compare
Thanks for the contribution @rudrakhp! I'll review it this week. |
8af9e58
to
4d2c1fe
Compare
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.
@rudrakhp thanks for the contribution. The main comment I have is we need to make changes that are backwards compatible. Some of the API changes you've proposed are not. We can look into introducing a new type if required for the new functionality and think about deprecating the old one.
e1de25d
to
381b151
Compare
2619e32
to
36b8968
Compare
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.
LGTM 👍. Thanks for the contribution 😃!
topdown/cache/cache.go
Outdated
defer ticker.Stop() | ||
for { | ||
select { | ||
case <-ticker.C: |
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 would happen if we still in the middle of a clean up and ticker ticks? We should probably stop the ticker, do the work and recreate it.
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.
@ashutosh-narkar Only one clean up routine runs at a time, since we listen for more ticker events only after the clean up routine returns. But the time period StaleEntryEvictionPeriodSeconds
is enforced between start of each routine. We should reset the ticker if we intend to enforce it between end of previous routine and start of the new one.
We should probably stop the ticker, do the work and recreate it.
SGTM, added changes for the same.
36b8968
to
06392ab
Compare
d5f7c13
to
8117bac
Compare
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.
@rudrakhp this is getting close. Few comments inline 👇 Thanks for working on this.
topdown/cache/cache.go
Outdated
func NewInterQueryCacheWithContext(ctx context.Context, config *Config) InterQueryCache { | ||
iqCache := newCache(config) | ||
if iqCache.staleEntryEvictionTimePeriodSeconds() > 0 { | ||
iqCache.ticker = time.NewTicker(time.Duration(iqCache.staleEntryEvictionTimePeriodSeconds()) * time.Second) |
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.
We don't need to set the ticker on the cache struct. It would be cleaner to just define the ticker locally, then stop the ticker in both the select cases and in case <-iqCache.ticker.C
you'd just recreate it after the iqCache.cleanStaleValues()
call.
@@ -2159,7 +2159,7 @@ func TestNewInterQueryCacheValue(t *testing.T) { | |||
Body: io.NopCloser(bytes.NewBuffer(b)), | |||
} | |||
|
|||
result, err := newInterQueryCacheValue(BuiltinContext{}, response, b, &forceCacheParams{}) | |||
result, _, err := newInterQueryCacheValue(BuiltinContext{}, response, b, &forceCacheParams{}) |
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 would be good if we add some tests which exercise the new optional params.
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.
@ashutosh-narkar Not sure I got this right, if you are talking about the new cache config params, we have unit tests in cache_test.go
exercising them already.
http_tests.go
wasn't using the new NewInterQueryCacheWithContext
and InsertWithExpiry
, have updated it now if that's what you meant. Let me know if there is anything more to test in http_test.go
.
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.
I mean in the tests in http_test.go
we invoke http.send
with its params. I was suggesting to add a case where we call http.send
with the new params.
select { | ||
case <-iqCache.ticker.C: | ||
iqCache.cleanStaleValues() | ||
case <-ctx.Done(): |
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.
A unit test that cancels the context to test this code path would be good.
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.
@ashutosh-narkar Can you check the test TestCancelNewInterQueryCacheWithContext
already added? Let me know if you think we can test anything more.
ac9a974
to
4595908
Compare
4595908
to
b3586f4
Compare
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.
@rudrakhp thanks for working on this. Just couple of small comments and then we can get this in. It would be great if you could add an explanation about this change in the commit message body.
topdown/cache/cache.go
Outdated
if iqCache.staleEntryEvictionTimePeriodSeconds() > 0 { | ||
cleanupTicker := time.NewTicker(time.Duration(iqCache.staleEntryEvictionTimePeriodSeconds()) * time.Second) | ||
go func() { | ||
defer cleanupTicker.Stop() |
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.
Nit: We could just move this in the <-ctx.Done()
case and avoid the defer statement.
@@ -2159,7 +2159,7 @@ func TestNewInterQueryCacheValue(t *testing.T) { | |||
Body: io.NopCloser(bytes.NewBuffer(b)), | |||
} | |||
|
|||
result, err := newInterQueryCacheValue(BuiltinContext{}, response, b, &forceCacheParams{}) | |||
result, _, err := newInterQueryCacheValue(BuiltinContext{}, response, b, &forceCacheParams{}) |
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.
I mean in the tests in http_test.go
we invoke http.send
with its params. I was suggesting to add a case where we call http.send
with the new params.
b3586f4
to
1b7b293
Compare
Regularly clean up of cache entries that have expired for a more efficient use of memory. Introduce two new parameters to tune clean up frequency and threshold for forced FIFO eviction. Fixes open-policy-agent#5320 Signed-off-by: Rudrakh Panigrahi <rudrakh97@gmail.com>
1b7b293
to
264ff95
Compare
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.
LGTM. Thanks for working on this @rudrakhp!
Why the changes in this PR are needed?
What are the changes in this PR?
Two new configurable parameters:
inter_query_builtin_cache.forced_eviction_threshold_percentage
: By default100
, an integer that denotes the percentage ofinter_query_builtin_cache.max_size_bytes
that cache usage can grow to before cache will start forcefully evicting entries in a FIFO fashion.inter_query_builtin_cache.stale_entry_eviction_period_seconds
: By default0
, an integer that represents the time period in seconds when the stale entry cleanup routine runs.Notes to assist PR review:
http.send
cache cleaning #5320cache.go
andhttp.go
. Rest are for passing around context and logger objects.Further comments:
N/A