Minor locking tweaks to reduce contention #647
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.
A couple of places where a Publish was locked FOR UPDATE can safely use shared locks rather than exclusive locks:
when adding publish items, we want to lock the Publish to ensure it remains in PENDING state during our update. But we won't actually change the state of the Publish, so we only need a read/shared lock.
similarly during commit, we want to lock the Publish to ensure it's in PENDING state. In phase2 commit, we actually then update the state of the Publish, so we need an exclusive lock. In phase1 commit we don't modify the Publish and it's safe to use a read/shared lock.
When using the postgresql dialect, "read=True" here maps to "FOR SHARE" which is documented at [1].
This change is meant to improve performance by eliminating some scenarios where a request would have had to wait to obtain an exclusive lock on the publish; mainly during Pulp publish of many repos, where many update and phase1 commit requests will be happening concurrently for the same publish.
[1] https://www.postgresql.org/docs/current/explicit-locking.html#LOCKING-ROWS