Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix claim queue size #6257

Merged
merged 7 commits into from
Nov 4, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions polkadot/runtime/parachains/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use frame_system::pallet_prelude::BlockNumberFor;
pub use polkadot_core_primitives::v2::BlockNumber;
use polkadot_primitives::{
CoreIndex, GroupIndex, GroupRotationInfo, Id as ParaId, ScheduledCore, ValidatorIndex,
SchedulerParams,
};
use sp_runtime::traits::One;

Expand Down Expand Up @@ -131,7 +132,7 @@ impl<T: Config> Pallet<T> {
pub(crate) fn initializer_on_new_session(
notification: &SessionChangeNotification<BlockNumberFor<T>>,
) {
let SessionChangeNotification { validators, new_config, prev_config, .. } = notification;
let SessionChangeNotification { validators, new_config, .. } = notification;
let config = new_config;
let assigner_cores = config.scheduler_params.num_cores;

Expand Down Expand Up @@ -186,7 +187,7 @@ impl<T: Config> Pallet<T> {
}

// Resize and populate claim queue.
Self::maybe_resize_claim_queue(prev_config.scheduler_params.num_cores, assigner_cores);
Self::maybe_resize_claim_queue();
Self::populate_claim_queue_after_session_change();

let now = frame_system::Pallet::<T>::block_number() + One::one();
Expand All @@ -203,6 +204,14 @@ impl<T: Config> Pallet<T> {
ValidatorGroups::<T>::decode_len().unwrap_or(0)
}

/// Claim queue len
fn expected_claim_queue_len(config: &SchedulerParams<BlockNumberFor<T>>) -> u32 {
sandreim marked this conversation as resolved.
Show resolved Hide resolved
core::cmp::min(
config.num_cores,
Self::num_availability_cores() as u32
)
}

/// Get the group assigned to a specific core by index at the current block number. Result
/// undefined if the core index is unknown or the block number is less than the session start
/// index.
Expand Down Expand Up @@ -325,11 +334,11 @@ impl<T: Config> Pallet<T> {
/// and fill the queue from the assignment provider.
pub(crate) fn advance_claim_queue(except_for: &BTreeSet<CoreIndex>) {
let config = configuration::ActiveConfig::<T>::get();
let num_assigner_cores = config.scheduler_params.num_cores;
let expected_claim_queue_len = Self::expected_claim_queue_len(&config.scheduler_params);
// Extra sanity, config should already never be smaller than 1:
let n_lookahead = config.scheduler_params.lookahead.max(1);

for core_idx in 0..num_assigner_cores {
for core_idx in 0..expected_claim_queue_len {
let core_idx = CoreIndex::from(core_idx);

if !except_for.contains(&core_idx) {
Expand All @@ -345,11 +354,16 @@ impl<T: Config> Pallet<T> {
}

// on new session
fn maybe_resize_claim_queue(old_core_count: u32, new_core_count: u32) {
if new_core_count < old_core_count {
fn maybe_resize_claim_queue() {
let cq = ClaimQueue::<T>::get();
let Some((old_max_core, _)) = cq.last_key_value() else {return};
let config = configuration::ActiveConfig::<T>::get();
let new_core_count = Self::expected_claim_queue_len(&config.scheduler_params);

if new_core_count < (old_max_core.0 + 1) {
sandreim marked this conversation as resolved.
Show resolved Hide resolved
ClaimQueue::<T>::mutate(|cq| {
let to_remove: Vec<_> = cq
.range(CoreIndex(new_core_count)..CoreIndex(old_core_count))
.range(CoreIndex(new_core_count)..=*old_max_core)
.map(|(k, _)| *k)
.collect();
for key in to_remove {
Expand All @@ -367,9 +381,9 @@ impl<T: Config> Pallet<T> {
let config = configuration::ActiveConfig::<T>::get();
// Extra sanity, config should already never be smaller than 1:
let n_lookahead = config.scheduler_params.lookahead.max(1);
let new_core_count = config.scheduler_params.num_cores;
let expected_claim_queue_len = Self::expected_claim_queue_len(&config.scheduler_params);

for core_idx in 0..new_core_count {
for core_idx in 0..expected_claim_queue_len {
let core_idx = CoreIndex::from(core_idx);
Self::fill_claim_queue(core_idx, n_lookahead);
}
Expand Down
Loading