Skip to content

Commit

Permalink
feat: Implement config loader
Browse files Browse the repository at this point in the history
Co-authored-by: jyc0011 <jyc0011@gmail.com>
  • Loading branch information
jopemachine committed Oct 4, 2024
1 parent fde9778 commit 029c9b7
Show file tree
Hide file tree
Showing 13 changed files with 505 additions and 241 deletions.
243 changes: 231 additions & 12 deletions Cargo.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
[node]
id = 1
log_dir = "./logs"
compacted_log_size_threshold = 1073741824
save_compacted_logs = true

[raft]
[raft_config]
tick_interval = 0.2
election_tick = 10
heartbeat_tick = 3
omit_heartbeat_log = true

[storage]
log_dir = "./logs"
compacted_log_size_threshold = 1073741824
omit_heartbeat_log = false
32 changes: 15 additions & 17 deletions examples/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
use raftify::{Config, ConfigBuilder, Peers, RaftConfig};
use std::path::Path;

use raftify::{load_configs, Config, ConfigBuilder, Peers};

#[cfg(feature = "tls")]
use raftify::TlsConfig;

use crate::utils::{ensure_directory_exist, get_storage_path};

pub fn build_config(node_id: u64, initial_peers: Option<Peers>) -> Config {
let raft_config = RaftConfig {
id: node_id,
election_tick: 10,
heartbeat_tick: 3,
omit_heartbeat_log: false,
..Default::default()
};

let storage_pth = get_storage_path("./logs", node_id);
ensure_directory_exist(&storage_pth).expect("Failed to create storage directory");

#[allow(unused_mut)]
let mut config_builder = ConfigBuilder::new()
.log_dir("./logs".to_owned())
.save_compacted_logs(true)
.compacted_log_dir("./logs".to_owned())
.compacted_log_size_threshold(1024 * 1024 * 1024)
.raft_config(raft_config)
.tick_interval(0.2);
let path = Path::new(file!())
.parent()
.unwrap()
.parent()
.unwrap()
.join("memstore/common_raftnode_config.toml");

let config_builder = ConfigBuilder::from_config(
load_configs(path.to_str().unwrap()).expect("Failed to load common config"),
)
.set_node_id(node_id);

#[allow(unused_mut)]
#[cfg(feature = "tls")]
{
let client_tls_config = TlsConfig {
Expand Down
75 changes: 21 additions & 54 deletions harness/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,63 +1,30 @@
use std::fs;
use serde::Deserialize;
use raftify::{Config, Peers, RaftConfig}; // ConfigBuilder 삭제
use crate::utils::{ensure_directory_exist, get_storage_path};
use raftify::{Config, ConfigBuilder, Peers, RaftConfig};

#[derive(Deserialize)]
struct NodeConfig {
id: u64,
}

#[derive(Deserialize)]
struct RaftConfigToml {
election_tick: u64,
heartbeat_tick: u64,
omit_heartbeat_log: bool,
}

#[derive(Deserialize)]
struct StorageConfig {
log_dir: String,
compacted_log_size_threshold: u64,
}

#[derive(Deserialize)]
struct ConfigToml {
node: NodeConfig,
raft: RaftConfigToml,
storage: StorageConfig,
}

// TOML 파일에서 설정을 읽어오는 함수
pub fn load_config() -> ConfigToml {
let config_content = fs::read_to_string("config.toml").expect("Failed to read config.toml");
toml::from_str(&config_content).expect("Failed to parse config.toml")
}

// ConfigBuilder 없이 Config 직접 생성
pub fn build_config(initial_peers: Option<Peers>) -> Config {
// TOML 파일에서 설정을 읽음
let config = load_config();

// RaftConfig 생성
pub fn build_config(node_id: u64, initial_peers: Option<Peers>) -> Config {
let raft_config = RaftConfig {
id: config.node.id,
election_tick: config.raft.election_tick as usize,
heartbeat_tick: config.raft.heartbeat_tick as usize,
omit_heartbeat_log: config.raft.omit_heartbeat_log,
id: node_id,
election_tick: 10,
heartbeat_tick: 3,
omit_heartbeat_log: true,
..Default::default()
};

let storage_path = get_storage_path(&config.storage.log_dir, config.node.id);
let storage_path = get_storage_path("./logs", node_id);
ensure_directory_exist(&storage_path).expect("Failed to create storage directory");

Config {
raft_config,
log_dir: storage_path.clone(),
save_compacted_logs: true,
compacted_log_dir: storage_path,
compacted_log_size_threshold: config.storage.compacted_log_size_threshold,
initial_peers, // 초기 피어 설정
..Default::default()
}
let config_builder = ConfigBuilder::new()
.log_dir(storage_path.clone())
.save_compacted_logs(true)
.compacted_log_dir(storage_path)
.compacted_log_size_threshold(1024 * 1024 * 1024)
.raft_config(raft_config);

let config_builder = if let Some(peers) = initial_peers {
config_builder.initial_peers(peers)
} else {
config_builder
};

config_builder.build()
}
2 changes: 1 addition & 1 deletion raft-rs
16 changes: 10 additions & 6 deletions raftify-cli/src/commands/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ use serde_json::Value;
use std::{collections::HashMap, fs, path::Path, sync::Arc};

use raftify::{
create_client, raft::{
create_client,
raft::{
formatter::{format_entry, format_snapshot},
logger::Slogger,
Storage,
}, raft_node::utils::format_debugging_info, raft_service, Config, ConfigBuilder, HeedStorage, Result, StableStorage, StorageType
},
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<()> {
let config = ConfigBuilder::new()
.log_dir(path.to_string())
.build();
let config = ConfigBuilder::new().log_dir(path.to_string()).build();

let storage = match LogStorage::STORAGE_TYPE {
StorageType::Heed => HeedStorage::create(
Expand Down Expand Up @@ -55,7 +56,10 @@ pub fn debug_persisted<LogStorage: StableStorage>(path: &str, logger: slog::Logg
Ok(())
}

pub fn debug_persisted_all<LogStorage: StableStorage>(path_str: &str, logger: slog::Logger) -> Result<()> {
pub fn debug_persisted_all<LogStorage: StableStorage>(
path_str: &str,
logger: slog::Logger,
) -> Result<()> {
let path = match fs::canonicalize(Path::new(&path_str)) {
Ok(absolute_path) => absolute_path,
Err(e) => {
Expand Down
3 changes: 2 additions & 1 deletion raftify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bytes = "1.7.2"
log = { version = "0.4", features = ["std"] }
parking_lot = "0.12.3"
prost = "0.11"
raft = { version = "0.7.10", features = ["prost-codec", "default-logger"], default-features = false, package = "jopemachine-raft" }
raft = { version = "0.7.13", features = ["prost-codec", "default-logger"], default-features = false, package = "jopemachine-raft" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
slog = "2"
Expand All @@ -29,6 +29,7 @@ chrono = "0.4.38"
heed = { version = "0.20.5", optional = true }
heed-traits = { version = "0.20", optional = true }
rocksdb = { version = "0.19.0", optional = true }
config = "0.14.0"

[features]
default = ["heed_storage", "tls"]
Expand Down
140 changes: 0 additions & 140 deletions raftify/src/config.rs

This file was deleted.

Loading

0 comments on commit 029c9b7

Please sign in to comment.