Repository navigation
Database Backend: Use configured serializer, instead of always pickling. - #5907
martincapello wants to merge 12 commits into
Conversation
Signed-off-by: martincapello <[email protected]>
georgepsarakis
left a comment
There was a problem hiding this comment.
Nice work! Some tests are breaking, strings seem to be converted to unicode now as far as I can tell. Could also please add a test for this backend, which uses the JSON serializer?
| 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.BLOB, nullable=True) |
There was a problem hiding this comment.
Can we use LargeBinary with custom length instead so that the largest possible datatype is used (at least for MySQL):
- https://stackoverflow.com/a/43791881/920374
- https://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html#data-types-storage-reqs-strings
I think PostgreSQL BYTEA columns have a limit of 1GB, so that RDBMS will be covered too.
Signed-off-by: martincapello <[email protected]>
…he first time. Signed-off-by: martincapello <[email protected]>
|
Will try to add a test as soon as I can. |
Signed-off-by: martincapello <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5907 +/- ##
==========================================
+ Coverage 83.19% 83.45% +0.26%
==========================================
Files 145 145
Lines 17126 16819 -307
Branches 2110 2100 -10
==========================================
- Hits 14248 14037 -211
+ Misses 2663 2566 -97
- Partials 215 216 +1 ☔ View full report in Codecov by Harness. |
…sult serializer instead of pickle. Signed-off-by: martincapello <[email protected]>
|
@georgepsarakis I ended up creating a clone of the database backend tests with a different setup (the result serializer is set to json in this case). Does this makes sense? Or do you prefer a fixtures based approach modifying the current class? |
|
I have an improvement for this...will push a commit on monday |
|
@martincapello it would be best if all files for this backend are contained within a single file. It would make it easier for future contributions to write tests that will cover all serializers. Perhaps parametrize helps here. |
|
Oh yeah, I didn't mean to add a new file, just a new class into the same file. I tried to use parametrize, but ran into some issues that I will explain on monday. |
Signed-off-by: martincapello <[email protected]>
|
This is the issue I ran into: @pytest.mark.parametrize(
'result_serializer', ['pickle', 'json'],
ids=['using pickle', 'using json']
)For instance, for the following test (which I believe it should be renamed as "test_is_serialized", but we can discuss this later, I'll keep the name as is for now): @pytest.mark.parametrize(
'result_serializer', ['pickle', 'json'],
ids=['using pickle', 'using json']
)
def test_is_pickled(self, result_serializer):
self.app.conf.result_serializer = result_serializer
tb = DatabaseBackend(self.uri, app=self.app)
tid2 = uuid()
result = {'foo': 'baz', 'bar': SomeClass(12345)}
tb.mark_as_done(tid2, result)
# is serialized properly.
rindb = tb.get_result(tid2)
assert rindb.get('foo') == 'baz'
assert rindb.get('bar').data == 12345When I run the above test, it fails for the json result serializer, because rindb.get('bar') doesn't have a member named "data", this is due to rindb.get('bar') returning a dict (which is what the json decoder returns as far as I understood). @pytest.mark.parametrize(
'result_serializer', ['pickle', 'json'],
ids=['using pickle', 'using json']
)
def test_is_pickled(self, result_serializer):
self.app.conf.result_serializer = result_serializer
tb = DatabaseBackend(self.uri, app=self.app)
tid2 = uuid()
result = {'foo': 'baz', 'bar': SomeClass(12345)}
tb.mark_as_done(tid2, result)
# is serialized properly.
rindb = tb.get_result(tid2)
assert rindb.get('foo') == 'baz'
if result_serializer == 'json': # <--- custom logic to workaround the issue
assert rindb.get('bar').get('data') == 12345
else:
assert rindb.get('bar').data == 12345As you can see, I needed to add some logic to determine the right assertion to do, which -IMO- goes against the spirit of using parametrize.
If I wasn't clear, it might be easier to take a look at PR's code 🙂 |
Signed-off-by: martincapello <[email protected]>
|
@georgepsarakis any thoughts on my latest updates? |
| 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) |
There was a problem hiding this comment.
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:
PickleType builds upon the Binary type to apply Python’s pickle.dumps() to incoming objects, and pickle.loads() on the way out, allowing any pickleable Python object to be stored as a serialized binary field.
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.
Can a celery client access to tasks results that were already ran in the past?
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.loads on 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.
Yes, this is possible if someone has access to the result (task) ID.
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.
sqla should have support jsonfield as well, need to check that
| 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) |
I am not really sure how you end up getting this result when using the JSON serializer, since assert rindb.get('bar').get('data') == 12345I think the most easy way to test, would be to make the expected result parametric too, as seen in other test cases. |
Sorry, but I'm not following you here, because the expected result is the same (12345 in this case) for both serializers. Instead, the thing that changes is the structure of the |
|
Any news on this? |
Rebases the changes defined on #2881 (since they are almost 4 years old) and refactors them a bit.
Original PR's comment: