2018-07-03 09:35:01 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package ecclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2018-08-06 15:24:30 +01:00
|
|
|
"io/ioutil"
|
2018-08-02 16:12:19 +01:00
|
|
|
"sort"
|
2018-07-03 09:35:01 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2018-11-06 17:49:17 +00:00
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2"
|
2018-07-03 09:35:01 +01:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/eestream"
|
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-27 19:35:27 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-07-03 09:35:01 +01:00
|
|
|
"storj.io/storj/pkg/ranger"
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-07-03 09:35:01 +01:00
|
|
|
"storj.io/storj/pkg/transport"
|
2018-08-20 16:11:54 +01:00
|
|
|
"storj.io/storj/pkg/utils"
|
2018-07-03 09:35:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var mon = monkit.Package()
|
|
|
|
|
|
|
|
// Client defines an interface for storing erasure coded data to piece store nodes
|
|
|
|
type Client interface {
|
2018-09-18 05:39:06 +01:00
|
|
|
Put(ctx context.Context, nodes []*pb.Node, rs eestream.RedundancyStrategy,
|
2018-11-06 17:49:17 +00:00
|
|
|
pieceID psclient.PieceID, data io.Reader, expiration time.Time, pba *pb.PayerBandwidthAllocation, authorization *pb.SignedMessage) (successfulNodes []*pb.Node, err error)
|
2018-09-18 05:39:06 +01:00
|
|
|
Get(ctx context.Context, nodes []*pb.Node, es eestream.ErasureScheme,
|
2018-11-06 17:49:17 +00:00
|
|
|
pieceID psclient.PieceID, size int64, pba *pb.PayerBandwidthAllocation, authorization *pb.SignedMessage) (ranger.Ranger, error)
|
|
|
|
Delete(ctx context.Context, nodes []*pb.Node, pieceID psclient.PieceID, authorization *pb.SignedMessage) error
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
type psClientFunc func(context.Context, transport.Client, *pb.Node, int) (psclient.Client, error)
|
|
|
|
type psClientHelper func(context.Context, *pb.Node) (psclient.Client, error)
|
2018-07-03 09:35:01 +01:00
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
type ecClient struct {
|
|
|
|
transport transport.Client
|
|
|
|
memoryLimit int
|
|
|
|
newPSClientFunc psClientFunc
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
// NewClient from the given identity and max buffer memory
|
|
|
|
func NewClient(identity *provider.FullIdentity, memoryLimit int) Client {
|
|
|
|
tc := transport.NewClient(identity)
|
|
|
|
return &ecClient{
|
|
|
|
transport: tc,
|
|
|
|
memoryLimit: memoryLimit,
|
|
|
|
newPSClientFunc: psclient.NewPSClient,
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
func (ec *ecClient) newPSClient(ctx context.Context, n *pb.Node) (psclient.Client, error) {
|
|
|
|
return ec.newPSClientFunc(ctx, ec.transport, n, 0)
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
func (ec *ecClient) Put(ctx context.Context, nodes []*pb.Node, rs eestream.RedundancyStrategy,
|
2018-11-06 17:49:17 +00:00
|
|
|
pieceID psclient.PieceID, data io.Reader, expiration time.Time, pba *pb.PayerBandwidthAllocation, authorization *pb.SignedMessage) (successfulNodes []*pb.Node, err error) {
|
2018-07-03 09:35:01 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-09-27 11:45:19 +01:00
|
|
|
|
2018-07-03 09:35:01 +01:00
|
|
|
if len(nodes) != rs.TotalCount() {
|
2018-12-11 16:05:14 +00:00
|
|
|
return nil, Error.New("size of nodes slice (%d) does not match total count (%d) of erasure scheme", len(nodes), rs.TotalCount())
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
2018-12-11 16:05:14 +00:00
|
|
|
|
|
|
|
if nonNilCount(nodes) < rs.RepairThreshold() {
|
|
|
|
return nil, Error.New("number of non-nil nodes (%d) is less than repair threshold (%d) of erasure scheme", nonNilCount(nodes), rs.RepairThreshold())
|
|
|
|
}
|
|
|
|
|
2018-08-02 16:12:19 +01:00
|
|
|
if !unique(nodes) {
|
2018-09-27 11:45:19 +01:00
|
|
|
return nil, Error.New("duplicated nodes are not allowed")
|
2018-08-02 16:12:19 +01:00
|
|
|
}
|
2018-09-27 11:45:19 +01:00
|
|
|
|
2018-09-27 12:52:18 +01:00
|
|
|
padded := eestream.PadReader(ioutil.NopCloser(data), rs.StripeSize())
|
2018-11-06 17:49:17 +00:00
|
|
|
readers, err := eestream.EncodeReader(ctx, padded, rs, ec.memoryLimit)
|
2018-07-03 09:35:01 +01:00
|
|
|
if err != nil {
|
2018-09-27 11:45:19 +01:00
|
|
|
return nil, err
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
2018-09-27 11:45:19 +01:00
|
|
|
|
|
|
|
type info struct {
|
2018-10-04 14:52:12 +01:00
|
|
|
i int
|
2018-09-27 11:45:19 +01:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
infos := make(chan info, len(nodes))
|
|
|
|
|
2018-07-03 09:35:01 +01:00
|
|
|
for i, n := range nodes {
|
2018-10-16 16:53:25 +01:00
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
go func(i int, n *pb.Node) {
|
2018-10-15 15:10:20 +01:00
|
|
|
if n == nil {
|
|
|
|
_, err := io.Copy(ioutil.Discard, readers[i])
|
|
|
|
infos <- info{i: i, err: err}
|
2018-10-16 16:53:25 +01:00
|
|
|
return
|
2018-10-15 15:10:20 +01:00
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
derivedPieceID, err := pieceID.Derive(n.Id.Bytes())
|
2018-10-16 16:53:25 +01:00
|
|
|
|
2018-07-16 20:22:34 +01:00
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorf("Failed deriving piece id for %s: %v", pieceID, err)
|
2018-09-27 11:45:19 +01:00
|
|
|
infos <- info{i: i, err: err}
|
2018-07-16 20:22:34 +01:00
|
|
|
return
|
|
|
|
}
|
2018-11-06 17:49:17 +00:00
|
|
|
ps, err := ec.newPSClient(ctx, n)
|
2018-07-03 09:35:01 +01:00
|
|
|
if err != nil {
|
2018-10-18 15:48:57 +01:00
|
|
|
zap.S().Errorf("Failed dialing for putting piece %s -> %s to node %s: %v",
|
2018-11-29 18:39:27 +00:00
|
|
|
pieceID, derivedPieceID, n.Id, err)
|
2018-09-27 11:45:19 +01:00
|
|
|
infos <- info{i: i, err: err}
|
2018-07-03 09:35:01 +01:00
|
|
|
return
|
|
|
|
}
|
2018-10-30 16:24:46 +00:00
|
|
|
err = ps.Put(ctx, derivedPieceID, readers[i], expiration, pba, authorization)
|
2018-07-16 20:22:34 +01:00
|
|
|
// normally the bellow call should be deferred, but doing so fails
|
|
|
|
// randomly the unit tests
|
2018-08-20 16:11:54 +01:00
|
|
|
utils.LogClose(ps)
|
2018-09-27 11:45:19 +01:00
|
|
|
// io.ErrUnexpectedEOF means the piece upload was interrupted due to slow connection.
|
|
|
|
// No error logging for this case.
|
|
|
|
if err != nil && err != io.ErrUnexpectedEOF {
|
2018-07-06 09:51:13 +01:00
|
|
|
zap.S().Errorf("Failed putting piece %s -> %s to node %s: %v",
|
2018-11-29 18:39:27 +00:00
|
|
|
pieceID, derivedPieceID, n.Id, err)
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
2018-09-27 11:45:19 +01:00
|
|
|
infos <- info{i: i, err: err}
|
2018-07-03 09:35:01 +01:00
|
|
|
}(i, n)
|
|
|
|
}
|
2018-09-27 11:45:19 +01:00
|
|
|
|
|
|
|
successfulNodes = make([]*pb.Node, len(nodes))
|
|
|
|
var successfulCount int
|
|
|
|
for range nodes {
|
|
|
|
info := <-infos
|
|
|
|
if info.err == nil {
|
|
|
|
successfulNodes[info.i] = nodes[info.i]
|
|
|
|
successfulCount++
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
2018-09-27 11:45:19 +01:00
|
|
|
|
2018-10-04 14:52:12 +01:00
|
|
|
/* clean up the partially uploaded segment's pieces */
|
|
|
|
defer func() {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err = utils.CombineErrors(
|
|
|
|
Error.New("upload cancelled by user"),
|
2018-10-17 12:40:11 +01:00
|
|
|
ec.Delete(context.Background(), nodes, pieceID, authorization),
|
2018-10-04 14:52:12 +01:00
|
|
|
)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-09-27 11:45:19 +01:00
|
|
|
if successfulCount < rs.RepairThreshold() {
|
|
|
|
return nil, Error.New("successful puts (%d) less than repair threshold (%d)", successfulCount, rs.RepairThreshold())
|
|
|
|
}
|
|
|
|
|
|
|
|
return successfulNodes, nil
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
func (ec *ecClient) Get(ctx context.Context, nodes []*pb.Node, es eestream.ErasureScheme,
|
2018-11-06 17:49:17 +00:00
|
|
|
pieceID psclient.PieceID, size int64, pba *pb.PayerBandwidthAllocation, authorization *pb.SignedMessage) (rr ranger.Ranger, err error) {
|
2018-07-03 09:35:01 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-09-11 05:52:14 +01:00
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
if len(nodes) != es.TotalCount() {
|
|
|
|
return nil, Error.New("size of nodes slice (%d) does not match total count (%d) of erasure scheme", len(nodes), es.TotalCount())
|
|
|
|
}
|
|
|
|
|
|
|
|
if nonNilCount(nodes) < es.RequiredCount() {
|
|
|
|
return nil, Error.New("number of non-nil nodes (%d) is less than required count (%d) of erasure scheme", nonNilCount(nodes), es.RequiredCount())
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
2018-09-27 11:45:19 +01:00
|
|
|
|
2018-09-27 12:52:18 +01:00
|
|
|
paddedSize := calcPadded(size, es.StripeSize())
|
2018-08-06 15:24:30 +01:00
|
|
|
pieceSize := paddedSize / int64(es.RequiredCount())
|
2018-09-14 15:10:43 +01:00
|
|
|
rrs := map[int]ranger.Ranger{}
|
2018-09-27 11:45:19 +01:00
|
|
|
|
2018-07-03 09:35:01 +01:00
|
|
|
type rangerInfo struct {
|
|
|
|
i int
|
2018-09-14 15:10:43 +01:00
|
|
|
rr ranger.Ranger
|
2018-07-03 09:35:01 +01:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
ch := make(chan rangerInfo, len(nodes))
|
2018-09-27 11:45:19 +01:00
|
|
|
|
2018-07-03 09:35:01 +01:00
|
|
|
for i, n := range nodes {
|
2018-10-04 14:52:12 +01:00
|
|
|
if n == nil {
|
2018-09-27 11:45:19 +01:00
|
|
|
ch <- rangerInfo{i: i, rr: nil, err: nil}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
go func(i int, n *pb.Node) {
|
2018-11-29 18:39:27 +00:00
|
|
|
derivedPieceID, err := pieceID.Derive(n.Id.Bytes())
|
2018-07-16 20:22:34 +01:00
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorf("Failed deriving piece id for %s: %v", pieceID, err)
|
|
|
|
ch <- rangerInfo{i: i, rr: nil, err: err}
|
|
|
|
return
|
|
|
|
}
|
2018-09-08 14:52:19 +01:00
|
|
|
|
|
|
|
rr := &lazyPieceRanger{
|
2018-11-06 17:49:17 +00:00
|
|
|
newPSClientHelper: ec.newPSClient,
|
|
|
|
node: n,
|
|
|
|
id: derivedPieceID,
|
|
|
|
size: pieceSize,
|
|
|
|
pba: pba,
|
|
|
|
authorization: authorization,
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
2018-09-08 14:52:19 +01:00
|
|
|
|
2018-09-05 18:08:39 +01:00
|
|
|
ch <- rangerInfo{i: i, rr: rr, err: nil}
|
2018-07-03 09:35:01 +01:00
|
|
|
}(i, n)
|
|
|
|
}
|
2018-09-27 11:45:19 +01:00
|
|
|
|
2018-07-03 09:35:01 +01:00
|
|
|
for range nodes {
|
|
|
|
rri := <-ch
|
2018-09-27 11:45:19 +01:00
|
|
|
if rri.err == nil && rri.rr != nil {
|
2018-07-03 09:35:01 +01:00
|
|
|
rrs[rri.i] = rri.rr
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 11:45:19 +01:00
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
rr, err = eestream.Decode(rrs, es, ec.memoryLimit)
|
2018-08-06 15:24:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-27 11:45:19 +01:00
|
|
|
|
2018-09-14 15:10:43 +01:00
|
|
|
return eestream.Unpad(rr, int(paddedSize-size))
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
func (ec *ecClient) Delete(ctx context.Context, nodes []*pb.Node, pieceID psclient.PieceID, authorization *pb.SignedMessage) (err error) {
|
2018-07-03 09:35:01 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-09-27 11:45:19 +01:00
|
|
|
|
2018-07-03 09:35:01 +01:00
|
|
|
errs := make(chan error, len(nodes))
|
2018-09-27 11:45:19 +01:00
|
|
|
|
2018-07-03 09:35:01 +01:00
|
|
|
for _, n := range nodes {
|
2018-09-27 11:45:19 +01:00
|
|
|
if n == nil {
|
|
|
|
errs <- nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
go func(n *pb.Node) {
|
2018-11-29 18:39:27 +00:00
|
|
|
derivedPieceID, err := pieceID.Derive(n.Id.Bytes())
|
2018-07-16 20:22:34 +01:00
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorf("Failed deriving piece id for %s: %v", pieceID, err)
|
|
|
|
errs <- err
|
|
|
|
return
|
|
|
|
}
|
2018-11-06 17:49:17 +00:00
|
|
|
ps, err := ec.newPSClient(ctx, n)
|
2018-07-03 09:35:01 +01:00
|
|
|
if err != nil {
|
2018-10-18 15:48:57 +01:00
|
|
|
zap.S().Errorf("Failed dialing for deleting piece %s -> %s from node %s: %v",
|
2018-11-29 18:39:27 +00:00
|
|
|
pieceID, derivedPieceID, n.Id, err)
|
2018-07-03 09:35:01 +01:00
|
|
|
errs <- err
|
|
|
|
return
|
|
|
|
}
|
2018-10-17 12:40:11 +01:00
|
|
|
err = ps.Delete(ctx, derivedPieceID, authorization)
|
2018-07-16 20:22:34 +01:00
|
|
|
// normally the bellow call should be deferred, but doing so fails
|
|
|
|
// randomly the unit tests
|
2018-08-20 16:11:54 +01:00
|
|
|
utils.LogClose(ps)
|
2018-07-03 09:35:01 +01:00
|
|
|
if err != nil {
|
2018-07-06 09:51:13 +01:00
|
|
|
zap.S().Errorf("Failed deleting piece %s -> %s from node %s: %v",
|
2018-11-29 18:39:27 +00:00
|
|
|
pieceID, derivedPieceID, n.Id, err)
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
errs <- err
|
|
|
|
}(n)
|
|
|
|
}
|
2018-09-27 11:45:19 +01:00
|
|
|
|
2018-07-03 09:35:01 +01:00
|
|
|
allerrs := collectErrors(errs, len(nodes))
|
2018-09-27 11:45:19 +01:00
|
|
|
|
2018-07-03 09:35:01 +01:00
|
|
|
if len(allerrs) > 0 && len(allerrs) == len(nodes) {
|
|
|
|
return allerrs[0]
|
|
|
|
}
|
2018-09-27 11:45:19 +01:00
|
|
|
|
2018-07-03 09:35:01 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectErrors(errs <-chan error, size int) []error {
|
|
|
|
var result []error
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
err := <-errs
|
|
|
|
if err != nil {
|
|
|
|
result = append(result, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2018-07-16 20:22:34 +01:00
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
func unique(nodes []*pb.Node) bool {
|
2018-08-02 16:12:19 +01:00
|
|
|
if len(nodes) < 2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
ids := make(storj.NodeIDList, len(nodes))
|
2018-08-02 16:12:19 +01:00
|
|
|
for i, n := range nodes {
|
2018-11-29 18:39:27 +00:00
|
|
|
if n != nil {
|
|
|
|
ids[i] = n.Id
|
|
|
|
}
|
2018-08-02 16:12:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// sort the ids and check for identical neighbors
|
2018-11-29 18:39:27 +00:00
|
|
|
sort.Sort(ids)
|
|
|
|
// sort.Slice(ids, func(i, k int) bool { return ids[i].Less(ids[k]) })
|
2018-08-02 16:12:19 +01:00
|
|
|
for i := 1; i < len(ids); i++ {
|
2018-11-29 18:39:27 +00:00
|
|
|
if ids[i] != (storj.NodeID{}) && ids[i] == ids[i-1] {
|
2018-08-02 16:12:19 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2018-08-06 15:24:30 +01:00
|
|
|
|
|
|
|
func calcPadded(size int64, blockSize int) int64 {
|
|
|
|
mod := size % int64(blockSize)
|
|
|
|
if mod == 0 {
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
return size + int64(blockSize) - mod
|
|
|
|
}
|
2018-09-08 14:52:19 +01:00
|
|
|
|
|
|
|
type lazyPieceRanger struct {
|
2018-11-06 17:49:17 +00:00
|
|
|
ranger ranger.Ranger
|
|
|
|
newPSClientHelper psClientHelper
|
|
|
|
node *pb.Node
|
|
|
|
id psclient.PieceID
|
|
|
|
size int64
|
|
|
|
pba *pb.PayerBandwidthAllocation
|
|
|
|
authorization *pb.SignedMessage
|
2018-09-08 14:52:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size implements Ranger.Size
|
|
|
|
func (lr *lazyPieceRanger) Size() int64 {
|
|
|
|
return lr.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Range implements Ranger.Range to be lazily connected
|
|
|
|
func (lr *lazyPieceRanger) Range(ctx context.Context, offset, length int64) (io.ReadCloser, error) {
|
|
|
|
if lr.ranger == nil {
|
2018-11-06 17:49:17 +00:00
|
|
|
ps, err := lr.newPSClientHelper(ctx, lr.node)
|
2018-09-08 14:52:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-17 12:40:11 +01:00
|
|
|
ranger, err := ps.Get(ctx, lr.id, lr.size, lr.pba, lr.authorization)
|
2018-09-08 14:52:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lr.ranger = ranger
|
|
|
|
}
|
|
|
|
return lr.ranger.Range(ctx, offset, length)
|
|
|
|
}
|
2018-11-02 15:22:01 +00:00
|
|
|
|
2018-12-11 16:05:14 +00:00
|
|
|
func nonNilCount(nodes []*pb.Node) int {
|
2018-11-02 15:22:01 +00:00
|
|
|
total := 0
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node != nil {
|
|
|
|
total++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return total
|
|
|
|
}
|