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"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
"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"
|
2019-10-16 17:50:29 +01:00
|
|
|
"storj.io/storj/satellite/accounting"
|
|
|
|
"storj.io/storj/satellite/accounting/live"
|
|
|
|
"storj.io/storj/storage/redis/redisserver"
|
2019-05-10 02:39:21 +01:00
|
|
|
)
|
|
|
|
|
2019-10-16 17:50:29 +01:00
|
|
|
func TestLiveAccountingCache(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
backend string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
backend: "redis",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2020-01-31 18:28:42 +00:00
|
|
|
redis, err := redisserver.Mini()
|
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 {
|
|
|
|
var config live.Config
|
|
|
|
if tt.backend == "redis" {
|
|
|
|
config = live.Config{
|
2020-01-31 18:28:42 +00:00
|
|
|
StorageBackend: "redis://" + redis.Addr() + "?db=0",
|
2019-10-16 17:50:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cache, err := live.NewCache(zaptest.NewLogger(t).Named("live-accounting"), config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
projectIDs, sum, err := populateCache(ctx, cache)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// make sure all of the "projects" got all space updates and got right totals
|
|
|
|
for _, projID := range projectIDs {
|
|
|
|
spaceUsed, err := cache.GetProjectStorageUsage(ctx, projID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equalf(t, sum, spaceUsed, "projectID %v", projID)
|
|
|
|
}
|
|
|
|
|
2019-10-31 17:27:38 +00:00
|
|
|
negativeVal := int64(-100)
|
|
|
|
sum += negativeVal
|
2019-10-16 17:50:29 +01:00
|
|
|
|
|
|
|
for _, projID := range projectIDs {
|
2019-10-31 17:27:38 +00:00
|
|
|
err = cache.AddProjectStorageUsage(ctx, projID, negativeVal)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-10-16 17:50:29 +01:00
|
|
|
spaceUsed, err := cache.GetProjectStorageUsage(ctx, projID)
|
|
|
|
require.NoError(t, err)
|
2019-10-31 17:27:38 +00:00
|
|
|
assert.EqualValues(t, sum, spaceUsed)
|
2019-10-16 17:50:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRedisCacheConcurrency(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2020-01-31 18:28:42 +00:00
|
|
|
redis, err := redisserver.Mini()
|
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
|
|
|
|
|
|
|
config := live.Config{
|
2020-01-31 18:28:42 +00:00
|
|
|
StorageBackend: "redis://" + redis.Addr() + "?db=0",
|
2019-10-16 17:50:29 +01:00
|
|
|
}
|
|
|
|
cache, err := live.NewCache(zaptest.NewLogger(t).Named("live-accounting"), config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
projectID := testrand.UUID()
|
|
|
|
|
2019-05-10 02:39:21 +01:00
|
|
|
const (
|
2019-10-16 17:50:29 +01:00
|
|
|
numConcurrent = 100
|
2019-10-31 17:27:38 +00:00
|
|
|
spaceUsed = 10
|
2019-10-16 17:50:29 +01:00
|
|
|
)
|
2019-10-31 17:27:38 +00:00
|
|
|
expectedSum := spaceUsed * numConcurrent
|
2019-10-16 17:50:29 +01:00
|
|
|
|
|
|
|
var group errgroup.Group
|
|
|
|
for i := 0; i < numConcurrent; i++ {
|
|
|
|
group.Go(func() error {
|
2019-10-31 17:27:38 +00:00
|
|
|
return cache.AddProjectStorageUsage(ctx, projectID, spaceUsed)
|
2019-10-16 17:50:29 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
require.NoError(t, group.Wait())
|
|
|
|
|
2019-10-31 17:27:38 +00:00
|
|
|
total, err := cache.GetProjectStorageUsage(ctx, projectID)
|
2019-10-16 17:50:29 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-10-31 17:27:38 +00:00
|
|
|
require.EqualValues(t, expectedSum, total)
|
2019-10-16 17:50:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func populateCache(ctx context.Context, cache accounting.Cache) (projectIDs []uuid.UUID, sum int64, _ error) {
|
|
|
|
const (
|
2020-01-31 18:28:42 +00:00
|
|
|
valuesListSize = 10
|
2019-10-16 17:50:29 +01:00
|
|
|
valueMultiplier = 4096
|
2020-01-31 18:28:42 +00:00
|
|
|
numProjects = 100
|
2019-10-16 17:50:29 +01:00
|
|
|
)
|
2019-05-10 02:39:21 +01:00
|
|
|
// make a largish list of varying values
|
|
|
|
someValues := make([]int64, valuesListSize)
|
|
|
|
for i := range someValues {
|
|
|
|
someValues[i] = int64((i + 1) * valueMultiplier)
|
2019-10-31 17:27:38 +00:00
|
|
|
sum += someValues[i]
|
2019-05-10 02:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// make up some project IDs
|
2019-10-16 17:50:29 +01:00
|
|
|
projectIDs = make([]uuid.UUID, numProjects)
|
2019-05-10 02:39:21 +01:00
|
|
|
for i := range projectIDs {
|
2019-06-26 11:38:51 +01:00
|
|
|
projectIDs[i] = 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())
|
|
|
|
for _, projID := range projectIDs {
|
|
|
|
projID := projID
|
|
|
|
errg.Go(func() error {
|
|
|
|
// have each project sending the values in a different order
|
|
|
|
myValues := make([]int64, valuesListSize)
|
|
|
|
copy(myValues, someValues)
|
|
|
|
rand.Shuffle(valuesListSize, func(v1, v2 int) {
|
|
|
|
myValues[v1], myValues[v2] = myValues[v2], myValues[v1]
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, val := range myValues {
|
2019-10-31 17:27:38 +00:00
|
|
|
if err := cache.AddProjectStorageUsage(ctx, projID, val); err != nil {
|
2019-05-10 02:39:21 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-16 17:50:29 +01:00
|
|
|
return projectIDs, sum, errg.Wait()
|
2019-05-10 02:39:21 +01:00
|
|
|
}
|
2019-10-31 17:27:38 +00:00
|
|
|
|
|
|
|
func TestGetAllProjectTotals(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
backend string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
backend: "redis",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2020-01-31 18:28:42 +00:00
|
|
|
redis, err := redisserver.Mini()
|
2019-10-31 17:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-31 18:28:42 +00:00
|
|
|
defer ctx.Check(redis.Close)
|
2019-10-31 17:27:38 +00:00
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
var config live.Config
|
|
|
|
if tt.backend == "redis" {
|
|
|
|
config = live.Config{
|
2020-01-31 18:28:42 +00:00
|
|
|
StorageBackend: "redis://" + redis.Addr() + "?db=0",
|
2019-10-31 17:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cache, err := live.NewCache(zaptest.NewLogger(t).Named("live-accounting"), config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
projectTotals, err := cache.GetAllProjectTotals(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|