fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
673 lines
23 KiB
673 lines
23 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
8 years ago
|
// This source code is licensed under both the GPLv2 (found in the
|
||
|
// COPYING file in the root directory) and Apache 2.0 License
|
||
|
// (found in the LICENSE.Apache file in the root directory).
|
||
11 years ago
|
//
|
||
8 years ago
|
|
||
|
#ifndef ROCKSDB_LITE
|
||
|
|
||
6 years ago
|
#include "logging/auto_roll_logger.h"
|
||
8 years ago
|
#include <errno.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include <algorithm>
|
||
12 years ago
|
#include <cmath>
|
||
11 years ago
|
#include <fstream>
|
||
8 years ago
|
#include <iostream>
|
||
11 years ago
|
#include <iterator>
|
||
8 years ago
|
#include <string>
|
||
|
#include <thread>
|
||
|
#include <vector>
|
||
6 years ago
|
#include "logging/logging.h"
|
||
9 years ago
|
#include "port/port.h"
|
||
8 years ago
|
#include "rocksdb/db.h"
|
||
6 years ago
|
#include "test_util/sync_point.h"
|
||
|
#include "test_util/testharness.h"
|
||
12 years ago
|
|
||
11 years ago
|
namespace rocksdb {
|
||
8 years ago
|
namespace {
|
||
|
class NoSleepEnv : public EnvWrapper {
|
||
|
public:
|
||
|
NoSleepEnv(Env* base) : EnvWrapper(base) {}
|
||
6 years ago
|
void SleepForMicroseconds(int micros) override {
|
||
8 years ago
|
fake_time_ += static_cast<uint64_t>(micros);
|
||
|
}
|
||
|
|
||
6 years ago
|
uint64_t NowNanos() override { return fake_time_ * 1000; }
|
||
8 years ago
|
|
||
6 years ago
|
uint64_t NowMicros() override { return fake_time_; }
|
||
8 years ago
|
|
||
|
private:
|
||
|
uint64_t fake_time_ = 6666666666;
|
||
|
};
|
||
|
} // namespace
|
||
12 years ago
|
|
||
6 years ago
|
// In this test we only want to Log some simple log message with
|
||
|
// no format. LogMessage() provides such a simple interface and
|
||
|
// avoids the [format-security] warning which occurs when you
|
||
|
// call ROCKS_LOG_INFO(logger, log_message) directly.
|
||
|
namespace {
|
||
|
void LogMessage(Logger* logger, const char* message) {
|
||
|
ROCKS_LOG_INFO(logger, "%s", message);
|
||
|
}
|
||
|
|
||
|
void LogMessage(const InfoLogLevel log_level, Logger* logger,
|
||
|
const char* message) {
|
||
|
Log(log_level, logger, "%s", message);
|
||
|
}
|
||
|
} // namespace
|
||
|
|
||
10 years ago
|
class AutoRollLoggerTest : public testing::Test {
|
||
12 years ago
|
public:
|
||
|
static void InitTestDb() {
|
||
10 years ago
|
#ifdef OS_WIN
|
||
|
// Replace all slashes in the path so windows CompSpec does not
|
||
|
// become confused
|
||
|
std::string testDir(kTestDir);
|
||
|
std::replace_if(testDir.begin(), testDir.end(),
|
||
10 years ago
|
[](char ch) { return ch == '/'; }, '\\');
|
||
10 years ago
|
std::string deleteCmd = "if exist " + testDir + " rd /s /q " + testDir;
|
||
|
#else
|
||
|
std::string deleteCmd = "rm -rf " + kTestDir;
|
||
|
#endif
|
||
12 years ago
|
ASSERT_TRUE(system(deleteCmd.c_str()) == 0);
|
||
12 years ago
|
Env::Default()->CreateDir(kTestDir);
|
||
|
}
|
||
|
|
||
9 years ago
|
void RollLogFileBySizeTest(AutoRollLogger* logger, size_t log_max_size,
|
||
|
const std::string& log_message);
|
||
8 years ago
|
void RollLogFileByTimeTest(Env*, AutoRollLogger* logger, size_t time,
|
||
|
const std::string& log_message);
|
||
6 years ago
|
// return list of files under kTestDir that contains "LOG"
|
||
|
std::vector<std::string> GetLogFiles() {
|
||
|
std::vector<std::string> ret;
|
||
|
std::vector<std::string> files;
|
||
|
Status s = default_env->GetChildren(kTestDir, &files);
|
||
|
// Should call ASSERT_OK() here but it doesn't compile. It's not
|
||
|
// worth the time figuring out why.
|
||
|
EXPECT_TRUE(s.ok());
|
||
|
for (const auto& f : files) {
|
||
|
if (f.find("LOG") != std::string::npos) {
|
||
|
ret.push_back(f);
|
||
|
}
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
// Delete all log files under kTestDir
|
||
|
void CleanupLogFiles() {
|
||
|
for (const std::string& f : GetLogFiles()) {
|
||
|
ASSERT_OK(default_env->DeleteFile(kTestDir + "/" + f));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void RollNTimesBySize(Logger* auto_roll_logger, size_t file_num,
|
||
|
size_t max_log_file_size) {
|
||
|
// Roll the log 4 times, and it will trim to 3 files.
|
||
|
std::string dummy_large_string;
|
||
|
dummy_large_string.assign(max_log_file_size, '=');
|
||
|
auto_roll_logger->SetInfoLogLevel(InfoLogLevel::INFO_LEVEL);
|
||
|
for (size_t i = 0; i < file_num + 1; i++) {
|
||
|
// Log enough bytes to trigger at least one roll.
|
||
|
LogMessage(auto_roll_logger, dummy_large_string.c_str());
|
||
|
LogMessage(auto_roll_logger, "");
|
||
|
}
|
||
|
}
|
||
12 years ago
|
|
||
9 years ago
|
static const std::string kSampleMessage;
|
||
|
static const std::string kTestDir;
|
||
|
static const std::string kLogFile;
|
||
8 years ago
|
static Env* default_env;
|
||
12 years ago
|
};
|
||
|
|
||
9 years ago
|
const std::string AutoRollLoggerTest::kSampleMessage(
|
||
12 years ago
|
"this is the message to be written to the log file!!");
|
||
7 years ago
|
const std::string AutoRollLoggerTest::kTestDir(
|
||
|
test::PerThreadDBPath("db_log_test"));
|
||
|
const std::string AutoRollLoggerTest::kLogFile(
|
||
|
test::PerThreadDBPath("db_log_test") + "/LOG");
|
||
8 years ago
|
Env* AutoRollLoggerTest::default_env = Env::Default();
|
||
12 years ago
|
|
||
|
void AutoRollLoggerTest::RollLogFileBySizeTest(AutoRollLogger* logger,
|
||
|
size_t log_max_size,
|
||
9 years ago
|
const std::string& log_message) {
|
||
11 years ago
|
logger->SetInfoLogLevel(InfoLogLevel::INFO_LEVEL);
|
||
12 years ago
|
// measure the size of each message, which is supposed
|
||
|
// to be equal or greater than log_message.size()
|
||
12 years ago
|
LogMessage(logger, log_message.c_str());
|
||
12 years ago
|
size_t message_size = logger->GetLogFileSize();
|
||
|
size_t current_log_size = message_size;
|
||
|
|
||
|
// Test the cases when the log file will not be rolled.
|
||
|
while (current_log_size + message_size < log_max_size) {
|
||
12 years ago
|
LogMessage(logger, log_message.c_str());
|
||
12 years ago
|
current_log_size += message_size;
|
||
|
ASSERT_EQ(current_log_size, logger->GetLogFileSize());
|
||
|
}
|
||
|
|
||
|
// Now the log file will be rolled
|
||
12 years ago
|
LogMessage(logger, log_message.c_str());
|
||
12 years ago
|
// Since rotation is checked before actual logging, we need to
|
||
|
// trigger the rotation by logging another message.
|
||
|
LogMessage(logger, log_message.c_str());
|
||
|
|
||
|
ASSERT_TRUE(message_size == logger->GetLogFileSize());
|
||
12 years ago
|
}
|
||
|
|
||
8 years ago
|
void AutoRollLoggerTest::RollLogFileByTimeTest(Env* env, AutoRollLogger* logger,
|
||
|
size_t time,
|
||
|
const std::string& log_message) {
|
||
|
uint64_t expected_ctime;
|
||
|
uint64_t actual_ctime;
|
||
|
|
||
12 years ago
|
uint64_t total_log_size;
|
||
10 years ago
|
EXPECT_OK(env->GetFileSize(kLogFile, &total_log_size));
|
||
8 years ago
|
expected_ctime = logger->TEST_ctime();
|
||
12 years ago
|
logger->SetCallNowMicrosEveryNRecords(0);
|
||
|
|
||
|
// -- Write to the log for several times, which is supposed
|
||
|
// to be finished before time.
|
||
|
for (int i = 0; i < 10; ++i) {
|
||
8 years ago
|
env->SleepForMicroseconds(50000);
|
||
|
LogMessage(logger, log_message.c_str());
|
||
|
EXPECT_OK(logger->GetStatus());
|
||
|
// Make sure we always write to the same log file (by
|
||
|
// checking the create time);
|
||
|
|
||
|
actual_ctime = logger->TEST_ctime();
|
||
|
|
||
|
// Also make sure the log size is increasing.
|
||
|
EXPECT_EQ(expected_ctime, actual_ctime);
|
||
|
EXPECT_GT(logger->GetLogFileSize(), total_log_size);
|
||
|
total_log_size = logger->GetLogFileSize();
|
||
12 years ago
|
}
|
||
|
|
||
|
// -- Make the log file expire
|
||
8 years ago
|
env->SleepForMicroseconds(static_cast<int>(time * 1000000));
|
||
12 years ago
|
LogMessage(logger, log_message.c_str());
|
||
12 years ago
|
|
||
|
// At this time, the new log file should be created.
|
||
8 years ago
|
actual_ctime = logger->TEST_ctime();
|
||
|
EXPECT_LT(expected_ctime, actual_ctime);
|
||
10 years ago
|
EXPECT_LT(logger->GetLogFileSize(), total_log_size);
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
TEST_F(AutoRollLoggerTest, RollLogFileBySize) {
|
||
12 years ago
|
InitTestDb();
|
||
12 years ago
|
size_t log_max_size = 1024 * 5;
|
||
6 years ago
|
size_t keep_log_file_num = 10;
|
||
12 years ago
|
|
||
6 years ago
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_max_size, 0,
|
||
|
keep_log_file_num);
|
||
12 years ago
|
|
||
12 years ago
|
RollLogFileBySizeTest(&logger, log_max_size,
|
||
12 years ago
|
kSampleMessage + ":RollLogFileBySize");
|
||
|
}
|
||
|
|
||
10 years ago
|
TEST_F(AutoRollLoggerTest, RollLogFileByTime) {
|
||
8 years ago
|
NoSleepEnv nse(Env::Default());
|
||
12 years ago
|
|
||
8 years ago
|
size_t time = 2;
|
||
|
size_t log_size = 1024 * 5;
|
||
6 years ago
|
size_t keep_log_file_num = 10;
|
||
8 years ago
|
|
||
|
InitTestDb();
|
||
|
// -- Test the existence of file during the server restart.
|
||
|
ASSERT_EQ(Status::NotFound(), default_env->FileExists(kLogFile));
|
||
6 years ago
|
AutoRollLogger logger(&nse, kTestDir, "", log_size, time, keep_log_file_num);
|
||
8 years ago
|
ASSERT_OK(default_env->FileExists(kLogFile));
|
||
12 years ago
|
|
||
8 years ago
|
RollLogFileByTimeTest(&nse, &logger, time,
|
||
|
kSampleMessage + ":RollLogFileByTime");
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
TEST_F(AutoRollLoggerTest, OpenLogFilesMultipleTimesWithOptionLog_max_size) {
|
||
12 years ago
|
// If only 'log_max_size' options is specified, then every time
|
||
11 years ago
|
// when rocksdb is restarted, a new empty log file will be created.
|
||
12 years ago
|
InitTestDb();
|
||
|
// WORKAROUND:
|
||
|
// avoid complier's complaint of "comparison between signed
|
||
|
// and unsigned integer expressions" because literal 0 is
|
||
|
// treated as "singed".
|
||
|
size_t kZero = 0;
|
||
|
size_t log_size = 1024;
|
||
6 years ago
|
size_t keep_log_file_num = 10;
|
||
12 years ago
|
|
||
6 years ago
|
AutoRollLogger* logger = new AutoRollLogger(Env::Default(), kTestDir, "",
|
||
|
log_size, 0, keep_log_file_num);
|
||
12 years ago
|
|
||
12 years ago
|
LogMessage(logger, kSampleMessage.c_str());
|
||
12 years ago
|
ASSERT_GT(logger->GetLogFileSize(), kZero);
|
||
|
delete logger;
|
||
|
|
||
|
// reopens the log file and an empty log file will be created.
|
||
6 years ago
|
logger = new AutoRollLogger(Env::Default(), kTestDir, "", log_size, 0, 10);
|
||
12 years ago
|
ASSERT_EQ(logger->GetLogFileSize(), kZero);
|
||
|
delete logger;
|
||
|
}
|
||
|
|
||
10 years ago
|
TEST_F(AutoRollLoggerTest, CompositeRollByTimeAndSizeLogger) {
|
||
11 years ago
|
size_t time = 2, log_max_size = 1024 * 5;
|
||
6 years ago
|
size_t keep_log_file_num = 10;
|
||
12 years ago
|
|
||
|
InitTestDb();
|
||
|
|
||
8 years ago
|
NoSleepEnv nse(Env::Default());
|
||
6 years ago
|
AutoRollLogger logger(&nse, kTestDir, "", log_max_size, time,
|
||
|
keep_log_file_num);
|
||
12 years ago
|
|
||
|
// Test the ability to roll by size
|
||
8 years ago
|
RollLogFileBySizeTest(&logger, log_max_size,
|
||
|
kSampleMessage + ":CompositeRollByTimeAndSizeLogger");
|
||
12 years ago
|
|
||
|
// Test the ability to roll by Time
|
||
8 years ago
|
RollLogFileByTimeTest(&nse, &logger, time,
|
||
|
kSampleMessage + ":CompositeRollByTimeAndSizeLogger");
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
#ifndef OS_WIN
|
||
10 years ago
|
// TODO: does not build for Windows because of PosixLogger use below. Need to
|
||
|
// port
|
||
10 years ago
|
TEST_F(AutoRollLoggerTest, CreateLoggerFromOptions) {
|
||
11 years ago
|
DBOptions options;
|
||
8 years ago
|
NoSleepEnv nse(Env::Default());
|
||
6 years ago
|
std::shared_ptr<Logger> logger;
|
||
12 years ago
|
|
||
|
// Normal logger
|
||
9 years ago
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, options, &logger));
|
||
12 years ago
|
ASSERT_TRUE(dynamic_cast<PosixLogger*>(logger.get()));
|
||
12 years ago
|
|
||
|
// Only roll by size
|
||
|
InitTestDb();
|
||
|
options.max_log_file_size = 1024;
|
||
9 years ago
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, options, &logger));
|
||
12 years ago
|
AutoRollLogger* auto_roll_logger =
|
||
|
dynamic_cast<AutoRollLogger*>(logger.get());
|
||
12 years ago
|
ASSERT_TRUE(auto_roll_logger);
|
||
12 years ago
|
RollLogFileBySizeTest(
|
||
|
auto_roll_logger, options.max_log_file_size,
|
||
|
kSampleMessage + ":CreateLoggerFromOptions - size");
|
||
|
|
||
|
// Only roll by Time
|
||
8 years ago
|
options.env = &nse;
|
||
12 years ago
|
InitTestDb();
|
||
|
options.max_log_file_size = 0;
|
||
11 years ago
|
options.log_file_time_to_roll = 2;
|
||
9 years ago
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, options, &logger));
|
||
12 years ago
|
auto_roll_logger =
|
||
|
dynamic_cast<AutoRollLogger*>(logger.get());
|
||
8 years ago
|
RollLogFileByTimeTest(&nse, auto_roll_logger, options.log_file_time_to_roll,
|
||
|
kSampleMessage + ":CreateLoggerFromOptions - time");
|
||
12 years ago
|
|
||
|
// roll by both Time and size
|
||
|
InitTestDb();
|
||
|
options.max_log_file_size = 1024 * 5;
|
||
11 years ago
|
options.log_file_time_to_roll = 2;
|
||
9 years ago
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, options, &logger));
|
||
12 years ago
|
auto_roll_logger =
|
||
|
dynamic_cast<AutoRollLogger*>(logger.get());
|
||
8 years ago
|
RollLogFileBySizeTest(auto_roll_logger, options.max_log_file_size,
|
||
|
kSampleMessage + ":CreateLoggerFromOptions - both");
|
||
|
RollLogFileByTimeTest(&nse, auto_roll_logger, options.log_file_time_to_roll,
|
||
|
kSampleMessage + ":CreateLoggerFromOptions - both");
|
||
6 years ago
|
|
||
|
// Set keep_log_file_num
|
||
|
{
|
||
|
const size_t kFileNum = 3;
|
||
|
InitTestDb();
|
||
|
options.max_log_file_size = 512;
|
||
|
options.log_file_time_to_roll = 2;
|
||
|
options.keep_log_file_num = kFileNum;
|
||
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, options, &logger));
|
||
|
auto_roll_logger = dynamic_cast<AutoRollLogger*>(logger.get());
|
||
|
|
||
|
// Roll the log 4 times, and it will trim to 3 files.
|
||
|
std::string dummy_large_string;
|
||
|
dummy_large_string.assign(options.max_log_file_size, '=');
|
||
|
auto_roll_logger->SetInfoLogLevel(InfoLogLevel::INFO_LEVEL);
|
||
|
for (size_t i = 0; i < kFileNum + 1; i++) {
|
||
|
// Log enough bytes to trigger at least one roll.
|
||
|
LogMessage(auto_roll_logger, dummy_large_string.c_str());
|
||
|
LogMessage(auto_roll_logger, "");
|
||
|
}
|
||
|
|
||
|
std::vector<std::string> files = GetLogFiles();
|
||
|
ASSERT_EQ(kFileNum, files.size());
|
||
|
|
||
|
CleanupLogFiles();
|
||
|
}
|
||
|
|
||
|
// Set keep_log_file_num and dbname is different from
|
||
|
// db_log_dir.
|
||
|
{
|
||
|
const size_t kFileNum = 3;
|
||
|
InitTestDb();
|
||
|
options.max_log_file_size = 512;
|
||
|
options.log_file_time_to_roll = 2;
|
||
|
options.keep_log_file_num = kFileNum;
|
||
|
options.db_log_dir = kTestDir;
|
||
|
ASSERT_OK(CreateLoggerFromOptions("/dummy/db/name", options, &logger));
|
||
|
auto_roll_logger = dynamic_cast<AutoRollLogger*>(logger.get());
|
||
|
|
||
|
// Roll the log 4 times, and it will trim to 3 files.
|
||
|
std::string dummy_large_string;
|
||
|
dummy_large_string.assign(options.max_log_file_size, '=');
|
||
|
auto_roll_logger->SetInfoLogLevel(InfoLogLevel::INFO_LEVEL);
|
||
|
for (size_t i = 0; i < kFileNum + 1; i++) {
|
||
|
// Log enough bytes to trigger at least one roll.
|
||
|
LogMessage(auto_roll_logger, dummy_large_string.c_str());
|
||
|
LogMessage(auto_roll_logger, "");
|
||
|
}
|
||
|
|
||
|
std::vector<std::string> files = GetLogFiles();
|
||
|
ASSERT_EQ(kFileNum, files.size());
|
||
|
for (const auto& f : files) {
|
||
|
ASSERT_TRUE(f.find("dummy") != std::string::npos);
|
||
|
}
|
||
|
|
||
|
// Cleaning up those files.
|
||
|
CleanupLogFiles();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
TEST_F(AutoRollLoggerTest, AutoDeleting) {
|
||
|
for (int attempt = 0; attempt < 2; attempt++) {
|
||
|
// In the first attemp, db_log_dir is not set, while in the
|
||
|
// second it is set.
|
||
|
std::string dbname = (attempt == 0) ? kTestDir : "/test/dummy/dir";
|
||
|
std::string db_log_dir = (attempt == 0) ? "" : kTestDir;
|
||
|
|
||
|
InitTestDb();
|
||
|
const size_t kMaxFileSize = 512;
|
||
|
{
|
||
|
size_t log_num = 8;
|
||
|
AutoRollLogger logger(Env::Default(), dbname, db_log_dir, kMaxFileSize, 0,
|
||
|
log_num);
|
||
|
RollNTimesBySize(&logger, log_num, kMaxFileSize);
|
||
|
|
||
|
ASSERT_EQ(log_num, GetLogFiles().size());
|
||
|
}
|
||
|
// Shrink number of files
|
||
|
{
|
||
|
size_t log_num = 5;
|
||
|
AutoRollLogger logger(Env::Default(), dbname, db_log_dir, kMaxFileSize, 0,
|
||
|
log_num);
|
||
|
ASSERT_EQ(log_num, GetLogFiles().size());
|
||
|
|
||
|
RollNTimesBySize(&logger, 3, kMaxFileSize);
|
||
|
ASSERT_EQ(log_num, GetLogFiles().size());
|
||
|
}
|
||
|
|
||
|
// Increase number of files again.
|
||
|
{
|
||
|
size_t log_num = 7;
|
||
|
AutoRollLogger logger(Env::Default(), dbname, db_log_dir, kMaxFileSize, 0,
|
||
|
log_num);
|
||
|
ASSERT_EQ(6, GetLogFiles().size());
|
||
|
|
||
|
RollNTimesBySize(&logger, 3, kMaxFileSize);
|
||
|
ASSERT_EQ(log_num, GetLogFiles().size());
|
||
|
}
|
||
|
|
||
|
CleanupLogFiles();
|
||
|
}
|
||
12 years ago
|
}
|
||
9 years ago
|
|
||
|
TEST_F(AutoRollLoggerTest, LogFlushWhileRolling) {
|
||
|
DBOptions options;
|
||
6 years ago
|
std::shared_ptr<Logger> logger;
|
||
9 years ago
|
|
||
|
InitTestDb();
|
||
|
options.max_log_file_size = 1024 * 5;
|
||
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, options, &logger));
|
||
|
AutoRollLogger* auto_roll_logger =
|
||
|
dynamic_cast<AutoRollLogger*>(logger.get());
|
||
|
ASSERT_TRUE(auto_roll_logger);
|
||
8 years ago
|
rocksdb::port::Thread flush_thread;
|
||
9 years ago
|
|
||
9 years ago
|
// Notes:
|
||
|
// (1) Need to pin the old logger before beginning the roll, as rolling grabs
|
||
|
// the mutex, which would prevent us from accessing the old logger. This
|
||
|
// also marks flush_thread with AutoRollLogger::Flush:PinnedLogger.
|
||
|
// (2) Need to reset logger during PosixLogger::Flush() to exercise a race
|
||
|
// condition case, which is executing the flush with the pinned (old)
|
||
|
// logger after auto-roll logger has cut over to a new logger.
|
||
|
// (3) PosixLogger::Flush() happens in both threads but its SyncPoints only
|
||
|
// are enabled in flush_thread (the one pinning the old logger).
|
||
|
rocksdb::SyncPoint::GetInstance()->LoadDependencyAndMarkers(
|
||
|
{{"AutoRollLogger::Flush:PinnedLogger",
|
||
|
"AutoRollLoggerTest::LogFlushWhileRolling:PreRollAndPostThreadInit"},
|
||
|
{"PosixLogger::Flush:Begin1",
|
||
|
"AutoRollLogger::ResetLogger:BeforeNewLogger"},
|
||
|
{"AutoRollLogger::ResetLogger:AfterNewLogger",
|
||
|
"PosixLogger::Flush:Begin2"}},
|
||
|
{{"AutoRollLogger::Flush:PinnedLogger", "PosixLogger::Flush:Begin1"},
|
||
|
{"AutoRollLogger::Flush:PinnedLogger", "PosixLogger::Flush:Begin2"}});
|
||
9 years ago
|
rocksdb::SyncPoint::GetInstance()->EnableProcessing();
|
||
9 years ago
|
|
||
8 years ago
|
flush_thread = port::Thread ([&]() { auto_roll_logger->Flush(); });
|
||
9 years ago
|
TEST_SYNC_POINT(
|
||
|
"AutoRollLoggerTest::LogFlushWhileRolling:PreRollAndPostThreadInit");
|
||
9 years ago
|
RollLogFileBySizeTest(auto_roll_logger, options.max_log_file_size,
|
||
|
kSampleMessage + ":LogFlushWhileRolling");
|
||
|
flush_thread.join();
|
||
|
rocksdb::SyncPoint::GetInstance()->DisableProcessing();
|
||
|
}
|
||
|
|
||
|
#endif // OS_WIN
|
||
12 years ago
|
|
||
10 years ago
|
TEST_F(AutoRollLoggerTest, InfoLogLevel) {
|
||
11 years ago
|
InitTestDb();
|
||
|
|
||
|
size_t log_size = 8192;
|
||
11 years ago
|
size_t log_lines = 0;
|
||
11 years ago
|
// an extra-scope to force the AutoRollLogger to flush the log file when it
|
||
|
// becomes out of scope.
|
||
|
{
|
||
6 years ago
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_size, 0, 10);
|
||
10 years ago
|
for (int log_level = InfoLogLevel::HEADER_LEVEL;
|
||
11 years ago
|
log_level >= InfoLogLevel::DEBUG_LEVEL; log_level--) {
|
||
11 years ago
|
logger.SetInfoLogLevel((InfoLogLevel)log_level);
|
||
11 years ago
|
for (int log_type = InfoLogLevel::DEBUG_LEVEL;
|
||
10 years ago
|
log_type <= InfoLogLevel::HEADER_LEVEL; log_type++) {
|
||
11 years ago
|
// log messages with log level smaller than log_level will not be
|
||
|
// logged.
|
||
|
LogMessage((InfoLogLevel)log_type, &logger, kSampleMessage.c_str());
|
||
|
}
|
||
10 years ago
|
log_lines += InfoLogLevel::HEADER_LEVEL - log_level + 1;
|
||
11 years ago
|
}
|
||
10 years ago
|
for (int log_level = InfoLogLevel::HEADER_LEVEL;
|
||
11 years ago
|
log_level >= InfoLogLevel::DEBUG_LEVEL; log_level--) {
|
||
11 years ago
|
logger.SetInfoLogLevel((InfoLogLevel)log_level);
|
||
|
|
||
|
// again, messages with level smaller than log_level will not be logged.
|
||
8 years ago
|
ROCKS_LOG_HEADER(&logger, "%s", kSampleMessage.c_str());
|
||
|
ROCKS_LOG_DEBUG(&logger, "%s", kSampleMessage.c_str());
|
||
|
ROCKS_LOG_INFO(&logger, "%s", kSampleMessage.c_str());
|
||
|
ROCKS_LOG_WARN(&logger, "%s", kSampleMessage.c_str());
|
||
|
ROCKS_LOG_ERROR(&logger, "%s", kSampleMessage.c_str());
|
||
|
ROCKS_LOG_FATAL(&logger, "%s", kSampleMessage.c_str());
|
||
10 years ago
|
log_lines += InfoLogLevel::HEADER_LEVEL - log_level + 1;
|
||
11 years ago
|
}
|
||
|
}
|
||
7 years ago
|
std::ifstream inFile(AutoRollLoggerTest::kLogFile.c_str());
|
||
|
size_t lines = std::count(std::istreambuf_iterator<char>(inFile),
|
||
|
std::istreambuf_iterator<char>(), '\n');
|
||
|
ASSERT_EQ(log_lines, lines);
|
||
|
inFile.close();
|
||
|
}
|
||
|
|
||
|
TEST_F(AutoRollLoggerTest, Close) {
|
||
|
InitTestDb();
|
||
|
|
||
|
size_t log_size = 8192;
|
||
|
size_t log_lines = 0;
|
||
6 years ago
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_size, 0, 10);
|
||
7 years ago
|
for (int log_level = InfoLogLevel::HEADER_LEVEL;
|
||
|
log_level >= InfoLogLevel::DEBUG_LEVEL; log_level--) {
|
||
|
logger.SetInfoLogLevel((InfoLogLevel)log_level);
|
||
|
for (int log_type = InfoLogLevel::DEBUG_LEVEL;
|
||
|
log_type <= InfoLogLevel::HEADER_LEVEL; log_type++) {
|
||
|
// log messages with log level smaller than log_level will not be
|
||
|
// logged.
|
||
|
LogMessage((InfoLogLevel)log_type, &logger, kSampleMessage.c_str());
|
||
|
}
|
||
|
log_lines += InfoLogLevel::HEADER_LEVEL - log_level + 1;
|
||
|
}
|
||
|
for (int log_level = InfoLogLevel::HEADER_LEVEL;
|
||
|
log_level >= InfoLogLevel::DEBUG_LEVEL; log_level--) {
|
||
|
logger.SetInfoLogLevel((InfoLogLevel)log_level);
|
||
|
|
||
|
// again, messages with level smaller than log_level will not be logged.
|
||
|
ROCKS_LOG_HEADER(&logger, "%s", kSampleMessage.c_str());
|
||
|
ROCKS_LOG_DEBUG(&logger, "%s", kSampleMessage.c_str());
|
||
|
ROCKS_LOG_INFO(&logger, "%s", kSampleMessage.c_str());
|
||
|
ROCKS_LOG_WARN(&logger, "%s", kSampleMessage.c_str());
|
||
|
ROCKS_LOG_ERROR(&logger, "%s", kSampleMessage.c_str());
|
||
|
ROCKS_LOG_FATAL(&logger, "%s", kSampleMessage.c_str());
|
||
|
log_lines += InfoLogLevel::HEADER_LEVEL - log_level + 1;
|
||
|
}
|
||
|
ASSERT_EQ(logger.Close(), Status::OK());
|
||
|
|
||
11 years ago
|
std::ifstream inFile(AutoRollLoggerTest::kLogFile.c_str());
|
||
11 years ago
|
size_t lines = std::count(std::istreambuf_iterator<char>(inFile),
|
||
11 years ago
|
std::istreambuf_iterator<char>(), '\n');
|
||
|
ASSERT_EQ(log_lines, lines);
|
||
|
inFile.close();
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
// Test the logger Header function for roll over logs
|
||
|
// We expect the new logs creates as roll over to carry the headers specified
|
||
9 years ago
|
static std::vector<std::string> GetOldFileNames(const std::string& path) {
|
||
|
std::vector<std::string> ret;
|
||
10 years ago
|
|
||
9 years ago
|
const std::string dirname = path.substr(/*start=*/0, path.find_last_of("/"));
|
||
|
const std::string fname = path.substr(path.find_last_of("/") + 1);
|
||
10 years ago
|
|
||
9 years ago
|
std::vector<std::string> children;
|
||
10 years ago
|
Env::Default()->GetChildren(dirname, &children);
|
||
|
|
||
|
// We know that the old log files are named [path]<something>
|
||
|
// Return all entities that match the pattern
|
||
10 years ago
|
for (auto& child : children) {
|
||
10 years ago
|
if (fname != child && child.find(fname) == 0) {
|
||
|
ret.push_back(dirname + "/" + child);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
// Return the number of lines where a given pattern was found in the file
|
||
9 years ago
|
static size_t GetLinesCount(const std::string& fname,
|
||
|
const std::string& pattern) {
|
||
|
std::stringstream ssbuf;
|
||
|
std::string line;
|
||
10 years ago
|
size_t count = 0;
|
||
|
|
||
9 years ago
|
std::ifstream inFile(fname.c_str());
|
||
10 years ago
|
ssbuf << inFile.rdbuf();
|
||
|
|
||
|
while (getline(ssbuf, line)) {
|
||
|
if (line.find(pattern) != std::string::npos) {
|
||
|
count++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return count;
|
||
|
}
|
||
|
|
||
10 years ago
|
TEST_F(AutoRollLoggerTest, LogHeaderTest) {
|
||
10 years ago
|
static const size_t MAX_HEADERS = 10;
|
||
|
static const size_t LOG_MAX_SIZE = 1024 * 5;
|
||
|
static const std::string HEADER_STR = "Log header line";
|
||
|
|
||
10 years ago
|
// test_num == 0 -> standard call to Header()
|
||
|
// test_num == 1 -> call to Log() with InfoLogLevel::HEADER_LEVEL
|
||
|
for (int test_num = 0; test_num < 2; test_num++) {
|
||
10 years ago
|
|
||
10 years ago
|
InitTestDb();
|
||
10 years ago
|
|
||
6 years ago
|
AutoRollLogger logger(Env::Default(), kTestDir, /*db_log_dir=*/"",
|
||
|
LOG_MAX_SIZE, /*log_file_time_to_roll=*/0,
|
||
|
/*keep_log_file_num=*/10);
|
||
10 years ago
|
|
||
10 years ago
|
if (test_num == 0) {
|
||
|
// Log some headers explicitly using Header()
|
||
|
for (size_t i = 0; i < MAX_HEADERS; i++) {
|
||
6 years ago
|
Header(&logger, "%s %" ROCKSDB_PRIszt, HEADER_STR.c_str(), i);
|
||
10 years ago
|
}
|
||
|
} else if (test_num == 1) {
|
||
|
// HEADER_LEVEL should make this behave like calling Header()
|
||
|
for (size_t i = 0; i < MAX_HEADERS; i++) {
|
||
6 years ago
|
ROCKS_LOG_HEADER(&logger, "%s %" ROCKSDB_PRIszt, HEADER_STR.c_str(), i);
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
const std::string newfname = logger.TEST_log_fname();
|
||
10 years ago
|
|
||
|
// Log enough data to cause a roll over
|
||
|
int i = 0;
|
||
|
for (size_t iter = 0; iter < 2; iter++) {
|
||
|
while (logger.GetLogFileSize() < LOG_MAX_SIZE) {
|
||
|
Info(&logger, (kSampleMessage + ":LogHeaderTest line %d").c_str(), i);
|
||
|
++i;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
Info(&logger, "Rollover");
|
||
|
}
|
||
|
|
||
|
// Flush the log for the latest file
|
||
|
LogFlush(&logger);
|
||
10 years ago
|
|
||
10 years ago
|
const auto oldfiles = GetOldFileNames(newfname);
|
||
10 years ago
|
|
||
10 years ago
|
ASSERT_EQ(oldfiles.size(), (size_t) 2);
|
||
10 years ago
|
|
||
10 years ago
|
for (auto& oldfname : oldfiles) {
|
||
10 years ago
|
// verify that the files rolled over
|
||
|
ASSERT_NE(oldfname, newfname);
|
||
|
// verify that the old log contains all the header logs
|
||
|
ASSERT_EQ(GetLinesCount(oldfname, HEADER_STR), MAX_HEADERS);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
TEST_F(AutoRollLoggerTest, LogFileExistence) {
|
||
11 years ago
|
rocksdb::DB* db;
|
||
|
rocksdb::Options options;
|
||
9 years ago
|
#ifdef OS_WIN
|
||
|
// Replace all slashes in the path so windows CompSpec does not
|
||
|
// become confused
|
||
|
std::string testDir(kTestDir);
|
||
|
std::replace_if(testDir.begin(), testDir.end(),
|
||
|
[](char ch) { return ch == '/'; }, '\\');
|
||
|
std::string deleteCmd = "if exist " + testDir + " rd /s /q " + testDir;
|
||
|
#else
|
||
9 years ago
|
std::string deleteCmd = "rm -rf " + kTestDir;
|
||
9 years ago
|
#endif
|
||
11 years ago
|
ASSERT_EQ(system(deleteCmd.c_str()), 0);
|
||
|
options.max_log_file_size = 100 * 1024 * 1024;
|
||
|
options.create_if_missing = true;
|
||
|
ASSERT_OK(rocksdb::DB::Open(options, kTestDir, &db));
|
||
8 years ago
|
ASSERT_OK(default_env->FileExists(kLogFile));
|
||
11 years ago
|
delete db;
|
||
|
}
|
||
|
|
||
11 years ago
|
} // namespace rocksdb
|
||
12 years ago
|
|
||
|
int main(int argc, char** argv) {
|
||
10 years ago
|
::testing::InitGoogleTest(&argc, argv);
|
||
|
return RUN_ALL_TESTS();
|
||
12 years ago
|
}
|
||
8 years ago
|
|
||
|
#else
|
||
|
#include <stdio.h>
|
||
|
|
||
7 years ago
|
int main(int /*argc*/, char** /*argv*/) {
|
||
8 years ago
|
fprintf(stderr,
|
||
|
"SKIPPED as AutoRollLogger is not supported in ROCKSDB_LITE\n");
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
#endif // !ROCKSDB_LITE
|