Skip to content

Redis Result Consumer: unsubscribe on message success - #4666

Merged
thedrow merged 2 commits into
celery:masterfrom
georgepsarakis:bug/3812_result_consumer_unsubscribe
Apr 28, 2018
Merged

thedrow merged 2 commits into
celery:masterfrom
georgepsarakis:bug/3812_result_consumer_unsubscribe

Conversation

@georgepsarakis

@georgepsarakis georgepsarakis commented Apr 18, 2018 •

Copy link
Copy Markdown
Contributor
  • Reset connection pool and PubSub internal state after consumer fork.
  • Unsubscribe from channel when task state changes to ready state.

Addresses issues discussed in #3812 and #3808 .

Comment thread celery/backends/redis.py
)
return self._pubsub

def on_after_fork(self):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this method should be called to possibly avoid re-use of the same object references (and thus perpetuate the leaks), but I am not entirely sure. Perhaps I can make it a separate pull request.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the re-instantiation of Redis client objects however also addresses this comment: #3808 (comment)

Comment thread celery/backends/redis.py Outdated
self._pubsub = None
super(ResultConsumer, self).on_after_fork()

def on_state_change(self, meta, message):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual fix is in this method.

@auvipy auvipy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kindly fix the lint errors to trigger travis test matrix

/home/travis/build/celery/celery/celery/backends/redis.py:8:1: F401 'kombu.utils.objects.cached_property' imported but unused
/home/travis/build/celery/celery/celery/backends/redis.py:81:9: F841 local variable '_' is assigned to but never used

@georgepsarakis

Copy link
Copy Markdown
Contributor Author

Good observation @auvipy , thanks.

@codecov

codecov Bot commented Apr 18, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #4666 into master will increase coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4666      +/-   ##
==========================================
+ Coverage   82.52%   82.56%   +0.04%     
==========================================
  Files         140      140              
  Lines       15894    15905      +11     
  Branches     1980     1982       +2     
==========================================
+ Hits        13116    13132      +16     
+ Misses       2586     2580       -6     
- Partials      192      193       +1
Impacted Files Coverage Δ
celery/backends/redis.py 90.37% <100%> (+2.65%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update aa636bc...26603a7. Read the comment docs.

@thedrow thedrow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point I'm mostly neatpicking.

This is good work!

Comment thread celery/backends/redis.py Outdated
@property
def client(self):
return self._create_client(**self.connparams)
if self._client is None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this change help to resolve the issue?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thedrow PubSub objects seem to maintain an internal channel registry, which they use to reconnect to all channels, in the event of a disconnection. I wanted to reset both the client connection pool and the pubsub instance, just to make sure that these do not introduce any leaks.

Comment thread celery/backends/redis.py Outdated
self._pubsub = None

@property
def pubsub(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this change resolve the issue?
I'd rather not introduce new API endpoints if they are not needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove it and use PubSub.close() which resets all internal state.

Comment thread celery/backends/redis.py Outdated

def on_after_fork(self):
self.backend._client = None
self._pubsub = None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can still use a cached_property. If you del self._pubsub a new value will be created (as far as I recall).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that seems to be the case, thanks for pointing it out!

Comment thread celery/backends/redis.py

def on_state_change(self, meta, message):
super(ResultConsumer, self).on_state_change(meta, message)
if meta['status'] in states.READY_STATES:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract this logic to a private method. maybe_cancel_ready_task() or something similar.
It will convey the meaning of the code more clearly.

Comment thread t/integration/test_canvas.py Outdated
for _ in range(total_chords)
]

for _ in range(TIMEOUT):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're working on this code, we could use manager.join() instead of the custom logic in this test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

manager.join() seems to block until the result set is completed. I just wanted to test for tasks in STARTED state. Perhaps I am missing something.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. The manager has a wait_for method that could be used to wait for such state.
Also, maybe we should add is_started and assert_started to the manager and use that instead.
Whatever you choose is fine with me but the logic as it is now could be replaced with something generic.

Comment thread t/integration/test_canvas.py Outdated

for _ in range(TIMEOUT):
if async_result.state == 'STARTED':
if all([result.state == 'STARTED' for result in async_results]):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to create a list here.
As a general rule, avoid lists when possible.

all(result.state == 'STARTED' for result in async_results)

will work and finish faster if the test fails.

Comment thread t/integration/test_canvas.py Outdated
channels_before = \
len(redis_client.execute_command('PUBSUB CHANNELS'))
assert async_result.get(timeout=TIMEOUT) == 24
assert channels_before == 2 * total_chords + initial_channels

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment about this assert? It's not clear why that would be the case without diving into the problem this test proves that it no longer exists.

@georgepsarakis

Copy link
Copy Markdown
Contributor Author

@thedrow thank you very much for the review. I agree on all comments.

@georgepsarakis

Copy link
Copy Markdown
Contributor Author

@thedrow I have completed the changes that I could think of, I believe I simplified the code to a large extent and improved the test case to be more explicit, but let me know if you find further issues.

@thedrow thedrow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did the unit tests disappear to?
Please add them back so we won't reduce our coverage.

@georgepsarakis

Copy link
Copy Markdown
Contributor Author

@thedrow I will add unit tests for the new function.

@georgepsarakis

Copy link
Copy Markdown
Contributor Author

@thedrow I have added methods to the Manager mixin and some unit tests for redis.ResultConsumer. What do you think?

@thedrow thedrow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from the minor branch coverage reduction which we can resolve in another PR this LGTM.
Please open an issue about it before we merge.

@georgepsarakis

Copy link
Copy Markdown
Contributor Author

@thedrow thanks. I do not mind opening a new issue, but what about #3808 and #3812 , which already contain references and feedback? Perhaps adding a new issue will scatter the information even more.

@thedrow

thedrow commented Apr 24, 2018

Copy link
Copy Markdown
Contributor

@georgepsarakis The issue is about the technical debt created by merging this PR.
The issues you mentioned will already be solved by this PR.

- Use on_after_fork in consumer to reset PubSub and connection pool
  internal states.
- Improve Canvas integration test.
@georgepsarakis

Copy link
Copy Markdown
Contributor Author

@thedrow I believe I also fixed code coverage for the diff (codecov/patch), which addresses your last comment regarding technical debt created by this PR. If you meant something different, please let me know.

@thedrow thedrow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful! Merging :)
Great job everybody in tracking this down and fixing this bug.

@bartloop

bartloop commented May 4, 2018

Copy link
Copy Markdown
Contributor

I have rerun @mleginus's sample code from issue #3812 and I still get the failure.

My pip3 freeze is:
pip3 freeze | grep celery
celery==4.2.0rc3

The output of his script is:

Redis channels count before chord execution 0
Redis channels count after chord execution 10
Redis channels count before chord execution 10
Redis channels count after chord execution 20
Redis channels count before chord execution 20
Redis channels count after chord execution 30

Several minutes after running the script, running 'redis-cli pubsub channels' still shows the celery-task-meta-xxx elements as still subscribed.

I've tried this on two machines, one Ubuntu 16 and the other mac os X. Redis 4.0.7.

I have confirmed that the tasks are actually succeeding: AsyncResult(task_id.id).status == "SUCCESS".

Therefore, I believe the issue is still that the pubsub client is not receiving the task_succeed message from the work. Today is my first day looking at celery code, so my beliefs should not be trusted.

I have found a workaround for now, which is not very nice: for tasks that have no return value, I call
task_id.backend.result_consumer.cancel_for(task_id.task_id) using the task_id returned from .delay().

Let me know if I can provide any other info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants