Skip to content

Commit

Permalink
fix PR
Browse files Browse the repository at this point in the history
  • Loading branch information
kingluo committed May 11, 2023
1 parent 2fbbd22 commit 1c0102f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 74 deletions.
24 changes: 15 additions & 9 deletions apisix/core/config_etcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ local setmetatable = setmetatable
local ngx_sleep = require("apisix.core.utils").sleep
local ngx_timer_at = ngx.timer.at
local ngx_time = ngx.time
local ngx_sleep = ngx.sleep
local sub_str = string.sub
local tostring = tostring
local tonumber = tonumber
local xpcall = xpcall
local debug = debug
local string = string
local error = error
local pairs = pairs
local next = next
local assert = assert
local rand = math.random
local constants = require("apisix.constants")
local health_check = require("resty.etcd.health_check")
Expand Down Expand Up @@ -111,7 +115,7 @@ local function handle_compacted(err)
local res, err2 = watch_ctx.cli:get(watch_ctx.prefix)
if not res then
log.error(err2)
ngx.sleep(3)
ngx_sleep(3)
else
watch_ctx.rev = res.body.header.revision
break
Expand All @@ -124,7 +128,7 @@ end
local function produce_res(res, err)
-- append res and notify pending watchers
--log.warn("append res, res: ", require("inspect")(res), ", err: ", require("inspect")(err))
table.insert(watch_ctx.res, {res=res, err=err})
insert_tab(watch_ctx.res, {res=res, err=err})
for _, sema in pairs(watch_ctx.sema) do
sema:post()
end
Expand All @@ -151,8 +155,10 @@ local function run_watch(premature)
local rev = 0
if loaded_configuration then
local _, res = next(loaded_configuration)
rev = tonumber(res.headers["X-Etcd-Index"])
assert(rev > 0, 'invalid res.headers["X-Etcd-Index"]')
if res then
rev = tonumber(res.headers["X-Etcd-Index"])
assert(rev > 0, 'invalid res.headers["X-Etcd-Index"]')
end
end

if rev == 0 then
Expand Down Expand Up @@ -187,7 +193,7 @@ local function run_watch(premature)
log.error("watchdir: ", func_err)
handle_compacted(func_err)
produce_res(nil, func_err)
ngx.sleep(3)
ngx_sleep(3)
goto restart_watch
end

Expand Down Expand Up @@ -409,7 +415,7 @@ local function http_waitdir(self, etcd_cli, key, modified_index, timeout)
table.clear(res2.result.events)
for _, evt in ipairs(res.result.events) do
if evt.kv.key:find(key) == 1 then
table.insert(res2.result.events, evt)
insert_tab(res2.result.events, evt)
end
end
--log.warn("res2: ", require("inspect")(res2))
Expand Down Expand Up @@ -597,7 +603,7 @@ local function sync_data(self)
return nil, "missing 'key' arguments"
end

if not self.use_grpc then
if not self.etcd_cli.use_grpc then
init_watch_ctx(self.key)
end

Expand Down Expand Up @@ -741,7 +747,7 @@ local function sync_data(self)
for i = 1, #values_original do
local val = values_original[i]
if val then
table.insert(self.values, val)
insert_tab(self.values, val)
end
end

Expand Down Expand Up @@ -1045,7 +1051,7 @@ local function create_formatter(prefix)
for _, item in ipairs(res.body.kvs) do
if curr_dir_data then
if core_str.has_prefix(item.key, curr_key) then
table.insert(curr_dir_data, etcd_apisix.kvs_to_node(item))
insert_tab(curr_dir_data, etcd_apisix.kvs_to_node(item))
goto CONTINUE
end

Expand Down
68 changes: 5 additions & 63 deletions t/core/etcd-sync.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,65 +22,7 @@ run_tests;

__DATA__

=== TEST 1: minus timeout to watch repeatedly
--- yaml_config
deployment:
role: traditional
role_traditional:
config_provider: etcd
etcd:
# this test requires the HTTP long pull as the gRPC stream is shared and can't change
# default timeout in the fly
use_grpc: false
admin:
admin_key: null
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
local t = require("lib.test_admin").test

local consumers, _ = core.config.new("/consumers", {
automatic = true,
item_schema = core.schema.consumer,
timeout = 0.2
})

ngx.sleep(0.6)
local idx = consumers.prev_index

local code, body = t('/apisix/admin/consumers',
ngx.HTTP_PUT,
[[{
"username": "jobs",
"plugins": {
"basic-auth": {
"username": "jobs",
"password": "123456"
}
}
}]])

ngx.sleep(2)
local new_idx = consumers.prev_index
core.log.info("idx:", idx, " new_idx: ", new_idx)
if new_idx > idx then
ngx.say("prev_index updated")
else
ngx.say("prev_index not update")
end
}
}
--- request
GET /t
--- response_body
prev_index updated
--- error_log eval
qr/(create watch stream for key|cancel watch connection success)/



=== TEST 2: using default timeout
=== TEST 1: using default timeout
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -126,7 +68,7 @@ waitdir key



=== TEST 3: no update
=== TEST 2: no update
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -162,7 +104,7 @@ prev_index not update



=== TEST 4: bad plugin configuration (validated via incremental sync)
=== TEST 3: bad plugin configuration (validated via incremental sync)
--- config
location /t {
content_by_lua_block {
Expand All @@ -182,7 +124,7 @@ property "uri" validation failed



=== TEST 5: bad plugin configuration (validated via full sync)
=== TEST 4: bad plugin configuration (validated via full sync)
--- config
location /t {
content_by_lua_block {
Expand All @@ -196,7 +138,7 @@ property "uri" validation failed



=== TEST 6: bad plugin configuration (validated without sync during start)
=== TEST 5: bad plugin configuration (validated without sync during start)
--- extra_yaml_config
disable_sync_configuration_during_start: true
--- config
Expand Down
4 changes: 2 additions & 2 deletions t/plugin/error-log-logger-skywalking.t
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ qr/Batch Processor\[error-log-logger\] failed to process entries: error while se
--- request
GET /tg
--- response_body
--- error_log eval
qr/.*\[\{\"body\":\{\"text\":\{\"text\":\".*this is an error message for test.*\"\}\},\"endpoint\":\"\",\"service\":\"APISIX\",\"serviceInstance\":\"instance\".*/
--- error_log
this is an error message for test
--- wait: 5
Expand Down

0 comments on commit 1c0102f

Please sign in to comment.