perf: bulk-prefetch grades in ProblemGradeReport and bound its per-learner caches - #38944
perf: bulk-prefetch grades in ProblemGradeReport and bound its per-learner caches#38944blarghmatey wants to merge 1 commit into
Conversation
…arner caches
ProblemGradeReport read persisted grades one learner at a time, unlike
CourseGradeReport, whose _rows_for_users opens with a _CourseGradeBulkContext.
With nothing prefetched, PersistentSubsectionGrade.bulk_read_grades missed the
per-course RequestCache and fell through to a query per learner -- and this
report walks course_grade.problem_scores, so it touches more subsection data per
learner than the course report does.
Measured with CaptureQueriesContext over the report, varying cohort size:
learners 5 10 25 50
before 20 30 60 110 (fits 2N + 10)
after 13 13 13 13
Adds a regression test asserting the flat count at two cohort sizes; a
single-size assertion would pass while a per-learner read crept back in.
_ProblemGradeBulkContext is deliberately narrower than _CourseGradeBulkContext:
the problem report emits no cohort, team, certificate or course-tag columns, so
prefetching those would be wasted work. It loads persisted course and subsection
grades plus enrollment states, which is what the rows actually read.
Also in the same report path:
* Pass the already-loaded block structure to grading_context() instead of
calling grading_context_for_course(), which re-enters get_course_in_cache.
BlockStructureManager.get_collected() deserializes fresh from the cache
backend on every call, so a large course paid that cost, and its peak
allocation, twice. CourseGradeReport already does this correctly.
* Clear the visible-blocks and subsection-override caches between batches.
RequestCache is only flushed when the task ends, and unlike the course-keyed
grade prefetches -- replaced wholesale each batch -- these are keyed per
(user, course) and only ever grow, though nothing reads an entry again once
that learner's row is written. The _clear_caches hook already existed and
was called per batch; the base implementation was empty. Adds the missing
clear_prefetched_data() counterparts to VisibleBlocks and
PersistentSubsectionGradeOverride, and a
clear_prefetched_grade_overrides_and_visible_blocks() api function pairing
with the existing prefetch_grade_overrides_and_visible_blocks().
* Wire USER_BATCH_SIZE through to grouper(). It was defined on
CourseGradeReport but never referenced, so the effective batch size was
grouper's hardcoded default and tuning the constant did nothing. Moved to
GradeReportBase so both reports honour it.
CSV contents and format are unchanged by all of the above.
Refs: openedx#38943
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ki1NYMjSjVB4uz1gdgsjBh
|
Thanks for the pull request, @blarghmatey! 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. |
|
@openedx/wg-maintenance-openedx-platform-oncall — this is ready for engineering review. Checklist from the bot above:
Short version: Also folds in three smaller fixes from the same issue: a double block-structure deserialization, two per-learner RequestCaches that were never evicted across a long task, and a Related: #38946 does the remaining finding from #38943 (streaming the report upload). Independent of this one — they touch different parts of |
What
Addresses four of the five findings in #38943. The remaining one (Finding 4, streaming
the report upload through
DjangoStorageReportStore.store) is left for a separate PRbecause it touches shared storage code used by every instructor-task report, not just
grade reports, and wants its own test pass.
Why it matters
ProblemGradeReportread persisted grades one learner at a time, unlikeCourseGradeReport, whose_rows_for_usersopens with a_CourseGradeBulkContext. Withnothing prefetched,
PersistentSubsectionGrade.bulk_read_gradesmissed the per-courseRequestCache and fell through to a query per learner — and this report walks
course_grade.problem_scores, so it touches more subsection data per learner than thecourse report does.
The issue flagged the attribution as unmeasured. It is now measured, with
CaptureQueriesContextaround the report while varying cohort size:Before fits
2N + 10; after is flat. At a full 100-learner batch that is 210 queriesversus 13, and the report batches at 100, so a 5,000-learner course goes from roughly
10,500 grade-related queries to about 700.
Changes
Finding 1 — bulk prefetch. New
_ProblemGradeBulkContext, deliberately narrower than_CourseGradeBulkContext: the problem report emits no cohort, team, certificate orcourse-tag columns, so prefetching those would be wasted work. It loads persisted course
and subsection grades plus enrollment states, which is what the rows actually read.
Finding 2 — double block-structure load.
graded_scorable_blocks_headernow passesthe already-loaded structure to
grading_context()instead of callinggrading_context_for_course(), which re-entersget_course_in_cache.BlockStructureManager.get_collected()deserializes fresh from the cache backend on everycall, so a large course paid that cost — and its peak allocation — twice.
CourseGradeReportalready does this correctly.Finding 3 — unbounded per-learner caches. RequestCache is only flushed on
task_postrun. Unlike the course-keyed grade prefetches, which are replaced wholesaleeach batch, the visible-blocks and subsection-override caches are keyed per
(user, course)and only ever grow — yet nothing reads an entry again once that learner'srow has been written. The
_clear_cacheshook already existed and was already called perbatch; the base implementation was simply empty. This adds the missing
clear_prefetched_data()counterparts toVisibleBlocksandPersistentSubsectionGradeOverride, plus aclear_prefetched_grade_overrides_and_visible_blocks()api function pairing with theexisting
prefetch_grade_overrides_and_visible_blocks().Finding 5 — dead constant.
USER_BATCH_SIZEwas defined onCourseGradeReportbutnever referenced, so the effective batch size was
grouper's hardcoded default and tuningthe constant did nothing. Moved to
GradeReportBaseand wired through, so both reportshonour it.
Testing
lms/djangoapps/instructor_task/tests/test_tasks_helper.py— 104 passed (103existing plus the new query-count test).
lms/djangoapps/grades/tests/— 741 passed, including the existingtest_query_countsassertions, which are unchanged.TestInstructorGradeReport::test_query_countsassertion forCourseGradeReport(48 queries) still passes unmodified, confirming the shared_clear_cachesandUSER_BATCH_SIZEchanges do not alter that report's behaviour.The new test asserts the same query count at two different cohort sizes rather than one.
That is the point of it: a single-size assertion would keep passing if the per-learner
read crept back in.
Backward compatibility
CSV contents and format are unchanged. No schema or data migration. Findings 1–3 reuse
helpers the codebase already relies on; the only new public surface is the
clear_prefetched_*counterpart to an existingprefetch_*function.Context
Found while investigating a 5,154-learner course whose problem grade report took ~80
minutes end to end, of which the CSV upload was 0.17s — essentially all of it in the
per-learner compile loop. Related: #38911 proposes bringing the same
CourseGradeReportbulk-prefetch pattern to the synchronous CCX grade report; same defect class, different
code path.