-
Notifications
You must be signed in to change notification settings - Fork 33
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
Avoid discarding transaction too early in queries #31
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,10 @@ type Datastore struct { | |
// the badger Datastore. | ||
type txn struct { | ||
txn *badger.Txn | ||
|
||
// Whether this transaction has been implicitly created as a result of a direct Datastore | ||
// method invocation. | ||
implicit bool | ||
} | ||
|
||
// Options are the badger datastore options, reexported here for convenience. | ||
|
@@ -79,11 +83,17 @@ func NewDatastore(path string, options *Options) (*Datastore, error) { | |
// can be mutated without incurring changes to the underlying Datastore until | ||
// the transaction is Committed. | ||
func (d *Datastore) NewTransaction(readOnly bool) ds.Txn { | ||
return &txn{d.DB.NewTransaction(!readOnly)} | ||
return &txn{d.DB.NewTransaction(!readOnly), false} | ||
} | ||
|
||
// newImplicitTransaction creates a transaction marked as 'implicit'. | ||
// Implicit transactions are created by Datastore methods performing single operations. | ||
func (d *Datastore) newImplicitTransaction(readOnly bool) ds.Txn { | ||
return &txn{d.DB.NewTransaction(!readOnly), true} | ||
} | ||
|
||
func (d *Datastore) Put(key ds.Key, value []byte) error { | ||
txn := d.NewTransaction(false) | ||
txn := d.newImplicitTransaction(false) | ||
defer txn.Discard() | ||
|
||
if err := txn.Put(key, value); err != nil { | ||
|
@@ -94,7 +104,7 @@ func (d *Datastore) Put(key ds.Key, value []byte) error { | |
} | ||
|
||
func (d *Datastore) PutWithTTL(key ds.Key, value []byte, ttl time.Duration) error { | ||
txn := d.NewTransaction(false).(*txn) | ||
txn := d.newImplicitTransaction(false).(*txn) | ||
defer txn.Discard() | ||
|
||
if err := txn.PutWithTTL(key, value, ttl); err != nil { | ||
|
@@ -105,7 +115,7 @@ func (d *Datastore) PutWithTTL(key ds.Key, value []byte, ttl time.Duration) erro | |
} | ||
|
||
func (d *Datastore) SetTTL(key ds.Key, ttl time.Duration) error { | ||
txn := d.NewTransaction(false).(*txn) | ||
txn := d.newImplicitTransaction(false).(*txn) | ||
defer txn.Discard() | ||
|
||
if err := txn.SetTTL(key, ttl); err != nil { | ||
|
@@ -116,21 +126,21 @@ func (d *Datastore) SetTTL(key ds.Key, ttl time.Duration) error { | |
} | ||
|
||
func (d *Datastore) Get(key ds.Key) (value []byte, err error) { | ||
txn := d.NewTransaction(true) | ||
txn := d.newImplicitTransaction(true) | ||
defer txn.Discard() | ||
|
||
return txn.Get(key) | ||
} | ||
|
||
func (d *Datastore) Has(key ds.Key) (bool, error) { | ||
txn := d.NewTransaction(true) | ||
txn := d.newImplicitTransaction(true) | ||
defer txn.Discard() | ||
|
||
return txn.Has(key) | ||
} | ||
|
||
func (d *Datastore) Delete(key ds.Key) error { | ||
txn := d.NewTransaction(false) | ||
txn := d.newImplicitTransaction(false) | ||
defer txn.Discard() | ||
|
||
err := txn.Delete(key) | ||
|
@@ -142,9 +152,10 @@ func (d *Datastore) Delete(key ds.Key) error { | |
} | ||
|
||
func (d *Datastore) Query(q dsq.Query) (dsq.Results, error) { | ||
txn := d.NewTransaction(true) | ||
defer txn.Discard() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the error this PR solves:
|
||
|
||
txn := d.newImplicitTransaction(true) | ||
// We cannot defer txn.Discard() here, as the txn must remain active while the iterator is open. | ||
// https://github.com/dgraph-io/badger/commit/b1ad1e93e483bbfef123793ceedc9a7e34b09f79 | ||
// The closing logic in the query goprocess takes care of discarding the implicit transaction. | ||
return txn.Query(q) | ||
} | ||
|
||
|
@@ -243,6 +254,11 @@ func (t *txn) Query(q dsq.Query) (dsq.Results, error) { | |
qrb := dsq.NewResultBuilder(q) | ||
|
||
qrb.Process.Go(func(worker goprocess.Process) { | ||
if t.implicit { | ||
// this iterator is part of an implicit transaction, so when we're done we must discard | ||
// the transaction. It's safe to discard the txn it because it contains the iterator only. | ||
defer t.Discard() | ||
} | ||
defer it.Close() | ||
|
||
for sent := 0; it.ValidForPrefix(prefix); sent++ { | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now that we have more than one value in the struct, let's address them by name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I merged before addressing this. Worth opening another PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe just leave a todo for yourself! i think it's fine.