2020-12-09 16:34:37 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package nodes
|
|
|
|
|
|
|
|
import (
|
2021-01-07 23:26:31 +00:00
|
|
|
"bytes"
|
2020-12-09 16:34:37 +00:00
|
|
|
"context"
|
2021-07-05 15:08:14 +01:00
|
|
|
"time"
|
2020-12-09 16:34:37 +00:00
|
|
|
|
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-12-26 01:16:43 +00:00
|
|
|
"storj.io/common/rpc"
|
2021-07-05 16:12:38 +01:00
|
|
|
"storj.io/common/rpc/rpcstatus"
|
2020-12-09 16:34:37 +00:00
|
|
|
"storj.io/common/storj"
|
2020-12-26 01:16:43 +00:00
|
|
|
"storj.io/storj/private/multinodepb"
|
2020-12-09 16:34:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
|
|
|
|
// Error is an error class for nodes service error.
|
2021-04-28 09:06:17 +01:00
|
|
|
Error = errs.Class("nodes")
|
2021-03-26 22:17:34 +00:00
|
|
|
// ErrNodeNotReachable is an error class that indicates that we are not able to establish drpc connection with node.
|
|
|
|
ErrNodeNotReachable = errs.Class("node is not reachable")
|
2021-07-05 16:12:38 +01:00
|
|
|
// ErrNodeAPIKeyInvalid is an error class that indicates that we uses wrong api key.
|
|
|
|
ErrNodeAPIKeyInvalid = errs.Class("node api key is invalid")
|
2020-12-09 16:34:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Service exposes all nodes related logic.
|
|
|
|
//
|
|
|
|
// architecture: Service
|
|
|
|
type Service struct {
|
2020-12-26 01:16:43 +00:00
|
|
|
log *zap.Logger
|
|
|
|
dialer rpc.Dialer
|
|
|
|
nodes DB
|
2020-12-09 16:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates new instance of Service.
|
2020-12-26 01:16:43 +00:00
|
|
|
func NewService(log *zap.Logger, dialer rpc.Dialer, nodes DB) *Service {
|
2020-12-09 16:34:37 +00:00
|
|
|
return &Service{
|
2020-12-26 01:16:43 +00:00
|
|
|
log: log,
|
|
|
|
dialer: dialer,
|
|
|
|
nodes: nodes,
|
2020-12-09 16:34:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds new node to the system.
|
2021-12-07 07:38:25 +00:00
|
|
|
func (service *Service) Add(ctx context.Context, node Node) (err error) {
|
2020-12-09 16:34:37 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2021-07-05 16:12:38 +01:00
|
|
|
|
|
|
|
// trying to connect to node to check its availability.
|
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
2021-12-07 07:38:25 +00:00
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
2021-07-05 16:12:38 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return ErrNodeNotReachable.Wrap(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
nodeClient := multinodepb.NewDRPCNodeClient(conn)
|
|
|
|
header := &multinodepb.RequestHeader{
|
2021-12-07 07:38:25 +00:00
|
|
|
ApiKey: node.APISecret,
|
2021-07-05 16:12:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// making test request to check node api key.
|
|
|
|
_, err = nodeClient.Version(ctx, &multinodepb.VersionRequest{Header: header})
|
|
|
|
if err != nil {
|
|
|
|
if rpcstatus.Code(err) == rpcstatus.Unauthenticated {
|
|
|
|
return ErrNodeAPIKeyInvalid.Wrap(err)
|
|
|
|
}
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2021-12-07 07:38:25 +00:00
|
|
|
return Error.Wrap(service.nodes.Add(ctx, node))
|
2020-12-09 16:34:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 13:13:42 +01:00
|
|
|
// List returns list of all nodes.
|
|
|
|
func (service *Service) List(ctx context.Context) (_ []Node, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
nodes, err := service.nodes.List(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
2020-11-20 12:40:32 +00:00
|
|
|
// UpdateName will update name of the specified node.
|
|
|
|
func (service *Service) UpdateName(ctx context.Context, id storj.NodeID, name string) (err error) {
|
2020-12-09 16:34:37 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-11-20 12:40:32 +00:00
|
|
|
return Error.Wrap(service.nodes.UpdateName(ctx, id, name))
|
2020-12-09 16:34:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 12:40:32 +00:00
|
|
|
// Get retrieves node by id.
|
|
|
|
func (service *Service) Get(ctx context.Context, id storj.NodeID) (_ Node, err error) {
|
2020-12-09 16:34:37 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-11-20 12:40:32 +00:00
|
|
|
|
2020-12-17 15:05:18 +00:00
|
|
|
node, err := service.nodes.Get(ctx, id)
|
2020-11-20 12:40:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return Node{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes node from the system.
|
|
|
|
func (service *Service) Remove(ctx context.Context, id storj.NodeID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return Error.Wrap(service.nodes.Remove(ctx, id))
|
2020-12-09 16:34:37 +00:00
|
|
|
}
|
2020-12-26 01:16:43 +00:00
|
|
|
|
|
|
|
// ListInfos queries node basic info from all nodes via rpc.
|
|
|
|
func (service *Service) ListInfos(ctx context.Context) (_ []NodeInfo, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
nodes, err := service.nodes.List(ctx)
|
|
|
|
if err != nil {
|
2021-01-21 16:52:19 +00:00
|
|
|
if ErrNoNode.Has(err) {
|
|
|
|
return []NodeInfo{}, nil
|
|
|
|
}
|
2020-12-26 01:16:43 +00:00
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var infos []NodeInfo
|
|
|
|
for _, node := range nodes {
|
2021-07-16 14:07:56 +01:00
|
|
|
info := func() NodeInfo {
|
|
|
|
nodeInfo := NodeInfo{
|
|
|
|
ID: node.ID,
|
|
|
|
Name: node.Name,
|
|
|
|
}
|
2020-12-26 01:16:43 +00:00
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-07-16 14:07:56 +01:00
|
|
|
nodeInfo.Status = StatusNotReachable
|
|
|
|
return nodeInfo
|
2020-12-26 01:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
nodeClient := multinodepb.NewDRPCNodeClient(conn)
|
|
|
|
storageClient := multinodepb.NewDRPCStorageClient(conn)
|
|
|
|
bandwidthClient := multinodepb.NewDRPCBandwidthClient(conn)
|
2021-06-16 17:46:25 +01:00
|
|
|
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
|
2020-12-26 01:16:43 +00:00
|
|
|
|
|
|
|
header := &multinodepb.RequestHeader{
|
|
|
|
ApiKey: node.APISecret,
|
|
|
|
}
|
|
|
|
|
2021-01-05 07:59:22 +00:00
|
|
|
nodeVersion, err := nodeClient.Version(ctx, &multinodepb.VersionRequest{Header: header})
|
2020-12-26 01:16:43 +00:00
|
|
|
if err != nil {
|
2021-07-16 14:07:56 +01:00
|
|
|
if rpcstatus.Code(err) == rpcstatus.Unauthenticated {
|
|
|
|
nodeInfo.Status = StatusUnauthorized
|
|
|
|
return nodeInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeInfo.Status = StatusStorageNodeInternalError
|
|
|
|
return nodeInfo
|
2020-12-26 01:16:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 07:59:22 +00:00
|
|
|
lastContact, err := nodeClient.LastContact(ctx, &multinodepb.LastContactRequest{Header: header})
|
2020-12-26 01:16:43 +00:00
|
|
|
if err != nil {
|
2021-07-16 14:07:56 +01:00
|
|
|
// TODO: since rpcstatus.Unauthenticated was checked in nodeVersion this sort of error can be caused
|
|
|
|
// only if new apikey was issued during ListInfos method call.
|
|
|
|
nodeInfo.Status = StatusStorageNodeInternalError
|
|
|
|
return nodeInfo
|
2020-12-26 01:16:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 07:59:22 +00:00
|
|
|
diskSpace, err := storageClient.DiskSpace(ctx, &multinodepb.DiskSpaceRequest{Header: header})
|
2020-12-26 01:16:43 +00:00
|
|
|
if err != nil {
|
2021-07-16 14:07:56 +01:00
|
|
|
nodeInfo.Status = StatusStorageNodeInternalError
|
|
|
|
return nodeInfo
|
2020-12-26 01:16:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 19:17:46 +00:00
|
|
|
earned, err := payoutClient.Earned(ctx, &multinodepb.EarnedRequest{Header: header})
|
|
|
|
if err != nil {
|
2021-07-16 14:07:56 +01:00
|
|
|
nodeInfo.Status = StatusStorageNodeInternalError
|
|
|
|
return nodeInfo
|
2021-01-07 19:17:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 07:59:22 +00:00
|
|
|
bandwidthSummaryRequest := &multinodepb.BandwidthMonthSummaryRequest{
|
|
|
|
Header: header,
|
|
|
|
}
|
|
|
|
bandwidthSummary, err := bandwidthClient.MonthSummary(ctx, bandwidthSummaryRequest)
|
2020-12-26 01:16:43 +00:00
|
|
|
if err != nil {
|
2021-07-16 14:07:56 +01:00
|
|
|
nodeInfo.Status = StatusStorageNodeInternalError
|
|
|
|
return nodeInfo
|
2020-12-26 01:16:43 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 14:07:56 +01:00
|
|
|
nodeInfo.Version = nodeVersion.Version
|
|
|
|
nodeInfo.LastContact = lastContact.LastContact
|
|
|
|
nodeInfo.DiskSpaceUsed = diskSpace.GetUsedPieces() + diskSpace.GetUsedTrash()
|
|
|
|
nodeInfo.DiskSpaceLeft = diskSpace.GetAvailable()
|
|
|
|
nodeInfo.BandwidthUsed = bandwidthSummary.GetUsed()
|
|
|
|
nodeInfo.TotalEarned = earned.Total
|
|
|
|
nodeInfo.Status = nodeStatus(lastContact.LastContact)
|
|
|
|
|
|
|
|
return nodeInfo
|
2020-12-26 01:16:43 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
infos = append(infos, info)
|
|
|
|
}
|
|
|
|
|
|
|
|
return infos, nil
|
|
|
|
}
|
2021-01-05 07:59:22 +00:00
|
|
|
|
|
|
|
// ListInfosSatellite queries node satellite specific info from all nodes via rpc.
|
|
|
|
func (service *Service) ListInfosSatellite(ctx context.Context, satelliteID storj.NodeID) (_ []NodeInfoSatellite, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
nodes, err := service.nodes.List(ctx)
|
|
|
|
if err != nil {
|
2021-01-21 16:52:19 +00:00
|
|
|
if ErrNoNode.Has(err) {
|
|
|
|
return []NodeInfoSatellite{}, nil
|
|
|
|
}
|
2021-01-05 07:59:22 +00:00
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var infos []NodeInfoSatellite
|
|
|
|
for _, node := range nodes {
|
2021-07-16 14:07:56 +01:00
|
|
|
info := func() NodeInfoSatellite {
|
|
|
|
nodeInfoSatellite := NodeInfoSatellite{
|
|
|
|
ID: node.ID,
|
|
|
|
Name: node.Name,
|
|
|
|
}
|
2021-01-05 07:59:22 +00:00
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-07-16 14:07:56 +01:00
|
|
|
nodeInfoSatellite.Status = StatusNotReachable
|
|
|
|
return nodeInfoSatellite
|
2021-01-05 07:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
nodeClient := multinodepb.NewDRPCNodeClient(conn)
|
2021-06-16 17:46:25 +01:00
|
|
|
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
|
2021-01-05 07:59:22 +00:00
|
|
|
|
|
|
|
header := &multinodepb.RequestHeader{
|
|
|
|
ApiKey: node.APISecret,
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeVersion, err := nodeClient.Version(ctx, &multinodepb.VersionRequest{Header: header})
|
|
|
|
if err != nil {
|
2021-07-16 14:07:56 +01:00
|
|
|
if rpcstatus.Code(err) == rpcstatus.Unauthenticated {
|
|
|
|
nodeInfoSatellite.Status = StatusUnauthorized
|
|
|
|
return nodeInfoSatellite
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeInfoSatellite.Status = StatusStorageNodeInternalError
|
|
|
|
return nodeInfoSatellite
|
2021-01-05 07:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lastContact, err := nodeClient.LastContact(ctx, &multinodepb.LastContactRequest{Header: header})
|
|
|
|
if err != nil {
|
2021-07-16 14:07:56 +01:00
|
|
|
// TODO: since rpcstatus.Unauthenticated was checked in Version this sort of error can be caused
|
|
|
|
// only if new apikey was issued during ListInfosSatellite method call.
|
|
|
|
nodeInfoSatellite.Status = StatusStorageNodeInternalError
|
|
|
|
return nodeInfoSatellite
|
2021-01-05 07:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rep, err := nodeClient.Reputation(ctx, &multinodepb.ReputationRequest{
|
|
|
|
Header: header,
|
|
|
|
SatelliteId: satelliteID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-07-16 14:07:56 +01:00
|
|
|
nodeInfoSatellite.Status = StatusStorageNodeInternalError
|
|
|
|
return nodeInfoSatellite
|
2021-01-05 07:59:22 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 16:52:19 +00:00
|
|
|
earned, err := payoutClient.Earned(ctx, &multinodepb.EarnedRequest{Header: header})
|
|
|
|
if err != nil {
|
2021-07-16 14:07:56 +01:00
|
|
|
nodeInfoSatellite.Status = StatusStorageNodeInternalError
|
|
|
|
return nodeInfoSatellite
|
2021-01-21 16:52:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 14:07:56 +01:00
|
|
|
nodeInfoSatellite.Version = nodeVersion.Version
|
|
|
|
nodeInfoSatellite.LastContact = lastContact.LastContact
|
|
|
|
nodeInfoSatellite.OnlineScore = rep.Online.Score
|
|
|
|
nodeInfoSatellite.AuditScore = rep.Audit.Score
|
|
|
|
nodeInfoSatellite.SuspensionScore = rep.Audit.SuspensionScore
|
|
|
|
nodeInfoSatellite.TotalEarned = earned.Total
|
|
|
|
nodeInfoSatellite.Status = nodeStatus(lastContact.LastContact)
|
|
|
|
|
|
|
|
return nodeInfoSatellite
|
2021-01-05 07:59:22 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
infos = append(infos, info)
|
|
|
|
}
|
|
|
|
|
|
|
|
return infos, nil
|
|
|
|
}
|
2021-01-07 23:26:31 +00:00
|
|
|
|
|
|
|
// TrustedSatellites returns list of unique trusted satellites node urls.
|
|
|
|
func (service *Service) TrustedSatellites(ctx context.Context) (_ storj.NodeURLs, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2021-03-26 22:17:34 +00:00
|
|
|
listNodes, err := service.nodes.List(ctx)
|
2021-01-07 23:26:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var trustedSatellites storj.NodeURLs
|
2021-03-26 22:17:34 +00:00
|
|
|
for _, node := range listNodes {
|
2021-01-07 23:26:31 +00:00
|
|
|
nodeURLs, err := service.trustedSatellites(ctx, node)
|
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
if ErrNodeNotReachable.Has(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-07 23:26:31 +00:00
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
trustedSatellites = appendUniqueNodeURLs(trustedSatellites, nodeURLs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return trustedSatellites, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// trustedSatellites retrieves list of trusted satellites node urls for a node.
|
|
|
|
func (service *Service) trustedSatellites(ctx context.Context, node Node) (_ storj.NodeURLs, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
return storj.NodeURLs{}, ErrNodeNotReachable.Wrap(err)
|
2021-01-07 23:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
nodeClient := multinodepb.NewDRPCNodeClient(conn)
|
|
|
|
|
|
|
|
header := &multinodepb.RequestHeader{
|
|
|
|
ApiKey: node.APISecret,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := nodeClient.TrustedSatellites(ctx, &multinodepb.TrustedSatellitesRequest{Header: header})
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var nodeURLs storj.NodeURLs
|
|
|
|
for _, url := range resp.TrustedSatellites {
|
|
|
|
nodeURLs = append(nodeURLs, storj.NodeURL{
|
|
|
|
ID: url.NodeId,
|
|
|
|
Address: url.GetAddress(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeURLs, nil
|
|
|
|
}
|
|
|
|
|
2021-07-05 15:08:14 +01:00
|
|
|
// nodeStatus chooses node status offline or online depends on LastContact.
|
|
|
|
func nodeStatus(lastContact time.Time) Status {
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
|
|
|
if now.Sub(lastContact) < time.Hour*3 {
|
|
|
|
return StatusOnline
|
|
|
|
}
|
|
|
|
|
|
|
|
return StatusOffline
|
|
|
|
}
|
|
|
|
|
2021-01-07 23:26:31 +00:00
|
|
|
// appendUniqueNodeURLs appends unique node urls from incoming slice.
|
|
|
|
func appendUniqueNodeURLs(slice storj.NodeURLs, nodeURLs storj.NodeURLs) storj.NodeURLs {
|
|
|
|
for _, nodeURL := range nodeURLs {
|
|
|
|
slice = appendUniqueNodeURL(slice, nodeURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
return slice
|
|
|
|
}
|
|
|
|
|
|
|
|
// appendUniqueNodeURL appends node url if it is unique.
|
|
|
|
func appendUniqueNodeURL(slice storj.NodeURLs, nodeURL storj.NodeURL) storj.NodeURLs {
|
|
|
|
for _, existing := range slice {
|
|
|
|
if bytes.Equal(existing.ID.Bytes(), nodeURL.ID.Bytes()) {
|
|
|
|
return slice
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
slice = append(slice, nodeURL)
|
|
|
|
return slice
|
|
|
|
}
|