2019-04-02 19:21:18 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
2019-05-28 16:36:52 +01:00
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-05-28 16:36:52 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/memory"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2021-12-14 13:49:33 +00:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2019-04-02 19:21:18 +01:00
|
|
|
)
|
|
|
|
|
2019-10-04 20:09:52 +01:00
|
|
|
var mon = monkit.Package()
|
|
|
|
|
2019-05-28 16:36:52 +01:00
|
|
|
var (
|
2020-08-11 15:50:01 +01:00
|
|
|
// ErrProjectUsage general error for project usage.
|
2021-04-28 09:06:17 +01:00
|
|
|
ErrProjectUsage = errs.Class("project usage")
|
2019-05-28 16:36:52 +01:00
|
|
|
)
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// Service is handling project usage related logic.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2019-11-15 14:27:44 +00:00
|
|
|
type Service struct {
|
2019-05-28 16:36:52 +01:00
|
|
|
projectAccountingDB ProjectAccounting
|
2019-10-16 17:50:29 +01:00
|
|
|
liveAccounting Cache
|
2020-09-09 20:20:44 +01:00
|
|
|
projectLimitCache *ProjectLimitCache
|
2021-12-14 13:49:33 +00:00
|
|
|
metabaseDB metabase.DB
|
2020-06-02 00:19:10 +01:00
|
|
|
bandwidthCacheTTL time.Duration
|
2020-05-04 19:05:08 +01:00
|
|
|
nowFn func() time.Time
|
2021-05-25 18:43:47 +01:00
|
|
|
asOfSystemInterval time.Duration
|
2019-05-28 16:36:52 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// NewService created new instance of project usage service.
|
2021-12-14 13:49:33 +00:00
|
|
|
func NewService(projectAccountingDB ProjectAccounting, liveAccounting Cache, limitCache *ProjectLimitCache, metabaseDB metabase.DB, bandwidthCacheTTL, asOfSystemInterval time.Duration) *Service {
|
2019-11-15 14:27:44 +00:00
|
|
|
return &Service{
|
2019-05-28 16:36:52 +01:00
|
|
|
projectAccountingDB: projectAccountingDB,
|
|
|
|
liveAccounting: liveAccounting,
|
2020-09-09 20:20:44 +01:00
|
|
|
projectLimitCache: limitCache,
|
2021-12-14 13:49:33 +00:00
|
|
|
metabaseDB: metabaseDB,
|
2020-06-02 00:19:10 +01:00
|
|
|
bandwidthCacheTTL: bandwidthCacheTTL,
|
2020-05-04 19:05:08 +01:00
|
|
|
nowFn: time.Now,
|
2021-05-25 18:43:47 +01:00
|
|
|
asOfSystemInterval: asOfSystemInterval,
|
2019-05-28 16:36:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExceedsBandwidthUsage returns true if the bandwidth usage limits have been exceeded
|
|
|
|
// for a project in the past month (30 days). The usage limit is (e.g 25GB) multiplied by the redundancy
|
|
|
|
// expansion factor, so that the uplinks have a raw limit.
|
2020-12-21 17:27:12 +00:00
|
|
|
//
|
|
|
|
// Among others,it can return one of the following errors returned by
|
|
|
|
// storj.io/storj/satellite/accounting.Cache except the ErrKeyNotFound, wrapped
|
|
|
|
// by ErrProjectUsage.
|
2020-08-28 16:10:02 +01:00
|
|
|
func (usage *Service) ExceedsBandwidthUsage(ctx context.Context, projectID uuid.UUID) (_ bool, limit memory.Size, err error) {
|
2019-06-04 12:36:27 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-05-28 16:36:52 +01:00
|
|
|
var group errgroup.Group
|
|
|
|
var bandwidthGetTotal int64
|
2020-06-02 00:19:10 +01:00
|
|
|
var bandwidthUsage int64
|
2019-05-28 16:36:52 +01:00
|
|
|
|
|
|
|
group.Go(func() error {
|
2019-12-12 12:58:15 +00:00
|
|
|
var err error
|
2020-09-09 20:20:44 +01:00
|
|
|
limit, err = usage.projectLimitCache.GetProjectBandwidthLimit(ctx, projectID)
|
2019-05-28 16:36:52 +01:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
var err error
|
2020-06-02 00:19:10 +01:00
|
|
|
|
|
|
|
// Get the current bandwidth usage from cache.
|
|
|
|
bandwidthUsage, err = usage.liveAccounting.GetProjectBandwidthUsage(ctx, projectID, usage.nowFn())
|
|
|
|
if err != nil {
|
|
|
|
// Verify If the cache key was not found
|
2020-12-21 17:27:12 +00:00
|
|
|
if ErrKeyNotFound.Has(err) {
|
2020-06-02 00:19:10 +01:00
|
|
|
|
|
|
|
// Get current bandwidth value from database.
|
|
|
|
now := usage.nowFn()
|
2021-05-25 14:12:01 +01:00
|
|
|
bandwidthGetTotal, err = usage.GetProjectBandwidth(ctx, projectID, now.Year(), now.Month(), now.Day())
|
2020-06-02 00:19:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create cache key with database value.
|
|
|
|
err = usage.liveAccounting.UpdateProjectBandwidthUsage(ctx, projectID, bandwidthGetTotal, usage.bandwidthCacheTTL, usage.nowFn())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bandwidthUsage = bandwidthGetTotal
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 16:36:52 +01:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
err = group.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return false, 0, ErrProjectUsage.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2020-06-02 00:19:10 +01:00
|
|
|
// Verify the bandwidth usage cache.
|
|
|
|
if bandwidthUsage >= limit.Int64() {
|
2019-05-28 16:36:52 +01:00
|
|
|
return true, limit, nil
|
2019-04-02 19:21:18 +01:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:36:52 +01:00
|
|
|
return false, limit, nil
|
|
|
|
}
|
|
|
|
|
2022-01-14 15:51:40 +00:00
|
|
|
// UploadLimit contains upload limit characteristics.
|
|
|
|
type UploadLimit struct {
|
|
|
|
ExceedsStorage bool
|
|
|
|
StorageLimit memory.Size
|
|
|
|
ExceedsSegments bool
|
|
|
|
SegmentsLimit int64
|
2019-05-28 16:36:52 +01:00
|
|
|
}
|
|
|
|
|
2022-01-14 15:51:40 +00:00
|
|
|
// ExceedsUploadLimits returns combined checks for storage and segment limits.
|
|
|
|
func (usage *Service) ExceedsUploadLimits(ctx context.Context, projectID uuid.UUID, checkSegmentsLimit bool) (limit UploadLimit, err error) {
|
2021-12-14 13:49:33 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
var group errgroup.Group
|
2022-01-14 15:51:40 +00:00
|
|
|
var segmentUsage, storageUsage int64
|
|
|
|
|
|
|
|
if checkSegmentsLimit {
|
|
|
|
group.Go(func() error {
|
|
|
|
var err error
|
|
|
|
limit.SegmentsLimit, err = usage.projectLimitCache.GetProjectSegmentLimit(ctx, projectID)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
var err error
|
|
|
|
segmentUsage, err = usage.GetProjectSegmentUsage(ctx, projectID)
|
2022-01-21 10:13:23 +00:00
|
|
|
// Verify If the cache key was not found
|
|
|
|
if err != nil && ErrKeyNotFound.Has(err) {
|
|
|
|
segmentGetTotal, err := usage.GetProjectSegments(ctx, projectID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create cache key with database value.
|
2022-05-12 14:53:08 +01:00
|
|
|
if err := usage.liveAccounting.UpdateProjectSegmentUsage(ctx, projectID, segmentGetTotal); err != nil {
|
2022-01-21 10:13:23 +00:00
|
|
|
return err
|
2022-01-14 15:51:40 +00:00
|
|
|
}
|
2022-01-21 10:13:23 +00:00
|
|
|
|
|
|
|
segmentUsage = segmentGetTotal
|
|
|
|
return nil
|
2022-01-14 15:51:40 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
2021-12-14 13:49:33 +00:00
|
|
|
|
|
|
|
group.Go(func() error {
|
|
|
|
var err error
|
2022-01-14 15:51:40 +00:00
|
|
|
limit.StorageLimit, err = usage.projectLimitCache.GetProjectStorageLimit(ctx, projectID)
|
2021-12-14 13:49:33 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
var err error
|
2022-01-14 15:51:40 +00:00
|
|
|
storageUsage, err = usage.GetProjectStorageTotals(ctx, projectID)
|
2021-12-14 13:49:33 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
err = group.Wait()
|
|
|
|
if err != nil {
|
2022-01-14 15:51:40 +00:00
|
|
|
return UploadLimit{}, ErrProjectUsage.Wrap(err)
|
2021-12-14 13:49:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 15:51:40 +00:00
|
|
|
if segmentUsage >= limit.SegmentsLimit {
|
|
|
|
limit.ExceedsSegments = true
|
2021-12-14 13:49:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 15:51:40 +00:00
|
|
|
if storageUsage >= limit.StorageLimit.Int64() {
|
|
|
|
limit.ExceedsStorage = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return limit, nil
|
2021-12-14 13:49:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 12:58:15 +00:00
|
|
|
// GetProjectStorageTotals returns total amount of storage used by project.
|
2020-12-21 17:27:12 +00:00
|
|
|
//
|
|
|
|
// It can return one of the following errors returned by
|
|
|
|
// storj.io/storj/satellite/accounting.Cache.GetProjectStorageUsage except the
|
|
|
|
// ErrKeyNotFound, wrapped by ErrProjectUsage.
|
2019-12-12 12:58:15 +00:00
|
|
|
func (usage *Service) GetProjectStorageTotals(ctx context.Context, projectID uuid.UUID) (total int64, err error) {
|
|
|
|
defer mon.Task()(&ctx, projectID)(&err)
|
2019-06-04 12:36:27 +01:00
|
|
|
|
2019-10-31 17:27:38 +00:00
|
|
|
total, err = usage.liveAccounting.GetProjectStorageUsage(ctx, projectID)
|
2020-12-21 17:27:12 +00:00
|
|
|
if ErrKeyNotFound.Has(err) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2019-10-31 17:27:38 +00:00
|
|
|
|
|
|
|
return total, ErrProjectUsage.Wrap(err)
|
2019-05-28 16:36:52 +01:00
|
|
|
}
|
|
|
|
|
2019-12-12 12:58:15 +00:00
|
|
|
// GetProjectBandwidthTotals returns total amount of allocated bandwidth used for past 30 days.
|
|
|
|
func (usage *Service) GetProjectBandwidthTotals(ctx context.Context, projectID uuid.UUID) (_ int64, err error) {
|
|
|
|
defer mon.Task()(&ctx, projectID)(&err)
|
|
|
|
|
2020-03-11 22:00:58 +00:00
|
|
|
// from the beginning of the current month
|
2020-05-04 19:05:08 +01:00
|
|
|
year, month, _ := usage.nowFn().Date()
|
2019-12-12 12:58:15 +00:00
|
|
|
|
2021-09-17 18:53:44 +01:00
|
|
|
total, err := usage.projectAccountingDB.GetProjectBandwidth(ctx, projectID, year, month, 1, usage.asOfSystemInterval)
|
2019-12-12 12:58:15 +00:00
|
|
|
return total, ErrProjectUsage.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2021-05-25 14:12:01 +01:00
|
|
|
// GetProjectBandwidth returns project allocated bandwidth for the specified year, month and day.
|
|
|
|
func (usage *Service) GetProjectBandwidth(ctx context.Context, projectID uuid.UUID, year int, month time.Month, day int) (_ int64, err error) {
|
2020-05-01 14:24:12 +01:00
|
|
|
defer mon.Task()(&ctx, projectID)(&err)
|
|
|
|
|
2021-05-25 18:43:47 +01:00
|
|
|
total, err := usage.projectAccountingDB.GetProjectBandwidth(ctx, projectID, year, month, day, usage.asOfSystemInterval)
|
2020-05-01 14:24:12 +01:00
|
|
|
return total, ErrProjectUsage.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2021-12-14 13:49:33 +00:00
|
|
|
// GetProjectSegments returns project allocated segment usage.
|
|
|
|
func (usage *Service) GetProjectSegments(ctx context.Context, projectID uuid.UUID) (_ int64, err error) {
|
|
|
|
defer mon.Task()(&ctx, projectID)(&err)
|
|
|
|
|
|
|
|
total, err := usage.metabaseDB.GetProjectSegmentCount(ctx, metabase.GetProjectSegmentCount{
|
|
|
|
ProjectID: projectID,
|
2022-01-21 13:38:31 +00:00
|
|
|
|
|
|
|
AsOfSystemInterval: usage.asOfSystemInterval,
|
2021-12-14 13:49:33 +00:00
|
|
|
})
|
|
|
|
return total, ErrProjectUsage.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-12-12 12:58:15 +00:00
|
|
|
// GetProjectStorageLimit returns current project storage limit.
|
|
|
|
func (usage *Service) GetProjectStorageLimit(ctx context.Context, projectID uuid.UUID) (_ memory.Size, err error) {
|
|
|
|
defer mon.Task()(&ctx, projectID)(&err)
|
2020-09-09 20:20:44 +01:00
|
|
|
return usage.projectLimitCache.GetProjectStorageLimit(ctx, projectID)
|
2019-12-12 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetProjectBandwidthLimit returns current project bandwidth limit.
|
|
|
|
func (usage *Service) GetProjectBandwidthLimit(ctx context.Context, projectID uuid.UUID) (_ memory.Size, err error) {
|
|
|
|
defer mon.Task()(&ctx, projectID)(&err)
|
2020-09-09 20:20:44 +01:00
|
|
|
return usage.projectLimitCache.GetProjectBandwidthLimit(ctx, projectID)
|
2019-12-12 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 15:06:20 +00:00
|
|
|
// GetProjectSegmentLimit returns current project segment limit.
|
|
|
|
func (usage *Service) GetProjectSegmentLimit(ctx context.Context, projectID uuid.UUID) (_ int64, err error) {
|
|
|
|
defer mon.Task()(&ctx, projectID)(&err)
|
|
|
|
return usage.projectLimitCache.GetProjectSegmentLimit(ctx, projectID)
|
|
|
|
}
|
|
|
|
|
2020-01-07 10:41:19 +00:00
|
|
|
// UpdateProjectLimits sets new value for project's bandwidth and storage limit.
|
2021-12-03 15:06:20 +00:00
|
|
|
// TODO remove because it's not used.
|
2020-01-07 10:41:19 +00:00
|
|
|
func (usage *Service) UpdateProjectLimits(ctx context.Context, projectID uuid.UUID, limit memory.Size) (err error) {
|
|
|
|
defer mon.Task()(&ctx, projectID)(&err)
|
|
|
|
|
|
|
|
return ErrProjectUsage.Wrap(usage.projectAccountingDB.UpdateProjectUsageLimit(ctx, projectID, limit))
|
|
|
|
}
|
|
|
|
|
2020-06-02 00:19:10 +01:00
|
|
|
// GetProjectBandwidthUsage get the current bandwidth usage from cache.
|
2020-12-21 17:27:12 +00:00
|
|
|
//
|
|
|
|
// It can return one of the following errors returned by
|
|
|
|
// storj.io/storj/satellite/accounting.Cache.GetProjectBandwidthUsage, wrapped
|
|
|
|
// by ErrProjectUsage.
|
2020-06-02 00:19:10 +01:00
|
|
|
func (usage *Service) GetProjectBandwidthUsage(ctx context.Context, projectID uuid.UUID) (currentUsed int64, err error) {
|
|
|
|
return usage.liveAccounting.GetProjectBandwidthUsage(ctx, projectID, usage.nowFn())
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateProjectBandwidthUsage increments the bandwidth cache key for a specific project.
|
2020-12-21 17:27:12 +00:00
|
|
|
//
|
|
|
|
// It can return one of the following errors returned by
|
|
|
|
// storj.io/storj/satellite/accounting.Cache.UpdatProjectBandwidthUsage, wrapped
|
|
|
|
// by ErrProjectUsage.
|
2020-06-02 00:19:10 +01:00
|
|
|
func (usage *Service) UpdateProjectBandwidthUsage(ctx context.Context, projectID uuid.UUID, increment int64) (err error) {
|
|
|
|
return usage.liveAccounting.UpdateProjectBandwidthUsage(ctx, projectID, increment, usage.bandwidthCacheTTL, usage.nowFn())
|
|
|
|
}
|
|
|
|
|
2021-11-25 12:53:52 +00:00
|
|
|
// GetProjectSegmentUsage get the current segment usage from cache.
|
|
|
|
//
|
|
|
|
// It can return one of the following errors returned by
|
|
|
|
// storj.io/storj/satellite/accounting.Cache.GetProjectSegmentUsage.
|
|
|
|
func (usage *Service) GetProjectSegmentUsage(ctx context.Context, projectID uuid.UUID) (currentUsed int64, err error) {
|
|
|
|
return usage.liveAccounting.GetProjectSegmentUsage(ctx, projectID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateProjectSegmentUsage increments the segment cache key for a specific project.
|
|
|
|
//
|
|
|
|
// It can return one of the following errors returned by
|
|
|
|
// storj.io/storj/satellite/accounting.Cache.UpdatProjectSegmentUsage.
|
|
|
|
func (usage *Service) UpdateProjectSegmentUsage(ctx context.Context, projectID uuid.UUID, increment int64) (err error) {
|
2022-05-12 14:53:08 +01:00
|
|
|
return usage.liveAccounting.UpdateProjectSegmentUsage(ctx, projectID, increment)
|
2021-11-25 12:53:52 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:36:52 +01:00
|
|
|
// AddProjectStorageUsage lets the live accounting know that the given
|
2019-10-31 17:27:38 +00:00
|
|
|
// project has just added spaceUsed bytes of storage (from the user's
|
|
|
|
// perspective; i.e. segment size).
|
2020-12-21 17:27:12 +00:00
|
|
|
//
|
|
|
|
// It can return one of the following errors returned by
|
|
|
|
// storj.io/storj/satellite/accounting.Cache.AddProjectStorageUsage, wrapped by
|
|
|
|
// ErrProjectUsage.
|
2019-10-31 17:27:38 +00:00
|
|
|
func (usage *Service) AddProjectStorageUsage(ctx context.Context, projectID uuid.UUID, spaceUsed int64) (err error) {
|
|
|
|
defer mon.Task()(&ctx, projectID)(&err)
|
|
|
|
return usage.liveAccounting.AddProjectStorageUsage(ctx, projectID, spaceUsed)
|
2019-04-02 19:21:18 +01:00
|
|
|
}
|
2020-05-04 19:05:08 +01:00
|
|
|
|
|
|
|
// SetNow allows tests to have the Service act as if the current time is whatever they want.
|
|
|
|
func (usage *Service) SetNow(now func() time.Time) {
|
|
|
|
usage.nowFn = now
|
|
|
|
}
|