Skip to content

Commit 92057ff

Browse files
authored
perf: various improvements in pusher, pushsync, salud, reacher (#4958)
1 parent 5456d14 commit 92057ff

File tree

16 files changed

+257
-128
lines changed

16 files changed

+257
-128
lines changed

openapi/Swarm.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
openapi: 3.0.3
22

33
info:
4-
version: 7.2.0
4+
version: 7.3.0
55
title: Bee API
66
description: "A list of the currently provided Interfaces to interact with the swarm, implementing file operations and sending messages"
77

openapi/SwarmCommon.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
openapi: 3.0.3
22
info:
3-
version: 4.2.0
3+
version: 4.3.0
44
title: Common Data Types
55
description: |
66
\*****bzzz*****

pkg/node/node.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,15 @@ func NewBee(
250250
}
251251
}(b)
252252

253-
if o.ReserveCapacityDoubling < 0 || o.ReserveCapacityDoubling > 1 {
253+
if !o.FullNodeMode && o.ReserveCapacityDoubling != 0 {
254+
return nil, fmt.Errorf("reserve capacity doubling is only allowed for full nodes")
255+
}
256+
257+
const maxAllowedDoubling = 1
258+
if o.ReserveCapacityDoubling < 0 || o.ReserveCapacityDoubling > maxAllowedDoubling {
254259
return nil, fmt.Errorf("config reserve capacity doubling has to be between default: 0 and maximum: 1")
255260
}
261+
var shallowReceiptTolerance = maxAllowedDoubling - o.ReserveCapacityDoubling
256262

257263
reserveCapacity := (1 << o.ReserveCapacityDoubling) * storer.DefaultReserveCapacity
258264

@@ -723,7 +729,7 @@ func NewBee(
723729
Batchstore: batchStore,
724730
StateStore: stateStore,
725731
RadiusSetter: kad,
726-
WarmupDuration: o.WarmupTime,
732+
WarmupDuration: warmupTime,
727733
Logger: logger,
728734
Tracer: tracer,
729735
CacheMinEvictCount: cacheMinEvictCount,
@@ -955,7 +961,7 @@ func NewBee(
955961
}
956962
}
957963

958-
pushSyncProtocol := pushsync.New(swarmAddress, networkID, nonce, p2ps, localStore, waitNetworkRFunc, kad, o.FullNodeMode && !o.BootnodeMode, pssService.TryUnwrap, gsocService.Handle, validStamp, logger, acc, pricer, signer, tracer, warmupTime)
964+
pushSyncProtocol := pushsync.New(swarmAddress, networkID, nonce, p2ps, localStore, waitNetworkRFunc, kad, o.FullNodeMode && !o.BootnodeMode, pssService.TryUnwrap, gsocService.Handle, validStamp, logger, acc, pricer, signer, tracer, warmupTime, uint8(shallowReceiptTolerance))
959965
b.pushSyncCloser = pushSyncProtocol
960966

961967
// set the pushSyncer in the PSS
@@ -964,7 +970,7 @@ func NewBee(
964970
retrieval := retrieval.New(swarmAddress, waitNetworkRFunc, localStore, p2ps, kad, logger, acc, pricer, tracer, o.RetrievalCaching)
965971
localStore.SetRetrievalService(retrieval)
966972

967-
pusherService := pusher.New(networkID, localStore, pushSyncProtocol, validStamp, logger, warmupTime, pusher.DefaultRetryCount)
973+
pusherService := pusher.New(networkID, localStore, pushSyncProtocol, batchStore, logger, warmupTime, pusher.DefaultRetryCount)
968974
b.pusherCloser = pusherService
969975

970976
pusherService.AddFeed(localStore.PusherFeed())

pkg/p2p/libp2p/internal/reacher/reacher.go

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818

1919
const (
2020
pingTimeout = time.Second * 15
21-
workers = 8
21+
workers = 16
2222
retryAfterDuration = time.Minute * 5
2323
)
2424

@@ -32,8 +32,8 @@ type reacher struct {
3232
mu sync.Mutex
3333
peers map[string]*peer
3434

35-
work chan struct{}
36-
quit chan struct{}
35+
newPeer chan struct{}
36+
quit chan struct{}
3737

3838
pinger p2p.Pinger
3939
notifier p2p.ReachableNotifier
@@ -53,7 +53,7 @@ type Options struct {
5353
func New(streamer p2p.Pinger, notifier p2p.ReachableNotifier, o *Options) *reacher {
5454

5555
r := &reacher{
56-
work: make(chan struct{}, 1),
56+
newPeer: make(chan struct{}, 1),
5757
quit: make(chan struct{}),
5858
pinger: streamer,
5959
peers: make(map[string]*peer),
@@ -103,7 +103,7 @@ func (r *reacher) manage() {
103103
select {
104104
case <-r.quit:
105105
return
106-
case <-r.work:
106+
case <-r.newPeer:
107107
continue
108108
case <-time.After(tryAfter):
109109
continue
@@ -115,12 +115,12 @@ func (r *reacher) manage() {
115115
select {
116116
case <-r.quit:
117117
return
118-
case <-r.work:
118+
case <-r.newPeer:
119119
continue
120120
}
121121
}
122122

123-
// send p to channel
123+
// ping peer
124124
select {
125125
case <-r.quit:
126126
return
@@ -135,10 +135,6 @@ func (r *reacher) ping(c chan *peer, ctx context.Context) {
135135

136136
for p := range c {
137137

138-
r.mu.Lock()
139-
overlay := p.overlay
140-
r.mu.Unlock()
141-
142138
now := time.Now()
143139

144140
ctxt, cancel := context.WithTimeout(ctx, r.options.PingTimeout)
@@ -149,14 +145,12 @@ func (r *reacher) ping(c chan *peer, ctx context.Context) {
149145
if err == nil {
150146
r.metrics.Pings.WithLabelValues("success").Inc()
151147
r.metrics.PingTime.WithLabelValues("success").Observe(time.Since(now).Seconds())
152-
r.notifier.Reachable(overlay, p2p.ReachabilityStatusPublic)
148+
r.notifier.Reachable(p.overlay, p2p.ReachabilityStatusPublic)
153149
} else {
154150
r.metrics.Pings.WithLabelValues("failure").Inc()
155151
r.metrics.PingTime.WithLabelValues("failure").Observe(time.Since(now).Seconds())
156-
r.notifier.Reachable(overlay, p2p.ReachabilityStatusPrivate)
152+
r.notifier.Reachable(p.overlay, p2p.ReachabilityStatusPrivate)
157153
}
158-
159-
r.notifyManage()
160154
}
161155
}
162156

@@ -191,13 +185,6 @@ func (r *reacher) tryAcquirePeer() (*peer, time.Duration) {
191185
return nil, time.Until(nextClosest)
192186
}
193187

194-
func (r *reacher) notifyManage() {
195-
select {
196-
case r.work <- struct{}{}:
197-
default:
198-
}
199-
}
200-
201188
// Connected adds a new peer to the queue for testing reachability.
202189
func (r *reacher) Connected(overlay swarm.Address, addr ma.Multiaddr) {
203190
r.mu.Lock()
@@ -207,7 +194,10 @@ func (r *reacher) Connected(overlay swarm.Address, addr ma.Multiaddr) {
207194
r.peers[overlay.ByteString()] = &peer{overlay: overlay, addr: addr}
208195
}
209196

210-
r.notifyManage()
197+
select {
198+
case r.newPeer <- struct{}{}:
199+
default:
200+
}
211201
}
212202

213203
// Disconnected removes a peer from the queue.

pkg/postage/interface.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,13 @@ type ChainSnapshot struct {
4242
// on the current (highest available) block.
4343
type Storer interface {
4444
ChainStateGetter
45+
BatchExist
4546

4647
Radius() uint8
4748

4849
// Get returns a batch from the store with the given ID.
4950
Get([]byte) (*Batch, error)
5051

51-
// Exists reports whether batch referenced by the give id exists.
52-
Exists([]byte) (bool, error)
53-
5452
// Iterate iterates through stored batches.
5553
Iterate(func(*Batch) (bool, error)) error
5654

@@ -73,6 +71,11 @@ type Storer interface {
7371
SetBatchExpiryHandler(BatchExpiryHandler)
7472
}
7573

74+
type BatchExist interface {
75+
// Exists reports whether batch referenced by the give id exists.
76+
Exists([]byte) (bool, error)
77+
}
78+
7679
// StorageRadiusSetter is used to calculate total batch commitment of the network.
7780
type CommitmentGetter interface {
7881
Commitment() (uint64, error)

pkg/pusher/pusher.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type Service struct {
5151
networkID uint64
5252
storer Storer
5353
pushSyncer pushsync.PushSyncer
54-
validStamp postage.ValidStampFn
54+
batchExist postage.BatchExist
5555
logger log.Logger
5656
metrics metrics
5757
quit chan struct{}
@@ -63,19 +63,15 @@ type Service struct {
6363

6464
const (
6565
traceDuration = 30 * time.Second // duration for every root tracing span
66-
ConcurrentPushes = 100 // how many chunks to push simultaneously
66+
ConcurrentPushes = swarm.Branches // how many chunks to push simultaneously
6767
DefaultRetryCount = 6
6868
)
6969

70-
var (
71-
ErrInvalidAddress = errors.New("invalid address")
72-
)
73-
7470
func New(
7571
networkID uint64,
7672
storer Storer,
7773
pushSyncer pushsync.PushSyncer,
78-
validStamp postage.ValidStampFn,
74+
batchExist postage.BatchExist,
7975
logger log.Logger,
8076
warmupTime time.Duration,
8177
retryCount int,
@@ -84,7 +80,7 @@ func New(
8480
networkID: networkID,
8581
storer: storer,
8682
pushSyncer: pushSyncer,
87-
validStamp: validStamp,
83+
batchExist: batchExist,
8884
logger: logger.WithName(loggerName).Register(),
8985
metrics: newMetrics(),
9086
quit: make(chan struct{}),
@@ -251,14 +247,14 @@ func (s *Service) pushDeferred(ctx context.Context, logger log.Logger, op *Op) (
251247

252248
defer s.inflight.delete(op.identityAddress, op.Chunk.Stamp().BatchID())
253249

254-
if _, err := s.validStamp(op.Chunk); err != nil {
250+
ok, err := s.batchExist.Exists(op.Chunk.Stamp().BatchID())
251+
if !ok || err != nil {
255252
loggerV1.Warning(
256-
"stamp with is no longer valid, skipping syncing for chunk",
253+
"stamp is no longer valid, skipping syncing for chunk",
257254
"batch_id", hex.EncodeToString(op.Chunk.Stamp().BatchID()),
258255
"chunk_address", op.Chunk.Address(),
259256
"error", err,
260257
)
261-
262258
return false, errors.Join(err, s.storer.Report(ctx, op.Chunk, storage.ChunkCouldNotSync))
263259
}
264260

@@ -311,10 +307,10 @@ func (s *Service) pushDirect(ctx context.Context, logger log.Logger, op *Op) err
311307
}
312308
}()
313309

314-
_, err = s.validStamp(op.Chunk)
315-
if err != nil {
316-
logger.Warning(
317-
"stamp with is no longer valid, skipping direct upload for chunk",
310+
ok, err := s.batchExist.Exists(op.Chunk.Stamp().BatchID())
311+
if !ok || err != nil {
312+
loggerV1.Warning(
313+
"stamp is no longer valid, skipping direct upload for chunk",
318314
"batch_id", hex.EncodeToString(op.Chunk.Stamp().BatchID()),
319315
"chunk_address", op.Chunk.Address(),
320316
"error", err,

pkg/pusher/pusher_test.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/ethersphere/bee/v2/pkg/crypto"
1818
"github.com/ethersphere/bee/v2/pkg/log"
1919
"github.com/ethersphere/bee/v2/pkg/postage"
20+
batchstoremock "github.com/ethersphere/bee/v2/pkg/postage/batchstore/mock"
2021
"github.com/ethersphere/bee/v2/pkg/pusher"
2122
"github.com/ethersphere/bee/v2/pkg/pushsync"
2223
pushsyncmock "github.com/ethersphere/bee/v2/pkg/pushsync/mock"
@@ -33,9 +34,9 @@ const spinTimeout = time.Second * 3
3334

3435
var (
3536
block = common.HexToHash("0x1").Bytes()
36-
defaultMockValidStamp = func(ch swarm.Chunk) (swarm.Chunk, error) {
37-
return ch, nil
38-
}
37+
defaultMockBatchStore = batchstoremock.New(batchstoremock.WithExistsFunc(func(b []byte) (bool, error) {
38+
return true, nil
39+
}))
3940
defaultRetryCount = 3
4041
)
4142

@@ -134,7 +135,7 @@ func TestChunkSyncing(t *testing.T) {
134135
t,
135136
storer,
136137
pushSyncService,
137-
defaultMockValidStamp,
138+
defaultMockBatchStore,
138139
defaultRetryCount,
139140
)
140141

@@ -181,7 +182,7 @@ func TestChunkStored(t *testing.T) {
181182
t,
182183
storer,
183184
pushSyncService,
184-
defaultMockValidStamp,
185+
defaultMockBatchStore,
185186
defaultRetryCount,
186187
)
187188

@@ -239,7 +240,7 @@ func TestSendChunkAndReceiveInvalidReceipt(t *testing.T) {
239240
t,
240241
storer,
241242
pushSyncService,
242-
defaultMockValidStamp,
243+
defaultMockBatchStore,
243244
defaultRetryCount,
244245
)
245246

@@ -283,7 +284,7 @@ func TestSendChunkAndTimeoutinReceivingReceipt(t *testing.T) {
283284
t,
284285
storer,
285286
pushSyncService,
286-
defaultMockValidStamp,
287+
defaultMockBatchStore,
287288
defaultRetryCount,
288289
)
289290

@@ -326,7 +327,7 @@ func TestPusherRetryShallow(t *testing.T) {
326327
t,
327328
storer,
328329
pushSyncService,
329-
defaultMockValidStamp,
330+
defaultMockBatchStore,
330331
defaultRetryCount,
331332
)
332333

@@ -364,9 +365,10 @@ func TestChunkWithInvalidStampSkipped(t *testing.T) {
364365
})
365366

366367
wantErr := errors.New("dummy error")
367-
validStamp := func(ch swarm.Chunk) (swarm.Chunk, error) {
368-
return nil, wantErr
369-
}
368+
369+
bmock := batchstoremock.New(batchstoremock.WithExistsFunc(func(b []byte) (bool, error) {
370+
return false, wantErr
371+
}))
370372

371373
storer := &mockStorer{
372374
chunks: make(chan swarm.Chunk),
@@ -376,7 +378,7 @@ func TestChunkWithInvalidStampSkipped(t *testing.T) {
376378
t,
377379
storer,
378380
pushSyncService,
379-
validStamp,
381+
bmock,
380382
defaultRetryCount,
381383
)
382384

@@ -412,7 +414,7 @@ func createPusher(
412414
t *testing.T,
413415
storer pusher.Storer,
414416
pushSyncService pushsync.PushSyncer,
415-
validStamp postage.ValidStampFn,
417+
validStamp postage.BatchExist,
416418
retryCount int,
417419
) *pusher.Service {
418420
t.Helper()

0 commit comments

Comments
 (0)