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

feat: finish code for background flat storage creation #8053

Merged
merged 10 commits into from
Nov 21, 2022
Merged

Conversation

Longarithm
Copy link
Member

@Longarithm Longarithm commented Nov 15, 2022

Implement two remaining steps for background flat storage creation:

Fetching state

We split the state into several parts and fetch them one by one. Number of parts is based on the state size limit, which I set to 10 MiB. Then, fetching is executed in several steps so that we could save intermediate results. Currently each step includes 20 state parts. They are fetched using threads of a separate rayon pool, which size is limited by 4 threads so that it doesn't affect block processing.

Here I also introduce FetchingStateStatus which defines the current progress. It makes testing more convenient: for lightweight tests it is enough to fetch only one part, alhough on production we need thousands of parts.

After state for a shard is fully fetched, we start catching up - which means that we move flat storage head forward and apply all saved deltas, limiting it by 50 blocks at once.

After we fully caught up, we finally create flat storage state.

Testing

@Longarithm Longarithm self-assigned this Nov 15, 2022
@Longarithm Longarithm marked this pull request as ready for review November 15, 2022 15:28
@Longarithm Longarithm requested a review from a team as a code owner November 15, 2022 15:28
@Longarithm Longarithm changed the title draft: finish code for background flat storage creation feat: finish code for background flat storage creation Nov 15, 2022
@matklad matklad removed their request for review November 15, 2022 18:05
Copy link
Contributor

@jakmeier jakmeier left a comment

Choose a reason for hiding this comment

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

Looks solid to me, great to see this progress!

@mzhangmzz I think you should also take a look before we merge, feels like your expertise around the chain and flat state are required here.

@Longarithm Do you plan to add tests to this PR or will it be a follow-up? I would tend towards a separate follow-up PR but it's up to you.

chain/chain/src/flat_storage_creator.rs Outdated Show resolved Hide resolved
core/store/src/columns.rs Outdated Show resolved Hide resolved
chain/chain/src/flat_storage_creator.rs Show resolved Hide resolved
chain/chain/src/flat_storage_creator.rs Show resolved Hide resolved
@jakmeier jakmeier mentioned this pull request Nov 16, 2022
26 tasks
@Longarithm
Copy link
Member Author

Do you plan to add tests to this PR or will it be a follow-up? I would tend towards a separate follow-up PR but it's up to you.

Yeah, they will be in follow-up PRs.

Copy link
Contributor

@mzhangmzz mzhangmzz left a comment

Choose a reason for hiding this comment

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

The PR looks great! Thanks for the awesome work and sorry for the super late review.

Most of my comments are about adding more comments :) Let's try to make the code easier to read for other people and the future us. I approved it to unblock, but please address the comments.

chain/chain/src/flat_storage_creator.rs Outdated Show resolved Hide resolved
chain/chain/src/flat_storage_creator.rs Show resolved Hide resolved
chain/chain/src/flat_storage_creator.rs Show resolved Hide resolved
use near_store::Store;
use near_store::{Trie, TrieDBStorage, TrieTraversalItem};
use std::sync::atomic::AtomicU64;
use std::sync::Arc;
use tracing::debug;
use tracing::info;

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add a paragraph at the beginning of this file to describe on a high level how flat state is migrated, what the steps are, and how FlatStateCreator is used in the code. I know that you already have comments throughout the code, but I think it is still valuable to have an overview to link everything together and help the readers to have a general picture before they dive into the code.

chain/chain/src/flat_storage_creator.rs Outdated Show resolved Hide resolved
merged_delta.merge(delta.as_ref());
}

if old_flat_head != flat_head {
Copy link
Contributor

Choose a reason for hiding this comment

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

is it possible that old_flat_head == flat_head == final_head?

Copy link
Member Author

Choose a reason for hiding this comment

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

In reality - no, because final head should move forward at least once during FetchingState step.
And even if it is the case, it is fine to wait on this step until final head moves forward.

chain/chain/src/flat_storage_creator.rs Outdated Show resolved Hide resolved
// If we reached chain final head, we can finish catchup and finally create flat storage.
store_helper::finish_catchup(&mut store_update, shard_id);
store_update.commit()?;
debug!(target: "chain", %shard_id, %flat_head, %height, "Creating flat storage");
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this debug statement intended? Should it print something like flat storage done?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it is intended.
I agree that "Flat storage creation done" sounds better. Also changed debug! to info! to places where major part of work is finished so users will be aware of status but there won't be much spam in logs.

chain/chain/src/flat_storage_creator.rs Outdated Show resolved Hide resolved
let mut store_update = chain_store.store().store_update();
store_helper::set_flat_head(&mut store_update, shard_id, &block_hash);
store_helper::set_fetching_state_step(&mut store_update, shard_id, 0u64);
store_helper::set_fetching_state_status(&mut store_update, shard_id, status);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we store flat head here with a different prefix, maybe something like set_temporary_flat_head to distinguish it from when flat head is actually set and the flat state is ready to use? This way, there is no way the code can accidentally create a flat storage if flat state is not ready.

Copy link
Member Author

Choose a reason for hiding this comment

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

We probably should. Let's do it separately because this change is already quite big and write some unit test checking that FS can't be created accidentally.

@Longarithm Longarithm merged commit e20a8f7 into master Nov 21, 2022
@Longarithm Longarithm deleted the fs-spawn branch November 21, 2022 19:46
nikurt pushed a commit that referenced this pull request Nov 22, 2022
Co-authored-by: Jakob Meier <mail@jakobmeier.ch>
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.

4 participants