Repository navigation
fix: recalculate vote counts in a single atomic update - #1650
Open
ChangkeunJ wants to merge 1 commit into
Open
ChangkeunJ wants to merge 1 commit into
ChangkeunJ wants to merge 1 commit into
Conversation
GetAndSaveVoteResult counted the active vote activities first and then saved the total as an absolute value. Any vote committed between the count and the write was lost when the slower request wrote last, so vote_count drifted away from the real number of votes (apache#1646). Recount the activities inside the UPDATE statement itself so the stored total always reflects the committed state at write time. The up/down counts returned to the caller keep using the existing count helpers.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1646
Proposed Changes
vote_countin a single atomicUPDATEthat recounts the active vote activities in subqueries, instead of counting first and writing the total as an absolute value. A slower request can no longer overwrite a newer total with a stale one.GetAndSaveVoteResultreturning the up/down counts from the existing count helpers, so the API response is unchanged.internal/repo/repo_test/vote_result_race_test.gowith a count test and a concurrent regression test (fresh question per round, 32 concurrent voters). Before this change the storedvote_countregularly ended up below the real count (e.g. 19 or 26 instead of 32); with this change every round stores the exact total.Note: the count and the write still see a statement-level snapshot, so a vote committing at the exact moment of the update can land after that update's recount. The next vote's recalculation heals the total, and the lost-update drift from the issue can no longer accumulate.