From 7de556ed5de4c449f67ac7e42dd33d5b338d36dd Mon Sep 17 00:00:00 2001
From: Georgy Moshkin <gmoshkin@picodata.io>
Date: Wed, 20 Mar 2024 12:45:06 +0300
Subject: [PATCH] refactor: stub implementation for box.cfg parameters from
 config.yaml

---
 src/lib.rs       |  6 +++---
 src/tarantool.rs | 40 ++++++++++++++++++++++++++++++----------
 2 files changed, 33 insertions(+), 13 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs
index 2c99498d90..c494bab383 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -620,7 +620,7 @@ fn start_discover(
     luamod::setup(config);
     assert!(!tarantool::is_box_configured());
 
-    let cfg = tarantool::Cfg::for_discovery(config);
+    let cfg = tarantool::Cfg::for_discovery(config)?;
     let (storage, raft_storage) = init_common(config, &cfg)?;
     discovery::init_global(config.instance.peers().iter().map(|a| a.to_host_port()));
 
@@ -706,7 +706,7 @@ fn start_boot(config: &PicodataConfig) -> Result<(), Error> {
     luamod::setup(config);
     assert!(!tarantool::is_box_configured());
 
-    let cfg = tarantool::Cfg::for_cluster_bootstrap(config, &instance);
+    let cfg = tarantool::Cfg::for_cluster_bootstrap(config, &instance)?;
     let (storage, raft_storage) = init_common(config, &cfg)?;
 
     let cs = raft::ConfState {
@@ -797,7 +797,7 @@ fn start_join(config: &PicodataConfig, instance_address: String) -> Result<(), E
     luamod::setup(config);
     assert!(!tarantool::is_box_configured());
 
-    let cfg = tarantool::Cfg::for_instance_join(config, &resp);
+    let cfg = tarantool::Cfg::for_instance_join(config, &resp)?;
     let (storage, raft_storage) = init_common(config, &cfg)?;
 
     let raft_id = resp.instance.raft_id;
diff --git a/src/tarantool.rs b/src/tarantool.rs
index 5fb2d1ccc2..ac812eb6ca 100644
--- a/src/tarantool.rs
+++ b/src/tarantool.rs
@@ -1,6 +1,7 @@
 use crate::config::ElectionMode;
 use crate::config::PicodataConfig;
 use crate::instance::Instance;
+use crate::introspection::Introspection;
 use crate::rpc::join;
 use crate::schema::PICO_SERVICE_USER_NAME;
 use crate::traft::error::Error;
@@ -109,7 +110,7 @@ impl Cfg {
     /// configuration we either will go into discovery phase after which we will
     /// rebootstrap and go to the next phase (either boot or join), or if the
     /// storage is already initialize we will go into the post join phase.
-    pub fn for_discovery(config: &PicodataConfig) -> Self {
+    pub fn for_discovery(config: &PicodataConfig) -> Result<Self, Error> {
         let mut res = Self {
             // These will either be chosen on the next phase or are already
             // chosen and will be restored from the local storage.
@@ -134,12 +135,15 @@ impl Cfg {
             ..Default::default()
         };
 
-        res.set_core_parameters(config);
-        res
+        res.set_core_parameters(config)?;
+        Ok(res)
     }
 
     /// Initial configuration for the cluster bootstrap phase.
-    pub fn for_cluster_bootstrap(config: &PicodataConfig, leader: &Instance) -> Self {
+    pub fn for_cluster_bootstrap(
+        config: &PicodataConfig,
+        leader: &Instance,
+    ) -> Result<Self, Error> {
         let mut res = Self {
             // At this point uuids must be valid, it will be impossible to
             // change them until the instance is expelled.
@@ -160,13 +164,16 @@ impl Cfg {
             ..Default::default()
         };
 
-        res.set_core_parameters(config);
-        res
+        res.set_core_parameters(config)?;
+        Ok(res)
     }
 
     /// Initial configuration for the new instance joining to an already
     /// initialized cluster.
-    pub fn for_instance_join(config: &PicodataConfig, resp: &join::Response) -> Self {
+    pub fn for_instance_join(
+        config: &PicodataConfig,
+        resp: &join::Response,
+    ) -> Result<Self, Error> {
         let mut replication_cfg = Vec::with_capacity(resp.box_replication.len());
         let password = crate::pico_service::pico_service_password();
         for address in &resp.box_replication {
@@ -196,11 +203,11 @@ impl Cfg {
             ..Default::default()
         };
 
-        res.set_core_parameters(config);
-        res
+        res.set_core_parameters(config)?;
+        Ok(res)
     }
 
-    pub fn set_core_parameters(&mut self, config: &PicodataConfig) {
+    pub fn set_core_parameters(&mut self, config: &PicodataConfig) -> Result<(), Error> {
         self.log = config.instance.log.clone();
         self.log_level = Some(config.instance.log_level() as _);
 
@@ -208,8 +215,21 @@ impl Cfg {
         self.memtx_dir = config.instance.data_dir();
         self.vinyl_dir = config.instance.data_dir();
 
+        // FIXME: make the loop below work with default values
         self.other_fields
             .insert("memtx_memory".into(), config.instance.memtx_memory().into());
+
+        #[rustfmt::skip]
+        const MAPPING: &[(&str, &str)] = &[
+        ];
+        for (box_field, picodata_field) in MAPPING {
+            let value = config
+                .get_field_as_rmpv(picodata_field)
+                .map_err(|e| Error::other(format!("internal error: {e}")))?;
+            self.other_fields.insert((*box_field).into(), value);
+        }
+
+        Ok(())
     }
 }
 
-- 
GitLab