merkledag.go 9.66 KB
Newer Older
1
// package merkledag implements the IPFS Merkle DAG datastructures.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
2 3 4
package merkledag

import (
5
	"context"
6
	"fmt"
7
	"sync"
Jeromy's avatar
Jeromy committed
8

9
	bserv "github.com/ipfs/go-ipfs/blockservice"
Jeromy's avatar
Jeromy committed
10

Steven Allen's avatar
Steven Allen committed
11 12
	ipldcbor "gx/ipfs/QmNRz7BDWfdFNVLt7AVvmRefkrURD25EeoipcXqo6yoXU1/go-ipld-cbor"
	cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
13
	ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
14
	blocks "gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15 16
)

17 18 19 20
// TODO: We should move these registrations elsewhere. Really, most of the IPLD
// functionality should go in a `go-ipld` repo but that will take a lot of work
// and design.
func init() {
21 22 23
	ipld.Register(cid.DagProtobuf, DecodeProtobufBlock)
	ipld.Register(cid.Raw, DecodeRawBlock)
	ipld.Register(cid.DagCBOR, ipldcbor.DecodeBlock)
24 25
}

26
// NewDAGService constructs a new DAGService (using the default implementation).
27
func NewDAGService(bs bserv.BlockService) *dagService {
28
	return &dagService{Blocks: bs}
Jeromy's avatar
Jeromy committed
29 30
}

31
// dagService is an IPFS Merkle DAG service.
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
32 33
// - the root is virtual (like a forest)
// - stores nodes' data in a BlockService
34 35
// TODO: should cache Nodes that are in memory, and be
//       able to free some of them when vm pressure is high
36
type dagService struct {
37
	Blocks bserv.BlockService
38 39
}

40
// Add adds a node to the dagService, storing the block in the BlockService
41
func (n *dagService) Add(ctx context.Context, nd ipld.Node) error {
42
	if n == nil { // FIXME remove this assertion. protect with constructor invariant
43
		return fmt.Errorf("dagService is nil")
44 45
	}

46
	return n.Blocks.AddBlock(nd)
47 48
}

49
func (n *dagService) AddMany(ctx context.Context, nds []ipld.Node) error {
50 51 52
	blks := make([]blocks.Block, len(nds))
	for i, nd := range nds {
		blks[i] = nd
53
	}
54
	return n.Blocks.AddBlocks(blks)
55 56
}

57
// Get retrieves a node from the dagService, fetching the block in the BlockService
58
func (n *dagService) Get(ctx context.Context, c *cid.Cid) (ipld.Node, error) {
59
	if n == nil {
60
		return nil, fmt.Errorf("dagService is nil")
61
	}
Jeromy's avatar
Jeromy committed
62

63 64
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
65

Jeromy's avatar
Jeromy committed
66
	b, err := n.Blocks.GetBlock(ctx, c)
67
	if err != nil {
68
		if err == bserv.ErrNotFound {
69
			return nil, ipld.ErrNotFound
70
		}
Jeromy's avatar
Jeromy committed
71
		return nil, fmt.Errorf("Failed to get block for %s: %v", c, err)
72 73
	}

74
	return ipld.Decode(b)
75
}
Jeromy's avatar
Jeromy committed
76

77 78
// GetLinks return the links for the node, the node doesn't necessarily have
// to exist locally.
79
func (n *dagService) GetLinks(ctx context.Context, c *cid.Cid) ([]*ipld.Link, error) {
80 81 82
	if c.Type() == cid.Raw {
		return nil, nil
	}
83 84 85 86
	node, err := n.Get(ctx, c)
	if err != nil {
		return nil, err
	}
87
	return node.Links(), nil
88 89
}

90 91
func (n *dagService) Remove(ctx context.Context, c *cid.Cid) error {
	return n.Blocks.DeleteBlock(c)
92 93
}

94 95 96 97 98 99 100 101 102 103 104 105 106
// RemoveMany removes multiple nodes from the DAG. It will likely be faster than
// removing them individually.
//
// This operation is not atomic. If it returns an error, some nodes may or may
// not have been removed.
func (n *dagService) RemoveMany(ctx context.Context, cids []*cid.Cid) error {
	// TODO(#4608): make this batch all the way down.
	for _, c := range cids {
		if err := n.Blocks.DeleteBlock(c); err != nil {
			return err
		}
	}
	return nil
Jeromy's avatar
Jeromy committed
107 108
}

109 110 111
// GetLinksDirect creates a function to get the links for a node, from
// the node, bypassing the LinkService.  If the node does not exist
// locally (and can not be retrieved) an error will be returned.
112 113
func GetLinksDirect(serv ipld.NodeGetter) GetLinks {
	return func(ctx context.Context, c *cid.Cid) ([]*ipld.Link, error) {
114
		nd, err := serv.Get(ctx, c)
115
		if err != nil {
Jeromy's avatar
Jeromy committed
116
			if err == bserv.ErrNotFound {
117
				err = ipld.ErrNotFound
Jeromy's avatar
Jeromy committed
118
			}
119 120
			return nil, err
		}
121
		return nd.Links(), nil
122 123 124
	}
}

125 126 127 128
type sesGetter struct {
	bs *bserv.Session
}

129
// Get gets a single node from the DAG.
130
func (sg *sesGetter) Get(ctx context.Context, c *cid.Cid) (ipld.Node, error) {
131
	blk, err := sg.bs.GetBlock(ctx, c)
Jeromy's avatar
Jeromy committed
132 133
	switch err {
	case bserv.ErrNotFound:
134
		return nil, ipld.ErrNotFound
Jeromy's avatar
Jeromy committed
135
	default:
136
		return nil, err
Jeromy's avatar
Jeromy committed
137 138
	case nil:
		// noop
139 140
	}

141
	return ipld.Decode(blk)
142 143
}

144
// GetMany gets many nodes at once, batching the request if possible.
145
func (sg *sesGetter) GetMany(ctx context.Context, keys []*cid.Cid) <-chan *ipld.NodeOption {
146 147 148
	return getNodesFromBG(ctx, sg.bs, keys)
}

Jeromy's avatar
Jeromy committed
149
// Session returns a NodeGetter using a new session for block fetches.
Jeromy's avatar
Jeromy committed
150 151 152 153
func (ds *dagService) Session(ctx context.Context) ipld.NodeGetter {
	return &sesGetter{bserv.NewSession(ctx, ds.Blocks)}
}

154
// FetchGraph fetches all nodes that are children of the given node
155 156
func FetchGraph(ctx context.Context, root *cid.Cid, serv ipld.DAGService) error {
	var ng ipld.NodeGetter = serv
157 158
	ds, ok := serv.(*dagService)
	if ok {
159
		ng = &sesGetter{bserv.NewSession(ctx, ds.Blocks)}
160 161
	}

162 163
	v, _ := ctx.Value("progress").(*ProgressTracker)
	if v == nil {
164
		return EnumerateChildrenAsync(ctx, GetLinksDirect(ng), root, cid.NewSet().Visit)
165 166 167 168 169 170 171 172 173 174
	}
	set := cid.NewSet()
	visit := func(c *cid.Cid) bool {
		if set.Visit(c) {
			v.Increment()
			return true
		} else {
			return false
		}
	}
175
	return EnumerateChildrenAsync(ctx, GetLinksDirect(ng), root, visit)
Jeromy's avatar
Jeromy committed
176
}
177

Jeromy's avatar
Jeromy committed
178 179
// FindLinks searches this nodes links for the given key,
// returns the indexes of any links pointing to it
Jeromy's avatar
Jeromy committed
180
func FindLinks(links []*cid.Cid, c *cid.Cid, start int) []int {
Jeromy's avatar
Jeromy committed
181
	var out []int
Jeromy's avatar
Jeromy committed
182 183
	for i, lnk_c := range links[start:] {
		if c.Equals(lnk_c) {
Jeromy's avatar
Jeromy committed
184
			out = append(out, i+start)
Jeromy's avatar
Jeromy committed
185 186
		}
	}
Jeromy's avatar
Jeromy committed
187
	return out
Jeromy's avatar
Jeromy committed
188 189
}

190 191 192 193 194
// GetMany gets many nodes from the DAG at once.
//
// This method may not return all requested nodes (and may or may not return an
// error indicating that it failed to do so. It is up to the caller to verify
// that it received all nodes.
195
func (n *dagService) GetMany(ctx context.Context, keys []*cid.Cid) <-chan *ipld.NodeOption {
196
	return getNodesFromBG(ctx, n.Blocks, keys)
197 198
}

199 200
func getNodesFromBG(ctx context.Context, bs bserv.BlockGetter, keys []*cid.Cid) <-chan *ipld.NodeOption {
	out := make(chan *ipld.NodeOption, len(keys))
201
	blocks := bs.GetBlocks(ctx, keys)
202 203
	var count int

204 205 206 207 208 209
	go func() {
		defer close(out)
		for {
			select {
			case b, ok := <-blocks:
				if !ok {
210
					if count != len(keys) {
211
						out <- &ipld.NodeOption{Err: fmt.Errorf("failed to fetch all nodes")}
212
					}
213 214
					return
				}
Jeromy's avatar
Jeromy committed
215

216
				nd, err := ipld.Decode(b)
217
				if err != nil {
218
					out <- &ipld.NodeOption{Err: err}
219 220
					return
				}
Jeromy's avatar
Jeromy committed
221

222
				out <- &ipld.NodeOption{Node: nd}
Jeromy's avatar
Jeromy committed
223 224
				count++

225
			case <-ctx.Done():
226
				out <- &ipld.NodeOption{Err: ctx.Err()}
Jeromy's avatar
Jeromy committed
227
				return
228 229 230
			}
		}
	}()
231
	return out
232 233
}

234 235
// GetLinks is the type of function passed to the EnumerateChildren function(s)
// for getting the children of an IPLD node.
236
type GetLinks func(context.Context, *cid.Cid) ([]*ipld.Link, error)
Jeromy's avatar
Jeromy committed
237

238 239 240 241
// GetLinksWithDAG returns a GetLinks function that tries to use the given
// NodeGetter as a LinkGetter to get the children of a given IPLD node. This may
// allow us to traverse the DAG without actually loading and parsing the node in
// question (if we already have the links cached).
242 243 244
func GetLinksWithDAG(ng ipld.NodeGetter) GetLinks {
	return func(ctx context.Context, c *cid.Cid) ([]*ipld.Link, error) {
		return ipld.GetLinks(ctx, ng, c)
Jeromy's avatar
Jeromy committed
245
	}
246
}
247

248 249 250
// 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?
251 252 253
func EnumerateChildren(ctx context.Context, getLinks GetLinks, root *cid.Cid, visit func(*cid.Cid) bool) error {
	links, err := getLinks(ctx, root)
	if err != nil {
254 255
		return err
	}
256
	for _, lnk := range links {
257
		c := lnk.Cid
Jeromy's avatar
Jeromy committed
258
		if visit(c) {
259
			err = EnumerateChildren(ctx, getLinks, c, visit)
260 261 262 263 264 265 266
			if err != nil {
				return err
			}
		}
	}
	return nil
}
Jeromy's avatar
Jeromy committed
267

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
type ProgressTracker struct {
	Total int
	lk    sync.Mutex
}

func (p *ProgressTracker) DeriveContext(ctx context.Context) context.Context {
	return context.WithValue(ctx, "progress", p)
}

func (p *ProgressTracker) Increment() {
	p.lk.Lock()
	defer p.lk.Unlock()
	p.Total++
}

func (p *ProgressTracker) Value() int {
	p.lk.Lock()
	defer p.lk.Unlock()
	return p.Total
}

289 290 291
// FetchGraphConcurrency is total number of concurrent fetches that
// 'fetchNodes' will start at a time
var FetchGraphConcurrency = 8
Jeromy's avatar
Jeromy committed
292

293 294 295 296
// EnumerateChildrenAsync is equivalent to EnumerateChildren *except* that it
// fetches children in parallel.
//
// NOTE: It *does not* make multiple concurrent calls to the passed `visit` function.
297
func EnumerateChildrenAsync(ctx context.Context, getLinks GetLinks, c *cid.Cid, visit func(*cid.Cid) bool) error {
298
	feed := make(chan *cid.Cid)
299
	out := make(chan []*ipld.Link)
300 301 302
	done := make(chan struct{})

	var setlk sync.Mutex
303

304 305
	errChan := make(chan error)
	fetchersCtx, cancel := context.WithCancel(ctx)
306

307
	defer cancel()
308

309 310
	for i := 0; i < FetchGraphConcurrency; i++ {
		go func() {
311
			for ic := range feed {
312
				links, err := getLinks(ctx, ic)
313 314 315
				if err != nil {
					errChan <- err
					return
Jeromy's avatar
Jeromy committed
316
				}
317

318 319 320
				setlk.Lock()
				unseen := visit(ic)
				setlk.Unlock()
321

322
				if unseen {
323
					select {
324
					case out <- links:
325
					case <-fetchersCtx.Done():
326 327 328
						return
					}
				}
Jeromy's avatar
Jeromy committed
329
				select {
330
				case done <- struct{}{}:
331
				case <-fetchersCtx.Done():
Jeromy's avatar
Jeromy committed
332 333
				}
			}
334
		}()
Jeromy's avatar
Jeromy committed
335
	}
336
	defer close(feed)
Jeromy's avatar
Jeromy committed
337

338
	send := feed
339
	var todobuffer []*cid.Cid
340
	var inProgress int
Jeromy's avatar
Jeromy committed
341

342
	next := c
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	for {
		select {
		case send <- next:
			inProgress++
			if len(todobuffer) > 0 {
				next = todobuffer[0]
				todobuffer = todobuffer[1:]
			} else {
				next = nil
				send = nil
			}
		case <-done:
			inProgress--
			if inProgress == 0 && next == nil {
				return nil
			}
359 360
		case links := <-out:
			for _, lnk := range links {
361 362 363 364 365 366
				if next == nil {
					next = lnk.Cid
					send = feed
				} else {
					todobuffer = append(todobuffer, lnk.Cid)
				}
Jeromy's avatar
Jeromy committed
367
			}
368 369
		case err := <-errChan:
			return err
370

371
		case <-ctx.Done():
372
			return ctx.Err()
373
		}
Jeromy's avatar
Jeromy committed
374
	}
375

Jeromy's avatar
Jeromy committed
376
}
377

378 379 380 381
var _ ipld.LinkGetter = &dagService{}
var _ ipld.NodeGetter = &dagService{}
var _ ipld.NodeGetter = &sesGetter{}
var _ ipld.DAGService = &dagService{}