-
Notifications
You must be signed in to change notification settings - Fork 2.6k
grandpa: cleanup stale entries in set id session mapping #13237
Changes from 1 commit
bcc6c9a
0cca7c1
5b07212
50b175d
7f3b301
ac6caad
cb3fadb
0598c70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,5 +15,53 @@ | |
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use frame_support::{ | ||
| traits::{Get, OnRuntimeUpgrade}, | ||
| weights::Weight, | ||
| }; | ||
| use sp_runtime::SaturatedConversion; | ||
|
|
||
| use crate::{Config, CurrentSetId, SetIdSession, LOG_TARGET}; | ||
|
|
||
| /// Version 4. | ||
| pub mod v4; | ||
|
|
||
| /// This migration will clean up all stale set id -> session entries from the | ||
| /// `SetIdSession` storage map, only the latest `max_set_id_session_entries` | ||
| /// will be kept. | ||
| /// | ||
| /// This migration should be added with a runtime upgrade that introduces the | ||
| /// `MaxSetIdSessionEntries` constant to the pallet (although it could also be | ||
| /// done later on). | ||
| pub struct CleanupSetIdSessionMap<T>(sp_std::marker::PhantomData<T>); | ||
| impl<T: Config> OnRuntimeUpgrade for CleanupSetIdSessionMap<T> { | ||
| fn on_runtime_upgrade() -> Weight { | ||
| // NOTE: since this migration will loop over all stale entries in the | ||
| // map we need to set some cutoff value, otherwise the migration might | ||
| // take too long to run. for scenarios where there are that many entries | ||
| // to cleanup a multiblock migration will be needed instead. | ||
| if CurrentSetId::<T>::get() > 25_000 { | ||
|
Comment on lines
+38
to
+42
Member
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. Just curious. In this case what is the strategy to cleanup the leftovers? Also at this point can't we at least use a best effort approach and cleanup the first Another idea/question (not a super expert of runtime upgrades... but I assume it can work)
Contributor
Author
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. I just put this (random) cut-off here to prevent any users from shooting themselves in the foot. By "multi-block" migration I mean exactly what you suggested, just keep track of the current state of cleanup and remove N entries every block until it's finished. The reason I didn't implement this is because we don't need it for any of our networks and I figured it was unnecessary to add the extra complexity. I put this note here in case any other user of substrate needs to implement this (TBH even 25000 entries might be fast enough to do in a single block, I didn't test this as our limits are way lower than this ~6500).
Member
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. Got it! Thank you |
||
| log::warn!( | ||
| target: LOG_TARGET, | ||
| "CleanupSetIdSessionMap migration was aborted since there are too many entries to cleanup." | ||
| ); | ||
|
|
||
| return T::DbWeight::get().reads(1) | ||
| } | ||
|
|
||
| cleanup_set_id_sesion_map::<T>() | ||
| } | ||
| } | ||
|
|
||
| fn cleanup_set_id_sesion_map<T: Config>() -> Weight { | ||
| let until_set_id = | ||
| CurrentSetId::<T>::get().saturating_sub(T::MaxSetIdSessionEntries::get() as u64); | ||
|
andresilva marked this conversation as resolved.
Outdated
|
||
|
|
||
| for set_id in 0..until_set_id { | ||
| SetIdSession::<T>::remove(set_id); | ||
| } | ||
|
|
||
| T::DbWeight::get() | ||
| .reads(1) | ||
| .saturating_add(T::DbWeight::get().writes((0..until_set_id).count().saturated_into())) | ||
|
andresilva marked this conversation as resolved.
Outdated
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.