Prevent deadlock when importing database #8648
Merged
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.
What
A deadlock was intermittently happening when importing an archive into a database. This manifested as an error like:
The reason this happened is the due to the following:
SELECT
query that appeared in the above errorattempts
table (and therefore has obtained a lock onattempts
), but before it has began processing thejobs
table (and therefore has not obtained a lock onjobs
), then the JobSubmitter will obtain a lock on thejobs
table, then try to obtain a lock on theattempts
table in order to join it with thejobs
table. This results in a deadlock once the importDatabase logic moves on to the jobs table.How
This PR prevents the deadlock by making the importDatabase logic explicitly obtain a lock on all tables that it is going to import into at the beginning of the transaction, thus making the window between obtaining locks on the different tables minuscule.