2019-03-28 20:09:23 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package orders
|
|
|
|
|
|
|
|
import (
|
2019-06-25 16:58:42 +01:00
|
|
|
"bytes"
|
2019-03-28 20:09:23 +00:00
|
|
|
"context"
|
2019-07-11 23:44:47 +01:00
|
|
|
"math"
|
2019-03-28 20:09:23 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/pb"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/pkg/signing"
|
2019-03-28 20:09:23 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/satellite/overlay"
|
|
|
|
"storj.io/storj/uplink/eestream"
|
2019-03-28 20:09:23 +00:00
|
|
|
)
|
|
|
|
|
2019-06-21 11:38:40 +01:00
|
|
|
// Config is a configuration struct for orders Service.
|
|
|
|
type Config struct {
|
2019-08-19 14:36:11 +01:00
|
|
|
Expiration time.Duration `help:"how long until an order expires" default:"168h"` // 7 days
|
|
|
|
SettlementBatchSize int `help:"how many orders to batch per transaction" default:"250"`
|
2019-06-21 11:38:40 +01:00
|
|
|
}
|
|
|
|
|
2019-03-28 20:09:23 +00:00
|
|
|
// Service for creating order limits.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2019-03-28 20:09:23 +00:00
|
|
|
type Service struct {
|
2019-07-11 23:44:47 +01:00
|
|
|
log *zap.Logger
|
|
|
|
satellite signing.Signer
|
2019-08-06 17:35:59 +01:00
|
|
|
overlay *overlay.Service
|
2019-07-11 23:44:47 +01:00
|
|
|
orders DB
|
|
|
|
satelliteAddress *pb.NodeAddress
|
|
|
|
orderExpiration time.Duration
|
|
|
|
repairMaxExcessRateOptimalThreshold float64
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates new service for creating order limits.
|
2019-07-11 23:44:47 +01:00
|
|
|
func NewService(
|
2019-08-06 17:35:59 +01:00
|
|
|
log *zap.Logger, satellite signing.Signer, overlay *overlay.Service,
|
2019-07-11 23:44:47 +01:00
|
|
|
orders DB, orderExpiration time.Duration, satelliteAddress *pb.NodeAddress,
|
|
|
|
repairMaxExcessRateOptimalThreshold float64,
|
|
|
|
) *Service {
|
2019-03-28 20:09:23 +00:00
|
|
|
return &Service{
|
2019-07-11 23:44:47 +01:00
|
|
|
log: log,
|
|
|
|
satellite: satellite,
|
2019-08-06 17:35:59 +01:00
|
|
|
overlay: overlay,
|
2019-07-11 23:44:47 +01:00
|
|
|
orders: orders,
|
|
|
|
satelliteAddress: satelliteAddress,
|
|
|
|
orderExpiration: orderExpiration,
|
|
|
|
repairMaxExcessRateOptimalThreshold: repairMaxExcessRateOptimalThreshold,
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyOrderLimitSignature verifies that the signature inside order limit belongs to the satellite.
|
2019-07-01 16:54:11 +01:00
|
|
|
func (service *Service) VerifyOrderLimitSignature(ctx context.Context, signed *pb.OrderLimit) (err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-05 14:47:01 +01:00
|
|
|
return signing.VerifyOrderLimitSignature(ctx, service.satellite, signed)
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
func (service *Service) createSerial(ctx context.Context) (_ storj.SerialNumber, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-28 20:09:23 +00:00
|
|
|
uuid, err := uuid.New()
|
|
|
|
if err != nil {
|
|
|
|
return storj.SerialNumber{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
return storj.SerialNumber(*uuid), nil
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
func (service *Service) saveSerial(ctx context.Context, serialNumber storj.SerialNumber, bucketID []byte, expiresAt time.Time) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-28 20:09:23 +00:00
|
|
|
return service.orders.CreateSerialInfo(ctx, serialNumber, bucketID, expiresAt)
|
|
|
|
}
|
|
|
|
|
2019-06-25 16:58:42 +01:00
|
|
|
func (service *Service) updateBandwidth(ctx context.Context, projectID uuid.UUID, bucketName []byte, addressedOrderLimits ...*pb.AddressedOrderLimit) (err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-04-01 21:14:58 +01:00
|
|
|
if len(addressedOrderLimits) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-10 15:58:28 +01:00
|
|
|
|
2019-04-01 21:14:58 +01:00
|
|
|
var action pb.PieceAction
|
2019-06-10 15:58:28 +01:00
|
|
|
|
|
|
|
var bucketAllocation int64
|
|
|
|
var nodesAllocation int64
|
|
|
|
nodes := make([]storj.NodeID, 0, len(addressedOrderLimits))
|
|
|
|
|
2019-04-01 21:14:58 +01:00
|
|
|
for _, addressedOrderLimit := range addressedOrderLimits {
|
|
|
|
if addressedOrderLimit != nil {
|
|
|
|
orderLimit := addressedOrderLimit.Limit
|
2019-06-10 15:58:28 +01:00
|
|
|
|
|
|
|
if nodesAllocation == 0 {
|
|
|
|
nodesAllocation = orderLimit.Limit
|
|
|
|
} else if nodesAllocation != orderLimit.Limit {
|
|
|
|
return Error.New("inconsistent allocations had %d got %d", nodesAllocation, orderLimit.Limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes = append(nodes, orderLimit.StorageNodeId)
|
2019-04-01 21:14:58 +01:00
|
|
|
action = orderLimit.Action
|
2019-06-10 15:58:28 +01:00
|
|
|
|
|
|
|
bucketAllocation += orderLimit.Limit
|
2019-04-01 21:14:58 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-10 15:58:28 +01:00
|
|
|
|
2019-04-09 20:12:58 +01:00
|
|
|
now := time.Now().UTC()
|
2019-04-04 16:20:59 +01:00
|
|
|
intervalStart := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
|
2019-04-01 21:14:58 +01:00
|
|
|
|
2019-06-12 16:00:29 +01:00
|
|
|
// TODO: all of this below should be a single db transaction. in fact, this whole function should probably be part of an existing transaction
|
2019-06-25 16:58:42 +01:00
|
|
|
if err := service.orders.UpdateBucketBandwidthAllocation(ctx, projectID, bucketName, action, bucketAllocation, intervalStart); err != nil {
|
2019-06-10 15:58:28 +01:00
|
|
|
return Error.Wrap(err)
|
2019-04-01 21:14:58 +01:00
|
|
|
}
|
2019-06-10 15:58:28 +01:00
|
|
|
|
|
|
|
if err := service.orders.UpdateStoragenodeBandwidthAllocation(ctx, nodes, action, nodesAllocation, intervalStart); err != nil {
|
|
|
|
return Error.Wrap(err)
|
2019-04-01 21:14:58 +01:00
|
|
|
}
|
2019-06-10 15:58:28 +01:00
|
|
|
|
2019-04-01 21:14:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-28 20:09:23 +00:00
|
|
|
// CreateGetOrderLimits creates the order limits for downloading the pieces of pointer.
|
2019-07-11 21:51:40 +01:00
|
|
|
func (service *Service) CreateGetOrderLimits(ctx context.Context, bucketID []byte, pointer *pb.Pointer) (_ []*pb.AddressedOrderLimit, privateKey storj.PiecePrivateKey, err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-07-11 21:51:40 +01:00
|
|
|
|
2019-03-28 20:09:23 +00:00
|
|
|
rootPieceID := pointer.GetRemote().RootPieceId
|
2019-07-09 22:54:00 +01:00
|
|
|
pieceExpiration := pointer.ExpirationDate
|
|
|
|
orderExpiration := time.Now().Add(service.orderExpiration)
|
2019-03-28 20:09:23 +00:00
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
piecePublicKey, piecePrivateKey, err := storj.NewPieceKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-03-28 20:09:23 +00:00
|
|
|
serialNumber, err := service.createSerial(ctx)
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
redundancy, err := eestream.NewRedundancyStrategyFromProto(pointer.GetRemote().GetRedundancy())
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pieceSize := eestream.CalcPieceSize(pointer.GetSegmentSize(), redundancy)
|
|
|
|
|
|
|
|
var combinedErrs error
|
|
|
|
var limits []*pb.AddressedOrderLimit
|
|
|
|
for _, piece := range pointer.GetRemote().GetRemotePieces() {
|
2019-08-06 17:35:59 +01:00
|
|
|
node, err := service.overlay.Get(ctx, piece.NodeId)
|
2019-03-28 20:09:23 +00:00
|
|
|
if err != nil {
|
2019-08-06 17:35:59 +01:00
|
|
|
service.log.Debug("error getting node from overlay", zap.Error(err))
|
2019-03-28 20:09:23 +00:00
|
|
|
combinedErrs = errs.Combine(combinedErrs, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-06-24 15:46:10 +01:00
|
|
|
if node.Disqualified != nil {
|
|
|
|
service.log.Debug("node is disqualified", zap.Stringer("ID", node.Id))
|
2019-08-21 17:30:29 +01:00
|
|
|
combinedErrs = errs.Combine(combinedErrs, overlay.ErrNodeDisqualified.New("%v", node.Id))
|
2019-06-24 15:46:10 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-06 17:35:59 +01:00
|
|
|
if !service.overlay.IsOnline(node) {
|
2019-06-18 00:37:44 +01:00
|
|
|
service.log.Debug("node is offline", zap.Stringer("ID", node.Id))
|
2019-08-21 17:30:29 +01:00
|
|
|
combinedErrs = errs.Combine(combinedErrs, overlay.ErrNodeOffline.New("%v", node.Id))
|
2019-03-29 08:53:43 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-01 16:54:11 +01:00
|
|
|
orderLimit, err := signing.SignOrderLimit(ctx, service.satellite, &pb.OrderLimit{
|
2019-06-21 10:19:52 +01:00
|
|
|
SerialNumber: serialNumber,
|
|
|
|
SatelliteId: service.satellite.ID(),
|
|
|
|
SatelliteAddress: service.satelliteAddress,
|
2019-07-11 21:51:40 +01:00
|
|
|
UplinkPublicKey: piecePublicKey,
|
2019-06-21 10:19:52 +01:00
|
|
|
StorageNodeId: piece.NodeId,
|
2019-07-03 17:53:15 +01:00
|
|
|
PieceId: rootPieceID.Derive(piece.NodeId, piece.PieceNum),
|
2019-06-21 10:19:52 +01:00
|
|
|
Action: pb.PieceAction_GET,
|
|
|
|
Limit: pieceSize,
|
2019-07-09 22:54:00 +01:00
|
|
|
PieceExpiration: pieceExpiration,
|
2019-07-02 17:06:12 +01:00
|
|
|
OrderCreation: time.Now(),
|
2019-06-21 10:19:52 +01:00
|
|
|
OrderExpiration: orderExpiration,
|
2019-03-28 20:09:23 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
limits = append(limits, &pb.AddressedOrderLimit{
|
|
|
|
Limit: orderLimit,
|
|
|
|
StorageNodeAddress: node.Address,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(limits) < redundancy.RequiredCount() {
|
|
|
|
err = Error.New("not enough nodes available: got %d, required %d", len(limits), redundancy.RequiredCount())
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, errs.Combine(err, combinedErrs)
|
2019-03-29 09:53:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 22:54:00 +01:00
|
|
|
err = service.saveSerial(ctx, serialNumber, bucketID, orderExpiration)
|
2019-03-29 09:53:53 +00:00
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-29 09:53:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 16:58:42 +01:00
|
|
|
projectID, bucketName, err := SplitBucketID(bucketID)
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-06-25 16:58:42 +01:00
|
|
|
}
|
|
|
|
if err := service.updateBandwidth(ctx, *projectID, bucketName, limits...); err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-04-01 21:14:58 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
return limits, piecePrivateKey, nil
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePutOrderLimits creates the order limits for uploading pieces to nodes.
|
2019-07-11 21:51:40 +01:00
|
|
|
func (service *Service) CreatePutOrderLimits(ctx context.Context, bucketID []byte, nodes []*pb.Node, expiration time.Time, maxPieceSize int64) (_ storj.PieceID, _ []*pb.AddressedOrderLimit, privateKey storj.PiecePrivateKey, err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-07-09 22:54:00 +01:00
|
|
|
|
|
|
|
orderExpiration := time.Now().Add(service.orderExpiration)
|
2019-03-28 20:09:23 +00:00
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
piecePublicKey, piecePrivateKey, err := storj.NewPieceKey()
|
|
|
|
if err != nil {
|
|
|
|
return storj.PieceID{}, nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-03-28 20:09:23 +00:00
|
|
|
serialNumber, err := service.createSerial(ctx)
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return storj.PieceID{}, nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rootPieceID := storj.NewPieceID()
|
|
|
|
limits := make([]*pb.AddressedOrderLimit, len(nodes))
|
|
|
|
var pieceNum int32
|
|
|
|
for _, node := range nodes {
|
2019-07-01 16:54:11 +01:00
|
|
|
orderLimit, err := signing.SignOrderLimit(ctx, service.satellite, &pb.OrderLimit{
|
2019-06-21 10:19:52 +01:00
|
|
|
SerialNumber: serialNumber,
|
|
|
|
SatelliteId: service.satellite.ID(),
|
|
|
|
SatelliteAddress: service.satelliteAddress,
|
2019-07-11 21:51:40 +01:00
|
|
|
UplinkPublicKey: piecePublicKey,
|
2019-06-21 10:19:52 +01:00
|
|
|
StorageNodeId: node.Id,
|
2019-07-03 17:53:15 +01:00
|
|
|
PieceId: rootPieceID.Derive(node.Id, pieceNum),
|
2019-06-21 10:19:52 +01:00
|
|
|
Action: pb.PieceAction_PUT,
|
|
|
|
Limit: maxPieceSize,
|
|
|
|
PieceExpiration: expiration,
|
2019-07-02 17:06:12 +01:00
|
|
|
OrderCreation: time.Now(),
|
2019-06-21 10:19:52 +01:00
|
|
|
OrderExpiration: orderExpiration,
|
2019-03-28 20:09:23 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return storj.PieceID{}, nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
limits[pieceNum] = &pb.AddressedOrderLimit{
|
|
|
|
Limit: orderLimit,
|
|
|
|
StorageNodeAddress: node.Address,
|
|
|
|
}
|
|
|
|
pieceNum++
|
|
|
|
}
|
|
|
|
|
2019-07-09 22:54:00 +01:00
|
|
|
err = service.saveSerial(ctx, serialNumber, bucketID, orderExpiration)
|
2019-03-29 09:53:53 +00:00
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return storj.PieceID{}, nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-29 09:53:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 16:58:42 +01:00
|
|
|
projectID, bucketName, err := SplitBucketID(bucketID)
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return storj.PieceID{}, nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-06-25 16:58:42 +01:00
|
|
|
}
|
|
|
|
if err := service.updateBandwidth(ctx, *projectID, bucketName, limits...); err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return storj.PieceID{}, nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-04-01 21:14:58 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
return rootPieceID, limits, piecePrivateKey, nil
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDeleteOrderLimits creates the order limits for deleting the pieces of pointer.
|
2019-07-11 21:51:40 +01:00
|
|
|
func (service *Service) CreateDeleteOrderLimits(ctx context.Context, bucketID []byte, pointer *pb.Pointer) (_ []*pb.AddressedOrderLimit, _ storj.PiecePrivateKey, err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-07-11 21:51:40 +01:00
|
|
|
|
2019-03-28 20:09:23 +00:00
|
|
|
rootPieceID := pointer.GetRemote().RootPieceId
|
2019-07-09 22:54:00 +01:00
|
|
|
pieceExpiration := pointer.ExpirationDate
|
|
|
|
orderExpiration := time.Now().Add(service.orderExpiration)
|
2019-03-28 20:09:23 +00:00
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
piecePublicKey, piecePrivateKey, err := storj.NewPieceKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-03-28 20:09:23 +00:00
|
|
|
serialNumber, err := service.createSerial(ctx)
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var combinedErrs error
|
|
|
|
var limits []*pb.AddressedOrderLimit
|
|
|
|
for _, piece := range pointer.GetRemote().GetRemotePieces() {
|
2019-08-06 17:35:59 +01:00
|
|
|
node, err := service.overlay.Get(ctx, piece.NodeId)
|
2019-03-28 20:09:23 +00:00
|
|
|
if err != nil {
|
2019-08-06 17:35:59 +01:00
|
|
|
service.log.Error("error getting node from overlay", zap.Error(err))
|
2019-03-28 20:09:23 +00:00
|
|
|
combinedErrs = errs.Combine(combinedErrs, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-06-24 15:46:10 +01:00
|
|
|
if node.Disqualified != nil {
|
|
|
|
service.log.Debug("node is disqualified", zap.Stringer("ID", node.Id))
|
2019-08-21 17:30:29 +01:00
|
|
|
combinedErrs = errs.Combine(combinedErrs, overlay.ErrNodeDisqualified.New("%v", node.Id))
|
2019-06-24 15:46:10 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-06 17:35:59 +01:00
|
|
|
if !service.overlay.IsOnline(node) {
|
2019-06-18 00:37:44 +01:00
|
|
|
service.log.Debug("node is offline", zap.Stringer("ID", node.Id))
|
2019-08-21 17:30:29 +01:00
|
|
|
combinedErrs = errs.Combine(combinedErrs, overlay.ErrNodeOffline.New("%v", node.Id))
|
2019-03-29 08:53:43 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-01 16:54:11 +01:00
|
|
|
orderLimit, err := signing.SignOrderLimit(ctx, service.satellite, &pb.OrderLimit{
|
2019-06-21 10:19:52 +01:00
|
|
|
SerialNumber: serialNumber,
|
|
|
|
SatelliteId: service.satellite.ID(),
|
|
|
|
SatelliteAddress: service.satelliteAddress,
|
2019-07-11 21:51:40 +01:00
|
|
|
UplinkPublicKey: piecePublicKey,
|
2019-06-21 10:19:52 +01:00
|
|
|
StorageNodeId: piece.NodeId,
|
2019-07-03 17:53:15 +01:00
|
|
|
PieceId: rootPieceID.Derive(piece.NodeId, piece.PieceNum),
|
2019-06-21 10:19:52 +01:00
|
|
|
Action: pb.PieceAction_DELETE,
|
|
|
|
Limit: 0,
|
2019-07-09 22:54:00 +01:00
|
|
|
PieceExpiration: pieceExpiration,
|
2019-07-02 17:06:12 +01:00
|
|
|
OrderCreation: time.Now(),
|
2019-06-21 10:19:52 +01:00
|
|
|
OrderExpiration: orderExpiration,
|
2019-03-28 20:09:23 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
limits = append(limits, &pb.AddressedOrderLimit{
|
|
|
|
Limit: orderLimit,
|
|
|
|
StorageNodeAddress: node.Address,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(limits) == 0 {
|
|
|
|
err = Error.New("failed creating order limits for all nodes")
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, errs.Combine(err, combinedErrs)
|
2019-03-29 09:53:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 22:54:00 +01:00
|
|
|
err = service.saveSerial(ctx, serialNumber, bucketID, orderExpiration)
|
2019-03-29 09:53:53 +00:00
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-29 09:53:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
return limits, piecePrivateKey, nil
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateAuditOrderLimits creates the order limits for auditing the pieces of pointer.
|
2019-07-11 21:51:40 +01:00
|
|
|
func (service *Service) CreateAuditOrderLimits(ctx context.Context, bucketID []byte, pointer *pb.Pointer, skip map[storj.NodeID]bool) (_ []*pb.AddressedOrderLimit, _ storj.PiecePrivateKey, err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-28 20:09:23 +00:00
|
|
|
rootPieceID := pointer.GetRemote().RootPieceId
|
|
|
|
redundancy := pointer.GetRemote().GetRedundancy()
|
|
|
|
shareSize := redundancy.GetErasureShareSize()
|
|
|
|
totalPieces := redundancy.GetTotal()
|
2019-07-11 21:51:40 +01:00
|
|
|
|
2019-07-09 22:54:00 +01:00
|
|
|
pieceExpiration := pointer.ExpirationDate
|
|
|
|
orderExpiration := time.Now().Add(service.orderExpiration)
|
2019-03-28 20:09:23 +00:00
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
piecePublicKey, piecePrivateKey, err := storj.NewPieceKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-03-28 20:09:23 +00:00
|
|
|
serialNumber, err := service.createSerial(ctx)
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var combinedErrs error
|
|
|
|
var limitsCount int32
|
|
|
|
limits := make([]*pb.AddressedOrderLimit, totalPieces)
|
|
|
|
for _, piece := range pointer.GetRemote().GetRemotePieces() {
|
2019-05-27 12:13:47 +01:00
|
|
|
if skip[piece.NodeId] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-06 17:35:59 +01:00
|
|
|
node, err := service.overlay.Get(ctx, piece.NodeId)
|
2019-03-28 20:09:23 +00:00
|
|
|
if err != nil {
|
2019-08-06 17:35:59 +01:00
|
|
|
service.log.Error("error getting node from overlay", zap.Error(err))
|
2019-03-28 20:09:23 +00:00
|
|
|
combinedErrs = errs.Combine(combinedErrs, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-06-20 14:56:04 +01:00
|
|
|
if node.Disqualified != nil {
|
|
|
|
service.log.Debug("node is disqualified", zap.Stringer("ID", node.Id))
|
2019-08-21 17:30:29 +01:00
|
|
|
combinedErrs = errs.Combine(combinedErrs, overlay.ErrNodeDisqualified.New("%v", node.Id))
|
2019-06-20 14:56:04 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-06 17:35:59 +01:00
|
|
|
if !service.overlay.IsOnline(node) {
|
2019-06-18 00:37:44 +01:00
|
|
|
service.log.Debug("node is offline", zap.Stringer("ID", node.Id))
|
2019-08-21 17:30:29 +01:00
|
|
|
combinedErrs = errs.Combine(combinedErrs, overlay.ErrNodeOffline.New("%v", node.Id))
|
2019-03-29 08:53:43 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-01 16:54:11 +01:00
|
|
|
orderLimit, err := signing.SignOrderLimit(ctx, service.satellite, &pb.OrderLimit{
|
2019-06-21 10:19:52 +01:00
|
|
|
SerialNumber: serialNumber,
|
|
|
|
SatelliteId: service.satellite.ID(),
|
|
|
|
SatelliteAddress: service.satelliteAddress,
|
2019-07-11 21:51:40 +01:00
|
|
|
UplinkPublicKey: piecePublicKey,
|
2019-06-21 10:19:52 +01:00
|
|
|
StorageNodeId: piece.NodeId,
|
2019-07-03 17:53:15 +01:00
|
|
|
PieceId: rootPieceID.Derive(piece.NodeId, piece.PieceNum),
|
2019-06-21 10:19:52 +01:00
|
|
|
Action: pb.PieceAction_GET_AUDIT,
|
|
|
|
Limit: int64(shareSize),
|
2019-07-09 22:54:00 +01:00
|
|
|
PieceExpiration: pieceExpiration,
|
2019-07-02 17:06:12 +01:00
|
|
|
OrderCreation: time.Now(),
|
2019-06-21 10:19:52 +01:00
|
|
|
OrderExpiration: orderExpiration,
|
2019-03-28 20:09:23 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
limits[piece.GetPieceNum()] = &pb.AddressedOrderLimit{
|
|
|
|
Limit: orderLimit,
|
|
|
|
StorageNodeAddress: node.Address,
|
|
|
|
}
|
|
|
|
limitsCount++
|
|
|
|
}
|
2019-03-29 08:53:43 +00:00
|
|
|
|
2019-03-28 20:09:23 +00:00
|
|
|
if limitsCount < redundancy.GetMinReq() {
|
|
|
|
err = Error.New("not enough nodes available: got %d, required %d", limitsCount, redundancy.GetMinReq())
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, errs.Combine(err, combinedErrs)
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 22:54:00 +01:00
|
|
|
err = service.saveSerial(ctx, serialNumber, bucketID, orderExpiration)
|
2019-03-29 09:53:53 +00:00
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-29 09:53:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 16:58:42 +01:00
|
|
|
projectID, bucketName, err := SplitBucketID(bucketID)
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return limits, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-06-25 16:58:42 +01:00
|
|
|
}
|
|
|
|
if err := service.updateBandwidth(ctx, *projectID, bucketName, limits...); err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-04-01 21:14:58 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
return limits, piecePrivateKey, nil
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 12:13:47 +01:00
|
|
|
// CreateAuditOrderLimit creates an order limit for auditing a single the piece from a pointer.
|
2019-07-11 21:51:40 +01:00
|
|
|
func (service *Service) CreateAuditOrderLimit(ctx context.Context, bucketID []byte, nodeID storj.NodeID, pieceNum int32, rootPieceID storj.PieceID, shareSize int32) (limit *pb.AddressedOrderLimit, _ storj.PiecePrivateKey, err error) {
|
2019-07-03 17:53:15 +01:00
|
|
|
// TODO reduce number of params ?
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-07-09 22:54:00 +01:00
|
|
|
|
|
|
|
orderExpiration := time.Now().Add(service.orderExpiration)
|
2019-05-27 12:13:47 +01:00
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
piecePublicKey, piecePrivateKey, err := storj.NewPieceKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-05-27 12:13:47 +01:00
|
|
|
serialNumber, err := service.createSerial(ctx)
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-05-27 12:13:47 +01:00
|
|
|
}
|
|
|
|
|
2019-08-06 17:35:59 +01:00
|
|
|
node, err := service.overlay.Get(ctx, nodeID)
|
2019-05-27 12:13:47 +01:00
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-05-27 12:13:47 +01:00
|
|
|
}
|
|
|
|
|
2019-06-24 15:46:10 +01:00
|
|
|
if node.Disqualified != nil {
|
2019-08-21 17:30:29 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, overlay.ErrNodeDisqualified.New("%v", nodeID)
|
2019-06-24 15:46:10 +01:00
|
|
|
}
|
|
|
|
|
2019-08-06 17:35:59 +01:00
|
|
|
if !service.overlay.IsOnline(node) {
|
2019-08-21 17:30:29 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, overlay.ErrNodeOffline.New("%v", nodeID)
|
2019-05-27 12:13:47 +01:00
|
|
|
}
|
|
|
|
|
2019-07-01 16:54:11 +01:00
|
|
|
orderLimit, err := signing.SignOrderLimit(ctx, service.satellite, &pb.OrderLimit{
|
2019-06-21 10:19:52 +01:00
|
|
|
SerialNumber: serialNumber,
|
|
|
|
SatelliteId: service.satellite.ID(),
|
|
|
|
SatelliteAddress: service.satelliteAddress,
|
2019-07-11 21:51:40 +01:00
|
|
|
UplinkPublicKey: piecePublicKey,
|
2019-06-21 10:19:52 +01:00
|
|
|
StorageNodeId: nodeID,
|
2019-07-03 17:53:15 +01:00
|
|
|
PieceId: rootPieceID.Derive(nodeID, pieceNum),
|
2019-06-21 10:19:52 +01:00
|
|
|
Action: pb.PieceAction_GET_AUDIT,
|
|
|
|
Limit: int64(shareSize),
|
2019-07-02 17:06:12 +01:00
|
|
|
OrderCreation: time.Now(),
|
2019-06-21 10:19:52 +01:00
|
|
|
OrderExpiration: orderExpiration,
|
2019-05-27 12:13:47 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-05-27 12:13:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
limit = &pb.AddressedOrderLimit{
|
|
|
|
Limit: orderLimit,
|
|
|
|
StorageNodeAddress: node.Address,
|
|
|
|
}
|
|
|
|
|
2019-07-09 22:54:00 +01:00
|
|
|
err = service.saveSerial(ctx, serialNumber, bucketID, orderExpiration)
|
2019-05-27 12:13:47 +01:00
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-05-27 12:13:47 +01:00
|
|
|
}
|
|
|
|
|
2019-06-25 16:58:42 +01:00
|
|
|
projectID, bucketName, err := SplitBucketID(bucketID)
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return limit, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-06-25 16:58:42 +01:00
|
|
|
}
|
|
|
|
if err := service.updateBandwidth(ctx, *projectID, bucketName, limit); err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-05-27 12:13:47 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
return limit, piecePrivateKey, nil
|
2019-05-27 12:13:47 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:44:47 +01:00
|
|
|
// CreateGetRepairOrderLimits creates the order limits for downloading the
|
|
|
|
// healthy pieces of pointer as the source for repair.
|
|
|
|
//
|
|
|
|
// The length of the returned orders slice is the total number of pieces of the
|
|
|
|
// segment, setting to null the ones which don't correspond to a healthy piece.
|
2019-03-28 20:09:23 +00:00
|
|
|
// CreateGetRepairOrderLimits creates the order limits for downloading the healthy pieces of pointer as the source for repair.
|
2019-07-11 21:51:40 +01:00
|
|
|
func (service *Service) CreateGetRepairOrderLimits(ctx context.Context, bucketID []byte, pointer *pb.Pointer, healthy []*pb.RemotePiece) (_ []*pb.AddressedOrderLimit, _ storj.PiecePrivateKey, err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-07-11 21:51:40 +01:00
|
|
|
|
2019-03-28 20:09:23 +00:00
|
|
|
rootPieceID := pointer.GetRemote().RootPieceId
|
2019-04-03 14:17:32 +01:00
|
|
|
redundancy, err := eestream.NewRedundancyStrategyFromProto(pointer.GetRemote().GetRedundancy())
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-04-03 14:17:32 +01:00
|
|
|
}
|
2019-07-11 21:51:40 +01:00
|
|
|
|
2019-04-03 14:17:32 +01:00
|
|
|
pieceSize := eestream.CalcPieceSize(pointer.GetSegmentSize(), redundancy)
|
|
|
|
totalPieces := redundancy.TotalCount()
|
2019-07-09 22:54:00 +01:00
|
|
|
pieceExpiration := pointer.ExpirationDate
|
|
|
|
orderExpiration := time.Now().Add(service.orderExpiration)
|
2019-03-28 20:09:23 +00:00
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
piecePublicKey, piecePrivateKey, err := storj.NewPieceKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-03-28 20:09:23 +00:00
|
|
|
serialNumber, err := service.createSerial(ctx)
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 08:53:43 +00:00
|
|
|
var combinedErrs error
|
2019-04-03 14:17:32 +01:00
|
|
|
var limitsCount int
|
2019-03-28 20:09:23 +00:00
|
|
|
limits := make([]*pb.AddressedOrderLimit, totalPieces)
|
|
|
|
for _, piece := range healthy {
|
2019-08-06 17:35:59 +01:00
|
|
|
node, err := service.overlay.Get(ctx, piece.NodeId)
|
2019-03-28 20:09:23 +00:00
|
|
|
if err != nil {
|
2019-08-06 17:35:59 +01:00
|
|
|
service.log.Error("error getting node from the overlay", zap.Error(err))
|
2019-03-29 08:53:43 +00:00
|
|
|
combinedErrs = errs.Combine(combinedErrs, err)
|
|
|
|
continue
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 15:46:10 +01:00
|
|
|
if node.Disqualified != nil {
|
|
|
|
service.log.Debug("node is disqualified", zap.Stringer("ID", node.Id))
|
2019-08-21 17:30:29 +01:00
|
|
|
combinedErrs = errs.Combine(combinedErrs, overlay.ErrNodeDisqualified.New("%v", node.Id))
|
2019-06-24 15:46:10 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-06 17:35:59 +01:00
|
|
|
if !service.overlay.IsOnline(node) {
|
2019-06-18 00:37:44 +01:00
|
|
|
service.log.Debug("node is offline", zap.Stringer("ID", node.Id))
|
2019-08-21 17:30:29 +01:00
|
|
|
combinedErrs = errs.Combine(combinedErrs, overlay.ErrNodeOffline.New("%v", node.Id))
|
2019-03-29 08:53:43 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-01 16:54:11 +01:00
|
|
|
orderLimit, err := signing.SignOrderLimit(ctx, service.satellite, &pb.OrderLimit{
|
2019-06-21 10:19:52 +01:00
|
|
|
SerialNumber: serialNumber,
|
|
|
|
SatelliteId: service.satellite.ID(),
|
|
|
|
SatelliteAddress: service.satelliteAddress,
|
2019-07-11 21:51:40 +01:00
|
|
|
UplinkPublicKey: piecePublicKey,
|
2019-06-21 10:19:52 +01:00
|
|
|
StorageNodeId: piece.NodeId,
|
2019-07-03 17:53:15 +01:00
|
|
|
PieceId: rootPieceID.Derive(piece.NodeId, piece.PieceNum),
|
2019-06-21 10:19:52 +01:00
|
|
|
Action: pb.PieceAction_GET_REPAIR,
|
|
|
|
Limit: pieceSize,
|
2019-07-09 22:54:00 +01:00
|
|
|
PieceExpiration: pieceExpiration,
|
2019-07-02 17:06:12 +01:00
|
|
|
OrderCreation: time.Now(),
|
2019-06-21 10:19:52 +01:00
|
|
|
OrderExpiration: orderExpiration,
|
2019-03-28 20:09:23 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
limits[piece.GetPieceNum()] = &pb.AddressedOrderLimit{
|
|
|
|
Limit: orderLimit,
|
|
|
|
StorageNodeAddress: node.Address,
|
|
|
|
}
|
2019-04-03 14:17:32 +01:00
|
|
|
limitsCount++
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 14:17:32 +01:00
|
|
|
if limitsCount < redundancy.RequiredCount() {
|
|
|
|
err = Error.New("not enough nodes available: got %d, required %d", limitsCount, redundancy.RequiredCount())
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, errs.Combine(err, combinedErrs)
|
2019-03-29 08:53:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 22:54:00 +01:00
|
|
|
err = service.saveSerial(ctx, serialNumber, bucketID, orderExpiration)
|
2019-03-29 09:53:53 +00:00
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-29 09:53:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 16:58:42 +01:00
|
|
|
projectID, bucketName, err := SplitBucketID(bucketID)
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return limits, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-06-25 16:58:42 +01:00
|
|
|
}
|
|
|
|
if err := service.updateBandwidth(ctx, *projectID, bucketName, limits...); err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-04-01 21:14:58 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
return limits, piecePrivateKey, nil
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePutRepairOrderLimits creates the order limits for uploading the repaired pieces of pointer to newNodes.
|
2019-07-11 21:51:40 +01:00
|
|
|
func (service *Service) CreatePutRepairOrderLimits(ctx context.Context, bucketID []byte, pointer *pb.Pointer, getOrderLimits []*pb.AddressedOrderLimit, newNodes []*pb.Node) (_ []*pb.AddressedOrderLimit, _ storj.PiecePrivateKey, err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-07-09 22:54:00 +01:00
|
|
|
orderExpiration := time.Now().Add(service.orderExpiration)
|
2019-03-28 20:09:23 +00:00
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
piecePublicKey, piecePrivateKey, err := storj.NewPieceKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-03-28 20:09:23 +00:00
|
|
|
serialNumber, err := service.createSerial(ctx)
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:44:47 +01:00
|
|
|
var limits []*pb.AddressedOrderLimit
|
|
|
|
{ // Create the order limits for being used to upload the repaired pieces
|
|
|
|
redundancy, err := eestream.NewRedundancyStrategyFromProto(pointer.GetRemote().GetRedundancy())
|
|
|
|
if err != nil {
|
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:44:47 +01:00
|
|
|
totalPieces := redundancy.TotalCount()
|
|
|
|
limits = make([]*pb.AddressedOrderLimit, totalPieces)
|
|
|
|
|
|
|
|
totalPiecesAfterRepair := int(
|
|
|
|
math.Ceil(
|
|
|
|
float64(redundancy.OptimalThreshold()) * (1 + service.repairMaxExcessRateOptimalThreshold),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if totalPiecesAfterRepair > totalPieces {
|
|
|
|
totalPiecesAfterRepair = totalPieces
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:44:47 +01:00
|
|
|
var numCurrentPieces int
|
|
|
|
for _, o := range getOrderLimits {
|
|
|
|
if o != nil {
|
|
|
|
numCurrentPieces++
|
|
|
|
}
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:44:47 +01:00
|
|
|
var (
|
|
|
|
totalPiecesToRepair = totalPiecesAfterRepair - numCurrentPieces
|
|
|
|
rootPieceID = pointer.GetRemote().RootPieceId
|
|
|
|
pieceSize = eestream.CalcPieceSize(pointer.GetSegmentSize(), redundancy)
|
|
|
|
pieceNum int32
|
|
|
|
)
|
|
|
|
for _, node := range newNodes {
|
|
|
|
for int(pieceNum) < totalPieces && getOrderLimits[pieceNum] != nil {
|
|
|
|
pieceNum++
|
|
|
|
}
|
|
|
|
|
|
|
|
if int(pieceNum) >= totalPieces { // should not happen
|
|
|
|
return nil, storj.PiecePrivateKey{}, Error.New("piece num greater than total pieces: %d >= %d", pieceNum, totalPieces)
|
|
|
|
}
|
|
|
|
|
|
|
|
orderLimit, err := signing.SignOrderLimit(ctx, service.satellite, &pb.OrderLimit{
|
|
|
|
SerialNumber: serialNumber,
|
|
|
|
SatelliteId: service.satellite.ID(),
|
|
|
|
SatelliteAddress: service.satelliteAddress,
|
|
|
|
UplinkPublicKey: piecePublicKey,
|
|
|
|
StorageNodeId: node.Id,
|
|
|
|
PieceId: rootPieceID.Derive(node.Id, pieceNum),
|
|
|
|
Action: pb.PieceAction_PUT_REPAIR,
|
|
|
|
Limit: pieceSize,
|
|
|
|
PieceExpiration: pointer.ExpirationDate,
|
|
|
|
OrderCreation: time.Now(),
|
|
|
|
OrderExpiration: orderExpiration,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
limits[pieceNum] = &pb.AddressedOrderLimit{
|
|
|
|
Limit: orderLimit,
|
|
|
|
StorageNodeAddress: node.Address,
|
|
|
|
}
|
|
|
|
pieceNum++
|
|
|
|
totalPiecesToRepair--
|
|
|
|
|
|
|
|
if totalPiecesToRepair == 0 {
|
|
|
|
break
|
|
|
|
}
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 22:54:00 +01:00
|
|
|
err = service.saveSerial(ctx, serialNumber, bucketID, orderExpiration)
|
2019-03-29 09:53:53 +00:00
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-03-29 09:53:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 16:58:42 +01:00
|
|
|
projectID, bucketName, err := SplitBucketID(bucketID)
|
|
|
|
if err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return limits, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-06-25 16:58:42 +01:00
|
|
|
}
|
|
|
|
if err := service.updateBandwidth(ctx, *projectID, bucketName, limits...); err != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
return nil, storj.PiecePrivateKey{}, Error.Wrap(err)
|
2019-04-01 21:14:58 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
return limits, piecePrivateKey, nil
|
2019-03-28 20:09:23 +00:00
|
|
|
}
|
2019-04-05 08:42:56 +01:00
|
|
|
|
|
|
|
// UpdateGetInlineOrder updates amount of inline GET bandwidth for given bucket
|
2019-06-25 16:58:42 +01:00
|
|
|
func (service *Service) UpdateGetInlineOrder(ctx context.Context, projectID uuid.UUID, bucketName []byte, amount int64) (err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-04-09 20:12:58 +01:00
|
|
|
now := time.Now().UTC()
|
2019-04-05 08:42:56 +01:00
|
|
|
intervalStart := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
|
|
|
|
|
2019-06-25 16:58:42 +01:00
|
|
|
return service.orders.UpdateBucketBandwidthInline(ctx, projectID, bucketName, pb.PieceAction_GET, amount, intervalStart)
|
2019-04-05 08:42:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePutInlineOrder updates amount of inline PUT bandwidth for given bucket
|
2019-06-25 16:58:42 +01:00
|
|
|
func (service *Service) UpdatePutInlineOrder(ctx context.Context, projectID uuid.UUID, bucketName []byte, amount int64) (err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-04-09 20:12:58 +01:00
|
|
|
now := time.Now().UTC()
|
2019-04-05 08:42:56 +01:00
|
|
|
intervalStart := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
|
|
|
|
|
2019-06-25 16:58:42 +01:00
|
|
|
return service.orders.UpdateBucketBandwidthInline(ctx, projectID, bucketName, pb.PieceAction_PUT, amount, intervalStart)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SplitBucketID takes a bucketID, splits on /, and returns a projectID and bucketName
|
|
|
|
func SplitBucketID(bucketID []byte) (projectID *uuid.UUID, bucketName []byte, err error) {
|
|
|
|
pathElements := bytes.Split(bucketID, []byte("/"))
|
|
|
|
if len(pathElements) > 1 {
|
|
|
|
bucketName = pathElements[1]
|
|
|
|
}
|
|
|
|
projectID, err = uuid.Parse(string(pathElements[0]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return projectID, bucketName, nil
|
2019-04-05 08:42:56 +01:00
|
|
|
}
|