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"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2019-10-04 20:09:52 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2019-05-28 16:36:52 +01:00
|
|
|
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/memory"
|
2019-04-02 19:21:18 +01:00
|
|
|
)
|
|
|
|
|
2019-10-04 20:09:52 +01:00
|
|
|
var mon = monkit.Package()
|
|
|
|
|
2019-04-02 19:21:18 +01:00
|
|
|
const (
|
|
|
|
// AverageDaysInMonth is how many days in a month
|
|
|
|
AverageDaysInMonth = 30
|
2019-04-08 22:35:54 +01:00
|
|
|
// ExpansionFactor is the expansion for redundancy, based on the default
|
|
|
|
// redundancy scheme for the uplink.
|
|
|
|
ExpansionFactor = 3
|
2019-04-02 19:21:18 +01:00
|
|
|
)
|
|
|
|
|
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
|
2019-12-12 12:58:15 +00:00
|
|
|
bandwidthGetTotal, err = usage.GetProjectBandwidthTotals(ctx, projectID)
|
2019-05-28 16:36:52 +01:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
err = group.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return false, 0, ErrProjectUsage.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
maxUsage := limit.Int64() * int64(ExpansionFactor)
|
2019-04-08 22:35:54 +01:00
|
|
|
if bandwidthGetTotal >= maxUsage {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExceedsStorageUsage returns true if the storage 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.
|
|
|
|
// Ref: https://storjlabs.atlassian.net/browse/V3-1274
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
maxUsage := limit.Int64() * int64(ExpansionFactor)
|
2019-10-16 17:50:29 +01:00
|
|
|
if totalUsed >= maxUsage {
|
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-05-28 16:36:52 +01:00
|
|
|
lastCountInline, lastCountRemote, err := usage.projectAccountingDB.GetStorageTotals(ctx, projectID)
|
|
|
|
if err != nil {
|
2019-12-12 12:58:15 +00:00
|
|
|
return 0, ErrProjectUsage.Wrap(err)
|
2019-05-28 16:36:52 +01:00
|
|
|
}
|
2019-10-16 17:50:29 +01:00
|
|
|
cachedTotal, err := usage.liveAccounting.GetProjectStorageUsage(ctx, projectID)
|
2019-05-28 16:36:52 +01:00
|
|
|
if err != nil {
|
2019-12-12 12:58:15 +00:00
|
|
|
return 0, ErrProjectUsage.Wrap(err)
|
2019-05-28 16:36:52 +01:00
|
|
|
}
|
2019-10-16 17:50:29 +01:00
|
|
|
return lastCountInline + lastCountRemote + cachedTotal, nil
|
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)
|
|
|
|
|
|
|
|
from := time.Now().AddDate(0, 0, -AverageDaysInMonth) // past 30 days
|
|
|
|
|
|
|
|
total, err := usage.projectAccountingDB.GetAllocatedBandwidthTotal(ctx, projectID, from)
|
|
|
|
return total, ErrProjectUsage.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:36:52 +01:00
|
|
|
// AddProjectStorageUsage lets the live accounting know that the given
|
|
|
|
// project has just added inlineSpaceUsed bytes of inline space usage
|
|
|
|
// and remoteSpaceUsed bytes of remote space usage.
|
2019-11-15 14:27:44 +00:00
|
|
|
func (usage *Service) AddProjectStorageUsage(ctx context.Context, projectID uuid.UUID, inlineSpaceUsed, remoteSpaceUsed int64) (err error) {
|
2019-06-04 12:36:27 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-05-28 16:36:52 +01:00
|
|
|
return usage.liveAccounting.AddProjectStorageUsage(ctx, projectID, inlineSpaceUsed, remoteSpaceUsed)
|
2019-04-02 19:21:18 +01:00
|
|
|
}
|