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

perf(etcd): refactor some code to improve performance #12011

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 14 additions & 12 deletions apisix/core/config_etcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ local NGX_INFO = ngx.INFO
local check_schema = require("apisix.core.schema").check
local exiting = ngx.worker.exiting
local insert_tab = table.insert
local remove_tab = table.remove
local type = type
local ipairs = ipairs
local setmetatable = setmetatable
Expand Down Expand Up @@ -741,22 +742,23 @@ local function sync_data(self)

-- avoid space waste
if self.sync_times > 100 then
local values_original = table.clone(self.values)
table.clear(self.values)

for i = 1, #values_original do
local val = values_original[i]
local pre = 1
local cur = 1
table.clear(self.values_hash)
log.info("clear stale data in `values_hash` for key: ", key)
for _, val in ipairs(self.values) do
if val then
table.insert(self.values, val)
self.values[pre] = val
key = short_key(self, val.key)
self.values_hash[key] = pre
pre = pre + 1
end
end

table.clear(self.values_hash)
log.info("clear stale data in `values_hash` for key: ", key)
cur = cur + 1
end

for i = 1, #self.values do
key = short_key(self, self.values[i].key)
self.values_hash[key] = i
for i = cur - 1, pre, -1 do
remove_tab(self.values, i)
end

self.sync_times = 0
Expand Down