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


##########
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>,
+}
+
+impl MetadataSnapshot {
+    /// Create a new snapshot with the given sequence number.
+    pub fn new(sequence_number: u64) -> Self {
+        Self {
+            created_at: iggy_common::IggyTimestamp::now().as_micros(),
+            sequence_number,
+            users: None,
+            streams: None,
+            consumer_groups: None,
+        }
+    }
+
+    /// Encode the snapshot to msgpack bytes.
+    pub fn encode(&self) -> Result<Vec<u8>, SnapshotError> {
+        rmp_serde::to_vec(self).map_err(SnapshotError::Serialize)
+    }
+
+    /// Decode a snapshot from msgpack bytes.
+    pub fn decode(bytes: &[u8]) -> Result<Self, SnapshotError> {
+        rmp_serde::from_slice(bytes).map_err(SnapshotError::Deserialize)
+    }
+}
+
+/// Trait implemented by each `{Name}Inner` state machine to support 
snapshotting.
+/// Each state machine defines its own snapshot
+/// type for serialization and provides conversion methods.
+pub trait Snapshotable {
+    /// The serde-serializable snapshot representation of this state.
+    /// This should be a plain struct with only serializable types and no 
wrappers
+    /// like `Arc`, `AtomicUsize`, or other non-serializable wrappers.
+    type Snapshot: Serialize + DeserializeOwned;
+
+    /// Convert the current in-memory state into a serializable snapshot.
+    fn to_snapshot(&self) -> Self::Snapshot;
+
+    /// Restore in-memory state from a snapshot representation.
+    fn from_snapshot(snapshot: Self::Snapshot) -> Result<Self, SnapshotError>
+    where
+        Self: Sized;
+}
+
+/// Trait for filling a typed snapshot with state machine data.
+///
+/// Each state machine implements this to write its serialized state
+/// to its specific field in the `MetadataSnapshot` struct.
+pub trait FillSnapshot {
+    /// Fill the snapshot with this state machine's data.
+    fn fill_snapshot(&self, snapshot: &mut MetadataSnapshot) -> Result<(), 
SnapshotError>;
+}
+
+/// Trait for restoring state machine data from a typed snapshot.
+///
+/// Each state machine implements this to read its state from
+/// its specific field in the `MetadataSnapshot` struct.
+pub trait RestoreSnapshot: Sized {
+    /// Restore this state machine from the snapshot.
+    fn restore_snapshot(snapshot: &MetadataSnapshot) -> Result<Self, 
SnapshotError>;

Review Comment:
   You can put an bound on the `RestoreSnapshot` `trait RestoreSnapshot: 
Snapshotable` and access the assoc type from `Snapshotable`. I think the same 
applies to `FillSnapshot` trait.



##########
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;
+}
+
+#[derive(Debug, Clone)]
+#[allow(unused)]
+pub struct IggySnapshot {
+    snapshot: MetadataSnapshot,
+}
+
+#[allow(unused)]
+impl IggySnapshot {
+    pub fn new(sequence_number: u64) -> Self {
+        Self {
+            snapshot: MetadataSnapshot::new(sequence_number),
+        }
+    }
+
+    pub fn snapshot(&self) -> &MetadataSnapshot {
+        &self.snapshot
+    }
+}
+
+impl Snapshot for IggySnapshot {
+    type Error = SnapshotError;
+
+    fn create<T>(stm: &T, sequence_number: u64) -> Result<Self, SnapshotError>
+    where
+        T: FillSnapshot,
+    {
+        let mut snapshot = MetadataSnapshot {
+            created_at: IggyTimestamp::now().as_micros(),
+            sequence_number,
+            users: None,
+            streams: None,
+            consumer_groups: None,
+        };
+
+        stm.fill_snapshot(&mut snapshot)?;
+
+        Ok(Self { snapshot })
+    }
+
+    fn encode(&self) -> Result<Vec<u8>, SnapshotError> {
+        self.snapshot.encode()
+    }
+
+    fn decode(bytes: &[u8]) -> Result<Self, SnapshotError> {
+        let snapshot = MetadataSnapshot::decode(bytes)?;
+        Ok(Self { snapshot })
+    }
+
+    fn restore<T>(&self) -> Result<T, SnapshotError>
+    where
+        T: RestoreSnapshot,
+    {
+        T::restore_snapshot(&self.snapshot)
+    }
+
+    fn sequence_number(&self) -> u64 {

Review Comment:
   use assoc type for the `sequence_number` type.



##########
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;
+}
+
+#[derive(Debug, Clone)]
+#[allow(unused)]
+pub struct IggySnapshot {
+    snapshot: MetadataSnapshot,
+}
+
+#[allow(unused)]
+impl IggySnapshot {
+    pub fn new(sequence_number: u64) -> Self {
+        Self {
+            snapshot: MetadataSnapshot::new(sequence_number),
+        }
+    }
+
+    pub fn snapshot(&self) -> &MetadataSnapshot {
+        &self.snapshot
+    }
+}
+
+impl Snapshot for IggySnapshot {
+    type Error = SnapshotError;
+
+    fn create<T>(stm: &T, sequence_number: u64) -> Result<Self, SnapshotError>
+    where
+        T: FillSnapshot,
+    {
+        let mut snapshot = MetadataSnapshot {
+            created_at: IggyTimestamp::now().as_micros(),
+            sequence_number,
+            users: None,
+            streams: None,
+            consumer_groups: None,
+        };
+
+        stm.fill_snapshot(&mut snapshot)?;
+
+        Ok(Self { snapshot })
+    }
+
+    fn encode(&self) -> Result<Vec<u8>, SnapshotError> {
+        self.snapshot.encode()
+    }
+
+    fn decode(bytes: &[u8]) -> Result<Self, SnapshotError> {
+        let snapshot = MetadataSnapshot::decode(bytes)?;
+        Ok(Self { snapshot })
+    }
+
+    fn restore<T>(&self) -> Result<T, SnapshotError>
+    where
+        T: RestoreSnapshot,
+    {
+        T::restore_snapshot(&self.snapshot)
+    }
+
+    fn sequence_number(&self) -> u64 {
+        self.snapshot.sequence_number
+    }
+
+    fn created_at(&self) -> u64 {

Review Comment:
   same as above, use assoc type.



##########
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>,
+}
+
+impl MetadataSnapshot {
+    /// Create a new snapshot with the given sequence number.
+    pub fn new(sequence_number: u64) -> Self {
+        Self {
+            created_at: iggy_common::IggyTimestamp::now().as_micros(),
+            sequence_number,
+            users: None,
+            streams: None,
+            consumer_groups: None,
+        }
+    }
+
+    /// Encode the snapshot to msgpack bytes.
+    pub fn encode(&self) -> Result<Vec<u8>, SnapshotError> {
+        rmp_serde::to_vec(self).map_err(SnapshotError::Serialize)
+    }
+
+    /// Decode a snapshot from msgpack bytes.
+    pub fn decode(bytes: &[u8]) -> Result<Self, SnapshotError> {
+        rmp_serde::from_slice(bytes).map_err(SnapshotError::Deserialize)
+    }
+}
+
+/// Trait implemented by each `{Name}Inner` state machine to support 
snapshotting.
+/// Each state machine defines its own snapshot
+/// type for serialization and provides conversion methods.
+pub trait Snapshotable {
+    /// The serde-serializable snapshot representation of this state.
+    /// This should be a plain struct with only serializable types and no 
wrappers
+    /// like `Arc`, `AtomicUsize`, or other non-serializable wrappers.
+    type Snapshot: Serialize + DeserializeOwned;
+
+    /// Convert the current in-memory state into a serializable snapshot.
+    fn to_snapshot(&self) -> Self::Snapshot;
+
+    /// Restore in-memory state from a snapshot representation.
+    fn from_snapshot(snapshot: Self::Snapshot) -> Result<Self, SnapshotError>
+    where
+        Self: Sized;
+}
+
+/// Trait for filling a typed snapshot with state machine data.
+///
+/// Each state machine implements this to write its serialized state
+/// to its specific field in the `MetadataSnapshot` struct.
+pub trait FillSnapshot {
+    /// Fill the snapshot with this state machine's data.
+    fn fill_snapshot(&self, snapshot: &mut MetadataSnapshot) -> Result<(), 
SnapshotError>;
+}
+
+/// Trait for restoring state machine data from a typed snapshot.
+///
+/// Each state machine implements this to read its state from
+/// its specific field in the `MetadataSnapshot` struct.
+pub trait RestoreSnapshot: Sized {
+    /// Restore this state machine from the snapshot.
+    fn restore_snapshot(snapshot: &MetadataSnapshot) -> Result<Self, 
SnapshotError>;

Review Comment:
   Lets make the snapshot type here be generic, instead of `MetadataSnapshot`. 



##########
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;
+}
+
+#[derive(Debug, Clone)]
+#[allow(unused)]
+pub struct IggySnapshot {
+    snapshot: MetadataSnapshot,
+}
+
+#[allow(unused)]
+impl IggySnapshot {
+    pub fn new(sequence_number: u64) -> Self {
+        Self {
+            snapshot: MetadataSnapshot::new(sequence_number),
+        }
+    }
+
+    pub fn snapshot(&self) -> &MetadataSnapshot {
+        &self.snapshot
+    }
+}
+
+impl Snapshot for IggySnapshot {
+    type Error = SnapshotError;
+
+    fn create<T>(stm: &T, sequence_number: u64) -> Result<Self, SnapshotError>
+    where
+        T: FillSnapshot,
+    {
+        let mut snapshot = MetadataSnapshot {
+            created_at: IggyTimestamp::now().as_micros(),
+            sequence_number,
+            users: None,
+            streams: None,
+            consumer_groups: None,
+        };
+
+        stm.fill_snapshot(&mut snapshot)?;
+
+        Ok(Self { snapshot })
+    }
+
+    fn encode(&self) -> Result<Vec<u8>, SnapshotError> {
+        self.snapshot.encode()
+    }
+
+    fn decode(bytes: &[u8]) -> Result<Self, SnapshotError> {
+        let snapshot = MetadataSnapshot::decode(bytes)?;
+        Ok(Self { snapshot })
+    }
+
+    fn restore<T>(&self) -> Result<T, SnapshotError>

Review Comment:
   Let's maybe move this method on the `state` , similarly how we have 
`stm.fill_snapshot`.



##########
core/metadata/src/stm/consumer_group.rs:
##########
@@ -225,3 +227,150 @@ impl Handler for ConsumerGroupsInner {
         }
     }
 }
+
+/// Consumer group member snapshot representation for serialization.
+#[derive(Debug, Clone, Serialize, Deserialize)]

Review Comment:
   I see, which members are problematic in particular ? I think your solution 
is valid, but maybe in the future we could look into getting rid of the 
intermediary. If it's tractable we could create an issue for this.  



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