From 1c0102f120d122c66b64f1bf2585e30c536638e9 Mon Sep 17 00:00:00 2001 From: kingluo Date: Thu, 11 May 2023 02:17:17 +0800 Subject: [PATCH] fix PR --- apisix/core/config_etcd.lua | 24 +++++---- t/core/etcd-sync.t | 68 ++------------------------ t/plugin/error-log-logger-skywalking.t | 4 +- 3 files changed, 22 insertions(+), 74 deletions(-) diff --git a/apisix/core/config_etcd.lua b/apisix/core/config_etcd.lua index 7d25c387eb924..93c1c9f8e5db7 100644 --- a/apisix/core/config_etcd.lua +++ b/apisix/core/config_etcd.lua @@ -36,6 +36,7 @@ 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 @@ -43,6 +44,9 @@ 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") @@ -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 @@ -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 @@ -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 @@ -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 @@ -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)) @@ -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 @@ -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 @@ -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 diff --git a/t/core/etcd-sync.t b/t/core/etcd-sync.t index e74ae19ec710c..aef5e23619a9b 100644 --- a/t/core/etcd-sync.t +++ b/t/core/etcd-sync.t @@ -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 { @@ -126,7 +68,7 @@ waitdir key -=== TEST 3: no update +=== TEST 2: no update --- config location /t { content_by_lua_block { @@ -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 { @@ -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 { @@ -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 diff --git a/t/plugin/error-log-logger-skywalking.t b/t/plugin/error-log-logger-skywalking.t index ffebf1cf4fa59..edb5003c09885 100644 --- a/t/plugin/error-log-logger-skywalking.t +++ b/t/plugin/error-log-logger-skywalking.t @@ -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