From a98ff3f8f806f84428239ffc484d413e49289b01 Mon Sep 17 00:00:00 2001
From: Mergen Imeev <imeevma@tarantool.org>
Date: Mon, 3 Jul 2023 15:00:08 +0300
Subject: [PATCH] config: rework the way the meta is populated

This patch reworks the way the meta is populated. This is done to
incrementally populate the metadata, instead of set the metadata at the
end of reading data from the source. This allows to get the correct meta
in cases where getting data from the source failed.

Follow-up #8789

NO_DOC=feature not yet released
NO_TEST=tested in EE
NO_CHANGELOG=feature not yet released
---
 src/box/lua/config/init.lua        | 14 +++++++++-----
 src/box/lua/config/source/env.lua  |  6 ------
 src/box/lua/config/source/file.lua |  6 ------
 3 files changed, 9 insertions(+), 17 deletions(-)

diff --git a/src/box/lua/config/init.lua b/src/box/lua/config/init.lua
index fb0986c77c..52e8abb0bf 100644
--- a/src/box/lua/config/init.lua
+++ b/src/box/lua/config/init.lua
@@ -80,13 +80,18 @@ function methods._alert(self, alert)
     table.insert(self._alerts, alert)
 end
 
+function methods._meta(self, source_name, key, value)
+    local data = self._metadata[source_name] or {}
+    data[key] = value
+    self._metadata[source_name] = data
+end
+
 function methods._register_source(self, source)
     assert(type(source) == 'table')
     assert(source.name ~= nil)
     assert(source.type == 'instance' or source.type == 'cluster')
     assert(source.sync ~= nil)
     assert(source.get ~= nil)
-    assert(source.meta ~= nil)
     table.insert(self._sources, source)
 end
 
@@ -134,7 +139,6 @@ function methods._collect(self, opts)
 
     -- For error reporting.
     local source_info = {}
-    self._meta = {}
 
     for _, source in ipairs(self._sources) do
         -- Gather config values.
@@ -146,7 +150,6 @@ function methods._collect(self, opts)
         if sync_source == source.name or sync_source == 'all' then
             source.sync(self, iconfig)
         end
-        self._meta[source.name] = source.meta()
 
         -- Validate configurations gathered from the sources.
         if source.type == 'instance' then
@@ -285,6 +288,7 @@ function methods._reload_noexc(self, opts)
     broadcast(self)
 
     self._alerts = {}
+    self._metadata = {}
     local ok, err = pcall(self._collect, self, opts)
     if ok then
         ok, err = pcall(self._apply, self)
@@ -321,7 +325,7 @@ function methods.info(self)
     selfcheck(self, 'info')
     return {
         alerts = self._alerts,
-        meta = self._meta,
+        meta = self._metadata,
         status = self._status,
     }
 end
@@ -343,7 +347,7 @@ local function new()
         -- Track situations when something is going wrong.
         _alerts = {},
         -- Metadata from sources.
-        _meta = {},
+        _metadata = {},
         -- Current status.
         _status = 'uninitialized',
     }, mt)
diff --git a/src/box/lua/config/source/env.lua b/src/box/lua/config/source/env.lua
index b14d5ade55..8dadbe8b2b 100644
--- a/src/box/lua/config/source/env.lua
+++ b/src/box/lua/config/source/env.lua
@@ -26,10 +26,6 @@ local function get()
     return values
 end
 
-local function meta()
-    return nil
-end
-
 return {
     name = 'env',
     -- The type is either 'instance' or 'cluster'.
@@ -40,6 +36,4 @@ return {
     --
     -- source.get()
     get = get,
-    -- Metadata of the current configuration from the source.
-    meta = meta,
 }
diff --git a/src/box/lua/config/source/file.lua b/src/box/lua/config/source/file.lua
index 4648eff85f..041f85688b 100644
--- a/src/box/lua/config/source/file.lua
+++ b/src/box/lua/config/source/file.lua
@@ -75,10 +75,6 @@ local function get()
     return values
 end
 
-local function meta()
-    return nil
-end
-
 return {
     name = 'file',
     -- The type is either 'instance' or 'cluster'.
@@ -89,6 +85,4 @@ return {
     --
     -- source.get()
     get = get,
-    -- Metadata of the current configuration from the source.
-    meta = meta,
 }
-- 
GitLab