From f4905ca422ab863da8abd29cba90c793e042e272 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sat, 25 Feb 2023 10:53:20 +1300 Subject: [PATCH 1/2] relay: initialize the ASN util when starting the service --- go.mod | 2 +- go.sum | 4 ++-- p2p/protocol/circuitv2/relay/relay.go | 3 +++ test-plans/go.mod | 2 +- test-plans/go.sum | 4 ++-- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index ac5631de42..617b820b43 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/klauspost/compress v1.15.12 github.com/libp2p/go-buffer-pool v0.1.0 github.com/libp2p/go-flow-metrics v0.1.0 - github.com/libp2p/go-libp2p-asn-util v0.2.0 + github.com/libp2p/go-libp2p-asn-util v0.3.0 github.com/libp2p/go-libp2p-testing v0.12.0 github.com/libp2p/go-mplex v0.7.0 github.com/libp2p/go-msgio v0.3.0 diff --git a/go.sum b/go.sum index 5509e55183..6bfd02a819 100644 --- a/go.sum +++ b/go.sum @@ -318,8 +318,8 @@ github.com/libp2p/go-cidranger v1.1.0 h1:ewPN8EZ0dd1LSnrtuwd4709PXVcITVeuwbag38y github.com/libp2p/go-cidranger v1.1.0/go.mod h1:KWZTfSr+r9qEo9OkI9/SIEeAtw+NNoU0dXIXt15Okic= github.com/libp2p/go-flow-metrics v0.1.0 h1:0iPhMI8PskQwzh57jB9WxIuIOQ0r+15PChFGkx3Q3WM= github.com/libp2p/go-flow-metrics v0.1.0/go.mod h1:4Xi8MX8wj5aWNDAZttg6UPmc0ZrnFNsMtpsYUClFtro= -github.com/libp2p/go-libp2p-asn-util v0.2.0 h1:rg3+Os8jbnO5DxkC7K/Utdi+DkY3q/d1/1q+8WeNAsw= -github.com/libp2p/go-libp2p-asn-util v0.2.0/go.mod h1:WoaWxbHKBymSN41hWSq/lGKJEca7TNm58+gGJi2WsLI= +github.com/libp2p/go-libp2p-asn-util v0.3.0 h1:gMDcMyYiZKkocGXDQ5nsUQyquC9+H+iLEQHwOCZ7s8s= +github.com/libp2p/go-libp2p-asn-util v0.3.0/go.mod h1:B1mcOrKUE35Xq/ASTmQ4tN3LNzVVaMNmq2NACuqyB9w= github.com/libp2p/go-libp2p-testing v0.12.0 h1:EPvBb4kKMWO29qP4mZGyhVzUyR25dvfUIK5WDu6iPUA= github.com/libp2p/go-libp2p-testing v0.12.0/go.mod h1:KcGDRXyN7sQCllucn1cOOS+Dmm7ujhfEyXQL5lvkcPg= github.com/libp2p/go-mplex v0.7.0 h1:BDhFZdlk5tbr0oyFq/xv/NPGfjbnrsDam1EvutpBDbY= diff --git a/p2p/protocol/circuitv2/relay/relay.go b/p2p/protocol/circuitv2/relay/relay.go index ca50d42469..ee6f5528f4 100644 --- a/p2p/protocol/circuitv2/relay/relay.go +++ b/p2p/protocol/circuitv2/relay/relay.go @@ -18,6 +18,7 @@ import ( logging "github.com/ipfs/go-log/v2" pool "github.com/libp2p/go-buffer-pool" + asnutil "github.com/libp2p/go-libp2p-asn-util" ma "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr/net" ) @@ -545,6 +546,8 @@ func (r *Relay) makeLimitMsg(p peer.ID) *pbv2.Limit { } func (r *Relay) background() { + asnutil.Store.Init() + ticker := time.NewTicker(time.Minute) defer ticker.Stop() diff --git a/test-plans/go.mod b/test-plans/go.mod index 5c0445263f..2d60359b61 100644 --- a/test-plans/go.mod +++ b/test-plans/go.mod @@ -40,7 +40,7 @@ require ( github.com/libp2p/go-buffer-pool v0.1.0 // indirect github.com/libp2p/go-cidranger v1.1.0 // indirect github.com/libp2p/go-flow-metrics v0.1.0 // indirect - github.com/libp2p/go-libp2p-asn-util v0.2.0 // indirect + github.com/libp2p/go-libp2p-asn-util v0.3.0 // indirect github.com/libp2p/go-mplex v0.7.0 // indirect github.com/libp2p/go-msgio v0.3.0 // indirect github.com/libp2p/go-nat v0.1.0 // indirect diff --git a/test-plans/go.sum b/test-plans/go.sum index 08138b32c5..a20af0bb30 100644 --- a/test-plans/go.sum +++ b/test-plans/go.sum @@ -280,8 +280,8 @@ github.com/libp2p/go-cidranger v1.1.0 h1:ewPN8EZ0dd1LSnrtuwd4709PXVcITVeuwbag38y github.com/libp2p/go-cidranger v1.1.0/go.mod h1:KWZTfSr+r9qEo9OkI9/SIEeAtw+NNoU0dXIXt15Okic= github.com/libp2p/go-flow-metrics v0.1.0 h1:0iPhMI8PskQwzh57jB9WxIuIOQ0r+15PChFGkx3Q3WM= github.com/libp2p/go-flow-metrics v0.1.0/go.mod h1:4Xi8MX8wj5aWNDAZttg6UPmc0ZrnFNsMtpsYUClFtro= -github.com/libp2p/go-libp2p-asn-util v0.2.0 h1:rg3+Os8jbnO5DxkC7K/Utdi+DkY3q/d1/1q+8WeNAsw= -github.com/libp2p/go-libp2p-asn-util v0.2.0/go.mod h1:WoaWxbHKBymSN41hWSq/lGKJEca7TNm58+gGJi2WsLI= +github.com/libp2p/go-libp2p-asn-util v0.3.0 h1:gMDcMyYiZKkocGXDQ5nsUQyquC9+H+iLEQHwOCZ7s8s= +github.com/libp2p/go-libp2p-asn-util v0.3.0/go.mod h1:B1mcOrKUE35Xq/ASTmQ4tN3LNzVVaMNmq2NACuqyB9w= github.com/libp2p/go-libp2p-testing v0.12.0 h1:EPvBb4kKMWO29qP4mZGyhVzUyR25dvfUIK5WDu6iPUA= github.com/libp2p/go-mplex v0.7.0 h1:BDhFZdlk5tbr0oyFq/xv/NPGfjbnrsDam1EvutpBDbY= github.com/libp2p/go-mplex v0.7.0/go.mod h1:rW8ThnRcYWft/Jb2jeORBmPd6xuG3dGxWN/W168L9EU= From 4de4b8993c0ab4662c2ec2a0f675ebced2ce58f4 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sat, 25 Feb 2023 10:53:36 +1300 Subject: [PATCH 2/2] Revert "Fix flaky TestMetricsNoAllocNoCover test (#2142)" This reverts commit cbcdd7908474784d4a79452e6b33faefaff566e4. --- p2p/protocol/identify/id.go | 16 ++++++++-------- p2p/protocol/identify/metrics.go | 12 ++++++------ .../{metrics_alloc_test => }/metrics_test.go | 19 ++++++------------- 3 files changed, 20 insertions(+), 27 deletions(-) rename p2p/protocol/identify/{metrics_alloc_test => }/metrics_test.go (61%) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 91c351e5bd..72d591ec55 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -81,12 +81,12 @@ type IDService interface { io.Closer } -type IdentifyPushSupport uint8 +type identifyPushSupport uint8 const ( - IdentifyPushSupportUnknown IdentifyPushSupport = iota - IdentifyPushSupported - IdentifyPushUnsupported + identifyPushSupportUnknown identifyPushSupport = iota + identifyPushSupported + identifyPushUnsupported ) type entry struct { @@ -96,7 +96,7 @@ type entry struct { // PushSupport saves our knowledge about the peer's support of the Identify Push protocol. // Before the identify request returns, we don't know yet if the peer supports Identify Push. - PushSupport IdentifyPushSupport + PushSupport identifyPushSupport // Sequence is the sequence number of the last snapshot we sent to this peer. Sequence uint64 } @@ -271,7 +271,7 @@ func (ids *idService) sendPushes(ctx context.Context) { for c, e := range ids.conns { // Push even if we don't know if push is supported. // This will be only the case while the IdentifyWaitChan call is in flight. - if e.PushSupport == IdentifyPushSupported || e.PushSupport == IdentifyPushSupportUnknown { + if e.PushSupport == identifyPushSupported || e.PushSupport == identifyPushSupportUnknown { conns = append(conns, c) } } @@ -496,9 +496,9 @@ func (ids *idService) handleIdentifyResponse(s network.Stream, isPush bool) erro } sup, err := ids.Host.Peerstore().SupportsProtocols(c.RemotePeer(), IDPush) if supportsIdentifyPush := err == nil && len(sup) > 0; supportsIdentifyPush { - e.PushSupport = IdentifyPushSupported + e.PushSupport = identifyPushSupported } else { - e.PushSupport = IdentifyPushUnsupported + e.PushSupport = identifyPushUnsupported } if ids.metricsTracer != nil { diff --git a/p2p/protocol/identify/metrics.go b/p2p/protocol/identify/metrics.go index f11a5851db..28598fa33b 100644 --- a/p2p/protocol/identify/metrics.go +++ b/p2p/protocol/identify/metrics.go @@ -95,7 +95,7 @@ type MetricsTracer interface { TriggeredPushes(event any) // ConnPushSupport counts peers by Push Support - ConnPushSupport(IdentifyPushSupport) + ConnPushSupport(identifyPushSupport) // IdentifyReceived tracks metrics on receiving an identify response IdentifyReceived(isPush bool, numProtocols int, numAddrs int) @@ -146,7 +146,7 @@ func (t *metricsTracer) TriggeredPushes(ev any) { pushesTriggered.WithLabelValues(*tags...).Inc() } -func (t *metricsTracer) IncrementPushSupport(s IdentifyPushSupport) { +func (t *metricsTracer) IncrementPushSupport(s identifyPushSupport) { tags := metricshelper.GetStringSlice() defer metricshelper.PutStringSlice(tags) @@ -186,7 +186,7 @@ func (t *metricsTracer) IdentifyReceived(isPush bool, numProtocols int, numAddrs numAddrsReceived.Observe(float64(numAddrs)) } -func (t *metricsTracer) ConnPushSupport(support IdentifyPushSupport) { +func (t *metricsTracer) ConnPushSupport(support identifyPushSupport) { tags := metricshelper.GetStringSlice() defer metricshelper.PutStringSlice(tags) @@ -194,11 +194,11 @@ func (t *metricsTracer) ConnPushSupport(support IdentifyPushSupport) { connPushSupportTotal.WithLabelValues(*tags...).Inc() } -func getPushSupport(s IdentifyPushSupport) string { +func getPushSupport(s identifyPushSupport) string { switch s { - case IdentifyPushSupported: + case identifyPushSupported: return "supported" - case IdentifyPushUnsupported: + case identifyPushUnsupported: return "not supported" default: return "unknown" diff --git a/p2p/protocol/identify/metrics_alloc_test/metrics_test.go b/p2p/protocol/identify/metrics_test.go similarity index 61% rename from p2p/protocol/identify/metrics_alloc_test/metrics_test.go rename to p2p/protocol/identify/metrics_test.go index 961daf1406..2cf5a209a1 100644 --- a/p2p/protocol/identify/metrics_alloc_test/metrics_test.go +++ b/p2p/protocol/identify/metrics_test.go @@ -1,18 +1,12 @@ //go:build nocover -// These tests are in their own package to avoid transitively pulling in other -// deps that may run background tasks in their init and thus allocate. Looking -// at you -// [go-libp2p-asn-util](https://github.com/libp2p/go-libp2p-asn-util/blob/master/asn.go#L14) - -package identify_alloc_test +package identify import ( "math/rand" "testing" "github.com/libp2p/go-libp2p/core/event" - "github.com/libp2p/go-libp2p/p2p/protocol/identify" ) func TestMetricsNoAllocNoCover(t *testing.T) { @@ -22,20 +16,19 @@ func TestMetricsNoAllocNoCover(t *testing.T) { event.EvtNATDeviceTypeChanged{}, } - pushSupport := []identify.IdentifyPushSupport{ - identify.IdentifyPushSupportUnknown, - identify.IdentifyPushSupported, - identify.IdentifyPushUnsupported, + pushSupport := []identifyPushSupport{ + identifyPushSupportUnknown, + identifyPushSupported, + identifyPushUnsupported, } - tr := identify.NewMetricsTracer() + tr := NewMetricsTracer() tests := map[string]func(){ "TriggeredPushes": func() { tr.TriggeredPushes(events[rand.Intn(len(events))]) }, "ConnPushSupport": func() { tr.ConnPushSupport(pushSupport[rand.Intn(len(pushSupport))]) }, "IdentifyReceived": func() { tr.IdentifyReceived(rand.Intn(2) == 0, rand.Intn(20), rand.Intn(20)) }, "IdentifySent": func() { tr.IdentifySent(rand.Intn(2) == 0, rand.Intn(20), rand.Intn(20)) }, } - for method, f := range tests { allocs := testing.AllocsPerRun(1000, f) if allocs > 0 {