-
Notifications
You must be signed in to change notification settings - Fork 201
Delay updateAll() & only update subset immediately
#815
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
Open
lejeunerenard
wants to merge
14
commits into
main
Choose a base branch
from
min-delay-update-all
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
682bfce
Delay `updateAll()` & only update subset immediately
lejeunerenard 36f8e67
Lint
lejeunerenard 5d01efd
Update `MIN_PEER_UPDATE_ALL` to `30`
lejeunerenard c11ee68
Refactor updateAll delay amount out into a function
lejeunerenard a7990ad
Change `getUpdateAllDelay` to return a clamped linear delay
lejeunerenard 73ce602
Clear timeout when running full scan `updateAll()`
lejeunerenard e5048c3
Update `MIN_PEER_UPDATE_ALL` to `40`
lejeunerenard 598281c
Merge branch 'main' into min-delay-update-all
lejeunerenard 0632661
Add test for stuck full scan & fix by checking reorg after clear
lejeunerenard d07cb0b
Merge branch 'main' into min-delay-update-all
lejeunerenard 0003de0
Lint `test/replicate.js`
lejeunerenard 7b2ad5d
Clear `_updateAllBump` timer when replicator is destroyed
lejeunerenard 9454328
Refactor test to just check if timer is `null`ed
lejeunerenard 725aa1a
Add `updateSubset()` to update a subset of peers instead of passing arg
lejeunerenard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,8 +48,8 @@ const SCALE_LATENCY = 50 | |
| const NOT_DOWNLOADING_SLACK = (20000 + Math.random() * 20000) | 0 | ||
| const MAX_PEERS_UPGRADE = 3 | ||
| const LAST_BLOCKS = 256 | ||
|
|
||
| const MAX_RANGES = 64 | ||
| const MIN_PEER_UPDATE_ALL = 40 | ||
|
|
||
| const NOT_AVAILABLE = 1 | ||
| const INVALID_REQUEST = 2 | ||
|
|
@@ -753,7 +753,7 @@ class Peer { | |
|
|
||
| if (this.remoteOpened === false) { | ||
| this.replicator._ifAvailable-- | ||
| this.replicator.updateAll() | ||
| this.replicator.queueUpdateAll() | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -1297,7 +1297,7 @@ class Peer { | |
| const req = request > 0 ? this.replicator._inflight.get(request) : null | ||
|
|
||
| if (req === null || req.peer !== this) { | ||
| this.replicator.updateAll() | ||
| this.replicator.queueUpdateAll() | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -2020,6 +2020,9 @@ module.exports = class Replicator { | |
| this._notDownloadingLinger = notDownloadingLinger | ||
| this._notDownloadingTimer = null | ||
|
|
||
| this._updateAllBump = null | ||
| this._updateAllBound = this.updateAll.bind(this) | ||
|
|
||
| const self = this | ||
| this._onstreamclose = onstreamclose | ||
|
|
||
|
|
@@ -2202,7 +2205,7 @@ module.exports = class Replicator { | |
|
|
||
| const ref = this._addUpgrade().attach(session) | ||
|
|
||
| this.updateAll() | ||
| this.queueUpdateAll() | ||
|
|
||
| return ref | ||
| } | ||
|
|
@@ -2212,7 +2215,7 @@ module.exports = class Replicator { | |
| const ref = b.attach(session) | ||
|
|
||
| this._queueBlock(b) | ||
| this.updateAll() | ||
| this.queueUpdateAll() | ||
|
|
||
| return ref | ||
| } | ||
|
|
@@ -2222,7 +2225,7 @@ module.exports = class Replicator { | |
| const ref = s.attach(session) | ||
|
|
||
| this._seeks.push(s) | ||
| this.updateAll() | ||
| this.queueUpdateAll() | ||
|
|
||
| return ref | ||
| } | ||
|
|
@@ -2257,7 +2260,7 @@ module.exports = class Replicator { | |
| return ref | ||
| } | ||
|
|
||
| this.updateAll() | ||
| this.queueUpdateAll() | ||
|
|
||
| return ref | ||
| } | ||
|
|
@@ -2279,7 +2282,7 @@ module.exports = class Replicator { | |
| } | ||
|
|
||
| for (const replicator of updated) { | ||
| replicator.updateAll() | ||
| replicator.queueUpdateAll() | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -2291,7 +2294,7 @@ module.exports = class Replicator { | |
| cleared = true | ||
| } | ||
|
|
||
| if (cleared) this.updateAll() | ||
| if (cleared) this.queueUpdateAll() | ||
| } | ||
|
|
||
| _matchingRequest(req, data) { | ||
|
|
@@ -2452,7 +2455,7 @@ module.exports = class Replicator { | |
|
|
||
| this._onpeerupdate(false, peer) | ||
| if (inflight) { | ||
| this.updateAll() | ||
| this.queueUpdateAll() | ||
| } else { | ||
| this._checkUpgradeIfAvailable() | ||
| } | ||
|
|
@@ -2645,7 +2648,7 @@ module.exports = class Replicator { | |
| this._updatesPending = 0 | ||
| } | ||
|
|
||
| if (this._inflight.idle || updateAll) this.updateAll() | ||
| if (this._inflight.idle || updateAll) this.queueUpdateAll() | ||
| } | ||
|
|
||
| _clearRequest(peer, req) { | ||
|
|
@@ -2694,7 +2697,7 @@ module.exports = class Replicator { | |
| this._clearRequest(peer, req) | ||
| } | ||
|
|
||
| this.updateAll() | ||
| this.queueUpdateAll() | ||
| } | ||
|
|
||
| _openSkipBitfield() { | ||
|
|
@@ -2817,7 +2820,7 @@ module.exports = class Replicator { | |
| const f = this._addReorg(data.fork, peer) | ||
|
|
||
| if (f === null) { | ||
| this.updateAll() | ||
| this.queueUpdateAll() | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -2838,7 +2841,7 @@ module.exports = class Replicator { | |
| } | ||
| } | ||
|
|
||
| this.updateAll() | ||
| this.queueUpdateAll() | ||
| } | ||
|
|
||
| // Never throws, allowed to run in the background | ||
|
|
@@ -2878,7 +2881,7 @@ module.exports = class Replicator { | |
| for (const f of old) f.resolve() | ||
| f.resolve() | ||
|
|
||
| this.updateAll() | ||
| this.queueUpdateAll() | ||
| } | ||
|
|
||
| _maybeUpdate() { | ||
|
|
@@ -3000,16 +3003,30 @@ module.exports = class Replicator { | |
| this._checkUpgradeIfAvailable() | ||
| } | ||
|
|
||
| updateAll() { | ||
| updateSubset() { | ||
| return this.updateAll(false) | ||
| } | ||
|
|
||
| updateAll(all = true) { | ||
| // Clear queued scan if a full scan | ||
| if (this._updateAllBump !== null && all) { | ||
| clearTimeout(this._updateAllBump) | ||
| this._updateAllBump = null | ||
| } | ||
|
|
||
| // Quick shortcut to wait for flushing reorgs - not needed but less waisted requests | ||
| if (this._applyingReorg !== null) return | ||
|
|
||
| const peers = new RandomIterator(this.peers) | ||
|
|
||
| const limit = all ? peers.length : MIN_PEER_UPDATE_ALL | ||
| let tried = 0 | ||
| for (const peer of peers) { | ||
| tried++ | ||
| if (this._updatePeer(peer) === true) { | ||
| peers.requeue() | ||
| } | ||
| if (tried >= limit) break | ||
| } | ||
|
|
||
| // Check if we can skip the non primary check fully | ||
|
|
@@ -3018,15 +3035,33 @@ module.exports = class Replicator { | |
| return | ||
| } | ||
|
|
||
| tried = 0 | ||
| for (const peer of peers.restart()) { | ||
| tried++ | ||
| if (this._updatePeerNonPrimary(peer) === true) { | ||
| peers.requeue() | ||
| } | ||
| if (tried >= limit) break | ||
| } | ||
|
|
||
| this._checkUpgradeIfAvailable() | ||
| } | ||
|
|
||
| getUpdateAllDelay(peers) { | ||
| return Math.min(3000, Math.max(100, (peers.length - MIN_PEER_UPDATE_ALL) * 5)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok if MIN_PEER_UPDATE is relatively non small, like 40-50
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
| } | ||
|
|
||
| queueUpdateAll() { | ||
| if (this.peers.length < MIN_PEER_UPDATE_ALL) return this.updateAll() | ||
|
|
||
| // Immediately scan subset | ||
| this.updateSubset() | ||
|
|
||
| // Schedule full scan | ||
| if (this._updateAllBump !== null) return //skip if already scheduled | ||
| this._updateAllBump = setTimeout(this._updateAllBound, this.getUpdateAllDelay(this.peers)) | ||
| } | ||
|
|
||
| onpeerdestroy() { | ||
| if (--this._active === 0) this.core.checkIfIdle() | ||
| } | ||
|
|
@@ -3090,6 +3125,11 @@ module.exports = class Replicator { | |
| this._notDownloadingTimer = null | ||
| } | ||
|
|
||
| if (this._updateAllBump) { | ||
| clearTimeout(this._updateAllBump) | ||
| this._updateAllBump = null | ||
| } | ||
|
|
||
| while (this.peers.length) { | ||
| const peer = this.peers[this.peers.length - 1] | ||
| this.detachFrom(peer.protomux) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.