Repository navigation
mongodb backend: keep the first SUCCESS instead of overwriting it - #10944
Conversation
Signed-off-by: Aniket <[email protected]>
|
I tested this against MongoDB 7.0 with It fixes the case from #10929. With the crash script from the issue, main prints The read and the write can still race.
I ran two writer processes in parallel, 200 rounds per test. Each number is how many rounds went wrong:
Suggested fix: put the status check in the MongoDB checks the filter and writes the document in one step. If the stored document already has SUCCESS, the filter doesn't match, and the upsert raises query = {'_id': task_id, 'status': {'$ne': states.SUCCESS}}
for _ in range(2):
try:
self.collection.replace_one(query, meta, upsert=True)
break
except DuplicateKeyError:
# The document has a SUCCESS, or another run just inserted it.
pass
except InvalidDocument as exc:
raise EncodeError(exc) from exc
The new unit test checks that |
Put the status check in the replace_one filter so the check and the write happen in one step, and ignore DuplicateKeyError from an upsert against an existing SUCCESS, with one retry for a document inserted by a concurrent run. Signed-off-by: Aniket <[email protected]>
|
Thanks for testing this against a real MongoDB and for the detailed race numbers. I have pushed a commit that puts the status check in the replace_one filter and ignores DuplicateKeyError, with one retry for a document inserted by a concurrent run, and updated the tests to check the filter and the retry. |
|
Re-tested ed9a3cb against MongoDB 7. The race is gone:
I think it's ready once the PR text is updated. It still says A line under "Unreleased" in Retry and mutation checksWith
|
Signed-off-by: Aniket <[email protected]>
|
Thanks for re-testing against a real MongoDB and for the mutation checks. I have updated the PR text to describe the filter based write, and added a Changelog entry under Unreleased noting that writes after a SUCCESS are ignored, including abort on a finished task and reusing a task id until forget() clears it. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10944 +/- ##
=======================================
Coverage 91.04% 91.04%
=======================================
Files 153 153
Lines 21586 21591 +5
Branches 2661 2662 +1
=======================================
+ Hits 19653 19658 +5
Misses 1624 1624
Partials 309 309
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
Refs #10929
The MongoDB backend stored every result with an unconditional
replace_one, so when a task ran a second time (redelivery after the worker was killed past the visibility timeout, as in the report) the second run overwrote the first run's SUCCESS, and later updates could replace it too.BaseKeyValueStoreBackendalready ignores any write once a SUCCESS is stored, and #10920 applies the same rule to the database backend. This does the same for MongoDB:_store_resultputs the status check in thereplace_onefilter (statusis not SUCCESS), so the check and the write happen in one step. When the stored document is already a SUCCESS the filter does not match and the upsert raisesDuplicateKeyError, which is ignored, with one retry for a document inserted by a concurrent run.Cassandra has the same bug, but this PR does not cover it: its write path goes through the session/table-creation lifecycle in
_get_connection, and a pre-read there needs more care than a straight port of this change.Tests:
test_store_result_after_success_is_ignored,test_store_result_retries_duplicate_keyandtest_store_result_keeps_first_successcover the filter, the retry and keeping the first SUCCESS. They fail on the old read-then-write logic and pass with this change. The mongodb unit test file shows no new failures against the unmodified tree. AChangelog.rstentry under Unreleased notes that writes after a SUCCESS are now ignored.