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"
|
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 (
|
|
|
|
// ErrProjectUsage general error for project usage
|
|
|
|
ErrProjectUsage = errs.Class("project usage error")
|
|
|
|
)
|
|
|
|
|
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
|
2019-05-28 16:36:52 +01:00
|
|
|
maxAlphaUsage memory.Size
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// NewService created new instance of project usage service.
|
|
|
|
func NewService(projectAccountingDB ProjectAccounting, liveAccounting Cache, maxAlphaUsage memory.Size) *Service {
|
|
|
|
return &Service{
|
2019-05-28 16:36:52 +01:00
|
|
|
projectAccountingDB: projectAccountingDB,
|
|
|
|
liveAccounting: liveAccounting,
|
|
|
|
maxAlphaUsage: maxAlphaUsage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2019-04-02 19:21:18 +01:00
|
|
|
// Ref: https://storjlabs.atlassian.net/browse/V3-1274
|
2019-11-15 14:27:44 +00:00
|
|
|
func (usage *Service) ExceedsBandwidthUsage(ctx context.Context, projectID uuid.UUID, bucketID []byte) (_ 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
|
|
|
|
|
|
|
|
// TODO(michal): to reduce db load, consider using a cache to retrieve the project.UsageLimit value if needed
|
|
|
|
group.Go(func() error {
|
2019-12-12 12:58:15 +00:00
|
|
|
var err error
|
|
|
|
limit, err = usage.GetProjectBandwidthLimit(ctx, projectID)
|
2019-05-28 16:36:52 +01:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
var err error
|
2020-05-01 14:24:12 +01:00
|
|
|
bandwidthGetTotal, err = usage.GetCurrentBandwidthAllocated(ctx, projectID)
|
2019-05-28 16:36:52 +01:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
err = group.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return false, 0, ErrProjectUsage.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2020-03-11 10:51:22 +00:00
|
|
|
if bandwidthGetTotal >= 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
|
|
|
|
}
|
|
|
|
|
2019-10-31 17:27:38 +00:00
|
|
|
// ExceedsStorageUsage returns true if the storage usage for a project is currently over that project's limit.
|
2019-11-15 14:27:44 +00:00
|
|
|
func (usage *Service) ExceedsStorageUsage(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
|
2019-10-16 17:50:29 +01:00
|
|
|
var totalUsed int64
|
2019-05-28 16:36:52 +01:00
|
|
|
|
|
|
|
// TODO(michal): to reduce db load, consider using a cache to retrieve the project.UsageLimit value if needed
|
|
|
|
group.Go(func() error {
|
2019-12-12 12:58:15 +00:00
|
|
|
var err error
|
|
|
|
limit, err = usage.GetProjectStorageLimit(ctx, projectID)
|
2019-05-28 16:36:52 +01:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
var err error
|
2019-12-12 12:58:15 +00:00
|
|
|
totalUsed, err = usage.GetProjectStorageTotals(ctx, projectID)
|
2019-05-28 16:36:52 +01:00
|
|
|
return err
|
|
|
|
})
|
2019-12-12 12:58:15 +00:00
|
|
|
|
2019-05-28 16:36:52 +01:00
|
|
|
err = group.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return false, 0, ErrProjectUsage.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-10-31 17:27:38 +00:00
|
|
|
if totalUsed >= 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
|
|
|
|
}
|
|
|
|
|
2019-12-12 12:58:15 +00:00
|
|
|
// GetProjectStorageTotals returns total amount of storage used by project.
|
|
|
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
year, month, _ := time.Now().Date()
|
|
|
|
from := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC)
|
2019-12-12 12:58:15 +00:00
|
|
|
|
|
|
|
total, err := usage.projectAccountingDB.GetAllocatedBandwidthTotal(ctx, projectID, from)
|
|
|
|
return total, ErrProjectUsage.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2020-05-01 14:24:12 +01:00
|
|
|
// GetCurrentBandwidthAllocated returns allocated bandwidth for the current month
|
|
|
|
func (usage *Service) GetCurrentBandwidthAllocated(ctx context.Context, projectID uuid.UUID) (_ int64, err error) {
|
|
|
|
defer mon.Task()(&ctx, projectID)(&err)
|
|
|
|
|
|
|
|
total, err := usage.projectAccountingDB.GetCurrentBandwidthAllocated(ctx, projectID)
|
|
|
|
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)
|
|
|
|
|
|
|
|
limit, err := usage.projectAccountingDB.GetProjectStorageLimit(ctx, projectID)
|
|
|
|
if err != nil {
|
|
|
|
return 0, ErrProjectUsage.Wrap(err)
|
|
|
|
}
|
|
|
|
if limit == 0 {
|
|
|
|
return usage.maxAlphaUsage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return limit, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
limit, err := usage.projectAccountingDB.GetProjectBandwidthLimit(ctx, projectID)
|
|
|
|
if err != nil {
|
|
|
|
return 0, ErrProjectUsage.Wrap(err)
|
|
|
|
}
|
|
|
|
if limit == 0 {
|
|
|
|
return usage.maxAlphaUsage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return limit, nil
|
|
|
|
}
|
|
|
|
|
2020-01-07 10:41:19 +00:00
|
|
|
// UpdateProjectLimits sets new value for project's bandwidth and storage limit.
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
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).
|
|
|
|
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
|
|
|
}
|