Skip to content

Commit

Permalink
Fix v0 deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjstone committed Nov 6, 2023
1 parent 4dbaecc commit 52e4932
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
15 changes: 9 additions & 6 deletions common/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use async_trait::async_trait;
use camino::{Utf8Path, Utf8PathBuf};
use serde::{de::DeserializeOwned, Serialize};
use slog::{debug, warn, Logger};
use slog::{error, info, warn, Logger};

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down Expand Up @@ -84,8 +84,11 @@ impl<T: Ledgerable> Ledger<T> {
// Read all the ledgers that we can.
let mut ledgers = vec![];
for path in paths.iter() {
if let Ok(ledger) = T::read_from(log, &path).await {
ledgers.push(ledger);
match T::read_from(log, &path).await {
Ok(ledger) => ledgers.push(ledger),
Err(err) => {
error!(log, "Failed to read ledger: {err}"; "path" => %path)
}
}
}

Expand Down Expand Up @@ -169,7 +172,7 @@ pub trait Ledgerable: DeserializeOwned + Serialize + Send + Sync {
/// Reads from `path` as a json-serialized version of `Self`.
async fn read_from(log: &Logger, path: &Utf8Path) -> Result<Self, Error> {
if path.exists() {
debug!(log, "Reading ledger from {}", path);
info!(log, "Reading ledger from {}", path);
<Self as Ledgerable>::deserialize(
&tokio::fs::read_to_string(&path)
.await
Expand All @@ -180,7 +183,7 @@ pub trait Ledgerable: DeserializeOwned + Serialize + Send + Sync {
err,
})
} else {
debug!(log, "No ledger in {path}");
warn!(log, "No ledger in {path}");
Err(Error::NotFound)
}
}
Expand All @@ -191,7 +194,7 @@ pub trait Ledgerable: DeserializeOwned + Serialize + Send + Sync {
log: &Logger,
path: &Utf8Path,
) -> Result<(), Error> {
debug!(log, "Writing ledger to {}", path);
info!(log, "Writing ledger to {}", path);
let as_str = serde_json::to_string(&self).map_err(|err| {
Error::JsonSerialize { path: path.to_path_buf(), err }
})?;
Expand Down
33 changes: 21 additions & 12 deletions sled-agent/src/bootstrap/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ impl TryFrom<UnvalidatedRackInitializeRequest> for RackInitializeRequest {
pub type Certificate = nexus_client::types::Certificate;
pub type RecoverySiloConfig = nexus_client::types::RecoverySiloConfig;

// A wrapper around StartSledAgentRequestV0 that was used
// for the ledger format.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
struct PersistentSledAgentRequest {
request: StartSledAgentRequestV0,
}

/// The version of `StartSledAgentRequest` we originally shipped with.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
pub struct StartSledAgentRequestV0 {
Expand Down Expand Up @@ -312,7 +319,7 @@ impl Ledgerable for StartSledAgentRequest {

// We don't have the latest version. Try to deserialize v0 and then
// convert it to the latest version.
let v0 = serde_json::from_str::<StartSledAgentRequestV0>(s)?;
let v0 = serde_json::from_str::<PersistentSledAgentRequest>(s)?.request;
Ok(v0.into())
}
}
Expand Down Expand Up @@ -414,24 +421,26 @@ mod tests {

#[test]
fn serialize_start_sled_agent_v0_deserialize_v1() {
let v0 = StartSledAgentRequestV0 {
id: Uuid::new_v4(),
rack_id: Uuid::new_v4(),
ntp_servers: vec![String::from("test.pool.example.com")],
dns_servers: vec!["1.1.1.1".parse().unwrap()],
use_trust_quorum: false,
subnet: Ipv6Subnet::new(Ipv6Addr::LOCALHOST),
let v0 = PersistentSledAgentRequest {
request: StartSledAgentRequestV0 {
id: Uuid::new_v4(),
rack_id: Uuid::new_v4(),
ntp_servers: vec![String::from("test.pool.example.com")],
dns_servers: vec!["1.1.1.1".parse().unwrap()],
use_trust_quorum: false,
subnet: Ipv6Subnet::new(Ipv6Addr::LOCALHOST),
},
};
let serialized = serde_json::to_string(&v0).unwrap();
let expected = StartSledAgentRequest {
generation: 0,
schema_version: 1,
body: StartSledAgentRequestBody {
id: v0.id,
rack_id: v0.rack_id,
use_trust_quorum: false,
id: v0.request.id,
rack_id: v0.request.rack_id,
use_trust_quorum: v0.request.use_trust_quorum,
is_lrtq_learner: false,
subnet: v0.subnet,
subnet: v0.request.subnet,
},
};

Expand Down

0 comments on commit 52e4932

Please sign in to comment.