Move create entry "similarity" check inside of DataStore #2475
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.
The entry similarity check happens only on the API level, but not within the same transaction that the entry creation takes place in. This causes a small window where two concurrent requests for similar entries can result in both being created.
This PR moves the similarity check down to the SQL DataStore layer inside of the transaction that creates the entry. It also introduces a new DataStore function CreateOrReturnRegistrationEntry that returns the existing entry, that is useful for the API, but otherwise allows us to not have to update the existing CreateRegistrationEntry callers at this time. Both CreateRegistrationEntry and CreateOrReturnRegistrationEntry go through the same code paths and differ only in how they treat the case when a similar entry exists. CreateRegistrationEntry fails with AlreadyExists, while CreateOrReturnRegistrationEntry returns the existing entry along with a bool indicating that it is an existing entry.
This PR does NOT address the issue that UpdateRegistrationEntry can end up creating two "similar" entries.