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"
|
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/eestream"
|
|
|
|
"storj.io/storj/pkg/piecestore/rpc/client"
|
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"
|
|
|
|
"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
|
|
|
proto "storj.io/storj/protos/overlay"
|
2018-08-17 18:40:15 +01:00
|
|
|
pb "storj.io/storj/protos/piecestore"
|
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-07-12 23:37:50 +01:00
|
|
|
Put(ctx context.Context, nodes []*proto.Node, rs eestream.RedundancyStrategy,
|
2018-07-03 09:35:01 +01:00
|
|
|
pieceID client.PieceID, data io.Reader, expiration time.Time) error
|
2018-07-12 23:37:50 +01:00
|
|
|
Get(ctx context.Context, nodes []*proto.Node, es eestream.ErasureScheme,
|
2018-07-03 09:35:01 +01:00
|
|
|
pieceID client.PieceID, size int64) (ranger.RangeCloser, error)
|
2018-07-12 23:37:50 +01:00
|
|
|
Delete(ctx context.Context, nodes []*proto.Node, pieceID client.PieceID) error
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type dialer interface {
|
2018-07-12 23:37:50 +01:00
|
|
|
dial(ctx context.Context, node *proto.Node) (ps client.PSClient, err error)
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type defaultDialer struct {
|
2018-08-27 19:35:27 +01:00
|
|
|
t transport.Client
|
|
|
|
identity *provider.FullIdentity
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
|
2018-07-12 23:37:50 +01:00
|
|
|
func (d *defaultDialer) dial(ctx context.Context, node *proto.Node) (ps client.PSClient, err error) {
|
2018-07-03 09:35:01 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
c, err := d.t.DialNode(ctx, node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-08-27 19:35:27 +01:00
|
|
|
return client.NewPSClient(c, 0, d.identity.Key)
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ecClient struct {
|
|
|
|
d dialer
|
|
|
|
mbm int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient from the given TransportClient and max buffer memory
|
2018-08-27 19:35:27 +01:00
|
|
|
func NewClient(identity *provider.FullIdentity, t transport.Client, mbm int) Client {
|
|
|
|
d := defaultDialer{identity: identity, t: t}
|
2018-08-17 18:40:15 +01:00
|
|
|
return &ecClient{d: &d, mbm: mbm}
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
|
2018-07-12 23:37:50 +01:00
|
|
|
func (ec *ecClient) Put(ctx context.Context, nodes []*proto.Node, rs eestream.RedundancyStrategy,
|
2018-07-03 09:35:01 +01:00
|
|
|
pieceID client.PieceID, data io.Reader, expiration time.Time) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
if len(nodes) != rs.TotalCount() {
|
2018-08-10 10:14:23 +01:00
|
|
|
return Error.New("number of nodes (%d) do not match total count (%d) of erasure scheme",
|
|
|
|
len(nodes), rs.TotalCount())
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
2018-08-02 16:12:19 +01:00
|
|
|
if !unique(nodes) {
|
|
|
|
return Error.New("duplicated nodes are not allowed")
|
|
|
|
}
|
2018-08-06 15:24:30 +01:00
|
|
|
padded := eestream.PadReader(ioutil.NopCloser(data), rs.DecodedBlockSize())
|
|
|
|
readers, err := eestream.EncodeReader(ctx, padded, rs, ec.mbm)
|
2018-07-03 09:35:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
errs := make(chan error, len(readers))
|
|
|
|
for i, n := range nodes {
|
2018-07-12 23:37:50 +01:00
|
|
|
go func(i int, n *proto.Node) {
|
2018-07-16 20:22:34 +01:00
|
|
|
derivedPieceID, err := pieceID.Derive([]byte(n.GetId()))
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorf("Failed deriving piece id for %s: %v", pieceID, err)
|
|
|
|
errs <- err
|
|
|
|
return
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
ps, err := ec.d.dial(ctx, n)
|
|
|
|
if err != nil {
|
2018-07-06 09:51:13 +01:00
|
|
|
zap.S().Errorf("Failed putting piece %s -> %s to node %s: %v",
|
|
|
|
pieceID, derivedPieceID, n.GetId(), err)
|
2018-07-03 09:35:01 +01:00
|
|
|
errs <- err
|
|
|
|
return
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
err = ps.Put(ctx, derivedPieceID, readers[i], expiration, &pb.PayerBandwidthAllocation{})
|
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 putting piece %s -> %s to node %s: %v",
|
|
|
|
pieceID, derivedPieceID, n.GetId(), err)
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
errs <- err
|
|
|
|
}(i, n)
|
|
|
|
}
|
|
|
|
allerrs := collectErrors(errs, len(readers))
|
|
|
|
sc := len(readers) - len(allerrs)
|
|
|
|
if sc < rs.MinimumThreshold() {
|
|
|
|
return Error.New(
|
|
|
|
"successful puts (%d) less than minimum threshold (%d)",
|
|
|
|
sc, rs.MinimumThreshold())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-12 23:37:50 +01:00
|
|
|
func (ec *ecClient) Get(ctx context.Context, nodes []*proto.Node, es eestream.ErasureScheme,
|
2018-07-03 09:35:01 +01:00
|
|
|
pieceID client.PieceID, size int64) (rr ranger.RangeCloser, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-09-11 05:52:14 +01:00
|
|
|
|
2018-07-03 09:35:01 +01:00
|
|
|
if len(nodes) != es.TotalCount() {
|
2018-09-11 05:52:14 +01:00
|
|
|
return nil, Error.New("number of nodes (%v) do not match total count (%v) of erasure scheme", len(nodes), es.TotalCount())
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
2018-08-06 15:24:30 +01:00
|
|
|
paddedSize := calcPadded(size, es.DecodedBlockSize())
|
|
|
|
pieceSize := paddedSize / int64(es.RequiredCount())
|
2018-07-03 09:35:01 +01:00
|
|
|
rrs := map[int]ranger.RangeCloser{}
|
|
|
|
type rangerInfo struct {
|
|
|
|
i int
|
|
|
|
rr ranger.RangeCloser
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
ch := make(chan rangerInfo, len(nodes))
|
|
|
|
for i, n := range nodes {
|
2018-07-12 23:37:50 +01:00
|
|
|
go func(i int, n *proto.Node) {
|
2018-07-16 20:22:34 +01:00
|
|
|
derivedPieceID, err := pieceID.Derive([]byte(n.GetId()))
|
|
|
|
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{
|
|
|
|
dialer: ec.d,
|
|
|
|
node: n,
|
|
|
|
id: derivedPieceID,
|
|
|
|
size: pieceSize,
|
|
|
|
pba: &pb.PayerBandwidthAllocation{},
|
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)
|
|
|
|
}
|
|
|
|
for range nodes {
|
|
|
|
rri := <-ch
|
|
|
|
if rri.err == nil {
|
|
|
|
rrs[rri.i] = rri.rr
|
|
|
|
}
|
|
|
|
}
|
2018-08-06 15:24:30 +01:00
|
|
|
rr, err = eestream.Decode(rrs, es, ec.mbm)
|
|
|
|
if err != nil {
|
2018-09-05 18:08:39 +01:00
|
|
|
for _, rr := range rrs {
|
|
|
|
_ = rr.Close()
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
uprr, err := eestream.Unpad(rr, int(paddedSize-size))
|
|
|
|
if err != nil {
|
|
|
|
_ = rr.Close()
|
2018-08-06 15:24:30 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-05 18:08:39 +01:00
|
|
|
return uprr, nil
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
|
2018-07-12 23:37:50 +01:00
|
|
|
func (ec *ecClient) Delete(ctx context.Context, nodes []*proto.Node, pieceID client.PieceID) (err error) {
|
2018-07-03 09:35:01 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
errs := make(chan error, len(nodes))
|
|
|
|
for _, n := range nodes {
|
2018-07-12 23:37:50 +01:00
|
|
|
go func(n *proto.Node) {
|
2018-07-16 20:22:34 +01:00
|
|
|
derivedPieceID, err := pieceID.Derive([]byte(n.GetId()))
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorf("Failed deriving piece id for %s: %v", pieceID, err)
|
|
|
|
errs <- err
|
|
|
|
return
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
ps, err := ec.d.dial(ctx, n)
|
|
|
|
if err != nil {
|
2018-07-06 09:51:13 +01:00
|
|
|
zap.S().Errorf("Failed deleting piece %s -> %s from node %s: %v",
|
|
|
|
pieceID, derivedPieceID, n.GetId(), err)
|
2018-07-03 09:35:01 +01:00
|
|
|
errs <- err
|
|
|
|
return
|
|
|
|
}
|
2018-07-06 09:51:13 +01:00
|
|
|
err = ps.Delete(ctx, derivedPieceID)
|
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",
|
|
|
|
pieceID, derivedPieceID, n.GetId(), err)
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
errs <- err
|
|
|
|
}(n)
|
|
|
|
}
|
|
|
|
allerrs := collectErrors(errs, len(nodes))
|
|
|
|
if len(allerrs) > 0 && len(allerrs) == len(nodes) {
|
|
|
|
return allerrs[0]
|
|
|
|
}
|
|
|
|
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-08-02 16:12:19 +01:00
|
|
|
func unique(nodes []*proto.Node) bool {
|
|
|
|
if len(nodes) < 2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := make([]string, len(nodes))
|
|
|
|
for i, n := range nodes {
|
|
|
|
ids[i] = n.GetId()
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort the ids and check for identical neighbors
|
|
|
|
sort.Strings(ids)
|
|
|
|
for i := 1; i < len(ids); i++ {
|
|
|
|
if ids[i] == ids[i-1] {
|
|
|
|
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 {
|
|
|
|
ranger ranger.RangeCloser
|
|
|
|
dialer dialer
|
|
|
|
node *proto.Node
|
|
|
|
id client.PieceID
|
|
|
|
size int64
|
|
|
|
pba *pb.PayerBandwidthAllocation
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size implements Ranger.Size
|
|
|
|
func (lr *lazyPieceRanger) Size() int64 {
|
|
|
|
return lr.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size implements Ranger.Close
|
|
|
|
func (lr *lazyPieceRanger) Close() error {
|
|
|
|
if lr.ranger == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return lr.ranger.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
ps, err := lr.dialer.dial(ctx, lr.node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ranger, err := ps.Get(ctx, lr.id, lr.size, lr.pba)
|
|
|
|
// no ps.CloseConn() here, the connection will be closed by
|
|
|
|
// the caller using RangeCloser.Close
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lr.ranger = ranger
|
|
|
|
}
|
|
|
|
return lr.ranger.Range(ctx, offset, length)
|
|
|
|
}
|