Skip to content

Commit fcf7ec5

Browse files
committed
Add replica management
1 parent c2d0771 commit fcf7ec5

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;
@@ -329,6 +330,9 @@ fn execute_commands(
329330
}
330331

331332
fn entry_repl(driver: Arc<TypeDBDriver>, runtime: BackgroundRuntime) -> Repl<ConsoleContext> {
333+
let server_commands = Subcommand::new("server")
334+
.add(CommandLeaf::new("version", "Retrieve server version.", server_version));
335+
332336
let database_commands = Subcommand::new("database")
333337
.add(CommandLeaf::new("list", "List databases on the server.", database_list))
334338
.add(CommandLeaf::new_with_input(
@@ -402,6 +406,25 @@ fn entry_repl(driver: Arc<TypeDBDriver>, runtime: BackgroundRuntime) -> Repl<Con
402406
user_update_password,
403407
));
404408

409+
let replica_commands = Subcommand::new("replica")
410+
.add(CommandLeaf::new("list", "List replicas.", replica_list))
411+
.add(CommandLeaf::new("primary", "Get current primary replica.", replica_primary))
412+
.add(CommandLeaf::new_with_inputs(
413+
"register",
414+
"Register new replica.",
415+
vec![
416+
CommandInput::new("replica id", get_word, None, None),
417+
CommandInput::new("address", get_word, None, None),
418+
],
419+
replica_register,
420+
))
421+
.add(CommandLeaf::new_with_input(
422+
"deregister",
423+
"Deregister existing replica.",
424+
CommandInput::new("replica id", get_word, None, None),
425+
replica_deregister,
426+
));
427+
405428
let transaction_commands = Subcommand::new("transaction")
406429
.add(CommandLeaf::new_with_input(
407430
"read",
@@ -425,8 +448,10 @@ fn entry_repl(driver: Arc<TypeDBDriver>, runtime: BackgroundRuntime) -> Repl<Con
425448
let history_path = home_dir().unwrap_or_else(|| temp_dir()).join(ENTRY_REPL_HISTORY);
426449

427450
let repl = Repl::new(PROMPT.to_owned(), history_path, false, None)
451+
.add(server_commands)
428452
.add(database_commands)
429453
.add(user_commands)
454+
.add(replica_commands)
430455
.add(transaction_commands);
431456

432457
repl

src/operations.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ use crate::{
2525
transaction_repl, ConsoleContext,
2626
};
2727

28+
pub(crate) fn server_version(context: &mut ConsoleContext, _input: &[String]) -> CommandResult {
29+
let driver = context.driver.clone();
30+
let server_version = context
31+
.background_runtime
32+
.run(async move { driver.server_version().await })
33+
.map_err(|err| Box::new(err) as Box<dyn Error + Send>)?;
34+
println!("{}", server_version);
35+
Ok(())
36+
}
37+
2838
pub(crate) fn database_list(context: &mut ConsoleContext, _input: &[String]) -> CommandResult {
2939
let driver = context.driver.clone();
3040
let databases = context
@@ -186,6 +196,55 @@ pub(crate) fn user_update_password(context: &mut ConsoleContext, input: &[String
186196
Ok(())
187197
}
188198

199+
pub(crate) fn replica_list(context: &mut ConsoleContext, _input: &[String]) -> CommandResult {
200+
let driver = context.driver.clone();
201+
let replicas = driver.replicas();
202+
if replicas.is_empty() {
203+
println!("No replicas are present.");
204+
} else {
205+
for replica in replicas {
206+
println!("{}", replica.address());
207+
}
208+
}
209+
Ok(())
210+
}
211+
212+
pub(crate) fn replica_primary(context: &mut ConsoleContext, _input: &[String]) -> CommandResult {
213+
let driver = context.driver.clone();
214+
let primary_replica = driver.primary_replica();
215+
if let Some(primary_replica) = primary_replica {
216+
println!("{}", primary_replica.address());
217+
} else {
218+
println!("No primary replica is present.");
219+
}
220+
Ok(())
221+
}
222+
223+
pub(crate) fn replica_register(context: &mut ConsoleContext, input: &[String]) -> CommandResult {
224+
let driver = context.driver.clone();
225+
let replica_id: u64 = input[0].parse().map_err(|_| Box::new(ReplError { message: format!("Replica id '{}' must be a number.", input[0]) })
226+
as Box<dyn Error + Send>)?;
227+
let address = input[1].clone();
228+
context
229+
.background_runtime
230+
.run(async move { driver.register_replica(replica_id, address).await })
231+
.map_err(|err| Box::new(err) as Box<dyn Error + Send>)?;
232+
println!("Successfully registered replica.");
233+
Ok(())
234+
}
235+
236+
pub(crate) fn replica_deregister(context: &mut ConsoleContext, input: &[String]) -> CommandResult {
237+
let driver = context.driver.clone();
238+
let replica_id: u64 = input[0].parse().map_err(|_| Box::new(ReplError { message: format!("Replica id '{}' must be a number.", input[0]) })
239+
as Box<dyn Error + Send>)?;
240+
context
241+
.background_runtime
242+
.run(async move { driver.deregister_replica(replica_id).await })
243+
.map_err(|err| Box::new(err) as Box<dyn Error + Send>)?;
244+
println!("Successfully deregistered replica.");
245+
Ok(())
246+
}
247+
189248
pub(crate) fn transaction_read(context: &mut ConsoleContext, input: &[String]) -> CommandResult {
190249
let driver = context.driver.clone();
191250
let db_name = &input[0];

0 commit comments

Comments
 (0)