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: discard badger transactions #5204

Merged
merged 3 commits into from
Oct 17, 2024
Merged

Conversation

Sidddddarth
Copy link
Member

@Sidddddarth Sidddddarth commented Oct 16, 2024

Description

According to badger documentation:

func (db *badger.DB) NewTransaction(update bool) *badger.Txn

NewTransaction creates a new transaction.
Badger supports concurrent execution of transactions,
providing serializable snapshot isolation, avoiding write skews.
Badger achieves this by tracking the keys read and at Commit time,
ensuring that these read keys weren't concurrently modified by another transaction.

For read-only transactions, set update to false.
In this mode, we don't track the rows read for any changes.
Thus, any long running iterations done in this mode wouldn't pay this overhead.

Running transactions concurrently is OK.
However, a transaction itself isn't thread safe, and should only be run serially.
It doesn't matter if a transaction is created by one goroutine and passed down to other,
as long as the Txn APIs are called serially.

When you create a new transaction, it is absolutely essential to call Discard().
This should be done irrespective of what the update param is set to.
Commit API internally runs Discard, but running it twice wouldn't cause any issues.

txn := db.NewTransaction(false)
defer txn.Discard()
// Call various APIs.

and

func (txn *badger.Txn) Discard()

Discard discards a created transaction.
This method is very important and must be called.
Commit method calls this internally, however, calling this multiple times doesn't cause any issues.
So, this can safely be called via a defer right when transaction is created.

NOTE: If any operations are run on a discarded transaction, ErrDiscardedTxn is returned.

Linear Ticket

< Replace with Linear Link ( create or search linear ticket) or >

Security

  • The code changed/added as part of this pull request won't create any security issues with how the software is being used.

Copy link

codecov bot commented Oct 17, 2024

Codecov Report

Attention: Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.

Project coverage is 74.64%. Comparing base (6edd75f) to head (bf5813a).
Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
services/dedup/badger/badger.go 83.33% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5204      +/-   ##
==========================================
+ Coverage   73.91%   74.64%   +0.73%     
==========================================
  Files         428      417      -11     
  Lines       50416    49913     -503     
==========================================
- Hits        37266    37260       -6     
+ Misses      10730    10233     -497     
  Partials     2420     2420              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Sidddddarth Sidddddarth enabled auto-merge (squash) October 17, 2024 06:00
@Sidddddarth Sidddddarth enabled auto-merge (squash) October 17, 2024 06:00
@Sidddddarth Sidddddarth merged commit 9683021 into master Oct 17, 2024
55 checks passed
@Sidddddarth Sidddddarth deleted the fix.badgerTransactionDiscard branch October 17, 2024 06:12
This was referenced Oct 28, 2024
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