2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-14 01:22:18 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package rollup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2019-04-02 19:21:18 +01:00
|
|
|
"storj.io/storj/internal/memory"
|
2018-12-12 21:24:08 +00:00
|
|
|
"storj.io/storj/pkg/accounting"
|
2019-04-04 16:20:59 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-01-16 19:30:33 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-11-14 01:22:18 +00:00
|
|
|
)
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
// Config contains configurable values for rollup
|
|
|
|
type Config struct {
|
2019-04-02 19:21:18 +01:00
|
|
|
Interval time.Duration `help:"how frequently rollup should run" devDefault:"120s" default:"6h"`
|
|
|
|
MaxAlphaUsage memory.Size `help:"the bandwidth and storage usage limit for the alpha release" default:"25GB"`
|
2018-11-14 01:22:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 16:20:59 +01:00
|
|
|
// Service is the rollup service for totalling data on storage nodes on daily intervals
|
|
|
|
type Service struct {
|
2018-11-14 01:22:18 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
ticker *time.Ticker
|
2018-12-14 14:27:21 +00:00
|
|
|
db accounting.DB
|
2018-11-14 01:22:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
// New creates a new rollup service
|
2019-04-04 16:20:59 +01:00
|
|
|
func New(logger *zap.Logger, db accounting.DB, interval time.Duration) *Service {
|
|
|
|
return &Service{
|
2018-11-14 01:22:18 +00:00
|
|
|
logger: logger,
|
|
|
|
ticker: time.NewTicker(interval),
|
2018-11-26 21:49:55 +00:00
|
|
|
db: db,
|
2018-12-14 14:27:21 +00:00
|
|
|
}
|
2018-11-14 01:22:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
// Run the Rollup loop
|
2019-04-04 16:20:59 +01:00
|
|
|
func (r *Service) Run(ctx context.Context) (err error) {
|
2019-01-24 20:05:53 +00:00
|
|
|
r.logger.Info("Rollup service starting up")
|
2018-11-14 01:22:18 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
for {
|
2019-04-04 16:20:59 +01:00
|
|
|
err = r.Rollup(ctx)
|
2018-11-14 01:22:18 +00:00
|
|
|
if err != nil {
|
2018-12-12 21:24:08 +00:00
|
|
|
r.logger.Error("Query failed", zap.Error(err))
|
2018-11-14 01:22:18 +00:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-r.ticker.C: // wait for the next interval to happen
|
2019-01-23 19:58:44 +00:00
|
|
|
case <-ctx.Done(): // or the Rollup is canceled via context
|
2018-11-14 01:22:18 +00:00
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 16:20:59 +01:00
|
|
|
// Rollup aggregates storage and bandwidth amounts for the time interval
|
|
|
|
func (r *Service) Rollup(ctx context.Context) error {
|
2019-01-23 19:58:44 +00:00
|
|
|
// only Rollup new things - get LastRollup
|
2019-02-01 18:50:12 +00:00
|
|
|
lastRollup, err := r.db.LastTimestamp(ctx, accounting.LastRollup)
|
2019-01-16 19:30:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2019-04-04 16:20:59 +01:00
|
|
|
rollupStats := make(accounting.RollupStats)
|
|
|
|
latestTally, err := r.RollupStorage(ctx, lastRollup, rollupStats)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
if len(rollupStats) == 0 {
|
|
|
|
r.logger.Info("RollupStats is empty after RollupStorage")
|
|
|
|
}
|
|
|
|
err = r.RollupBW(ctx, lastRollup, rollupStats)
|
2019-01-16 19:30:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2019-04-04 16:20:59 +01:00
|
|
|
if len(rollupStats) == 0 {
|
|
|
|
r.logger.Info("RollupStats is empty after RollupBW")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = r.db.SaveRollup(ctx, latestTally, rollupStats)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
// Delete already rolled up tallies
|
|
|
|
err = r.db.DeleteRawBefore(ctx, latestTally)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RollupStorage rolls up storage tally, modifies rollupStats map
|
|
|
|
func (r *Service) RollupStorage(ctx context.Context, lastRollup time.Time, rollupStats accounting.RollupStats) (latestTally time.Time, err error) {
|
|
|
|
tallies, err := r.db.GetRawSince(ctx, lastRollup)
|
|
|
|
if err != nil {
|
|
|
|
return time.Now(), Error.Wrap(err)
|
|
|
|
}
|
2019-01-16 19:30:33 +00:00
|
|
|
if len(tallies) == 0 {
|
|
|
|
r.logger.Info("Rollup found no new tallies")
|
2019-04-04 16:20:59 +01:00
|
|
|
return time.Now(), nil
|
2019-01-16 19:30:33 +00:00
|
|
|
}
|
2019-01-23 19:58:44 +00:00
|
|
|
//loop through tallies and build Rollup
|
2019-01-16 19:30:33 +00:00
|
|
|
for _, tallyRow := range tallies {
|
|
|
|
node := tallyRow.NodeID
|
|
|
|
if tallyRow.CreatedAt.After(latestTally) {
|
|
|
|
latestTally = tallyRow.CreatedAt
|
|
|
|
}
|
2019-04-04 16:20:59 +01:00
|
|
|
//create or get AccoutingRollup day entry
|
2019-01-16 19:30:33 +00:00
|
|
|
iDay := tallyRow.IntervalEndTime
|
|
|
|
iDay = time.Date(iDay.Year(), iDay.Month(), iDay.Day(), 0, 0, 0, 0, iDay.Location())
|
|
|
|
if rollupStats[iDay] == nil {
|
|
|
|
rollupStats[iDay] = make(map[storj.NodeID]*accounting.Rollup)
|
|
|
|
}
|
|
|
|
if rollupStats[iDay][node] == nil {
|
|
|
|
rollupStats[iDay][node] = &accounting.Rollup{NodeID: node, StartTime: iDay}
|
|
|
|
}
|
2019-04-04 16:20:59 +01:00
|
|
|
//increment data at rest sum
|
2019-01-16 19:30:33 +00:00
|
|
|
switch tallyRow.DataType {
|
|
|
|
case accounting.AtRest:
|
|
|
|
rollupStats[iDay][node].AtRestTotal += tallyRow.DataTotal
|
|
|
|
default:
|
2019-04-04 16:20:59 +01:00
|
|
|
r.logger.Info("rollupStorage no longer supports non-accounting.AtRest datatypes")
|
2019-01-16 19:30:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//remove the latest day (which we cannot know is complete), then push to DB
|
|
|
|
latestTally = time.Date(latestTally.Year(), latestTally.Month(), latestTally.Day(), 0, 0, 0, 0, latestTally.Location())
|
|
|
|
delete(rollupStats, latestTally)
|
2019-01-18 16:53:23 +00:00
|
|
|
if len(rollupStats) == 0 {
|
|
|
|
r.logger.Info("Rollup only found tallies for today")
|
2019-04-04 16:20:59 +01:00
|
|
|
return time.Now(), nil
|
2019-01-18 16:53:23 +00:00
|
|
|
}
|
2019-04-04 16:20:59 +01:00
|
|
|
|
|
|
|
return latestTally, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RollupBW aggregates the bandwidth rollups, modifies rollupStats map
|
|
|
|
func (r *Service) RollupBW(ctx context.Context, lastRollup time.Time, rollupStats accounting.RollupStats) error {
|
|
|
|
var latestTally time.Time
|
2019-04-09 20:12:58 +01:00
|
|
|
bws, err := r.db.GetStoragenodeBandwidthSince(ctx, lastRollup.UTC())
|
2019-03-12 13:08:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2019-04-04 16:20:59 +01:00
|
|
|
if len(bws) == 0 {
|
|
|
|
r.logger.Info("Rollup found no new bw rollups")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, row := range bws {
|
|
|
|
nodeID := row.NodeID
|
|
|
|
if row.IntervalStart.After(latestTally) {
|
|
|
|
latestTally = row.IntervalStart
|
|
|
|
}
|
|
|
|
day := row.IntervalStart
|
|
|
|
day = time.Date(day.Year(), day.Month(), day.Day(), 0, 0, 0, 0, day.Location())
|
|
|
|
if rollupStats[day] == nil {
|
|
|
|
rollupStats[day] = make(map[storj.NodeID]*accounting.Rollup)
|
|
|
|
}
|
|
|
|
if rollupStats[day][nodeID] == nil {
|
|
|
|
rollupStats[day][nodeID] = &accounting.Rollup{NodeID: nodeID, StartTime: day}
|
|
|
|
}
|
|
|
|
switch row.Action {
|
|
|
|
case uint(pb.PieceAction_INVALID):
|
|
|
|
r.logger.Info("invalid order action type")
|
|
|
|
case uint(pb.PieceAction_PUT):
|
|
|
|
rollupStats[day][nodeID].PutTotal += int64(row.Settled)
|
|
|
|
case uint(pb.PieceAction_GET):
|
|
|
|
rollupStats[day][nodeID].GetTotal += int64(row.Settled)
|
|
|
|
case uint(pb.PieceAction_GET_AUDIT):
|
|
|
|
rollupStats[day][nodeID].GetAuditTotal += int64(row.Settled)
|
|
|
|
case uint(pb.PieceAction_GET_REPAIR):
|
|
|
|
rollupStats[day][nodeID].GetRepairTotal += int64(row.Settled)
|
|
|
|
case uint(pb.PieceAction_PUT_REPAIR):
|
|
|
|
rollupStats[day][nodeID].PutRepairTotal += int64(row.Settled)
|
|
|
|
default:
|
|
|
|
r.logger.Info("delete order type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//remove the latest day (which we cannot know is complete), then push to DB
|
|
|
|
latestTally = time.Date(latestTally.Year(), latestTally.Month(), latestTally.Day(), 0, 0, 0, 0, latestTally.Location())
|
|
|
|
delete(rollupStats, latestTally)
|
|
|
|
if len(rollupStats) == 0 {
|
|
|
|
r.logger.Info("Rollup only found data for today")
|
|
|
|
}
|
|
|
|
return nil
|
2018-11-14 01:22:18 +00:00
|
|
|
}
|