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

Use create_or_find_by to reduce activity status deadlocks #4547

Merged
merged 3 commits into from
Apr 6, 2023

Conversation

jorg-vr
Copy link
Contributor

@jorg-vr jorg-vr commented Apr 6, 2023

This pull request replace find_or_create_by with create_or_find_by to reduce the number of deadlocks.

According to the docs:

This is similar to find_or_create_by, but avoids the problem of stale reads between the SELECT and the INSERT, as that method needs to first query the table, then attempt to insert a row if none is found.

I left in the single retry attempt because a deadlock is still possible if an activity_status is deleted:

While we avoid the race condition between SELECT -> INSERT from find_or_create_by, we actually have another race condition between INSERT -> SELECT, which can be triggered if a DELETE between those two statements is run by another client. But for most applications, that's a significantly less likely condition to hit.

To be able to use this method, I had to remove the rails unique validation. We already have a database unique index for this, which throws the expected error. I adjusted one test, to fix it and to make sure this error gets thrown.

I also had to change the method in the one other place where activity status are created to be able to handle these new unique errors.

See #1877 for some history on this issue.

Closes #4479

@jorg-vr jorg-vr added the bug Something isn't working label Apr 6, 2023
@jorg-vr jorg-vr self-assigned this Apr 6, 2023
@jorg-vr jorg-vr marked this pull request as ready for review April 6, 2023 10:08
@jorg-vr jorg-vr requested a review from a team as a code owner April 6, 2023 10:08
@jorg-vr jorg-vr requested review from bmesuere and niknetniko and removed request for a team April 6, 2023 10:08
@jorg-vr jorg-vr merged commit fa930a3 into develop Apr 6, 2023
@jorg-vr jorg-vr deleted the fix/activity_status_for branch April 6, 2023 12:42
@jorg-vr jorg-vr restored the fix/activity_status_for branch May 23, 2023 13:28
@jorg-vr jorg-vr deleted the fix/activity_status_for branch May 23, 2023 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid duplicate key error for activity statuses
3 participants