Skip to content

Commit

Permalink
fix: not saving etcd revision (#10671)
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyDluffy6017 authored Dec 21, 2023
1 parent 4f0b85f commit 0a85ba4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 35 deletions.
72 changes: 38 additions & 34 deletions apisix/core/config_etcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,53 +132,57 @@ 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.timeout = local_conf.etcd.watch_timeout

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 initialised, 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 = {}
opts.timeout = 50 -- second
opts.timeout = watch_ctx.timeout or 50 -- second
opts.need_cancel = true
opts.start_revision = watch_ctx.rev

Expand Down
3 changes: 2 additions & 1 deletion conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,8 @@ deployment: # Deployment configurations
host: # Set etcd address(es) in the same etcd cluster.
- "http://127.0.0.1:2379" # If TLS is enabled for etcd, use https://127.0.0.1:2379.
prefix: /apisix # Set etcd prefix.
timeout: 30 # Set timeout in seconds.
timeout: 30 # The timeout when connect/read/write to etcd, Set timeout in seconds.
watch_timeout: 50 # The timeout when watch etcd
# resync_delay: 5 # Set resync time in seconds after a sync failure.
# The actual resync time would be resync_delay plus 50% random jitter.
# health_check_timeout: 10 # Set timeout in seconds for etcd health check.
Expand Down
30 changes: 30 additions & 0 deletions t/core/config_etcd.t
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,33 @@ passed
hello world
passed
{"error_msg":"404 Route Not Found"}



=== TEST 13: the main watcher should be initialised once
--- yaml_config
apisix:
node_listen: 1984
deployment:
role: traditional
role_traditional:
config_provider: etcd
admin:
admin_key: null
etcd:
host:
- "http://127.0.0.1:2379"
watch_timeout: 1
--- config
location /t {
content_by_lua_block {
ngx.sleep(1)
}
}
--- request
GET /t
--- grep_error_log eval
qr/main etcd watcher initialised, revision=/
--- grep_error_log_out
main etcd watcher initialised, revision=
main etcd watcher initialised, revision=

0 comments on commit 0a85ba4

Please sign in to comment.