2020-01-10 18:53:42 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package orders_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-02-08 13:09:21 +00:00
|
|
|
"strconv"
|
2020-01-10 18:53:42 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-02-08 13:09:21 +00:00
|
|
|
"go.uber.org/zap"
|
2020-01-10 18:53:42 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
|
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/pb"
|
2023-02-08 13:09:21 +00:00
|
|
|
"storj.io/common/signing"
|
|
|
|
"storj.io/common/storj"
|
2020-01-10 18:53:42 +00:00
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-01-10 18:53:42 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/accounting"
|
2023-02-08 13:09:21 +00:00
|
|
|
"storj.io/storj/satellite/internalpb"
|
|
|
|
"storj.io/storj/satellite/metabase"
|
2020-01-10 18:53:42 +00:00
|
|
|
"storj.io/storj/satellite/orders"
|
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
2023-01-11 14:15:08 +00:00
|
|
|
func getSettledBandwidth(ctx context.Context, accountingDB accounting.ProjectAccounting, projectID uuid.UUID, since time.Time) (int64, error) {
|
|
|
|
total, err := accountingDB.GetProjectSettledBandwidthTotal(ctx, projectID, since.Add(-time.Hour))
|
2020-01-10 18:53:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return total, nil
|
|
|
|
}
|
|
|
|
|
2020-01-17 22:55:53 +00:00
|
|
|
// TestRollupsWriteCacheBatchLimitReached makes sure bandwidth rollup values are not written to the
|
2020-01-10 18:53:42 +00:00
|
|
|
// db until the batch size is reached.
|
2020-01-17 22:55:53 +00:00
|
|
|
func TestRollupsWriteCacheBatchLimitReached(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2020-01-10 18:53:42 +00:00
|
|
|
useBatchSize := 10
|
|
|
|
amount := (memory.MB * 500).Int64()
|
2020-04-02 15:18:08 +01:00
|
|
|
projectID := testrand.UUID()
|
2020-03-10 22:05:01 +00:00
|
|
|
startTime := time.Now()
|
2020-01-10 18:53:42 +00:00
|
|
|
|
2020-01-17 22:55:53 +00:00
|
|
|
rwc := orders.NewRollupsWriteCache(zaptest.NewLogger(t), db.Orders(), useBatchSize)
|
2020-01-10 18:53:42 +00:00
|
|
|
|
|
|
|
accountingDB := db.ProjectAccounting()
|
|
|
|
|
2023-01-11 14:15:08 +00:00
|
|
|
expectedTotal := int64(0)
|
2020-01-10 18:53:42 +00:00
|
|
|
// use different bucketName for each write, so they don't get aggregated yet
|
|
|
|
for i := 0; i < useBatchSize-1; i++ {
|
|
|
|
bucketName := fmt.Sprintf("my_files_%d", i)
|
2023-01-11 14:15:08 +00:00
|
|
|
err := rwc.UpdateBucketBandwidthSettle(ctx, projectID, []byte(bucketName), pb.PieceAction_GET, amount, 0, startTime)
|
2020-01-10 18:53:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// check that nothing was actually written since it should just be stored
|
2023-01-11 14:15:08 +00:00
|
|
|
total, err := getSettledBandwidth(ctx, accountingDB, projectID, startTime)
|
2020-01-10 18:53:42 +00:00
|
|
|
require.NoError(t, err)
|
2023-01-11 14:15:08 +00:00
|
|
|
require.Zero(t, total)
|
|
|
|
|
|
|
|
expectedTotal += amount
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 22:55:53 +00:00
|
|
|
whenDone := rwc.OnNextFlush()
|
2020-01-10 18:53:42 +00:00
|
|
|
// write one more rollup record to hit the threshold
|
2020-01-17 22:55:53 +00:00
|
|
|
err := rwc.UpdateBucketBandwidthAllocation(ctx, projectID, []byte("my_files_last"), pb.PieceAction_GET, amount, startTime)
|
2020-01-10 18:53:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// make sure flushing is done
|
|
|
|
select {
|
|
|
|
case <-whenDone:
|
|
|
|
break
|
|
|
|
case <-ctx.Done():
|
|
|
|
t.Fatal(ctx.Err())
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:15:08 +00:00
|
|
|
total, err := getSettledBandwidth(ctx, accountingDB, projectID, startTime)
|
2020-01-10 18:53:42 +00:00
|
|
|
require.NoError(t, err)
|
2023-01-11 14:15:08 +00:00
|
|
|
require.Equal(t, expectedTotal, total)
|
2020-01-10 18:53:42 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-17 22:55:53 +00:00
|
|
|
// TestRollupsWriteCacheBatchChore makes sure bandwidth rollup values are not written to the
|
2020-01-10 18:53:42 +00:00
|
|
|
// db until the chore flushes the DB (assuming the batch size is not reached).
|
2020-01-17 22:55:53 +00:00
|
|
|
func TestRollupsWriteCacheBatchChore(t *testing.T) {
|
2020-01-10 18:53:42 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
},
|
|
|
|
func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
useBatchSize := 10
|
|
|
|
amount := (memory.MB * 500).Int64()
|
2020-04-02 15:18:08 +01:00
|
|
|
projectID := testrand.UUID()
|
2020-03-10 22:05:01 +00:00
|
|
|
startTime := time.Now()
|
2020-01-10 18:53:42 +00:00
|
|
|
|
|
|
|
planet.Satellites[0].Orders.Chore.Loop.Pause()
|
|
|
|
|
|
|
|
accountingDB := planet.Satellites[0].DB.ProjectAccounting()
|
|
|
|
ordersDB := planet.Satellites[0].Orders.DB
|
|
|
|
|
2023-01-11 14:15:08 +00:00
|
|
|
expectedTotal := int64(0)
|
2020-01-10 18:53:42 +00:00
|
|
|
for i := 0; i < useBatchSize-1; i++ {
|
|
|
|
bucketName := fmt.Sprintf("my_files_%d", i)
|
2023-01-11 14:15:08 +00:00
|
|
|
err := ordersDB.UpdateBucketBandwidthSettle(ctx, projectID, []byte(bucketName), pb.PieceAction_GET, amount, 0, startTime)
|
2020-01-10 18:53:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// check that nothing was actually written
|
2023-01-11 14:15:08 +00:00
|
|
|
total, err := getSettledBandwidth(ctx, accountingDB, projectID, startTime)
|
2020-01-10 18:53:42 +00:00
|
|
|
require.NoError(t, err)
|
2023-01-11 14:15:08 +00:00
|
|
|
require.Zero(t, total)
|
|
|
|
|
|
|
|
expectedTotal += amount
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 22:55:53 +00:00
|
|
|
rwc := ordersDB.(*orders.RollupsWriteCache)
|
|
|
|
whenDone := rwc.OnNextFlush()
|
2020-01-10 18:53:42 +00:00
|
|
|
// wait for Loop to complete
|
|
|
|
planet.Satellites[0].Orders.Chore.Loop.TriggerWait()
|
|
|
|
|
|
|
|
// make sure flushing is done
|
|
|
|
select {
|
|
|
|
case <-whenDone:
|
|
|
|
break
|
|
|
|
case <-ctx.Done():
|
|
|
|
t.Fatal(ctx.Err())
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:15:08 +00:00
|
|
|
total, err := getSettledBandwidth(ctx, accountingDB, projectID, startTime)
|
2020-01-10 18:53:42 +00:00
|
|
|
require.NoError(t, err)
|
2023-01-11 14:15:08 +00:00
|
|
|
require.Equal(t, expectedTotal, total)
|
2020-01-10 18:53:42 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-26 20:29:33 +01:00
|
|
|
func TestUpdateBucketBandwidth(t *testing.T) {
|
2020-01-10 18:53:42 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
},
|
|
|
|
func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2020-01-15 21:45:17 +00:00
|
|
|
// don't let the loop flush our cache while we're checking it
|
|
|
|
planet.Satellites[0].Orders.Chore.Loop.Pause()
|
2020-01-10 18:53:42 +00:00
|
|
|
ordersDB := planet.Satellites[0].Orders.DB
|
|
|
|
|
|
|
|
// setup: check there is nothing in the cache to start
|
|
|
|
cache, ok := ordersDB.(*orders.RollupsWriteCache)
|
|
|
|
require.True(t, ok)
|
|
|
|
size := cache.CurrentSize()
|
2020-06-26 20:29:33 +01:00
|
|
|
require.Equal(t, 0, size)
|
2020-01-10 18:53:42 +00:00
|
|
|
|
2020-06-26 20:29:33 +01:00
|
|
|
// setup: add an allocated and settled item to the cache
|
2020-04-02 15:18:08 +01:00
|
|
|
projectID := testrand.UUID()
|
2020-01-10 18:53:42 +00:00
|
|
|
bucketName := []byte("testbucketname")
|
|
|
|
amount := (memory.MB * 500).Int64()
|
2021-11-23 22:50:42 +00:00
|
|
|
intervalStart := time.Now()
|
2023-01-11 14:15:08 +00:00
|
|
|
err := ordersDB.UpdateBucketBandwidthInline(ctx, projectID, bucketName, pb.PieceAction_GET, amount, intervalStart)
|
2020-01-10 18:53:42 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-23 22:50:42 +00:00
|
|
|
err = ordersDB.UpdateBucketBandwidthSettle(ctx, projectID, bucketName, pb.PieceAction_PUT, amount, 0, intervalStart)
|
2020-06-26 20:29:33 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-10 18:53:42 +00:00
|
|
|
|
|
|
|
// test: confirm there is one item in the cache now
|
|
|
|
size = cache.CurrentSize()
|
2020-06-26 20:29:33 +01:00
|
|
|
require.Equal(t, 2, size)
|
2020-01-10 18:53:42 +00:00
|
|
|
projectMap := cache.CurrentData()
|
2020-06-26 20:29:33 +01:00
|
|
|
expectedKeyAllocated := orders.CacheKey{
|
2021-11-23 22:50:42 +00:00
|
|
|
ProjectID: projectID,
|
|
|
|
BucketName: string(bucketName),
|
|
|
|
Action: pb.PieceAction_GET,
|
|
|
|
IntervalStart: time.Date(intervalStart.Year(), intervalStart.Month(), intervalStart.Day(), intervalStart.Hour(), 0, 0, 0, intervalStart.Location()).Unix(),
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
2020-06-26 20:29:33 +01:00
|
|
|
expectedKeySettled := orders.CacheKey{
|
2021-11-23 22:50:42 +00:00
|
|
|
ProjectID: projectID,
|
|
|
|
BucketName: string(bucketName),
|
|
|
|
Action: pb.PieceAction_PUT,
|
|
|
|
IntervalStart: time.Date(intervalStart.Year(), intervalStart.Month(), intervalStart.Day(), intervalStart.Hour(), 0, 0, 0, intervalStart.Location()).Unix(),
|
2020-06-26 20:29:33 +01:00
|
|
|
}
|
|
|
|
expectedCacheDataAllocated := orders.CacheData{
|
2023-01-11 14:15:08 +00:00
|
|
|
Inline: amount,
|
|
|
|
Allocated: 0,
|
2020-06-26 20:29:33 +01:00
|
|
|
Settled: 0,
|
|
|
|
}
|
|
|
|
expectedCacheDataSettled := orders.CacheData{
|
|
|
|
Inline: 0,
|
|
|
|
Allocated: 0,
|
|
|
|
Settled: amount,
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
2021-11-23 22:50:42 +00:00
|
|
|
require.Equal(t, expectedCacheDataAllocated, projectMap[expectedKeyAllocated])
|
|
|
|
require.Equal(t, expectedCacheDataSettled, projectMap[expectedKeySettled])
|
2020-01-10 18:53:42 +00:00
|
|
|
|
|
|
|
// setup: add another item to the cache but with a different projectID
|
2020-04-02 15:18:08 +01:00
|
|
|
projectID2 := testrand.UUID()
|
2020-01-10 18:53:42 +00:00
|
|
|
amount2 := (memory.MB * 10).Int64()
|
2023-01-11 14:15:08 +00:00
|
|
|
err = ordersDB.UpdateBucketBandwidthInline(ctx, projectID2, bucketName, pb.PieceAction_GET, amount2, intervalStart)
|
2020-01-10 18:53:42 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-23 22:50:42 +00:00
|
|
|
err = ordersDB.UpdateBucketBandwidthSettle(ctx, projectID2, bucketName, pb.PieceAction_GET, amount2, 0, intervalStart)
|
2020-06-26 20:29:33 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-10 18:53:42 +00:00
|
|
|
size = cache.CurrentSize()
|
2020-06-26 20:29:33 +01:00
|
|
|
require.Equal(t, 3, size)
|
2020-01-10 18:53:42 +00:00
|
|
|
projectMap2 := cache.CurrentData()
|
|
|
|
|
2020-06-26 20:29:33 +01:00
|
|
|
// test: confirm there are 3 items in the cache now with different projectIDs
|
|
|
|
expectedKey := orders.CacheKey{
|
2021-11-23 22:50:42 +00:00
|
|
|
ProjectID: projectID2,
|
|
|
|
BucketName: string(bucketName),
|
|
|
|
Action: pb.PieceAction_GET,
|
|
|
|
IntervalStart: time.Date(intervalStart.Year(), intervalStart.Month(), intervalStart.Day(), intervalStart.Hour(), 0, 0, 0, intervalStart.Location()).Unix(),
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
2020-06-26 20:29:33 +01:00
|
|
|
expectedData := orders.CacheData{
|
2023-01-11 14:15:08 +00:00
|
|
|
Inline: amount2,
|
|
|
|
Allocated: 0,
|
2020-06-26 20:29:33 +01:00
|
|
|
Settled: amount2,
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
2020-06-26 20:29:33 +01:00
|
|
|
require.Equal(t, projectMap2[expectedKey], expectedData)
|
|
|
|
require.Equal(t, len(projectMap2), 3)
|
2020-01-10 18:53:42 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2023-02-08 13:09:21 +00:00
|
|
|
|
|
|
|
func TestEndpointAndCacheContextCanceled(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Orders.FlushBatchSize = 3
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
storagenode := planet.StorageNodes[0]
|
|
|
|
ordersDB := planet.Satellites[0].Orders.DB
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
// create orders to trigger RollupsWriteCache flush
|
|
|
|
projectID := testrand.UUID()
|
|
|
|
requests := []*pb.SettlementRequest{}
|
|
|
|
singleOrderAmount := int64(50)
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
piecePublicKey, piecePrivateKey, err := storj.NewPieceKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
bucketname := "testbucket" + strconv.Itoa(i)
|
|
|
|
|
|
|
|
bucketLocation := metabase.BucketLocation{
|
|
|
|
ProjectID: projectID,
|
|
|
|
BucketName: bucketname,
|
|
|
|
}
|
|
|
|
|
|
|
|
serialNumber := testrand.SerialNumber()
|
|
|
|
key := satellite.Config.Orders.EncryptionKeys.Default
|
|
|
|
encrypted, err := key.EncryptMetadata(
|
|
|
|
serialNumber,
|
|
|
|
&internalpb.OrderLimitMetadata{
|
|
|
|
CompactProjectBucketPrefix: bucketLocation.CompactPrefix(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
limit := &pb.OrderLimit{
|
|
|
|
SerialNumber: serialNumber,
|
|
|
|
SatelliteId: satellite.ID(),
|
|
|
|
UplinkPublicKey: piecePublicKey,
|
|
|
|
StorageNodeId: storagenode.ID(),
|
|
|
|
PieceId: storj.NewPieceID(),
|
|
|
|
Action: pb.PieceAction_GET,
|
|
|
|
Limit: 1000,
|
|
|
|
PieceExpiration: time.Time{},
|
|
|
|
OrderCreation: now,
|
|
|
|
OrderExpiration: now.Add(24 * time.Hour),
|
|
|
|
EncryptedMetadataKeyId: key.ID[:],
|
|
|
|
EncryptedMetadata: encrypted,
|
|
|
|
}
|
|
|
|
|
|
|
|
orderLimit, err := signing.SignOrderLimit(ctx, signing.SignerFromFullIdentity(satellite.Identity), limit)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
order, err := signing.SignUplinkOrder(ctx, piecePrivateKey, &pb.Order{
|
|
|
|
SerialNumber: serialNumber,
|
|
|
|
Amount: singleOrderAmount,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
requests = append(requests, &pb.SettlementRequest{
|
|
|
|
Limit: orderLimit,
|
|
|
|
Order: order,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := storagenode.Dialer.DialNodeURL(ctx, storj.NodeURL{ID: satellite.ID(), Address: satellite.Addr()})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(conn.Close)
|
|
|
|
|
|
|
|
stream, err := pb.NewDRPCOrdersClient(conn).SettlementWithWindow(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(stream.Close)
|
|
|
|
|
|
|
|
for _, request := range requests {
|
|
|
|
err := stream.Send(&pb.SettlementRequest{
|
|
|
|
Limit: request.Limit,
|
|
|
|
Order: request.Order,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp, err := stream.CloseAndRecv()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, pb.SettlementWithWindowResponse_ACCEPTED, resp.Status)
|
|
|
|
|
|
|
|
rwc := ordersDB.(*orders.RollupsWriteCache)
|
|
|
|
whenDone := rwc.OnNextFlush()
|
|
|
|
|
|
|
|
// make sure flushing is done
|
|
|
|
select {
|
|
|
|
case <-whenDone:
|
|
|
|
break
|
|
|
|
case <-ctx.Done():
|
|
|
|
t.Fatal(ctx.Err())
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify that orders were stored in DB
|
|
|
|
bucketBandwidth, err := getSettledBandwidth(ctx, planet.Satellites[0].DB.ProjectAccounting(), projectID, now)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, singleOrderAmount*int64(len(requests)), bucketBandwidth)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|