Skip to content

Commit

Permalink
[feature] #4059: introduce snapshot "mode" (#4365)
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Balashov <43530070+0x009922@users.noreply.github.com>
  • Loading branch information
0x009922 authored Mar 14, 2024
1 parent be0e67f commit 8d0157a
Show file tree
Hide file tree
Showing 16 changed files with 297 additions and 136 deletions.
52 changes: 31 additions & 21 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use iroha_core::{
query::store::LiveQueryStore,
queue::Queue,
smartcontracts::isi::Registrable as _,
snapshot::{try_read_snapshot, SnapshotMaker, SnapshotMakerHandle},
snapshot::{
try_read_snapshot, SnapshotMaker, SnapshotMakerHandle, TryReadError as TryReadSnapshotError,
},
sumeragi::{SumeragiHandle, SumeragiStartArgs},
IrohaNetwork,
};
Expand Down Expand Up @@ -57,8 +59,8 @@ pub struct Iroha {
pub kura: Arc<Kura>,
/// Torii web server
pub torii: Option<Torii>,
/// Snapshot service
pub snapshot_maker: SnapshotMakerHandle,
/// Snapshot service. Might be not started depending on the config.
pub snapshot_maker: Option<SnapshotMakerHandle>,
/// Thread handlers
thread_handlers: Vec<ThreadHandler>,

Expand Down Expand Up @@ -215,30 +217,37 @@ impl Iroha {
let live_query_store_handle = LiveQueryStore::from_config(config.live_query_store).start();

let block_count = kura.init()?;
let wsv = try_read_snapshot(

let wsv = match try_read_snapshot(
&config.snapshot.store_dir,
&kura,
live_query_store_handle.clone(),
block_count,
)
.map_or_else(
|error| {
iroha_logger::warn!(%error, "Failed to load wsv from snapshot, creating empty wsv");
WorldStateView::from_config(
config.chain_wide,
world,
Arc::clone(&kura),
live_query_store_handle.clone(),
)
},
|wsv| {
) {
Ok(wsv) => {
iroha_logger::info!(
at_height = wsv.height(),
"Successfully loaded wsv from snapshot"
"Successfully loaded WSV from a snapshot"
);
wsv
},
);
Some(wsv)
}
Err(TryReadSnapshotError::NotFound) => {
iroha_logger::info!("Didn't find a snapshot of WSV, creating an empty one");
None
}
Err(error) => {
iroha_logger::warn!(%error, "Failed to load WSV from a snapshot, creating an empty one");
None
}
}.unwrap_or_else(|| {
WorldStateView::from_config(
config.chain_wide,
world,
Arc::clone(&kura),
live_query_store_handle.clone(),
)

});

let queue = Arc::new(Queue::from_config(config.queue));
match Self::start_telemetry(&logger, &config).await? {
Expand Down Expand Up @@ -298,7 +307,8 @@ impl Iroha {
}
.start();

let snapshot_maker = SnapshotMaker::from_config(&config.snapshot, sumeragi.clone()).start();
let snapshot_maker =
SnapshotMaker::from_config(&config.snapshot, &sumeragi).map(SnapshotMaker::start);

let kiso = KisoHandle::new(config.clone());

Expand Down
12 changes: 6 additions & 6 deletions config/src/kura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use serde_with::{DeserializeFromStr, SerializeDisplay};
SerializeDisplay,
)]
#[strum(serialize_all = "snake_case")]
pub enum Mode {
pub enum InitMode {
/// Strict validation of all blocks.
#[default]
Strict,
Expand All @@ -28,13 +28,13 @@ pub enum Mode {

#[cfg(test)]
mod tests {
use crate::kura::Mode;
use crate::kura::InitMode;

#[test]
fn init_mode_display_reprs() {
assert_eq!(format!("{}", Mode::Strict), "strict");
assert_eq!(format!("{}", Mode::Fast), "fast");
assert_eq!("strict".parse::<Mode>().unwrap(), Mode::Strict);
assert_eq!("fast".parse::<Mode>().unwrap(), Mode::Fast);
assert_eq!(format!("{}", InitMode::Strict), "strict");
assert_eq!(format!("{}", InitMode::Fast), "fast");
assert_eq!("strict".parse::<InitMode>().unwrap(), InitMode::Strict);
assert_eq!("fast".parse::<InitMode>().unwrap(), InitMode::Fast);
}
}
1 change: 1 addition & 0 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod client_api;
pub mod kura;
pub mod logger;
pub mod parameters;
pub mod snapshot;
4 changes: 2 additions & 2 deletions config/src/parameters/actual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use url::Url;
pub use user::{Logger, Queue, Snapshot};

use crate::{
kura::Mode,
kura::InitMode,
parameters::{
defaults, user,
user::{CliContext, RootPartial},
Expand Down Expand Up @@ -119,7 +119,7 @@ impl Genesis {
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct Kura {
pub init_mode: Mode,
pub init_mode: InitMode,
pub store_dir: PathBuf,
pub debug_output_new_blocks: bool,
}
Expand Down
3 changes: 1 addition & 2 deletions config/src/parameters/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ pub mod snapshot {
use super::*;

pub const DEFAULT_STORE_DIR: &str = "./storage/snapshot";
// Default frequency of making snapshots is 1 minute, need to be adjusted for larger world state view size
// The default frequency of making snapshots is 1 minute, need to be adjusted for larger world state view size
pub const DEFAULT_CREATE_EVERY: Duration = Duration::from_secs(60);
pub const DEFAULT_ENABLED: bool = true;
}

pub mod chain_wide {
Expand Down
17 changes: 9 additions & 8 deletions config/src/parameters/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ use iroha_primitives::{addr::SocketAddr, unique_vec::UniqueVec};
use url::Url;

use crate::{
kura::Mode,
logger::Format,
kura::InitMode as KuraInitMode,
logger::Format as LoggerFormat,
parameters::{actual, defaults::telemetry::*},
snapshot::Mode as SnapshotMode,
};

mod boilerplate;
Expand Down Expand Up @@ -343,7 +344,7 @@ pub enum GenesisConfigError {

#[derive(Debug)]
pub struct Kura {
pub init_mode: Mode,
pub init_mode: KuraInitMode,
pub store_dir: PathBuf,
pub debug: KuraDebug,
}
Expand Down Expand Up @@ -469,7 +470,7 @@ pub struct Logger {
// looks inconsistent
pub level: Level,
/// Output format
pub format: Format,
pub format: LoggerFormat,
#[cfg(feature = "tokio-console")]
/// Address of tokio console (only available under "tokio-console" feature)
pub tokio_console_address: SocketAddr,
Expand All @@ -480,7 +481,7 @@ impl Default for Logger {
fn default() -> Self {
Self {
level: Level::default(),
format: Format::default(),
format: LoggerFormat::default(),
#[cfg(feature = "tokio-console")]
tokio_console_address: super::defaults::logger::DEFAULT_TOKIO_CONSOLE_ADDR,
}
Expand Down Expand Up @@ -541,9 +542,9 @@ impl Telemetry {

#[derive(Debug, Clone)]
pub struct Snapshot {
pub mode: SnapshotMode,
pub create_every: Duration,
pub store_dir: PathBuf,
pub creation_enabled: bool,
}

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -574,7 +575,7 @@ impl ChainWide {
asset_definition_metadata_limits,
account_metadata_limits,
domain_metadata_limits,
ident_length_limits: identifier_length_limits,
ident_length_limits,
executor_fuel_limit,
executor_max_memory,
wasm_fuel_limit,
Expand All @@ -590,7 +591,7 @@ impl ChainWide {
asset_definition_metadata_limits,
account_metadata_limits,
domain_metadata_limits,
ident_length_limits: identifier_length_limits,
ident_length_limits,
executor_runtime: actual::WasmRuntime {
fuel_limit: executor_fuel_limit,
max_memory_bytes: executor_max_memory.get(),
Expand Down
23 changes: 9 additions & 14 deletions config/src/parameters/user/boilerplate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use serde::{Deserialize, Serialize};
use url::Url;

use crate::{
kura::Mode,
kura::InitMode as KuraInitMode,
logger::Format,
parameters::{
defaults::{self, chain_wide::*, network::*, queue::*, torii::*},
Expand All @@ -36,6 +36,7 @@ use crate::{
SumeragiDebug, Telemetry, TelemetryDev, Torii,
},
},
snapshot::Mode as SnapshotMode,
};

#[derive(Deserialize, Serialize, Debug, Default, Merge)]
Expand Down Expand Up @@ -260,7 +261,7 @@ impl FromEnv for GenesisPartial {
#[derive(Clone, Deserialize, Serialize, Debug, Default, Merge)]
#[serde(deny_unknown_fields, default)]
pub struct KuraPartial {
pub init_mode: UserField<Mode>,
pub init_mode: UserField<KuraInitMode>,
pub store_dir: UserField<PathBuf>,
pub debug: KuraDebugPartial,
}
Expand Down Expand Up @@ -595,19 +596,17 @@ impl FromEnvDefaultFallback for TelemetryPartial {}
#[derive(Debug, Clone, Deserialize, Serialize, Default, Merge)]
#[serde(deny_unknown_fields, default)]
pub struct SnapshotPartial {
pub mode: UserField<SnapshotMode>,
pub create_every: UserField<HumanDuration>,
pub store_dir: UserField<PathBuf>,
pub creation_enabled: UserField<bool>,
}

impl UnwrapPartial for SnapshotPartial {
type Output = Snapshot;

fn unwrap_partial(self) -> UnwrapPartialResult<Self::Output> {
Ok(Snapshot {
creation_enabled: self
.creation_enabled
.unwrap_or(defaults::snapshot::DEFAULT_ENABLED),
mode: self.mode.unwrap_or_default(),
create_every: self
.create_every
.get()
Expand All @@ -627,26 +626,22 @@ impl FromEnv for SnapshotPartial {
{
let mut emitter = Emitter::new();

let mode =
ParseEnvResult::parse_simple(&mut emitter, env, "SNAPSHOT_MODE", "snapshot.mode")
.into();
let store_dir = ParseEnvResult::parse_simple(
&mut emitter,
env,
"SNAPSHOT_STORE_DIR",
"snapshot.store_dir",
)
.into();
let creation_enabled = ParseEnvResult::parse_simple(
&mut emitter,
env,
"SNAPSHOT_CREATION_ENABLED",
"snapshot.creation_enabled",
)
.into();

emitter.finish()?;

Ok(Self {
mode,
store_dir,
creation_enabled,
..Self::default()
})
}
Expand Down
36 changes: 36 additions & 0 deletions config/src/snapshot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! Configuration related to Snapshot specifically

/// Functioning mode of the Snapshot Iroha module
#[derive(
Copy,
Clone,
Debug,
Default,
strum::Display,
strum::EnumString,
serde_with::SerializeDisplay,
serde_with::DeserializeFromStr,
)]
#[strum(serialize_all = "snake_case")]
pub enum Mode {
/// Read the snapshot on startup, update periodically
#[default]
ReadWrite,
/// Read the snapshot on startup, do not update
Readonly,
/// Do not read or write the snapshot
Disabled,
}

#[cfg(test)]
mod tests {
use crate::snapshot::Mode;

#[test]
fn mode_display_form() {
assert_eq!(
format!("{} {} {}", Mode::ReadWrite, Mode::Readonly, Mode::Disabled),
"read_write readonly disabled"
);
}
}
8 changes: 4 additions & 4 deletions config/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ fn minimal_config_snapshot() -> Result<()> {
future_threshold: 1s,
},
snapshot: Snapshot {
mode: ReadWrite,
create_every: 60s,
store_dir: "./storage/snapshot",
creation_enabled: true,
},
telemetry: None,
dev_telemetry: None,
Expand Down Expand Up @@ -367,13 +367,13 @@ fn full_envs_set_is_consumed() -> Result<()> {
future_threshold: None,
},
snapshot: SnapshotPartial {
mode: Some(
ReadWrite,
),
create_every: None,
store_dir: Some(
"/snapshot/path/from/env",
),
creation_enabled: Some(
false,
),
},
telemetry: TelemetryPartial {
name: None,
Expand Down
2 changes: 1 addition & 1 deletion config/tests/fixtures/full.env
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ KURA_STORE_DIR=/store/path/from/env
KURA_DEBUG_OUTPUT_NEW_BLOCKS=false
LOG_LEVEL=DEBUG
LOG_FORMAT=pretty
SNAPSHOT_MODE=read_write
SNAPSHOT_STORE_DIR=/snapshot/path/from/env
SNAPSHOT_CREATION_ENABLED=false
4 changes: 2 additions & 2 deletions config/tests/fixtures/full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ transaction_time_to_live = 100
future_threshold = 50

[snapshot]
creation_enabled = true
mode = "read_write"
create_every = 60_000
store_dir = "./storage/snapshot"

Expand All @@ -64,7 +64,7 @@ out_file = "./dev-telemetry.json5"
max_transactions_in_block = 512
block_time = 2_000
commit_time = 4_000
transaction_limits = {max_instruction_number = 4096, max_wasm_size_bytes = 4194304 }
transaction_limits = { max_instruction_number = 4096, max_wasm_size_bytes = 4194304 }
asset_metadata_limits = { capacity = 1048576, max_entry_len = 4096 }
asset_definition_metadata_limits = { capacity = 1048576, max_entry_len = 4096 }
account_metadata_limits = { capacity = 1048576, max_entry_len = 4096 }
Expand Down
2 changes: 1 addition & 1 deletion configs/peer.template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
# future_threshold = "1s"

[snapshot]
# creation_enabled = true
# mode = "read_write"
# create_every = "1min"
# store_dir = "./storage/snapshot"

Expand Down
Loading

0 comments on commit 8d0157a

Please sign in to comment.