Skip to content

Commit abb1fbb

Browse files
committed
Add replica management
1 parent de0d504 commit abb1fbb

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/main.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use crate::{
4040
},
4141
runtime::BackgroundRuntime,
4242
};
43+
use crate::operations::{replica_deregister, replica_list, replica_primary, replica_register, server_version};
4344

4445
mod cli;
4546
mod completions;
@@ -326,6 +327,9 @@ fn execute_commands(context: &mut ConsoleContext, mut input: &str, must_log_comm
326327
}
327328

328329
fn entry_repl(driver: Arc<TypeDBDriver>, runtime: BackgroundRuntime) -> Repl<ConsoleContext> {
330+
let server_commands = Subcommand::new("server")
331+
.add(CommandLeaf::new("version", "Retrieve server version.", server_version));
332+
329333
let database_commands = Subcommand::new("database")
330334
.add(CommandLeaf::new("list", "List databases on the server.", database_list))
331335
.add(CommandLeaf::new_with_input(
@@ -410,6 +414,25 @@ fn entry_repl(driver: Arc<TypeDBDriver>, runtime: BackgroundRuntime) -> Repl<Con
410414
user_update_password,
411415
));
412416

417+
let replica_commands = Subcommand::new("replica")
418+
.add(CommandLeaf::new("list", "List replicas.", replica_list))
419+
.add(CommandLeaf::new("primary", "Get current primary replica.", replica_primary))
420+
.add(CommandLeaf::new_with_inputs(
421+
"register",
422+
"Register new replica.",
423+
vec![
424+
CommandInput::new("replica id", get_word, None, None),
425+
CommandInput::new("address", get_word, None, None),
426+
],
427+
replica_register,
428+
))
429+
.add(CommandLeaf::new_with_input(
430+
"deregister",
431+
"Deregister existing replica.",
432+
CommandInput::new("replica id", get_word, None, None),
433+
replica_deregister,
434+
));
435+
413436
let transaction_commands = Subcommand::new("transaction")
414437
.add(CommandLeaf::new_with_input(
415438
"read",
@@ -445,8 +468,10 @@ fn entry_repl(driver: Arc<TypeDBDriver>, runtime: BackgroundRuntime) -> Repl<Con
445468
let history_path = home_dir().unwrap_or_else(|| temp_dir()).join(ENTRY_REPL_HISTORY);
446469

447470
let repl = Repl::new(PROMPT.to_owned(), history_path, false, None)
471+
.add(server_commands)
448472
.add(database_commands)
449473
.add(user_commands)
474+
.add(replica_commands)
450475
.add(transaction_commands);
451476

452477
repl

src/operations.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ use crate::{
2121
transaction_repl, ConsoleContext,
2222
};
2323

24+
pub(crate) fn server_version(context: &mut ConsoleContext, _input: &[String]) -> CommandResult {
25+
let driver = context.driver.clone();
26+
let server_version = context
27+
.background_runtime
28+
.run(async move { driver.server_version().await })
29+
.map_err(|err| Box::new(err) as Box<dyn Error + Send>)?;
30+
println!("{}", server_version);
31+
Ok(())
32+
}
33+
2434
pub(crate) fn database_list(context: &mut ConsoleContext, _input: &[String]) -> CommandResult {
2535
let driver = context.driver.clone();
2636
let databases = context
@@ -201,6 +211,55 @@ pub(crate) fn user_update_password(context: &mut ConsoleContext, input: &[String
201211
Ok(())
202212
}
203213

214+
pub(crate) fn replica_list(context: &mut ConsoleContext, _input: &[String]) -> CommandResult {
215+
let driver = context.driver.clone();
216+
let replicas = driver.replicas();
217+
if replicas.is_empty() {
218+
println!("No replicas are present.");
219+
} else {
220+
for replica in replicas {
221+
println!("{}", replica.address());
222+
}
223+
}
224+
Ok(())
225+
}
226+
227+
pub(crate) fn replica_primary(context: &mut ConsoleContext, _input: &[String]) -> CommandResult {
228+
let driver = context.driver.clone();
229+
let primary_replica = driver.primary_replica();
230+
if let Some(primary_replica) = primary_replica {
231+
println!("{}", primary_replica.address());
232+
} else {
233+
println!("No primary replica is present.");
234+
}
235+
Ok(())
236+
}
237+
238+
pub(crate) fn replica_register(context: &mut ConsoleContext, input: &[String]) -> CommandResult {
239+
let driver = context.driver.clone();
240+
let replica_id: u64 = input[0].parse().map_err(|_| Box::new(ReplError { message: format!("Replica id '{}' must be a number.", input[0]) })
241+
as Box<dyn Error + Send>)?;
242+
let address = input[1].clone();
243+
context
244+
.background_runtime
245+
.run(async move { driver.register_replica(replica_id, address).await })
246+
.map_err(|err| Box::new(err) as Box<dyn Error + Send>)?;
247+
println!("Successfully registered replica.");
248+
Ok(())
249+
}
250+
251+
pub(crate) fn replica_deregister(context: &mut ConsoleContext, input: &[String]) -> CommandResult {
252+
let driver = context.driver.clone();
253+
let replica_id: u64 = input[0].parse().map_err(|_| Box::new(ReplError { message: format!("Replica id '{}' must be a number.", input[0]) })
254+
as Box<dyn Error + Send>)?;
255+
context
256+
.background_runtime
257+
.run(async move { driver.deregister_replica(replica_id).await })
258+
.map_err(|err| Box::new(err) as Box<dyn Error + Send>)?;
259+
println!("Successfully deregistered replica.");
260+
Ok(())
261+
}
262+
204263
pub(crate) fn transaction_read(context: &mut ConsoleContext, input: &[String]) -> CommandResult {
205264
let driver = context.driver.clone();
206265
let db_name = &input[0];

0 commit comments

Comments
 (0)