From 517a9aa6c7ebb471d77ad6e8d6a81f555bc34534 Mon Sep 17 00:00:00 2001 From: monkeyDluffy6017 <375636559@qq.com> Date: Wed, 31 Aug 2022 16:28:46 +0800 Subject: [PATCH 1/5] fix: reload apisix just once when log roate --- apisix/plugins/log-rotate.lua | 71 ++++++++++++++++++++++++++++++----- t/plugin/log-rotate2.t | 7 ++-- 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/apisix/plugins/log-rotate.lua b/apisix/plugins/log-rotate.lua index 571fc9b52525..60dc7611c8cb 100644 --- a/apisix/plugins/log-rotate.lua +++ b/apisix/plugins/log-rotate.lua @@ -37,7 +37,9 @@ local str_format = string.format local str_reverse = string.reverse local tab_insert = table.insert local tab_sort = table.sort - +local new_tab = require "table.new" +local ngx_sleep = require("apisix.core.utils").sleep +local isempty = require "table.isempty" local local_conf @@ -169,6 +171,32 @@ local function rename_file(log, date_str) end +local function wait_old_nginx_exit() + local found = true + local max_try = 10 -- 10 seconds + local prefix = ngx.config.prefix() + local file_path = prefix .. "/logs/nginx.pid.oldbin" + + for i = 1, max_try do + -- nginx will delete nginx.pid.oldbin after old master process quit + found = file_exists(file_path) + if found then + if i <= max_try then + ngx_sleep(1) + end + + else + break + end + end + + if found then + core.log.error("failed to shutdown the old master after a long time, " + .. "but we still choose to compress log file") + end +end + + local function compression_file(new_file) if not new_file or type(new_file) ~= "string" then core.log.info("compression file: ", new_file, " invalid") @@ -219,6 +247,12 @@ end local function rotate_file(files, now_time, max_kept) + if isempty(files) then + return + end + + local new_files = new_tab(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 +260,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 + tab_insert(new_files, new_file) + end - if enable_compression then + -- send signal to reopen + 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 + -- to avoid losing logs during compression + ngx_sleep(0.5) + wait_old_nginx_exit() + + 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 +333,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 = new_tab(2, 0) + if access_log_file_size >= max_size then - rotate_file({DEFAULT_ACCESS_LOG_FILENAME}, now_time, max_kept) + tab_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) + tab_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 1a28f33e8829..fb5283c97b0a 100644 --- a/t/plugin/log-rotate2.t +++ b/t/plugin/log-rotate2.t @@ -34,7 +34,7 @@ plugins: - log-rotate plugin_attr: log-rotate: - interval: 1 + interval: 2 max_kept: 3 enable_compression: true _EOC_ @@ -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 @@ -138,6 +138,7 @@ start xxxxxx } --- response_body passed +--- timeout: 10 From aa1419696056ac146748de71361249b3106ca7d7 Mon Sep 17 00:00:00 2001 From: monkeyDluffy6017 <375636559@qq.com> Date: Tue, 6 Sep 2022 14:26:12 +0800 Subject: [PATCH 2/5] fix: waiting for the end of the old requests --- apisix/plugins/log-rotate.lua | 42 +++++++++-------------------------- t/plugin/log-rotate2.t | 8 +++---- 2 files changed, 14 insertions(+), 36 deletions(-) diff --git a/apisix/plugins/log-rotate.lua b/apisix/plugins/log-rotate.lua index 60dc7611c8cb..2467ac4a7491 100644 --- a/apisix/plugins/log-rotate.lua +++ b/apisix/plugins/log-rotate.lua @@ -48,6 +48,7 @@ local INTERVAL = 60 * 60 -- rotate interval (unit: second) local MAX_KEPT = 24 * 7 -- max number of log files will be kept local MAX_SIZE = -1 -- max size of file will be rotated local COMPRESSION_FILE_SUFFIX = ".tar.gz" -- compression file suffix +local WAIT_TIME_BEFORE_COMPRESS = 60 -- wait time before compress local rotate_time local default_logs local enable_compression = false @@ -171,32 +172,6 @@ local function rename_file(log, date_str) end -local function wait_old_nginx_exit() - local found = true - local max_try = 10 -- 10 seconds - local prefix = ngx.config.prefix() - local file_path = prefix .. "/logs/nginx.pid.oldbin" - - for i = 1, max_try do - -- nginx will delete nginx.pid.oldbin after old master process quit - found = file_exists(file_path) - if found then - if i <= max_try then - ngx_sleep(1) - end - - else - break - end - end - - if found then - core.log.error("failed to shutdown the old master after a long time, " - .. "but we still choose to compress log file") - end -end - - local function compression_file(new_file) if not new_file or type(new_file) ~= "string" then core.log.info("compression file: ", new_file, " invalid") @@ -246,7 +221,7 @@ local function file_size(file) end -local function rotate_file(files, now_time, max_kept) +local function rotate_file(files, now_time, max_kept, wait_time) if isempty(files) then return end @@ -263,7 +238,7 @@ local function rotate_file(files, now_time, max_kept) tab_insert(new_files, new_file) end - -- send signal to reopen + -- 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")) @@ -272,9 +247,9 @@ local function rotate_file(files, now_time, max_kept) end if enable_compression then + -- Waiting for the end of the old requests -- to avoid losing logs during compression - ngx_sleep(0.5) - wait_old_nginx_exit() + ngx_sleep(wait_time) for _, new_file in ipairs(new_files) do compression_file(new_file) @@ -299,11 +274,14 @@ local function rotate() local interval = INTERVAL local max_kept = MAX_KEPT local max_size = MAX_SIZE + -- for test + local wait_time = WAIT_TIME_BEFORE_COMPRESS local attr = plugin.plugin_attr(plugin_name) if attr then interval = attr.interval or interval max_kept = attr.max_kept or max_kept max_size = attr.max_size or max_size + wait_time = attr.wait_time or wait_time enable_compression = attr.enable_compression or enable_compression end @@ -329,7 +307,7 @@ local function rotate() if now_time >= rotate_time then local files = {DEFAULT_ACCESS_LOG_FILENAME, DEFAULT_ERROR_LOG_FILENAME} - rotate_file(files, now_time, max_kept) + rotate_file(files, now_time, max_kept, wait_time) -- reset rotate time rotate_time = rotate_time + interval @@ -347,7 +325,7 @@ local function rotate() tab_insert(files, DEFAULT_ERROR_LOG_FILENAME) end - rotate_file(files, now_time, max_kept) + rotate_file(files, now_time, max_kept, wait_time) end end diff --git a/t/plugin/log-rotate2.t b/t/plugin/log-rotate2.t index fb5283c97b0a..8d05a2c216aa 100644 --- a/t/plugin/log-rotate2.t +++ b/t/plugin/log-rotate2.t @@ -34,9 +34,10 @@ plugins: - log-rotate plugin_attr: log-rotate: - interval: 2 + interval: 1 max_kept: 3 enable_compression: true + wait_time: 0 _EOC_ $block->set_value("yaml_config", $yaml_config); @@ -61,7 +62,7 @@ __DATA__ location /t { content_by_lua_block { ngx.log(ngx.ERR, "start xxxxxx") - ngx.sleep(3.5) + ngx.sleep(2.5) local has_split_access_file = false local has_split_error_file = false local lfs = require("lfs") @@ -105,7 +106,7 @@ start xxxxxx --- config location /t { content_by_lua_block { - ngx.sleep(3) + ngx.sleep(2) local default_logs = {} for file_name in lfs.dir(ngx.config.prefix() .. "/logs/") do @@ -138,7 +139,6 @@ start xxxxxx } --- response_body passed ---- timeout: 10 From ed976039ad1fca3497a02892aac45bed9de8be33 Mon Sep 17 00:00:00 2001 From: monkeyDluffy6017 <375636559@qq.com> Date: Tue, 6 Sep 2022 17:34:31 +0800 Subject: [PATCH 3/5] bugfix: change wait time to 0.5s --- apisix/plugins/log-rotate.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apisix/plugins/log-rotate.lua b/apisix/plugins/log-rotate.lua index 2467ac4a7491..bd2908073a99 100644 --- a/apisix/plugins/log-rotate.lua +++ b/apisix/plugins/log-rotate.lua @@ -48,7 +48,7 @@ local INTERVAL = 60 * 60 -- rotate interval (unit: second) local MAX_KEPT = 24 * 7 -- max number of log files will be kept local MAX_SIZE = -1 -- max size of file will be rotated local COMPRESSION_FILE_SUFFIX = ".tar.gz" -- compression file suffix -local WAIT_TIME_BEFORE_COMPRESS = 60 -- wait time before compress +local WAIT_TIME_BEFORE_COMPRESS = 0.5 -- wait time before compress local rotate_time local default_logs local enable_compression = false @@ -247,7 +247,7 @@ local function rotate_file(files, now_time, max_kept, wait_time) end if enable_compression then - -- Waiting for the end of the old requests + -- Waiting for nginx reopen files -- to avoid losing logs during compression ngx_sleep(wait_time) From 150218582b51883f0288ef7e7d24c529beef47e4 Mon Sep 17 00:00:00 2001 From: monkeyDluffy6017 <375636559@qq.com> Date: Wed, 7 Sep 2022 14:41:10 +0800 Subject: [PATCH 4/5] style: use core.table.xx --- apisix/core/table.lua | 1 + apisix/plugins/log-rotate.lua | 20 ++++++++------------ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/apisix/core/table.lua b/apisix/core/table.lua index b307cc25d544..4346863079cf 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 bd2908073a99..b9b02b5811d6 100644 --- a/apisix/plugins/log-rotate.lua +++ b/apisix/plugins/log-rotate.lua @@ -35,11 +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 new_tab = require "table.new" local ngx_sleep = require("apisix.core.utils").sleep -local isempty = require "table.isempty" local local_conf @@ -138,12 +134,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 @@ -222,11 +218,11 @@ end local function rotate_file(files, now_time, max_kept, wait_time) - if isempty(files) then + if core.table.isempty(files) then return end - local new_files = new_tab(2, 0) + 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) @@ -235,7 +231,7 @@ local function rotate_file(files, now_time, max_kept, wait_time) return end - tab_insert(new_files, new_file) + core.table.insert(new_files, new_file) end -- send signal to reopen log files @@ -315,14 +311,14 @@ local function rotate() 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 = new_tab(2, 0) + local files = core.table.new(2, 0) if access_log_file_size >= max_size then - tab_insert(files, DEFAULT_ACCESS_LOG_FILENAME) + core.table.insert(files, DEFAULT_ACCESS_LOG_FILENAME) end if error_log_file_size >= max_size then - tab_insert(files, DEFAULT_ERROR_LOG_FILENAME) + core.table.insert(files, DEFAULT_ERROR_LOG_FILENAME) end rotate_file(files, now_time, max_kept, wait_time) From 2c521ae9e415f0ba10d7e3284663f22ac314bccb Mon Sep 17 00:00:00 2001 From: monkeyDluffy6017 <375636559@qq.com> Date: Thu, 8 Sep 2022 14:24:57 +0800 Subject: [PATCH 5/5] refactor: use hardcore wait time other than config --- apisix/plugins/log-rotate.lua | 12 ++++-------- t/plugin/log-rotate2.t | 5 ++--- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/apisix/plugins/log-rotate.lua b/apisix/plugins/log-rotate.lua index b9b02b5811d6..60b1e3ddb547 100644 --- a/apisix/plugins/log-rotate.lua +++ b/apisix/plugins/log-rotate.lua @@ -44,7 +44,6 @@ local INTERVAL = 60 * 60 -- rotate interval (unit: second) local MAX_KEPT = 24 * 7 -- max number of log files will be kept local MAX_SIZE = -1 -- max size of file will be rotated local COMPRESSION_FILE_SUFFIX = ".tar.gz" -- compression file suffix -local WAIT_TIME_BEFORE_COMPRESS = 0.5 -- wait time before compress local rotate_time local default_logs local enable_compression = false @@ -217,7 +216,7 @@ local function file_size(file) end -local function rotate_file(files, now_time, max_kept, wait_time) +local function rotate_file(files, now_time, max_kept) if core.table.isempty(files) then return end @@ -245,7 +244,7 @@ local function rotate_file(files, now_time, max_kept, wait_time) if enable_compression then -- Waiting for nginx reopen files -- to avoid losing logs during compression - ngx_sleep(wait_time) + ngx_sleep(0.5) for _, new_file in ipairs(new_files) do compression_file(new_file) @@ -270,14 +269,11 @@ local function rotate() local interval = INTERVAL local max_kept = MAX_KEPT local max_size = MAX_SIZE - -- for test - local wait_time = WAIT_TIME_BEFORE_COMPRESS local attr = plugin.plugin_attr(plugin_name) if attr then interval = attr.interval or interval max_kept = attr.max_kept or max_kept max_size = attr.max_size or max_size - wait_time = attr.wait_time or wait_time enable_compression = attr.enable_compression or enable_compression end @@ -303,7 +299,7 @@ local function rotate() if now_time >= rotate_time then local files = {DEFAULT_ACCESS_LOG_FILENAME, DEFAULT_ERROR_LOG_FILENAME} - rotate_file(files, now_time, max_kept, wait_time) + rotate_file(files, now_time, max_kept) -- reset rotate time rotate_time = rotate_time + interval @@ -321,7 +317,7 @@ local function rotate() core.table.insert(files, DEFAULT_ERROR_LOG_FILENAME) end - rotate_file(files, now_time, max_kept, wait_time) + rotate_file(files, now_time, max_kept) end end diff --git a/t/plugin/log-rotate2.t b/t/plugin/log-rotate2.t index 8d05a2c216aa..617a29b5a7d2 100644 --- a/t/plugin/log-rotate2.t +++ b/t/plugin/log-rotate2.t @@ -37,7 +37,6 @@ plugin_attr: interval: 1 max_kept: 3 enable_compression: true - wait_time: 0 _EOC_ $block->set_value("yaml_config", $yaml_config); @@ -62,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") @@ -106,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