2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-07-23 21:05:02 +01:00
|
|
|
// 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"
|
2019-01-02 18:47:34 +00:00
|
|
|
ecclient "storj.io/storj/pkg/storage/ec"
|
2018-10-25 21:28:16 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
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)
|
|
|
|
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-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSegmentStore creates a new instance of segmentStore
|
2018-12-17 21:05:05 +00:00
|
|
|
func NewSegmentStore(oc overlay.Client, ec ecclient.Client, pdb pdbclient.Client, rs eestream.RedundancyStrategy, threshold int) Store {
|
2019-02-05 10:54:25 +00:00
|
|
|
return &segmentStore{
|
|
|
|
oc: oc,
|
|
|
|
ec: ec,
|
|
|
|
pdb: pdb,
|
|
|
|
rs: rs,
|
|
|
|
thresholdSize: threshold,
|
|
|
|
}
|
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{
|
2018-12-17 21:05:05 +00:00
|
|
|
Amount: s.rs.TotalCount(),
|
|
|
|
Bandwidth: sizedReader.Size() / int64(s.rs.TotalCount()),
|
|
|
|
Space: sizedReader.Size() / int64(s.rs.TotalCount()),
|
|
|
|
Excluded: nil,
|
2018-12-11 17:30:14 +00:00
|
|
|
})
|
2018-08-01 21:25:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, Error.Wrap(err)
|
|
|
|
}
|
2019-01-02 18:47:34 +00:00
|
|
|
for _, v := range nodes {
|
2019-02-01 17:55:47 +00:00
|
|
|
if v != nil {
|
|
|
|
v.Type.DPanicOnInvalid("ss put")
|
|
|
|
}
|
2019-01-02 18:47:34 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2019-01-28 19:45:25 +00:00
|
|
|
pba, err := s.pdb.PayerBandwidthAllocation(ctx, pb.BandwidthAction_PUT)
|
2018-11-26 18:21:44 +00:00
|
|
|
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
|
|
|
|
|
2018-12-13 07:12:36 +00:00
|
|
|
pointer, err = makeRemotePointer(successfulNodes, s.rs, 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
|
|
|
|
|
|
|
// 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-13 07:12:36 +00:00
|
|
|
nodes, err = lookupAndAlignNodes(ctx, s.oc, nodes, seg)
|
2018-12-11 16:05:14 +00:00
|
|
|
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-12-13 07:12:36 +00:00
|
|
|
rs, err := makeRedundancyStrategy(pr.GetRemote().GetRedundancy())
|
2018-08-14 16:15:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
needed := calcNeededNodes(pr.GetRemote().GetRedundancy())
|
2018-12-13 07:12:36 +00:00
|
|
|
selected := make([]*pb.Node, rs.TotalCount())
|
2018-12-11 16:05:14 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2019-01-02 18:47:34 +00:00
|
|
|
node.Type.DPanicOnInvalid("ss get")
|
2018-11-02 15:22:01 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 15:12:19 +00:00
|
|
|
authorization := s.pdb.SignedMessage()
|
2018-12-13 07:12:36 +00:00
|
|
|
rr, err = s.ec.Get(ctx, selected, rs, 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-12-13 07:12:36 +00:00
|
|
|
// makeRemotePointer creates a pointer of type remote
|
|
|
|
func makeRemotePointer(nodes []*pb.Node, rs eestream.RedundancyStrategy, pieceID psclient.PieceID, readerSize int64, exp *timestamp.Timestamp, metadata []byte) (pointer *pb.Pointer, err error) {
|
|
|
|
var remotePieces []*pb.RemotePiece
|
|
|
|
for i := range nodes {
|
|
|
|
if nodes[i] == nil {
|
|
|
|
continue
|
2018-12-11 16:05:14 +00:00
|
|
|
}
|
2019-01-02 18:47:34 +00:00
|
|
|
nodes[i].Type.DPanicOnInvalid("makeremotepointer")
|
2018-12-13 07:12:36 +00:00
|
|
|
remotePieces = append(remotePieces, &pb.RemotePiece{
|
|
|
|
PieceNum: int32(i),
|
|
|
|
NodeId: nodes[i].Id,
|
|
|
|
})
|
2018-12-11 16:05:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 07:12:36 +00:00
|
|
|
pointer = &pb.Pointer{
|
|
|
|
Type: pb.Pointer_REMOTE,
|
|
|
|
Remote: &pb.RemoteSegment{
|
|
|
|
Redundancy: &pb.RedundancyScheme{
|
|
|
|
Type: pb.RedundancyScheme_RS,
|
|
|
|
MinReq: int32(rs.RequiredCount()),
|
|
|
|
Total: int32(rs.TotalCount()),
|
|
|
|
RepairThreshold: int32(rs.RepairThreshold()),
|
|
|
|
SuccessThreshold: int32(rs.OptimalThreshold()),
|
|
|
|
ErasureShareSize: int32(rs.ErasureShareSize()),
|
|
|
|
},
|
|
|
|
PieceId: string(pieceID),
|
|
|
|
RemotePieces: remotePieces,
|
|
|
|
},
|
|
|
|
SegmentSize: readerSize,
|
|
|
|
ExpirationDate: exp,
|
|
|
|
Metadata: metadata,
|
2018-12-11 16:05:14 +00:00
|
|
|
}
|
2018-12-13 07:12:36 +00:00
|
|
|
return pointer, nil
|
2018-12-11 16:05:14 +00:00
|
|
|
}
|
|
|
|
|
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-13 07:12:36 +00:00
|
|
|
nodes, err = lookupAndAlignNodes(ctx, s.oc, nodes, seg)
|
2018-12-11 16:05:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
2018-08-01 21:25:06 +01:00
|
|
|
}
|
2019-01-02 18:47:34 +00:00
|
|
|
for _, v := range nodes {
|
|
|
|
if v != nil {
|
|
|
|
v.Type.DPanicOnInvalid("ss delete")
|
|
|
|
}
|
|
|
|
}
|
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-12-13 07:12:36 +00:00
|
|
|
// List retrieves paths to segments and their metadata stored in the pointerdb
|
|
|
|
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-10-30 18:06:12 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-12-13 07:12:36 +00:00
|
|
|
pdbItems, more, err := s.pdb.List(ctx, prefix, startAfter, endBefore, recursive, limit, metaFlags)
|
2018-10-30 18:06:12 +00:00
|
|
|
if err != nil {
|
2018-12-13 07:12:36 +00:00
|
|
|
return nil, false, err
|
2018-11-12 16:10:44 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 07:12:36 +00:00
|
|
|
items = make([]ListItem, len(pdbItems))
|
|
|
|
for i, itm := range pdbItems {
|
|
|
|
items[i] = ListItem{
|
|
|
|
Path: itm.Path,
|
|
|
|
Meta: convertMeta(itm.Pointer),
|
|
|
|
IsPrefix: itm.IsPrefix,
|
2018-10-30 18:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 07:12:36 +00:00
|
|
|
return items, more, nil
|
|
|
|
}
|
2018-10-30 18:06:12 +00:00
|
|
|
|
2018-12-13 07:12:36 +00:00
|
|
|
func makeRedundancyStrategy(scheme *pb.RedundancyScheme) (eestream.RedundancyStrategy, error) {
|
|
|
|
fc, err := infectious.NewFEC(int(scheme.GetMinReq()), int(scheme.GetTotal()))
|
2018-10-30 18:06:12 +00:00
|
|
|
if err != nil {
|
2018-12-13 07:12:36 +00:00
|
|
|
return eestream.RedundancyStrategy{}, Error.Wrap(err)
|
2018-10-30 18:06:12 +00:00
|
|
|
}
|
2018-12-13 07:12:36 +00:00
|
|
|
es := eestream.NewRSScheme(fc, int(scheme.GetErasureShareSize()))
|
|
|
|
return eestream.NewRedundancyStrategy(es, int(scheme.GetRepairThreshold()), int(scheme.GetSuccessThreshold()))
|
|
|
|
}
|
2018-10-30 18:06:12 +00:00
|
|
|
|
2018-12-13 07:12:36 +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)
|
2018-10-30 18:06:12 +00:00
|
|
|
|
2018-12-13 07:12:36 +00:00
|
|
|
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
|
2018-10-30 18:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 07:12:36 +00:00
|
|
|
needed := rs.GetMinReq() + extra
|
|
|
|
|
|
|
|
if needed > rs.GetTotal() {
|
|
|
|
needed = rs.GetTotal()
|
2018-10-30 18:06:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 07:12:36 +00:00
|
|
|
return needed
|
2018-10-30 18:06:12 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2018-12-13 07:12:36 +00:00
|
|
|
func lookupAndAlignNodes(ctx context.Context, oc overlay.Client, nodes []*pb.Node, seg *pb.RemoteSegment) (result []*pb.Node, err error) {
|
2018-12-11 16:05:14 +00:00
|
|
|
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
|
2018-12-13 07:12:36 +00:00
|
|
|
nodes, err = oc.BulkLookup(ctx, nodeIds)
|
2018-12-11 16:05:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
2019-01-02 18:47:34 +00:00
|
|
|
for _, v := range nodes {
|
2019-02-01 17:55:47 +00:00
|
|
|
if v != nil {
|
|
|
|
v.Type.DPanicOnInvalid("lookup and align nodes")
|
|
|
|
}
|
2019-01-02 18:47:34 +00: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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|