adds segmentStore Get, List, Delete (#143)

* adds segmentStore Get, List, Delete

* adds proper ecclient delete call in segmentstore delete, adds overlayHelper function
This commit is contained in:
Natalie Villasana 2018-07-16 10:40:02 -04:00 committed by JT Olio
parent b547062cf3
commit 821c3d0351

View File

@ -5,10 +5,10 @@ package segment
import (
"context"
"fmt"
"io"
"time"
"github.com/gogo/protobuf/proto"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
@ -43,16 +43,16 @@ type Store interface {
}
type segmentStore struct {
oc opb.OverlayClient
ec ecclient.Client
pc ppb.PointerDBClient
rs eestream.RedundancyStrategy
oc opb.OverlayClient
ec ecclient.Client
pdb ppb.PointerDBClient
rs eestream.RedundancyStrategy
}
// NewSegmentStore creates a new instance of segmentStore
func NewSegmentStore(oc opb.OverlayClient, ec ecclient.Client, pc ppb.PointerDBClient,
func NewSegmentStore(oc opb.OverlayClient, ec ecclient.Client, pdb ppb.PointerDBClient,
rs eestream.RedundancyStrategy) Store {
return &segmentStore{oc: oc, ec: ec, pc: pc, rs: rs}
return &segmentStore{oc: oc, ec: ec, pdb: pdb, rs: rs}
}
// Put uploads a file to an erasure code client
@ -85,7 +85,7 @@ func (s *segmentStore) Put(ctx context.Context, path paths.Path, data io.Reader,
// creates pointer
pr := ppb.PutRequest{
Path: []byte(fmt.Sprintf("%s", path)),
Path: []byte(path.String()),
Pointer: &ppb.Pointer{
Type: ppb.Pointer_REMOTE,
Remote: &ppb.RemoteSegment{
@ -105,7 +105,7 @@ func (s *segmentStore) Put(ctx context.Context, path paths.Path, data io.Reader,
}
// puts pointer to pointerDB
_, err = s.pc.Put(ctx, &pr)
_, err = s.pdb.Put(ctx, &pr)
if err != nil || status.Code(err) == codes.Internal {
zap.L().Error("failed to put", zap.Error(err))
return Error.Wrap(err)
@ -113,21 +113,128 @@ func (s *segmentStore) Put(ctx context.Context, path paths.Path, data io.Reader,
return nil
}
// Get retrieves a file from the erasure code client with help from overlay and pointerdb
// Get retrieves a file using erasure code, overlay, and pointerdb clients
func (s *segmentStore) Get(ctx context.Context, path paths.Path) (ranger.Ranger, Meta, error) {
m := Meta{
Data: []byte(""),
m := Meta{}
// TODO: remove this chunk after pointerdb client interface merged
gr := &ppb.GetRequest{
Path: []byte(path.String()),
APIKey: nil,
}
return nil, m, nil
pdbRes, err := s.pdb.Get(ctx, gr)
if err != nil {
return nil, m, err
}
// TODO: remove this chunk after pointerdb client interface merged
pointer := &ppb.Pointer{}
err = proto.Unmarshal(pdbRes.Pointer, pointer)
if err != nil {
return nil, m, err
}
if pointer.Type != ppb.Pointer_REMOTE {
zap.L().Error("TODO: only getting remote pointers supported")
return nil, m, err
}
nodes, err := s.overlayHelper(ctx, pointer.Remote)
if err != nil {
return nil, m, err
}
pid := client.PieceID(pointer.Remote.PieceId)
ecRes, err := s.ec.Get(ctx, nodes, s.rs, pid, pointer.Size)
if err != nil {
return nil, m, err
}
m.Data = pointer.Metadata
return ecRes, m, nil
}
// Delete issues deletes of a file to all piece stores and deletes from pointerdb
// Delete tells piece stores to delete a segment and deletes pointer from pointerdb
func (s *segmentStore) Delete(ctx context.Context, path paths.Path) error {
// TODO: remove this chunk after pointerdb client interface merged
gr := &ppb.GetRequest{
Path: []byte(path.String()),
APIKey: nil,
}
// gets pointer from pointerdb
pdbRes, err := s.pdb.Get(ctx, gr)
if err != nil {
return err
}
// TODO: remove this chunk after pointerdb client interface merged
pointer := &ppb.Pointer{}
err = proto.Unmarshal(pdbRes.Pointer, pointer)
if err != nil {
return err
}
nodes, err := s.overlayHelper(ctx, pointer.Remote)
if err != nil {
return err
}
// ecclient sends delete request
err = s.ec.Delete(ctx, nodes, client.PieceID(pointer.Remote.PieceId))
if err != nil {
return err
}
// TODO: remove this chunk after pointerdb client interface merged
dr := &ppb.DeleteRequest{
Path: []byte(path.String()),
APIKey: nil,
}
// deletes pointer from pointerdb
_, err = s.pdb.Delete(ctx, dr)
if err != nil {
return err
}
return nil
}
// overlayHelper calls Lookup to get node addresses from the overlay
func (s *segmentStore) overlayHelper(ctx context.Context, rem *ppb.RemoteSegment) (nodes []*opb.Node, err error) {
for i := 0; i < len(rem.RemotePieces); i++ {
overlayRes, err := s.oc.Lookup(ctx, &opb.LookupRequest{NodeID: rem.RemotePieces[i].NodeId})
if err != nil {
return nil, err
}
nodes = append(nodes, overlayRes.Node)
}
return nodes, nil
}
// List lists paths stored in the pointerdb
func (s *segmentStore) List(ctx context.Context, startingPath, endingPath paths.Path) (
paths []paths.Path, truncated bool, err error) {
return nil, true, nil
listPaths []paths.Path, truncated bool, err error) {
// TODO: remove this chunk after pointerdb client interface merged
lr := &ppb.ListRequest{
StartingPathKey: []byte(startingPath.String()),
// TODO: change limit to endingPath when supported
Limit: 1,
APIKey: nil,
}
res, err := s.pdb.List(ctx, lr)
if err != nil {
return nil, false, err
}
for _, path := range res.Paths {
np := paths.New(string(path[:]))
listPaths = append(listPaths, np)
}
return listPaths, res.Truncated, nil
}