storj/pkg/piecestore/rpc/server/server.go
Alexander Leitner b0db33f919
Bandwidth accounting (#134)
* captplanet standalone farmer setup

* Bandwidth Allocation

* utils.Close method changed to utils.LogClose

* Get build temporarily working

* Get/Put for PSClient should take payer bandwidth allocations rather than the NewPSClient function

* Update example client to reflect changes in client API

* Update ecclient to use latest PSClient, Make NewPSClient return error also

* Updated pieceranger tests to check for errors; sign method should take byte array

* Handle defers in store.go better

* Fix defer functions in psdb.go

* fun times

* Protobuf bandwidthallocation data is now a byte array

* Remove psservice package and merge it into pstore server

* Write wrapper for database calls

* Change all expiration names in protobuf to be more informative; add defer in retrieve; remove old comment

* Make PSDB tests implementation independent rather than method independent

* get rid of payer, renter in ecclient

* add context monitoring in store and retrieve
2018-08-17 13:40:15 -04:00

136 lines
3.0 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package server
import (
"log"
"os"
"path/filepath"
"go.uber.org/zap"
"golang.org/x/net/context"
monkit "gopkg.in/spacemonkeygo/monkit.v2"
"storj.io/storj/pkg/piecestore"
"storj.io/storj/pkg/piecestore/rpc/server/psdb"
"storj.io/storj/pkg/provider"
pb "storj.io/storj/protos/piecestore"
)
var (
mon = monkit.Package()
)
// Config contains everything necessary for a server
type Config struct {
Path string `help:"path to store data in" default:"$CONFDIR"`
}
// Run implements provider.Responsibility
func (c Config) Run(ctx context.Context, server *provider.Provider) (err error) {
defer mon.Task()(&ctx)(&err)
s, err := Initialize(ctx, c)
if err != nil {
return err
}
go func() {
err := s.DB.DeleteExpiredLoop(ctx)
zap.S().Fatal("Error in DeleteExpiredLoop: %v\n", err)
}()
pb.RegisterPieceStoreRoutesServer(server.GRPC(), s)
defer func() {
log.Fatal(s.Stop(ctx))
}()
return server.Run(ctx)
}
// Server -- GRPC server meta data used in route calls
type Server struct {
DataDir string
DB *psdb.PSDB
}
// Initialize -- initializes a server struct
func Initialize(ctx context.Context, config Config) (*Server, error) {
dbPath := filepath.Join(config.Path, "piecestore.db")
dataDir := filepath.Join(config.Path, "piece-store-data")
psDB, err := psdb.OpenPSDB(ctx, dataDir, dbPath)
if err != nil {
return nil, err
}
return &Server{DataDir: dataDir, DB: psDB}, nil
}
// Stop the piececstore node
func (s *Server) Stop(ctx context.Context) (err error) {
return s.DB.Close()
}
// Piece -- Send meta data about a stored by by Id
func (s *Server) Piece(ctx context.Context, in *pb.PieceId) (*pb.PieceSummary, error) {
log.Printf("Getting Meta for %s...", in.Id)
path, err := pstore.PathByID(in.GetId(), s.DataDir)
if err != nil {
return nil, err
}
fileInfo, err := os.Stat(path)
if err != nil {
return nil, err
}
// Read database to calculate expiration
ttl, err := s.DB.GetTTLByID(in.GetId())
if err != nil {
return nil, err
}
log.Printf("Successfully retrieved meta for %s.", in.Id)
return &pb.PieceSummary{Id: in.GetId(), Size: fileInfo.Size(), ExpirationUnixSec: ttl}, nil
}
// Delete -- Delete data by Id from piecestore
func (s *Server) Delete(ctx context.Context, in *pb.PieceDelete) (*pb.PieceDeleteSummary, error) {
log.Printf("Deleting %s...", in.Id)
if err := s.deleteByID(in.GetId()); err != nil {
return nil, err
}
log.Printf("Successfully deleted %s.", in.Id)
return &pb.PieceDeleteSummary{Message: OK}, nil
}
func (s *Server) deleteByID(id string) error {
if err := pstore.Delete(id, s.DataDir); err != nil {
return err
}
if err := s.DB.DeleteTTLByID(id); err != nil {
return err
}
log.Printf("Deleted data of id (%s) from piecestore\n", id)
return nil
}
func (s *Server) verifySignature(ba *pb.RenterBandwidthAllocation) error {
// TODO: verify signature
// data := ba.GetData()
// signature := ba.GetSignature()
log.Printf("Verified signature\n")
return nil
}