Skip to content

Commit 1fb9a6d

Browse files
authored
eth/protocols, prp/tracker: add support for req/rep rtt tracking (#22608)
* eth/protocols, prp/tracker: add support for req/rep rtt tracking * p2p/tracker: sanity cap the number of pending requests * pap/tracker: linter <3 * p2p/tracker: disable entire tracker if no metrics are enabled
1 parent 9357280 commit 1fb9a6d

File tree

9 files changed

+309
-11
lines changed

9 files changed

+309
-11
lines changed

eth/protocols/eth/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func handleMessage(backend Backend, peer *Peer) error {
223223
if peer.Version() >= ETH66 {
224224
handlers = eth66
225225
}
226-
// Track the emount of time it takes to serve the request and run the handler
226+
// Track the amount of time it takes to serve the request and run the handler
227227
if metrics.Enabled {
228228
h := fmt.Sprintf("%s/%s/%d/%#02x", p2p.HandleHistName, ProtocolName, peer.Version(), msg.Code)
229229
defer func(start time.Time) {

eth/protocols/eth/handlers.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ func handleBlockHeaders66(backend Backend, msg Decoder, peer *Peer) error {
327327
if err := msg.Decode(res); err != nil {
328328
return fmt.Errorf("%w: message %v: %v", errDecode, msg, err)
329329
}
330+
requestTracker.Fulfil(peer.id, peer.version, BlockHeadersMsg, res.RequestId)
331+
330332
return backend.Handle(peer, &res.BlockHeadersPacket)
331333
}
332334

@@ -345,6 +347,8 @@ func handleBlockBodies66(backend Backend, msg Decoder, peer *Peer) error {
345347
if err := msg.Decode(res); err != nil {
346348
return fmt.Errorf("%w: message %v: %v", errDecode, msg, err)
347349
}
350+
requestTracker.Fulfil(peer.id, peer.version, BlockBodiesMsg, res.RequestId)
351+
348352
return backend.Handle(peer, &res.BlockBodiesPacket)
349353
}
350354

@@ -363,6 +367,8 @@ func handleNodeData66(backend Backend, msg Decoder, peer *Peer) error {
363367
if err := msg.Decode(res); err != nil {
364368
return fmt.Errorf("%w: message %v: %v", errDecode, msg, err)
365369
}
370+
requestTracker.Fulfil(peer.id, peer.version, NodeDataMsg, res.RequestId)
371+
366372
return backend.Handle(peer, &res.NodeDataPacket)
367373
}
368374

@@ -381,6 +387,8 @@ func handleReceipts66(backend Backend, msg Decoder, peer *Peer) error {
381387
if err := msg.Decode(res); err != nil {
382388
return fmt.Errorf("%w: message %v: %v", errDecode, msg, err)
383389
}
390+
requestTracker.Fulfil(peer.id, peer.version, ReceiptsMsg, res.RequestId)
391+
384392
return backend.Handle(peer, &res.ReceiptsPacket)
385393
}
386394

@@ -506,5 +514,7 @@ func handlePooledTransactions66(backend Backend, msg Decoder, peer *Peer) error
506514
}
507515
peer.markTransaction(tx.Hash())
508516
}
517+
requestTracker.Fulfil(peer.id, peer.version, PooledTransactionsMsg, txs.RequestId)
518+
509519
return backend.Handle(peer, &txs.PooledTransactionsPacket)
510520
}

eth/protocols/eth/peer.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,11 @@ func (p *Peer) RequestOneHeader(hash common.Hash) error {
413413
Reverse: false,
414414
}
415415
if p.Version() >= ETH66 {
416+
id := rand.Uint64()
417+
418+
requestTracker.Track(p.id, p.version, GetBlockHeadersMsg, BlockHeadersMsg, id)
416419
return p2p.Send(p.rw, GetBlockHeadersMsg, &GetBlockHeadersPacket66{
417-
RequestId: rand.Uint64(),
420+
RequestId: id,
418421
GetBlockHeadersPacket: &query,
419422
})
420423
}
@@ -432,8 +435,11 @@ func (p *Peer) RequestHeadersByHash(origin common.Hash, amount int, skip int, re
432435
Reverse: reverse,
433436
}
434437
if p.Version() >= ETH66 {
438+
id := rand.Uint64()
439+
440+
requestTracker.Track(p.id, p.version, GetBlockHeadersMsg, BlockHeadersMsg, id)
435441
return p2p.Send(p.rw, GetBlockHeadersMsg, &GetBlockHeadersPacket66{
436-
RequestId: rand.Uint64(),
442+
RequestId: id,
437443
GetBlockHeadersPacket: &query,
438444
})
439445
}
@@ -451,8 +457,11 @@ func (p *Peer) RequestHeadersByNumber(origin uint64, amount int, skip int, rever
451457
Reverse: reverse,
452458
}
453459
if p.Version() >= ETH66 {
460+
id := rand.Uint64()
461+
462+
requestTracker.Track(p.id, p.version, GetBlockHeadersMsg, BlockHeadersMsg, id)
454463
return p2p.Send(p.rw, GetBlockHeadersMsg, &GetBlockHeadersPacket66{
455-
RequestId: rand.Uint64(),
464+
RequestId: id,
456465
GetBlockHeadersPacket: &query,
457466
})
458467
}
@@ -476,8 +485,11 @@ func (p *Peer) ExpectRequestHeadersByNumber(origin uint64, amount int, skip int,
476485
func (p *Peer) RequestBodies(hashes []common.Hash) error {
477486
p.Log().Debug("Fetching batch of block bodies", "count", len(hashes))
478487
if p.Version() >= ETH66 {
488+
id := rand.Uint64()
489+
490+
requestTracker.Track(p.id, p.version, GetBlockBodiesMsg, BlockBodiesMsg, id)
479491
return p2p.Send(p.rw, GetBlockBodiesMsg, &GetBlockBodiesPacket66{
480-
RequestId: rand.Uint64(),
492+
RequestId: id,
481493
GetBlockBodiesPacket: hashes,
482494
})
483495
}
@@ -489,8 +501,11 @@ func (p *Peer) RequestBodies(hashes []common.Hash) error {
489501
func (p *Peer) RequestNodeData(hashes []common.Hash) error {
490502
p.Log().Debug("Fetching batch of state data", "count", len(hashes))
491503
if p.Version() >= ETH66 {
504+
id := rand.Uint64()
505+
506+
requestTracker.Track(p.id, p.version, GetNodeDataMsg, NodeDataMsg, id)
492507
return p2p.Send(p.rw, GetNodeDataMsg, &GetNodeDataPacket66{
493-
RequestId: rand.Uint64(),
508+
RequestId: id,
494509
GetNodeDataPacket: hashes,
495510
})
496511
}
@@ -501,8 +516,11 @@ func (p *Peer) RequestNodeData(hashes []common.Hash) error {
501516
func (p *Peer) RequestReceipts(hashes []common.Hash) error {
502517
p.Log().Debug("Fetching batch of receipts", "count", len(hashes))
503518
if p.Version() >= ETH66 {
519+
id := rand.Uint64()
520+
521+
requestTracker.Track(p.id, p.version, GetReceiptsMsg, ReceiptsMsg, id)
504522
return p2p.Send(p.rw, GetReceiptsMsg, &GetReceiptsPacket66{
505-
RequestId: rand.Uint64(),
523+
RequestId: id,
506524
GetReceiptsPacket: hashes,
507525
})
508526
}
@@ -513,8 +531,11 @@ func (p *Peer) RequestReceipts(hashes []common.Hash) error {
513531
func (p *Peer) RequestTxs(hashes []common.Hash) error {
514532
p.Log().Debug("Fetching batch of transactions", "count", len(hashes))
515533
if p.Version() >= ETH66 {
534+
id := rand.Uint64()
535+
536+
requestTracker.Track(p.id, p.version, GetPooledTransactionsMsg, PooledTransactionsMsg, id)
516537
return p2p.Send(p.rw, GetPooledTransactionsMsg, &GetPooledTransactionsPacket66{
517-
RequestId: rand.Uint64(),
538+
RequestId: id,
518539
GetPooledTransactionsPacket: hashes,
519540
})
520541
}

eth/protocols/eth/tracker.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2021 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package eth
18+
19+
import (
20+
"time"
21+
22+
"github.com/ethereum/go-ethereum/p2p/tracker"
23+
)
24+
25+
// requestTracker is a singleton tracker for eth/66 and newer request times.
26+
var requestTracker = tracker.New(ProtocolName, 5*time.Minute)

eth/protocols/snap/handler.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ func handleMessage(backend Backend, peer *Peer) error {
227227
return fmt.Errorf("accounts not monotonically increasing: #%d [%x] vs #%d [%x]", i-1, res.Accounts[i-1].Hash[:], i, res.Accounts[i].Hash[:])
228228
}
229229
}
230+
requestTracker.Fulfil(peer.id, peer.version, AccountRangeMsg, res.ID)
231+
230232
return backend.Handle(peer, res)
231233

232234
case msg.Code == GetStorageRangesMsg:
@@ -360,6 +362,8 @@ func handleMessage(backend Backend, peer *Peer) error {
360362
}
361363
}
362364
}
365+
requestTracker.Fulfil(peer.id, peer.version, StorageRangesMsg, res.ID)
366+
363367
return backend.Handle(peer, res)
364368

365369
case msg.Code == GetByteCodesMsg:
@@ -404,6 +408,8 @@ func handleMessage(backend Backend, peer *Peer) error {
404408
if err := msg.Decode(res); err != nil {
405409
return fmt.Errorf("%w: message %v: %v", errDecode, msg, err)
406410
}
411+
requestTracker.Fulfil(peer.id, peer.version, ByteCodesMsg, res.ID)
412+
407413
return backend.Handle(peer, res)
408414

409415
case msg.Code == GetTrieNodesMsg:
@@ -497,6 +503,8 @@ func handleMessage(backend Backend, peer *Peer) error {
497503
if err := msg.Decode(res); err != nil {
498504
return fmt.Errorf("%w: message %v: %v", errDecode, msg, err)
499505
}
506+
requestTracker.Fulfil(peer.id, peer.version, TrieNodesMsg, res.ID)
507+
500508
return backend.Handle(peer, res)
501509

502510
default:

eth/protocols/snap/peer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ func (p *Peer) Log() log.Logger {
6565
// trie, starting with the origin.
6666
func (p *Peer) RequestAccountRange(id uint64, root common.Hash, origin, limit common.Hash, bytes uint64) error {
6767
p.logger.Trace("Fetching range of accounts", "reqid", id, "root", root, "origin", origin, "limit", limit, "bytes", common.StorageSize(bytes))
68+
69+
requestTracker.Track(p.id, p.version, GetAccountRangeMsg, AccountRangeMsg, id)
6870
return p2p.Send(p.rw, GetAccountRangeMsg, &GetAccountRangePacket{
6971
ID: id,
7072
Root: root,
@@ -83,6 +85,7 @@ func (p *Peer) RequestStorageRanges(id uint64, root common.Hash, accounts []comm
8385
} else {
8486
p.logger.Trace("Fetching ranges of small storage slots", "reqid", id, "root", root, "accounts", len(accounts), "first", accounts[0], "bytes", common.StorageSize(bytes))
8587
}
88+
requestTracker.Track(p.id, p.version, GetStorageRangesMsg, StorageRangesMsg, id)
8689
return p2p.Send(p.rw, GetStorageRangesMsg, &GetStorageRangesPacket{
8790
ID: id,
8891
Root: root,
@@ -96,6 +99,8 @@ func (p *Peer) RequestStorageRanges(id uint64, root common.Hash, accounts []comm
9699
// RequestByteCodes fetches a batch of bytecodes by hash.
97100
func (p *Peer) RequestByteCodes(id uint64, hashes []common.Hash, bytes uint64) error {
98101
p.logger.Trace("Fetching set of byte codes", "reqid", id, "hashes", len(hashes), "bytes", common.StorageSize(bytes))
102+
103+
requestTracker.Track(p.id, p.version, GetByteCodesMsg, ByteCodesMsg, id)
99104
return p2p.Send(p.rw, GetByteCodesMsg, &GetByteCodesPacket{
100105
ID: id,
101106
Hashes: hashes,
@@ -107,6 +112,8 @@ func (p *Peer) RequestByteCodes(id uint64, hashes []common.Hash, bytes uint64) e
107112
// a specificstate trie.
108113
func (p *Peer) RequestTrieNodes(id uint64, root common.Hash, paths []TrieNodePathSet, bytes uint64) error {
109114
p.logger.Trace("Fetching set of trie nodes", "reqid", id, "root", root, "pathsets", len(paths), "bytes", common.StorageSize(bytes))
115+
116+
requestTracker.Track(p.id, p.version, GetTrieNodesMsg, TrieNodesMsg, id)
110117
return p2p.Send(p.rw, GetTrieNodesMsg, &GetTrieNodesPacket{
111118
ID: id,
112119
Root: root,

eth/protocols/snap/tracker.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2021 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package snap
18+
19+
import (
20+
"time"
21+
22+
"github.com/ethereum/go-ethereum/p2p/tracker"
23+
)
24+
25+
// requestTracker is a singleton tracker for request times.
26+
var requestTracker = tracker.New(ProtocolName, time.Minute)

p2p/metrics.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ const (
3333

3434
// HandleHistName is the prefix of the per-packet serving time histograms.
3535
HandleHistName = "p2p/handle"
36-
37-
// WaitHistName is the prefix of the per-packet (req only) waiting time histograms.
38-
WaitHistName = "p2p/wait"
3936
)
4037

4138
var (

0 commit comments

Comments
 (0)