2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-06-13 19:22:32 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-11-19 14:40:01 +00:00
|
|
|
package overlay_test
|
2018-06-13 19:22:32 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-12-17 18:47:26 +00:00
|
|
|
"math/rand"
|
2018-06-13 19:22:32 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-09 21:17:49 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-02-06 13:32:42 +00:00
|
|
|
|
2018-11-16 16:31:14 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2018-11-19 14:40:01 +00:00
|
|
|
"storj.io/storj/pkg/overlay"
|
2018-12-17 18:47:26 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-12-04 20:18:26 +00:00
|
|
|
"storj.io/storj/pkg/statdb"
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-12-27 09:56:25 +00:00
|
|
|
"storj.io/storj/satellite"
|
2018-12-17 20:14:16 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
2018-06-13 19:22:32 +01:00
|
|
|
)
|
|
|
|
|
2019-01-15 16:08:45 +00:00
|
|
|
func TestCache_Database(t *testing.T) {
|
2019-02-06 13:32:42 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-01-15 16:08:45 +00:00
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
testCache(ctx, t, db.OverlayCache(), db.StatDB())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCache(ctx context.Context, t *testing.T, store overlay.DB, sdb statdb.DB) {
|
2018-12-17 18:47:26 +00:00
|
|
|
valid1ID := storj.NodeID{}
|
|
|
|
valid2ID := storj.NodeID{}
|
|
|
|
missingID := storj.NodeID{}
|
|
|
|
|
|
|
|
_, _ = rand.Read(valid1ID[:])
|
|
|
|
_, _ = rand.Read(valid2ID[:])
|
|
|
|
_, _ = rand.Read(missingID[:])
|
|
|
|
|
2018-12-20 13:57:54 +00:00
|
|
|
cache := overlay.NewCache(store, sdb)
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-11-19 14:40:01 +00:00
|
|
|
{ // Put
|
2018-12-17 18:47:26 +00:00
|
|
|
err := cache.Put(ctx, valid1ID, pb.Node{Id: valid1ID})
|
2018-11-16 16:31:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-17 18:47:26 +00:00
|
|
|
|
|
|
|
err = cache.Put(ctx, valid2ID, pb.Node{Id: valid2ID})
|
2018-11-16 16:31:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-19 14:40:01 +00:00
|
|
|
}
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-11-19 14:40:01 +00:00
|
|
|
{ // Get
|
2018-12-17 18:47:26 +00:00
|
|
|
_, err := cache.Get(ctx, storj.NodeID{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, err == overlay.ErrEmptyNode)
|
|
|
|
|
|
|
|
valid1, err := cache.Get(ctx, valid1ID)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, valid1.Id, valid1ID)
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
valid2, err := cache.Get(ctx, valid2ID)
|
2018-12-17 18:47:26 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, valid2.Id, valid2ID)
|
|
|
|
}
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-12-17 18:47:26 +00:00
|
|
|
invalid2, err := cache.Get(ctx, missingID)
|
2018-11-16 16:31:14 +00:00
|
|
|
assert.Error(t, err)
|
2018-12-17 18:47:26 +00:00
|
|
|
assert.True(t, err == overlay.ErrNodeNotFound)
|
2018-11-16 16:31:14 +00:00
|
|
|
assert.Nil(t, invalid2)
|
|
|
|
|
2019-01-15 16:08:45 +00:00
|
|
|
// TODO: add erroring database test
|
2018-11-19 14:40:01 +00:00
|
|
|
}
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-11-19 14:40:01 +00:00
|
|
|
{ // GetAll
|
2018-11-29 18:39:27 +00:00
|
|
|
nodes, err := cache.GetAll(ctx, storj.NodeIDList{valid2ID, valid1ID, valid2ID})
|
2018-12-04 20:18:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, nodes[0].Id, valid2ID)
|
|
|
|
assert.Equal(t, nodes[1].Id, valid1ID)
|
|
|
|
assert.Equal(t, nodes[2].Id, valid2ID)
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-12-17 18:47:26 +00:00
|
|
|
nodes, err = cache.GetAll(ctx, storj.NodeIDList{valid1ID, missingID})
|
2018-12-04 20:18:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, nodes[0].Id, valid1ID)
|
|
|
|
assert.Nil(t, nodes[1])
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodes, err = cache.GetAll(ctx, make(storj.NodeIDList, 2))
|
2018-12-04 20:18:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, nodes[0])
|
|
|
|
assert.Nil(t, nodes[1])
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
_, err = cache.GetAll(ctx, storj.NodeIDList{})
|
2018-11-19 14:40:01 +00:00
|
|
|
assert.True(t, overlay.OverlayError.Has(err))
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2019-01-15 16:08:45 +00:00
|
|
|
// TODO: add erroring database test
|
2018-11-19 14:40:01 +00:00
|
|
|
}
|
2018-12-21 18:35:21 +00:00
|
|
|
|
2019-01-30 16:29:18 +00:00
|
|
|
{ // List
|
|
|
|
list, err := cache.List(ctx, storj.NodeID{}, 3)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, list)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Paginate
|
|
|
|
|
|
|
|
// should return two nodes
|
|
|
|
nodes, more, err := cache.Paginate(ctx, 0, 2)
|
|
|
|
assert.NotNil(t, more)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(nodes), 2)
|
|
|
|
|
|
|
|
// should return no nodes
|
|
|
|
zero, more, err := cache.Paginate(ctx, 0, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, more)
|
|
|
|
assert.NotEqual(t, len(zero), 0)
|
|
|
|
}
|
|
|
|
|
2018-12-21 18:35:21 +00:00
|
|
|
{ // Delete
|
|
|
|
// Test standard delete
|
|
|
|
err := cache.Delete(ctx, valid1ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Check that it was deleted
|
|
|
|
deleted, err := cache.Get(ctx, valid1ID)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, deleted)
|
|
|
|
assert.True(t, err == overlay.ErrNodeNotFound)
|
|
|
|
|
|
|
|
// Test idempotent delete / non existent key delete
|
|
|
|
err = cache.Delete(ctx, valid1ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Test empty key delete
|
|
|
|
err = cache.Delete(ctx, storj.NodeID{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, err == overlay.ErrEmptyNode)
|
|
|
|
}
|
2018-11-16 16:31:14 +00:00
|
|
|
}
|
2019-02-09 21:17:49 +00:00
|
|
|
|
|
|
|
func TestRandomizedSelection(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
totalNodes := 1000
|
|
|
|
selectIterations := 100
|
|
|
|
numNodesToSelect := 100
|
|
|
|
minSelectCount := 1
|
|
|
|
|
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
cache := db.OverlayCache()
|
|
|
|
allIDs := make(storj.NodeIDList, totalNodes)
|
|
|
|
nodeCounts := make(map[storj.NodeID]int)
|
|
|
|
|
|
|
|
// put nodes in cache
|
|
|
|
for i := 0; i < totalNodes; i++ {
|
|
|
|
newID := storj.NodeID{}
|
|
|
|
_, _ = rand.Read(newID[:])
|
|
|
|
err := cache.Update(ctx, &pb.Node{
|
|
|
|
Id: newID,
|
|
|
|
Type: pb.NodeType_STORAGE,
|
|
|
|
Restrictions: &pb.NodeRestrictions{},
|
|
|
|
Reputation: &pb.NodeStats{},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
allIDs[i] = newID
|
|
|
|
nodeCounts[newID] = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// select numNodesToSelect nodes selectIterations times
|
|
|
|
for i := 0; i < selectIterations; i++ {
|
|
|
|
nodes, err := cache.SelectNodes(ctx, numNodesToSelect, &overlay.NodeCriteria{
|
|
|
|
Type: pb.NodeType_STORAGE,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, nodes, numNodesToSelect)
|
|
|
|
|
|
|
|
for _, node := range nodes {
|
|
|
|
nodeCounts[node.Id]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// expect that each node has been selected at least minSelectCount times
|
|
|
|
for _, id := range allIDs {
|
|
|
|
count := nodeCounts[id]
|
|
|
|
assert.True(t, count >= minSelectCount)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|