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"
|
2019-08-16 18:14:02 +01:00
|
|
|
"sync"
|
2018-07-23 21:05:02 +01:00
|
|
|
"time"
|
|
|
|
|
2019-09-10 16:39:47 +01:00
|
|
|
"github.com/vivint/infectious"
|
2019-07-09 22:54:00 +01:00
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-07-23 21:05:02 +01:00
|
|
|
"storj.io/storj/pkg/ranger"
|
2018-10-25 21:28:16 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/uplink/ecclient"
|
|
|
|
"storj.io/storj/uplink/eestream"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/uplink/metainfo"
|
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 {
|
2019-04-10 23:27:04 +01:00
|
|
|
Modified time.Time
|
|
|
|
Expiration time.Time
|
|
|
|
Size int64
|
|
|
|
Data []byte
|
2018-07-30 19:57:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2019-10-28 16:23:20 +00:00
|
|
|
// Ranger creates a ranger for downloading erasure codes from piece store nodes.
|
|
|
|
Ranger(ctx context.Context, info storj.SegmentDownloadInfo, limits []*pb.AddressedOrderLimit, objectRS storj.RedundancyScheme) (ranger.Ranger, error)
|
2019-10-29 15:49:16 +00:00
|
|
|
Put(ctx context.Context, data io.Reader, expiration time.Time, limits []*pb.AddressedOrderLimit, piecePrivateKey storj.PiecePrivateKey) (_ []*pb.SegmentPieceUploadResult, size int64, err error)
|
2019-09-10 16:39:47 +01:00
|
|
|
Delete(ctx context.Context, streamID storj.StreamID, segmentIndex int32) (err error)
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type segmentStore struct {
|
2019-10-29 15:49:16 +00:00
|
|
|
metainfo *metainfo.Client
|
|
|
|
ec ecclient.Client
|
|
|
|
rs eestream.RedundancyStrategy
|
|
|
|
rngMu sync.Mutex
|
|
|
|
rng *rand.Rand
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSegmentStore creates a new instance of segmentStore
|
2019-10-29 15:49:16 +00:00
|
|
|
func NewSegmentStore(metainfo *metainfo.Client, ec ecclient.Client, rs eestream.RedundancyStrategy) Store {
|
2019-02-05 10:54:25 +00:00
|
|
|
return &segmentStore{
|
2019-10-29 15:49:16 +00:00
|
|
|
metainfo: metainfo,
|
|
|
|
ec: ec,
|
|
|
|
rs: rs,
|
|
|
|
rng: rand.New(rand.NewSource(time.Now().UnixNano())),
|
2019-02-05 10:54:25 +00:00
|
|
|
}
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put uploads a segment to an erasure code client
|
2019-10-29 15:49:16 +00:00
|
|
|
func (s *segmentStore) Put(ctx context.Context, data io.Reader, expiration time.Time, limits []*pb.AddressedOrderLimit, piecePrivateKey storj.PiecePrivateKey) (_ []*pb.SegmentPieceUploadResult, size int64, err error) {
|
2018-07-23 21:05:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-10-29 15:49:16 +00:00
|
|
|
sizedReader := SizeReader(NewPeekThresholdReader(data))
|
2019-09-10 16:39:47 +01:00
|
|
|
successfulNodes, successfulHashes, err := s.ec.Put(ctx, limits, piecePrivateKey, s.rs, sizedReader, expiration)
|
2018-08-01 21:25:06 +01:00
|
|
|
if err != nil {
|
2019-10-29 15:49:16 +00:00
|
|
|
return nil, size, Error.Wrap(err)
|
2018-08-01 21:25:06 +01:00
|
|
|
}
|
2018-07-23 21:05:02 +01:00
|
|
|
|
2019-09-10 16:39:47 +01:00
|
|
|
uploadResults := make([]*pb.SegmentPieceUploadResult, 0, len(successfulNodes))
|
|
|
|
for i := range successfulNodes {
|
|
|
|
if successfulNodes[i] == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
uploadResults = append(uploadResults, &pb.SegmentPieceUploadResult{
|
|
|
|
PieceNum: int32(i),
|
|
|
|
NodeId: successfulNodes[i].Id,
|
|
|
|
Hash: successfulHashes[i],
|
|
|
|
})
|
|
|
|
}
|
2019-10-17 19:01:40 +01:00
|
|
|
|
|
|
|
if l := len(uploadResults); l < s.rs.OptimalThreshold() {
|
2019-10-29 15:49:16 +00:00
|
|
|
return nil, size, Error.New("uploaded results (%d) are below the optimal threshold (%d)", l, s.rs.OptimalThreshold())
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-10-29 15:49:16 +00:00
|
|
|
return uploadResults, sizedReader.Size(), nil
|
2018-08-01 21:25:06 +01:00
|
|
|
}
|
2018-07-23 21:05:02 +01:00
|
|
|
|
2019-10-28 16:23:20 +00:00
|
|
|
// Ranger creates a ranger for downloading erasure codes from piece store nodes.
|
|
|
|
func (s *segmentStore) Ranger(
|
|
|
|
ctx context.Context, info storj.SegmentDownloadInfo, limits []*pb.AddressedOrderLimit, objectRS storj.RedundancyScheme,
|
|
|
|
) (rr ranger.Ranger, err error) {
|
|
|
|
defer mon.Task()(&ctx, info, limits, objectRS)(&err)
|
2018-07-23 21:05:02 +01:00
|
|
|
|
2019-09-19 00:18:14 +01:00
|
|
|
// no order limits also means its inline segment
|
2019-10-29 14:39:17 +00:00
|
|
|
if len(info.EncryptedInlineData) != 0 || len(limits) == 0 {
|
2019-10-28 16:23:20 +00:00
|
|
|
return ranger.ByteRanger(info.EncryptedInlineData), nil
|
2019-10-29 14:39:17 +00:00
|
|
|
}
|
2018-11-02 15:22:01 +00:00
|
|
|
|
2019-10-29 14:39:17 +00:00
|
|
|
needed := CalcNeededNodes(objectRS)
|
|
|
|
selected := make([]*pb.AddressedOrderLimit, len(limits))
|
|
|
|
s.rngMu.Lock()
|
|
|
|
perm := s.rng.Perm(len(limits))
|
|
|
|
s.rngMu.Unlock()
|
|
|
|
|
|
|
|
for _, i := range perm {
|
|
|
|
limit := limits[i]
|
|
|
|
if limit == nil {
|
|
|
|
continue
|
2018-08-01 21:25:06 +01:00
|
|
|
}
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-10-29 14:39:17 +00:00
|
|
|
selected[i] = limit
|
|
|
|
|
|
|
|
needed--
|
|
|
|
if needed <= 0 {
|
|
|
|
break
|
2018-12-11 16:05:14 +00:00
|
|
|
}
|
2019-10-29 14:39:17 +00:00
|
|
|
}
|
2018-12-11 16:05:14 +00:00
|
|
|
|
2019-10-29 14:39:17 +00:00
|
|
|
fc, err := infectious.NewFEC(int(objectRS.RequiredShares), int(objectRS.TotalShares))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-12-11 16:05:14 +00:00
|
|
|
}
|
2019-10-29 14:39:17 +00:00
|
|
|
es := eestream.NewRSScheme(fc, int(objectRS.ShareSize))
|
|
|
|
redundancy, err := eestream.NewRedundancyStrategy(es, int(objectRS.RepairShares), int(objectRS.OptimalShares))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rr, err = s.ec.Get(ctx, selected, info.PiecePrivateKey, redundancy, info.Size)
|
|
|
|
return rr, Error.Wrap(err)
|
2018-12-11 16:05:14 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// Delete requests the satellite to delete a segment and tells storage nodes
|
|
|
|
// to delete the segment's pieces.
|
2019-09-10 16:39:47 +01:00
|
|
|
func (s *segmentStore) Delete(ctx context.Context, streamID storj.StreamID, segmentIndex int32) (err error) {
|
2018-07-23 21:05:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-09-23 22:41:58 +01:00
|
|
|
_, limits, privateKey, err := s.metainfo.BeginDeleteSegment(ctx, metainfo.BeginDeleteSegmentParams{
|
2019-09-10 16:39:47 +01:00
|
|
|
StreamID: streamID,
|
|
|
|
Position: storj.SegmentPosition{
|
|
|
|
Index: segmentIndex,
|
|
|
|
},
|
|
|
|
})
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2018-11-06 17:03:11 +00:00
|
|
|
|
2019-09-10 16:39:47 +01:00
|
|
|
if len(limits) != 0 {
|
|
|
|
// remote segment - delete the pieces from storage nodes
|
|
|
|
err = s.ec.Delete(ctx, limits, privateKey)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
2018-07-23 21:05:02 +01:00
|
|
|
|
2019-09-23 22:41:58 +01:00
|
|
|
// don't do FinishDeleteSegment at the moment to avoid satellite round trip
|
|
|
|
// FinishDeleteSegment doesn't implement any specific logic at the moment
|
2018-07-23 21:05:02 +01:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
return nil
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// CalcNeededNodes calculate how many minimum nodes are needed for download,
|
2018-12-13 07:12:36 +00:00
|
|
|
// based on t = k + (n-o)k/o
|
2019-09-10 16:39:47 +01:00
|
|
|
func CalcNeededNodes(rs storj.RedundancyScheme) int32 {
|
2018-12-13 07:12:36 +00:00
|
|
|
extra := int32(1)
|
2018-10-30 18:06:12 +00:00
|
|
|
|
2019-09-10 16:39:47 +01:00
|
|
|
if rs.OptimalShares > 0 {
|
|
|
|
extra = int32(((rs.TotalShares - rs.OptimalShares) * rs.RequiredShares) / rs.OptimalShares)
|
2018-12-13 07:12:36 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 16:39:47 +01:00
|
|
|
needed := int32(rs.RequiredShares) + extra
|
2018-12-13 07:12:36 +00:00
|
|
|
|
2019-09-10 16:39:47 +01:00
|
|
|
if needed > int32(rs.TotalShares) {
|
|
|
|
needed = int32(rs.TotalShares)
|
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
|
|
|
}
|