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

LogCleaner: avoid scanning logs too frequently #776

Merged
merged 1 commit into from
Jan 25, 2022
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
4 changes: 4 additions & 0 deletions src/cleanup_with_absolute_prefix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ TEST(CleanImmediatelyWithAbsolutePrefix, logging) {
LOG(INFO) << "cleanup test";
}

for (unsigned i = 0; i < 10; ++i) {
LOG(ERROR) << "cleanup test";
}

google::DisableLogCleaner();
}

Expand Down
26 changes: 23 additions & 3 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ GLOG_DEFINE_int32(logbuflevel, 0,
" ...)");
GLOG_DEFINE_int32(logbufsecs, 30,
"Buffer log messages for at most this many seconds");

GLOG_DEFINE_int32(logcleansecs, 60 * 5, // every 5 minutes
"Clean overdue logs every this many seconds");

GLOG_DEFINE_int32(logemaillevel, 999,
"Email log messages logged at this level or higher"
" (0 means email all; 3 means email FATAL only;"
Expand Down Expand Up @@ -484,9 +488,12 @@ class LogCleaner {
void Enable(unsigned int overdue_days);
void Disable();

// update next_cleanup_time_
void UpdateCleanUpTime();

void Run(bool base_filename_selected,
const string& base_filename,
const string& filename_extension) const;
const string& filename_extension);

bool enabled() const { return enabled_; }

Expand All @@ -503,6 +510,7 @@ class LogCleaner {

bool enabled_;
unsigned int overdue_days_;
int64 next_cleanup_time_; // cycle count at which to clean overdue log
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As before: why using a signed integer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As before: I use integer because another timer ( e.g. next_flush_time_ ) use integer, too.

};

LogCleaner log_cleaner;
Expand Down Expand Up @@ -1303,7 +1311,7 @@ void LogFileObject::Write(bool force_flush,
}
}

LogCleaner::LogCleaner() : enabled_(false), overdue_days_(7) {}
LogCleaner::LogCleaner() : enabled_(false), overdue_days_(7), next_cleanup_time_(0) {}

void LogCleaner::Enable(unsigned int overdue_days) {
enabled_ = true;
Expand All @@ -1314,12 +1322,24 @@ void LogCleaner::Disable() {
enabled_ = false;
}

void LogCleaner::UpdateCleanUpTime() {
const int64 next = (FLAGS_logcleansecs
* 1000000); // in usec
next_cleanup_time_ = CycleClock_Now() + UsecToCycles(next);
}

void LogCleaner::Run(bool base_filename_selected,
const string& base_filename,
const string& filename_extension) const {
const string& filename_extension) {
assert(enabled_);
assert(!base_filename_selected || !base_filename.empty());

// avoid scanning logs too frequently
if (CycleClock_Now() < next_cleanup_time_) {
return;
TommyWu-fdgkhdkgh marked this conversation as resolved.
Show resolved Hide resolved
}
UpdateCleanUpTime();

vector<string> dirs;

if (!base_filename_selected) {
Expand Down