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

Commit

Permalink
Managing a configuration for gc of overlay. This commit will be revert
Browse files Browse the repository at this point in the history
(static value makes more sense for the time being).
  • Loading branch information
cheme committed Aug 5, 2019
1 parent 21aa65d commit c8938d3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 19 deletions.
4 changes: 4 additions & 0 deletions core/client/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use sr_primitives::{generic::BlockId, Justification, StorageOverlay, ChildrenSto
use sr_primitives::traits::{Block as BlockT, NumberFor};
use state_machine::backend::Backend as StateBackend;
use state_machine::ChangesTrieStorage as StateChangesTrieStorage;
use state_machine::OverlayedSettings;
use consensus::well_known_cache_keys;
use hash_db::Hasher;
use trie::MemoryDB;
Expand Down Expand Up @@ -196,6 +197,9 @@ pub trait Backend<Block, H>: AuxStore + Send + Sync where
/// something that the import of a block would interfere with, e.g. importing
/// a new block or calculating the best head.
fn get_import_lock(&self) -> &Mutex<()>;

/// Settings to use with overlay.
fn overlay_settings(&self) -> OverlayedSettings;
}

/// Offchain workers local storage.
Expand Down
1 change: 1 addition & 0 deletions core/client/src/call_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ where
side_effects_handler: Option<&mut O>,
) -> error::Result<Vec<u8>> {
let mut changes = OverlayedChanges::default();
changes.change_settings(self.backend.overlay_settings());
let state = self.backend.state_at(*id)?;
let return_data = state_machine::new(
&state,
Expand Down
1 change: 1 addition & 0 deletions core/state-machine/src/changes_trie/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ mod test {
])),
].into_iter().collect(),
},
settings: Default::default(),
operation_from_last_gc: 0,
};

Expand Down
1 change: 1 addition & 0 deletions core/state-machine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ mod tests {
])),
].into_iter().collect(),
},
settings: Default::default(),
operation_from_last_gc: 0,
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/state-machine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub use changes_trie::{
prune as prune_changes_tries,
oldest_non_pruned_trie as oldest_non_pruned_changes_trie
};
pub use overlayed_changes::OverlayedChanges;
pub use overlayed_changes::{OverlayedChanges, OverlayedSettings};
pub use proving_backend::{
create_proof_check_backend, create_proof_check_backend_storage,
Recorder as ProofRecorder, ProvingBackend,
Expand Down
64 changes: 46 additions & 18 deletions core/state-machine/src/overlayed_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,42 @@ pub(crate) enum TransactionState {
}


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

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

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

#[derive(Debug, Clone)]
/// Settings regarding overlayed changes internal (mainly gc).
pub struct OverlayedSettings {
/// Treshold of operation before running a garbage colletion
/// on a transaction operation.
/// Should be same as `TRIGGER_COMMIT_GC` or higher
/// (we most likely do not want lower as transaction are
/// possibly more frequent than commit).
pub trigger_transaction_gc: usize,
/// Treshold of operation before running a garbage colletion
/// on a commit operation.
/// We may want a lower value than for a transaction, even
/// a 1 if we want to do it between every operation.
pub trigger_commit_gc: usize,
/// Used to count big content as multiple operation.
/// This is a number of octet.
/// Set to 0 to ignore.
pub add_content_size_unit: usize,
}

impl Default for OverlayedSettings {
fn default() -> Self {
OverlayedSettings {
trigger_transaction_gc: TRIGGER_TRANSACTION_GC,
trigger_commit_gc: TRIGGER_COMMIT_GC,
add_content_size_unit: ADD_CONTENT_SIZE_UNIT,
}
}
}

/// The overlayed changes to state to be queried on top of the backend.
///
/// A transaction shares all prospective changes within an inner overlay
Expand All @@ -74,6 +92,8 @@ pub struct OverlayedChanges {
/// Changes trie configuration. None by default, but could be installed by the
/// runtime if it supports change tries.
pub(crate) changes_trie_config: Option<ChangesTrieConfig>,
/// Settings regarding overlayed changes internal (mainly gc).
pub(crate) settings: OverlayedSettings,
/// Counter of number of operation between garbage collection.
/// Add or delete cost one, additional cost per size by counting a fix size
/// as a unit.
Expand Down Expand Up @@ -652,6 +672,13 @@ impl OverlayedChangeSet {
}

impl OverlayedChanges {

/// Change overlayed change settings.
pub fn change_settings(&mut self, settings: OverlayedSettings) {
self.settings = settings;
}


/// Whether the overlayed changes are empty.
pub fn is_empty(&self) -> bool {
self.changes.is_empty()
Expand Down Expand Up @@ -707,8 +734,8 @@ impl OverlayedChanges {
}

fn add_cost_op(&mut self, val: &Option<Vec<u8>>) {
let content_cost = if ADD_CONTENT_SIZE_UNIT > 0 {
val.as_ref().map(|s| s.len() / ADD_CONTENT_SIZE_UNIT).unwrap_or(0)
let content_cost = if self.settings.add_content_size_unit > 0 {
val.as_ref().map(|s| s.len() / self.settings.add_content_size_unit).unwrap_or(0)
} else { 0 };
self.operation_from_last_gc += 1 + content_cost;
}
Expand Down Expand Up @@ -772,7 +799,7 @@ impl OverlayedChanges {
/// Discard prospective changes to state.
pub fn discard_prospective(&mut self) {
self.changes.discard_prospective();
if self.operation_from_last_gc > TRIGGER_COMMIT_GC {
if self.operation_from_last_gc > self.settings.trigger_commit_gc {
self.operation_from_last_gc = 0;
self.gc(true);
}
Expand All @@ -781,7 +808,7 @@ impl OverlayedChanges {
/// Commit prospective changes to state.
pub fn commit_prospective(&mut self) {
self.changes.commit_prospective();
if self.operation_from_last_gc > TRIGGER_COMMIT_GC {
if self.operation_from_last_gc > self.settings.trigger_commit_gc {
self.operation_from_last_gc = 0;
self.gc(true);
}
Expand All @@ -790,7 +817,7 @@ impl OverlayedChanges {
/// Create a new transactional layer.
pub fn start_transaction(&mut self) {
self.changes.start_transaction();
if self.operation_from_last_gc > TRIGGER_TRANSACTION_GC {
if self.operation_from_last_gc > self.settings.trigger_transaction_gc {
self.operation_from_last_gc = 0;
self.gc(true);
}
Expand All @@ -800,7 +827,7 @@ impl OverlayedChanges {
/// A transaction is always running (history always end with pending).
pub fn discard_transaction(&mut self) {
self.changes.discard_transaction();
if self.operation_from_last_gc > TRIGGER_TRANSACTION_GC {
if self.operation_from_last_gc > self.settings.trigger_transaction_gc {
self.operation_from_last_gc = 0;
self.gc(true);
}
Expand Down Expand Up @@ -859,6 +886,7 @@ impl OverlayedChanges {
let mut result = OverlayedChanges {
changes,
changes_trie_config,
settings: Default::default(),
operation_from_last_gc: 0,
};
committed.into_iter().for_each(|(k, v)| result.set_storage(k, v));
Expand Down

0 comments on commit c8938d3

Please sign in to comment.