2019-05-10 02:39:21 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-10-16 17:50:29 +01:00
|
|
|
package live_test
|
2019-05-10 02:39:21 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
2020-12-21 17:27:12 +00:00
|
|
|
"time"
|
2019-05-10 02:39:21 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-07-31 13:09:45 +01:00
|
|
|
"go.uber.org/zap/zaptest"
|
2019-05-10 02:39:21 +01:00
|
|
|
"golang.org/x/sync/errgroup"
|
2019-06-26 11:38:51 +01:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2021-03-09 17:42:10 +00:00
|
|
|
"storj.io/storj/private/testredis"
|
2019-10-16 17:50:29 +01:00
|
|
|
"storj.io/storj/satellite/accounting"
|
|
|
|
"storj.io/storj/satellite/accounting/live"
|
2019-05-10 02:39:21 +01:00
|
|
|
)
|
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
func TestAddGetProjectStorageAndBandwidthUsage(t *testing.T) {
|
2019-10-16 17:50:29 +01:00
|
|
|
tests := []struct {
|
|
|
|
backend string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
backend: "redis",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2021-03-09 17:42:10 +00:00
|
|
|
redis, err := testredis.Start(ctx)
|
2019-10-16 17:50:29 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-31 18:28:42 +00:00
|
|
|
defer ctx.Check(redis.Close)
|
2019-10-16 17:50:29 +01:00
|
|
|
|
|
|
|
for _, tt := range tests {
|
2020-12-21 17:27:12 +00:00
|
|
|
tt := tt
|
|
|
|
t.Run(tt.backend, func(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
|
|
|
|
var config live.Config
|
|
|
|
if tt.backend == "redis" {
|
|
|
|
config = live.Config{
|
|
|
|
StorageBackend: "redis://" + redis.Addr() + "?db=0",
|
|
|
|
}
|
2019-10-16 17:50:29 +01:00
|
|
|
}
|
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
cache, err := live.NewCache(zaptest.NewLogger(t).Named("live-accounting"), config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(cache.Close)
|
2019-10-16 17:50:29 +01:00
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
populatedData, err := populateCache(ctx, cache)
|
2019-10-16 17:50:29 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
// make sure all of the "projects" got all space updates and got right totals
|
|
|
|
for _, pdata := range populatedData {
|
|
|
|
pdata := pdata
|
|
|
|
|
|
|
|
t.Run("storage", func(t *testing.T) {
|
|
|
|
spaceUsed, err := cache.GetProjectStorageUsage(ctx, pdata.projectID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equalf(t, pdata.storageSum, spaceUsed, "projectID %v", pdata.projectID)
|
|
|
|
|
|
|
|
// upate it again and check
|
|
|
|
negativeVal := -(rand.Int63n(pdata.storageSum) + 1)
|
|
|
|
pdata.storageSum += negativeVal
|
|
|
|
err = cache.AddProjectStorageUsage(ctx, pdata.projectID, negativeVal)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
spaceUsed, err = cache.GetProjectStorageUsage(ctx, pdata.projectID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, pdata.storageSum, spaceUsed)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bandwidth", func(t *testing.T) {
|
|
|
|
bandwidthUsed, err := cache.GetProjectBandwidthUsage(ctx, pdata.projectID, pdata.bandwidthNow)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equalf(t, pdata.bandwidthSum, bandwidthUsed, "projectID %v", pdata.projectID)
|
|
|
|
|
|
|
|
// upate it again and check
|
|
|
|
negativeVal := -(rand.Int63n(pdata.bandwidthSum) + 1)
|
|
|
|
pdata.bandwidthSum += negativeVal
|
|
|
|
err = cache.UpdateProjectBandwidthUsage(ctx, pdata.projectID, negativeVal, time.Second*2, pdata.bandwidthNow)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
bandwidthUsed, err = cache.GetProjectBandwidthUsage(ctx, pdata.projectID, pdata.bandwidthNow)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, pdata.bandwidthSum, bandwidthUsed)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAllProjectTotals(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
backend string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
backend: "redis",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2021-03-09 17:42:10 +00:00
|
|
|
redis, err := testredis.Start(ctx)
|
2020-12-21 17:27:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(redis.Close)
|
2019-10-16 17:50:29 +01:00
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.backend, func(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
|
|
|
|
var config live.Config
|
|
|
|
if tt.backend == "redis" {
|
|
|
|
config = live.Config{
|
|
|
|
StorageBackend: "redis://" + redis.Addr() + "?db=0",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cache, err := live.NewCache(zaptest.NewLogger(t).Named("live-accounting"), config)
|
2019-10-31 17:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2020-12-21 17:27:12 +00:00
|
|
|
defer ctx.Check(cache.Close)
|
|
|
|
|
|
|
|
projectIDs := make([]uuid.UUID, 1000)
|
|
|
|
for i := range projectIDs {
|
|
|
|
projectIDs[i] = testrand.UUID()
|
|
|
|
err := cache.AddProjectStorageUsage(ctx, projectIDs[i], int64(i))
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2019-10-31 17:27:38 +00:00
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
projectTotals, err := cache.GetAllProjectTotals(ctx)
|
2019-10-16 17:50:29 +01:00
|
|
|
require.NoError(t, err)
|
2020-12-21 17:27:12 +00:00
|
|
|
require.Len(t, projectTotals, len(projectIDs))
|
|
|
|
|
|
|
|
// make sure each project ID and total was received
|
|
|
|
for _, projID := range projectIDs {
|
|
|
|
total, err := cache.GetProjectStorageUsage(ctx, projID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, total, projectTotals[projID])
|
|
|
|
}
|
|
|
|
})
|
2019-10-16 17:50:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
func TestLiveAccountingCache_ProjectBandwidthUsage_expiration(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
backend string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
backend: "redis",
|
|
|
|
},
|
|
|
|
}
|
2019-10-16 17:50:29 +01:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2021-03-09 17:42:10 +00:00
|
|
|
redis, err := testredis.Start(ctx)
|
2019-10-16 17:50:29 +01:00
|
|
|
require.NoError(t, err)
|
2020-12-21 17:27:12 +00:00
|
|
|
|
2020-01-31 18:28:42 +00:00
|
|
|
defer ctx.Check(redis.Close)
|
2019-10-16 17:50:29 +01:00
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.backend, func(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
|
|
|
|
var config live.Config
|
|
|
|
if tt.backend == "redis" {
|
|
|
|
config = live.Config{
|
|
|
|
StorageBackend: "redis://" + redis.Addr() + "?db=0",
|
|
|
|
}
|
|
|
|
}
|
2019-10-16 17:50:29 +01:00
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
cache, err := live.NewCache(zaptest.NewLogger(t).Named("live-accounting"), config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(cache.Close)
|
2019-10-16 17:50:29 +01:00
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
var (
|
|
|
|
projectID = testrand.UUID()
|
|
|
|
now = time.Now()
|
|
|
|
)
|
|
|
|
err = cache.UpdateProjectBandwidthUsage(ctx, projectID, rand.Int63n(4096)+1, time.Second, now)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if tt.backend == "redis" {
|
2021-03-09 17:42:10 +00:00
|
|
|
redis.FastForward(time.Second)
|
2020-12-21 17:27:12 +00:00
|
|
|
}
|
2019-10-16 17:50:29 +01:00
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
_, err = cache.GetProjectBandwidthUsage(ctx, projectID, now)
|
|
|
|
require.Error(t, err)
|
2019-10-16 17:50:29 +01:00
|
|
|
})
|
|
|
|
}
|
2020-12-21 17:27:12 +00:00
|
|
|
}
|
2019-10-16 17:50:29 +01:00
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
type populateCacheData struct {
|
|
|
|
projectID uuid.UUID
|
|
|
|
storageSum int64
|
|
|
|
bandwidthSum int64
|
|
|
|
bandwidthNow time.Time
|
2019-10-16 17:50:29 +01:00
|
|
|
}
|
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
func populateCache(ctx context.Context, cache accounting.Cache) ([]populateCacheData, error) {
|
|
|
|
var (
|
|
|
|
valuesListSize = rand.Intn(10) + 10
|
|
|
|
numProjects = rand.Intn(100) + 100
|
|
|
|
valueStorageMultiplier = rand.Int63n(4095) + 1
|
|
|
|
valueBandwdithMultiplier = rand.Int63n(4095) + 1
|
2019-10-16 17:50:29 +01:00
|
|
|
)
|
2019-05-10 02:39:21 +01:00
|
|
|
// make a largish list of varying values
|
2020-12-21 17:27:12 +00:00
|
|
|
baseValues := make([]int64, valuesListSize)
|
|
|
|
for i := range baseValues {
|
|
|
|
baseValues[i] = rand.Int63n(int64(valuesListSize)) + 1
|
2019-05-10 02:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// make up some project IDs
|
2020-12-21 17:27:12 +00:00
|
|
|
populatedData := make([]populateCacheData, numProjects)
|
|
|
|
for i := range populatedData {
|
|
|
|
populatedData[i] = populateCacheData{
|
|
|
|
projectID: testrand.UUID(),
|
|
|
|
}
|
2019-05-10 02:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// send lots of space used updates for all of these projects to the live
|
|
|
|
// accounting store.
|
|
|
|
errg, ctx := errgroup.WithContext(context.Background())
|
2020-12-21 17:27:12 +00:00
|
|
|
for i, pdata := range populatedData {
|
|
|
|
var (
|
|
|
|
i = i
|
|
|
|
projID = pdata.projectID
|
|
|
|
)
|
|
|
|
|
2019-05-10 02:39:21 +01:00
|
|
|
errg.Go(func() error {
|
|
|
|
// have each project sending the values in a different order
|
|
|
|
myValues := make([]int64, valuesListSize)
|
2020-12-21 17:27:12 +00:00
|
|
|
copy(myValues, baseValues)
|
2019-05-10 02:39:21 +01:00
|
|
|
rand.Shuffle(valuesListSize, func(v1, v2 int) {
|
|
|
|
myValues[v1], myValues[v2] = myValues[v2], myValues[v1]
|
|
|
|
})
|
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
now := time.Now()
|
|
|
|
populatedData[i].bandwidthNow = now
|
|
|
|
|
2019-05-10 02:39:21 +01:00
|
|
|
for _, val := range myValues {
|
2020-12-21 17:27:12 +00:00
|
|
|
storageVal := val * valueStorageMultiplier
|
|
|
|
populatedData[i].storageSum += storageVal
|
|
|
|
if err := cache.AddProjectStorageUsage(ctx, projID, storageVal); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bandwidthVal := val * valueBandwdithMultiplier
|
|
|
|
populatedData[i].bandwidthSum += bandwidthVal
|
|
|
|
if err := cache.UpdateProjectBandwidthUsage(ctx, projID, bandwidthVal, time.Hour, now); err != nil {
|
2019-05-10 02:39:21 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-12-21 17:27:12 +00:00
|
|
|
|
2019-05-10 02:39:21 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
return populatedData, errg.Wait()
|
2019-10-31 17:27:38 +00:00
|
|
|
}
|