Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move go-libp2p-resource-manager here #1564

Closed
wants to merge 118 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
118 commits
Select commit Hold shift + click to select a range
f6ede80
basic resource scopes
vyzo Dec 22, 2021
7a0a266
improve memory checking, adjust GrowBuffer signatures
vyzo Dec 22, 2021
a22a0ca
add helper method to release all buffers
vyzo Dec 22, 2021
7121cd2
sanity check ReleaseMemory as well
vyzo Dec 22, 2021
8422805
add nil receiver safety to basic resource scopes
vyzo Dec 22, 2021
ea82da2
refactor scopes, introduce constrained scopes
vyzo Dec 23, 2021
3eadcb8
rename BasicResourceScope to Resources
vyzo Dec 23, 2021
0ffe21b
fix comment typo
vyzo Dec 23, 2021
5b9e80c
release buffer map at done
vyzo Dec 23, 2021
d97d328
embed mutex in scope impl, add constructors
vyzo Dec 23, 2021
82138ca
flesh out concrete scopes
vyzo Dec 23, 2021
2eef359
fix brainfart
vyzo Dec 23, 2021
f1bada6
stream service cannot be set before peer
vyzo Dec 23, 2021
9d3039e
only count fds if the connection actually uses an fd
vyzo Dec 23, 2021
5ad0a60
only allocate buffer map if the application requests a buffer, sanity…
vyzo Dec 23, 2021
0ee9be5
update scope stat definition
vyzo Dec 23, 2021
df41e36
flesh out resource manager
vyzo Dec 23, 2021
5942e5b
add ResourceManager constructor and peer/protocol scope gc
vyzo Dec 23, 2021
aa3cfae
implement scope viewers
vyzo Dec 23, 2021
f6946eb
move ref counting to resource scopes
vyzo Dec 23, 2021
c338609
make connection/stream Done idempotent
vyzo Dec 23, 2021
714f216
add missing done checks in ForChild methods
vyzo Dec 23, 2021
677e439
more descriptive invalid buffer error in Grow
vyzo Dec 23, 2021
8a2bbe3
update implementation for interface changes
vyzo Dec 24, 2021
040d83c
consistent ref counting
vyzo Dec 24, 2021
c556bbb
static limits
vyzo Dec 24, 2021
6a777b0
some cleanup for rcmgr scopes
vyzo Dec 26, 2021
7c1a775
consolidate Rerve/ReleaseForChild in aggregate method
vyzo Dec 26, 2021
f7a8e30
implement buffers
vyzo Dec 27, 2021
d14e4f2
add resources test
vyzo Dec 27, 2021
5543186
simplify stream/conn direction management
vyzo Dec 27, 2021
03407e5
more robust handling of transactions
vyzo Dec 27, 2021
f296c73
fix test
vyzo Dec 27, 2021
e48142e
add comment for the distinction between DAG and txn scopes
vyzo Dec 27, 2021
296f0a6
nil data when the scope is done
vyzo Dec 27, 2021
5a8ca01
basic resource scope tests
vyzo Dec 27, 2021
776a6d9
basic txn test, fix bugz
vyzo Dec 27, 2021
603f93c
moar txn tests
vyzo Dec 28, 2021
5876aed
add resource scope DAG test
vyzo Dec 28, 2021
55b9c51
more resource scope tests, fix bug
vyzo Dec 28, 2021
df51fad
resource manager test, fix bugz
vyzo Dec 28, 2021
a756c80
fix typo
vyzo Dec 28, 2021
555a843
remove unneded casts
vyzo Dec 28, 2021
46912fa
unexport resources
vyzo Dec 28, 2021
2a8ff37
update for interface changes; RIP buffers.
vyzo Dec 28, 2021
180996a
fix tests
vyzo Dec 28, 2021
a123bc5
alias the interface errors instead of redefining them
vyzo Dec 29, 2021
ceaa89b
Update scope.go
vyzo Dec 29, 2021
d4ec0eb
Update scope.go
vyzo Dec 29, 2021
4780eaa
use network.ErrResource*
vyzo Dec 29, 2021
3372fef
embed resources to avoid pointer indirection
vyzo Dec 29, 2021
d50af48
manage fds together with connss
vyzo Dec 29, 2021
4b61630
fix tests
vyzo Dec 29, 2021
3590b24
unexport implementation types
vyzo Dec 30, 2021
e81343f
fix tests
vyzo Dec 30, 2021
ddde34c
proper mutex hat
vyzo Dec 30, 2021
102bd3f
update for interface changes
vyzo Dec 31, 2021
93e874d
update for memory status indicator
vyzo Jan 5, 2022
8713e6b
fix tests
vyzo Jan 5, 2022
0c41381
default limiter
vyzo Jan 5, 2022
e118da9
implement dynamic limiter
vyzo Jan 5, 2022
958d1d0
fix tests
vyzo Jan 5, 2022
528598a
update for reservation interface changes
vyzo Jan 6, 2022
493c5d4
fix tests
vyzo Jan 6, 2022
8170501
add checks for reservation levels
vyzo Jan 6, 2022
68d98fd
refactor limiters
vyzo Jan 6, 2022
51e0f9e
add appropriate build tags for dynamic limiter
vyzo Jan 6, 2022
63eab4e
fix build tags for v1.16 toolchain
vyzo Jan 6, 2022
23b5dce
named scopes for better errors to aid debugging resource allocation f…
vyzo Jan 6, 2022
df11be6
move resource from protocol to service when a service for a stream is…
vyzo Jan 6, 2022
9b0d9b8
add fluid copy constructors for limits
vyzo Jan 8, 2022
841386b
add support for per service peer limits
vyzo Jan 8, 2022
cd37cf2
add test for per service peer limits
vyzo Jan 8, 2022
f152e03
RIP gosigar
vyzo Jan 9, 2022
0b3dc01
adjust memory limit multipliers for the default limiters
vyzo Jan 11, 2022
8efed93
adjust default limits
vyzo Jan 13, 2022
7514c54
rename txn to span
vyzo Jan 14, 2022
4d4ada8
introduce per protocol peer limits, don't transfer resources out of p…
vyzo Jan 14, 2022
7ae1962
fix tests
vyzo Jan 14, 2022
4ce184d
rename constraints to edges
vyzo Jan 14, 2022
96a9261
log, don't panic on resource release bugs
vyzo Jan 14, 2022
f70b17f
add logging around blocked reservations/conns/streams
vyzo Jan 14, 2022
93fcb58
add total stream and conn limit
vyzo Jan 14, 2022
a641329
extensions api
vyzo Jan 14, 2022
b29d6fe
add options to NewResourceManager constructor
vyzo Jan 14, 2022
ffacb78
fix test
vyzo Jan 14, 2022
d4ad950
tracing instrumentation
vyzo Jan 15, 2022
74e2de5
implement tracer
vyzo Jan 15, 2022
77288d1
fix omit empty decl
vyzo Jan 15, 2022
6b44e49
short circuit write/flush if there are no pending events
vyzo Jan 15, 2022
2cb1962
emit start event to trace with the limiter
vyzo Jan 15, 2022
840df24
add limit json config parser
vyzo Jan 16, 2022
ac5219c
add limit config parsing unit test
vyzo Jan 16, 2022
0ef82d9
refactor limit defaults for easy access and user manipulation
vyzo Jan 16, 2022
1a383bd
normalize limiter constructors
vyzo Jan 17, 2022
50378f0
sort results in List* api methods
vyzo Jan 17, 2022
86adb8a
downgrade BUG log to warning
vyzo Jan 18, 2022
15acc34
Merge pull request #5 from libp2p/fix/bug-log
vyzo Jan 18, 2022
a69f13d
make protocols and peers sticky when setting their limit
vyzo Jan 19, 2022
5d75fd5
Merge pull request #7 from libp2p/feat/sticky-limits
vyzo Jan 19, 2022
f9c6f04
avoid the nil is not nil footgun
vyzo Feb 3, 2022
7c88598
Merge pull request #9 from libp2p/fix/nil-is-not-nil
vyzo Feb 3, 2022
5b89154
metrics collection support
vyzo Feb 11, 2022
2090cc6
simplify metrics method names
vyzo Feb 12, 2022
fc0176c
blocked memory reservation metrics
vyzo Feb 12, 2022
38c17a4
fix test
vyzo Feb 12, 2022
5b7e876
add Allow metrics
vyzo Feb 14, 2022
74c7296
Merge pull request #11 from libp2p/feat/metrics
vyzo Feb 14, 2022
44d9738
improve debug logging
vyzo Mar 10, 2022
e7ac18a
Merge pull request #12 from libp2p/feat/improve-debug-log
vyzo Mar 10, 2022
d78a5b0
use a string for trace events (#15)
marten-seemann Apr 8, 2022
164c569
fix sign of delta of release_memory tracer event (#16)
marten-seemann Apr 10, 2022
a474648
fix: decode peer IDs from limits.json (#19)
guseggert Apr 18, 2022
184a581
fix: typos in config err msgs
guseggert Apr 19, 2022
0a9828b
feat: export basic limiter config
guseggert Apr 21, 2022
1666d38
Merge pull request #21 from libp2p/feat/export-limiter-config
vyzo Apr 21, 2022
6a9b8ca
move go-libp2p-resource-manager here
marten-seemann May 26, 2022
9444cb4
switch from github.com/libp2p/go-libp2p-resource-manager to p2p/host/…
marten-seemann May 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package libp2p
import (
"crypto/rand"

rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/libp2p/go-libp2p/p2p/security/noise"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/libp2p/go-libp2p-core/crypto"

"github.com/libp2p/go-libp2p-peerstore/pstoremem"
rcmgr "github.com/libp2p/go-libp2p-resource-manager"
"github.com/multiformats/go-multiaddr"
)

Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ require (
github.com/libp2p/go-libp2p-circuit v0.6.0
github.com/libp2p/go-libp2p-core v0.16.1
github.com/libp2p/go-libp2p-peerstore v0.6.0
github.com/libp2p/go-libp2p-resource-manager v0.3.0
github.com/libp2p/go-libp2p-testing v0.9.2
github.com/libp2p/go-mplex v0.7.0
github.com/libp2p/go-msgio v0.2.0
Expand All @@ -42,6 +41,7 @@ require (
github.com/multiformats/go-multihash v0.1.0
github.com/multiformats/go-multistream v0.3.1
github.com/multiformats/go-varint v0.0.6
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
github.com/prometheus/client_golang v1.12.1
github.com/raulk/go-watchdog v1.2.0
github.com/stretchr/testify v1.7.0
Expand Down Expand Up @@ -96,7 +96,6 @@ require (
github.com/nxadm/tail v1.4.8 // indirect
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/opencontainers/runtime-spec v1.0.2 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,6 @@ github.com/libp2p/go-libp2p-pnet v0.2.0 h1:J6htxttBipJujEjz1y0a5+eYoiPcFHhSYHH6n
github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA=
github.com/libp2p/go-libp2p-quic-transport v0.16.0 h1:aVg9/jr+R2esov5sH7wkXrmYmqJiUjtLMLYX3L9KYdY=
github.com/libp2p/go-libp2p-quic-transport v0.16.0/go.mod h1:1BXjVMzr+w7EkPfiHkKnwsWjPjtfaNT0q8RS3tGDvEQ=
github.com/libp2p/go-libp2p-resource-manager v0.3.0 h1:2+cYxUNi33tcydsVLt6K5Fv2E3OTiVeafltecAj15E0=
github.com/libp2p/go-libp2p-resource-manager v0.3.0/go.mod h1:K+eCkiapf+ey/LADO4TaMpMTP9/Qde/uLlrnRqV4PLQ=
github.com/libp2p/go-libp2p-swarm v0.10.0 h1:1yr7UCwxCN92cw9g9Q+fnJSlk7lOB1RetoEewxhGVL0=
github.com/libp2p/go-libp2p-swarm v0.10.0/go.mod h1:71ceMcV6Rg/0rIQ97rsZWMzto1l9LnNquef+efcRbmA=
github.com/libp2p/go-libp2p-testing v0.1.1/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eqnjVyavGroI2nxEM0=
Expand Down
3 changes: 1 addition & 2 deletions limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"github.com/libp2p/go-libp2p-core/protocol"

"github.com/libp2p/go-libp2p/p2p/host/autonat"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
relayv1 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv1/relay"
circuit "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/proto"
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"

rcmgr "github.com/libp2p/go-libp2p-resource-manager"
)

// SetDefaultServiceLimits sets the default limits for bundled libp2p services
Expand Down
147 changes: 147 additions & 0 deletions p2p/host/resource-manager/extapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package rcmgr

import (
"bytes"
"sort"
"strings"

"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
)

// ResourceScopeLimiter is a trait interface that allows you to access scope limits.
type ResourceScopeLimiter interface {
Limit() Limit
SetLimit(Limit)
}

var _ ResourceScopeLimiter = (*resourceScope)(nil)

// ResourceManagerStat is a trait that allows you to access resource manager state.
type ResourceManagerState interface {
ListServices() []string
ListProtocols() []protocol.ID
ListPeers() []peer.ID

Stat() ResourceManagerStat
}

type ResourceManagerStat struct {
System network.ScopeStat
Transient network.ScopeStat
Services map[string]network.ScopeStat
Protocols map[protocol.ID]network.ScopeStat
Peers map[peer.ID]network.ScopeStat
}

var _ ResourceManagerState = (*resourceManager)(nil)

func (s *resourceScope) Limit() Limit {
s.Lock()
defer s.Unlock()

return s.rc.limit
}

func (s *resourceScope) SetLimit(limit Limit) {
s.Lock()
defer s.Unlock()

s.rc.limit = limit
}

func (s *protocolScope) SetLimit(limit Limit) {
s.rcmgr.setStickyProtocol(s.proto)
s.resourceScope.SetLimit(limit)
}

func (s *peerScope) SetLimit(limit Limit) {
s.rcmgr.setStickyPeer(s.peer)
s.resourceScope.SetLimit(limit)
}

func (r *resourceManager) ListServices() []string {
r.mx.Lock()
defer r.mx.Unlock()

result := make([]string, 0, len(r.svc))
for svc := range r.svc {
result = append(result, svc)
}

sort.Slice(result, func(i, j int) bool {
return strings.Compare(result[i], result[j]) < 0
})

return result
}

func (r *resourceManager) ListProtocols() []protocol.ID {
r.mx.Lock()
defer r.mx.Unlock()

result := make([]protocol.ID, 0, len(r.proto))
for p := range r.proto {
result = append(result, p)
}

sort.Slice(result, func(i, j int) bool {
return strings.Compare(string(result[i]), string(result[j])) < 0
})

return result
}

func (r *resourceManager) ListPeers() []peer.ID {
r.mx.Lock()
defer r.mx.Unlock()

result := make([]peer.ID, 0, len(r.peer))
for p := range r.peer {
result = append(result, p)
}

sort.Slice(result, func(i, j int) bool {
return bytes.Compare([]byte(result[i]), []byte(result[j])) < 0
})

return result
}

func (r *resourceManager) Stat() (result ResourceManagerStat) {
r.mx.Lock()
svcs := make([]*serviceScope, 0, len(r.svc))
for _, svc := range r.svc {
svcs = append(svcs, svc)
}
protos := make([]*protocolScope, 0, len(r.proto))
for _, proto := range r.proto {
protos = append(protos, proto)
}
peers := make([]*peerScope, 0, len(r.peer))
for _, peer := range r.peer {
peers = append(peers, peer)
}
r.mx.Unlock()

// Note: there is no global lock, so the system is updating while we are dumping its state...
// as such stats might not exactly add up to the system level; we take the system stat
// last nonetheless so that this is the most up-to-date snapshot
result.Peers = make(map[peer.ID]network.ScopeStat, len(peers))
for _, peer := range peers {
result.Peers[peer.peer] = peer.Stat()
}
result.Protocols = make(map[protocol.ID]network.ScopeStat, len(protos))
for _, proto := range protos {
result.Protocols[proto.proto] = proto.Stat()
}
result.Services = make(map[string]network.ScopeStat, len(svcs))
for _, svc := range svcs {
result.Services[svc.name] = svc.Stat()
}
result.Transient = r.transient.Stat()
result.System = r.system.Stat()

return result
}
Loading