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"
|
2020-07-14 14:04:38 +01:00
|
|
|
"errors"
|
2019-12-03 05:21:46 +00:00
|
|
|
|
2019-12-05 03:34:44 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-12-27 09:56:25 +00:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/pb"
|
2019-12-03 05:21:46 +00:00
|
|
|
"storj.io/storj/private/dbutil"
|
2018-12-21 15:11:19 +00:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
2020-01-22 19:00:46 +00:00
|
|
|
// RepairQueueSelectLimit defines how many items can be selected at the same time.
|
|
|
|
const RepairQueueSelectLimit = 1000
|
|
|
|
|
2018-12-21 15:11:19 +00:00
|
|
|
type repairQueue struct {
|
2019-12-14 02:29:54 +00:00
|
|
|
db *satelliteDB
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 20:54:05 +01:00
|
|
|
func (r *repairQueue) Insert(ctx context.Context, seg *pb.InjuredSegment, numHealthy int) (alreadyInserted bool, err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-02-21 21:32:05 +00:00
|
|
|
// insert if not exists, or update healthy count if does exist
|
2020-05-22 20:54:05 +01:00
|
|
|
var query string
|
|
|
|
|
|
|
|
// we want to insert the segment if it is not in the queue, but update the number of healthy pieces if it already is in the queue
|
|
|
|
// we also want to know if the result was an insert or an update - this is the reasoning for the xmax section of the postgres query
|
|
|
|
// and the separate cockroach query (which the xmax trick does not work for)
|
|
|
|
switch r.db.implementation {
|
|
|
|
case dbutil.Postgres:
|
|
|
|
query = `
|
|
|
|
INSERT INTO injuredsegments
|
|
|
|
(
|
|
|
|
path, data, num_healthy_pieces
|
|
|
|
)
|
|
|
|
VALUES (
|
|
|
|
$1, $2, $3
|
|
|
|
)
|
|
|
|
ON CONFLICT (path)
|
|
|
|
DO UPDATE
|
|
|
|
SET num_healthy_pieces=$3
|
|
|
|
RETURNING (xmax != 0) AS alreadyInserted
|
2020-02-21 21:32:05 +00:00
|
|
|
`
|
2020-05-22 20:54:05 +01:00
|
|
|
case dbutil.Cockroach:
|
|
|
|
query = `
|
|
|
|
WITH updater AS (
|
|
|
|
UPDATE injuredsegments SET num_healthy_pieces = $3 WHERE path = $1
|
|
|
|
RETURNING *
|
|
|
|
)
|
|
|
|
INSERT INTO injuredsegments (path, data, num_healthy_pieces)
|
|
|
|
SELECT $1, $2, $3
|
|
|
|
WHERE NOT EXISTS (SELECT * FROM updater)
|
|
|
|
RETURNING false
|
|
|
|
`
|
|
|
|
}
|
|
|
|
rows, err := r.db.QueryContext(ctx, query, seg.Path, seg, numHealthy)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-06-03 21:31:21 +01:00
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
2020-05-22 20:54:05 +01:00
|
|
|
|
|
|
|
if !rows.Next() {
|
|
|
|
// cockroach query does not return anything if the segment is already in the queue
|
|
|
|
alreadyInserted = true
|
|
|
|
} else {
|
|
|
|
err = rows.Scan(&alreadyInserted)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 21:31:21 +01:00
|
|
|
return alreadyInserted, rows.Err()
|
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-19 09:20:52 +00:00
|
|
|
err = r.db.QueryRowContext(ctx, `
|
2020-02-11 15:33:34 +00:00
|
|
|
UPDATE injuredsegments SET attempted = now() WHERE path = (
|
2019-12-19 09:20:52 +00:00
|
|
|
SELECT path FROM injuredsegments
|
2020-02-11 15:33:34 +00:00
|
|
|
WHERE attempted IS NULL OR attempted < now() - interval '6 hours'
|
2020-02-21 21:32:05 +00:00
|
|
|
ORDER BY num_healthy_pieces ASC, attempted LIMIT 1
|
2019-12-19 09:20:52 +00:00
|
|
|
) RETURNING data`).Scan(&seg)
|
2019-12-03 05:21:46 +00:00
|
|
|
case dbutil.Postgres:
|
|
|
|
err = r.db.QueryRowContext(ctx, `
|
2020-02-11 15:33:34 +00:00
|
|
|
UPDATE injuredsegments SET attempted = now() WHERE path = (
|
2019-12-03 05:21:46 +00:00
|
|
|
SELECT path FROM injuredsegments
|
2020-02-11 15:33:34 +00:00
|
|
|
WHERE attempted IS NULL OR attempted < now() - interval '6 hours'
|
2020-02-21 21:32:05 +00:00
|
|
|
ORDER BY num_healthy_pieces ASC, attempted NULLS FIRST FOR UPDATE SKIP LOCKED LIMIT 1
|
2019-12-03 05:21:46 +00:00
|
|
|
) 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
|
|
|
}
|
2020-07-14 14:04:38 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2019-03-14 21:12:47 +00:00
|
|
|
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)
|
2020-01-22 19:00:46 +00:00
|
|
|
if limit <= 0 || limit > RepairQueueSelectLimit {
|
|
|
|
limit = RepairQueueSelectLimit
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
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)
|
|
|
|
}
|
2020-01-16 14:27:24 +00:00
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
|
|
|
|
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
|
|
|
}
|
2020-01-16 14:27:24 +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)
|
|
|
|
}
|