Repository navigation
Database Backend: Use configured serializer, instead of always pickling. #5907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
martincapello
wants to merge
12
commits into
celery:main
from
martincapello:3025-db-result-serialization
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b9b4ca3
Modify db backend result column to BLOB type
m4ddav3 8df52d3
Encode backend db result, and ensure_bytes as it is a BLOB in the db
m4ddav3 76d64c4
Remove PickleType import
m4ddav3 674a477
Decode the value retrieved from the DB
m4ddav3 49c9cf5
Encode and decode result when using groups
m4ddav3 b7d2087
Added .vscode/
martincapello 76797ae
Changed sa.BLOB by sa.LargeBinary
martincapello b3eed09
Fixed according to original PR. I broke this when tried to refactor t…
martincapello 06812fd
Fixed E226 missing whitespace around arithmetic operator
martincapello ef2b9b4
Cloned DatabaseBackend tests to test the same cases but using json re…
martincapello a25179b
Refactored to avoid having duplicated tests.
martincapello 4b0a1e4
Fixed W293 blank line contains whitespace
martincapello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,3 +31,4 @@ htmlcov/ | |
| coverage.xml | ||
| test.db | ||
| pip-wheel-metadata/ | ||
| .vscode/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,6 @@ | |
| from datetime import datetime | ||
|
|
||
| import sqlalchemy as sa | ||
| from sqlalchemy.types import PickleType | ||
|
|
||
| from celery import states | ||
| from celery.five import python_2_unicode_compatible | ||
|
|
@@ -26,7 +25,7 @@ class Task(ResultModelBase): | |
| primary_key=True, autoincrement=True) | ||
| task_id = sa.Column(sa.String(155), unique=True) | ||
| status = sa.Column(sa.String(50), default=states.PENDING) | ||
| result = sa.Column(PickleType, nullable=True) | ||
| result = sa.Column(sa.LargeBinary(length=(2 ** 31) - 1), nullable=True) | ||
| date_done = sa.Column(sa.DateTime, default=datetime.utcnow, | ||
| onupdate=datetime.utcnow, nullable=True) | ||
| traceback = sa.Column(sa.Text, nullable=True) | ||
|
|
@@ -83,7 +82,7 @@ class TaskSet(ResultModelBase): | |
| id = sa.Column(sa.Integer, sa.Sequence('taskset_id_sequence'), | ||
| autoincrement=True, primary_key=True) | ||
| taskset_id = sa.Column(sa.String(155), unique=True) | ||
| result = sa.Column(PickleType, nullable=True) | ||
| result = sa.Column(sa.LargeBinary(length=(2 ** 31) - 1), nullable=True) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a breaking change? |
||
| date_done = sa.Column(sa.DateTime, default=datetime.utcnow, | ||
| nullable=True) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this a breaking change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure...I believe this could break celery apps using a serializer other than pickle when the results backend is sqlalchemy based.
According to the sqlalchemy docs:
So, the database column should stay binary (I've been able to confirm this using postgres), the issue with PickleType is that Sqlalchemy pickles the objects automatically, despite having a json (or any but pickle) serializer configured for our app.
Hence, it seems that results from old task runs (tasks ran before this change) will have a pickled results instead of a json serialized one.
Can a celery client access to tasks results that were already ran in the past? I'm asking because am a celery noob.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is possible if someone has access to the result (task) ID.
This is indeed a breaking change, for configurations that used a different serializer than the pickle serializer. Perhaps this can be solved by allowing a transitional fallback to
pickle.loadson a deserialization error, but only with a configuration option. So the new serializer support will be disabled by default, and we emit a warning for the deprecation in the next version.How does that sound?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright then, let's suppose that this issue never existed and the serialization was working fine for any serializer. Also suppose someone started using celery configured with the json serializer, and after some time he changes his mind and decides to use pickle serializer. In this case, if someone has access to a result (task) ID corresponding to a task ran before the serializer change, would in this case the same deserialization error be expected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sqla should have support jsonfield as well, need to check that