-
Notifications
You must be signed in to change notification settings - Fork 438
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
issue #1038: enable leveldb posix log to switch and evict oldest one #1084
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,14 @@ | |
#ifndef STORAGE_LEVELDB_UTIL_POSIX_LOGGER_H_ | ||
#define STORAGE_LEVELDB_UTIL_POSIX_LOGGER_H_ | ||
|
||
#include <algorithm> | ||
#include <stdio.h> | ||
#include <sys/time.h> | ||
#include <time.h> | ||
#include <algorithm> | ||
#include <set> | ||
#include "leveldb/env.h" | ||
#include "port/port.h" | ||
#include "util/mutexlock.h" | ||
|
||
namespace leveldb { | ||
|
||
|
@@ -101,6 +104,101 @@ class PosixLogger : public Logger { | |
} | ||
}; | ||
|
||
class EnhancePosixLogger : public Logger { | ||
private: | ||
port::Mutex mutex_; | ||
std::string fname_; | ||
uint64_t (*gettid_)(); // Return the thread id for the current thread | ||
int64_t log_size_; | ||
uint64_t log_count_; | ||
int64_t cur_log_size_; | ||
Logger* logger_; | ||
std::set<std::string> log_list_; | ||
char* logbuf_; | ||
public: | ||
EnhancePosixLogger(const std::string& fname, uint64_t (*gettid)(), | ||
int64_t log_size = 2000000000, int64_t log_count = 30) | ||
: fname_(fname), | ||
gettid_(gettid), | ||
log_size_(log_size), | ||
log_count_(log_count), | ||
cur_log_size_(0), | ||
logger_(NULL), | ||
logbuf_(new char[30000]) {} | ||
virtual ~EnhancePosixLogger() { | ||
if (logger_) { | ||
delete logger_; | ||
} | ||
delete[] logbuf_; | ||
} | ||
virtual void Logv(const char* format, va_list ap) { | ||
TrySwitchLog(format, ap); | ||
assert(logger_); | ||
logger_->Logv(format, ap); | ||
} | ||
|
||
private: | ||
void TrySwitchLog(const char* format, va_list ap) { | ||
// try switch log | ||
MutexLock l(&mutex_); | ||
if (logger_ == NULL || cur_log_size_ > log_size_) { | ||
const uint64_t thread_id = (*gettid_)(); | ||
char timebuf[64]; | ||
char* p = timebuf; | ||
|
||
struct timeval now_tv; | ||
gettimeofday(&now_tv, NULL); | ||
const time_t seconds = now_tv.tv_sec; | ||
struct tm t; | ||
localtime_r(&seconds, &t); | ||
p += snprintf(p, 64, | ||
#ifdef OS_LINUX | ||
"%04d-%02d-%02d-%02d:%02d:%02d:%06d.%llu", | ||
#else | ||
"%04d/%02d/%02d-%02d:%02d:%02d.%06d.%llx", | ||
#endif | ||
t.tm_year + 1900, | ||
t.tm_mon + 1, | ||
t.tm_mday, | ||
t.tm_hour, | ||
t.tm_min, | ||
t.tm_sec, | ||
static_cast<int>(now_tv.tv_usec), | ||
static_cast<long long unsigned int>(thread_id)); | ||
std::string logname(fname_ + "."); | ||
logname.append(timebuf); | ||
|
||
Logger* slog = NULL; | ||
FILE* f = fopen(logname.c_str(), "w"); | ||
if (f) { | ||
slog = new PosixLogger(f, gettid_); | ||
if (logger_) { | ||
delete logger_; | ||
} | ||
logger_ = slog; | ||
cur_log_size_= 0; | ||
log_list_.insert(logname); | ||
|
||
// try delete log | ||
if (log_list_.size() > log_count_) { | ||
std::set<std::string>::iterator it = log_list_.begin(); | ||
if (it != log_list_.end()) { | ||
remove(it->c_str()); | ||
} | ||
} // evict oldest log | ||
} // new log open suc | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mutex should be released here. |
||
|
||
char* pl = logbuf_; | ||
char* limit = logbuf_ + 30000; | ||
va_list backup_ap; | ||
va_copy(backup_ap, ap); | ||
pl += vsnprintf(pl, limit - pl, format, backup_ap); | ||
va_end(backup_ap); | ||
cur_log_size_ += (pl >= limit) ? 30000: (pl - logbuf_); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to get cur_log_size through the return value of PosixLogger::Logv to reduce duplicate calculation and memory copy. |
||
} | ||
}; | ||
|
||
} // namespace leveldb | ||
|
||
#endif // STORAGE_LEVELDB_UTIL_POSIX_LOGGER_H_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log_list_ should be initialized by list the directory.