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

issue #1038: enable leveldb posix log to switch and evict oldest one #1084

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/leveldb/util/env_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,10 @@ class PosixEnv : public Env {
}

virtual Status NewLogger(const std::string& fname, Logger** result) {
*result = new EnhancePosixLogger(fname, &PosixEnv::gettid);
return Status::OK();

/*
FILE* f = fopen(fname.c_str(), "w");
if (f == NULL) {
*result = NULL;
Expand All @@ -791,6 +795,7 @@ class PosixEnv : public Env {
*result = new PosixLogger(f, &PosixEnv::gettid);
return Status::OK();
}
*/
}
virtual void SetLogger(Logger* logger) {
Logger::SetDefaultLogger(logger);
Expand Down
100 changes: 99 additions & 1 deletion src/leveldb/util/posix_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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]) {}
Copy link
Collaborator

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.

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
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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_);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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_