Skip to content

Commit

Permalink
feat: Support TLS config (#139)
Browse files Browse the repository at this point in the history
* feat: Support TLS config

* fix: CI

* chore: Turn off tls flag in example code

* chore: fix CI

* chore: delete obsoleted comment

* fix: Resolve todo
  • Loading branch information
jopemachine authored Oct 3, 2024
1 parent e1a12d4 commit 4ba70d0
Show file tree
Hide file tree
Showing 25 changed files with 791 additions and 182 deletions.
120 changes: 112 additions & 8 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ default-members = [
exclude = ["raft-rs", "raftify-cli"]

[workspace.package]
version = "0.1.78"
version = "0.1.80"
authors = ["Lablup Inc."]
edition = "2021"
description = "Experimental High level Raft framework"
Expand Down
4 changes: 4 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ keywords.workspace = true

[dependencies]
raftify.workspace = true

[features]
default = []
tls = []
5 changes: 3 additions & 2 deletions examples/memstore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.40", features = ["full"] }
color-backtrace = "0.6.1"
raftify = { version = "0.1.78" }
raftify_cli = { version = "0.1.1" }
raftify = { version = "0.1.80" }
raftify_cli = { version = "0.1.80" }

[features]
default = ["heed_storage"]
inmemory_storage = []
heed_storage = []
rocksdb_storage = []
tls = []
12 changes: 5 additions & 7 deletions examples/memstore/dynamic-members/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,17 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
Some(peer_addr) => {
log::info!("Running in Follower mode");

let ticket = Raft::request_id(options.raft_addr.clone(), peer_addr.clone())
let ticket = Raft::request_id(options.raft_addr.clone(), peer_addr.clone(), None)
.await
.unwrap();
let node_id = ticket.reserved_id;
let mut cfg = build_config(node_id);

cfg.initial_peers = Some(ticket.peers.clone().into());
let cfg = build_config(node_id, Some(ticket.peers.clone().into()));

#[cfg(feature = "inmemory_storage")]
let log_storage = MemStorage::create();

#[cfg(feature = "heed_storage")]
let log_storage = HeedStorage::create(&cfg.log_dir, &cfg.clone(), logger.clone())
let log_storage = HeedStorage::create(cfg.get_log_dir(), &cfg.clone(), logger.clone())
.expect("Failed to create storage");

#[cfg(feature = "rocksdb_storage")]
Expand Down Expand Up @@ -121,13 +119,13 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
}

let leader_node_id = 1;
let cfg = build_config(leader_node_id);
let cfg = build_config(leader_node_id, None);

#[cfg(feature = "inmemory_storage")]
let log_storage = MemStorage::create();

#[cfg(feature = "heed_storage")]
let log_storage = HeedStorage::create(&cfg.log_dir, &cfg.clone(), logger.clone())
let log_storage = HeedStorage::create(cfg.get_log_dir(), &cfg.clone(), logger.clone())
.expect("Failed to create storage");

#[cfg(feature = "rocksdb_storage")]
Expand Down
2 changes: 1 addition & 1 deletion examples/memstore/src/client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use memstore_example_harness::state_machine::LogEntry;
#[actix_rt::main]
async fn main() {
println!("---Message propose---");
let mut leader_client = create_client(&"127.0.0.1:60061").await.unwrap();
let mut leader_client = create_client(&"127.0.0.1:60061", None).await.unwrap();

leader_client
.propose(raft_service::ProposeArgs {
Expand Down
1 change: 0 additions & 1 deletion examples/memstore/static-members/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ default = ["heed_storage"]
inmemory_storage = []
heed_storage = []
rocksdb_storage = []

5 changes: 2 additions & 3 deletions examples/memstore/static-members/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,13 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
.get_node_id_by_addr(options.raft_addr.clone())
.unwrap();

let mut cfg = build_config(node_id);
cfg.initial_peers = Some(initial_peers.clone());
let cfg = build_config(node_id, Some(initial_peers.clone()));

#[cfg(feature = "inmemory_storage")]
let log_storage = MemStorage::create();

#[cfg(feature = "heed_storage")]
let log_storage = HeedStorage::create(&cfg.log_dir, &cfg.clone(), logger.clone())
let log_storage = HeedStorage::create(cfg.get_log_dir(), &cfg.clone(), logger.clone())
.expect("Failed to create storage");

#[cfg(feature = "rocksdb_storage")]
Expand Down
43 changes: 32 additions & 11 deletions examples/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use raftify::{Config, RaftConfig};
use raftify::{Config, ConfigBuilder, Peers, RaftConfig};

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

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

pub fn build_config(node_id: u64) -> Config {
pub fn build_config(node_id: u64, initial_peers: Option<Peers>) -> Config {
let raft_config = RaftConfig {
id: node_id,
election_tick: 10,
Expand All @@ -14,14 +17,32 @@ pub fn build_config(node_id: u64) -> Config {
let storage_pth = get_storage_path("./logs", node_id);
ensure_directory_exist(&storage_pth).expect("Failed to create storage directory");

Config {
bootstrap_from_snapshot: false,
tick_interval: 0.2,
log_dir: storage_pth.clone(),
save_compacted_logs: true,
compacted_log_dir: storage_pth,
compacted_log_size_threshold: 1024 * 1024 * 1024,
raft_config,
..Default::default()
#[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);

#[cfg(feature = "tls")]
{
let client_tls_config = TlsConfig {
cert_path: None,
key_path: None,
ca_cert_path: Some("./ca_cert.pem".to_string()),
domain_name: Some("localhost".to_string()),
};

config_builder = config_builder.global_client_tls_config(client_tls_config);
}

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

config_builder.build()
}
Loading

0 comments on commit 4ba70d0

Please sign in to comment.