TTL-cache slow read-only CKAN actions (organization_list, group_list) - #256
Merged
Merged
Conversation
organization_list and group_list with all_fields=true are an N+1 in CKAN: it calls organization_show once per org, and each of those runs its own Solr query for the dataset count. On oddk-prod that is 51 sequential queries and ~9.3s. The ODDK theme fetches both in its "/" handler, so every home page render pays it, and frontend-v2's own /organization route pays it again. Measured: home 11.9s, /organization 12.5s. Cache the results at getJsonResponse, the single funnel for all CKAN calls, behind an action allowlist so nothing user- or query-specific is affected -- package_search in particular still goes straight to CKAN every time. Off by default (API_CACHE_TTL=0) so other portals on this repo are unchanged; ODDK opts in via env. Entries hold the in-flight promise rather than just the value, so concurrent misses collapse into one upstream call instead of each firing its own 51 queries. A failed refresh serves the last good value so a CKAN blip cannot blank a page that has rendered before. The cache is per-pod and cold after a restart, which is accepted: the point is that the Nth visitor does not pay what the 1st does. The upstream CKAN fix that would remove the need for this is tracked in datopian/tech-devops#668. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
organization_listandgroup_listwithall_fields=trueare an N+1 in CKAN._group_or_org_listcallsorganization_showonce per organization, and each of those runs its own Solr query for the dataset count. On oddk-prod that is 51 sequential queries, ~9.3s.The ODDK theme fetches both in its
/handler, so every home page render pays it, and frontend-v2's own/organizationroute pays it again. Measured on prod://organizationorganization_list(plain)organization_list?all_fields=true&include_dataset_count=falseorganization_list?all_fields=true&include_dataset_count=trueNot a capacity problem — CKAN sat at 1–23m CPU against a 1000m limit throughout. It is idle, waiting on 51 sequential round-trips.
What this does
Caches results at
getJsonResponse, the single funnel for every CKAN call inlib/dms.js, behind an action allowlist.getJsonResponse's signature is unchanged, so no call site moves; the raw request is split out intofetchJsonResponse.API_CACHE_TTL=0), so other portals on this repo are unaffected. ODDK opts in withAPI_CACHE_TTL=3600.organization_list,group_list). Anything user- or query-specific —package_searchabove all — still goes straight to CKAN on every request.The cache is in-process, so it is per-pod and cold after a restart. That is a deliberate accepted trade: the point is that the Nth visitor does not pay what the 1st does. Redis was considered and rejected for now — it would need
REDIS_URLplumbed into the ODDK frontend env, which is not there today.Deploying to ODDK
Two steps, and this PR is only the first:
datopian/dx-helm-oddk-prod, bump the pinned commit infrontend/Dockerfile(currentlygit checkout 99f6782e426c8d85d09a555e0b02e96e4839565d) to this branch's merge commit, and setAPI_CACHE_TTL=3600in the frontend env.Until step 2, prod does not pick this up — the Dockerfile pins an exact SHA.
Testing
tests/lib/cache.test.js, 8 tests, all passing: key stability and separation, hit within TTL, refetch after expiry, single-flight collapse, stale-on-error, propagation with nothing cached, and retry after failure.Integration-checked against
DmsModeldirectly:organization_listserved from cache on the second call, both param orderings sharing one entry,package_searchnever cached, and no caching at all whenAPI_CACHE_TTLis unset.tests/lib/index.test.jshas 2 pre-existing failures onoddk/custom-facet(apackage_searchnock fixture that does not match the branch's facet params). Verified identical with and without this change by stashing it — not introduced here, andgetOrganizations api workspasses.Follow-up
The real fix is upstream: make CKAN's
all_fieldspath use the batchedgroup_list_dictizeinstead of per-orgorganization_show, which would take this from 9.3s to ~1.1s and make caching unnecessary. Tracked indatopian/tech-devops#668.