From 26b73702a9e439d41e9b4e1ed30399be45ee194d Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Wed, 19 Oct 2022 15:10:27 +0800 Subject: [PATCH 1/9] refactor: adjusting the position of ai module --- apisix/event.lua | 40 +++++++++++++++++++++++++++++++++ apisix/http/route.lua | 4 ++-- apisix/init.lua | 2 +- apisix/{core => plugins}/ai.lua | 36 ++++++++++++++++++++--------- apisix/router.lua | 2 -- conf/config-default.yaml | 1 + t/admin/plugins.t | 1 + t/{core => plugin}/ai.t | 1 - 8 files changed, 70 insertions(+), 17 deletions(-) create mode 100644 apisix/event.lua rename apisix/{core => plugins}/ai.lua (83%) rename t/{core => plugin}/ai.t (99%) diff --git a/apisix/event.lua b/apisix/event.lua new file mode 100644 index 000000000000..53c968891852 --- /dev/null +++ b/apisix/event.lua @@ -0,0 +1,40 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +local constants = { + CREATE_NEW_HTTP_ROUTER = 1, +} + +local _M = { + constants = constants, +} + +local events = {} + +function _M.push(type, ...) + local handler = events[type] + if handler then + handler(...) + end +end + +function _M.register(type, handler) + -- TODO: we can register more than one handler + events[type] = handler +end + +return _M diff --git a/apisix/http/route.lua b/apisix/http/route.lua index 28e121708dc3..dbc85d603597 100644 --- a/apisix/http/route.lua +++ b/apisix/http/route.lua @@ -21,7 +21,7 @@ local service_fetch = require("apisix.http.service").get local core = require("apisix.core") local expr = require("resty.expr.v1") local plugin_checker = require("apisix.plugin").plugin_checker -local ai = require("apisix.core.ai") +local event = require("apisix.event") local ipairs = ipairs local type = type local error = error @@ -92,7 +92,7 @@ function _M.create_radixtree_uri_router(routes, uri_routes, with_parameter) end end - ai.routes_analyze(uri_routes) + event.push(event.constants.CREATE_NEW_HTTP_ROUTER, uri_routes) core.log.info("route items: ", core.json.delay_encode(uri_routes, true)) if with_parameter then diff --git a/apisix/init.lua b/apisix/init.lua index 04920196d24b..9bb9eafc1da4 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -143,8 +143,8 @@ function _M.http_init_worker() end end - plugin.init_worker() router.http_init_worker() + plugin.init_worker() require("apisix.http.service").init_worker() plugin_config.init_worker() require("apisix.consumer").init_worker() diff --git a/apisix/core/ai.lua b/apisix/plugins/ai.lua similarity index 83% rename from apisix/core/ai.lua rename to apisix/plugins/ai.lua index 57a8a60e0e1b..65a45bc9028f 100644 --- a/apisix/core/ai.lua +++ b/apisix/plugins/ai.lua @@ -16,6 +16,8 @@ -- local require = require local core = require("apisix.core") +local router = require("apisix.router") +local event = require("apisix.event") local ipairs = ipairs local pcall = pcall local loadstring = loadstring @@ -43,10 +45,23 @@ local route_lrucache = core.lrucache.new({ count = 512 }) -local _M = {} +local schema = { + type = "object", + properties = { + } +} -local orig_router_match -local router +local plugin_name = "ai" + +local _M = { + version = 0.1, + priority = 25000, + name = plugin_name, + schema = schema, + scope = "global", +} + +local orig_router_match = router.router_http.match local function match_route(ctx) orig_router_match(ctx) @@ -57,7 +72,7 @@ end local function ai_match(ctx) local key = get_cache_key_func(ctx) core.log.info("route cache key: ", core.log.delay_exec(encode_base64, key)) - local ver = router.user_routes.conf_version + local ver = router.router_http.user_routes.conf_version local route_cache = route_lrucache(key, ver, match_route, ctx) -- if the version has not changed, use the cached route @@ -89,7 +104,7 @@ local function gen_get_cache_key_func(route_flags) end -function _M.routes_analyze(routes) +local function routes_analyze(routes) -- TODO: we need to add a option in config.yaml to enable this feature(default is true) local route_flags = core.table.new(0, 2) for _, route in ipairs(routes) do @@ -116,23 +131,22 @@ function _M.routes_analyze(routes) if route_flags["vars"] or route_flags["filter_fun"] or route_flags["remote_addr"] then - router.match = orig_router_match + router.router_http.match = orig_router_match else core.log.info("use ai plane to match route") - router.match = ai_match + router.router_http.match = ai_match local ok, err = gen_get_cache_key_func(route_flags) if not ok then core.log.error("generate get_cache_key_func failed:", err) - router.match = orig_router_match + router.router_http.match = orig_router_match end end end -function _M.init_worker(router_http) - router = router_http - orig_router_match = router.match +function _M.init() + event.register(event.constants.CREATE_NEW_HTTP_ROUTER, routes_analyze) end return _M diff --git a/apisix/router.lua b/apisix/router.lua index 4b6ed6c32600..9bdafebbdb6e 100644 --- a/apisix/router.lua +++ b/apisix/router.lua @@ -19,7 +19,6 @@ local http_route = require("apisix.http.route") local apisix_upstream = require("apisix.upstream") local core = require("apisix.core") local plugin_checker = require("apisix.plugin").plugin_checker -local ai_init = require("apisix.core.ai").init_worker local str_lower = string.lower local error = error local ipairs = ipairs @@ -84,7 +83,6 @@ function _M.http_init_worker() local router_http = require("apisix.http.router." .. router_http_name) attach_http_router_common_methods(router_http) - ai_init(router_http) router_http.init_worker(filter) _M.router_http = router_http diff --git a/conf/config-default.yaml b/conf/config-default.yaml index 526c65866018..bad0e41e4c1d 100755 --- a/conf/config-default.yaml +++ b/conf/config-default.yaml @@ -389,6 +389,7 @@ graphql: #cmd: ["ls", "-l"] plugins: # plugin list (sorted by priority) + - ai # priority: 25000 - real-ip # priority: 23000 - client-control # priority: 22000 - proxy-control # priority: 21990 diff --git a/t/admin/plugins.t b/t/admin/plugins.t index b13919138f08..74827e437ebf 100644 --- a/t/admin/plugins.t +++ b/t/admin/plugins.t @@ -61,6 +61,7 @@ __DATA__ } --- response_body +ai real-ip client-control proxy-control diff --git a/t/core/ai.t b/t/plugin/ai.t similarity index 99% rename from t/core/ai.t rename to t/plugin/ai.t index 14d4e7b10273..3c0cd62d97e0 100644 --- a/t/core/ai.t +++ b/t/plugin/ai.t @@ -130,7 +130,6 @@ use ai plane to match route if i == 1 then -- arg_k = a, match route res, err = httpc:request_uri(uri1) - ngx.log(ngx.WARN, "res : ", require("inspect")(res)) assert(res.status == 200) else -- arg_k = v, not match route From 8e93f8bc4a2f92170d56b15ee87f06b56b71f2bc Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Wed, 19 Oct 2022 15:33:04 +0800 Subject: [PATCH 2/9] resolve comments --- apisix/event.lua | 4 ++-- apisix/http/route.lua | 2 +- apisix/plugins/ai.lua | 8 ++------ 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/apisix/event.lua b/apisix/event.lua index 53c968891852..df410fce887c 100644 --- a/apisix/event.lua +++ b/apisix/event.lua @@ -15,12 +15,12 @@ -- limitations under the License. -- -local constants = { +local CONST = { CREATE_NEW_HTTP_ROUTER = 1, } local _M = { - constants = constants, + CONST = CONST, } local events = {} diff --git a/apisix/http/route.lua b/apisix/http/route.lua index dbc85d603597..32ffcc777cd7 100644 --- a/apisix/http/route.lua +++ b/apisix/http/route.lua @@ -92,7 +92,7 @@ function _M.create_radixtree_uri_router(routes, uri_routes, with_parameter) end end - event.push(event.constants.CREATE_NEW_HTTP_ROUTER, uri_routes) + event.push(event.CONST.CREATE_NEW_HTTP_ROUTER, uri_routes) core.log.info("route items: ", core.json.delay_encode(uri_routes, true)) if with_parameter then diff --git a/apisix/plugins/ai.lua b/apisix/plugins/ai.lua index 65a45bc9028f..f7bdf802a3cf 100644 --- a/apisix/plugins/ai.lua +++ b/apisix/plugins/ai.lua @@ -45,11 +45,7 @@ local route_lrucache = core.lrucache.new({ count = 512 }) -local schema = { - type = "object", - properties = { - } -} +local schema = {} local plugin_name = "ai" @@ -146,7 +142,7 @@ end function _M.init() - event.register(event.constants.CREATE_NEW_HTTP_ROUTER, routes_analyze) + event.register(event.CONST.CREATE_NEW_HTTP_ROUTER, routes_analyze) end return _M From 5df63c458d43ed026a762f9313c9ae3bb098bfca Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Wed, 19 Oct 2022 15:46:37 +0800 Subject: [PATCH 3/9] resolve comments --- apisix/event.lua | 2 +- apisix/http/route.lua | 2 +- apisix/plugins/ai.lua | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apisix/event.lua b/apisix/event.lua index df410fce887c..e84957b04f19 100644 --- a/apisix/event.lua +++ b/apisix/event.lua @@ -16,7 +16,7 @@ -- local CONST = { - CREATE_NEW_HTTP_ROUTER = 1, + BUILD_ROUTER = 1, } local _M = { diff --git a/apisix/http/route.lua b/apisix/http/route.lua index 32ffcc777cd7..e3fb012d2ad8 100644 --- a/apisix/http/route.lua +++ b/apisix/http/route.lua @@ -92,7 +92,7 @@ function _M.create_radixtree_uri_router(routes, uri_routes, with_parameter) end end - event.push(event.CONST.CREATE_NEW_HTTP_ROUTER, uri_routes) + event.push(event.CONST.BUILD_ROUTER, uri_routes) core.log.info("route items: ", core.json.delay_encode(uri_routes, true)) if with_parameter then diff --git a/apisix/plugins/ai.lua b/apisix/plugins/ai.lua index f7bdf802a3cf..0fa76e4fb812 100644 --- a/apisix/plugins/ai.lua +++ b/apisix/plugins/ai.lua @@ -142,7 +142,7 @@ end function _M.init() - event.register(event.CONST.CREATE_NEW_HTTP_ROUTER, routes_analyze) + event.register(event.CONST.BUILD_ROUTER, routes_analyze) end return _M From 66461256cc933b69f75c47551f5f298a948929fc Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Wed, 19 Oct 2022 15:53:07 +0800 Subject: [PATCH 4/9] resolve comments --- apisix/core.lua | 1 + apisix/{ => core}/event.lua | 0 apisix/http/route.lua | 2 +- apisix/plugins/ai.lua | 2 +- 4 files changed, 3 insertions(+), 2 deletions(-) rename apisix/{ => core}/event.lua (100%) diff --git a/apisix/core.lua b/apisix/core.lua index f421716e6bdc..7e317f7e553b 100644 --- a/apisix/core.lua +++ b/apisix/core.lua @@ -54,4 +54,5 @@ return { os = require("apisix.core.os"), pubsub = require("apisix.core.pubsub"), math = require("apisix.core.math"), + event = require("apisix.core.event"), } diff --git a/apisix/event.lua b/apisix/core/event.lua similarity index 100% rename from apisix/event.lua rename to apisix/core/event.lua diff --git a/apisix/http/route.lua b/apisix/http/route.lua index e3fb012d2ad8..6292b577a071 100644 --- a/apisix/http/route.lua +++ b/apisix/http/route.lua @@ -21,7 +21,7 @@ local service_fetch = require("apisix.http.service").get local core = require("apisix.core") local expr = require("resty.expr.v1") local plugin_checker = require("apisix.plugin").plugin_checker -local event = require("apisix.event") +local event = require("apisix.core.event") local ipairs = ipairs local type = type local error = error diff --git a/apisix/plugins/ai.lua b/apisix/plugins/ai.lua index 0fa76e4fb812..731e8340dd88 100644 --- a/apisix/plugins/ai.lua +++ b/apisix/plugins/ai.lua @@ -17,7 +17,7 @@ local require = require local core = require("apisix.core") local router = require("apisix.router") -local event = require("apisix.event") +local event = require("apisix.core.event") local ipairs = ipairs local pcall = pcall local loadstring = loadstring From d0d388d97cb034e0b23ec399e720276cb0adf277 Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Wed, 19 Oct 2022 19:54:41 +0800 Subject: [PATCH 5/9] fix CI --- apisix/init.lua | 4 +++- apisix/plugins/ai.lua | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/apisix/init.lua b/apisix/init.lua index 9bb9eafc1da4..2030e0241d51 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -143,8 +143,8 @@ function _M.http_init_worker() end end - router.http_init_worker() plugin.init_worker() + router.http_init_worker() require("apisix.http.service").init_worker() plugin_config.init_worker() require("apisix.consumer").init_worker() @@ -152,6 +152,8 @@ function _M.http_init_worker() apisix_upstream.init_worker() require("apisix.plugins.ext-plugin.init").init_worker() + -- TODO: need to revisit code layering and avoid similar hacking + require("apisix.plugins.ai").init_worker() local_conf = core.config.local_conf() diff --git a/apisix/plugins/ai.lua b/apisix/plugins/ai.lua index 731e8340dd88..05f602a89f3f 100644 --- a/apisix/plugins/ai.lua +++ b/apisix/plugins/ai.lua @@ -57,7 +57,8 @@ local _M = { scope = "global", } -local orig_router_match = router.router_http.match +local orig_router_match + local function match_route(ctx) orig_router_match(ctx) @@ -145,4 +146,9 @@ function _M.init() event.register(event.CONST.BUILD_ROUTER, routes_analyze) end + +function _M.init_worker() + orig_router_match = router.router_http.match +end + return _M From 34f6c571363d46f8c9a51ecd856463523eaa38fd Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Wed, 19 Oct 2022 19:55:29 +0800 Subject: [PATCH 6/9] resolve comments --- apisix/core/event.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/apisix/core/event.lua b/apisix/core/event.lua index e84957b04f19..38459ff11060 100644 --- a/apisix/core/event.lua +++ b/apisix/core/event.lua @@ -25,6 +25,7 @@ local _M = { local events = {} + function _M.push(type, ...) local handler = events[type] if handler then From 91a25dc2f02304a22d8b92bc2b8cd3fbe3791e2d Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Wed, 19 Oct 2022 21:04:12 +0800 Subject: [PATCH 7/9] fix CI --- t/core/config.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/core/config.t b/t/core/config.t index 29d1cc52dc07..18191dae724f 100644 --- a/t/core/config.t +++ b/t/core/config.t @@ -38,7 +38,7 @@ __DATA__ GET /t --- response_body etcd host: http://127.0.0.1:2379 -first plugin: "real-ip" +first plugin: "ai" From 70cf532c848f19aee6400f6fe89b972881d7e9ff Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Wed, 19 Oct 2022 21:24:51 +0800 Subject: [PATCH 8/9] rerun From 2dfdf614b2921be7cb5baeca2a2ced16675b6c88 Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Wed, 19 Oct 2022 21:26:02 +0800 Subject: [PATCH 9/9] trigger CI --- apisix/plugins/ai.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/plugins/ai.lua b/apisix/plugins/ai.lua index 05f602a89f3f..6b60aac29c1f 100644 --- a/apisix/plugins/ai.lua +++ b/apisix/plugins/ai.lua @@ -102,7 +102,7 @@ end local function routes_analyze(routes) - -- TODO: we need to add a option in config.yaml to enable this feature(default is true) + -- TODO: need to add a option in config.yaml to enable this feature(default is true) local route_flags = core.table.new(0, 2) for _, route in ipairs(routes) do if route.methods then