krishvishal commented on code in PR #2675:
URL: https://github.com/apache/iggy/pull/2675#discussion_r2797920275


##########
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:
   Done. I've added a new field. But it doesn't do anything else for now. Added 
a TODO. 



##########
core/metadata/src/impls/metadata.rs:
##########
@@ -24,6 +26,106 @@ use journal::{Journal, JournalHandle};
 use message_bus::MessageBus;
 use tracing::{debug, warn};
 
+/// Trait for metadata snapshot implementations.
+///
+/// This is the interface that `MetadataHandle::Snapshot` must satisfy.
+/// It provides methods for creating, encoding, decoding, and restoring 
snapshots.
+#[allow(unused)]
+pub trait Snapshot: Sized {
+    /// The error type for snapshot operations.
+    type Error: std::error::Error;
+
+    /// Create a snapshot from the current state of a state machine.
+    ///
+    /// # Arguments
+    /// * `stm` - The state machine to snapshot
+    /// * `sequence_number` - Monotonically increasing snapshot sequence number
+    fn create<T>(stm: &T, sequence_number: u64) -> Result<Self, Self::Error>
+    where
+        T: FillSnapshot;
+
+    /// Encode the snapshot to msgpack bytes.
+    fn encode(&self) -> Result<Vec<u8>, Self::Error>;
+
+    /// Decode a snapshot from msgpack bytes.
+    fn decode(bytes: &[u8]) -> Result<Self, Self::Error>;
+
+    /// Restore a state machine from this snapshot.
+    fn restore<T>(&self) -> Result<T, Self::Error>
+    where
+        T: RestoreSnapshot;
+
+    /// Get the snapshot sequence number.
+    fn sequence_number(&self) -> u64;
+
+    /// Get the timestamp when this snapshot was created.
+    fn created_at(&self) -> u64;
+}

Review Comment:
   Done.



-- 
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]

Reply via email to