2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-21 15:11:19 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-14 21:12:47 +00:00
|
|
|
"database/sql"
|
2019-12-03 05:21:46 +00:00
|
|
|
|
|
|
|
"github.com/cockroachdb/cockroach-go/crdb"
|
2019-12-05 03:34:44 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-12-27 09:56:25 +00:00
|
|
|
|
2018-12-21 15:11:19 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-12-03 05:21:46 +00:00
|
|
|
"storj.io/storj/private/dbutil"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/dbutil/pgutil"
|
2018-12-21 15:11:19 +00:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
type repairQueue struct {
|
2019-12-14 02:29:54 +00:00
|
|
|
db *satelliteDB
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
func (r *repairQueue) Insert(ctx context.Context, seg *pb.InjuredSegment) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
_, err = r.db.ExecContext(ctx, r.db.Rebind(`INSERT INTO injuredsegments ( path, data ) VALUES ( ?, ? )`), seg.Path, seg)
|
2019-04-23 12:13:57 +01:00
|
|
|
if err != nil {
|
2019-10-18 22:27:57 +01:00
|
|
|
if pgutil.IsConstraintError(err) {
|
2019-04-23 12:13:57 +01:00
|
|
|
return nil // quietly fail on reinsert
|
|
|
|
}
|
|
|
|
return err
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
2019-04-23 12:13:57 +01:00
|
|
|
return nil
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 22:27:57 +01:00
|
|
|
func (r *repairQueue) Select(ctx context.Context) (seg *pb.InjuredSegment, err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-12-14 02:29:54 +00:00
|
|
|
switch r.db.implementation {
|
2019-12-03 05:21:46 +00:00
|
|
|
case dbutil.Cockroach:
|
2019-12-14 02:29:54 +00:00
|
|
|
err = crdb.ExecuteTx(ctx, r.db.DB.DB, nil, func(tx *sql.Tx) error {
|
2019-12-03 05:21:46 +00:00
|
|
|
return tx.QueryRowContext(ctx, `
|
|
|
|
UPDATE injuredsegments SET attempted = now() AT TIME ZONE 'UTC' WHERE path = (
|
|
|
|
SELECT path FROM injuredsegments
|
|
|
|
WHERE attempted IS NULL OR attempted < now() AT TIME ZONE 'UTC' - interval '1 hour'
|
|
|
|
ORDER BY attempted LIMIT 1
|
|
|
|
) RETURNING data`).Scan(&seg)
|
|
|
|
})
|
|
|
|
case dbutil.Postgres:
|
|
|
|
err = r.db.QueryRowContext(ctx, `
|
|
|
|
UPDATE injuredsegments SET attempted = now() AT TIME ZONE 'UTC' WHERE path = (
|
|
|
|
SELECT path FROM injuredsegments
|
|
|
|
WHERE attempted IS NULL OR attempted < now() AT TIME ZONE 'UTC' - interval '1 hour'
|
|
|
|
ORDER BY attempted NULLS FIRST FOR UPDATE SKIP LOCKED LIMIT 1
|
|
|
|
) RETURNING data`).Scan(&seg)
|
|
|
|
default:
|
2019-12-14 02:29:54 +00:00
|
|
|
return seg, errs.New("invalid dbType: %v", r.db.implementation)
|
2019-12-03 05:21:46 +00:00
|
|
|
}
|
2019-03-14 21:12:47 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = storage.ErrEmptyQueue.New("")
|
2018-12-27 09:56:25 +00:00
|
|
|
}
|
2019-03-14 21:12:47 +00:00
|
|
|
return seg, err
|
|
|
|
}
|
2018-12-21 15:11:19 +00:00
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
func (r *repairQueue) Delete(ctx context.Context, seg *pb.InjuredSegment) (err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-04-16 19:14:09 +01:00
|
|
|
_, err = r.db.ExecContext(ctx, r.db.Rebind(`DELETE FROM injuredsegments WHERE path = ?`), seg.Path)
|
2019-07-23 15:28:06 +01:00
|
|
|
return Error.Wrap(err)
|
2019-04-16 19:14:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *repairQueue) SelectN(ctx context.Context, limit int) (segs []pb.InjuredSegment, err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-21 15:11:19 +00:00
|
|
|
if limit <= 0 || limit > storage.LookupLimit {
|
|
|
|
limit = storage.LookupLimit
|
|
|
|
}
|
2019-04-16 19:14:09 +01:00
|
|
|
//todo: strictly enforce order-by or change tests
|
|
|
|
rows, err := r.db.QueryContext(ctx, r.db.Rebind(`SELECT data FROM injuredsegments LIMIT ?`), limit)
|
2019-07-25 16:01:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
2019-04-16 19:14:09 +01:00
|
|
|
for rows.Next() {
|
|
|
|
var seg pb.InjuredSegment
|
|
|
|
err = rows.Scan(&seg)
|
|
|
|
if err != nil {
|
2019-07-25 16:01:44 +01:00
|
|
|
return segs, Error.Wrap(err)
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
2019-04-16 19:14:09 +01:00
|
|
|
segs = append(segs, seg)
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
2019-07-25 16:01:44 +01:00
|
|
|
return segs, Error.Wrap(rows.Err())
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
2019-07-30 16:21:40 +01:00
|
|
|
|
|
|
|
func (r *repairQueue) Count(ctx context.Context) (count int, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
// Count every segment regardless of how recently repair was last attempted
|
|
|
|
err = r.db.QueryRowContext(ctx, r.db.Rebind(`SELECT COUNT(*) as count FROM injuredsegments`)).Scan(&count)
|
|
|
|
|
|
|
|
return count, Error.Wrap(err)
|
|
|
|
}
|