2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-08 16:18:28 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
package tally_test
|
2018-11-08 16:18:28 +00:00
|
|
|
|
|
|
|
import (
|
2019-06-20 20:15:13 +01:00
|
|
|
"fmt"
|
2018-11-08 16:18:28 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-06-25 16:58:42 +01:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2019-02-07 19:22:49 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-01 18:50:12 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-12-07 09:59:31 +00:00
|
|
|
|
2019-04-09 14:48:35 +01:00
|
|
|
"storj.io/storj/internal/memory"
|
2018-12-07 11:55:25 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-02-01 18:50:12 +00:00
|
|
|
"storj.io/storj/internal/testplanet"
|
2019-06-26 11:38:51 +01:00
|
|
|
"storj.io/storj/internal/testrand"
|
2019-04-03 04:55:24 +01:00
|
|
|
"storj.io/storj/internal/teststorj"
|
2019-04-09 14:48:35 +01:00
|
|
|
"storj.io/storj/pkg/accounting"
|
|
|
|
"storj.io/storj/pkg/encryption"
|
2019-06-20 20:15:13 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-04-03 04:55:24 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-06-20 20:15:13 +01:00
|
|
|
"storj.io/storj/storagenode"
|
2018-11-08 16:18:28 +00:00
|
|
|
)
|
|
|
|
|
2019-05-10 20:05:42 +01:00
|
|
|
func TestDeleteTalliesBefore(t *testing.T) {
|
2019-04-03 04:55:24 +01:00
|
|
|
tests := []struct {
|
|
|
|
eraseBefore time.Time
|
|
|
|
expectedRaws int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
eraseBefore: time.Now(),
|
|
|
|
expectedRaws: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
eraseBefore: time.Now().Add(24 * time.Hour),
|
|
|
|
expectedRaws: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2019-05-29 14:30:16 +01:00
|
|
|
test := tt
|
2019-04-03 04:55:24 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 0,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
id := teststorj.NodeIDFromBytes([]byte{})
|
|
|
|
nodeData := make(map[storj.NodeID]float64)
|
|
|
|
nodeData[id] = float64(1000)
|
|
|
|
|
2019-05-10 20:05:42 +01:00
|
|
|
err := planet.Satellites[0].DB.StoragenodeAccounting().SaveTallies(ctx, time.Now(), nodeData)
|
2019-04-03 04:55:24 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-05-29 14:30:16 +01:00
|
|
|
err = planet.Satellites[0].DB.StoragenodeAccounting().DeleteTalliesBefore(ctx, test.eraseBefore)
|
2019-04-03 04:55:24 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-05-10 20:05:42 +01:00
|
|
|
raws, err := planet.Satellites[0].DB.StoragenodeAccounting().GetTallies(ctx)
|
2019-04-03 04:55:24 +01:00
|
|
|
require.NoError(t, err)
|
2019-05-29 14:30:16 +01:00
|
|
|
assert.Len(t, raws, test.expectedRaws)
|
2019-04-03 04:55:24 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 14:48:35 +01:00
|
|
|
|
2019-04-29 18:46:38 +01:00
|
|
|
func TestOnlyInline(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
tallySvc := planet.Satellites[0].Accounting.Tally
|
|
|
|
uplink := planet.Uplinks[0]
|
|
|
|
|
2019-06-25 16:58:42 +01:00
|
|
|
projects, err1 := planet.Satellites[0].DB.Console().Projects().GetAll(ctx)
|
2019-06-13 17:58:40 +01:00
|
|
|
if err1 != nil {
|
|
|
|
assert.NoError(t, err1)
|
|
|
|
}
|
2019-06-26 11:38:51 +01:00
|
|
|
projectID := projects[0].ID
|
2019-06-13 17:58:40 +01:00
|
|
|
|
2019-04-29 18:46:38 +01:00
|
|
|
// Setup: create data for the uplink to upload
|
2019-06-26 11:38:51 +01:00
|
|
|
expectedData := testrand.Bytes(1 * memory.KiB)
|
2019-04-29 18:46:38 +01:00
|
|
|
|
|
|
|
// Setup: get the expected size of the data that will be stored in pointer
|
2019-06-19 09:11:27 +01:00
|
|
|
// Since the data is small enough to be stored inline, when it is encrypted, we only
|
|
|
|
// add 16 bytes of encryption authentication overhead. No encryption block
|
|
|
|
// padding will be added since we are not chunking data that we store inline.
|
|
|
|
const encryptionAuthOverhead = 16 // bytes
|
|
|
|
expectedTotalBytes := len(expectedData) + encryptionAuthOverhead
|
2019-04-29 18:46:38 +01:00
|
|
|
|
|
|
|
// Setup: The data in this tally should match the pointer that the uplink.upload created
|
2019-06-13 17:58:40 +01:00
|
|
|
expectedBucketName := "testbucket"
|
2019-04-29 18:46:38 +01:00
|
|
|
expectedTally := accounting.BucketTally{
|
2019-06-13 17:58:40 +01:00
|
|
|
BucketName: []byte(expectedBucketName),
|
2019-06-25 16:58:42 +01:00
|
|
|
ProjectID: projectID[:],
|
2019-04-29 18:46:38 +01:00
|
|
|
Segments: 1,
|
|
|
|
InlineSegments: 1,
|
|
|
|
Files: 1,
|
|
|
|
InlineFiles: 1,
|
2019-06-19 09:11:27 +01:00
|
|
|
Bytes: int64(expectedTotalBytes),
|
|
|
|
InlineBytes: int64(expectedTotalBytes),
|
2019-04-29 18:46:38 +01:00
|
|
|
MetadataSize: 111, // brittle, this is hardcoded since its too difficult to get this value progamatically
|
|
|
|
}
|
2019-06-25 16:58:42 +01:00
|
|
|
// The projectID should be the 16 bytes uuid representation, not 36 byte string representation
|
|
|
|
assert.Equal(t, 16, len(projectID[:]))
|
2019-04-29 18:46:38 +01:00
|
|
|
|
|
|
|
// Execute test: upload a file, then calculate at rest data
|
2019-06-26 11:38:51 +01:00
|
|
|
err := uplink.Upload(ctx, planet.Satellites[0], expectedBucketName, "test/path", expectedData)
|
2019-04-29 18:46:38 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Run calculate twice to test unique constraint issue
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
latestTally, actualNodeData, actualBucketData, err := tallySvc.CalculateAtRestData(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, actualNodeData, 0)
|
|
|
|
|
2019-05-10 20:05:42 +01:00
|
|
|
_, err = planet.Satellites[0].DB.ProjectAccounting().SaveTallies(ctx, latestTally, actualBucketData)
|
2019-04-29 18:46:38 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Confirm the correct bucket storage tally was created
|
|
|
|
assert.Equal(t, len(actualBucketData), 1)
|
|
|
|
for bucketID, actualTally := range actualBucketData {
|
|
|
|
assert.Contains(t, bucketID, expectedBucketName)
|
|
|
|
assert.Equal(t, expectedTally, *actualTally)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-13 17:58:40 +01:00
|
|
|
func TestCalculateNodeAtRestData(t *testing.T) {
|
2019-04-09 14:48:35 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
tallySvc := planet.Satellites[0].Accounting.Tally
|
|
|
|
uplink := planet.Uplinks[0]
|
|
|
|
|
|
|
|
// Setup: create 50KiB of data for the uplink to upload
|
2019-06-26 11:38:51 +01:00
|
|
|
expectedData := testrand.Bytes(50 * memory.KiB)
|
2019-04-09 14:48:35 +01:00
|
|
|
|
|
|
|
// Setup: get the expected size of the data that will be stored in pointer
|
|
|
|
uplinkConfig := uplink.GetConfig(planet.Satellites[0])
|
|
|
|
expectedTotalBytes, err := encryption.CalcEncryptedSize(int64(len(expectedData)), uplinkConfig.GetEncryptionScheme())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Execute test: upload a file, then calculate at rest data
|
|
|
|
expectedBucketName := "testbucket"
|
|
|
|
err = uplink.Upload(ctx, planet.Satellites[0], expectedBucketName, "test/path", expectedData)
|
2019-06-13 17:58:40 +01:00
|
|
|
|
2019-04-09 14:48:35 +01:00
|
|
|
assert.NoError(t, err)
|
2019-06-13 17:58:40 +01:00
|
|
|
_, actualNodeData, _, err := tallySvc.CalculateAtRestData(ctx)
|
2019-04-09 14:48:35 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Confirm the correct number of shares were stored
|
|
|
|
uplinkRS := uplinkConfig.GetRedundancyScheme()
|
|
|
|
if !correctRedundencyScheme(len(actualNodeData), uplinkRS) {
|
|
|
|
t.Fatalf("expected between: %d and %d, actual: %d", uplinkRS.RepairShares, uplinkRS.TotalShares, len(actualNodeData))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Confirm the correct number of bytes were stored on each node
|
|
|
|
for _, actualTotalBytes := range actualNodeData {
|
|
|
|
assert.Equal(t, int64(actualTotalBytes), expectedTotalBytes)
|
|
|
|
}
|
2019-06-13 17:58:40 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCalculateBucketAtRestData(t *testing.T) {
|
2019-06-20 20:15:13 +01:00
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
2019-06-25 16:58:42 +01:00
|
|
|
project string
|
2019-06-20 20:15:13 +01:00
|
|
|
segmentIndex string
|
|
|
|
bucketName string
|
|
|
|
objectName string
|
|
|
|
inline bool
|
|
|
|
last bool
|
|
|
|
}{
|
2019-06-25 16:58:42 +01:00
|
|
|
{"bucket, no objects", "9656af6e-2d9c-42fa-91f2-bfd516a722d7", "", "mockBucketName", "", true, false},
|
|
|
|
{"inline, same project, same bucket", "9656af6e-2d9c-42fa-91f2-bfd516a722d7", "l", "mockBucketName", "mockObjectName", true, true},
|
|
|
|
{"remote, same project, same bucket", "9656af6e-2d9c-42fa-91f2-bfd516a722d7", "s0", "mockBucketName", "mockObjectName1", false, false},
|
|
|
|
{"last segment, same project, different bucket", "9656af6e-2d9c-42fa-91f2-bfd516a722d7", "l", "mockBucketName1", "mockObjectName2", false, true},
|
|
|
|
{"different project", "9656af6e-2d9c-42fa-91f2-bfd516a722d1", "s0", "mockBucketName", "mockObjectName", false, false},
|
2019-06-20 20:15:13 +01:00
|
|
|
}
|
2019-06-13 17:58:40 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2019-06-20 20:15:13 +01:00
|
|
|
satellitePeer := planet.Satellites[0]
|
|
|
|
redundancyScheme := planet.Uplinks[0].GetConfig(satellitePeer).GetRedundancyScheme()
|
|
|
|
expectedBucketTallies := make(map[string]*accounting.BucketTally)
|
|
|
|
for _, tt := range testCases {
|
|
|
|
tt := tt // avoid scopelint error, ref: https://github.com/golangci/golangci-lint/issues/281
|
|
|
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-06-25 16:58:42 +01:00
|
|
|
projectID, err := uuid.Parse(tt.project)
|
|
|
|
require.NoError(t, err)
|
2019-06-20 20:15:13 +01:00
|
|
|
|
|
|
|
// setup: create a pointer and save it to pointerDB
|
|
|
|
pointer, _ := makePointer(planet.StorageNodes, redundancyScheme, int64(2), tt.inline)
|
|
|
|
metainfo := satellitePeer.Metainfo.Service
|
2019-06-25 16:58:42 +01:00
|
|
|
objectPath := fmt.Sprintf("%s/%s/%s/%s", tt.project, tt.segmentIndex, tt.bucketName, tt.objectName)
|
2019-06-20 20:15:13 +01:00
|
|
|
if tt.objectName == "" {
|
2019-06-25 16:58:42 +01:00
|
|
|
objectPath = fmt.Sprintf("%s/%s/%s", tt.project, tt.segmentIndex, tt.bucketName)
|
2019-06-20 20:15:13 +01:00
|
|
|
}
|
2019-06-25 16:58:42 +01:00
|
|
|
err = metainfo.Put(ctx, objectPath, pointer)
|
2019-06-20 20:15:13 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// setup: create expected bucket tally for the pointer just created, but only if
|
|
|
|
// the pointer was for an object and not just for a bucket
|
|
|
|
if tt.objectName != "" {
|
2019-06-25 16:58:42 +01:00
|
|
|
bucketID := fmt.Sprintf("%s/%s", tt.project, tt.bucketName)
|
2019-06-20 20:15:13 +01:00
|
|
|
newTally := addBucketTally(expectedBucketTallies[bucketID], tt.inline, tt.last)
|
|
|
|
newTally.BucketName = []byte(tt.bucketName)
|
2019-06-25 16:58:42 +01:00
|
|
|
newTally.ProjectID = projectID[:]
|
2019-06-20 20:15:13 +01:00
|
|
|
expectedBucketTallies[bucketID] = newTally
|
|
|
|
}
|
|
|
|
|
|
|
|
// test: calculate at rest data
|
|
|
|
tallySvc := satellitePeer.Accounting.Tally
|
|
|
|
_, _, actualBucketData, err := tallySvc.CalculateAtRestData(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, len(expectedBucketTallies), len(actualBucketData))
|
|
|
|
for bucket, actualTally := range actualBucketData {
|
|
|
|
assert.Equal(t, *expectedBucketTallies[bucket], *actualTally)
|
|
|
|
}
|
|
|
|
})
|
2019-06-13 17:58:40 +01:00
|
|
|
}
|
2019-06-20 20:15:13 +01:00
|
|
|
})
|
|
|
|
}
|
2019-06-13 17:58:40 +01:00
|
|
|
|
2019-06-20 20:15:13 +01:00
|
|
|
// addBucketTally creates a new expected bucket tally based on the
|
|
|
|
// pointer that was just created for the test case
|
|
|
|
func addBucketTally(existingTally *accounting.BucketTally, inline, last bool) *accounting.BucketTally {
|
|
|
|
// if there is already an existing tally for this project and bucket, then
|
|
|
|
// add the new pointer data to the existing tally
|
|
|
|
if existingTally != nil {
|
|
|
|
existingTally.Segments++
|
|
|
|
existingTally.Bytes += int64(2)
|
|
|
|
existingTally.MetadataSize += int64(12)
|
|
|
|
existingTally.RemoteSegments++
|
|
|
|
existingTally.RemoteBytes += int64(2)
|
|
|
|
return existingTally
|
|
|
|
}
|
2019-06-13 17:58:40 +01:00
|
|
|
|
2019-06-20 20:15:13 +01:00
|
|
|
// if the pointer was inline, create a tally with inline info
|
|
|
|
if inline {
|
|
|
|
newInlineTally := accounting.BucketTally{
|
|
|
|
Segments: int64(1),
|
|
|
|
InlineSegments: int64(1),
|
|
|
|
Files: int64(1),
|
|
|
|
InlineFiles: int64(1),
|
|
|
|
Bytes: int64(2),
|
|
|
|
InlineBytes: int64(2),
|
|
|
|
MetadataSize: int64(12),
|
2019-06-13 17:58:40 +01:00
|
|
|
}
|
2019-06-20 20:15:13 +01:00
|
|
|
return &newInlineTally
|
|
|
|
}
|
2019-06-13 17:58:40 +01:00
|
|
|
|
2019-06-20 20:15:13 +01:00
|
|
|
// if the pointer was remote, create a tally with remote info
|
|
|
|
newRemoteTally := accounting.BucketTally{
|
|
|
|
Segments: int64(1),
|
|
|
|
RemoteSegments: int64(1),
|
|
|
|
Bytes: int64(2),
|
|
|
|
RemoteBytes: int64(2),
|
|
|
|
MetadataSize: int64(12),
|
|
|
|
}
|
2019-06-18 03:20:40 +01:00
|
|
|
|
2019-06-20 20:15:13 +01:00
|
|
|
if last {
|
|
|
|
newRemoteTally.Files++
|
|
|
|
newRemoteTally.RemoteFiles++
|
|
|
|
}
|
2019-06-18 03:20:40 +01:00
|
|
|
|
2019-06-20 20:15:13 +01:00
|
|
|
return &newRemoteTally
|
|
|
|
}
|
2019-06-13 17:58:40 +01:00
|
|
|
|
2019-06-20 20:15:13 +01:00
|
|
|
// makePointer creates a pointer
|
|
|
|
func makePointer(storageNodes []*storagenode.Peer, rs storj.RedundancyScheme,
|
|
|
|
segmentSize int64, inline bool) (*pb.Pointer, error) {
|
2019-06-18 03:20:40 +01:00
|
|
|
|
2019-06-20 20:15:13 +01:00
|
|
|
if inline {
|
|
|
|
inlinePointer := &pb.Pointer{
|
|
|
|
Type: pb.Pointer_INLINE,
|
|
|
|
InlineSegment: make([]byte, segmentSize),
|
|
|
|
SegmentSize: segmentSize,
|
|
|
|
Metadata: []byte("fakemetadata"),
|
|
|
|
}
|
|
|
|
return inlinePointer, nil
|
|
|
|
}
|
2019-06-13 17:58:40 +01:00
|
|
|
|
2019-06-20 20:15:13 +01:00
|
|
|
pieces := make([]*pb.RemotePiece, 0, len(storageNodes))
|
|
|
|
for i, storagenode := range storageNodes {
|
|
|
|
pieces = append(pieces, &pb.RemotePiece{
|
|
|
|
PieceNum: int32(i),
|
|
|
|
NodeId: storagenode.ID(),
|
|
|
|
})
|
|
|
|
}
|
2019-04-09 14:48:35 +01:00
|
|
|
|
2019-06-20 20:15:13 +01:00
|
|
|
pointer := &pb.Pointer{
|
|
|
|
Type: pb.Pointer_REMOTE,
|
|
|
|
Remote: &pb.RemoteSegment{
|
|
|
|
Redundancy: &pb.RedundancyScheme{
|
|
|
|
Type: pb.RedundancyScheme_RS,
|
|
|
|
MinReq: int32(rs.RequiredShares),
|
|
|
|
Total: int32(rs.TotalShares),
|
|
|
|
RepairThreshold: int32(rs.RepairShares),
|
|
|
|
SuccessThreshold: int32(rs.OptimalShares),
|
|
|
|
ErasureShareSize: rs.ShareSize,
|
|
|
|
},
|
|
|
|
RemotePieces: pieces,
|
|
|
|
},
|
|
|
|
SegmentSize: segmentSize,
|
|
|
|
Metadata: []byte("fakemetadata"),
|
|
|
|
}
|
|
|
|
|
|
|
|
return pointer, nil
|
2019-04-09 14:48:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func correctRedundencyScheme(shareCount int, uplinkRS storj.RedundancyScheme) bool {
|
|
|
|
|
|
|
|
// The shareCount should be a value between RequiredShares and TotalShares where
|
|
|
|
// RequiredShares is the min number of shares required to recover a segment and
|
|
|
|
// TotalShares is the number of shares to encode
|
|
|
|
if int(uplinkRS.RepairShares) <= shareCount && shareCount <= int(uplinkRS.TotalShares) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|