Skip to content

Commit

Permalink
scaffold cli for replication server
Browse files Browse the repository at this point in the history
  • Loading branch information
jdockerty committed Apr 15, 2024
1 parent abc456c commit 1ef3038
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ name = "sqrl_bench"
harness = false

[features]
default = ["replication"]
replication = []


Expand Down
34 changes: 33 additions & 1 deletion src/bin/sqrl-server.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use clap::Parser;
use sqrl::client::RemoteNodeClient;
use sqrl::replication;
use sqrl::replication::ReplicatedServer;
use sqrl::KvStore;
use sqrl::StandaloneServer;
use sqrl::ENGINE_FILE;
use std::{ffi::OsString, path::PathBuf};
use std::{fmt::Display, net::SocketAddr};
use tokio::sync::Mutex;

mod proto {
tonic::include_proto!("actions");
Expand All @@ -23,6 +27,18 @@ struct App {

#[arg(long, global = true, default_value = default_log_location())]
log_file: PathBuf,

#[cfg(feature = "replication")]
#[arg(long, default_value = "follower")]
replication_mode: replication::Mode,

#[cfg(feature = "replication")]
#[arg(
long,
requires_if(replication::Mode::Leader, "replication_mode"),
value_delimiter = ','
)]
followers: Vec<String>,
}

fn default_log_location() -> OsString {
Expand Down Expand Up @@ -52,5 +68,21 @@ async fn main() -> anyhow::Result<()> {
let app = App::parse();
// We must error if the previous storage engine was not 'sqrl' as it is incompatible.
KvStore::engine_is_sqrl(app.engine_name.to_string(), app.log_file.join(ENGINE_FILE))?;
StandaloneServer::new(app.log_file, app.addr)?.run().await
match app.replication_mode {
replication::Mode::Leader => {
assert_eq!(
app.followers.len(),
2,
"Only 2 followers are configurable at present"
);
let clients = [
Mutex::new(RemoteNodeClient::new(app.followers[0].clone()).await?),
Mutex::new(RemoteNodeClient::new(app.followers[1].clone()).await?),
];
ReplicatedServer::new(clients, app.log_file, app.addr)?
.run()
.await
}
replication::Mode::Follower => StandaloneServer::new(app.log_file, app.addr)?.run().await,
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod engine;
mod error;
/// Provides asynchronous cache replication over a network to a number of other
/// cache nodes.
mod replication;
pub mod replication;
/// GRPC server which contains the key-value store.
mod server;
/// Implementation of the key-value store.
Expand Down
15 changes: 15 additions & 0 deletions src/replication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,18 @@
mod server;

pub use server::ReplicatedServer;

#[derive(Debug, Clone, clap::ValueEnum)]
pub enum Mode {
Leader,
Follower,
}

impl From<Mode> for clap::builder::OsStr {
fn from(value: Mode) -> Self {
match value {
Mode::Leader => "leader".into(),
Mode::Follower => "follower".into(),
}
}
}
46 changes: 35 additions & 11 deletions tests/replicated_store.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
use assert_cmd::prelude::*;
use std::process::Command;
use tempfile::TempDir;

#[test]
fn replicated_setup() {
let temp_dir = TempDir::new().unwrap();
let mut cmd = Command::cargo_bin("sqrl-server").unwrap();
cmd.args(["--replication-mode", "leader", "--addr", "127.0.0.1:7000"])
.current_dir(&temp_dir).assert().success();
}
//use assert_cmd::prelude::*;
//use std::process::Command;
//use tempfile::TempDir;
//
//#[test]
//fn replicated_setup() {
// println!("Starting follower_one");
// let mut follower_one = Command::cargo_bin("sqrl-server").unwrap();
// follower_one
// .args(["--addr", "127.0.0.1:7001"])
// .current_dir(&TempDir::new().unwrap())
// .assert()
// .success();
//
// println!("Starting follower_two");
// let mut follower_two = Command::cargo_bin("sqrl-server").unwrap();
// follower_two
// .args(["--addr", "127.0.0.1:7002"])
// .current_dir(&TempDir::new().unwrap())
// .assert()
// .success();
// let mut replication_server = Command::cargo_bin("sqrl-server").unwrap();
// replication_server
// .args([
// "--replication-mode",
// "leader",
// "--addr",
// "127.0.0.1:7000",
// "--followers",
// "127.0.0.1:7001,127.0.0.1:7002",
// ])
// .current_dir(&TempDir::new().unwrap())
// .assert()
// .success();
//}

0 comments on commit 1ef3038

Please sign in to comment.