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

refactor: Make rootstore require Batching and TxnDatastore #940

Merged
merged 9 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions cli/serverdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
"os"
"os/signal"

ds "github.com/ipfs/go-datastore"
"github.com/spf13/cobra"

ds "github.com/sourcenetwork/defradb/datastore"
badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3"
"github.com/sourcenetwork/defradb/db"
"github.com/sourcenetwork/defradb/errors"
Expand All @@ -36,7 +36,7 @@ var serverDumpCmd = &cobra.Command{
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt)

var rootstore ds.Batching
var rootstore ds.RootStore
var err error
if datastore == badgerDatastoreName {
info, err := os.Stat(cfg.Datastore.Badger.Path)
Expand Down
4 changes: 2 additions & 2 deletions cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"strings"

badger "github.com/dgraph-io/badger/v3"
ds "github.com/ipfs/go-datastore"
fredcarle marked this conversation as resolved.
Show resolved Hide resolved
ma "github.com/multiformats/go-multiaddr"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -30,6 +29,7 @@ import (
httpapi "github.com/sourcenetwork/defradb/api/http"
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/config"
ds "github.com/sourcenetwork/defradb/datastore"
badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3"
"github.com/sourcenetwork/defradb/db"
"github.com/sourcenetwork/defradb/errors"
Expand Down Expand Up @@ -203,7 +203,7 @@ func (di *defraInstance) close(ctx context.Context) {
func start(ctx context.Context) (*defraInstance, error) {
log.FeedbackInfo(ctx, "Starting DefraDB service...")

var rootstore ds.Batching
var rootstore ds.RootStore

var err error
if cfg.Datastore.Store == badgerDatastoreName {
Expand Down
3 changes: 1 addition & 2 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package client
import (
"context"

ds "github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore"

"github.com/sourcenetwork/defradb/datastore"
Expand All @@ -28,7 +27,7 @@ type DB interface {
GetCollectionBySchemaID(context.Context, string) (Collection, error)
GetAllCollections(ctx context.Context) ([]Collection, error)

Root() ds.Batching
Root() datastore.RootStore
Blockstore() blockstore.Blockstore

NewTxn(context.Context, bool) (datastore.Txn, error)
Expand Down
12 changes: 11 additions & 1 deletion datastore/memory/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,15 @@ import "github.com/sourcenetwork/defradb/errors"
var (
ErrReadOnlyTxn = errors.New("read only transaction")
ErrTxnDiscarded = errors.New("transaction discarded")
ErrTxnConflict = errors.New("transaction conflict")
ErrTxnConflict = txnConflictError{errors.New("transaction conflict")}
)

type txnConflictError struct {
error
}

// custom error formatting that is non idiomatic but matches the
// Badger transaction conflict message
func (e txnConflictError) Error() string {
return "Transaction Conflict. Please retry"
}
2 changes: 1 addition & 1 deletion datastore/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (d *Datastore) Put(ctx context.Context, key ds.Key, value []byte) (err erro

// Query implements ds.Query
func (d *Datastore) Query(ctx context.Context, q dsq.Query) (dsq.Results, error) {
re := make([]dsq.Entry, 0, d.values.Len())
re := make([]dsq.Entry, 0, d.values.Height())
iter := d.values.Iter()
for iter.Next() {
// fast forward to last inserted version
Expand Down
22 changes: 10 additions & 12 deletions datastore/memory/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (t *basicTxn) Query(ctx context.Context, q dsq.Query) (dsq.Results, error)
return nil, ErrTxnDiscarded
}
// best effort allocation
re := make([]dsq.Entry, 0, t.ds.values.Len()+t.ops.Len())
re := make([]dsq.Entry, 0, t.ds.values.Height()+t.ops.Height())
iter := t.ds.values.Iter()
iterOps := t.ops.Iter()
iterOpsHasValue := iterOps.Next()
Expand Down Expand Up @@ -208,20 +208,18 @@ func (t *basicTxn) Commit(ctx context.Context) error {
if t.discarded {
return ErrTxnDiscarded
}
if t.readOnly {
return ErrReadOnlyTxn
Copy link
Contributor

Choose a reason for hiding this comment

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

question: My brain is blanking here now - why are you removing this error?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because our collection api does a commit (c.commitImplicitTxn) on Get which is probably used to discard the transaction but nonetheless expects no errors of committing a read only transaction. Committing on a read only transaction now simply discards the transaction without error.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have any (other) protection against mutations made during a read only transaction?

Copy link
Member

Choose a reason for hiding this comment

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

The actual Put method checks if its readonly before doing the operation.

The commitImplicitTxn is unrelated to this kind of stuff tho. Not sure how I missed this line, its def not an error to Commit a readonly transaction, its just basically a no-op.

}
defer t.Discard(ctx)

c := commit{
tx: t,
err: make(chan error),
if !t.readOnly {
c := commit{
tx: t,
err: make(chan error),
}
t.ds.commit <- c
return <-c.err
}
t.ds.commit <- c
e := <-c.err

t.Discard(ctx)

return e
return nil
}

func (t *basicTxn) checkForConflicts(ctx context.Context) error {
Expand Down
Loading