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

Add dbless persistence #8670

Merged
merged 3 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
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
25 changes: 22 additions & 3 deletions kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,15 @@ local function flush_delayed_response(ctx)
end


local function has_declarative_config(kong_config)
return kong_config.declarative_config or kong_config.declarative_config_string
end


local function parse_declarative_config(kong_config)
local dc = declarative.new_config(kong_config)

if not kong_config.declarative_config and not kong_config.declarative_config_string then
if not has_declarative_config(kong_config) then
-- return an empty configuration,
-- including only the default workspace
local entities, _, _, meta = dc:parse_table({ _format_version = "2.1" })
Expand Down Expand Up @@ -676,7 +681,17 @@ function Kong.init_worker()
return
end

if declarative_entities then
if not has_declarative_config(kong.configuration) and
declarative.get_current_hash() ~= nil then
-- if there is no declarative config set and a config is present in LMDB,
-- just build the router and plugins iterator
ngx_log(ngx_INFO, "found persisted lmdb config, loading...")
local ok, err = declarative_init_build()
if not ok then
stash_init_worker_error("failed to initialize declarative config: " .. err)
return
end
elseif declarative_entities then
ok, err = load_declarative_config(kong.configuration,
declarative_entities,
declarative_meta)
Expand All @@ -688,7 +703,11 @@ function Kong.init_worker()
else
-- stream does not need to load declarative config again, just build
-- the router and plugins iterator
declarative_init_build()
local ok, err = declarative_init_build()
if not ok then
stash_init_worker_error("failed to initialize declarative config: " .. err)
return
end
end
end

Expand Down
99 changes: 14 additions & 85 deletions spec/02-integration/02-cmd/03-reload_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,6 @@ local helpers = require "spec.helpers"
local cjson = require "cjson"


local function get_kong_workers()
local workers
helpers.wait_until(function()
local pok, admin_client = pcall(helpers.admin_client)
if not pok then
return false
end
local res = admin_client:send {
method = "GET",
path = "/",
}
if not res or res.status ~= 200 then
return false
end
local body = assert.res_status(200, res)
local json = cjson.decode(body)

admin_client:close()
workers = json.pids.workers
return true
end, 10)
return workers
end


local function assert_wait_call(fn, ...)
local res
local args = { ... }
Expand All @@ -38,52 +13,6 @@ local function assert_wait_call(fn, ...)
end


local function wait_until_no_common_workers(workers, expected_total, strategy)
if strategy == "cassandra" then
ngx.sleep(0.5)
end
helpers.wait_until(function()
local pok, admin_client = pcall(helpers.admin_client)
if not pok then
return false
end
local res = assert(admin_client:send {
method = "GET",
path = "/",
})
assert.res_status(200, res)
local json = cjson.decode(assert.res_status(200, res))
admin_client:close()

local new_workers = json.pids.workers
local total = 0
local common = 0
if new_workers then
for _, v in ipairs(new_workers) do
total = total + 1
for _, v_old in ipairs(workers) do
if v == v_old then
common = common + 1
break
end
end
end
end
return common == 0 and total == (expected_total or total)
end)
end


local function kong_reload(strategy, ...)
local workers = get_kong_workers()
local ok, err = helpers.kong_exec(...)
if ok then
wait_until_no_common_workers(workers, 1, strategy)
end
return ok, err
end


for _, strategy in helpers.each_strategy() do

describe("kong reload #" .. strategy, function()
Expand All @@ -104,7 +33,7 @@ describe("kong reload #" .. strategy, function()
local nginx_pid = assert_wait_call(helpers.file.read, helpers.test_conf.nginx_pid)

-- kong_exec uses test conf too, so same prefix
assert(kong_reload(strategy, "reload --prefix " .. helpers.test_conf.prefix))
assert(helpers.reload_kong(strategy, "reload --prefix " .. helpers.test_conf.prefix))

local nginx_pid_after = assert_wait_call(helpers.file.read, helpers.test_conf.nginx_pid)

Expand All @@ -121,14 +50,14 @@ describe("kong reload #" .. strategy, function()
local client = helpers.http_client("0.0.0.0", 9002, 5000)
client:close()

local workers = get_kong_workers()
local workers = helpers.get_kong_workers()

local nginx_pid = assert(helpers.file.read(helpers.test_conf.nginx_pid),
"no nginx master PID")

assert(helpers.kong_exec("reload --conf spec/fixtures/reload.conf"))

wait_until_no_common_workers(workers, 1)
helpers.wait_until_no_common_workers(workers, 1)

-- same master PID
assert.equal(nginx_pid, helpers.file.read(helpers.test_conf.nginx_pid))
Expand All @@ -147,7 +76,7 @@ describe("kong reload #" .. strategy, function()
local client = helpers.http_client("0.0.0.0", 9002, 5000)
client:close()

local workers = get_kong_workers()
local workers = helpers.get_kong_workers()

local nginx_pid = assert(helpers.file.read(helpers.test_conf.nginx_pid),
"no nginx master PID")
Expand All @@ -156,7 +85,7 @@ describe("kong reload #" .. strategy, function()
proxy_listen = "0.0.0.0:9000"
}))

wait_until_no_common_workers(workers, 1)
helpers.wait_until_no_common_workers(workers, 1)

-- same master PID
assert.equal(nginx_pid, helpers.file.read(helpers.test_conf.nginx_pid))
Expand All @@ -171,7 +100,7 @@ describe("kong reload #" .. strategy, function()
proxy_listen = "0.0.0.0:9002"
}, nil, true))

local workers = get_kong_workers()
local workers = helpers.get_kong_workers()

-- http_client errors out if cannot connect
local client = helpers.http_client("0.0.0.0", 9002, 5000)
Expand All @@ -181,7 +110,7 @@ describe("kong reload #" .. strategy, function()
.. " --nginx-conf spec/fixtures/custom_nginx.template"))


wait_until_no_common_workers(workers, 1)
helpers.wait_until_no_common_workers(workers, 1)

-- new server
client = helpers.http_client(helpers.mock_upstream_host,
Expand Down Expand Up @@ -213,7 +142,7 @@ describe("kong reload #" .. strategy, function()
local pids_1 = json.pids
client:close()

assert(kong_reload(strategy, "reload --prefix " .. helpers.test_conf.prefix))
assert(helpers.reload_kong(strategy, "reload --prefix " .. helpers.test_conf.prefix))

client = helpers.admin_client()
local res = assert(client:get("/"))
Expand Down Expand Up @@ -250,7 +179,7 @@ describe("kong reload #" .. strategy, function()
local node_id_1 = json.node_id
client:close()

assert(kong_reload(strategy, "reload --prefix " .. helpers.test_conf.prefix))
assert(helpers.reload_kong(strategy, "reload --prefix " .. helpers.test_conf.prefix))

client = helpers.admin_client()
local res = assert(client:get("/"))
Expand Down Expand Up @@ -326,7 +255,7 @@ describe("kong reload #" .. strategy, function()
- example.test
]], yaml_file)

assert(kong_reload(strategy, "reload --prefix " .. helpers.test_conf.prefix, {
assert(helpers.reload_kong(strategy, "reload --prefix " .. helpers.test_conf.prefix, {
declarative_config = yaml_file,
}))

Expand Down Expand Up @@ -396,7 +325,7 @@ describe("kong reload #" .. strategy, function()
return true
end)

assert(kong_reload(strategy, "reload --prefix " .. helpers.test_conf.prefix))
assert(helpers.reload_kong(strategy, "reload --prefix " .. helpers.test_conf.prefix))

admin_client = assert(helpers.admin_client())
local res = assert(admin_client:send {
Expand Down Expand Up @@ -493,7 +422,7 @@ describe("kong reload #" .. strategy, function()
return true
end)

assert(kong_reload(strategy, "reload --prefix " .. helpers.test_conf.prefix))
assert(helpers.reload_kong(strategy, "reload --prefix " .. helpers.test_conf.prefix))

admin_client = assert(helpers.admin_client())
local res = assert(admin_client:send {
Expand Down Expand Up @@ -584,7 +513,7 @@ describe("kong reload #" .. strategy, function()
weight: 100
]], yaml_file)

assert(kong_reload(strategy, "reload --prefix " .. helpers.test_conf.prefix, {
assert(helpers.reload_kong(strategy, "reload --prefix " .. helpers.test_conf.prefix, {
declarative_config = yaml_file,
}))

Expand Down Expand Up @@ -733,7 +662,7 @@ describe("key-auth plugin invalidation on dbless reload #off", function()
keyauth_credentials:
- key: my-new-key
]], yaml_file)
assert(kong_reload("off", "reload --prefix " .. helpers.test_conf.prefix, {
assert(helpers.reload_kong("off", "reload --prefix " .. helpers.test_conf.prefix, {
declarative_config = yaml_file,
}))

Expand Down
Loading