Skip to content

Commit

Permalink
feat: make reserve capacity configurable and dynamic (#4847)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinconic authored Oct 4, 2024
1 parent 88ba195 commit a8066a2
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 23 deletions.
2 changes: 2 additions & 0 deletions cmd/bee/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const (
optionNameWhitelistedWithdrawalAddress = "withdrawal-addresses-whitelist"
optionNameTransactionDebugMode = "transaction-debug-mode"
optionMinimumStorageRadius = "minimum-storage-radius"
optionReserveCapacityDoubling = "reserve-capacity-doubling"
)

// nolint:gochecknoinits
Expand Down Expand Up @@ -290,6 +291,7 @@ func (c *command) setAllFlags(cmd *cobra.Command) {
cmd.Flags().StringSlice(optionNameWhitelistedWithdrawalAddress, []string{}, "withdrawal target addresses")
cmd.Flags().Bool(optionNameTransactionDebugMode, false, "skips the gas estimate step for contract transactions")
cmd.Flags().Uint(optionMinimumStorageRadius, 0, "minimum radius storage threshold")
cmd.Flags().Int(optionReserveCapacityDoubling, 0, "reserve capacity doubling")
}

func newLogger(cmd *cobra.Command, verbosity string) (log.Logger, error) {
Expand Down
18 changes: 9 additions & 9 deletions cmd/bee/cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func dbInfoCmd(cmd *cobra.Command) {
Logger: logger,
RadiusSetter: noopRadiusSetter{},
Batchstore: new(postage.NoOpBatchStore),
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
CacheCapacity: 1_000_000,
})
if err != nil {
Expand Down Expand Up @@ -166,7 +166,7 @@ func dbCompactCmd(cmd *cobra.Command) {
Logger: logger,
RadiusSetter: noopRadiusSetter{},
Batchstore: new(postage.NoOpBatchStore),
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
}, validation)
if err != nil {
return fmt.Errorf("localstore: %w", err)
Expand Down Expand Up @@ -221,7 +221,7 @@ func dbValidatePinsCmd(cmd *cobra.Command) {
Logger: logger,
RadiusSetter: noopRadiusSetter{},
Batchstore: new(postage.NoOpBatchStore),
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
})
if err != nil {
return fmt.Errorf("localstore: %w", err)
Expand Down Expand Up @@ -283,7 +283,7 @@ func dbRepairReserve(cmd *cobra.Command) {
Logger: logger,
RadiusSetter: noopRadiusSetter{},
Batchstore: new(postage.NoOpBatchStore),
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
CacheCapacity: 1_000_000,
})
if err != nil {
Expand Down Expand Up @@ -347,7 +347,7 @@ func dbValidateCmd(cmd *cobra.Command) {
Logger: logger,
RadiusSetter: noopRadiusSetter{},
Batchstore: new(postage.NoOpBatchStore),
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
})
if err != nil {
return fmt.Errorf("localstore: %w", err)
Expand Down Expand Up @@ -410,7 +410,7 @@ func dbExportReserveCmd(cmd *cobra.Command) {
Logger: logger,
RadiusSetter: noopRadiusSetter{},
Batchstore: new(postage.NoOpBatchStore),
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
CacheCapacity: 1_000_000,
})
if err != nil {
Expand Down Expand Up @@ -493,7 +493,7 @@ func dbExportPinningCmd(cmd *cobra.Command) {
Logger: logger,
RadiusSetter: noopRadiusSetter{},
Batchstore: new(postage.NoOpBatchStore),
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
CacheCapacity: 1_000_000,
})
if err != nil {
Expand Down Expand Up @@ -603,7 +603,7 @@ func dbImportReserveCmd(cmd *cobra.Command) {
Logger: logger,
RadiusSetter: noopRadiusSetter{},
Batchstore: new(postage.NoOpBatchStore),
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
CacheCapacity: 1_000_000,
})
if err != nil {
Expand Down Expand Up @@ -687,7 +687,7 @@ func dbImportPinningCmd(cmd *cobra.Command) {
Logger: logger,
RadiusSetter: noopRadiusSetter{},
Batchstore: new(postage.NoOpBatchStore),
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
CacheCapacity: 1_000_000,
})
if err != nil {
Expand Down
17 changes: 8 additions & 9 deletions cmd/bee/cmd/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/ethersphere/bee/v2/cmd/bee/cmd"
"github.com/ethersphere/bee/v2/pkg/log"
"github.com/ethersphere/bee/v2/pkg/node"
"github.com/ethersphere/bee/v2/pkg/postage"
storagetest "github.com/ethersphere/bee/v2/pkg/storage/testing"
"github.com/ethersphere/bee/v2/pkg/storer"
Expand All @@ -35,7 +34,7 @@ func TestDBExportImport(t *testing.T) {
Batchstore: new(postage.NoOpBatchStore),
RadiusSetter: kademlia.NewTopologyDriver(),
Logger: testutil.NewLogger(t),
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
}, dir1)

chunks := make(map[string]int)
Expand Down Expand Up @@ -64,7 +63,7 @@ func TestDBExportImport(t *testing.T) {
Batchstore: new(postage.NoOpBatchStore),
RadiusSetter: kademlia.NewTopologyDriver(),
Logger: testutil.NewLogger(t),
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
}, dir2)

err = db2.ReserveIterateChunks(func(chunk swarm.Chunk) (bool, error) {
Expand Down Expand Up @@ -95,7 +94,7 @@ func TestDBExportImportPinning(t *testing.T) {
Batchstore: new(postage.NoOpBatchStore),
RadiusSetter: kademlia.NewTopologyDriver(),
Logger: testutil.NewLogger(t),
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
}, dir1)

chunks := make(map[string]int)
Expand Down Expand Up @@ -139,7 +138,7 @@ func TestDBExportImportPinning(t *testing.T) {
Batchstore: new(postage.NoOpBatchStore),
RadiusSetter: kademlia.NewTopologyDriver(),
Logger: testutil.NewLogger(t),
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
}, dir2)
addresses, err := db2.Pins()
if err != nil {
Expand Down Expand Up @@ -183,7 +182,7 @@ func TestDBNuke_FLAKY(t *testing.T) {
Batchstore: new(postage.NoOpBatchStore),
RadiusSetter: kademlia.NewTopologyDriver(),
Logger: log.Noop,
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
}, dataDir)

nChunks := 10
Expand Down Expand Up @@ -213,7 +212,7 @@ func TestDBNuke_FLAKY(t *testing.T) {
Batchstore: new(postage.NoOpBatchStore),
RadiusSetter: kademlia.NewTopologyDriver(),
Logger: log.Noop,
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
}, path.Join(dataDir, "localstore"))
if err != nil {
t.Fatal(err)
Expand All @@ -238,7 +237,7 @@ func TestDBInfo(t *testing.T) {
Batchstore: new(postage.NoOpBatchStore),
RadiusSetter: kademlia.NewTopologyDriver(),
Logger: testutil.NewLogger(t),
ReserveCapacity: node.ReserveCapacity,
ReserveCapacity: storer.DefaultReserveCapacity,
}, dir1)

nChunks := 10
Expand All @@ -265,7 +264,7 @@ func TestDBInfo(t *testing.T) {
t.Fatal(err)
}

if !strings.Contains(buf.String(), fmt.Sprintf("\"msg\"=\"reserve\" \"size_within_radius\"=%d \"total_size\"=%d \"capacity\"=%d", nChunks, nChunks, node.ReserveCapacity)) {
if !strings.Contains(buf.String(), fmt.Sprintf("\"msg\"=\"reserve\" \"size_within_radius\"=%d \"total_size\"=%d \"capacity\"=%d", nChunks, nChunks, storer.DefaultReserveCapacity)) {
t.Fatal("reserve info not correct")
}
}
Expand Down
1 change: 1 addition & 0 deletions cmd/bee/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ func buildBeeNode(ctx context.Context, c *command, cmd *cobra.Command, logger lo
WhitelistedWithdrawalAddress: c.config.GetStringSlice(optionNameWhitelistedWithdrawalAddress),
TrxDebugMode: c.config.GetBool(optionNameTransactionDebugMode),
MinimumStorageRadius: c.config.GetUint(optionMinimumStorageRadius),
ReserveCapacityDoubling: c.config.GetInt(optionReserveCapacityDoubling),
})

return b, err
Expand Down
2 changes: 2 additions & 0 deletions packaging/bee.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ password-file: "/var/lib/bee/password"
# mainnet: true
## minimum radius storage threshold
# minimum-storage-radius: 0
## reserve capacity doubling (default 0, maximum 1)
reserve-capacity-doubling: 0
2 changes: 2 additions & 0 deletions packaging/homebrew-amd64/bee.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ password-file: "/usr/local/var/lib/swarm-bee/password"
# mainnet: true
# ## minimum radius storage threshold
# minimum-storage-radius: 0
## reserve capacity doubling (default 0, maximum 1)
reserve-capacity-doubling: 0
2 changes: 2 additions & 0 deletions packaging/homebrew-arm64/bee.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ password-file: "/opt/homebrew/var/lib/swarm-bee/password"
# mainnet: true
## minimum radius storage threshold
# minimum-storage-radius: 0
## reserve capacity doubling (default 0, maximum 1)
reserve-capacity-doubling: 0
2 changes: 2 additions & 0 deletions packaging/scoop/bee.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@ password-file: "./password"
# mainnet: true
## minimum radius storage threshold
# minimum-storage-radius: 0
## reserve capacity doubling (default 0, maximum 1)
reserve-capacity-doubling: 0
16 changes: 12 additions & 4 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ type Options struct {
WhitelistedWithdrawalAddress []string
TrxDebugMode bool
MinimumStorageRadius uint
ReserveCapacityDoubling int
}

const (
Expand All @@ -184,9 +185,7 @@ const (
minPaymentThreshold = 2 * refreshRate // minimal accepted payment threshold of full nodes
maxPaymentThreshold = 24 * refreshRate // maximal accepted payment threshold of full nodes
mainnetNetworkID = uint64(1) //
ReserveCapacity = 4_194_304 // 2^22 chunks
reserveWakeUpDuration = 15 * time.Minute // time to wait before waking up reserveWorker
reserveTreshold = ReserveCapacity * 5 / 10
reserveMinEvictCount = 1_000
cacheMinEvictCount = 10_000
)
Expand Down Expand Up @@ -354,13 +353,21 @@ func NewBee(
var batchStore postage.Storer = new(postage.NoOpBatchStore)
var evictFn func([]byte) error

var reserveCapacity int

if o.ReserveCapacityDoubling >= 0 && o.ReserveCapacityDoubling <= 1 {
reserveCapacity = 1 << (22 + o.ReserveCapacityDoubling)
} else {
return nil, fmt.Errorf("config reserve capacity doubling has to be between default: 0 and maximum: 1")
}

if chainEnabled {
batchStore, err = batchstore.New(
stateStore,
func(id []byte) error {
return evictFn(id)
},
ReserveCapacity,
reserveCapacity,
logger,
)
if err != nil {
Expand Down Expand Up @@ -724,7 +731,7 @@ func NewBee(

if o.FullNodeMode && !o.BootnodeMode {
// configure reserve only for full node
lo.ReserveCapacity = ReserveCapacity
lo.ReserveCapacity = reserveCapacity
lo.ReserveWakeUpDuration = reserveWakeUpDuration
lo.ReserveMinEvictCount = reserveMinEvictCount
lo.RadiusSetter = kad
Expand Down Expand Up @@ -1036,6 +1043,7 @@ func NewBee(
}

isFullySynced := func() bool {
reserveTreshold := reserveCapacity * 5 / 10
return localStore.ReserveSize() >= reserveTreshold && pullerService.SyncRate() == 0
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/storer/storer.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ const (
defaultDisableSeeksCompaction = false
defaultCacheCapacity = uint64(1_000_000)
defaultBgCacheWorkers = 16
DefaultReserveCapacity = 1 << 22 // 4194304 chunks

indexPath = "indexstore"
sharkyPath = "sharky"
Expand Down Expand Up @@ -396,7 +397,7 @@ func defaultOptions() *Options {
LdbDisableSeeksCompaction: defaultDisableSeeksCompaction,
CacheCapacity: defaultCacheCapacity,
Logger: log.Noop,
ReserveCapacity: 4_194_304, // 2^22 chunks
ReserveCapacity: DefaultReserveCapacity,
ReserveWakeUpDuration: time.Minute * 30,
}
}
Expand Down

0 comments on commit a8066a2

Please sign in to comment.