If the date(include year, month and day) in oldest cfg file name and the
date of created oldest log file is different, logd cannot find out the
oldest cfg file. The oldest cfg file is never removed when log file rotation.
So number of cfg file is huge after time while the log file has already been
rotated.
Update to remove the cfg in this case.
Also limit the number of cfg files should be removed if there are still huge
cfg files in disk to avoid hanging main thread
---
src/log/logd/lgs_filehdl.cc | 8 ++--
src/log/logd/lgs_stream.cc | 96 -
2 files changed, 47 insertions(+), 57 deletions(-)
diff --git a/src/log/logd/lgs_filehdl.cc b/src/log/logd/lgs_filehdl.cc
index be2b95e1f..f31e45883 100644
--- a/src/log/logd/lgs_filehdl.cc
+++ b/src/log/logd/lgs_filehdl.cc
@@ -794,10 +794,10 @@ int get_number_of_cfg_files_hdl(void *indata, void
*outdata,
}
}
-if ((old_ind != -1) && (cfg_old_date == log_old_date) &&
-(cfg_old_time <= log_old_time)) {
- TRACE_1(" (cfg_old_date:%d == log_old_date:%d) &&"
- " (cfg_old_time:%d <= log_old_time:%d )",
+if ((old_ind != -1) && (((cfg_old_date == log_old_date) &&
+(cfg_old_time <= log_old_time)) || (cfg_old_date < log_old_date))) {
+ TRACE_1(" (cfg_old_date:%d - log_old_date:%d) -"
+ " (cfg_old_time:%d - log_old_time:%d )",
cfg_old_date, log_old_date, cfg_old_time, log_old_time);
TRACE_1("oldest: %s", cfg_namelist[old_ind]->d_name);
n = snprintf(oldest_file, max_outsize, "%s/%s", path.c_str(),
diff --git a/src/log/logd/lgs_stream.cc b/src/log/logd/lgs_stream.cc
index d31ae170b..cb9a935e7 100644
--- a/src/log/logd/lgs_stream.cc
+++ b/src/log/logd/lgs_stream.cc
@@ -270,69 +270,59 @@ static int delete_config_file(log_stream_t *stream) {
/**
* Remove oldest log file until there are 'maxFilesRotated' - 1 files left
+ * The oldest cfg files are also removed
*
* @param stream
- * @return -1 on error
+ * @return true/false
*/
-static int rotate_if_needed(log_stream_t *stream) {
+static bool rotate_if_needed(log_stream_t *stream) {
char oldest_log_file[PATH_MAX];
char oldest_cfg_file[PATH_MAX];
- int rc = 0;
- int log_file_cnt, cfg_file_cnt;
- bool oldest_cfg = false;
+ const int max_files_rotated = static_cast(stream->maxFilesRotated);
TRACE_ENTER();
- /* Rotate out log files from previous lifes */
- if ((log_file_cnt = get_number_of_log_files_h(stream, oldest_log_file)) ==
- -1) {
-rc = -1;
-goto done;
- }
-
- /* Rotate out cfg files from previous lifes */
- if (!((cfg_file_cnt = get_number_of_cfg_files_h(stream, oldest_cfg_file)) ==
--1)) {
-oldest_cfg = true;
- }
-
- TRACE("delete oldest_cfg_file: %s oldest_log_file %s", oldest_cfg_file,
-oldest_log_file);
-
- /*
- ** Remove until we have one less than allowed, we are just about to
- ** create a new one again.
- */
- while (log_file_cnt >= static_cast(stream->maxFilesRotated)) {
-if ((rc = file_unlink_h(oldest_log_file)) == -1) {
- LOG_NO("Could not log delete: %s - %s", oldest_log_file,
strerror(errno));
- goto done;
+ // Get number of log files and the oldest log file
+ int log_file_cnt = get_number_of_log_files_h(stream, oldest_log_file);
+ while (log_file_cnt >= max_files_rotated) {
+TRACE("Delete oldest_log_file %s", oldest_log_file);
+if (file_unlink_h(oldest_log_file) == -1) {
+ LOG_NO("Delete log file fail: %s - %s", oldest_log_file,
strerror(errno));
+ return false;
}
-
-if (oldest_cfg == true) {
- oldest_cfg = false;
- if ((rc = file_unlink_h(oldest_cfg_file)) == -1) {
-LOG_NO("Could not cfg delete: %s - %s", oldest_cfg_file,
- strerror(errno));
-goto done;
- }
-}
-
-if ((log_file_cnt = get_number_of_log_files_h(stream, oldest_log_file)) ==
--1) {
- rc = -1;
- goto done;
+log_file_cnt = get_number_of_log_files_h(stream, oldest_log_file);
+ }
+ if (log_file_cnt == -1) return false;
+
+ // Housekeeping for cfg files
+ int number_deleted_files = 0;
+ int cfg_file_cnt = get_number_of_cfg_files_h(stream, oldest_cfg_file);
+ while (cfg_file_cnt >= max_files_rotated) {
+TRACE("Delete oldest_cfg_file %s", oldest_cfg_file);
+if (file_unlink_h(oldest_cfg_file) == -1) {
+ LOG_NO("Delete cfg file fail: %s - %s", oldest_cfg_file,
strerror(errno));
+ return false;
}
-
-if (!((cfg_file_cnt = get_number_of_cfg_files_h(stream, oldest_cfg_file))
==
- -1)) {
- oldest_cfg = true;
+++number_deleted_files;
+cfg_file_cnt = get_number_of_cfg_files_h(stream, oldest_cfg_file);
+
+// If there is too much cfg files that the rotation hasn't deleted them
+// in previous, lgs should limit the deleting to avoid main thread is hung
+// due to the deleting huge cfg files will take long times.
+// The workaround here is that hard-code to