2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package inspector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-07-06 14:40:58 +01:00
|
|
|
"net"
|
2019-03-18 10:55:06 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-03-18 10:55:06 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/pb"
|
2020-01-14 11:46:15 +00:00
|
|
|
"storj.io/common/rpc/rpcstatus"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/storagenode/bandwidth"
|
2019-09-11 21:41:43 +01:00
|
|
|
"storj.io/storj/storagenode/contact"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/storagenode/pieces"
|
2019-04-26 06:17:18 +01:00
|
|
|
"storj.io/storj/storagenode/piecestore"
|
2019-03-18 10:55:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
|
|
|
|
// Error is the default error class for piecestore monitor errors
|
|
|
|
Error = errs.Class("piecestore inspector")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Endpoint does inspectory things
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Endpoint
|
2019-03-18 10:55:06 +00:00
|
|
|
type Endpoint struct {
|
2019-08-08 02:47:30 +01:00
|
|
|
log *zap.Logger
|
|
|
|
pieceStore *pieces.Store
|
2019-09-19 20:56:34 +01:00
|
|
|
contact *contact.Service
|
2019-09-11 21:41:43 +01:00
|
|
|
pingStats *contact.PingStats
|
2019-08-08 02:47:30 +01:00
|
|
|
usageDB bandwidth.DB
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-07-06 14:40:58 +01:00
|
|
|
startTime time.Time
|
|
|
|
pieceStoreConfig piecestore.OldConfig
|
|
|
|
dashboardAddress net.Addr
|
2019-09-19 20:56:34 +01:00
|
|
|
externalAddress string
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEndpoint creates piecestore inspector instance
|
2019-07-06 14:40:58 +01:00
|
|
|
func NewEndpoint(
|
|
|
|
log *zap.Logger,
|
2019-08-08 02:47:30 +01:00
|
|
|
pieceStore *pieces.Store,
|
2019-09-19 20:56:34 +01:00
|
|
|
contact *contact.Service,
|
2019-09-11 21:41:43 +01:00
|
|
|
pingStats *contact.PingStats,
|
2019-07-06 14:40:58 +01:00
|
|
|
usageDB bandwidth.DB,
|
|
|
|
pieceStoreConfig piecestore.OldConfig,
|
2020-01-14 11:46:15 +00:00
|
|
|
dashboardAddress net.Addr,
|
2019-09-19 20:56:34 +01:00
|
|
|
externalAddress string) *Endpoint {
|
2019-07-06 14:40:58 +01:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
return &Endpoint{
|
2019-07-06 14:40:58 +01:00
|
|
|
log: log,
|
2019-08-08 02:47:30 +01:00
|
|
|
pieceStore: pieceStore,
|
2019-09-19 20:56:34 +01:00
|
|
|
contact: contact,
|
2019-09-11 21:41:43 +01:00
|
|
|
pingStats: pingStats,
|
2019-07-06 14:40:58 +01:00
|
|
|
usageDB: usageDB,
|
|
|
|
pieceStoreConfig: pieceStoreConfig,
|
2020-01-14 11:46:15 +00:00
|
|
|
dashboardAddress: dashboardAddress,
|
2019-07-06 14:40:58 +01:00
|
|
|
startTime: time.Now(),
|
2019-09-19 20:56:34 +01:00
|
|
|
externalAddress: externalAddress,
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 11:46:15 +00:00
|
|
|
// Stats returns current statistics about the storage node
|
|
|
|
func (inspector *Endpoint) Stats(ctx context.Context, in *pb.StatsRequest) (out *pb.StatSummaryResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return inspector.retrieveStats(ctx)
|
|
|
|
}
|
|
|
|
|
2019-06-04 13:31:39 +01:00
|
|
|
func (inspector *Endpoint) retrieveStats(ctx context.Context) (_ *pb.StatSummaryResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-30 13:16:08 +00:00
|
|
|
|
|
|
|
// Space Usage
|
2020-01-07 23:34:51 +00:00
|
|
|
_, piecesContentSize, err := inspector.pieceStore.SpaceUsedForPieces(ctx)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
2020-01-14 11:46:15 +00:00
|
|
|
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
2019-04-15 11:12:22 +01:00
|
|
|
usage, err := bandwidth.TotalMonthlySummary(ctx, inspector.usageDB)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
2020-01-14 11:46:15 +00:00
|
|
|
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
2019-03-27 19:44:18 +00:00
|
|
|
ingress := usage.Put + usage.PutRepair
|
|
|
|
egress := usage.Get + usage.GetAudit + usage.GetRepair
|
|
|
|
|
2019-04-15 11:12:22 +01:00
|
|
|
totalUsedBandwidth := usage.Total()
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
return &pb.StatSummaryResponse{
|
2020-02-12 21:19:42 +00:00
|
|
|
UsedSpace: piecesContentSize,
|
|
|
|
AvailableSpace: inspector.pieceStoreConfig.AllocatedDiskSpace.Int64() - piecesContentSize,
|
|
|
|
UsedIngress: ingress,
|
|
|
|
UsedEgress: egress,
|
|
|
|
UsedBandwidth: totalUsedBandwidth,
|
2019-03-18 10:55:06 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-14 11:46:15 +00:00
|
|
|
// Dashboard returns dashboard information
|
|
|
|
func (inspector *Endpoint) Dashboard(ctx context.Context, in *pb.DashboardRequest) (out *pb.DashboardResponse, err error) {
|
2019-03-18 10:55:06 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-01-14 11:46:15 +00:00
|
|
|
return inspector.getDashboardData(ctx)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 13:31:39 +01:00
|
|
|
func (inspector *Endpoint) getDashboardData(ctx context.Context) (_ *pb.DashboardResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
statsSummary, err := inspector.retrieveStats(ctx)
|
|
|
|
if err != nil {
|
2020-01-14 11:46:15 +00:00
|
|
|
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 19:31:39 +01:00
|
|
|
lastPingedAt := inspector.pingStats.WhenLastPinged()
|
2019-09-11 21:41:43 +01:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
return &pb.DashboardResponse{
|
2019-10-03 19:31:39 +01:00
|
|
|
NodeId: inspector.contact.Local().Id,
|
|
|
|
InternalAddress: "",
|
|
|
|
ExternalAddress: inspector.contact.Local().Address.Address,
|
|
|
|
LastPinged: lastPingedAt,
|
|
|
|
DashboardAddress: inspector.dashboardAddress.String(),
|
|
|
|
Uptime: ptypes.DurationProto(time.Since(inspector.startTime)),
|
|
|
|
Stats: statsSummary,
|
2019-03-18 10:55:06 +00:00
|
|
|
}, nil
|
|
|
|
}
|