-
Notifications
You must be signed in to change notification settings - Fork 14.1k
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] Automatically add relevant Jinja methods to cache key if present #9572
[fix] Automatically add relevant Jinja methods to cache key if present #9572
Conversation
@@ -173,7 +173,6 @@ def test_viz_cache_key(self): | |||
viz = slc.viz | |||
qobj = viz.query_obj() | |||
cache_key = viz.cache_key(qobj) | |||
self.assertEqual(cache_key, viz.cache_key(qobj)) |
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.
This seems unnecessary, i.e., it's equivalent to,
self.assertEqual(viz.cache_key(qobj), viz.cache_key(qobj))
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 have no idea what I was thinking there 😄
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 wonder if there is the risk that this will cause lots of unnecessary cache misses? I would almost prefer to use cache_key_wrapper
for a case like this, as it should be the main tool for adding non-standard cache keys. I can imagine it's a feature that very few probably use or know about, so perhaps it should be promoted somehow when people use jinja in their queries.
@@ -173,7 +173,6 @@ def test_viz_cache_key(self): | |||
viz = slc.viz | |||
qobj = viz.query_obj() | |||
cache_key = viz.cache_key(qobj) | |||
self.assertEqual(cache_key, viz.cache_key(qobj)) |
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 have no idea what I was thinking there 😄
@villebro I wasn't aware of |
@john-bodley the way I see it, |
@villebro I agree with your point (and noted) that the presence of a URL parameter does not mean it will be used by the query but per the documentation,
I perceive one would expect that if one were using them as defined they wouldn't expect the cached payload to be incorrect, i.e., it feels like a bug if they didn't know they needed to wrap the call with Maybe there's a common ground we could agree on (however I wasn't able to get it working but maybe you could shed some light on maybe how to do it). Is there a way of possibly calling |
I agree, users will probably expect url_params to be added to cache keys. Perhaps we default def url_param(param: str, default: Optional[str] = None, add_to_cache_keys: bool=True) -> Optional[Any]:
... Then we also need to change the regex regex = re.compile(r"\{\{.*(cache_key_wrapper\(.*\)|url_param\(.*).*\}\}") to make sure we don't unnecessarily trigger re-evaluation of jinja code. As I added this feature, I can put together a PR for it if you wish. Oh, the syntax for {{ cache_key_wrapper(url_param("key", "my default value")) }} |
Looking more closely at the default jinja functions, it appears they should all default to being added to extra cache keys, except
|
edc8c8c
to
145b8ea
Compare
3b92f5a
to
13f7a08
Compare
@villebro I took a brief stab at this (commit) though it's untested. You have more context here but my sense is that the My one concern is it seems that the {
"current_user_id": ["user_1", "user_2",],
"url_param": [{"foo": "bar"},],
} Note in the case of the URL parameters having both the key and value is probably necessary for completeness. |
Thanks, I'll test and review shortly. It would be great if you can see this through, as getting a fresh set of eyes on the old code is always positive. If we can completely drop |
13f7a08
to
175ad04
Compare
Codecov Report
@@ Coverage Diff @@
## master #9572 +/- ##
==========================================
- Coverage 70.52% 70.52% -0.01%
==========================================
Files 574 574
Lines 30152 30161 +9
Branches 3060 3066 +6
==========================================
+ Hits 21265 21271 +6
- Misses 8776 8779 +3
Partials 111 111
Continue to review full report at Codecov.
|
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 think this looks mostly good, just a couple of small changes that I believe are vital for this functionality. Also, we should probably add a note in UPDATING.md
informing current users of cache_key_wrapper
that the new default behaviour for url_param
, current_username
and current_user_id
will automatically add the value to extra_cache_keys
unless explicitly setting add_to_cache_keys = False
.
self.extra_cache_keys = extra_cache_keys | ||
self.extra_cache_keys = extra_cache_keys or [] | ||
|
||
def current_user_id(self, add_to_cache_keys: bool = True) -> Optional[int]: |
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 think the references to the moved methods need to be updated in docs/sqllab.rst
.
superset/viz.py
Outdated
|
||
url_params = self.form_data.get("url_params") | ||
|
||
if url_params: | ||
cache_dict["url_params"] = url_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.
I don't think this should be added here, as calling url_params
in the Jinja context will now add the relevant values to extra_keys
.
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.
This needs to be removed.
superset/viz_sip38.py
Outdated
|
||
url_params = self.form_data.get("url_params") | ||
|
||
if url_params: | ||
cache_dict["url_params"] = url_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.
same here (thanks for keeping this in sync)
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.
See ^^.
tests/core_tests.py
Outdated
def test_cache_key_changes_with_url_parameters(self): | ||
self.login(username="admin") | ||
slc = self.get_slice("Girls", db.session) | ||
viz = slc.viz | ||
qobj = viz.query_obj() | ||
cache_key_original = viz.cache_key(qobj) | ||
viz.form_data["url_params"] = {"foo": "bar"} | ||
self.assertNotEqual(cache_key_original, viz.cache_key(qobj)) | ||
|
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 would recommend adding a test making sure that the cache key does not change when url_params
changes, unless some part of the query_obj
is referencing one of the keys present in url_params
using Jinja. The point being we want to make sure only the url params that are used affect the cache key.
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.
Agreed.
The
With regards to |
@villebro could you think of an example when |
@john-bodley I think any |
175ad04
to
81268b2
Compare
@villebro thanks for the feedback. I believe this is now ready for re-review. |
e3684e0
to
36df720
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.
Very nice! 👍 If you could also add a note in UPDATING.md
to let users know this is now much simpler, ie. builtin Jinja functions don't need to be wrapped anymore, this is ready to go.
36df720
to
9006bb3
Compare
@villebro I updated |
CATEGORY
Choose one
SUMMARY
The URL parameters were not being used in the cache key resulting in issues per the attached screenshot. This PR adds the URL params (as well as other JInja macros including
current_user_id
andcurrent_username
) to the cache key by default if they are leveraged by the query.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
Before
After
TEST PLAN
Updated unit tests.
ADDITIONAL INFORMATION
REVIEWERS
to: @villebro