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.
|
|
|
|
|
2019-04-23 09:47:16 +01:00
|
|
|
package queue_test
|
2018-10-02 00:25:41 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
2020-09-09 21:52:22 +01:00
|
|
|
"time"
|
2018-10-02 00:25:41 +01:00
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/errs2"
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/testcontext"
|
2018-12-27 09:56:25 +00:00
|
|
|
"storj.io/storj/satellite"
|
2020-10-30 10:41:22 +00:00
|
|
|
"storj.io/storj/satellite/internalpb"
|
2018-12-21 15:11:19 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
2019-04-16 19:14:09 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-10-02 00:25:41 +01:00
|
|
|
)
|
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
func TestInsertSelect(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2018-12-21 15:11:19 +00:00
|
|
|
q := db.RepairQueue()
|
|
|
|
|
2020-10-30 10:41:22 +00:00
|
|
|
seg := &internalpb.InjuredSegment{
|
2019-07-11 18:26:07 +01:00
|
|
|
Path: []byte("abc"),
|
2018-12-21 15:11:19 +00:00
|
|
|
LostPieces: []int32{int32(1), int32(3)},
|
|
|
|
}
|
2020-05-22 20:54:05 +01:00
|
|
|
alreadyInserted, err := q.Insert(ctx, seg, 10)
|
2019-04-16 19:14:09 +01:00
|
|
|
require.NoError(t, err)
|
2020-05-22 20:54:05 +01:00
|
|
|
require.False(t, alreadyInserted)
|
2019-04-16 19:14:09 +01:00
|
|
|
s, err := q.Select(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = q.Delete(ctx, s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, pb.Equal(s, seg))
|
2018-12-21 15:11:19 +00:00
|
|
|
})
|
2018-10-02 00:25:41 +01:00
|
|
|
}
|
|
|
|
|
2019-04-22 17:41:53 +01:00
|
|
|
func TestInsertDuplicate(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-04-22 17:41:53 +01:00
|
|
|
q := db.RepairQueue()
|
|
|
|
|
2020-10-30 10:41:22 +00:00
|
|
|
seg := &internalpb.InjuredSegment{
|
2019-07-11 18:26:07 +01:00
|
|
|
Path: []byte("abc"),
|
2019-04-22 17:41:53 +01:00
|
|
|
LostPieces: []int32{int32(1), int32(3)},
|
|
|
|
}
|
2020-05-22 20:54:05 +01:00
|
|
|
alreadyInserted, err := q.Insert(ctx, seg, 10)
|
2019-04-22 17:41:53 +01:00
|
|
|
require.NoError(t, err)
|
2020-05-22 20:54:05 +01:00
|
|
|
require.False(t, alreadyInserted)
|
|
|
|
alreadyInserted, err = q.Insert(ctx, seg, 10)
|
2019-04-22 17:41:53 +01:00
|
|
|
require.NoError(t, err)
|
2020-05-22 20:54:05 +01:00
|
|
|
require.True(t, alreadyInserted)
|
2019-04-22 17:41:53 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-02 00:25:41 +01:00
|
|
|
func TestDequeueEmptyQueue(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2018-12-21 15:11:19 +00:00
|
|
|
q := db.RepairQueue()
|
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
_, err := q.Select(ctx)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, storage.ErrEmptyQueue.Has(err), "error should of class EmptyQueue")
|
2018-12-21 15:11:19 +00:00
|
|
|
})
|
2018-10-02 00:25:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSequential(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2018-12-21 15:11:19 +00:00
|
|
|
q := db.RepairQueue()
|
|
|
|
|
2020-05-22 20:54:05 +01:00
|
|
|
const N = 20
|
2020-10-30 10:41:22 +00:00
|
|
|
var addSegs []*internalpb.InjuredSegment
|
2018-12-21 15:11:19 +00:00
|
|
|
for i := 0; i < N; i++ {
|
2020-10-30 10:41:22 +00:00
|
|
|
seg := &internalpb.InjuredSegment{
|
2019-07-11 18:26:07 +01:00
|
|
|
Path: []byte(strconv.Itoa(i)),
|
2018-12-21 15:11:19 +00:00
|
|
|
LostPieces: []int32{int32(i)},
|
|
|
|
}
|
2020-05-22 20:54:05 +01:00
|
|
|
alreadyInserted, err := q.Insert(ctx, seg, 10)
|
2019-04-16 19:14:09 +01:00
|
|
|
require.NoError(t, err)
|
2020-05-22 20:54:05 +01:00
|
|
|
require.False(t, alreadyInserted)
|
2018-12-21 15:11:19 +00:00
|
|
|
addSegs = append(addSegs, seg)
|
2018-10-02 00:25:41 +01:00
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
list, err := q.SelectN(ctx, N)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, list, N)
|
2019-12-10 16:32:54 +00:00
|
|
|
|
|
|
|
sort.SliceStable(list, func(i, j int) bool { return list[i].LostPieces[0] < list[j].LostPieces[0] })
|
|
|
|
|
2018-12-21 15:11:19 +00:00
|
|
|
for i := 0; i < N; i++ {
|
2019-04-16 19:14:09 +01:00
|
|
|
require.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++ {
|
2019-04-16 19:14:09 +01:00
|
|
|
s, err := q.Select(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = q.Delete(ctx, s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
expected := s.LostPieces[0]
|
|
|
|
require.True(t, pb.Equal(addSegs[expected], s))
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
|
|
|
})
|
2018-10-02 00:25:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParallel(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2018-12-27 09:56:25 +00:00
|
|
|
q := db.RepairQueue()
|
2020-05-22 20:54:05 +01:00
|
|
|
const N = 20
|
2020-10-30 10:41:22 +00:00
|
|
|
entries := make(chan *internalpb.InjuredSegment, N)
|
2019-04-24 11:15:46 +01:00
|
|
|
|
|
|
|
var inserts errs2.Group
|
2018-12-27 09:56:25 +00:00
|
|
|
// Add to queue concurrently
|
|
|
|
for i := 0; i < N; i++ {
|
2019-04-24 11:15:46 +01:00
|
|
|
i := i
|
|
|
|
inserts.Go(func() error {
|
2020-10-30 10:41:22 +00:00
|
|
|
_, err := q.Insert(ctx, &internalpb.InjuredSegment{
|
2019-07-11 18:26:07 +01:00
|
|
|
Path: []byte(strconv.Itoa(i)),
|
2018-12-27 09:56:25 +00:00
|
|
|
LostPieces: []int32{int32(i)},
|
2020-02-21 21:32:05 +00:00
|
|
|
}, 10)
|
2020-05-22 20:54:05 +01:00
|
|
|
return err
|
2019-04-24 11:15:46 +01:00
|
|
|
})
|
2018-12-27 09:56:25 +00:00
|
|
|
}
|
2019-04-24 11:15:46 +01:00
|
|
|
require.Empty(t, inserts.Wait(), "unexpected queue.Insert errors")
|
2018-10-02 00:25:41 +01:00
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
// Remove from queue concurrently
|
2019-04-24 11:15:46 +01:00
|
|
|
var remove errs2.Group
|
2018-10-08 20:37:27 +01:00
|
|
|
for i := 0; i < N; i++ {
|
2019-04-24 11:15:46 +01:00
|
|
|
remove.Go(func() error {
|
2019-04-16 19:14:09 +01:00
|
|
|
s, err := q.Select(ctx)
|
2018-10-08 20:37:27 +01:00
|
|
|
if err != nil {
|
2019-04-24 11:15:46 +01:00
|
|
|
return err
|
2018-10-08 20:37:27 +01:00
|
|
|
}
|
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
err = q.Delete(ctx, s)
|
2018-10-08 20:37:27 +01:00
|
|
|
if err != nil {
|
2019-04-24 11:15:46 +01:00
|
|
|
return err
|
2018-10-08 20:37:27 +01:00
|
|
|
}
|
2019-04-16 19:14:09 +01:00
|
|
|
|
|
|
|
entries <- s
|
2019-04-24 11:15:46 +01:00
|
|
|
return nil
|
|
|
|
})
|
2018-10-08 20:37:27 +01:00
|
|
|
}
|
2019-04-16 19:14:09 +01:00
|
|
|
|
2019-04-24 11:15:46 +01:00
|
|
|
require.Empty(t, remove.Wait(), "unexpected queue.Select/Delete errors")
|
|
|
|
close(entries)
|
2018-10-08 20:37:27 +01:00
|
|
|
|
2020-10-30 10:41:22 +00:00
|
|
|
var items []*internalpb.InjuredSegment
|
2018-10-08 20:37:27 +01:00
|
|
|
for segment := range entries {
|
|
|
|
items = append(items, segment)
|
|
|
|
}
|
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
sort.Slice(items, func(i, k int) bool {
|
|
|
|
return items[i].LostPieces[0] < items[k].LostPieces[0]
|
|
|
|
})
|
|
|
|
|
2018-10-08 20:37:27 +01:00
|
|
|
// check if the enqueued and dequeued elements match
|
|
|
|
for i := 0; i < N; i++ {
|
2019-04-16 19:14:09 +01:00
|
|
|
require.Equal(t, items[i].LostPieces[0], int32(i))
|
2018-10-08 20:37:27 +01:00
|
|
|
}
|
2019-04-16 19:14:09 +01:00
|
|
|
})
|
2018-10-08 20:37:27 +01:00
|
|
|
}
|
2020-09-09 21:52:22 +01:00
|
|
|
|
|
|
|
func TestClean(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
q := db.RepairQueue()
|
|
|
|
|
2020-10-30 10:41:22 +00:00
|
|
|
seg1 := &internalpb.InjuredSegment{
|
2020-09-09 21:52:22 +01:00
|
|
|
Path: []byte("seg1"),
|
|
|
|
LostPieces: []int32{int32(1), int32(3)},
|
|
|
|
}
|
2020-10-30 10:41:22 +00:00
|
|
|
seg2 := &internalpb.InjuredSegment{
|
2020-09-09 21:52:22 +01:00
|
|
|
Path: []byte("seg2"),
|
|
|
|
LostPieces: []int32{int32(1), int32(3)},
|
|
|
|
}
|
2020-10-30 10:41:22 +00:00
|
|
|
seg3 := &internalpb.InjuredSegment{
|
2020-09-09 21:52:22 +01:00
|
|
|
Path: []byte("seg3"),
|
|
|
|
LostPieces: []int32{int32(1), int32(3)},
|
|
|
|
}
|
|
|
|
|
|
|
|
timeBeforeInsert1 := time.Now()
|
|
|
|
|
2020-10-21 23:02:54 +01:00
|
|
|
segmentHealth := 1.3
|
|
|
|
_, err := q.Insert(ctx, seg1, segmentHealth)
|
2020-09-09 21:52:22 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-21 23:02:54 +01:00
|
|
|
_, err = q.Insert(ctx, seg2, segmentHealth)
|
2020-09-09 21:52:22 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-21 23:02:54 +01:00
|
|
|
_, err = q.Insert(ctx, seg3, segmentHealth)
|
2020-09-09 21:52:22 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
count, err := q.Count(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 3, count)
|
|
|
|
|
|
|
|
d, err := q.Clean(ctx, timeBeforeInsert1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(0), d)
|
|
|
|
|
|
|
|
count, err = q.Count(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 3, count)
|
|
|
|
|
|
|
|
timeBeforeInsert2 := time.Now()
|
|
|
|
|
|
|
|
// seg1 "becomes healthy", so do not update it
|
|
|
|
// seg2 stays at the same health
|
2020-10-21 23:02:54 +01:00
|
|
|
_, err = q.Insert(ctx, seg2, segmentHealth)
|
2020-09-09 21:52:22 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// seg3 has a lower health
|
2020-10-21 23:02:54 +01:00
|
|
|
_, err = q.Insert(ctx, seg3, segmentHealth-0.1)
|
2020-09-09 21:52:22 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
count, err = q.Count(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 3, count)
|
|
|
|
|
|
|
|
d, err = q.Clean(ctx, timeBeforeInsert2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(1), d)
|
|
|
|
|
|
|
|
count, err = q.Count(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, count)
|
|
|
|
|
|
|
|
d, err = q.Clean(ctx, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(2), d)
|
|
|
|
|
|
|
|
count, err = q.Count(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, count)
|
|
|
|
})
|
|
|
|
}
|