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"
|
|
|
|
"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/testcontext"
|
2021-06-17 16:05:04 +01:00
|
|
|
"storj.io/common/testrand"
|
|
|
|
"storj.io/common/uuid"
|
2018-12-27 09:56:25 +00:00
|
|
|
"storj.io/storj/satellite"
|
2021-06-17 16:05:04 +01:00
|
|
|
"storj.io/storj/satellite/repair/queue"
|
2018-12-21 15:11:19 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
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()
|
|
|
|
|
2022-04-19 10:22:05 +01:00
|
|
|
seg := createInjuredSegment()
|
|
|
|
seg.SegmentHealth = 0.4
|
|
|
|
|
2021-06-17 16:05:04 +01:00
|
|
|
alreadyInserted, err := q.Insert(ctx, seg)
|
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)
|
2021-06-17 16:05:04 +01:00
|
|
|
require.Equal(t, seg.StreamID, s.StreamID)
|
|
|
|
require.Equal(t, seg.Position, s.Position)
|
|
|
|
require.Equal(t, seg.SegmentHealth, s.SegmentHealth)
|
|
|
|
require.WithinDuration(t, time.Now(), s.InsertedAt, 5*time.Second)
|
|
|
|
require.NotZero(t, s.UpdatedAt)
|
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()
|
|
|
|
|
2022-04-19 10:22:05 +01:00
|
|
|
seg := createInjuredSegment()
|
2021-06-17 16:05:04 +01:00
|
|
|
alreadyInserted, err := q.Insert(ctx, seg)
|
2019-04-22 17:41:53 +01:00
|
|
|
require.NoError(t, err)
|
2020-05-22 20:54:05 +01:00
|
|
|
require.False(t, alreadyInserted)
|
2021-06-17 16:05:04 +01:00
|
|
|
alreadyInserted, err = q.Insert(ctx, seg)
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-19 10:22:05 +01:00
|
|
|
func TestInsertBatchOfOne(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
q := db.RepairQueue()
|
|
|
|
|
|
|
|
writeSegments := []*queue.InjuredSegment{
|
2022-04-19 10:22:05 +01:00
|
|
|
createInjuredSegment(),
|
2022-04-19 10:22:05 +01:00
|
|
|
}
|
|
|
|
newlyInserted, err := q.InsertBatch(ctx, writeSegments)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, newlyInserted, 1)
|
|
|
|
|
|
|
|
writeSegments[0].SegmentHealth = 5
|
|
|
|
newlyInserted, err = q.InsertBatch(ctx, writeSegments)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, newlyInserted, 0)
|
|
|
|
|
|
|
|
readSegments, err := q.SelectN(ctx, 1000)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, readSegments, 1)
|
|
|
|
require.Equal(t, writeSegments[0].StreamID, readSegments[0].StreamID)
|
|
|
|
require.Equal(t, writeSegments[0].Position, readSegments[0].Position)
|
|
|
|
require.Equal(t, writeSegments[0].SegmentHealth, readSegments[0].SegmentHealth)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInsertOverlappingBatches(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
q := db.RepairQueue()
|
|
|
|
|
|
|
|
requireDbState := func(expectedSegments []queue.InjuredSegment) {
|
|
|
|
sort := func(segments []queue.InjuredSegment) {
|
|
|
|
sort.Slice(segments, func(i, j int) bool {
|
|
|
|
return segments[i].StreamID.Less(segments[j].StreamID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
dbSegments, err := q.SelectN(ctx, 1000)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sort(dbSegments)
|
|
|
|
sort(expectedSegments)
|
|
|
|
|
|
|
|
require.Equal(t, len(expectedSegments), len(dbSegments))
|
|
|
|
|
|
|
|
for i := range expectedSegments {
|
|
|
|
require.Equal(t, expectedSegments[i].StreamID, dbSegments[i].StreamID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 10:22:05 +01:00
|
|
|
writeSegment1 := createInjuredSegment()
|
|
|
|
writeSegment2 := createInjuredSegment()
|
|
|
|
writeSegment3 := createInjuredSegment()
|
2022-04-19 10:22:05 +01:00
|
|
|
|
2022-04-19 10:22:05 +01:00
|
|
|
newlyInserted, err := q.InsertBatch(ctx, []*queue.InjuredSegment{writeSegment1, writeSegment2})
|
2022-04-19 10:22:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, newlyInserted, 2)
|
2022-04-19 10:22:05 +01:00
|
|
|
require.Equal(t, newlyInserted[0], writeSegment1)
|
|
|
|
require.Equal(t, newlyInserted[1], writeSegment2)
|
|
|
|
requireDbState([]queue.InjuredSegment{*writeSegment1, *writeSegment2})
|
2022-04-19 10:22:05 +01:00
|
|
|
|
2022-04-19 10:22:05 +01:00
|
|
|
newlyInserted, err = q.InsertBatch(ctx, []*queue.InjuredSegment{writeSegment2, writeSegment3})
|
2022-04-19 10:22:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, newlyInserted, 1)
|
2022-04-19 10:22:05 +01:00
|
|
|
require.Equal(t, newlyInserted[0], writeSegment3)
|
|
|
|
requireDbState([]queue.InjuredSegment{*writeSegment1, *writeSegment2, *writeSegment3})
|
2022-04-19 10:22:05 +01:00
|
|
|
|
2022-04-19 10:22:05 +01:00
|
|
|
newlyInserted, err = q.InsertBatch(ctx, []*queue.InjuredSegment{writeSegment1, writeSegment3})
|
2022-04-19 10:22:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, newlyInserted, 0)
|
2022-04-19 10:22:05 +01:00
|
|
|
requireDbState([]queue.InjuredSegment{*writeSegment1, *writeSegment2, *writeSegment3})
|
2022-04-19 10:22:05 +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)
|
2023-04-06 14:44:54 +01:00
|
|
|
require.True(t, queue.ErrEmpty.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
|
2021-06-17 16:05:04 +01:00
|
|
|
var addSegs []*queue.InjuredSegment
|
2018-12-21 15:11:19 +00:00
|
|
|
for i := 0; i < N; i++ {
|
2021-06-17 16:05:04 +01:00
|
|
|
seg := &queue.InjuredSegment{
|
|
|
|
StreamID: uuid.UUID{byte(i)},
|
|
|
|
SegmentHealth: 6,
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
2021-06-17 16:05:04 +01:00
|
|
|
alreadyInserted, err := q.Insert(ctx, seg)
|
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
|
|
|
|
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)
|
2021-06-17 16:05:04 +01:00
|
|
|
|
|
|
|
require.Equal(t, addSegs[i].StreamID, s.StreamID)
|
|
|
|
require.Equal(t, addSegs[i].Position, s.Position)
|
|
|
|
require.Equal(t, addSegs[i].SegmentHealth, s.SegmentHealth)
|
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
|
2021-06-17 16:05:04 +01:00
|
|
|
entries := make(chan *queue.InjuredSegment, N)
|
|
|
|
|
|
|
|
expectedSegments := make([]*queue.InjuredSegment, N)
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
expectedSegments[i] = &queue.InjuredSegment{
|
|
|
|
StreamID: testrand.UUID(),
|
|
|
|
SegmentHealth: float64(i),
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2021-06-17 16:05:04 +01:00
|
|
|
alreadyInserted, err := q.Insert(ctx, expectedSegments[i])
|
|
|
|
require.False(t, alreadyInserted)
|
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
|
|
|
|
2021-06-17 16:05:04 +01:00
|
|
|
var items []*queue.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 {
|
2021-06-17 16:05:04 +01:00
|
|
|
return items[i].SegmentHealth < items[k].SegmentHealth
|
2019-04-16 19:14:09 +01:00
|
|
|
})
|
|
|
|
|
2018-10-08 20:37:27 +01:00
|
|
|
// check if the enqueued and dequeued elements match
|
|
|
|
for i := 0; i < N; i++ {
|
2021-06-17 16:05:04 +01:00
|
|
|
require.Equal(t, expectedSegments[i].StreamID, items[i].StreamID)
|
|
|
|
require.Equal(t, expectedSegments[i].Position, items[i].Position)
|
|
|
|
require.Equal(t, expectedSegments[i].SegmentHealth, items[i].SegmentHealth)
|
2018-10-08 20:37:27 +01:00
|
|
|
}
|
2021-06-17 16:05:04 +01:00
|
|
|
|
|
|
|
count, err := q.Count(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Zero(t, count)
|
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()
|
|
|
|
|
2021-06-17 16:05:04 +01:00
|
|
|
seg1 := &queue.InjuredSegment{
|
|
|
|
StreamID: testrand.UUID(),
|
2020-09-09 21:52:22 +01:00
|
|
|
}
|
2021-06-17 16:05:04 +01:00
|
|
|
seg2 := &queue.InjuredSegment{
|
|
|
|
StreamID: testrand.UUID(),
|
2020-09-09 21:52:22 +01:00
|
|
|
}
|
2021-06-17 16:05:04 +01:00
|
|
|
seg3 := &queue.InjuredSegment{
|
|
|
|
StreamID: testrand.UUID(),
|
2020-09-09 21:52:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
timeBeforeInsert1 := time.Now()
|
|
|
|
|
2020-10-21 23:02:54 +01:00
|
|
|
segmentHealth := 1.3
|
2021-06-17 16:05:04 +01:00
|
|
|
_, err := q.Insert(ctx, seg1)
|
2020-09-09 21:52:22 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-17 16:05:04 +01:00
|
|
|
_, err = q.Insert(ctx, seg2)
|
2020-09-09 21:52:22 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-17 16:05:04 +01:00
|
|
|
_, err = q.Insert(ctx, seg3)
|
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
|
2021-06-17 16:05:04 +01:00
|
|
|
_, err = q.Insert(ctx, seg2)
|
2020-09-09 21:52:22 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// seg3 has a lower health
|
2021-06-17 16:05:04 +01:00
|
|
|
seg3.SegmentHealth = segmentHealth - 0.1
|
|
|
|
_, err = q.Insert(ctx, seg3)
|
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)
|
|
|
|
})
|
|
|
|
}
|