Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unnecessary YAML Config reloads #9065

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions apisix/core/config_yaml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions t/cli/test_standalone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
. ./t/cli/common.sh

standalone() {
rm -f conf/apisix.yaml.link
clean_up
git checkout conf/apisix.yaml
}
Expand Down Expand Up @@ -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"
Loading