satellite/repair: avoid TestDBAccess

Change-Id: I34adb58cd67fba5917032f2f328d75b1c4afdbbf
This commit is contained in:
Egon Elbre 2020-11-30 13:03:40 +02:00
parent 0771cdb0b1
commit 28ea63be92
3 changed files with 28 additions and 43 deletions

View File

@ -8,7 +8,6 @@ import (
"time" "time"
"storj.io/storj/satellite/internalpb" "storj.io/storj/satellite/internalpb"
"storj.io/storj/satellite/satellitedb/dbx"
) )
// RepairQueue implements queueing for segments that need repairing. // RepairQueue implements queueing for segments that need repairing.
@ -29,7 +28,6 @@ type RepairQueue interface {
// Count counts the number of segments in the repair queue. // Count counts the number of segments in the repair queue.
Count(ctx context.Context) (count int, err error) Count(ctx context.Context) (count int, err error)
// TestDBAccess returns raw RepairQueue database access for test purposes. // TestingSetAttemptedTime sets attempted time for a repairpath.
// TODO: remove dependency. TestingSetAttemptedTime(ctx context.Context, repairpath []byte, t time.Time) (rowsAffected int64, err error)
TestDBAccess() *dbx.DB
} }

View File

@ -4,7 +4,6 @@
package queue_test package queue_test
import ( import (
"context"
"math/rand" "math/rand"
"strconv" "strconv"
"testing" "testing"
@ -16,7 +15,6 @@ import (
"storj.io/common/testcontext" "storj.io/common/testcontext"
"storj.io/storj/satellite" "storj.io/storj/satellite"
"storj.io/storj/satellite/internalpb" "storj.io/storj/satellite/internalpb"
"storj.io/storj/satellite/satellitedb/dbx"
"storj.io/storj/satellite/satellitedb/satellitedbtest" "storj.io/storj/satellite/satellitedb/satellitedbtest"
"storj.io/storj/storage" "storj.io/storj/storage"
) )
@ -68,32 +66,19 @@ func TestOrder(t *testing.T) {
require.False(t, alreadyInserted) require.False(t, alreadyInserted)
} }
// TODO: remove dependency on *dbx.DB updateList := []struct {
dbAccess := db.RepairQueue().TestDBAccess() path []byte
attempted time.Time
err := dbAccess.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) error { }{
updateList := []struct { {recentRepairPath, time.Now()},
path []byte {oldRepairPath, time.Now().Add(-7 * time.Hour)},
attempted time.Time {olderRepairPath, time.Now().Add(-8 * time.Hour)},
}{ }
{recentRepairPath, time.Now()}, for _, item := range updateList {
{oldRepairPath, time.Now().Add(-7 * time.Hour)}, rowsAffected, err := db.RepairQueue().TestingSetAttemptedTime(ctx, item.path, item.attempted)
{olderRepairPath, time.Now().Add(-8 * time.Hour)}, require.NoError(t, err)
} require.EqualValues(t, 1, rowsAffected)
for _, item := range updateList { }
res, err := tx.Tx.ExecContext(ctx, dbAccess.Rebind(`UPDATE injuredsegments SET attempted = ? WHERE path = ?`), item.attempted, item.path)
if err != nil {
return err
}
count, err := res.RowsAffected()
if err != nil {
return err
}
require.EqualValues(t, 1, count)
}
return nil
})
require.NoError(t, err)
// path with attempted = null should be selected first // path with attempted = null should be selected first
injuredSeg, err := repairQueue.Select(ctx) injuredSeg, err := repairQueue.Select(ctx)
@ -132,9 +117,6 @@ func TestOrderHealthyPieces(t *testing.T) {
// ("path/g", 10, null) // ("path/g", 10, null)
// ("path/h", 10, now-8h) // ("path/h", 10, now-8h)
// TODO: remove dependency on *dbx.DB
dbAccess := db.RepairQueue().TestDBAccess()
// insert the 8 segments according to the plan above // insert the 8 segments according to the plan above
injuredSegList := []struct { injuredSegList := []struct {
path []byte path []byte
@ -164,11 +146,9 @@ func TestOrderHealthyPieces(t *testing.T) {
// next, if applicable, update the "attempted at" timestamp // next, if applicable, update the "attempted at" timestamp
if !item.attempted.IsZero() { if !item.attempted.IsZero() {
res, err := dbAccess.ExecContext(ctx, dbAccess.Rebind(`UPDATE injuredsegments SET attempted = ? WHERE path = ?`), item.attempted, item.path) rowsAffected, err := db.RepairQueue().TestingSetAttemptedTime(ctx, item.path, item.attempted)
require.NoError(t, err) require.NoError(t, err)
count, err := res.RowsAffected() require.EqualValues(t, 1, rowsAffected)
require.NoError(t, err)
require.EqualValues(t, 1, count)
} }
} }

View File

@ -24,10 +24,6 @@ type repairQueue struct {
db *satelliteDB db *satelliteDB
} }
func (r *repairQueue) TestDBAccess() *dbx.DB {
return r.db.DB
}
func (r *repairQueue) Insert(ctx context.Context, seg *internalpb.InjuredSegment, segmentHealth float64) (alreadyInserted bool, err error) { func (r *repairQueue) Insert(ctx context.Context, seg *internalpb.InjuredSegment, segmentHealth float64) (alreadyInserted bool, err error) {
defer mon.Task()(&ctx)(&err) defer mon.Task()(&ctx)(&err)
// insert if not exists, or update healthy count if does exist // insert if not exists, or update healthy count if does exist
@ -151,3 +147,14 @@ func (r *repairQueue) Count(ctx context.Context) (count int, err error) {
return count, Error.Wrap(err) return count, Error.Wrap(err)
} }
// TestingSetAttemptedTime sets attempted time for a repairpath.
func (r *repairQueue) TestingSetAttemptedTime(ctx context.Context, repairpath []byte, t time.Time) (rowsAffected int64, err error) {
defer mon.Task()(&ctx)(&err)
res, err := r.db.ExecContext(ctx, r.db.Rebind(`UPDATE injuredsegments SET attempted = ? WHERE path = ?`), t, repairpath)
if err != nil {
return 0, Error.Wrap(err)
}
count, err := res.RowsAffected()
return count, Error.Wrap(err)
}