@@ -45,7 +45,8 @@ use frame_support::{pallet_prelude::*, traits::Defensive};
45
45
use frame_system:: pallet_prelude:: BlockNumberFor ;
46
46
pub use polkadot_core_primitives:: v2:: BlockNumber ;
47
47
use polkadot_primitives:: {
48
- CoreIndex , GroupIndex , GroupRotationInfo , Id as ParaId , ScheduledCore , ValidatorIndex ,
48
+ CoreIndex , GroupIndex , GroupRotationInfo , Id as ParaId , ScheduledCore , SchedulerParams ,
49
+ ValidatorIndex ,
49
50
} ;
50
51
use sp_runtime:: traits:: One ;
51
52
@@ -131,7 +132,7 @@ impl<T: Config> Pallet<T> {
131
132
pub ( crate ) fn initializer_on_new_session (
132
133
notification : & SessionChangeNotification < BlockNumberFor < T > > ,
133
134
) {
134
- let SessionChangeNotification { validators, new_config, prev_config , .. } = notification;
135
+ let SessionChangeNotification { validators, new_config, .. } = notification;
135
136
let config = new_config;
136
137
let assigner_cores = config. scheduler_params . num_cores ;
137
138
@@ -186,7 +187,7 @@ impl<T: Config> Pallet<T> {
186
187
}
187
188
188
189
// Resize and populate claim queue.
189
- Self :: maybe_resize_claim_queue ( prev_config . scheduler_params . num_cores , assigner_cores ) ;
190
+ Self :: maybe_resize_claim_queue ( ) ;
190
191
Self :: populate_claim_queue_after_session_change ( ) ;
191
192
192
193
let now = frame_system:: Pallet :: < T > :: block_number ( ) + One :: one ( ) ;
@@ -203,6 +204,12 @@ impl<T: Config> Pallet<T> {
203
204
ValidatorGroups :: < T > :: decode_len ( ) . unwrap_or ( 0 )
204
205
}
205
206
207
+ /// Expected claim queue len. Can be different than the real length if for example we don't have
208
+ /// assignments for a core.
209
+ fn expected_claim_queue_len ( config : & SchedulerParams < BlockNumberFor < T > > ) -> u32 {
210
+ core:: cmp:: min ( config. num_cores , Self :: num_availability_cores ( ) as u32 )
211
+ }
212
+
206
213
/// Get the group assigned to a specific core by index at the current block number. Result
207
214
/// undefined if the core index is unknown or the block number is less than the session start
208
215
/// index.
@@ -325,11 +332,11 @@ impl<T: Config> Pallet<T> {
325
332
/// and fill the queue from the assignment provider.
326
333
pub ( crate ) fn advance_claim_queue ( except_for : & BTreeSet < CoreIndex > ) {
327
334
let config = configuration:: ActiveConfig :: < T > :: get ( ) ;
328
- let num_assigner_cores = config. scheduler_params . num_cores ;
335
+ let expected_claim_queue_len = Self :: expected_claim_queue_len ( & config. scheduler_params ) ;
329
336
// Extra sanity, config should already never be smaller than 1:
330
337
let n_lookahead = config. scheduler_params . lookahead . max ( 1 ) ;
331
338
332
- for core_idx in 0 ..num_assigner_cores {
339
+ for core_idx in 0 ..expected_claim_queue_len {
333
340
let core_idx = CoreIndex :: from ( core_idx) ;
334
341
335
342
if !except_for. contains ( & core_idx) {
@@ -345,13 +352,16 @@ impl<T: Config> Pallet<T> {
345
352
}
346
353
347
354
// on new session
348
- fn maybe_resize_claim_queue ( old_core_count : u32 , new_core_count : u32 ) {
349
- if new_core_count < old_core_count {
355
+ fn maybe_resize_claim_queue ( ) {
356
+ let cq = ClaimQueue :: < T > :: get ( ) ;
357
+ let Some ( ( old_max_core, _) ) = cq. last_key_value ( ) else { return } ;
358
+ let config = configuration:: ActiveConfig :: < T > :: get ( ) ;
359
+ let new_core_count = Self :: expected_claim_queue_len ( & config. scheduler_params ) ;
360
+
361
+ if new_core_count < ( old_max_core. 0 + 1 ) {
350
362
ClaimQueue :: < T > :: mutate ( |cq| {
351
- let to_remove: Vec < _ > = cq
352
- . range ( CoreIndex ( new_core_count) ..CoreIndex ( old_core_count) )
353
- . map ( |( k, _) | * k)
354
- . collect ( ) ;
363
+ let to_remove: Vec < _ > =
364
+ cq. range ( CoreIndex ( new_core_count) ..=* old_max_core) . map ( |( k, _) | * k) . collect ( ) ;
355
365
for key in to_remove {
356
366
if let Some ( dropped_assignments) = cq. remove ( & key) {
357
367
Self :: push_back_to_assignment_provider ( dropped_assignments. into_iter ( ) ) ;
@@ -367,9 +377,9 @@ impl<T: Config> Pallet<T> {
367
377
let config = configuration:: ActiveConfig :: < T > :: get ( ) ;
368
378
// Extra sanity, config should already never be smaller than 1:
369
379
let n_lookahead = config. scheduler_params . lookahead . max ( 1 ) ;
370
- let new_core_count = config. scheduler_params . num_cores ;
380
+ let expected_claim_queue_len = Self :: expected_claim_queue_len ( & config. scheduler_params ) ;
371
381
372
- for core_idx in 0 ..new_core_count {
382
+ for core_idx in 0 ..expected_claim_queue_len {
373
383
let core_idx = CoreIndex :: from ( core_idx) ;
374
384
Self :: fill_claim_queue ( core_idx, n_lookahead) ;
375
385
}
0 commit comments