2019-09-04 15:13:43 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storageusage_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2019-09-04 15:13:43 +01:00
|
|
|
"storj.io/storj/storagenode"
|
|
|
|
"storj.io/storj/storagenode/storagenodedb/storagenodedbtest"
|
|
|
|
"storj.io/storj/storagenode/storageusage"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStorageUsage(t *testing.T) {
|
|
|
|
const (
|
|
|
|
satelliteNum = 10
|
|
|
|
days = 30
|
|
|
|
)
|
|
|
|
|
2022-10-10 12:35:58 +01:00
|
|
|
now := time.Now().UTC()
|
2019-09-04 15:13:43 +01:00
|
|
|
|
|
|
|
var satellites []storj.NodeID
|
|
|
|
satelliteID := testrand.NodeID()
|
|
|
|
|
|
|
|
satellites = append(satellites, satelliteID)
|
|
|
|
for i := 0; i < satelliteNum-1; i++ {
|
|
|
|
satellites = append(satellites, testrand.NodeID())
|
|
|
|
}
|
|
|
|
|
2022-10-10 12:35:58 +01:00
|
|
|
stamps := storagenodedbtest.MakeStorageUsageStamps(satellites, days, now)
|
2019-09-04 15:13:43 +01:00
|
|
|
|
2022-12-05 14:10:04 +00:00
|
|
|
var totalSummary, averageUsage float64
|
2022-07-21 12:15:03 +01:00
|
|
|
expectedDailyStamps := make(map[storj.NodeID]map[time.Time]storageusage.Stamp)
|
|
|
|
expectedDailyStampsTotals := make(map[time.Time]float64)
|
2022-12-05 14:10:04 +00:00
|
|
|
summaryBySatellite := make(map[storj.NodeID]float64)
|
|
|
|
averageBySatellite := make(map[storj.NodeID]float64)
|
2019-09-04 15:13:43 +01:00
|
|
|
|
|
|
|
for _, stamp := range stamps {
|
2022-07-21 12:15:03 +01:00
|
|
|
if expectedDailyStamps[stamp.SatelliteID] == nil {
|
|
|
|
expectedDailyStamps[stamp.SatelliteID] = map[time.Time]storageusage.Stamp{}
|
2019-09-04 15:13:43 +01:00
|
|
|
}
|
2022-07-21 12:15:03 +01:00
|
|
|
expectedDailyStamps[stamp.SatelliteID][stamp.IntervalStart.UTC()] = stamp
|
|
|
|
}
|
2019-09-04 15:13:43 +01:00
|
|
|
|
2022-12-05 14:10:04 +00:00
|
|
|
totalUsageBytes := float64(0)
|
|
|
|
totalStamps := float64(0)
|
2022-07-21 12:15:03 +01:00
|
|
|
for _, satellite := range satellites {
|
2022-12-05 14:10:04 +00:00
|
|
|
satelliteUsageBytes := float64(0)
|
2022-07-21 12:15:03 +01:00
|
|
|
for _, stamp := range expectedDailyStamps[satellite] {
|
|
|
|
intervalStart := stamp.IntervalStart.UTC()
|
2022-12-05 14:10:04 +00:00
|
|
|
|
|
|
|
expectedDailyStampsTotals[intervalStart] += stamp.AtRestTotal
|
|
|
|
|
|
|
|
summaryBySatellite[satellite] += stamp.AtRestTotal
|
|
|
|
totalSummary += stamp.AtRestTotal
|
|
|
|
|
|
|
|
satelliteUsageBytes += stamp.AtRestTotalBytes
|
|
|
|
totalUsageBytes += stamp.AtRestTotalBytes
|
|
|
|
|
|
|
|
totalStamps++
|
2022-07-21 12:15:03 +01:00
|
|
|
}
|
2022-12-05 14:10:04 +00:00
|
|
|
|
|
|
|
averageBySatellite[satellite] = satelliteUsageBytes / float64(len(expectedDailyStamps[satellite]))
|
2019-09-04 15:13:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-31 12:53:06 +00:00
|
|
|
averageUsage = totalUsageBytes / float64(len(expectedDailyStampsTotals))
|
2022-12-05 14:10:04 +00:00
|
|
|
|
2020-01-20 14:56:12 +00:00
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
2019-09-04 15:13:43 +01:00
|
|
|
storageUsageDB := db.StorageUsage()
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("store", func(t *testing.T) {
|
2019-09-04 15:13:43 +01:00
|
|
|
err := storageUsageDB.Store(ctx, stamps)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("get daily", func(t *testing.T) {
|
2022-12-10 00:27:15 +00:00
|
|
|
from := now.AddDate(0, 0, -20)
|
|
|
|
res, err := storageUsageDB.GetDaily(ctx, satelliteID, from, now)
|
2019-09-04 15:13:43 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, res)
|
|
|
|
|
|
|
|
for _, stamp := range res {
|
|
|
|
assert.Equal(t, satelliteID, stamp.SatelliteID)
|
2022-07-21 12:15:03 +01:00
|
|
|
assert.Equal(t, expectedDailyStamps[satelliteID][stamp.IntervalStart].AtRestTotal, stamp.AtRestTotal)
|
2022-12-05 14:10:04 +00:00
|
|
|
assert.Equal(t, expectedDailyStamps[satelliteID][stamp.IntervalStart].AtRestTotalBytes, stamp.AtRestTotalBytes)
|
|
|
|
assert.Equal(t, expectedDailyStamps[satelliteID][stamp.IntervalStart].IntervalInHours, stamp.IntervalInHours)
|
2019-09-04 15:13:43 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("get daily total", func(t *testing.T) {
|
2019-09-04 15:13:43 +01:00
|
|
|
res, err := storageUsageDB.GetDailyTotal(ctx, time.Time{}, now)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, res)
|
|
|
|
assert.Equal(t, days, len(res))
|
|
|
|
|
|
|
|
for _, stamp := range res {
|
2022-07-21 12:15:03 +01:00
|
|
|
// there can be inconsistencies in the values due to rounding off errors
|
|
|
|
// and can make the test flaky.
|
|
|
|
// rounding the values to 5 decimal places to avoid flakiness
|
|
|
|
assert.Equal(t, roundFloat(expectedDailyStampsTotals[stamp.IntervalStart]), roundFloat(stamp.AtRestTotal))
|
2019-09-04 15:13:43 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("summary satellite", func(t *testing.T) {
|
2022-12-05 14:10:04 +00:00
|
|
|
summ, averageUsageInBytes, err := storageUsageDB.SatelliteSummary(ctx, satelliteID, time.Time{}, now)
|
2019-09-04 15:13:43 +01:00
|
|
|
assert.NoError(t, err)
|
2022-12-05 14:10:04 +00:00
|
|
|
assert.Equal(t, roundFloat(summaryBySatellite[satelliteID]), roundFloat(summ))
|
|
|
|
assert.Equal(t, roundFloat(averageBySatellite[satelliteID]), roundFloat(averageUsageInBytes))
|
2019-09-04 15:13:43 +01:00
|
|
|
})
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("summary", func(t *testing.T) {
|
2022-12-05 14:10:04 +00:00
|
|
|
summ, averageUsageInBytes, err := storageUsageDB.Summary(ctx, time.Time{}, now)
|
2019-09-04 15:13:43 +01:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 12:35:58 +01:00
|
|
|
assert.Equal(t, roundFloat(totalSummary), roundFloat(summ))
|
2022-12-05 14:10:04 +00:00
|
|
|
assert.Equal(t, roundFloat(averageUsage), roundFloat(averageUsageInBytes))
|
2019-09-04 15:13:43 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyStorageUsage(t *testing.T) {
|
2020-01-20 14:56:12 +00:00
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
2022-12-05 14:10:04 +00:00
|
|
|
var emptySummary, zeroHourInterval float64
|
2019-09-04 15:13:43 +01:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
storageUsageDB := db.StorageUsage()
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("get daily", func(t *testing.T) {
|
2019-09-04 15:13:43 +01:00
|
|
|
res, err := storageUsageDB.GetDaily(ctx, storj.NodeID{}, time.Time{}, now)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, res)
|
|
|
|
})
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("get daily total", func(t *testing.T) {
|
2019-09-04 15:13:43 +01:00
|
|
|
res, err := storageUsageDB.GetDailyTotal(ctx, time.Time{}, now)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, res)
|
|
|
|
})
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("summary satellite", func(t *testing.T) {
|
2022-12-10 00:27:15 +00:00
|
|
|
summ, averageUsageInBytes, err := storageUsageDB.SatelliteSummary(ctx, storj.NodeID{}, time.Time{}, now)
|
2019-09-04 15:13:43 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, emptySummary, summ)
|
2022-12-10 00:27:15 +00:00
|
|
|
assert.Equal(t, zeroHourInterval, averageUsageInBytes)
|
2019-09-04 15:13:43 +01:00
|
|
|
})
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("summary", func(t *testing.T) {
|
2022-12-10 00:27:15 +00:00
|
|
|
summ, averageUsageInBytes, err := storageUsageDB.Summary(ctx, time.Time{}, now)
|
2019-09-04 15:13:43 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, emptySummary, summ)
|
2022-12-10 00:27:15 +00:00
|
|
|
assert.Equal(t, zeroHourInterval, averageUsageInBytes)
|
2019-09-04 15:13:43 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-23 13:19:13 +01:00
|
|
|
func TestZeroStorageUsage(t *testing.T) {
|
|
|
|
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
|
|
|
|
storageUsageDB := db.StorageUsage()
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
|
|
|
satelliteID := testrand.NodeID()
|
|
|
|
stamp := storageusage.Stamp{
|
|
|
|
SatelliteID: satelliteID,
|
|
|
|
AtRestTotal: 0,
|
|
|
|
IntervalStart: time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()),
|
|
|
|
IntervalEndTime: now,
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedStamp := []storageusage.Stamp{stamp}
|
|
|
|
|
|
|
|
t.Run("store", func(t *testing.T) {
|
|
|
|
err := storageUsageDB.Store(ctx, []storageusage.Stamp{stamp})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get daily", func(t *testing.T) {
|
|
|
|
res, err := storageUsageDB.GetDaily(ctx, satelliteID, time.Time{}, now)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(res), 1)
|
|
|
|
assert.Equal(t, expectedStamp[0].AtRestTotal, res[0].AtRestTotal)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get daily total", func(t *testing.T) {
|
|
|
|
res, err := storageUsageDB.GetDailyTotal(ctx, time.Time{}, now)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(res), 1)
|
|
|
|
assert.Equal(t, expectedStamp[0].AtRestTotal, res[0].AtRestTotal)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-21 12:15:03 +01:00
|
|
|
// RoundFloat rounds float value to 5 decimal places.
|
|
|
|
func roundFloat(value float64) float64 {
|
|
|
|
return math.Round(value*100000) / 100000
|
|
|
|
}
|