Skip to content

Commit

Permalink
Two reshardings
Browse files Browse the repository at this point in the history
  • Loading branch information
staffik committed Dec 17, 2024
1 parent 7712f8b commit 44da438
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
7 changes: 5 additions & 2 deletions chain/chain/src/resharding/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use near_primitives::challenge::PartialState;
use near_primitives::hash::CryptoHash;
use near_primitives::shard_layout::{get_block_shard_uid, ShardLayout};
use near_primitives::types::chunk_extra::ChunkExtra;
use near_store::adapter::trie_store::get_shard_uid_mapping;
use near_store::adapter::{StoreAdapter, StoreUpdateAdapter};
use near_store::trie::mem::mem_trie_update::TrackingMode;
use near_store::trie::ops::resharding::RetainMode;
Expand Down Expand Up @@ -140,16 +141,18 @@ impl ReshardingManager {
}

/// Store in the database the mapping of ShardUId from children to the parent shard,
/// so that subsequent accesses to the State will use the parent shard's UId as a prefix for the database key.
/// so that subsequent accesses to the State will use the parent shard's UId prefix
/// as a prefix for the database key.
fn set_state_shard_uid_mapping(
&mut self,
split_shard_event: &ReshardingSplitShardParams,
) -> io::Result<()> {
let mut store_update = self.store.trie_store().store_update();
let parent_shard_uid = split_shard_event.parent_shard;
let parent_shard_uid_prefix = get_shard_uid_mapping(&self.store, parent_shard_uid);
// TODO(resharding) No need to set the mapping for children shards that we won't track just after resharding?
for child_shard_uid in split_shard_event.children_shards() {
store_update.set_shard_uid_mapping(child_shard_uid, parent_shard_uid);
store_update.set_shard_uid_mapping(child_shard_uid, parent_shard_uid_prefix);
}
store_update.commit()
}
Expand Down
12 changes: 7 additions & 5 deletions core/primitives-core/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,13 @@ impl ProtocolFeature {
ProtocolFeature::ShuffleShardAssignments => 143,
ProtocolFeature::CurrentEpochStateSync => 144,
ProtocolFeature::SimpleNightshadeV4 => 145,
// Protocol version 146 is reserved for resharding tests.
// ProtocolFeature::SimpleNightshadeV4 => 146,
#[cfg(feature = "protocol_feature_relaxed_chunk_validation")]
ProtocolFeature::RelaxedChunkValidation => 146,
ProtocolFeature::ExcludeExistingCodeFromWitnessForCodeLen => 147,
ProtocolFeature::BandwidthScheduler => 148,
ProtocolFeature::BlockHeightForReceiptId => 149,
ProtocolFeature::RelaxedChunkValidation => 147,
ProtocolFeature::ExcludeExistingCodeFromWitnessForCodeLen => 148,
ProtocolFeature::BandwidthScheduler => 149,
ProtocolFeature::BlockHeightForReceiptId => 150,
// Place features that are not yet in Nightly below this line.
}
}
Expand All @@ -288,7 +290,7 @@ impl ProtocolFeature {
const STABLE_PROTOCOL_VERSION: ProtocolVersion = 74;

// On nightly, pick big enough version to support all features.
const NIGHTLY_PROTOCOL_VERSION: ProtocolVersion = 149;
const NIGHTLY_PROTOCOL_VERSION: ProtocolVersion = 150;

/// Largest protocol version supported by the current binary.
pub const PROTOCOL_VERSION: ProtocolVersion = if cfg!(feature = "nightly_protocol") {
Expand Down
57 changes: 42 additions & 15 deletions integration-tests/src/test_loop/tests/resharding_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use near_chain_configs::test_genesis::{TestGenesisBuilder, ValidatorsSpec};
use near_chain_configs::DEFAULT_GC_NUM_EPOCHS_TO_KEEP;
use near_client::Query;
use near_o11y::testonly::init_test_logger;
use near_primitives::epoch_manager::EpochConfigStore;
use near_primitives::epoch_manager::{EpochConfig, EpochConfigStore};
use near_primitives::shard_layout::ShardLayout;
use near_primitives::types::{
AccountId, BlockHeightDelta, BlockId, BlockReference, Gas, ShardId, ShardIndex,
Expand Down Expand Up @@ -86,6 +86,8 @@ struct TestReshardingParameters {
// TODO(resharding) Remove this when negative refcounts are properly handled.
/// Whether to allow negative refcount being a result of the database update.
allow_negative_refcount: bool,
/// Make two reshardings in a row.
reshard_twice: bool,
}

impl TestReshardingParametersBuilder {
Expand Down Expand Up @@ -163,6 +165,7 @@ impl TestReshardingParametersBuilder {
delay_flat_state_resharding: self.delay_flat_state_resharding.unwrap_or(0),
short_yield_timeout: self.short_yield_timeout.unwrap_or(false),
allow_negative_refcount: self.allow_negative_refcount.unwrap_or(false),
reshard_twice: self.reshard_twice.unwrap_or(false),
}
}

Expand Down Expand Up @@ -549,6 +552,18 @@ fn check_deleted_account_availability(
assert!(!rpc_node_result.is_ok());
}

fn derive_new_epoch_config_from_boundary(
base_epoch_config: &EpochConfig,
boundary_account: &AccountId,
) -> EpochConfig {
let base_shard_layout = &base_epoch_config.shard_layout;
let mut epoch_config = base_epoch_config.clone();
epoch_config.shard_layout =
ShardLayout::derive_shard_layout(&base_shard_layout, boundary_account.clone());
tracing::info!(target: "test", ?base_shard_layout, new_shard_layout=?epoch_config.shard_layout, "shard layout");
epoch_config
}

/// Base setup to check sanity of Resharding V3.
/// TODO(#11881): add the following scenarios:
/// - Nodes must not track all shards. State sync must succeed.
Expand Down Expand Up @@ -592,20 +607,24 @@ fn test_resharding_v3_base(params: TestReshardingParameters) {

let base_shard_layout = get_base_shard_layout(params.base_shard_layout_version);
base_epoch_config.shard_layout = base_shard_layout.clone();

let new_boundary_account = "account6".parse().unwrap();
let parent_shard_uid = base_shard_layout.account_id_to_shard_uid(&new_boundary_account);
let mut epoch_config = base_epoch_config.clone();
epoch_config.shard_layout =
ShardLayout::derive_shard_layout(&base_shard_layout, new_boundary_account.clone());
tracing::info!(target: "test", ?base_shard_layout, new_shard_layout=?epoch_config.shard_layout, "shard layout");

let expected_num_shards = epoch_config.shard_layout.num_shards();
let epoch_config_store = EpochConfigStore::test(BTreeMap::from_iter(vec![
(base_protocol_version, Arc::new(base_epoch_config)),
(base_protocol_version + 1, Arc::new(epoch_config)),
]));

let mut new_boundary_account = "account6".parse().unwrap();
let epoch_config =
derive_new_epoch_config_from_boundary(&base_epoch_config, &new_boundary_account);

let mut epoch_configs = vec![
(base_protocol_version, Arc::new(base_epoch_config.clone())),
(base_protocol_version + 1, Arc::new(epoch_config.clone())),
];
if params.reshard_twice {
new_boundary_account = "account7".parse().unwrap();
let second_resharding_epoch_config =
derive_new_epoch_config_from_boundary(&epoch_config, &new_boundary_account);
epoch_configs.push((base_protocol_version + 2, Arc::new(second_resharding_epoch_config)));
}
let expected_num_shards = epoch_configs.last().unwrap().1.shard_layout.num_shards();
let parent_shard_uid =
base_epoch_config.shard_layout.account_id_to_shard_uid(&new_boundary_account);
let epoch_config_store = EpochConfigStore::test(BTreeMap::from_iter(epoch_configs));
let genesis = TestGenesisBuilder::new()
.genesis_time_from_clock(&builder.clock())
.shard_layout(base_shard_layout)
Expand Down Expand Up @@ -790,6 +809,14 @@ fn test_resharding_v3() {
test_resharding_v3_base(TestReshardingParametersBuilder::default().build());
}

#[test]
// TODO(resharding) Fix it and un-ignore,
// see https://near.zulipchat.com/#narrow/channel/407288-core.2Fresharding/topic/testloop.20failures/near/489466638)
#[ignore]
fn test_resharding_v3_twice() {
test_resharding_v3_base(TestReshardingParametersBuilder::default().reshard_twice(true).build());
}

#[test]
fn test_resharding_v3_drop_chunks_before() {
let chunk_ranges_to_drop = HashMap::from([(1, -2..0)]);
Expand Down

0 comments on commit 44da438

Please sign in to comment.