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"
|
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"
|
2019-09-19 20:56:34 +01:00
|
|
|
"storj.io/storj/satellite/contact"
|
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"`
|
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
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Chore
|
2018-12-14 19:23:54 +00:00
|
|
|
type Discovery struct {
|
2019-09-19 20:56:34 +01:00
|
|
|
log *zap.Logger
|
|
|
|
cache *overlay.Service
|
|
|
|
contact *contact.Service
|
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
|
|
|
|
2019-09-19 20:56:34 +01:00
|
|
|
Refresh sync2.Cycle
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new discovery service.
|
2019-09-19 20:56:34 +01:00
|
|
|
func New(logger *zap.Logger, ol *overlay.Service, contact *contact.Service, config Config) *Discovery {
|
2019-02-08 09:25:13 +00:00
|
|
|
discovery := &Discovery{
|
2019-09-19 20:56:34 +01:00
|
|
|
log: logger,
|
|
|
|
cache: ol,
|
|
|
|
contact: contact,
|
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)
|
|
|
|
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()
|
|
|
|
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
|
|
|
|
})
|
|
|
|
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() {
|
2019-09-19 20:56:34 +01:00
|
|
|
info, err := discovery.contact.FetchInfo(ctx, *node)
|
2019-07-12 15:35:48 +01:00
|
|
|
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
|
|
|
|
}
|