2019-08-08 14:47:04 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package nodestats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/sync2"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/date"
|
2020-03-15 22:16:56 +00:00
|
|
|
"storj.io/storj/storagenode/heldamount"
|
2019-08-08 14:47:04 +01:00
|
|
|
"storj.io/storj/storagenode/reputation"
|
|
|
|
"storj.io/storj/storagenode/storageusage"
|
|
|
|
"storj.io/storj/storagenode/trust"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config defines nodestats cache configuration
|
|
|
|
type Config struct {
|
|
|
|
MaxSleep time.Duration `help:"maximum duration to wait before requesting data" releaseDefault:"300s" devDefault:"1s"`
|
|
|
|
ReputationSync time.Duration `help:"how often to sync reputation" releaseDefault:"4h" devDefault:"1m"`
|
|
|
|
StorageSync time.Duration `help:"how often to sync storage" releaseDefault:"12h" devDefault:"2m"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// CacheStorage encapsulates cache DBs
|
|
|
|
type CacheStorage struct {
|
|
|
|
Reputation reputation.DB
|
|
|
|
StorageUsage storageusage.DB
|
2020-03-15 22:16:56 +00:00
|
|
|
HeldAmount heldamount.DB
|
2019-08-08 14:47:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache runs cache loop and stores reputation stats
|
|
|
|
// and storage usage into db
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Chore
|
2019-08-08 14:47:04 +01:00
|
|
|
type Cache struct {
|
|
|
|
log *zap.Logger
|
|
|
|
|
2020-03-15 22:16:56 +00:00
|
|
|
db CacheStorage
|
|
|
|
service *Service
|
|
|
|
heldamountService *heldamount.Service
|
|
|
|
trust *trust.Pool
|
2019-08-08 14:47:04 +01:00
|
|
|
|
2020-01-29 15:37:50 +00:00
|
|
|
maxSleep time.Duration
|
|
|
|
Reputation *sync2.Cycle
|
|
|
|
Storage *sync2.Cycle
|
2019-08-08 14:47:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCache creates new caching service instance
|
2020-03-15 22:16:56 +00:00
|
|
|
func NewCache(log *zap.Logger, config Config, db CacheStorage, service *Service, heldamountService *heldamount.Service, trust *trust.Pool) *Cache {
|
2019-08-08 14:47:04 +01:00
|
|
|
return &Cache{
|
2020-03-15 22:16:56 +00:00
|
|
|
log: log,
|
|
|
|
db: db,
|
|
|
|
service: service,
|
|
|
|
heldamountService: heldamountService,
|
|
|
|
trust: trust,
|
|
|
|
maxSleep: config.MaxSleep,
|
|
|
|
Reputation: sync2.NewCycle(config.ReputationSync),
|
2020-03-30 18:43:38 +01:00
|
|
|
Storage: sync2.NewCycle(config.StorageSync),
|
2019-08-08 14:47:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs loop
|
|
|
|
func (cache *Cache) Run(ctx context.Context) error {
|
|
|
|
var group errgroup.Group
|
|
|
|
|
2020-01-29 15:37:50 +00:00
|
|
|
cache.Reputation.Start(ctx, &group, func(ctx context.Context) error {
|
2019-08-09 12:12:32 +01:00
|
|
|
if err := cache.sleep(ctx); err != nil {
|
|
|
|
return err
|
2019-08-08 14:47:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err := cache.CacheReputationStats(ctx)
|
|
|
|
if err != nil {
|
|
|
|
cache.log.Error("Get stats query failed", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2020-01-29 15:37:50 +00:00
|
|
|
cache.Storage.Start(ctx, &group, func(ctx context.Context) error {
|
2019-08-09 12:12:32 +01:00
|
|
|
if err := cache.sleep(ctx); err != nil {
|
|
|
|
return err
|
2019-08-08 14:47:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err := cache.CacheSpaceUsage(ctx)
|
|
|
|
if err != nil {
|
|
|
|
cache.log.Error("Get disk space usage query failed", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2020-03-24 12:39:13 +00:00
|
|
|
err = cache.CacheHeldAmount(ctx)
|
|
|
|
if err != nil {
|
|
|
|
cache.log.Error("Get held amount query failed", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2019-08-08 14:47:04 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return group.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CacheReputationStats queries node stats from all the satellites
|
|
|
|
// known to the storagenode and stores information into db
|
|
|
|
func (cache *Cache) CacheReputationStats(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
return cache.satelliteLoop(ctx, func(satellite storj.NodeID) error {
|
|
|
|
stats, err := cache.service.GetReputationStats(ctx, satellite)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = cache.db.Reputation.Store(ctx, *stats); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CacheSpaceUsage queries disk space usage from all the satellites
|
|
|
|
// known to the storagenode and stores information into db
|
|
|
|
func (cache *Cache) CacheSpaceUsage(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
// get current month edges
|
|
|
|
startDate, endDate := date.MonthBoundary(time.Now().UTC())
|
|
|
|
|
|
|
|
return cache.satelliteLoop(ctx, func(satellite storj.NodeID) error {
|
|
|
|
spaceUsages, err := cache.service.GetDailyStorageUsage(ctx, satellite, startDate, endDate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cache.db.StorageUsage.Store(ctx, spaceUsages)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-15 22:16:56 +00:00
|
|
|
// CacheHeldAmount queries held amount stats and payments from all the satellites known to the storagenode and stores info into db.
|
|
|
|
func (cache *Cache) CacheHeldAmount(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
return cache.satelliteLoop(ctx, func(satellite storj.NodeID) error {
|
2020-03-24 12:39:13 +00:00
|
|
|
payStub, err := cache.heldamountService.GetPaystubStats(ctx, satellite, time.Now().AddDate(0, -1, 0).String())
|
2020-03-15 22:16:56 +00:00
|
|
|
if err != nil {
|
2020-03-24 12:39:13 +00:00
|
|
|
if heldamount.ErrNoPayStubForPeriod.Has(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-15 22:16:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-03-24 12:39:13 +00:00
|
|
|
|
|
|
|
if payStub != nil {
|
|
|
|
if err = cache.db.HeldAmount.StorePayStub(ctx, *payStub); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-15 22:16:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 12:39:13 +00:00
|
|
|
payment, err := cache.heldamountService.GetPayment(ctx, satellite, time.Now().AddDate(0, -1, 0).String())
|
2020-03-15 22:16:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-24 12:39:13 +00:00
|
|
|
if payment != nil {
|
|
|
|
if err = cache.db.HeldAmount.StorePayment(ctx, *payment); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-15 22:16:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:12:32 +01:00
|
|
|
// sleep for random interval in [0;maxSleep)
|
|
|
|
// returns error if context was cancelled
|
|
|
|
func (cache *Cache) sleep(ctx context.Context) error {
|
|
|
|
if cache.maxSleep <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-22 01:57:44 +01:00
|
|
|
jitter := time.Duration(rand.Int63n(int64(cache.maxSleep)))
|
2019-08-09 12:12:32 +01:00
|
|
|
if !sync2.Sleep(ctx, jitter) {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-08 14:47:04 +01:00
|
|
|
// satelliteLoop loops over all satellites from trust pool executing provided fn, caching errors if occurred,
|
|
|
|
// on each step checks if context has been cancelled
|
|
|
|
func (cache *Cache) satelliteLoop(ctx context.Context, fn func(id storj.NodeID) error) error {
|
|
|
|
var groupErr errs.Group
|
|
|
|
for _, satellite := range cache.trust.GetSatellites(ctx) {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
groupErr.Add(fn(satellite))
|
|
|
|
}
|
|
|
|
|
|
|
|
return groupErr.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes underlying cycles
|
|
|
|
func (cache *Cache) Close() error {
|
|
|
|
defer mon.Task()(nil)(nil)
|
2020-01-29 15:37:50 +00:00
|
|
|
cache.Reputation.Close()
|
|
|
|
cache.Storage.Close()
|
2019-08-08 14:47:04 +01:00
|
|
|
return nil
|
|
|
|
}
|