2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-08-17 18:40:15 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
package psserver
|
2018-08-17 18:40:15 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-09-10 10:18:42 +01:00
|
|
|
"fmt"
|
2018-08-17 18:40:15 +01:00
|
|
|
"io"
|
|
|
|
"os"
|
2018-09-10 10:18:42 +01:00
|
|
|
"sync/atomic"
|
2018-08-17 18:40:15 +01:00
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/zeebo/errs"
|
2018-11-08 20:27:07 +00:00
|
|
|
"go.uber.org/zap"
|
2018-09-10 10:18:42 +01:00
|
|
|
|
|
|
|
"storj.io/storj/internal/sync2"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-08-17 18:40:15 +01:00
|
|
|
"storj.io/storj/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RetrieveError is a type of error for failures in Server.Retrieve()
|
|
|
|
var RetrieveError = errs.Class("retrieve error")
|
|
|
|
|
|
|
|
// Retrieve -- Retrieve data from piecestore and send to client
|
|
|
|
func (s *Server) Retrieve(stream pb.PieceStoreRoutes_RetrieveServer) (err error) {
|
|
|
|
ctx := stream.Context()
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
// Receive Signature
|
|
|
|
recv, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return RetrieveError.Wrap(err)
|
|
|
|
}
|
|
|
|
if recv == nil {
|
|
|
|
return RetrieveError.New("error receiving piece data")
|
|
|
|
}
|
|
|
|
|
2018-10-17 12:40:11 +01:00
|
|
|
authorization := recv.GetAuthorization()
|
|
|
|
if err := s.verifier(authorization); err != nil {
|
2018-11-08 20:27:07 +00:00
|
|
|
return ServerError.Wrap(err)
|
2018-10-17 12:40:11 +01:00
|
|
|
}
|
|
|
|
|
2018-08-17 18:40:15 +01:00
|
|
|
pd := recv.GetPieceData()
|
|
|
|
if pd == nil {
|
|
|
|
return RetrieveError.New("PieceStore message is nil")
|
|
|
|
}
|
|
|
|
|
2018-12-17 15:23:02 +00:00
|
|
|
s.log.Debug("Retrieving",
|
|
|
|
zap.String("Piece ID", fmt.Sprint(pd.GetId())),
|
|
|
|
zap.Int64("Offset", pd.GetOffset()),
|
|
|
|
zap.Int64("Size", pd.GetPieceSize()),
|
|
|
|
)
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-10-23 17:03:35 +01:00
|
|
|
id, err := getNamespacedPieceID([]byte(pd.GetId()), getNamespace(authorization))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-17 18:40:15 +01:00
|
|
|
// Get path to data being retrieved
|
2019-01-11 11:26:39 +00:00
|
|
|
path, err := s.storage.PiecePath(id)
|
2018-08-17 18:40:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the path exists
|
|
|
|
fileInfo, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return RetrieveError.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the size specified
|
2018-11-20 17:09:35 +00:00
|
|
|
totalToRead := pd.GetPieceSize()
|
2018-08-17 18:40:15 +01:00
|
|
|
fileSize := fileInfo.Size()
|
|
|
|
|
|
|
|
// Read the entire file if specified -1 but make sure we do it from the correct offset
|
2018-11-20 17:09:35 +00:00
|
|
|
if pd.GetPieceSize() <= -1 || totalToRead+pd.GetOffset() > fileSize {
|
2018-08-17 18:40:15 +01:00
|
|
|
totalToRead = fileSize - pd.GetOffset()
|
|
|
|
}
|
|
|
|
|
2018-10-23 17:03:35 +01:00
|
|
|
retrieved, allocated, err := s.retrieveData(ctx, stream, id, pd.GetOffset(), totalToRead)
|
2018-08-17 18:40:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-24 16:09:37 +00:00
|
|
|
s.log.Info("Successfully retrieved",
|
2018-12-17 15:23:02 +00:00
|
|
|
zap.String("Piece ID", fmt.Sprint(pd.GetId())),
|
|
|
|
zap.Int64("Allocated", allocated),
|
|
|
|
zap.Int64("Retrieved", retrieved),
|
|
|
|
)
|
2018-08-17 18:40:15 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) retrieveData(ctx context.Context, stream pb.PieceStoreRoutes_RetrieveServer, id string, offset, length int64) (retrieved, allocated int64, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-01-11 11:26:39 +00:00
|
|
|
storeFile, err := s.storage.Reader(ctx, id, offset, length)
|
2018-08-17 18:40:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer utils.LogClose(storeFile)
|
|
|
|
|
|
|
|
writer := NewStreamWriter(s, stream)
|
2018-09-10 10:18:42 +01:00
|
|
|
allocationTracking := sync2.NewThrottle()
|
|
|
|
totalAllocated := int64(0)
|
|
|
|
|
|
|
|
// Bandwidth Allocation recv loop
|
|
|
|
go func() {
|
|
|
|
var lastTotal int64
|
|
|
|
var lastAllocation *pb.RenterBandwidthAllocation
|
|
|
|
defer func() {
|
|
|
|
if lastAllocation == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := s.DB.WriteBandwidthAllocToDB(lastAllocation)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: handle error properly
|
2018-12-17 15:23:02 +00:00
|
|
|
s.log.Error("WriteBandwidthAllocToDB Error:", zap.Error(err))
|
2018-09-10 10:18:42 +01:00
|
|
|
}
|
|
|
|
}()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-10 10:18:42 +01:00
|
|
|
for {
|
|
|
|
recv, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
allocationTracking.Fail(err)
|
|
|
|
return
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-11-24 02:46:53 +00:00
|
|
|
alloc := recv.GetBandwidthAllocation()
|
2018-09-10 10:18:42 +01:00
|
|
|
allocData := &pb.RenterBandwidthAllocation_Data{}
|
|
|
|
if err = proto.Unmarshal(alloc.GetData(), allocData); err != nil {
|
|
|
|
allocationTracking.Fail(err)
|
|
|
|
return
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2019-01-25 18:05:21 +00:00
|
|
|
if err = s.verifySignature(stream.Context(), alloc); err != nil {
|
2018-09-10 10:18:42 +01:00
|
|
|
allocationTracking.Fail(err)
|
|
|
|
return
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2019-01-06 18:51:01 +00:00
|
|
|
if allocData.GetPayerAllocation() == nil {
|
|
|
|
allocationTracking.Fail(StoreError.New("no payer bandwidth allocation"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pbaData := &pb.PayerBandwidthAllocation_Data{}
|
|
|
|
if err = proto.Unmarshal(allocData.GetPayerAllocation().GetData(), pbaData); err != nil {
|
|
|
|
allocationTracking.Fail(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-10 11:41:57 +00:00
|
|
|
if err = s.verifyPayerAllocation(pbaData, "GET"); err != nil {
|
2019-01-06 18:51:01 +00:00
|
|
|
allocationTracking.Fail(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-10 10:18:42 +01:00
|
|
|
// TODO: break when lastTotal >= allocData.GetPayer_allocation().GetData().GetMax_size()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-10 10:18:42 +01:00
|
|
|
if lastTotal > allocData.GetTotal() {
|
|
|
|
allocationTracking.Fail(fmt.Errorf("got lower allocation was %v got %v", lastTotal, allocData.GetTotal()))
|
|
|
|
return
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-10 10:18:42 +01:00
|
|
|
atomic.StoreInt64(&totalAllocated, allocData.GetTotal())
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-10 10:18:42 +01:00
|
|
|
if err = allocationTracking.Produce(allocData.GetTotal() - lastTotal); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lastAllocation = alloc
|
|
|
|
lastTotal = allocData.GetTotal()
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
2018-09-10 10:18:42 +01:00
|
|
|
}()
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-10 10:18:42 +01:00
|
|
|
// Data send loop
|
|
|
|
messageSize := int64(32 << 10)
|
|
|
|
used := int64(0)
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-10 10:18:42 +01:00
|
|
|
for used < length {
|
|
|
|
nextMessageSize, err := allocationTracking.ConsumeOrWait(messageSize)
|
|
|
|
if err != nil {
|
|
|
|
allocationTracking.Fail(err)
|
|
|
|
break
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
2018-09-10 10:18:42 +01:00
|
|
|
used += nextMessageSize
|
|
|
|
n, err := io.CopyN(writer, storeFile, nextMessageSize)
|
|
|
|
// correct errors when needed
|
|
|
|
if n != nextMessageSize {
|
|
|
|
if pErr := allocationTracking.Produce(nextMessageSize - n); pErr != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
used -= nextMessageSize - n
|
|
|
|
}
|
|
|
|
// break on error
|
2018-08-17 18:40:15 +01:00
|
|
|
if err != nil {
|
2018-09-10 10:18:42 +01:00
|
|
|
allocationTracking.Fail(err)
|
|
|
|
break
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 15:04:42 +01:00
|
|
|
// write to bandwidth usage table
|
|
|
|
if err = s.DB.AddBandwidthUsed(used); err != nil {
|
|
|
|
return retrieved, allocated, StoreError.New("failed to write bandwidth info to database: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-10 10:18:42 +01:00
|
|
|
// TODO: handle errors
|
|
|
|
// _ = stream.Close()
|
|
|
|
|
|
|
|
return used, atomic.LoadInt64(&totalAllocated), allocationTracking.Err()
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|