-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6068 from ipfs/features/provide-roots-on-add-and-pin
Provide root node immediately on add and pin add
- Loading branch information
Showing
12 changed files
with
523 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package coreapi | ||
|
||
import ( | ||
cid "github.com/ipfs/go-cid" | ||
) | ||
|
||
// ProviderAPI brings Provider behavior to CoreAPI | ||
type ProviderAPI CoreAPI | ||
|
||
// Provide the given cid using the current provider | ||
func (api *ProviderAPI) Provide(cid cid.Cid) error { | ||
return api.provider.Provide(cid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package provider | ||
|
||
import "github.com/ipfs/go-cid" | ||
|
||
type offlineProvider struct{} | ||
|
||
// NewOfflineProvider creates a Provider that does nothing | ||
func NewOfflineProvider() Provider { | ||
return &offlineProvider{} | ||
} | ||
|
||
func (op *offlineProvider) Run() {} | ||
|
||
func (op *offlineProvider) Provide(cid cid.Cid) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Package provider implements structures and methods to provide blocks, | ||
// keep track of which blocks are provided, and to allow those blocks to | ||
// be reprovided. | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"github.com/ipfs/go-cid" | ||
logging "github.com/ipfs/go-log" | ||
"github.com/libp2p/go-libp2p-routing" | ||
) | ||
|
||
var log = logging.Logger("provider") | ||
|
||
const provideOutgoingWorkerLimit = 8 | ||
|
||
// Provider announces blocks to the network | ||
type Provider interface { | ||
// Run is used to begin processing the provider work | ||
Run() | ||
// Provide takes a cid and makes an attempt to announce it to the network | ||
Provide(cid.Cid) error | ||
} | ||
|
||
type provider struct { | ||
ctx context.Context | ||
// the CIDs for which provide announcements should be made | ||
queue *Queue | ||
// used to announce providing to the network | ||
contentRouting routing.ContentRouting | ||
} | ||
|
||
// NewProvider creates a provider that announces blocks to the network using a content router | ||
func NewProvider(ctx context.Context, queue *Queue, contentRouting routing.ContentRouting) Provider { | ||
return &provider{ | ||
ctx: ctx, | ||
queue: queue, | ||
contentRouting: contentRouting, | ||
} | ||
} | ||
|
||
// Start workers to handle provide requests. | ||
func (p *provider) Run() { | ||
p.handleAnnouncements() | ||
} | ||
|
||
// Provide the given cid using specified strategy. | ||
func (p *provider) Provide(root cid.Cid) error { | ||
p.queue.Enqueue(root) | ||
return nil | ||
} | ||
|
||
// Handle all outgoing cids by providing (announcing) them | ||
func (p *provider) handleAnnouncements() { | ||
for workers := 0; workers < provideOutgoingWorkerLimit; workers++ { | ||
go func() { | ||
for p.ctx.Err() == nil { | ||
select { | ||
case <-p.ctx.Done(): | ||
return | ||
case c := <-p.queue.Dequeue(): | ||
log.Info("announce - start - ", c) | ||
if err := p.contentRouting.Provide(p.ctx, c, true); err != nil { | ||
log.Warningf("Unable to provide entry: %s, %s", c, err) | ||
} | ||
log.Info("announce - end - ", c) | ||
} | ||
} | ||
}() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
||
cid "github.com/ipfs/go-cid" | ||
datastore "github.com/ipfs/go-datastore" | ||
sync "github.com/ipfs/go-datastore/sync" | ||
blocksutil "github.com/ipfs/go-ipfs-blocksutil" | ||
pstore "github.com/libp2p/go-libp2p-peerstore" | ||
) | ||
|
||
var blockGenerator = blocksutil.NewBlockGenerator() | ||
|
||
type mockRouting struct { | ||
provided chan cid.Cid | ||
} | ||
|
||
func (r *mockRouting) Provide(ctx context.Context, cid cid.Cid, recursive bool) error { | ||
r.provided <- cid | ||
return nil | ||
} | ||
|
||
func (r *mockRouting) FindProvidersAsync(ctx context.Context, cid cid.Cid, timeout int) <-chan pstore.PeerInfo { | ||
return nil | ||
} | ||
|
||
func mockContentRouting() *mockRouting { | ||
r := mockRouting{} | ||
r.provided = make(chan cid.Cid) | ||
return &r | ||
} | ||
|
||
func TestAnnouncement(t *testing.T) { | ||
ctx := context.Background() | ||
defer ctx.Done() | ||
|
||
ds := sync.MutexWrap(datastore.NewMapDatastore()) | ||
queue, err := NewQueue(ctx, "test", ds) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
r := mockContentRouting() | ||
|
||
provider := NewProvider(ctx, queue, r) | ||
provider.Run() | ||
|
||
cids := cid.NewSet() | ||
|
||
for i := 0; i < 100; i++ { | ||
c := blockGenerator.Next().Cid() | ||
cids.Add(c) | ||
} | ||
|
||
go func() { | ||
for _, c := range cids.Keys() { | ||
err = provider.Provide(c) | ||
// A little goroutine stirring to exercise some different states | ||
r := rand.Intn(10) | ||
time.Sleep(time.Microsecond * time.Duration(r)) | ||
} | ||
}() | ||
|
||
for cids.Len() > 0 { | ||
select { | ||
case cp := <-r.provided: | ||
if !cids.Has(cp) { | ||
t.Fatal("Wrong CID provided") | ||
} | ||
cids.Remove(cp) | ||
case <-time.After(time.Second * 5): | ||
t.Fatal("Timeout waiting for cids to be provided.") | ||
} | ||
} | ||
} |
Oops, something went wrong.