2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-10-02 00:25:41 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-12-21 15:11:19 +00:00
|
|
|
package queue_test
|
2018-10-02 00:25:41 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2018-12-21 15:11:19 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
"storj.io/storj/pkg/datarepair/queue"
|
2018-10-02 00:25:41 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-12-27 09:56:25 +00:00
|
|
|
"storj.io/storj/satellite"
|
2018-12-21 15:11:19 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
2018-10-08 20:37:27 +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-02 00:25:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEnqueueDequeue(t *testing.T) {
|
2018-12-27 09:56:25 +00:00
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2018-12-21 15:11:19 +00:00
|
|
|
|
|
|
|
q := db.RepairQueue()
|
|
|
|
|
|
|
|
seg := &pb.InjuredSegment{
|
|
|
|
Path: "abc",
|
|
|
|
LostPieces: []int32{int32(1), int32(3)},
|
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
err := q.Enqueue(ctx, seg)
|
2018-12-21 15:11:19 +00:00
|
|
|
assert.NoError(t, err)
|
2018-10-02 00:25:41 +01:00
|
|
|
|
2018-12-27 09:56:25 +00:00
|
|
|
s, err := q.Dequeue(ctx)
|
2018-12-21 15:11:19 +00:00
|
|
|
assert.NoError(t, err)
|
2019-01-28 19:45:25 +00:00
|
|
|
assert.True(t, pb.Equal(&s, seg))
|
2018-12-21 15:11:19 +00:00
|
|
|
})
|
2018-10-02 00:25:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDequeueEmptyQueue(t *testing.T) {
|
2018-12-27 09:56:25 +00:00
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2018-12-21 15:11:19 +00:00
|
|
|
|
|
|
|
q := db.RepairQueue()
|
|
|
|
|
2018-12-27 09:56:25 +00:00
|
|
|
s, err := q.Dequeue(ctx)
|
2018-12-21 15:11:19 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, pb.InjuredSegment{}, s)
|
|
|
|
})
|
2018-10-02 00:25:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSequential(t *testing.T) {
|
2018-12-27 09:56:25 +00:00
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2018-12-21 15:11:19 +00:00
|
|
|
|
|
|
|
q := db.RepairQueue()
|
|
|
|
|
|
|
|
const N = 100
|
|
|
|
var addSegs []*pb.InjuredSegment
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
seg := &pb.InjuredSegment{
|
|
|
|
Path: strconv.Itoa(i),
|
|
|
|
LostPieces: []int32{int32(i)},
|
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
err := q.Enqueue(ctx, seg)
|
2018-12-21 15:11:19 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
addSegs = append(addSegs, seg)
|
2018-10-02 00:25:41 +01:00
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
|
|
|
|
list, err := q.Peekqueue(ctx, 100)
|
2018-10-02 00:25:41 +01:00
|
|
|
assert.NoError(t, err)
|
2018-12-21 15:11:19 +00:00
|
|
|
for i := 0; i < N; i++ {
|
2019-01-28 19:45:25 +00:00
|
|
|
assert.True(t, pb.Equal(addSegs[i], &list[i]))
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
|
|
|
|
// TODO: fix out of order issue
|
2018-12-21 15:11:19 +00:00
|
|
|
for i := 0; i < N; i++ {
|
2018-12-27 09:56:25 +00:00
|
|
|
dequeued, err := q.Dequeue(ctx)
|
2018-12-21 15:11:19 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-27 09:56:25 +00:00
|
|
|
expected := dequeued.LostPieces[0]
|
2019-01-28 19:45:25 +00:00
|
|
|
assert.True(t, pb.Equal(addSegs[expected], &dequeued))
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
|
|
|
})
|
2018-10-02 00:25:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParallel(t *testing.T) {
|
2018-12-27 09:56:25 +00:00
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2018-10-02 00:25:41 +01:00
|
|
|
|
2018-12-27 09:56:25 +00:00
|
|
|
q := db.RepairQueue()
|
|
|
|
const N = 100
|
|
|
|
errs := make(chan error, N*2)
|
|
|
|
entries := make(chan *pb.InjuredSegment, N*2)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(N)
|
|
|
|
// Add to queue concurrently
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
|
|
|
err := q.Enqueue(ctx, &pb.InjuredSegment{
|
|
|
|
Path: strconv.Itoa(i),
|
|
|
|
LostPieces: []int32{int32(i)},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
errs <- err
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2018-10-02 00:25:41 +01:00
|
|
|
|
2018-12-27 09:56:25 +00:00
|
|
|
wg.Add(N)
|
|
|
|
// Remove from queue concurrently
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
|
|
|
segment, err := q.Dequeue(ctx)
|
|
|
|
if err != nil {
|
|
|
|
errs <- err
|
|
|
|
}
|
|
|
|
entries <- &segment
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
close(errs)
|
|
|
|
close(entries)
|
|
|
|
|
|
|
|
for err := range errs {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var items []*pb.InjuredSegment
|
|
|
|
for segment := range entries {
|
|
|
|
items = append(items, segment)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(items, func(i, k int) bool {
|
|
|
|
return items[i].LostPieces[0] < items[k].LostPieces[0]
|
|
|
|
})
|
|
|
|
|
|
|
|
// check if the enqueued and dequeued elements match
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
assert.Equal(t, items[i].LostPieces[0], int32(i))
|
|
|
|
}
|
|
|
|
})
|
2018-10-02 00:25:41 +01:00
|
|
|
}
|
2018-10-08 20:37:27 +01:00
|
|
|
|
|
|
|
func BenchmarkRedisSequential(b *testing.B) {
|
|
|
|
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-08 20:37:27 +01:00
|
|
|
assert.NoError(b, err)
|
2018-12-21 15:11:19 +00:00
|
|
|
q := queue.NewQueue(client)
|
2018-10-08 20:37:27 +01:00
|
|
|
benchmarkSequential(b, q)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTeststoreSequential(b *testing.B) {
|
2018-12-21 15:11:19 +00:00
|
|
|
q := queue.NewQueue(testqueue.New())
|
2018-10-08 20:37:27 +01:00
|
|
|
benchmarkSequential(b, q)
|
|
|
|
}
|
|
|
|
|
2018-12-21 15:11:19 +00:00
|
|
|
func benchmarkSequential(b *testing.B, q queue.RepairQueue) {
|
2018-12-27 09:56:25 +00:00
|
|
|
ctx := testcontext.New(b)
|
|
|
|
defer ctx.Cleanup()
|
2018-12-21 15:11:19 +00:00
|
|
|
|
2018-10-08 20:37:27 +01:00
|
|
|
b.ResetTimer()
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
const N = 100
|
|
|
|
var addSegs []*pb.InjuredSegment
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
seg := &pb.InjuredSegment{
|
|
|
|
Path: strconv.Itoa(i),
|
|
|
|
LostPieces: []int32{int32(i)},
|
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
err := q.Enqueue(ctx, seg)
|
2018-10-08 20:37:27 +01:00
|
|
|
assert.NoError(b, err)
|
|
|
|
addSegs = append(addSegs, seg)
|
|
|
|
}
|
|
|
|
for i := 0; i < N; i++ {
|
2018-12-27 09:56:25 +00:00
|
|
|
dqSeg, err := q.Dequeue(ctx)
|
2018-10-08 20:37:27 +01:00
|
|
|
assert.NoError(b, err)
|
2019-01-28 19:45:25 +00:00
|
|
|
assert.True(b, pb.Equal(addSegs[i], &dqSeg))
|
2018-10-08 20:37:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRedisParallel(b *testing.B) {
|
|
|
|
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-08 20:37:27 +01:00
|
|
|
assert.NoError(b, err)
|
2018-12-21 15:11:19 +00:00
|
|
|
q := queue.NewQueue(client)
|
2018-10-08 20:37:27 +01:00
|
|
|
benchmarkParallel(b, q)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTeststoreParallel(b *testing.B) {
|
2018-12-21 15:11:19 +00:00
|
|
|
q := queue.NewQueue(testqueue.New())
|
2018-10-08 20:37:27 +01:00
|
|
|
benchmarkParallel(b, q)
|
|
|
|
}
|
|
|
|
|
2018-12-21 15:11:19 +00:00
|
|
|
func benchmarkParallel(b *testing.B, q queue.RepairQueue) {
|
2018-12-27 09:56:25 +00:00
|
|
|
ctx := testcontext.New(b)
|
|
|
|
defer ctx.Cleanup()
|
2018-12-21 15:11:19 +00:00
|
|
|
|
2018-10-08 20:37:27 +01:00
|
|
|
b.ResetTimer()
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
const N = 100
|
|
|
|
errs := make(chan error, N*2)
|
|
|
|
entries := make(chan *pb.InjuredSegment, N*2)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(N)
|
|
|
|
// Add to queue concurrently
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
2018-12-27 09:56:25 +00:00
|
|
|
err := q.Enqueue(ctx, &pb.InjuredSegment{
|
2018-10-08 20:37:27 +01:00
|
|
|
Path: strconv.Itoa(i),
|
|
|
|
LostPieces: []int32{int32(i)},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
errs <- err
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
wg.Add(N)
|
|
|
|
// Remove from queue concurrently
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
2018-12-27 09:56:25 +00:00
|
|
|
segment, err := q.Dequeue(ctx)
|
2018-10-08 20:37:27 +01:00
|
|
|
if err != nil {
|
|
|
|
errs <- err
|
|
|
|
}
|
|
|
|
entries <- &segment
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
close(errs)
|
|
|
|
close(entries)
|
|
|
|
|
|
|
|
for err := range errs {
|
|
|
|
b.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var items []*pb.InjuredSegment
|
|
|
|
for segment := range entries {
|
|
|
|
items = append(items, segment)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(items, func(i, k int) bool { return items[i].LostPieces[0] < items[k].LostPieces[0] })
|
|
|
|
// check if the enqueued and dequeued elements match
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
assert.Equal(b, items[i].LostPieces[0], int32(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|