Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Clean up the way logcontexts and threads work in the pushers #4075

Merged
merged 10 commits into from
Oct 24, 2018

Conversation

richvdh
Copy link
Member

@richvdh richvdh commented Oct 20, 2018

This was all a bit of an inconsistent confusing mess, and led to things being
done with no logcontext and leaky logcontexts.

The starting point is to note that each pusher has its own loop which runs for
as long as it has work to do. This should run in its own background thread with
its own logcontext, so we start by making that so (see _start_processing and
_process in each of HttpPusher and EmailPusher).

Once we've done that, it turns out that the hooks to notify the pushers
(on_started, on_new_notifications, on_new_receipts) may as well be
synchronous because all they have to do is set off the background process (a
slight exception is HttpPusher.on_new_receipts, which needs its own
background process to keep it symetric with the others). That
means that we can remove the run_as_background_process calls from PusherPool.

Sorry about the size of this PR - it's broken down into commits doing roughly the above, preceeded by a bunch of refactoring to make it tractable.

Fixes: #4066

@richvdh richvdh requested review from a team and removed request for a team October 20, 2018 01:17
@richvdh richvdh force-pushed the rav/fix_pusher_logcontexts branch from 212d615 to 1ff43b6 Compare October 22, 2018 15:11
This is public (or at least, called from outside the class), so ought to have a
better name.
... and use it from start_pusher_by_id. This mostly simplifies
start_pusher_by_id.
simplifies the interface to _start_pushers
Each pusher has its own loop which runs for as long as it has work to do. This
should run in its own background thread with its own logcontext, as other
similar loops elsewhere in the system do - which means that CPU usage is
consistently attributed to that loop, rather than to whatever request happened
to start the loop.
`on_new_notifications` and `on_new_receipts` in `HttpPusher` and `EmailPusher`
now always return synchronously, so we can remove the `defer.gatherResults` on
their results, and the `run_as_background_process` wrappers can be removed too
because the PusherPool methods will now complete quickly enough.
This brings it into line with on_new_notifications and on_new_receipts. It
requires a little bit of hoop-jumping in EmailPusher to load the throttle
params before the first loop.
We don't do anything with the result, so this is needed to give this code a
logcontext.
@richvdh richvdh force-pushed the rav/fix_pusher_logcontexts branch from 1ff43b6 to abd9914 Compare October 22, 2018 15:12
@richvdh
Copy link
Member Author

richvdh commented Oct 23, 2018

worth noting that github is showing these commits in the wrong order.

@richvdh richvdh requested a review from a team October 23, 2018 13:27
Copy link
Member

@erikjohnston erikjohnston left a comment

Choose a reason for hiding this comment

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

This looks like a big improvement. I'm not hugely thrilled about the disconnect between checks to is_processing and actually setting is_processing, it looks like it could be quite a large footgun if we're not careful. Could we add a check within the processing loops too? So that yes we normally bail out early, but for paranoia's sake we double check before actually setting it and doing the work?

@@ -192,6 +192,9 @@ def _on_new_receipts(self, min_stream_id, max_stream_id, affected_room_ids):
@defer.inlineCallbacks
def start_pusher_by_id(self, app_id, pushkey, user_id):
"""Look up the details for the given pusher, and start it"""
if not self._start_pushers:
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this a function?

Copy link
Member Author

Choose a reason for hiding this comment

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

fucksticks. who would have a a field and a function with the same name modulo an underscore

@richvdh richvdh requested a review from erikjohnston October 24, 2018 08:25
@richvdh
Copy link
Member Author

richvdh commented Oct 24, 2018

build has failed due to pep8 due to new flake8 version - will investigate separately.

@richvdh richvdh merged commit e0b9d5f into develop Oct 24, 2018
@richvdh richvdh deleted the rav/fix_pusher_logcontexts branch October 29, 2018 12:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants