From b26cef588af140fc1e7f1409d735c8b734c43ac8 Mon Sep 17 00:00:00 2001
From: Yaroslav Dynnikov <yaroslav.dynnikov@gmail.com>
Date: Tue, 22 Nov 2022 02:24:40 +0300
Subject: [PATCH] rename: split governor-related modules

---
 src/{traft/governor.rs => governor/cc.rs} | 78 ---------------------
 src/governor/migration.rs                 | 83 +++++++++++++++++++++++
 src/governor/mod.rs                       |  7 ++
 src/main.rs                               |  1 +
 src/traft/mod.rs                          |  1 -
 src/traft/node.rs                         |  3 +-
 6 files changed, 93 insertions(+), 80 deletions(-)
 rename src/{traft/governor.rs => governor/cc.rs} (82%)
 create mode 100644 src/governor/migration.rs
 create mode 100644 src/governor/mod.rs

diff --git a/src/traft/governor.rs b/src/governor/cc.rs
similarity index 82%
rename from src/traft/governor.rs
rename to src/governor/cc.rs
index 7e4a32c81c..7f174b7331 100644
--- a/src/traft/governor.rs
+++ b/src/governor/cc.rs
@@ -9,10 +9,6 @@ use crate::traft::Peer;
 use crate::traft::RaftId;
 use crate::traft::TargetGradeVariant;
 
-use super::Migration;
-use super::Replicaset;
-use super::ReplicasetId;
-
 struct RaftConf<'a> {
     all: BTreeMap<RaftId, &'a Peer>,
     voters: BTreeSet<RaftId>,
@@ -172,27 +168,6 @@ pub(crate) fn raft_conf_change(
     Some(conf_change)
 }
 
-pub(crate) fn waiting_migrations<'a>(
-    migrations: &'a mut [Migration],
-    replicasets: &'a [Replicaset],
-    desired_schema_version: u64,
-) -> Vec<(u64, Vec<ReplicasetId>)> {
-    migrations.sort_by(|a, b| a.id.partial_cmp(&b.id).unwrap());
-    let mut res: Vec<(u64, Vec<ReplicasetId>)> = Vec::new();
-    for m in migrations {
-        let mut rs: Vec<ReplicasetId> = Vec::new();
-        for r in replicasets {
-            if r.current_schema_version < m.id && m.id <= desired_schema_version {
-                rs.push(r.replicaset_id.clone());
-            }
-        }
-        if !rs.is_empty() {
-            res.push((m.id, rs));
-        }
-    }
-    res
-}
-
 #[cfg(test)]
 mod tests {
     use ::raft::prelude as raft;
@@ -392,57 +367,4 @@ mod tests {
             None
         );
     }
-
-    use crate::traft::{InstanceId, Migration, Replicaset, ReplicasetId};
-
-    use super::waiting_migrations;
-
-    macro_rules! m {
-        ($id:literal, $body:literal) => {
-            Migration {
-                id: $id,
-                body: $body.to_string(),
-            }
-        };
-    }
-
-    macro_rules! r {
-        ($id:literal, $schema_version:literal) => {
-            Replicaset {
-                replicaset_id: ReplicasetId($id.to_string()),
-                replicaset_uuid: "".to_string(),
-                master_id: InstanceId("i0".to_string()),
-                weight: 1.0,
-                current_schema_version: $schema_version,
-            }
-        };
-    }
-
-    macro_rules! expect {
-        [$(($migration_id:literal, $($replicaset_id:literal),*)),*] => [vec![
-            $((
-                $migration_id,
-                vec![$(ReplicasetId($replicaset_id.to_string())),*].to_vec()
-            )),*
-        ].to_vec()];
-    }
-
-    #[test]
-    fn test_waiting_migrations() {
-        let ms = vec![m!(1, "m1"), m!(2, "m2"), m!(3, "m3")].to_vec();
-        let rs = vec![r!("r1", 0), r!("r2", 2), r!("r3", 1)].to_vec();
-        assert_eq!(waiting_migrations(&mut ms.clone(), &rs, 0), expect![]);
-        assert_eq!(
-            waiting_migrations(&mut ms.clone(), &rs, 1),
-            expect![(1, "r1")]
-        );
-        assert_eq!(
-            waiting_migrations(&mut ms.clone(), &rs, 2),
-            expect![(1, "r1"), (2, "r1", "r3")]
-        );
-        assert_eq!(
-            waiting_migrations(&mut ms.clone(), &rs, 3),
-            expect![(1, "r1"), (2, "r1", "r3"), (3, "r1", "r2", "r3")]
-        );
-    }
 }
diff --git a/src/governor/migration.rs b/src/governor/migration.rs
new file mode 100644
index 0000000000..98b4293bb5
--- /dev/null
+++ b/src/governor/migration.rs
@@ -0,0 +1,83 @@
+use crate::traft::Migration;
+use crate::traft::Replicaset;
+use crate::traft::ReplicasetId;
+
+pub(crate) fn waiting_migrations<'a>(
+    migrations: &'a mut [Migration],
+    replicasets: &'a [Replicaset],
+    desired_schema_version: u64,
+) -> Vec<(u64, Vec<ReplicasetId>)> {
+    migrations.sort_by(|a, b| a.id.partial_cmp(&b.id).unwrap());
+    let mut res: Vec<(u64, Vec<ReplicasetId>)> = Vec::new();
+    for m in migrations {
+        let mut rs: Vec<ReplicasetId> = Vec::new();
+        for r in replicasets {
+            if r.current_schema_version < m.id && m.id <= desired_schema_version {
+                rs.push(r.replicaset_id.clone());
+            }
+        }
+        if !rs.is_empty() {
+            res.push((m.id, rs));
+        }
+    }
+    res
+}
+
+#[cfg(test)]
+mod tests {
+    use crate::traft::InstanceId;
+    use crate::traft::Migration;
+    use crate::traft::Replicaset;
+    use crate::traft::ReplicasetId;
+
+    use super::waiting_migrations;
+
+    macro_rules! m {
+        ($id:literal, $body:literal) => {
+            Migration {
+                id: $id,
+                body: $body.to_string(),
+            }
+        };
+    }
+
+    macro_rules! r {
+        ($id:literal, $schema_version:literal) => {
+            Replicaset {
+                replicaset_id: ReplicasetId($id.to_string()),
+                replicaset_uuid: "".to_string(),
+                master_id: InstanceId("i0".to_string()),
+                weight: 1.0,
+                current_schema_version: $schema_version,
+            }
+        };
+    }
+
+    macro_rules! expect {
+        [$(($migration_id:literal, $($replicaset_id:literal),*)),*] => [vec![
+            $((
+                $migration_id,
+                vec![$(ReplicasetId($replicaset_id.to_string())),*].to_vec()
+            )),*
+        ].to_vec()];
+    }
+
+    #[test]
+    fn test_waiting_migrations() {
+        let ms = vec![m!(1, "m1"), m!(2, "m2"), m!(3, "m3")].to_vec();
+        let rs = vec![r!("r1", 0), r!("r2", 2), r!("r3", 1)].to_vec();
+        assert_eq!(waiting_migrations(&mut ms.clone(), &rs, 0), expect![]);
+        assert_eq!(
+            waiting_migrations(&mut ms.clone(), &rs, 1),
+            expect![(1, "r1")]
+        );
+        assert_eq!(
+            waiting_migrations(&mut ms.clone(), &rs, 2),
+            expect![(1, "r1"), (2, "r1", "r3")]
+        );
+        assert_eq!(
+            waiting_migrations(&mut ms.clone(), &rs, 3),
+            expect![(1, "r1"), (2, "r1", "r3"), (3, "r1", "r2", "r3")]
+        );
+    }
+}
diff --git a/src/governor/mod.rs b/src/governor/mod.rs
new file mode 100644
index 0000000000..f32005da51
--- /dev/null
+++ b/src/governor/mod.rs
@@ -0,0 +1,7 @@
+pub(crate) mod cc;
+pub(crate) mod migration;
+
+pub(crate) use cc::raft_conf_change;
+pub(crate) use migration::waiting_migrations;
+
+// TODO place governor code here
diff --git a/src/main.rs b/src/main.rs
index 4dfe3a9ee8..50bae6a2f0 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -27,6 +27,7 @@ use traft::error::Error;
 mod app;
 mod args;
 mod discovery;
+mod governor;
 mod ipc;
 mod kvcell;
 mod r#loop;
diff --git a/src/traft/mod.rs b/src/traft/mod.rs
index d11d3cec58..1ed152a727 100644
--- a/src/traft/mod.rs
+++ b/src/traft/mod.rs
@@ -2,7 +2,6 @@
 
 pub mod error;
 pub mod event;
-pub mod governor;
 mod network;
 pub mod node;
 pub mod notify;
diff --git a/src/traft/node.rs b/src/traft/node.rs
index 29cd284e7e..1a1af988a3 100644
--- a/src/traft/node.rs
+++ b/src/traft/node.rs
@@ -27,10 +27,11 @@ use std::time::Duration;
 use std::time::Instant;
 use tarantool::space::UpdateOps;
 
+use crate::governor::raft_conf_change;
+use crate::governor::waiting_migrations;
 use crate::kvcell::KVCell;
 use crate::r#loop::{FlowControl, Loop};
 use crate::stringify_cfunc;
-use crate::traft::governor::{raft_conf_change, waiting_migrations};
 use crate::traft::rpc;
 use crate::traft::storage::ClusterSpace;
 use crate::traft::ContextCoercion as _;
-- 
GitLab