|
511 | 511 | ---@field prefix string Mandatory prefix inside etcd tree |
512 | 512 | ---@field uuid? 'auto' When auto config generates replicaset_uuid and instance_uuid for nodes |
513 | 513 | ---@field fixed? table Optional ETCD tree |
| 514 | +---@field local_cache? boolean Optional flag for cache etcd configuration between package reload |
514 | 515 |
|
515 | 516 | ---Loads configuration from etcd and evaluate master_selection_policy |
516 | 517 | ---@param M moonlibs.config |
@@ -543,6 +544,10 @@ local function etcd_load( M, etcd_conf, local_cfg ) |
543 | 544 | end |
544 | 545 | M.etcd = etcd |
545 | 546 |
|
| 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 | + |
546 | 551 | function M.etcd.get_common(e) |
547 | 552 | local common_cfg = e:list(prefix .. "/common") |
548 | 553 | assert(common_cfg.box,"no box config in etcd common tree") |
@@ -590,12 +595,35 @@ local function etcd_load( M, etcd_conf, local_cfg ) |
590 | 595 | return all_cfg |
591 | 596 | end |
592 | 597 |
|
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 |
594 | 620 |
|
595 | | - local all_cfg = etcd:get_all() |
596 | 621 | 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)) |
598 | 625 | end |
| 626 | + |
599 | 627 | local common_cfg = all_cfg.common |
600 | 628 | local all_instances_cfg = all_cfg.instances |
601 | 629 |
|
@@ -789,6 +817,8 @@ local M |
789 | 817 | def = k |
790 | 818 | k = self |
791 | 819 | end |
| 820 | + assert(type(M._flat) == 'table', ('internal cfg (M._flat) have unexpected type "%s"'):format(type(M._flat))) |
| 821 | + |
792 | 822 | if M._flat[k] ~= nil then |
793 | 823 | return M._flat[k] |
794 | 824 | elseif def ~= nil then |
|
0 commit comments