diff --git a/apisix/core/config_yaml.lua b/apisix/core/config_yaml.lua index 8511eb583fa7..ce8c8321663a 100644 --- a/apisix/core/config_yaml.lua +++ b/apisix/core/config_yaml.lua @@ -63,7 +63,7 @@ local mt = { local apisix_yaml -local apisix_yaml_ctime +local apisix_yaml_mtime local function read_apisix_yaml(premature, pre_mtime) if premature then return @@ -74,9 +74,8 @@ local function read_apisix_yaml(premature, pre_mtime) return end - -- log.info("change: ", json.encode(attributes)) - local last_change_time = attributes.change - if apisix_yaml_ctime == last_change_time then + local last_modification_time = attributes.modification + if apisix_yaml_mtime == last_modification_time then return end @@ -114,7 +113,7 @@ local function read_apisix_yaml(premature, pre_mtime) end apisix_yaml = apisix_yaml_new - apisix_yaml_ctime = last_change_time + apisix_yaml_mtime = last_modification_time log.warn("config file ", apisix_yaml_path, " reloaded.") end @@ -124,12 +123,12 @@ local function sync_data(self) return nil, "missing 'key' arguments" end - if not apisix_yaml_ctime then + if not apisix_yaml_mtime then log.warn("wait for more time") return nil, "failed to read local file " .. apisix_yaml_path end - if self.conf_version == apisix_yaml_ctime then + if self.conf_version == apisix_yaml_mtime then return true end @@ -138,7 +137,7 @@ local function sync_data(self) if not items then self.values = new_tab(8, 0) self.values_hash = new_tab(0, 8) - self.conf_version = apisix_yaml_ctime + self.conf_version = apisix_yaml_mtime return true end @@ -155,7 +154,7 @@ local function sync_data(self) self.values_hash = new_tab(0, 1) local item = items - local conf_item = {value = item, modifiedIndex = apisix_yaml_ctime, + local conf_item = {value = item, modifiedIndex = apisix_yaml_mtime, key = "/" .. self.key} local data_valid = true @@ -202,7 +201,7 @@ local function sync_data(self) end local key = item.id or "arr_" .. i - local conf_item = {value = item, modifiedIndex = apisix_yaml_ctime, + local conf_item = {value = item, modifiedIndex = apisix_yaml_mtime, key = "/" .. self.key .. "/" .. key} if data_valid and self.item_schema then @@ -236,7 +235,7 @@ local function sync_data(self) end end - self.conf_version = apisix_yaml_ctime + self.conf_version = apisix_yaml_mtime return true end diff --git a/t/cli/test_standalone.sh b/t/cli/test_standalone.sh index a0d91c11c4a0..57b665294ce4 100755 --- a/t/cli/test_standalone.sh +++ b/t/cli/test_standalone.sh @@ -20,6 +20,7 @@ . ./t/cli/common.sh standalone() { + rm -f conf/apisix.yaml.link clean_up git checkout conf/apisix.yaml } @@ -138,3 +139,19 @@ if [ ! $code -eq 200 ]; then fi echo "passed: resolve variables in apisix.yaml conf success" + +# Avoid unnecessary config reloads +## Wait for a second else `st_ctime` won't increase +sleep 1 +expected_config_reloads=$(grep "config file $(pwd)/conf/apisix.yaml reloaded." logs/error.log | wc -l) + +## Create a symlink to change the link count and as a result `st_ctime` +ln conf/apisix.yaml conf/apisix.yaml.link +sleep 1 + +actual_config_reloads=$(grep "config file $(pwd)/conf/apisix.yaml reloaded." logs/error.log | wc -l) +if [ $expected_config_reloads -ne $actual_config_reloads ]; then + echo "failed: apisix.yaml was reloaded" + exit 1 +fi +echo "passed: apisix.yaml was not reloaded"