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


##########
core/metadata/src/stm/stream.rs:
##########
@@ -466,3 +508,160 @@ impl StateHandler for DeletePartitions {
         }
     }
 }
+
+/// Snapshot representation for the Streams state machine.
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct StreamsSnapshot {
+    pub items: Vec<(usize, StreamSnapshot)>,
+}
+
+impl Snapshotable for Streams {
+    type Snapshot = StreamsSnapshot;
+
+    fn to_snapshot(&self) -> Self::Snapshot {
+        self.snapshot_read(|inner| {
+            let items: Vec<(usize, StreamSnapshot)> = inner
+                .items
+                .iter()
+                .map(|(stream_id, stream)| {
+                    let (size_bytes, messages_count, segments_count) =
+                        stream.stats.load_for_snapshot();
+                    let topics: Vec<(usize, TopicSnapshot)> = stream
+                        .topics
+                        .iter()
+                        .map(|(topic_id, topic)| {
+                            let (t_size, t_msgs, t_segs) = 
topic.stats.load_for_snapshot();
+                            (
+                                topic_id,
+                                TopicSnapshot {
+                                    id: topic.id,
+                                    name: topic.name.to_string(),
+                                    created_at: topic.created_at,
+                                    replication_factor: 
topic.replication_factor,
+                                    message_expiry: topic.message_expiry,
+                                    compression_algorithm: 
topic.compression_algorithm,
+                                    max_topic_size: topic.max_topic_size,
+                                    stats: StatsSnapshot {
+                                        size_bytes: t_size,
+                                        messages_count: t_msgs,
+                                        segments_count: t_segs,
+                                    },
+                                    partitions: topic
+                                        .partitions
+                                        .iter()
+                                        .map(|p| PartitionSnapshot {
+                                            id: p.id,
+                                            created_at: p.created_at,
+                                        })
+                                        .collect(),
+                                    round_robin_counter: topic
+                                        .round_robin_counter
+                                        .load(Ordering::Relaxed),
+                                },
+                            )
+                        })
+                        .collect();
+                    (
+                        stream_id,
+                        StreamSnapshot {
+                            id: stream.id,
+                            name: stream.name.to_string(),
+                            created_at: stream.created_at,
+                            stats: StatsSnapshot {
+                                size_bytes,
+                                messages_count,
+                                segments_count,
+                            },
+                            topics,
+                        },
+                    )
+                })
+                .collect();
+            StreamsSnapshot { items }
+        })
+    }
+
+    fn from_snapshot(
+        snapshot: Self::Snapshot,
+    ) -> Result<Self, crate::stm::snapshot::SnapshotError> {
+        use crate::stm::snapshot::SnapshotError;
+
+        let mut items: Slab<Stream> = Slab::new();
+        let mut index: AHashMap<Arc<str>, usize> = AHashMap::new();
+
+        for (expected_id, stream_snap) in snapshot.items {
+            let stream_stats = Arc::new(StreamStats::default());
+            stream_stats.store_from_snapshot(
+                stream_snap.stats.size_bytes,
+                stream_snap.stats.messages_count,
+                stream_snap.stats.segments_count,
+            );
+
+            let mut topics: Slab<Topic> = Slab::new();
+            let mut topic_index: AHashMap<Arc<str>, usize> = AHashMap::new();
+
+            for (expected_topic_id, topic_snap) in stream_snap.topics {
+                let topic_stats = 
Arc::new(TopicStats::new(stream_stats.clone()));
+                topic_stats.store_from_snapshot(
+                    topic_snap.stats.size_bytes,
+                    topic_snap.stats.messages_count,
+                    topic_snap.stats.segments_count,
+                );
+                let topic_name: Arc<str> = Arc::from(topic_snap.name.as_str());
+                let topic = Topic {
+                    id: topic_snap.id,
+                    name: topic_name.clone(),
+                    created_at: topic_snap.created_at,
+                    replication_factor: topic_snap.replication_factor,
+                    message_expiry: topic_snap.message_expiry,
+                    compression_algorithm: topic_snap.compression_algorithm,
+                    max_topic_size: topic_snap.max_topic_size,
+                    stats: topic_stats,
+                    partitions: topic_snap
+                        .partitions
+                        .into_iter()
+                        .map(|p| Partition {
+                            id: p.id,
+                            created_at: p.created_at,
+                        })
+                        .collect(),
+                    round_robin_counter: 
Arc::new(AtomicUsize::new(topic_snap.round_robin_counter)),
+                };
+                let actual_topic_id = topics.insert(topic);
+                if actual_topic_id != expected_topic_id {
+                    return Err(SnapshotError::SlabIdMismatch {
+                        section: "streams.topics",
+                        expected: expected_topic_id,
+                        actual: actual_topic_id,
+                    });
+                }
+                topic_index.insert(topic_name, actual_topic_id);
+            }
+
+            let stream_name: Arc<str> = Arc::from(stream_snap.name.as_str());
+            let stream = Stream {
+                id: stream_snap.id,
+                name: stream_name.clone(),
+                created_at: stream_snap.created_at,
+                stats: stream_stats,
+                topics,
+                topic_index,
+            };
+
+            let actual_id = items.insert(stream);
+            if actual_id != expected_id {
+                return Err(SnapshotError::SlabIdMismatch {
+                    section: "streams",
+                    expected: expected_id,
+                    actual: actual_id,
+                });
+            }
+            index.insert(stream_name, actual_id);
+        }
+
+        let inner = StreamsInner { index, items };
+        Ok(inner.into())
+    }
+}

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