Skip to content

Instantly share code, notes, and snippets.

@liori
Created May 5, 2024 19:05
Show Gist options
  • Save liori/4668e511a3afec7cd14d5069b8b75927 to your computer and use it in GitHub Desktop.
Save liori/4668e511a3afec7cd14d5069b8b75927 to your computer and use it in GitHub Desktop.
commit bed5703cf1077adbb93009fd61f3083527a7b2e2
Author: Tomasz Melcer <liori@exroot.org>
Date: Sun Apr 23 23:34:46 2023 +0200
add bandwidth only when settling orders
Change-Id: Id032f75189d9206ac79db8ef7018d9911b4727c8
diff --git a/storagenode/inspector/inspector_test.go b/storagenode/inspector/inspector_test.go
index be4baed6d..98c4ea0b4 100644
--- a/storagenode/inspector/inspector_test.go
+++ b/storagenode/inspector/inspector_test.go
@@ -65,6 +65,8 @@ func TestInspectorStats(t *testing.T) {
var downloaded int
for _, storageNode := range planet.StorageNodes {
+ storageNode.Storage2.Orders.SendOrders(ctx, time.Now().Add(24*time.Hour))
+
response, err := storageNode.Storage2.Inspector.Stats(ctx, &internalpb.StatsRequest{})
require.NoError(t, err)
diff --git a/storagenode/orders/service.go b/storagenode/orders/service.go
index 4b818815f..584ffbfd0 100644
--- a/storagenode/orders/service.go
+++ b/storagenode/orders/service.go
@@ -6,6 +6,8 @@ package orders
import (
"context"
"math/rand"
+ "storj.io/common/context2"
+ "storj.io/storj/storagenode/bandwidth"
"sync"
"time"
@@ -98,6 +100,7 @@ type Service struct {
dialer rpc.Dialer
ordersStore *FileStore
orders DB
+ bandwidth bandwidth.DB
trust *trust.Pool
Sender *sync2.Cycle
@@ -105,12 +108,13 @@ type Service struct {
}
// NewService creates an order service.
-func NewService(log *zap.Logger, dialer rpc.Dialer, ordersStore *FileStore, orders DB, trust *trust.Pool, config Config) *Service {
+func NewService(log *zap.Logger, dialer rpc.Dialer, ordersStore *FileStore, orders DB, bandwidth bandwidth.DB, trust *trust.Pool, config Config) *Service {
return &Service{
log: log,
dialer: dialer,
ordersStore: ordersStore,
orders: orders,
+ bandwidth: bandwidth,
config: config,
trust: trust,
@@ -235,6 +239,19 @@ func (service *Service) SendOrders(ctx context.Context, now time.Time) {
log.Warn("skipping order settlement for untrusted satellite. Order will be archived", zap.String("satellite ID", satelliteID.String()))
}
+ amountsByActions := make(map[pb.PieceAction]int64)
+ for _, order := range unsentInfo.InfoList {
+ amountsByActions[order.Limit.Action] += order.Order.Amount
+ }
+ for action, amount := range amountsByActions {
+ // TODO liori: better time.
+ // TODO liori: might be imprecise, see b6026b9ff3b41d0d80e1cd1bae13f567856385ca
+ err := service.bandwidth.Add(context2.WithoutCancellation(ctx), satelliteID, action, amount, time.Now())
+ if err != nil {
+ service.log.Error("failed to add bandwidth usage", zap.String("satellite ID", satelliteID.String()), zap.String("action", action.String()), zap.Int64("amount", amount), zap.Error(err))
+ }
+ }
+
err = service.ordersStore.Archive(satelliteID, unsentInfo, time.Now().UTC(), status)
if err != nil {
log.Error("failed to archive orders", zap.Error(err))
diff --git a/storagenode/peer.go b/storagenode/peer.go
index 9856e3d2d..81eb5c91f 100644
--- a/storagenode/peer.go
+++ b/storagenode/peer.go
@@ -621,6 +621,7 @@ func New(log *zap.Logger, full *identity.FullIdentity, db DB, revocationDB exten
dialer,
peer.OrdersStore,
peer.DB.Orders(),
+ peer.DB.Bandwidth(),
peer.Storage2.Trust,
config.Storage2.Orders,
)
diff --git a/storagenode/piecestore/endpoint.go b/storagenode/piecestore/endpoint.go
index 7e7640d52..029a83cda 100644
--- a/storagenode/piecestore/endpoint.go
+++ b/storagenode/piecestore/endpoint.go
@@ -21,7 +21,6 @@ import (
"golang.org/x/sync/errgroup"
"storj.io/common/bloomfilter"
- "storj.io/common/context2"
"storj.io/common/errs2"
"storj.io/common/identity"
"storj.io/common/memory"
@@ -922,16 +921,6 @@ func (endpoint *Endpoint) beginSaveOrder(limit *pb.OrderLimit) (_commit func(ctx
err = commit(&ordersfile.Info{Limit: limit, Order: order})
if err != nil {
endpoint.log.Error("failed to add order", zap.Error(err))
- } else {
- amount := order.Amount
- if amountFunc != nil {
- amount = amountFunc()
- }
- // We always want to save order to the database to be able to settle.
- err = endpoint.usage.Add(context2.WithoutCancellation(ctx), limit.SatelliteId, limit.Action, amount, time.Now())
- if err != nil {
- endpoint.log.Error("failed to add bandwidth usage", zap.Error(err))
- }
}
}, nil
}
diff --git a/storagenode/piecestore/endpoint_test.go b/storagenode/piecestore/endpoint_test.go
index 497068689..b57a683b1 100644
--- a/storagenode/piecestore/endpoint_test.go
+++ b/storagenode/piecestore/endpoint_test.go
@@ -77,6 +77,7 @@ func TestUploadAndPartialDownload(t *testing.T) {
var totalBandwidthUsage bandwidth.Usage
for _, storagenode := range planet.StorageNodes {
+ storagenode.Storage2.Orders.SendOrders(ctx, time.Now().Add(24*time.Hour))
usage, err := storagenode.DB.Bandwidth().Summary(ctx, time.Now().Add(-10*time.Hour), time.Now().Add(10*time.Hour))
require.NoError(t, err)
totalBandwidthUsage.Add(usage)
@@ -197,6 +198,7 @@ func TestUpload(t *testing.T) {
require.NoError(t, planet.WaitForStorageNodeEndpoints(ctx))
from, to := date.MonthBoundary(time.Now().UTC())
+ planet.StorageNodes[0].Storage2.Orders.SendOrders(ctx, time.Now().Add(24*time.Hour))
summary, err := planet.StorageNodes[0].DB.Bandwidth().SatelliteIngressSummary(ctx, planet.Satellites[0].ID(), from, to)
require.NoError(t, err)
require.Equal(t, expectedIngressAmount, summary.Put)
@jtolio
Copy link

jtolio commented May 6, 2024

hello! this is a great patch! would you be willing to sign https://forms.gle/5qfiYnT4HYi95R7JA so we can pull this in?

@liori
Copy link
Author

liori commented May 6, 2024

@liori
Copy link
Author

liori commented May 6, 2024

Though, reminder, it needs some more work.

@jtolio
Copy link

jtolio commented May 7, 2024

Oh you're right! I should have checked first. Thank you!

@jtolio
Copy link

jtolio commented May 7, 2024

I pushed this change to https://review.dev.storj.io/c/storj/storj/+/13086 if you want to follow along there. We had another approach in https://review.dev.storj.io/c/storj/storj/+/13082 but we like your insight!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment