satellite/accounting: use single call to get project limits

segment and usage limits now taken by single call

Closes https://github.com/storj/team-metainfo/issues/124

Change-Id: I55e05e199d2575cb8d985f4d75ba061002c97f0f
This commit is contained in:
Qweder93 2022-09-12 14:54:12 +03:00 committed by Storj Robot
parent 601cadb92c
commit 04411e6871
3 changed files with 31 additions and 78 deletions

View File

@ -98,20 +98,6 @@ func (c *ProjectLimitCache) Get(ctx context.Context, projectID uuid.UUID) (Proje
return limits, nil
}
// GetProjectStorageLimit returns the storage usage limit for a project ID.
func (c *ProjectLimitCache) GetProjectStorageLimit(ctx context.Context, projectID uuid.UUID) (_ memory.Size, err error) {
defer mon.Task()(&ctx)(&err)
projectLimits, err := c.Get(ctx, projectID)
if err != nil {
return 0, err
}
if projectLimits.Usage == nil {
return c.defaultMaxUsage, nil
}
return memory.Size(*projectLimits.Usage), nil
}
// GetProjectBandwidthLimit return the bandwidth usage limit for a project ID.
func (c *ProjectLimitCache) GetProjectBandwidthLimit(ctx context.Context, projectID uuid.UUID) (_ memory.Size, err error) {
defer mon.Task()(&ctx)(&err)
@ -124,18 +110,3 @@ func (c *ProjectLimitCache) GetProjectBandwidthLimit(ctx context.Context, projec
}
return memory.Size(*projectLimits.Bandwidth), nil
}
// GetProjectSegmentLimit returns the segment limit for a project ID.
func (c *ProjectLimitCache) GetProjectSegmentLimit(ctx context.Context, projectID uuid.UUID) (_ int64, err error) {
defer mon.Task()(&ctx)(&err)
projectLimits, err := c.Get(ctx, projectID)
if err != nil {
return 0, err
}
if projectLimits.Segments == nil {
return c.defaultMaxSegments, nil
}
return *projectLimits.Segments, nil
}

View File

@ -10,7 +10,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"storj.io/common/memory"
"storj.io/common/testcontext"
"storj.io/common/testrand"
"storj.io/common/uuid"
@ -86,31 +85,21 @@ func TestProjectLimitCache(t *testing.T) {
assert.Equal(t, accounting.ProjectLimits{}, actualLimitsFromDB)
// storage
actualStorageLimitFromCache, err := projectLimitCache.GetProjectStorageLimit(ctx, projectID)
_, err = projectLimitCache.GetProjectLimits(ctx, projectID)
assert.Error(t, err)
assert.Equal(t, memory.Size(errorLimit), actualStorageLimitFromCache)
actualStorageLimitFromSvc, err := projectUsageSvc.GetProjectStorageLimit(ctx, projectID)
assert.Error(t, err)
assert.Equal(t, memory.Size(errorLimit), actualStorageLimitFromSvc)
assert.EqualValues(t, errorLimit, actualStorageLimitFromSvc)
// bandwidth
actualBandwidthLimitFromCache, err := projectLimitCache.GetProjectBandwidthLimit(ctx, projectID)
assert.Error(t, err)
assert.Equal(t, memory.Size(errorLimit), actualBandwidthLimitFromCache)
assert.EqualValues(t, errorLimit, actualBandwidthLimitFromCache)
actualBandwidthLimitFromSvc, err := projectUsageSvc.GetProjectBandwidthLimit(ctx, projectID)
assert.Error(t, err)
assert.Equal(t, memory.Size(errorLimit), actualBandwidthLimitFromSvc)
// segments
actualSegmentLimitFromCache, err := projectLimitCache.GetProjectSegmentLimit(ctx, projectID)
assert.Error(t, err)
assert.EqualValues(t, errorLimit, actualSegmentLimitFromCache)
actualSegmentLimitFromSvc, err := projectUsageSvc.GetProjectSegmentLimit(ctx, projectID)
assert.Error(t, err)
assert.EqualValues(t, errorLimit, actualSegmentLimitFromSvc)
assert.EqualValues(t, errorLimit, actualBandwidthLimitFromSvc)
})
t.Run("default limits", func(t *testing.T) {
@ -128,13 +117,13 @@ func TestProjectLimitCache(t *testing.T) {
assert.NoError(t, err)
assert.Nil(t, actualStorageLimitFromDB)
actualStorageLimitFromCache, err := projectLimitCache.GetProjectStorageLimit(ctx, testProject.ID)
actualLimitFromCache, err := projectLimitCache.GetProjectLimits(ctx, testProject.ID)
assert.NoError(t, err)
assert.Equal(t, memory.Size(*dbDefaultLimits.Usage), actualStorageLimitFromCache)
assert.EqualValues(t, *dbDefaultLimits.Usage, *actualLimitFromCache.Usage)
actualStorageLimitFromSvc, err := projectUsageSvc.GetProjectStorageLimit(ctx, testProject.ID)
assert.NoError(t, err)
assert.Equal(t, memory.Size(*dbDefaultLimits.Usage), actualStorageLimitFromSvc)
assert.EqualValues(t, *dbDefaultLimits.Usage, actualStorageLimitFromSvc)
actualBandwidthLimitFromDB, err := accountingDB.GetProjectBandwidthLimit(ctx, testProject.ID)
assert.NoError(t, err)
@ -142,11 +131,11 @@ func TestProjectLimitCache(t *testing.T) {
actualBandwidthLimitFromCache, err := projectLimitCache.GetProjectBandwidthLimit(ctx, testProject.ID)
assert.NoError(t, err)
assert.Equal(t, memory.Size(*dbDefaultLimits.Bandwidth), actualBandwidthLimitFromCache)
assert.EqualValues(t, *dbDefaultLimits.Bandwidth, actualBandwidthLimitFromCache)
actualBandwidthLimitFromSvc, err := projectUsageSvc.GetProjectBandwidthLimit(ctx, testProject.ID)
assert.NoError(t, err)
assert.Equal(t, memory.Size(*dbDefaultLimits.Bandwidth), actualBandwidthLimitFromSvc)
assert.EqualValues(t, *dbDefaultLimits.Bandwidth, actualBandwidthLimitFromSvc)
})
t.Run("update limits in the database", func(t *testing.T) {
@ -159,7 +148,7 @@ func TestProjectLimitCache(t *testing.T) {
actualStorageLimitFromDB, err := accountingDB.GetProjectStorageLimit(ctx, testProject.ID)
assert.NoError(t, err)
require.Equal(t, int64(expectedUsageLimit), *actualStorageLimitFromDB)
require.EqualValues(t, expectedUsageLimit, *actualStorageLimitFromDB)
actualLimitsFromDB, err := accountingDB.GetProjectLimits(ctx, testProject.ID)
assert.NoError(t, err)
@ -169,39 +158,32 @@ func TestProjectLimitCache(t *testing.T) {
assert.Equal(t, accounting.ProjectLimits{Usage: &usageLimits, Bandwidth: &bwLimits, Segments: &segmentsLimits}, actualLimitsFromDB)
// storage
actualStorageLimitFromCache, err := projectLimitCache.GetProjectStorageLimit(ctx, testProject.ID)
actualLimitFromCache, err := projectLimitCache.GetProjectLimits(ctx, testProject.ID)
assert.NoError(t, err)
require.Equal(t, memory.Size(expectedUsageLimit), actualStorageLimitFromCache)
require.EqualValues(t, expectedUsageLimit, *actualLimitFromCache.Usage)
require.EqualValues(t, expectedSegmentLimit, *actualLimitFromCache.Segments)
actualStorageLimitFromSvc, err := projectUsageSvc.GetProjectStorageLimit(ctx, testProject.ID)
assert.NoError(t, err)
require.Equal(t, memory.Size(expectedUsageLimit), actualStorageLimitFromSvc)
require.EqualValues(t, expectedUsageLimit, actualStorageLimitFromSvc)
// bandwidth
actualBandwidthLimitFromDB, err := accountingDB.GetProjectBandwidthLimit(ctx, testProject.ID)
require.NoError(t, err)
require.Equal(t, int64(expectedBandwidthLimit), *actualBandwidthLimitFromDB)
require.EqualValues(t, expectedBandwidthLimit, *actualBandwidthLimitFromDB)
actualBandwidthLimitFromCache, err := projectLimitCache.GetProjectBandwidthLimit(ctx, testProject.ID)
assert.NoError(t, err)
require.Equal(t, memory.Size(expectedBandwidthLimit), actualBandwidthLimitFromCache)
require.EqualValues(t, expectedBandwidthLimit, actualBandwidthLimitFromCache)
actualBandwidthLimitFromSvc, err := projectUsageSvc.GetProjectBandwidthLimit(ctx, testProject.ID)
assert.NoError(t, err)
require.Equal(t, memory.Size(expectedBandwidthLimit), actualBandwidthLimitFromSvc)
require.EqualValues(t, expectedBandwidthLimit, actualBandwidthLimitFromSvc)
// segments
actualSegmentLimitFromDB, err := accountingDB.GetProjectSegmentLimit(ctx, testProject.ID)
require.NoError(t, err)
require.EqualValues(t, expectedSegmentLimit, *actualSegmentLimitFromDB)
actualSegmentLimitFromCache, err := projectLimitCache.GetProjectSegmentLimit(ctx, testProject.ID)
assert.NoError(t, err)
require.EqualValues(t, expectedSegmentLimit, actualSegmentLimitFromCache)
actualSegmentLimitFromSvc, err := projectUsageSvc.GetProjectSegmentLimit(ctx, testProject.ID)
assert.NoError(t, err)
require.EqualValues(t, expectedSegmentLimit, actualSegmentLimitFromSvc)
})
})
}

View File

@ -128,9 +128,15 @@ func (usage *Service) ExceedsUploadLimits(
group.Go(func() error {
var err error
limit.SegmentsLimit, err = usage.projectLimitCache.GetProjectSegmentLimit(ctx, projectID)
limits, err := usage.projectLimitCache.GetProjectLimits(ctx, projectID)
if err != nil {
return err
}
limit.SegmentsLimit = *limits.Segments
limit.StorageLimit = memory.Size(*limits.Usage)
return nil
})
group.Go(func() error {
var err error
segmentUsage, err = usage.liveAccounting.GetProjectSegmentUsage(ctx, projectID)
@ -141,11 +147,6 @@ func (usage *Service) ExceedsUploadLimits(
return err
})
group.Go(func() error {
var err error
limit.StorageLimit, err = usage.projectLimitCache.GetProjectStorageLimit(ctx, projectID)
return err
})
group.Go(func() error {
var err error
storageUsage, err = usage.GetProjectStorageTotals(ctx, projectID)
@ -228,7 +229,12 @@ func (usage *Service) GetProjectBandwidth(ctx context.Context, projectID uuid.UU
// 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)
return usage.projectLimitCache.GetProjectStorageLimit(ctx, projectID)
limits, err := usage.projectLimitCache.GetProjectLimits(ctx, projectID)
if err != nil {
return 0, ErrProjectUsage.Wrap(err)
}
return memory.Size(*limits.Usage), nil
}
// GetProjectBandwidthLimit returns current project bandwidth limit.
@ -237,12 +243,6 @@ func (usage *Service) GetProjectBandwidthLimit(ctx context.Context, projectID uu
return usage.projectLimitCache.GetProjectBandwidthLimit(ctx, projectID)
}
// 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)
}
// UpdateProjectLimits sets new value for project's bandwidth and storage limit.
// TODO remove because it's not used.
func (usage *Service) UpdateProjectLimits(ctx context.Context, projectID uuid.UUID, limit memory.Size) (err error) {