2019-06-26 19:55:22 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package nodestats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-07-02 13:05:58 +01:00
|
|
|
"time"
|
2019-06-26 19:55:22 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/rpc"
|
|
|
|
"storj.io/common/storj"
|
2019-08-08 14:47:04 +01:00
|
|
|
"storj.io/storj/storagenode/reputation"
|
|
|
|
"storj.io/storj/storagenode/storageusage"
|
|
|
|
"storj.io/storj/storagenode/trust"
|
2019-06-26 19:55:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// NodeStatsServiceErr defines node stats service error
|
|
|
|
NodeStatsServiceErr = errs.Class("node stats service error")
|
|
|
|
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
|
|
|
|
2019-07-04 11:34:23 +01:00
|
|
|
// Client encapsulates NodeStatsClient with underlying connection
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Client
|
2019-07-04 11:34:23 +01:00
|
|
|
type Client struct {
|
2019-09-19 05:46:39 +01:00
|
|
|
conn *rpc.Conn
|
2019-12-22 14:52:55 +00:00
|
|
|
pb.DRPCNodeStatsClient
|
2019-07-04 11:34:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes underlying client connection
|
|
|
|
func (c *Client) Close() error {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
// Service retrieves info from satellites using an rpc client
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2019-06-26 19:55:22 +01:00
|
|
|
type Service struct {
|
|
|
|
log *zap.Logger
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
dialer rpc.Dialer
|
|
|
|
trust *trust.Pool
|
2019-06-26 19:55:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates new instance of service
|
2019-09-19 05:46:39 +01:00
|
|
|
func NewService(log *zap.Logger, dialer rpc.Dialer, trust *trust.Pool) *Service {
|
2019-06-26 19:55:22 +01:00
|
|
|
return &Service{
|
2019-09-19 05:46:39 +01:00
|
|
|
log: log,
|
|
|
|
dialer: dialer,
|
|
|
|
trust: trust,
|
2019-06-26 19:55:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 14:47:04 +01:00
|
|
|
// GetReputationStats retrieves reputation stats from particular satellite
|
|
|
|
func (s *Service) GetReputationStats(ctx context.Context, satelliteID storj.NodeID) (_ *reputation.Stats, err error) {
|
2019-06-26 19:55:22 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-08-08 14:47:04 +01:00
|
|
|
client, err := s.dial(ctx, satelliteID)
|
2019-06-26 19:55:22 +01:00
|
|
|
if err != nil {
|
2019-07-02 13:05:58 +01:00
|
|
|
return nil, NodeStatsServiceErr.Wrap(err)
|
2019-06-26 19:55:22 +01:00
|
|
|
}
|
2019-09-19 05:46:39 +01:00
|
|
|
defer func() { err = errs.Combine(err, client.Close()) }()
|
2019-07-04 11:34:23 +01:00
|
|
|
|
2019-07-08 15:33:43 +01:00
|
|
|
resp, err := client.GetStats(ctx, &pb.GetStatsRequest{})
|
2019-06-26 19:55:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, NodeStatsServiceErr.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-07-08 15:33:43 +01:00
|
|
|
uptime := resp.GetUptimeCheck()
|
|
|
|
audit := resp.GetAuditCheck()
|
|
|
|
|
2019-08-08 14:47:04 +01:00
|
|
|
return &reputation.Stats{
|
2019-07-08 15:33:43 +01:00
|
|
|
SatelliteID: satelliteID,
|
2019-08-08 14:47:04 +01:00
|
|
|
Uptime: reputation.Metric{
|
|
|
|
TotalCount: uptime.GetTotalCount(),
|
|
|
|
SuccessCount: uptime.GetSuccessCount(),
|
|
|
|
Alpha: uptime.GetReputationAlpha(),
|
|
|
|
Beta: uptime.GetReputationBeta(),
|
|
|
|
Score: uptime.GetReputationScore(),
|
2019-07-08 15:33:43 +01:00
|
|
|
},
|
2019-08-08 14:47:04 +01:00
|
|
|
Audit: reputation.Metric{
|
|
|
|
TotalCount: audit.GetTotalCount(),
|
|
|
|
SuccessCount: audit.GetSuccessCount(),
|
|
|
|
Alpha: audit.GetReputationAlpha(),
|
|
|
|
Beta: audit.GetReputationBeta(),
|
|
|
|
Score: audit.GetReputationScore(),
|
2019-07-08 15:33:43 +01:00
|
|
|
},
|
2019-08-28 21:54:12 +01:00
|
|
|
Disqualified: resp.GetDisqualified(),
|
|
|
|
UpdatedAt: time.Now(),
|
2019-06-26 19:55:22 +01:00
|
|
|
}, nil
|
|
|
|
}
|
2019-07-02 13:05:58 +01:00
|
|
|
|
2019-08-08 14:47:04 +01:00
|
|
|
// GetDailyStorageUsage returns daily storage usage over a period of time for a particular satellite
|
|
|
|
func (s *Service) GetDailyStorageUsage(ctx context.Context, satelliteID storj.NodeID, from, to time.Time) (_ []storageusage.Stamp, err error) {
|
2019-07-02 13:05:58 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-08-08 14:47:04 +01:00
|
|
|
client, err := s.dial(ctx, satelliteID)
|
2019-07-02 13:05:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, NodeStatsServiceErr.Wrap(err)
|
|
|
|
}
|
2019-09-19 05:46:39 +01:00
|
|
|
defer func() { err = errs.Combine(err, client.Close()) }()
|
2019-07-04 11:34:23 +01:00
|
|
|
|
2019-08-08 14:47:04 +01:00
|
|
|
resp, err := client.DailyStorageUsage(ctx, &pb.DailyStorageUsageRequest{From: from, To: to})
|
2019-07-02 13:05:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, NodeStatsServiceErr.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fromSpaceUsageResponse(resp, satelliteID), nil
|
|
|
|
}
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
// dial dials the NodeStats client for the satellite by id
|
2019-08-08 14:47:04 +01:00
|
|
|
func (s *Service) dial(ctx context.Context, satelliteID storj.NodeID) (_ *Client, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
address, err := s.trust.GetAddress(ctx, satelliteID)
|
2019-07-02 13:05:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.New("unable to find satellite %s: %v", satelliteID, err)
|
|
|
|
}
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
conn, err := s.dialer.DialAddressID(ctx, address, satelliteID)
|
2019-07-02 13:05:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.New("unable to connect to the satellite %s: %v", satelliteID, err)
|
|
|
|
}
|
|
|
|
|
2019-07-04 11:34:23 +01:00
|
|
|
return &Client{
|
2019-12-22 14:52:55 +00:00
|
|
|
conn: conn,
|
2019-12-22 15:07:50 +00:00
|
|
|
DRPCNodeStatsClient: pb.NewDRPCNodeStatsClient(conn.Raw()),
|
2019-07-04 11:34:23 +01:00
|
|
|
}, nil
|
2019-07-02 13:05:58 +01:00
|
|
|
}
|
|
|
|
|
2019-08-08 14:47:04 +01:00
|
|
|
// fromSpaceUsageResponse get DiskSpaceUsage slice from pb.SpaceUsageResponse
|
|
|
|
func fromSpaceUsageResponse(resp *pb.DailyStorageUsageResponse, satelliteID storj.NodeID) []storageusage.Stamp {
|
|
|
|
var stamps []storageusage.Stamp
|
2019-07-02 13:05:58 +01:00
|
|
|
|
|
|
|
for _, pbUsage := range resp.GetDailyStorageUsage() {
|
2019-08-08 14:47:04 +01:00
|
|
|
stamps = append(stamps, storageusage.Stamp{
|
2019-09-04 15:13:43 +01:00
|
|
|
SatelliteID: satelliteID,
|
|
|
|
AtRestTotal: pbUsage.AtRestTotal,
|
|
|
|
IntervalStart: pbUsage.Timestamp,
|
2019-07-02 13:05:58 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return stamps
|
|
|
|
}
|