2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-19 20:39:25 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package overlay_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-04-26 13:15:06 +01:00
|
|
|
"time"
|
2018-11-19 20:39:25 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-01-31 18:49:00 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-11-19 20:39:25 +00:00
|
|
|
|
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
"storj.io/storj/internal/testplanet"
|
2019-01-29 19:42:43 +00:00
|
|
|
"storj.io/storj/pkg/overlay"
|
2019-01-31 18:49:00 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-11-19 20:39:25 +00:00
|
|
|
)
|
|
|
|
|
2019-01-31 18:49:00 +00:00
|
|
|
func TestNodeSelection(t *testing.T) {
|
2019-03-29 12:55:05 +00:00
|
|
|
t.Skip("flaky")
|
2019-02-06 13:32:42 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 10, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
var err error
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
|
|
|
|
// This sets a reputable audit count for a certain number of nodes.
|
|
|
|
for i, node := range planet.StorageNodes {
|
|
|
|
for k := 0; k < i; k++ {
|
2019-03-29 08:53:43 +00:00
|
|
|
_, err := satellite.DB.OverlayCache().UpdateStats(ctx, &overlay.UpdateRequest{
|
|
|
|
NodeID: node.ID(),
|
|
|
|
IsUp: true,
|
|
|
|
AuditSuccess: true,
|
|
|
|
})
|
2019-02-06 13:32:42 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
2019-01-31 18:49:00 +00:00
|
|
|
|
2019-02-06 13:32:42 +00:00
|
|
|
// ensure all storagenodes are in overlay service
|
|
|
|
for _, storageNode := range planet.StorageNodes {
|
2019-04-22 10:07:50 +01:00
|
|
|
err = satellite.Overlay.Service.Put(ctx, storageNode.ID(), storageNode.Local().Node)
|
2019-01-29 19:42:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2019-02-06 13:32:42 +00:00
|
|
|
|
|
|
|
type test struct {
|
|
|
|
Preferences overlay.NodeSelectionConfig
|
|
|
|
ExcludeCount int
|
2019-03-23 08:06:11 +00:00
|
|
|
RequestCount int
|
2019-02-06 13:32:42 +00:00
|
|
|
ExpectedCount int
|
|
|
|
ShouldFailWith *errs.Class
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range []test{
|
|
|
|
{ // all reputable nodes, only reputable nodes requested
|
|
|
|
Preferences: overlay.NodeSelectionConfig{
|
2019-04-23 21:47:11 +01:00
|
|
|
AuditCount: 0,
|
|
|
|
NewNodePercentage: 0,
|
2019-04-26 13:15:06 +01:00
|
|
|
OnlineWindow: time.Hour,
|
2019-02-06 13:32:42 +00:00
|
|
|
},
|
|
|
|
RequestCount: 5,
|
|
|
|
ExpectedCount: 5,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // all reputable nodes, reputable and new nodes requested
|
|
|
|
Preferences: overlay.NodeSelectionConfig{
|
2019-04-23 21:47:11 +01:00
|
|
|
AuditCount: 0,
|
|
|
|
NewNodePercentage: 1,
|
2019-04-26 13:15:06 +01:00
|
|
|
OnlineWindow: time.Hour,
|
2019-02-06 13:32:42 +00:00
|
|
|
},
|
|
|
|
RequestCount: 5,
|
|
|
|
ExpectedCount: 5,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // all reputable nodes except one, reputable and new nodes requested
|
|
|
|
Preferences: overlay.NodeSelectionConfig{
|
2019-04-23 21:47:11 +01:00
|
|
|
AuditCount: 1,
|
|
|
|
NewNodePercentage: 1,
|
2019-04-26 13:15:06 +01:00
|
|
|
OnlineWindow: time.Hour,
|
2019-02-06 13:32:42 +00:00
|
|
|
},
|
|
|
|
RequestCount: 5,
|
|
|
|
ExpectedCount: 6,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // 50-50 reputable and new nodes, reputable and new nodes requested (new node ratio 1.0)
|
|
|
|
Preferences: overlay.NodeSelectionConfig{
|
2019-04-23 21:47:11 +01:00
|
|
|
AuditCount: 5,
|
|
|
|
NewNodePercentage: 1,
|
2019-04-26 13:15:06 +01:00
|
|
|
OnlineWindow: time.Hour,
|
2019-02-06 13:32:42 +00:00
|
|
|
},
|
|
|
|
RequestCount: 2,
|
|
|
|
ExpectedCount: 4,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // 50-50 reputable and new nodes, reputable and new nodes requested (new node ratio 0.5)
|
|
|
|
Preferences: overlay.NodeSelectionConfig{
|
2019-04-23 21:47:11 +01:00
|
|
|
AuditCount: 5,
|
|
|
|
NewNodePercentage: 0.5,
|
2019-04-26 13:15:06 +01:00
|
|
|
OnlineWindow: time.Hour,
|
2019-02-06 13:32:42 +00:00
|
|
|
},
|
|
|
|
RequestCount: 4,
|
|
|
|
ExpectedCount: 6,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // all new nodes except one, reputable and new nodes requested (happy path)
|
|
|
|
Preferences: overlay.NodeSelectionConfig{
|
2019-04-23 21:47:11 +01:00
|
|
|
AuditCount: 8,
|
|
|
|
NewNodePercentage: 1,
|
2019-04-26 13:15:06 +01:00
|
|
|
OnlineWindow: time.Hour,
|
2019-02-06 13:32:42 +00:00
|
|
|
},
|
|
|
|
RequestCount: 1,
|
|
|
|
ExpectedCount: 2,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // all new nodes except one, reputable and new nodes requested (not happy path)
|
|
|
|
Preferences: overlay.NodeSelectionConfig{
|
2019-04-23 21:47:11 +01:00
|
|
|
AuditCount: 9,
|
|
|
|
NewNodePercentage: 1,
|
2019-04-26 13:15:06 +01:00
|
|
|
OnlineWindow: time.Hour,
|
2019-02-06 13:32:42 +00:00
|
|
|
},
|
|
|
|
RequestCount: 2,
|
|
|
|
ExpectedCount: 3,
|
|
|
|
ShouldFailWith: &overlay.ErrNotEnoughNodes,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // all new nodes, reputable and new nodes requested
|
|
|
|
Preferences: overlay.NodeSelectionConfig{
|
2019-04-23 21:47:11 +01:00
|
|
|
AuditCount: 50,
|
|
|
|
NewNodePercentage: 1,
|
2019-04-26 13:15:06 +01:00
|
|
|
OnlineWindow: time.Hour,
|
2019-02-06 13:32:42 +00:00
|
|
|
},
|
|
|
|
RequestCount: 2,
|
|
|
|
ExpectedCount: 2,
|
|
|
|
ShouldFailWith: &overlay.ErrNotEnoughNodes,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // audit threshold edge case (1)
|
|
|
|
Preferences: overlay.NodeSelectionConfig{
|
2019-04-23 21:47:11 +01:00
|
|
|
AuditCount: 9,
|
|
|
|
NewNodePercentage: 0,
|
2019-04-26 13:15:06 +01:00
|
|
|
OnlineWindow: time.Hour,
|
2019-02-06 13:32:42 +00:00
|
|
|
},
|
|
|
|
RequestCount: 1,
|
|
|
|
ExpectedCount: 1,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // audit threshold edge case (2)
|
|
|
|
Preferences: overlay.NodeSelectionConfig{
|
2019-04-23 21:47:11 +01:00
|
|
|
AuditCount: 0,
|
|
|
|
NewNodePercentage: 1,
|
2019-04-26 13:15:06 +01:00
|
|
|
OnlineWindow: time.Hour,
|
2019-02-06 13:32:42 +00:00
|
|
|
},
|
|
|
|
RequestCount: 1,
|
|
|
|
ExpectedCount: 1,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // excluded node ids being excluded
|
|
|
|
Preferences: overlay.NodeSelectionConfig{
|
2019-04-23 21:47:11 +01:00
|
|
|
AuditCount: 5,
|
|
|
|
NewNodePercentage: 0,
|
2019-04-26 13:15:06 +01:00
|
|
|
OnlineWindow: time.Hour,
|
2019-02-06 13:32:42 +00:00
|
|
|
},
|
|
|
|
ExcludeCount: 7,
|
|
|
|
RequestCount: 5,
|
|
|
|
ExpectedCount: 3,
|
|
|
|
ShouldFailWith: &overlay.ErrNotEnoughNodes,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
} {
|
|
|
|
t.Logf("#%2d. %+v", i, tt)
|
2019-03-23 08:06:11 +00:00
|
|
|
service := planet.Satellites[0].Overlay.Service
|
2019-02-06 13:32:42 +00:00
|
|
|
|
|
|
|
var excludedNodes []storj.NodeID
|
|
|
|
for _, storageNode := range planet.StorageNodes[:tt.ExcludeCount] {
|
|
|
|
excludedNodes = append(excludedNodes, storageNode.ID())
|
|
|
|
}
|
2019-01-29 19:42:43 +00:00
|
|
|
|
2019-03-23 08:06:11 +00:00
|
|
|
response, err := service.FindStorageNodesWithPreferences(ctx, overlay.FindStorageNodesRequest{
|
|
|
|
FreeBandwidth: 0,
|
|
|
|
FreeDisk: 0,
|
|
|
|
RequestedCount: tt.RequestCount,
|
|
|
|
ExcludedNodes: excludedNodes,
|
|
|
|
}, &tt.Preferences)
|
|
|
|
|
|
|
|
t.Log(len(response), err)
|
2019-02-06 13:32:42 +00:00
|
|
|
if tt.ShouldFailWith != nil {
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, tt.ShouldFailWith.Has(err))
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2019-01-29 19:42:43 +00:00
|
|
|
|
2019-03-23 08:06:11 +00:00
|
|
|
assert.Equal(t, tt.ExpectedCount, len(response))
|
2019-01-29 19:42:43 +00:00
|
|
|
}
|
2019-02-06 13:32:42 +00:00
|
|
|
})
|
2019-01-29 19:42:43 +00:00
|
|
|
}
|