-
Notifications
You must be signed in to change notification settings - Fork 154
async updateBase
#4174
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
Draft
advaita-saha
wants to merge
1
commit into
master
Choose a base branch
from
smooth-fcu
base: master
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.
+54
−16
Draft
async updateBase
#4174
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -295,7 +295,8 @@ func updateFinalized(c: ForkedChainRef, finalized: BlockRef, fcuHead: BlockRef) | |
| doAssert(candidate.isNil.not) | ||
| c.latest = candidate | ||
|
|
||
| proc updateBase(c: ForkedChainRef, base: BlockRef): uint = | ||
| proc updateBase(c: ForkedChainRef, base: BlockRef): Future[uint] | ||
| {.async: (raises: [CancelledError]).} = | ||
| ## | ||
| ## A1 - A2 - A3 D5 - D6 | ||
| ## / / | ||
|
|
@@ -338,20 +339,45 @@ with --debug-eager-state-root.""" | |
| # and prevent other modules accessing expired baseTxFrame. | ||
| c.baseTxFrame = base.txFrame | ||
|
|
||
| # Cleanup in-memory blocks starting from base backward | ||
| # e.g. B2 backward. | ||
| var count = 0'u | ||
| let postPersistTime = Moment.now() | ||
|
|
||
| # The disk-flush burst is done. Commit the new base pointer now, *before* | ||
| # the in-memory cleanup burst, so ForkedChain invariants (c.base, | ||
| # c.baseTxFrame) stay coherent even if cleanup is interrupted by shutdown | ||
| # cancellation. Capture `oldFrontier` first because `c.base.parent = nil` | ||
| # mutates `base.parent`, which the cleanup iterator walks. | ||
| let oldFrontier = base.parent | ||
| c.base = base | ||
| c.base.parent = nil | ||
| c.base.finalize() | ||
|
|
||
| # Hand the chronos loop a chance to service pending RPC / networking before | ||
| # we enter the cleanup burst, which with a full persistBatchSize=256 can | ||
| # iterate hundreds of blocks scanning txRecords. | ||
| await sleepAsync(0.milliseconds) | ||
|
|
||
| for it in ancestors(base.parent): | ||
| # Cleanup in-memory blocks starting from the previous base backward | ||
| # e.g. B2 backward. Yield every `cleanupYieldChunk` ancestors so a single | ||
| # updateBase can't hog the event loop for the full cleanup duration. | ||
| const cleanupYieldChunk = 16 | ||
| var | ||
| count = 0'u | ||
| sinceYield = 0 | ||
|
|
||
| for it in ancestors(oldFrontier): | ||
| c.removeBlockFromCache(it) | ||
| inc count | ||
| inc sinceYield | ||
| if sinceYield >= cleanupYieldChunk: | ||
| sinceYield = 0 | ||
| await sleepAsync(0.milliseconds) | ||
|
|
||
| # Update base branch | ||
| c.base = base | ||
| c.base.parent = nil | ||
| let finishTime = Moment.now() | ||
|
|
||
| # Base block always have finalized marker | ||
| c.base.finalize() | ||
| # Aggregate split timings for the "Finalized blocks persisted" log so we | ||
| # can see at a glance which phase is eating the budget. | ||
| c.persistMs += (postPersistTime - startTime).milliseconds | ||
| c.cleanupMs += (finishTime - postPersistTime).milliseconds | ||
|
|
||
| if c.dynamicBatchSize: | ||
| # Dynamicly adjust the persistBatchSize based on the recorded run time. | ||
|
|
@@ -367,9 +393,7 @@ with --debug-eager-state-root.""" | |
| batchSizeLowerBound = 4 | ||
| batchSizeUpperBound = 256 | ||
|
|
||
| let | ||
| finishTime = Moment.now() | ||
| runTime = (finishTime - startTime).milliseconds | ||
| let runTime = (finishTime - startTime).milliseconds | ||
|
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. I would remove cleanupMs since that part is no longer blocking the event loop after this change. Then runTime used here should just equal persistMs |
||
|
|
||
| if runTime < targetTimeLowerBound and c.persistBatchSize < batchSizeUpperBound: | ||
| c.persistBatchSize = min(c.persistBatchSize + 4, batchSizeUpperBound) | ||
|
|
@@ -385,7 +409,7 @@ with --debug-eager-state-root.""" | |
| proc processUpdateBase(c: ForkedChainRef): Future[Result[void, string]] {.async: (raises: [CancelledError]).} = | ||
| if c.baseQueue.len > 0: | ||
| let base = c.baseQueue.popFirst() | ||
| c.persistedCount += c.updateBase(base) | ||
| c.persistedCount += await c.updateBase(base) | ||
|
|
||
| const | ||
| minLogInterval = 5 | ||
|
|
@@ -406,7 +430,9 @@ proc processUpdateBase(c: ForkedChainRef): Future[Result[void, string]] {.async: | |
| pendingFCU = c.pendingFCU.short, | ||
| resolvedFinNum = c.latestFinalized.number, | ||
| resolvedFinHash = c.latestFinalized.hash.short, | ||
| dbSnapshotsCount = c.baseTxFrame.aTx.db.snapshots.len() | ||
| dbSnapshotsCount = c.baseTxFrame.aTx.db.snapshots.len(), | ||
| persistMs = c.persistMs, | ||
| cleanupMs = c.cleanupMs | ||
| else: | ||
| debug "Finalized blocks persisted", | ||
| nBlocks = c.persistedCount, | ||
|
|
@@ -416,9 +442,13 @@ proc processUpdateBase(c: ForkedChainRef): Future[Result[void, string]] {.async: | |
| pendingFCU = c.pendingFCU.short, | ||
| resolvedFinNum = c.latestFinalized.number, | ||
| resolvedFinHash = c.latestFinalized.hash.short, | ||
| dbSnapshotsCount = c.baseTxFrame.aTx.db.snapshots.len() | ||
| dbSnapshotsCount = c.baseTxFrame.aTx.db.snapshots.len(), | ||
| persistMs = c.persistMs, | ||
| cleanupMs = c.cleanupMs | ||
| c.lastBaseLogTime = time | ||
| c.persistedCount = 0 | ||
| c.persistMs = 0 | ||
| c.cleanupMs = 0 | ||
| return ok() | ||
|
|
||
| if c.queue.isNil: | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be implemented in a simpler way. Why not just add an i index to the iterator and then if i mod 16 == 0 then run the sleep.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes agreed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe the
removeBlockFromCachefunction could become async so you can simply avoid the sleep altogether.