|
| 1 | +.. _openedx-learning-adr-0004: |
| 2 | + |
| 3 | +4. How should learner competency mastery be recorded concurrently and at scale? |
| 4 | +================================================================================ |
| 5 | + |
| 6 | +Status |
| 7 | +------ |
| 8 | +Proposed. |
| 9 | + |
| 10 | +Context |
| 11 | +------- |
| 12 | +A learner's mastery of one competency is stored at three levels of the criteria tree: the graded |
| 13 | +leaf criterion, each criteria group above it, and the competency itself. There is one row per |
| 14 | +learner and node, updated in place (:ref:`openedx-learning-adr-0002`, |
| 15 | +:ref:`openedx-learning-adr-0003`). Each row holds one of three values, lowest to highest: |
| 16 | +``AttemptedNotDemonstrated``, ``PartiallyAttempted``, ``Demonstrated``. |
| 17 | + |
| 18 | +One grade change updates the leaf and then every row above it, for many learners at once. This ADR |
| 19 | +decides how those updates stay correct when two of them for the same learner overlap. |
| 20 | + |
| 21 | +The problem: a group requires both Assignment A and Assignment B, and celery tasks recomputing |
| 22 | +grades and competency statuses for this worker run at the same time. That is likely to happen |
| 23 | +when instructor actions trigger multiple subsection grading events. |
| 24 | +Each of the two writers sees its own assignment done and the other still outstanding, |
| 25 | +so both write "not demonstrated" for the group. Both are wrong, both have finished, and nothing is |
| 26 | +left to correct it. |
| 27 | + |
| 28 | +Two constraints shape the answer. First, **the grading task cannot be one transaction**: it reads |
| 29 | +MongoDB and memcached, queues further celery tasks, publishes events, and triggers database writes |
| 30 | +owned by four other Django apps. Wrapping all of that would roll back other apps' data and publish |
| 31 | +events for a grade that never commits. Second, **everything above the leaf is derived**: a group's |
| 32 | +value can always be recalculated from the leaves beneath it, so the leaf is the only row that is a |
| 33 | +direct consequence of the grade. |
| 34 | + |
| 35 | +That constraint is why the whole grading task can't be one transaction. A separate constraint is why |
| 36 | +even a transaction scoped only to the leaf and its rollup would still be wrong: two tasks finishing different |
| 37 | +assignments for the same group race on that group's row regardless of transaction size, since each transaction |
| 38 | +hides its writes from the other until it commits (Rejected Alternative 2). |
| 39 | + |
| 40 | +Decision |
| 41 | +-------- |
| 42 | + |
| 43 | +1. **Write the leaf status in the same transaction as the grade. Nothing above it.** |
| 44 | + The grading task calls one openedx-core function, which writes the leaf, so the grade and its |
| 45 | + leaf commit or fail together. |
| 46 | + |
| 47 | +2. **Schedule rollup as a secondary celery task, asynchronously after the grade is recorded.** |
| 48 | + This separation avoids direct contention between concurrent grade changes on shared parent nodes. |
| 49 | + The secondary task is automatically retried by celery if it fails. |
| 50 | + |
| 51 | +3. **Rollup commits each level before reading the next, with no locks.** |
| 52 | + A writer sees only committed data, so whichever writer reads a parent last sees all its children |
| 53 | + at their final values and computes the correct result. This depends on Decision 4, which provides |
| 54 | + a reconciliation rule. |
| 55 | + |
| 56 | +4. **Automatic updates may only raise a status, never lower it.** |
| 57 | + Each write stores whichever is higher, the stored or the newly computed value, so concurrent |
| 58 | + writers cannot overwrite each other. A writer reading stale data can only compute a value that |
| 59 | + is too low, and too low is discarded. That is also what makes celery's repeated and out-of-order |
| 60 | + delivery harmless. |
| 61 | + |
| 62 | +5. **Add a manually-invoked recovery mechanism.** |
| 63 | + For example a management command or Django admin action forces roll-ups to recalculate for a given range, to recover from |
| 64 | + operational failures, content tagging errors, or bugs in the roll-up code that celery's retry |
| 65 | + won't catch. |
| 66 | + |
| 67 | +6. **Only a direct staff edit may lower a status.** A staff correction may set any value, and the |
| 68 | + rows above it are recalculated and overwritten rather than merged. A later grade change can raise |
| 69 | + what an edit lowered, but never lower what an edit raised. The staff correction takes a row lock |
| 70 | + on the ``StudentCompetencyCriteriaGroupStatus`` row for that learner and the competency's root |
| 71 | + criteria group, the group with no parent; no other path takes a lock. |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +Rejected Alternatives |
| 76 | +--------------------- |
| 77 | + |
| 78 | +1. Lock each criteria group row before recalculating it. |
| 79 | + |
| 80 | + - Pros: |
| 81 | + - Correctness comes from making contending writers take turns, which is easier to prove than |
| 82 | + an argument about the order of commits and reads. |
| 83 | + - Cons: |
| 84 | + - One grade change can affect several leaves of the same tree, so a writer can need several |
| 85 | + locks at once, which introduces deadlocks that need their own detection and retry code. |
| 86 | + - It puts a lock wait on every grade change. MySQL waits 50 seconds by default, inside a task |
| 87 | + allowed 300 seconds in total. |
| 88 | + - Correctness would depend on the isolation level, silently, and SQLite has no row locks, so |
| 89 | + the test suite could not exercise it. |
| 90 | + |
| 91 | +2. Share one transaction between the grade and the whole roll-up, not just the leaf. |
| 92 | + |
| 93 | + - Pros: |
| 94 | + - The grade and every mastery row it touches would commit or fail together, so no roll-up |
| 95 | + could ever be left unfinished and Decisions 2 and 5 would be unnecessary. |
| 96 | + - Cons: |
| 97 | + - The grading task cannot be wrapped in a transaction at all, for the reasons in the Context. |
| 98 | + - Wrapping only the roll-up is worse than doing nothing: it hides each writer's changes from |
| 99 | + the other until both have finished, which is the problem in the Context again, one level up |
| 100 | + the tree and harder to diagnose. |
| 101 | + |
| 102 | +3. Take one lock on the learner's competency root-group status row, then recalculate the whole |
| 103 | + subtree beneath it. |
| 104 | + |
| 105 | + - Pros: |
| 106 | + - Easy to reason about: one lock, always the same row, so no deadlock and no ordering |
| 107 | + argument. |
| 108 | + - Cons: |
| 109 | + - It puts a lock, and its timeout handling, on every grade change rather than only on the |
| 110 | + rare path that lowers a value. |
| 111 | + - It needs row locks, which SQLite does not support. |
| 112 | + |
| 113 | + This is the right shape for the paths that lower a value, and Decision 6 uses it there. |
| 114 | + |
| 115 | +4. Use a coarser lock, either one per deployment or one per learner. |
| 116 | + |
| 117 | + - Pros: |
| 118 | + - A single lock replaces the ordering argument in Decision 3. |
| 119 | + - Cons: |
| 120 | + - A deployment-wide lock serializes every learner behind every other, giving up the |
| 121 | + throughput bursty grading needs. |
| 122 | + - The "one per learner" option makes a learner's unrelated competencies wait for each |
| 123 | + other, since one lock would then cover every tree they have. |
| 124 | + - Either kind adds machinery for acquiring and releasing locks, and for recovering from a |
| 125 | + dead lock holder, across a very large key space. |
| 126 | + |
| 127 | +5. Recalculate the derived levels on every read instead of storing them. |
| 128 | + |
| 129 | + - Pros: |
| 130 | + - No roll-up writes at all, so there is nothing to keep consistent. |
| 131 | + - Cons: |
| 132 | + - It moves a full bottom-up tree evaluation onto every read, the opposite of what dashboards |
| 133 | + need. |
| 134 | + - Already settled against in :ref:`openedx-learning-adr-0002`. Unresolved item 1 is the |
| 135 | + narrower version still open. |
| 136 | + |
| 137 | +6. Send an event to openedx-core and do all the work in a separate celery task. |
| 138 | + |
| 139 | + - Pros: |
| 140 | + - Recording a grade would not depend on the competency code being installed or fast. |
| 141 | + - Cons: |
| 142 | + - openedx-core is a library and cannot own a celery queue, so every caller would supply one. |
| 143 | + - The leaf would no longer commit with the grade, giving up the one guarantee Decision 1 is |
| 144 | + cheap enough to keep. |
| 145 | + |
| 146 | +7. Detect conflicts optimistically, with a version column and a retry loop for the losing write. |
| 147 | + |
| 148 | + - Pros: |
| 149 | + - Contention costs a retry rather than a wait. |
| 150 | + - Cons: |
| 151 | + - Decision 4 is already optimistic, without the retry loop. A write that loses has computed |
| 152 | + a value that is too low, and discarding those is exactly what Decision 4 does. |
| 153 | + |
| 154 | +8. Read-after-write: after writing the leaf, re-read each parent's children before rolling up, to catch a race |
| 155 | + with another writer already in flight. |
| 156 | + |
| 157 | + - Pros: |
| 158 | + - Recovers from the race within the same request, without a separate marker or job. |
| 159 | + - Cons: |
| 160 | + - Decision 1 shares a transaction only between the grade and its leaf, and Decision 3 commits each rollup level |
| 161 | + separately before reading the next. That leaves no single transaction boundary for a read-after-write check to |
| 162 | + run inside: by the time a re-read would happen, the level below has already committed and could change again |
| 163 | + before the write completes. |
| 164 | + - It also only checks for a race at the moment each parent is read. If the worker crashes mid-cascade before reaching the next read, |
| 165 | + nothing notices the rollup was left unfinished. Decision 5's manual recovery mechanism exists to catch that case. |
0 commit comments