Skip to content

Commit

Permalink
fix: not saving etcd revision
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyDluffy6017 committed Dec 20, 2023
1 parent 4f0b85f commit 0eee937
Showing 1 changed file with 36 additions and 33 deletions.
69 changes: 36 additions & 33 deletions apisix/core/config_etcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,49 +132,52 @@ local function do_run_watch(premature)
return
end

local local_conf, err = config_local.local_conf()
if not local_conf then
error("no local conf: " .. err)
end
watch_ctx.prefix = local_conf.etcd.prefix .. "/"
-- the main watcher first start
if watch_ctx.started == false then
local local_conf, err = config_local.local_conf()
if not local_conf then
error("no local conf: " .. err)
end
watch_ctx.prefix = local_conf.etcd.prefix .. "/"

watch_ctx.cli, err = get_etcd()
if not watch_ctx.cli then
error("failed to create etcd instance: " .. string(err))
end
watch_ctx.cli, err = get_etcd()
if not watch_ctx.cli then
error("failed to create etcd instance: " .. string(err))
end

local rev = 0
if loaded_configuration then
local _, res = next(loaded_configuration)
if res then
rev = tonumber(res.headers["X-Etcd-Index"])
assert(rev > 0, 'invalid res.headers["X-Etcd-Index"]')
local rev = 0
if loaded_configuration then
local _, res = next(loaded_configuration)
if res then
rev = tonumber(res.headers["X-Etcd-Index"])
assert(rev > 0, 'invalid res.headers["X-Etcd-Index"]')
end
end
end

if rev == 0 then
while true do
local res, err = watch_ctx.cli:get(watch_ctx.prefix)
if not res then
log.error("etcd get: ", err)
ngx_sleep(3)
else
rev = tonumber(res.body.header.revision)
break
if rev == 0 then
while true do
local res, err = watch_ctx.cli:get(watch_ctx.prefix)
if not res then
log.error("etcd get: ", err)
ngx_sleep(3)
else
rev = tonumber(res.body.header.revision)
break
end
end
end
end

watch_ctx.rev = rev + 1
watch_ctx.started = true
watch_ctx.rev = rev + 1
watch_ctx.started = true

log.info("main etcd watcher started, revision=", watch_ctx.rev)
log.info("main etcd watcher started, revision=", watch_ctx.rev)

if watch_ctx.wait_init then
for _, sema in pairs(watch_ctx.wait_init) do
sema:post()
if watch_ctx.wait_init then
for _, sema in pairs(watch_ctx.wait_init) do
sema:post()
end
watch_ctx.wait_init = nil
end
watch_ctx.wait_init = nil
end

local opts = {}
Expand Down

0 comments on commit 0eee937

Please sign in to comment.