Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Check Last ID when updating site config to prevent race conditions#42691

Merged
pjlast merged 6 commits intomainfrom
pjlast/35428-check-last-id-site-config-update
Oct 7, 2022
Merged

Check Last ID when updating site config to prevent race conditions#42691
pjlast merged 6 commits intomainfrom
pjlast/35428-check-last-id-site-config-update

Conversation

@pjlast
Copy link
Contributor

@pjlast pjlast commented Oct 7, 2022

Solves #35428

Adds a check when making Site Config updates to ensure that race conditions don't occur. The Last ID, which is supplied alongside a site config update, is used to determine if a site config update happened before this update was attempted.

See Loom for demonstration in UI: https://www.loom.com/share/f315821af6af4c11ba333f294a81d57f

Test plan

Unit tests and manual testing

@pjlast pjlast requested a review from a team October 7, 2022 11:03
@cla-bot cla-bot bot added the cla-signed label Oct 7, 2022
Comment on lines +33 to +36
confStore.SiteGetLatestFunc.SetDefaultReturn(
&database.SiteConfig{ID: 1},
nil,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
confStore.SiteGetLatestFunc.SetDefaultReturn(
&database.SiteConfig{ID: 1},
nil,
)
confStore.SiteGetLatestFunc.SetDefaultReturn(&database.SiteConfig{ID: 1}, nil)

Comment on lines +42 to +48
err := confSource.Write(context.Background(), conftypes.RawUnified{}, 0)
assert.Error(t, err)
})

t.Run("no error when correct last ID", func(t *testing.T) {
err := confSource.Write(context.Background(), conftypes.RawUnified{}, 1)
assert.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick (!!!): instead of using literal values like 0 and 1 here I'd reference the correct value and/or give names to the wrong values, just so it's clear what the test is doing. Granted (that's why I wrote "Nitpick") in this case it is pretty clear, but still:

conf := &database.SiteConfig{ID: 1}
// [...]
err := confSource.Write(context.Background(), conftypes.RawUnified{}, conf.ID+9999)
// [...]
err := confSource.Write(context.Background(), conftypes.RawUnified{}, conf.ID)

that's what I would write

@pjlast pjlast enabled auto-merge (squash) October 7, 2022 11:59
@pjlast pjlast linked an issue Oct 7, 2022 that may be closed by this pull request
@pjlast pjlast merged commit 3a58276 into main Oct 7, 2022
@pjlast pjlast deleted the pjlast/35428-check-last-id-site-config-update branch October 7, 2022 12:12
pjlast added a commit that referenced this pull request Oct 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

updateSiteConfig mutation does not check lastID

3 participants