2018-10-02 20:46:29 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package checker
|
2018-10-09 17:09:33 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
2018-10-30 19:16:40 +00:00
|
|
|
"time"
|
2018-10-09 17:09:33 +01:00
|
|
|
|
2018-11-20 18:29:07 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-10-09 17:09:33 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"go.uber.org/zap"
|
2018-11-29 14:57:00 +00:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/internal/teststorj"
|
2018-10-11 21:25:54 +01:00
|
|
|
"storj.io/storj/pkg/auth"
|
2018-10-09 17:09:33 +01:00
|
|
|
"storj.io/storj/pkg/datarepair/queue"
|
|
|
|
"storj.io/storj/pkg/overlay"
|
|
|
|
"storj.io/storj/pkg/overlay/mocks"
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/pointerdb"
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-10-09 17:09:33 +01:00
|
|
|
"storj.io/storj/storage/redis"
|
|
|
|
"storj.io/storj/storage/redis/redisserver"
|
2018-11-08 13:53:27 +00:00
|
|
|
"storj.io/storj/storage/testqueue"
|
2018-10-09 17:09:33 +01:00
|
|
|
"storj.io/storj/storage/teststore"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ctx = context.Background()
|
|
|
|
|
|
|
|
func TestIdentifyInjuredSegments(t *testing.T) {
|
|
|
|
logger := zap.NewNop()
|
2018-10-11 15:35:55 +01:00
|
|
|
pointerdb := pointerdb.NewServer(teststore.New(), &overlay.Cache{}, logger, pointerdb.Config{}, nil)
|
2018-10-09 17:09:33 +01:00
|
|
|
|
2018-11-08 13:53:27 +00:00
|
|
|
repairQueue := queue.NewQueue(testqueue.New())
|
2018-10-09 17:09:33 +01:00
|
|
|
|
|
|
|
const N = 25
|
|
|
|
nodes := []*pb.Node{}
|
|
|
|
segs := []*pb.InjuredSegment{}
|
|
|
|
//fill a pointerdb
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
s := strconv.Itoa(i)
|
2018-11-29 18:39:27 +00:00
|
|
|
ids := teststorj.NodeIDsFromStrings([]string{s + "a", s + "b", s + "c", s + "d"}...)
|
2018-10-09 17:09:33 +01:00
|
|
|
|
|
|
|
p := &pb.Pointer{
|
|
|
|
Remote: &pb.RemoteSegment{
|
|
|
|
Redundancy: &pb.RedundancyScheme{
|
|
|
|
RepairThreshold: int32(2),
|
|
|
|
},
|
|
|
|
PieceId: strconv.Itoa(i),
|
|
|
|
RemotePieces: []*pb.RemotePiece{
|
|
|
|
{PieceNum: 0, NodeId: ids[0]},
|
|
|
|
{PieceNum: 1, NodeId: ids[1]},
|
|
|
|
{PieceNum: 2, NodeId: ids[2]},
|
|
|
|
{PieceNum: 3, NodeId: ids[3]},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
req := &pb.PutRequest{
|
|
|
|
Path: p.Remote.PieceId,
|
|
|
|
Pointer: p,
|
|
|
|
}
|
|
|
|
ctx = auth.WithAPIKey(ctx, nil)
|
|
|
|
resp, err := pointerdb.Put(ctx, req)
|
|
|
|
assert.NotNil(t, resp)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
//nodes for cache
|
|
|
|
selection := rand.Intn(4)
|
|
|
|
for _, v := range ids[:selection] {
|
2018-11-29 18:39:27 +00:00
|
|
|
n := &pb.Node{Id: v, Type: pb.NodeType_STORAGE, Address: &pb.NodeAddress{Address: ""}}
|
2018-10-09 17:09:33 +01:00
|
|
|
nodes = append(nodes, n)
|
|
|
|
}
|
|
|
|
pieces := []int32{0, 1, 2, 3}
|
|
|
|
//expected injured segments
|
|
|
|
if len(ids[:selection]) < int(p.Remote.Redundancy.RepairThreshold) {
|
|
|
|
seg := &pb.InjuredSegment{
|
|
|
|
Path: p.Remote.PieceId,
|
|
|
|
LostPieces: pieces[selection:],
|
|
|
|
}
|
|
|
|
segs = append(segs, seg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//fill a overlay cache
|
|
|
|
overlayServer := mocks.NewOverlay(nodes)
|
|
|
|
limit := 0
|
2018-10-30 19:16:40 +00:00
|
|
|
interval := time.Second
|
|
|
|
checker := newChecker(pointerdb, repairQueue, overlayServer, limit, logger, interval)
|
2018-11-08 16:18:28 +00:00
|
|
|
err := checker.identifyInjuredSegments(ctx)
|
2018-10-09 17:09:33 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
//check if the expected segments were added to the queue
|
|
|
|
dequeued := []*pb.InjuredSegment{}
|
|
|
|
for i := 0; i < len(segs); i++ {
|
|
|
|
injSeg, err := repairQueue.Dequeue()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
dequeued = append(dequeued, &injSeg)
|
|
|
|
}
|
|
|
|
sort.Slice(segs, func(i, k int) bool { return segs[i].Path < segs[k].Path })
|
|
|
|
sort.Slice(dequeued, func(i, k int) bool { return dequeued[i].Path < dequeued[k].Path })
|
|
|
|
|
|
|
|
for i := 0; i < len(segs); i++ {
|
|
|
|
assert.True(t, proto.Equal(segs[i], dequeued[i]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:18:28 +00:00
|
|
|
func TestOfflineNodes(t *testing.T) {
|
2018-10-09 17:09:33 +01:00
|
|
|
logger := zap.NewNop()
|
2018-10-11 15:35:55 +01:00
|
|
|
pointerdb := pointerdb.NewServer(teststore.New(), &overlay.Cache{}, logger, pointerdb.Config{}, nil)
|
2018-10-09 17:09:33 +01:00
|
|
|
|
2018-11-08 13:53:27 +00:00
|
|
|
repairQueue := queue.NewQueue(testqueue.New())
|
2018-10-09 17:09:33 +01:00
|
|
|
const N = 50
|
|
|
|
nodes := []*pb.Node{}
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeIDs := storj.NodeIDList{}
|
2018-10-09 17:09:33 +01:00
|
|
|
expectedOffline := []int32{}
|
|
|
|
for i := 0; i < N; i++ {
|
2018-11-29 18:39:27 +00:00
|
|
|
id := teststorj.NodeIDFromString(strconv.Itoa(i))
|
|
|
|
n := &pb.Node{Id: id, Type: pb.NodeType_STORAGE, Address: &pb.NodeAddress{Address: ""}}
|
2018-10-09 17:09:33 +01:00
|
|
|
nodes = append(nodes, n)
|
|
|
|
if i%(rand.Intn(5)+2) == 0 {
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeIDs = append(nodeIDs, teststorj.NodeIDFromString("id"+id.String()))
|
2018-10-09 17:09:33 +01:00
|
|
|
expectedOffline = append(expectedOffline, int32(i))
|
|
|
|
} else {
|
|
|
|
nodeIDs = append(nodeIDs, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
overlayServer := mocks.NewOverlay(nodes)
|
|
|
|
limit := 0
|
2018-10-30 19:16:40 +00:00
|
|
|
interval := time.Second
|
|
|
|
checker := newChecker(pointerdb, repairQueue, overlayServer, limit, logger, interval)
|
2018-10-09 17:09:33 +01:00
|
|
|
offline, err := checker.offlineNodes(ctx, nodeIDs)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedOffline, offline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkIdentifyInjuredSegments(b *testing.B) {
|
|
|
|
logger := zap.NewNop()
|
2018-10-11 15:35:55 +01:00
|
|
|
pointerdb := pointerdb.NewServer(teststore.New(), &overlay.Cache{}, logger, pointerdb.Config{}, nil)
|
2018-10-09 17:09:33 +01:00
|
|
|
|
|
|
|
addr, cleanup, err := redisserver.Start()
|
|
|
|
defer cleanup()
|
|
|
|
assert.NoError(b, err)
|
2018-11-08 13:53:27 +00:00
|
|
|
client, err := redis.NewQueue(addr, "", 1)
|
2018-10-09 17:09:33 +01:00
|
|
|
assert.NoError(b, err)
|
|
|
|
repairQueue := queue.NewQueue(client)
|
|
|
|
|
|
|
|
const N = 25
|
|
|
|
nodes := []*pb.Node{}
|
|
|
|
segs := []*pb.InjuredSegment{}
|
|
|
|
//fill a pointerdb
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
s := strconv.Itoa(i)
|
2018-11-29 18:39:27 +00:00
|
|
|
ids := teststorj.NodeIDsFromStrings([]string{s + "a", s + "b", s + "c", s + "d"}...)
|
2018-10-09 17:09:33 +01:00
|
|
|
|
|
|
|
p := &pb.Pointer{
|
|
|
|
Remote: &pb.RemoteSegment{
|
|
|
|
Redundancy: &pb.RedundancyScheme{
|
|
|
|
RepairThreshold: int32(2),
|
|
|
|
},
|
|
|
|
PieceId: strconv.Itoa(i),
|
|
|
|
RemotePieces: []*pb.RemotePiece{
|
|
|
|
{PieceNum: 0, NodeId: ids[0]},
|
|
|
|
{PieceNum: 1, NodeId: ids[1]},
|
|
|
|
{PieceNum: 2, NodeId: ids[2]},
|
|
|
|
{PieceNum: 3, NodeId: ids[3]},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
req := &pb.PutRequest{
|
|
|
|
Path: p.Remote.PieceId,
|
|
|
|
Pointer: p,
|
|
|
|
}
|
|
|
|
ctx = auth.WithAPIKey(ctx, nil)
|
|
|
|
resp, err := pointerdb.Put(ctx, req)
|
|
|
|
assert.NotNil(b, resp)
|
|
|
|
assert.NoError(b, err)
|
|
|
|
|
|
|
|
//nodes for cache
|
|
|
|
selection := rand.Intn(4)
|
|
|
|
for _, v := range ids[:selection] {
|
2018-11-29 18:39:27 +00:00
|
|
|
n := &pb.Node{Id: v, Type: pb.NodeType_STORAGE, Address: &pb.NodeAddress{Address: ""}}
|
2018-10-09 17:09:33 +01:00
|
|
|
nodes = append(nodes, n)
|
|
|
|
}
|
|
|
|
pieces := []int32{0, 1, 2, 3}
|
|
|
|
//expected injured segments
|
|
|
|
if len(ids[:selection]) < int(p.Remote.Redundancy.RepairThreshold) {
|
|
|
|
seg := &pb.InjuredSegment{
|
|
|
|
Path: p.Remote.PieceId,
|
|
|
|
LostPieces: pieces[selection:],
|
|
|
|
}
|
|
|
|
segs = append(segs, seg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//fill a overlay cache
|
|
|
|
overlayServer := mocks.NewOverlay(nodes)
|
|
|
|
limit := 0
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2018-10-30 19:16:40 +00:00
|
|
|
interval := time.Second
|
|
|
|
checker := newChecker(pointerdb, repairQueue, overlayServer, limit, logger, interval)
|
2018-11-08 16:18:28 +00:00
|
|
|
err = checker.identifyInjuredSegments(ctx)
|
2018-10-09 17:09:33 +01:00
|
|
|
assert.NoError(b, err)
|
|
|
|
|
|
|
|
//check if the expected segments were added to the queue
|
|
|
|
dequeued := []*pb.InjuredSegment{}
|
|
|
|
for i := 0; i < len(segs); i++ {
|
|
|
|
injSeg, err := repairQueue.Dequeue()
|
|
|
|
assert.NoError(b, err)
|
|
|
|
dequeued = append(dequeued, &injSeg)
|
|
|
|
}
|
|
|
|
sort.Slice(segs, func(i, k int) bool { return segs[i].Path < segs[k].Path })
|
|
|
|
sort.Slice(dequeued, func(i, k int) bool { return dequeued[i].Path < dequeued[k].Path })
|
|
|
|
|
|
|
|
for i := 0; i < len(segs); i++ {
|
|
|
|
assert.True(b, proto.Equal(segs[i], dequeued[i]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|