|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//! Many of the ZFS operations sled-agent performs are not atomic, because they |
| 6 | +//! involve multiple lower-level ZFS operations. This module implements a tokio |
| 7 | +//! task that serializes a set of operations to ensure no two operations could |
| 8 | +//! be executing concurrently. |
| 9 | +//! |
| 10 | +//! It uses the common pattern of "a task with a mpsc channel to send requests, |
| 11 | +//! using oneshot channels to send responses". |
| 12 | +
|
| 13 | +use camino::Utf8PathBuf; |
| 14 | +use id_map::IdMap; |
| 15 | +use id_map::IdMappable; |
| 16 | +use nexus_sled_agent_shared::inventory::InventoryDataset; |
| 17 | +use omicron_common::disk::DatasetConfig; |
| 18 | +use omicron_common::zpool_name::ZpoolName; |
| 19 | +use omicron_uuid_kinds::DatasetUuid; |
| 20 | +use sled_storage::config::MountConfig; |
| 21 | +use sled_storage::manager::NestedDatasetConfig; |
| 22 | +use sled_storage::manager::NestedDatasetListOptions; |
| 23 | +use sled_storage::manager::NestedDatasetLocation; |
| 24 | +use slog::Logger; |
| 25 | +use slog::warn; |
| 26 | +use std::collections::BTreeSet; |
| 27 | +use std::sync::Arc; |
| 28 | +use tokio::sync::mpsc; |
| 29 | + |
| 30 | +#[derive(Debug, thiserror::Error)] |
| 31 | +pub enum DatasetTaskError { |
| 32 | + #[error("cannot perform dataset operations: waiting for key manager")] |
| 33 | + WaitingForKeyManager, |
| 34 | + #[error("dataset task busy; cannot service new requests")] |
| 35 | + Busy, |
| 36 | + #[error("internal error: dataset task exited!")] |
| 37 | + Exited, |
| 38 | +} |
| 39 | + |
| 40 | +#[derive(Debug)] |
| 41 | +pub(crate) struct DatasetEnsureResult(IdMap<SingleDatasetEnsureResult>); |
| 42 | + |
| 43 | +#[derive(Debug, Clone)] |
| 44 | +struct SingleDatasetEnsureResult { |
| 45 | + config: DatasetConfig, |
| 46 | + state: DatasetState, |
| 47 | +} |
| 48 | + |
| 49 | +impl IdMappable for SingleDatasetEnsureResult { |
| 50 | + type Id = DatasetUuid; |
| 51 | + |
| 52 | + fn id(&self) -> Self::Id { |
| 53 | + self.config.id |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#[derive(Debug, Clone)] |
| 58 | +enum DatasetState { |
| 59 | + Mounted, |
| 60 | + FailedToMount, // TODO add error |
| 61 | + UuidMismatch(DatasetUuid), |
| 62 | + ZpoolNotFound, |
| 63 | + ParentMissingFromConfig, |
| 64 | + ParentFailedToMount, |
| 65 | +} |
| 66 | + |
| 67 | +#[derive(Debug)] |
| 68 | +pub(crate) struct DatasetTaskHandle(mpsc::Sender<DatasetTaskRequest>); |
| 69 | + |
| 70 | +impl DatasetTaskHandle { |
| 71 | + pub fn spawn_dataset_task( |
| 72 | + mount_config: Arc<MountConfig>, |
| 73 | + base_log: &Logger, |
| 74 | + ) -> Self { |
| 75 | + // We don't expect too many concurrent requests to this task, and want |
| 76 | + // to detect "the task is wedged" pretty quickly. Common operations: |
| 77 | + // |
| 78 | + // 1. Reconciler wants to ensure datasets (at most 1 at a time) |
| 79 | + // 2. Inventory requests from Nexus (likely at most 3 at a time) |
| 80 | + // 3. Support bundle operations (unlikely to be multiple concurrently) |
| 81 | + // |
| 82 | + // so we'll pick a number that allows all of those plus a little |
| 83 | + // overhead. |
| 84 | + let (request_tx, request_rx) = mpsc::channel(16); |
| 85 | + |
| 86 | + tokio::spawn( |
| 87 | + DatasetTask { |
| 88 | + mount_config, |
| 89 | + request_rx, |
| 90 | + log: base_log.new(slog::o!("component" => "DatasetTask")), |
| 91 | + } |
| 92 | + .run(), |
| 93 | + ); |
| 94 | + |
| 95 | + Self(request_tx) |
| 96 | + } |
| 97 | + |
| 98 | + pub async fn datasets_ensure( |
| 99 | + &self, |
| 100 | + _dataset_configs: IdMap<DatasetConfig>, |
| 101 | + _zpools: BTreeSet<ZpoolName>, |
| 102 | + ) -> Result<DatasetEnsureResult, DatasetTaskError> { |
| 103 | + unimplemented!() |
| 104 | + } |
| 105 | + |
| 106 | + pub async fn inventory( |
| 107 | + &self, |
| 108 | + _zpools: BTreeSet<ZpoolName>, |
| 109 | + ) -> Result<Vec<InventoryDataset>, DatasetTaskError> { |
| 110 | + unimplemented!() |
| 111 | + } |
| 112 | + |
| 113 | + pub async fn nested_dataset_ensure_mounted( |
| 114 | + &self, |
| 115 | + _dataset: NestedDatasetLocation, |
| 116 | + ) -> Result<Utf8PathBuf, DatasetTaskError> { |
| 117 | + unimplemented!() |
| 118 | + } |
| 119 | + |
| 120 | + pub async fn nested_dataset_ensure( |
| 121 | + &self, |
| 122 | + _config: NestedDatasetConfig, |
| 123 | + ) -> Result<(), DatasetTaskError> { |
| 124 | + unimplemented!() |
| 125 | + } |
| 126 | + |
| 127 | + pub async fn nested_dataset_destroy( |
| 128 | + &self, |
| 129 | + _name: NestedDatasetLocation, |
| 130 | + ) -> Result<(), DatasetTaskError> { |
| 131 | + unimplemented!() |
| 132 | + } |
| 133 | + |
| 134 | + pub async fn nested_dataset_list( |
| 135 | + &self, |
| 136 | + _name: NestedDatasetLocation, |
| 137 | + _options: NestedDatasetListOptions, |
| 138 | + ) -> Result<Vec<NestedDatasetConfig>, DatasetTaskError> { |
| 139 | + unimplemented!() |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +struct DatasetTask { |
| 144 | + mount_config: Arc<MountConfig>, |
| 145 | + request_rx: mpsc::Receiver<DatasetTaskRequest>, |
| 146 | + log: Logger, |
| 147 | +} |
| 148 | + |
| 149 | +impl DatasetTask { |
| 150 | + async fn run(mut self) { |
| 151 | + while let Some(req) = self.request_rx.recv().await { |
| 152 | + self.handle_request(req).await; |
| 153 | + } |
| 154 | + warn!(self.log, "all request handles closed; exiting dataset task"); |
| 155 | + } |
| 156 | + |
| 157 | + async fn handle_request(&mut self, _req: DatasetTaskRequest) { |
| 158 | + unimplemented!() |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +enum DatasetTaskRequest {} |
0 commit comments