2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-10-31 18:47:25 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package agreementsender
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2019-01-09 14:41:50 +00:00
|
|
|
"storj.io/storj/pkg/kademlia"
|
2018-10-31 18:47:25 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-06 17:49:17 +00:00
|
|
|
"storj.io/storj/pkg/piecestore/psserver/psdb"
|
2018-10-31 18:47:25 +00:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-01-09 14:41:50 +00:00
|
|
|
"storj.io/storj/pkg/transport"
|
2018-10-31 18:47:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ASError wraps errors returned from agreementsender package
|
|
|
|
ASError = errs.Class("agreement sender error")
|
|
|
|
)
|
|
|
|
|
|
|
|
// AgreementSender maintains variables required for reading bandwidth agreements from a DB and sending them to a Payers
|
2019-01-23 10:39:03 +00:00
|
|
|
type AgreementSender struct { // TODO: rename to service
|
2019-01-10 12:50:50 +00:00
|
|
|
DB *psdb.DB
|
|
|
|
log *zap.Logger
|
|
|
|
transport transport.Client
|
|
|
|
kad *kademlia.Kademlia
|
|
|
|
checkInterval time.Duration
|
2018-10-31 18:47:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 10:39:03 +00:00
|
|
|
// TODO: take transport instead of identity as argument
|
|
|
|
|
2019-01-09 14:41:50 +00:00
|
|
|
// New creates an Agreement Sender
|
2019-01-10 12:50:50 +00:00
|
|
|
func New(log *zap.Logger, DB *psdb.DB, identity *provider.FullIdentity, kad *kademlia.Kademlia, checkInterval time.Duration) *AgreementSender {
|
|
|
|
return &AgreementSender{DB: DB, log: log, transport: transport.NewClient(identity), kad: kad, checkInterval: checkInterval}
|
2018-10-31 18:47:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 14:41:50 +00:00
|
|
|
// Run the agreement sender with a context to check for cancel
|
2019-01-23 10:39:03 +00:00
|
|
|
func (as *AgreementSender) Run(ctx context.Context) error {
|
2019-01-09 14:41:50 +00:00
|
|
|
//todo: we likely don't want to stop on err, but consider returning errors via a channel
|
2019-01-10 12:50:50 +00:00
|
|
|
ticker := time.NewTicker(as.checkInterval)
|
2018-10-31 18:47:25 +00:00
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
2019-01-10 12:50:50 +00:00
|
|
|
as.log.Debug("AgreementSender is running", zap.Duration("duration", as.checkInterval))
|
2019-01-09 14:41:50 +00:00
|
|
|
agreementGroups, err := as.DB.GetBandwidthAllocations()
|
|
|
|
if err != nil {
|
|
|
|
as.log.Error("Agreementsender could not retrieve bandwidth allocations", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for satellite, agreements := range agreementGroups {
|
|
|
|
as.sendAgreementsToSatellite(ctx, satellite, agreements)
|
|
|
|
}
|
2018-10-31 18:47:25 +00:00
|
|
|
select {
|
2019-01-09 14:41:50 +00:00
|
|
|
case <-ticker.C:
|
2018-10-31 18:47:25 +00:00
|
|
|
case <-ctx.Done():
|
2019-01-23 10:39:03 +00:00
|
|
|
return ctx.Err()
|
2019-01-09 14:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 18:47:25 +00:00
|
|
|
|
2019-01-09 14:41:50 +00:00
|
|
|
func (as *AgreementSender) sendAgreementsToSatellite(ctx context.Context, satID storj.NodeID, agreements []*psdb.Agreement) {
|
|
|
|
as.log.Info("Sending agreements to satellite", zap.Int("number of agreements", len(agreements)), zap.String("satellite id", satID.String()))
|
2019-01-10 12:50:50 +00:00
|
|
|
// todo: cache kad responses if this interval is very small
|
2019-01-09 14:41:50 +00:00
|
|
|
// Get satellite ip from kademlia
|
|
|
|
satellite, err := as.kad.FindNode(ctx, satID)
|
|
|
|
if err != nil {
|
2019-01-11 18:15:49 +00:00
|
|
|
as.log.Warn("Agreementsender could not find satellite", zap.Error(err))
|
2019-01-09 14:41:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Create client from satellite ip
|
|
|
|
conn, err := as.transport.DialNode(ctx, &satellite)
|
|
|
|
if err != nil {
|
2019-01-11 18:15:49 +00:00
|
|
|
as.log.Warn("Agreementsender could not dial satellite", zap.Error(err))
|
2019-01-09 14:41:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
client := pb.NewBandwidthClient(conn)
|
|
|
|
defer func() {
|
|
|
|
err := conn.Close()
|
|
|
|
if err != nil {
|
2019-01-11 18:15:49 +00:00
|
|
|
as.log.Warn("Agreementsender failed to close connection", zap.Error(err))
|
2019-01-09 14:41:50 +00:00
|
|
|
}
|
|
|
|
}()
|
2018-10-31 18:47:25 +00:00
|
|
|
|
2019-01-11 18:15:49 +00:00
|
|
|
//todo: stop sending these one-by-one, send all at once
|
2019-01-09 14:41:50 +00:00
|
|
|
for _, agreement := range agreements {
|
|
|
|
msg := &pb.RenterBandwidthAllocation{
|
|
|
|
Data: agreement.Agreement,
|
|
|
|
Signature: agreement.Signature,
|
|
|
|
}
|
|
|
|
// Send agreement to satellite
|
|
|
|
r, err := client.BandwidthAgreements(ctx, msg)
|
2019-01-11 18:15:49 +00:00
|
|
|
if err != nil || r.GetStatus() == pb.AgreementsSummary_FAIL {
|
|
|
|
as.log.Warn("Agreementsender failed to send agreement to satellite : will retry", zap.Error(err))
|
|
|
|
continue
|
2019-01-12 00:31:34 +00:00
|
|
|
} else if r.GetStatus() == pb.AgreementsSummary_REJECTED {
|
2019-01-11 18:15:49 +00:00
|
|
|
//todo: something better than a delete here?
|
|
|
|
as.log.Error("Agreementsender had agreement explicitly rejected by satellite : will delete", zap.Error(err))
|
2019-01-09 14:41:50 +00:00
|
|
|
}
|
|
|
|
// Delete from PSDB by signature
|
|
|
|
if err = as.DB.DeleteBandwidthAllocationBySignature(agreement.Signature); err != nil {
|
|
|
|
as.log.Error("Agreementsender failed to delete bandwidth allocation", zap.Error(err))
|
2018-10-31 18:47:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|