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: accelerate the creation of the consumer cache #11840

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
51 changes: 44 additions & 7 deletions apisix/consumer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ local function filter_consumers_list(data_list)
return list
end

local function plugin_consumer()
local plugin_consumer
do
local consumers_id_cache = {}
function plugin_consumer()
local plugins = {}

if consumers.values == nil then
Expand All @@ -101,10 +104,19 @@ local function plugin_consumer()
if not plugins[name] then
plugins[name] = {
nodes = {},
len = 0,
conf_version = consumers.conf_version
}
end

local cached_consumer = consumers_id_cache[val.value.id]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know why we have to add consumers_id_cache

it seems useless

if cached_consumer and
cached_consumer.modifiedIndex == val.modifiedIndex then
plugins[name].len = plugins[name].len + 1
core.table.insert(plugins[name].nodes, plugins[name].len,
cached_consumer)
goto CONTINUE_INTERNAL
end
-- if the val is a Consumer, clone it to the local consumer;
-- if the val is a Credential, to get the Consumer by consumer_name and then clone
-- it to the local consumer.
Expand Down Expand Up @@ -141,7 +153,10 @@ local function plugin_consumer()
consumer.auth_conf = config
core.log.info("consumer:", core.json.delay_encode(consumer))
core.table.insert(plugins[name].nodes, consumer)
consumers_id_cache[val.value.id] = consumer
end

::CONTINUE_INTERNAL::
end

::CONTINUE::
Expand All @@ -150,6 +165,9 @@ local function plugin_consumer()
return plugins
end

end


_M.filter_consumers_list = filter_consumers_list

function _M.get_consumer_key_from_credential_key(key)
Expand Down Expand Up @@ -186,24 +204,43 @@ function _M.consumers()
end


local function create_consume_cache(consumers_conf, key_attr)
local create_consume_cache
do
local consumers_plugin_key_lrucache_tab = {}

local function create_new_consumer(consumer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need a better name, it is not creating a new consumer

it should be transform or fill secret, what do you think?

local new_consumer = core.table.clone(consumer)
new_consumer.auth_conf = secret.fetch_secrets(new_consumer.auth_conf, false)
return new_consumer
end


function create_consume_cache(consumers_conf, key_attr, plugin_name)
local consumer_names = {}
local lru_cache = consumers_plugin_key_lrucache_tab[plugin_name]
if lru_cache == nil then
lru_cache = core.lrucache.new({
ttl = 60 * 60 * 24, count = 20480
})
consumers_plugin_key_lrucache_tab[plugin_name] = lru_cache
end

for _, consumer in ipairs(consumers_conf.nodes) do
core.log.info("consumer node: ", core.json.delay_encode(consumer))
local new_consumer = core.table.clone(consumer)
new_consumer.auth_conf = secret.fetch_secrets(new_consumer.auth_conf, true,
new_consumer.auth_conf, "")
consumer_names[new_consumer.auth_conf[key_attr]] = new_consumer
local new_consumer = lru_cache(consumer.auth_conf[key_attr],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think: this should be enough, and it seems easier

Suggested change
local new_consumer = lru_cache(consumer.auth_conf[key_attr],
local new_consumer = lru_cache(consumer, nil, create_new_consumer, consumer)

consumer.modifiedIndex, create_new_consumer, consumer)
consumer_names[consumer.auth_conf[key_attr]] = new_consumer
end

return consumer_names
end

end


function _M.consumers_kv(plugin_name, consumer_conf, key_attr)
local consumers = lrucache("consumers_key#" .. plugin_name, consumer_conf.conf_version,
create_consume_cache, consumer_conf, key_attr)
create_consume_cache, consumer_conf, key_attr, plugin_name)

return consumers
end
Expand Down
Loading