diff --git a/chain/chain/src/chain.rs b/chain/chain/src/chain.rs index c9c8bdf848f..515473c397f 100644 --- a/chain/chain/src/chain.rs +++ b/chain/chain/src/chain.rs @@ -18,8 +18,8 @@ use crate::types::{ }; use crate::update_shard::{ apply_new_chunk, process_missing_chunks_range, process_shard_update, NewChunkData, - NewChunkResult, OldChunkData, ShardContext, ShardUpdateReason, ShardUpdateResult, - StateSplitData, StorageContext, + NewChunkResult, OldChunkData, ReshardingData, ShardContext, ShardUpdateReason, + ShardUpdateResult, StorageContext, }; use crate::validate::{ validate_challenge, validate_chunk_proofs, validate_chunk_with_chunk_extra, @@ -35,7 +35,7 @@ use chrono::Duration; use crossbeam_channel::{unbounded, Receiver, Sender}; use itertools::Itertools; use lru::LruCache; -use near_chain_configs::{MutableConfigValue, StateSplitConfig, StateSplitHandle}; +use near_chain_configs::{MutableConfigValue, ReshardingConfig, ReshardingHandle}; #[cfg(feature = "new_epoch_sync")] use near_chain_primitives::error::epoch_sync::EpochSyncInfoError; use near_chain_primitives::error::{BlockKnownError, Error, LogTransientStorageError}; @@ -268,11 +268,11 @@ pub struct Chain { snapshot_callbacks: Option, /// Configuration for resharding. - pub(crate) state_split_config: MutableConfigValue, + pub(crate) resharding_config: MutableConfigValue, // A handle that allows the main process to interrupt resharding if needed. // This typically happens when the main process is interrupted. - pub state_split_handle: StateSplitHandle, + pub resharding_handle: ReshardingHandle, } impl Drop for Chain { @@ -366,11 +366,11 @@ impl Chain { pending_state_patch: Default::default(), requested_state_parts: StateRequestTracker::new(), snapshot_callbacks: None, - state_split_config: MutableConfigValue::new( - StateSplitConfig::default(), - "state_split_config", + resharding_config: MutableConfigValue::new( + ReshardingConfig::default(), + "resharding_config", ), - state_split_handle: StateSplitHandle::new(), + resharding_handle: ReshardingHandle::new(), }) } @@ -546,8 +546,8 @@ impl Chain { pending_state_patch: Default::default(), requested_state_parts: StateRequestTracker::new(), snapshot_callbacks, - state_split_config: chain_config.state_split_config, - state_split_handle: StateSplitHandle::new(), + resharding_config: chain_config.resharding_config, + resharding_handle: ReshardingHandle::new(), }) } @@ -3156,19 +3156,20 @@ impl Chain { }) } - fn get_split_state_roots( + fn get_resharding_state_roots( &self, block: &Block, shard_id: ShardId, ) -> Result, Error> { let next_shard_layout = self.epoch_manager.get_shard_layout(block.header().next_epoch_id())?; - let new_shards = next_shard_layout.get_split_shard_uids(shard_id).unwrap_or_else(|| { - panic!( - "shard layout must contain maps of all shards to its split shards {} {:?}", - shard_id, next_shard_layout, - ); - }); + let new_shards = + next_shard_layout.get_children_shards_uids(shard_id).unwrap_or_else(|| { + panic!( + "shard layout must contain maps of all shards to its children shards {} {:?}", + shard_id, next_shard_layout, + ); + }); new_shards .iter() .map(|shard_uid| { @@ -3300,14 +3301,14 @@ impl Chain { cares_about_shard_this_epoch, cares_about_shard_next_epoch, ); - let need_to_split_states = will_shard_layout_change && cares_about_shard_next_epoch; + let need_to_reshard = will_shard_layout_change && cares_about_shard_next_epoch; let shard_uid = self.epoch_manager.shard_id_to_uid(shard_id, epoch_id)?; Ok(ShardContext { shard_uid, cares_about_shard_this_epoch, will_shard_layout_change, should_apply_transactions, - need_to_split_states, + need_to_reshard, }) } @@ -3328,24 +3329,24 @@ impl Chain { let prev_hash = block.header().prev_hash(); let shard_context = self.get_shard_context(me, block.header(), shard_id, mode)?; - // We can only split states when states are ready, i.e., mode != ApplyChunksMode::NotCaughtUp - // 1) if should_apply_transactions == true && split_state_roots.is_some(), - // that means split states are ready. - // `apply_split_state_changes` will apply updates to split_states - // 2) if should_apply_transactions == true && split_state_roots.is_none(), - // that means split states are not ready yet. - // `apply_split_state_changes` will return `state_changes_for_split_states`, + // We can only perform resharding when states are ready, i.e., mode != ApplyChunksMode::NotCaughtUp + // 1) if should_apply_transactions == true && resharding_state_roots.is_some(), + // that means children shards are ready. + // `apply_resharding_state_changes` will apply updates to the children shards + // 2) if should_apply_transactions == true && resharding_state_roots.is_none(), + // that means children shards are not ready yet. + // `apply_resharding_state_changes` will return `state_changes_for_resharding`, // which will be stored to the database in `process_apply_chunks` - // 3) if should_apply_transactions == false && split_state_roots.is_some() + // 3) if should_apply_transactions == false && resharding_state_roots.is_some() // This implies mode == CatchingUp and cares_about_shard_this_epoch == true, // otherwise should_apply_transactions will be true // That means transactions have already been applied last time when apply_chunks are - // called with mode NotCaughtUp, therefore `state_changes_for_split_states` have been + // called with mode NotCaughtUp, therefore `state_changes_for_resharding` have been // stored in the database. Then we can safely read that and apply that to the split // states - let split_state_roots = - if shard_context.need_to_split_states && mode != ApplyChunksMode::NotCaughtUp { - Some(self.get_split_state_roots(block, shard_id)?) + let resharding_state_roots = + if shard_context.need_to_reshard && mode != ApplyChunksMode::NotCaughtUp { + Some(self.get_resharding_state_roots(block, shard_id)?) } else { None }; @@ -3427,7 +3428,7 @@ impl Chain { is_first_block_with_chunk_of_version, chunk, receipts, - split_state_roots, + resharding_state_roots, storage_context, }) } else { @@ -3436,21 +3437,21 @@ impl Chain { prev_chunk_extra: ChunkExtra::clone( self.get_chunk_extra(prev_hash, &shard_context.shard_uid)?.as_ref(), ), - split_state_roots, + resharding_state_roots, storage_context, }) } - } else if let Some(split_state_roots) = split_state_roots { + } else if let Some(resharding_state_roots) = resharding_state_roots { assert!( mode == ApplyChunksMode::CatchingUp && shard_context.cares_about_shard_this_epoch ); let state_changes = - self.chain_store().get_state_changes_for_split_states(block.hash(), shard_id)?; - ShardUpdateReason::StateSplit(StateSplitData { + self.chain_store().get_state_changes_for_resharding(block.hash(), shard_id)?; + ShardUpdateReason::Resharding(ReshardingData { block_hash: *block.hash(), block_height: block.header().height(), state_changes, - split_state_roots, + resharding_state_roots, }) } else { return Ok(None); @@ -3498,9 +3499,9 @@ impl Chain { let block_header = self.get_block_header(&block_hash)?; let prev_block_header = self.get_previous_header(&block_header)?; let shard_context = self.get_shard_context(me, &block_header, shard_id, mode)?; - if shard_context.need_to_split_states { + if shard_context.need_to_reshard { return Err(Error::Other(String::from( - "State split occurred in blocks range, it is not supported yet", + "Resharding occurred in blocks range, it is not supported yet", ))); } execution_contexts.push(( @@ -3662,7 +3663,7 @@ impl Chain { let block_header = self.get_block_header(&prev_chunk_block_hash)?; let prev_block_header = self.get_previous_header(&block_header)?; let shard_context = self.get_shard_context(me, &block_header, shard_id, mode)?; - if shard_context.need_to_split_states { + if shard_context.need_to_reshard { return Ok(None); } ( @@ -3709,28 +3710,24 @@ impl Chain { // TODO(logunov): use `validate_chunk_with_chunk_extra` assert_eq!(current_chunk_extra.state_root(), &prev_chunk.prev_state_root()); // Process previous chunk. - let NewChunkResult { - gas_limit, - shard_uid, - apply_result, - apply_split_result_or_state_changes: _, - } = apply_new_chunk( - parent_span, - NewChunkData { - chunk: prev_chunk, - receipts, - split_state_roots: None, - block: prev_chunk_block_context.clone(), - is_first_block_with_chunk_of_version: false, - storage_context: StorageContext { - storage_data_source: StorageDataSource::DbTrieOnly, - state_patch: Default::default(), + let NewChunkResult { gas_limit, shard_uid, apply_result, resharding_results: _ } = + apply_new_chunk( + parent_span, + NewChunkData { + chunk: prev_chunk, + receipts, + resharding_state_roots: None, + block: prev_chunk_block_context.clone(), + is_first_block_with_chunk_of_version: false, + storage_context: StorageContext { + storage_data_source: StorageDataSource::DbTrieOnly, + state_patch: Default::default(), + }, }, - }, - prev_chunk_shard_context, - runtime.as_ref(), - epoch_manager.as_ref(), - )?; + prev_chunk_shard_context, + runtime.as_ref(), + epoch_manager.as_ref(), + )?; let (outcome_root, _) = ApplyTransactionResult::compute_outcomes_proof(&apply_result.outcomes); current_chunk_extra = ChunkExtra::new( @@ -3866,8 +3863,8 @@ fn sync_hash_not_first_hash(sync_hash: CryptoHash) -> Error { /// We want to guarantee that transactions are only applied once for each shard, /// even though apply_chunks may be called twice, once with /// ApplyChunksMode::NotCaughtUp once with ApplyChunksMode::CatchingUp. Note -/// that it does not guard whether we split states or not, see the comments -/// before `need_to_split_state` +/// that it does not guard whether the children shards are ready or not, see the +/// comments before `need_to_reshard` fn get_should_apply_transactions( mode: ApplyChunksMode, cares_about_shard_this_epoch: bool, @@ -4229,8 +4226,8 @@ impl Chain { shard_ids = shard_ids .into_iter() .flat_map(|id| { - next_shard_layout.get_split_shard_ids(id).unwrap_or_else(|| { - panic!("invalid shard layout {:?} because it does not contain split shards for parent shard {}", next_shard_layout, id) + next_shard_layout.get_children_shards_ids(id).unwrap_or_else(|| { + panic!("invalid shard layout {:?} because it does not contain children shards for parent shard {}", next_shard_layout, id) }) }) .collect(); diff --git a/chain/chain/src/chain_update.rs b/chain/chain/src/chain_update.rs index 36331cc04a4..fb58a31559b 100644 --- a/chain/chain/src/chain_update.rs +++ b/chain/chain/src/chain_update.rs @@ -4,11 +4,11 @@ use crate::metrics::{SHARD_LAYOUT_NUM_SHARDS, SHARD_LAYOUT_VERSION}; use crate::store::{ChainStore, ChainStoreAccess, ChainStoreUpdate}; use crate::types::{ - ApplySplitStateResultOrStateChanges, ApplyTransactionResult, ApplyTransactionsBlockContext, - ApplyTransactionsChunkContext, RuntimeAdapter, RuntimeStorageConfig, + ApplyTransactionResult, ApplyTransactionsBlockContext, ApplyTransactionsChunkContext, + ReshardingResults, RuntimeAdapter, RuntimeStorageConfig, }; use crate::update_shard::{ - NewChunkResult, OldChunkResult, ShardBlockUpdateResult, ShardUpdateResult, StateSplitResult, + NewChunkResult, OldChunkResult, ReshardingResult, ShardBlockUpdateResult, ShardUpdateResult, }; use crate::{metrics, DoomslugThresholdMode}; use crate::{Chain, Doomslug}; @@ -172,29 +172,30 @@ impl<'a> ChainUpdate<'a> { Ok(()) } - /// Postprocess split state results or state changes, do the necessary update on chain - /// for split state results: store the chunk extras and trie changes for the split states - /// for state changes, store the state changes for splitting states - fn process_split_state( + /// Postprocess resharding results and do the necessary update on chain for + /// resharding results. + /// - Store the chunk extras and trie changes for the apply results. + /// - Store the state changes to be applided later for the store results. + fn process_resharding_results( &mut self, block: &Block, shard_uid: &ShardUId, - apply_results_or_state_changes: ApplySplitStateResultOrStateChanges, + resharding_results: ReshardingResults, ) -> Result<(), Error> { let block_hash = block.hash(); let prev_hash = block.header().prev_hash(); let height = block.header().height(); - match apply_results_or_state_changes { - ApplySplitStateResultOrStateChanges::ApplySplitStateResults(mut results) => { - tracing::debug!(target: "resharding", height, ?shard_uid, "process_split_state apply"); + match resharding_results { + ReshardingResults::ApplyReshardingResults(mut results) => { + tracing::debug!(target: "resharding", height, ?shard_uid, "process_resharding_results apply"); // Sort the results so that the gas reassignment is deterministic. results.sort_unstable_by_key(|r| r.shard_uid); // Drop the mutability as we no longer need it. let results = results; - // Split validator_proposals, gas_burnt, balance_burnt to each split shard - // and store the chunk extra for split shards + // Split validator_proposals, gas_burnt, balance_burnt to each child shard + // and store the chunk extra for children shards // Note that here we do not split outcomes by the new shard layout, we simply store // the outcome_root from the parent shard. This is because outcome proofs are // generated per shard using the old shard layout and stored in the database. @@ -220,24 +221,24 @@ impl<'a> ChainUpdate<'a> { } let num_split_shards = next_epoch_shard_layout - .get_split_shard_uids(shard_uid.shard_id()) + .get_children_shards_uids(shard_uid.shard_id()) .unwrap_or_else(|| panic!("invalid shard layout {:?}", next_epoch_shard_layout)) .len() as NumShards; let total_gas_used = chunk_extra.gas_used(); let total_balance_burnt = chunk_extra.balance_burnt(); - // The gas remainder, the split shards will be reassigned one + // The gas remainder, the children shards will be reassigned one // unit each until its depleted. let mut gas_res = total_gas_used % num_split_shards; - // The gas quotient, the split shards will be reassigned the + // The gas quotient, the children shards will be reassigned the // full value each. let gas_split = total_gas_used / num_split_shards; - // The balance remainder, the split shards will be reassigned one + // The balance remainder, the children shards will be reassigned one // unit each until its depleted. let mut balance_res = (total_balance_burnt % num_split_shards as u128) as NumShards; - // The balance quotient, the split shards will be reassigned the + // The balance quotient, the children shards will be reassigned the // full value each. let balance_split = total_balance_burnt / (num_split_shards as u128); @@ -300,9 +301,9 @@ impl<'a> ChainUpdate<'a> { assert_eq!(sum_gas_used, total_gas_used); assert_eq!(sum_balance_burnt, total_balance_burnt); } - ApplySplitStateResultOrStateChanges::StateChangesForSplitStates(state_changes) => { - tracing::debug!(target: "resharding", height, ?shard_uid, "process_split_state store"); - self.chain_store_update.add_state_changes_for_split_states( + ReshardingResults::StoreReshardingResults(state_changes) => { + tracing::debug!(target: "resharding", height, ?shard_uid, "process_resharding_results store"); + self.chain_store_update.add_state_changes_for_resharding( *block_hash, shard_uid.shard_id(), state_changes, @@ -312,7 +313,7 @@ impl<'a> ChainUpdate<'a> { Ok(()) } - /// Processed results of applying chunk + /// Process results of applying chunk fn process_apply_chunk_result( &mut self, block: &Block, @@ -326,7 +327,7 @@ impl<'a> ChainUpdate<'a> { gas_limit, shard_uid, apply_result, - apply_split_result_or_state_changes, + resharding_results, }) => { let (outcome_root, outcome_paths) = ApplyTransactionResult::compute_outcomes_proof(&apply_result.outcomes); @@ -369,14 +370,14 @@ impl<'a> ChainUpdate<'a> { apply_result.outcomes, outcome_paths, ); - if let Some(apply_results_or_state_changes) = apply_split_result_or_state_changes { - self.process_split_state(block, &shard_uid, apply_results_or_state_changes)?; + if let Some(resharding_results) = resharding_results { + self.process_resharding_results(block, &shard_uid, resharding_results)?; } } ShardBlockUpdateResult::OldChunk(OldChunkResult { shard_uid, apply_result, - apply_split_result_or_state_changes, + resharding_results, }) => { let old_extra = self.chain_store_update.get_chunk_extra(prev_hash, &shard_uid)?; @@ -396,17 +397,17 @@ impl<'a> ChainUpdate<'a> { self.chain_store_update.save_chunk_extra(block_hash, &shard_uid, new_extra); self.chain_store_update.save_trie_changes(apply_result.trie_changes); - if let Some(apply_results_or_state_changes) = apply_split_result_or_state_changes { - self.process_split_state(block, &shard_uid, apply_results_or_state_changes)?; + if let Some(resharding_config) = resharding_results { + self.process_resharding_results(block, &shard_uid, resharding_config)?; } } - ShardBlockUpdateResult::StateSplit(StateSplitResult { shard_uid, results }) => { + ShardBlockUpdateResult::Resharding(ReshardingResult { shard_uid, results }) => { self.chain_store_update - .remove_state_changes_for_split_states(*block.hash(), shard_uid.shard_id()); - self.process_split_state( + .remove_state_changes_for_resharding(*block.hash(), shard_uid.shard_id()); + self.process_resharding_results( block, &shard_uid, - ApplySplitStateResultOrStateChanges::ApplySplitStateResults(results), + ReshardingResults::ApplyReshardingResults(results), )?; } }; diff --git a/chain/chain/src/metrics.rs b/chain/chain/src/metrics.rs index 724829e9e2e..f5922188fbe 100644 --- a/chain/chain/src/metrics.rs +++ b/chain/chain/src/metrics.rs @@ -143,7 +143,7 @@ pub(crate) static LARGEST_FINAL_HEIGHT: Lazy = Lazy::new(|| { }); pub(crate) enum ReshardingStatus { - /// The StateSplitRequest was send to the SyncJobsActor. + /// The ReshardingRequest was send to the SyncJobsActor. Scheduled, /// The SyncJobsActor is performing the resharding. BuildingState, diff --git a/chain/chain/src/resharding.rs b/chain/chain/src/resharding.rs index 3bff78da6f3..87a5680a1cf 100644 --- a/chain/chain/src/resharding.rs +++ b/chain/chain/src/resharding.rs @@ -1,5 +1,5 @@ /// Implementation for all resharding logic. -/// StateSplitRequest and StateSplitResponse are exchanged across the client_actor and SyncJobsActor. +/// ReshardingRequest and ReshardingResponse are exchanged across the client_actor and SyncJobsActor. /// build_state_for_split_shards_preprocessing and build_state_for_split_shards_postprocessing are handled /// by the client_actor while the heavy resharding build_state_for_split_shards is done by SyncJobsActor /// so as to not affect client. @@ -10,7 +10,7 @@ use crate::metrics::{ }; use crate::Chain; use itertools::Itertools; -use near_chain_configs::{MutableConfigValue, StateSplitConfig, StateSplitHandle}; +use near_chain_configs::{MutableConfigValue, ReshardingConfig, ReshardingHandle}; use near_chain_primitives::error::Error; use near_primitives::errors::StorageError::StorageInconsistentState; use near_primitives::hash::CryptoHash; @@ -22,7 +22,7 @@ use near_store::flat::{ store_helper, BlockInfo, FlatStorageError, FlatStorageManager, FlatStorageReadyStatus, FlatStorageStatus, }; -use near_store::split_state::get_delayed_receipts; +use near_store::resharding::get_delayed_receipts; use near_store::trie::SnapshotError; use near_store::{ShardTries, ShardUId, StorageError, Store, Trie, TrieDBStorage, TrieStorage}; use std::collections::{HashMap, HashSet}; @@ -31,12 +31,12 @@ use std::sync::Arc; use std::time::Duration; use tracing::debug; -/// StateSplitRequest has all the information needed to start a resharding job. This message is sent +/// ReshardingRequest has all the information needed to start a resharding job. This message is sent /// from ClientActor to SyncJobsActor. We do not want to stall the ClientActor with a long running /// resharding job. The SyncJobsActor is helpful for handling such long running jobs. #[derive(actix::Message)] #[rtype(result = "()")] -pub struct StateSplitRequest { +pub struct ReshardingRequest { pub tries: Arc, // The block hash of the first block of the epoch. pub sync_hash: CryptoHash, @@ -53,17 +53,17 @@ pub struct StateSplitRequest { // Time we've spent polling for the state snapshot to be ready. We autofail after a certain time. pub curr_poll_time: Duration, // Configuration for resharding. Can be used to throttle resharding if needed. - pub config: MutableConfigValue, + pub config: MutableConfigValue, // A handle that allows the main process to interrupt resharding if needed. // This typically happens when the main process is interrupted. - pub handle: StateSplitHandle, + pub handle: ReshardingHandle, } // Skip `runtime_adapter`, because it's a complex object that has complex logic // and many fields. -impl Debug for StateSplitRequest { +impl Debug for ReshardingRequest { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.debug_struct("StateSplitRequest") + f.debug_struct("ReshardingRequest") .field("tries", &"") .field("sync_hash", &self.sync_hash) .field("prev_hash", &self.prev_hash) @@ -76,10 +76,10 @@ impl Debug for StateSplitRequest { } } -// StateSplitResponse is the response sent from SyncJobsActor to ClientActor once resharding is completed. +// ReshardingResponse is the response sent from SyncJobsActor to ClientActor once resharding is completed. #[derive(actix::Message, Debug)] #[rtype(result = "()")] -pub struct StateSplitResponse { +pub struct ReshardingResponse { pub sync_hash: CryptoHash, pub shard_id: ShardId, pub new_state_roots: Result, Error>, @@ -106,7 +106,7 @@ fn get_checked_account_id_to_shard_uid_fn( } } -// Format of the trie key, value pair that is used in tries.add_values_to_split_states() function +// Format of the trie key, value pair that is used in tries.add_values_to_children_states() function type TrieEntry = (Vec, Option>); struct TrieUpdateBatch { @@ -117,7 +117,7 @@ struct TrieUpdateBatch { // Function to return batches of trie key, value pairs from flat storage iter. We return None at the end of iter. // The batch size is roughly batch_memory_limit. fn get_trie_update_batch( - config: &StateSplitConfig, + config: &ReshardingConfig, iter: &mut impl Iterator, Option>), FlatStorageError>>, ) -> Result, FlatStorageError> { let mut size: u64 = 0; @@ -138,7 +138,7 @@ fn get_trie_update_batch( } fn apply_delayed_receipts<'a>( - config: &StateSplitConfig, + config: &ReshardingConfig, tries: &ShardTries, orig_shard_uid: ShardUId, orig_state_root: StateRoot, @@ -152,7 +152,7 @@ fn apply_delayed_receipts<'a>( while let Some((next_index, receipts)) = get_delayed_receipts(&orig_trie_update, start_index, config.batch_size)? { - let (store_update, updated_state_roots) = tries.apply_delayed_receipts_to_split_states( + let (store_update, updated_state_roots) = tries.apply_delayed_receipts_to_children_states( &new_state_roots, &receipts, account_id_to_shard_uid, @@ -197,11 +197,11 @@ fn read_flat_state_value( } impl Chain { - pub fn build_state_for_split_shards_preprocessing( + pub fn build_state_for_resharding_preprocessing( &self, sync_hash: &CryptoHash, shard_id: ShardId, - state_split_scheduler: &dyn Fn(StateSplitRequest), + resharding_scheduler: &dyn Fn(ReshardingRequest), ) -> Result<(), Error> { tracing::debug!(target: "resharding", ?shard_id, ?sync_hash, "preprocessing started"); let block_header = self.get_block_header(sync_hash)?; @@ -216,7 +216,7 @@ impl Chain { let prev_prev_hash = prev_block_header.prev_hash(); let state_root = *self.get_chunk_extra(&prev_hash, &shard_uid)?.state_root(); - state_split_scheduler(StateSplitRequest { + resharding_scheduler(ReshardingRequest { tries: Arc::new(self.runtime_adapter.get_tries()), sync_hash: *sync_hash, prev_hash: *prev_hash, @@ -225,8 +225,8 @@ impl Chain { state_root, next_epoch_shard_layout, curr_poll_time: Duration::ZERO, - config: self.state_split_config.clone(), - handle: self.state_split_handle.clone(), + config: self.resharding_config.clone(), + handle: self.resharding_handle.clone(), }); RESHARDING_STATUS @@ -237,15 +237,15 @@ impl Chain { /// Function to check whether the snapshot is ready for resharding or not. We return true if the snapshot is not /// ready and we need to retry/reschedule the resharding job. - pub fn retry_build_state_for_split_shards(state_split_request: &StateSplitRequest) -> bool { - let StateSplitRequest { tries, prev_prev_hash, curr_poll_time, config, .. } = - state_split_request; + pub fn retry_build_state_for_split_shards(resharding_request: &ReshardingRequest) -> bool { + let ReshardingRequest { tries, prev_prev_hash, curr_poll_time, config, .. } = + resharding_request; let config = config.get(); // Do not retry if we have spent more than max_poll_time // The error would be caught in build_state_for_split_shards and propagated to client actor if curr_poll_time > &config.max_poll_time { - tracing::warn!(target: "resharding", ?curr_poll_time, ?config.max_poll_time, "exceeded max poll time while waiting for snapsthot"); + tracing::warn!(target: "resharding", ?curr_poll_time, ?config.max_poll_time, "exceeded max poll time while waiting for snapshot"); return false; } @@ -265,12 +265,12 @@ impl Chain { } pub fn build_state_for_split_shards( - state_split_request: StateSplitRequest, - ) -> StateSplitResponse { - let shard_uid = state_split_request.shard_uid; + resharding_request: ReshardingRequest, + ) -> ReshardingResponse { + let shard_uid = resharding_request.shard_uid; let shard_id = shard_uid.shard_id(); - let sync_hash = state_split_request.sync_hash; - let new_state_roots = Self::build_state_for_split_shards_impl(state_split_request); + let sync_hash = resharding_request.sync_hash; + let new_state_roots = Self::build_state_for_split_shards_impl(resharding_request); match &new_state_roots { Ok(_) => {} Err(err) => { @@ -280,13 +280,13 @@ impl Chain { .set(ReshardingStatus::Failed.into()); } } - StateSplitResponse { shard_id, sync_hash, new_state_roots } + ReshardingResponse { shard_id, sync_hash, new_state_roots } } fn build_state_for_split_shards_impl( - state_split_request: StateSplitRequest, + resharding_request: ReshardingRequest, ) -> Result, Error> { - let StateSplitRequest { + let ReshardingRequest { tries, prev_hash, prev_prev_hash, @@ -296,12 +296,12 @@ impl Chain { config, handle, .. - } = state_split_request; + } = resharding_request; tracing::debug!(target: "resharding", config=?config.get(), ?shard_uid, "build_state_for_split_shards_impl starting"); let shard_id = shard_uid.shard_id(); let new_shards = next_epoch_shard_layout - .get_split_shard_uids(shard_id) + .get_children_shards_uids(shard_id) .ok_or(Error::InvalidShardId(shard_id))?; let mut state_roots: HashMap<_, _> = new_shards.iter().map(|shard_uid| (*shard_uid, Trie::EMPTY_ROOT)).collect(); @@ -376,14 +376,14 @@ impl Chain { batch }; - // Apply the batch - add values to the split states. + // Apply the batch - add values to the children shards. let TrieUpdateBatch { entries, size } = batch; let store_update = { let histogram = RESHARDING_BATCH_APPLY_TIME.with_label_values(&metrics_labels); let _timer = histogram.start_timer(); // TODO(#9435): This is highly inefficient as for each key in the batch, we are parsing the account_id // A better way would be to use the boundary account to construct the from and to key range for flat storage iterator - let (store_update, new_state_roots) = tries.add_values_to_split_states( + let (store_update, new_state_roots) = tries.add_values_to_children_states( &state_roots, entries, &checked_account_id_to_shard_uid, @@ -437,7 +437,7 @@ impl Chain { // here we store the state roots in chunk_extra in the database for later use let chunk_extra = ChunkExtra::new_with_only_state_root(&state_root); chain_store_update.save_chunk_extra(&prev_hash, &shard_uid, chunk_extra); - debug!(target:"resharding", "Finish building split state for shard {:?} {:?} {:?} ", shard_uid, prev_hash, state_root); + debug!(target:"resharding", "Finish building resharding for shard {:?} {:?} {:?} ", shard_uid, prev_hash, state_root); } chain_store_update.commit()?; diff --git a/chain/chain/src/store.rs b/chain/chain/src/store.rs index afe40a6a07e..93ca253559d 100644 --- a/chain/chain/src/store.rs +++ b/chain/chain/src/store.rs @@ -33,7 +33,7 @@ use near_primitives::trie_key::{trie_key_parsers, TrieKey}; use near_primitives::types::chunk_extra::ChunkExtra; use near_primitives::types::{ BlockExtra, BlockHeight, EpochId, NumBlocks, ShardId, StateChanges, StateChangesExt, - StateChangesForSplitStates, StateChangesKinds, StateChangesKindsExt, StateChangesRequest, + StateChangesForResharding, StateChangesKinds, StateChangesKindsExt, StateChangesRequest, }; use near_primitives::utils::{ get_block_shard_id, get_outcome_id_block_hash, get_outcome_id_block_hash_rev, index_to_bytes, @@ -520,11 +520,11 @@ impl ChainStore { .collect() } - pub fn get_state_changes_for_split_states( + pub fn get_state_changes_for_resharding( &self, block_hash: &CryptoHash, shard_id: ShardId, - ) -> Result { + ) -> Result { let key = &get_block_shard_id(block_hash, shard_id); option_to_not_found( self.store.get_ser(DBCol::StateChangesForSplitStates, key), @@ -645,7 +645,7 @@ impl ChainStore { shard_id: ShardId, receipts_shard_id: ShardId, ) -> Result<(), Error> { - let split_shard_ids = shard_layout.get_split_shard_ids(receipts_shard_id); + let split_shard_ids = shard_layout.get_children_shards_ids(receipts_shard_id); let split_shard_ids = split_shard_ids.ok_or(Error::InvalidSplitShardsIds(shard_id, receipts_shard_id))?; @@ -1438,9 +1438,11 @@ pub struct ChainStoreUpdate<'a> { final_head: Option, largest_target_height: Option, trie_changes: Vec, - // All state changes made by a chunk, this is only used for splitting states - add_state_changes_for_split_states: HashMap<(CryptoHash, ShardId), StateChangesForSplitStates>, - remove_state_changes_for_split_states: HashSet<(CryptoHash, ShardId)>, + + // All state changes made by a chunk, this is only used for resharding. + add_state_changes_for_resharding: HashMap<(CryptoHash, ShardId), StateChangesForResharding>, + remove_state_changes_for_resharding: HashSet<(CryptoHash, ShardId)>, + add_blocks_to_catchup: Vec<(CryptoHash, CryptoHash)>, // A pair (prev_hash, hash) to be removed from blocks to catchup remove_blocks_to_catchup: Vec<(CryptoHash, CryptoHash)>, @@ -1465,8 +1467,8 @@ impl<'a> ChainStoreUpdate<'a> { final_head: None, largest_target_height: None, trie_changes: vec![], - add_state_changes_for_split_states: HashMap::new(), - remove_state_changes_for_split_states: HashSet::new(), + add_state_changes_for_resharding: HashMap::new(), + remove_state_changes_for_resharding: HashSet::new(), add_blocks_to_catchup: vec![], remove_blocks_to_catchup: vec![], remove_prev_blocks_to_catchup: vec![], @@ -2075,26 +2077,26 @@ impl<'a> ChainStoreUpdate<'a> { self.trie_changes.push(trie_changes); } - pub fn add_state_changes_for_split_states( + pub fn add_state_changes_for_resharding( &mut self, block_hash: CryptoHash, shard_id: ShardId, - state_changes: StateChangesForSplitStates, + state_changes: StateChangesForResharding, ) { let prev = - self.add_state_changes_for_split_states.insert((block_hash, shard_id), state_changes); + self.add_state_changes_for_resharding.insert((block_hash, shard_id), state_changes); // We should not save state changes for the same chunk twice assert!(prev.is_none()); } - pub fn remove_state_changes_for_split_states( + pub fn remove_state_changes_for_resharding( &mut self, block_hash: CryptoHash, shard_id: ShardId, ) { // We should not remove state changes for the same chunk twice let value_not_present = - self.remove_state_changes_for_split_states.insert((block_hash, shard_id)); + self.remove_state_changes_for_resharding.insert((block_hash, shard_id)); assert!(value_not_present); } @@ -2524,8 +2526,7 @@ impl<'a> ChainStoreUpdate<'a> { } } - for ((block_hash, shard_id), state_changes) in - self.add_state_changes_for_split_states.drain() + for ((block_hash, shard_id), state_changes) in self.add_state_changes_for_resharding.drain() { store_update.set_ser( DBCol::StateChangesForSplitStates, @@ -2533,7 +2534,7 @@ impl<'a> ChainStoreUpdate<'a> { &state_changes, )?; } - for (block_hash, shard_id) in self.remove_state_changes_for_split_states.drain() { + for (block_hash, shard_id) in self.remove_state_changes_for_resharding.drain() { store_update.delete( DBCol::StateChangesForSplitStates, &get_block_shard_id(&block_hash, shard_id), diff --git a/chain/chain/src/test_utils/kv_runtime.rs b/chain/chain/src/test_utils/kv_runtime.rs index ddf23a282d8..95b61af0029 100644 --- a/chain/chain/src/test_utils/kv_runtime.rs +++ b/chain/chain/src/test_utils/kv_runtime.rs @@ -1,6 +1,6 @@ use super::ValidatorSchedule; use crate::types::{ - ApplySplitStateResult, ApplyTransactionResult, ApplyTransactionsBlockContext, + ApplyResultForResharding, ApplyTransactionResult, ApplyTransactionsBlockContext, ApplyTransactionsChunkContext, RuntimeAdapter, RuntimeStorageConfig, }; use crate::BlockHeader; @@ -32,7 +32,7 @@ use near_primitives::transaction::{ use near_primitives::types::validator_stake::ValidatorStake; use near_primitives::types::{ AccountId, ApprovalStake, Balance, BlockHeight, EpochHeight, EpochId, Gas, Nonce, NumShards, - ShardId, StateChangesForSplitStates, StateRoot, StateRootNode, ValidatorInfoIdentifier, + ShardId, StateChangesForResharding, StateRoot, StateRootNode, ValidatorInfoIdentifier, }; use near_primitives::validator_mandates::AssignmentWeight; use near_primitives::version::{ProtocolVersion, PROTOCOL_VERSION}; @@ -1377,14 +1377,14 @@ impl RuntimeAdapter for KeyValueRuntime { Ok(false) } - fn apply_update_to_split_states( + fn apply_update_to_children_states( &self, _block_hash: &CryptoHash, _block_height: BlockHeight, _state_roots: HashMap, _next_shard_layout: &ShardLayout, - _state_changes: StateChangesForSplitStates, - ) -> Result, Error> { + _state_changes: StateChangesForResharding, + ) -> Result, Error> { Ok(vec![]) } diff --git a/chain/chain/src/types.rs b/chain/chain/src/types.rs index 81d2cde513f..b7fc024f4b6 100644 --- a/chain/chain/src/types.rs +++ b/chain/chain/src/types.rs @@ -5,7 +5,7 @@ use borsh::{BorshDeserialize, BorshSerialize}; use chrono::DateTime; use chrono::Utc; use near_chain_configs::MutableConfigValue; -use near_chain_configs::StateSplitConfig; +use near_chain_configs::ReshardingConfig; use near_primitives::sandbox::state_patch::SandboxStatePatch; use near_store::flat::FlatStorageManager; use near_store::StorageError; @@ -26,7 +26,7 @@ use near_primitives::transaction::{ExecutionOutcomeWithId, SignedTransaction}; use near_primitives::types::validator_stake::{ValidatorStake, ValidatorStakeIter}; use near_primitives::types::{ Balance, BlockHeight, BlockHeightDelta, EpochId, Gas, MerkleHash, NumBlocks, ShardId, - StateChangesForSplitStates, StateRoot, StateRootNode, + StateChangesForResharding, StateRoot, StateRootNode, }; use near_primitives::version::{ ProtocolVersion, MIN_GAS_PRICE_NEP_92, MIN_GAS_PRICE_NEP_92_FIX, MIN_PROTOCOL_VERSION_NEP_92, @@ -79,25 +79,24 @@ pub struct AcceptedBlock { } #[derive(Debug)] -pub struct ApplySplitStateResult { +pub struct ApplyResultForResharding { pub shard_uid: ShardUId, pub trie_changes: WrappedTrieChanges, pub new_root: StateRoot, } -// This struct captures two cases -// when apply transactions, split states may or may not be ready -// if it's ready, apply transactions also apply updates to split states and this enum will be -// ApplySplitStateResults -// otherwise, it simply returns the state changes needed to be applied to split states +// ReshardingResults contains the results of applying depending on whether +// resharding is finished. +// If resharding is finished the results should be applied immediately. +// If resharding is not finished the results should be stored and applied later. #[derive(Debug)] -pub enum ApplySplitStateResultOrStateChanges { - /// Immediately apply the split state result. +pub enum ReshardingResults { + /// Immediately apply the resharding result. /// Happens during IsCaughtUp and CatchingUp - ApplySplitStateResults(Vec), - /// Store the split state results so that they can be applied later. + ApplyReshardingResults(Vec), + /// Store the resharding results so that they can be applied later. /// Happens during NotCaughtUp. - StateChangesForSplitStates(StateChangesForSplitStates), + StoreReshardingResults(StateChangesForResharding), } #[derive(Debug)] @@ -214,7 +213,7 @@ pub struct ChainConfig { /// Currently used for flat storage background creation. pub background_migration_threads: usize, /// The resharding configuration. - pub state_split_config: MutableConfigValue, + pub resharding_config: MutableConfigValue, } impl ChainConfig { @@ -222,9 +221,9 @@ impl ChainConfig { Self { save_trie_changes: true, background_migration_threads: 1, - state_split_config: MutableConfigValue::new( - StateSplitConfig::default(), - "state_split_config", + resharding_config: MutableConfigValue::new( + ReshardingConfig::default(), + "resharding_config", ), } } @@ -427,14 +426,14 @@ pub trait RuntimeAdapter: Send + Sync { /// Returns false if the resulting part doesn't match the expected one. fn validate_state_part(&self, state_root: &StateRoot, part_id: PartId, data: &[u8]) -> bool; - fn apply_update_to_split_states( + fn apply_update_to_children_states( &self, block_hash: &CryptoHash, block_height: BlockHeight, state_roots: HashMap, next_shard_layout: &ShardLayout, - state_changes: StateChangesForSplitStates, - ) -> Result, Error>; + state_changes: StateChangesForResharding, + ) -> Result, Error>; /// Should be executed after accepting all the parts to set up a new state. fn apply_state_part( diff --git a/chain/chain/src/update_shard.rs b/chain/chain/src/update_shard.rs index f76e90debd2..a4aca5d4d60 100644 --- a/chain/chain/src/update_shard.rs +++ b/chain/chain/src/update_shard.rs @@ -1,8 +1,8 @@ use crate::crypto_hash_timer::CryptoHashTimer; use crate::types::{ - ApplySplitStateResult, ApplySplitStateResultOrStateChanges, ApplyTransactionResult, - ApplyTransactionsBlockContext, ApplyTransactionsChunkContext, RuntimeAdapter, - RuntimeStorageConfig, StorageDataSource, + ApplyResultForResharding, ApplyTransactionResult, ApplyTransactionsBlockContext, + ApplyTransactionsChunkContext, ReshardingResults, RuntimeAdapter, RuntimeStorageConfig, + StorageDataSource, }; use near_chain_primitives::Error; use near_epoch_manager::EpochManagerAdapter; @@ -12,7 +12,7 @@ use near_primitives::sandbox::state_patch::SandboxStatePatch; use near_primitives::shard_layout::ShardUId; use near_primitives::sharding::ShardChunk; use near_primitives::types::chunk_extra::ChunkExtra; -use near_primitives::types::{BlockHeight, Gas, StateChangesForSplitStates, StateRoot}; +use near_primitives::types::{BlockHeight, Gas, StateChangesForResharding, StateRoot}; use std::collections::HashMap; /// Result of updating a shard for some block when it has a new chunk for this @@ -22,7 +22,7 @@ pub struct NewChunkResult { pub(crate) shard_uid: ShardUId, pub(crate) gas_limit: Gas, pub(crate) apply_result: ApplyTransactionResult, - pub(crate) apply_split_result_or_state_changes: Option, + pub(crate) resharding_results: Option, } /// Result of updating a shard for some block when it doesn't have a new chunk @@ -33,16 +33,16 @@ pub struct OldChunkResult { /// Note that despite the naming, no transactions are applied in this case. /// TODO(logunov): exclude receipts/txs context from all related types. pub(crate) apply_result: ApplyTransactionResult, - pub(crate) apply_split_result_or_state_changes: Option, + pub(crate) resharding_results: Option, } -/// Result of updating a shard for some block when we apply only split state +/// Result of updating a shard for some block when we apply only resharding /// changes due to resharding. #[derive(Debug)] -pub struct StateSplitResult { - // parent shard of the split states +pub struct ReshardingResult { + // parent shard of the pub(crate) shard_uid: ShardUId, - pub(crate) results: Vec, + pub(crate) results: Vec, } /// Result of processing shard update, covering both stateful and stateless scenarios. @@ -61,16 +61,16 @@ pub enum ShardUpdateResult { pub enum ShardBlockUpdateResult { NewChunk(NewChunkResult), OldChunk(OldChunkResult), - StateSplit(StateSplitResult), + Resharding(ReshardingResult), } -/// State roots of split shards which are ready. -type SplitStateRoots = HashMap; +/// State roots of children shards which are ready. +type ReshardingStateRoots = HashMap; pub(crate) struct NewChunkData { pub chunk: ShardChunk, pub receipts: Vec, - pub split_state_roots: Option, + pub resharding_state_roots: Option, pub block: ApplyTransactionsBlockContext, pub is_first_block_with_chunk_of_version: bool, pub storage_context: StorageContext, @@ -78,20 +78,20 @@ pub(crate) struct NewChunkData { pub(crate) struct OldChunkData { pub prev_chunk_extra: ChunkExtra, - pub split_state_roots: Option, + pub resharding_state_roots: Option, pub block: ApplyTransactionsBlockContext, pub storage_context: StorageContext, } -pub(crate) struct StateSplitData { - pub split_state_roots: SplitStateRoots, - pub state_changes: StateChangesForSplitStates, +pub(crate) struct ReshardingData { + pub resharding_state_roots: ReshardingStateRoots, + pub state_changes: StateChangesForResharding, pub block_height: BlockHeight, pub block_hash: CryptoHash, } /// Reason to update a shard when new block appears on chain. -/// All types include state roots for split shards in case of resharding. +/// All types include state roots for children shards in case of resharding. pub(crate) enum ShardUpdateReason { /// Block has a new chunk for the shard. /// Contains chunk itself and all new incoming receipts to the shard. @@ -100,9 +100,9 @@ pub(crate) enum ShardUpdateReason { /// Instead, previous chunk header is copied. /// Contains result of shard update for previous block. OldChunk(OldChunkData), - /// See comment to `split_state_roots` in `Chain::get_update_shard_job`. + /// See comment to `resharding_state_roots` in `Chain::get_update_shard_job`. /// Process only state changes caused by resharding. - StateSplit(StateSplitData), + Resharding(ReshardingData), } /// Information about shard to update. @@ -115,7 +115,7 @@ pub(crate) struct ShardContext { /// Whether transactions should be applied. pub should_apply_transactions: bool, /// See comment in `get_update_shard_job`. - pub need_to_split_states: bool, + pub need_to_reshard: bool, } /// Information about storage used for applying txs and receipts. @@ -149,8 +149,8 @@ pub(crate) fn process_shard_update( runtime, epoch_manager, )?), - ShardUpdateReason::StateSplit(data) => ShardBlockUpdateResult::StateSplit( - apply_state_split(parent_span, data, shard_context.shard_uid, runtime, epoch_manager)?, + ShardUpdateReason::Resharding(data) => ShardBlockUpdateResult::Resharding( + apply_resharding(parent_span, data, shard_context.shard_uid, runtime, epoch_manager)?, ), }) } @@ -169,22 +169,21 @@ pub(crate) fn process_missing_chunks_range( ) -> Result, Error> { let mut result = vec![]; for (block_context, shard_context) in execution_contexts { - let OldChunkResult { shard_uid, apply_result, apply_split_result_or_state_changes: _ } = - apply_old_chunk( - parent_span, - OldChunkData { - block: block_context.clone(), - split_state_roots: None, - prev_chunk_extra: current_chunk_extra.clone(), - storage_context: StorageContext { - storage_data_source: StorageDataSource::DbTrieOnly, - state_patch: Default::default(), - }, + let OldChunkResult { shard_uid, apply_result, resharding_results: _ } = apply_old_chunk( + parent_span, + OldChunkData { + block: block_context.clone(), + resharding_state_roots: None, + prev_chunk_extra: current_chunk_extra.clone(), + storage_context: StorageContext { + storage_data_source: StorageDataSource::DbTrieOnly, + state_patch: Default::default(), }, - shard_context, - runtime, - epoch_manager, - )?; + }, + shard_context, + runtime, + epoch_manager, + )?; *current_chunk_extra.state_root_mut() = apply_result.new_root; result.push((block_context.block_hash, shard_uid, current_chunk_extra.clone())); } @@ -203,7 +202,7 @@ pub(crate) fn apply_new_chunk( block, chunk, receipts, - split_state_roots, + resharding_state_roots, is_first_block_with_chunk_of_version, storage_context, } = data; @@ -240,12 +239,12 @@ pub(crate) fn apply_new_chunk( ) { Ok(apply_result) => { let apply_split_result_or_state_changes = if shard_context.will_shard_layout_change { - Some(apply_split_state_changes( + Some(apply_resharding_state_changes( epoch_manager, runtime, block, &apply_result, - split_state_roots, + resharding_state_roots, )?) } else { None @@ -254,7 +253,7 @@ pub(crate) fn apply_new_chunk( gas_limit, shard_uid: shard_context.shard_uid, apply_result, - apply_split_result_or_state_changes, + resharding_results: apply_split_result_or_state_changes, }) } Err(err) => Err(err), @@ -271,7 +270,7 @@ fn apply_old_chunk( runtime: &dyn RuntimeAdapter, epoch_manager: &dyn EpochManagerAdapter, ) -> Result { - let OldChunkData { prev_chunk_extra, split_state_roots, block, storage_context } = data; + let OldChunkData { prev_chunk_extra, resharding_state_roots, block, storage_context } = data; let shard_id = shard_context.shard_uid.shard_id(); let _span = tracing::debug_span!( target: "chain", @@ -302,12 +301,12 @@ fn apply_old_chunk( ) { Ok(apply_result) => { let apply_split_result_or_state_changes = if shard_context.will_shard_layout_change { - Some(apply_split_state_changes( + Some(apply_resharding_state_changes( epoch_manager, runtime, block, &apply_result, - split_state_roots, + resharding_state_roots, )?) } else { None @@ -315,77 +314,76 @@ fn apply_old_chunk( Ok(OldChunkResult { shard_uid: shard_context.shard_uid, apply_result, - apply_split_result_or_state_changes, + resharding_results: apply_split_result_or_state_changes, }) } Err(err) => Err(err), } } -/// Applies only split state changes but not applies any transactions. -fn apply_state_split( +/// Applies only resharding changes but not applies any transactions. +fn apply_resharding( parent_span: &tracing::Span, - data: StateSplitData, + data: ReshardingData, shard_uid: ShardUId, runtime: &dyn RuntimeAdapter, epoch_manager: &dyn EpochManagerAdapter, -) -> Result { - let StateSplitData { split_state_roots, state_changes, block_height: height, block_hash } = +) -> Result { + let ReshardingData { resharding_state_roots, state_changes, block_height: height, block_hash } = data; let shard_id = shard_uid.shard_id(); let _span = tracing::debug_span!( target: "chain", parent: parent_span, - "split_state", + "resharding", shard_id, ?shard_uid) .entered(); let next_epoch_id = epoch_manager.get_next_epoch_id(&block_hash)?; let next_epoch_shard_layout = epoch_manager.get_shard_layout(&next_epoch_id)?; - let results = runtime.apply_update_to_split_states( + let results = runtime.apply_update_to_children_states( &block_hash, height, - split_state_roots, + resharding_state_roots, &next_epoch_shard_layout, state_changes, )?; - Ok(StateSplitResult { shard_uid, results }) + Ok(ReshardingResult { shard_uid, results }) } -/// Process ApplyTransactionResult to apply changes to split states -/// When shards will change next epoch, -/// if `split_state_roots` is not None, that means states for the split shards are ready -/// this function updates these states and return apply results for these states -/// otherwise, this function returns state changes needed to be applied to split -/// states. These state changes will be stored in the database by `process_split_state` -fn apply_split_state_changes( +/// Process ApplyTransactionResult to apply changes to children shards. When +/// shards will change next epoch, +/// - if `resharding_state_roots` is not None, that means states for the +/// children shards are ready this function updates these states and returns +/// apply results for these states +/// - otherwise, this function returns state changes needed to be applied to +/// children shards. These state changes will be stored in the database by +/// `process_resharding_results` +fn apply_resharding_state_changes( epoch_manager: &dyn EpochManagerAdapter, runtime_adapter: &dyn RuntimeAdapter, block: ApplyTransactionsBlockContext, apply_result: &ApplyTransactionResult, - split_state_roots: Option, -) -> Result { - let state_changes = StateChangesForSplitStates::from_raw_state_changes( + resharding_state_roots: Option, +) -> Result { + let state_changes = StateChangesForResharding::from_raw_state_changes( apply_result.trie_changes.state_changes(), apply_result.processed_delayed_receipts.clone(), ); - let next_epoch_shard_layout = { - let next_epoch_id = - epoch_manager.get_next_epoch_id_from_prev_block(&block.prev_block_hash)?; - epoch_manager.get_shard_layout(&next_epoch_id)? - }; - // split states are ready, apply update to them now - if let Some(state_roots) = split_state_roots { - let split_state_results = runtime_adapter.apply_update_to_split_states( + let next_epoch_id = epoch_manager.get_next_epoch_id_from_prev_block(&block.prev_block_hash)?; + let next_shard_layout = epoch_manager.get_shard_layout(&next_epoch_id)?; + if let Some(state_roots) = resharding_state_roots { + // children states are ready, apply update to them now + let resharding_results = runtime_adapter.apply_update_to_children_states( &block.block_hash, block.height, state_roots, - &next_epoch_shard_layout, + &next_shard_layout, state_changes, )?; - Ok(ApplySplitStateResultOrStateChanges::ApplySplitStateResults(split_state_results)) + Ok(ReshardingResults::ApplyReshardingResults(resharding_results)) } else { - // split states are not ready yet, store state changes in consolidated_state_changes - Ok(ApplySplitStateResultOrStateChanges::StateChangesForSplitStates(state_changes)) + // children states are not ready yet, store state changes in consolidated_state_changes + Ok(ReshardingResults::StoreReshardingResults(state_changes)) } } diff --git a/chain/client-primitives/src/types.rs b/chain/client-primitives/src/types.rs index 50ab3aa914a..b31fe297637 100644 --- a/chain/client-primitives/src/types.rs +++ b/chain/client-primitives/src/types.rs @@ -96,8 +96,8 @@ pub enum ShardSyncStatus { StateDownloadScheduling, StateDownloadApplying, StateDownloadComplete, - StateSplitScheduling, - StateSplitApplying, + ReshardingScheduling, + ReshardingApplying, StateSyncDone, } @@ -109,8 +109,8 @@ impl ShardSyncStatus { ShardSyncStatus::StateDownloadScheduling => 2, ShardSyncStatus::StateDownloadApplying => 3, ShardSyncStatus::StateDownloadComplete => 4, - ShardSyncStatus::StateSplitScheduling => 5, - ShardSyncStatus::StateSplitApplying => 6, + ShardSyncStatus::ReshardingScheduling => 5, + ShardSyncStatus::ReshardingApplying => 6, ShardSyncStatus::StateSyncDone => 7, } } @@ -133,8 +133,8 @@ impl ToString for ShardSyncStatus { ShardSyncStatus::StateDownloadScheduling => "scheduling".to_string(), ShardSyncStatus::StateDownloadApplying => "applying".to_string(), ShardSyncStatus::StateDownloadComplete => "download complete".to_string(), - ShardSyncStatus::StateSplitScheduling => "split scheduling".to_string(), - ShardSyncStatus::StateSplitApplying => "split applying".to_string(), + ShardSyncStatus::ReshardingScheduling => "resharding scheduling".to_string(), + ShardSyncStatus::ReshardingApplying => "resharding applying".to_string(), ShardSyncStatus::StateSyncDone => "done".to_string(), } } diff --git a/chain/client/src/client.rs b/chain/client/src/client.rs index f313e4f6c20..ce601eba66d 100644 --- a/chain/client/src/client.rs +++ b/chain/client/src/client.rs @@ -26,7 +26,7 @@ use near_chain::chain::{ }; use near_chain::flat_storage_creator::FlatStorageCreator; use near_chain::orphan::OrphanMissingChunks; -use near_chain::resharding::StateSplitRequest; +use near_chain::resharding::ReshardingRequest; use near_chain::state_snapshot_actor::SnapshotCallbacks; use near_chain::test_utils::format_hash; use near_chain::types::RuntimeAdapter; @@ -193,7 +193,7 @@ pub struct Client { impl Client { pub(crate) fn update_client_config(&self, update_client_config: UpdateableClientConfig) { self.config.expected_shutdown.update(update_client_config.expected_shutdown); - self.config.state_split_config.update(update_client_config.state_split_config); + self.config.resharding_config.update(update_client_config.resharding_config); self.config .produce_chunk_add_transactions_time_limit .update(update_client_config.produce_chunk_add_transactions_time_limit); @@ -247,7 +247,7 @@ impl Client { let chain_config = ChainConfig { save_trie_changes: config.save_trie_changes, background_migration_threads: config.client_background_migration_threads, - state_split_config: config.state_split_config.clone(), + resharding_config: config.resharding_config.clone(), }; let chain = Chain::new( epoch_manager.clone(), @@ -2276,7 +2276,7 @@ impl Client { highest_height_peers: &[HighestHeightPeerInfo], state_parts_task_scheduler: &dyn Fn(ApplyStatePartsRequest), block_catch_up_task_scheduler: &dyn Fn(BlockCatchUpRequest), - state_split_scheduler: &dyn Fn(StateSplitRequest), + resharding_scheduler: &dyn Fn(ReshardingRequest), apply_chunks_done_callback: DoneApplyChunkCallback, state_parts_arbiter_handle: &ArbiterHandle, ) -> Result<(), Error> { @@ -2348,7 +2348,7 @@ impl Client { highest_height_peers, tracking_shards, state_parts_task_scheduler, - state_split_scheduler, + resharding_scheduler, state_parts_arbiter_handle, use_colour, self.runtime_adapter.clone(), @@ -2399,10 +2399,10 @@ impl Client { me: &Option, ) -> Result, Error> { let prev_hash = *self.chain.get_block(&sync_hash)?.header().prev_hash(); - let need_to_split_states = self.epoch_manager.will_shard_layout_change(&prev_hash)?; + let need_to_reshard = self.epoch_manager.will_shard_layout_change(&prev_hash)?; - if !need_to_split_states { - debug!(target: "catchup", "do not need to split states for shards"); + if !need_to_reshard { + debug!(target: "catchup", "do not need to reshard"); return Ok(HashMap::new()); } @@ -2427,7 +2427,7 @@ impl Client { if self.shard_tracker.care_about_shard(me.as_ref(), &prev_hash, shard_id, true) { let shard_sync_download = ShardSyncDownload { downloads: vec![], - status: ShardSyncStatus::StateSplitScheduling, + status: ShardSyncStatus::ReshardingScheduling, }; Some((shard_id, shard_sync_download)) } else { diff --git a/chain/client/src/client_actor.rs b/chain/client/src/client_actor.rs index d89b57ab310..cbb2e5118f3 100644 --- a/chain/client/src/client_actor.rs +++ b/chain/client/src/client_actor.rs @@ -27,7 +27,7 @@ use near_async::messaging::{CanSend, Sender}; use near_chain::chain::{ ApplyStatePartsRequest, ApplyStatePartsResponse, BlockCatchUpRequest, BlockCatchUpResponse, }; -use near_chain::resharding::{StateSplitRequest, StateSplitResponse}; +use near_chain::resharding::{ReshardingRequest, ReshardingResponse}; use near_chain::state_snapshot_actor::SnapshotCallbacks; use near_chain::test_utils::format_hash; use near_chain::types::RuntimeAdapter; @@ -37,7 +37,7 @@ use near_chain::{ byzantine_assert, near_chain_primitives, Block, BlockHeader, BlockProcessingArtifact, ChainGenesis, DoneApplyChunkCallback, Provenance, }; -use near_chain_configs::{ClientConfig, LogSummaryStyle, StateSplitHandle}; +use near_chain_configs::{ClientConfig, LogSummaryStyle, ReshardingHandle}; use near_chain_primitives::error::EpochErrorResultToChainError; use near_chunks::adapter::ShardsManagerRequestFromClient; use near_chunks::client::ShardsManagerResponse; @@ -116,7 +116,7 @@ pub struct ClientActor { sync_started: bool, state_parts_task_scheduler: Box, block_catch_up_scheduler: Box, - state_split_scheduler: Box, + resharding_scheduler: Box, state_parts_client_arbiter: Arbiter, #[cfg(feature = "sandbox")] @@ -212,7 +212,7 @@ impl ClientActor { block_catch_up_scheduler: create_sync_job_scheduler::( sync_jobs_actor_addr.clone(), ), - state_split_scheduler: create_sync_job_scheduler::( + resharding_scheduler: create_sync_job_scheduler::( sync_jobs_actor_addr, ), state_parts_client_arbiter: state_parts_arbiter, @@ -1514,7 +1514,7 @@ impl ClientActor { &self.network_info.highest_height_peers, &self.state_parts_task_scheduler, &self.block_catch_up_scheduler, - &self.state_split_scheduler, + &self.resharding_scheduler, self.get_apply_chunks_done_callback(), &self.state_parts_client_arbiter.handle(), ) { @@ -1748,7 +1748,7 @@ impl ClientActor { &self.network_info.highest_height_peers, shards_to_sync, &self.state_parts_task_scheduler, - &self.state_split_scheduler, + &self.resharding_scheduler, &self.state_parts_client_arbiter.handle(), use_colour, self.client.runtime_adapter.clone(), @@ -1912,22 +1912,22 @@ impl Handler> for ClientActor { } } -impl Handler> for ClientActor { +impl Handler> for ClientActor { type Result = (); #[perf] fn handle( &mut self, - msg: WithSpanContext, + msg: WithSpanContext, _: &mut Self::Context, ) -> Self::Result { let (_span, msg) = handler_debug_span!(target: "client", msg); tracing::debug!(target: "client", ?msg); if let Some((sync, _, _)) = self.client.catchup_state_syncs.get_mut(&msg.sync_hash) { // We are doing catchup - sync.set_split_result(msg.shard_id, msg.new_state_roots); + sync.set_resharding_result(msg.shard_id, msg.new_state_roots); } else { - self.client.state_sync.set_split_result(msg.shard_id, msg.new_state_roots); + self.client.state_sync.set_resharding_result(msg.shard_id, msg.new_state_roots); } } } @@ -2031,7 +2031,7 @@ pub fn start_client( sender: Option>, adv: crate::adversarial::Controls, config_updater: Option, -) -> (Addr, ArbiterHandle, StateSplitHandle) { +) -> (Addr, ArbiterHandle, ReshardingHandle) { let client_arbiter = Arbiter::new(); let client_arbiter_handle = client_arbiter.handle(); @@ -2051,7 +2051,7 @@ pub fn start_client( snapshot_callbacks, ) .unwrap(); - let state_split_handle = client.chain.state_split_handle.clone(); + let resharding_handle = client.chain.resharding_handle.clone(); let client_addr = ClientActor::start_in_arbiter(&client_arbiter_handle, move |ctx| { ClientActor::new( client, @@ -2068,5 +2068,5 @@ pub fn start_client( ) .unwrap() }); - (client_addr, client_arbiter_handle, state_split_handle) + (client_addr, client_arbiter_handle, resharding_handle) } diff --git a/chain/client/src/sync/state.rs b/chain/client/src/sync/state.rs index 33cec97c81c..4fab3657432 100644 --- a/chain/client/src/sync/state.rs +++ b/chain/client/src/sync/state.rs @@ -30,7 +30,7 @@ use futures::{future, FutureExt}; use near_async::messaging::CanSendAsync; use near_chain::chain::ApplyStatePartsRequest; use near_chain::near_chain_primitives; -use near_chain::resharding::StateSplitRequest; +use near_chain::resharding::ReshardingRequest; use near_chain::types::RuntimeAdapter; use near_chain::Chain; use near_chain_configs::{ExternalStorageConfig, ExternalStorageLocation, SyncConfig}; @@ -140,7 +140,8 @@ pub struct StateSync { state_parts_apply_results: HashMap>, /// Maps shard_id to result of splitting state for resharding. - split_state_roots: HashMap, near_chain::Error>>, + resharding_state_roots: + HashMap, near_chain::Error>>, /// Message queue to process the received state parts. state_parts_mpsc_tx: Sender, @@ -201,7 +202,7 @@ impl StateSync { network_adapter, timeout, state_parts_apply_results: HashMap::new(), - split_state_roots: HashMap::new(), + resharding_state_roots: HashMap::new(), state_parts_mpsc_rx: rx, state_parts_mpsc_tx: tx, } @@ -220,7 +221,7 @@ impl StateSync { tracking_shards: Vec, now: DateTime, state_parts_task_scheduler: &dyn Fn(ApplyStatePartsRequest), - state_split_scheduler: &dyn Fn(StateSplitRequest), + resharding_scheduler: &dyn Fn(ReshardingRequest), state_parts_arbiter_handle: &ArbiterHandle, use_colour: bool, runtime_adapter: Arc, @@ -237,7 +238,7 @@ impl StateSync { // correct reason. When changing it please also update the tests. panic!("cannot sync to the first epoch after sharding upgrade. Please wait for the next epoch or find peers that are more up to date"); } - let split_states = epoch_manager.will_shard_layout_change(&prev_hash)?; + let need_to_reshard = epoch_manager.will_shard_layout_change(&prev_hash)?; for shard_id in tracking_shards { let version = prev_shard_layout.version(); @@ -288,22 +289,22 @@ impl StateSync { } ShardSyncStatus::StateDownloadComplete => { shard_sync_done = self - .sync_shards_download_complete_status(split_states, shard_sync_download); + .sync_shards_download_complete_status(need_to_reshard, shard_sync_download); } - ShardSyncStatus::StateSplitScheduling => { - debug_assert!(split_states); - self.sync_shards_state_split_scheduling_status( + ShardSyncStatus::ReshardingScheduling => { + debug_assert!(need_to_reshard); + self.sync_shards_resharding_scheduling_status( shard_id, shard_sync_download, sync_hash, chain, - state_split_scheduler, + resharding_scheduler, me, )?; } - ShardSyncStatus::StateSplitApplying => { - debug_assert!(split_states); - shard_sync_done = self.sync_shards_state_split_applying_status( + ShardSyncStatus::ReshardingApplying => { + debug_assert!(need_to_reshard); + shard_sync_done = self.sync_shards_resharding_applying_status( shard_uid, shard_sync_download, sync_hash, @@ -404,13 +405,13 @@ impl StateSync { self.state_parts_apply_results.insert(shard_id, apply_result); } - // Called by the client actor, when it finished splitting the state. - pub fn set_split_result( + // Called by the client actor, when it finished resharding. + pub fn set_resharding_result( &mut self, shard_id: ShardId, result: Result, near_chain::Error>, ) { - self.split_state_roots.insert(shard_id, result); + self.resharding_state_roots.insert(shard_id, result); } /// Find the hash of the first block on the same epoch (and chain) of block with hash `sync_hash`. @@ -665,7 +666,7 @@ impl StateSync { // Shards to sync. tracking_shards: Vec, state_parts_task_scheduler: &dyn Fn(ApplyStatePartsRequest), - state_split_scheduler: &dyn Fn(StateSplitRequest), + resharding_scheduler: &dyn Fn(ReshardingRequest), state_parts_arbiter_handle: &ArbiterHandle, use_colour: bool, runtime_adapter: Arc, @@ -694,7 +695,7 @@ impl StateSync { tracking_shards, now, state_parts_task_scheduler, - state_split_scheduler, + resharding_scheduler, state_parts_arbiter_handle, use_colour, runtime_adapter, @@ -956,14 +957,14 @@ impl StateSync { fn sync_shards_download_complete_status( &mut self, - split_states: bool, + need_to_reshard: bool, shard_sync_download: &mut ShardSyncDownload, ) -> bool { // If the shard layout is changing in this epoch - we have to apply it right now. - if split_states { + if need_to_reshard { *shard_sync_download = ShardSyncDownload { downloads: vec![], - status: ShardSyncStatus::StateSplitScheduling, + status: ShardSyncStatus::ReshardingScheduling, }; false } else { @@ -974,35 +975,35 @@ impl StateSync { } } - fn sync_shards_state_split_scheduling_status( + fn sync_shards_resharding_scheduling_status( &mut self, shard_id: ShardId, shard_sync_download: &mut ShardSyncDownload, sync_hash: CryptoHash, chain: &Chain, - state_split_scheduler: &dyn Fn(StateSplitRequest), + resharding_scheduler: &dyn Fn(ReshardingRequest), me: &Option, ) -> Result<(), near_chain::Error> { - chain.build_state_for_split_shards_preprocessing( + chain.build_state_for_resharding_preprocessing( &sync_hash, shard_id, - state_split_scheduler, + resharding_scheduler, )?; tracing::debug!(target: "sync", %shard_id, %sync_hash, ?me, "resharding scheduled"); *shard_sync_download = - ShardSyncDownload { downloads: vec![], status: ShardSyncStatus::StateSplitApplying }; + ShardSyncDownload { downloads: vec![], status: ShardSyncStatus::ReshardingApplying }; Ok(()) } /// Returns whether the State Sync for the given shard is complete. - fn sync_shards_state_split_applying_status( + fn sync_shards_resharding_applying_status( &mut self, shard_uid: ShardUId, shard_sync_download: &mut ShardSyncDownload, sync_hash: CryptoHash, chain: &mut Chain, ) -> Result { - let result = self.split_state_roots.remove(&shard_uid.shard_id()); + let result = self.resharding_state_roots.remove(&shard_uid.shard_id()); let mut shard_sync_done = false; if let Some(state_roots) = result { chain.build_state_for_split_shards_postprocessing( @@ -1317,7 +1318,7 @@ mod test { }; let apply_parts_fn = move |_: ApplyStatePartsRequest| {}; - let state_split_fn = move |_: StateSplitRequest| {}; + let resharding_fn = move |_: ReshardingRequest| {}; let secret_key = SecretKey::from_random(near_crypto::KeyType::ED25519); let public_key = secret_key.public_key(); @@ -1342,7 +1343,7 @@ mod test { &[highest_height_peer_info], vec![0], &apply_parts_fn, - &state_split_fn, + &resharding_fn, &Arbiter::new().handle(), false, runtime, diff --git a/chain/client/src/sync_jobs_actor.rs b/chain/client/src/sync_jobs_actor.rs index e745f112f7b..0f5c1394146 100644 --- a/chain/client/src/sync_jobs_actor.rs +++ b/chain/client/src/sync_jobs_actor.rs @@ -6,7 +6,7 @@ use near_chain::chain::{ do_apply_chunks, ApplyStatePartsRequest, ApplyStatePartsResponse, BlockCatchUpRequest, BlockCatchUpResponse, }; -use near_chain::resharding::StateSplitRequest; +use near_chain::resharding::ReshardingRequest; use near_chain::Chain; use near_o11y::{handler_debug_span, OpenTelemetrySpanExt, WithSpanContext, WithSpanContextExt}; use near_performance_metrics_macros::perf; @@ -147,39 +147,39 @@ impl actix::Handler> for SyncJobsActor { } } -impl actix::Handler> for SyncJobsActor { +impl actix::Handler> for SyncJobsActor { type Result = (); #[perf] fn handle( &mut self, - msg: WithSpanContext, + msg: WithSpanContext, context: &mut Self::Context, ) -> Self::Result { - let (_span, mut state_split_request) = handler_debug_span!(target: "resharding", msg); - let config = state_split_request.config.get(); + let (_span, mut resharding_request) = handler_debug_span!(target: "resharding", msg); + let config = resharding_request.config.get(); // Wait for the initial delay. It should only be used in tests. let initial_delay = config.initial_delay; - if state_split_request.curr_poll_time == Duration::ZERO && initial_delay > Duration::ZERO { - tracing::debug!(target: "resharding", ?state_split_request, ?initial_delay, "Waiting for the initial delay"); - state_split_request.curr_poll_time += initial_delay; - context.notify_later(state_split_request.with_span_context(), initial_delay); + if resharding_request.curr_poll_time == Duration::ZERO && initial_delay > Duration::ZERO { + tracing::debug!(target: "resharding", ?resharding_request, ?initial_delay, "Waiting for the initial delay"); + resharding_request.curr_poll_time += initial_delay; + context.notify_later(resharding_request.with_span_context(), initial_delay); return; } - if Chain::retry_build_state_for_split_shards(&state_split_request) { + if Chain::retry_build_state_for_split_shards(&resharding_request) { // Actix implementation let's us send message to ourselves with a delay. // In case snapshots are not ready yet, we will retry resharding later. let retry_delay = config.retry_delay; - tracing::debug!(target: "resharding", ?state_split_request, ?retry_delay, "Snapshot missing, retrying resharding later"); - state_split_request.curr_poll_time += retry_delay; - context.notify_later(state_split_request.with_span_context(), retry_delay); + tracing::debug!(target: "resharding", ?resharding_request, ?retry_delay, "Snapshot missing, retrying resharding later"); + resharding_request.curr_poll_time += retry_delay; + context.notify_later(resharding_request.with_span_context(), retry_delay); return; } - tracing::debug!(target: "resharding", ?state_split_request, "Starting resharding"); - let response = Chain::build_state_for_split_shards(state_split_request); + tracing::debug!(target: "resharding", ?resharding_request, "Starting resharding"); + let response = Chain::build_state_for_split_shards(resharding_request); self.client_addr.do_send(response.with_span_context()); } } diff --git a/chain/client/src/test_utils/client.rs b/chain/client/src/test_utils/client.rs index b7a6bc7f7c1..2b5e47244bb 100644 --- a/chain/client/src/test_utils/client.rs +++ b/chain/client/src/test_utils/client.rs @@ -9,7 +9,7 @@ use crate::Client; use actix_rt::{Arbiter, System}; use itertools::Itertools; use near_chain::chain::{do_apply_chunks, BlockCatchUpRequest}; -use near_chain::resharding::StateSplitRequest; +use near_chain::resharding::ReshardingRequest; use near_chain::test_utils::{wait_for_all_blocks_in_processing, wait_for_block_in_processing}; use near_chain::{Chain, ChainStoreAccess, Provenance}; use near_client_primitives::types::Error; @@ -226,10 +226,10 @@ pub fn run_catchup( let block_catch_up = move |msg: BlockCatchUpRequest| { block_inside_messages.write().unwrap().push(msg); }; - let state_split_messages = Arc::new(RwLock::new(vec![])); - let state_split_inside_messages = state_split_messages.clone(); - let state_split = move |msg: StateSplitRequest| { - state_split_inside_messages.write().unwrap().push(msg); + let resharding_messages = Arc::new(RwLock::new(vec![])); + let resharding_inside_messages = resharding_messages.clone(); + let resharding = move |msg: ReshardingRequest| { + resharding_inside_messages.write().unwrap().push(msg); }; let _ = System::new(); let state_parts_arbiter_handle = Arbiter::new().handle(); @@ -238,7 +238,7 @@ pub fn run_catchup( highest_height_peers, &f, &block_catch_up, - &state_split, + &resharding, Arc::new(|_| {}), &state_parts_arbiter_handle, )?; @@ -258,13 +258,15 @@ pub fn run_catchup( } catchup_done = false; } - for msg in state_split_messages.write().unwrap().drain(..) { + for msg in resharding_messages.write().unwrap().drain(..) { let response = Chain::build_state_for_split_shards(msg); if let Some((sync, _, _)) = client.catchup_state_syncs.get_mut(&response.sync_hash) { // We are doing catchup - sync.set_split_result(response.shard_id, response.new_state_roots); + sync.set_resharding_result(response.shard_id, response.new_state_roots); } else { - client.state_sync.set_split_result(response.shard_id, response.new_state_roots); + client + .state_sync + .set_resharding_result(response.shard_id, response.new_state_roots); } catchup_done = false; } diff --git a/chain/client/src/test_utils/setup.rs b/chain/client/src/test_utils/setup.rs index d65752466ea..e081e0a0485 100644 --- a/chain/client/src/test_utils/setup.rs +++ b/chain/client/src/test_utils/setup.rs @@ -21,7 +21,7 @@ use near_chain::state_snapshot_actor::SnapshotCallbacks; use near_chain::test_utils::{KeyValueRuntime, MockEpochManager, ValidatorSchedule}; use near_chain::types::{ChainConfig, RuntimeAdapter}; use near_chain::{Chain, ChainGenesis, DoomslugThresholdMode}; -use near_chain_configs::{ClientConfig, MutableConfigValue, StateSplitConfig}; +use near_chain_configs::{ClientConfig, MutableConfigValue, ReshardingConfig}; use near_chunks::adapter::ShardsManagerRequestFromClient; use near_chunks::client::ShardsManagerResponse; use near_chunks::shards_manager_actor::start_shards_manager; @@ -114,9 +114,9 @@ pub fn setup( ChainConfig { save_trie_changes: true, background_migration_threads: 1, - state_split_config: MutableConfigValue::new( - StateSplitConfig::default(), - "state_split_config", + resharding_config: MutableConfigValue::new( + ReshardingConfig::default(), + "resharding_config", ), }, None, @@ -242,9 +242,9 @@ pub fn setup_only_view( ChainConfig { save_trie_changes: true, background_migration_threads: 1, - state_split_config: MutableConfigValue::new( - StateSplitConfig::default(), - "state_split_config", + resharding_config: MutableConfigValue::new( + ReshardingConfig::default(), + "resharding_config", ), }, None, @@ -1023,9 +1023,9 @@ pub fn setup_synchronous_shards_manager( ChainConfig { save_trie_changes: true, background_migration_threads: 1, - state_split_config: MutableConfigValue::new( - StateSplitConfig::default(), - "state_split_config", + resharding_config: MutableConfigValue::new( + ReshardingConfig::default(), + "resharding_config", ), }, // irrelevant None, diff --git a/chain/epoch-manager/src/lib.rs b/chain/epoch-manager/src/lib.rs index 18380cb4d22..6de6e74850f 100644 --- a/chain/epoch-manager/src/lib.rs +++ b/chain/epoch-manager/src/lib.rs @@ -1088,7 +1088,7 @@ impl EpochManager { if self.will_shard_layout_change(parent_hash)? { let shard_layout = self.get_shard_layout(&next_epoch_id)?; let split_shards = shard_layout - .get_split_shard_ids(shard_id) + .get_children_shards_ids(shard_id) .expect("all shard layouts expect the first one must have a split map"); for next_shard_id in split_shards { if self.cares_about_shard_in_epoch( diff --git a/core/chain-configs/src/client_config.rs b/core/chain-configs/src/client_config.rs index a314a8be534..631136089e5 100644 --- a/core/chain-configs/src/client_config.rs +++ b/core/chain-configs/src/client_config.rs @@ -154,11 +154,11 @@ impl SyncConfig { // A handle that allows the main process to interrupt resharding if needed. // This typically happens when the main process is interrupted. #[derive(Clone)] -pub struct StateSplitHandle { +pub struct ReshardingHandle { keep_going: Arc, } -impl StateSplitHandle { +impl ReshardingHandle { pub fn new() -> Self { Self { keep_going: Arc::new(AtomicBool::new(true)) } } @@ -175,7 +175,7 @@ impl StateSplitHandle { /// Configuration for resharding. #[derive(serde::Serialize, serde::Deserialize, Clone, Copy, Debug, PartialEq)] #[serde(default)] -pub struct StateSplitConfig { +pub struct ReshardingConfig { /// The soft limit on the size of a single batch. The batch size can be /// decreased if resharding is consuming too many resources and interfering /// with regular node operation. @@ -200,7 +200,7 @@ pub struct StateSplitConfig { pub max_poll_time: Duration, } -impl Default for StateSplitConfig { +impl Default for ReshardingConfig { fn default() -> Self { // Conservative default for a slower resharding that puts as little // extra load on the node as possible. @@ -410,7 +410,7 @@ pub struct ClientConfig { // Allows more detailed logging, for example a list of orphaned blocks. pub enable_multiline_logging: bool, // Configuration for resharding. - pub state_split_config: MutableConfigValue, + pub resharding_config: MutableConfigValue, /// If the node is not a chunk producer within that many blocks, then route /// to upcoming chunk producers. pub tx_routing_height_horizon: BlockHeightDelta, @@ -493,9 +493,9 @@ impl ClientConfig { state_sync: StateSyncConfig::default(), transaction_pool_size_limit: None, enable_multiline_logging: false, - state_split_config: MutableConfigValue::new( - StateSplitConfig::default(), - "state_split_config", + resharding_config: MutableConfigValue::new( + ReshardingConfig::default(), + "resharding_config", ), tx_routing_height_horizon: 4, produce_chunk_add_transactions_time_limit: MutableConfigValue::new( diff --git a/core/chain-configs/src/lib.rs b/core/chain-configs/src/lib.rs index 379bdd6751f..4e8754cf895 100644 --- a/core/chain-configs/src/lib.rs +++ b/core/chain-configs/src/lib.rs @@ -15,7 +15,7 @@ pub use client_config::{ default_transaction_pool_size_limit, default_trie_viewer_state_size_limit, default_tx_routing_height_horizon, default_view_client_threads, default_view_client_throttle_period, ClientConfig, DumpConfig, ExternalStorageConfig, - ExternalStorageLocation, GCConfig, LogSummaryStyle, StateSplitConfig, StateSplitHandle, + ExternalStorageLocation, GCConfig, LogSummaryStyle, ReshardingConfig, ReshardingHandle, StateSyncConfig, SyncConfig, DEFAULT_GC_NUM_EPOCHS_TO_KEEP, DEFAULT_STATE_SYNC_NUM_CONCURRENT_REQUESTS_EXTERNAL, DEFAULT_STATE_SYNC_NUM_CONCURRENT_REQUESTS_ON_CATCHUP_EXTERNAL, MIN_GC_NUM_EPOCHS_TO_KEEP, diff --git a/core/chain-configs/src/updateable_config.rs b/core/chain-configs/src/updateable_config.rs index 9ab439a8c45..2814d651185 100644 --- a/core/chain-configs/src/updateable_config.rs +++ b/core/chain-configs/src/updateable_config.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize, Serializer}; use std::sync::{Arc, Mutex}; use std::{fmt::Debug, time::Duration}; -use crate::StateSplitConfig; +use crate::ReshardingConfig; /// A wrapper for a config value that can be updated while the node is running. /// When initializing sub-objects (e.g. `ShardsManager`), please make sure to @@ -92,7 +92,7 @@ pub struct UpdateableClientConfig { pub expected_shutdown: Option, // Configuration for resharding. - pub state_split_config: StateSplitConfig, + pub resharding_config: ReshardingConfig, /// Time limit for adding transactions in produce_chunk() pub produce_chunk_add_transactions_time_limit: Option, diff --git a/core/primitives/src/shard_layout.rs b/core/primitives/src/shard_layout.rs index 34b9497e55d..3b5129d81d2 100644 --- a/core/primitives/src/shard_layout.rs +++ b/core/primitives/src/shard_layout.rs @@ -24,7 +24,7 @@ use std::{fmt, str}; /// `version` and `shard_id` /// /// `get_parent_shard_id` and `get_split_shard_ids` -/// `ShardLayout` also includes information needed for splitting shards. In particular, it encodes +/// `ShardLayout` also includes information needed for resharding. In particular, it encodes /// which shards from the previous shard layout split to which shards in the following shard layout. /// If shard A in shard layout 0 splits to shard B and C in shard layout 1, /// we call shard A the parent shard of shard B and C. @@ -156,7 +156,6 @@ impl ShardLayout { } /// Returns the simple nightshade layout, version 2, that will be used in production. - /// This is work in progress and the exact way of splitting is yet to be determined. pub fn get_simple_nightshade_layout_v2() -> ShardLayout { ShardLayout::v1( vec!["aurora", "aurora-0", "kkuuue2akv_1630967379.near", "tge-lockup.sweat"] @@ -170,15 +169,15 @@ impl ShardLayout { /// Given a parent shard id, return the shard uids for the shards in the current shard layout that /// are split from this parent shard. If this shard layout has no parent shard layout, return None - pub fn get_split_shard_uids(&self, parent_shard_id: ShardId) -> Option> { - self.get_split_shard_ids(parent_shard_id).map(|shards| { + pub fn get_children_shards_uids(&self, parent_shard_id: ShardId) -> Option> { + self.get_children_shards_ids(parent_shard_id).map(|shards| { shards.into_iter().map(|id| ShardUId::from_shard_id_and_layout(id, self)).collect() }) } /// Given a parent shard id, return the shard ids for the shards in the current shard layout that /// are split from this parent shard. If this shard layout has no parent shard layout, return None - pub fn get_split_shard_ids(&self, parent_shard_id: ShardId) -> Option> { + pub fn get_children_shards_ids(&self, parent_shard_id: ShardId) -> Option> { match self { Self::V0(_) => None, Self::V1(v1) => match &v1.shards_split_map { @@ -507,11 +506,11 @@ mod tests { 1, ); assert_eq!( - shard_layout.get_split_shard_uids(0).unwrap(), + shard_layout.get_children_shards_uids(0).unwrap(), (0..3).map(|x| ShardUId { version: 1, shard_id: x }).collect::>() ); assert_eq!( - shard_layout.get_split_shard_uids(1).unwrap(), + shard_layout.get_children_shards_uids(1).unwrap(), (3..6).map(|x| ShardUId { version: 1, shard_id: x }).collect::>() ); for x in 0..3 { diff --git a/core/primitives/src/types.rs b/core/primitives/src/types.rs index ed67af10323..b92d38d674e 100644 --- a/core/primitives/src/types.rs +++ b/core/primitives/src/types.rs @@ -212,7 +212,7 @@ pub struct ConsolidatedStateChange { } #[derive(Debug, Clone, BorshSerialize, BorshDeserialize)] -pub struct StateChangesForSplitStates { +pub struct StateChangesForResharding { pub changes: Vec, // we need to store deleted receipts here because StateChanges will only include // trie keys for removed values and account information can not be inferred from @@ -220,7 +220,7 @@ pub struct StateChangesForSplitStates { pub processed_delayed_receipts: Vec, } -impl StateChangesForSplitStates { +impl StateChangesForResharding { pub fn from_raw_state_changes( changes: &[RawStateChangesWithTrieKey], processed_delayed_receipts: Vec, diff --git a/core/store/src/columns.rs b/core/store/src/columns.rs index 650bf0d43f9..6a2ea17b3b6 100644 --- a/core/store/src/columns.rs +++ b/core/store/src/columns.rs @@ -247,9 +247,11 @@ pub enum DBCol { /// - *Rows*: height (u64) /// - *Column type*: Vec HeaderHashesByHeight, - /// State changes made by a chunk, used for splitting states + /// State changes made by a chunk, used for resharding. Historically + /// resharding was also called State Splitting since the name. + /// TODO(resharding) rename to StateChangesForResharding if safe. /// - *Rows*: BlockShardId (BlockHash || ShardId) - 40 bytes - /// - *Column type*: StateChangesForSplitStates + /// - *Column type*: StateChangesForResharding StateChangesForSplitStates, /// Transaction or receipt outcome, by outcome ID (transaction or receipt hash) and block /// hash. Multiple outcomes may be stored for the same outcome ID in case of forks. diff --git a/core/store/src/lib.rs b/core/store/src/lib.rs index 07f37ce7e00..9f1090f8b50 100644 --- a/core/store/src/lib.rs +++ b/core/store/src/lib.rs @@ -33,7 +33,7 @@ use crate::db::{refcount, DBIterator, DBOp, DBSlice, DBTransaction, Database, St pub use crate::trie::iterator::{TrieIterator, TrieTraversalItem}; pub use crate::trie::update::{TrieUpdate, TrieUpdateIterator, TrieUpdateValuePtr}; pub use crate::trie::{ - estimator, split_state, ApplyStatePartResult, KeyForStateChanges, KeyLookupMode, NibbleSlice, + estimator, resharding, ApplyStatePartResult, KeyForStateChanges, KeyLookupMode, NibbleSlice, PartialStorage, PrefetchApi, PrefetchError, RawTrieNode, RawTrieNodeWithSize, ShardTries, StateSnapshot, StateSnapshotConfig, Trie, TrieAccess, TrieCache, TrieCachingStorage, TrieChanges, TrieConfig, TrieDBStorage, TrieStorage, WrappedTrieChanges, diff --git a/core/store/src/trie/mod.rs b/core/store/src/trie/mod.rs index f5e38cc3af0..1afe9e80575 100644 --- a/core/store/src/trie/mod.rs +++ b/core/store/src/trie/mod.rs @@ -47,8 +47,8 @@ pub mod mem; mod nibble_slice; mod prefetching_trie_storage; mod raw_node; +pub mod resharding; mod shard_tries; -pub mod split_state; mod state_parts; mod state_snapshot; mod trie_recording; diff --git a/core/store/src/trie/split_state.rs b/core/store/src/trie/resharding.rs similarity index 94% rename from core/store/src/trie/split_state.rs rename to core/store/src/trie/resharding.rs index a826d5307dd..ce0834c96fb 100644 --- a/core/store/src/trie/split_state.rs +++ b/core/store/src/trie/resharding.rs @@ -10,7 +10,7 @@ use near_primitives::state_part::PartId; use near_primitives::trie_key::trie_key_parsers::parse_account_id_from_raw_key; use near_primitives::trie_key::TrieKey; use near_primitives::types::{ - ConsolidatedStateChange, StateChangeCause, StateChangesForSplitStates, StateRoot, + ConsolidatedStateChange, StateChangeCause, StateChangesForResharding, StateRoot, }; use std::collections::HashMap; @@ -26,16 +26,16 @@ impl Trie { } impl ShardTries { - /// applies `changes` to split states - /// and returns the generated TrieUpdate for all split states - /// Note that this function is different from the function `add_values_to_split_states` - /// This function is used for applying updates to split states when processing blocks - /// `add_values_to_split_states` are used to generate the initial states for shards split + /// applies `changes` to children states during resharding + /// and returns the generated TrieUpdate for all children states + /// Note that this function is different from the function `add_values_to_children_states` + /// This function is used for applying updates to children states when processing blocks + /// `add_values_to_children_states` are used to generate the initial states for states split /// from the original parent shard. - pub fn apply_state_changes_to_split_states( + pub fn apply_state_changes_to_children_states( &self, state_roots: &HashMap, - changes: StateChangesForSplitStates, + changes: StateChangesForResharding, account_id_to_shard_uid: &dyn Fn(&AccountId) -> ShardUId, ) -> Result, StorageError> { let mut trie_updates: HashMap<_, _> = self.get_trie_updates(state_roots); @@ -76,7 +76,7 @@ impl ShardTries { } } for (_, update) in trie_updates.iter_mut() { - // StateChangeCause should always be Resharding for processing split state. + // StateChangeCause should always be Resharding for processing resharding. // We do not want to commit the state_changes from resharding as they are already handled while // processing parent shard update.commit(StateChangeCause::Resharding); @@ -87,7 +87,7 @@ impl ShardTries { let insert_receipts: Vec<_> = insert_receipts.into_iter().map(|(_, receipt)| receipt).collect(); - apply_delayed_receipts_to_split_states_impl( + apply_delayed_receipts_to_children_states_impl( &mut trie_updates, &insert_receipts, &changes.processed_delayed_receipts, @@ -102,14 +102,14 @@ impl ShardTries { /// The caller must guarantee that `state_roots` contains all shard_ids /// that `key_to_shard_id` that may return /// Ignore changes on DelayedReceipts or DelayedReceiptsIndices - /// Returns `store_update` and the new state_roots for split states - pub fn add_values_to_split_states( + /// Returns `store_update` and the new state_roots for children shards + pub fn add_values_to_children_states( &self, state_roots: &HashMap, values: Vec<(Vec, Option>)>, account_id_to_shard_id: &dyn Fn(&AccountId) -> ShardUId, ) -> Result<(StoreUpdate, HashMap), StorageError> { - self.add_values_to_split_states_impl(state_roots, values, &|raw_key| { + self.add_values_to_children_states_impl(state_roots, values, &|raw_key| { // Here changes on DelayedReceipts or DelayedReceiptsIndices will be excluded // This is because we cannot migrate delayed receipts part by part. They have to be // reconstructed in the new states after all DelayedReceipts are ready in the original @@ -126,7 +126,7 @@ impl ShardTries { }) } - fn add_values_to_split_states_impl( + fn add_values_to_children_states_impl( &self, state_roots: &HashMap, values: Vec<(Vec, Option>)>, @@ -164,14 +164,14 @@ impl ShardTries { .collect() } - pub fn apply_delayed_receipts_to_split_states( + pub fn apply_delayed_receipts_to_children_states( &self, state_roots: &HashMap, receipts: &[Receipt], account_id_to_shard_uid: &dyn Fn(&AccountId) -> ShardUId, ) -> Result<(StoreUpdate, HashMap), StorageError> { let mut trie_updates: HashMap<_, _> = self.get_trie_updates(state_roots); - apply_delayed_receipts_to_split_states_impl( + apply_delayed_receipts_to_children_states_impl( &mut trie_updates, receipts, &[], @@ -197,7 +197,7 @@ impl ShardTries { } } -fn apply_delayed_receipts_to_split_states_impl( +fn apply_delayed_receipts_to_children_states_impl( trie_updates: &mut HashMap, insert_receipts: &[Receipt], delete_receipts: &[Receipt], @@ -269,7 +269,7 @@ fn apply_delayed_receipts_to_split_states_impl( TrieKey::DelayedReceiptIndices, delayed_receipts_indices_by_shard.get(shard_uid).unwrap(), ); - // StateChangeCause should always be Resharding for processing split state. + // StateChangeCause should always be Resharding for processing resharding. // We do not want to commit the state_changes from resharding as they are already handled while // processing parent shard trie_update.commit(StateChangeCause::Resharding); @@ -317,7 +317,7 @@ pub fn get_delayed_receipts( #[cfg(test)] mod tests { - use crate::split_state::{apply_delayed_receipts_to_split_states_impl, get_delayed_receipts}; + use crate::resharding::{apply_delayed_receipts_to_children_states_impl, get_delayed_receipts}; use crate::test_utils::{ gen_changes, gen_receipts, get_all_delayed_receipts, test_populate_trie, TestTriesBuilder, }; @@ -333,7 +333,7 @@ mod tests { use std::collections::HashMap; #[test] - fn test_add_values_to_split_states() { + fn test_add_values_to_children_states() { let mut rng = rand::thread_rng(); for _ in 0..20 { @@ -354,7 +354,7 @@ mod tests { ); let (store_update, new_state_roots) = tries - .add_values_to_split_states_impl(&state_roots, changes, &|raw_key| { + .add_values_to_children_states_impl(&state_roots, changes, &|raw_key| { Ok(Some(ShardUId { version: 1, shard_id: (hash(raw_key).0[0] as NumShards % num_shards) as u32, @@ -441,7 +441,7 @@ mod tests { account_id_to_shard_id: &dyn Fn(&AccountId) -> ShardUId, ) -> HashMap { let mut trie_updates: HashMap<_, _> = tries.get_trie_updates(&state_roots); - apply_delayed_receipts_to_split_states_impl( + apply_delayed_receipts_to_children_states_impl( &mut trie_updates, new_receipts, delete_receipts, diff --git a/core/store/src/trie/shard_tries.rs b/core/store/src/trie/shard_tries.rs index 79d208a30d0..efef51983c6 100644 --- a/core/store/src/trie/shard_tries.rs +++ b/core/store/src/trie/shard_tries.rs @@ -504,7 +504,7 @@ impl WrappedTrieChanges { ); // Resharding changes must not be finalized, however they may be introduced here when we are - // evaluating changes for split state in process_split_state function + // evaluating changes for resharding in process_resharding_results function change_with_trie_key .changes .retain(|change| change.cause != StateChangeCause::Resharding); diff --git a/core/store/src/trie/state_parts.rs b/core/store/src/trie/state_parts.rs index 58727c87cc2..f2482d35511 100644 --- a/core/store/src/trie/state_parts.rs +++ b/core/store/src/trie/state_parts.rs @@ -580,7 +580,7 @@ mod tests { (b"aaa".to_vec(), Some(vec![3; value_len])), (b"aaaa".to_vec(), Some(vec![4; value_len])), ]; - // We split state into `num_keys + 1` parts for convenience of testing, + // We reshard into `num_keys + 1` parts for convenience of testing, // because right boundaries are exclusive. This way first part is // empty and other parts contain exactly one key. let num_parts = trie_changes.len() + 1; diff --git a/docs/architecture/how/resharding.md b/docs/architecture/how/resharding.md index e145d306f5e..b9d25b4dd28 100644 --- a/docs/architecture/how/resharding.md +++ b/docs/architecture/how/resharding.md @@ -58,11 +58,11 @@ about to preprocess is the first block of the epoch (X+1) - it calls ``get_state_sync_info``, which is responsible for figuring out which shards will be needed in next epoch (X+2). -This is the moment, when node can request new shards that it didn't track before (using StateSync) - and if it detects that the shard layout would change in the next epoch, it also involves the StateSync - but skips the download part (as it already has the data) - and starts from state splitting. +This is the moment, when node can request new shards that it didn't track before (using StateSync) - and if it detects that the shard layout would change in the next epoch, it also involves the StateSync - but skips the download part (as it already has the data) - and starts from resharding. -StateSync in this phase would send the ``StateSplitRequest`` to the ``SyncJobsActor`` (you can think about the ``SyncJobsActor`` as a background thread). +StateSync in this phase would send the ``ReshardingRequest`` to the ``SyncJobsActor`` (you can think about the ``SyncJobsActor`` as a background thread). -We'd use the background thread to do the state splitting: the goal is to change the one trie (that represents the state of the current shard) - to multiple tries (one for each of the new shards). +We'd use the background thread to perform resharding: the goal is to change the one trie (that represents the state of the current shard) - to multiple tries (one for each of the new shards). In order to split a trie into children tries we use a snapshot of the flat storage. We iterate over all of the entries in the flat storage and we build the children tries by inserting the parent entry into either of the children tries. @@ -145,16 +145,16 @@ Here is an example of what that may look like in a grafana dashboard. Please kee ### Throttling -The resharding process can be quite resource intensive and affect the regular operation of a node. In order to mitigate that as well as limit any need for increasing hardware specifications of the nodes throttling was added. Throttling slows down resharding to not have it impact other node operations. Throttling can be configured by adjusting the state_split_config in the node config file. +The resharding process can be quite resource intensive and affect the regular operation of a node. In order to mitigate that as well as limit any need for increasing hardware specifications of the nodes throttling was added. Throttling slows down resharding to not have it impact other node operations. Throttling can be configured by adjusting the resharding_config in the node config file. * batch_size - controls the size of batches in which resharding moves data around. Setting a smaller batch size will slow down the resharding process and make it less resource consuming. * batch_delay - controls the delay between processing of batches. Setting a smaller batch delay will speed up the resharding process and make it more resource consuming. -The remainig fields in the StateSplitConfig are only intended for testing purposes and should remain set to their default values. +The remainig fields in the ReshardingConfig are only intended for testing purposes and should remain set to their default values. -The default configuration for StateSplitConfig should provide a good and safe setting for resharding in the production networks. There is no need for node operators to make any changes to it unless they observe issues. +The default configuration for ReshardingConfig should provide a good and safe setting for resharding in the production networks. There is no need for node operators to make any changes to it unless they observe issues. -The state split config can be adjusted at runtime, without restarting the node. The config needs to be updated first and then a SIGHUP signal should be sent to the neard process. When received the signal neard will update the config and print a log message showing what fields were changed. It's recommended to check the log to make sure the relevant config change was correctly picked up. +The resharding config can be adjusted at runtime, without restarting the node. The config needs to be updated first and then a SIGHUP signal should be sent to the neard process. When received the signal neard will update the config and print a log message showing what fields were changed. It's recommended to check the log to make sure the relevant config change was correctly picked up. ## Future possibilities diff --git a/integration-tests/src/tests/client/flat_storage.rs b/integration-tests/src/tests/client/flat_storage.rs index c689246f4c7..57afd6b9f19 100644 --- a/integration-tests/src/tests/client/flat_storage.rs +++ b/integration-tests/src/tests/client/flat_storage.rs @@ -314,7 +314,7 @@ fn test_flat_storage_creation_start_from_state_part() { let store = create_test_store(); // Process some blocks with flat storage. - // Split state into two parts and return trie keys corresponding to each part. + // Reshard into two parts and return trie keys corresponding to each part. const NUM_PARTS: u64 = 2; let trie_keys: Vec<_> = { let mut env = setup_env(&genesis, store.clone()); diff --git a/integration-tests/src/tests/client/resharding.rs b/integration-tests/src/tests/client/resharding.rs index ec7b7d44bc4..11009173ac8 100644 --- a/integration-tests/src/tests/client/resharding.rs +++ b/integration-tests/src/tests/client/resharding.rs @@ -157,7 +157,7 @@ impl DropChunkCondition { /// Test environment prepared for testing the sharding upgrade. /// Epoch 0, blocks 1-5 : genesis shard layout -/// Epoch 1, blocks 6-10 : genesis shard layout, state split happens +/// Epoch 1, blocks 6-10 : genesis shard layout, resharding happens /// Epoch 2: blocks 10-15: target shard layout, shard layout is upgraded /// Epoch 3: blocks 16-20: target shard layout, /// @@ -364,7 +364,7 @@ impl TestReshardingEnv { env.process_shards_manager_responses_and_finish_processing_blocks(j); } - // after state split, check chunk extra exists and the states are correct + // after resharding, check chunk extra exists and the states are correct for account_id in self.initial_accounts.iter() { check_account(env, account_id, &block); } @@ -484,7 +484,7 @@ impl TestReshardingEnv { .get_shard_layout_from_prev_block(&last_block_hash) .unwrap(); - shard_layout.get_split_shard_ids(shard_id as ShardId).unwrap() + shard_layout.get_children_shards_ids(shard_id as ShardId).unwrap() }; for target_shard_id in target_shard_ids { @@ -608,9 +608,9 @@ impl TestReshardingEnv { } } - /// Check that after split state is finished, the artifacts stored in storage is removed - fn check_split_states_artifacts(&mut self) { - tracing::debug!(target: "test", "checking split states artifacts"); + /// Check that after resharding is finished, the artifacts stored in storage is removed + fn check_resharding_artifacts(&mut self) { + tracing::debug!(target: "test", "checking resharding artifacts"); let env = &mut self.env; let head = env.clients[0].chain.head().unwrap(); @@ -623,7 +623,7 @@ impl TestReshardingEnv { let res = env.clients[0] .chain .chain_store() - .get_state_changes_for_split_states(&block_hash, shard_id); + .get_state_changes_for_resharding(&block_hash, shard_id); assert_matches!(res, Err(error) => { assert_matches!(error, Error::DBNotFoundErr(_)); }) @@ -982,7 +982,7 @@ fn test_shard_layout_upgrade_simple_impl( test_env.check_tx_outcomes(false); test_env.check_accounts(accounts_to_check.iter().collect()); - test_env.check_split_states_artifacts(); + test_env.check_resharding_artifacts(); test_env.check_outgoing_receipts_reassigned(&resharding_type); tracing::info!(target: "test", "test_shard_layout_upgrade_simple_impl finished"); } @@ -1036,7 +1036,7 @@ fn test_shard_layout_upgrade_gc_impl(resharding_type: ReshardingType, rng_seed: // GC period is about 5 epochs. We should expect to see state deleted at the end of the 7th epoch // Epoch 0, blocks 1-5 : genesis shard layout - // Epoch 1, blocks 6-10 : genesis shard layout, state split happens + // Epoch 1, blocks 6-10 : genesis shard layout, resharding happens // Epoch 2: blocks 10-15: target shard layout, shard layout is upgraded // Epoch 3-7: target shard layout, waiting for GC to happen // Epoch 8: block 37: GC happens, state is deleted @@ -1323,7 +1323,7 @@ fn test_shard_layout_upgrade_cross_contract_calls_impl( test_env.check_accounts(new_accounts); - test_env.check_split_states_artifacts(); + test_env.check_resharding_artifacts(); } // Test cross contract calls @@ -1397,7 +1397,7 @@ fn test_shard_layout_upgrade_incoming_receipts_impl( successful_txs.iter().flat_map(|tx_hash| new_accounts.get(tx_hash)).collect(); test_env.check_accounts(new_accounts); - test_env.check_split_states_artifacts(); + test_env.check_resharding_artifacts(); } // This test doesn't make much sense for the V1 resharding. That is because in @@ -1470,7 +1470,7 @@ fn test_missing_chunks( successful_txs.iter().flat_map(|tx_hash| new_accounts.get(tx_hash)).collect(); test_env.check_accounts(new_accounts); - test_env.check_split_states_artifacts(); + test_env.check_resharding_artifacts(); } fn test_shard_layout_upgrade_missing_chunks( diff --git a/nearcore/src/config.rs b/nearcore/src/config.rs index e51ccb11031..597444c267b 100644 --- a/nearcore/src/config.rs +++ b/nearcore/src/config.rs @@ -11,7 +11,7 @@ use near_chain_configs::{ default_transaction_pool_size_limit, default_trie_viewer_state_size_limit, default_tx_routing_height_horizon, default_view_client_threads, default_view_client_throttle_period, get_initial_supply, ClientConfig, GCConfig, Genesis, - GenesisConfig, GenesisValidationMode, LogSummaryStyle, MutableConfigValue, StateSplitConfig, + GenesisConfig, GenesisValidationMode, LogSummaryStyle, MutableConfigValue, ReshardingConfig, StateSyncConfig, }; use near_config_utils::{ValidationError, ValidationErrors}; @@ -307,7 +307,7 @@ pub struct Config { /// chunks and underutilizing the capacity of the network. pub transaction_pool_size_limit: Option, // Configuration for resharding. - pub state_split_config: StateSplitConfig, + pub resharding_config: ReshardingConfig, /// If the node is not a chunk producer within that many blocks, then route /// to upcoming chunk producers. pub tx_routing_height_horizon: BlockHeightDelta, @@ -356,7 +356,7 @@ impl Default for Config { state_sync_enabled: default_state_sync_enabled(), transaction_pool_size_limit: default_transaction_pool_size_limit(), enable_multiline_logging: default_enable_multiline_logging(), - state_split_config: StateSplitConfig::default(), + resharding_config: ReshardingConfig::default(), tx_routing_height_horizon: default_tx_routing_height_horizon(), produce_chunk_add_transactions_time_limit: default_produce_chunk_add_transactions_time_limit(), @@ -656,9 +656,9 @@ impl NearConfig { state_sync: config.state_sync.unwrap_or_default(), transaction_pool_size_limit: config.transaction_pool_size_limit, enable_multiline_logging: config.enable_multiline_logging.unwrap_or(true), - state_split_config: MutableConfigValue::new( - config.state_split_config, - "state_split_config", + resharding_config: MutableConfigValue::new( + config.resharding_config, + "resharding_config", ), tx_routing_height_horizon: config.tx_routing_height_horizon, produce_chunk_add_transactions_time_limit: MutableConfigValue::new( diff --git a/nearcore/src/dyn_config.rs b/nearcore/src/dyn_config.rs index 0917128c482..eff211a89f9 100644 --- a/nearcore/src/dyn_config.rs +++ b/nearcore/src/dyn_config.rs @@ -47,7 +47,7 @@ pub fn get_updateable_client_config(config: Config) -> UpdateableClientConfig { // Keep this list in-sync with `core/dyn-configs/README.md`. UpdateableClientConfig { expected_shutdown: config.expected_shutdown, - state_split_config: config.state_split_config, + resharding_config: config.resharding_config, produce_chunk_add_transactions_time_limit: config.produce_chunk_add_transactions_time_limit, } } diff --git a/nearcore/src/lib.rs b/nearcore/src/lib.rs index eb59304b446..27c62e00305 100644 --- a/nearcore/src/lib.rs +++ b/nearcore/src/lib.rs @@ -17,7 +17,7 @@ use near_chain::state_snapshot_actor::{ }; use near_chain::types::RuntimeAdapter; use near_chain::{Chain, ChainGenesis}; -use near_chain_configs::StateSplitHandle; +use near_chain_configs::ReshardingHandle; use near_chain_configs::SyncConfig; use near_chunks::shards_manager_actor::start_shards_manager; use near_client::sync::adapter::SyncAdapter; @@ -216,7 +216,7 @@ pub struct NearNode { pub flat_state_migration_handle: FlatStateValuesInliningMigrationHandle, // A handle that allows the main process to interrupt resharding if needed. // This typically happens when the main process is interrupted. - pub state_split_handle: StateSplitHandle, + pub resharding_handle: ReshardingHandle, } pub fn start_with_config(home_dir: &Path, config: NearConfig) -> anyhow::Result { @@ -333,7 +333,7 @@ pub fn start_with_config_and_synchronization( get_make_snapshot_callback(state_snapshot_actor, runtime.get_flat_storage_manager()); let snapshot_callbacks = SnapshotCallbacks { make_snapshot_callback, delete_snapshot_callback }; - let (client_actor, client_arbiter_handle, state_split_handle) = start_client( + let (client_actor, client_arbiter_handle, resharding_handle) = start_client( config.client_config.clone(), chain_genesis.clone(), epoch_manager.clone(), @@ -446,7 +446,7 @@ pub fn start_with_config_and_synchronization( cold_store_loop_handle, state_sync_dump_handle, flat_state_migration_handle, - state_split_handle, + resharding_handle, }) } diff --git a/nearcore/src/runtime/mod.rs b/nearcore/src/runtime/mod.rs index 8f6f6444c1d..5e6a2482337 100644 --- a/nearcore/src/runtime/mod.rs +++ b/nearcore/src/runtime/mod.rs @@ -5,7 +5,7 @@ use crate::NearConfig; use borsh::BorshDeserialize; use errors::FromStateViewerErrors; use near_chain::types::{ - ApplySplitStateResult, ApplyTransactionResult, ApplyTransactionsBlockContext, + ApplyResultForResharding, ApplyTransactionResult, ApplyTransactionsBlockContext, ApplyTransactionsChunkContext, RuntimeAdapter, RuntimeStorageConfig, StorageDataSource, Tip, }; use near_chain::Error; @@ -30,7 +30,7 @@ use near_primitives::transaction::SignedTransaction; use near_primitives::trie_key::TrieKey; use near_primitives::types::{ AccountId, Balance, BlockHeight, EpochHeight, EpochId, EpochInfoProvider, Gas, MerkleHash, - ShardId, StateChangeCause, StateChangesForSplitStates, StateRoot, StateRootNode, + ShardId, StateChangeCause, StateChangesForResharding, StateRoot, StateRootNode, }; use near_primitives::version::ProtocolVersion; use near_primitives::views::{ @@ -1085,24 +1085,24 @@ impl RuntimeAdapter for NightshadeRuntime { } } - fn apply_update_to_split_states( + fn apply_update_to_children_states( &self, block_hash: &CryptoHash, block_height: BlockHeight, state_roots: HashMap, next_epoch_shard_layout: &ShardLayout, - state_changes_for_split_states: StateChangesForSplitStates, - ) -> Result, Error> { - let trie_updates = self.tries.apply_state_changes_to_split_states( + state_changes_for_resharding: StateChangesForResharding, + ) -> Result, Error> { + let trie_updates = self.tries.apply_state_changes_to_children_states( &state_roots, - state_changes_for_split_states, + state_changes_for_resharding, &|account_id| account_id_to_shard_uid(account_id, next_epoch_shard_layout), )?; - let mut applied_split_state_results: Vec<_> = vec![]; + let mut applied_resharding_results: Vec<_> = vec![]; for (shard_uid, trie_update) in trie_updates { let (_, trie_changes, state_changes) = trie_update.finalize()?; - // All state changes that are related to split state should have StateChangeCause as Resharding + // All state changes that are related to resharding should have StateChangeCause as Resharding // We do not want to commit the state_changes from resharding as they are already handled while // processing parent shard debug_assert!(state_changes.iter().all(|raw_state_changes| raw_state_changes @@ -1118,14 +1118,14 @@ impl RuntimeAdapter for NightshadeRuntime { *block_hash, block_height, ); - applied_split_state_results.push(ApplySplitStateResult { + applied_resharding_results.push(ApplyResultForResharding { shard_uid, new_root, trie_changes: wrapped_trie_changes, }); } - Ok(applied_split_state_results) + Ok(applied_resharding_results) } fn apply_state_part( diff --git a/nearcore/src/runtime/tests.rs b/nearcore/src/runtime/tests.rs index e426d997aca..7cf57744f96 100644 --- a/nearcore/src/runtime/tests.rs +++ b/nearcore/src/runtime/tests.rs @@ -648,11 +648,6 @@ fn test_verify_validator_signature() { .unwrap()); } -#[test] -fn test_split_states() { - init_test_logger(); -} - // TODO (#7327): enable test when flat storage will support state sync. #[ignore] #[test] diff --git a/neard/src/cli.rs b/neard/src/cli.rs index 90c23d1f35e..acb5881a3ff 100644 --- a/neard/src/cli.rs +++ b/neard/src/cli.rs @@ -556,7 +556,7 @@ impl RunCmd { cold_store_loop_handle, state_sync_dump_handle, flat_state_migration_handle, - state_split_handle, + resharding_handle, .. } = nearcore::start_with_config_and_synchronization( home_dir, @@ -583,7 +583,7 @@ impl RunCmd { if let Some(handle) = state_sync_dump_handle { handle.stop() } - state_split_handle.stop(); + resharding_handle.stop(); flat_state_migration_handle.stop(); futures::future::join_all(rpc_servers.iter().map(|(name, server)| async move { server.stop(true).await; diff --git a/pytest/lib/resharding_lib.py b/pytest/lib/resharding_lib.py index 5cc678aeaba..6fa23b90fc8 100644 --- a/pytest/lib/resharding_lib.py +++ b/pytest/lib/resharding_lib.py @@ -143,7 +143,7 @@ def get_epoch_offset(binary_protocol_version): def get_client_config_changes(num_nodes, initial_delay=None): single = { "tracked_shards": [0], - "state_split_config": { + "resharding_config": { "batch_size": 1000000, # don't throttle resharding "batch_delay": { @@ -158,7 +158,7 @@ def get_client_config_changes(num_nodes, initial_delay=None): } } if initial_delay is not None: - single["state_split_config"]["initial_delay"] = { + single["resharding_config"]["initial_delay"] = { "secs": initial_delay, "nanos": 0 } diff --git a/tools/speedy_sync/src/main.rs b/tools/speedy_sync/src/main.rs index a3ad5e45a74..622e1024495 100644 --- a/tools/speedy_sync/src/main.rs +++ b/tools/speedy_sync/src/main.rs @@ -1,7 +1,7 @@ use borsh::{BorshDeserialize, BorshSerialize}; use near_chain::types::{ChainConfig, Tip}; use near_chain::{Chain, ChainGenesis, DoomslugThresholdMode}; -use near_chain_configs::{GenesisValidationMode, MutableConfigValue, StateSplitConfig}; +use near_chain_configs::{GenesisValidationMode, MutableConfigValue, ReshardingConfig}; use near_epoch_manager::shard_tracker::{ShardTracker, TrackedConfig}; use near_epoch_manager::types::EpochInfoAggregator; use near_epoch_manager::EpochManager; @@ -243,9 +243,9 @@ fn load_snapshot(load_cmd: LoadCmd) { ChainConfig { save_trie_changes: config.client_config.save_trie_changes, background_migration_threads: 1, - state_split_config: MutableConfigValue::new( - StateSplitConfig::default(), - "state_split_config", + resharding_config: MutableConfigValue::new( + ReshardingConfig::default(), + "resharding_config", ), }, None,