Repository navigation
fix(redis): add result_chord_expires for chord keys - #10827
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10827 +/- ##
=======================================
Coverage 90.40% 90.41%
=======================================
Files 153 153
Lines 21079 21093 +14
Branches 2544 2547 +3
=======================================
+ Hits 19057 19071 +14
Misses 1710 1710
Partials 312 312
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
MehrazRumman
left a comment
There was a problem hiding this comment.
I checked this out and ran it against main. Since there is no Redis server here, I used a real in-process worker with the Redis result backend backed by fakeredis, which honours TTLs. The chord had three 1.5 s header tasks and the worker was solo.
The fix works
| Setting | main | this PR |
|---|---|---|
result_expires=1 only |
body never runs | body never runs (unchanged) |
result_expires=1, result_chord_expires=60 |
body never runs (option unknown) | body runs |
result_expires=1, result_chord_expires=0 |
body never runs | body runs |
The default stays the same, so long chords still need the new setting. The docs for chords and result_expires should point to it, since that is where people hitting #4207 will look. The new unit tests fail on main and pass here, and the rest of test_redis.py and test_base.py passes.
Problem: a GroupResult the user saved can get a shorter TTL
on_chord_part_return() now also runs EXPIRE on the group key (celery-taskset-meta-<gid>) with chord_expires. That key is not only the saved header of a chord with nested groups: it is also where GroupResult.save() stores a header the user saves to restore later, which is a common pattern (res = chord(...).apply_async(); res.parent.save()).
With result_expires=3600 and result_chord_expires=60:
| main | this PR | |
|---|---|---|
| TTL of the saved GroupResult after the chord finishes | about 3600 s | 59 s |
So GroupResult.restore() starts returning None after a minute instead of an hour. Never shortening that key below result_expires avoids it:
expires = self.chord_expires
if expires:
pipeline = pipeline \
.expire(jkey, expires) \
.expire(tkey, expires) \
.expire(skey, expires)
if self.expires:
# The group key may also be a GroupResult the user saved
# with result_expires: never shorten it below that.
pipeline = pipeline.expire(
self.get_key_for_group(gid), max(expires, self.expires))With this, the saved GroupResult keeps about 3600 s, a nested header still gets result_chord_expires when that is longer, and the slow chord above still completes. test_on_chord_part_return_uses_chord_expires would need the group key's expected TTL updated.
Smaller points
_set_chord_key()callsclient.set()directly instead ofRedisBackend.set(). That skips the_MAX_STR_VALUE_SIZEcheck, so a nested header too large for Redis no longer raisesBackendStoreError('value too large for Redis backend'). It also skips thepublish(), which is harmless because nothing subscribes to group keys. Keeping the size check in_set_chord_key()would keep the old error.prepare_expires(..., type=int)truncates, soresult_chord_expires=0.5becomes0, which means "never expires". Rounding up, or rejecting values between 0 and 1, would avoid that surprise.- With
result_chord_expires=0, the.j,.tand.skeys have no TTL until the chord completes. If a chord is abandoned (for example a lost header message), they stay forever. The docs say "never expires while the chord is open"; adding "including chords that never complete" would make the trade-off clear. - #10830 (cache backend) contains this commit as its first commit. Merging this one first and rebasing #10830 onto it would keep the reviews separate.
|
The group key refresh could shorten a On the smaller points:
The changes are in ed7c68a |
|
@jheld do you have any objection merging this pr? |
Description
Fixes #4207.
On the Redis backend, a chord's size, results and counter keys expire after
result_expires, measured from the chord's start or from the last header task to finish. If a header task takes longer than that, the size key is gone, so the chord's body never runs and nothing is logged.This adds
result_chord_expiresfor those keys and for the header that a chord with nested groups saves. Unset, it falls back toresult_expires, as today.0means the keys don't expire while the chord is open.The results of the header tasks themselves still use
result_expires. The saved header is now written like the other chord keys, so withresult_backend_always_retryit no longer goes throughGroupResult.save()'s retry loop.Tests cover each write and the fallback. An integration test checks the TTLs against Redis and that the saved header still restores.