2020-04-14 21:50:02 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package overlay_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/sync2"
|
|
|
|
"storj.io/common/testcontext"
|
2020-05-06 18:22:54 +01:00
|
|
|
"storj.io/common/testrand"
|
2022-02-25 16:53:24 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
2020-04-14 21:50:02 +01:00
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/overlay"
|
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
2020-05-06 18:22:54 +01:00
|
|
|
var nodeSelectionConfig = overlay.NodeSelectionConfig{
|
2020-04-14 21:50:02 +01:00
|
|
|
NewNodeFraction: 0.2,
|
|
|
|
MinimumVersion: "v1.0.0",
|
|
|
|
OnlineWindow: 4 * time.Hour,
|
|
|
|
DistinctIP: true,
|
|
|
|
MinimumDiskSpace: 100 * memory.MiB,
|
2021-06-15 11:49:56 +01:00
|
|
|
|
|
|
|
AsOfSystemTime: overlay.AsOfSystemTimeConfig{
|
|
|
|
Enabled: true,
|
|
|
|
DefaultInterval: -time.Microsecond,
|
|
|
|
},
|
2020-04-14 21:50:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// staleness is how stale the cache can be before we sync with
|
2020-08-11 15:50:01 +01:00
|
|
|
// the database to refresh the cache.
|
2020-04-14 21:50:02 +01:00
|
|
|
|
2022-06-28 12:53:39 +01:00
|
|
|
// using a low time will force the cache to refresh every time.
|
|
|
|
lowStaleness = 2 * time.Nanosecond
|
2020-04-14 21:50:02 +01:00
|
|
|
|
|
|
|
// using a positive time will make it so that the cache is only refreshed when
|
2020-08-11 15:50:01 +01:00
|
|
|
// it hasn't been in the past hour.
|
2020-04-14 21:50:02 +01:00
|
|
|
highStaleness = time.Hour
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRefresh(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2022-06-28 12:53:39 +01:00
|
|
|
cache, err := overlay.NewUploadSelectionCache(zap.NewNop(),
|
2020-04-14 21:50:02 +01:00
|
|
|
db.OverlayCache(),
|
|
|
|
lowStaleness,
|
2020-05-06 18:22:54 +01:00
|
|
|
nodeSelectionConfig,
|
2020-04-14 21:50:02 +01:00
|
|
|
)
|
2022-06-28 12:53:39 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cacheCtx, cacheCancel := context.WithCancel(ctx)
|
|
|
|
defer cacheCancel()
|
|
|
|
ctx.Go(func() error { return cache.Run(cacheCtx) })
|
|
|
|
|
2020-04-14 21:50:02 +01:00
|
|
|
// the cache should have no nodes to start
|
2022-06-28 12:53:39 +01:00
|
|
|
err = cache.Refresh(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
reputable, new, err := cache.Size(ctx)
|
2020-04-14 21:50:02 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, reputable)
|
|
|
|
require.Equal(t, 0, new)
|
|
|
|
|
|
|
|
// add some nodes to the database
|
|
|
|
const nodeCount = 2
|
2020-07-08 15:28:49 +01:00
|
|
|
addNodesToNodesTable(ctx, t, db.OverlayCache(), nodeCount, 0)
|
2020-04-14 21:50:02 +01:00
|
|
|
|
|
|
|
// confirm nodes are in the cache once
|
|
|
|
err = cache.Refresh(ctx)
|
|
|
|
require.NoError(t, err)
|
2022-06-28 12:53:39 +01:00
|
|
|
reputable, new, err = cache.Size(ctx)
|
|
|
|
require.NoError(t, err)
|
2020-04-14 21:50:02 +01:00
|
|
|
require.Equal(t, 2, new)
|
|
|
|
require.Equal(t, 0, reputable)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-28 14:33:53 +00:00
|
|
|
func addNodesToNodesTable(ctx context.Context, t *testing.T, db overlay.DB, count, makeReputable int) (ids []storj.NodeID) {
|
2020-04-14 21:50:02 +01:00
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
subnet := strconv.Itoa(i) + ".1.2"
|
|
|
|
addr := subnet + ".3:8080"
|
|
|
|
n := overlay.NodeCheckInInfo{
|
|
|
|
NodeID: storj.NodeID{byte(i)},
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Address: addr,
|
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
|
|
|
},
|
|
|
|
LastNet: subnet,
|
|
|
|
LastIPPort: addr,
|
|
|
|
IsUp: true,
|
|
|
|
Capacity: &pb.NodeCapacity{
|
2020-07-08 15:28:49 +01:00
|
|
|
FreeDisk: 200 * memory.MiB.Int64(),
|
2020-04-14 21:50:02 +01:00
|
|
|
},
|
|
|
|
Version: &pb.NodeVersion{
|
|
|
|
Version: "v1.1.0",
|
|
|
|
CommitHash: "",
|
|
|
|
Timestamp: time.Time{},
|
|
|
|
Release: true,
|
|
|
|
},
|
|
|
|
}
|
2020-05-06 18:22:54 +01:00
|
|
|
err := db.UpdateCheckIn(ctx, n, time.Now().UTC(), nodeSelectionConfig)
|
2020-04-14 21:50:02 +01:00
|
|
|
require.NoError(t, err)
|
2020-04-24 17:11:04 +01:00
|
|
|
|
2020-07-08 15:28:49 +01:00
|
|
|
// make designated nodes reputable
|
|
|
|
if i < makeReputable {
|
2021-07-07 20:20:23 +01:00
|
|
|
vettedAt, err := db.TestVetNode(ctx, storj.NodeID{byte(i)})
|
2020-04-24 17:11:04 +01:00
|
|
|
require.NoError(t, err)
|
2021-07-07 20:20:23 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, vettedAt)
|
2021-01-28 14:33:53 +00:00
|
|
|
ids = append(ids, storj.NodeID{byte(i)})
|
2020-04-24 17:11:04 +01:00
|
|
|
}
|
2020-04-14 21:50:02 +01:00
|
|
|
}
|
2021-01-28 14:33:53 +00:00
|
|
|
return ids
|
2020-04-14 21:50:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type mockdb struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
callCount int
|
2020-05-06 18:00:07 +01:00
|
|
|
reputable []*overlay.SelectedNode
|
|
|
|
new []*overlay.SelectedNode
|
2020-04-14 21:50:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockdb) SelectAllStorageNodesUpload(ctx context.Context, selectionCfg overlay.NodeSelectionConfig) (reputable, new []*overlay.SelectedNode, err error) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
sync2.Sleep(ctx, 500*time.Millisecond)
|
|
|
|
m.callCount++
|
2020-05-06 18:00:07 +01:00
|
|
|
|
|
|
|
reputable = make([]*overlay.SelectedNode, len(m.reputable))
|
|
|
|
for i, n := range m.reputable {
|
|
|
|
reputable[i] = n.Clone()
|
|
|
|
}
|
|
|
|
new = make([]*overlay.SelectedNode, len(m.new))
|
|
|
|
for i, n := range m.new {
|
|
|
|
new[i] = n.Clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
return reputable, new, nil
|
2020-04-14 21:50:02 +01:00
|
|
|
}
|
2020-05-06 18:00:07 +01:00
|
|
|
|
2020-04-14 21:50:02 +01:00
|
|
|
func TestRefreshConcurrent(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
// concurrent cache.Refresh with high staleness, where high staleness means the
|
|
|
|
// cache should only be refreshed the first time we call cache.Refresh
|
|
|
|
mockDB := mockdb{}
|
2022-06-28 12:53:39 +01:00
|
|
|
cache, err := overlay.NewUploadSelectionCache(zap.NewNop(),
|
2020-04-14 21:50:02 +01:00
|
|
|
&mockDB,
|
|
|
|
highStaleness,
|
2020-05-06 18:22:54 +01:00
|
|
|
nodeSelectionConfig,
|
2020-04-14 21:50:02 +01:00
|
|
|
)
|
2022-06-28 12:53:39 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cacheCtx, cacheCancel := context.WithCancel(ctx)
|
|
|
|
defer cacheCancel()
|
|
|
|
ctx.Go(func() error { return cache.Run(cacheCtx) })
|
2020-04-14 21:50:02 +01:00
|
|
|
|
|
|
|
var group errgroup.Group
|
|
|
|
group.Go(func() error {
|
|
|
|
return cache.Refresh(ctx)
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
return cache.Refresh(ctx)
|
|
|
|
})
|
2022-06-28 12:53:39 +01:00
|
|
|
require.NoError(t, group.Wait())
|
2020-04-14 21:50:02 +01:00
|
|
|
|
|
|
|
require.Equal(t, 1, mockDB.callCount)
|
|
|
|
|
|
|
|
// concurrent cache.Refresh with low staleness, where low staleness
|
|
|
|
// means that the cache will refresh *every time* cache.Refresh is called
|
|
|
|
mockDB = mockdb{}
|
2022-06-28 12:53:39 +01:00
|
|
|
cache, err = overlay.NewUploadSelectionCache(zap.NewNop(),
|
2020-04-14 21:50:02 +01:00
|
|
|
&mockDB,
|
|
|
|
lowStaleness,
|
2020-05-06 18:22:54 +01:00
|
|
|
nodeSelectionConfig,
|
2020-04-14 21:50:02 +01:00
|
|
|
)
|
2022-06-28 12:53:39 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
ctx.Go(func() error { return cache.Run(cacheCtx) })
|
2020-04-14 21:50:02 +01:00
|
|
|
group.Go(func() error {
|
|
|
|
return cache.Refresh(ctx)
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
return cache.Refresh(ctx)
|
|
|
|
})
|
|
|
|
err = group.Wait()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-28 12:53:39 +01:00
|
|
|
require.True(t, 1 <= mockDB.callCount && mockDB.callCount <= 2, "calls %d", mockDB.callCount)
|
2020-04-14 21:50:02 +01:00
|
|
|
}
|
|
|
|
|
2020-05-06 18:00:07 +01:00
|
|
|
func TestGetNodes(t *testing.T) {
|
2020-04-14 21:50:02 +01:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2020-05-06 18:22:54 +01:00
|
|
|
var nodeSelectionConfig = overlay.NodeSelectionConfig{
|
2020-04-14 21:50:02 +01:00
|
|
|
NewNodeFraction: 0.2,
|
|
|
|
MinimumVersion: "v1.0.0",
|
|
|
|
OnlineWindow: 4 * time.Hour,
|
|
|
|
DistinctIP: true,
|
|
|
|
MinimumDiskSpace: 100 * memory.MiB,
|
|
|
|
}
|
2022-06-28 12:53:39 +01:00
|
|
|
cache, err := overlay.NewUploadSelectionCache(zap.NewNop(),
|
2020-04-14 21:50:02 +01:00
|
|
|
db.OverlayCache(),
|
|
|
|
lowStaleness,
|
2020-05-06 18:22:54 +01:00
|
|
|
nodeSelectionConfig,
|
2020-04-14 21:50:02 +01:00
|
|
|
)
|
2022-06-28 12:53:39 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cacheCtx, cacheCancel := context.WithCancel(ctx)
|
|
|
|
defer cacheCancel()
|
|
|
|
ctx.Go(func() error { return cache.Run(cacheCtx) })
|
|
|
|
|
2020-04-14 21:50:02 +01:00
|
|
|
// the cache should have no nodes to start
|
2022-06-28 12:53:39 +01:00
|
|
|
reputable, new, err := cache.Size(ctx)
|
|
|
|
require.NoError(t, err)
|
2020-04-14 21:50:02 +01:00
|
|
|
require.Equal(t, 0, reputable)
|
|
|
|
require.Equal(t, 0, new)
|
|
|
|
|
2020-07-08 15:28:49 +01:00
|
|
|
// add 4 nodes to the database and vet 2
|
2020-04-14 21:50:02 +01:00
|
|
|
const nodeCount = 4
|
2020-07-08 15:28:49 +01:00
|
|
|
nodeIds := addNodesToNodesTable(ctx, t, db.OverlayCache(), nodeCount, 2)
|
|
|
|
require.Len(t, nodeIds, 2)
|
2020-04-14 21:50:02 +01:00
|
|
|
|
|
|
|
// confirm cache.GetNodes returns the correct nodes
|
|
|
|
selectedNodes, err := cache.GetNodes(ctx, overlay.FindStorageNodesRequest{RequestedCount: 2})
|
|
|
|
require.NoError(t, err)
|
2022-06-28 12:53:39 +01:00
|
|
|
reputable, new, err = cache.Size(ctx)
|
|
|
|
require.NoError(t, err)
|
2020-07-08 15:28:49 +01:00
|
|
|
require.Equal(t, 2, new)
|
|
|
|
require.Equal(t, 2, reputable)
|
2020-04-14 21:50:02 +01:00
|
|
|
require.Equal(t, 2, len(selectedNodes))
|
|
|
|
for _, node := range selectedNodes {
|
|
|
|
require.NotEqual(t, node.ID, "")
|
|
|
|
require.NotEqual(t, node.Address.Address, "")
|
|
|
|
require.NotEqual(t, node.LastIPPort, "")
|
|
|
|
require.NotEqual(t, node.LastNet, "")
|
|
|
|
require.NotEqual(t, node.LastNet, "")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-05-06 18:00:07 +01:00
|
|
|
|
2022-02-25 16:53:24 +00:00
|
|
|
func TestGetNodesExcludeCountryCodes(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 2, UplinkCount: 0,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
err := planet.Satellites[0].Overlay.Service.TestNodeCountryCode(ctx, planet.StorageNodes[0].ID(), "FR")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cache := planet.Satellites[0].Overlay.Service.UploadSelectionCache
|
|
|
|
|
|
|
|
// confirm cache.GetNodes returns the correct nodes
|
|
|
|
selectedNodes, err := cache.GetNodes(ctx, overlay.FindStorageNodesRequest{RequestedCount: 2})
|
|
|
|
// we only expect one node to be returned, even though we requested two, so there will be an error
|
|
|
|
require.Error(t, err)
|
|
|
|
|
2022-06-28 12:53:39 +01:00
|
|
|
_, new, err := cache.Size(ctx)
|
|
|
|
require.NoError(t, err)
|
2022-02-25 16:53:24 +00:00
|
|
|
require.Equal(t, 2, new)
|
|
|
|
require.Equal(t, 1, len(selectedNodes))
|
|
|
|
// the node that was returned should be the one that does not have the "FR" country code
|
|
|
|
require.Equal(t, planet.StorageNodes[1].ID(), selectedNodes[0].ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-06 18:00:07 +01:00
|
|
|
func TestGetNodesConcurrent(t *testing.T) {
|
2020-04-14 21:50:02 +01:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2020-05-06 18:00:07 +01:00
|
|
|
reputableNodes := []*overlay.SelectedNode{{
|
|
|
|
ID: storj.NodeID{1},
|
|
|
|
Address: &pb.NodeAddress{Address: "127.0.0.9"},
|
|
|
|
LastNet: "127.0.0",
|
|
|
|
LastIPPort: "127.0.0.9:8000",
|
|
|
|
}}
|
|
|
|
newNodes := []*overlay.SelectedNode{{
|
|
|
|
ID: storj.NodeID{1},
|
|
|
|
Address: &pb.NodeAddress{Address: "127.0.0.10"},
|
|
|
|
LastNet: "127.0.0",
|
|
|
|
LastIPPort: "127.0.0.10:8000",
|
|
|
|
}}
|
|
|
|
|
2020-04-14 21:50:02 +01:00
|
|
|
// concurrent GetNodes with high staleness, where high staleness means the
|
|
|
|
// cache should only be refreshed the first time we call cache.GetNodes
|
2020-05-06 18:00:07 +01:00
|
|
|
mockDB := mockdb{
|
|
|
|
reputable: reputableNodes,
|
|
|
|
new: newNodes,
|
|
|
|
}
|
2022-06-28 12:53:39 +01:00
|
|
|
cache, err := overlay.NewUploadSelectionCache(zap.NewNop(),
|
2020-04-14 21:50:02 +01:00
|
|
|
&mockDB,
|
|
|
|
highStaleness,
|
2020-05-06 18:22:54 +01:00
|
|
|
nodeSelectionConfig,
|
2020-04-14 21:50:02 +01:00
|
|
|
)
|
2022-06-28 12:53:39 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cacheCtx, cacheCancel := context.WithCancel(ctx)
|
|
|
|
defer cacheCancel()
|
|
|
|
ctx.Go(func() error { return cache.Run(cacheCtx) })
|
2020-04-14 21:50:02 +01:00
|
|
|
|
|
|
|
var group errgroup.Group
|
|
|
|
group.Go(func() error {
|
2020-05-06 18:00:07 +01:00
|
|
|
nodes, err := cache.GetNodes(ctx, overlay.FindStorageNodesRequest{
|
2020-05-07 12:54:48 +01:00
|
|
|
RequestedCount: 1,
|
2020-05-06 18:00:07 +01:00
|
|
|
})
|
|
|
|
for i := range nodes {
|
|
|
|
nodes[i].ID = storj.NodeID{byte(i)}
|
|
|
|
nodes[i].Address.Address = "123.123.123.123"
|
|
|
|
}
|
|
|
|
nodes[0] = nil
|
2020-04-14 21:50:02 +01:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
2020-05-06 18:00:07 +01:00
|
|
|
nodes, err := cache.GetNodes(ctx, overlay.FindStorageNodesRequest{
|
2020-05-07 12:54:48 +01:00
|
|
|
RequestedCount: 1,
|
2020-05-06 18:00:07 +01:00
|
|
|
})
|
|
|
|
for i := range nodes {
|
|
|
|
nodes[i].ID = storj.NodeID{byte(i)}
|
|
|
|
nodes[i].Address.Address = "123.123.123.123"
|
|
|
|
}
|
|
|
|
nodes[0] = nil
|
2020-04-14 21:50:02 +01:00
|
|
|
return err
|
|
|
|
})
|
2022-06-28 12:53:39 +01:00
|
|
|
|
|
|
|
require.NoError(t, group.Wait())
|
2020-04-14 21:50:02 +01:00
|
|
|
// expect only one call to the db via cache.GetNodes
|
|
|
|
require.Equal(t, 1, mockDB.callCount)
|
|
|
|
|
|
|
|
// concurrent get nodes with low staleness, where low staleness means that
|
|
|
|
// the cache will refresh each time cache.GetNodes is called
|
2020-05-06 18:00:07 +01:00
|
|
|
mockDB = mockdb{
|
|
|
|
reputable: reputableNodes,
|
|
|
|
new: newNodes,
|
|
|
|
}
|
2022-06-28 12:53:39 +01:00
|
|
|
cache, err = overlay.NewUploadSelectionCache(zap.NewNop(),
|
2020-04-14 21:50:02 +01:00
|
|
|
&mockDB,
|
|
|
|
lowStaleness,
|
2020-05-06 18:22:54 +01:00
|
|
|
nodeSelectionConfig,
|
2020-04-14 21:50:02 +01:00
|
|
|
)
|
2022-06-28 12:53:39 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx.Go(func() error { return cache.Run(cacheCtx) })
|
2020-04-14 21:50:02 +01:00
|
|
|
|
|
|
|
group.Go(func() error {
|
2020-05-06 18:00:07 +01:00
|
|
|
nodes, err := cache.GetNodes(ctx, overlay.FindStorageNodesRequest{
|
2020-05-07 12:54:48 +01:00
|
|
|
RequestedCount: 1,
|
2020-05-06 18:00:07 +01:00
|
|
|
})
|
|
|
|
for i := range nodes {
|
|
|
|
nodes[i].ID = storj.NodeID{byte(i)}
|
|
|
|
nodes[i].Address.Address = "123.123.123.123"
|
|
|
|
}
|
|
|
|
nodes[0] = nil
|
2020-04-14 21:50:02 +01:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
group.Go(func() error {
|
2020-05-06 18:00:07 +01:00
|
|
|
nodes, err := cache.GetNodes(ctx, overlay.FindStorageNodesRequest{
|
2020-05-07 12:54:48 +01:00
|
|
|
RequestedCount: 1,
|
2020-05-06 18:00:07 +01:00
|
|
|
})
|
|
|
|
for i := range nodes {
|
|
|
|
nodes[i].ID = storj.NodeID{byte(i)}
|
|
|
|
nodes[i].Address.Address = "123.123.123.123"
|
|
|
|
}
|
|
|
|
nodes[0] = nil
|
2020-04-14 21:50:02 +01:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
err = group.Wait()
|
|
|
|
require.NoError(t, err)
|
2022-06-28 12:53:39 +01:00
|
|
|
// expect up to two calls to the db via cache.GetNodes
|
|
|
|
require.True(t, 1 <= mockDB.callCount && mockDB.callCount <= 2, "calls %d", mockDB.callCount)
|
2020-04-14 21:50:02 +01:00
|
|
|
}
|
|
|
|
|
2020-05-06 18:22:54 +01:00
|
|
|
func TestGetNodesDistinct(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
reputableNodes := []*overlay.SelectedNode{{
|
|
|
|
ID: testrand.NodeID(),
|
|
|
|
Address: &pb.NodeAddress{Address: "127.0.0.9"},
|
|
|
|
LastNet: "127.0.0",
|
|
|
|
LastIPPort: "127.0.0.9:8000",
|
|
|
|
}, {
|
|
|
|
ID: testrand.NodeID(),
|
|
|
|
Address: &pb.NodeAddress{Address: "127.0.0.6"},
|
|
|
|
LastNet: "127.0.0",
|
|
|
|
LastIPPort: "127.0.0.6:8000",
|
|
|
|
}, {
|
|
|
|
ID: testrand.NodeID(),
|
|
|
|
Address: &pb.NodeAddress{Address: "127.0.1.7"},
|
|
|
|
LastNet: "127.0.1",
|
|
|
|
LastIPPort: "127.0.1.7:8000",
|
|
|
|
}, {
|
|
|
|
ID: testrand.NodeID(),
|
|
|
|
Address: &pb.NodeAddress{Address: "127.0.2.7"},
|
|
|
|
LastNet: "127.0.2",
|
|
|
|
LastIPPort: "127.0.2.7:8000",
|
|
|
|
}}
|
|
|
|
|
|
|
|
newNodes := []*overlay.SelectedNode{{
|
|
|
|
ID: testrand.NodeID(),
|
|
|
|
Address: &pb.NodeAddress{Address: "127.0.0.10"},
|
|
|
|
LastNet: "127.0.0",
|
|
|
|
LastIPPort: "127.0.0.10:8000",
|
|
|
|
}, {
|
|
|
|
ID: testrand.NodeID(),
|
|
|
|
Address: &pb.NodeAddress{Address: "127.0.1.8"},
|
|
|
|
LastNet: "127.0.1",
|
|
|
|
LastIPPort: "127.0.1.8:8000",
|
|
|
|
}, {
|
|
|
|
ID: testrand.NodeID(),
|
|
|
|
Address: &pb.NodeAddress{Address: "127.0.2.8"},
|
|
|
|
LastNet: "127.0.2",
|
|
|
|
LastIPPort: "127.0.2.8:8000",
|
|
|
|
}}
|
|
|
|
|
|
|
|
mockDB := mockdb{
|
|
|
|
reputable: reputableNodes,
|
|
|
|
new: newNodes,
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// test that distinct ip doesn't return same last net
|
|
|
|
config := nodeSelectionConfig
|
|
|
|
config.NewNodeFraction = 0.5
|
|
|
|
config.DistinctIP = true
|
2022-06-28 12:53:39 +01:00
|
|
|
cache, err := overlay.NewUploadSelectionCache(zap.NewNop(),
|
2020-05-06 18:22:54 +01:00
|
|
|
&mockDB,
|
|
|
|
highStaleness,
|
|
|
|
config,
|
|
|
|
)
|
2022-06-28 12:53:39 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cacheCtx, cacheCancel := context.WithCancel(ctx)
|
|
|
|
defer cacheCancel()
|
|
|
|
ctx.Go(func() error { return cache.Run(cacheCtx) })
|
2020-05-06 18:22:54 +01:00
|
|
|
|
|
|
|
// selecting 3 should be possible
|
|
|
|
nodes, err := cache.GetNodes(ctx, overlay.FindStorageNodesRequest{
|
2020-05-07 12:54:48 +01:00
|
|
|
RequestedCount: 3,
|
2020-05-06 18:22:54 +01:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
seen := make(map[string]bool)
|
|
|
|
for _, n := range nodes {
|
|
|
|
require.False(t, seen[n.LastNet])
|
|
|
|
seen[n.LastNet] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// selecting 6 is impossible
|
|
|
|
_, err = cache.GetNodes(ctx, overlay.FindStorageNodesRequest{
|
2020-05-07 12:54:48 +01:00
|
|
|
RequestedCount: 6,
|
2020-05-06 18:22:54 +01:00
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // test that distinctIP=true allows selecting 6 nodes
|
|
|
|
config := nodeSelectionConfig
|
|
|
|
config.NewNodeFraction = 0.5
|
|
|
|
config.DistinctIP = false
|
2022-06-28 12:53:39 +01:00
|
|
|
cache, err := overlay.NewUploadSelectionCache(zap.NewNop(),
|
2020-05-06 18:22:54 +01:00
|
|
|
&mockDB,
|
|
|
|
highStaleness,
|
|
|
|
config,
|
|
|
|
)
|
2022-06-28 12:53:39 +01:00
|
|
|
require.NoError(t, err)
|
2020-05-06 18:22:54 +01:00
|
|
|
|
2022-06-28 12:53:39 +01:00
|
|
|
cacheCtx, cacheCancel := context.WithCancel(ctx)
|
|
|
|
defer cacheCancel()
|
|
|
|
ctx.Go(func() error { return cache.Run(cacheCtx) })
|
|
|
|
|
|
|
|
_, err = cache.GetNodes(ctx, overlay.FindStorageNodesRequest{
|
2020-05-07 12:54:48 +01:00
|
|
|
RequestedCount: 6,
|
2020-05-06 18:22:54 +01:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 18:00:07 +01:00
|
|
|
func TestGetNodesError(t *testing.T) {
|
2020-04-14 21:50:02 +01:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
mockDB := mockdb{}
|
2022-06-28 12:53:39 +01:00
|
|
|
cache, err := overlay.NewUploadSelectionCache(zap.NewNop(),
|
2020-04-14 21:50:02 +01:00
|
|
|
&mockDB,
|
|
|
|
highStaleness,
|
2020-05-06 18:22:54 +01:00
|
|
|
nodeSelectionConfig,
|
2020-04-14 21:50:02 +01:00
|
|
|
)
|
2022-06-28 12:53:39 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cacheCtx, cacheCancel := context.WithCancel(ctx)
|
|
|
|
defer cacheCancel()
|
|
|
|
ctx.Go(func() error { return cache.Run(cacheCtx) })
|
2020-04-14 21:50:02 +01:00
|
|
|
|
|
|
|
// there should be 0 nodes in the cache
|
2022-06-28 12:53:39 +01:00
|
|
|
reputable, new, err := cache.Size(ctx)
|
|
|
|
require.NoError(t, err)
|
2020-04-14 21:50:02 +01:00
|
|
|
require.Equal(t, 0, reputable)
|
|
|
|
require.Equal(t, 0, new)
|
|
|
|
|
|
|
|
// since the cache has no nodes, we should not be able
|
|
|
|
// to get 2 storage nodes from it and we expect an error
|
2022-06-28 12:53:39 +01:00
|
|
|
_, err = cache.GetNodes(ctx, overlay.FindStorageNodesRequest{RequestedCount: 2})
|
2020-04-14 21:50:02 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
2020-04-24 17:11:04 +01:00
|
|
|
|
|
|
|
func TestNewNodeFraction(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
newNodeFraction := 0.2
|
2020-05-06 18:22:54 +01:00
|
|
|
var nodeSelectionConfig = overlay.NodeSelectionConfig{
|
2020-04-24 17:11:04 +01:00
|
|
|
NewNodeFraction: newNodeFraction,
|
|
|
|
MinimumVersion: "v1.0.0",
|
|
|
|
OnlineWindow: 4 * time.Hour,
|
|
|
|
DistinctIP: true,
|
|
|
|
MinimumDiskSpace: 10 * memory.MiB,
|
|
|
|
}
|
2022-06-28 12:53:39 +01:00
|
|
|
cache, err := overlay.NewUploadSelectionCache(zap.NewNop(),
|
2020-04-24 17:11:04 +01:00
|
|
|
db.OverlayCache(),
|
|
|
|
lowStaleness,
|
2020-05-06 18:22:54 +01:00
|
|
|
nodeSelectionConfig,
|
2020-04-24 17:11:04 +01:00
|
|
|
)
|
2022-06-28 12:53:39 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cacheCtx, cacheCancel := context.WithCancel(ctx)
|
|
|
|
defer cacheCancel()
|
|
|
|
ctx.Go(func() error { return cache.Run(cacheCtx) })
|
|
|
|
|
2020-04-24 17:11:04 +01:00
|
|
|
// the cache should have no nodes to start
|
2022-06-28 12:53:39 +01:00
|
|
|
err = cache.Refresh(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
reputable, new, err := cache.Size(ctx)
|
2020-04-24 17:11:04 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, reputable)
|
|
|
|
require.Equal(t, 0, new)
|
|
|
|
|
|
|
|
// add some nodes to the database, some are reputable and some are new nodes
|
|
|
|
const nodeCount = 10
|
2020-07-08 15:28:49 +01:00
|
|
|
repIDs := addNodesToNodesTable(ctx, t, db.OverlayCache(), nodeCount, 4)
|
|
|
|
require.Len(t, repIDs, 4)
|
2020-04-24 17:11:04 +01:00
|
|
|
// confirm nodes are in the cache once
|
|
|
|
err = cache.Refresh(ctx)
|
|
|
|
require.NoError(t, err)
|
2022-06-28 12:53:39 +01:00
|
|
|
reputable, new, err = cache.Size(ctx)
|
|
|
|
require.NoError(t, err)
|
2020-04-24 17:11:04 +01:00
|
|
|
require.Equal(t, 6, new)
|
|
|
|
require.Equal(t, 4, reputable)
|
|
|
|
|
|
|
|
// select nodes and confirm correct new node fraction
|
|
|
|
n, err := cache.GetNodes(ctx, overlay.FindStorageNodesRequest{RequestedCount: 5})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(n), 5)
|
|
|
|
var reputableCount int
|
|
|
|
for _, id := range repIDs {
|
|
|
|
for _, node := range n {
|
|
|
|
if id == node.ID {
|
|
|
|
reputableCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 15:28:49 +01:00
|
|
|
require.Equal(t, len(n)-reputableCount, int(5*newNodeFraction)) // 1, 1
|
2020-04-24 17:11:04 +01:00
|
|
|
})
|
|
|
|
}
|