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"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/pb"
|
2018-12-04 16:26:30 +00:00
|
|
|
"storj.io/storj/pkg/utils"
|
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 {
|
2018-12-04 16:26:30 +00:00
|
|
|
db *dbx.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
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
|
2018-12-17 18:54:46 +00:00
|
|
|
_, err = tx.Create_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 {
|
2018-12-12 13:15:34 +00:00
|
|
|
return Error.Wrap(utils.CombineErrors(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)
|
2018-12-17 18:54:46 +00:00
|
|
|
_, err = tx.Update_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 {
|
2018-12-12 13:15:34 +00:00
|
|
|
return Error.Wrap(utils.CombineErrors(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) {
|
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-03-15 20:21:52 +00:00
|
|
|
// Getlimited number of irreparable segments by offset
|
|
|
|
func (db *irreparableDB) GetLimited(ctx context.Context, limit int, offset int64) (resp []*pb.IrreparableSegment, err error) {
|
|
|
|
rows, err := db.db.Limited_Irreparabledb_OrderBy_Asc_Segmentpath(ctx, limit, offset)
|
|
|
|
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) {
|
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
|
|
|
}
|