-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathupgrade_subnets.rs
More file actions
255 lines (224 loc) · 9.29 KB
/
upgrade_subnets.rs
File metadata and controls
255 lines (224 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use std::{fmt::Display, time::Duration};
use backon::{ExponentialBuilder, Retryable};
use comfy_table::CellAlignment;
use ic_registry_subnet_type::SubnetType;
use ic_types::PrincipalId;
use itertools::Itertools;
use reqwest::ClientBuilder;
use crate::{
ic_admin::{IcAdminProposal, IcAdminProposalCommand, IcAdminProposalOptions},
qualification::comfy_table_util::Table,
};
use super::{step::Step, util::StepCtx};
pub struct UpgradeSubnets {
pub subnet_type: Option<SubnetType>,
pub to_version: String,
pub action: Action,
}
pub enum Action {
Upgrade,
Downgrade,
}
impl Display for Action {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Action::Upgrade => "Upgrade".to_string(),
Action::Downgrade => "Downgrade".to_string(),
}
)
}
}
impl Step for UpgradeSubnets {
fn help(&self) -> String {
format!(
"{} all the {} to version {}",
self.action,
match self.subnet_type {
Some(s) => match s {
SubnetType::Application => "application subnets",
SubnetType::System => "system subnets",
SubnetType::VerifiedApplication => "verified-application subnets",
SubnetType::CloudEngine => "cloud-engine subnets",
},
None => "unassigned nodes",
},
self.to_version
)
}
fn name(&self) -> String {
format!(
"{}_{}_version",
self.action,
match self.subnet_type {
Some(s) => match s {
SubnetType::Application => "application_subnet",
SubnetType::System => "system_subnet",
SubnetType::VerifiedApplication => "verified-application_subnet",
SubnetType::CloudEngine => "cloud-engine_subnet",
},
None => "unassigned_nodes",
}
)
}
async fn execute(&self, ctx: &StepCtx) -> anyhow::Result<()> {
let registry = ctx.dre_ctx().registry().await;
let subnets = registry.subnets().await?;
ctx.print_text(format!("Found total of {} nodes", registry.nodes().await?.len()));
ctx.print_subnet_versions().await?;
if let Some(subnet_type) = &self.subnet_type {
for subnet in subnets
.values()
.filter(|s| s.subnet_type.eq(subnet_type) && !s.replica_version.eq(&self.to_version))
{
ctx.print_text(format!(
"Upgrading subnet {}: {} -> {}",
subnet.principal, &subnet.replica_version, &self.to_version
));
// Place proposal
let place_proposal = || async {
ctx.dre_ctx()
.ic_admin_executor()
.await?
.submit(
&IcAdminProposal::new(
IcAdminProposalCommand::DeployGuestosToAllSubnetNodes {
subnet: subnet.principal,
version: self.to_version.clone(),
},
IcAdminProposalOptions {
title: Some(format!("Propose to upgrade subnet {} to {}", subnet.principal, &self.to_version)),
summary: Some("Qualification testing".to_string()),
motivation: Some("Qualification testing".to_string()),
},
),
None,
)
.await
};
place_proposal.retry(ExponentialBuilder::default()).await?;
ctx.print_text(format!("Placed proposal for subnet {}", subnet.principal));
// Wait for the version to be active on the subnet
wait_for_subnet_revision(ctx, Some(subnet.principal), &self.to_version).await?;
ctx.print_text(format!(
"Subnet {} successfully upgraded to version {}",
subnet.principal, &self.to_version
));
ctx.print_subnet_versions().await?;
}
} else {
let registry = ctx.dre_ctx().registry().await;
let unassigned_nodes_version = registry.unassigned_nodes_replica_version().await?;
if unassigned_nodes_version.to_string() == self.to_version {
ctx.print_text(format!("Unassigned nodes are already on {}, skipping", self.to_version));
return Ok(());
}
ctx.print_text(format!(
"Upgrading unassigned version: {} -> {}",
&unassigned_nodes_version, &self.to_version
));
let place_proposal = || async {
ctx.dre_ctx()
.ic_admin_executor()
.await?
.submit(
&IcAdminProposal::new(
IcAdminProposalCommand::DeployGuestosToAllUnassignedNodes {
replica_version: self.to_version.clone(),
},
IcAdminProposalOptions {
title: Some("Upgrading unassigned nodes".to_string()),
summary: Some("Upgrading unassigned nodes".to_string()),
motivation: Some("Upgrading unassigned nodes".to_string()),
},
),
None,
)
.await
};
place_proposal.retry(ExponentialBuilder::default()).await?;
wait_for_subnet_revision(ctx, None, &self.to_version).await?;
ctx.print_text(format!("Unassigned nodes successfully upgraded to version {}", &self.to_version));
ctx.print_subnet_versions().await?;
}
Ok(())
}
}
const MAX_TRIES: usize = 100;
const SLEEP: Duration = Duration::from_secs(10);
const TIMEOUT: Duration = Duration::from_secs(60);
const PLACEHOLDER: &str = "upgrading...";
async fn wait_for_subnet_revision(ctx: &StepCtx, subnet: Option<PrincipalId>, revision: &str) -> anyhow::Result<()> {
let client = ClientBuilder::new().timeout(TIMEOUT).build()?;
let registry = ctx.dre_ctx().registry().await;
for i in 0..MAX_TRIES {
tokio::time::sleep(SLEEP).await;
ctx.print_text(format!(
"- {} - Checking if {} on {}",
i,
match &subnet {
Some(p) => format!("{} subnet is", p),
None => "unassigned nodes are".to_string(),
},
revision
));
if let Err(e) = registry.sync_with_nns().await {
ctx.print_text(format!("Received error when syncing registry: {}", e));
continue;
}
// Fetch the nodes of the subnet
let nodes = registry.nodes().await?;
let nodes = nodes.values().filter(|n| n.subnet_id.eq(&subnet)).collect_vec();
let mut nodes_with_reivison = vec![];
// Fetch the metrics of each node and check if it
// contains the revision somewhere
for node in nodes {
let url = format!("http://[{}]:9090/metrics", node.ip_addr.unwrap());
let response = match client.get(&url).send().await {
Ok(r) => match r.error_for_status() {
Ok(r) => match r.text().await {
Ok(r) => r,
Err(e) => {
ctx.print_text(format!("Received error {}, skipping...", e));
continue;
}
},
Err(e) => {
ctx.print_text(format!("Received error {}, skipping...", e));
continue;
}
},
Err(e) => {
ctx.print_text(format!("Received error {}, skipping...", e));
continue;
}
};
if response.contains(revision) {
nodes_with_reivison.push((node.principal.to_string(), revision));
continue;
}
nodes_with_reivison.push((node.principal.to_string(), PLACEHOLDER));
}
// print the status of nodes and versions
let table = Table::new()
.with_columns(&[("Node Id", CellAlignment::Center), ("Revision", CellAlignment::Left)])
.with_rows(
nodes_with_reivison
.iter()
.map(|(pr, v)| vec![pr.to_string(), v.to_string()])
.collect_vec(),
)
.to_table();
ctx.print_table(table);
// Check if done
if !nodes_with_reivison.iter().any(|(_, r)| *r == PLACEHOLDER) {
return Ok(());
}
}
anyhow::bail!(
"Maximum number of retires reached and the revision is not empty on all nodes in the subnet {}",
subnet.map(|p| p.to_string()).unwrap_or("of unassigned nodes".to_string())
)
}