Skip to content

Fix race condition getting optimizers stuck, leaving collection in yellow state - #5111

Merged
timvisee merged 6 commits into
devfrom
fix-stuck-optimizers
Sep 23, 2024
Merged

timvisee merged 6 commits into
devfrom
fix-stuck-optimizers

Conversation

@timvisee

@timvisee timvisee commented Sep 19, 2024 •

Copy link
Copy Markdown
Member

This PR fixes a race condition which would allow the optimizers for a collection to get stuck. The result is a collection that remains in yellow state forever, unless a new update operation is send to trigger the remaining optimizations.

The PR has two changes:

  1. patch race condition (with an extra branch)
  2. remove a level of nesting in the match statement. The bulk of the logic now happens after the match rather than inside it, other branches use continue/break.

The data race is caused by an ordering problem between three things:

  1. condition that checks max allowed handles (max 1 in this case)
  2. clean up of handles that are finished
  3. signal at the end of optimization to trigger new optimizations

Basically, we trigger the optimizers again before cleaning the finished handle up (through a callback). If checking the maximum allowed handles on a new run, we'd still be at the maximum, cancelling the new optimization. There are no other ongoing optimizations to trigger the optimizer loop once again, getting it stuck. In other words, the problem is that we retrigger optimizations before cleaning up the handles. Sadly that's not an easy thing to fix.

This PR patches the problem by triggering optimizations even if we clean up any finished handles. This means we pass the handle limit condition, which prevents it getting stuck. We have tested this patch on our benchmark server. It has shown to solve the problem.

I'm not super satisfied with the way the optimization loops works right now. I suggest to merge this PR, and attempt to refactor it separately to make this data race impossible.

Details

We did not catch this before because we have no optimization handle limit by default. In case of our benchmark server we have a limit of 1 set however.

Detailed scenario, in order:

  1. Thread 1: one optimization is running
  2. Thread 1: optimization is at the end, sends optimizer signal
  3. Thread 2: optimization worker triggered, received signal
  4. Thread 2: optimization worker fails condition, already at max of 1 handle
  5. Thread 1: optimization is finished
  6. some time passes
  7. Thread 2: optimization worker triggered by clean interval
  8. Thread 2: optimization worker cleans finished handle
  9. now we're stuck, we won't receive new signals

This PR patches point 8.

All Submissions:

  • Contributions should target the dev branch. Did you create your branch from dev?
  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

New Feature Submissions:

  1. Does your submission pass tests?
  2. Have you formatted your code locally using cargo +nightly fmt --all command prior to submission?
  3. Have you checked your code using cargo clippy --all --all-features command?

@timvisee timvisee changed the title Attempt to fix data race in optimizers getting them stuck Fix race condition getting optimizers stuck, collection remains in yellow state Sep 20, 2024
@timvisee timvisee changed the title Fix race condition getting optimizers stuck, collection remains in yellow state Fix race condition getting optimizers stuck, leaving collection in yellow state Sep 20, 2024
@timvisee
timvisee marked this pull request as ready for review September 20, 2024 16:53
@timvisee
timvisee requested a review from agourlay September 21, 2024 08:37

@tellet-q tellet-q 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.

lgtm, we ran a test to check if the log line appears and it did

.into_iter()
.rev()
.map(|i| handles.remove(i))
.map(|i| handles.swap_remove(i))

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.

Using swap_removes means losing ordering of handles.
Just want to confirm that it is not something we care about :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It doesn't matter, but thanks for confirming!

We only keep the handles to count the tasks that are still ongoing, and to catch any panics. Tasks themselves run asynchronously in their own thread. We don't care in what order or at what time we clean them up.

// Skip if we reached limit, an ongoing optimization that finishes will trigger this loop again
let limit = max_handles.saturating_sub(optimization_handles.lock().await.len());
if limit == 0 {
log::trace!("Skipping optimization check, we reached optimization thread limit");

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.

I never use trace because it is way too verbose.
Would it make sense to bump this one to debug?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It can happen a lot and cause a lot of log spam in debug. A lot as in, with every update operation you send. That's why we demoted it a long time ago to trace.

It actually was useful in our benchmarking setup a few days ago where we changed logging to trace just to confirm some branches were hit.

Do you think setting it to debug would be worth the log spam?

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.

Let's keep it to trace if it is noisy 👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Less noisy than I remembered. Still noisy enough though.

Screencast.from.2024-09-23.10-40-58.mp4

@agourlay agourlay 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.

Thank you very much for the care put in the explanation and thought process.

This LGTM but not gonna lie the state machine is getting difficult to grasp.

@timvisee
timvisee merged commit 07d5efb into dev Sep 23, 2024
@timvisee
timvisee deleted the fix-stuck-optimizers branch September 23, 2024 08:42
timvisee added a commit that referenced this pull request Sep 23, 2024
…llow state (#5111)

* Return whether any task was cleaned

* Retrigger optimizers after cleanup timeout if we cleared a handle

* Flatten match used as decision logic on whether to run optimizers

* Add comment to elaborate we have a branch to prevent race condition

* Rename force to ignore_max_handles

* Use swap remove as it is a bit more efficient
@timvisee timvisee mentioned this pull request Sep 23, 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.

4 participants