You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2022/09/08 09:18:02 UTC
[apisix] branch master updated: fix: reload once when log rotate (#7869)
This is an automated email from the ASF dual-hosted git repository.
spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 8b5c9b020 fix: reload once when log rotate (#7869)
8b5c9b020 is described below
commit 8b5c9b02064f37b9c04e901a86ab752967ca9e47
Author: levy liu <37...@qq.com>
AuthorDate: Thu Sep 8 17:17:54 2022 +0800
fix: reload once when log rotate (#7869)
---
apisix/core/table.lua | 1 +
apisix/plugins/log-rotate.lua | 49 ++++++++++++++++++++++++++++++-------------
t/plugin/log-rotate2.t | 4 ++--
3 files changed, 38 insertions(+), 16 deletions(-)
diff --git a/apisix/core/table.lua b/apisix/core/table.lua
index b307cc25d..434686307 100644
--- a/apisix/core/table.lua
+++ b/apisix/core/table.lua
@@ -41,6 +41,7 @@ local _M = {
sort = table.sort,
clone = require("table.clone"),
isarray = require("table.isarray"),
+ isempty = require("table.isempty"),
}
diff --git a/apisix/plugins/log-rotate.lua b/apisix/plugins/log-rotate.lua
index 571fc9b52..60b1e3ddb 100644
--- a/apisix/plugins/log-rotate.lua
+++ b/apisix/plugins/log-rotate.lua
@@ -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
@@ -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
@@ -219,6 +217,12 @@ 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)
@@ -226,17 +230,28 @@ local function rotate_file(files, now_time, max_kept)
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
@@ -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
diff --git a/t/plugin/log-rotate2.t b/t/plugin/log-rotate2.t
index 1a28f33e8..617a29b5a 100644
--- a/t/plugin/log-rotate2.t
+++ b/t/plugin/log-rotate2.t
@@ -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")
@@ -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