2018-11-08 16:18:28 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-11-14 01:22:18 +00:00
|
|
|
package tally
|
2018-11-08 16:18:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"go.uber.org/zap"
|
2018-12-07 09:59:31 +00:00
|
|
|
|
2018-12-05 14:03:23 +00:00
|
|
|
"storj.io/storj/pkg/accounting"
|
2018-12-07 09:59:31 +00:00
|
|
|
"storj.io/storj/pkg/bwagreement"
|
2018-11-08 16:18:28 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/pointerdb"
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-11-08 16:18:28 +00:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
2018-11-14 01:22:18 +00:00
|
|
|
// Tally is the service for accounting for data stored on each storage node
|
2018-11-08 16:18:28 +00:00
|
|
|
type Tally interface {
|
|
|
|
Run(ctx context.Context) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type tally struct {
|
2018-12-14 14:27:21 +00:00
|
|
|
pointerdb *pointerdb.Server
|
|
|
|
overlay pb.OverlayServer
|
|
|
|
limit int
|
|
|
|
logger *zap.Logger
|
|
|
|
ticker *time.Ticker
|
|
|
|
accountingDB accounting.DB
|
|
|
|
bwAgreementDB bwagreement.DB // bwagreements database
|
2018-11-08 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 14:27:21 +00:00
|
|
|
func newTally(logger *zap.Logger, accountingDB accounting.DB, bwAgreementDB bwagreement.DB, pointerdb *pointerdb.Server, overlay pb.OverlayServer, limit int, interval time.Duration) *tally {
|
2018-11-08 16:18:28 +00:00
|
|
|
return &tally{
|
2018-12-14 14:27:21 +00:00
|
|
|
pointerdb: pointerdb,
|
|
|
|
overlay: overlay,
|
|
|
|
limit: limit,
|
|
|
|
logger: logger,
|
|
|
|
ticker: time.NewTicker(interval),
|
|
|
|
accountingDB: accountingDB,
|
|
|
|
bwAgreementDB: bwAgreementDB,
|
2018-12-05 14:03:23 +00:00
|
|
|
}
|
2018-11-08 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 01:22:18 +00:00
|
|
|
// Run the tally loop
|
2018-11-08 16:18:28 +00:00
|
|
|
func (t *tally) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
for {
|
2018-12-12 21:24:08 +00:00
|
|
|
err = t.calculateAtRestData(ctx)
|
2018-11-08 16:18:28 +00:00
|
|
|
if err != nil {
|
2018-12-12 21:24:08 +00:00
|
|
|
t.logger.Error("calculateAtRestData failed", zap.Error(err))
|
|
|
|
}
|
2018-12-18 17:18:42 +00:00
|
|
|
err = t.queryBW(ctx)
|
2018-12-12 21:24:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.logger.Error("Query for bandwith failed", zap.Error(err))
|
2018-11-08 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-t.ticker.C: // wait for the next interval to happen
|
2018-11-14 01:22:18 +00:00
|
|
|
case <-ctx.Done(): // or the tally is canceled via context
|
2018-11-08 16:18:28 +00:00
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 21:24:08 +00:00
|
|
|
// calculateAtRestData iterates through the pieces on pointerdb and calculates
|
|
|
|
// the amount of at-rest data stored on each respective node
|
|
|
|
func (t *tally) calculateAtRestData(ctx context.Context) (err error) {
|
2018-11-08 16:18:28 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-12 21:24:08 +00:00
|
|
|
var nodeData = make(map[storj.NodeID]int64)
|
2018-11-08 16:18:28 +00:00
|
|
|
err = t.pointerdb.Iterate(ctx, &pb.IterateRequest{Recurse: true},
|
|
|
|
func(it storage.Iterator) error {
|
|
|
|
var item storage.ListItem
|
2018-12-12 21:24:08 +00:00
|
|
|
for it.Next(&item) {
|
2018-11-08 16:18:28 +00:00
|
|
|
pointer := &pb.Pointer{}
|
|
|
|
err = proto.Unmarshal(item.Value, pointer)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2018-12-12 21:24:08 +00:00
|
|
|
remote := pointer.GetRemote()
|
|
|
|
if remote == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pieces := remote.GetRemotePieces()
|
|
|
|
if pieces == nil {
|
|
|
|
t.logger.Debug("no pieces on remote segment")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
segmentSize := pointer.GetSegmentSize()
|
|
|
|
redundancy := remote.GetRedundancy()
|
|
|
|
if redundancy == nil {
|
|
|
|
t.logger.Debug("no redundancy scheme present")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
minReq := redundancy.GetMinReq()
|
|
|
|
if minReq <= 0 {
|
|
|
|
t.logger.Debug("pointer minReq must be an int greater than 0")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pieceSize := segmentSize / int64(minReq)
|
|
|
|
for _, piece := range pieces {
|
|
|
|
nodeData[piece.NodeId] += pieceSize
|
2018-11-08 16:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
2018-12-14 14:27:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2018-12-18 17:18:42 +00:00
|
|
|
latestTally, isNil, err := t.accountingDB.LastRawTime(ctx, accounting.LastAtRestTally)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
if isNil {
|
|
|
|
latestTally = time.Now().UTC()
|
|
|
|
}
|
|
|
|
return Error.Wrap(t.accountingDB.SaveAtRestRaw(ctx, latestTally, nodeData))
|
2018-11-08 16:18:28 +00:00
|
|
|
}
|
2018-12-05 14:03:23 +00:00
|
|
|
|
2018-12-18 17:18:42 +00:00
|
|
|
// queryBW queries bandwidth allocation database, selecting all new contracts since the last collection run time.
|
2018-12-05 14:03:23 +00:00
|
|
|
// Grouping by storage node ID and adding total of bandwidth to granular data table.
|
2018-12-18 17:18:42 +00:00
|
|
|
func (t *tally) queryBW(ctx context.Context) error {
|
|
|
|
lastBwTally, isNil, err := t.accountingDB.LastRawTime(ctx, accounting.LastBandwidthTally)
|
2018-12-05 14:03:23 +00:00
|
|
|
if err != nil {
|
2018-12-14 14:27:21 +00:00
|
|
|
return Error.Wrap(err)
|
2018-12-05 14:03:23 +00:00
|
|
|
}
|
2018-12-07 09:59:31 +00:00
|
|
|
var bwAgreements []bwagreement.Agreement
|
2018-12-14 14:27:21 +00:00
|
|
|
if isNil {
|
2018-12-05 14:03:23 +00:00
|
|
|
t.logger.Info("Tally found no existing bandwith tracking data")
|
2018-12-14 14:27:21 +00:00
|
|
|
bwAgreements, err = t.bwAgreementDB.GetAgreements(ctx)
|
2018-12-05 14:03:23 +00:00
|
|
|
} else {
|
2018-12-14 14:27:21 +00:00
|
|
|
bwAgreements, err = t.bwAgreementDB.GetAgreementsSince(ctx, lastBwTally)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
2018-12-05 14:03:23 +00:00
|
|
|
}
|
|
|
|
if len(bwAgreements) == 0 {
|
|
|
|
t.logger.Info("Tally found no new bandwidth allocations")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sum totals by node id ... todo: add nodeid as SQL column so DB can do this?
|
|
|
|
bwTotals := make(map[string]int64)
|
|
|
|
var latestBwa time.Time
|
|
|
|
for _, baRow := range bwAgreements {
|
|
|
|
rbad := &pb.RenterBandwidthAllocation_Data{}
|
2018-12-07 09:59:31 +00:00
|
|
|
if err := proto.Unmarshal(baRow.Agreement, rbad); err != nil {
|
2018-12-05 14:03:23 +00:00
|
|
|
t.logger.DPanic("Could not deserialize renter bwa in tally query")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if baRow.CreatedAt.After(latestBwa) {
|
|
|
|
latestBwa = baRow.CreatedAt
|
|
|
|
}
|
2018-12-17 18:11:29 +00:00
|
|
|
bwTotals[rbad.StorageNodeId.String()] += rbad.GetTotal()
|
2018-12-05 14:03:23 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 17:18:42 +00:00
|
|
|
return Error.Wrap(t.accountingDB.SaveBWRaw(ctx, lastBwTally, bwTotals))
|
2018-12-05 14:03:23 +00:00
|
|
|
}
|