@@ -15,6 +15,7 @@ import (
1515 deciface "github.com/ipfs/go-bitswap/decision"
1616 bsbpm "github.com/ipfs/go-bitswap/internal/blockpresencemanager"
1717 "github.com/ipfs/go-bitswap/internal/decision"
18+ "github.com/ipfs/go-bitswap/internal/defaults"
1819 bsgetter "github.com/ipfs/go-bitswap/internal/getter"
1920 bsmq "github.com/ipfs/go-bitswap/internal/messagequeue"
2021 "github.com/ipfs/go-bitswap/internal/notifications"
@@ -42,15 +43,6 @@ var sflog = log.Desugar()
4243
4344var _ exchange.SessionExchange = (* Bitswap )(nil )
4445
45- const (
46- // these requests take at _least_ two minutes at the moment.
47- provideTimeout = time .Minute * 3
48- defaultProvSearchDelay = time .Second
49-
50- // Number of concurrent workers in decision engine that process requests to the blockstore
51- defaulEngineBlockstoreWorkerCount = 128
52- )
53-
5446var (
5547 // HasBlockBufferSize is the buffer size of the channel for new blocks
5648 // that need to be provided. They should get pulled over by the
6254
6355 // the 1<<18+15 is to observe old file chunks that are 1<<18 + 14 in size
6456 metricsBuckets = []float64 {1 << 6 , 1 << 10 , 1 << 14 , 1 << 18 , 1 << 18 + 15 , 1 << 22 }
57+
58+ timeMetricsBuckets = []float64 {1 , 10 , 30 , 60 , 90 , 120 , 600 }
6559)
6660
6761// Option defines the functional option type that can be used to configure
@@ -100,6 +94,36 @@ func EngineBlockstoreWorkerCount(count int) Option {
10094 }
10195}
10296
97+ // EngineTaskWorkerCount sets the number of worker threads used inside the engine
98+ func EngineTaskWorkerCount (count int ) Option {
99+ if count <= 0 {
100+ panic (fmt .Sprintf ("Engine task worker count is %d but must be > 0" , count ))
101+ }
102+ return func (bs * Bitswap ) {
103+ bs .engineTaskWorkerCount = count
104+ }
105+ }
106+
107+ func TaskWorkerCount (count int ) Option {
108+ if count <= 0 {
109+ panic (fmt .Sprintf ("task worker count is %d but must be > 0" , count ))
110+ }
111+ return func (bs * Bitswap ) {
112+ bs .taskWorkerCount = count
113+ }
114+ }
115+
116+ // MaxOutstandingBytesPerPeer describes approximately how much work we are will to have outstanding to a peer at any
117+ // given time. Setting it to 0 will disable any limiting.
118+ func MaxOutstandingBytesPerPeer (count int ) Option {
119+ if count < 0 {
120+ panic (fmt .Sprintf ("max outstanding bytes per peer is %d but must be >= 0" , count ))
121+ }
122+ return func (bs * Bitswap ) {
123+ bs .engineMaxOutstandingBytesPerPeer = count
124+ }
125+ }
126+
103127// SetSendDontHaves indicates what to do when the engine receives a want-block
104128// for a block that is not in the blockstore. Either
105129// - Send a DONT_HAVE message
@@ -147,6 +171,17 @@ func New(parent context.Context, network bsnet.BitSwapNetwork,
147171 sentHistogram := metrics .NewCtx (ctx , "sent_all_blocks_bytes" , "Histogram of blocks sent by" +
148172 " this bitswap" ).Histogram (metricsBuckets )
149173
174+ sendTimeHistogram := metrics .NewCtx (ctx , "send_times" , "Histogram of how long it takes to send messages" +
175+ " in this bitswap" ).Histogram (timeMetricsBuckets )
176+
177+ pendingEngineGauge := metrics .NewCtx (ctx , "pending_tasks" , "Total number of pending tasks" ).Gauge ()
178+
179+ activeEngineGauge := metrics .NewCtx (ctx , "active_tasks" , "Total number of active tasks" ).Gauge ()
180+
181+ pendingBlocksGauge := metrics .NewCtx (ctx , "pending_block_tasks" , "Total number of pending blockstore tasks" ).Gauge ()
182+
183+ activeBlocksGauge := metrics .NewCtx (ctx , "active_block_tasks" , "Total number of active blockstore tasks" ).Gauge ()
184+
150185 px := process .WithTeardown (func () error {
151186 return nil
152187 })
@@ -192,26 +227,30 @@ func New(parent context.Context, network bsnet.BitSwapNetwork,
192227 sm = bssm .New (ctx , sessionFactory , sim , sessionPeerManagerFactory , bpm , pm , notif , network .Self ())
193228
194229 bs = & Bitswap {
195- blockstore : bstore ,
196- network : network ,
197- process : px ,
198- newBlocks : make (chan cid.Cid , HasBlockBufferSize ),
199- provideKeys : make (chan cid.Cid , provideKeysBufferSize ),
200- pm : pm ,
201- pqm : pqm ,
202- sm : sm ,
203- sim : sim ,
204- notif : notif ,
205- counters : new (counters ),
206- dupMetric : dupHist ,
207- allMetric : allHist ,
208- sentHistogram : sentHistogram ,
209- provideEnabled : true ,
210- provSearchDelay : defaultProvSearchDelay ,
211- rebroadcastDelay : delay .Fixed (time .Minute ),
212- engineBstoreWorkerCount : defaulEngineBlockstoreWorkerCount ,
213- engineSetSendDontHaves : true ,
214- simulateDontHavesOnTimeout : true ,
230+ blockstore : bstore ,
231+ network : network ,
232+ process : px ,
233+ newBlocks : make (chan cid.Cid , HasBlockBufferSize ),
234+ provideKeys : make (chan cid.Cid , provideKeysBufferSize ),
235+ pm : pm ,
236+ pqm : pqm ,
237+ sm : sm ,
238+ sim : sim ,
239+ notif : notif ,
240+ counters : new (counters ),
241+ dupMetric : dupHist ,
242+ allMetric : allHist ,
243+ sentHistogram : sentHistogram ,
244+ sendTimeHistogram : sendTimeHistogram ,
245+ provideEnabled : true ,
246+ provSearchDelay : defaults .ProvSearchDelay ,
247+ rebroadcastDelay : delay .Fixed (time .Minute ),
248+ engineBstoreWorkerCount : defaults .BitswapEngineBlockstoreWorkerCount ,
249+ engineTaskWorkerCount : defaults .BitswapEngineTaskWorkerCount ,
250+ taskWorkerCount : defaults .BitswapTaskWorkerCount ,
251+ engineMaxOutstandingBytesPerPeer : defaults .BitswapMaxOutstandingBytesPerPeer ,
252+ engineSetSendDontHaves : true ,
253+ simulateDontHavesOnTimeout : true ,
215254 }
216255
217256 // apply functional options before starting and running bitswap
@@ -220,7 +259,20 @@ func New(parent context.Context, network bsnet.BitSwapNetwork,
220259 }
221260
222261 // Set up decision engine
223- bs .engine = decision .NewEngine (bstore , bs .engineBstoreWorkerCount , network .ConnectionManager (), network .Self (), bs .engineScoreLedger )
262+ bs .engine = decision .NewEngine (
263+ ctx ,
264+ bstore ,
265+ bs .engineBstoreWorkerCount ,
266+ bs .engineTaskWorkerCount ,
267+ bs .engineMaxOutstandingBytesPerPeer ,
268+ network .ConnectionManager (),
269+ network .Self (),
270+ bs .engineScoreLedger ,
271+ pendingEngineGauge ,
272+ activeEngineGauge ,
273+ pendingBlocksGauge ,
274+ activeBlocksGauge ,
275+ )
224276 bs .engine .SetSendDontHaves (bs .engineSetSendDontHaves )
225277
226278 bs .pqm .Startup ()
@@ -277,9 +329,10 @@ type Bitswap struct {
277329 counters * counters
278330
279331 // Metrics interface metrics
280- dupMetric metrics.Histogram
281- allMetric metrics.Histogram
282- sentHistogram metrics.Histogram
332+ dupMetric metrics.Histogram
333+ allMetric metrics.Histogram
334+ sentHistogram metrics.Histogram
335+ sendTimeHistogram metrics.Histogram
283336
284337 // External statistics interface
285338 wiretap WireTap
@@ -303,6 +356,15 @@ type Bitswap struct {
303356 // how many worker threads to start for decision engine blockstore worker
304357 engineBstoreWorkerCount int
305358
359+ // how many worker threads to start for decision engine task worker
360+ engineTaskWorkerCount int
361+
362+ // the total number of simultaneous threads sending outgoing messages
363+ taskWorkerCount int
364+
365+ // the total amount of bytes that a peer should have outstanding, it is utilized by the decision engine
366+ engineMaxOutstandingBytesPerPeer int
367+
306368 // the score ledger used by the decision engine
307369 engineScoreLedger deciface.ScoreLedger
308370
0 commit comments