2021-01-28 14:33:53 +00:00
|
|
|
// Copyright (C) 2021 Storj Labs, Incache.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package overlay_test
|
|
|
|
|
|
|
|
import (
|
2022-06-28 12:53:39 +01:00
|
|
|
"context"
|
2021-01-28 14:33:53 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
|
|
|
"storj.io/storj/satellite"
|
2023-07-07 09:31:58 +01:00
|
|
|
"storj.io/storj/satellite/nodeselection"
|
2021-01-28 14:33:53 +00:00
|
|
|
"storj.io/storj/satellite/overlay"
|
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
var downloadSelectionCacheConfig = overlay.DownloadSelectionCacheConfig{
|
|
|
|
Staleness: lowStaleness,
|
|
|
|
OnlineWindow: time.Hour,
|
|
|
|
AsOfSystemTime: overlay.AsOfSystemTimeConfig{Enabled: true, DefaultInterval: time.Minute},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownloadSelectionCacheState_Refresh(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.NewDownloadSelectionCache(zap.NewNop(),
|
2021-01-28 14:33:53 +00:00
|
|
|
db.OverlayCache(),
|
2023-07-06 13:35:26 +01:00
|
|
|
overlay.NewPlacementRules().CreateFilters,
|
2021-01-28 14:33:53 +00:00
|
|
|
downloadSelectionCacheConfig,
|
|
|
|
)
|
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) })
|
2021-01-28 14:33:53 +00: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)
|
|
|
|
count, err := cache.Size(ctx)
|
2021-01-28 14:33:53 +00:00
|
|
|
require.NoError(t, err)
|
2022-06-28 12:53:39 +01:00
|
|
|
require.Equal(t, 0, count)
|
2021-01-28 14:33:53 +00:00
|
|
|
|
|
|
|
// add some nodes to the database
|
|
|
|
const nodeCount = 2
|
|
|
|
addNodesToNodesTable(ctx, t, db.OverlayCache(), nodeCount, 0)
|
|
|
|
|
|
|
|
// confirm nodes are in the cache once
|
|
|
|
err = cache.Refresh(ctx)
|
|
|
|
require.NoError(t, err)
|
2022-06-28 12:53:39 +01:00
|
|
|
count, err = cache.Size(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, nodeCount, count)
|
2021-01-28 14:33:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownloadSelectionCacheState_GetNodeIPs(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.NewDownloadSelectionCache(zap.NewNop(),
|
2021-01-28 14:33:53 +00:00
|
|
|
db.OverlayCache(),
|
2023-07-06 13:35:26 +01:00
|
|
|
overlay.NewPlacementRules().CreateFilters,
|
2021-01-28 14:33:53 +00:00
|
|
|
downloadSelectionCacheConfig,
|
|
|
|
)
|
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) })
|
2021-01-28 14:33:53 +00:00
|
|
|
|
|
|
|
// add some nodes to the database
|
|
|
|
const nodeCount = 2
|
|
|
|
ids := addNodesToNodesTable(ctx, t, db.OverlayCache(), nodeCount, 0)
|
|
|
|
|
|
|
|
// confirm nodes are in the cache once
|
2023-06-07 12:58:25 +01:00
|
|
|
nodeips, err := cache.GetNodeIPsFromPlacement(ctx, ids, storj.EveryCountry)
|
2021-01-28 14:33:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
for _, id := range ids {
|
|
|
|
require.NotEmpty(t, nodeips[id])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownloadSelectionCacheState_IPs(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2023-07-07 09:31:58 +01:00
|
|
|
node := &nodeselection.SelectedNode{
|
2021-01-28 14:33:53 +00:00
|
|
|
ID: testrand.NodeID(),
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Address: "1.0.1.1:8080",
|
|
|
|
},
|
|
|
|
LastNet: "1.0.1",
|
|
|
|
LastIPPort: "1.0.1.1:8080",
|
|
|
|
}
|
|
|
|
|
2023-07-07 09:31:58 +01:00
|
|
|
state := overlay.NewDownloadSelectionCacheState([]*nodeselection.SelectedNode{node})
|
2021-01-28 14:33:53 +00:00
|
|
|
require.Equal(t, state.Size(), 1)
|
|
|
|
|
|
|
|
ips := state.IPs([]storj.NodeID{testrand.NodeID(), node.ID})
|
|
|
|
require.Len(t, ips, 1)
|
|
|
|
require.Equal(t, node.LastIPPort, ips[node.ID])
|
|
|
|
}
|
2022-06-22 21:26:53 +01:00
|
|
|
|
|
|
|
func TestDownloadSelectionCache_GetNodes(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
// add some reputable nodes to the database
|
|
|
|
const nodeCount = 2
|
|
|
|
ids := addNodesToNodesTable(ctx, t, db.OverlayCache(), nodeCount, nodeCount)
|
|
|
|
|
|
|
|
// create new cache and select nodes
|
|
|
|
cache, err := overlay.NewDownloadSelectionCache(zap.NewNop(),
|
|
|
|
db.OverlayCache(),
|
2023-07-06 13:35:26 +01:00
|
|
|
overlay.NewPlacementRules().CreateFilters,
|
2022-06-22 21:26:53 +01:00
|
|
|
overlay.DownloadSelectionCacheConfig{
|
|
|
|
Staleness: time.Hour,
|
|
|
|
OnlineWindow: time.Hour,
|
|
|
|
AsOfSystemTime: overlay.AsOfSystemTimeConfig{Enabled: true, DefaultInterval: time.Minute},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cacheCtx, cacheCancel := context.WithCancel(ctx)
|
|
|
|
defer cacheCancel()
|
|
|
|
ctx.Go(func() error { return cache.Run(cacheCtx) })
|
|
|
|
|
|
|
|
// get nodes, expect to see all nodes
|
|
|
|
nodes, err := cache.GetNodes(ctx, ids)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, nodes, nodeCount)
|
|
|
|
|
|
|
|
// disqualify one node
|
2022-10-11 17:13:29 +01:00
|
|
|
_, err = db.OverlayCache().DisqualifyNode(ctx, ids[0], time.Now(), overlay.DisqualificationReasonAuditFailure)
|
2022-06-22 21:26:53 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
// suspend the other node
|
|
|
|
err = db.OverlayCache().TestSuspendNodeUnknownAudit(ctx, ids[1], time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// cache should still contain disqualified node since it has not refreshed
|
|
|
|
nodes, err = cache.GetNodes(ctx, ids)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, nodes, nodeCount)
|
|
|
|
|
|
|
|
// update cache staleness so it refreshes on the next call to GetNodes
|
|
|
|
err = cache.Refresh(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// cache should not contain disqualified node after refresh
|
|
|
|
// it should still contain the suspended node, since a suspended node can still be used for download
|
|
|
|
nodes, err = cache.GetNodes(ctx, ids)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, nodes, nodeCount-1)
|
|
|
|
for _, n := range nodes {
|
|
|
|
require.NotEqual(t, ids[0], n.ID)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|