Avoid iteration for stopping race condition#516
Merged
etiennebarrie merged 2 commits intomainfrom Nov 3, 2021
Merged
Conversation
nvasilevski
approved these changes
Nov 2, 2021
adrianna-chang-shopify
approved these changes
Nov 2, 2021
| assert_no_enqueued_jobs | ||
| end | ||
|
|
||
| test ".perform_now skips iterations when Run is cancelled even when a race condition happens" do |
Contributor
There was a problem hiding this comment.
Test name is a little unclear, maybe:
Suggested change
| test ".perform_now skips iterations when Run is cancelled even when a race condition happens" do | |
| test ".perform_now avoids iterating and cancels Run when race occurs between starting and cancelling a Run" do |
?
Member
Author
There was a problem hiding this comment.
Right, I used that with symmetry with the previous test. Updated both.
Comment on lines
+68
to
+70
| run = @run | ||
| CustomTaskJob.race_condition_hook = -> do | ||
| Run.find(run.id).update(status: :cancelling) |
Contributor
There was a problem hiding this comment.
Do we need to assign run = @run? Can't we just use
CustomTaskJob.race_condition_hook = -> do
Run.find(@run.id).update(status: :cancelling)
end?
Member
Author
There was a problem hiding this comment.
Good catch, I needed that before because I used before_perform directly.
e2309e7 to
a71a3db
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #515
First, we can entirely avoid the "optimistic update" if the run is already stopping, like we were doing before in the non-race-condition case.
Also, since we know whether or not the record was updated, we know it was indeed stopping (but not its actual status), so we can prevent even a single iteration from happening (but we have to reload to get the actual status to either moved to paused or cancelled).
I tested the first point by re-using
count_uncached_queries.For the second point, I tested it by simulating a race condition for the model, but I also added a test at the job level for good measure. It also causes the race condition by running in a
before_performcallback. This runs after Active Job deserializing the Run, but since it's prepended, beforeTaskJobConcern#before_perform, when we do the optimistic update.