storj/pkg/overlay/cache_test.go

177 lines
4.2 KiB
Go
Raw Normal View History

2019-01-24 20:15:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
2018-11-19 14:40:01 +00:00
package overlay_test
import (
"context"
2018-12-17 18:47:26 +00:00
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2019-03-23 08:06:11 +00:00
"go.uber.org/zap/zaptest"
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-11-30 13:40:13 +00:00
"storj.io/storj/pkg/storj"
"storj.io/storj/satellite"
"storj.io/storj/satellite/satellitedb/satellitedbtest"
)
func TestCache_Database(t *testing.T) {
t.Parallel()
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
ctx := testcontext.New(t)
defer ctx.Cleanup()
testCache(ctx, t, db.OverlayCache())
})
}
func testCache(ctx context.Context, t *testing.T, store overlay.DB) {
2018-12-17 18:47:26 +00:00
valid1ID := storj.NodeID{}
valid2ID := storj.NodeID{}
missingID := storj.NodeID{}
address := &pb.NodeAddress{Address: "127.0.0.1:0"}
2018-12-17 18:47:26 +00:00
_, _ = rand.Read(valid1ID[:])
_, _ = rand.Read(valid2ID[:])
_, _ = rand.Read(missingID[:])
cache := overlay.NewCache(zaptest.NewLogger(t), store, overlay.NodeSelectionConfig{OnlineWindow: time.Hour})
2018-11-16 16:31:14 +00:00
2018-11-19 14:40:01 +00:00
{ // Put
err := cache.Put(ctx, valid1ID, pb.Node{Id: valid1ID, Address: address})
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, Address: address})
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)
}
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)
assert.True(t, overlay.ErrNodeNotFound.Has(err))
2018-11-16 16:31:14 +00:00
assert.Nil(t, invalid2)
// TODO: add erroring database test
2018-11-19 14:40:01 +00:00
}
2018-11-16 16:31:14 +00:00
{ // 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-11-16 16:31:14 +00:00
}
func TestRandomizedSelection(t *testing.T) {
t.Parallel()
totalNodes := 1000
selectIterations := 100
numNodesToSelect := 100
minSelectCount := 3 // TODO: compute this limit better
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[:])
2019-04-22 10:07:50 +01:00
err := cache.UpdateAddress(ctx, &pb.Node{Id: newID})
require.NoError(t, err)
_, err = cache.UpdateNodeInfo(ctx, newID, &pb.InfoResponse{
Type: pb.NodeType_STORAGE,
Capacity: &pb.NodeCapacity{},
})
require.NoError(t, err)
_, err = cache.UpdateUptime(ctx, newID, true)
require.NoError(t, err)
allIDs[i] = newID
nodeCounts[newID] = 0
}
// select numNodesToSelect nodes selectIterations times
for i := 0; i < selectIterations; i++ {
var nodes []*pb.Node
var err error
2019-04-24 11:35:50 +01:00
if i%2 == 0 {
nodes, err = cache.SelectStorageNodes(ctx, numNodesToSelect, &overlay.NodeCriteria{OnlineWindow: time.Hour})
require.NoError(t, err)
} else {
nodes, err = cache.SelectNewStorageNodes(ctx, numNodesToSelect, &overlay.NodeCriteria{
OnlineWindow: time.Hour,
AuditCount: 1,
})
require.NoError(t, err)
}
require.Len(t, nodes, numNodesToSelect)
for _, node := range nodes {
nodeCounts[node.Id]++
}
}
belowThreshold := 0
table := []int{}
// expect that each node has been selected at least minSelectCount times
for _, id := range allIDs {
count := nodeCounts[id]
if count < minSelectCount {
belowThreshold++
}
if count >= len(table) {
table = append(table, make([]int, count-len(table)+1)...)
}
table[count]++
}
if belowThreshold > totalNodes*1/100 {
t.Errorf("%d out of %d were below threshold %d", belowThreshold, totalNodes, minSelectCount)
for count, amount := range table {
t.Logf("%3d = %4d", count, amount)
}
}
})
}