apache / apisix

The Cloud-Native API Gateway
https://apisix.apache.org/blog/
Apache License 2.0
14.46k stars 2.52k forks source link

bug: set route immedidately after start APISIX will not effect #9992

Open jiangfucheng opened 1 year ago

jiangfucheng commented 1 year ago

Current Behavior

When add "set route" and "hit route" logic in one "TEST" block, I found the "set route" will not effect, eg:

=== TEST 1: test route
--- ONLY
--- yaml_config
deployment:
  role: traditional
  role_traditional:
    config_provider: etcd
  admin:
    admin_key: null
  etcd:
    prefix: "/apisix-test"
--- config
    location /t {
        content_by_lua_block {
        -- ngx.sleep(0.5)
            local http = require "resty.http"
            local t = require("lib.test_admin").test

            local code, body = t('/apisix/admin/routes/1',
                 ngx.HTTP_PUT,
                 [[{
                        "uri": "/hello",
                        "upstream": {
                            "type": "roundrobin",
                            "nodes": {
                                "127.0.0.1:1980": 1
                            }
                        }
                }]]
                )
            if code >= 300 then
                ngx.status = code
                return
            end
            ngx.say(body)

            -- hit
            local httpc = http.new()
            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
            local res, err = httpc:request_uri(uri, {
                method = "GET"
            })

            if not res then
                ngx.log(ngx.ERR, err)
                return
            end
            ngx.print(res.body)
        }
    }
--- request
GET /t
--- response_body
passed
hello world

I debug APISIX and found the reason is in this logic:

https://github.com/apache/apisix/blob/fed6f79b1e284590e91c03c90fda3b6ba6c4aa06/apisix/core/config_etcd.lua#L509-L522

the values variable is a object but not a list(table)

the calling code is here:

https://github.com/apache/apisix/blob/fed6f79b1e284590e91c03c90fda3b6ba6c4aa06/apisix/core/config_etcd.lua#L621C19-L621C19

  1. Through the above logic, we can find that APISIX received the events but did not handle it correctly.
  2. According to my test, only the key is prefix: /apisix-test this format will cause this bug.

I am currently not familiar with the code for lua-resty-etcd, so I haven't find the real reason yet.

Expected Behavior

The test case should be passed no matter execute any times

Error Logs

No response

Steps to Reproduce

  1. execute the test case pasted above.(most of the time it fails, so we need execute multiple to validations)

Environment

wklken commented 1 year ago

if you use apisix 3.2.2 or 3.4.x, it may related to this bug https://github.com/apache/apisix/pull/9967

jiangfucheng commented 1 year ago

if you use apisix 3.2.2 or 3.4.x, it may related to this bug #9967

No, it's another bug, https://github.com/apache/apisix/pull/9967 have been merged