fix: ensure navigation sidebar serves fresh data after course publish#38785
fix: ensure navigation sidebar serves fresh data after course publish#38785wgu-taylor-payne wants to merge 1 commit into
Conversation
|
Thanks for the pull request, @wgu-taylor-payne! This repository is currently maintained by Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review. 🔘 Get product approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. DetailsWhere can I find more information?If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources: When can I expect my changes to be merged?Our goal is to get community contributions seen and reviewed as efficiently as possible. However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
5103ff8 to
4374146
Compare
ormsbee
left a comment
There was a problem hiding this comment.
I don't think this is operationally feasible for large courses and high traffic. Let's talk more about other possible mitigations.
|
|
||
| if not course_blocks: | ||
| # Ensure the block structure cache is up-to-date before reading. | ||
| get_block_structure_manager(course_key).update_collected_if_needed() |
There was a problem hiding this comment.
Going through the collection phase of a large course can be extremely expensive, which is why it's done asynchronously in celery tasks or management commands (it can often exceed the 30s timeout that many sites use for giving up on web worker requests). Placing it in the GET here also risks causing a stampede if it is a popular course that many concurrent users are trying to access, as parallel workers try to recompute the same collection phase data.
There was a problem hiding this comment.
Thank you, I appreciate this feedback. I'll look into another way of preventing the stale cache.
There was a problem hiding this comment.
@ormsbee I've pushed a new approach where instead of caching on the course version number, we cache on a block structure version which is updated after the block structure has been updated. We keep this block structure version in the cache for each course. I've updated the PR description with more details on this. Any thoughts on this approach?
|
For instance, I think a course's navigation being incorrect for a minute after a deletion is a bad, but not necessarily release-blocking bug (FYI @crathbun428 and @jmakowski1123, who can weigh in here). If the wrong navigation is getting cached for an hour, then maybe that's the part that we should focus on for this fix. |
|
I agree with Dave, I would not classify a 30-60sec cache as a blocker. But an hour is a bigger problem. |
ed9f8a0 to
4490373
Compare
| Returns the current block structure version for the given course. | ||
| This version changes each time the block structure cache is rebuilt. | ||
| """ | ||
| return cache.get(BLOCK_STRUCTURE_VERSION_KEY.format(course_key), '') |
There was a problem hiding this comment.
This is going to be '' when this is first deployed to a site, right (e.g. right after a version upgrade, looking at already-published courses). Why use this instead of the data_version field that's on the BlockStructureModel?
There was a problem hiding this comment.
Thanks for pointing that out; I agree that it would be a better value to use for the cache keys. I've updated my code to use it.
After a course publish in Studio, the CourseNavigationBlocksView can cache stale block structure data for up to 1 hour. This happens because the block structure rebuild task runs with a 30-second delay, but the navigation view may be hit during that window, read the old block structure from its cache, and store the stale result under the new course_version key. The fix adds an update_collected_if_needed() call on cache miss, ensuring the block structure is fresh before we build and cache the navigation tree. This only runs on cache misses and adds negligible overhead for the common case (block structure already up-to-date).
4490373 to
8c70271
Compare
Description
After a course publish in Studio, the navigation sidebar (
CourseNavigationBlocksView) can serve stale block structure data for up to 1 hour. This happens because the sidebar cache key previously usedcourse_version(which changes immediately on publish), causing a cache miss during the ~30s window before the async block structure rebuild task completes. The view would read the still-stale block structure, cache that stale result under the new key for 1 hour, and serve stale data indefinitely.Fix: Replace
course_versionwithblock_structure_versionin the navigation sidebar cache key. This version is derived fromBlockStructureModel.data_versionand only changes when the block structure rebuild task actually completes. Additionally,get_block_structure_version()never populates the cache — only the writer (_update_block_structure_version, called after a successful rebuild) sets the cached value. This eliminates a race condition where concurrent readers could permanently cache a stale version.Impacted user roles: Learner (sees correct course outline sooner after a publish), Course Author (changes reflected faster).
Fixes openedx/wg-build-test-release#600
How it works
update_course_in_cacherebuilds the block structure, then calls_update_block_structure_version()which reads the newdata_versionfromBlockStructureModeland sets it in cache withtimeout=Noneget_block_structure_version()reads from cache; on a miss it falls back to the database without caching the result — the cache is populated exclusively by the writer after a successful rebuildCourseNavigationBlocksViewusesblock_structure_versionin its cache key instead ofcourse_versionWhy not synchronous rebuild on the request path?
The initial approach called
update_collected_if_needed()on the GET path. As noted in review, block structure collection for large courses can exceed 30s and risks a stampede under concurrent load. The version-based approach avoids any expensive work on the request path.Performance impact
cache.get()call to readblock_structure_version.BlockStructureModel.objects.get()by primary keyget_course_outline_block_tree()runs and result is cached for 1 hourDeploy considerations
BlockStructureModel.data_versionstores the same value ascourse.course_version(set fromroot_block.course_versionat rebuild time). Since the sidebar cache key substitutes one for the other, existing cached sidebar entries remain warm — the key resolves to the same string.The
block_structure_versioncache key itself won't exist until the first post-deploy publish per course. Until then,get_block_structure_version()falls through to a single PK database lookup onBlockStructureModel.Supporting information
This issue was discovered testing an internal Open edX instance. After deleting a unit in Studio and refreshing the course in the learning MFE within a few seconds, the deleted unit remained visible in the sidebar for up to 1 hour.
The root cause involves three components:
course_versionTesting instructions
Automated:
Manual (requires async Celery):
Without the fix: unit remains visible for up to 1 hour after step 2.
With the fix: unit disappears after step 4 (once the rebuild task completes).
Deadline
None
Other information
block_structure_versioncache key is set withtimeout=None(no expiry) by the writer. Readers never populate the cache, eliminating the race condition entirely.AI Usage
Used Kiro to aid in root cause discovery, evaluate multiple fix approaches (synchronous rebuild, TTL-based self-healing, writer-only cache ownership), debug the race condition via log analysis, and draft this PR description.