Skip to content
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

fix: subscribe to server shutdown signal in insert task #553

Merged
merged 5 commits into from
Oct 17, 2024

Conversation

dav1do
Copy link
Contributor

@dav1do dav1do commented Oct 7, 2024

We previously relied on http server shutdown closing the channel to signal the task to stop, which could take at least one tick interval of the task to trigger. Now, we listen to the daemon shutdown signal so it's faster.

previously relied on http server shutdown closing channel exclusively, now we listen to the shutdown signal internally
@dav1do dav1do requested review from stbrody and a team as code owners October 7, 2024 19:52
@dav1do dav1do requested review from gvelez17 and nathanielc and removed request for a team October 7, 2024 19:52
@dav1do dav1do temporarily deployed to github-tests-2024 October 7, 2024 20:10 — with GitHub Actions Inactive
Copy link
Collaborator

@stbrody stbrody left a comment

Choose a reason for hiding this comment

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

core change looks good, but unit tests are failing to build

loop {
let should_exit = shutdown || event_rx.is_closed();
// make sure the events queue doesn't get too deep when we're under heavy load
Copy link
Collaborator

Choose a reason for hiding this comment

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

nitpick, it confused me to have this code come in the middle of the shutdown logic. Can we put line 412 and the block starting on line 417 next to each other? Ideally as the first thing in the loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

reworked the logic a bit to stop using channel openness at all or share anything between shutdown and normal processing. I added draining the channel before shutting down. lmk what you think.

@dav1do dav1do temporarily deployed to github-tests-2024 October 7, 2024 21:37 — with GitHub Actions Inactive
Moves all shutdown logic to one branch, which now tries to pull
any pending events from the channel before exiting rather than
use channel openness as proxy for shutdown. The select! branches
no longer do any async logic and just set variables for the outer
loop to use, as it avoids any subtle cancel safety issues with &mut self.
@dav1do dav1do requested a review from stbrody October 7, 2024 21:57
moved all processing after select! so the loop is now 1) wait for something to happen (interval, new events, shutdown) and 2) handle shutdown or process events if appropriate 3) repeat loop
@dav1do dav1do temporarily deployed to github-tests-2024 October 7, 2024 22:19 — with GitHub Actions Inactive
Copy link
Collaborator

@stbrody stbrody left a comment

Choose a reason for hiding this comment

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

there's enough rust and tokio things going on here that I feel like it's probably good if @nathanielc takes a look rather than me just approving it.

tracing::info!("Shutting down insert task after processing current batch");
if !event_rx.is_empty() {
let remaining_event_cnt = event_rx.len();
let mut buf = Vec::with_capacity(remaining_event_cnt);
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we not just use the buf that was already allocated on line 412?

Copy link
Contributor Author

@dav1do dav1do Oct 9, 2024

Choose a reason for hiding this comment

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

Yeah, makes sense. We can resize and use it (or just use it since recv_many might resize automatically - I’ll verify). It’s the last allocation before exiting so I didn’t think too closely about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can't use it actually. it gets moved into the select branch even though we don't follow that arm in this case. I added a comment to that effect. expanding the macro might make it easier to see but I didn't bother.

// process events at the interval or when we're under heavy load.
// we do it outside the select! to avoid any cancel safety issues
// even though we should be okay since we're using tokio channels/intervals
if events.len() >= EVENT_INSERT_QUEUE_SIZE || process_early {
Copy link
Collaborator

Choose a reason for hiding this comment

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

what's "early" about the processing when process_early is true? Isn't the "early" processing really when the events.len() is too long, but the processing when that bool is true is actually the regularly scheduled processing due to the interval ticking?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair. Process interval is a better name. I’ll rename it. It was early in my head because we want to get N requests before we do work so if we don’t get enough in time we process them anyway before that (ie early) but it’s not very clear.

@dav1do dav1do requested a review from stbrody October 9, 2024 23:42
@dav1do dav1do temporarily deployed to github-tests-2024 October 9, 2024 23:58 — with GitHub Actions Inactive
@nathanielc nathanielc added this pull request to the merge queue Oct 17, 2024
Merged via the queue into main with commit 1a11353 Oct 17, 2024
5 checks passed
@nathanielc nathanielc deleted the fix/insert-task-stop branch October 17, 2024 16:58
@smrz2001 smrz2001 mentioned this pull request Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants