Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit c8938d3

Browse files
committed
Managing a configuration for gc of overlay. This commit will be revert
(static value makes more sense for the time being).
1 parent 21aa65d commit c8938d3

File tree

6 files changed

+54
-19
lines changed

6 files changed

+54
-19
lines changed

core/client/src/backend.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use sr_primitives::{generic::BlockId, Justification, StorageOverlay, ChildrenSto
2323
use sr_primitives::traits::{Block as BlockT, NumberFor};
2424
use state_machine::backend::Backend as StateBackend;
2525
use state_machine::ChangesTrieStorage as StateChangesTrieStorage;
26+
use state_machine::OverlayedSettings;
2627
use consensus::well_known_cache_keys;
2728
use hash_db::Hasher;
2829
use trie::MemoryDB;
@@ -196,6 +197,9 @@ pub trait Backend<Block, H>: AuxStore + Send + Sync where
196197
/// something that the import of a block would interfere with, e.g. importing
197198
/// a new block or calculating the best head.
198199
fn get_import_lock(&self) -> &Mutex<()>;
200+
201+
/// Settings to use with overlay.
202+
fn overlay_settings(&self) -> OverlayedSettings;
199203
}
200204

201205
/// Offchain workers local storage.

core/client/src/call_executor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ where
188188
side_effects_handler: Option<&mut O>,
189189
) -> error::Result<Vec<u8>> {
190190
let mut changes = OverlayedChanges::default();
191+
changes.change_settings(self.backend.overlay_settings());
191192
let state = self.backend.state_at(*id)?;
192193
let return_data = state_machine::new(
193194
&state,

core/state-machine/src/changes_trie/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ mod test {
259259
])),
260260
].into_iter().collect(),
261261
},
262+
settings: Default::default(),
262263
operation_from_last_gc: 0,
263264
};
264265

core/state-machine/src/ext.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ mod tests {
380380
])),
381381
].into_iter().collect(),
382382
},
383+
settings: Default::default(),
383384
operation_from_last_gc: 0,
384385
}
385386
}

core/state-machine/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub use changes_trie::{
5252
prune as prune_changes_tries,
5353
oldest_non_pruned_trie as oldest_non_pruned_changes_trie
5454
};
55-
pub use overlayed_changes::OverlayedChanges;
55+
pub use overlayed_changes::{OverlayedChanges, OverlayedSettings};
5656
pub use proving_backend::{
5757
create_proof_check_backend, create_proof_check_backend_storage,
5858
Recorder as ProofRecorder, ProvingBackend,

core/state-machine/src/overlayed_changes.rs

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,42 @@ pub(crate) enum TransactionState {
4545
}
4646

4747

48-
/// Treshold of operation before running a garbage colletion
49-
/// on a transaction operation.
50-
/// Should be same as `TRIGGER_COMMIT_GC` or higher
51-
/// (we most likely do not want lower as transaction are
52-
/// possibly more frequent than commit).
5348
const TRIGGER_TRANSACTION_GC: usize = 10_000;
5449

55-
/// Treshold of operation before running a garbage colletion
56-
/// on a commit operation.
57-
/// We may want a lower value than for a transaction, even
58-
/// a 1 if we want to do it between every operation.
5950
const TRIGGER_COMMIT_GC: usize = 1_000;
6051

61-
/// Used to count big content as multiple operation.
62-
/// This is a number of octet.
63-
/// Set to 0 to ignore.
6452
const ADD_CONTENT_SIZE_UNIT: usize = 64;
6553

54+
#[derive(Debug, Clone)]
55+
/// Settings regarding overlayed changes internal (mainly gc).
56+
pub struct OverlayedSettings {
57+
/// Treshold of operation before running a garbage colletion
58+
/// on a transaction operation.
59+
/// Should be same as `TRIGGER_COMMIT_GC` or higher
60+
/// (we most likely do not want lower as transaction are
61+
/// possibly more frequent than commit).
62+
pub trigger_transaction_gc: usize,
63+
/// Treshold of operation before running a garbage colletion
64+
/// on a commit operation.
65+
/// We may want a lower value than for a transaction, even
66+
/// a 1 if we want to do it between every operation.
67+
pub trigger_commit_gc: usize,
68+
/// Used to count big content as multiple operation.
69+
/// This is a number of octet.
70+
/// Set to 0 to ignore.
71+
pub add_content_size_unit: usize,
72+
}
73+
74+
impl Default for OverlayedSettings {
75+
fn default() -> Self {
76+
OverlayedSettings {
77+
trigger_transaction_gc: TRIGGER_TRANSACTION_GC,
78+
trigger_commit_gc: TRIGGER_COMMIT_GC,
79+
add_content_size_unit: ADD_CONTENT_SIZE_UNIT,
80+
}
81+
}
82+
}
83+
6684
/// The overlayed changes to state to be queried on top of the backend.
6785
///
6886
/// A transaction shares all prospective changes within an inner overlay
@@ -74,6 +92,8 @@ pub struct OverlayedChanges {
7492
/// Changes trie configuration. None by default, but could be installed by the
7593
/// runtime if it supports change tries.
7694
pub(crate) changes_trie_config: Option<ChangesTrieConfig>,
95+
/// Settings regarding overlayed changes internal (mainly gc).
96+
pub(crate) settings: OverlayedSettings,
7797
/// Counter of number of operation between garbage collection.
7898
/// Add or delete cost one, additional cost per size by counting a fix size
7999
/// as a unit.
@@ -652,6 +672,13 @@ impl OverlayedChangeSet {
652672
}
653673

654674
impl OverlayedChanges {
675+
676+
/// Change overlayed change settings.
677+
pub fn change_settings(&mut self, settings: OverlayedSettings) {
678+
self.settings = settings;
679+
}
680+
681+
655682
/// Whether the overlayed changes are empty.
656683
pub fn is_empty(&self) -> bool {
657684
self.changes.is_empty()
@@ -707,8 +734,8 @@ impl OverlayedChanges {
707734
}
708735

709736
fn add_cost_op(&mut self, val: &Option<Vec<u8>>) {
710-
let content_cost = if ADD_CONTENT_SIZE_UNIT > 0 {
711-
val.as_ref().map(|s| s.len() / ADD_CONTENT_SIZE_UNIT).unwrap_or(0)
737+
let content_cost = if self.settings.add_content_size_unit > 0 {
738+
val.as_ref().map(|s| s.len() / self.settings.add_content_size_unit).unwrap_or(0)
712739
} else { 0 };
713740
self.operation_from_last_gc += 1 + content_cost;
714741
}
@@ -772,7 +799,7 @@ impl OverlayedChanges {
772799
/// Discard prospective changes to state.
773800
pub fn discard_prospective(&mut self) {
774801
self.changes.discard_prospective();
775-
if self.operation_from_last_gc > TRIGGER_COMMIT_GC {
802+
if self.operation_from_last_gc > self.settings.trigger_commit_gc {
776803
self.operation_from_last_gc = 0;
777804
self.gc(true);
778805
}
@@ -781,7 +808,7 @@ impl OverlayedChanges {
781808
/// Commit prospective changes to state.
782809
pub fn commit_prospective(&mut self) {
783810
self.changes.commit_prospective();
784-
if self.operation_from_last_gc > TRIGGER_COMMIT_GC {
811+
if self.operation_from_last_gc > self.settings.trigger_commit_gc {
785812
self.operation_from_last_gc = 0;
786813
self.gc(true);
787814
}
@@ -790,7 +817,7 @@ impl OverlayedChanges {
790817
/// Create a new transactional layer.
791818
pub fn start_transaction(&mut self) {
792819
self.changes.start_transaction();
793-
if self.operation_from_last_gc > TRIGGER_TRANSACTION_GC {
820+
if self.operation_from_last_gc > self.settings.trigger_transaction_gc {
794821
self.operation_from_last_gc = 0;
795822
self.gc(true);
796823
}
@@ -800,7 +827,7 @@ impl OverlayedChanges {
800827
/// A transaction is always running (history always end with pending).
801828
pub fn discard_transaction(&mut self) {
802829
self.changes.discard_transaction();
803-
if self.operation_from_last_gc > TRIGGER_TRANSACTION_GC {
830+
if self.operation_from_last_gc > self.settings.trigger_transaction_gc {
804831
self.operation_from_last_gc = 0;
805832
self.gc(true);
806833
}
@@ -859,6 +886,7 @@ impl OverlayedChanges {
859886
let mut result = OverlayedChanges {
860887
changes,
861888
changes_trie_config,
889+
settings: Default::default(),
862890
operation_from_last_gc: 0,
863891
};
864892
committed.into_iter().for_each(|(k, v)| result.set_storage(k, v));

0 commit comments

Comments
 (0)