2019-04-09 14:48:35 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package accounting_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-09-04 17:05:34 +01:00
|
|
|
"math"
|
2019-04-09 14:48:35 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2019-08-08 14:47:04 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-04-09 14:48:35 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-06-13 13:46:08 +01:00
|
|
|
|
2019-04-09 14:48:35 +01:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-06-26 11:38:51 +01:00
|
|
|
"storj.io/storj/internal/testrand"
|
2019-04-09 14:48:35 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
"storj.io/storj/satellite"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/satellite/accounting"
|
2019-04-09 14:48:35 +01:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSaveBucketTallies(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
// Setup: create bucket storage tallies
|
2019-06-26 11:38:51 +01:00
|
|
|
projectID := testrand.UUID()
|
|
|
|
|
|
|
|
bucketTallies, expectedTallies, err := createBucketStorageTallies(projectID)
|
2019-04-09 14:48:35 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Execute test: retrieve the save tallies and confirm they contains the expected data
|
|
|
|
intervalStart := time.Now()
|
2019-05-10 20:05:42 +01:00
|
|
|
pdb := db.ProjectAccounting()
|
2019-09-12 18:31:50 +01:00
|
|
|
|
|
|
|
err = pdb.SaveTallies(ctx, intervalStart, bucketTallies)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tallies, err := pdb.GetTallies(ctx)
|
2019-04-09 14:48:35 +01:00
|
|
|
require.NoError(t, err)
|
2019-09-12 18:31:50 +01:00
|
|
|
for _, tally := range tallies {
|
2019-04-09 14:48:35 +01:00
|
|
|
require.Contains(t, expectedTallies, tally)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-08 14:47:04 +01:00
|
|
|
func TestStorageNodeUsage(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-10-04 20:09:52 +01:00
|
|
|
const days = 30
|
2019-09-04 17:05:34 +01:00
|
|
|
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
2019-08-08 14:47:04 +01:00
|
|
|
nodeID := testrand.NodeID()
|
2019-09-04 17:05:34 +01:00
|
|
|
startDate := now.Add(time.Hour * 24 * -days)
|
2019-08-08 14:47:04 +01:00
|
|
|
|
|
|
|
var nodes storj.NodeIDList
|
2019-10-04 20:09:52 +01:00
|
|
|
nodes = append(nodes, nodeID, testrand.NodeID(), testrand.NodeID(), testrand.NodeID())
|
2019-08-08 14:47:04 +01:00
|
|
|
|
2019-09-04 17:05:34 +01:00
|
|
|
rollups, tallies, lastDate := makeRollupsAndStorageNodeStorageTallies(nodes, startDate, days)
|
2019-08-08 14:47:04 +01:00
|
|
|
|
2019-09-04 17:05:34 +01:00
|
|
|
lastRollup := rollups[lastDate]
|
|
|
|
delete(rollups, lastDate)
|
2019-08-08 14:47:04 +01:00
|
|
|
|
2019-09-04 17:05:34 +01:00
|
|
|
accountingDB := db.StoragenodeAccounting()
|
|
|
|
|
|
|
|
// create last rollup timestamp
|
|
|
|
_, err := accountingDB.LastTimestamp(ctx, accounting.LastRollup)
|
2019-08-08 14:47:04 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-09-04 17:05:34 +01:00
|
|
|
// save tallies
|
|
|
|
for latestTally, tallies := range tallies {
|
|
|
|
err = accountingDB.SaveTallies(ctx, latestTally, tallies)
|
|
|
|
require.NoError(t, err)
|
2019-08-08 14:47:04 +01:00
|
|
|
}
|
|
|
|
|
2019-09-04 17:05:34 +01:00
|
|
|
// save rollup
|
|
|
|
err = accountingDB.SaveRollup(ctx, lastDate.Add(time.Hour*-24), rollups)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Run("usage with pending tallies", func(t *testing.T) {
|
|
|
|
nodeStorageUsages, err := accountingDB.QueryStorageNodeUsage(ctx, nodeID, time.Time{}, now)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, nodeStorageUsages)
|
|
|
|
assert.Equal(t, days, len(nodeStorageUsages))
|
2019-08-08 14:47:04 +01:00
|
|
|
|
2019-09-04 17:05:34 +01:00
|
|
|
// check usage from rollups
|
|
|
|
for _, usage := range nodeStorageUsages[:len(nodeStorageUsages)-1] {
|
|
|
|
assert.Equal(t, nodeID, usage.NodeID)
|
2019-10-04 20:09:52 +01:00
|
|
|
dateRollup, ok := rollups[usage.Timestamp.UTC()]
|
|
|
|
if assert.True(t, ok) {
|
|
|
|
nodeRollup, ok := dateRollup[nodeID]
|
|
|
|
if assert.True(t, ok) {
|
|
|
|
assert.Equal(t, nodeRollup.AtRestTotal, usage.StorageUsed)
|
|
|
|
}
|
|
|
|
}
|
2019-09-04 17:05:34 +01:00
|
|
|
}
|
2019-08-08 14:47:04 +01:00
|
|
|
|
2019-09-04 17:05:34 +01:00
|
|
|
// check last usage that calculated from tallies
|
|
|
|
lastUsage := nodeStorageUsages[len(nodeStorageUsages)-1]
|
|
|
|
|
2019-10-04 20:09:52 +01:00
|
|
|
assert.Equal(t, nodeID, lastUsage.NodeID)
|
|
|
|
assert.Equal(t, lastRollup[nodeID].StartTime, lastUsage.Timestamp.UTC())
|
|
|
|
assert.Equal(t, lastRollup[nodeID].AtRestTotal, lastUsage.StorageUsed)
|
2019-09-04 17:05:34 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("usage entirely from rollups", func(t *testing.T) {
|
|
|
|
const (
|
|
|
|
start = 10
|
|
|
|
// should be greater than 2
|
|
|
|
// not to include tallies into result
|
|
|
|
end = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
startDate, endDate := now.Add(time.Hour*24*-start), now.Add(time.Hour*24*-end)
|
|
|
|
|
|
|
|
nodeStorageUsages, err := accountingDB.QueryStorageNodeUsage(ctx, nodeID, startDate, endDate)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, nodeStorageUsages)
|
|
|
|
assert.Equal(t, start-end, len(nodeStorageUsages))
|
|
|
|
|
|
|
|
for _, usage := range nodeStorageUsages {
|
|
|
|
assert.Equal(t, nodeID, usage.NodeID)
|
2019-10-04 20:09:52 +01:00
|
|
|
dateRollup, ok := rollups[usage.Timestamp.UTC()]
|
|
|
|
if assert.True(t, ok) {
|
|
|
|
nodeRollup, ok := dateRollup[nodeID]
|
|
|
|
if assert.True(t, ok) {
|
|
|
|
assert.Equal(t, nodeRollup.AtRestTotal, usage.StorageUsed)
|
|
|
|
}
|
|
|
|
}
|
2019-09-04 17:05:34 +01:00
|
|
|
}
|
|
|
|
})
|
2019-08-08 14:47:04 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-09 14:48:35 +01:00
|
|
|
func createBucketStorageTallies(projectID uuid.UUID) (map[string]*accounting.BucketTally, []accounting.BucketTally, error) {
|
|
|
|
bucketTallies := make(map[string]*accounting.BucketTally)
|
|
|
|
var expectedTallies []accounting.BucketTally
|
|
|
|
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
bucketName := fmt.Sprintf("%s%d", "testbucket", i)
|
|
|
|
bucketID := storj.JoinPaths(projectID.String(), bucketName)
|
|
|
|
|
|
|
|
// Setup: The data in this tally should match the pointer that the uplink.upload created
|
|
|
|
tally := accounting.BucketTally{
|
2019-06-21 16:38:37 +01:00
|
|
|
BucketName: []byte(bucketName),
|
2019-09-13 14:51:41 +01:00
|
|
|
ProjectID: projectID,
|
|
|
|
ObjectCount: int64(1),
|
2019-04-09 14:48:35 +01:00
|
|
|
InlineSegments: int64(1),
|
|
|
|
RemoteSegments: int64(1),
|
|
|
|
InlineBytes: int64(1),
|
|
|
|
RemoteBytes: int64(1),
|
|
|
|
MetadataSize: int64(1),
|
|
|
|
}
|
|
|
|
bucketTallies[bucketID] = &tally
|
|
|
|
expectedTallies = append(expectedTallies, tally)
|
|
|
|
|
|
|
|
}
|
|
|
|
return bucketTallies, expectedTallies, nil
|
|
|
|
}
|
2019-08-08 14:47:04 +01:00
|
|
|
|
2019-09-04 17:05:34 +01:00
|
|
|
// make rollups and tallies for specified nodes and date range.
|
|
|
|
func makeRollupsAndStorageNodeStorageTallies(nodes []storj.NodeID, start time.Time, days int) (accounting.RollupStats, map[time.Time]map[storj.NodeID]float64, time.Time) {
|
2019-08-08 14:47:04 +01:00
|
|
|
rollups := make(accounting.RollupStats)
|
2019-09-04 17:05:34 +01:00
|
|
|
tallies := make(map[time.Time]map[storj.NodeID]float64)
|
|
|
|
|
|
|
|
const (
|
|
|
|
hours = 12
|
|
|
|
)
|
2019-08-08 14:47:04 +01:00
|
|
|
|
2019-09-04 17:05:34 +01:00
|
|
|
for i := 0; i < days; i++ {
|
|
|
|
startDay := time.Date(start.Year(), start.Month(), start.Day()+i, 0, 0, 0, 0, start.Location())
|
|
|
|
if rollups[startDay] == nil {
|
|
|
|
rollups[startDay] = make(map[storj.NodeID]*accounting.Rollup)
|
2019-08-08 14:47:04 +01:00
|
|
|
}
|
|
|
|
|
2019-09-04 17:05:34 +01:00
|
|
|
for _, node := range nodes {
|
2019-08-08 14:47:04 +01:00
|
|
|
rollup := &accounting.Rollup{
|
2019-09-04 17:05:34 +01:00
|
|
|
NodeID: node,
|
|
|
|
StartTime: startDay,
|
2019-08-08 14:47:04 +01:00
|
|
|
}
|
|
|
|
|
2019-09-04 17:05:34 +01:00
|
|
|
for h := 0; h < hours; h++ {
|
|
|
|
startTime := startDay.Add(time.Hour * time.Duration(h))
|
|
|
|
if tallies[startTime] == nil {
|
|
|
|
tallies[startTime] = make(map[storj.NodeID]float64)
|
|
|
|
}
|
|
|
|
|
|
|
|
tallieAtRest := math.Round(testrand.Float64n(1000))
|
|
|
|
tallies[startTime][node] = tallieAtRest
|
|
|
|
rollup.AtRestTotal += tallieAtRest
|
|
|
|
}
|
2019-08-08 14:47:04 +01:00
|
|
|
|
2019-09-04 17:05:34 +01:00
|
|
|
rollups[startDay][node] = rollup
|
|
|
|
}
|
2019-08-08 14:47:04 +01:00
|
|
|
}
|
|
|
|
|
2019-10-04 20:09:52 +01:00
|
|
|
return rollups, tallies, time.Date(start.Year(), start.Month(), start.Day()+days-1, 0, 0, 0, 0, start.Location())
|
2019-08-08 14:47:04 +01:00
|
|
|
}
|