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: add isolation level in transaction repository #824

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kushsharma
Copy link
Member

Currently there were chances to have race conditions while writing to transaction repository. I have added a test to verify it doesn't happen. Database is using repeatable read as the isolation level to avoid overlapping transactions.

Copy link

vercel bot commented Nov 29, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
frontier ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 29, 2024 3:56am

Currently there were chances to have race conditions while writing
to transaction repository. I have added a test to verify it doesn't
happen. Database is using repeatable read as the isolation level
to avoid overlapping transactions.

Signed-off-by: Kush Sharma <[email protected]>
@coveralls
Copy link

Pull Request Test Coverage Report for Build 12078725933

Details

  • 4 of 139 (2.88%) changed or added relevant lines in 4 files are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.002%) to 30.821%

Changes Missing Coverage Covered Lines Changed/Added Lines %
internal/store/postgres/group_repository.go 0 15 0.0%
internal/store/postgres/organization_repository.go 0 19 0.0%
internal/store/postgres/billing_transactions_repository.go 0 101 0.0%
Files with Coverage Reduction New Missed Lines %
internal/store/postgres/billing_transactions_repository.go 2 3.06%
Totals Coverage Status
Change from base Build 12078240619: -0.002%
Covered Lines: 7626
Relevant Lines: 24743

💛 - Coveralls

}); err != nil {
err := r.withRetry(ctx, func() error {
return r.dbc.WithTxn(ctx, sql.TxOptions{
Isolation: sql.LevelRepeatableRead,
Copy link
Contributor

Choose a reason for hiding this comment

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

So if I understand correctly, we check balance by always looping over the table and calculating difference. We insert new rows for debit/credit. How will sql.LevelRepeatableRead prevent race condition here? Phantom reads and Write Skew are allowed in Repeatable Read as far I can see. It won't prevent new rows being written and hence change of balance mid transaction.
Can you test if this isolation is working fine? Maybe write a Python or Go code and hit the API concurrently 20-30 times for the same org. If let's say starting balance is 105, and we deduct 10 credit each call then only 10 calls should succeed and the balance should be 5 at the end.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agree, Serializable would be best and safest(with some added latency), that's what I proposed in the last PR. Although I don't see the issue happening in the test https://github.com/raystack/frontier/pull/824/files#diff-aaeca6450e0dcd11871799ac2b14da4225d7a76f94c04a9d4636e44ae780773eR1115

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.

3 participants