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

#5359 Fix interquery cache concurrency bug #5361

Merged
merged 2 commits into from
Nov 24, 2022

Conversation

asleire
Copy link
Contributor

@asleire asleire commented Nov 4, 2022

This fixes the issue where concurrent requests with identical cache keys cause the interquery cache size usage counter to become invalid, as discussed here

if oldValueOk {
c.usage -= oldValue.SizeInBytes()
}

Copy link
Member

Choose a reason for hiding this comment

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

It would be better if we tried to lock the action from the caller rather than do this as described here. Although this helps in representing the correct cache size, we'll be unnecessarily adding items to the list.

@netlify
Copy link

netlify bot commented Nov 14, 2022

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 8db515e5939f40ca1eb0397911805a207252eaca
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/6373ed5e73ff2c0008b96e38
😎 Deploy Preview https://deploy-preview-5361--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@asleire
Copy link
Contributor Author

asleire commented Nov 14, 2022

@ashutosh-narkar I added a test and comment per your request

@asleire
Copy link
Contributor Author

asleire commented Nov 14, 2022

I switched the fix with a one liner, c.unsafeDelete(k), let me know if you prefer the other one

@@ -138,6 +138,9 @@ func (c *cache) unsafeInsert(k ast.Value, v InterQueryCacheValue) (dropped int)
}
}

// By deleting the old value, if it exists, we ensure the usage variable stays correct
Copy link
Member

Choose a reason for hiding this comment

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

Can we also explain the scenario where we need to take this action. For eg, the concurrent request case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fix simply prevents code like this from breaking the internal state of the cache:

	cache.Insert(ast.StringTerm("foo6").Value, cacheValue6)
	cache.Insert(ast.StringTerm("foo6").Value, cacheValue6)

I don't see the need to document very explicitly who might call cache.Insert in such a way or why they might do it. At least I wouldn't know how to write that documentation. Feel free to update the comment or give me a suggestion on how to write it


if dropped != 0 {
t.Fatal("Expected dropped to be zero")
}
Copy link
Member

Choose a reason for hiding this comment

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

Can we add a test case like this to show the concurrent use case.

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 added a unit test with concurrent inserts

@ashutosh-narkar
Copy link
Member

@asleire can you please squash your commits and sign it as well.

@asleire
Copy link
Contributor Author

asleire commented Nov 15, 2022

@asleire can you please squash your commits and sign it as well.

Done :)

Signed-off-by: Aleksander <Alekken@live.no>
Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

Thanks a lot!

@srenatus srenatus merged commit 955464e into open-policy-agent:main Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants