2018-08-17 18:40:15 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"storj.io/storj/pkg/piecestore"
|
|
|
|
"storj.io/storj/pkg/utils"
|
|
|
|
pb "storj.io/storj/protos/piecestore"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OK - Success!
|
|
|
|
const OK = "OK"
|
|
|
|
|
|
|
|
// StoreError is a type of error for failures in Server.Store()
|
|
|
|
var StoreError = errs.Class("store error")
|
|
|
|
|
|
|
|
// Store incoming data using piecestore
|
|
|
|
func (s *Server) Store(reqStream pb.PieceStoreRoutes_StoreServer) (err error) {
|
|
|
|
ctx := reqStream.Context()
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
// Receive id/ttl
|
|
|
|
recv, err := reqStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return StoreError.Wrap(err)
|
|
|
|
}
|
|
|
|
if recv == nil {
|
|
|
|
return StoreError.New("Error receiving Piece metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
pd := recv.GetPiecedata()
|
|
|
|
if pd == nil {
|
|
|
|
return StoreError.New("PieceStore message is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Storing %s...", pd.GetId())
|
|
|
|
|
|
|
|
if pd.GetId() == "" {
|
|
|
|
return StoreError.New("Piece ID not specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
total, err := s.storeData(ctx, reqStream, pd.GetId())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-13 15:30:45 +01:00
|
|
|
if err = s.DB.AddTTL(pd.GetId(), pd.GetExpirationUnixSec(), total); err != nil {
|
|
|
|
deleteErr := s.deleteByID(pd.GetId())
|
|
|
|
return StoreError.New("failed to write piece meta data to database: %v", utils.CombineErrors(err, deleteErr))
|
2018-09-11 13:40:45 +01:00
|
|
|
}
|
|
|
|
|
2018-08-17 18:40:15 +01:00
|
|
|
log.Printf("Successfully stored %s.", pd.GetId())
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
return reqStream.SendAndClose(&pb.PieceStoreSummary{Message: OK, TotalReceived: total})
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) storeData(ctx context.Context, stream pb.PieceStoreRoutes_StoreServer, id string) (total int64, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
// Delete data if we error
|
|
|
|
defer func() {
|
|
|
|
if err != nil && err != io.EOF {
|
2018-08-27 19:35:27 +01:00
|
|
|
if deleteErr := s.deleteByID(id); deleteErr != nil {
|
|
|
|
log.Printf("Failed on deleteByID in Store: %s", deleteErr.Error())
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Initialize file for storing data
|
|
|
|
storeFile, err := pstore.StoreWriter(id, s.DataDir)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer utils.LogClose(storeFile)
|
|
|
|
|
|
|
|
reader := NewStreamReader(s, stream)
|
|
|
|
|
|
|
|
defer func() {
|
2018-08-27 19:35:27 +01:00
|
|
|
baWriteErr := s.DB.WriteBandwidthAllocToDB(reader.bandwidthAllocation)
|
|
|
|
if baWriteErr != nil {
|
|
|
|
log.Printf("WriteBandwidthAllocToDB Error: %s\n", baWriteErr.Error())
|
2018-08-17 18:40:15 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
total, err = io.Copy(storeFile, reader)
|
|
|
|
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return total, nil
|
|
|
|
}
|