Repository navigation
fix(backends.database): store task children with database backend (#8336) - #10606
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #10606 +/- ##
==========================================
+ Coverage 89.07% 89.09% +0.01%
==========================================
Files 153 153
Lines 20143 20189 +46
Branches 2372 2380 +8
==========================================
+ Hits 17943 17987 +44
- Misses 1896 1897 +1
- Partials 304 305 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
|
Hi @auvipy, I have added comprehensive unit test coverage in
Patch test coverage for the modified lines is now 100% and all 89 database backend tests pass cleanly. Please take a look! |
|
@MehrazRumman feel free to try and review it. our main concern is backward compatibility here |
|
Hi @auvipy @MehrazRumman, Regarding backward compatibility, this PR specifically avoids the regression that caused #8338 to be reverted in #8475:
|
MehrazRumman
left a comment
There was a problem hiding this comment.
Review
Thanks for picking this up and for the careful write-up of why #8338 was reverted. The serializer handling is correct now: children go through self.encode / self.decode, so result_serializer = json works and there is no forced pickle. I checked out the branch and ran t/unit/backends/test_database.py locally, all 89 tests pass.
Since backward compatibility is the main concern, I focused on that, and I think there is one gap that needs fixing before merge.
Blocking
1. If the automatic migration fails, the backend is broken exactly as in #8475, but now silently.
_migrate_missing_columns wraps everything in except Exception: pass. If ALTER TABLE is refused (a read-only DB role, a DBA-managed schema, a dialect quirk), nothing is logged and the worker starts up looking healthy. The very next store_result or get_task_meta then fails, because the ORM always selects the children column:
OperationalError: (sqlite3.OperationalError) no such column: celery_taskmeta.children
I reproduced this by stubbing the migration to a no-op against a pre-existing table without the column. Suggested changes:
- Log at
warning(orerror) level when the ALTER fails, with the exact statement so the operator can run it by hand. - Document the manual migration in the changelog / database backend docs for users who do not allow DDL from the app.
- Ideally, make the read path tolerant: if the column is known to be missing, defer
childrenon the mapped class or fall back to a select that excludes it, so a failed migration degrades to "children not stored" rather than "backend unusable".
Worth discussing
2. Running DDL on existing production tables at worker start-up. Celery already does create_all, so there is precedent, but altering an existing table is more intrusive. On MySQL before 8.0 ADD COLUMN rebuilds the table and takes a lock, which can be a very long stall on a large celery_taskmeta. Multiple workers starting at once will also race on the ALTER; on most databases the losers just error and get swallowed, but it is worth a comment in the code and a note in the docs. Maintainers may prefer an opt-in setting for the auto-migration.
3. Unquoted identifiers in the ALTER statement. table.fullname and col.name are built with an f-string. They are not user-controlled in the injection sense, but database_table_names / database_table_schemas let users pick names that may need quoting. Using engine.dialect.identifier_preparer.quote(...) would be safer and costs nothing.
Minor
- The
hasattr(task, 'children')guard and theTaskWithoutChildrentest cover a path that cannot happen in practice, since_update_resultis always called with the ORM model. Not harmful, just noise. - The PR description says children are reconstructed as
AsyncResultobjects in this backend; in the code that actually happens in the baseAsyncResult.childrenpath viaresult_from_tuple. Worth correcting the description so it does not mislead future readers. - A changelog entry describing the new column and the manual
ALTER TABLEstatement would help operators who upgrade.
Once point 1 is addressed (at minimum, log the failure and document the manual migration), I think this is in good shape.
…r missing children column (celery#8336)
…esolve conflicts with celery#10606
…lery#8336) (celery#10606) * fix(backends.database): store task children with database backend (celery#8336) * test(backends.database): add edge case coverage for children storage and migration (celery#8336) * fix(backends.database): improve backward compatibility and logging for missing children column (celery#8336) --------- Co-authored-by: Asif Saif Uddin {"Auvi":"অভি"} <[email protected]>
Summary
Fixes #8336
Resolves the issue where
AsyncResult.childrenwas not populated or retained when using the database result backend (celery.backends.database:DatabaseBackend).Background & Context
A previous attempt to store children (#8338) was reverted in #8475 because:
sa.Column(PickleType)directly in the model, forcing Python pickle regardless ofresult_serializer.childrencolumn crashed on queries (OperationalError: no such column: celery_taskmeta.children).Solution
children = sa.Column(sa.LargeBinary, nullable=True)toTaskincelery/backends/database/models.py.self.encode(children)on store andself.decode(meta['children'])on retrieval, respectingresult_serializer(JSON, pickle, etc.)._migrate_missing_columnshelper inSessionManager.prepare_models()that checks existing schema and automatically executesALTER TABLE celery_taskmeta ADD COLUMN children BLOB/BYTEAif the column is missing, gracefully catching existing setups and permission constraints.[self.app.AsyncResult(cid) for cid in children]whenchildrenis fetched.Test Coverage
Added comprehensive unit tests in
t/unit/backends/test_database.py:test_store_result_with_children: Validates storing and retrieving children with both JSON and Pickle serializers.test_async_result_children: End-to-end test verifyingAsyncResult.childrenreturns list ofAsyncResultobjects.test_migrate_missing_columns: Verifies database auto-migration addschildrencolumn to legacy tables without data loss.cc @auvipy