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-04-02 19:21:18 +01:00
|
|
|
"storj.io/storj/internal/memory"
|
2019-05-28 16:36:52 +01:00
|
|
|
"storj.io/storj/pkg/accounting/live"
|
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")
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProjectUsage defines project usage
|
|
|
|
type ProjectUsage struct {
|
|
|
|
projectAccountingDB ProjectAccounting
|
|
|
|
liveAccounting live.Service
|
|
|
|
maxAlphaUsage memory.Size
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewProjectUsage created new instance of project usage service
|
|
|
|
func NewProjectUsage(projectAccountingDB ProjectAccounting, liveAccounting live.Service, maxAlphaUsage memory.Size) *ProjectUsage {
|
|
|
|
return &ProjectUsage{
|
|
|
|
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-05-28 16:36:52 +01:00
|
|
|
func (usage *ProjectUsage) 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
|
|
|
|
limit = usage.maxAlphaUsage
|
|
|
|
|
|
|
|
// TODO(michal): to reduce db load, consider using a cache to retrieve the project.UsageLimit value if needed
|
|
|
|
group.Go(func() error {
|
|
|
|
projectLimit, err := usage.projectAccountingDB.GetProjectUsageLimits(ctx, projectID)
|
|
|
|
if projectLimit > 0 {
|
|
|
|
limit = projectLimit
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
var err error
|
|
|
|
from := time.Now().AddDate(0, 0, -AverageDaysInMonth) // past 30 days
|
|
|
|
bandwidthGetTotal, err = usage.projectAccountingDB.GetAllocatedBandwidthTotal(ctx, bucketID, from)
|
|
|
|
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
|
|
|
|
func (usage *ProjectUsage) 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
|
|
|
|
var inlineTotal, remoteTotal int64
|
|
|
|
limit = usage.maxAlphaUsage
|
|
|
|
|
|
|
|
// TODO(michal): to reduce db load, consider using a cache to retrieve the project.UsageLimit value if needed
|
|
|
|
group.Go(func() error {
|
|
|
|
projectLimit, err := usage.projectAccountingDB.GetProjectUsageLimits(ctx, projectID)
|
|
|
|
if projectLimit > 0 {
|
|
|
|
limit = projectLimit
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
var err error
|
|
|
|
inlineTotal, remoteTotal, err = usage.getProjectStorageTotals(ctx, projectID)
|
|
|
|
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 inlineTotal+remoteTotal >= 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-06-04 12:36:27 +01:00
|
|
|
func (usage *ProjectUsage) getProjectStorageTotals(ctx context.Context, projectID uuid.UUID) (inline int64, remote int64, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-05-28 16:36:52 +01:00
|
|
|
lastCountInline, lastCountRemote, err := usage.projectAccountingDB.GetStorageTotals(ctx, projectID)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
rtInline, rtRemote, err := usage.liveAccounting.GetProjectStorageUsage(ctx, projectID)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return lastCountInline + rtInline, lastCountRemote + rtRemote, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-06-04 12:36:27 +01:00
|
|
|
func (usage *ProjectUsage) AddProjectStorageUsage(ctx context.Context, projectID uuid.UUID, inlineSpaceUsed, remoteSpaceUsed int64) (err error) {
|
|
|
|
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
|
|
|
}
|