2019-06-25 11:40:32 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package nodestats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-06-25 11:40:32 +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-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/satellite/accounting"
|
|
|
|
"storj.io/storj/satellite/overlay"
|
2020-04-08 11:19:06 +01:00
|
|
|
"storj.io/storj/satellite/payments/paymentsconfig"
|
2021-07-07 20:20:23 +01:00
|
|
|
"storj.io/storj/satellite/reputation"
|
2019-06-25 11:40:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// Endpoint for querying node stats for the SNO.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Endpoint
|
2019-06-25 11:40:32 +01:00
|
|
|
type Endpoint struct {
|
2021-03-29 09:58:04 +01:00
|
|
|
pb.DRPCNodeStatsUnimplementedServer
|
|
|
|
|
2019-07-02 11:42:09 +01:00
|
|
|
log *zap.Logger
|
|
|
|
overlay overlay.DB
|
2021-08-02 18:48:55 +01:00
|
|
|
reputation *reputation.Service
|
2019-07-02 11:42:09 +01:00
|
|
|
accounting accounting.StoragenodeAccounting
|
2020-04-08 11:19:06 +01:00
|
|
|
config paymentsconfig.Config
|
2019-06-25 11:40:32 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// NewEndpoint creates new endpoint.
|
2021-08-02 18:48:55 +01:00
|
|
|
func NewEndpoint(log *zap.Logger, overlay overlay.DB, reputation *reputation.Service, accounting accounting.StoragenodeAccounting, config paymentsconfig.Config) *Endpoint {
|
2019-06-25 11:40:32 +01:00
|
|
|
return &Endpoint{
|
2019-07-02 11:42:09 +01:00
|
|
|
log: log,
|
|
|
|
overlay: overlay,
|
2021-07-07 20:20:23 +01:00
|
|
|
reputation: reputation,
|
2019-07-02 11:42:09 +01:00
|
|
|
accounting: accounting,
|
2020-04-08 11:19:06 +01:00
|
|
|
config: config,
|
2019-06-25 11:40:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// GetStats sends node stats for client node.
|
2019-07-08 15:33:43 +01:00
|
|
|
func (e *Endpoint) GetStats(ctx context.Context, req *pb.GetStatsRequest) (_ *pb.GetStatsResponse, err error) {
|
2019-07-04 11:41:40 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
peer, err := identity.PeerIdentityFromContext(ctx)
|
|
|
|
if err != nil {
|
2019-09-19 05:46:39 +01:00
|
|
|
return nil, rpcstatus.Error(rpcstatus.Unauthenticated, err.Error())
|
2019-07-04 11:41:40 +01:00
|
|
|
}
|
|
|
|
node, err := e.overlay.Get(ctx, peer.ID)
|
|
|
|
if err != nil {
|
2019-08-20 10:26:55 +01:00
|
|
|
if overlay.ErrNodeNotFound.Has(err) {
|
2020-07-14 14:31:02 +01:00
|
|
|
return nil, nil
|
2019-08-20 10:26:55 +01:00
|
|
|
}
|
|
|
|
e.log.Error("overlay.Get failed", zap.Error(err))
|
2019-09-19 05:46:39 +01:00
|
|
|
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
|
2019-07-04 11:41:40 +01:00
|
|
|
}
|
2021-07-07 20:20:23 +01:00
|
|
|
reputationInfo, err := e.reputation.Get(ctx, peer.ID)
|
|
|
|
if err != nil {
|
2021-08-02 18:48:55 +01:00
|
|
|
e.log.Error("reputation.Get failed", zap.Error(err))
|
|
|
|
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
|
2020-12-11 21:15:17 +00:00
|
|
|
}
|
2019-07-04 11:41:40 +01:00
|
|
|
|
2019-07-08 15:33:43 +01:00
|
|
|
auditScore := calculateReputationScore(
|
2021-07-07 20:20:23 +01:00
|
|
|
reputationInfo.AuditReputationAlpha,
|
|
|
|
reputationInfo.AuditReputationBeta)
|
2019-07-08 15:33:43 +01:00
|
|
|
|
2020-05-18 11:01:34 +01:00
|
|
|
unknownScore := calculateReputationScore(
|
2021-07-07 20:20:23 +01:00
|
|
|
reputationInfo.UnknownAuditReputationAlpha,
|
|
|
|
reputationInfo.UnknownAuditReputationBeta)
|
2020-05-18 11:01:34 +01:00
|
|
|
|
2019-07-08 15:33:43 +01:00
|
|
|
return &pb.GetStatsResponse{
|
|
|
|
AuditCheck: &pb.ReputationStats{
|
2021-07-07 20:20:23 +01:00
|
|
|
TotalCount: reputationInfo.TotalAuditCount,
|
|
|
|
SuccessCount: reputationInfo.AuditSuccessCount,
|
|
|
|
ReputationAlpha: reputationInfo.AuditReputationAlpha,
|
|
|
|
ReputationBeta: reputationInfo.AuditReputationBeta,
|
|
|
|
UnknownReputationAlpha: reputationInfo.UnknownAuditReputationAlpha,
|
|
|
|
UnknownReputationBeta: reputationInfo.UnknownAuditReputationBeta,
|
2020-05-03 17:30:54 +01:00
|
|
|
ReputationScore: auditScore,
|
2020-05-18 11:01:34 +01:00
|
|
|
UnknownReputationScore: unknownScore,
|
2019-07-08 15:33:43 +01:00
|
|
|
},
|
2021-07-07 20:20:23 +01:00
|
|
|
OnlineScore: reputationInfo.OnlineScore,
|
|
|
|
Disqualified: reputationInfo.Disqualified,
|
|
|
|
Suspended: reputationInfo.UnknownAuditSuspended,
|
|
|
|
OfflineSuspended: reputationInfo.OfflineSuspended,
|
|
|
|
OfflineUnderReview: reputationInfo.UnderReview,
|
|
|
|
VettedAt: reputationInfo.VettedAt,
|
|
|
|
AuditHistory: reputation.AuditHistoryToPB(reputationInfo.AuditHistory),
|
2020-09-02 16:37:54 +01:00
|
|
|
JoinedAt: node.CreatedAt,
|
2019-06-25 11:40:32 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// DailyStorageUsage returns slice of daily storage usage for given period of time sorted in ASC order by date.
|
2019-07-02 11:42:09 +01:00
|
|
|
func (e *Endpoint) DailyStorageUsage(ctx context.Context, req *pb.DailyStorageUsageRequest) (_ *pb.DailyStorageUsageResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
peer, err := identity.PeerIdentityFromContext(ctx)
|
|
|
|
if err != nil {
|
2019-09-19 05:46:39 +01:00
|
|
|
return nil, rpcstatus.Error(rpcstatus.Unauthenticated, err.Error())
|
2019-07-02 11:42:09 +01:00
|
|
|
}
|
|
|
|
node, err := e.overlay.Get(ctx, peer.ID)
|
|
|
|
if err != nil {
|
2019-08-20 10:26:55 +01:00
|
|
|
if overlay.ErrNodeNotFound.Has(err) {
|
2020-07-14 14:31:02 +01:00
|
|
|
return nil, nil
|
2019-08-20 10:26:55 +01:00
|
|
|
}
|
|
|
|
e.log.Error("overlay.Get failed", zap.Error(err))
|
2019-09-19 05:46:39 +01:00
|
|
|
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
|
2019-07-02 11:42:09 +01:00
|
|
|
}
|
|
|
|
|
2019-08-08 14:47:04 +01:00
|
|
|
nodeSpaceUsages, err := e.accounting.QueryStorageNodeUsage(ctx, node.Id, req.GetFrom(), req.GetTo())
|
2019-07-02 11:42:09 +01:00
|
|
|
if err != nil {
|
2019-08-20 10:26:55 +01:00
|
|
|
e.log.Error("accounting.QueryStorageNodeUsage failed", zap.Error(err))
|
2019-09-19 05:46:39 +01:00
|
|
|
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
|
2019-07-02 11:42:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.DailyStorageUsageResponse{
|
|
|
|
NodeId: node.Id,
|
2019-08-08 14:47:04 +01:00
|
|
|
DailyStorageUsage: toProtoDailyStorageUsage(nodeSpaceUsages),
|
2019-07-02 11:42:09 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:19:06 +01:00
|
|
|
// PricingModel returns pricing model for storagenode.
|
|
|
|
func (e *Endpoint) PricingModel(ctx context.Context, req *pb.PricingModelRequest) (_ *pb.PricingModelResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
return &pb.PricingModelResponse{
|
|
|
|
EgressBandwidthPrice: e.config.NodeEgressBandwidthPrice,
|
|
|
|
RepairBandwidthPrice: e.config.NodeRepairBandwidthPrice,
|
|
|
|
DiskSpacePrice: e.config.NodeDiskSpacePrice,
|
|
|
|
AuditBandwidthPrice: e.config.NodeAuditBandwidthPrice,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// toProtoDailyStorageUsage converts StorageNodeUsage to PB DailyStorageUsageResponse_StorageUsage.
|
2019-08-08 14:47:04 +01:00
|
|
|
func toProtoDailyStorageUsage(usages []accounting.StorageNodeUsage) []*pb.DailyStorageUsageResponse_StorageUsage {
|
2019-07-02 11:42:09 +01:00
|
|
|
var pbUsages []*pb.DailyStorageUsageResponse_StorageUsage
|
|
|
|
|
|
|
|
for _, usage := range usages {
|
|
|
|
pbUsages = append(pbUsages, &pb.DailyStorageUsageResponse_StorageUsage{
|
2019-08-08 14:47:04 +01:00
|
|
|
AtRestTotal: usage.StorageUsed,
|
|
|
|
Timestamp: usage.Timestamp,
|
2019-07-02 11:42:09 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return pbUsages
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// calculateReputationScore is helper method to calculate reputation score value.
|
2019-07-04 11:41:40 +01:00
|
|
|
func calculateReputationScore(alpha, beta float64) float64 {
|
2019-06-25 11:40:32 +01:00
|
|
|
return alpha / (alpha + beta)
|
|
|
|
}
|