Skip to content

Commit 2c20455

Browse files
committed
Cache etcd config
* store config in global variable '\0moonlibs.config.etcd_cache' * save and load config from local cache only if etcd_conf.local_cache defined * use cache only in call config.etcd.get_all_from_cache * handle attempt to index nil _flat
1 parent e1a5082 commit 2c20455

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

config.lua

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ end
511511
---@field prefix string Mandatory prefix inside etcd tree
512512
---@field uuid? 'auto' When auto config generates replicaset_uuid and instance_uuid for nodes
513513
---@field fixed? table Optional ETCD tree
514+
---@field local_cache? boolean Optional flag for cache etcd configuration between package reload
514515

515516
---Loads configuration from etcd and evaluate master_selection_policy
516517
---@param M moonlibs.config
@@ -543,6 +544,10 @@ local function etcd_load( M, etcd_conf, local_cfg )
543544
end
544545
M.etcd = etcd
545546

547+
local local_cache_needed = etcd_conf.local_cache
548+
local local_cache_used = false
549+
local local_cache_var = '\0moonlibs.config.etcd_cache'
550+
546551
function M.etcd.get_common(e)
547552
local common_cfg = e:list(prefix .. "/common")
548553
assert(common_cfg.box,"no box config in etcd common tree")
@@ -590,12 +595,35 @@ local function etcd_load( M, etcd_conf, local_cfg )
590595
return all_cfg
591596
end
592597

593-
etcd:discovery()
598+
function M.etcd.get_all_from_cache(e)
599+
return rawget(_G, local_cache_var)
600+
end
601+
602+
local ok, discovery_err = pcall(etcd.discovery, etcd)
603+
if not ok and local_cache_needed then
604+
local_cache_used = true
605+
log.error('Use local cache of cfg because etcd discovery failed: "%s"', discovery_err)
606+
elseif not ok then
607+
error(discovery_err)
608+
end
609+
610+
local all_cfg
611+
if local_cache_used then
612+
all_cfg = etcd:get_all_from_cache()
613+
assert(all_cfg, "Local etcd cache is empty")
614+
else
615+
all_cfg = etcd:get_all()
616+
if local_cache_needed then
617+
rawset(_G, local_cache_var, all_cfg)
618+
end
619+
end
594620

595-
local all_cfg = etcd:get_all()
596621
if etcd_conf.print_config then
597-
print("Loaded config from etcd",yaml.encode(all_cfg))
622+
print("Loaded config from",
623+
local_cache_used and 'etcd local cache' or 'etcd',
624+
yaml.encode(all_cfg))
598625
end
626+
599627
local common_cfg = all_cfg.common
600628
local all_instances_cfg = all_cfg.instances
601629

@@ -789,6 +817,8 @@ local M
789817
def = k
790818
k = self
791819
end
820+
assert(type(M._flat) == 'table', ('internal cfg (M._flat) have unexpected type "%s"'):format(type(M._flat)))
821+
792822
if M._flat[k] ~= nil then
793823
return M._flat[k]
794824
elseif def ~= nil then

config/etcd.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function M:request(method, path, args )
158158

159159
local len = #self.endpoints
160160
for i = 0, len - 1 do
161-
local cur = self.current + i
161+
local cur = (self.current or 1) + i
162162
if cur > len then
163163
cur = cur % len
164164
end

0 commit comments

Comments
 (0)