Skip to content

Commit

Permalink
Introduce first strategic provider: do nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelavila committed May 2, 2019
1 parent 648fa3a commit 5c3480e
Show file tree
Hide file tree
Showing 23 changed files with 578 additions and 405 deletions.
1 change: 0 additions & 1 deletion core/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/ipfs/go-metrics-interface"
"go.uber.org/fx"

"github.com/ipfs/go-ipfs/core/bootstrap"
"github.com/ipfs/go-ipfs/core/node"
)
Expand Down
2 changes: 1 addition & 1 deletion core/commands/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ Trigger reprovider to announce our data to network.
return ErrNotOnline
}

err = nd.Reprovider.Trigger(req.Context)
err = nd.Provider.Reprovide(req.Context)
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/ipfs/go-ipfs/pin"
"github.com/ipfs/go-ipfs/provider"
"github.com/ipfs/go-ipfs/repo"
rp "github.com/ipfs/go-ipfs/reprovide"

bserv "github.com/ipfs/go-blockservice"
bstore "github.com/ipfs/go-ipfs-blockstore"
Expand Down Expand Up @@ -94,8 +93,7 @@ type IpfsNode struct {
Routing routing.IpfsRouting `optional:"true"` // the routing system. recommend ipfs-dht
Exchange exchange.Interface // the block exchange + strategy (bitswap)
Namesys namesys.NameSystem // the name system, resolves paths to hashes
Provider provider.Provider // the value provider system
Reprovider *rp.Reprovider `optional:"true"` // the value reprovider system
Provider provider.System // the value provider system
IpnsRepub *ipnsrp.Republisher `optional:"true"`

AutoNAT *autonat.AutoNATService `optional:"true"`
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type CoreAPI struct {
namesys namesys.NameSystem
routing routing.IpfsRouting

provider provider.Provider
provider provider.System

pubSub *pubsub.PubSub

Expand Down
4 changes: 3 additions & 1 deletion core/node/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package node
import (
"context"
"fmt"
"github.com/ipfs/go-ipfs-config"

"github.com/ipfs/go-ipfs/core/node/helpers"
"github.com/ipfs/go-ipfs/pin"
Expand Down Expand Up @@ -59,8 +60,9 @@ func Dag(bs blockservice.BlockService) format.DAGService {
}

// OnlineExchange creates new LibP2P backed block exchange (BitSwap)
func OnlineExchange(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, rt routing.IpfsRouting, bs blockstore.GCBlockstore) exchange.Interface {
func OnlineExchange(mctx helpers.MetricsCtx, lc fx.Lifecycle, cfg *config.Config, host host.Host, rt routing.IpfsRouting, bs blockstore.GCBlockstore) exchange.Interface {
bitswapNetwork := network.NewFromIpfsHost(host, rt)
bitswap.ProvideEnabled = !cfg.Experimental.StrategicProviding
exch := bitswap.New(helpers.LifecycleCtx(mctx, lc), bitswapNetwork, bs)
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
Expand Down
58 changes: 11 additions & 47 deletions core/node/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ import (
pubsub "github.com/libp2p/go-libp2p-pubsub"

"github.com/ipfs/go-ipfs/core/node/libp2p"
"github.com/ipfs/go-ipfs/p2p"
"github.com/ipfs/go-ipfs/provider"
"github.com/ipfs/go-ipfs/reprovide"

offline "github.com/ipfs/go-ipfs-exchange-offline"
offroute "github.com/ipfs/go-ipfs-routing/offline"
"github.com/ipfs/go-ipfs/p2p"
"github.com/ipfs/go-path/resolver"
uio "github.com/ipfs/go-unixfs/io"
"go.uber.org/fx"
Expand Down Expand Up @@ -188,42 +186,6 @@ var IPNS = fx.Options(
fx.Provide(RecordValidator),
)

// Providers groups units managing provider routing records
func Providers(cfg *config.Config) fx.Option {
reproviderInterval := kReprovideFrequency
if cfg.Reprovider.Interval != "" {
dur, err := time.ParseDuration(cfg.Reprovider.Interval)
if err != nil {
return fx.Error(err)
}

reproviderInterval = dur
}

var keyProvider fx.Option
switch cfg.Reprovider.Strategy {
case "all":
fallthrough
case "":
keyProvider = fx.Provide(reprovide.NewBlockstoreProvider)
case "roots":
keyProvider = fx.Provide(reprovide.NewPinnedProvider(true))
case "pinned":
keyProvider = fx.Provide(reprovide.NewPinnedProvider(false))
default:
return fx.Error(fmt.Errorf("unknown reprovider strategy '%s'", cfg.Reprovider.Strategy))
}

return fx.Options(
fx.Provide(ProviderQueue),
fx.Provide(ProviderCtor),
fx.Provide(ReproviderCtor(reproviderInterval)),
keyProvider,

fx.Invoke(Reprovider),
)
}

// Online groups online-only units
func Online(bcfg *BuildCfg, cfg *config.Config) fx.Option {

Expand Down Expand Up @@ -272,17 +234,19 @@ func Online(bcfg *BuildCfg, cfg *config.Config) fx.Option {
fx.Provide(p2p.New),

LibP2P(bcfg, cfg),
Providers(cfg),
OnlineProviders(cfg),
)
}

// Offline groups offline alternatives to Online units
var Offline = fx.Options(
fx.Provide(offline.Exchange),
fx.Provide(Namesys(0)),
fx.Provide(offroute.NewOfflineRouter),
fx.Provide(provider.NewOfflineProvider),
)
func Offline(cfg *config.Config) fx.Option {
return fx.Options(
fx.Provide(offline.Exchange),
fx.Provide(Namesys(0)),
fx.Provide(offroute.NewOfflineRouter),
OfflineProviders(cfg),
)
}

// Core groups basic IPFS services
var Core = fx.Options(
Expand All @@ -297,7 +261,7 @@ func Networked(bcfg *BuildCfg, cfg *config.Config) fx.Option {
if bcfg.Online {
return Online(bcfg, cfg)
}
return Offline
return Offline(cfg)
}

// IPFS builds a group of fx Options based on the passed BuildCfg
Expand Down
105 changes: 87 additions & 18 deletions core/node/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,119 @@ package node

import (
"context"
"fmt"
"time"

"github.com/libp2p/go-libp2p-routing"
"go.uber.org/fx"

"github.com/ipfs/go-ipfs-config"
"github.com/ipfs/go-ipfs/core/node/helpers"
"github.com/ipfs/go-ipfs/provider"
q "github.com/ipfs/go-ipfs/provider/queue"
"github.com/ipfs/go-ipfs/provider/simple"
"github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-ipfs/reprovide"
"github.com/libp2p/go-libp2p-routing"
)

const kReprovideFrequency = time.Hour * 12

// SIMPLE

// ProviderQueue creates new datastore backed provider queue
func ProviderQueue(mctx helpers.MetricsCtx, lc fx.Lifecycle, repo repo.Repo) (*provider.Queue, error) {
return provider.NewQueue(helpers.LifecycleCtx(mctx, lc), "provider-v1", repo.Datastore())
func ProviderQueue(mctx helpers.MetricsCtx, lc fx.Lifecycle, repo repo.Repo) (*q.Queue, error) {
return q.NewQueue(helpers.LifecycleCtx(mctx, lc), "provider-v1", repo.Datastore())
}

// SimpleProviderCtor creates new record provider
func SimpleProviderCtor(mctx helpers.MetricsCtx, lc fx.Lifecycle, queue *q.Queue, rt routing.IpfsRouting) provider.Provider {
return simple.NewProvider(helpers.LifecycleCtx(mctx, lc), queue, rt)
}

// ProviderCtor creates new record provider
func ProviderCtor(mctx helpers.MetricsCtx, lc fx.Lifecycle, queue *provider.Queue, rt routing.IpfsRouting) provider.Provider {
p := provider.NewProvider(helpers.LifecycleCtx(mctx, lc), queue, rt)
// SimpleReproviderCtor creates new reprovider
func SimpleReproviderCtor(reproviderInterval time.Duration) func(helpers.MetricsCtx, fx.Lifecycle, routing.IpfsRouting, simple.KeyChanFunc) (provider.Reprovider, error) {
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, rt routing.IpfsRouting, keyProvider simple.KeyChanFunc) (provider.Reprovider, error) {
return simple.NewReprovider(helpers.LifecycleCtx(mctx, lc), reproviderInterval, rt, keyProvider), nil
}
}

// SimpleProviderSysCtor creates new provider system
func SimpleProviderSysCtor(lc fx.Lifecycle, p provider.Provider, r provider.Reprovider) provider.System {
sys := provider.NewSystem(p, r)
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
p.Run()
sys.Run()
return nil
},
OnStop: func(ctx context.Context) error {
return p.Close()
return sys.Close()
},
})
return sys
}

return p
// SimpleOfflineProviderSysCtor creates a new offline provider system
func SimpleOfflineProviderSysCtor(p provider.Provider, r provider.Reprovider) provider.System {
return provider.NewSystem(p, r)
}

// ReproviderCtor creates new reprovider
func ReproviderCtor(reproviderInterval time.Duration) func(helpers.MetricsCtx, fx.Lifecycle, routing.IpfsRouting, reprovide.KeyChanFunc) (*reprovide.Reprovider, error) {
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, rt routing.IpfsRouting, keyProvider reprovide.KeyChanFunc) (*reprovide.Reprovider, error) {
return reprovide.NewReprovider(helpers.LifecycleCtx(mctx, lc), reproviderInterval, rt, keyProvider), nil
// STRATEGIC

// StrategicProviderSysCtor creates new provider system
func StrategicProviderSysCtor() provider.System {
return provider.NewOfflineProvider()
}

// StrategicOfflineProviderSysCtor creates a new offline provider system
func StrategicOfflineProviderSysCtor() provider.System {
return provider.NewOfflineProvider()
}

// ONLINE/OFFLINE

// OnlineProviders groups units managing provider routing records online
func OnlineProviders(cfg *config.Config) fx.Option {
reproviderInterval := kReprovideFrequency
if cfg.Reprovider.Interval != "" {
dur, err := time.ParseDuration(cfg.Reprovider.Interval)
if err != nil {
return fx.Error(err)
}

reproviderInterval = dur
}

if cfg.Experimental.StrategicProviding {
return fx.Provide(StrategicProviderSysCtor)
} else {
var keyProvider fx.Option
switch cfg.Reprovider.Strategy {
case "all":
fallthrough
case "":
keyProvider = fx.Provide(simple.NewBlockstoreProvider)
case "roots":
keyProvider = fx.Provide(simple.NewPinnedProvider(true))
case "pinned":
keyProvider = fx.Provide(simple.NewPinnedProvider(false))
default:
return fx.Error(fmt.Errorf("unknown reprovider strategy '%s'", cfg.Reprovider.Strategy))
}

return fx.Options(
fx.Provide(ProviderQueue),
fx.Provide(SimpleProviderCtor),
keyProvider,
fx.Provide(SimpleReproviderCtor(reproviderInterval)),
fx.Provide(SimpleProviderSysCtor),
)
}
}

// Reprovider runs the reprovider service
func Reprovider(lp lcProcess, reprovider *reprovide.Reprovider) error {
lp.Append(reprovider.Run)
return nil
// OfflineProviders groups units managing provider routing records offline
func OfflineProviders(cfg *config.Config) fx.Option {
if cfg.Experimental.StrategicProviding {
return fx.Provide(StrategicOfflineProviderSysCtor)
} else {
return fx.Provide(SimpleOfflineProviderSysCtor)
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,8 @@ require (
)

go 1.12

replace (
github.com/ipfs/go-bitswap v0.0.4 => ../go-bitswap
github.com/ipfs/go-ipfs-config v0.0.2 => ../go-ipfs-config
)
20 changes: 14 additions & 6 deletions provider/offline.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package provider

import "github.com/ipfs/go-cid"
import (
"context"
"github.com/ipfs/go-cid"
)

type offlineProvider struct{}

// NewOfflineProvider creates a Provider that does nothing
func NewOfflineProvider() Provider {
// NewOfflineProvider creates a ProviderSystem that does nothing
func NewOfflineProvider() System {
return &offlineProvider{}
}

func (op *offlineProvider) Run() {}
func (op *offlineProvider) Run() {
}

func (op *offlineProvider) Provide(cid cid.Cid) error {
func (op *offlineProvider) Close() error {
return nil
}

func (op *offlineProvider) Close() error {
func (op *offlineProvider) Provide(_ cid.Cid) error {
return nil
}

func (op *offlineProvider) Reprovide(_ context.Context) error {
return nil
}
Loading

2 comments on commit 5c3480e

@GitCop
Copy link

@GitCop GitCop commented on 5c3480e May 2, 2019

Choose a reason for hiding this comment

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

There were the following issues with your Pull Request

We ask for a few features in the commit message for Open Source licensing hygiene and commit message clarity.
git commit --amend can often help you quickly improve the commit message.
Guidelines and a script are available to help in the long run.
Your feedback on GitCop is welcome on this issue.


This message was auto-generated by https://gitcop.com

@GitCop
Copy link

@GitCop GitCop commented on 5c3480e May 2, 2019

Choose a reason for hiding this comment

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

There were the following issues with your Pull Request

We ask for a few features in the commit message for Open Source licensing hygiene and commit message clarity.
git commit --amend can often help you quickly improve the commit message.
Guidelines and a script are available to help in the long run.
Your feedback on GitCop is welcome on this issue.


This message was auto-generated by https://gitcop.com

Please sign in to comment.