From 9147c156bfd75468566eb1560f494ec6776f6284 Mon Sep 17 00:00:00 2001
From: Yaroslav Dynnikov <yaroslav.dynnikov@gmail.com>
Date: Fri, 20 May 2022 19:28:17 +0300
Subject: [PATCH] chore: move code around

Both JoinRequest and JoinResponse are going to be used in other modules.
Move them one level above from `traft::node::*` to `traft::*`.
---
 src/main.rs       |  9 ++++-----
 src/traft/mod.rs  | 24 ++++++++++++++++++++++++
 src/traft/node.rs | 23 +----------------------
 3 files changed, 29 insertions(+), 27 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 2c8033ef85..57cd484c0e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -454,7 +454,7 @@ fn start_boot(args: &args::Run) {
 fn start_join(args: &args::Run, leader_address: String) {
     tlog!(Info, ">>>>> start_join({leader_address})");
 
-    let req = traft::node::JoinRequest {
+    let req = traft::JoinRequest {
         instance_id: args.instance_id.clone(),
         replicaset_id: args.replicaset_id.clone(),
         voter: false,
@@ -463,8 +463,7 @@ fn start_join(args: &args::Run, leader_address: String) {
 
     use traft::node::raft_join;
     let fn_name = stringify_cfunc!(raft_join);
-    let resp: traft::node::JoinResponse =
-        tarantool::net_box_call_retry(&leader_address, fn_name, &req);
+    let resp: traft::JoinResponse = tarantool::net_box_call_retry(&leader_address, fn_name, &req);
 
     picolib_setup(args);
     assert!(tarantool::cfg().is_none());
@@ -564,7 +563,7 @@ fn postjoin(args: &args::Run) {
         }
 
         tlog!(Warning, "initiating self-promotion of {me:?}");
-        let req = traft::node::JoinRequest {
+        let req = traft::JoinRequest {
             instance_id: me.instance_id.clone(),
             replicaset_id: None, // TODO
             voter: true,
@@ -583,7 +582,7 @@ fn postjoin(args: &args::Run) {
                 fiber::sleep(timeout.saturating_sub(now.elapsed()));
                 continue;
             }
-            Ok(traft::node::JoinResponse { .. }) => {
+            Ok(traft::JoinResponse { .. }) => {
                 break;
             }
         };
diff --git a/src/traft/mod.rs b/src/traft/mod.rs
index 04cedc3230..0a246a6a5f 100644
--- a/src/traft/mod.rs
+++ b/src/traft/mod.rs
@@ -269,3 +269,27 @@ pub trait ContextCoercion: Serialize + DeserializeOwned {
         ContextCoercion::write_to_bytes(Some(self))
     }
 }
+
+///////////////////////////////////////////////////////////////////////////////
+/// Request to join the cluster.
+#[derive(Clone, Debug, Serialize, Deserialize)]
+pub struct JoinRequest {
+    pub instance_id: String,
+    pub replicaset_id: Option<String>,
+    pub advertise_address: String,
+    pub voter: bool,
+}
+impl AsTuple for JoinRequest {}
+
+///////////////////////////////////////////////////////////////////////////////
+/// Response to a JoinRequest
+#[derive(Clone, Debug, Serialize, Deserialize)]
+pub struct JoinResponse {
+    pub peer: Peer,
+    pub raft_group: Vec<Peer>,
+    pub box_replication: Vec<String>,
+    // TODO add later:
+    // Other parameters necessary for box.cfg()
+    // pub read_only: bool,
+}
+impl AsTuple for JoinResponse {}
diff --git a/src/traft/node.rs b/src/traft/node.rs
index d61d71b945..d3ce78b2a9 100644
--- a/src/traft/node.rs
+++ b/src/traft/node.rs
@@ -13,8 +13,6 @@ use ::tarantool::fiber;
 use ::tarantool::proc;
 use ::tarantool::tlua;
 use ::tarantool::transaction::start_transaction;
-use ::tarantool::tuple::AsTuple;
-use serde::{Deserialize, Serialize};
 use std::cell::RefCell;
 use std::collections::HashMap;
 use std::convert::TryFrom;
@@ -35,6 +33,7 @@ use crate::traft;
 use crate::traft::ConnectionPool;
 use crate::traft::LogicalClock;
 use crate::traft::Storage;
+use crate::traft::{JoinRequest, JoinResponse};
 
 type RawNode = raft::RawNode<Storage>;
 type Notify = fiber::Channel<Result<u64, RaftError>>;
@@ -616,26 +615,6 @@ fn raft_main_loop(
     }
 }
 
-#[derive(Clone, Debug, Serialize, Deserialize)]
-pub struct JoinRequest {
-    pub instance_id: String,
-    pub replicaset_id: Option<String>,
-    pub advertise_address: String,
-    pub voter: bool,
-}
-impl AsTuple for JoinRequest {}
-
-#[derive(Clone, Debug, Serialize, Deserialize)]
-pub struct JoinResponse {
-    pub peer: traft::Peer,
-    pub raft_group: Vec<traft::Peer>,
-    pub box_replication: Vec<String>,
-    // TODO add later:
-    // Other parameters necessary for box.cfg()
-    // pub read_only: bool,
-}
-impl AsTuple for JoinResponse {}
-
 fn raft_join_loop(inbox: Mailbox<(JoinRequest, Notify)>, main_inbox: Mailbox<NormalRequest>) {
     loop {
         let batch = inbox.receive_all(Duration::MAX);
-- 
GitLab