2019-09-04 19:29:34 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package contact
|
|
|
|
|
|
|
|
import (
|
2019-12-30 19:42:10 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-09-19 20:56:34 +01:00
|
|
|
"sync"
|
2020-04-02 21:44:51 +01:00
|
|
|
"time"
|
2019-09-19 20:56:34 +01:00
|
|
|
|
2019-09-10 17:05:07 +01:00
|
|
|
"github.com/zeebo/errs"
|
2019-09-04 19:29:34 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-12-30 19:42:10 +00:00
|
|
|
"storj.io/common/pb"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/rpc"
|
2019-12-30 19:42:10 +00:00
|
|
|
"storj.io/common/rpc/rpcstatus"
|
|
|
|
"storj.io/common/storj"
|
2019-09-04 19:29:34 +01:00
|
|
|
"storj.io/storj/satellite/overlay"
|
|
|
|
)
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Config contains configurable values for contact service.
|
2019-09-19 20:56:34 +01:00
|
|
|
type Config struct {
|
2020-04-02 21:44:51 +01:00
|
|
|
ExternalAddress string `user:"true" help:"the public address of the node, useful for nodes behind NAT" default:""`
|
|
|
|
Timeout time.Duration `help:"timeout for pinging storage nodes" default:"10m0s"`
|
2019-09-19 20:56:34 +01:00
|
|
|
}
|
|
|
|
|
2019-09-12 17:33:04 +01:00
|
|
|
// Service is the contact service between storage nodes and satellites.
|
|
|
|
// It is responsible for updating general node information like address, capacity, and uptime.
|
|
|
|
// It is also responsible for updating peer identity information for verifying signatures from that node.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2019-09-04 19:29:34 +01:00
|
|
|
type Service struct {
|
2019-09-19 20:56:34 +01:00
|
|
|
log *zap.Logger
|
|
|
|
|
|
|
|
mutex sync.Mutex
|
|
|
|
self *overlay.NodeDossier
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
overlay *overlay.Service
|
|
|
|
peerIDs overlay.PeerIdentities
|
|
|
|
dialer rpc.Dialer
|
2020-04-02 21:44:51 +01:00
|
|
|
|
|
|
|
timeout time.Duration
|
2019-09-04 19:29:34 +01:00
|
|
|
}
|
|
|
|
|
2019-09-12 17:33:04 +01:00
|
|
|
// NewService creates a new contact service.
|
2020-04-02 21:44:51 +01:00
|
|
|
func NewService(log *zap.Logger, self *overlay.NodeDossier, overlay *overlay.Service, peerIDs overlay.PeerIdentities, dialer rpc.Dialer, timeout time.Duration) *Service {
|
2019-09-04 19:29:34 +01:00
|
|
|
return &Service{
|
2019-09-19 05:46:39 +01:00
|
|
|
log: log,
|
|
|
|
self: self,
|
|
|
|
overlay: overlay,
|
|
|
|
peerIDs: peerIDs,
|
|
|
|
dialer: dialer,
|
2020-04-02 21:44:51 +01:00
|
|
|
timeout: timeout,
|
2019-09-04 19:29:34 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-19 20:56:34 +01:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Local returns the satellite node dossier.
|
2019-09-19 20:56:34 +01:00
|
|
|
func (service *Service) Local() overlay.NodeDossier {
|
|
|
|
service.mutex.Lock()
|
|
|
|
defer service.mutex.Unlock()
|
|
|
|
return *service.self
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Close closes resources.
|
2019-09-19 20:56:34 +01:00
|
|
|
func (service *Service) Close() error { return nil }
|
2019-12-30 19:42:10 +00:00
|
|
|
|
|
|
|
// PingBack pings the node to test connectivity.
|
2020-05-19 17:42:00 +01:00
|
|
|
func (service *Service) PingBack(ctx context.Context, nodeurl storj.NodeURL) (_ bool, _ string, err error) {
|
2019-12-30 19:42:10 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-04-02 21:44:51 +01:00
|
|
|
if service.timeout > 0 {
|
|
|
|
var cancel func()
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, service.timeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
|
2019-12-30 19:42:10 +00:00
|
|
|
pingNodeSuccess := true
|
|
|
|
var pingErrorMessage string
|
|
|
|
|
2020-05-19 17:42:00 +01:00
|
|
|
client, err := dialNodeURL(ctx, service.dialer, nodeurl)
|
2019-12-30 19:42:10 +00:00
|
|
|
if err != nil {
|
|
|
|
// If there is an error from trying to dial and ping the node, return that error as
|
|
|
|
// pingErrorMessage and not as the err. We want to use this info to update
|
|
|
|
// node contact info and do not want to terminate execution by returning an err
|
|
|
|
mon.Event("failed dial")
|
|
|
|
pingNodeSuccess = false
|
2020-05-19 16:09:00 +01:00
|
|
|
pingErrorMessage = fmt.Sprintf("failed to dial storage node (ID: %s) at address %s: %q",
|
2020-05-19 17:42:00 +01:00
|
|
|
nodeurl.ID, nodeurl.Address, err,
|
2020-05-19 16:09:00 +01:00
|
|
|
)
|
|
|
|
service.log.Debug("pingBack failed to dial storage node",
|
|
|
|
zap.String("pingErrorMessage", pingErrorMessage),
|
|
|
|
)
|
2019-12-30 19:42:10 +00:00
|
|
|
return pingNodeSuccess, pingErrorMessage, nil
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, client.Close()) }()
|
|
|
|
|
|
|
|
_, err = client.pingNode(ctx, &pb.ContactPingRequest{})
|
|
|
|
if err != nil {
|
|
|
|
mon.Event("failed ping node")
|
|
|
|
pingNodeSuccess = false
|
|
|
|
pingErrorMessage = fmt.Sprintf("failed to ping storage node, your node indicated error code: %d, %q", rpcstatus.Code(err), err)
|
2020-05-19 16:09:00 +01:00
|
|
|
service.log.Debug("pingBack pingNode error",
|
2020-05-19 17:42:00 +01:00
|
|
|
zap.Stringer("Node ID", nodeurl.ID),
|
2020-05-19 16:09:00 +01:00
|
|
|
zap.String("pingErrorMessage", pingErrorMessage),
|
|
|
|
)
|
2019-12-30 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pingNodeSuccess, pingErrorMessage, nil
|
|
|
|
}
|