use std::time::Duration;
use rand::RngExt;
use crate::AsyncRuntime;
use crate::Config;
use crate::RaftTypeConfig;
use crate::engine::time_state;
use crate::type_config::alias::AsyncRuntimeOf;
#[derive(Clone, Debug)]
#[derive(PartialEq, Eq)]
pub(crate) struct EngineConfig<C: RaftTypeConfig> {
pub(crate) id: C::NodeId,
pub(crate) max_in_snapshot_log_to_keep: u64,
pub(crate) purge_batch_size: u64,
pub(crate) max_payload_entries: u64,
pub(crate) allow_log_reversion: bool,
pub(crate) election_timeout_min: u64,
pub(crate) election_timeout_max: u64,
pub(crate) timer_config: time_state::Config,
pub(crate) enable_leader_restore: bool,
}
impl<C> EngineConfig<C>
where C: RaftTypeConfig
{
pub(crate) fn new(id: C::NodeId, config: &Config) -> Self {
let mut this = Self {
id,
max_in_snapshot_log_to_keep: config.max_in_snapshot_log_to_keep,
purge_batch_size: config.purge_batch_size,
max_payload_entries: config.max_payload_entries,
allow_log_reversion: config.get_allow_log_reversion(),
election_timeout_min: config.election_timeout_min,
election_timeout_max: config.election_timeout_max,
timer_config: time_state::Config {
election_timeout: Duration::ZERO, smaller_log_timeout: Duration::from_millis(config.election_timeout_max * 2),
leader_lease: Duration::from_millis(config.election_timeout_max),
},
enable_leader_restore: config.enable_leader_restore(),
};
this.resample_election_timeout();
this
}
#[allow(dead_code)]
pub(crate) fn new_default(id: C::NodeId) -> Self {
Self {
id,
max_in_snapshot_log_to_keep: 1000,
purge_batch_size: 256,
max_payload_entries: 300,
allow_log_reversion: false,
election_timeout_min: 150,
election_timeout_max: 300,
timer_config: time_state::Config::default(),
enable_leader_restore: true,
}
}
pub(crate) fn resample_election_timeout(&mut self) {
let election_timeout =
AsyncRuntimeOf::<C>::thread_rng().random_range(self.election_timeout_min..self.election_timeout_max);
self.timer_config.election_timeout = Duration::from_millis(election_timeout);
}
}