Skip to content

Commit

Permalink
feat(cli): Adding ability to resize a subnet by adding or removing nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
sasa-tomic committed Jun 6, 2023
1 parent da0f535 commit cd097f6
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 112 deletions.
13 changes: 9 additions & 4 deletions rs/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,15 @@ pub(crate) mod subnet {
verbose: bool,
},

/// Extends the size of the subnet
Extend {
/// Resize the subnet
Resize {
// Number of nodes to be added
size: usize,
#[clap(long)]
add: usize,

// Number of nodes to be removed
#[clap(long)]
remove: usize,

/// Features or Node IDs to exclude from the available nodes pool
#[clap(long, num_args(1..))]
Expand All @@ -129,7 +134,7 @@ pub(crate) mod subnet {
#[clap(long, num_args(1..))]
include: Vec<PrincipalId>,

/// Motivation for extending the subnet
/// Motivation for resing the subnet
#[clap(short, long)]
motivation: Option<String>,

Expand Down
8 changes: 4 additions & 4 deletions rs/cli/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use async_trait::async_trait;
use decentralization::SubnetChangeResponse;
use ic_base_types::PrincipalId;
use ic_management_types::{
requests::{MembershipReplaceRequest, NodesRemoveRequest, NodesRemoveResponse, SubnetExtendRequest},
requests::{MembershipReplaceRequest, NodesRemoveRequest, NodesRemoveResponse, SubnetResizeRequest},
Network, NetworkError, ReplicaRelease, SubnetMembershipChangeProposal,
};
use log::error;
Expand Down Expand Up @@ -61,11 +61,11 @@ impl DashboardBackendClient {
.await
}

pub async fn subnet_extend(&self, request: SubnetExtendRequest) -> anyhow::Result<SubnetChangeResponse> {
pub async fn subnet_resize(&self, request: SubnetResizeRequest) -> anyhow::Result<SubnetChangeResponse> {
reqwest::Client::new()
.post(
self.url
.join("subnet/membership/extend")
.join("subnet/membership/resize")
.map_err(|e| anyhow::anyhow!(e))?,
)
.json(&request)
Expand Down Expand Up @@ -101,7 +101,7 @@ impl RESTRequestBuilder for reqwest::RequestBuilder {
if let Err(e) = response_result.error_for_status_ref() {
let response = response_result.text().await?;
match serde_json::from_str(&response) {
Ok(NetworkError::ExtensionFailed(s)) => {
Ok(NetworkError::ResizeFailed(s)) => {
error!("{}", s);
Err(anyhow::anyhow!("failed request (error: {})", e))
}
Expand Down
9 changes: 5 additions & 4 deletions rs/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async fn main() -> Result<(), anyhow::Error> {
cli::Commands::Subnet(subnet) => {
let runner = runner::Runner::from_opts(&cli_opts).await?;
match &subnet.subcommand {
cli::subnet::Commands::Deploy { .. } | cli::subnet::Commands::Extend { .. } => {
cli::subnet::Commands::Deploy { .. } | cli::subnet::Commands::Resize { .. } => {
if subnet.id.is_none() {
cmd.error(ErrorKind::MissingRequiredArgument, "Required argument `id` not found")
.exit();
Expand Down Expand Up @@ -125,11 +125,12 @@ async fn main() -> Result<(), anyhow::Error> {
}, *verbose)
.await
}
cli::subnet::Commands::Extend { size, include, exclude, motivation, verbose, } => {
cli::subnet::Commands::Resize { add, remove, include, exclude, motivation, verbose, } => {
if let Some(motivation) = motivation.clone() {
runner.subnet_extend(ic_management_types::requests::SubnetExtendRequest {
runner.subnet_resize(ic_management_types::requests::SubnetResizeRequest {
subnet: subnet.id.unwrap(),
size: *size,
add: *add,
remove: *remove,
exclude: exclude.clone().into(),
include: include.clone().into(),
}, motivation, *verbose).await
Expand Down
45 changes: 32 additions & 13 deletions rs/cli/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,47 @@ impl Runner {
Ok(())
}

pub async fn subnet_extend(
pub async fn subnet_resize(
&self,
request: ic_management_types::requests::SubnetExtendRequest,
request: ic_management_types::requests::SubnetResizeRequest,
motivation: String,
verbose: bool,
) -> anyhow::Result<()> {
let subnet = request.subnet;
let change = self.dashboard_backend_client.subnet_extend(request).await?;
let change = self.dashboard_backend_client.subnet_resize(request).await?;
if verbose {
if let Some(run_log) = &change.run_log {
println!("{}\n", run_log.join("\n"));
}
}
println!("{}", change);

self.run_membership_change(
change,
ProposeOptions {
title: format!("Extend subnet {subnet}").into(),
summary: format!("Extend subnet {subnet}").into(),
motivation: motivation.clone().into(),
simulate: false,
},
)
.await
if change.added.is_empty() && change.removed.is_empty() {
return Ok(());
}
if change.added.len() == change.removed.len() {
self.run_membership_change(
change.clone(),
ops_subnet_node_replace::replace_proposal_options(&change)?,
)
.await
} else {
let action = if change.added.len() < change.removed.len() {
"Removing nodes from"
} else {
"Adding nodes to"
};
self.run_membership_change(
change,
ProposeOptions {
title: format!("{action} subnet {subnet}").into(),
summary: format!("{action} subnet {subnet}").into(),
motivation: motivation.clone().into(),
simulate: false,
},
)
.await
}
}

pub async fn membership_replace(
Expand All @@ -76,6 +92,9 @@ impl Runner {
}
println!("{}", change);

if change.added.is_empty() && change.removed.is_empty() {
return Ok(());
}
self.run_membership_change(
change.clone(),
ops_subnet_node_replace::replace_proposal_options(&change)?,
Expand Down
3 changes: 3 additions & 0 deletions rs/decentralization/src/nakamoto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ mod tests {
DecentralizedSubnet {
id: PrincipalId::new_subnet_test_id(subnet_num),
nodes: new_test_nodes("feat", num_nodes, num_dfinity_nodes),
removed_nodes: Vec::new(),
min_nakamoto_coefficients: None,
comment: None,
run_log: Vec::new(),
Expand All @@ -474,6 +475,7 @@ mod tests {
num_dfinity_nodes,
feature_to_override,
),
removed_nodes: Vec::new(),
min_nakamoto_coefficients: None,
comment: None,
run_log: Vec::new(),
Expand Down Expand Up @@ -713,6 +715,7 @@ mod tests {
.cloned()
.filter(|n| !re_unhealthy_nodes.is_match(&n.id.to_string()))
.collect(),
removed_nodes: Vec::new(),
min_nakamoto_coefficients: None,
comment: None,
run_log: Vec::new(),
Expand Down
Loading

0 comments on commit cd097f6

Please sign in to comment.