-
Notifications
You must be signed in to change notification settings - Fork 38
/
blueprint_execution.rs
596 lines (543 loc) · 20.6 KB
/
blueprint_execution.rs
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Background task for realizing a plan blueprint
use crate::app::background::{Activator, BackgroundTask};
use futures::future::BoxFuture;
use futures::FutureExt;
use internal_dns::resolver::Resolver;
use nexus_db_queries::context::OpContext;
use nexus_db_queries::db::DataStore;
use nexus_reconfigurator_execution::RealizeBlueprintOutput;
use nexus_types::deployment::{
execution::EventBuffer, Blueprint, BlueprintTarget,
};
use serde_json::json;
use std::sync::Arc;
use tokio::sync::watch;
use update_engine::NestedError;
use uuid::Uuid;
/// Background task that takes a [`Blueprint`] and realizes the change to
/// the state of the system based on the `Blueprint`.
pub struct BlueprintExecutor {
datastore: Arc<DataStore>,
resolver: Resolver,
rx_blueprint: watch::Receiver<Option<Arc<(BlueprintTarget, Blueprint)>>>,
nexus_id: Uuid,
tx: watch::Sender<usize>,
saga_recovery: Activator,
}
impl BlueprintExecutor {
pub fn new(
datastore: Arc<DataStore>,
resolver: Resolver,
rx_blueprint: watch::Receiver<
Option<Arc<(BlueprintTarget, Blueprint)>>,
>,
nexus_id: Uuid,
saga_recovery: Activator,
) -> BlueprintExecutor {
let (tx, _) = watch::channel(0);
BlueprintExecutor {
datastore,
resolver,
rx_blueprint,
nexus_id,
tx,
saga_recovery,
}
}
pub fn watcher(&self) -> watch::Receiver<usize> {
self.tx.subscribe()
}
/// Implementation for `BackgroundTask::activate` for `BlueprintExecutor`,
/// added here to produce better compile errors.
///
/// The presence of `boxed()` in `BackgroundTask::activate` has caused some
/// confusion with compilation errors in the past. So separate this method
/// out.
async fn activate_impl<'a>(
&mut self,
opctx: &OpContext,
) -> serde_json::Value {
// Get the latest blueprint, cloning to prevent holding a read lock
// on the watch.
let update = self.rx_blueprint.borrow_and_update().clone();
let Some(update) = update else {
warn!(
&opctx.log, "Blueprint execution: skipped";
"reason" => "no blueprint",
);
return json!({"error": "no blueprint" });
};
let (bp_target, blueprint) = &*update;
if !bp_target.enabled {
warn!(&opctx.log,
"Blueprint execution: skipped";
"reason" => "blueprint disabled",
"target_id" => %blueprint.id);
return json!({
"target_id": blueprint.id.to_string(),
"enabled": false,
});
}
let (sender, mut receiver) = update_engine::channel();
let receiver_task = tokio::spawn(async move {
// TODO: report progress
let mut event_buffer = EventBuffer::default();
while let Some(event) = receiver.recv().await {
event_buffer.add_event(event);
}
event_buffer.generate_report()
});
let result = nexus_reconfigurator_execution::realize_blueprint(
opctx,
&self.datastore,
&self.resolver,
blueprint,
self.nexus_id,
sender,
)
.await;
// Get the report for the receiver task.
let event_report =
receiver_task.await.map_err(|error| NestedError::new(&error));
// Trigger anybody waiting for this to finish.
self.tx.send_modify(|count| *count = *count + 1);
// Return the result as a `serde_json::Value`
match result {
Ok(RealizeBlueprintOutput { needs_saga_recovery }) => {
// If executing the blueprint requires activating the saga
// recovery background task, do that now.
if needs_saga_recovery {
info!(&opctx.log, "activating saga recovery task");
self.saga_recovery.activate();
}
json!({
"target_id": blueprint.id.to_string(),
"enabled": true,
// Note: The field "error" is treated as special by omdb,
// and if that field is present then nothing else is
// displayed.
"execution_error": null,
"needs_saga_recovery": needs_saga_recovery,
"event_report": event_report,
})
}
Err(error) => {
json!({
"target_id": blueprint.id.to_string(),
"enabled": true,
// Note: The field "error" is treated as special by omdb,
// and if that field is present then nothing else is
// displayed.
"execution_error": NestedError::new(error.as_ref()),
"event_report": event_report,
})
}
}
}
}
impl BackgroundTask for BlueprintExecutor {
fn activate<'a>(
&'a mut self,
opctx: &'a OpContext,
) -> BoxFuture<'a, serde_json::Value> {
self.activate_impl(opctx).boxed()
}
}
#[cfg(test)]
mod test {
use super::BlueprintExecutor;
use crate::app::background::{Activator, BackgroundTask};
use httptest::matchers::{all_of, request};
use httptest::responders::status_code;
use httptest::Expectation;
use nexus_db_model::{
ByteCount, SledBaseboard, SledSystemHardware, SledUpdate, Zpool,
};
use nexus_db_queries::authn;
use nexus_db_queries::context::OpContext;
use nexus_db_queries::db::DataStore;
use nexus_sled_agent_shared::inventory::OmicronZoneDataset;
use nexus_test_utils_macros::nexus_test;
use nexus_types::deployment::execution::{
EventBuffer, EventReport, ExecutionComponent, ExecutionStepId,
ReconfiguratorExecutionSpec, StepInfo,
};
use nexus_types::deployment::BlueprintZoneFilter;
use nexus_types::deployment::{
blueprint_zone_type, Blueprint, BlueprintPhysicalDisksConfig,
BlueprintTarget, BlueprintZoneConfig, BlueprintZoneDisposition,
BlueprintZoneType, BlueprintZonesConfig, CockroachDbPreserveDowngrade,
};
use nexus_types::external_api::views::SledState;
use omicron_common::api::external::Generation;
use omicron_common::zpool_name::ZpoolName;
use omicron_uuid_kinds::GenericUuid;
use omicron_uuid_kinds::OmicronZoneUuid;
use omicron_uuid_kinds::SledUuid;
use omicron_uuid_kinds::ZpoolUuid;
use serde::Deserialize;
use serde_json::json;
use std::collections::BTreeMap;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::sync::watch;
use update_engine::{NestedError, TerminalKind};
use uuid::Uuid;
type ControlPlaneTestContext =
nexus_test_utils::ControlPlaneTestContext<crate::Server>;
async fn create_blueprint(
datastore: &DataStore,
opctx: &OpContext,
blueprint_zones: BTreeMap<SledUuid, BlueprintZonesConfig>,
blueprint_disks: BTreeMap<SledUuid, BlueprintPhysicalDisksConfig>,
dns_version: Generation,
) -> (BlueprintTarget, Blueprint) {
let id = Uuid::new_v4();
// Assume all sleds are active.
let sled_state = blueprint_zones
.keys()
.copied()
.map(|sled_id| (sled_id, SledState::Active))
.collect::<BTreeMap<_, _>>();
// Ensure the blueprint we're creating is the current target (required
// for successful blueprint realization). This requires its parent to be
// the existing target, so fetch that first.
let current_target = datastore
.blueprint_target_get_current(opctx)
.await
.expect("fetched current target blueprint");
let target = BlueprintTarget {
target_id: id,
enabled: true,
time_made_target: chrono::Utc::now(),
};
let blueprint = Blueprint {
id,
blueprint_zones,
blueprint_disks,
sled_state,
cockroachdb_setting_preserve_downgrade:
CockroachDbPreserveDowngrade::DoNotModify,
parent_blueprint_id: Some(current_target.target_id),
internal_dns_version: dns_version,
external_dns_version: dns_version,
cockroachdb_fingerprint: String::new(),
time_created: chrono::Utc::now(),
creator: "test".to_string(),
comment: "test blueprint".to_string(),
};
datastore
.blueprint_insert(opctx, &blueprint)
.await
.expect("inserted new blueprint");
datastore
.blueprint_target_set_current(opctx, target)
.await
.expect("set new blueprint as current target");
(target, blueprint)
}
#[nexus_test(server = crate::Server)]
async fn test_deploy_omicron_zones(cptestctx: &ControlPlaneTestContext) {
// Set up the test.
let nexus = &cptestctx.server.server_context().nexus;
let datastore = nexus.datastore();
let resolver = nexus.resolver();
let opctx = OpContext::for_background(
cptestctx.logctx.log.clone(),
nexus.authz.clone(),
authn::Context::internal_api(),
datastore.clone(),
);
// Create some fake sled-agent servers to respond to zone puts and add
// sleds to CRDB.
let mut s1 = httptest::Server::run();
let mut s2 = httptest::Server::run();
let sled_id1 = SledUuid::new_v4();
let sled_id2 = SledUuid::new_v4();
let rack_id = Uuid::new_v4();
for (i, (sled_id, server)) in
[(sled_id1, &s1), (sled_id2, &s2)].iter().enumerate()
{
let SocketAddr::V6(addr) = server.addr() else {
panic!("Expected Ipv6 address. Got {}", server.addr());
};
let update = SledUpdate::new(
sled_id.into_untyped_uuid(),
addr,
SledBaseboard {
serial_number: i.to_string(),
part_number: "test".into(),
revision: 1,
},
SledSystemHardware {
is_scrimlet: false,
usable_hardware_threads: 4,
usable_physical_ram: ByteCount(1000.into()),
reservoir_size: ByteCount(999.into()),
},
rack_id,
nexus_db_model::Generation::new(),
);
datastore
.sled_upsert(update)
.await
.expect("Failed to insert sled to db");
}
let (blueprint_tx, blueprint_rx) = watch::channel(None);
let mut task = BlueprintExecutor::new(
datastore.clone(),
resolver.clone(),
blueprint_rx,
Uuid::new_v4(),
Activator::new(),
);
// Now we're ready.
//
// With no target blueprint, the task should fail with an appropriate
// message.
let value = task.activate(&opctx).await;
assert_eq!(value, json!({"error": "no blueprint"}));
// With a target blueprint having no zones, the task should trivially
// complete and report a successful (empty) summary.
let generation = Generation::new();
let blueprint = Arc::new(
create_blueprint(
&datastore,
&opctx,
BTreeMap::new(),
BTreeMap::new(),
generation,
)
.await,
);
let blueprint_id = blueprint.1.id;
blueprint_tx.send(Some(blueprint)).unwrap();
let mut value = task.activate(&opctx).await;
let event_buffer = extract_event_buffer(&mut value);
println!("activating with no zones: {:?}", value);
assert_eq!(
value,
json!({
"target_id": blueprint_id,
"execution_error": null,
"enabled": true,
"needs_saga_recovery": false,
})
);
assert_event_buffer_completed(&event_buffer);
// Create a non-empty blueprint describing two servers and verify that
// the task correctly winds up making requests to both of them and
// reporting success.
fn make_zones(
disposition: BlueprintZoneDisposition,
) -> BlueprintZonesConfig {
let pool_id = ZpoolUuid::new_v4();
BlueprintZonesConfig {
generation: Generation::new(),
zones: vec![BlueprintZoneConfig {
disposition,
id: OmicronZoneUuid::new_v4(),
underlay_address: "::1".parse().unwrap(),
filesystem_pool: Some(ZpoolName::new_external(pool_id)),
zone_type: BlueprintZoneType::InternalDns(
blueprint_zone_type::InternalDns {
dataset: OmicronZoneDataset {
pool_name: format!("oxp_{}", pool_id)
.parse()
.unwrap(),
},
dns_address: "[::1]:0".parse().unwrap(),
gz_address: "::1".parse().unwrap(),
gz_address_index: 0,
http_address: "[::1]:12345".parse().unwrap(),
},
),
}],
}
}
let generation = generation.next();
// Both in-service and quiesced zones should be deployed.
//
// TODO: add expunged zones to the test (should not be deployed).
let mut blueprint = create_blueprint(
&datastore,
&opctx,
BTreeMap::from([
(sled_id1, make_zones(BlueprintZoneDisposition::InService)),
(sled_id2, make_zones(BlueprintZoneDisposition::Quiesced)),
]),
BTreeMap::new(),
generation,
)
.await;
// Insert records for the zpools backing the datasets in these zones.
for (sled_id, config) in
blueprint.1.all_omicron_zones(BlueprintZoneFilter::All)
{
let Some(dataset) = config.zone_type.durable_dataset() else {
continue;
};
let pool_id = dataset.dataset.pool_name.id();
let zpool = Zpool::new(
pool_id.into_untyped_uuid(),
sled_id.into_untyped_uuid(),
Uuid::new_v4(), // physical_disk_id
);
datastore
.zpool_insert(&opctx, zpool)
.await
.expect("failed to upsert zpool");
}
blueprint_tx.send(Some(Arc::new(blueprint.clone()))).unwrap();
// Make sure that requests get made to the sled agent. This is not a
// careful check of exactly what gets sent. For that, see the tests in
// nexus-reconfigurator-execution.
for s in [&mut s1, &mut s2] {
s.expect(
Expectation::matching(all_of![request::method_path(
"PUT",
"/omicron-zones"
),])
.respond_with(status_code(204)),
);
}
// Activate the task to trigger zone configuration on the sled-agents
let mut value = task.activate(&opctx).await;
let event_buffer = extract_event_buffer(&mut value);
println!("activating two sled agents: {:?}", value);
assert_eq!(
value,
json!({
"target_id": blueprint.1.id.to_string(),
"execution_error": null,
"enabled": true,
"needs_saga_recovery": false,
})
);
assert_event_buffer_completed(&event_buffer);
s1.verify_and_clear();
s2.verify_and_clear();
// Now, disable the target and make sure that we _don't_ invoke the sled
// agent. It's enough to just not set expectations.
blueprint.1.internal_dns_version =
blueprint.1.internal_dns_version.next();
blueprint.0.enabled = false;
blueprint_tx.send(Some(Arc::new(blueprint.clone()))).unwrap();
let value = task.activate(&opctx).await;
println!("when disabled: {:?}", value);
assert_eq!(
value,
json!({
"enabled": false,
"target_id": blueprint.1.id.to_string()
})
);
s1.verify_and_clear();
s2.verify_and_clear();
// Do it all again, but configure one of the servers to fail so we can
// verify the task's returned summary of what happened.
blueprint.0.enabled = true;
blueprint_tx.send(Some(Arc::new(blueprint))).unwrap();
s1.expect(
Expectation::matching(request::method_path(
"PUT",
"/omicron-zones",
))
.respond_with(status_code(204)),
);
s2.expect(
Expectation::matching(request::method_path(
"PUT",
"/omicron-zones",
))
.respond_with(status_code(500)),
);
#[derive(Deserialize)]
struct ErrorResult {
execution_error: NestedError,
}
let mut value = task.activate(&opctx).await;
let event_buffer = extract_event_buffer(&mut value);
println!("after failure: {:?}", value);
let result: ErrorResult = serde_json::from_value(value).unwrap();
assert_eq!(
result.execution_error.message(),
"step failed: Deploy Omicron zones"
);
assert_event_buffer_failed_at(
&event_buffer,
ExecutionComponent::OmicronZones,
ExecutionStepId::Ensure,
);
s1.verify_and_clear();
s2.verify_and_clear();
}
fn extract_event_buffer(value: &mut serde_json::Value) -> EventBuffer {
let event_report = value
.as_object_mut()
.expect("value is an object")
.remove("event_report")
.expect("event_report exists");
let event_report: Result<EventReport, NestedError> =
serde_json::from_value(event_report)
.expect("event_report is valid");
let event_report = event_report.expect("event_report is Ok");
let mut event_buffer = EventBuffer::default();
event_buffer.add_event_report(event_report);
event_buffer
}
fn assert_event_buffer_completed(event_buffer: &EventBuffer) {
let execution_status = event_buffer
.root_execution_summary()
.expect("event buffer has root execution summary")
.execution_status;
let terminal_info =
execution_status.terminal_info().unwrap_or_else(|| {
panic!(
"execution status has terminal info: {:?}",
execution_status
);
});
assert_eq!(
terminal_info.kind,
TerminalKind::Completed,
"execution should have completed successfully"
);
}
fn assert_event_buffer_failed_at(
event_buffer: &EventBuffer,
component: ExecutionComponent,
step_id: ExecutionStepId,
) {
let execution_status = event_buffer
.root_execution_summary()
.expect("event buffer has root execution summary")
.execution_status;
let terminal_info =
execution_status.terminal_info().unwrap_or_else(|| {
panic!(
"execution status has terminal info: {:?}",
execution_status
);
});
assert_eq!(
terminal_info.kind,
TerminalKind::Failed,
"execution should have failed"
);
let step =
event_buffer.get(&terminal_info.step_key).expect("step exists");
let step_info = StepInfo::<ReconfiguratorExecutionSpec>::from_generic(
step.step_info().clone(),
)
.expect("step info follows ReconfiguratorExecutionSpec");
assert_eq!(
(step_info.component, step_info.id),
(component, step_id),
"component and step id matches expected"
);
}
}