hubcio commented on code in PR #2675: URL: https://github.com/apache/iggy/pull/2675#discussion_r2793340512
########## core/metadata/src/stm/snapshot.rs: ########## @@ -0,0 +1,260 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use serde::{Deserialize, Serialize, de::DeserializeOwned}; +use std::fmt; + +use crate::stm::consumer_group::ConsumerGroupsSnapshot; +use crate::stm::stream::StreamsSnapshot; +use crate::stm::user::UsersSnapshot; + +#[derive(Debug)] +pub enum SnapshotError { + /// A required section is missing from the snapshot. + MissingSection(&'static str), + /// Serialization failed. + Serialize(rmp_serde::encode::Error), + /// Deserialization failed. + Deserialize(rmp_serde::decode::Error), + /// Slab ID mismatch during snapshot restore. + SlabIdMismatch { + section: &'static str, + expected: usize, + actual: usize, + }, +} + +impl fmt::Display for SnapshotError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + SnapshotError::MissingSection(name) => { + write!(f, "missing snapshot section: {}", name) + } + SnapshotError::Serialize(e) => write!(f, "snapshot serialization failed: {}", e), + SnapshotError::Deserialize(e) => write!(f, "snapshot deserialization failed: {}", e), + SnapshotError::SlabIdMismatch { + section, + expected, + actual, + } => { + write!( + f, + "slab ID mismatch in section '{}': expected {}, got {}", + section, expected, actual + ) + } + } + } +} + +impl std::error::Error for SnapshotError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + SnapshotError::Serialize(e) => Some(e), + SnapshotError::Deserialize(e) => Some(e), + _ => None, + } + } +} + +/// The snapshot container for all metadata state machines. +/// Each field corresponds to one state machine's serialized state. +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +pub struct MetadataSnapshot { + /// Timestamp when the snapshot was created (microseconds since epoch). + pub created_at: u64, + /// Monotonically increasing snapshot sequence number. + pub sequence_number: u64, + /// Users state machine snapshot data. + pub users: Option<UsersSnapshot>, + /// Streams state machine snapshot data. + pub streams: Option<StreamsSnapshot>, + /// Consumer groups state machine snapshot data. + pub consumer_groups: Option<ConsumerGroupsSnapshot>, +} Review Comment: I think we should add `version: u32` field. What if someone will add a new node to cluster, which will have other snapshot format because he wants to update cluster one-by-one, while maintaining 100% uptime on production? This opens up possibility of implementing migrators. I'm not saying this should be done now (migrators) but version should be OK to put for now. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
