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 (
|
2019-03-19 12:56:59 +00:00
|
|
|
"io"
|
2018-10-31 18:47:25 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/net/context"
|
2019-03-19 12:56:59 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2018-10-31 18:47:25 +00:00
|
|
|
|
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-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-02-11 11:17:32 +00:00
|
|
|
func New(log *zap.Logger, DB *psdb.DB, tc transport.Client, kad *kademlia.Kademlia, checkInterval time.Duration) *AgreementSender {
|
|
|
|
return &AgreementSender{DB: DB, log: log, transport: tc, 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-03-19 12:56:59 +00:00
|
|
|
as.log.Debug("is running", zap.Duration("duration", as.checkInterval))
|
2019-01-09 14:41:50 +00:00
|
|
|
agreementGroups, err := as.DB.GetBandwidthAllocations()
|
|
|
|
if err != nil {
|
2019-03-19 12:56:59 +00:00
|
|
|
as.log.Error("could not retrieve bandwidth allocations", zap.Error(err))
|
2019-01-09 14:41:50 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-03-19 12:56:59 +00:00
|
|
|
|
|
|
|
if len(agreementGroups) > 0 {
|
|
|
|
var group errgroup.Group
|
|
|
|
// send agreement payouts
|
|
|
|
for satellite, agreements := range agreementGroups {
|
|
|
|
satellite, agreements := satellite, agreements
|
|
|
|
group.Go(func() error {
|
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Hour)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
as.SettleAgreements(timedCtx, satellite, agreements)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ = group.Wait() // doesn't return errors
|
2019-01-09 14:41:50 +00:00
|
|
|
}
|
2019-03-05 22:20:46 +00:00
|
|
|
|
|
|
|
// Delete older payout irrespective of its status
|
|
|
|
if err = as.DB.DeleteBandwidthAllocationPayouts(); err != nil {
|
2019-03-19 12:56:59 +00:00
|
|
|
as.log.Error("failed to delete bandwidth allocation", zap.Error(err))
|
2019-03-05 22:20:46 +00:00
|
|
|
}
|
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-03-19 12:56:59 +00:00
|
|
|
// SettleAgreements uploads agreements to the satellite
|
|
|
|
func (as *AgreementSender) SettleAgreements(ctx context.Context, satelliteID storj.NodeID, agreements []*psdb.Agreement) {
|
|
|
|
as.log.Info("sending agreements to satellite", zap.Int("number of agreements", len(agreements)), zap.String("satellite id", satelliteID.String()))
|
|
|
|
|
|
|
|
satellite, err := as.kad.FindNode(ctx, satelliteID)
|
2019-01-09 14:41:50 +00:00
|
|
|
if err != nil {
|
2019-03-21 10:59:22 +00:00
|
|
|
as.log.Warn("could not find satellite", zap.String("satellite id", satelliteID.String()), zap.Error(err))
|
2019-01-09 14:41:50 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-19 12:56:59 +00:00
|
|
|
|
2019-01-09 14:41:50 +00:00
|
|
|
conn, err := as.transport.DialNode(ctx, &satellite)
|
|
|
|
if err != nil {
|
2019-03-21 10:59:22 +00:00
|
|
|
as.log.Warn("could not dial satellite", zap.String("satellite id", satelliteID.String()), zap.Error(err))
|
2019-01-09 14:41:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
2019-03-05 22:20:46 +00:00
|
|
|
if err := conn.Close(); err != nil {
|
2019-03-21 10:59:22 +00:00
|
|
|
as.log.Warn("failed to close connection", zap.String("satellite id", satelliteID.String()), zap.Error(err))
|
2019-01-09 14:41:50 +00:00
|
|
|
}
|
|
|
|
}()
|
2018-10-31 18:47:25 +00:00
|
|
|
|
2019-03-19 12:56:59 +00:00
|
|
|
client, err := pb.NewBandwidthClient(conn).Settlement(ctx)
|
|
|
|
if err != nil {
|
2019-03-21 10:59:22 +00:00
|
|
|
as.log.Error("failed to start settlement", zap.String("satellite id", satelliteID.String()), zap.Error(err))
|
2019-03-19 12:56:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var group errgroup.Group
|
|
|
|
group.Go(func() error {
|
|
|
|
for _, agreement := range agreements {
|
|
|
|
err := client.Send(&pb.BandwidthSettlementRequest{
|
|
|
|
Allocation: &agreement.Agreement,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return client.CloseSend()
|
|
|
|
})
|
|
|
|
|
|
|
|
for {
|
|
|
|
response, err := client.Recv()
|
2019-01-28 19:45:25 +00:00
|
|
|
if err != nil {
|
2019-03-19 12:56:59 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2019-03-21 10:59:22 +00:00
|
|
|
as.log.Error("failed to recv response", zap.String("satellite id", satelliteID.String()), zap.Error(err))
|
2019-03-19 12:56:59 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch response.Status {
|
|
|
|
case pb.AgreementsSummary_REJECTED:
|
|
|
|
err = as.DB.UpdateBandwidthAllocationStatus(response.SerialNumber, psdb.AgreementStatusReject)
|
|
|
|
if err != nil {
|
2019-03-21 10:59:22 +00:00
|
|
|
as.log.Error("error", zap.String("satellite id", satelliteID.String()), zap.Error(err))
|
2019-03-05 22:20:46 +00:00
|
|
|
}
|
2019-03-19 12:56:59 +00:00
|
|
|
case pb.AgreementsSummary_OK:
|
|
|
|
err = as.DB.UpdateBandwidthAllocationStatus(response.SerialNumber, psdb.AgreementStatusSent)
|
2019-03-05 22:20:46 +00:00
|
|
|
if err != nil {
|
2019-03-21 10:59:22 +00:00
|
|
|
as.log.Error("error", zap.String("satellite id", satelliteID.String()), zap.Error(err))
|
2019-01-28 19:45:25 +00:00
|
|
|
}
|
2019-03-19 12:56:59 +00:00
|
|
|
default:
|
2019-03-21 10:59:22 +00:00
|
|
|
as.log.Error("unexpected response", zap.String("satellite id", satelliteID.String()), zap.Error(err))
|
2019-01-09 14:41:50 +00:00
|
|
|
}
|
2018-10-31 18:47:25 +00:00
|
|
|
}
|
2019-03-19 12:56:59 +00:00
|
|
|
|
|
|
|
if err := group.Wait(); err != nil {
|
2019-03-21 10:59:22 +00:00
|
|
|
as.log.Error("sending agreements returned an error", zap.String("satellite id", satelliteID.String()), zap.Error(err))
|
2019-03-19 12:56:59 +00:00
|
|
|
}
|
2018-10-31 18:47:25 +00:00
|
|
|
}
|