2021-12-09 15:05:21 +00:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"storj.io/common/memory"
|
2022-05-16 16:47:47 +01:00
|
|
|
"storj.io/common/pb"
|
2021-12-09 15:05:21 +00:00
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
|
|
|
"storj.io/storj/private/testplanet"
|
2022-05-16 16:47:47 +01:00
|
|
|
"storj.io/storj/satellite/accounting"
|
2021-12-09 15:05:21 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2022-05-16 16:47:47 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2021-12-09 15:05:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_DailyUsage(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 1},
|
|
|
|
func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
const (
|
2022-05-16 16:47:47 +01:00
|
|
|
firstBucketName = "testbucket0"
|
|
|
|
secondBucketName = "testbucket1"
|
2021-12-09 15:05:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
inFiveMinutes := time.Now().Add(5 * time.Minute)
|
|
|
|
|
|
|
|
var (
|
|
|
|
satelliteSys = planet.Satellites[0]
|
|
|
|
uplink = planet.Uplinks[0]
|
|
|
|
projectID = uplink.Projects[0].ID
|
|
|
|
)
|
|
|
|
|
|
|
|
newUser := console.CreateUser{
|
|
|
|
FullName: "Project Daily Usage Test",
|
|
|
|
ShortName: "",
|
|
|
|
Email: "du@test.test",
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := satelliteSys.AddUser(ctx, newUser, 3)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = satelliteSys.DB.Console().ProjectMembers().Insert(ctx, user.ID, projectID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
usage0, err := satelliteSys.DB.ProjectAccounting().GetProjectDailyUsageByDateRange(ctx, projectID, now, inFiveMinutes, 0)
|
|
|
|
require.NoError(t, err)
|
2021-12-17 14:47:35 +00:00
|
|
|
require.Zero(t, len(usage0.AllocatedBandwidthUsage))
|
|
|
|
require.Zero(t, len(usage0.SettledBandwidthUsage))
|
2021-12-09 15:05:21 +00:00
|
|
|
require.Zero(t, len(usage0.StorageUsage))
|
|
|
|
|
2022-05-16 16:47:47 +01:00
|
|
|
segment := int64(15000)
|
2021-12-09 15:05:21 +00:00
|
|
|
|
2022-05-16 16:47:47 +01:00
|
|
|
firstBucketLocation := metabase.BucketLocation{
|
|
|
|
ProjectID: projectID,
|
|
|
|
BucketName: firstBucketName,
|
|
|
|
}
|
|
|
|
secondBucketLocation := metabase.BucketLocation{
|
|
|
|
ProjectID: projectID,
|
|
|
|
BucketName: secondBucketName,
|
|
|
|
}
|
|
|
|
tallies := map[metabase.BucketLocation]*accounting.BucketTally{
|
|
|
|
firstBucketLocation: {
|
|
|
|
BucketLocation: firstBucketLocation,
|
|
|
|
TotalBytes: segment,
|
|
|
|
},
|
|
|
|
secondBucketLocation: {
|
|
|
|
BucketLocation: secondBucketLocation,
|
|
|
|
TotalBytes: segment,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = satelliteSys.DB.ProjectAccounting().SaveTallies(ctx, now, tallies)
|
2021-12-09 15:05:21 +00:00
|
|
|
require.NoError(t, err)
|
2022-05-16 16:47:47 +01:00
|
|
|
err = satelliteSys.DB.Orders().UpdateBucketBandwidthAllocation(ctx, projectID, []byte(firstBucketName), pb.PieceAction_GET, segment, inFiveMinutes)
|
2021-12-09 15:05:21 +00:00
|
|
|
require.NoError(t, err)
|
2022-05-16 16:47:47 +01:00
|
|
|
err = satelliteSys.DB.Orders().UpdateBucketBandwidthSettle(ctx, projectID, []byte(firstBucketName), pb.PieceAction_GET, segment, 0, inFiveMinutes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = satelliteSys.DB.Orders().UpdateBucketBandwidthAllocation(ctx, projectID, []byte(secondBucketName), pb.PieceAction_GET, segment, inFiveMinutes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = planet.Satellites[0].DB.Orders().UpdateBucketBandwidthSettle(ctx, projectID, []byte(secondBucketName), pb.PieceAction_GET, segment, 0, inFiveMinutes)
|
2021-12-09 15:05:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
usage1, err := satelliteSys.DB.ProjectAccounting().GetProjectDailyUsageByDateRange(ctx, projectID, now, inFiveMinutes, 0)
|
|
|
|
require.NoError(t, err)
|
2022-05-16 16:47:47 +01:00
|
|
|
require.Equal(t, 2*segment, usage1.StorageUsage[0].Value)
|
|
|
|
require.Equal(t, 2*segment, usage1.AllocatedBandwidthUsage[0].Value)
|
|
|
|
require.Equal(t, 2*segment, usage1.SettledBandwidthUsage[0].Value)
|
2021-12-09 15:05:21 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2022-02-17 13:48:39 +00:00
|
|
|
|
|
|
|
func Test_GetSingleBucketRollup(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 1},
|
|
|
|
func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
const (
|
|
|
|
bucketName = "testbucket"
|
|
|
|
firstPath = "path"
|
|
|
|
secondPath = "another_path"
|
|
|
|
)
|
|
|
|
|
|
|
|
now := time.Now().UTC()
|
|
|
|
inFiveMinutes := time.Now().Add(5 * time.Minute).UTC()
|
|
|
|
|
|
|
|
var (
|
|
|
|
satelliteSys = planet.Satellites[0]
|
2022-04-14 12:20:18 +01:00
|
|
|
upl = planet.Uplinks[0]
|
|
|
|
projectID = upl.Projects[0].ID
|
2022-02-17 13:48:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
newUser := console.CreateUser{
|
|
|
|
FullName: "Project Single Bucket Rollup",
|
|
|
|
ShortName: "",
|
|
|
|
Email: "sbur@test.test",
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := satelliteSys.AddUser(ctx, newUser, 3)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = satelliteSys.DB.Console().ProjectMembers().Insert(ctx, user.ID, projectID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
planet.Satellites[0].Orders.Chore.Loop.Pause()
|
|
|
|
satelliteSys.Accounting.Tally.Loop.Pause()
|
|
|
|
|
|
|
|
timeTruncateDown := func(t time.Time) time.Time {
|
|
|
|
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location())
|
|
|
|
}
|
|
|
|
|
|
|
|
usage0, err := satelliteSys.DB.ProjectAccounting().GetSingleBucketUsageRollup(ctx, projectID, bucketName, now, inFiveMinutes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, bucketName, usage0.BucketName)
|
|
|
|
require.Equal(t, projectID, usage0.ProjectID)
|
|
|
|
require.Equal(t, timeTruncateDown(now), usage0.Since)
|
|
|
|
require.Equal(t, inFiveMinutes, usage0.Before)
|
|
|
|
require.Zero(t, usage0.GetEgress)
|
|
|
|
require.Zero(t, usage0.ObjectCount)
|
|
|
|
require.Zero(t, usage0.AuditEgress)
|
|
|
|
require.Zero(t, usage0.RepairEgress)
|
|
|
|
require.Zero(t, usage0.MetadataSize)
|
|
|
|
require.Zero(t, usage0.TotalSegments)
|
|
|
|
require.Zero(t, usage0.TotalStoredData)
|
|
|
|
|
|
|
|
firstSegment := testrand.Bytes(100 * memory.KiB)
|
|
|
|
secondSegment := testrand.Bytes(200 * memory.KiB)
|
|
|
|
|
2022-04-14 12:20:18 +01:00
|
|
|
err = upl.Upload(ctx, satelliteSys, bucketName, firstPath, firstSegment)
|
2022-02-17 13:48:39 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-14 12:20:18 +01:00
|
|
|
|
|
|
|
err = upl.Upload(ctx, satelliteSys, bucketName, secondPath, secondSegment)
|
2022-02-17 13:48:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-14 12:20:18 +01:00
|
|
|
_, err = upl.Download(ctx, satelliteSys, bucketName, firstPath)
|
2022-02-17 13:48:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NoError(t, planet.WaitForStorageNodeEndpoints(ctx))
|
|
|
|
tomorrow := time.Now().Add(24 * time.Hour)
|
|
|
|
planet.StorageNodes[0].Storage2.Orders.SendOrders(ctx, tomorrow)
|
|
|
|
|
|
|
|
planet.Satellites[0].Orders.Chore.Loop.TriggerWait()
|
|
|
|
satelliteSys.Accounting.Tally.Loop.TriggerWait()
|
|
|
|
// We trigger tally one more time because the most recent tally is skipped in service method.
|
|
|
|
satelliteSys.Accounting.Tally.Loop.TriggerWait()
|
|
|
|
|
|
|
|
usage1, err := satelliteSys.DB.ProjectAccounting().GetSingleBucketUsageRollup(ctx, projectID, bucketName, now, inFiveMinutes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Greater(t, usage1.GetEgress, 0.0)
|
|
|
|
require.Greater(t, usage1.ObjectCount, 0.0)
|
|
|
|
require.Greater(t, usage1.MetadataSize, 0.0)
|
|
|
|
require.Greater(t, usage1.TotalSegments, 0.0)
|
|
|
|
require.Greater(t, usage1.TotalStoredData, 0.0)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|