Skip to content

Commit

Permalink
remove runtime occurences
Browse files Browse the repository at this point in the history
  • Loading branch information
Hennzau committed Jan 7, 2025
1 parent 8d1f1f0 commit 960fc29
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
19 changes: 11 additions & 8 deletions zfctl/src/daemon_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub(crate) enum DaemonCommand {
#[command(verbatim_doc_comment)]
#[command(group(
ArgGroup::new("exclusive")
.args(&["id", "name"])
.args(&["daemon_id", "daemon_name"])
.required(true)
.multiple(false)
))]
Expand All @@ -82,10 +82,10 @@ pub(crate) enum DaemonCommand {
///
/// Note that if several daemons share the same name, the first to
/// answer will be selected.
name: Option<String>,
daemon_name: Option<String>,
/// The unique identifier of the Zenoh-Flow daemon to contact.
#[arg(short = 'i', long = "id")]
id: Option<RuntimeId>,
daemon_id: Option<RuntimeId>,
},
}

Expand Down Expand Up @@ -155,8 +155,11 @@ impl DaemonCommand {

println!("{table}");
}
DaemonCommand::Status { id, name } => {
let runtime_id = match (id, name) {
DaemonCommand::Status {
daemon_id,
daemon_name,
} => {
let runtime_id = match (daemon_id, daemon_name) {
(Some(id), _) => id,
(None, Some(name)) => get_runtime_by_name(&session, &name).await,
(None, None) => {
Expand All @@ -165,9 +168,9 @@ impl DaemonCommand {
// any group.
// (2) The `group` macro has `multiple = false` which indicates that only a single entry for
// any group is accepted.
// (3) The `runtime_id` and `runtime_name` fields belong to the same group "runtime".
// (3) The `daemon_id` and `daemon_name` fields belong to the same group "exclusive".
//
// => A single entry for the group "runtime" is required (and mandatory).
// => A single entry for the group "exclusive" is required (and mandatory).
unreachable!()
}
};
Expand All @@ -189,7 +192,7 @@ impl DaemonCommand {
.await
.map_err(|e| {
anyhow!(
"Failed to query Zenoh-Flow runtime < {} >: {:?}",
"Failed to query Zenoh-Flow daemon < {} >: {:?}",
runtime_id,
e
)
Expand Down
8 changes: 4 additions & 4 deletions zfctl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ enum Command {
/// to contact. If no name or id is provided, one is randomly selected.
#[command(group(
ArgGroup::new("exclusive")
.args(&["runtime_id", "daemon_name"])
.args(&["daemon_id", "daemon_name"])
.required(false)
.multiple(false)
))]
Expand All @@ -78,7 +78,7 @@ enum Command {
command: InstanceCommand,
/// The unique identifier of the Zenoh-Flow daemon to contact.
#[arg(short = 'i', long = "id", verbatim_doc_comment)]
runtime_id: Option<RuntimeId>,
daemon_id: Option<RuntimeId>,
/// The name of the Zenoh-Flow daemon to contact.
///
/// If several daemons share the same name, `zfctl` will abort
Expand Down Expand Up @@ -123,10 +123,10 @@ async fn main() -> Result<()> {
match zfctl.command {
Command::Instance {
command,
runtime_id,
daemon_id,
daemon_name,
} => {
let orchestrator_id = match (runtime_id, daemon_name) {
let orchestrator_id = match (daemon_id, daemon_name) {
(Some(id), _) => id,
(None, Some(name)) => get_runtime_by_name(&session, &name).await,
(None, None) => get_random_runtime(&session).await,
Expand Down
30 changes: 15 additions & 15 deletions zfctl/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ use zenoh::{query::ConsolidationMode, Session};
use zenoh_flow_commons::RuntimeId;
use zenoh_flow_daemon::queries::{selector_all_runtimes, RuntimeInfo, RuntimesQuery};

/// Returns the list of [RuntimeInfo] of the reachable Zenoh-Flow Runtime(s).
/// Returns the list of [RuntimeInfo] of the reachable Zenoh-Flow Daemon(s).
///
/// # Panic
///
/// This function will panic if:
/// - (internal error) the query to list the Zenoh-Flow Runtimes could not be serialised by `serde_json`,
/// - (internal error) the query to list the Zenoh-Flow Daemons could not be serialised by `serde_json`,
/// - the query on the Zenoh network failed,
/// - no Zenoh-Flow Runtime is reachable.
/// - no Zenoh-Flow Daemon is reachable.
pub(crate) async fn get_all_runtimes(session: &Session) -> Vec<RuntimeInfo> {
let value = serde_json::to_vec(&RuntimesQuery::List)
.unwrap_or_else(|e| panic!("`serde_json` failed to serialize `RuntimeQuery::List`: {e:?}"));

let runtime_replies = session
.get(selector_all_runtimes())
.payload(value)
// We want to address all the Zenoh-Flow runtimes that are reachable on the Zenoh network.
// We want to address all the Zenoh-Flow daemons that are reachable on the Zenoh network.
.consolidation(ConsolidationMode::None)
.await
.unwrap_or_else(|e| panic!("Failed to query available runtimes:\n{:?}", e));
.unwrap_or_else(|e| panic!("Failed to query available daemons:\n{:?}", e));

let mut runtimes = Vec::new();
while let Ok(reply) = runtime_replies.recv_async().await {
Expand All @@ -56,21 +56,21 @@ pub(crate) async fn get_all_runtimes(session: &Session) -> Vec<RuntimeInfo> {

if runtimes.is_empty() {
panic!(
"No Zenoh-Flow runtime were detected. Have you checked if (i) they are up and (ii) \
"No Zenoh-Flow daemon were detected. Have you checked if (i) they are up and (ii) \
reachable through Zenoh?"
);
}

runtimes
}

/// Returns the unique identifier of the Zenoh-Flow Runtime that has the provided `name`.
/// Returns the unique identifier of the Zenoh-Flow Daemon that has the provided `name`.
///
/// # Panic
///
/// This function will panic if:
/// - there is no Zenoh-Flow Runtime that has the provided name,
/// - there are more than 1 Zenoh-Flow Runtime with the provided name.
/// - there is no Zenoh-Flow Daemon that has the provided name,
/// - there are more than 1 Zenoh-Flow Daemon with the provided name.
pub(crate) async fn get_runtime_by_name(session: &Session, name: &str) -> RuntimeId {
let runtimes = get_all_runtimes(session).await;
let mut matching_runtimes = runtimes
Expand All @@ -79,29 +79,29 @@ pub(crate) async fn get_runtime_by_name(session: &Session, name: &str) -> Runtim
.collect_vec();

if matching_runtimes.is_empty() {
panic!("Found no Zenoh-Flow Runtime with name < {name} >");
panic!("Found no Zenoh-Flow Daemon with name < {name} >");
} else if matching_runtimes.len() > 1 {
tracing::error!("Found multiple Zenoh-Flow Runtimes named < {name} >:");
tracing::error!("Found multiple Zenoh-Flow Daemons named < {name} >:");
matching_runtimes.iter().for_each(|&r_info| {
tracing::error!("- {} - (id) {}", r_info.name, r_info.id);
});
panic!(
"There are multiple Zenoh-Flow Runtimes named < {name} >, please use their 'id' \
"There are multiple Zenoh-Flow Daemons named < {name} >, please use their 'id' \
instead"
);
} else {
matching_runtimes.pop().unwrap().id.clone()
}
}

/// Returns the unique identifier of a reachable Zenoh-Flow Runtime.
/// Returns the unique identifier of a reachable Zenoh-Flow Daemon.
///
/// # Panic
///
/// This function will panic if:
/// - (internal error) the query to list the Zenoh-Flow Runtimes could not be serialised by `serde_json`,
/// - (internal error) the query to list the Zenoh-Flow Daemons could not be serialised by `serde_json`,
/// - the query on the Zenoh network failed,
/// - no Zenoh-Flow Runtime is reachable.
/// - no Zenoh-Flow Daemon is reachable.
pub(crate) async fn get_random_runtime(session: &Session) -> RuntimeId {
let mut runtimes = get_all_runtimes(session).await;
let orchestrator = runtimes.remove(rand::thread_rng().gen_range(0..runtimes.len()));
Expand Down

0 comments on commit 960fc29

Please sign in to comment.