Skip to content

Cache peer score #175

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

Merged
merged 10 commits into from
Dec 15, 2021
Merged

Cache peer score #175

merged 10 commits into from
Dec 15, 2021

Conversation

twoeths
Copy link
Contributor

@twoeths twoeths commented Dec 6, 2021

Motivation

  • In lodestar, it takes 4% of cpu time just to compute peer score

Description

  • Cache peer score if topic stat & peer stat are unchanged for maximum decayInterval time period
  • Main consumer of computeScore() is acceptFrom() function which is called per incoming message. If peer score >= 0 we allow up to 128 messages and 1s not to compute score (this is safe enough considering grayListThreshold=-16000
  • Closes computeScore performance issue #169

Thanks Teku for the work libp2p/jvm-libp2p#184

@twoeths twoeths force-pushed the tuyen/peer-score-performance branch from 56da592 to 6716d26 Compare December 8, 2021 08:42
@codecov-commenter
Copy link

codecov-commenter commented Dec 8, 2021

Codecov Report

Merging #175 (2029715) into master (1125869) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #175   +/-   ##
=======================================
  Coverage   76.58%   76.58%           
=======================================
  Files           2        2           
  Lines        1905     1905           
  Branches      142      140    -2     
=======================================
  Hits         1459     1459           
  Misses        446      446           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1125869...2029715. Read the comment docs.

@twoeths twoeths marked this pull request as ready for review December 9, 2021 09:47
@twoeths twoeths requested a review from a team December 9, 2021 09:47
*
* @type {Map<string, AcceptFromWhiteListEntry}
*/
this.acceptFromWhitelist = new Map()
Copy link
Member

Choose a reason for hiding this comment

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

I think we need a place where these entries get routinely pruned so they don't build up.
Maybe you can prune old entries on every N heartbeats

Copy link
Member

Choose a reason for hiding this comment

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

also remove the peer's entry in _removePeer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wemeetagain so I removed it in _removePeer but regarding this

I think we need a place where these entries get routinely pruned so they don't build up.
Maybe you can prune old entries on every N heartbeats

I don't think it's necessary as we only do the prune in _removePeer() function for other map as well (unless there's already a leak somewhere, we'll have to do the prune regularly for all of the map). In acceptFrom() we also do the prune if the score is negative

@twoeths twoeths force-pushed the tuyen/peer-score-performance branch from 631b466 to 17c8976 Compare December 12, 2021 08:06
/**
* The time after which the cached score for a peer is no longer valid.
*/
scoreCacheUntil: Map<string, number>
Copy link
Contributor

Choose a reason for hiding this comment

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

Clarify exactly what number is: unix timestamp in miliseconds

Copy link
Contributor

Choose a reason for hiding this comment

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

Consider merging scoreCache and scoreCacheUntil, it will reduce the memory cost and require a single set and prune operation

ts/index.ts Outdated
// after 128 messages or 1s
this.acceptFromWhitelist.set(id, {
messagesAccepted: 0,
acceptUntil: now + ACCEPT_FROM_WHITE_LIST_DURATION_MS
Copy link
Contributor

Choose a reason for hiding this comment

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

Really nice solution 👍 🔥

expect(scoreSpy.getCall(2)).to.be.undefined
})

// TODO: run this in a unit test setup
Copy link
Contributor Author

Choose a reason for hiding this comment

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

there's an issue that sinon causes the test to never finish in firefox environment. I think current test set up is like an e2e test where we'll run it in different OSs and browsers, and sinon is not expected to run on it, we need a separate test:unit where we can stub any functions

Copy link
Member

@wemeetagain wemeetagain left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@dapplion dapplion left a comment

Choose a reason for hiding this comment

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

Looks great!

@wemeetagain wemeetagain merged commit 349d5a6 into master Dec 15, 2021
@wemeetagain wemeetagain deleted the tuyen/peer-score-performance branch December 15, 2021 16:29
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.

computeScore performance issue
4 participants