Skip to content

Commit

Permalink
bugfix: deleted the object by Admin API, and the cache was not proper…
Browse files Browse the repository at this point in the history
…ly cleaned up during etcd data synchronization. apache#2168
  • Loading branch information
johzchen committed Dec 14, 2020
1 parent fad7894 commit 4c40f9d
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions apisix/core/config_etcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local table = require("apisix.core.table")
local config_local = require("apisix.core.config_local")
local log = require("apisix.core.log")
local json = require("apisix.core.json")
Expand Down Expand Up @@ -314,33 +315,41 @@ local function sync_data(self)
res.value.id = key
self.values[pre_index] = res
res.clean_handlers = {}
log.info("update data by key: ", key)

else
self.sync_times = self.sync_times + 1
self.values[pre_index] = false
self.values_hash[key] = nil
log.info("delete data by key: ", key)
end

elseif res.value then
res.clean_handlers = {}
insert_tab(self.values, res)
self.values_hash[key] = #self.values
res.value.id = key
log.info("insert data by key: ", key)
end

-- avoid space waste
-- todo: need to cover this path, it is important.
if self.sync_times > 100 then
local count = 0
for i = 1, #self.values do
local val = self.values[i]
local values_original = table.clone(self.values)
table.clear(self.values)

for i = 1, #values_original do
local val = values_original[i]
self.values[i] = nil
if val then
count = count + 1
self.values[count] = val
table.insert(self.values, val)
end
end

for i = 1, count do
table.clear(self.values_hash)
log.info("clear stale data in `values_hash` for key: ", key)

for i = 1, #self.values do
key = short_key(self, self.values[i].key)
self.values_hash[key] = i
end
Expand Down

0 comments on commit 4c40f9d

Please sign in to comment.