Skip to content

Commit

Permalink
fix(*): drop luasocket in cli
Browse files Browse the repository at this point in the history
This is the follow-up PR of #11127

Changing the socket type from luasocket to openresty cosocket causes some test fail weirdly. After investigating, it's mainly because the cosocket support yield and setkeepalive. See the comments in tests.

https://konghq.atlassian.net/browse/FTI-4937
  • Loading branch information
catbro666 committed Aug 8, 2023
1 parent 593b5b2 commit e9a7309
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 73 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@
[#11090](https://github.com/Kong/kong/pull/11090)
- Remove kong branding from kong HTML error template.
[#11150](https://github.com/Kong/kong/pull/11150)
- Drop luasocket in cli
[#11177](https://github.com/Kong/kong/pull/11177)

#### Status API

Expand Down
8 changes: 8 additions & 0 deletions bin/busted
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ require("kong.globalpatches")({
rbusted = true
})

-- some libraries used in test like spec/helpers
-- calls cosocket in module level, and as LuaJIT's
-- `require` is implemented in C, this throws
-- "attempt to yield across C-call boundary" error
-- the following pure-lua implementation is to bypass
-- this limitation, without need to modify all tests
_G.require = require "spec.require".require

-- Busted command-line runner
require 'busted.runner'({ standalone = false })

Expand Down
3 changes: 3 additions & 0 deletions kong/cmd/utils/migrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ local function reset(schema_state, db, ttl)
-- failed to acquire locks - maybe locks table was dropped?
log.error(err .. " - retrying without cluster lock")
log("Resetting database...")
-- ROLLBACK in order to solve this error
-- ERROR: current transaction is aborted, commands ignored until end of transaction block
assert(db.connector:query("ROLLBACK;"))
assert(db:schema_reset())
log("Database successfully reset")
return true
Expand Down
23 changes: 10 additions & 13 deletions kong/db/strategies/postgres/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,7 @@ local setkeepalive

local function reconnect(config)
local phase = get_phase()
if phase == "init" or phase == "init_worker" or ngx.IS_CLI then
-- Force LuaSocket usage in the CLI in order to allow for self-signed
-- certificates to be trusted (via opts.cafile) in the resty-cli
-- interpreter (no way to set lua_ssl_trusted_certificate).
if phase == "init" or phase == "init_worker" then
config.socket_type = "luasocket"

else
Expand All @@ -219,16 +216,16 @@ local function reconnect(config)
return nil, err
end

if connection.sock:getreusedtimes() == 0 then
if config.schema == "" then
local res = connection:query("SELECT CURRENT_SCHEMA AS schema")
if res and res[1] and res[1].schema and res[1].schema ~= null then
config.schema = res[1].schema
else
config.schema = "public"
end
if config.schema == "" then
local res = connection:query("SELECT CURRENT_SCHEMA AS schema")
if res and res[1] and res[1].schema and res[1].schema ~= null then
config.schema = res[1].schema
else
config.schema = "public"
end
end

if connection.sock:getreusedtimes() == 0 then
ok, err = connection:query(concat {
"SET SCHEMA ", connection:escape_literal(config.schema), ";\n",
"SET TIME ZONE ", connection:escape_literal("UTC"), ";",
Expand Down Expand Up @@ -298,7 +295,7 @@ function _mt:init()
local res, err = self:query("SHOW server_version_num;")
local ver = tonumber(res and res[1] and res[1].server_version_num)
if not ver then
return nil, "failed to retrieve PostgreSQL server_version_num: " .. err
return nil, "failed to retrieve PostgreSQL server_version_num: " .. (err or "")
end

local major = floor(ver / 10000)
Expand Down
4 changes: 4 additions & 0 deletions spec/02-integration/02-cmd/10-migrations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ for _, strategy in helpers.each_strategy() do

local db = assert(DB.new(tmp_conf, strategy))
assert(db:init_connector())
-- in spec/helpers.lua, db has already been init'ed
-- the stored connection will be reused here,
-- so we need to set schema explicitly to 'kong_migrations_tests'
assert(db:connect())
assert(db.connector:query("SET SCHEMA 'kong_migrations_tests';\n"))
finally(function()
db.connector:close()
end)
Expand Down
69 changes: 32 additions & 37 deletions spec/02-integration/03-db/01-db_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,7 @@ for _, strategy in helpers.each_strategy() do
assert(db:close())
end)

it("returns opened connection when using cosockets", function()
-- bin/busted runs with ngx.IS_CLI = true, which forces luasocket to
-- be used in the DB connector (for custom CAs to work)
-- Disable this behavior for this test, especially considering we
-- are running within resty-cli, and thus within timer_by_lua (which
-- can support cosockets).
it("returns opened connection when IS_CLI=false", function()
ngx.IS_CLI = false

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -264,7 +259,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

it("returns opened connection when using luasocket", function()
it("returns opened connection when IS_CLI=true", function()
ngx.IS_CLI = true

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -278,15 +273,15 @@ for _, strategy in helpers.each_strategy() do
assert.is_table(conn)

if strategy == "postgres" then
assert.equal("luasocket", db.connector:get_stored_connection().sock_type)
assert.equal("nginx", db.connector:get_stored_connection().sock_type)
assert.is_false(db.connector:get_stored_connection().config.ssl)

end

db:close()
end)

postgres_only("returns opened connection with ssl (cosockets)", function()
postgres_only("returns opened connection with ssl (IS_CLI=false)", function()
ngx.IS_CLI = false

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -312,7 +307,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

postgres_only("returns opened connection with ssl (luasocket)", function()
postgres_only("returns opened connection with ssl (IS_CLI=true)", function()
ngx.IS_CLI = true

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -330,15 +325,15 @@ for _, strategy in helpers.each_strategy() do
assert.is_table(conn)

if strategy == "postgres" then
assert.equal("luasocket", db.connector:get_stored_connection().sock_type)
assert.equal("nginx", db.connector:get_stored_connection().sock_type)
assert.is_true(db.connector:get_stored_connection().config.ssl)

end

db:close()
end)

postgres_only("connects to postgres with readonly account (cosockets)", function()
postgres_only("connects to postgres with readonly account (IS_CLI=false)", function()
ngx.IS_CLI = false

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand Down Expand Up @@ -366,7 +361,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

postgres_only("connects to postgres with readonly account (luasocket)", function()
postgres_only("connects to postgres with readonly account (IS_CLI=true)", function()
ngx.IS_CLI = true

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -385,13 +380,13 @@ for _, strategy in helpers.each_strategy() do

assert.is_nil(db.connector:get_stored_connection("read"))

assert.equal("luasocket", db.connector:get_stored_connection("write").sock_type)
assert.equal("nginx", db.connector:get_stored_connection("write").sock_type)

if strategy == "portgres" then
assert.is_false(db.connector:get_stored_connection("write").config.ssl)
end

assert.equal("luasocket", db.connector:get_stored_connection().sock_type)
assert.equal("nginx", db.connector:get_stored_connection().sock_type)

if strategy == "portgres" then
assert.is_false(db.connector:get_stored_connection("write").config.ssl)
Expand All @@ -407,7 +402,7 @@ for _, strategy in helpers.each_strategy() do
helpers.get_db_utils(strategy, {})
end)

it("returns true when there is a stored connection (cosockets)", function()
it("returns true when there is a stored connection (IS_CLI=false)", function()
ngx.IS_CLI = false

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -432,7 +427,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

it("returns true when there is a stored connection (luasocket)", function()
it("returns true when there is a stored connection (IS_CLI=true)", function()
ngx.IS_CLI = true

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -446,7 +441,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_table(conn)

if strategy == "postgres" then
assert.equal("luasocket", db.connector:get_stored_connection().sock_type)
assert.equal("nginx", db.connector:get_stored_connection().sock_type)
assert.is_false(db.connector:get_stored_connection().config.ssl)

end
Expand All @@ -457,7 +452,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

postgres_only("returns true when there is a stored connection with ssl (cosockets)", function()
postgres_only("returns true when there is a stored connection with ssl (IS_CLI=false)", function()
ngx.IS_CLI = false

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand Down Expand Up @@ -485,7 +480,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

postgres_only("returns true when there is a stored connection with ssl (luasocket)", function()
postgres_only("returns true when there is a stored connection with ssl (IS_CLI=true)", function()
ngx.IS_CLI = true

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -503,7 +498,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_table(conn)

if strategy == "postgres" then
assert.equal("luasocket", db.connector:get_stored_connection().sock_type)
assert.equal("nginx", db.connector:get_stored_connection().sock_type)
assert.is_true(db.connector:get_stored_connection().config.ssl)

end
Expand All @@ -514,7 +509,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

it("returns true when there is no stored connection (cosockets)", function()
it("returns true when there is no stored connection (IS_CLI=false)", function()
ngx.IS_CLI = false

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -527,7 +522,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_true(db:setkeepalive())
end)

it("returns true when there is no stored connection (luasocket)", function()
it("returns true when there is no stored connection (IS_CLI=true)", function()
ngx.IS_CLI = true

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -540,7 +535,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_true(db:setkeepalive())
end)

postgres_only("keepalives both read only and write connection (cosockets)", function()
postgres_only("keepalives both read only and write connection (IS_CLI=false)", function()
ngx.IS_CLI = false

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand Down Expand Up @@ -577,7 +572,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

postgres_only("connects and keepalives only write connection (luasocket)", function()
postgres_only("connects and keepalives only write connection (IS_CLI=true)", function()
ngx.IS_CLI = true

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -598,7 +593,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_nil(err)
assert.is_table(conn)

assert.equal("luasocket", db.connector:get_stored_connection("write").sock_type)
assert.equal("nginx", db.connector:get_stored_connection("write").sock_type)
assert.is_nil(db.connector:get_stored_connection("read"))

if strategy == "postgres" then
Expand All @@ -620,7 +615,7 @@ for _, strategy in helpers.each_strategy() do
helpers.get_db_utils(strategy, {})
end)

it("returns true when there is a stored connection (cosockets)", function()
it("returns true when there is a stored connection (IS_CLI=false)", function()
ngx.IS_CLI = false

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -643,7 +638,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_true(db:close())
end)

it("returns true when there is a stored connection (luasocket)", function()
it("returns true when there is a stored connection (IS_CLI=true)", function()
ngx.IS_CLI = true

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -657,7 +652,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_table(conn)

if strategy == "postgres" then
assert.equal("luasocket", db.connector:get_stored_connection().sock_type)
assert.equal("nginx", db.connector:get_stored_connection().sock_type)
assert.is_false(db.connector:get_stored_connection().config.ssl)

end
Expand All @@ -666,7 +661,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_true(db:close())
end)

postgres_only("returns true when there is a stored connection with ssl (cosockets)", function()
postgres_only("returns true when there is a stored connection with ssl (IS_CLI=false)", function()
ngx.IS_CLI = false

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -692,7 +687,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_true(db:close())
end)

postgres_only("returns true when there is a stored connection with ssl (luasocket)", function()
postgres_only("returns true when there is a stored connection with ssl (IS_CLI=true)", function()
ngx.IS_CLI = true

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -710,15 +705,15 @@ for _, strategy in helpers.each_strategy() do
assert.is_table(conn)

if strategy == "postgres" then
assert.equal("luasocket", db.connector:get_stored_connection().sock_type)
assert.equal("nginx", db.connector:get_stored_connection().sock_type)
assert.is_true(db.connector:get_stored_connection().config.ssl)

end

assert.is_true(db:close())
end)

it("returns true when there is no stored connection (cosockets)", function()
it("returns true when there is no stored connection (IS_CLI=false)", function()
ngx.IS_CLI = false

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -731,7 +726,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_true(db:close())
end)

it("returns true when there is no stored connection (luasocket)", function()
it("returns true when there is no stored connection (IS_CLI=true)", function()
ngx.IS_CLI = true

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -744,7 +739,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, db:close())
end)

postgres_only("returns true when both read-only and write connection exists (cosockets)", function()
postgres_only("returns true when both read-only and write connection exists (IS_CLI=false)", function()
ngx.IS_CLI = false

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand Down Expand Up @@ -781,7 +776,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

postgres_only("returns true when both read-only and write connection exists (luasocket)", function()
postgres_only("returns true when both read-only and write connection exists (IS_CLI=true)", function()
ngx.IS_CLI = true

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -801,7 +796,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_nil(err)
assert.is_table(conn)

assert.equal("luasocket", db.connector:get_stored_connection("write").sock_type)
assert.equal("nginx", db.connector:get_stored_connection("write").sock_type)
assert.is_nil(db.connector:get_stored_connection("read"))

if strategy == "postgres" then
Expand Down
Loading

0 comments on commit e9a7309

Please sign in to comment.