fix: handle potential DB conflict due to concurrent upload requests in postFile
#19005
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.
This issue manifests when users have multiple templates which rely on the same files, for example see: #17442
In the
files
table we have a constraint to enforce that there can only be one entry perhash, created_by
combo. When runningterraform apply
to update templates, this can mean that the file upload can fail for some of the templates as they hit a race condition all trying to insert at the same time.The fix here is to detect presence of the conflict error and run another
GetFileByHashAndCreator
. This should happen infrequently enough to not cause significant extra load on the DB. If in the future we notice that it does, we could change the underlying SQL for file insertion to run a CAS like call via theON CONFLICT
syntax.Note that the test added here will fail without the change from the first commit. I also tested this manually via
deploy.sh
in my own workspace.Apply diff example without code changes
Error from apply
Running the apply again succeeds
Apply diff example with code changes
apply works on the first try
The template files are as follows:
Top level main.tf
template/main.tf
template/README.md
To test, simply modify the template files (adding a comment in each is all you need, plus updating the version #) and then
terraform apply
with the--parallelism
flag.