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

Avoid discarding transaction too early in queries #31

Merged
merged 1 commit into from
Sep 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}
Copy link
Contributor

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

Copy link
Member Author

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?

Copy link
Contributor

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.

}

// 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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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()
Copy link
Member Author

Choose a reason for hiding this comment

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

This defer is problematic with badger master, as it discards the txn while the iterator is active.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is the error this PR solves:

panic: Unclosed iterator at time of Txn.Discard. [recovered]
	panic: Unclosed iterator at time of Txn.Discard.


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)
}

Expand Down Expand Up @@ -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++ {
Expand Down