Skip to content

Commit

Permalink
refactor: adjusting the position of ai module (#8120)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzssangglass authored Oct 20, 2022
1 parent 332928e commit 23356d6
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 17 deletions.
1 change: 1 addition & 0 deletions apisix/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
41 changes: 41 additions & 0 deletions apisix/core/event.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--
-- 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 CONST = {
BUILD_ROUTER = 1,
}

local _M = {
CONST = CONST,
}

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
4 changes: 2 additions & 2 deletions apisix/http/route.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.core.event")
local ipairs = ipairs
local type = type
local error = error
Expand Down Expand Up @@ -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.CONST.BUILD_ROUTER, uri_routes)
core.log.info("route items: ", core.json.delay_encode(uri_routes, true))

if with_parameter then
Expand Down
2 changes: 2 additions & 0 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
38 changes: 27 additions & 11 deletions apisix/core/ai.lua → apisix/plugins/ai.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
--
local require = require
local core = require("apisix.core")
local router = require("apisix.router")
local event = require("apisix.core.event")
local ipairs = ipairs
local pcall = pcall
local loadstring = loadstring
Expand Down Expand Up @@ -43,10 +45,20 @@ local route_lrucache = core.lrucache.new({
count = 512
})

local _M = {}
local schema = {}

local plugin_name = "ai"

local _M = {
version = 0.1,
priority = 25000,
name = plugin_name,
schema = schema,
scope = "global",
}

local orig_router_match
local router


local function match_route(ctx)
orig_router_match(ctx)
Expand All @@ -57,7 +69,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
Expand Down Expand Up @@ -89,8 +101,8 @@ local function gen_get_cache_key_func(route_flags)
end


function _M.routes_analyze(routes)
-- TODO: we need to add a option in config.yaml to enable this feature(default is true)
local function routes_analyze(routes)
-- 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
Expand All @@ -116,23 +128,27 @@ 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.CONST.BUILD_ROUTER, routes_analyze)
end


function _M.init_worker()
orig_router_match = router.router_http.match
end

return _M
2 changes: 0 additions & 2 deletions apisix/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions t/admin/plugins.t
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ __DATA__
}
--- response_body
ai
real-ip
client-control
proxy-control
Expand Down
2 changes: 1 addition & 1 deletion t/core/config.t
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ __DATA__
GET /t
--- response_body
etcd host: http://127.0.0.1:2379
first plugin: "real-ip"
first plugin: "ai"



Expand Down
1 change: 0 additions & 1 deletion t/core/ai.t → t/plugin/ai.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 23356d6

Please sign in to comment.