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

pin: use separate dagservice for storing pinsets #3103

Merged
merged 1 commit into from
Aug 21, 2016
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
6 changes: 4 additions & 2 deletions core/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,15 @@ func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {

n.Blocks = bserv.New(n.Blockstore, n.Exchange)
n.DAG = dag.NewDAGService(n.Blocks)
n.Pinning, err = pin.LoadPinner(n.Repo.Datastore(), n.DAG)

internalDag := dag.NewDAGService(bserv.New(n.Blockstore, offline.Exchange(n.Blockstore)))
Copy link
Member

Choose a reason for hiding this comment

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

We are using offline DAG service in many plances, maybe we should create helper function for that?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, thats probably a good idea

n.Pinning, err = pin.LoadPinner(n.Repo.Datastore(), n.DAG, internalDag)
if err != nil {
// TODO: we should move towards only running 'NewPinner' explicity on
// node init instead of implicitly here as a result of the pinner keys
// not being found in the datastore.
// this is kinda sketchy and could cause data loss
n.Pinning = pin.NewPinner(n.Repo.Datastore(), n.DAG)
n.Pinning = pin.NewPinner(n.Repo.Datastore(), n.DAG, internalDag)
}
n.Resolver = &path.Resolver{DAG: n.DAG}

Expand Down
25 changes: 2 additions & 23 deletions merkledag/merkledag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"sync"
"testing"

bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
key "github.com/ipfs/go-ipfs/blocks/key"
bserv "github.com/ipfs/go-ipfs/blockservice"
bstest "github.com/ipfs/go-ipfs/blockservice/test"
Expand All @@ -19,31 +18,11 @@ import (
chunk "github.com/ipfs/go-ipfs/importer/chunk"
. "github.com/ipfs/go-ipfs/merkledag"
dstest "github.com/ipfs/go-ipfs/merkledag/test"
"github.com/ipfs/go-ipfs/pin"
uio "github.com/ipfs/go-ipfs/unixfs/io"
ds "gx/ipfs/QmTxLSvdhwg68WJimdS6icLPhZi28aTp6b7uihC2Yb47Xk/go-datastore"
dssync "gx/ipfs/QmTxLSvdhwg68WJimdS6icLPhZi28aTp6b7uihC2Yb47Xk/go-datastore/sync"
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
"gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
)

type dagservAndPinner struct {
ds DAGService
mp pin.Pinner
}

func getDagservAndPinner(t *testing.T) dagservAndPinner {
db := dssync.MutexWrap(ds.NewMapDatastore())
bs := bstore.NewBlockstore(db)
blockserv := bserv.New(bs, offline.Exchange(bs))
dserv := NewDAGService(blockserv)
mpin := pin.NewPinner(db, dserv)
return dagservAndPinner{
ds: dserv,
mp: mpin,
}
}

func TestNode(t *testing.T) {

n1 := NodeWithData([]byte("beep"))
Expand Down Expand Up @@ -254,15 +233,15 @@ func TestEmptyKey(t *testing.T) {
}

func TestCantGet(t *testing.T) {
dsp := getDagservAndPinner(t)
ds := dstest.Mock()
a := NodeWithData([]byte("A"))

k, err := a.Key()
if err != nil {
t.Fatal(err)
}

_, err = dsp.ds.Get(context.Background(), k)
_, err = ds.Get(context.Background(), k)
if !strings.Contains(err.Error(), "not found") {
t.Fatal("expected err not found, got: ", err)
}
Expand Down
23 changes: 12 additions & 11 deletions pin/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,22 @@ type pinner struct {
// not delete them.
internalPin map[key.Key]struct{}
dserv mdag.DAGService
internal mdag.DAGService // dagservice used to store internal objects
dstore ds.Datastore
}

// NewPinner creates a new pinner using the given datastore as a backend
func NewPinner(dstore ds.Datastore, serv mdag.DAGService) Pinner {
func NewPinner(dstore ds.Datastore, serv, internal mdag.DAGService) Pinner {

// Load set from given datastore...
rcset := set.NewSimpleBlockSet()

dirset := set.NewSimpleBlockSet()

return &pinner{
recursePin: rcset,
directPin: dirset,
dserv: serv,
dstore: dstore,
internal: internal,
}
}

Expand Down Expand Up @@ -344,7 +344,7 @@ func (p *pinner) RemovePinWithMode(key key.Key, mode PinMode) {
}

// LoadPinner loads a pinner and its keysets from the given datastore
func LoadPinner(d ds.Datastore, dserv mdag.DAGService) (Pinner, error) {
func LoadPinner(d ds.Datastore, dserv, internal mdag.DAGService) (Pinner, error) {
p := new(pinner)

rootKeyI, err := d.Get(pinDatastoreKey)
Expand All @@ -361,7 +361,7 @@ func LoadPinner(d ds.Datastore, dserv mdag.DAGService) (Pinner, error) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5)
defer cancel()

root, err := dserv.Get(ctx, rootKey)
root, err := internal.Get(ctx, rootKey)
if err != nil {
return nil, fmt.Errorf("cannot find pinning root object: %v", err)
}
Expand All @@ -374,15 +374,15 @@ func LoadPinner(d ds.Datastore, dserv mdag.DAGService) (Pinner, error) {
}

{ // load recursive set
recurseKeys, err := loadSet(ctx, dserv, root, linkRecursive, recordInternal)
recurseKeys, err := loadSet(ctx, internal, root, linkRecursive, recordInternal)
if err != nil {
return nil, fmt.Errorf("cannot load recursive pins: %v", err)
}
p.recursePin = set.SimpleSetFromKeys(recurseKeys)
}

{ // load direct set
directKeys, err := loadSet(ctx, dserv, root, linkDirect, recordInternal)
directKeys, err := loadSet(ctx, internal, root, linkDirect, recordInternal)
if err != nil {
return nil, fmt.Errorf("cannot load direct pins: %v", err)
}
Expand All @@ -394,6 +394,7 @@ func LoadPinner(d ds.Datastore, dserv mdag.DAGService) (Pinner, error) {
// assign services
p.dserv = dserv
p.dstore = d
p.internal = internal

return p, nil
}
Expand Down Expand Up @@ -422,7 +423,7 @@ func (p *pinner) Flush() error {

root := &mdag.Node{}
{
n, err := storeSet(ctx, p.dserv, p.directPin.GetKeys(), recordInternal)
n, err := storeSet(ctx, p.internal, p.directPin.GetKeys(), recordInternal)
if err != nil {
return err
}
Expand All @@ -432,7 +433,7 @@ func (p *pinner) Flush() error {
}

{
n, err := storeSet(ctx, p.dserv, p.recursePin.GetKeys(), recordInternal)
n, err := storeSet(ctx, p.internal, p.recursePin.GetKeys(), recordInternal)
if err != nil {
return err
}
Expand All @@ -442,12 +443,12 @@ func (p *pinner) Flush() error {
}

// add the empty node, its referenced by the pin sets but never created
_, err := p.dserv.Add(new(mdag.Node))
_, err := p.internal.Add(new(mdag.Node))
if err != nil {
return err
}

k, err := p.dserv.Add(root)
k, err := p.internal.Add(root)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions pin/pin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestPinnerBasic(t *testing.T) {
dserv := mdag.NewDAGService(bserv)

// TODO does pinner need to share datastore with blockservice?
p := NewPinner(dstore, dserv)
p := NewPinner(dstore, dserv, dserv)

a, ak := randNode()
_, err := dserv.Add(a)
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestPinnerBasic(t *testing.T) {
t.Fatal(err)
}

np, err := LoadPinner(dstore, dserv)
np, err := LoadPinner(dstore, dserv, dserv)
if err != nil {
t.Fatal(err)
}
Expand All @@ -154,7 +154,7 @@ func TestDuplicateSemantics(t *testing.T) {
dserv := mdag.NewDAGService(bserv)

// TODO does pinner need to share datastore with blockservice?
p := NewPinner(dstore, dserv)
p := NewPinner(dstore, dserv, dserv)

a, _ := randNode()
_, err := dserv.Add(a)
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestFlush(t *testing.T) {
bserv := bs.New(bstore, offline.Exchange(bstore))

dserv := mdag.NewDAGService(bserv)
p := NewPinner(dstore, dserv)
p := NewPinner(dstore, dserv, dserv)
_, k := randNode()

p.PinWithMode(k, Recursive)
Expand All @@ -204,7 +204,7 @@ func TestPinRecursiveFail(t *testing.T) {
bserv := bs.New(bstore, offline.Exchange(bstore))
dserv := mdag.NewDAGService(bserv)

p := NewPinner(dstore, dserv)
p := NewPinner(dstore, dserv, dserv)

a, _ := randNode()
b, _ := randNode()
Expand Down