diff --git a/src/main.rs b/src/main.rs index 9165c279d6c2028265aeeb78baa357e39794064c..e2e497ee4456adc63cd04896e8739795aa6d6da5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ use ::tarantool::tlua; use ::tarantool::transaction::start_transaction; use std::convert::TryFrom; use std::time::{Duration, Instant}; +use traft::storage::{RaftSpace, RaftStateKey}; use traft::ExpelRequest; use clap::StructOpt as _; @@ -610,9 +611,15 @@ fn start_boot(args: &args::Run) { lc.inc(); init_entries.push({ let ctx = traft::EntryContextNormal { - op: traft::Op::PersistReplicationFactor { - replication_factor: args.init_replication_factor, - }, + op: traft::OpDML::insert( + RaftSpace::State, + &( + RaftStateKey::ReplicationFactor, + args.init_replication_factor, + ), + ) + .expect("cannot fail") + .into(), lc, }; let e = traft::Entry { diff --git a/src/traft/mod.rs b/src/traft/mod.rs index f5b6f74b867ef75b2c9977b2e300164dbda13ec1..6e8d89a4c86094a15e57950739d89fc1aa01e0ee 100644 --- a/src/traft/mod.rs +++ b/src/traft/mod.rs @@ -87,11 +87,6 @@ pub enum Op { PersistPeer { peer: Peer, }, - - #[serde(alias = "persist_replication_factor")] - PersistReplicationFactor { - replication_factor: u8, - }, /// Cluster-wide data modification operation. /// Should be used to manipulate the cluster-wide configuration. Dml(OpDML), @@ -107,9 +102,6 @@ impl std::fmt::Display for Op { Self::PersistPeer { peer } => { write!(f, "PersistPeer{}", peer) } - Self::PersistReplicationFactor { replication_factor } => { - write!(f, "PersistReplicationFactor({replication_factor})") - } Self::Dml(OpDML::Insert { space, tuple }) => { write!(f, "Insert({space}, {})", DisplayAsJson(tuple)) } @@ -170,10 +162,6 @@ impl Op { Storage::persist_peer(peer).unwrap(); Box::new(peer.clone()) } - Self::PersistReplicationFactor { replication_factor } => { - Storage::persist_replication_factor(*replication_factor).unwrap(); - Box::new(()) - } Self::Dml(op) => Box::new(op.result()), } } diff --git a/src/traft/storage.rs b/src/traft/storage.rs index d6c7a86c8beaf4d8b751522c8e73ce42dca34d93..fe309882253cacfb12af6241ddb34fb5676e7377 100644 --- a/src/traft/storage.rs +++ b/src/traft/storage.rs @@ -45,6 +45,33 @@ const RAFT_GROUP: &str = RaftSpace::Group.as_str(); const RAFT_STATE: &str = RaftSpace::State.as_str(); const RAFT_LOG: &str = RaftSpace::Log.as_str(); +//////////////////////////////////////////////////////////////////////////////// +// RaftStateKey +//////////////////////////////////////////////////////////////////////////////// + +define_str_enum! { + /// An enumeration of builtin raft spaces + pub enum RaftStateKey { + ReplicationFactor = "replication_factor", + Commit = "commit", + Applied = "applied", + Term = "term", + Vote = "vote", + Gen = "gen", + Voters = "voters", + Learners = "learners", + VotersOutgoing = "voters_outgoing", + LearnersNext = "learners_next", + AutoLeave = "auto_leave", + } + + FromStr::Err = UnknownRaftStateKey; +} + +#[derive(Error, Debug)] +#[error("unknown raft state key {0}")] +pub struct UnknownRaftStateKey(pub String); + //////////////////////////////////////////////////////////////////////////////// // Error //////////////////////////////////////////////////////////////////////////////// @@ -271,11 +298,7 @@ impl Storage { } pub fn replication_factor() -> Result<Option<u8>, StorageError> { - Storage::raft_state("replication_factor") - } - - pub fn persist_replication_factor(replication_factor: u8) -> Result<(), StorageError> { - Storage::persist_raft_state("replication_factor", replication_factor) + Storage::raft_state(RaftStateKey::ReplicationFactor.as_str()) } pub fn persist_commit(commit: RaftIndex) -> Result<(), StorageError> {