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()
|
|
|
|
|
2022-06-20 17:46:27 +01:00
|
|
|
// ErrProjectUsage general error for project usage.
|
|
|
|
var ErrProjectUsage = errs.Class("project usage")
|
2019-05-28 16:36:52 +01:00
|
|
|
|
2022-06-22 12:33:03 +01:00
|
|
|
// ErrProjectLimitExceeded is used when the configured limits of a project are reached.
|
|
|
|
var ErrProjectLimitExceeded = errs.Class("project limit")
|
|
|
|
|
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)
|
|
|
|
|
2022-06-20 17:46:27 +01:00
|
|
|
var (
|
|
|
|
group errgroup.Group
|
|
|
|
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
|
2023-03-10 09:37:50 +00:00
|
|
|
limit, err = usage.projectLimitCache.GetBandwidthLimit(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()
|
2022-06-20 17:46:27 +01:00
|
|
|
bandwidthUsage, 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.
|
2022-06-20 17:46:27 +01:00
|
|
|
_, err = usage.liveAccounting.InsertProjectBandwidthUsage(ctx, projectID, bandwidthUsage, usage.bandwidthCacheTTL, usage.nowFn())
|
2020-06-02 00:19:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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.
|
2022-06-22 12:33:03 +01:00
|
|
|
// Supply nonzero headroom parameters to check if there is room for a new object.
|
|
|
|
func (usage *Service) ExceedsUploadLimits(
|
2022-07-04 19:44:58 +01:00
|
|
|
ctx context.Context, projectID uuid.UUID, storageSizeHeadroom int64, segmentCountHeadroom int64) (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
|
|
|
|
|
2022-07-04 19:44:58 +01:00
|
|
|
group.Go(func() error {
|
|
|
|
var err error
|
2023-03-10 09:37:50 +00:00
|
|
|
limits, err := usage.projectLimitCache.GetLimits(ctx, projectID)
|
2022-09-12 12:54:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
limit.SegmentsLimit = *limits.Segments
|
|
|
|
limit.StorageLimit = memory.Size(*limits.Usage)
|
|
|
|
return nil
|
2022-07-04 19:44:58 +01:00
|
|
|
})
|
2022-09-12 12:54:12 +01:00
|
|
|
|
2022-07-04 19:44:58 +01:00
|
|
|
group.Go(func() error {
|
|
|
|
var err error
|
|
|
|
segmentUsage, err = usage.liveAccounting.GetProjectSegmentUsage(ctx, projectID)
|
|
|
|
// Verify If the cache key was not found
|
|
|
|
if err != nil && ErrKeyNotFound.Has(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
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
|
|
|
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-06-22 12:33:03 +01:00
|
|
|
limit.ExceedsSegments = (segmentUsage + segmentCountHeadroom) > limit.SegmentsLimit
|
|
|
|
limit.ExceedsStorage = (storageUsage + storageSizeHeadroom) > limit.StorageLimit.Int64()
|
|
|
|
|
|
|
|
return limit, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddProjectUsageUpToLimit increases segment and storage usage up to the projects limit.
|
|
|
|
// If the limit is exceeded, neither usage is increased and accounting.ErrProjectLimitExceeded is returned.
|
|
|
|
func (usage *Service) AddProjectUsageUpToLimit(ctx context.Context, projectID uuid.UUID, storage int64, segments int64) (err error) {
|
|
|
|
defer mon.Task()(&ctx, projectID)(&err)
|
|
|
|
|
2023-03-10 09:37:50 +00:00
|
|
|
limits, err := usage.projectLimitCache.GetLimits(ctx, projectID)
|
2022-06-22 12:33:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-12-14 13:49:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 12:33:03 +01:00
|
|
|
err = usage.liveAccounting.AddProjectStorageUsageUpToLimit(ctx, projectID, storage, *limits.Usage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-01-14 15:51:40 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 12:33:03 +01:00
|
|
|
err = usage.liveAccounting.AddProjectSegmentUsageUpToLimit(ctx, projectID, segments, *limits.Segments)
|
|
|
|
if ErrProjectLimitExceeded.Has(err) {
|
|
|
|
// roll back storage increase
|
|
|
|
err = usage.liveAccounting.AddProjectStorageUsage(ctx, projectID, -1*storage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
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)
|
|
|
|
}
|
|
|
|
|
2023-02-15 17:54:22 +00:00
|
|
|
// GetProjectSegmentTotals returns total amount of allocated segments used for past 30 days.
|
|
|
|
func (usage *Service) GetProjectSegmentTotals(ctx context.Context, projectID uuid.UUID) (total int64, err error) {
|
|
|
|
defer mon.Task()(&ctx, projectID)(&err)
|
|
|
|
|
|
|
|
total, err = usage.liveAccounting.GetProjectSegmentUsage(ctx, projectID)
|
|
|
|
if ErrKeyNotFound.Has(err) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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)
|
2023-03-10 09:37:50 +00:00
|
|
|
limits, err := usage.projectLimitCache.GetLimits(ctx, projectID)
|
2022-09-12 12:54:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, ErrProjectUsage.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return memory.Size(*limits.Usage), nil
|
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)
|
2023-03-10 09:37:50 +00:00
|
|
|
return usage.projectLimitCache.GetBandwidthLimit(ctx, projectID)
|
2019-12-12 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 17:54:22 +00:00
|
|
|
// GetProjectSegmentLimit returns current project segment limit.
|
|
|
|
func (usage *Service) GetProjectSegmentLimit(ctx context.Context, projectID uuid.UUID) (_ memory.Size, err error) {
|
|
|
|
defer mon.Task()(&ctx, projectID)(&err)
|
2023-03-10 09:37:50 +00:00
|
|
|
limits, err := usage.projectLimitCache.GetLimits(ctx, projectID)
|
2023-02-15 17:54:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, ErrProjectUsage.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return memory.Size(*limits.Usage), nil
|
|
|
|
}
|
|
|
|
|
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
|
2022-06-22 12:33:03 +01:00
|
|
|
// storj.io/storj/satellite/accounting.Cache.UpdateProjectBandwidthUsage, wrapped
|
2020-12-21 17:27:12 +00:00
|
|
|
// 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
|
|
|
|
}
|