2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-08-17 18:40:15 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-02-19 09:39:04 +00:00
|
|
|
package psdb_test
|
2018-08-17 18:40:15 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-09-08 16:34:55 +01:00
|
|
|
"strconv"
|
2018-08-17 18:40:15 +01:00
|
|
|
"testing"
|
2018-10-10 15:04:42 +01:00
|
|
|
"time"
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2019-03-05 22:20:46 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-19 09:39:04 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-01 05:46:16 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2018-10-16 12:43:44 +01:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/internal/teststorj"
|
2018-10-16 12:43:44 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-02-19 09:39:04 +00:00
|
|
|
"storj.io/storj/pkg/piecestore/psserver/psdb"
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-08-17 18:40:15 +01:00
|
|
|
)
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
const concurrency = 10
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2019-02-19 09:39:04 +00:00
|
|
|
func newDB(t testing.TB, id string) (*psdb.DB, func()) {
|
2019-01-28 19:45:25 +00:00
|
|
|
tmpdir, err := ioutil.TempDir("", "storj-psdb-"+id)
|
2019-02-19 09:39:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
dbpath := filepath.Join(tmpdir, "psdb.db")
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2019-02-19 09:39:04 +00:00
|
|
|
db, err := psdb.Open(dbpath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-03-01 05:46:16 +00:00
|
|
|
err = db.Migration().Run(zaptest.NewLogger(t), db)
|
2019-02-19 09:39:04 +00:00
|
|
|
require.NoError(t, err)
|
2018-09-11 14:13:25 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
return db, func() {
|
|
|
|
err := db.Close()
|
2019-02-19 09:39:04 +00:00
|
|
|
require.NoError(t, err)
|
2018-09-08 16:34:55 +01:00
|
|
|
err = os.RemoveAll(tmpdir)
|
2019-02-19 09:39:04 +00:00
|
|
|
require.NoError(t, err)
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:43:09 +00:00
|
|
|
func TestNewInmemory(t *testing.T) {
|
2019-02-19 09:39:04 +00:00
|
|
|
db, err := psdb.OpenInMemory()
|
2018-10-30 16:43:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
func TestHappyPath(t *testing.T) {
|
2019-01-28 19:45:25 +00:00
|
|
|
db, cleanup := newDB(t, "1")
|
2018-09-08 16:34:55 +01:00
|
|
|
defer cleanup()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
type TTL struct {
|
|
|
|
ID string
|
|
|
|
Expiration int64
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
tests := []TTL{
|
|
|
|
{ID: "", Expiration: 0},
|
|
|
|
{ID: "\x00", Expiration: ^int64(0)},
|
|
|
|
{ID: "test", Expiration: 666},
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 22:20:46 +00:00
|
|
|
bandwidthAllocation := func(serialnum, signature string, satelliteID storj.NodeID, total int64) *pb.Order {
|
2019-03-01 05:46:16 +00:00
|
|
|
return &pb.Order{
|
2019-03-05 22:20:46 +00:00
|
|
|
PayerAllocation: pb.OrderLimit{SatelliteId: satelliteID, SerialNumber: serialnum},
|
2019-03-01 05:46:16 +00:00
|
|
|
Total: total,
|
|
|
|
Signature: []byte(signature),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: use better data
|
|
|
|
nodeIDAB := teststorj.NodeIDFromString("AB")
|
|
|
|
allocationTests := []*pb.Order{
|
2019-03-05 22:20:46 +00:00
|
|
|
bandwidthAllocation("serialnum_1", "signed by test", nodeIDAB, 0),
|
|
|
|
bandwidthAllocation("serialnum_2", "signed by sigma", nodeIDAB, 10),
|
|
|
|
bandwidthAllocation("serialnum_3", "signed by sigma", nodeIDAB, 98),
|
|
|
|
bandwidthAllocation("serialnum_4", "signed by test", nodeIDAB, 3),
|
2019-03-01 05:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type bwUsage struct {
|
|
|
|
size int64
|
|
|
|
timenow time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
bwtests := []bwUsage{
|
|
|
|
// size is total size stored
|
|
|
|
{size: 1110, timenow: time.Now()},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Empty", func(t *testing.T) {
|
|
|
|
t.Run("Bandwidth Allocation", func(t *testing.T) {
|
|
|
|
for _, test := range allocationTests {
|
|
|
|
agreements, err := db.GetBandwidthAllocationBySignature(test.Signature)
|
|
|
|
require.Len(t, agreements, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Get all Bandwidth Allocations", func(t *testing.T) {
|
|
|
|
agreementGroups, err := db.GetBandwidthAllocations()
|
|
|
|
require.Len(t, agreementGroups, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("GetBandwidthUsedByDay", func(t *testing.T) {
|
|
|
|
for _, bw := range bwtests {
|
|
|
|
size, err := db.GetBandwidthUsedByDay(bw.timenow)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(0), size)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("GetTotalBandwidthBetween", func(t *testing.T) {
|
|
|
|
for _, bw := range bwtests {
|
|
|
|
size, err := db.GetTotalBandwidthBetween(bw.timenow, bw.timenow)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(0), size)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2018-12-20 18:29:05 +00:00
|
|
|
t.Run("Create", func(t *testing.T) {
|
2018-09-08 16:34:55 +01:00
|
|
|
for P := 0; P < concurrency; P++ {
|
|
|
|
t.Run("#"+strconv.Itoa(P), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
for _, ttl := range tests {
|
2018-09-13 15:30:45 +01:00
|
|
|
err := db.AddTTL(ttl.ID, ttl.Expiration, 0)
|
2018-09-08 16:34:55 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
})
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
t.Run("Get", func(t *testing.T) {
|
|
|
|
for P := 0; P < concurrency; P++ {
|
|
|
|
t.Run("#"+strconv.Itoa(P), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
for _, ttl := range tests {
|
|
|
|
expiration, err := db.GetTTLByID(ttl.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ttl.Expiration != expiration {
|
|
|
|
t.Fatalf("expected %d got %d", ttl.Expiration, expiration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
t.Run("Delete", func(t *testing.T) {
|
|
|
|
for P := 0; P < concurrency; P++ {
|
|
|
|
t.Run("Delete", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
for _, ttl := range tests {
|
|
|
|
err := db.DeleteTTLByID(ttl.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
})
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
t.Run("Get Deleted", func(t *testing.T) {
|
|
|
|
for P := 0; P < concurrency; P++ {
|
|
|
|
t.Run("#"+strconv.Itoa(P), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
for _, ttl := range tests {
|
|
|
|
expiration, err := db.GetTTLByID(ttl.ID)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if expiration != 0 {
|
|
|
|
t.Fatalf("expected expiration 0 got %d", expiration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
})
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
t.Run("Bandwidth Allocation", func(t *testing.T) {
|
|
|
|
for P := 0; P < concurrency; P++ {
|
|
|
|
t.Run("#"+strconv.Itoa(P), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
for _, test := range allocationTests {
|
|
|
|
err := db.WriteBandwidthAllocToDB(test)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
agreements, err := db.GetBandwidthAllocationBySignature(test.Signature)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
|
|
|
for _, agreement := range agreements {
|
2019-01-28 19:45:25 +00:00
|
|
|
if pb.Equal(agreement, test) {
|
2018-09-08 16:34:55 +01:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
t.Fatal("did not find added bandwidth allocation")
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
2018-11-01 16:40:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Get all Bandwidth Allocations", func(t *testing.T) {
|
|
|
|
for P := 0; P < concurrency; P++ {
|
|
|
|
t.Run("#"+strconv.Itoa(P), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
agreementGroups, err := db.GetBandwidthAllocations()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
|
|
|
for _, agreements := range agreementGroups {
|
|
|
|
for _, agreement := range agreements {
|
|
|
|
for _, test := range allocationTests {
|
2019-01-28 19:45:25 +00:00
|
|
|
if pb.Equal(&agreement.Agreement, test) {
|
2018-11-01 16:40:26 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
t.Fatal("did not find added bandwidth allocation")
|
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2018-10-10 15:04:42 +01:00
|
|
|
|
2019-02-22 15:51:39 +00:00
|
|
|
t.Run("GetBandwidthUsedByDay", func(t *testing.T) {
|
2018-10-10 15:04:42 +01:00
|
|
|
for P := 0; P < concurrency; P++ {
|
|
|
|
t.Run("#"+strconv.Itoa(P), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
for _, bw := range bwtests {
|
2019-02-22 15:51:39 +00:00
|
|
|
size, err := db.GetBandwidthUsedByDay(bw.timenow)
|
2018-10-10 15:04:42 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-22 15:51:39 +00:00
|
|
|
if bw.size != size {
|
2018-10-10 15:04:42 +01:00
|
|
|
t.Fatalf("expected %d got %d", bw.size, size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-02-22 15:51:39 +00:00
|
|
|
t.Run("GetTotalBandwidthBetween", func(t *testing.T) {
|
2018-10-10 15:04:42 +01:00
|
|
|
for P := 0; P < concurrency; P++ {
|
|
|
|
t.Run("#"+strconv.Itoa(P), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
for _, bw := range bwtests {
|
2019-02-22 15:51:39 +00:00
|
|
|
size, err := db.GetTotalBandwidthBetween(bw.timenow, bw.timenow)
|
2018-10-10 15:04:42 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-22 15:51:39 +00:00
|
|
|
if bw.size != size {
|
2018-10-10 15:04:42 +01:00
|
|
|
t.Fatalf("expected %d got %d", bw.size, size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2019-03-05 22:20:46 +00:00
|
|
|
|
|
|
|
type bwaUsage struct {
|
|
|
|
serialnum string
|
|
|
|
status psdb.AgreementStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
bwatests := []bwaUsage{
|
|
|
|
{serialnum: "serialnum_1", status: psdb.AgreementStatusUnsent},
|
|
|
|
{serialnum: "serialnum_2", status: psdb.AgreementStatusReject},
|
|
|
|
{serialnum: "serialnum_3", status: psdb.AgreementStatusSent},
|
|
|
|
}
|
|
|
|
t.Run("UpdateBandwidthAllocationStatus", func(t *testing.T) {
|
|
|
|
for P := 0; P < concurrency; P++ {
|
|
|
|
t.Run("#"+strconv.Itoa(P), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
for _, bw := range bwatests {
|
|
|
|
err := db.UpdateBandwidthAllocationStatus(bw.serialnum, bw.status)
|
|
|
|
require.NoError(t, err)
|
|
|
|
status, err := db.GetBwaStatusBySerialNum(bw.serialnum)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, bw.status, status)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-10-10 15:04:42 +01:00
|
|
|
}
|
|
|
|
|
2018-09-08 16:34:55 +01:00
|
|
|
func BenchmarkWriteBandwidthAllocation(b *testing.B) {
|
2019-01-28 19:45:25 +00:00
|
|
|
db, cleanup := newDB(b, "3")
|
2018-09-08 16:34:55 +01:00
|
|
|
defer cleanup()
|
|
|
|
const WritesPerLoop = 10
|
|
|
|
b.RunParallel(func(b *testing.PB) {
|
|
|
|
for b.Next() {
|
|
|
|
for i := 0; i < WritesPerLoop; i++ {
|
2019-02-22 21:17:35 +00:00
|
|
|
_ = db.WriteBandwidthAllocToDB(&pb.Order{
|
|
|
|
PayerAllocation: pb.OrderLimit{},
|
2019-01-28 19:45:25 +00:00
|
|
|
Total: 156,
|
|
|
|
Signature: []byte("signed by test"),
|
2018-09-08 16:34:55 +01:00
|
|
|
})
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
2018-09-08 16:34:55 +01:00
|
|
|
})
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|