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
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-12-27 09:56:25 +00:00
|
|
|
func (r *repairQueue) Enqueue(ctx context.Context, seg *pb.InjuredSegment) error {
|
2018-12-21 15:11:19 +00:00
|
|
|
val, err := proto.Marshal(seg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = r.db.Create_Injuredsegment(
|
2018-12-27 09:56:25 +00:00
|
|
|
ctx,
|
2018-12-21 15:11:19 +00:00
|
|
|
dbx.Injuredsegment_Info(val),
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-14 21:12:47 +00:00
|
|
|
func (r *repairQueue) postgresDequeue(ctx context.Context) (seg pb.InjuredSegment, err error) {
|
|
|
|
err = r.db.DB.QueryRowContext(ctx, `
|
|
|
|
DELETE FROM injuredsegments
|
|
|
|
WHERE id = ( SELECT id FROM injuredsegments ORDER BY id FOR UPDATE SKIP LOCKED LIMIT 1 )
|
|
|
|
RETURNING info
|
|
|
|
`).Scan(&seg)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = storage.ErrEmptyQueue.New("")
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
2019-03-14 21:12:47 +00:00
|
|
|
return seg, err
|
|
|
|
}
|
2018-12-21 15:11:19 +00:00
|
|
|
|
2019-03-14 21:12:47 +00:00
|
|
|
func (r *repairQueue) sqliteDequeue(ctx context.Context) (seg pb.InjuredSegment, err error) {
|
|
|
|
err = r.db.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) error {
|
|
|
|
var id int64
|
|
|
|
err = tx.Tx.QueryRowContext(ctx, `SELECT id, info FROM injuredsegments ORDER BY id LIMIT 1`).Scan(&id, &seg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res, err := tx.Tx.ExecContext(ctx, r.db.Rebind(`DELETE FROM injuredsegments WHERE id = ?`), id)
|
|
|
|
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-03-14 21:12:47 +00:00
|
|
|
func (r *repairQueue) Dequeue(ctx context.Context) (seg pb.InjuredSegment, err error) {
|
|
|
|
switch t := r.db.DB.Driver().(type) {
|
|
|
|
case *sqlite3.SQLiteDriver:
|
|
|
|
return r.sqliteDequeue(ctx)
|
|
|
|
case *pq.Driver:
|
|
|
|
return r.postgresDequeue(ctx)
|
|
|
|
default:
|
|
|
|
return seg, fmt.Errorf("Unsupported database %t", t)
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:56:25 +00:00
|
|
|
func (r *repairQueue) Peekqueue(ctx context.Context, limit int) ([]pb.InjuredSegment, error) {
|
2018-12-21 15:11:19 +00:00
|
|
|
if limit <= 0 || limit > storage.LookupLimit {
|
|
|
|
limit = storage.LookupLimit
|
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
rows, err := r.db.Limited_Injuredsegment(ctx, limit, 0)
|
2018-12-21 15:11:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
segments := make([]pb.InjuredSegment, 0)
|
|
|
|
for _, entry := range rows {
|
|
|
|
seg := &pb.InjuredSegment{}
|
|
|
|
if err = proto.Unmarshal(entry.Info, seg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
segments = append(segments, *seg)
|
|
|
|
}
|
|
|
|
return segments, nil
|
|
|
|
}
|