Skip to content

Commit

Permalink
feat(oom): spec code
Browse files Browse the repository at this point in the history
  • Loading branch information
oowl committed Sep 12, 2023
1 parent 5212e5b commit 018cb94
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
2 changes: 1 addition & 1 deletion kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ function Kong.init_worker()
return
end

ok, err = utils.set_worker_oom_score(ngx.worker.pid())
ok, err = utils.set_worker_oom_score(ngx.worker.id())
if not ok then
ngx_log(ngx_WARN, "failed to set worker oom_score_adj: ", err)
return
Expand Down
14 changes: 12 additions & 2 deletions kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1817,16 +1817,26 @@ function _M.set_worker_oom_score(worker_id)
return nil, "could not read oom_score_adj"
end

local oom_score_adj = tonumber(oom_score) - 100
local oom_score_adj = tonumber(oom_score) - 1000
if oom_score_adj < -1000 then
oom_score_adj = -1000
end

local ok, err = pl_file.write("/proc/self/oom_score_adj", tostring(oom_score_adj))
local pid = ngx.worker.pid()
local oom_score_file = "/proc/" .. pid .. "/oom_score_adj"

local io_file = io.open(oom_score_file, "w")
if not io_file then
return nil, "could not open oom_score_adj file"
end

local ok, err = io_file:write(tostring(oom_score_adj))
if not ok then
return nil, "could not write oom_score_adj: " .. err
end

ngx.log(ngx.ERR, "set oom_score_adj " .. pid .. " to ", tostring(oom_score_adj))

return true
end

Expand Down
88 changes: 88 additions & 0 deletions spec/02-integration/05-proxy/33-worker_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
local helpers = require "spec.helpers"

for _, flavor in ipairs({ "traditional", "traditional_compatible" }) do
for _, strategy in helpers.each_strategy({"postgres"}) do
describe("#worker Proxying [#" .. strategy .. "] [#" .. flavor .. "]", function()
local bp
local admin_client
local proxy_client

before_each(function()
bp = helpers.get_db_utils(strategy, {
"routes",
"services",
"upstreams",
"plugins",
})

local service = bp.services:insert()

bp.routes:insert {
hosts = { "worker.com" },
service = service,
}


bp.plugins:insert {
name = "pre-function",
service = { id = service.id },
config = {
access = {[[
local pl_read = require "pl.file".read
local pids = ngx.worker.pids()
local oom_scores = {}
local cjson = require "cjson"
ngx.log(ngx.ERR, "pids: ", cjson.encode(pids))
for i = 1, #pids do
local pid = pids[i]
local file = "/proc/" .. pid .. "/oom_score"
local adj_file = "/proc/" .. pid .. "/oom_score_adj"
local oom_score_adj = tonumber(pl_read(adj_file))
local oom_score = tonumber(pl_read(file))
ngx.log(ngx.ERR, "pid: ", pid, " oom_score: ", oom_score, " oom_score_adj: ", oom_score_adj)
oom_scores[i] = oom_score
end
local worker_0 = oom_scores[1]
for i = 2, #oom_scores do
if worker_0 >= oom_scores[i] then
ngx.log(ngx.ERR, "worker_0: ", worker_0, " worker_", i, ": ", oom_scores[i])
ngx.exit(500)
return
end
end
ngx.exit(200)
]]}
}
}

assert(helpers.start_kong({
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
plugins = "pre-function",
untrusted_lua = "on",
nginx_main_worker_processes = 8,
proxy_error_log = "/tmp/error.log",
}))
proxy_client = helpers.proxy_client()
admin_client = helpers.admin_client()
end)

after_each(function()
admin_client:close()
helpers.stop_kong()
end)

it("worker oom score", function()
-- populate cache
local res = assert(proxy_client:send {
path = "/",
headers = {
["Host"] = "worker.com",
},
})
assert.res_status(200, res)
end)
end)
end
end

0 comments on commit 018cb94

Please sign in to comment.