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"
|
|
|
|
"storj.io/storj/pkg/overlay"
|
2018-12-17 16:31:14 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-02-04 21:52:06 +00:00
|
|
|
RefreshInterval time.Duration `help:"the interval at which the cache refreshes itself in seconds" default:"1s"`
|
|
|
|
GraveyardInterval time.Duration `help:"the interval at which the the graveyard tries to resurrect nodes" default:"30s"`
|
|
|
|
DiscoveryInterval time.Duration `help:"the interval at which the satellite attempts to find new nodes via random node ID lookups" default:"1s"`
|
|
|
|
RefreshLimit int `help:"the amount of nodes refreshed at each interval" default:"100"`
|
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
|
|
|
|
cache *overlay.Cache
|
|
|
|
kad *kademlia.Kademlia
|
2019-01-30 20:29:33 +00:00
|
|
|
|
|
|
|
// refreshOffset tracks the offset of the current refresh cycle
|
|
|
|
refreshOffset int64
|
2019-02-08 09:25:13 +00:00
|
|
|
refreshLimit int
|
|
|
|
|
|
|
|
Refresh sync2.Cycle
|
|
|
|
Graveyard sync2.Cycle
|
|
|
|
Discovery sync2.Cycle
|
2019-01-18 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new discovery service.
|
2019-03-25 22:25:09 +00:00
|
|
|
func New(logger *zap.Logger, ol *overlay.Cache, 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
|
|
|
|
|
|
|
refreshOffset: 0,
|
2019-02-08 09:25:13 +00:00
|
|
|
refreshLimit: config.RefreshLimit,
|
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.Graveyard.SetInterval(config.GraveyardInterval)
|
|
|
|
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.Graveyard.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.Graveyard.Start(ctx, &group, func(ctx context.Context) error {
|
|
|
|
err := discovery.searchGraveyard(ctx)
|
|
|
|
if err != nil {
|
|
|
|
discovery.log.Error("graveyard resurrection failed: ", zap.Error(err))
|
|
|
|
}
|
|
|
|
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.
|
2018-12-14 19:23:54 +00:00
|
|
|
// We currently do not penalize nodes that are unresponsive,
|
|
|
|
// but should in the future.
|
2019-06-04 12:36:27 +01:00
|
|
|
func (discovery *Discovery) refresh(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
nodes := discovery.kad.Seen()
|
2018-12-14 19:23:54 +00:00
|
|
|
for _, v := range nodes {
|
2019-01-18 13:54:08 +00:00
|
|
|
if err := discovery.cache.Put(ctx, v.Id, *v); err != nil {
|
2018-12-14 19:23:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 09:25:13 +00:00
|
|
|
list, more, err := discovery.cache.Paginate(ctx, discovery.refreshOffset, discovery.refreshLimit)
|
2019-01-30 16:29:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// more means there are more rows to page through in the cache
|
|
|
|
if more == false {
|
2019-01-30 20:29:33 +00:00
|
|
|
discovery.refreshOffset = 0
|
2019-01-30 16:29:18 +00:00
|
|
|
} else {
|
2019-05-29 14:14:25 +01:00
|
|
|
discovery.refreshOffset += int64(len(list))
|
2019-01-30 16:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range list {
|
2019-02-06 12:25:29 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
ping, err := discovery.kad.Ping(ctx, node.Node)
|
2019-01-30 16:29:18 +00:00
|
|
|
if err != nil {
|
2019-01-31 14:51:38 +00:00
|
|
|
discovery.log.Info("could not ping node", zap.String("ID", node.Id.String()), zap.Error(err))
|
2019-03-25 22:25:09 +00:00
|
|
|
_, err := discovery.cache.UpdateUptime(ctx, node.Id, false)
|
2019-01-30 16:29:18 +00:00
|
|
|
if err != nil {
|
2019-03-25 22:25:09 +00:00
|
|
|
discovery.log.Error("could not update node uptime in cache", zap.String("ID", node.Id.String()), zap.Error(err))
|
2019-01-30 21:15:24 +00:00
|
|
|
}
|
2019-01-30 16:29:18 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-02-06 12:25:29 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
_, err = discovery.cache.UpdateUptime(ctx, ping.Id, true)
|
2019-01-30 16:29:18 +00:00
|
|
|
if err != nil {
|
2019-03-25 22:25:09 +00:00
|
|
|
discovery.log.Error("could not update node uptime in cache", zap.String("ID", ping.Id.String()), zap.Error(err))
|
2019-01-30 16:29:18 +00:00
|
|
|
}
|
2019-03-01 17:46:34 +00:00
|
|
|
|
2019-03-12 20:05:18 +00:00
|
|
|
// update wallet with correct info
|
2019-04-04 17:34:36 +01:00
|
|
|
info, err := discovery.kad.FetchInfo(ctx, node.Node)
|
2019-03-01 17:46:34 +00:00
|
|
|
if err != nil {
|
|
|
|
discovery.log.Warn("could not fetch node info", zap.String("ID", ping.GetAddress().String()))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-04-10 07:04:24 +01:00
|
|
|
_, err = discovery.cache.UpdateNodeInfo(ctx, ping.Id, info)
|
2019-03-01 17:46:34 +00:00
|
|
|
if err != nil {
|
2019-04-22 10:07:50 +01:00
|
|
|
discovery.log.Warn("could not update node info", zap.String("ID", ping.GetAddress().String()))
|
2019-03-01 17:46:34 +00:00
|
|
|
}
|
2019-01-30 16:29:18 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 19:23:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-04 21:52:06 +00:00
|
|
|
// graveyard attempts to ping all nodes in the Seen() map from Kademlia and adds them to the cache
|
|
|
|
// if they respond. This is an attempt to resurrect nodes that may have gone offline in the last hour
|
|
|
|
// and were removed from the cache due to an unsuccessful response.
|
2019-06-04 12:36:27 +01:00
|
|
|
func (discovery *Discovery) searchGraveyard(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-02-04 21:52:06 +00:00
|
|
|
seen := discovery.kad.Seen()
|
|
|
|
|
2019-02-06 12:25:29 +00:00
|
|
|
var errors errs.Group
|
2019-02-04 21:52:06 +00:00
|
|
|
for _, n := range seen {
|
2019-02-06 12:25:29 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2019-02-04 21:52:06 +00:00
|
|
|
ping, err := discovery.kad.Ping(ctx, *n)
|
|
|
|
if err != nil {
|
|
|
|
discovery.log.Debug("could not ping node in graveyard check")
|
|
|
|
// we don't want to report the ping error to ErrorGroup because it's to be expected here.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-02-06 12:25:29 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2019-02-04 21:52:06 +00:00
|
|
|
err = discovery.cache.Put(ctx, ping.Id, ping)
|
|
|
|
if err != nil {
|
|
|
|
discovery.log.Warn("could not update node uptime")
|
|
|
|
errors.Add(err)
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
_, err = discovery.cache.UpdateUptime(ctx, ping.Id, true)
|
2019-02-04 21:52:06 +00:00
|
|
|
if err != nil {
|
|
|
|
discovery.log.Warn("could not update node uptime")
|
|
|
|
errors.Add(err)
|
|
|
|
}
|
|
|
|
}
|
2019-02-06 12:25:29 +00:00
|
|
|
return errors.Err()
|
2019-02-04 21:52:06 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|