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"
|
|
|
|
|
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"
|
|
|
|
|
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"
|
2020-10-30 12:51:26 +00:00
|
|
|
"storj.io/storj/storagenode/internalpb"
|
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()
|
|
|
|
|
2020-08-11 15:50:01 +01:00
|
|
|
// Error is the default error class for piecestore monitor errors.
|
2019-03-18 10:55:06 +00:00
|
|
|
Error = errs.Class("piecestore inspector")
|
|
|
|
)
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// Endpoint implements the inspector endpoints.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Endpoint
|
2019-03-18 10:55:06 +00:00
|
|
|
type Endpoint struct {
|
2021-03-29 09:58:04 +01:00
|
|
|
internalpb.DRPCPieceStoreInspectorUnimplementedServer
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01: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-07-16 15:18:02 +01:00
|
|
|
// Stats returns current statistics about the storage node.
|
2020-10-30 12:51:26 +00:00
|
|
|
func (inspector *Endpoint) Stats(ctx context.Context, in *internalpb.StatsRequest) (out *internalpb.StatSummaryResponse, err error) {
|
2020-01-14 11:46:15 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return inspector.retrieveStats(ctx)
|
|
|
|
}
|
|
|
|
|
2020-10-30 12:51:26 +00:00
|
|
|
func (inspector *Endpoint) retrieveStats(ctx context.Context) (_ *internalpb.StatSummaryResponse, err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-30 13:16:08 +00:00
|
|
|
|
|
|
|
// Space Usage
|
2020-04-30 13:06:01 +01:00
|
|
|
piecesContentSize, err := inspector.pieceStore.SpaceUsedForPiecesAndTrash(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()
|
2020-08-02 17:21:43 +01:00
|
|
|
availableSpace := inspector.pieceStoreConfig.AllocatedDiskSpace.Int64() - piecesContentSize
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2020-10-30 12:51:26 +00:00
|
|
|
return &internalpb.StatSummaryResponse{
|
2020-02-12 21:19:42 +00:00
|
|
|
UsedSpace: piecesContentSize,
|
2020-08-02 17:21:43 +01:00
|
|
|
AvailableSpace: availableSpace,
|
2020-02-12 21:19:42 +00:00
|
|
|
UsedIngress: ingress,
|
|
|
|
UsedEgress: egress,
|
|
|
|
UsedBandwidth: totalUsedBandwidth,
|
2019-03-18 10:55:06 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Dashboard returns dashboard information.
|
2020-10-30 12:51:26 +00:00
|
|
|
func (inspector *Endpoint) Dashboard(ctx context.Context, in *internalpb.DashboardRequest) (out *internalpb.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
|
|
|
}
|
|
|
|
|
2020-10-30 12:51:26 +00:00
|
|
|
func (inspector *Endpoint) getDashboardData(ctx context.Context) (_ *internalpb.DashboardResponse, err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
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()
|
2020-05-20 14:40:25 +01:00
|
|
|
self := inspector.contact.Local()
|
2020-10-30 12:51:26 +00:00
|
|
|
return &internalpb.DashboardResponse{
|
2020-05-20 14:40:25 +01:00
|
|
|
NodeId: self.ID,
|
2019-10-03 19:31:39 +01:00
|
|
|
InternalAddress: "",
|
2020-05-20 14:40:25 +01:00
|
|
|
ExternalAddress: self.Address,
|
2019-10-03 19:31:39 +01:00
|
|
|
LastPinged: lastPingedAt,
|
|
|
|
DashboardAddress: inspector.dashboardAddress.String(),
|
2020-04-29 09:10:11 +01:00
|
|
|
Uptime: time.Since(inspector.startTime).String(),
|
2019-10-03 19:31:39 +01:00
|
|
|
Stats: statsSummary,
|
2019-03-18 10:55:06 +00:00
|
|
|
}, nil
|
|
|
|
}
|