Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Pretty print persisted log entries in CLI #151

Merged
merged 3 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions raftify-cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions raftify-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ built = "0.5"
clap = { version = "4.5.18", features = ["derive"] }
raftify = { version = "=0.1.81", features = ["heed_storage", "inmemory_storage", "rocksdb_storage"] }
rocksdb = "0.19.0"
comfy-table = "7.1.1"

[lib]
name = "raftify_cli"
Expand Down
51 changes: 46 additions & 5 deletions raftify-cli/src/commands/debug.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
use comfy_table::{Cell, CellAlignment, ContentArrangement, Table};
use core::panic;
use serde_json::Value;
use std::{collections::HashMap, fs, path::Path, sync::Arc};

use raftify::{
create_client,
raft::{
formatter::{format_entry, format_snapshot},
formatter::{format_entry, format_snapshot, CUSTOM_FORMATTER},
logger::Slogger,
Storage,
},
raft_node::utils::format_debugging_info,
raft_service, ConfigBuilder, HeedStorage, Result, StableStorage, StorageType,
};

pub fn debug_persisted<LogStorage: StableStorage>(path: &str, logger: slog::Logger) -> Result<()> {
pub fn debug_persisted<LogStorage: StableStorage>(
path: &str,
logger: slog::Logger,
print_as_table: bool,
) -> Result<()> {
let config = ConfigBuilder::new().log_dir(path.to_string()).build();

let storage = match LogStorage::STORAGE_TYPE {
Expand Down Expand Up @@ -42,8 +47,39 @@ pub fn debug_persisted<LogStorage: StableStorage>(path: &str, logger: slog::Logg
}

println!("---- Persisted entries ----");
for entry in entries.iter() {
println!("Key: {}, {:?}", entry.get_index(), format_entry(entry));

if print_as_table {
let mut table = Table::new();
let formatter = CUSTOM_FORMATTER.read().unwrap();

table
.set_header(vec![
Cell::new("index").set_alignment(CellAlignment::Center),
Cell::new("type").set_alignment(CellAlignment::Center),
Cell::new("data").set_alignment(CellAlignment::Center),
Cell::new("term").set_alignment(CellAlignment::Center),
])
.set_content_arrangement(ContentArrangement::Dynamic);

for entry in entries.iter() {
table.add_row(vec![
Cell::new(entry.get_index()).set_alignment(CellAlignment::Center),
Cell::new(
format!("{:?}", entry.get_entry_type())
.replace("Entry", "")
.replace("ConfChangeV2", "ConfChange"),
)
.set_alignment(CellAlignment::Center),
Cell::new(formatter.format_entry_data(&entry.data.clone().into()))
.set_alignment(CellAlignment::Left),
Cell::new(entry.get_term()).set_alignment(CellAlignment::Center),
]);
}
println!("{}", table);
} else {
for entry in entries.iter() {
println!("Key: {}, {:?}", entry.get_index(), format_entry(entry));
}
}

println!();
Expand All @@ -59,6 +95,7 @@ pub fn debug_persisted<LogStorage: StableStorage>(path: &str, logger: slog::Logg
pub fn debug_persisted_all<LogStorage: StableStorage>(
path_str: &str,
logger: slog::Logger,
print_as_table: bool,
) -> Result<()> {
let path = match fs::canonicalize(Path::new(&path_str)) {
Ok(absolute_path) => absolute_path,
Expand Down Expand Up @@ -90,7 +127,11 @@ pub fn debug_persisted_all<LogStorage: StableStorage>(

for name in dir_entries {
println!("*----- {name} -----*");
debug_persisted::<LogStorage>(&format!("{}/{}", path_str, name), logger.clone())?;
debug_persisted::<LogStorage>(
&format!("{}/{}", path_str, name),
logger.clone(),
print_as_table,
)?;
println!();
}
} else {
Expand Down
22 changes: 10 additions & 12 deletions raftify-cli/src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ enum DebugSubcommands {
Persisted {
/// The log directory path
path: String,
/// Print the output in a table format
#[arg(long, default_value_t = false)]
table: bool,
},
/// List persisted log entries and metadata for all local nodes
PersistedAll {
/// The log directory path
path: String,
/// Print the output in a table format
#[arg(long, default_value_t = false)]
table: bool,
},
/// List all log entries
Entries {
Expand All @@ -52,14 +58,6 @@ enum DebugSubcommands {
},
}

#[derive(Args)]
struct Dump {
/// The log directory path
path: String,
/// The peers to dump
peers: String,
}

pub async fn cli_handler<
LogEntry: AbstractLogEntry + Debug + Send + 'static,
LogStorage: StableStorage + Send + Sync + Clone + 'static,
Expand All @@ -76,11 +74,11 @@ pub async fn cli_handler<

match app.command {
Commands::Debug(x) => match x {
DebugSubcommands::Persisted { path } => {
debug_persisted::<LogStorage>(path.as_str(), logger.clone())?;
DebugSubcommands::Persisted { path, table } => {
debug_persisted::<LogStorage>(path.as_str(), logger.clone(), table)?;
}
DebugSubcommands::PersistedAll { path } => {
debug_persisted_all::<LogStorage>(path.as_str(), logger.clone())?;
DebugSubcommands::PersistedAll { path, table } => {
debug_persisted_all::<LogStorage>(path.as_str(), logger.clone(), table)?;
}
DebugSubcommands::Entries { address } => {
debug_entries(address.as_str()).await?;
Expand Down
Loading