2018-07-23 21:05:02 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
package segments
|
2018-07-23 21:05:02 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2018-12-11 16:05:14 +00:00
|
|
|
"math/rand"
|
2018-07-23 21:05:02 +01:00
|
|
|
"time"
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
2018-07-30 19:57:50 +01:00
|
|
|
"github.com/golang/protobuf/ptypes/timestamp"
|
2018-08-14 16:15:22 +01:00
|
|
|
"github.com/vivint/infectious"
|
2018-07-30 19:57:50 +01:00
|
|
|
"go.uber.org/zap"
|
2018-07-23 21:05:02 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2018-07-23 21:05:02 +01:00
|
|
|
"storj.io/storj/pkg/eestream"
|
|
|
|
"storj.io/storj/pkg/overlay"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-06 17:49:17 +00:00
|
|
|
"storj.io/storj/pkg/piecestore/psclient"
|
2018-08-22 16:07:00 +01:00
|
|
|
"storj.io/storj/pkg/pointerdb/pdbclient"
|
2018-07-23 21:05:02 +01:00
|
|
|
"storj.io/storj/pkg/ranger"
|
2018-10-30 18:06:12 +00:00
|
|
|
ecclient "storj.io/storj/pkg/storage/ec"
|
2018-10-25 21:28:16 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-10-30 18:06:12 +00:00
|
|
|
"storj.io/storj/pkg/utils"
|
2018-07-23 21:05:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
|
|
|
|
2018-07-30 19:57:50 +01:00
|
|
|
// Meta info about a segment
|
2018-07-23 21:05:02 +01:00
|
|
|
type Meta struct {
|
2018-07-30 19:57:50 +01:00
|
|
|
Modified time.Time
|
|
|
|
Expiration time.Time
|
|
|
|
Size int64
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListItem is a single item in a listing
|
|
|
|
type ListItem struct {
|
2018-10-25 21:28:16 +01:00
|
|
|
Path storj.Path
|
2018-09-07 15:20:15 +01:00
|
|
|
Meta Meta
|
|
|
|
IsPrefix bool
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store for segments
|
|
|
|
type Store interface {
|
2018-10-25 21:28:16 +01:00
|
|
|
Meta(ctx context.Context, path storj.Path) (meta Meta, err error)
|
|
|
|
Get(ctx context.Context, path storj.Path) (rr ranger.Ranger, meta Meta, err error)
|
2018-11-06 14:52:11 +00:00
|
|
|
Repair(ctx context.Context, path storj.Path, lostPieces []int32) (err error)
|
2018-10-25 21:28:16 +01:00
|
|
|
Put(ctx context.Context, data io.Reader, expiration time.Time, segmentInfo func() (storj.Path, []byte, error)) (meta Meta, err error)
|
|
|
|
Delete(ctx context.Context, path storj.Path) (err error)
|
|
|
|
List(ctx context.Context, prefix, startAfter, endBefore storj.Path, recursive bool, limit int, metaFlags uint32) (items []ListItem, more bool, err error)
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type segmentStore struct {
|
2018-08-01 21:25:06 +01:00
|
|
|
oc overlay.Client
|
|
|
|
ec ecclient.Client
|
2018-08-22 16:07:00 +01:00
|
|
|
pdb pdbclient.Client
|
2018-08-01 21:25:06 +01:00
|
|
|
rs eestream.RedundancyStrategy
|
|
|
|
thresholdSize int
|
2018-12-11 17:30:14 +00:00
|
|
|
nodeStats *pb.NodeStats
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSegmentStore creates a new instance of segmentStore
|
2018-12-11 17:30:14 +00:00
|
|
|
func NewSegmentStore(oc overlay.Client, ec ecclient.Client, pdb pdbclient.Client, rs eestream.RedundancyStrategy, threshold int,
|
|
|
|
nodeStats *pb.NodeStats) Store {
|
|
|
|
return &segmentStore{oc: oc, ec: ec, pdb: pdb, rs: rs, thresholdSize: threshold,
|
|
|
|
nodeStats: &pb.NodeStats{
|
|
|
|
UptimeRatio: nodeStats.UptimeRatio,
|
|
|
|
UptimeCount: nodeStats.UptimeCount,
|
|
|
|
AuditSuccessRatio: nodeStats.AuditSuccessRatio,
|
|
|
|
AuditCount: nodeStats.AuditCount,
|
|
|
|
},
|
|
|
|
}
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Meta retrieves the metadata of the segment
|
2018-10-25 21:28:16 +01:00
|
|
|
func (s *segmentStore) Meta(ctx context.Context, path storj.Path) (meta Meta, err error) {
|
2018-07-23 21:05:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-11-26 18:21:44 +00:00
|
|
|
pr, _, _, err := s.pdb.Get(ctx, path)
|
2018-07-23 21:05:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-07-30 19:57:50 +01:00
|
|
|
return convertMeta(pr), nil
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put uploads a segment to an erasure code client
|
2018-10-25 21:28:16 +01:00
|
|
|
func (s *segmentStore) Put(ctx context.Context, data io.Reader, expiration time.Time, segmentInfo func() (storj.Path, []byte, error)) (meta Meta, err error) {
|
2018-07-23 21:05:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-08-01 21:25:06 +01:00
|
|
|
exp, err := ptypes.TimestampProto(expiration)
|
2018-07-23 21:05:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-08-01 21:25:06 +01:00
|
|
|
peekReader := NewPeekThresholdReader(data)
|
|
|
|
remoteSized, err := peekReader.IsLargerThan(s.thresholdSize)
|
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
var path storj.Path
|
2018-10-03 14:05:40 +01:00
|
|
|
var pointer *pb.Pointer
|
2018-08-01 21:25:06 +01:00
|
|
|
if !remoteSized {
|
2018-10-03 14:05:40 +01:00
|
|
|
p, metadata, err := segmentInfo()
|
|
|
|
if err != nil {
|
|
|
|
return Meta{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
path = p
|
|
|
|
|
|
|
|
pointer = &pb.Pointer{
|
2018-09-18 05:39:06 +01:00
|
|
|
Type: pb.Pointer_INLINE,
|
2018-08-01 21:25:06 +01:00
|
|
|
InlineSegment: peekReader.thresholdBuf,
|
2018-11-20 17:09:35 +00:00
|
|
|
SegmentSize: int64(len(peekReader.thresholdBuf)),
|
2018-08-01 21:25:06 +01:00
|
|
|
ExpirationDate: exp,
|
|
|
|
Metadata: metadata,
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-11 17:30:14 +00:00
|
|
|
sizedReader := SizeReader(peekReader)
|
|
|
|
|
|
|
|
// uses overlay client to request a list of nodes according to configured standards
|
|
|
|
nodes, err := s.oc.Choose(ctx,
|
|
|
|
overlay.Options{
|
|
|
|
Amount: s.rs.TotalCount(),
|
|
|
|
Bandwidth: sizedReader.Size() / int64(s.rs.TotalCount()),
|
|
|
|
Space: sizedReader.Size() / int64(s.rs.TotalCount()),
|
|
|
|
Uptime: s.nodeStats.UptimeRatio,
|
|
|
|
UptimeCount: s.nodeStats.UptimeCount,
|
|
|
|
AuditSuccess: s.nodeStats.AuditSuccessRatio,
|
|
|
|
AuditCount: s.nodeStats.AuditCount,
|
|
|
|
Excluded: nil,
|
|
|
|
})
|
2018-08-01 21:25:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, Error.Wrap(err)
|
|
|
|
}
|
2018-11-06 17:49:17 +00:00
|
|
|
pieceID := psclient.NewPieceID()
|
2018-08-01 21:25:06 +01:00
|
|
|
|
2018-11-05 15:12:19 +00:00
|
|
|
authorization := s.pdb.SignedMessage()
|
2018-11-26 18:21:44 +00:00
|
|
|
pba, err := s.pdb.PayerBandwidthAllocation(ctx, pb.PayerBandwidthAllocation_PUT)
|
|
|
|
if err != nil {
|
|
|
|
return Meta{}, Error.Wrap(err)
|
|
|
|
}
|
2018-12-11 17:30:14 +00:00
|
|
|
|
2018-11-05 15:12:19 +00:00
|
|
|
successfulNodes, err := s.ec.Put(ctx, nodes, s.rs, pieceID, sizedReader, expiration, pba, authorization)
|
2018-08-01 21:25:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, Error.Wrap(err)
|
|
|
|
}
|
2018-10-03 14:05:40 +01:00
|
|
|
|
|
|
|
p, metadata, err := segmentInfo()
|
|
|
|
if err != nil {
|
|
|
|
return Meta{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
path = p
|
|
|
|
|
|
|
|
pointer, err = s.makeRemotePointer(successfulNodes, pieceID, sizedReader.Size(), exp, metadata)
|
2018-08-01 21:25:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// puts pointer to pointerDB
|
2018-10-03 14:05:40 +01:00
|
|
|
err = s.pdb.Put(ctx, path, pointer)
|
2018-08-01 21:25:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, Error.Wrap(err)
|
|
|
|
}
|
2018-07-23 21:05:02 +01:00
|
|
|
|
2018-08-01 21:25:06 +01:00
|
|
|
// get the metadata for the newly uploaded segment
|
|
|
|
m, err := s.Meta(ctx, path)
|
2018-07-23 21:05:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, Error.Wrap(err)
|
|
|
|
}
|
2018-08-01 21:25:06 +01:00
|
|
|
return m, nil
|
|
|
|
}
|
2018-07-23 21:05:02 +01:00
|
|
|
|
2018-08-01 21:25:06 +01:00
|
|
|
// makeRemotePointer creates a pointer of type remote
|
2018-11-06 17:49:17 +00:00
|
|
|
func (s *segmentStore) makeRemotePointer(nodes []*pb.Node, pieceID psclient.PieceID, readerSize int64, exp *timestamp.Timestamp, metadata []byte) (pointer *pb.Pointer, err error) {
|
2018-09-18 05:39:06 +01:00
|
|
|
var remotePieces []*pb.RemotePiece
|
2018-07-23 21:05:02 +01:00
|
|
|
for i := range nodes {
|
2018-10-08 15:19:54 +01:00
|
|
|
if nodes[i] == nil {
|
2018-09-27 11:45:19 +01:00
|
|
|
continue
|
|
|
|
}
|
2018-09-18 05:39:06 +01:00
|
|
|
remotePieces = append(remotePieces, &pb.RemotePiece{
|
2018-07-27 07:02:59 +01:00
|
|
|
PieceNum: int32(i),
|
2018-07-23 21:05:02 +01:00
|
|
|
NodeId: nodes[i].Id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
pointer = &pb.Pointer{
|
|
|
|
Type: pb.Pointer_REMOTE,
|
|
|
|
Remote: &pb.RemoteSegment{
|
|
|
|
Redundancy: &pb.RedundancyScheme{
|
|
|
|
Type: pb.RedundancyScheme_RS,
|
2018-07-27 07:02:59 +01:00
|
|
|
MinReq: int32(s.rs.RequiredCount()),
|
|
|
|
Total: int32(s.rs.TotalCount()),
|
2018-09-26 15:23:33 +01:00
|
|
|
RepairThreshold: int32(s.rs.RepairThreshold()),
|
|
|
|
SuccessThreshold: int32(s.rs.OptimalThreshold()),
|
2018-09-27 12:52:18 +01:00
|
|
|
ErasureShareSize: int32(s.rs.ErasureShareSize()),
|
2018-07-23 21:05:02 +01:00
|
|
|
},
|
|
|
|
PieceId: string(pieceID),
|
|
|
|
RemotePieces: remotePieces,
|
|
|
|
},
|
2018-11-20 17:09:35 +00:00
|
|
|
SegmentSize: readerSize,
|
2018-07-27 07:02:59 +01:00
|
|
|
ExpirationDate: exp,
|
|
|
|
Metadata: metadata,
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
2018-08-01 21:25:06 +01:00
|
|
|
return pointer, nil
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves a segment using erasure code, overlay, and pointerdb clients
|
2018-11-06 11:40:06 +00:00
|
|
|
func (s *segmentStore) Get(ctx context.Context, path storj.Path) (rr ranger.Ranger, meta Meta, err error) {
|
2018-07-23 21:05:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-11-26 18:21:44 +00:00
|
|
|
pr, nodes, pba, err := s.pdb.Get(ctx, path)
|
2018-07-23 21:05:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
switch pr.GetType() {
|
|
|
|
case pb.Pointer_INLINE:
|
|
|
|
rr = ranger.ByteRanger(pr.InlineSegment)
|
|
|
|
case pb.Pointer_REMOTE:
|
2018-08-01 21:25:06 +01:00
|
|
|
seg := pr.GetRemote()
|
2018-11-06 17:49:17 +00:00
|
|
|
pid := psclient.PieceID(seg.GetPieceId())
|
2018-11-06 17:03:11 +00:00
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
nodes, err = s.lookupAndAlignNodes(ctx, nodes, seg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, Error.Wrap(err)
|
2018-08-01 21:25:06 +01:00
|
|
|
}
|
2018-07-23 21:05:02 +01:00
|
|
|
|
2018-08-14 16:15:22 +01:00
|
|
|
es, err := makeErasureScheme(pr.GetRemote().GetRedundancy())
|
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
needed := calcNeededNodes(pr.GetRemote().GetRedundancy())
|
|
|
|
selected := make([]*pb.Node, es.TotalCount())
|
|
|
|
|
|
|
|
for _, i := range rand.Perm(len(nodes)) {
|
|
|
|
node := nodes[i]
|
|
|
|
if node == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
selected[i] = node
|
|
|
|
|
|
|
|
needed--
|
|
|
|
if needed <= 0 {
|
|
|
|
break
|
2018-11-02 15:22:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 15:12:19 +00:00
|
|
|
authorization := s.pdb.SignedMessage()
|
2018-12-11 16:05:14 +00:00
|
|
|
rr, err = s.ec.Get(ctx, selected, es, pid, pr.GetSegmentSize(), pba, authorization)
|
2018-08-01 21:25:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, Error.Wrap(err)
|
|
|
|
}
|
2018-12-11 16:05:14 +00:00
|
|
|
default:
|
|
|
|
return nil, Meta{}, Error.New("unsupported pointer type: %d", pr.GetType())
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
2018-07-30 19:57:50 +01:00
|
|
|
return rr, convertMeta(pr), nil
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
func makeErasureScheme(rs *pb.RedundancyScheme) (eestream.ErasureScheme, error) {
|
2018-08-14 16:15:22 +01:00
|
|
|
fc, err := infectious.NewFEC(int(rs.GetMinReq()), int(rs.GetTotal()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
es := eestream.NewRSScheme(fc, int(rs.GetErasureShareSize()))
|
|
|
|
return es, nil
|
|
|
|
}
|
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
// calcNeededNodes calculate how many minimum nodes are needed for download,
|
|
|
|
// based on t = k + (n-o)k/o
|
|
|
|
func calcNeededNodes(rs *pb.RedundancyScheme) int32 {
|
|
|
|
extra := int32(1)
|
|
|
|
|
|
|
|
if rs.GetSuccessThreshold() > 0 {
|
|
|
|
extra = ((rs.GetTotal() - rs.GetSuccessThreshold()) * rs.GetMinReq()) / rs.GetSuccessThreshold()
|
|
|
|
if extra == 0 {
|
|
|
|
// ensure there is at least one extra node, so we can have error detection/correction
|
|
|
|
extra = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
needed := rs.GetMinReq() + extra
|
|
|
|
|
|
|
|
if needed > rs.GetTotal() {
|
|
|
|
needed = rs.GetTotal()
|
|
|
|
}
|
|
|
|
|
|
|
|
return needed
|
|
|
|
}
|
|
|
|
|
2018-07-23 21:05:02 +01:00
|
|
|
// Delete tells piece stores to delete a segment and deletes pointer from pointerdb
|
2018-10-25 21:28:16 +01:00
|
|
|
func (s *segmentStore) Delete(ctx context.Context, path storj.Path) (err error) {
|
2018-07-23 21:05:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-11-26 18:21:44 +00:00
|
|
|
pr, nodes, _, err := s.pdb.Get(ctx, path)
|
2018-07-23 21:05:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
if pr.GetType() == pb.Pointer_REMOTE {
|
2018-08-01 21:25:06 +01:00
|
|
|
seg := pr.GetRemote()
|
2018-11-06 17:49:17 +00:00
|
|
|
pid := psclient.PieceID(seg.PieceId)
|
2018-11-06 17:03:11 +00:00
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
nodes, err = s.lookupAndAlignNodes(ctx, nodes, seg)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
2018-08-01 21:25:06 +01:00
|
|
|
}
|
2018-07-23 21:05:02 +01:00
|
|
|
|
2018-11-05 15:12:19 +00:00
|
|
|
authorization := s.pdb.SignedMessage()
|
2018-08-01 21:25:06 +01:00
|
|
|
// ecclient sends delete request
|
2018-11-05 15:12:19 +00:00
|
|
|
err = s.ec.Delete(ctx, nodes, pid, authorization)
|
2018-08-01 21:25:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// deletes pointer from pointerdb
|
2018-08-02 20:09:50 +01:00
|
|
|
return s.pdb.Delete(ctx, path)
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 18:06:12 +00:00
|
|
|
// Repair retrieves an at-risk segment and repairs and stores lost pieces on new nodes
|
2018-11-06 14:52:11 +00:00
|
|
|
func (s *segmentStore) Repair(ctx context.Context, path storj.Path, lostPieces []int32) (err error) {
|
2018-10-30 18:06:12 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
// Read the segment's pointer's info from the PointerDB
|
2018-11-26 18:21:44 +00:00
|
|
|
pr, originalNodes, pba, err := s.pdb.Get(ctx, path)
|
2018-10-30 18:06:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pr.GetType() != pb.Pointer_REMOTE {
|
2018-11-08 20:27:07 +00:00
|
|
|
return Error.New("cannot repair inline segment %s", psclient.PieceID(pr.GetInlineSegment()))
|
2018-10-30 18:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
seg := pr.GetRemote()
|
2018-11-06 17:49:17 +00:00
|
|
|
pid := psclient.PieceID(seg.GetPieceId())
|
2018-10-30 18:06:12 +00:00
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
originalNodes, err = s.lookupAndAlignNodes(ctx, originalNodes, seg)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
2018-10-30 18:06:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
// Get the nodes list that needs to be excluded
|
2018-11-29 18:39:27 +00:00
|
|
|
var excludeNodeIDs storj.NodeIDList
|
2018-10-30 18:06:12 +00:00
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
// Count the number of nil nodes thats needs to be repaired
|
2018-10-30 18:06:12 +00:00
|
|
|
totalNilNodes := 0
|
2018-11-12 16:10:44 +00:00
|
|
|
|
|
|
|
healthyNodes := make([]*pb.Node, len(originalNodes))
|
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
// Populate healthyNodes with all nodes from originalNodes except those correlating to indices in lostPieces
|
2018-11-12 16:10:44 +00:00
|
|
|
for i, v := range originalNodes {
|
|
|
|
if v == nil {
|
2018-10-30 18:06:12 +00:00
|
|
|
totalNilNodes++
|
2018-11-12 16:10:44 +00:00
|
|
|
continue
|
2018-10-30 18:06:12 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
excludeNodeIDs = append(excludeNodeIDs, v.Id)
|
2018-11-12 16:10:44 +00:00
|
|
|
|
|
|
|
// If node index exists in lostPieces, skip adding it to healthyNodes
|
|
|
|
if contains(lostPieces, i) {
|
|
|
|
totalNilNodes++
|
|
|
|
} else {
|
|
|
|
healthyNodes[i] = v
|
2018-10-30 18:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
// Request Overlay for n-h new storage nodes
|
2018-10-30 18:06:12 +00:00
|
|
|
op := overlay.Options{Amount: totalNilNodes, Space: 0, Excluded: excludeNodeIDs}
|
|
|
|
newNodes, err := s.oc.Choose(ctx, op)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-12 16:10:44 +00:00
|
|
|
if totalNilNodes != len(newNodes) {
|
|
|
|
return Error.New("Number of new nodes from overlay (%d) does not equal total nil nodes (%d)", len(newNodes), totalNilNodes)
|
|
|
|
}
|
|
|
|
|
2018-10-30 18:06:12 +00:00
|
|
|
totalRepairCount := len(newNodes)
|
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
// Make a repair nodes list just with new unique ids
|
|
|
|
repairNodes := make([]*pb.Node, len(healthyNodes))
|
|
|
|
for i, vr := range healthyNodes {
|
|
|
|
// Check that totalRepairCount is non-negative
|
2018-11-12 16:10:44 +00:00
|
|
|
if totalRepairCount < 0 {
|
|
|
|
return Error.New("Total repair count (%d) less than zero", totalRepairCount)
|
|
|
|
}
|
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
// Find the nil nodes in the healthyNodes list
|
2018-10-30 18:06:12 +00:00
|
|
|
if vr == nil {
|
2018-12-11 16:05:14 +00:00
|
|
|
// Assign the item in repairNodes list with an item from the newNode list
|
2018-10-30 18:06:12 +00:00
|
|
|
totalRepairCount--
|
2018-12-11 16:05:14 +00:00
|
|
|
repairNodes[i] = newNodes[totalRepairCount]
|
2018-10-30 18:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
// Check that all nil nodes have a replacement prepared
|
2018-11-12 16:10:44 +00:00
|
|
|
if totalRepairCount != 0 {
|
|
|
|
return Error.New("Failed to replace all nil nodes (%d). (%d) new nodes not inserted", len(newNodes), totalRepairCount)
|
|
|
|
}
|
|
|
|
|
2018-10-30 18:06:12 +00:00
|
|
|
es, err := makeErasureScheme(pr.GetRemote().GetRedundancy())
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-11-05 15:12:19 +00:00
|
|
|
signedMessage := s.pdb.SignedMessage()
|
2018-10-30 18:06:12 +00:00
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
// Download the segment using just the healthyNodes
|
2018-11-20 17:09:35 +00:00
|
|
|
rr, err := s.ec.Get(ctx, healthyNodes, es, pid, pr.GetSegmentSize(), pba, signedMessage)
|
2018-10-30 18:06:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := rr.Range(ctx, 0, rr.Size())
|
|
|
|
if err != nil {
|
2018-12-11 16:05:14 +00:00
|
|
|
return Error.Wrap(err)
|
2018-10-30 18:06:12 +00:00
|
|
|
}
|
|
|
|
defer utils.LogClose(r)
|
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
// Upload the repaired pieces to the repairNodes
|
|
|
|
successfulNodes, err := s.ec.Put(ctx, repairNodes, s.rs, pid, r, convertTime(pr.GetExpirationDate()), pba, signedMessage)
|
2018-10-30 18:06:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
// Merge the successful nodes list into the healthy nodes list
|
2018-11-12 16:10:44 +00:00
|
|
|
for i, v := range healthyNodes {
|
2018-10-30 18:06:12 +00:00
|
|
|
if v == nil {
|
|
|
|
// copy the successfuNode info
|
2018-11-12 16:10:44 +00:00
|
|
|
healthyNodes[i] = successfulNodes[i]
|
2018-10-30 18:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata := pr.GetMetadata()
|
2018-12-11 16:05:14 +00:00
|
|
|
pointer, err := s.makeRemotePointer(healthyNodes, pid, rr.Size(), pr.GetExpirationDate(), metadata)
|
2018-10-30 18:06:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the segment info in the pointerDB
|
|
|
|
return s.pdb.Put(ctx, path, pointer)
|
|
|
|
}
|
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
// lookupNodes, if necessary, calls Lookup to get node addresses from the overlay.
|
|
|
|
// It also realigns the nodes to an indexed list of nodes based on the piece number.
|
|
|
|
// Missing pieces are represented by a nil node.
|
|
|
|
func (s *segmentStore) lookupAndAlignNodes(ctx context.Context, nodes []*pb.Node, seg *pb.RemoteSegment) (result []*pb.Node, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if nodes == nil {
|
|
|
|
// Get list of all nodes IDs storing a piece from the segment
|
|
|
|
var nodeIds storj.NodeIDList
|
|
|
|
for _, p := range seg.RemotePieces {
|
|
|
|
nodeIds = append(nodeIds, p.NodeId)
|
|
|
|
}
|
|
|
|
// Lookup the node info from node IDs
|
|
|
|
nodes, err = s.oc.BulkLookup(ctx, nodeIds)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
2018-12-11 16:05:14 +00:00
|
|
|
|
|
|
|
// Realign the nodes
|
|
|
|
result = make([]*pb.Node, seg.GetRedundancy().GetTotal())
|
2018-09-27 11:45:19 +01:00
|
|
|
for i, p := range seg.GetRemotePieces() {
|
2018-12-11 16:05:14 +00:00
|
|
|
result[p.PieceNum] = nodes[i]
|
2018-09-27 11:45:19 +01:00
|
|
|
}
|
2018-12-11 16:05:14 +00:00
|
|
|
|
|
|
|
return result, nil
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// List retrieves paths to segments and their metadata stored in the pointerdb
|
2018-10-25 21:28:16 +01:00
|
|
|
func (s *segmentStore) List(ctx context.Context, prefix, startAfter, endBefore storj.Path, recursive bool, limit int, metaFlags uint32) (items []ListItem, more bool, err error) {
|
2018-07-23 21:05:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
pdbItems, more, err := s.pdb.List(ctx, prefix, startAfter, endBefore, recursive, limit, metaFlags)
|
2018-07-30 19:57:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
items = make([]ListItem, len(pdbItems))
|
|
|
|
for i, itm := range pdbItems {
|
|
|
|
items[i] = ListItem{
|
2018-09-11 14:13:25 +01:00
|
|
|
Path: itm.Path,
|
|
|
|
Meta: convertMeta(itm.Pointer),
|
2018-09-07 15:20:15 +01:00
|
|
|
IsPrefix: itm.IsPrefix,
|
2018-07-30 19:57:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return items, more, nil
|
|
|
|
}
|
|
|
|
|
2018-11-12 16:10:44 +00:00
|
|
|
// contains checks if n exists in list
|
|
|
|
func contains(list []int32, n int) bool {
|
|
|
|
for i := range list {
|
|
|
|
if n == int(list[i]) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-07-30 19:57:50 +01:00
|
|
|
// convertMeta converts pointer to segment metadata
|
2018-09-18 05:39:06 +01:00
|
|
|
func convertMeta(pr *pb.Pointer) Meta {
|
2018-07-30 19:57:50 +01:00
|
|
|
return Meta{
|
|
|
|
Modified: convertTime(pr.GetCreationDate()),
|
|
|
|
Expiration: convertTime(pr.GetExpirationDate()),
|
2018-11-20 17:09:35 +00:00
|
|
|
Size: pr.GetSegmentSize(),
|
2018-07-30 19:57:50 +01:00
|
|
|
Data: pr.GetMetadata(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertTime converts gRPC timestamp to Go time
|
|
|
|
func convertTime(ts *timestamp.Timestamp) time.Time {
|
2018-08-16 15:32:28 +01:00
|
|
|
if ts == nil {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
2018-07-30 19:57:50 +01:00
|
|
|
t, err := ptypes.Timestamp(ts)
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Warnf("Failed converting timestamp %v: %v", ts, err)
|
|
|
|
}
|
|
|
|
return t
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|