2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-04 16:26:30 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-12-10 19:08:45 +00:00
|
|
|
package satellitedb
|
2018-12-04 16:26:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-03-15 20:21:52 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2019-03-29 12:30:23 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-03-15 20:21:52 +00:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/pb"
|
2018-12-10 19:08:45 +00:00
|
|
|
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
2018-12-04 16:26:30 +00:00
|
|
|
)
|
|
|
|
|
2018-12-10 19:08:45 +00:00
|
|
|
type irreparableDB struct {
|
2019-12-14 02:29:54 +00:00
|
|
|
db *satelliteDB
|
2018-12-04 16:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IncrementRepairAttempts a db entry for to increment the repair attempts field
|
2019-03-15 20:21:52 +00:00
|
|
|
func (db *irreparableDB) IncrementRepairAttempts(ctx context.Context, segmentInfo *pb.IrreparableSegment) (err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-17 18:54:46 +00:00
|
|
|
tx, err := db.db.Open(ctx)
|
2018-12-04 16:26:30 +00:00
|
|
|
if err != nil {
|
2018-12-12 13:15:34 +00:00
|
|
|
return Error.Wrap(err)
|
2018-12-04 16:26:30 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 20:21:52 +00:00
|
|
|
bytes, err := proto.Marshal(segmentInfo.SegmentDetail)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dbxInfo, err := tx.Get_Irreparabledb_By_Segmentpath(ctx, dbx.Irreparabledb_Segmentpath(segmentInfo.Path))
|
2018-12-04 16:26:30 +00:00
|
|
|
if err != nil {
|
|
|
|
// no rows err, so create/insert an entry
|
2019-09-12 18:31:50 +01:00
|
|
|
err = tx.CreateNoReturn_Irreparabledb(
|
2018-12-04 16:26:30 +00:00
|
|
|
ctx,
|
2019-03-15 20:21:52 +00:00
|
|
|
dbx.Irreparabledb_Segmentpath(segmentInfo.Path),
|
|
|
|
dbx.Irreparabledb_Segmentdetail(bytes),
|
|
|
|
dbx.Irreparabledb_PiecesLostCount(int64(segmentInfo.LostPieces)),
|
|
|
|
dbx.Irreparabledb_SegDamagedUnixSec(segmentInfo.LastRepairAttempt),
|
2018-12-04 16:26:30 +00:00
|
|
|
dbx.Irreparabledb_RepairAttemptCount(segmentInfo.RepairAttemptCount),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2019-03-29 12:30:23 +00:00
|
|
|
return Error.Wrap(errs.Combine(err, tx.Rollback()))
|
2018-12-04 16:26:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// row exits increment the attempt counter
|
|
|
|
dbxInfo.RepairAttemptCount++
|
|
|
|
updateFields := dbx.Irreparabledb_Update_Fields{}
|
|
|
|
updateFields.RepairAttemptCount = dbx.Irreparabledb_RepairAttemptCount(dbxInfo.RepairAttemptCount)
|
2019-03-15 20:21:52 +00:00
|
|
|
updateFields.SegDamagedUnixSec = dbx.Irreparabledb_SegDamagedUnixSec(segmentInfo.LastRepairAttempt)
|
2019-09-12 18:31:50 +01:00
|
|
|
err = tx.UpdateNoReturn_Irreparabledb_By_Segmentpath(
|
2018-12-04 16:26:30 +00:00
|
|
|
ctx,
|
2018-12-17 18:54:46 +00:00
|
|
|
dbx.Irreparabledb_Segmentpath(dbxInfo.Segmentpath),
|
2018-12-04 16:26:30 +00:00
|
|
|
updateFields,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2019-03-29 12:30:23 +00:00
|
|
|
return Error.Wrap(errs.Combine(err, tx.Rollback()))
|
2018-12-04 16:26:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:34 +00:00
|
|
|
return Error.Wrap(tx.Commit())
|
2018-12-04 16:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get a irreparable's segment info from the db
|
2019-03-15 20:21:52 +00:00
|
|
|
func (db *irreparableDB) Get(ctx context.Context, segmentPath []byte) (resp *pb.IrreparableSegment, err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-04 16:26:30 +00:00
|
|
|
dbxInfo, err := db.db.Get_Irreparabledb_By_Segmentpath(ctx, dbx.Irreparabledb_Segmentpath(segmentPath))
|
|
|
|
if err != nil {
|
2019-03-15 20:21:52 +00:00
|
|
|
return &pb.IrreparableSegment{}, Error.Wrap(err)
|
2018-12-04 16:26:30 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 20:21:52 +00:00
|
|
|
p := &pb.Pointer{}
|
|
|
|
|
|
|
|
err = proto.Unmarshal(dbxInfo.Segmentdetail, p)
|
|
|
|
if err != nil {
|
|
|
|
return &pb.IrreparableSegment{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.IrreparableSegment{
|
|
|
|
Path: dbxInfo.Segmentpath,
|
|
|
|
SegmentDetail: p,
|
|
|
|
LostPieces: int32(dbxInfo.PiecesLostCount),
|
|
|
|
LastRepairAttempt: dbxInfo.SegDamagedUnixSec,
|
|
|
|
RepairAttemptCount: dbxInfo.RepairAttemptCount,
|
2018-12-04 16:26:30 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 17:21:21 +01:00
|
|
|
// GetLimited returns a list of irreparable segment info starting after the last segment info we retrieved
|
|
|
|
func (db *irreparableDB) GetLimited(ctx context.Context, limit int, lastSeenSegmentPath []byte) (resp []*pb.IrreparableSegment, err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-07-18 17:21:21 +01:00
|
|
|
// the offset is hardcoded to 0 since we are using the lastSeenSegmentPath to
|
|
|
|
// indicate the item we last listed instead. In a perfect world this db query would
|
|
|
|
// not take an offset as an argument, but currently dbx only supports `limitoffset`
|
|
|
|
const offset = 0
|
|
|
|
rows, err := db.db.Limited_Irreparabledb_By_Segmentpath_Greater_OrderBy_Asc_Segmentpath(ctx,
|
|
|
|
dbx.Irreparabledb_Segmentpath(lastSeenSegmentPath),
|
|
|
|
limit, offset,
|
|
|
|
)
|
2019-03-15 20:21:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, row := range rows {
|
|
|
|
p := &pb.Pointer{}
|
|
|
|
err = proto.Unmarshal(row.Segmentdetail, p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
segment := &pb.IrreparableSegment{
|
|
|
|
Path: row.Segmentpath,
|
|
|
|
SegmentDetail: p,
|
|
|
|
LostPieces: int32(row.PiecesLostCount),
|
|
|
|
LastRepairAttempt: row.SegDamagedUnixSec,
|
|
|
|
RepairAttemptCount: row.RepairAttemptCount,
|
|
|
|
}
|
|
|
|
resp = append(resp, segment)
|
|
|
|
}
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:26:30 +00:00
|
|
|
// Delete a irreparable's segment info from the db
|
2018-12-10 19:08:45 +00:00
|
|
|
func (db *irreparableDB) Delete(ctx context.Context, segmentPath []byte) (err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-04 16:26:30 +00:00
|
|
|
_, err = db.db.Delete_Irreparabledb_By_Segmentpath(ctx, dbx.Irreparabledb_Segmentpath(segmentPath))
|
|
|
|
|
2018-12-12 13:15:34 +00:00
|
|
|
return Error.Wrap(err)
|
2018-12-04 16:26:30 +00:00
|
|
|
}
|