Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: optimizing performance of router match with lrucache #8102

Merged
merged 11 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions apisix/core/improve.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
--
tzssangglass marked this conversation as resolved.
Show resolved Hide resolved
-- 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 require = require
local core = require("apisix.core")
local ipairs = ipairs
local type = type

local recovery_func = core.lrucache.new({
count = 512
})

local route_lrucache = core.lrucache.new({
count = 512
tzssangglass marked this conversation as resolved.
Show resolved Hide resolved
})

local enable_route_cache

local _M = {}

local ori_router_match
local router

local route_ckey
local route_cver

function _M.router_match(ctx)
-- TODO: generate cache key dynamically according to the user routes
route_ckey = ctx.var.uri .. "-" .. ctx.var.method .. "-" ..
ctx.var.host .. "-" .. ctx.var.remote_addr
route_cver = router.user_routes.conf_version
recovery_func(route_ckey, route_cver, function()
-- if the version has changed, fall back to the original router match
router.match = ori_router_match
return true
end)
end


local function create_router_cache(ctx)
-- replace the router match
router.match = function()
-- do nothing
core.log.info("hit route cache, key: ", route_ckey)
end
return ctx.matched_route
end


function _M.router_match_post(ctx)
if not enable_route_cache then
return
end

local route_cache = route_lrucache(route_ckey, route_cver,
create_router_cache, ctx)
-- if the version has not changed, use the cached route
if route_cache and not ctx.matched_route then
ctx.matched_route = route_cache
end
end


function _M.routes_analyze(routes)
for _, route in ipairs(routes) do
if type(route) == "table" then
if route.value.vars then
enable_route_cache = false
return
end

if route.value.filter_fun then
enable_route_cache = false
return
end

if route.value.priority and route.value.priority ~= 0 then
enable_route_cache = false
return
end
end
end
enable_route_cache = true
end


function _M.enable_route_cache()
tzssangglass marked this conversation as resolved.
Show resolved Hide resolved
return enable_route_cache
end


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

return _M
2 changes: 2 additions & 0 deletions apisix/http/router/radixtree_uri.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local require = require
local core = require("apisix.core")
local base_router = require("apisix.http.route")
local get_services = require("apisix.http.service").services
local routes_analyze = require("apisix.core.improve").routes_analyze
tzssangglass marked this conversation as resolved.
Show resolved Hide resolved
local cached_router_version
local cached_service_version

Expand Down Expand Up @@ -45,6 +46,7 @@ function _M.match(api_ctx)
return true
end

routes_analyze(user_routes.values)
return base_router.match_uri(uri_router, match_opts, api_ctx)
end

Expand Down
3 changes: 3 additions & 0 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ local xrpc = require("apisix.stream.xrpc")
local ctxdump = require("resty.ctxdump")
local debug = require("apisix.debug")
local pubsub_kafka = require("apisix.pubsub.kafka")
local improve = require("apisix.core.improve")
local ngx = ngx
local get_method = ngx.req.get_method
local ngx_exit = ngx.exit
Expand Down Expand Up @@ -374,7 +375,9 @@ function _M.http_access_phase()
api_ctx.var.real_request_uri = api_ctx.var.request_uri
api_ctx.var.request_uri = api_ctx.var.uri .. api_ctx.var.is_args .. (api_ctx.var.args or "")

improve.router_match(api_ctx)
router.router_http.match(api_ctx)
improve.router_match_post(api_ctx)
tzssangglass marked this conversation as resolved.
Show resolved Hide resolved

local route = api_ctx.matched_route
if not route then
Expand Down
2 changes: 2 additions & 0 deletions apisix/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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 improve_init = require("apisix.core.improve").init_worker
local str_lower = string.lower
local error = error
local ipairs = ipairs
Expand Down Expand Up @@ -83,6 +84,7 @@ function _M.http_init_worker()

local router_http = require("apisix.http.router." .. router_http_name)
attach_http_router_common_methods(router_http)
improve_init(router_http)
router_http.init_worker(filter)
_M.router_http = router_http

Expand Down
Loading