Skip to content

feat(gaq): minimum result-cache TTL for async chart-data queries - #43463

Merged
villebro merged 1 commit into
gaq-to-gtffrom
villebro/gaq-min-cache-ttl
Aug 24, 2026
Merged

villebro merged 1 commit into
gaq-to-gtffrom
villebro/gaq-min-cache-ttl

Conversation

@villebro

Copy link
Copy Markdown
Member

SUMMARY

Targets gaq-to-gtf. A short cache TTL can break the async chart-data flow: async runs each query, caches its result, and the client then re-issues the request to read it from cache. If the result-cache TTL is shorter than that round trip (task queue + execution + poll interval + re-request), the entry can be evicted before it's fetched, and the chart hangs / errors.

Add GLOBAL_ASYNC_QUERIES_MIN_CACHE_TTL (default 300s / 5 min): when a query runs on the async path, its result-cache TTL is floored to this value.

  • Applied in QueryContextProcessor.get_cache_timeout, gated on an is_async_execution flag set only by the async task (execute_chart_query). Synchronous /chart/data requests are never floored, even when GLOBAL_ASYNC_QUERIES is enabled — as requested.
  • A longer slice/dataset/deployment timeout is kept as-is (it's a floor, not an override); 0 ("cache forever" in flask-caching) is left untouched.
  • Doesn't affect the cache key (only the write TTL), so the client's sync re-request reads the same entry.

TESTING INSTRUCTIONS

  • pytest tests/unit_tests/common/test_async_min_cache_ttl.py — async floors a short TTL (60→300), keeps a longer one (3600), leaves 0 untouched, and does not floor sync execution.
  • pytest tests/unit_tests/common/test_query_context_processor.py tests/unit_tests/tasks/test_async_queries.py — unchanged behavior; mypy/ruff/pre-commit clean.

ADDITIONAL INFORMATION

  • Has associated issue
  • Required feature flags: GLOBAL_ASYNC_QUERIES (the floor only applies on the async path)
  • Changes UI
  • Includes DB Migration
  • Introduces new feature or API (GLOBAL_ASYNC_QUERIES_MIN_CACHE_TTL config)
  • Removes existing feature or API

Async chart data caches each query's result and the client then re-issues the
request to read it back, so a cache TTL shorter than that round trip could evict
the result before it is fetched, hanging the chart. Add
GLOBAL_ASYNC_QUERIES_MIN_CACHE_TTL (default 5 min): when a query runs async, its
result-cache TTL is floored to this value.

The floor is applied in QueryContextProcessor.get_cache_timeout, gated on an
is_async_execution flag set only by the async task (execute_chart_query), so
synchronous /chart/data requests are never affected even when GLOBAL_ASYNC_QUERIES
is enabled. A longer slice/dataset/deployment timeout is kept as-is, and 0
("cache forever") is left untouched.
@dosubot dosubot Bot added global:async-query Related to Async Queries feature infra:caching Infra setup and configuration related to caching labels Aug 24, 2026
@bito-code-review

bito-code-review Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Bito Automatic Review Skipped - Branch Excluded

Bito didn't auto-review because the source or target branch is excluded from automatic reviews.
No action is needed if you didn't intend for the agent to review it. Otherwise, to manually trigger a review, type /review in a comment and save.
You can change the branch exclusion settings here, or contact your Bito workspace admin at [email protected].

@netlify

netlify Bot commented Aug 24, 2026

Copy link
Copy Markdown

✅ Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit a271cb1
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6a8c5bf90798aa0008a8c71c
😎 Deploy Preview https://deploy-preview-43463--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@villebro

Copy link
Copy Markdown
Member Author

/review

Comment on lines +621 to +623
if 0 < timeout < min_ttl:
return min_ttl
return timeout

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggestion: The post-resolution floor also applies to NATIVE_FILTER_OPTIONS_CACHE_TIMEOUT, despite that timeout being an independent freshness policy for native filter option queries. An async native-filter request configured with a short value such as 30 seconds will be cached for 300 seconds and can serve stale filter values. Exclude native filter option queries from this floor or apply the floor before selecting their dedicated timeout. [cache]

Severity Level: Major ⚠️
- ⚠️ Native filter options remain stale beyond configured freshness.
- ⚠️ RLS-constrained filter values can remain outdated.
- ⚠️ Dashboard filter controls may show unavailable values.

Use CodeAnt Skill

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** superset/common/query_context_processor.py
**Line:** 621:623
**Comment:**
	*Cache: The post-resolution floor also applies to `NATIVE_FILTER_OPTIONS_CACHE_TIMEOUT`, despite that timeout being an independent freshness policy for native filter option queries. An async native-filter request configured with a short value such as 30 seconds will be cached for 300 seconds and can serve stale filter values. Exclude native filter option queries from this floor or apply the floor before selecting their dedicated timeout.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch — valid and reachable. Native filter option queries can run on the async path (FilterValue.tsx passes async_mode_override), so flooring their dedicated NATIVE_FILTER_OPTIONS_CACHE_TIMEOUT to the async minimum would serve stale (and potentially RLS-constrained) filter values for the floor's duration.

Fixed in 9a1b0d3cf5: _resolve_cache_timeout now reports whether the resolved timeout is floorable, and the native-filter-options branch returns floorable=False so get_cache_timeout skips the floor for it. Added test_async_native_filter_options_query_is_not_floored (30s stays 30s on the async path). The general result-cache timeouts (steps 3–5) and explicit custom_cache_timeout still floor, since those results genuinely need to survive the async cache→read-back round trip.

Comment on lines +125 to +127
# Floor the result-cache TTL: async caches the result for a follow-up
# request to read back (see get_cache_timeout).
query_context.is_async_execution = True

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggestion: Setting is_async_execution does not ensure that the cache entry is written with the floored TTL. If the initial cache miss races with another request that populates the same key, get_df_payload_result will accept that existing entry and skip set_query_result, leaving its original short expiration. The follow-up request can then miss the cache and schedule another query. The async task should refresh or rewrite the entry using the async timeout when it finds an existing result. [race condition]

Severity Level: Major ⚠️
- ❌ Async chart re-fetch can miss completed results.
- ⚠️ Identical queries may be scheduled repeatedly.
- ⚠️ Users can see delayed or failed chart rendering.

Use CodeAnt Skill

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** superset/tasks/async_queries.py
**Line:** 125:127
**Comment:**
	*Race Condition: Setting `is_async_execution` does not ensure that the cache entry is written with the floored TTL. If the initial cache miss races with another request that populates the same key, `get_df_payload_result` will accept that existing entry and skip `set_query_result`, leaving its original short expiration. The follow-up request can then miss the cache and schedule another query. The async task should refresh or rewrite the entry using the async timeout when it finds an existing result.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The mechanism is correct — on a cache hit get_df_payload_result skips set_query_result, so an entry another request populated with a short TTL keeps that TTL — but in practice this is low-severity and self-healing, so I've left it as-is:

  • Async vs. async can't race. Each QueryObject runs as a SHARED GTF task keyed by query_cache_key, so concurrent async requests for the same key dedup to one task. There is no second async writer.
  • The only race is async-task vs. a concurrent sync request (sync never floors). For it to actually cause a re-fetch miss, the sync-written TTL must expire in the window between the async task observing the hit and the client re-issuing — roughly one poll interval (~0.5–1s). Only a pathologically sub-second DATA_CACHE TTL would bite.
  • It self-heals. On a re-request miss the async flow just schedules the query again and succeeds on the next round; it degrades to one extra round trip, not a hang.

Re-setting the TTL on every async cache hit would add a cache write to the hot path (flask-caching has no cheap "touch"/EXPIRE through QueryCacheManager) to cover a self-correcting sub-second edge case, which isn't a good trade. Happy to revisit if you'd prefer the guard.

Comment on lines +64 to +66
mocker.patch.dict(current_app.config, {"GLOBAL_ASYNC_QUERIES_MIN_CACHE_TTL": 300})
# The floor applies only to async execution; a sync request keeps its timeout
# even when GLOBAL_ASYNC_QUERIES is enabled.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggestion: The test claims to verify synchronous behavior while GLOBAL_ASYNC_QUERIES is enabled, but it only patches the minimum TTL. If the implementation incorrectly floors synchronous requests whenever the global async feature is enabled, this test still passes because that feature remains at its default value. Patch GLOBAL_ASYNC_QUERIES to true in this test so the asserted sync-path gating is actually exercised. [incomplete implementation]

Severity Level: Minor 🧹
- ⚠️ Sync-path gating is not tested with async globally enabled.

Use CodeAnt Skill

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** tests/unit_tests/common/test_async_min_cache_ttl.py
**Line:** 64:66
**Comment:**
	*Incomplete Implementation: The test claims to verify synchronous behavior while `GLOBAL_ASYNC_QUERIES` is enabled, but it only patches the minimum TTL. If the implementation incorrectly floors synchronous requests whenever the global async feature is enabled, this test still passes because that feature remains at its default value. Patch `GLOBAL_ASYNC_QUERIES` to true in this test so the asserted sync-path gating is actually exercised.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fair — hardened in 9a1b0d3cf5. test_sync_execution_is_not_floored now patches FEATURE_FLAGS={"GLOBAL_ASYNC_QUERIES": True}, so the test proves the floor is gated on the execution mode (is_async_execution), not on the feature flag. If a future change accidentally gated on the flag instead, this test would now fail.

@codecov

codecov Bot commented Aug 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 78.85%. Comparing base (5ab7d5f) to head (a271cb1).

Files with missing lines Patch % Lines
superset/tasks/async_queries.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff               @@
##           gaq-to-gtf   #43463      +/-   ##
==============================================
- Coverage       78.86%   78.85%   -0.01%     
==============================================
  Files            2883     2883              
  Lines          164782   164776       -6     
  Branches        38083    38084       +1     
==============================================
- Hits           129949   129941       -8     
- Misses          32388    32390       +2     
  Partials         2445     2445              
Flag Coverage Δ
hive 38.13% <33.33%> (-0.01%) ⬇️
mysql 57.69% <50.00%> (-0.01%) ⬇️
postgres 57.73% <50.00%> (-0.01%) ⬇️
presto 40.07% <50.00%> (-0.01%) ⬇️
python 83.53% <91.66%> (-0.01%) ⬇️
sqlite 57.42% <50.00%> (-0.01%) ⬇️
unit 73.62% <91.66%> (-0.02%) ⬇️

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

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

@villebro
villebro merged commit 29af4ec into gaq-to-gtf Aug 24, 2026
73 checks passed
@villebro
villebro deleted the villebro/gaq-min-cache-ttl branch August 24, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

global:async-query Related to Async Queries feature infra:caching Infra setup and configuration related to caching size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant