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

fix: reload once when log rotate #7869

Merged
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
1 change: 1 addition & 0 deletions apisix/core/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ local _M = {
sort = table.sort,
clone = require("table.clone"),
isarray = require("table.isarray"),
isempty = require("table.isempty"),
}


Expand Down
49 changes: 35 additions & 14 deletions apisix/plugins/log-rotate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ local str_sub = string.sub
local str_find = string.find
local str_format = string.format
local str_reverse = string.reverse
local tab_insert = table.insert
local tab_sort = table.sort

local ngx_sleep = require("apisix.core.utils").sleep
local local_conf


Expand Down Expand Up @@ -135,12 +133,12 @@ local function scan_log_folder(log_file_name)
if n ~= nil then
local log_type = file:sub(n + 2)
if log_type == log_file_name then
tab_insert(t, file)
core.table.insert(t, file)
end
end
end

tab_sort(t, tab_sort_comp)
core.table.sort(t, tab_sort_comp)
return t, log_dir
end

Expand Down Expand Up @@ -219,24 +217,41 @@ end


local function rotate_file(files, now_time, max_kept)
if core.table.isempty(files) then
return
end

local new_files = core.table.new(2, 0)
-- rename the log files
for _, file in ipairs(files) do
local now_date = os_date("%Y-%m-%d_%H-%M-%S", now_time)
local new_file = rename_file(default_logs[file], now_date)
if not new_file then
return
end

local pid = process.get_master_pid()
core.log.warn("send USR1 signal to master process [", pid, "] for reopening log file")
local ok, err = signal.kill(pid, signal.signum("USR1"))
if not ok then
core.log.error("failed to send USR1 signal for reopening log file: ", err)
end
core.table.insert(new_files, new_file)
end

-- send signal to reopen log files
local pid = process.get_master_pid()
core.log.warn("send USR1 signal to master process [", pid, "] for reopening log file")
local ok, err = signal.kill(pid, signal.signum("USR1"))
if not ok then
core.log.error("failed to send USR1 signal for reopening log file: ", err)
end

if enable_compression then
if enable_compression then
-- Waiting for nginx reopen files
-- to avoid losing logs during compression
ngx_sleep(0.5)

for _, new_file in ipairs(new_files) do
compression_file(new_file)
end
end

for _, file in ipairs(files) do
-- clean the oldest file
local log_list, log_dir = scan_log_folder(file)
for i = max_kept + 1, #log_list do
Expand Down Expand Up @@ -288,15 +303,21 @@ local function rotate()

-- reset rotate time
rotate_time = rotate_time + interval

elseif max_size > 0 then
local access_log_file_size = file_size(default_logs[DEFAULT_ACCESS_LOG_FILENAME].file)
local error_log_file_size = file_size(default_logs[DEFAULT_ERROR_LOG_FILENAME].file)
local files = core.table.new(2, 0)

if access_log_file_size >= max_size then
rotate_file({DEFAULT_ACCESS_LOG_FILENAME}, now_time, max_kept)
core.table.insert(files, DEFAULT_ACCESS_LOG_FILENAME)
end

if error_log_file_size >= max_size then
rotate_file({DEFAULT_ERROR_LOG_FILENAME}, now_time, max_kept)
core.table.insert(files, DEFAULT_ERROR_LOG_FILENAME)
end

rotate_file(files, now_time, max_kept)
end
end

Expand Down
4 changes: 2 additions & 2 deletions t/plugin/log-rotate2.t
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ __DATA__
location /t {
content_by_lua_block {
ngx.log(ngx.ERR, "start xxxxxx")
ngx.sleep(2.5)
ngx.sleep(3.5)
local has_split_access_file = false
local has_split_error_file = false
local lfs = require("lfs")
Expand Down Expand Up @@ -105,7 +105,7 @@ start xxxxxx
--- config
location /t {
content_by_lua_block {
ngx.sleep(2)
ngx.sleep(3)

local default_logs = {}
for file_name in lfs.dir(ngx.config.prefix() .. "/logs/") do
Expand Down