Skip to content

fix: recalculate vote counts in a single atomic update - #1650

Open
ChangkeunJ wants to merge 1 commit into
apache:mainfrom
ChangkeunJ:fix/vote-count-race
Open

ChangkeunJ wants to merge 1 commit into
apache:mainfrom
ChangkeunJ:fix/vote-count-race

Conversation

@ChangkeunJ

Copy link
Copy Markdown

Fixes #1646

Proposed Changes

  • Recalculate the stored vote_count in a single atomic UPDATE that 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.
  • Keep GetAndSaveVoteResult returning the up/down counts from the existing count helpers, so the API response is unchanged.
  • Add internal/repo/repo_test/vote_result_race_test.go with a count test and a concurrent regression test (fresh question per round, 32 concurrent voters). Before this change the stored vote_count regularly 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.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Concurrent vote updates may overwrite vote_count with stale totals

1 participant