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

runtime: implement a receipt preparation pipeline #11839

Merged
merged 15 commits into from
Aug 7, 2024

Conversation

nagisa
Copy link
Collaborator

@nagisa nagisa commented Jul 24, 2024

This PR implements a very basic form of pipelining of the contract preparation. In particular with this PR only actions from one upcoming "interesting" local receipt is handled, with other receipt kinds (incoming, delayed) being left for a future change. I don't anticipate any trouble integrating them as they largely have similar data and structure as the local receipts seen here.

The primary reason I want this reviewed separately, however, is due to the changes to storage. You will notice that I have largely changed the code to read contracts without going through The State (i.e. it now reads the contract code directly via the code hash.) However at the same time writing of the contract continues involving The State; largely because I don't want to deal with migrations and similar such complexities… but also because TrieStorage does not provide write access. This also means that writing is transactional and I had to add some work-arounds to the store::contract::Storage in order to make deployed contracts visible before the commit.

runtime/runtime/src/lib.rs Outdated Show resolved Hide resolved
@nagisa nagisa force-pushed the contract-preparation-pipeline branch 2 times, most recently from fa024d1 to 0c1af4a Compare July 25, 2024 21:21
As a somewhat notable shift in the approach, we'll start focusing on
contract wasm code management by directly addressing it by the code
hash, rather than going through The State (Trie).

One notable snag here is that the transaction runtime doesn't actually
have a write access to the underlying storage, so we continue relying on
the `TrieUpdate` to write these contracts out. In the meanwhile we
maintain an in-memory map of deployed contracts to make the storage
appear consistent.
@nagisa nagisa force-pushed the contract-preparation-pipeline branch from 0c1af4a to 6c57adf Compare July 25, 2024 21:22
nagisa added 3 commits July 26, 2024 13:06
I'm seeing a lot of integration tests failing, not sure if its the setup
or what...

In particular suspect is the `yield_then_resume` test which fails to
link `sleep_nanos` but then it is also me running with integration
tests, so why is that host function coming up at all...?

EDIT: so `nextest-integration nightly` passes, but not `stable`. Which
suggests that somewhere passing of the config or whatnot is not working
out quite right…
I suspect that it is not actually exercised by our integration tests
however... time to write some, but lets give coverage a spin.
Copy link

codecov bot commented Aug 1, 2024

Codecov Report

Attention: Patch coverage is 94.29429% with 19 lines in your changes missing coverage. Please review.

Project coverage is 71.65%. Comparing base (0ff6755) to head (cfd0e46).
Report is 32 commits behind head on master.

Files Patch % Lines
runtime/runtime/src/pipelining.rs 92.26% 13 Missing and 1 partial ⚠️
runtime/runtime/src/lib.rs 92.64% 3 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #11839      +/-   ##
==========================================
- Coverage   71.80%   71.65%   -0.15%     
==========================================
  Files         796      809      +13     
  Lines      163372   164549    +1177     
  Branches   163372   164549    +1177     
==========================================
+ Hits       117312   117914     +602     
- Misses      41006    41569     +563     
- Partials     5054     5066      +12     
Flag Coverage Δ
backward-compatibility 0.23% <0.00%> (-0.01%) ⬇️
db-migration 0.23% <0.00%> (-0.01%) ⬇️
genesis-check 1.34% <0.00%> (-0.01%) ⬇️
integration-tests 37.85% <86.18%> (+0.02%) ⬆️
linux 71.43% <94.29%> (-0.13%) ⬇️
linux-nightly 71.26% <94.29%> (-0.13%) ⬇️
macos 54.52% <28.11%> (-0.17%) ⬇️
pytests 1.61% <0.00%> (-0.02%) ⬇️
sanity-checks 1.41% <0.00%> (-0.02%) ⬇️
unittests 66.07% <85.28%> (-0.18%) ⬇️
upgradability 0.28% <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.

@nagisa nagisa marked this pull request as ready for review August 2, 2024 11:32
@nagisa nagisa requested a review from a team as a code owner August 2, 2024 11:32
@nagisa nagisa requested review from saketh-are, pugachAG and Ekleog-NEAR and removed request for saketh-are August 2, 2024 11:32
@nagisa
Copy link
Collaborator Author

nagisa commented Aug 2, 2024

r? @Ekleog-NEAR for the runtimeish bits
r? @pugachAG for the storagey bits

This one I think is better reviewed as a whole PR (files are nicely separated and commits -- less so.) Feel free to note if you think any of the FIXMEs or TODOs are critical to resolve before this can land.

Side note: I'm surprised by how good our existent coverage is. Most of the uncovered lines are actually just panics :D

Copy link
Collaborator

@Ekleog-NEAR Ekleog-NEAR left a comment

Choose a reason for hiding this comment

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

I reviewed all the files except those in core/store.

Only one important comment that deserved the changes requested: the issue if one receipt were to contain two deploycontract actions.

Aside from that, the code looks nice! :)

runtime/runtime/src/actions.rs Outdated Show resolved Hide resolved
runtime/runtime/src/lib.rs Outdated Show resolved Hide resolved
runtime/runtime/src/pipelining.rs Show resolved Hide resolved
runtime/runtime/src/pipelining.rs Outdated Show resolved Hide resolved
entry.insert(Arc::clone(&task));
// FIXME: don't spawn all tasks at once. We want to keep some capacity for
// other things and also to control (in a way) the concurrency here.
rayon::spawn_fifo(move || {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should there be a specific rayon thread pool for the pipeline manager, to avoid racing tasks with other task pushers, and instead use the os preemptive scheduling?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think we want to have a separate thread pool, no. We already have an excess of thread pools with NCPU threads in each. But even if the pool is occupied with other business, the implementation here won't wait on it unnecessarily.

Copy link
Collaborator

Choose a reason for hiding this comment

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

My thinking was actually the opposite: it seems to me that this pipelining prefetch business could be busying the thread pool for other, more pressing business. And by limiting the pipelining prefetch thread pool to 1 thread, we could also make sure it doesn't take up too much compute.

This being said, feel free to ignore the suggestion, it's most likely not going to be anything critical :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fair enough, I realize now that this is actually more aggressive in scheduling new work than I meant it to be, so I'll need to rewrite this anyhow. Limiting the pool, however, isn't quite the right approach IMO.

runtime/runtime/src/pipelining.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@pugachAG pugachAG left a comment

Choose a reason for hiding this comment

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

storage parts LGTM, got a small question regarding the first receipt

core/store/src/contract.rs Outdated Show resolved Hide resolved
runtime/runtime/src/pipelining.rs Show resolved Hide resolved

let mut local_receipt_iter = local_receipts.iter();
let mut prep_lookahead_iter = local_receipts.iter();
let _ = prep_lookahead_iter.next(); // stagger preparation to not prepare the first receipt.
Copy link
Contributor

Choose a reason for hiding this comment

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

what if the first receipt contains deploy contract action, should we block the account_id for further pipelining in that case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good eye! I think I addressed this by not trying to be as smart. I should add a regression test for this too (please do not resolve this thread quite yet.)

@Ekleog-NEAR I also made changes which I think will make it clearer when and how many receipts are submitted in parallel, so the currently last commit should resolve your concerns with that too?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The changes LGTM :)

Copy link
Collaborator

@Ekleog-NEAR Ekleog-NEAR left a comment

Choose a reason for hiding this comment

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

r+ for the runtime bits :)

nagisa added 2 commits August 5, 2024 17:22
At the same time this change also reduces and limits the number of
receipts we have "in-flight" which was the original intent behind this
code.
@akhi3030
Copy link
Collaborator

akhi3030 commented Aug 6, 2024

CC: @#11319

nagisa added a commit to nagisa/nearcore that referenced this pull request Aug 6, 2024
This is a regression test for an issue identified in a review of near#11839
nagisa added a commit to nagisa/nearcore that referenced this pull request Aug 6, 2024
This is a regression test for an issue identified in a review of near#11839
@nagisa nagisa added this pull request to the merge queue Aug 6, 2024
@nagisa
Copy link
Collaborator Author

nagisa commented Aug 6, 2024

@race-of-sloths include

@race-of-sloths
Copy link

race-of-sloths commented Aug 6, 2024

@nagisa Thank you for your contribution! Your pull request is now a part of the Race of Sloths!
New Sloth joined the Race! Welcome!

Shows profile picture for the author of the PR

Current status: executed
Reviewer Score
@akhi3030 13

Your contribution is much appreciated with a final score of 13!
You have received 140 (130 base + 10 monthly bonus) Sloth points for this contribution

Congratulations @nagisa! Your PR was highly scored and you completed another monthly streak! To keep your monthly streak make another pull request next month and get 8+ score for it

What is the Race of Sloths

Race of Sloths is a friendly competition where you can participate in challenges and compete with other open-source contributors within your normal workflow

For contributors:

  • Tag @race-of-sloths inside your pull requests
  • Wait for the maintainer to review and score your pull request
  • Check out your position in the Leaderboard
  • Keep weekly and monthly streaks to reach higher positions
  • Boast your contributions with a dynamic picture of your Profile

For maintainers:

  • Score pull requests that participate in the Race of Sloths
  • Engage contributors with fair scoring and fast responses so they keep their streaks
  • Promote the Race to the point where the Race starts promoting you
  • Grow the community of your contributors

Feel free to check our website for additional details!

Bot commands
  • For contributors
    • Include a PR: @race-of-sloths include to enter the Race with your PR
  • For maintainers:
    • Invite contributor @race-of-sloths invite to invite the contributor to participate in a race or include it, if it's already a runner.
    • Assign points: @race-of-sloths score [1/2/3/5/8/13] to award points based on your assessment.
    • Reject this PR: @race-of-sloths exclude to send this PR back to the drawing board.
    • Exclude repo: @race-of-sloths pause to stop bot activity in this repo until @race-of-sloths unpause command is called

@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 6, 2024
@akhi3030
Copy link
Collaborator

akhi3030 commented Aug 6, 2024

@race-of-sloths score 13

nagisa added a commit to nagisa/nearcore that referenced this pull request Aug 7, 2024
This is a regression test for an issue identified in a review of near#11839
@nagisa nagisa added this pull request to the merge queue Aug 7, 2024
Merged via the queue into near:master with commit 08cec9d Aug 7, 2024
31 checks passed
@nagisa nagisa deleted the contract-preparation-pipeline branch August 7, 2024 11:42
nagisa added a commit to nagisa/nearcore that referenced this pull request Aug 7, 2024
This is a regression test for an issue identified in a review of near#11839
github-merge-queue bot pushed a commit that referenced this pull request Aug 7, 2024
…ctions (#11897)

During the development of #11839 an issue was identified that involved
the first local receipt containing a deploy action. This PR adds a
regression test for that issue. While here I also took an opportunity to
move the test suite into its own module since the original file was
getting… way too large.
Trisfald added a commit to Trisfald/nearcore that referenced this pull request Aug 16, 2024
github-merge-queue bot pushed a commit that referenced this pull request Aug 16, 2024
…11964)

This reverts commit 08cec9d.

Reason: mainnet nodes having the commit above get stuck at block
125824281-125824283
nagisa added a commit to nagisa/nearcore that referenced this pull request Aug 20, 2024
nagisa added a commit to nagisa/nearcore that referenced this pull request Aug 27, 2024
nagisa added a commit to nagisa/nearcore that referenced this pull request Sep 6, 2024
nagisa added a commit to nagisa/nearcore that referenced this pull request Sep 23, 2024
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.

5 participants