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"
|
|
|
|
"fmt"
|
2018-12-21 15:11:19 +00:00
|
|
|
|
2019-03-14 21:12:47 +00:00
|
|
|
"github.com/lib/pq"
|
|
|
|
sqlite3 "github.com/mattn/go-sqlite3"
|
2018-12-27 09:56:25 +00:00
|
|
|
|
2019-04-23 12:13:57 +01:00
|
|
|
"storj.io/storj/internal/dbutil/pgutil"
|
|
|
|
"storj.io/storj/internal/dbutil/sqliteutil"
|
2018-12-21 15:11:19 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
type repairQueue struct {
|
2018-12-27 09:56:25 +00:00
|
|
|
db *dbx.DB
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
func (r *repairQueue) Insert(ctx context.Context, seg *pb.InjuredSegment) error {
|
|
|
|
_, 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 {
|
|
|
|
if pgutil.IsConstraintError(err) || sqliteutil.IsConstraintError(err) {
|
|
|
|
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-04-16 19:14:09 +01:00
|
|
|
func (r *repairQueue) postgresSelect(ctx context.Context) (seg *pb.InjuredSegment, err error) {
|
2019-05-21 17:50:55 +01:00
|
|
|
err = r.db.QueryRowContext(ctx, `
|
|
|
|
UPDATE injuredsegments SET attempted = timezone('utc', now()) WHERE path = (
|
2019-04-16 19:14:09 +01:00
|
|
|
SELECT path FROM injuredsegments
|
2019-05-21 17:50:55 +01:00
|
|
|
WHERE attempted IS NULL OR attempted < timezone('utc', now()) - interval '1 hour'
|
2019-04-16 19:14:09 +01:00
|
|
|
ORDER BY path FOR UPDATE SKIP LOCKED LIMIT 1
|
2019-05-21 17:50:55 +01:00
|
|
|
) RETURNING data`).Scan(&seg)
|
2019-03-14 21:12:47 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = storage.ErrEmptyQueue.New("")
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
2019-04-16 19:14:09 +01:00
|
|
|
return
|
2019-03-14 21:12:47 +00:00
|
|
|
}
|
2018-12-21 15:11:19 +00:00
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
func (r *repairQueue) sqliteSelect(ctx context.Context) (seg *pb.InjuredSegment, err error) {
|
2019-03-14 21:12:47 +00:00
|
|
|
err = r.db.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) error {
|
2019-04-16 19:14:09 +01:00
|
|
|
var path string
|
|
|
|
err = tx.Tx.QueryRowContext(ctx, r.db.Rebind(`
|
|
|
|
SELECT path, data FROM injuredsegments
|
|
|
|
WHERE attempted IS NULL
|
|
|
|
OR attempted < datetime('now','-1 hours')
|
|
|
|
ORDER BY path LIMIT 1`)).Scan(&path, &seg)
|
2019-03-14 21:12:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-21 17:50:55 +01:00
|
|
|
res, err := tx.Tx.ExecContext(ctx, r.db.Rebind(`UPDATE injuredsegments SET attempted = datetime('now') WHERE path = ?`), path)
|
2019-03-14 21:12:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
count, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if count != 1 {
|
|
|
|
return fmt.Errorf("Expected 1, got %d segments deleted", count)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
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) Select(ctx context.Context) (seg *pb.InjuredSegment, err error) {
|
2019-03-14 21:12:47 +00:00
|
|
|
switch t := r.db.DB.Driver().(type) {
|
|
|
|
case *sqlite3.SQLiteDriver:
|
2019-04-16 19:14:09 +01:00
|
|
|
return r.sqliteSelect(ctx)
|
2019-03-14 21:12:47 +00:00
|
|
|
case *pq.Driver:
|
2019-04-16 19:14:09 +01:00
|
|
|
return r.postgresSelect(ctx)
|
2019-03-14 21:12:47 +00:00
|
|
|
default:
|
|
|
|
return seg, fmt.Errorf("Unsupported database %t", t)
|
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) {
|
|
|
|
_, err = r.db.ExecContext(ctx, r.db.Rebind(`DELETE FROM injuredsegments WHERE path = ?`), seg.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *repairQueue) SelectN(ctx context.Context, limit int) (segs []pb.InjuredSegment, err error) {
|
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)
|
|
|
|
for rows.Next() {
|
|
|
|
var seg pb.InjuredSegment
|
|
|
|
err = rows.Scan(&seg)
|
|
|
|
if err != nil {
|
|
|
|
return
|
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-04-16 19:14:09 +01:00
|
|
|
return segs, rows.Err()
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|