superset icon indicating copy to clipboard operation
superset copied to clipboard

fix(cachekey): use data_cache for chart query result invalidation

Open Abdulrehman-PIAIC80387 opened this issue 6 months ago • 5 comments

SUMMARY

Fixes #40489. POST /api/v1/cachekey/invalidate was calling cache_manager.cache.delete_many(...) (which uses CACHE_CONFIG.CACHE_KEY_PREFIX), but the chart query results tracked by CacheKey rows are written via cache_manager.data_cache (DATA_CACHE_CONFIG.CACHE_KEY_PREFIX).

When a deployment configures the two caches with different prefixes (the recommended multi-cache setup), delete_many issues DEL against the wrong Redis prefix and silently misses every key. The metadata rows are cleared, but Redis keeps serving stale chart data until TTL expiry. The mismatch is invisible in default-config deployments because both prefixes resolve to the same value.

Fix

One-line backend change in superset/cachekeys/api.py:

-            all_keys_deleted = cache_manager.cache.delete_many(*cache_keys)
+            all_keys_deleted = cache_manager.data_cache.delete_many(*cache_keys)

TESTING INSTRUCTIONS

pytest tests/integration_tests/cachekeys/api_tests.py -v
  • Existing tests updated to write/read via cache_manager.data_cache (they previously exercised the buggy path; cache and data_cache resolve to the same backend in the default test config so the asserts passed anyway).
  • New regression test test_invalidate_uses_data_cache_not_default_cache patches both cache_manager.data_cache.delete_many and cache_manager.cache.delete_many, then asserts the API calls the data cache and never touches the default cache — this is the precise condition #40489 fails on in production.

Manual reproduction (per the issue)

CACHE_CONFIG      = {"CACHE_TYPE": "RedisCache", "CACHE_KEY_PREFIX": "superset_", ...}
DATA_CACHE_CONFIG = {"CACHE_TYPE": "RedisCache", "CACHE_KEY_PREFIX": "superset_data_", ...}
  1. View a chart so it caches a result. redis-cli EXISTS superset_data_<hash> → 1.
  2. POST /api/v1/cachekey/invalidate {"datasource_uids":["<uid>"]} → 201.
  3. Before fix: redis-cli EXISTS superset_data_<hash> → still 1 (chart serves stale data).
  4. After fix: redis-cli EXISTS superset_data_<hash> → 0 (chart re-runs fresh query).

ADDITIONAL INFORMATION

  • [x] Has associated issue: #40489
  • [ ] Required feature flags:
  • [ ] Changes UI
  • [ ] Includes DB Migration
  • [ ] Introduces new feature or API
  • [ ] Removes existing feature or API

Abdulrehman-PIAIC80387 avatar May 28 '26 09:05 Abdulrehman-PIAIC80387

Code Review Agent Run #4ae5e8

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: 4477d70..4477d70
    • superset/cachekeys/api.py
    • tests/integration_tests/cachekeys/api_tests.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

bito-code-review[bot] avatar May 28 '26 09:05 bito-code-review[bot]

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 64.52%. Comparing base (dc64716) to head (963273d). :warning: Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #40493   +/-   ##
=======================================
  Coverage   64.52%   64.52%           
=======================================
  Files        2664     2664           
  Lines      146178   146178           
  Branches    33722    33722           
=======================================
  Hits        94319    94319           
  Misses      50142    50142           
  Partials     1717     1717           
Flag Coverage Δ
hive 39.22% <0.00%> (ø)
mysql 57.97% <100.00%> (ø)
postgres 58.04% <100.00%> (ø)
presto 40.80% <0.00%> (ø)
python 59.47% <100.00%> (ø)
sqlite 57.67% <100.00%> (ø)
unit 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar May 28 '26 09:05 codecov[bot]

@rusackas Looks like Copilot crashed, can you re-run it?

CaseyLeask avatar Jun 10 '26 01:06 CaseyLeask

Actually not yet. I'm out of credits on that one. :(

rusackas avatar Jun 10 '26 02:06 rusackas

@rusackas I don't know when Copilot credits refresh. Should I come back to this in July?

CaseyLeask avatar Jun 15 '26 04:06 CaseyLeask

Don't worry about Copilot here, I'm out of credits on it, but it's not my/our only way to review ;). We can probably ship this once it's rebased. I'll just do that now for ya :)

Verified that chart results write through data_cache via set_query_result(region=CacheRegion.DATA), and that's the same path that records the CacheKey row, so invalidating against the default cache was hitting the wrong backend. Fix is correct and the regression test is solid. Approving after the rebase...

rusackas avatar Jun 27 '26 00:06 rusackas

Bito Automatic Review Skipped – PR Already Merged

Bito scheduled an automatic review for this pull request, but the review was skipped because this PR was merged before the review could be run.
No action is needed if you didn't intend to review it. To get a review, you can type /review in a comment and save it

bito-code-review[bot] avatar Jun 27 '26 03:06 bito-code-review[bot]