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"
|
|
|
|
"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"
|
|
|
|
"go.uber.org/zap"
|
2018-07-23 21:05:02 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/eestream"
|
|
|
|
"storj.io/storj/pkg/kademlia"
|
|
|
|
"storj.io/storj/pkg/overlay"
|
|
|
|
"storj.io/storj/pkg/paths"
|
|
|
|
"storj.io/storj/pkg/piecestore/rpc/client"
|
|
|
|
"storj.io/storj/pkg/pointerdb"
|
|
|
|
"storj.io/storj/pkg/ranger"
|
|
|
|
"storj.io/storj/pkg/storage/ec"
|
|
|
|
opb "storj.io/storj/protos/overlay"
|
|
|
|
ppb "storj.io/storj/protos/pointerdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
Path paths.Path
|
|
|
|
Meta Meta
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store for segments
|
|
|
|
type Store interface {
|
|
|
|
Meta(ctx context.Context, path paths.Path) (meta Meta, err error)
|
|
|
|
Get(ctx context.Context, path paths.Path) (rr ranger.RangeCloser,
|
|
|
|
meta Meta, err error)
|
|
|
|
Put(ctx context.Context, path paths.Path, data io.Reader, metadata []byte,
|
|
|
|
expiration time.Time) (meta Meta, err error)
|
|
|
|
Delete(ctx context.Context, path paths.Path) (err error)
|
|
|
|
List(ctx context.Context, prefix, startAfter, endBefore paths.Path,
|
2018-07-30 19:57:50 +01:00
|
|
|
recursive bool, limit int, metaFlags uint32) (items []ListItem,
|
2018-07-23 21:05:02 +01:00
|
|
|
more bool, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type segmentStore struct {
|
2018-08-01 21:25:06 +01:00
|
|
|
oc overlay.Client
|
|
|
|
ec ecclient.Client
|
|
|
|
pdb pointerdb.Client
|
|
|
|
rs eestream.RedundancyStrategy
|
|
|
|
thresholdSize int
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSegmentStore creates a new instance of segmentStore
|
|
|
|
func NewSegmentStore(oc overlay.Client, ec ecclient.Client,
|
2018-08-01 21:25:06 +01:00
|
|
|
pdb pointerdb.Client, rs eestream.RedundancyStrategy, t int) Store {
|
|
|
|
return &segmentStore{oc: oc, ec: ec, pdb: pdb, rs: rs, thresholdSize: t}
|
2018-07-23 21:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Meta retrieves the metadata of the segment
|
|
|
|
func (s *segmentStore) Meta(ctx context.Context, path paths.Path) (meta Meta,
|
|
|
|
err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-08-02 20:09:50 +01: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
|
|
|
|
func (s *segmentStore) Put(ctx context.Context, path paths.Path, data io.Reader,
|
|
|
|
metadata []byte, expiration time.Time) (meta Meta, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-08-01 21:25:06 +01:00
|
|
|
var p *ppb.Pointer
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if !remoteSized {
|
|
|
|
p = &ppb.Pointer{
|
|
|
|
Type: ppb.Pointer_INLINE,
|
|
|
|
InlineSegment: peekReader.thresholdBuf,
|
|
|
|
Size: int64(len(peekReader.thresholdBuf)),
|
|
|
|
ExpirationDate: exp,
|
|
|
|
Metadata: metadata,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// uses overlay client to request a list of nodes
|
|
|
|
nodes, err := s.oc.Choose(ctx, s.rs.TotalCount(), 0)
|
|
|
|
if err != nil {
|
|
|
|
return Meta{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
pieceID := client.NewPieceID()
|
|
|
|
sizedReader := SizeReader(peekReader)
|
|
|
|
|
|
|
|
// puts file to ecclient
|
|
|
|
err = s.ec.Put(ctx, nodes, s.rs, pieceID, sizedReader, expiration)
|
|
|
|
if err != nil {
|
|
|
|
return Meta{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
p, err = s.makeRemotePointer(nodes, pieceID, sizedReader.Size(), exp, metadata)
|
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// puts pointer to pointerDB
|
2018-08-02 20:09:50 +01:00
|
|
|
err = s.pdb.Put(ctx, path, p)
|
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
|
|
|
|
func (s *segmentStore) makeRemotePointer(nodes []*opb.Node, pieceID client.PieceID, readerSize int64,
|
|
|
|
exp *timestamp.Timestamp, metadata []byte) (pointer *ppb.Pointer, err error) {
|
2018-07-23 21:05:02 +01:00
|
|
|
var remotePieces []*ppb.RemotePiece
|
|
|
|
for i := range nodes {
|
|
|
|
remotePieces = append(remotePieces, &ppb.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-08-01 21:25:06 +01:00
|
|
|
pointer = &ppb.Pointer{
|
2018-07-23 21:05:02 +01:00
|
|
|
Type: ppb.Pointer_REMOTE,
|
|
|
|
Remote: &ppb.RemoteSegment{
|
|
|
|
Redundancy: &ppb.RedundancyScheme{
|
|
|
|
Type: ppb.RedundancyScheme_RS,
|
2018-07-27 07:02:59 +01:00
|
|
|
MinReq: int32(s.rs.RequiredCount()),
|
|
|
|
Total: int32(s.rs.TotalCount()),
|
|
|
|
RepairThreshold: int32(s.rs.Min),
|
|
|
|
SuccessThreshold: int32(s.rs.Opt),
|
2018-07-23 21:05:02 +01:00
|
|
|
},
|
|
|
|
PieceId: string(pieceID),
|
|
|
|
RemotePieces: remotePieces,
|
|
|
|
},
|
2018-08-01 21:25:06 +01:00
|
|
|
Size: 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
|
|
|
|
func (s *segmentStore) Get(ctx context.Context, path paths.Path) (
|
|
|
|
rr ranger.RangeCloser, meta Meta, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-08-02 20:09:50 +01:00
|
|
|
pr, err := s.pdb.Get(ctx, path)
|
2018-07-23 21:05:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-08-01 21:25:06 +01:00
|
|
|
if pr.GetType() == ppb.Pointer_REMOTE {
|
|
|
|
seg := pr.GetRemote()
|
|
|
|
pid := client.PieceID(seg.PieceId)
|
|
|
|
nodes, err := s.lookupNodes(ctx, seg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, Error.Wrap(err)
|
|
|
|
}
|
2018-07-23 21:05:02 +01:00
|
|
|
|
2018-08-01 21:25:06 +01:00
|
|
|
rr, err = s.ec.Get(ctx, nodes, s.rs, pid, pr.GetSize())
|
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rr = ranger.NopCloser(ranger.ByteRanger(pr.InlineSegment))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Delete tells piece stores to delete a segment and deletes pointer from pointerdb
|
|
|
|
func (s *segmentStore) Delete(ctx context.Context, path paths.Path) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-08-02 20:09:50 +01:00
|
|
|
pr, err := s.pdb.Get(ctx, path)
|
2018-07-23 21:05:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-08-01 21:25:06 +01:00
|
|
|
if pr.GetType() == ppb.Pointer_REMOTE {
|
|
|
|
seg := pr.GetRemote()
|
|
|
|
pid := client.PieceID(seg.PieceId)
|
|
|
|
nodes, err := s.lookupNodes(ctx, seg)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2018-07-23 21:05:02 +01:00
|
|
|
|
2018-08-01 21:25:06 +01:00
|
|
|
// ecclient sends delete request
|
|
|
|
err = s.ec.Delete(ctx, nodes, pid)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// lookupNodes calls Lookup to get node addresses from the overlay
|
|
|
|
func (s *segmentStore) lookupNodes(ctx context.Context, seg *ppb.RemoteSegment) (
|
|
|
|
nodes []*opb.Node, err error) {
|
|
|
|
nodes = make([]*opb.Node, len(seg.GetRemotePieces()))
|
|
|
|
for i, p := range seg.GetRemotePieces() {
|
|
|
|
node, err := s.oc.Lookup(ctx, kademlia.StringToNodeID(p.GetNodeId()))
|
|
|
|
if err != nil {
|
2018-07-30 19:57:50 +01:00
|
|
|
// TODO(kaloyan): better error handling: failing to lookup a few
|
|
|
|
// nodes should not fail the request
|
2018-07-23 21:05:02 +01:00
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
nodes[i] = node
|
|
|
|
}
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// List retrieves paths to segments and their metadata stored in the pointerdb
|
|
|
|
func (s *segmentStore) List(ctx context.Context, prefix, startAfter,
|
2018-07-30 19:57:50 +01:00
|
|
|
endBefore paths.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-07-30 19:57:50 +01:00
|
|
|
pdbItems, more, err := s.pdb.List(ctx, prefix, startAfter, endBefore,
|
2018-08-02 20:09:50 +01:00
|
|
|
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{
|
|
|
|
Path: itm.Path,
|
|
|
|
Meta: convertMeta(itm.Pointer),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return items, more, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertMeta converts pointer to segment metadata
|
|
|
|
func convertMeta(pr *ppb.Pointer) Meta {
|
|
|
|
return Meta{
|
|
|
|
Modified: convertTime(pr.GetCreationDate()),
|
|
|
|
Expiration: convertTime(pr.GetExpirationDate()),
|
|
|
|
Size: pr.GetSize(),
|
|
|
|
Data: pr.GetMetadata(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertTime converts gRPC timestamp to Go time
|
|
|
|
func convertTime(ts *timestamp.Timestamp) time.Time {
|
|
|
|
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
|
|
|
}
|