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"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2018-12-27 09:56:25 +00:00
|
|
|
|
2018-12-21 15:11:19 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/utils"
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:56:25 +00:00
|
|
|
func (r *repairQueue) Dequeue(ctx context.Context) (pb.InjuredSegment, error) {
|
|
|
|
// TODO: fix out of order issue
|
|
|
|
tx, err := r.db.Open(ctx)
|
2018-12-21 15:11:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return pb.InjuredSegment{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:56:25 +00:00
|
|
|
res, err := tx.First_Injuredsegment(ctx)
|
2018-12-21 15:11:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return pb.InjuredSegment{}, Error.Wrap(utils.CombineErrors(err, tx.Rollback()))
|
2018-12-27 09:56:25 +00:00
|
|
|
} else if res == nil {
|
2019-01-02 16:00:32 +00:00
|
|
|
return pb.InjuredSegment{}, Error.Wrap(utils.CombineErrors(storage.ErrEmptyQueue.New(""), tx.Rollback()))
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:56:25 +00:00
|
|
|
deleted, err := tx.Delete_Injuredsegment_By_Id(
|
|
|
|
ctx,
|
|
|
|
dbx.Injuredsegment_Id(res.Id),
|
2018-12-21 15:11:19 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return pb.InjuredSegment{}, Error.Wrap(utils.CombineErrors(err, tx.Rollback()))
|
2018-12-27 09:56:25 +00:00
|
|
|
} else if !deleted {
|
2018-12-21 15:11:19 +00:00
|
|
|
return pb.InjuredSegment{}, Error.Wrap(utils.CombineErrors(Error.New("Injured segment not deleted"), tx.Rollback()))
|
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return pb.InjuredSegment{}, Error.Wrap(err)
|
|
|
|
}
|
2018-12-21 15:11:19 +00:00
|
|
|
|
|
|
|
seg := &pb.InjuredSegment{}
|
2018-12-27 09:56:25 +00:00
|
|
|
if err = proto.Unmarshal(res.Info, seg); err != nil {
|
|
|
|
return pb.InjuredSegment{}, Error.Wrap(err)
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
return *seg, nil
|
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
|
|
|
|
}
|