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"
|
2019-12-16 13:45:13 +00:00
|
|
|
"sort"
|
2018-06-13 19:22:32 +01:00
|
|
|
"testing"
|
2019-04-26 13:15:06 +01:00
|
|
|
"time"
|
2018-06-13 19:22:32 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-09 21:17:49 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-12-16 13:45:13 +00:00
|
|
|
"go.uber.org/zap"
|
2019-03-23 08:06:11 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2019-02-06 13:32:42 +00:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
2018-12-27 09:56:25 +00:00
|
|
|
"storj.io/storj/satellite"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/satellite/overlay"
|
2018-12-17 20:14:16 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
2019-12-18 10:41:02 +00:00
|
|
|
"storj.io/storj/storagenode"
|
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()
|
|
|
|
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2019-03-25 22:25:09 +00:00
|
|
|
testCache(ctx, t, db.OverlayCache())
|
2019-01-15 16:08:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-13 17:06:37 +01:00
|
|
|
// returns a NodeSelectionConfig with sensible test values
|
2019-06-13 22:51:18 +01:00
|
|
|
func testNodeSelectionConfig(auditCount int64, newNodePercentage float64, distinctIP bool) overlay.NodeSelectionConfig {
|
2019-06-13 17:06:37 +01:00
|
|
|
return overlay.NodeSelectionConfig{
|
|
|
|
UptimeCount: 0,
|
2019-06-13 22:51:18 +01:00
|
|
|
AuditCount: auditCount,
|
|
|
|
NewNodePercentage: newNodePercentage,
|
2019-06-13 17:06:37 +01:00
|
|
|
OnlineWindow: time.Hour,
|
2019-06-13 22:51:18 +01:00
|
|
|
DistinctIP: distinctIP,
|
|
|
|
|
2020-01-03 00:00:18 +00:00
|
|
|
AuditReputationRepairWeight: 1,
|
|
|
|
AuditReputationUplinkWeight: 1,
|
|
|
|
AuditReputationAlpha0: 1,
|
|
|
|
AuditReputationBeta0: 0,
|
|
|
|
AuditReputationLambda: 1,
|
|
|
|
AuditReputationWeight: 1,
|
|
|
|
AuditReputationDQ: 0.5,
|
2019-06-13 17:06:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
func testCache(ctx context.Context, t *testing.T, store overlay.DB) {
|
2019-06-26 11:38:51 +01:00
|
|
|
valid1ID := testrand.NodeID()
|
|
|
|
valid2ID := testrand.NodeID()
|
2019-07-12 15:35:48 +01:00
|
|
|
valid3ID := testrand.NodeID()
|
2019-06-26 11:38:51 +01:00
|
|
|
missingID := testrand.NodeID()
|
2019-05-30 18:35:04 +01:00
|
|
|
address := &pb.NodeAddress{Address: "127.0.0.1:0"}
|
2018-12-17 18:47:26 +00:00
|
|
|
|
2019-06-20 14:56:04 +01:00
|
|
|
nodeSelectionConfig := testNodeSelectionConfig(0, 0, false)
|
2019-08-06 17:35:59 +01:00
|
|
|
serviceConfig := overlay.Config{Node: nodeSelectionConfig, UpdateStatsBatchSize: 100}
|
|
|
|
service := overlay.NewService(zaptest.NewLogger(t), store, serviceConfig)
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-11-19 14:40:01 +00:00
|
|
|
{ // Put
|
2019-08-06 17:35:59 +01:00
|
|
|
err := service.Put(ctx, valid1ID, pb.Node{Id: valid1ID, Address: address})
|
2019-06-28 14:04:50 +01:00
|
|
|
require.NoError(t, err)
|
2018-12-17 18:47:26 +00:00
|
|
|
|
2019-08-06 17:35:59 +01:00
|
|
|
err = service.Put(ctx, valid2ID, pb.Node{Id: valid2ID, Address: address})
|
2019-06-28 14:04:50 +01:00
|
|
|
require.NoError(t, err)
|
2019-07-12 15:35:48 +01:00
|
|
|
|
2019-08-06 17:35:59 +01:00
|
|
|
err = service.Put(ctx, valid3ID, pb.Node{Id: valid3ID, Address: address})
|
2019-07-12 15:35:48 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-03 00:00:18 +00:00
|
|
|
// disqualify one node
|
|
|
|
err = service.DisqualifyNode(ctx, valid3ID)
|
2019-07-12 15:35:48 +01:00
|
|
|
require.NoError(t, 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
|
2019-08-06 17:35:59 +01:00
|
|
|
_, err := service.Get(ctx, storj.NodeID{})
|
2019-06-20 14:56:04 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, err == overlay.ErrEmptyNode)
|
2018-12-17 18:47:26 +00:00
|
|
|
|
2019-08-06 17:35:59 +01:00
|
|
|
valid1, err := service.Get(ctx, valid1ID)
|
2019-06-20 14:56:04 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, valid1.Id, valid1ID)
|
2018-12-17 18:47:26 +00:00
|
|
|
|
2019-08-06 17:35:59 +01:00
|
|
|
valid2, err := service.Get(ctx, valid2ID)
|
2019-06-20 14:56:04 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, valid2.Id, valid2ID)
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2019-08-06 17:35:59 +01:00
|
|
|
invalid2, err := service.Get(ctx, missingID)
|
2019-06-20 14:56:04 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, overlay.ErrNodeNotFound.Has(err))
|
|
|
|
require.Nil(t, invalid2)
|
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-11-16 16:31:14 +00:00
|
|
|
|
2019-01-30 16:29:18 +00:00
|
|
|
{ // Paginate
|
|
|
|
|
|
|
|
// should return two nodes
|
2019-08-06 17:35:59 +01:00
|
|
|
nodes, more, err := service.Paginate(ctx, 0, 2)
|
2019-01-30 16:29:18 +00:00
|
|
|
assert.NotNil(t, more)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(nodes), 2)
|
|
|
|
|
|
|
|
// should return no nodes
|
2019-08-06 17:35:59 +01:00
|
|
|
zero, more, err := service.Paginate(ctx, 0, 0)
|
2019-01-30 16:29:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, more)
|
|
|
|
assert.NotEqual(t, len(zero), 0)
|
|
|
|
}
|
2019-06-20 14:56:04 +01:00
|
|
|
|
2019-07-12 15:35:48 +01:00
|
|
|
{ // PaginateQualified
|
|
|
|
|
|
|
|
// should return two nodes
|
2019-08-06 17:35:59 +01:00
|
|
|
nodes, more, err := service.PaginateQualified(ctx, 0, 3)
|
2019-07-12 15:35:48 +01:00
|
|
|
assert.NotNil(t, more)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(nodes), 2)
|
|
|
|
}
|
|
|
|
|
2019-06-20 14:56:04 +01:00
|
|
|
{ // Reputation
|
2019-08-06 17:35:59 +01:00
|
|
|
valid1, err := service.Get(ctx, valid1ID)
|
2019-06-20 14:56:04 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, valid1.Id, valid1ID)
|
|
|
|
require.EqualValues(t, valid1.Reputation.AuditReputationAlpha, nodeSelectionConfig.AuditReputationAlpha0)
|
|
|
|
require.EqualValues(t, valid1.Reputation.AuditReputationBeta, nodeSelectionConfig.AuditReputationBeta0)
|
|
|
|
require.Nil(t, valid1.Reputation.Disqualified)
|
|
|
|
|
2019-08-06 17:35:59 +01:00
|
|
|
stats, err := service.UpdateStats(ctx, &overlay.UpdateRequest{
|
2019-06-20 14:56:04 +01:00
|
|
|
NodeID: valid1ID,
|
|
|
|
IsUp: true,
|
|
|
|
AuditSuccess: false,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
newAuditAlpha := 1
|
|
|
|
newAuditBeta := 1
|
|
|
|
require.EqualValues(t, stats.AuditReputationAlpha, newAuditAlpha)
|
|
|
|
require.EqualValues(t, stats.AuditReputationBeta, newAuditBeta)
|
|
|
|
require.NotNil(t, stats.Disqualified)
|
|
|
|
require.True(t, time.Now().UTC().Sub(*stats.Disqualified) < time.Minute)
|
|
|
|
|
2020-01-03 00:00:18 +00:00
|
|
|
err = service.DisqualifyNode(ctx, valid2ID)
|
2019-06-20 14:56:04 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// should not update once already disqualified
|
2019-08-06 17:35:59 +01:00
|
|
|
_, err = service.BatchUpdateStats(ctx, []*overlay.UpdateRequest{{
|
2019-06-20 14:56:04 +01:00
|
|
|
NodeID: valid2ID,
|
|
|
|
IsUp: false,
|
|
|
|
AuditSuccess: true,
|
2019-07-31 18:21:06 +01:00
|
|
|
}})
|
2019-06-20 14:56:04 +01:00
|
|
|
require.NoError(t, err)
|
2019-08-06 17:35:59 +01:00
|
|
|
dossier, err := service.Get(ctx, valid2ID)
|
2019-06-20 14:56:04 +01:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
2019-07-31 18:21:06 +01:00
|
|
|
require.EqualValues(t, dossier.Reputation.AuditReputationAlpha, nodeSelectionConfig.AuditReputationAlpha0)
|
|
|
|
require.EqualValues(t, dossier.Reputation.AuditReputationBeta, nodeSelectionConfig.AuditReputationBeta0)
|
|
|
|
require.NotNil(t, dossier.Disqualified)
|
2019-06-20 14:56:04 +01:00
|
|
|
}
|
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
|
2019-02-11 12:04:00 +00:00
|
|
|
minSelectCount := 3 // TODO: compute this limit better
|
2019-02-09 21:17:49 +00:00
|
|
|
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2019-02-09 21:17:49 +00:00
|
|
|
cache := db.OverlayCache()
|
|
|
|
allIDs := make(storj.NodeIDList, totalNodes)
|
|
|
|
nodeCounts := make(map[storj.NodeID]int)
|
2019-06-20 14:56:04 +01:00
|
|
|
defaults := overlay.NodeSelectionConfig{
|
2020-01-03 00:00:18 +00:00
|
|
|
AuditReputationAlpha0: 1,
|
|
|
|
AuditReputationBeta0: 0,
|
2019-06-20 14:56:04 +01:00
|
|
|
}
|
2019-02-09 21:17:49 +00:00
|
|
|
|
|
|
|
// put nodes in cache
|
|
|
|
for i := 0; i < totalNodes; i++ {
|
2019-06-26 11:38:51 +01:00
|
|
|
newID := testrand.NodeID()
|
2020-03-06 22:04:23 +00:00
|
|
|
n := pb.Node{Id: newID}
|
|
|
|
d := overlay.NodeDossier{Node: n, LastIPPort: "", LastNet: ""}
|
|
|
|
err := cache.UpdateAddress(ctx, &d, defaults)
|
2019-04-22 10:07:50 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = cache.UpdateNodeInfo(ctx, newID, &pb.InfoResponse{
|
|
|
|
Type: pb.NodeType_STORAGE,
|
|
|
|
Capacity: &pb.NodeCapacity{},
|
2019-02-09 21:17:49 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2019-06-20 14:56:04 +01:00
|
|
|
|
|
|
|
if i%2 == 0 { // make half of nodes "new" and half "vetted"
|
|
|
|
_, err = cache.UpdateStats(ctx, &overlay.UpdateRequest{
|
|
|
|
NodeID: newID,
|
|
|
|
IsUp: true,
|
|
|
|
AuditSuccess: true,
|
|
|
|
AuditLambda: 1,
|
|
|
|
AuditWeight: 1,
|
|
|
|
AuditDQ: 0.5,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-02-09 21:17:49 +00:00
|
|
|
allIDs[i] = newID
|
|
|
|
nodeCounts[newID] = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// select numNodesToSelect nodes selectIterations times
|
|
|
|
for i := 0; i < selectIterations; i++ {
|
2020-03-06 22:04:23 +00:00
|
|
|
var nodes []*overlay.NodeDossier
|
2019-02-11 17:10:32 +00:00
|
|
|
var err error
|
|
|
|
|
2019-04-24 11:35:50 +01:00
|
|
|
if i%2 == 0 {
|
2019-06-20 14:56:04 +01:00
|
|
|
nodes, err = cache.SelectStorageNodes(ctx, numNodesToSelect, &overlay.NodeCriteria{
|
|
|
|
OnlineWindow: time.Hour,
|
|
|
|
AuditCount: 1,
|
|
|
|
})
|
2019-02-11 17:10:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
2019-04-23 21:47:11 +01:00
|
|
|
nodes, err = cache.SelectNewStorageNodes(ctx, numNodesToSelect, &overlay.NodeCriteria{
|
2019-04-26 13:15:06 +01:00
|
|
|
OnlineWindow: time.Hour,
|
|
|
|
AuditCount: 1,
|
2019-02-11 17:10:32 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2019-02-09 21:17:49 +00:00
|
|
|
require.Len(t, nodes, numNodesToSelect)
|
|
|
|
|
|
|
|
for _, node := range nodes {
|
|
|
|
nodeCounts[node.Id]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 12:04:00 +00:00
|
|
|
belowThreshold := 0
|
|
|
|
|
|
|
|
table := []int{}
|
|
|
|
|
2019-02-09 21:17:49 +00:00
|
|
|
// expect that each node has been selected at least minSelectCount times
|
|
|
|
for _, id := range allIDs {
|
|
|
|
count := nodeCounts[id]
|
2019-02-11 12:04:00 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-02-09 21:17:49 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-05-30 20:52:33 +01:00
|
|
|
|
2019-06-11 14:30:28 +01:00
|
|
|
func TestNodeInfo(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 0,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
planet.StorageNodes[0].Storage2.Monitor.Loop.Pause()
|
|
|
|
|
|
|
|
node, err := planet.Satellites[0].Overlay.Service.Get(ctx, planet.StorageNodes[0].ID())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, pb.NodeType_STORAGE, node.Type)
|
|
|
|
assert.NotEmpty(t, node.Operator.Email)
|
|
|
|
assert.NotEmpty(t, node.Operator.Wallet)
|
|
|
|
assert.Equal(t, planet.StorageNodes[0].Local().Operator, node.Operator)
|
|
|
|
assert.NotEmpty(t, node.Capacity.FreeDisk)
|
|
|
|
assert.Equal(t, planet.StorageNodes[0].Local().Capacity, node.Capacity)
|
|
|
|
assert.NotEmpty(t, node.Version.Version)
|
|
|
|
assert.Equal(t, planet.StorageNodes[0].Local().Version.Version, node.Version.Version)
|
|
|
|
})
|
|
|
|
}
|
2019-09-19 19:37:31 +01:00
|
|
|
|
2019-12-16 13:45:13 +00:00
|
|
|
func TestKnownReliable(t *testing.T) {
|
|
|
|
onlineWindow := 500 * time.Millisecond
|
|
|
|
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 4, UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Overlay.Node.OnlineWindow = onlineWindow
|
|
|
|
},
|
2019-12-18 10:41:02 +00:00
|
|
|
StorageNode: func(index int, config *storagenode.Config) {
|
|
|
|
config.Contact.Interval = onlineWindow / 2
|
|
|
|
},
|
2019-12-16 13:45:13 +00:00
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
service := satellite.Overlay.Service
|
|
|
|
|
|
|
|
// Disqualify storage node #0
|
|
|
|
stats, err := service.UpdateStats(ctx, &overlay.UpdateRequest{
|
|
|
|
NodeID: planet.StorageNodes[0].ID(),
|
|
|
|
AuditSuccess: false,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, stats.Disqualified)
|
|
|
|
|
|
|
|
// Stop storage node #1
|
|
|
|
err = planet.StopPeer(planet.StorageNodes[1])
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = service.UpdateUptime(ctx, planet.StorageNodes[1].ID(), false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Sleep for the duration of the online window and check that storage node #1 is offline
|
|
|
|
time.Sleep(onlineWindow)
|
|
|
|
node, err := service.Get(ctx, planet.StorageNodes[1].ID())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, service.IsOnline(node))
|
|
|
|
|
|
|
|
// Check that only storage nodes #2 and #3 are reliable
|
|
|
|
result, err := service.KnownReliable(ctx, []storj.NodeID{
|
|
|
|
planet.StorageNodes[0].ID(),
|
|
|
|
planet.StorageNodes[1].ID(),
|
|
|
|
planet.StorageNodes[2].ID(),
|
|
|
|
planet.StorageNodes[3].ID(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, result, 2)
|
|
|
|
|
|
|
|
// Sort the storage nodes for predictable checks
|
|
|
|
expectedReliable := []pb.Node{planet.StorageNodes[2].Local().Node, planet.StorageNodes[3].Local().Node}
|
|
|
|
sort.Slice(expectedReliable, func(i, j int) bool { return expectedReliable[i].Id.Less(expectedReliable[j].Id) })
|
|
|
|
sort.Slice(result, func(i, j int) bool { return result[i].Id.Less(result[j].Id) })
|
|
|
|
|
|
|
|
// Assert the reliable nodes are the expected ones
|
|
|
|
for i, node := range result {
|
|
|
|
assert.Equal(t, expectedReliable[i].Id, node.Id)
|
|
|
|
assert.Equal(t, expectedReliable[i].Address, node.Address)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-19 19:37:31 +01:00
|
|
|
func TestUpdateCheckIn(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) { // setup
|
2019-09-19 19:37:31 +01:00
|
|
|
nodeID := storj.NodeID{1, 2, 3}
|
|
|
|
expectedEmail := "test@email.com"
|
2020-03-06 22:04:23 +00:00
|
|
|
expectedAddress := "1.2.4.4:8080"
|
2019-09-19 19:37:31 +01:00
|
|
|
info := overlay.NodeCheckInInfo{
|
|
|
|
NodeID: nodeID,
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Address: expectedAddress,
|
|
|
|
},
|
|
|
|
IsUp: true,
|
|
|
|
Capacity: &pb.NodeCapacity{
|
2020-02-12 21:19:42 +00:00
|
|
|
FreeDisk: int64(5678),
|
2019-09-19 19:37:31 +01:00
|
|
|
},
|
|
|
|
Operator: &pb.NodeOperator{
|
|
|
|
Email: expectedEmail,
|
|
|
|
Wallet: "0x123",
|
|
|
|
},
|
|
|
|
Version: &pb.NodeVersion{
|
|
|
|
Version: "v0.0.0",
|
|
|
|
CommitHash: "",
|
|
|
|
Timestamp: time.Time{},
|
|
|
|
Release: false,
|
|
|
|
},
|
2020-03-06 22:04:23 +00:00
|
|
|
LastIPPort: expectedAddress,
|
|
|
|
LastNet: "1.2.4",
|
2019-09-19 19:37:31 +01:00
|
|
|
}
|
|
|
|
expectedNode := &overlay.NodeDossier{
|
|
|
|
Node: pb.Node{
|
2020-03-06 22:04:23 +00:00
|
|
|
Id: nodeID,
|
2019-09-19 19:37:31 +01:00
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Address: info.Address.GetAddress(),
|
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Type: pb.NodeType_STORAGE,
|
|
|
|
Operator: pb.NodeOperator{
|
|
|
|
Email: info.Operator.GetEmail(),
|
|
|
|
Wallet: info.Operator.GetWallet(),
|
|
|
|
},
|
|
|
|
Capacity: pb.NodeCapacity{
|
2020-02-12 21:19:42 +00:00
|
|
|
FreeDisk: info.Capacity.GetFreeDisk(),
|
2019-09-19 19:37:31 +01:00
|
|
|
},
|
|
|
|
Reputation: overlay.NodeStats{
|
2020-01-03 00:00:18 +00:00
|
|
|
UptimeCount: 1,
|
|
|
|
UptimeSuccessCount: 1,
|
2019-09-19 19:37:31 +01:00
|
|
|
},
|
|
|
|
Version: pb.NodeVersion{
|
|
|
|
Version: "v0.0.0",
|
|
|
|
CommitHash: "",
|
|
|
|
Timestamp: time.Time{},
|
|
|
|
Release: false,
|
|
|
|
},
|
|
|
|
Contained: false,
|
|
|
|
Disqualified: nil,
|
|
|
|
PieceCount: 0,
|
2019-10-11 22:18:05 +01:00
|
|
|
ExitStatus: overlay.ExitStatus{NodeID: nodeID},
|
2020-03-06 22:04:23 +00:00
|
|
|
LastIPPort: expectedAddress,
|
|
|
|
LastNet: "1.2.4",
|
2019-09-19 19:37:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// confirm the node doesn't exist in nodes table yet
|
|
|
|
_, err := db.OverlayCache().Get(ctx, nodeID)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "node not found")
|
|
|
|
|
|
|
|
// check-in for that node id, which should add the node
|
|
|
|
// to the nodes tables in the database
|
|
|
|
startOfTest := time.Now().UTC()
|
2020-01-03 00:00:18 +00:00
|
|
|
err = db.OverlayCache().UpdateCheckIn(ctx, info, time.Now().UTC(), overlay.NodeSelectionConfig{})
|
2019-09-19 19:37:31 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// confirm that the node is now in the nodes table with the
|
|
|
|
// correct fields set
|
|
|
|
actualNode, err := db.OverlayCache().Get(ctx, nodeID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, actualNode.Reputation.LastContactSuccess.After(startOfTest))
|
2019-11-15 22:43:06 +00:00
|
|
|
require.True(t, actualNode.Reputation.LastContactFailure.UTC().Equal(time.Time{}.UTC()))
|
2019-09-19 19:37:31 +01:00
|
|
|
|
|
|
|
// we need to overwrite the times so that the deep equal considers them the same
|
|
|
|
expectedNode.Reputation.LastContactSuccess = actualNode.Reputation.LastContactSuccess
|
|
|
|
expectedNode.Reputation.LastContactFailure = actualNode.Reputation.LastContactFailure
|
|
|
|
expectedNode.Version.Timestamp = actualNode.Version.Timestamp
|
2019-10-23 02:06:01 +01:00
|
|
|
expectedNode.CreatedAt = actualNode.CreatedAt
|
2019-10-11 22:18:05 +01:00
|
|
|
require.Equal(t, expectedNode, actualNode)
|
2019-09-19 19:37:31 +01:00
|
|
|
|
|
|
|
// confirm that we can update the address field
|
|
|
|
startOfUpdateTest := time.Now().UTC()
|
|
|
|
expectedAddress = "9.8.7.6"
|
|
|
|
updatedInfo := overlay.NodeCheckInInfo{
|
|
|
|
NodeID: nodeID,
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Address: expectedAddress,
|
|
|
|
},
|
|
|
|
IsUp: true,
|
|
|
|
Version: &pb.NodeVersion{
|
2019-09-26 01:07:39 +01:00
|
|
|
Version: "v0.1.0",
|
|
|
|
CommitHash: "abc123",
|
|
|
|
Timestamp: time.Now().UTC(),
|
|
|
|
Release: true,
|
2019-09-19 19:37:31 +01:00
|
|
|
},
|
2020-03-06 22:04:23 +00:00
|
|
|
LastIPPort: expectedAddress,
|
|
|
|
LastNet: "9.8.7",
|
2019-09-19 19:37:31 +01:00
|
|
|
}
|
|
|
|
// confirm that the updated node is in the nodes table with the
|
|
|
|
// correct updated fields set
|
2020-01-03 00:00:18 +00:00
|
|
|
err = db.OverlayCache().UpdateCheckIn(ctx, updatedInfo, time.Now().UTC(), overlay.NodeSelectionConfig{})
|
2019-09-19 19:37:31 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
updatedNode, err := db.OverlayCache().Get(ctx, nodeID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, updatedNode.Reputation.LastContactSuccess.After(startOfUpdateTest))
|
|
|
|
require.True(t, updatedNode.Reputation.LastContactFailure.Equal(time.Time{}.UTC()))
|
|
|
|
require.Equal(t, updatedNode.Address.GetAddress(), expectedAddress)
|
|
|
|
require.Equal(t, updatedNode.Reputation.UptimeSuccessCount, actualNode.Reputation.UptimeSuccessCount+1)
|
2019-09-26 01:07:39 +01:00
|
|
|
require.Equal(t, updatedInfo.Version.GetVersion(), updatedNode.Version.GetVersion())
|
|
|
|
require.Equal(t, updatedInfo.Version.GetCommitHash(), updatedNode.Version.GetCommitHash())
|
|
|
|
require.Equal(t, updatedInfo.Version.GetRelease(), updatedNode.Version.GetRelease())
|
|
|
|
require.True(t, updatedNode.Version.GetTimestamp().After(info.Version.GetTimestamp()))
|
2019-09-19 19:37:31 +01:00
|
|
|
|
2020-03-06 22:04:23 +00:00
|
|
|
// confirm we can update IsUp field
|
2019-09-19 19:37:31 +01:00
|
|
|
startOfUpdateTest2 := time.Now().UTC()
|
|
|
|
updatedInfo2 := overlay.NodeCheckInInfo{
|
|
|
|
NodeID: nodeID,
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Address: "9.8.7.6",
|
|
|
|
},
|
|
|
|
IsUp: false,
|
|
|
|
Version: &pb.NodeVersion{
|
|
|
|
Version: "v0.0.0",
|
|
|
|
CommitHash: "",
|
|
|
|
Timestamp: time.Time{},
|
|
|
|
Release: false,
|
|
|
|
},
|
|
|
|
}
|
2020-01-03 00:00:18 +00:00
|
|
|
err = db.OverlayCache().UpdateCheckIn(ctx, updatedInfo2, time.Now().UTC(), overlay.NodeSelectionConfig{})
|
2019-09-19 19:37:31 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
updated2Node, err := db.OverlayCache().Get(ctx, nodeID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, updated2Node.Reputation.LastContactSuccess.Equal(updatedNode.Reputation.LastContactSuccess))
|
|
|
|
require.Equal(t, updated2Node.Reputation.UptimeSuccessCount, updatedNode.Reputation.UptimeSuccessCount)
|
|
|
|
require.True(t, updated2Node.Reputation.LastContactFailure.After(startOfUpdateTest2))
|
|
|
|
})
|
|
|
|
}
|
2019-12-30 17:10:24 +00:00
|
|
|
|
|
|
|
func TestCache_DowntimeTracking(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2019-12-30 17:10:24 +00:00
|
|
|
cache := db.OverlayCache()
|
|
|
|
defaults := overlay.NodeSelectionConfig{
|
2020-01-03 00:00:18 +00:00
|
|
|
AuditReputationAlpha0: 1,
|
|
|
|
AuditReputationBeta0: 0,
|
2019-12-30 17:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
totalNodes := 10
|
|
|
|
allIDs := make(storj.NodeIDList, totalNodes)
|
|
|
|
// put nodes in cache
|
|
|
|
for i := 0; i < totalNodes; i++ {
|
|
|
|
newID := testrand.NodeID()
|
2020-03-06 22:04:23 +00:00
|
|
|
n := pb.Node{Id: newID}
|
|
|
|
d := overlay.NodeDossier{Node: n, LastIPPort: "", LastNet: ""}
|
|
|
|
err := cache.UpdateAddress(ctx, &d, defaults)
|
2019-12-30 17:10:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = cache.UpdateNodeInfo(ctx, newID, &pb.InfoResponse{
|
|
|
|
Type: pb.NodeType_STORAGE,
|
|
|
|
Capacity: &pb.NodeCapacity{},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
allIDs[i] = newID
|
|
|
|
|
|
|
|
// make half of the nodes (0, 2, 4, 6, 8) offline + not disqualified
|
|
|
|
if i%2 == 0 {
|
2020-01-03 00:00:18 +00:00
|
|
|
_, err := cache.UpdateUptime(ctx, newID, false)
|
2019-12-30 17:10:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
// make first node (0) offline + disqualified
|
|
|
|
if i == 0 {
|
2020-01-03 00:00:18 +00:00
|
|
|
_, err := cache.UpdateUptime(ctx, newID, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = cache.DisqualifyNode(ctx, newID)
|
2019-12-30 17:10:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes, err := cache.GetOfflineNodesLimited(ctx, 10)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, nodes, 4)
|
|
|
|
// order of nodes should be least recently checked first
|
2020-01-06 20:06:05 +00:00
|
|
|
require.Equal(t, allIDs[2], nodes[0].ID)
|
|
|
|
require.Equal(t, allIDs[4], nodes[1].ID)
|
|
|
|
require.Equal(t, allIDs[6], nodes[2].ID)
|
|
|
|
require.Equal(t, allIDs[8], nodes[3].ID)
|
2019-12-30 17:10:24 +00:00
|
|
|
|
|
|
|
// test with limit
|
|
|
|
nodes, err = cache.GetOfflineNodesLimited(ctx, 2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, nodes, 2)
|
|
|
|
// order of nodes should be least recently checked first
|
2020-01-06 20:06:05 +00:00
|
|
|
require.Equal(t, allIDs[2], nodes[0].ID)
|
|
|
|
require.Equal(t, allIDs[4], nodes[1].ID)
|
2019-12-30 17:10:24 +00:00
|
|
|
})
|
|
|
|
}
|
2020-01-02 20:41:18 +00:00
|
|
|
|
|
|
|
func TestGetSuccesfulNodesNotCheckedInSince(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) { // setup
|
2020-01-02 20:41:18 +00:00
|
|
|
info1 := getNodeInfo(testrand.NodeID())
|
|
|
|
info2 := getNodeInfo(testrand.NodeID())
|
|
|
|
|
|
|
|
{ // check-in the nodes, which should add them
|
|
|
|
twoHoursAgo := time.Now().UTC().Add(-2 * time.Hour)
|
2020-01-03 00:00:18 +00:00
|
|
|
err := db.OverlayCache().UpdateCheckIn(ctx, info1, twoHoursAgo, overlay.NodeSelectionConfig{})
|
2020-01-02 20:41:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-03 00:00:18 +00:00
|
|
|
err = db.OverlayCache().UpdateCheckIn(ctx, info2, twoHoursAgo, overlay.NodeSelectionConfig{})
|
2020-01-02 20:41:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// update uptime so that node 2 has a last contact failure > last contact success
|
2020-01-03 00:00:18 +00:00
|
|
|
_, err = db.OverlayCache().UpdateUptime(ctx, info2.NodeID, false)
|
2020-01-02 20:41:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// should just get 1 node
|
|
|
|
nodeLastContacts, err := db.OverlayCache().GetSuccesfulNodesNotCheckedInSince(ctx, time.Duration(0))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, nodeLastContacts, 1)
|
2020-01-21 17:43:53 +00:00
|
|
|
require.WithinDuration(t, twoHoursAgo, nodeLastContacts[0].LastContactSuccess, time.Second)
|
2020-01-02 20:41:18 +00:00
|
|
|
require.True(t, nodeLastContacts[0].LastContactFailure.IsZero())
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // check-in again with current time
|
2020-01-03 00:00:18 +00:00
|
|
|
err := db.OverlayCache().UpdateCheckIn(ctx, info1, time.Now().UTC(), overlay.NodeSelectionConfig{})
|
2020-01-02 20:41:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
nodeLastContacts, err := db.OverlayCache().GetSuccesfulNodesNotCheckedInSince(ctx, time.Minute)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, nodeLastContacts, 0)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNodeInfo(nodeID storj.NodeID) overlay.NodeCheckInInfo {
|
|
|
|
return overlay.NodeCheckInInfo{
|
|
|
|
NodeID: nodeID,
|
|
|
|
IsUp: true,
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Address: "1.2.3.4",
|
|
|
|
},
|
|
|
|
Operator: &pb.NodeOperator{
|
|
|
|
Email: "test@email.com",
|
|
|
|
Wallet: "0x123",
|
|
|
|
},
|
|
|
|
Version: &pb.NodeVersion{
|
|
|
|
Version: "v0.0.0",
|
|
|
|
CommitHash: "",
|
|
|
|
Timestamp: time.Time{},
|
|
|
|
Release: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|