2019-09-04 19:29:34 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package contact
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-15 22:43:06 +00:00
|
|
|
"time"
|
2019-09-04 19:29:34 +01:00
|
|
|
|
2019-09-18 21:17:04 +01:00
|
|
|
"github.com/zeebo/errs"
|
2019-09-04 19:29:34 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/rpc/rpcstatus"
|
2019-09-19 19:37:31 +01:00
|
|
|
"storj.io/storj/satellite/overlay"
|
2019-09-04 19:29:34 +01:00
|
|
|
)
|
|
|
|
|
2019-10-30 18:57:21 +00:00
|
|
|
var (
|
|
|
|
errPingBackDial = errs.Class("pingback dialing error")
|
|
|
|
errCheckInIdentity = errs.Class("check-in identity error")
|
|
|
|
errCheckInNetwork = errs.Class("check-in network error")
|
|
|
|
)
|
|
|
|
|
2019-09-04 19:29:34 +01:00
|
|
|
// Endpoint implements the contact service Endpoints.
|
|
|
|
type Endpoint struct {
|
|
|
|
log *zap.Logger
|
|
|
|
service *Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEndpoint returns a new contact service endpoint.
|
|
|
|
func NewEndpoint(log *zap.Logger, service *Service) *Endpoint {
|
|
|
|
return &Endpoint{
|
|
|
|
log: log,
|
|
|
|
service: service,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 01:37:32 +01:00
|
|
|
// CheckIn is periodically called by storage nodes to keep the satellite informed of its existence,
|
2019-09-04 19:29:34 +01:00
|
|
|
// address, and operator information. In return, this satellite keeps the node informed of its
|
|
|
|
// reachability.
|
2019-09-14 01:37:32 +01:00
|
|
|
// When a node checks-in with the satellite, the satellite pings the node back to confirm they can
|
2019-09-10 17:05:07 +01:00
|
|
|
// successfully connect.
|
2019-09-14 01:37:32 +01:00
|
|
|
func (endpoint *Endpoint) CheckIn(ctx context.Context, req *pb.CheckInRequest) (_ *pb.CheckInResponse, err error) {
|
2019-09-04 19:29:34 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
peerID, err := identity.PeerIdentityFromContext(ctx)
|
2019-09-10 17:05:07 +01:00
|
|
|
if err != nil {
|
2019-10-30 18:57:21 +00:00
|
|
|
endpoint.log.Info("failed to get node ID from context", zap.String("node address", req.Address), zap.Error(err))
|
|
|
|
return nil, rpcstatus.Error(rpcstatus.Unknown, errCheckInIdentity.New("failed to get ID from context: %v", err).Error())
|
2019-09-10 17:05:07 +01:00
|
|
|
}
|
2019-09-12 17:33:04 +01:00
|
|
|
nodeID := peerID.ID
|
|
|
|
|
|
|
|
err = endpoint.service.peerIDs.Set(ctx, nodeID, peerID)
|
|
|
|
if err != nil {
|
2019-11-05 21:04:07 +00:00
|
|
|
endpoint.log.Info("failed to add peer identity entry for ID", zap.String("node address", req.Address), zap.Stringer("Node ID", nodeID), zap.Error(err))
|
2019-10-30 18:57:21 +00:00
|
|
|
return nil, rpcstatus.Error(rpcstatus.FailedPrecondition, errCheckInIdentity.New("failed to add peer identity entry for ID: %v", err).Error())
|
2019-09-12 17:33:04 +01:00
|
|
|
}
|
|
|
|
|
2019-09-19 19:37:31 +01:00
|
|
|
lastIP, err := overlay.GetNetwork(ctx, req.Address)
|
2019-09-10 17:05:07 +01:00
|
|
|
if err != nil {
|
2019-11-05 21:04:07 +00:00
|
|
|
endpoint.log.Info("failed to resolve IP from address", zap.String("node address", req.Address), zap.Stringer("Node ID", nodeID), zap.Error(err))
|
2019-10-30 18:57:21 +00:00
|
|
|
return nil, rpcstatus.Error(rpcstatus.InvalidArgument, errCheckInNetwork.New("failed to resolve IP from address: %s, err: %v", req.Address, err).Error())
|
2019-09-10 17:05:07 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 19:42:10 +00:00
|
|
|
pingNodeSuccess, pingErrorMessage, err := endpoint.service.PingBack(ctx, req.Address, nodeID)
|
2019-09-10 17:05:07 +01:00
|
|
|
if err != nil {
|
2019-11-05 21:04:07 +00:00
|
|
|
endpoint.log.Info("failed to ping back address", zap.String("node address", req.Address), zap.Stringer("Node ID", nodeID), zap.Error(err))
|
2019-10-30 18:57:21 +00:00
|
|
|
if errPingBackDial.Has(err) {
|
|
|
|
err = errCheckInNetwork.New("failed dialing address when attempting to ping node (ID: %s): %s, err: %v", nodeID, req.Address, err)
|
|
|
|
return nil, rpcstatus.Error(rpcstatus.NotFound, err.Error())
|
|
|
|
}
|
|
|
|
err = errCheckInNetwork.New("failed to ping node (ID: %s) at address: %s, err: %v", nodeID, req.Address, err)
|
|
|
|
return nil, rpcstatus.Error(rpcstatus.NotFound, err.Error())
|
2019-09-10 17:05:07 +01:00
|
|
|
}
|
2019-09-19 19:37:31 +01:00
|
|
|
nodeInfo := overlay.NodeCheckInInfo{
|
|
|
|
NodeID: peerID.ID,
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Address: req.Address,
|
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
|
|
|
},
|
|
|
|
LastIP: lastIP,
|
|
|
|
IsUp: pingNodeSuccess,
|
|
|
|
Capacity: req.Capacity,
|
|
|
|
Operator: req.Operator,
|
|
|
|
Version: req.Version,
|
2019-09-10 17:05:07 +01:00
|
|
|
}
|
2019-11-15 22:43:06 +00:00
|
|
|
err = endpoint.service.overlay.UpdateCheckIn(ctx, nodeInfo, time.Now().UTC())
|
2019-09-10 17:05:07 +01:00
|
|
|
if err != nil {
|
2019-11-05 21:04:07 +00:00
|
|
|
endpoint.log.Info("failed to update check in", zap.String("node address", req.Address), zap.Stringer("Node ID", nodeID), zap.Error(err))
|
2019-09-19 05:46:39 +01:00
|
|
|
return nil, rpcstatus.Error(rpcstatus.Internal, Error.Wrap(err).Error())
|
2019-09-10 17:05:07 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 18:57:21 +00:00
|
|
|
endpoint.log.Debug("checking in", zap.String("node addr", req.Address), zap.Bool("ping node success", pingNodeSuccess), zap.String("ping node err msg", pingErrorMessage))
|
2019-09-14 01:37:32 +01:00
|
|
|
return &pb.CheckInResponse{
|
2019-09-10 17:05:07 +01:00
|
|
|
PingNodeSuccess: pingNodeSuccess,
|
|
|
|
PingErrorMessage: pingErrorMessage,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-01-13 18:29:16 +00:00
|
|
|
|
|
|
|
// GetTime returns current timestamp
|
|
|
|
func (endpoint *Endpoint) GetTime(ctx context.Context, req *pb.GetTimeRequest) (_ *pb.GetTimeResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-01-10 01:58:59 +00:00
|
|
|
|
|
|
|
peerID, err := identity.PeerIdentityFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
endpoint.log.Info("failed to get node ID from context", zap.Error(err))
|
|
|
|
return nil, rpcstatus.Error(rpcstatus.Unauthenticated, errCheckInIdentity.New("failed to get ID from context: %v", err).Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
currentTimestamp := time.Now().UTC()
|
|
|
|
endpoint.log.Debug("get system current time", zap.Stringer("timestamp", currentTimestamp), zap.Stringer("node id", peerID.ID))
|
|
|
|
return &pb.GetTimeResponse{
|
|
|
|
Timestamp: currentTimestamp,
|
|
|
|
}, nil
|
2020-01-13 18:29:16 +00:00
|
|
|
}
|