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

[stateless_validation] Load memtrie async on catchup #10983

Merged
merged 8 commits into from
Apr 15, 2024
Merged

Conversation

staffik
Copy link
Contributor

@staffik staffik commented Apr 8, 2024

Context
Issue: #10982
Follow up to: #10820.

Modifies StateSync state machine so that memtrie load happens asynchronously on catchup.

Summary

  • Split chain.set_state_finalize() into:
    • create_flat_storage_for_shard()
    • schedule_load_memtrie()
    • actual set_state_finalize()
  • ^ we need it because creating flat storage and state finalize requires chain which cannot be passed in a message to the separate thread.
  • Code to trigger memtrie load in a separate thread, analogously to how apply state parts is done.
  • Modify shard sync stages:
    • StateDownloadScheduling --> StateApplyScheduling
      • Just changed the name as it was confusing. What happens there is scheduling applying of state parts.
    • StateDownloadApplying --> StateApplyComplete
      • What it actually did before was initializing flat storage and finalizing state update after state apply from previous stage.
      • Now it only initializes flat storage and schedules memtrie loading.
    • StateDownloadComplete --> StateApplyFinalizing
      • Before it was just deciding what next stage to transit into.
      • Now it also contains the finalizing state update logic that was previously in the previous stage.

Integration tests are to be done as a part of: #10844.

@staffik staffik added the A-stateless-validation Area: stateless validation label Apr 8, 2024
@staffik staffik force-pushed the memtrie-load-async branch from bfede3c to 49f7830 Compare April 9, 2024 11:21
Copy link

codecov bot commented Apr 9, 2024

Codecov Report

Attention: Patch coverage is 5.32544% with 160 lines in your changes are missing coverage. Please review.

Project coverage is 71.24%. Comparing base (4c0aa98) to head (b94876d).
Report is 20 commits behind head on master.

Files Patch % Lines
chain/client/src/sync/state.rs 5.81% 81 Missing ⚠️
chain/chain/src/chain.rs 0.00% 52 Missing ⚠️
chain/client/src/sync_jobs_actions.rs 0.00% 12 Missing ⚠️
chain/client-primitives/src/types.rs 0.00% 6 Missing ⚠️
chain/client/src/client_actions.rs 25.00% 3 Missing ⚠️
...hain/client/src/test_utils/sync_jobs_test_utils.rs 0.00% 3 Missing ⚠️
chain/client/src/sync_jobs_actor.rs 0.00% 1 Missing ⚠️
...client/src/test_utils/client_actions_test_utils.rs 0.00% 1 Missing ⚠️
tools/state-viewer/src/state_parts.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10983      +/-   ##
==========================================
- Coverage   71.33%   71.24%   -0.10%     
==========================================
  Files         760      758       -2     
  Lines      152288   152687     +399     
  Branches   152288   152687     +399     
==========================================
+ Hits       108640   108776     +136     
- Misses      39176    39450     +274     
+ Partials     4472     4461      -11     
Flag Coverage Δ
backward-compatibility 0.24% <0.00%> (-0.01%) ⬇️
db-migration 0.24% <0.00%> (-0.01%) ⬇️
genesis-check 1.43% <0.00%> (-0.01%) ⬇️
integration-tests 36.90% <4.73%> (-0.08%) ⬇️
linux 69.69% <5.32%> (-0.13%) ⬇️
linux-nightly 70.74% <5.32%> (-0.07%) ⬇️
macos 54.39% <5.32%> (+0.10%) ⬆️
pytests 1.66% <0.00%> (-0.01%) ⬇️
sanity-checks 1.44% <0.00%> (-0.01%) ⬇️
unittests 66.92% <5.32%> (-0.06%) ⬇️
upgradability 0.29% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@staffik staffik marked this pull request as ready for review April 9, 2024 13:31
@staffik staffik requested a review from a team as a code owner April 9, 2024 13:31
@shreyan-gupta shreyan-gupta self-requested a review April 9, 2024 16:42
@tayfunelmas
Copy link
Contributor

A high level question: Do the state sync/catchup operations run on a separate thread anyways? since the loading of memtrie is part of sync/catchup, what is the added benefit of doing this loading in async way?

@staffik
Copy link
Contributor Author

staffik commented Apr 10, 2024

A high level question: Do the state sync/catchup operations run on a separate thread anyways? since the loading of memtrie is part of sync/catchup, what is the added benefit of doing this loading in async way?

State sync/catchup high level functions run on the client thread.
This is the main function that is called periodically to see if there is something to sync.
sync_shards_status() works as a state machine and some parts of sync are requested to be done on a separate thread, e.g. via chain::schedule_apply_state_parts().

Copy link
Contributor

@shreyan-gupta shreyan-gupta left a comment

Choose a reason for hiding this comment

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

Looks good overall. Just confirming, this is only for the state sync with shard reshuffling and not for node startup right?

chain/client/src/sync/state.rs Outdated Show resolved Hide resolved
chain/client/src/sync/state.rs Outdated Show resolved Hide resolved
// (these are set via callback from ClientActor - both for sync and catchup).
let mut shard_sync_done = false;
if let Some(result) = self.load_memtrie_results.remove(&shard_id) {
result
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Why the and_then and or_else here? It's slightly hard to read. Can't we do a match instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We want the or_else arm both if result is error or set_state_finalize() returns error. If I simply do match, I would have to duplicate the or_else arm code. I did not find a cleaner approach.

chain/client/src/sync_jobs_actions.rs Show resolved Hide resolved
@staffik
Copy link
Contributor Author

staffik commented Apr 12, 2024

Looks good overall. Just confirming, this is only for the state sync with shard reshuffling and not for node startup right?

Yes, just for catchup before next epoch, not for startup.

@staffik staffik added this pull request to the merge queue Apr 15, 2024
Merged via the queue into master with commit 579c53c Apr 15, 2024
26 of 29 checks passed
@staffik staffik deleted the memtrie-load-async branch April 15, 2024 13:09
mooori pushed a commit to mooori/nearcore that referenced this pull request Apr 16, 2024
<p>This PR was automatically created by Snyk using the credentials of a
real user.</p><br /><h3>Snyk has created this PR to upgrade
react-router-dom from 6.19.0 to 6.20.0.</h3>

:information_source: Keep your dependencies up-to-date. This makes it
easier to fix existing vulnerabilities and to more quickly identify and
fix newly disclosed vulnerabilities when they affect your project.
<hr/>

- The recommended version is **2 versions** ahead of your current
version.
- The recommended version was released **21 days ago**, on 2023-11-22.


<details>
<summary><b>Release notes</b></summary>
<br/>
  <details>
    <summary>Package name: <b>react-router-dom</b></summary>
    <ul>
      <li>
<b>6.20.0</b> - <a
href="https://snyk.io/redirect/github/remix-run/react-router/releases/tag/react-router-native%406.20.0">2023-11-22</a></br><p>react-router-native@6.20.0</p>
      </li>
      <li>
        <b>6.20.0-pre.0</b> - 2023-11-21
      </li>
      <li>
        <b>6.19.0</b> - 2023-11-16
      </li>
    </ul>
from <a
href="https://snyk.io/redirect/github/remix-run/react-router/releases">react-router-dom
GitHub release notes</a>
  </details>
</details>


<details>
  <summary><b>Commit messages</b></summary>
  </br>
  <details>
    <summary>Package name: <b>react-router-dom</b></summary>
    <ul>
<li><a
href="https://snyk.io/redirect/github/remix-run/react-router/commit/3cc38eac4753702a9a8a1fe239e2138d63ac6cc5">3cc38ea</a>
chore: Update version for release (near#11050)</li>
<li><a
href="https://snyk.io/redirect/github/remix-run/react-router/commit/77862f95f71397d92b8b583a16674c56efccc55f">77862f9</a>
Exit prerelease mode</li>
<li><a
href="https://snyk.io/redirect/github/remix-run/react-router/commit/1621319ffa353bf33f33064d7611859df16286ee">1621319</a>
Update Release Notes TOC</li>
<li><a
href="https://snyk.io/redirect/github/remix-run/react-router/commit/f1f8ed0acffb3d6c2860c362fc2b376dbf87df24">f1f8ed0</a>
Update release notes</li>
<li><a
href="https://snyk.io/redirect/github/remix-run/react-router/commit/1e026b6f1ac34a774b4f77e5e3696251e8f79940">1e026b6</a>
chore: Update version for release (pre) (near#11047)</li>
<li><a
href="https://snyk.io/redirect/github/remix-run/react-router/commit/4a08d64c368c07816e753632345a13b8da050111">4a08d64</a>
Enter prerelease mode</li>
<li><a
href="https://snyk.io/redirect/github/remix-run/react-router/commit/c0b4e12d12c5abdf5f7723e71959c4eb5e9effd9">c0b4e12</a>
Merge branch &#x27;main&#x27; into release-next</li>
<li><a
href="https://snyk.io/redirect/github/remix-run/react-router/commit/58d421fc4c592661a68dea59edc507fc4668ba5d">58d421f</a>
Fix other code paths for resolveTo from a splat route (near#11045)</li>
<li><a
href="https://snyk.io/redirect/github/remix-run/react-router/commit/5f530a775cd266940f725894277b6ea7bc55b5d0">5f530a7</a>
Do not revalidate unmounted fetchers when v7_persistFetcher is enabled
(near#11044)</li>
<li><a
href="https://snyk.io/redirect/github/remix-run/react-router/commit/f320378b5145f59bb266a35a7655b563f712daef">f320378</a>
Add additional test case for near#10983</li>
<li><a
href="https://snyk.io/redirect/github/remix-run/react-router/commit/a48c43c8118bbf75b23b3ee748648bb3ee4d688e">a48c43c</a>
feat: export &#x60;PathParam&#x60; type (near#10719)</li>
<li><a
href="https://snyk.io/redirect/github/remix-run/react-router/commit/1d56e55d3f95730f99617dff23cf153f82394921">1d56e55</a>
Remove tag links from headings in CHANGELOG.md</li>
<li><a
href="https://snyk.io/redirect/github/remix-run/react-router/commit/3b1a7364c730209b4baed9454c7f6c17c55e3ba8">3b1a736</a>
Fix flaky test</li>
<li><a
href="https://snyk.io/redirect/github/remix-run/react-router/commit/406a1ddecd399ede2a517d7cae5f3ee63d02ed91">406a1dd</a>
Merge branch &#x27;release-next&#x27; into dev</li>
<li><a
href="https://snyk.io/redirect/github/remix-run/react-router/commit/6a5939b07c06c9dacd82705645dbbbe46de90e5e">6a5939b</a>
Merge branch &#x27;release-next&#x27;</li>
    </ul>

<a
href="https://snyk.io/redirect/github/remix-run/react-router/compare/dcf0c2a85aac3a78059a287ea478ff12adcb6a2d...3cc38eac4753702a9a8a1fe239e2138d63ac6cc5">Compare</a>
  </details>
</details>
<hr/>

**Note:** *You are seeing this because you or someone else with access
to this repository has authorized Snyk to open upgrade PRs.*

For more information: <img
src="https://api.segment.io/v1/pixel/track?data=eyJ3cml0ZUtleSI6InJyWmxZcEdHY2RyTHZsb0lYd0dUcVg4WkFRTnNCOUEwIiwiYW5vbnltb3VzSWQiOiIyZmI3YjkxYi0zN2NiLTQzYzgtOWNkYS02ODAzNTFmYzMwNmQiLCJldmVudCI6IlBSIHZpZXdlZCIsInByb3BlcnRpZXMiOnsicHJJZCI6IjJmYjdiOTFiLTM3Y2ItNDNjOC05Y2RhLTY4MDM1MWZjMzA2ZCJ9fQ=="
width="0" height="0"/>

🧐 [View latest project
report](https://app.snyk.io/org/ecp88/project/98480bdc-d80b-4fd1-89d7-c4c56a706763?utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;upgrade-pr)

🛠 [Adjust upgrade PR
settings](https://app.snyk.io/org/ecp88/project/98480bdc-d80b-4fd1-89d7-c4c56a706763/settings/integration?utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;upgrade-pr)

🔕 [Ignore this dependency or unsubscribe from future upgrade
PRs](https://app.snyk.io/org/ecp88/project/98480bdc-d80b-4fd1-89d7-c4c56a706763/settings/integration?pkg&#x3D;react-router-dom&amp;utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;upgrade-pr#auto-dep-upgrades)

<!---
(snyk:metadata:{"prId":"2fb7b91b-37cb-43c8-9cda-680351fc306d","prPublicId":"2fb7b91b-37cb-43c8-9cda-680351fc306d","dependencies":[{"name":"react-router-dom","from":"6.19.0","to":"6.20.0"}],"packageManager":"npm","type":"auto","projectUrl":"https://app.snyk.io/org/ecp88/project/98480bdc-d80b-4fd1-89d7-c4c56a706763?utm_source=github&utm_medium=referral&page=upgrade-pr","projectPublicId":"98480bdc-d80b-4fd1-89d7-c4c56a706763","env":"prod","prType":"upgrade","vulns":[],"issuesToFix":[],"upgrade":[],"upgradeInfo":{"versionsDiff":2,"publishedDate":"2023-11-22T16:56:32.720Z"},"templateVariants":[],"hasFixes":false,"isMajorUpgrade":false,"isBreakingChange":false,"priorityScoreList":[]})
--->

Co-authored-by: snyk-bot <snyk-bot@snyk.io>
Co-authored-by: nikurt <86772482+nikurt@users.noreply.github.com>
github-merge-queue bot pushed a commit that referenced this pull request Jun 26, 2024
Fix a wrong name for a state sync state I introduced in
#10983.
I called it `StateApplyComplete` while in fact the state apply is in
progress.
staffik added a commit that referenced this pull request Jun 27, 2024
Fix a wrong name for a state sync state I introduced in
#10983.
I called it `StateApplyComplete` while in fact the state apply is in
progress.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-stateless-validation Area: stateless validation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants