-
Notifications
You must be signed in to change notification settings - Fork 803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Elastic scaling: use an assumed CoreIndex
in candidate-backing
#3229
Merged
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
cab6ab9
Switch statement table from ParaId to CoreIndex
sandreim d2df658
cargo lock
sandreim 4a8b8d5
add experimental feature
sandreim 9244632
inject core_index from statements
sandreim 22e017b
temporary provisioner fix
sandreim 574b06a
Merge branch 'master' of github.com:paritytech/polkadot-sdk into sand…
sandreim fbb7351
cargo lock
sandreim 6c72918
It was damn hard to fix these tests
sandreim fc5c109
These tests were easy to fix
sandreim 33351b4
Fix comment
sandreim 0d994bf
clippy was angry
sandreim 534c019
A bit refactor and add a test
sandreim 10d86dd
taplo happy
sandreim 222609c
review feedback
sandreim a02e896
remove log
sandreim dd34850
more feedback
sandreim 838a846
Merge remote-tracking branch 'origin/master' into sandreim/backing_mu…
alindima ad98f18
use next up on available instead of occupied core index
alindima 606d7c4
ElasticScalingCoreIndex -> ElasticScalingMVP
alindima c793b89
small nits and typos
alindima 8398bb1
Merge remote-tracking branch 'origin/master' into sandreim/backing_mu…
alindima 9f70276
Merge remote-tracking branch 'origin/master' into sandreim/backing_mu…
alindima 9c3dd5c
cache Validator->Group mapping
alindima af1cd82
use Arc to avoid cloning
alindima File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,7 @@ use futures::{ | |
stream::FuturesOrdered, | ||
FutureExt, SinkExt, StreamExt, TryFutureExt, | ||
}; | ||
use schnellru::{ByLength, LruMap}; | ||
|
||
use error::{Error, FatalResult}; | ||
use polkadot_node_primitives::{ | ||
|
@@ -107,8 +108,9 @@ use polkadot_primitives::{ | |
vstaging::{node_features::FeatureIndex, NodeFeatures}, | ||
BackedCandidate, CandidateCommitments, CandidateHash, CandidateReceipt, | ||
CommittedCandidateReceipt, CoreIndex, CoreState, ExecutorParams, GroupIndex, GroupRotationInfo, | ||
Hash, Id as ParaId, PersistedValidationData, PvfExecKind, SigningContext, ValidationCode, | ||
ValidatorId, ValidatorIndex, ValidatorSignature, ValidityAttestation, | ||
Hash, Id as ParaId, IndexedVec, PersistedValidationData, PvfExecKind, SessionIndex, | ||
SigningContext, ValidationCode, ValidatorId, ValidatorIndex, ValidatorSignature, | ||
ValidityAttestation, | ||
}; | ||
use sp_keystore::KeystorePtr; | ||
use statement_table::{ | ||
|
@@ -232,8 +234,8 @@ struct PerRelayParentState { | |
inject_core_index: bool, | ||
/// The core states for all cores. | ||
cores: Vec<CoreState>, | ||
/// The validator groups at this relay parent. | ||
validator_groups: Vec<Vec<ValidatorIndex>>, | ||
/// The validator index -> group mapping at this relay parent. | ||
validator_to_group: IndexedVec<ValidatorIndex, Option<GroupIndex>>, | ||
/// The associated group rotation information. | ||
group_rotation_info: GroupRotationInfo, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those two are really per session. Storing them per relay parent is rather unnecessary. Seems fine for an interim solution though. |
||
} | ||
|
@@ -287,6 +289,8 @@ struct State { | |
/// This is guaranteed to have an entry for each candidate with a relay parent in the implicit | ||
/// or explicit view for which a `Seconded` statement has been successfully imported. | ||
per_candidate: HashMap<CandidateHash, PerCandidateState>, | ||
/// Cache the per-session Validator->Group mapping. | ||
validator_to_group_cache: LruMap<SessionIndex, IndexedVec<ValidatorIndex, Option<GroupIndex>>>, | ||
/// A cloneable sender which is dispatched to background candidate validation tasks to inform | ||
/// the main task of the result. | ||
background_validation_tx: mpsc::Sender<(Hash, ValidatedCandidateCommand)>, | ||
|
@@ -304,6 +308,7 @@ impl State { | |
per_leaf: HashMap::default(), | ||
per_relay_parent: HashMap::default(), | ||
per_candidate: HashMap::new(), | ||
validator_to_group_cache: LruMap::new(ByLength::new(2)), | ||
background_validation_tx, | ||
keystore, | ||
} | ||
|
@@ -986,7 +991,14 @@ async fn handle_active_leaves_update<Context>( | |
|
||
// construct a `PerRelayParent` from the runtime API | ||
// and insert it. | ||
let per = construct_per_relay_parent_state(ctx, maybe_new, &state.keystore, mode).await?; | ||
let per = construct_per_relay_parent_state( | ||
ctx, | ||
maybe_new, | ||
&state.keystore, | ||
&mut state.validator_to_group_cache, | ||
mode, | ||
) | ||
.await?; | ||
|
||
if let Some(per) = per { | ||
state.per_relay_parent.insert(maybe_new, per); | ||
|
@@ -1014,7 +1026,7 @@ macro_rules! try_runtime_api { | |
} | ||
|
||
fn core_index_from_statement( | ||
validator_groups: &[Vec<ValidatorIndex>], | ||
validator_to_group: &IndexedVec<ValidatorIndex, Option<GroupIndex>>, | ||
group_rotation_info: &GroupRotationInfo, | ||
cores: &[CoreState], | ||
statement: &SignedFullStatementWithPVD, | ||
|
@@ -1024,51 +1036,70 @@ fn core_index_from_statement( | |
|
||
let n_cores = cores.len(); | ||
|
||
gum::trace!(target: LOG_TARGET, ?group_rotation_info, ?statement, ?validator_groups, n_cores = ?cores.len() , ?candidate_hash, "Extracting core index from statement"); | ||
gum::trace!( | ||
target:LOG_TARGET, | ||
?group_rotation_info, | ||
?statement, | ||
?validator_to_group, | ||
n_cores = ?cores.len(), | ||
?candidate_hash, | ||
"Extracting core index from statement" | ||
); | ||
|
||
let statement_validator_index = statement.validator_index(); | ||
for (group_index, group) in validator_groups.iter().enumerate() { | ||
for validator_index in group { | ||
if *validator_index == statement_validator_index { | ||
// First check if the statement para id matches the core assignment. | ||
let core_index = | ||
group_rotation_info.core_for_group(GroupIndex(group_index as u32), n_cores); | ||
|
||
if core_index.0 as usize > n_cores { | ||
gum::warn!(target: LOG_TARGET, ?candidate_hash, ?core_index, n_cores, "Invalid CoreIndex"); | ||
return None | ||
} | ||
let Some(Some(group_index)) = validator_to_group.get(statement_validator_index) else { | ||
gum::debug!( | ||
target: LOG_TARGET, | ||
?group_rotation_info, | ||
?statement, | ||
?validator_to_group, | ||
n_cores = ?cores.len() , | ||
?candidate_hash, | ||
"Invalid validator index: {:?}", | ||
statement_validator_index | ||
); | ||
return None | ||
}; | ||
|
||
if let StatementWithPVD::Seconded(candidate, _pvd) = statement.payload() { | ||
let candidate_para_id = candidate.descriptor.para_id; | ||
let assigned_para_id = match &cores[core_index.0 as usize] { | ||
CoreState::Free => { | ||
gum::debug!(target: LOG_TARGET, ?candidate_hash, "Invalid CoreIndex, core is not assigned to any para_id"); | ||
return None | ||
}, | ||
CoreState::Occupied(occupied) => { | ||
if let Some(next) = &occupied.next_up_on_available { | ||
next.para_id | ||
} else { | ||
return None | ||
} | ||
}, | ||
CoreState::Scheduled(scheduled) => scheduled.para_id, | ||
}; | ||
// First check if the statement para id matches the core assignment. | ||
let core_index = group_rotation_info.core_for_group(*group_index, n_cores); | ||
|
||
if assigned_para_id != candidate_para_id { | ||
gum::debug!(target: LOG_TARGET, ?candidate_hash, ?core_index, ?assigned_para_id, ?candidate_para_id, "Invalid CoreIndex, core is assigned to a different para_id"); | ||
return None | ||
} | ||
return Some(core_index) | ||
if core_index.0 as usize > n_cores { | ||
gum::warn!(target: LOG_TARGET, ?candidate_hash, ?core_index, n_cores, "Invalid CoreIndex"); | ||
return None | ||
} | ||
|
||
if let StatementWithPVD::Seconded(candidate, _pvd) = statement.payload() { | ||
let candidate_para_id = candidate.descriptor.para_id; | ||
let assigned_para_id = match &cores[core_index.0 as usize] { | ||
CoreState::Free => { | ||
gum::debug!(target: LOG_TARGET, ?candidate_hash, "Invalid CoreIndex, core is not assigned to any para_id"); | ||
return None | ||
}, | ||
CoreState::Occupied(occupied) => | ||
if let Some(next) = &occupied.next_up_on_available { | ||
next.para_id | ||
} else { | ||
return Some(core_index) | ||
} | ||
} | ||
return None | ||
}, | ||
CoreState::Scheduled(scheduled) => scheduled.para_id, | ||
}; | ||
|
||
if assigned_para_id != candidate_para_id { | ||
gum::debug!( | ||
target: LOG_TARGET, | ||
?candidate_hash, | ||
?core_index, | ||
?assigned_para_id, | ||
?candidate_para_id, | ||
"Invalid CoreIndex, core is assigned to a different para_id" | ||
); | ||
return None | ||
} | ||
return Some(core_index) | ||
} else { | ||
return Some(core_index) | ||
} | ||
|
||
None | ||
} | ||
|
||
/// Load the data necessary to do backing work on top of a relay-parent. | ||
|
@@ -1077,6 +1108,10 @@ async fn construct_per_relay_parent_state<Context>( | |
ctx: &mut Context, | ||
relay_parent: Hash, | ||
keystore: &KeystorePtr, | ||
validator_to_group_cache: &mut LruMap< | ||
SessionIndex, | ||
IndexedVec<ValidatorIndex, Option<GroupIndex>>, | ||
>, | ||
mode: ProspectiveParachainsMode, | ||
) -> Result<Option<PerRelayParentState>, Error> { | ||
let parent = relay_parent; | ||
|
@@ -1172,7 +1207,21 @@ async fn construct_per_relay_parent_state<Context>( | |
groups.insert(core_index, g.clone()); | ||
} | ||
} | ||
gum::debug!(target: LOG_TARGET, ?groups, "TableContext" ); | ||
gum::debug!(target: LOG_TARGET, ?groups, "TableContext"); | ||
|
||
let validator_to_group = validator_to_group_cache | ||
.get_or_insert(session_index, || { | ||
let mut vector = vec![None; validators.len()]; | ||
|
||
for (group_idx, validator_group) in validator_groups.iter().enumerate() { | ||
for validator in validator_group { | ||
vector[validator.0 as usize] = Some(GroupIndex(group_idx as u32)); | ||
} | ||
} | ||
|
||
IndexedVec::<_, _>::from(vector) | ||
}) | ||
.expect("Just inserted"); | ||
|
||
let table_context = TableContext { validator, groups, validators, disabled_validators }; | ||
let table_config = TableConfig { | ||
|
@@ -1196,7 +1245,7 @@ async fn construct_per_relay_parent_state<Context>( | |
minimum_backing_votes, | ||
inject_core_index, | ||
cores, | ||
validator_groups, | ||
validator_to_group: validator_to_group.clone(), | ||
group_rotation_info, | ||
})) | ||
} | ||
|
@@ -1691,7 +1740,7 @@ async fn import_statement<Context>( | |
let stmt = primitive_statement_to_table(statement); | ||
|
||
let core = core_index_from_statement( | ||
&rp_state.validator_groups, | ||
&rp_state.validator_to_group, | ||
&rp_state.group_rotation_info, | ||
&rp_state.cores, | ||
statement, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it really necessary to copy this to the relay parent state? Can't we just keep the session index here and then fetch a reference from state directly where we need it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this being an LruMap, it makes it a bit harder to reason about the existence of the session index in the map.
And we can't store a mutable reference here and just populate the map when the session is not present because we'd need to hand out multiple mutable references.
Realistically, backing can only happen on the current session, right? So the LruMap doesn't need a capacity larger than 1 (so then, it doesn't even need to be a LruMap). Am I right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with current async backing code, we cannot start backing a candidate in the next session in advance. But we may want to keep this possibility, making the LruMap neccessary.
What I can do is wrap it in an Arc, because they don't need to modify it. Which would achieve the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done. I'll merge it once CI passes again