Skip to content

Commit

Permalink
chore : refactor build_config
Browse files Browse the repository at this point in the history
  • Loading branch information
jyc0011 committed Oct 3, 2024
1 parent ab3e150 commit fde9778
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 238 deletions.
11 changes: 11 additions & 0 deletions harness/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[node]
id = 1

[raft]
election_tick = 10
heartbeat_tick = 3
omit_heartbeat_log = true

[storage]
log_dir = "./logs"
compacted_log_size_threshold = 1073741824
76 changes: 54 additions & 22 deletions harness/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,63 @@
use raftify::{Config, ConfigBuilder, Peers, RaftConfig};

use std::fs;
use serde::Deserialize;
use raftify::{Config, Peers, RaftConfig}; // ConfigBuilder 삭제
use crate::utils::{ensure_directory_exist, get_storage_path};

pub fn build_config(node_id: u64, initial_peers: Option<Peers>) -> Config {
#[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 생성
let raft_config = RaftConfig {
id: node_id,
election_tick: 10,
heartbeat_tick: 3,
omit_heartbeat_log: true,
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,
..Default::default()
};

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

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()
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()
}
}
36 changes: 36 additions & 0 deletions raftify/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[node]
id = 1

[raft]
election_tick = 10
heartbeat_tick = 3
omit_heartbeat_log = true
applied = 0
max_size_per_msg = 1048576
max_inflight_msgs = 256
check_quorum = true
pre_vote = false
min_election_tick = 5
max_election_tick = 15
read_only_option = "Safe"
skip_bcast_commit = false
batch_append = true
priority = 0
max_uncommitted_size = 100
max_committed_size_per_ready = 256

[storage]
log_dir = "./logs"
compacted_log_size_threshold = 1073741824

[cluster]
cluster_id = "default"
tick_interval = 0.1
lmdb_map_size = 1073741824
conf_change_request_timeout = 2.0

[tls]
cert_path = "/path/cert"
key_path = "/path/key"
ca_cert_path = "/path/ca"
domain_name = "example.com"
Loading

0 comments on commit fde9778

Please sign in to comment.