2018-11-05 15:23:54 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package bwagreement
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-08 13:20:23 +00:00
|
|
|
"sync"
|
2018-11-05 15:23:54 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2018-11-09 22:15:35 +00:00
|
|
|
"storj.io/storj/internal/migrate"
|
2018-11-05 15:23:54 +00:00
|
|
|
dbx "storj.io/storj/pkg/bwagreement/dbx"
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server is an implementation of the pb.BandwidthServer interface
|
|
|
|
type Server struct {
|
2018-11-08 13:20:23 +00:00
|
|
|
DB *dbx.DB
|
|
|
|
mu sync.Mutex
|
2018-11-05 15:23:54 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
2018-11-08 13:20:23 +00:00
|
|
|
// Agreement is a struct that contains a bandwidth agreement and the associated signature
|
|
|
|
type Agreement struct {
|
|
|
|
Agreement []byte
|
|
|
|
Signature []byte
|
|
|
|
}
|
|
|
|
|
2018-11-05 15:23:54 +00:00
|
|
|
// NewServer creates instance of Server
|
|
|
|
func NewServer(driver, source string, logger *zap.Logger) (*Server, error) {
|
|
|
|
db, err := dbx.Open(driver, source)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-09 22:15:35 +00:00
|
|
|
err = migrate.Create("bwagreement", db)
|
|
|
|
if err != nil {
|
2018-11-05 15:23:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Server{
|
|
|
|
DB: db,
|
|
|
|
logger: logger,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-11-08 13:20:23 +00:00
|
|
|
func (s *Server) locked() func() {
|
|
|
|
s.mu.Lock()
|
|
|
|
return s.mu.Unlock
|
|
|
|
}
|
|
|
|
|
2018-11-05 15:23:54 +00:00
|
|
|
// Create a db entry for the provided storagenode
|
|
|
|
func (s *Server) Create(ctx context.Context, createBwAgreement *pb.RenterBandwidthAllocation) (bwagreement *dbx.Bwagreement, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-08 20:27:07 +00:00
|
|
|
s.logger.Debug("entering bwagreement Create")
|
2018-11-05 15:23:54 +00:00
|
|
|
|
|
|
|
signature := createBwAgreement.GetSignature()
|
|
|
|
data := createBwAgreement.GetData()
|
|
|
|
|
|
|
|
bwagreement, err = s.DB.Create_Bwagreement(
|
|
|
|
ctx,
|
|
|
|
dbx.Bwagreement_Signature(signature),
|
|
|
|
dbx.Bwagreement_Data(data),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return bwagreement, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BandwidthAgreements receives and stores bandwidth agreements from storage nodes
|
|
|
|
func (s *Server) BandwidthAgreements(stream pb.Bandwidth_BandwidthAgreementsServer) (err error) {
|
|
|
|
ctx := stream.Context()
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-08 13:20:23 +00:00
|
|
|
defer s.locked()()
|
2018-11-05 15:23:54 +00:00
|
|
|
|
|
|
|
ch := make(chan *pb.RenterBandwidthAllocation, 1)
|
|
|
|
errch := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
msg, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("Grpc Receive Error", zap.Error(err))
|
|
|
|
errch <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ch <- msg
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-errch:
|
|
|
|
return err
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case agreement := <-ch:
|
2018-11-08 13:20:23 +00:00
|
|
|
_, err = s.Create(ctx, agreement)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("DB entry creation Error", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-11-08 13:20:23 +00:00
|
|
|
|
|
|
|
// GetBandwidthAllocations all bandwidth agreements and sorts by satellite
|
|
|
|
func (s *Server) GetBandwidthAllocations(ctx context.Context) (rows []*dbx.Bwagreement, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
defer s.locked()()
|
|
|
|
rows, err = s.DB.All_Bwagreement(ctx)
|
|
|
|
return rows, err
|
|
|
|
}
|