@cryptotaxi247 / kubo / commits / 3d527753f

feat: enable Resource Manager by default

Gus Eggert committed May 4, 2022 at 18:49 UTC 3d527753ff4ee476ee06be283ef8c80e7610f98b
6 files changed +61 -55
config/swarm.go
+1 -1
@@ -138,7 +138,7 @@ type ConnMgr struct {
138 // ResourceMgr defines configuration options for the libp2p Network Resource Manager
139 // <https://github.com/libp2p/go-libp2p-resource-manager#readme>
140 type ResourceMgr struct {
141 - // Enables the Network Resource Manager feature
141 + // Enables the Network Resource Manager feature, default to on.
142 Enabled Flag `json:",omitempty"`
143 Limits *rcmgr.BasicLimiterConfig `json:",omitempty"`
144 }
core/node/libp2p/rcmgr.go
+1 -2
@@ -29,8 +29,7 @@ func ResourceManager(cfg config.SwarmConfig) func(fx.Lifecycle, repo.Repo) (netw
29 var manager network.ResourceManager
30 var opts Libp2pOpts
31
32 - // Config Swarm.ResourceMgr.Enabled decides if we run a real manager
33 - enabled := cfg.ResourceMgr.Enabled.WithDefault(false)
32 + enabled := cfg.ResourceMgr.Enabled.WithDefault(true)
33
34 /// ENV overrides Config (if present)
35 switch os.Getenv("LIBP2P_RCMGR") {
core/node/libp2p/rcmgr_metrics.go
+26 -14
@@ -1,6 +1,7 @@
1 package libp2p
2
3 import (
4 + "errors"
5 "strconv"
6
7 "github.com/libp2p/go-libp2p-core/network"
@@ -11,6 +12,17 @@ import (
12 "github.com/prometheus/client_golang/prometheus"
13 )
14
15 +func mustRegister(c prometheus.Collector) {
16 + err := prometheus.Register(c)
17 + are := prometheus.AlreadyRegisteredError{}
18 + if errors.As(err, &are) {
19 + return
20 + }
21 + if err != nil {
22 + panic(err)
23 + }
24 +}
25 +
26 func createRcmgrMetrics() rcmgr.MetricsReporter {
27 const (
28 direction = "direction"
@@ -26,7 +38,7 @@ func createRcmgrMetrics() rcmgr.MetricsReporter {
38 },
39 []string{direction, usesFD},
40 )
29 - prometheus.MustRegister(connAllowed)
41 + mustRegister(connAllowed)
42
43 connBlocked := prometheus.NewCounterVec(
44 prometheus.CounterOpts{
@@ -35,7 +47,7 @@ func createRcmgrMetrics() rcmgr.MetricsReporter {
47 },
48 []string{direction, usesFD},
49 )
38 - prometheus.MustRegister(connBlocked)
50 + mustRegister(connBlocked)
51
52 streamAllowed := prometheus.NewCounterVec(
53 prometheus.CounterOpts{
@@ -44,7 +56,7 @@ func createRcmgrMetrics() rcmgr.MetricsReporter {
56 },
57 []string{direction},
58 )
47 - prometheus.MustRegister(streamAllowed)
59 + mustRegister(streamAllowed)
60
61 streamBlocked := prometheus.NewCounterVec(
62 prometheus.CounterOpts{
@@ -53,19 +65,19 @@ func createRcmgrMetrics() rcmgr.MetricsReporter {
65 },
66 []string{direction},
67 )
56 - prometheus.MustRegister(streamBlocked)
68 + mustRegister(streamBlocked)
69
70 peerAllowed := prometheus.NewCounter(prometheus.CounterOpts{
71 Name: "libp2p_rcmgr_peers_allowed_total",
72 Help: "allowed peers",
73 })
62 - prometheus.MustRegister(peerAllowed)
74 + mustRegister(peerAllowed)
75
76 peerBlocked := prometheus.NewCounter(prometheus.CounterOpts{
77 Name: "libp2p_rcmgr_peer_blocked_total",
78 Help: "blocked peers",
79 })
68 - prometheus.MustRegister(peerBlocked)
80 + mustRegister(peerBlocked)
81
82 protocolAllowed := prometheus.NewCounterVec(
83 prometheus.CounterOpts{
@@ -74,7 +86,7 @@ func createRcmgrMetrics() rcmgr.MetricsReporter {
86 },
87 []string{protocol},
88 )
77 - prometheus.MustRegister(protocolAllowed)
89 + mustRegister(protocolAllowed)
90
91 protocolBlocked := prometheus.NewCounterVec(
92 prometheus.CounterOpts{
@@ -83,7 +95,7 @@ func createRcmgrMetrics() rcmgr.MetricsReporter {
95 },
96 []string{protocol},
97 )
86 - prometheus.MustRegister(protocolBlocked)
98 + mustRegister(protocolBlocked)
99
100 protocolPeerBlocked := prometheus.NewCounterVec(
101 prometheus.CounterOpts{
@@ -92,7 +104,7 @@ func createRcmgrMetrics() rcmgr.MetricsReporter {
104 },
105 []string{protocol},
106 )
95 - prometheus.MustRegister(protocolPeerBlocked)
107 + mustRegister(protocolPeerBlocked)
108
109 serviceAllowed := prometheus.NewCounterVec(
110 prometheus.CounterOpts{
@@ -101,7 +113,7 @@ func createRcmgrMetrics() rcmgr.MetricsReporter {
113 },
114 []string{service},
115 )
104 - prometheus.MustRegister(serviceAllowed)
116 + mustRegister(serviceAllowed)
117
118 serviceBlocked := prometheus.NewCounterVec(
119 prometheus.CounterOpts{
@@ -110,7 +122,7 @@ func createRcmgrMetrics() rcmgr.MetricsReporter {
122 },
123 []string{service},
124 )
113 - prometheus.MustRegister(serviceBlocked)
125 + mustRegister(serviceBlocked)
126
127 servicePeerBlocked := prometheus.NewCounterVec(
128 prometheus.CounterOpts{
@@ -119,19 +131,19 @@ func createRcmgrMetrics() rcmgr.MetricsReporter {
131 },
132 []string{service},
133 )
122 - prometheus.MustRegister(servicePeerBlocked)
134 + mustRegister(servicePeerBlocked)
135
136 memoryAllowed := prometheus.NewCounter(prometheus.CounterOpts{
137 Name: "libp2p_rcmgr_memory_allocations_allowed_total",
138 Help: "allowed memory allocations",
139 })
128 - prometheus.MustRegister(memoryAllowed)
140 + mustRegister(memoryAllowed)
141
142 memoryBlocked := prometheus.NewCounter(prometheus.CounterOpts{
143 Name: "libp2p_rcmgr_memory_allocations_blocked_total",
144 Help: "blocked memory allocations",
145 })
134 - prometheus.MustRegister(memoryBlocked)
146 + mustRegister(memoryBlocked)
147
148 return rcmgrMetrics{
149 connAllowed,
docs/config.md
+1 -4
@@ -1632,13 +1632,10 @@ The [libp2p Network Resource Manager](https://github.com/libp2p/go-libp2p-resour
1632 and tracking recource usage over time.
1633
1634 #### `Swarm.ResourceMgr.Enabled`
1635 -
1636 -**EXPERIMENTAL**: this feature is disabled by default, use with caution.
1637 -
1635 Enables the libp2p Network Resource Manager and auguments the default limits
1636 using user-defined ones in `Swarm.ResourceMgr.Limits` (if present).
1637
1641 -Default: `false`
1638 +Default: `true`
1639
1640 Type: `flag`
1641
test/sharness/t0116-prometheus-data/prometheus_metrics
+4
@@ -656,6 +656,10 @@ leveldb_datastore_sync_latency_seconds_bucket
656 leveldb_datastore_sync_latency_seconds_count
657 leveldb_datastore_sync_latency_seconds_sum
658 leveldb_datastore_sync_total
659 +libp2p_rcmgr_memory_allocations_allowed_total
660 +libp2p_rcmgr_memory_allocations_blocked_total
661 +libp2p_rcmgr_peer_blocked_total
662 +libp2p_rcmgr_peers_allowed_total
663 process_cpu_seconds_total
664 process_max_fds
665 process_open_fds
test/sharness/t0139-swarm-rcmgr.sh
+28 -34
@@ -17,40 +17,10 @@ test_expect_success 'disconnected: swarm stats requires running daemon' '
17 test_should_contain "missing ResourceMgr" actual
18 '
19
20 -# swarm limit|stats should fail in online mode by default
21 -# because Resource Manager is opt-in
20 +# swarm limit|stats should succeed in online mode by default
21 +# because Resource Manager is opt-out
22 test_launch_ipfs_daemon
23
24 -test_expect_success 'ResourceMgr disabled by default: swarm limit requires Swarm.ResourceMgr.Enabled' '
25 - test_expect_code 1 ipfs swarm limit system 2> actual &&
26 - test_should_contain "missing ResourceMgr" actual
27 -'
28 -test_expect_success 'ResourceMgr disabled by default: swarm stats requires Swarm.ResourceMgr.Enabled' '
29 - test_expect_code 1 ipfs swarm stats all 2> actual &&
30 - test_should_contain "missing ResourceMgr" actual
31 -'
32 -
33 -test_kill_ipfs_daemon
34 -
35 -test_expect_success "setting an invalid limit should result in a failure" "
36 - test_expect_code 1 ipfs config --json Swarm.ResourceMgr.Limits.System.Conns 'asdf' 2> actual &&
37 - test_should_contain 'failed to unmarshal' actual
38 -"
39 -
40 -# swarm limit|stat should work when Swarm.ResourceMgr.Enabled
41 -test_expect_success "test enabling resource manager" "
42 - ipfs config --json Swarm.ResourceMgr.Enabled true &&
43 - ipfs config --json Swarm.ResourceMgr &&
44 - jq -e '.Swarm.ResourceMgr.Enabled == true' < \"$IPFS_PATH/config\"
45 -"
46 -
47 -test_launch_ipfs_daemon
48 -
49 -test_expect_success "test setting system conns limit" "
50 - ipfs config --json Swarm.ResourceMgr.Enabled true &&
51 - ipfs config --json Swarm.ResourceMgr.Limits.System.Conns 99999
52 -"
53 -
24 # every scope has the same fields, so we only inspect System
25 test_expect_success 'ResourceMgr enabled: swarm limit' '
26 ipfs swarm limit system --enc=json | tee json &&
@@ -79,13 +49,18 @@ test_expect_success 'ResourceMgr enabled: swarm stats' '
49 # shut down the daemon, set a limit in the config, and verify that it's applied
50 test_kill_ipfs_daemon
51
82 -test_expect_success "set system conn limit" "
52 +test_expect_success "Set system conns limit while daemon is not running" "
53 ipfs config --json Swarm.ResourceMgr.Limits.System.Conns 99999
54 "
55
56 +test_expect_success "Set an invalid limit, which should result in a failure" "
57 + test_expect_code 1 ipfs config --json Swarm.ResourceMgr.Limits.System.Conns 'asdf' 2> actual &&
58 + test_should_contain 'failed to unmarshal' actual
59 +"
60 +
61 test_launch_ipfs_daemon
62
88 -test_expect_success 'ResourceMgr enabled: swarm limit' '
63 +test_expect_success 'Ensure the new system conns limit is applied' '
64 ipfs swarm limit system --enc=json | tee json &&
65 jq -e ".Conns == 99999" < json
66 '
@@ -152,4 +127,23 @@ test_expect_success 'Set limit for peer scope with an invalid peer ID' '
127
128 test_kill_ipfs_daemon
129
130 +# test correct behavior when resource manager is disabled
131 +test_expect_success 'Disable resource manager' '
132 + ipfs config --bool Swarm.ResourceMgr.Enabled false
133 +'
134 +
135 +test_launch_ipfs_daemon
136 +
137 +test_expect_success 'Swarm limit should fail since RM is disabled' '
138 + test_expect_code 1 ipfs swarm limit system 2> actual &&
139 + test_should_contain "missing ResourceMgr" actual
140 +'
141 +
142 +test_expect_success 'Swarm stats should fail since RM is disabled' '
143 + test_expect_code 1 ipfs swarm stats all 2> actual &&
144 + test_should_contain "missing ResourceMgr" actual
145 +'
146 +
147 +test_kill_ipfs_daemon
148 +
149 test_done