satellite/accounting: fix project limit cache usage

It looks that at some point we broke how project limits cache is used
and we were missing cache in most critical paths (upload/download).
This is fix for this issue.

I also adjusted cache methods naming.

Change-Id: Ic98372779a39365d0920fe3943f1f7a68b064173
This commit is contained in:
Michal Niewrzal 2023-03-10 10:37:50 +01:00 committed by Storj Robot
parent 0d981a6485
commit 0696256769
3 changed files with 46 additions and 46 deletions

View File

@ -62,8 +62,37 @@ func NewProjectLimitCache(db ProjectLimitDB, defaultMaxUsage, defaultMaxBandwidt
}
}
// GetProjectLimits returns current project limit for both storage and bandwidth.
func (c *ProjectLimitCache) GetProjectLimits(ctx context.Context, projectID uuid.UUID) (_ ProjectLimits, err error) {
// GetLimits returns the project limits from cache.
func (c *ProjectLimitCache) GetLimits(ctx context.Context, projectID uuid.UUID) (ProjectLimits, error) {
fn := func() (interface{}, error) {
return c.getProjectLimits(ctx, projectID)
}
projectLimits, err := c.state.Get(projectID.String(), fn)
if err != nil {
return ProjectLimits{}, ErrGetProjectLimitCache.Wrap(err)
}
limits, ok := projectLimits.(ProjectLimits)
if !ok {
return ProjectLimits{}, ErrProjectLimitType.New("cache Get error")
}
return limits, nil
}
// GetBandwidthLimit return the bandwidth usage limit for a project ID.
func (c *ProjectLimitCache) GetBandwidthLimit(ctx context.Context, projectID uuid.UUID) (_ memory.Size, err error) {
defer mon.Task()(&ctx)(&err)
projectLimits, err := c.GetLimits(ctx, projectID)
if err != nil {
return 0, err
}
if projectLimits.Bandwidth == nil {
return c.defaultMaxBandwidth, nil
}
return memory.Size(*projectLimits.Bandwidth), nil
}
// getProjectLimits returns project limits from DB.
func (c *ProjectLimitCache) getProjectLimits(ctx context.Context, projectID uuid.UUID) (_ ProjectLimits, err error) {
defer mon.Task()(&ctx, projectID)(&err)
projectLimits, err := c.projectLimitDB.GetProjectLimits(ctx, projectID)
@ -89,32 +118,3 @@ func (c *ProjectLimitCache) GetProjectLimits(ctx context.Context, projectID uuid
return projectLimits, nil
}
// Get returns the storage usage limit for a project ID.
func (c *ProjectLimitCache) Get(ctx context.Context, projectID uuid.UUID) (ProjectLimits, error) {
fn := func() (interface{}, error) {
return c.GetProjectLimits(ctx, projectID)
}
projectLimits, err := c.state.Get(projectID.String(), fn)
if err != nil {
return ProjectLimits{}, ErrGetProjectLimitCache.Wrap(err)
}
limits, ok := projectLimits.(ProjectLimits)
if !ok {
return ProjectLimits{}, ErrProjectLimitType.New("cache Get error")
}
return limits, 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)
projectLimits, err := c.Get(ctx, projectID)
if err != nil {
return 0, err
}
if projectLimits.Bandwidth == nil {
return c.defaultMaxBandwidth, nil
}
return memory.Size(*projectLimits.Bandwidth), nil
}

View File

@ -38,12 +38,12 @@ func TestProjectLimitCacheCallCount(t *testing.T) {
const expectedCallCount = 1
_, err = projectLimitCache.GetProjectBandwidthLimit(ctx, testProject.ID)
_, err = projectLimitCache.GetBandwidthLimit(ctx, testProject.ID)
require.NoError(t, err)
// if the data isn't in the cache we call into the database to get it
require.Equal(t, expectedCallCount, mdb.callCount)
_, err = projectLimitCache.GetProjectBandwidthLimit(ctx, testProject.ID)
_, err = projectLimitCache.GetBandwidthLimit(ctx, testProject.ID)
require.NoError(t, err)
// call count should still be 1 since the data is in the cache and we don't need
// to get it from the db
@ -85,7 +85,7 @@ func TestProjectLimitCache(t *testing.T) {
assert.Equal(t, accounting.ProjectLimits{}, actualLimitsFromDB)
// storage
_, err = projectLimitCache.GetProjectLimits(ctx, projectID)
_, err = projectLimitCache.GetLimits(ctx, projectID)
assert.Error(t, err)
actualStorageLimitFromSvc, err := projectUsageSvc.GetProjectStorageLimit(ctx, projectID)
@ -93,7 +93,7 @@ func TestProjectLimitCache(t *testing.T) {
assert.EqualValues(t, errorLimit, actualStorageLimitFromSvc)
// bandwidth
actualBandwidthLimitFromCache, err := projectLimitCache.GetProjectBandwidthLimit(ctx, projectID)
actualBandwidthLimitFromCache, err := projectLimitCache.GetBandwidthLimit(ctx, projectID)
assert.Error(t, err)
assert.EqualValues(t, errorLimit, actualBandwidthLimitFromCache)
@ -109,7 +109,7 @@ func TestProjectLimitCache(t *testing.T) {
Segments: &defaultSegmentLimit,
}, actualLimitsFromDB)
actualLimitsFromCache, err := projectLimitCache.GetProjectLimits(ctx, testProject.ID)
actualLimitsFromCache, err := projectLimitCache.GetLimits(ctx, testProject.ID)
assert.NoError(t, err)
assert.Equal(t, dbDefaultLimits, actualLimitsFromCache)
@ -117,7 +117,7 @@ func TestProjectLimitCache(t *testing.T) {
assert.NoError(t, err)
assert.Nil(t, actualStorageLimitFromDB)
actualLimitFromCache, err := projectLimitCache.GetProjectLimits(ctx, testProject.ID)
actualLimitFromCache, err := projectLimitCache.GetLimits(ctx, testProject.ID)
assert.NoError(t, err)
assert.EqualValues(t, *dbDefaultLimits.Usage, *actualLimitFromCache.Usage)
@ -129,7 +129,7 @@ func TestProjectLimitCache(t *testing.T) {
assert.NoError(t, err)
assert.Nil(t, actualBandwidthLimitFromDB)
actualBandwidthLimitFromCache, err := projectLimitCache.GetProjectBandwidthLimit(ctx, testProject.ID)
actualBandwidthLimitFromCache, err := projectLimitCache.GetBandwidthLimit(ctx, testProject.ID)
assert.NoError(t, err)
assert.EqualValues(t, *dbDefaultLimits.Bandwidth, actualBandwidthLimitFromCache)
@ -158,7 +158,7 @@ func TestProjectLimitCache(t *testing.T) {
assert.Equal(t, accounting.ProjectLimits{Usage: &usageLimits, Bandwidth: &bwLimits, Segments: &segmentsLimits}, actualLimitsFromDB)
// storage
actualLimitFromCache, err := projectLimitCache.GetProjectLimits(ctx, testProject.ID)
actualLimitFromCache, err := projectLimitCache.GetLimits(ctx, testProject.ID)
assert.NoError(t, err)
require.EqualValues(t, expectedUsageLimit, *actualLimitFromCache.Usage)
require.EqualValues(t, expectedSegmentLimit, *actualLimitFromCache.Segments)
@ -172,7 +172,7 @@ func TestProjectLimitCache(t *testing.T) {
require.NoError(t, err)
require.EqualValues(t, expectedBandwidthLimit, *actualBandwidthLimitFromDB)
actualBandwidthLimitFromCache, err := projectLimitCache.GetProjectBandwidthLimit(ctx, testProject.ID)
actualBandwidthLimitFromCache, err := projectLimitCache.GetBandwidthLimit(ctx, testProject.ID)
assert.NoError(t, err)
require.EqualValues(t, expectedBandwidthLimit, actualBandwidthLimitFromCache)

View File

@ -67,7 +67,7 @@ func (usage *Service) ExceedsBandwidthUsage(ctx context.Context, projectID uuid.
group.Go(func() error {
var err error
limit, err = usage.projectLimitCache.GetProjectBandwidthLimit(ctx, projectID)
limit, err = usage.projectLimitCache.GetBandwidthLimit(ctx, projectID)
return err
})
group.Go(func() error {
@ -128,7 +128,7 @@ func (usage *Service) ExceedsUploadLimits(
group.Go(func() error {
var err error
limits, err := usage.projectLimitCache.GetProjectLimits(ctx, projectID)
limits, err := usage.projectLimitCache.GetLimits(ctx, projectID)
if err != nil {
return err
}
@ -169,7 +169,7 @@ func (usage *Service) ExceedsUploadLimits(
func (usage *Service) AddProjectUsageUpToLimit(ctx context.Context, projectID uuid.UUID, storage int64, segments int64) (err error) {
defer mon.Task()(&ctx, projectID)(&err)
limits, err := usage.projectLimitCache.GetProjectLimits(ctx, projectID)
limits, err := usage.projectLimitCache.GetLimits(ctx, projectID)
if err != nil {
return err
}
@ -241,7 +241,7 @@ 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)
limits, err := usage.projectLimitCache.GetProjectLimits(ctx, projectID)
limits, err := usage.projectLimitCache.GetLimits(ctx, projectID)
if err != nil {
return 0, ErrProjectUsage.Wrap(err)
}
@ -252,13 +252,13 @@ func (usage *Service) GetProjectStorageLimit(ctx context.Context, projectID uuid
// 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)
return usage.projectLimitCache.GetProjectBandwidthLimit(ctx, projectID)
return usage.projectLimitCache.GetBandwidthLimit(ctx, projectID)
}
// 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)
limits, err := usage.projectLimitCache.GetProjectLimits(ctx, projectID)
limits, err := usage.projectLimitCache.GetLimits(ctx, projectID)
if err != nil {
return 0, ErrProjectUsage.Wrap(err)
}