Skip to content

Commit

Permalink
feat: Implement config loader (#143)
Browse files Browse the repository at this point in the history
* feat : check ports in a Windows environment

* chore : refactor build_config

* feat: Implement config loader
Co-authored-by: jyc0011 <jyc0011@gmail.com>

---------

Co-authored-by: jyc0011 <jyc0011211@gmail.com>
Co-authored-by: jyc0011 <122250128+jyc0011@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 4, 2024
1 parent 4ba70d0 commit fabc961
Show file tree
Hide file tree
Showing 14 changed files with 524 additions and 294 deletions.
243 changes: 231 additions & 12 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions examples/memstore/common_raftnode_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
log_dir = "./logs"
compacted_log_size_threshold = 1073741824
save_compacted_logs = true

[raft_config]
tick_interval = 0.2
election_tick = 10
heartbeat_tick = 3
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
3 changes: 1 addition & 2 deletions harness/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use raftify::{Config, ConfigBuilder, Peers, RaftConfig};

use crate::utils::{ensure_directory_exist, get_storage_path};
use raftify::{Config, ConfigBuilder, Peers, RaftConfig};

pub fn build_config(node_id: u64, initial_peers: Option<Peers>) -> Config {
let raft_config = RaftConfig {
Expand Down
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
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 fabc961

Please sign in to comment.