You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iggy.apache.org by gk...@apache.org on 2025/11/21 13:47:02 UTC

(iggy) 01/01: refactor(metadata, consesus): refactor metadata and consesus modules

This is an automated email from the ASF dual-hosted git repository.

gkoszyk pushed a commit to branch metadata_refactor
in repository https://gitbox.apache.org/repos/asf/iggy.git

commit 2a678a6f9f66560b9f78a7abf9cdbbb257570dfb
Author: numminex <g....@gmail.com>
AuthorDate: Fri Nov 21 14:46:49 2025 +0100

    refactor(metadata, consesus): refactor metadata and consesus modules
---
 core/consensus/src/impls.rs         | 45 ++++++++++++++++++
 core/consensus/src/lib.rs           | 27 ++++++-----
 core/metadata/src/impls/metadata.rs | 91 ++++++++++++++++++++++++-------------
 3 files changed, 117 insertions(+), 46 deletions(-)

diff --git a/core/consensus/src/impls.rs b/core/consensus/src/impls.rs
index be01c3b62..d3a47f3c3 100644
--- a/core/consensus/src/impls.rs
+++ b/core/consensus/src/impls.rs
@@ -15,4 +15,49 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use crate::{Consensus, Project};
+
 pub struct VsrConsensus;
+
+#[derive(Clone)]
+pub struct Request;
+
+impl Project<Prepare> for Request {
+    type Consensus = VsrConsensus;
+    fn project(self, _consensus: &Self::Consensus) -> Prepare {
+        Prepare
+    }
+}
+
+#[derive(Clone)]
+pub struct Prepare;
+
+impl Project<PrepareOk> for Prepare {
+    type Consensus = VsrConsensus;
+    fn project(self, _consensus: &Self::Consensus) -> PrepareOk {
+        PrepareOk
+    }
+}
+
+#[derive(Clone)]
+pub struct PrepareOk;
+
+impl Consensus for VsrConsensus {
+    type RequestMessage = Request;
+
+    type ReplicateMessage = Prepare;
+
+    type AckMessage = PrepareOk;
+
+    fn pipeline_message(&self, _message: Self::ReplicateMessage) {
+        todo!()
+    }
+
+    fn verify_pipeline(&self) {
+        todo!()
+    }
+
+    fn post_replicate_verify(&self, _message: &Self::ReplicateMessage) {
+        todo!()
+    }
+}
diff --git a/core/consensus/src/lib.rs b/core/consensus/src/lib.rs
index 0a02bc089..60737e1fd 100644
--- a/core/consensus/src/lib.rs
+++ b/core/consensus/src/lib.rs
@@ -14,24 +14,23 @@
 // KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License.
-use clock::Clock;
 
-pub trait Project<U, T, C>
-where
-    C: Consensus<T>,
-    T: Clock,
-{
-    fn project(self, consensus: &C) -> U;
+pub trait Project<U> {
+    type Consensus: Consensus;
+    fn project(self, consensus: &Self::Consensus) -> U;
 }
 
-pub trait Consensus<C>
-where
-    Self: Sized,
-    C: Clock,
-{
-    type RequestMessage: Project<Self::ReplicateMessage, C, Self>;
-    type ReplicateMessage: Project<Self::AckMessage, C, Self>;
+pub trait Consensus {
+    // I am wondering, whether we should create a dedicated trait for cloning, so it's explicit that we do ref counting.
+    type RequestMessage: Project<Self::ReplicateMessage, Consensus = Self> + Clone;
+    type ReplicateMessage: Project<Self::AckMessage, Consensus = Self> + Clone;
     type AckMessage;
+
+    fn pipeline_message(&self, message: Self::ReplicateMessage);
+    fn verify_pipeline(&self);
+
+    // TODO: Figure out how we can achieve that without exposing such methods in the Consensus trait.
+    fn post_replicate_verify(&self, message: &Self::ReplicateMessage);
 }
 
 mod impls;
diff --git a/core/metadata/src/impls/metadata.rs b/core/metadata/src/impls/metadata.rs
index f802d53f1..924ffa06f 100644
--- a/core/metadata/src/impls/metadata.rs
+++ b/core/metadata/src/impls/metadata.rs
@@ -14,56 +14,83 @@
 // KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License.
-use clock::Clock;
-use consensus::{Consensus, Project};
-use std::marker::PhantomData;
-use tracing::debug;
+use consensus::{Consensus, Prepare, Project, VsrConsensus};
+use tracing::{debug, warn};
 
 // TODO: Define a trait (probably in some external crate)
 #[expect(unused)]
-trait Metadata<C, T>
-where
-    C: Consensus<T>,
-    T: Clock,
-{
-    fn on_request(&self, message: C::RequestMessage);
-    fn on_replicate(&self, message: C::ReplicateMessage);
-    fn on_ack(&self, message: C::AckMessage);
+trait Metadata {
+    type Consensus: Consensus;
+    fn on_request(&self, message: <Self::Consensus as Consensus>::RequestMessage);
+    fn on_replicate(&self, message: <Self::Consensus as Consensus>::ReplicateMessage);
+    fn on_ack(&self, message: <Self::Consensus as Consensus>::AckMessage);
 }
 
 #[expect(unused)]
-struct IggyMetadata<C, M, J, S, T>
-where
-    C: Consensus<T>,
-    T: Clock,
-{
-    consensus: C,
+struct IggyMetadata<M, J, S> {
+    consensus: VsrConsensus,
     mux_stm: M,
     journal: J,
     snapshot: S,
-
-    _t: PhantomData<T>,
 }
 
-impl<C, M, J, S, T> Metadata<C, T> for IggyMetadata<C, M, J, S, T>
-where
-    C: Consensus<T>,
-    T: Clock,
-{
-    fn on_request(&self, message: C::RequestMessage) {
+impl<M, J, S> Metadata for IggyMetadata<M, J, S> {
+    type Consensus = VsrConsensus;
+    fn on_request(&self, message: <Self::Consensus as Consensus>::RequestMessage) {
+        // TODO: Bunch of asserts.
         debug!("handling metadata request");
-        let _message = message.project(&self.consensus);
+        let prepare = message.project(&self.consensus);
+        self.pipeline_prepare(prepare);
+    }
+
+    fn on_replicate(&self, message: <Self::Consensus as Consensus>::ReplicateMessage) {
+        if !self.fence_old_prepare(&message) {
+            self.replicate(message.clone());
+        } else {
+            warn!("received old prepare, not replicating");
+        }
     }
 
-    fn on_replicate(&self, _message: C::ReplicateMessage) {
+    fn on_ack(&self, _message: <Self::Consensus as Consensus>::AckMessage) {
         todo!()
     }
+}
+
+impl<M, J, S> IggyMetadata<M, J, S> {
+    #[expect(unused)]
+    fn pipeline_prepare(&self, prepare: Prepare) {
+        debug!("inserting prepare into metadata pipeline");
+        self.consensus.verify_pipeline();
+        self.consensus.pipeline_message(prepare.clone());
+
+        self.on_replicate(prepare.clone());
+        self.consensus.post_replicate_verify(&prepare);
+    }
+
+    fn fence_old_prepare(&self, _prepare: &Prepare) -> bool {
+        // TODO
+        false
+    }
 
-    fn on_ack(&self, _message: C::AckMessage) {
+    fn replicate(&self, _prepare: Prepare) {
         todo!()
     }
 }
 
-// TODO: Hide with associated types all of those generics, so they are not leaking to the upper layer
-#[expect(unused)]
-pub trait MetadataHandle {}
+// TODO: Hide with associated types all of those generics, so they are not leaking to the upper layer, or maybe even make of the `Metadata` trait itself.
+// Something like this:
+// pub trait MetadataHandle {
+//     type Consensus: Consensus<Self::Clock>;
+//     type Clock: Clock;
+//     type MuxStm;
+//     type Journal;
+//     type Snapshot;
+// }
+
+// pub trait Metadata<H: MetadataHandle> {
+//     fn on_request(&self, message: <H::Consensus as Consensus<H::Clock>>::RequestMessage); // Create type aliases for those long associated types
+//     fn on_replicate(&self, message: <H::Consensus as Consensus<H::Clock>>::ReplicateMessage);
+//     fn on_ack(&self, message: <H::Consensus as Consensus<H::Clock>>::AckMessage);
+// }
+
+// The error messages can get ugly from those associated types, but I think it's worth the fact that it hides a lot of the generics and their bounds.