2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-14 19:23:54 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-12-17 16:31:14 +00:00
|
|
|
"crypto/rand"
|
2019-01-18 13:54:08 +00:00
|
|
|
"time"
|
2018-12-14 19:23:54 +00:00
|
|
|
|
2018-12-17 16:31:14 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-12-22 00:48:53 +00:00
|
|
|
"go.uber.org/zap"
|
2019-02-08 09:25:13 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2019-06-04 12:36:27 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-12-20 21:45:06 +00:00
|
|
|
|
2019-02-08 09:25:13 +00:00
|
|
|
"storj.io/storj/internal/sync2"
|
2018-12-14 19:23:54 +00:00
|
|
|
"storj.io/storj/pkg/kademlia"
|
2018-12-17 16:31:14 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/satellite/overlay"
|
2018-12-17 16:31:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-06-04 12:36:27 +01:00
|
|
|
mon = monkit.Package()
|
2019-01-23 19:58:44 +00:00
|
|
|
|
|
|
|
// Error is a general error class of this package
|
|
|
|
Error = errs.Class("discovery error")
|
2018-12-14 19:23:54 +00:00
|
|
|
)
|
|
|
|
|
2019-01-30 16:29:18 +00:00
|
|
|
// Config loads on the configuration values for the cache
|
2019-01-23 19:58:44 +00:00
|
|
|
type Config struct {
|
2019-07-12 15:35:48 +01:00
|
|
|
RefreshInterval time.Duration `help:"the interval at which the cache refreshes itself in seconds" default:"1s"`
|
|
|
|
DiscoveryInterval time.Duration `help:"the interval at which the satellite attempts to find new nodes via random node ID lookups" default:"1s"`
|
2019-08-06 17:35:59 +01:00
|
|
|
RefreshLimit int `help:"the amount of nodes read from the overlay in a single pagination call" default:"100"`
|
2019-07-12 15:35:48 +01:00
|
|
|
RefreshConcurrency int `help:"the amount of nodes refreshed in parallel" default:"8"`
|
2019-01-30 16:29:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
// Discovery struct loads on cache, kad
|
2018-12-14 19:23:54 +00:00
|
|
|
type Discovery struct {
|
2019-03-25 22:25:09 +00:00
|
|
|
log *zap.Logger
|
2019-08-06 17:35:59 +01:00
|
|
|
cache *overlay.Service
|
2019-03-25 22:25:09 +00:00
|
|
|
kad *kademlia.Kademlia
|
2019-01-30 20:29:33 +00:00
|
|
|
|
2019-07-12 15:35:48 +01:00
|
|
|
refreshLimit int
|
|
|
|
refreshConcurrency int
|
2019-02-08 09:25:13 +00:00
|
|
|
|
|
|
|
Refresh sync2.Cycle
|
|
|
|
Discovery sync2.Cycle
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new discovery service.
|
2019-08-06 17:35:59 +01:00
|
|
|
func New(logger *zap.Logger, ol *overlay.Service, kad *kademlia.Kademlia, config Config) *Discovery {
|
2019-02-08 09:25:13 +00:00
|
|
|
discovery := &Discovery{
|
2019-03-25 22:25:09 +00:00
|
|
|
log: logger,
|
|
|
|
cache: ol,
|
|
|
|
kad: kad,
|
2019-01-30 20:29:33 +00:00
|
|
|
|
2019-07-12 15:35:48 +01:00
|
|
|
refreshLimit: config.RefreshLimit,
|
|
|
|
refreshConcurrency: config.RefreshConcurrency,
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
2018-12-14 19:23:54 +00:00
|
|
|
|
2019-02-08 09:25:13 +00:00
|
|
|
discovery.Refresh.SetInterval(config.RefreshInterval)
|
|
|
|
discovery.Discovery.SetInterval(config.DiscoveryInterval)
|
|
|
|
|
|
|
|
return discovery
|
2018-12-14 19:23:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
// Close closes resources
|
2019-02-08 09:25:13 +00:00
|
|
|
func (discovery *Discovery) Close() error {
|
|
|
|
discovery.Refresh.Close()
|
|
|
|
discovery.Discovery.Close()
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-18 13:54:08 +00:00
|
|
|
|
|
|
|
// Run runs the discovery service
|
2019-06-04 12:36:27 +01:00
|
|
|
func (discovery *Discovery) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-02-08 09:25:13 +00:00
|
|
|
var group errgroup.Group
|
|
|
|
discovery.Refresh.Start(ctx, &group, func(ctx context.Context) error {
|
|
|
|
err := discovery.refresh(ctx)
|
|
|
|
if err != nil {
|
|
|
|
discovery.log.Error("error with cache refresh: ", zap.Error(err))
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
2019-02-08 09:25:13 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
discovery.Discovery.Start(ctx, &group, func(ctx context.Context) error {
|
|
|
|
err := discovery.discover(ctx)
|
|
|
|
if err != nil {
|
|
|
|
discovery.log.Error("error with cache discovery: ", zap.Error(err))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return group.Wait()
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:29:33 +00:00
|
|
|
// refresh updates the cache db with the current DHT.
|
2019-06-04 12:36:27 +01:00
|
|
|
func (discovery *Discovery) refresh(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-07-12 15:35:48 +01:00
|
|
|
limiter := sync2.NewLimiter(discovery.refreshConcurrency)
|
2019-01-30 16:29:18 +00:00
|
|
|
|
2019-07-12 15:35:48 +01:00
|
|
|
var offset int64
|
2019-01-30 16:29:18 +00:00
|
|
|
|
2019-07-12 15:35:48 +01:00
|
|
|
for {
|
|
|
|
list, more, err := discovery.cache.PaginateQualified(ctx, offset, discovery.refreshLimit)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
2019-02-06 12:25:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 15:35:48 +01:00
|
|
|
if len(list) == 0 {
|
|
|
|
break
|
2019-06-24 13:39:47 +01:00
|
|
|
}
|
|
|
|
|
2019-07-12 15:35:48 +01:00
|
|
|
offset += int64(len(list))
|
2019-07-09 03:10:18 +01:00
|
|
|
|
2019-07-12 15:35:48 +01:00
|
|
|
for _, node := range list {
|
|
|
|
node := node
|
2019-07-09 03:10:18 +01:00
|
|
|
|
2019-07-12 15:35:48 +01:00
|
|
|
limiter.Go(ctx, func() {
|
|
|
|
// NB: FetchInfo updates node uptime already
|
|
|
|
info, err := discovery.kad.FetchInfo(ctx, *node)
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-01-30 16:29:18 +00:00
|
|
|
|
2019-07-12 15:35:48 +01:00
|
|
|
if err != nil {
|
|
|
|
discovery.log.Info("could not ping node", zap.Stringer("ID", node.Id), zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = discovery.cache.UpdateNodeInfo(ctx, node.Id, info); err != nil {
|
|
|
|
discovery.log.Warn("could not update node info", zap.Stringer("ID", node.GetAddress()))
|
|
|
|
}
|
|
|
|
})
|
2019-03-01 17:46:34 +00:00
|
|
|
}
|
2019-07-12 15:35:48 +01:00
|
|
|
|
|
|
|
if !more {
|
|
|
|
break
|
2019-03-01 17:46:34 +00:00
|
|
|
}
|
2019-01-30 16:29:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 15:35:48 +01:00
|
|
|
limiter.Wait()
|
2018-12-14 19:23:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-17 16:31:14 +00:00
|
|
|
// Discovery runs lookups for random node ID's to find new nodes in the network
|
2019-06-04 12:36:27 +01:00
|
|
|
func (discovery *Discovery) discover(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-12-17 16:31:14 +00:00
|
|
|
r, err := randomID()
|
|
|
|
if err != nil {
|
2019-01-23 19:58:44 +00:00
|
|
|
return Error.Wrap(err)
|
2018-12-17 16:31:14 +00:00
|
|
|
}
|
2019-01-18 13:54:08 +00:00
|
|
|
_, err = discovery.kad.FindNode(ctx, r)
|
2018-12-18 20:01:15 +00:00
|
|
|
if err != nil && !kademlia.NodeNotFound.Has(err) {
|
2019-01-23 19:58:44 +00:00
|
|
|
return Error.Wrap(err)
|
2018-12-17 16:31:14 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomID() (storj.NodeID, error) {
|
|
|
|
b := make([]byte, 32)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
2019-01-23 19:58:44 +00:00
|
|
|
return storj.NodeID{}, Error.Wrap(err)
|
2018-12-17 16:31:14 +00:00
|
|
|
}
|
|
|
|
return storj.NodeIDFromBytes(b)
|
|
|
|
}
|