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

fix: webhook race condition on hook creation #1175

Merged
merged 2 commits into from
Sep 6, 2024

Conversation

jbrockopp
Copy link
Contributor

@jbrockopp jbrockopp commented Sep 4, 2024

xref: go-vela/community#997

Currently, there is an inconvenient bug that our users experience when Vela processes a webhook:

{"error":"unable to create webhook OrgName/RepoName/BuildNumber: ERROR: duplicate key value violates unique constraint \"hooks_repo_id_number_key\" (SQLSTATE 23505)"}

This errors happens when we attempt to create a hook in the database that contains the same number and repo_id.

This attempts to resolve that issue by creating a retry loop with backoff that will repeat the following operations:

  • capture the last hook for the repo
  • set fields on the new hook object to be created in the database
  • create the webhook object in the database

Example

For the sake of discussion, I find examples helpful so let's say we have a repo where the last hook number is 10.

Using the example provided in the ticket, let's say we have two push events fire after a PR is merged:

  • a push to the target branch for the PR
  • a push for the deletion event of the source branch from the PR

In this scenario, the Vela server would process each event concurrently and attempt to insert a 11 hook record but one of these webhooks will fail to be processed due to the conflict as displayed above. This resolves that issue because in that scenario, we'd fall back to looking up the last hook for the repo (11) and updating the number to 12.

@jbrockopp jbrockopp self-assigned this Sep 4, 2024
@jbrockopp jbrockopp requested a review from a team as a code owner September 4, 2024 18:55
Copy link

codecov bot commented Sep 4, 2024

Codecov Report

Attention: Patch coverage is 0% with 30 lines in your changes missing coverage. Please review.

Project coverage is 52.81%. Comparing base (b998ec1) to head (b3d6ea7).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
api/webhook/post.go 0.00% 30 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1175      +/-   ##
==========================================
- Coverage   52.84%   52.81%   -0.04%     
==========================================
  Files         551      551              
  Lines       19199    19211      +12     
==========================================
  Hits        10146    10146              
- Misses       8489     8501      +12     
  Partials      564      564              
Files with missing lines Coverage Δ
api/webhook/post.go 0.00% <0.00%> (ø)

Copy link
Contributor

@plyr4 plyr4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sweet!

@jbrockopp jbrockopp added the bug Indicates a bug label Sep 5, 2024
@ecrupper ecrupper merged commit 89b0a4a into main Sep 6, 2024
11 of 13 checks passed
@ecrupper ecrupper deleted the fix/webhook/race_condition branch September 6, 2024 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants