2018-05-15 01:31:26 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-07-06 20:43:53 +01:00
|
|
|
package pointerdb
|
2018-05-15 01:31:26 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
"github.com/golang/protobuf/proto"
|
2018-07-27 07:02:59 +01:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
2018-08-27 18:28:16 +01:00
|
|
|
"github.com/zeebo/errs"
|
2018-05-15 01:31:26 +01:00
|
|
|
"go.uber.org/zap"
|
2018-05-30 03:47:40 +01:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2018-08-22 16:07:00 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-05-15 01:31:26 +01:00
|
|
|
|
2018-10-04 22:00:19 +01:00
|
|
|
"storj.io/storj/pkg/overlay"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-09-10 15:23:08 +01:00
|
|
|
"storj.io/storj/pkg/pointerdb/auth"
|
2018-07-30 19:57:50 +01:00
|
|
|
"storj.io/storj/pkg/storage/meta"
|
2018-06-13 19:22:32 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-05-15 01:31:26 +01:00
|
|
|
)
|
|
|
|
|
2018-08-22 16:07:00 +01:00
|
|
|
var (
|
2018-08-27 18:28:16 +01:00
|
|
|
mon = monkit.Package()
|
2018-08-27 15:04:46 +01:00
|
|
|
segmentError = errs.Class("segment error")
|
2018-08-22 16:07:00 +01:00
|
|
|
)
|
|
|
|
|
2018-05-15 01:31:26 +01:00
|
|
|
// Server implements the network state RPC service
|
|
|
|
type Server struct {
|
2018-06-13 19:22:32 +01:00
|
|
|
DB storage.KeyValueStore
|
2018-05-15 01:31:26 +01:00
|
|
|
logger *zap.Logger
|
2018-08-27 15:04:46 +01:00
|
|
|
config Config
|
2018-10-04 22:00:19 +01:00
|
|
|
cache *overlay.Cache
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates instance of Server
|
2018-10-04 22:00:19 +01:00
|
|
|
func NewServer(db storage.KeyValueStore, cache *overlay.Cache, logger *zap.Logger, c Config) *Server {
|
2018-05-15 01:31:26 +01:00
|
|
|
return &Server{
|
|
|
|
DB: db,
|
|
|
|
logger: logger,
|
2018-08-27 15:04:46 +01:00
|
|
|
config: c,
|
2018-10-04 22:00:19 +01:00
|
|
|
cache: cache,
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
func (s *Server) validateAuth(APIKey []byte) error {
|
|
|
|
if !auth.ValidateAPIKey(string(APIKey)) {
|
2018-08-27 18:28:16 +01:00
|
|
|
s.logger.Error("unauthorized request: ", zap.Error(status.Errorf(codes.Unauthenticated, "Invalid API credential")))
|
|
|
|
return status.Errorf(codes.Unauthenticated, "Invalid API credential")
|
2018-06-04 17:45:07 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-27 15:04:46 +01:00
|
|
|
func (s *Server) validateSegment(req *pb.PutRequest) error {
|
|
|
|
min := s.config.MinInlineSegmentSize
|
|
|
|
max := s.config.MaxInlineSegmentSize
|
|
|
|
inlineSize := len(req.GetPointer().InlineSegment)
|
|
|
|
remote := req.GetPointer().Remote
|
|
|
|
|
|
|
|
if remote != nil && req.GetPointer().GetSize() < min {
|
|
|
|
return segmentError.New("inline segment size %d less than minimum allowed %d", inlineSize, min)
|
|
|
|
}
|
|
|
|
|
|
|
|
if inlineSize > max {
|
|
|
|
return segmentError.New("inline segment size %d greater than maximum allowed %d", inlineSize, max)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-29 21:06:25 +01:00
|
|
|
// Put formats and hands off a key/value (path/pointer) to be saved to boltdb
|
2018-07-27 07:02:59 +01:00
|
|
|
func (s *Server) Put(ctx context.Context, req *pb.PutRequest) (resp *pb.PutResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-05-15 01:31:26 +01:00
|
|
|
|
2018-08-27 15:04:46 +01:00
|
|
|
err = s.validateSegment(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
2018-08-27 18:28:16 +01:00
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
if err = s.validateAuth(req.GetAPIKey()); err != nil {
|
2018-06-04 17:45:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
// Update the pointer with the creation date
|
|
|
|
req.GetPointer().CreationDate = ptypes.TimestampNow()
|
|
|
|
|
|
|
|
pointerBytes, err := proto.Marshal(req.GetPointer())
|
2018-05-30 03:47:40 +01:00
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("err marshaling pointer", zap.Error(err))
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
// TODO(kaloyan): make sure that we know we are overwriting the pointer!
|
|
|
|
// In such case we should delete the pieces of the old segment if it was
|
|
|
|
// a remote one.
|
|
|
|
if err = s.DB.Put([]byte(req.GetPath()), pointerBytes); err != nil {
|
2018-05-30 03:47:40 +01:00
|
|
|
s.logger.Error("err putting pointer", zap.Error(err))
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.PutResponse{}, nil
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get formats and hands off a file path to get from boltdb
|
2018-07-27 07:02:59 +01:00
|
|
|
func (s *Server) Get(ctx context.Context, req *pb.GetRequest) (resp *pb.GetResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-05-15 01:31:26 +01:00
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
if err = s.validateAuth(req.GetAPIKey()); err != nil {
|
2018-06-04 17:45:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
pointerBytes, err := s.DB.Get([]byte(req.GetPath()))
|
2018-05-15 01:31:26 +01:00
|
|
|
if err != nil {
|
2018-08-14 16:22:29 +01:00
|
|
|
if storage.ErrKeyNotFound.Has(err) {
|
|
|
|
return nil, status.Errorf(codes.NotFound, err.Error())
|
|
|
|
}
|
2018-07-27 07:02:59 +01:00
|
|
|
s.logger.Error("err getting pointer", zap.Error(err))
|
2018-05-30 03:47:40 +01:00
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
2018-10-04 22:00:19 +01:00
|
|
|
pointer := &pb.Pointer{}
|
|
|
|
err = proto.Unmarshal(pointerBytes, pointer)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("Error unmarshaling pointer")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nodes := []*pb.Node{}
|
|
|
|
|
|
|
|
var r = &pb.GetResponse{
|
|
|
|
Pointer: pointer,
|
|
|
|
Nodes: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.config.Overlay || pointer.Remote == nil {
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, piece := range pointer.Remote.RemotePieces {
|
|
|
|
node, err := s.cache.Get(ctx, piece.NodeId)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("Error getting node from cache")
|
|
|
|
}
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
r = &pb.GetResponse{
|
|
|
|
Pointer: pointer,
|
|
|
|
Nodes: nodes,
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
// List returns all Path keys in the Pointers bucket
|
2018-07-27 07:02:59 +01:00
|
|
|
func (s *Server) List(ctx context.Context, req *pb.ListRequest) (resp *pb.ListResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-05-15 01:31:26 +01:00
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
if err = s.validateAuth(req.APIKey); err != nil {
|
2018-06-04 17:45:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
var prefix storage.Key
|
|
|
|
if req.Prefix != "" {
|
|
|
|
prefix = storage.Key(req.Prefix)
|
|
|
|
if prefix[len(prefix)-1] != storage.Delimiter {
|
|
|
|
prefix = append(prefix, storage.Delimiter)
|
|
|
|
}
|
|
|
|
}
|
2018-07-27 07:02:59 +01:00
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
rawItems, more, err := storage.ListV2(s.DB, storage.ListOptions{
|
|
|
|
Prefix: prefix, //storage.Key(req.Prefix),
|
|
|
|
StartAfter: storage.Key(req.StartAfter),
|
|
|
|
EndBefore: storage.Key(req.EndBefore),
|
|
|
|
Recursive: req.Recursive,
|
|
|
|
Limit: int(req.Limit),
|
|
|
|
IncludeValue: req.MetaFlags != meta.None,
|
|
|
|
})
|
2018-07-27 07:02:59 +01:00
|
|
|
if err != nil {
|
2018-09-07 15:20:15 +01:00
|
|
|
return nil, status.Errorf(codes.Internal, "ListV2: %v", err)
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var items []*pb.ListResponse_Item
|
2018-09-07 15:20:15 +01:00
|
|
|
for _, rawItem := range rawItems {
|
2018-09-09 07:34:23 +01:00
|
|
|
items = append(items, s.createListItem(rawItem, req.MetaFlags))
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.ListResponse{Items: items, More: more}, nil
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
// createListItem creates a new list item with the given path. It also adds
|
|
|
|
// the metadata according to the given metaFlags.
|
2018-09-09 07:34:23 +01:00
|
|
|
func (s *Server) createListItem(rawItem storage.ListItem, metaFlags uint32) *pb.ListResponse_Item {
|
2018-09-07 15:20:15 +01:00
|
|
|
item := &pb.ListResponse_Item{
|
2018-09-09 07:34:23 +01:00
|
|
|
Path: rawItem.Key.String(),
|
2018-09-07 15:20:15 +01:00
|
|
|
IsPrefix: rawItem.IsPrefix,
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
2018-09-07 15:20:15 +01:00
|
|
|
if item.IsPrefix {
|
|
|
|
return item
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
err := s.setMetadata(item, rawItem.Value, metaFlags)
|
2018-07-27 07:02:59 +01:00
|
|
|
if err != nil {
|
|
|
|
s.logger.Warn("err retrieving metadata", zap.Error(err))
|
|
|
|
}
|
|
|
|
return item
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
// getMetadata adds the metadata to the given item pointer according to the
|
|
|
|
// given metaFlags
|
2018-09-07 15:20:15 +01:00
|
|
|
func (s *Server) setMetadata(item *pb.ListResponse_Item, data []byte, metaFlags uint32) (err error) {
|
|
|
|
if metaFlags == meta.None || len(data) == 0 {
|
2018-07-27 07:02:59 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pr := &pb.Pointer{}
|
2018-09-07 15:20:15 +01:00
|
|
|
err = proto.Unmarshal(data, pr)
|
2018-07-27 07:02:59 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
2018-07-27 07:02:59 +01:00
|
|
|
|
2018-07-30 19:57:50 +01:00
|
|
|
// Start with an empty pointer to and add only what's requested in
|
|
|
|
// metaFlags to safe to transfer payload
|
|
|
|
item.Pointer = &pb.Pointer{}
|
|
|
|
if metaFlags&meta.Modified != 0 {
|
|
|
|
item.Pointer.CreationDate = pr.GetCreationDate()
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
2018-07-30 19:57:50 +01:00
|
|
|
if metaFlags&meta.Expiration != 0 {
|
|
|
|
item.Pointer.ExpirationDate = pr.GetExpirationDate()
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
2018-07-30 19:57:50 +01:00
|
|
|
if metaFlags&meta.Size != 0 {
|
|
|
|
item.Pointer.Size = pr.GetSize()
|
|
|
|
}
|
|
|
|
if metaFlags&meta.UserDefined != 0 {
|
|
|
|
item.Pointer.Metadata = pr.GetMetadata()
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
|
|
|
|
2018-05-15 01:31:26 +01:00
|
|
|
// Delete formats and hands off a file path to delete from boltdb
|
2018-07-27 07:02:59 +01:00
|
|
|
func (s *Server) Delete(ctx context.Context, req *pb.DeleteRequest) (resp *pb.DeleteResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-07-06 20:43:53 +01:00
|
|
|
s.logger.Debug("entering pointerdb delete")
|
2018-05-15 01:31:26 +01:00
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
if err = s.validateAuth(req.GetAPIKey()); err != nil {
|
2018-06-04 17:45:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
err = s.DB.Delete([]byte(req.GetPath()))
|
2018-05-15 01:31:26 +01:00
|
|
|
if err != nil {
|
2018-06-29 21:06:25 +01:00
|
|
|
s.logger.Error("err deleting path and pointer", zap.Error(err))
|
2018-05-30 03:47:40 +01:00
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
2018-08-27 18:28:16 +01:00
|
|
|
s.logger.Debug("deleted pointer at path: " + req.GetPath())
|
2018-05-30 03:47:40 +01:00
|
|
|
return &pb.DeleteResponse{}, nil
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|