2022-12-15 19:22:18 +00:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package nodetally
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2023-04-24 09:32:56 +01:00
|
|
|
"github.com/zeebo/errs"
|
2022-12-15 19:22:18 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/storj/satellite/accounting"
|
2023-03-22 18:05:48 +00:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2022-12-15 19:22:18 +00:00
|
|
|
"storj.io/storj/satellite/metabase/rangedloop"
|
|
|
|
)
|
|
|
|
|
2023-04-24 09:32:56 +01:00
|
|
|
var (
|
|
|
|
// Error is a standard error class for this package.
|
|
|
|
Error = errs.Class("node tally")
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
|
|
|
|
2022-12-15 19:22:18 +00:00
|
|
|
var (
|
|
|
|
// check if Observer and Partial interfaces are satisfied.
|
2023-04-24 09:32:56 +01:00
|
|
|
_ rangedloop.Observer = (*Observer)(nil)
|
|
|
|
_ rangedloop.Partial = (*observerFork)(nil)
|
2022-12-15 19:22:18 +00:00
|
|
|
)
|
|
|
|
|
2023-04-24 09:32:56 +01:00
|
|
|
// Observer implements node tally ranged loop observer.
|
|
|
|
type Observer struct {
|
2022-12-15 19:22:18 +00:00
|
|
|
log *zap.Logger
|
|
|
|
accounting accounting.StoragenodeAccounting
|
|
|
|
|
2023-03-22 18:05:48 +00:00
|
|
|
metabaseDB *metabase.DB
|
|
|
|
|
2022-12-15 19:22:18 +00:00
|
|
|
nowFn func() time.Time
|
|
|
|
lastTallyTime time.Time
|
2023-03-22 18:05:48 +00:00
|
|
|
Node map[metabase.NodeAlias]float64
|
2022-12-15 19:22:18 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 09:32:56 +01:00
|
|
|
// NewObserver creates new tally range loop observer.
|
|
|
|
func NewObserver(log *zap.Logger, accounting accounting.StoragenodeAccounting, metabaseDB *metabase.DB) *Observer {
|
|
|
|
return &Observer{
|
2022-12-15 19:22:18 +00:00
|
|
|
log: log,
|
|
|
|
accounting: accounting,
|
2023-03-22 18:05:48 +00:00
|
|
|
metabaseDB: metabaseDB,
|
2022-12-15 19:22:18 +00:00
|
|
|
nowFn: time.Now,
|
2023-03-22 18:05:48 +00:00
|
|
|
Node: map[metabase.NodeAlias]float64{},
|
2022-12-15 19:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implements ranged loop observer start method.
|
2023-04-24 09:32:56 +01:00
|
|
|
func (observer *Observer) Start(ctx context.Context, time time.Time) (err error) {
|
2023-02-03 10:04:53 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2023-03-22 18:05:48 +00:00
|
|
|
observer.Node = map[metabase.NodeAlias]float64{}
|
2022-12-15 19:22:18 +00:00
|
|
|
observer.lastTallyTime, err = observer.accounting.LastTimestamp(ctx, accounting.LastAtRestTally)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if observer.lastTallyTime.IsZero() {
|
|
|
|
observer.lastTallyTime = observer.nowFn()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fork forks new node tally ranged loop partial.
|
2023-04-24 09:32:56 +01:00
|
|
|
func (observer *Observer) Fork(ctx context.Context) (_ rangedloop.Partial, err error) {
|
2023-02-03 10:04:53 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2023-04-24 09:32:56 +01:00
|
|
|
return newObserverFork(observer.log, observer.nowFn), nil
|
2022-12-15 19:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Join joins node tally ranged loop partial to main observer updating main per node usage map.
|
2023-04-24 09:32:56 +01:00
|
|
|
func (observer *Observer) Join(ctx context.Context, partial rangedloop.Partial) (err error) {
|
2023-02-03 10:04:53 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2023-04-24 09:32:56 +01:00
|
|
|
tallyPartial, ok := partial.(*observerFork)
|
2022-12-15 19:22:18 +00:00
|
|
|
if !ok {
|
|
|
|
return Error.New("expected partial type %T but got %T", tallyPartial, partial)
|
|
|
|
}
|
|
|
|
|
2023-03-22 18:05:48 +00:00
|
|
|
for alias, val := range tallyPartial.Node {
|
|
|
|
observer.Node[alias] += val
|
2022-12-15 19:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// for backwards compatibility.
|
|
|
|
var monRangedTally = monkit.ScopeNamed("storj.io/storj/satellite/accounting/tally")
|
|
|
|
|
|
|
|
// Finish calculates byte*hours from per node storage usage and save tallies to DB.
|
2023-04-24 09:32:56 +01:00
|
|
|
func (observer *Observer) Finish(ctx context.Context) (err error) {
|
2023-02-03 10:04:53 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2022-12-15 19:22:18 +00:00
|
|
|
finishTime := observer.nowFn()
|
|
|
|
|
|
|
|
// calculate byte hours, not just bytes
|
|
|
|
hours := finishTime.Sub(observer.lastTallyTime).Hours()
|
|
|
|
var totalSum float64
|
2023-03-22 18:05:48 +00:00
|
|
|
nodeIDs := make([]storj.NodeID, 0, len(observer.Node))
|
|
|
|
byteHours := make([]float64, 0, len(observer.Node))
|
|
|
|
nodeAliasMap, err := observer.metabaseDB.LatestNodesAliasMap(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for alias, pieceSize := range observer.Node {
|
2022-12-15 19:22:18 +00:00
|
|
|
totalSum += pieceSize
|
2023-03-22 18:05:48 +00:00
|
|
|
nodeID, ok := nodeAliasMap.Node(alias)
|
|
|
|
if !ok {
|
|
|
|
observer.log.Error("unrecognized node alias in ranged-loop tally", zap.Int32("node-alias", int32(alias)))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nodeIDs = append(nodeIDs, nodeID)
|
|
|
|
byteHours = append(byteHours, pieceSize*hours)
|
2022-12-15 19:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
monRangedTally.IntVal("nodetallies.totalsum").Observe(int64(totalSum)) //mon:locked
|
|
|
|
|
2023-03-22 18:05:48 +00:00
|
|
|
err = observer.accounting.SaveTallies(ctx, finishTime, nodeIDs, byteHours)
|
2022-12-15 19:22:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.New("StorageNodeAccounting.SaveTallies failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNow overrides the timestamp used to store the result.
|
2023-04-24 09:32:56 +01:00
|
|
|
func (observer *Observer) SetNow(nowFn func() time.Time) {
|
2022-12-15 19:22:18 +00:00
|
|
|
observer.nowFn = nowFn
|
|
|
|
}
|
|
|
|
|
2023-04-24 09:32:56 +01:00
|
|
|
// observerFork implements node tally ranged loop partial.
|
|
|
|
type observerFork struct {
|
2022-12-15 19:22:18 +00:00
|
|
|
log *zap.Logger
|
|
|
|
nowFn func() time.Time
|
|
|
|
|
2023-03-22 18:05:48 +00:00
|
|
|
Node map[metabase.NodeAlias]float64
|
2022-12-15 19:22:18 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 09:32:56 +01:00
|
|
|
// newObserverFork creates new node tally ranged loop fork.
|
|
|
|
func newObserverFork(log *zap.Logger, nowFn func() time.Time) *observerFork {
|
|
|
|
return &observerFork{
|
2022-12-15 19:22:18 +00:00
|
|
|
log: log,
|
|
|
|
nowFn: nowFn,
|
2023-03-22 18:05:48 +00:00
|
|
|
Node: map[metabase.NodeAlias]float64{},
|
2022-12-15 19:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process iterates over segment range updating partial node usage map.
|
2023-05-09 12:13:19 +01:00
|
|
|
func (partial *observerFork) Process(ctx context.Context, segments []rangedloop.Segment) error {
|
2022-12-15 19:22:18 +00:00
|
|
|
now := partial.nowFn()
|
|
|
|
|
|
|
|
for _, segment := range segments {
|
|
|
|
partial.processSegment(now, segment)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-09 12:13:19 +01:00
|
|
|
func (partial *observerFork) processSegment(now time.Time, segment rangedloop.Segment) {
|
2022-12-15 19:22:18 +00:00
|
|
|
if segment.Inline() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if segment.Expired(now) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add node info
|
|
|
|
minimumRequired := segment.Redundancy.RequiredShares
|
|
|
|
|
|
|
|
if minimumRequired <= 0 {
|
|
|
|
partial.log.Error("failed sanity check", zap.String("StreamID", segment.StreamID.String()), zap.Uint64("Position", segment.Position.Encode()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pieceSize := float64(segment.EncryptedSize / int32(minimumRequired)) // TODO: Add this as a method to RedundancyScheme
|
|
|
|
|
2023-03-22 18:05:48 +00:00
|
|
|
for _, piece := range segment.AliasPieces {
|
|
|
|
partial.Node[piece.Alias] += pieceSize
|
2022-12-15 19:22:18 +00:00
|
|
|
}
|
|
|
|
}
|