-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Add DAGService.GetLinks() method and use it in the GC and elsewhere. #3255
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"sync" | ||
|
||
bserv "github.com/ipfs/go-ipfs/blockservice" | ||
offline "github.com/ipfs/go-ipfs/exchange/offline" | ||
key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key" | ||
|
||
"context" | ||
|
@@ -28,10 +29,20 @@ type DAGService interface { | |
GetMany(context.Context, []*cid.Cid) <-chan *NodeOption | ||
|
||
Batch() *Batch | ||
|
||
LinkService | ||
} | ||
|
||
type LinkService interface { | ||
// Return all links for a node, may be more effect than | ||
// calling Get in DAGService | ||
GetLinks(context.Context, *cid.Cid) ([]*Link, error) | ||
|
||
GetOfflineLinkService() LinkService | ||
} | ||
|
||
func NewDAGService(bs *bserv.BlockService) DAGService { | ||
return &dagService{bs} | ||
func NewDAGService(bs *bserv.BlockService) *dagService { | ||
return &dagService{Blocks: bs} | ||
} | ||
|
||
// dagService is an IPFS Merkle DAG service. | ||
|
@@ -93,13 +104,30 @@ func (n *dagService) Get(ctx context.Context, c *cid.Cid) (*Node, error) { | |
return res, nil | ||
} | ||
|
||
func (n *dagService) GetLinks(ctx context.Context, c *cid.Cid) ([]*Link, error) { | ||
node, err := n.Get(ctx, c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return node.Links, nil | ||
} | ||
|
||
func (n *dagService) GetOfflineLinkService() LinkService { | ||
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. I think this can be moved to the main 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. Look at the return type. 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. Right, but the actual implementation is returning an offline DAGService, which is something that we would want to have available. I'm thinking that when we need an offline linkservice, we could call 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. I did it this way to enforce type safety. Otherwise we could be passed a LinkService and get a DAGService. If you really don't like that I can change it. 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. Type safety is good, lets move forward with this |
||
if n.Blocks.Exchange.IsOnline() { | ||
bsrv := bserv.New(n.Blocks.Blockstore, offline.Exchange(n.Blocks.Blockstore)) | ||
return NewDAGService(bsrv) | ||
} else { | ||
return n | ||
} | ||
} | ||
|
||
func (n *dagService) Remove(nd *Node) error { | ||
return n.Blocks.DeleteObject(nd) | ||
} | ||
|
||
// FetchGraph fetches all nodes that are children of the given node | ||
func FetchGraph(ctx context.Context, root *Node, serv DAGService) error { | ||
return EnumerateChildrenAsync(ctx, serv, root, cid.NewSet().Visit) | ||
func FetchGraph(ctx context.Context, c *cid.Cid, serv DAGService) error { | ||
return EnumerateChildrenAsync(ctx, serv, c, cid.NewSet().Visit) | ||
} | ||
|
||
// FindLinks searches this nodes links for the given key, | ||
|
@@ -366,19 +394,17 @@ func legacyCidFromLink(lnk *Link) *cid.Cid { | |
// EnumerateChildren will walk the dag below the given root node and add all | ||
// unseen children to the passed in set. | ||
// TODO: parallelize to avoid disk latency perf hits? | ||
func EnumerateChildren(ctx context.Context, ds DAGService, root *Node, visit func(*cid.Cid) bool, bestEffort bool) error { | ||
for _, lnk := range root.Links { | ||
func EnumerateChildren(ctx context.Context, ds LinkService, root *cid.Cid, visit func(*cid.Cid) bool, bestEffort bool) error { | ||
links, err := ds.GetLinks(ctx, root) | ||
if bestEffort && err == ErrNotFound { | ||
return nil | ||
} else if err != nil { | ||
return err | ||
} | ||
for _, lnk := range links { | ||
c := legacyCidFromLink(lnk) | ||
if visit(c) { | ||
child, err := ds.Get(ctx, c) | ||
if err != nil { | ||
if bestEffort && err == ErrNotFound { | ||
continue | ||
} else { | ||
return err | ||
} | ||
} | ||
err = EnumerateChildren(ctx, ds, child, visit, bestEffort) | ||
err = EnumerateChildren(ctx, ds, c, visit, bestEffort) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -387,7 +413,7 @@ func EnumerateChildren(ctx context.Context, ds DAGService, root *Node, visit fun | |
return nil | ||
} | ||
|
||
func EnumerateChildrenAsync(ctx context.Context, ds DAGService, root *Node, visit func(*cid.Cid) bool) error { | ||
func EnumerateChildrenAsync(ctx context.Context, ds DAGService, c *cid.Cid, visit func(*cid.Cid) bool) error { | ||
toprocess := make(chan []*cid.Cid, 8) | ||
nodes := make(chan *NodeOption, 8) | ||
|
||
|
@@ -397,6 +423,11 @@ func EnumerateChildrenAsync(ctx context.Context, ds DAGService, root *Node, visi | |
|
||
go fetchNodes(ctx, ds, toprocess, nodes) | ||
|
||
root, err := ds.Get(ctx, c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
nodes <- &NodeOption{Node: root} | ||
live := 1 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,6 @@ package gc | |
|
||
import ( | ||
bstore "github.com/ipfs/go-ipfs/blocks/blockstore" | ||
bserv "github.com/ipfs/go-ipfs/blockservice" | ||
offline "github.com/ipfs/go-ipfs/exchange/offline" | ||
dag "github.com/ipfs/go-ipfs/merkledag" | ||
pin "github.com/ipfs/go-ipfs/pin" | ||
key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key" | ||
|
@@ -24,13 +22,12 @@ var log = logging.Logger("gc") | |
// | ||
// The routine then iterates over every block in the blockstore and | ||
// deletes any block that is not found in the marked set. | ||
func GC(ctx context.Context, bs bstore.GCBlockstore, pn pin.Pinner, bestEffortRoots []*cid.Cid) (<-chan key.Key, error) { | ||
func GC(ctx context.Context, bs bstore.GCBlockstore, ls dag.LinkService, pn pin.Pinner, bestEffortRoots []*cid.Cid) (<-chan key.Key, error) { | ||
unlocker := bs.GCLock() | ||
|
||
bsrv := bserv.New(bs, offline.Exchange(bs)) | ||
ds := dag.NewDAGService(bsrv) | ||
ls = ls.GetOfflineLinkService() | ||
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. Ah, i see why this was on the |
||
|
||
gcs, err := ColoredSet(ctx, pn, ds, bestEffortRoots) | ||
gcs, err := ColoredSet(ctx, pn, ls, bestEffortRoots) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -71,16 +68,12 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, pn pin.Pinner, bestEffortRo | |
return output, nil | ||
} | ||
|
||
func Descendants(ctx context.Context, ds dag.DAGService, set key.KeySet, roots []*cid.Cid, bestEffort bool) error { | ||
func Descendants(ctx context.Context, ls dag.LinkService, set key.KeySet, roots []*cid.Cid, bestEffort bool) error { | ||
for _, c := range roots { | ||
set.Add(key.Key(c.Hash())) | ||
nd, err := ds.Get(ctx, c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// EnumerateChildren recursively walks the dag and adds the keys to the given set | ||
err = dag.EnumerateChildren(ctx, ds, nd, func(c *cid.Cid) bool { | ||
err := dag.EnumerateChildren(ctx, ls, c, func(c *cid.Cid) bool { | ||
k := key.Key(c.Hash()) | ||
seen := set.Has(k) | ||
if seen { | ||
|
@@ -97,16 +90,16 @@ func Descendants(ctx context.Context, ds dag.DAGService, set key.KeySet, roots [ | |
return nil | ||
} | ||
|
||
func ColoredSet(ctx context.Context, pn pin.Pinner, ds dag.DAGService, bestEffortRoots []*cid.Cid) (key.KeySet, error) { | ||
func ColoredSet(ctx context.Context, pn pin.Pinner, ls dag.LinkService, bestEffortRoots []*cid.Cid) (key.KeySet, error) { | ||
// KeySet currently implemented in memory, in the future, may be bloom filter or | ||
// disk backed to conserve memory. | ||
gcs := key.NewKeySet() | ||
err := Descendants(ctx, ds, gcs, pn.RecursiveKeys(), false) | ||
err := Descendants(ctx, ls, gcs, pn.RecursiveKeys(), false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = Descendants(ctx, ds, gcs, bestEffortRoots, true) | ||
err = Descendants(ctx, ls, gcs, bestEffortRoots, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -115,7 +108,7 @@ func ColoredSet(ctx context.Context, pn pin.Pinner, ds dag.DAGService, bestEffor | |
gcs.Add(key.Key(k.Hash())) | ||
} | ||
|
||
err = Descendants(ctx, ds, gcs, pn.InternalPins(), false) | ||
err = Descendants(ctx, ls, gcs, pn.InternalPins(), false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
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.
It doesnt look like we use this anywhere. Am i missing something or is this just dead code from a different approach?
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.
It is used. I added a
IsOnline()
method to exchange to be able to tell if the exchange is online or not.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.
Ah, yeah. I missed it on my previous pass through