From 62450571602b6c7dc2edaaeac14e811aa29ba54a Mon Sep 17 00:00:00 2001 From: Gyubong Date: Mon, 22 Jan 2024 18:14:49 +0900 Subject: [PATCH] Add`dump_peers` cli command --- README.md | 2 +- examples/memstore/static-members/src/main.rs | 6 ++-- raftify/src/cli/commands/dump.rs | 1 - raftify/src/cli/commands/utils.rs | 1 - raftify/src/cli/mod.rs | 33 +++++++++++++++++--- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 2140ee65..575674a5 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ If peer specifies the configuration of the initial members, the cluster will ope ```rust let raft_addr = "127.0.0.1:60062".to_owned(); let peer_addr = "127.0.0.1:60061".to_owned(); -let join_ticket = await Raft::request_id(raft_addr, peer_addr, logger.clone()); +let join_ticket = Raft::request_id(raft_addr, peer_addr, logger.clone()).await; let raft = Raft::new_follower( join_ticket.reserved_id, diff --git a/examples/memstore/static-members/src/main.rs b/examples/memstore/static-members/src/main.rs index 36a89b41..679af1d4 100644 --- a/examples/memstore/static-members/src/main.rs +++ b/examples/memstore/static-members/src/main.rs @@ -39,11 +39,9 @@ struct Options { fn validate_options(options: Options) -> Options { if options.peer_addr.is_some() && options.restore_wal_from.is_some() { panic!("Cannot restore WAL from follower node"); - } - else if options.peer_addr.is_some() && options.restore_wal_snapshot_from.is_some() { + } else if options.peer_addr.is_some() && options.restore_wal_snapshot_from.is_some() { panic!("Cannot restore WAL snapshot from follower node"); - } - else { + } else { options } } diff --git a/raftify/src/cli/commands/dump.rs b/raftify/src/cli/commands/dump.rs index 2e2a7ef7..8ed1936d 100644 --- a/raftify/src/cli/commands/dump.rs +++ b/raftify/src/cli/commands/dump.rs @@ -10,7 +10,6 @@ use std::{collections::HashMap, net::SocketAddr, sync::Arc}; use crate::{utils::to_confchange_v2, Config, HeedStorage, LogStore, Result}; /// Read all_entries and make the appropriate ConfChanges to make it peers compared to the peers given in json format. -#[allow(dead_code)] pub fn dump_peers(path: &str, peers: HashMap, logger: slog::Logger) -> Result<()> { let config = Config { log_dir: path.to_string(), diff --git a/raftify/src/cli/commands/utils.rs b/raftify/src/cli/commands/utils.rs index 4a4f4755..dac74b20 100644 --- a/raftify/src/cli/commands/utils.rs +++ b/raftify/src/cli/commands/utils.rs @@ -3,7 +3,6 @@ use std::collections::HashMap; use std::net::SocketAddr; use std::str::FromStr; -#[allow(dead_code)] pub fn parse_peers_json( peers: &str, ) -> Result, Box> { diff --git a/raftify/src/cli/mod.rs b/raftify/src/cli/mod.rs index d4af0084..c44bf5dc 100644 --- a/raftify/src/cli/mod.rs +++ b/raftify/src/cli/mod.rs @@ -3,7 +3,11 @@ include!(concat!(env!("OUT_DIR"), "/built.rs")); mod commands; use clap::{App, Arg, SubCommand}; -use commands::debug::{debug_entries, debug_node, debug_persisted}; +use commands::{ + debug::{debug_entries, debug_node, debug_persisted, debug_persitsted_all}, + dump::dump_peers, + utils::parse_peers_json, +}; use std::fmt::Debug; use crate::{ @@ -11,8 +15,6 @@ use crate::{ AbstractLogEntry, AbstractStateMachine, CustomFormatter, Result, }; -use self::commands::debug::debug_persitsted_all; - pub async fn cli_handler< LogEntry: AbstractLogEntry + Debug + Send + 'static, FSM: AbstractStateMachine + Debug + Clone + Send + Sync + 'static, @@ -64,8 +66,23 @@ pub async fn cli_handler< .index(1), ), ), + ) + .subcommand( + SubCommand::with_name("dump") + .arg( + Arg::with_name("path") + .help("The log directory path") + .required(true) + .index(1), + ) + .arg( + Arg::with_name("peers") + .help("The peers to dump") + .required(true) + .index(2), + ) + .about(""), ); - // .subcommand(SubCommand::with_name("health").about("Check health")) let matches = match custom_args { Some(args) => app.get_matches_from(args), @@ -101,5 +118,13 @@ pub async fn cli_handler< } }; + if let Some(("dump", dump_matches)) = matches.subcommand() { + if let Some(path) = dump_matches.value_of("path") { + if let Some(peers) = dump_matches.value_of("peers") { + dump_peers(path, parse_peers_json(peers).unwrap(), logger.clone())?; + } + } + }; + Ok(()) }