2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metainfo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2019-06-19 13:02:37 +01:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2019-03-18 10:55:06 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2019-07-09 22:54:00 +01:00
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2"
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/auth/grpcauth"
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
"storj.io/storj/pkg/transport"
|
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
|
|
|
|
// Error is the errs class of standard metainfo errors
|
|
|
|
Error = errs.Class("metainfo error")
|
|
|
|
)
|
|
|
|
|
2019-06-25 16:36:23 +01:00
|
|
|
// Client creates a grpcClient
|
|
|
|
type Client struct {
|
2019-03-18 10:55:06 +00:00
|
|
|
client pb.MetainfoClient
|
2019-06-25 16:36:23 +01:00
|
|
|
conn *grpc.ClientConn
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListItem is a single item in a listing
|
|
|
|
type ListItem struct {
|
|
|
|
Path storj.Path
|
|
|
|
Pointer *pb.Pointer
|
|
|
|
IsPrefix bool
|
|
|
|
}
|
|
|
|
|
2019-06-25 16:36:23 +01:00
|
|
|
// New used as a public function
|
|
|
|
func New(client pb.MetainfoClient) *Client {
|
|
|
|
return &Client{
|
|
|
|
client: client,
|
|
|
|
}
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 16:36:23 +01:00
|
|
|
// Dial dials to metainfo endpoint with the specified api key.
|
|
|
|
func Dial(ctx context.Context, tc transport.Client, address string, apiKey string) (*Client, error) {
|
2019-05-29 14:14:25 +01:00
|
|
|
apiKeyInjector := grpcauth.NewAPIKeyInjector(apiKey)
|
2019-03-18 10:55:06 +00:00
|
|
|
conn, err := tc.DialAddress(
|
|
|
|
ctx,
|
|
|
|
address,
|
|
|
|
grpc.WithUnaryInterceptor(apiKeyInjector),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-06-25 16:36:23 +01:00
|
|
|
return &Client{
|
|
|
|
client: pb.NewMetainfoClient(conn),
|
|
|
|
conn: conn,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the dialed connection.
|
|
|
|
func (client *Client) Close() error {
|
|
|
|
if client.conn != nil {
|
|
|
|
return Error.Wrap(client.conn.Close())
|
|
|
|
}
|
|
|
|
return nil
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSegment requests the order limits for creating a new segment
|
2019-06-25 16:36:23 +01:00
|
|
|
func (client *Client) CreateSegment(ctx context.Context, bucket string, path storj.Path, segmentIndex int64, redundancy *pb.RedundancyScheme, maxEncryptedSegmentSize int64, expiration time.Time) (limits []*pb.AddressedOrderLimit, rootPieceID storj.PieceID, err error) {
|
2019-03-18 10:55:06 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-07-08 14:33:15 +01:00
|
|
|
response, err := client.client.CreateSegmentOld(ctx, &pb.SegmentWriteRequestOld{
|
2019-03-18 10:55:06 +00:00
|
|
|
Bucket: []byte(bucket),
|
|
|
|
Path: []byte(path),
|
|
|
|
Segment: segmentIndex,
|
|
|
|
Redundancy: redundancy,
|
|
|
|
MaxEncryptedSegmentSize: maxEncryptedSegmentSize,
|
2019-07-09 22:54:00 +01:00
|
|
|
Expiration: expiration,
|
2019-03-18 10:55:06 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, rootPieceID, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.GetAddressedLimits(), response.RootPieceId, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommitSegment requests to store the pointer for the segment
|
2019-07-01 16:54:11 +01:00
|
|
|
func (client *Client) CommitSegment(ctx context.Context, bucket string, path storj.Path, segmentIndex int64, pointer *pb.Pointer, originalLimits []*pb.OrderLimit) (savedPointer *pb.Pointer, err error) {
|
2019-03-18 10:55:06 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-07-08 14:33:15 +01:00
|
|
|
response, err := client.client.CommitSegmentOld(ctx, &pb.SegmentCommitRequestOld{
|
2019-03-18 10:55:06 +00:00
|
|
|
Bucket: []byte(bucket),
|
|
|
|
Path: []byte(path),
|
|
|
|
Segment: segmentIndex,
|
|
|
|
Pointer: pointer,
|
|
|
|
OriginalLimits: originalLimits,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.GetPointer(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SegmentInfo requests the pointer of a segment
|
2019-06-25 16:36:23 +01:00
|
|
|
func (client *Client) SegmentInfo(ctx context.Context, bucket string, path storj.Path, segmentIndex int64) (pointer *pb.Pointer, err error) {
|
2019-03-18 10:55:06 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-07-08 14:33:15 +01:00
|
|
|
response, err := client.client.SegmentInfoOld(ctx, &pb.SegmentInfoRequestOld{
|
2019-03-18 10:55:06 +00:00
|
|
|
Bucket: []byte(bucket),
|
|
|
|
Path: []byte(path),
|
|
|
|
Segment: segmentIndex,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if status.Code(err) == codes.NotFound {
|
|
|
|
return nil, storage.ErrKeyNotFound.Wrap(err)
|
|
|
|
}
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.GetPointer(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadSegment requests the order limits for reading a segment
|
2019-06-25 16:36:23 +01:00
|
|
|
func (client *Client) ReadSegment(ctx context.Context, bucket string, path storj.Path, segmentIndex int64) (pointer *pb.Pointer, limits []*pb.AddressedOrderLimit, err error) {
|
2019-03-18 10:55:06 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-07-08 14:33:15 +01:00
|
|
|
response, err := client.client.DownloadSegmentOld(ctx, &pb.SegmentDownloadRequestOld{
|
2019-03-18 10:55:06 +00:00
|
|
|
Bucket: []byte(bucket),
|
|
|
|
Path: []byte(path),
|
|
|
|
Segment: segmentIndex,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if status.Code(err) == codes.NotFound {
|
|
|
|
return nil, nil, storage.ErrKeyNotFound.Wrap(err)
|
|
|
|
}
|
|
|
|
return nil, nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.GetPointer(), sortLimits(response.GetAddressedLimits(), response.GetPointer()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortLimits sorts order limits and fill missing ones with nil values
|
|
|
|
func sortLimits(limits []*pb.AddressedOrderLimit, pointer *pb.Pointer) []*pb.AddressedOrderLimit {
|
|
|
|
sorted := make([]*pb.AddressedOrderLimit, pointer.GetRemote().GetRedundancy().GetTotal())
|
|
|
|
for _, piece := range pointer.GetRemote().GetRemotePieces() {
|
|
|
|
sorted[piece.GetPieceNum()] = getLimitByStorageNodeID(limits, piece.NodeId)
|
|
|
|
}
|
|
|
|
return sorted
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLimitByStorageNodeID(limits []*pb.AddressedOrderLimit, storageNodeID storj.NodeID) *pb.AddressedOrderLimit {
|
|
|
|
for _, limit := range limits {
|
|
|
|
if limit.GetLimit().StorageNodeId == storageNodeID {
|
|
|
|
return limit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSegment requests the order limits for deleting a segment
|
2019-06-25 16:36:23 +01:00
|
|
|
func (client *Client) DeleteSegment(ctx context.Context, bucket string, path storj.Path, segmentIndex int64) (limits []*pb.AddressedOrderLimit, err error) {
|
2019-03-18 10:55:06 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-07-08 14:33:15 +01:00
|
|
|
response, err := client.client.DeleteSegmentOld(ctx, &pb.SegmentDeleteRequestOld{
|
2019-03-18 10:55:06 +00:00
|
|
|
Bucket: []byte(bucket),
|
|
|
|
Path: []byte(path),
|
|
|
|
Segment: segmentIndex,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if status.Code(err) == codes.NotFound {
|
|
|
|
return nil, storage.ErrKeyNotFound.Wrap(err)
|
|
|
|
}
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.GetAddressedLimits(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListSegments lists the available segments
|
2019-06-25 16:36:23 +01:00
|
|
|
func (client *Client) ListSegments(ctx context.Context, bucket string, prefix, startAfter, endBefore storj.Path, recursive bool, limit int32, metaFlags uint32) (items []ListItem, more bool, err error) {
|
2019-03-18 10:55:06 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-07-08 14:33:15 +01:00
|
|
|
response, err := client.client.ListSegmentsOld(ctx, &pb.ListSegmentsRequestOld{
|
2019-03-18 10:55:06 +00:00
|
|
|
Bucket: []byte(bucket),
|
|
|
|
Prefix: []byte(prefix),
|
|
|
|
StartAfter: []byte(startAfter),
|
|
|
|
EndBefore: []byte(endBefore),
|
|
|
|
Recursive: recursive,
|
|
|
|
Limit: limit,
|
|
|
|
MetaFlags: metaFlags,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
list := response.GetItems()
|
|
|
|
items = make([]ListItem, len(list))
|
|
|
|
for i, item := range list {
|
|
|
|
items[i] = ListItem{
|
|
|
|
Path: storj.Path(item.GetPath()),
|
|
|
|
Pointer: item.GetPointer(),
|
|
|
|
IsPrefix: item.IsPrefix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return items, response.GetMore(), nil
|
|
|
|
}
|
2019-06-13 02:35:37 +01:00
|
|
|
|
2019-06-19 13:02:37 +01:00
|
|
|
// SetAttribution tries to set the attribution information on the bucket.
|
2019-06-25 16:36:23 +01:00
|
|
|
func (client *Client) SetAttribution(ctx context.Context, bucket string, partnerID uuid.UUID) (err error) {
|
2019-06-13 02:35:37 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-07-08 14:33:15 +01:00
|
|
|
_, err = client.client.SetAttributionOld(ctx, &pb.SetAttributionRequestOld{
|
2019-06-19 13:02:37 +01:00
|
|
|
PartnerId: partnerID[:], // TODO: implement storj.UUID that can be sent using pb
|
2019-06-13 02:35:37 +01:00
|
|
|
BucketName: []byte(bucket),
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2019-06-27 18:36:51 +01:00
|
|
|
|
|
|
|
// GetProjectInfo gets the ProjectInfo for the api key associated with the metainfo client.
|
|
|
|
func (client *Client) GetProjectInfo(ctx context.Context) (resp *pb.ProjectInfoResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
return client.client.ProjectInfo(ctx, &pb.ProjectInfoRequest{})
|
|
|
|
}
|