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

feat(networking): prune peers from peerstore exceeding capacity #1513

Merged
merged 4 commits into from
Jan 31, 2023

Conversation

alrevuelta
Copy link
Contributor

@alrevuelta alrevuelta commented Jan 26, 2023

Closes #1504

Summary: Every PrunePeerStoreInterval the peerstore is pruned, removing peers from it if we have exceeded its capacity. The criteria is the following: peers that failed in the past are removed first, and if we are still over the capacity, not connected peers are removed.

Other changes:

  • New metric to track peerstore size.
  • Asserts that capacity > maxConnections, fails otherwise.

@status-im-auto
Copy link
Collaborator

status-im-auto commented Jan 26, 2023

Jenkins Builds

Commit #️⃣ Finished (UTC) Duration Platform Result
f9787b5 #1 2023-01-26 23:04:04 ~16 min macos 📄log
✔️ f2fc91b #2 2023-01-30 23:08:34 ~21 min macos 📦bin

@alrevuelta alrevuelta force-pushed the prune-peers-pt branch 2 times, most recently from 5c7e52f to c25ab02 Compare January 30, 2023 13:12
@alrevuelta alrevuelta marked this pull request as ready for review January 30, 2023 14:59
Copy link
Contributor

@LNSD LNSD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, check my comments

prunned += 1

# if we still need to prune, prune peers that are not connected
let notConnecteed = pm.peerStore.peers.filterIt(it.connectedness != Connected).mapIt(it.peerId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid exposing internal members of the types (the peers property in this case) and add accessor functions like the following:

Suggested change
let notConnecteed = pm.peerStore.peers.filterIt(it.connectedness != Connected).mapIt(it.peerId)
let notConnecteed = pm.peerStore.getNotConnectedPeers().mapIt(it.peerId)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good one thanks, fixed in 3df173c


# if we still need to prune, prune peers that are not connected
let notConnecteed = pm.peerStore.peers.filterIt(it.connectedness != Connected).mapIt(it.peerId)
for peerId in notConnecteed:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo:

Suggested change
for peerId in notConnecteed:
for peerId in notConnected:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed thanks 3df173c

Comment on lines 402 to 403
await sleepAsync(PrunePeerStoreInterval)
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split things into sub-procedures so you can return instead of repeating the sleepAsync(...) code. That will make the code more readable.

The sub-procedure should be a linear function wrapped and called by the periodic execution function. Mixing the loop code with the linear code makes things more complex to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed! should be fixed here. 3df173c
makes it easier to unit test also, added one in a94325a

error "Max number of connections can't be greater than PeerManager capacity",
capacity = capacity,
maxConnections = maxConnections
doAssert(false, "Max number of connections can't be greater than PeerManager capacity")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raising a Defect is the preferred way to finish a program in Nim. It is the panic equivalent of Go or Rust.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, i think i saw doAssert somewhere in libp2p and nimbus, but switched to raising Defect here.
3df173c

added also a test case, since its important.

Copy link
Contributor

@jm-clius jm-clius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

let peersToPrune = numPeers - capacity

# prune peers with too many failed attempts
var prunned = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var prunned = 0
var pruned = 0

:)

prunned += 1

# if we still need to prune, prune peers that are not connected
let notConnecteed = pm.peerStore.peers.filterIt(it.connectedness != Connected).mapIt(it.peerId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking my understanding: because maxConnections <= capacity we should always be able to reach our peersToPrune quota?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly!

slightly related, since enforcing this maxConnections <= capacity is quite important added a unit test for this here a94325a, otherwise, that wont be true.

@alrevuelta
Copy link
Contributor Author

@LNSD comments were addressed, mind checking and approving if ok?

Copy link
Contributor

@LNSD LNSD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(networking): Prune peers from peerstore
4 participants