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.
420 lines
14 KiB
420 lines
14 KiB
11 years ago
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
||
|
// This source code is licensed under the BSD-style license found in the
|
||
|
// LICENSE file in the root directory of this source tree. An additional grant
|
||
|
// of patent rights can be found in the PATENTS file in the same directory.
|
||
|
//
|
||
12 years ago
|
#include <string>
|
||
10 years ago
|
#include <vector>
|
||
12 years ago
|
#include <cmath>
|
||
11 years ago
|
#include <iostream>
|
||
|
#include <fstream>
|
||
|
#include <iterator>
|
||
|
#include <algorithm>
|
||
9 years ago
|
#include "db/auto_roll_logger.h"
|
||
12 years ago
|
#include "util/testharness.h"
|
||
11 years ago
|
#include "rocksdb/db.h"
|
||
12 years ago
|
#include <sys/stat.h>
|
||
|
#include <errno.h>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
11 years ago
|
namespace rocksdb {
|
||
12 years ago
|
|
||
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);
|
||
|
}
|
||
|
|
||
|
void RollLogFileBySizeTest(AutoRollLogger* logger,
|
||
|
size_t log_max_size,
|
||
|
const string& log_message);
|
||
|
uint64_t RollLogFileByTimeTest(AutoRollLogger* logger,
|
||
|
size_t time,
|
||
|
const string& log_message);
|
||
|
|
||
|
static const string kSampleMessage;
|
||
|
static const string kTestDir;
|
||
|
static const string kLogFile;
|
||
|
static Env* env;
|
||
|
};
|
||
|
|
||
|
const string AutoRollLoggerTest::kSampleMessage(
|
||
|
"this is the message to be written to the log file!!");
|
||
11 years ago
|
const string AutoRollLoggerTest::kTestDir(test::TmpDir() + "/db_log_test");
|
||
|
const string AutoRollLoggerTest::kLogFile(test::TmpDir() + "/db_log_test/LOG");
|
||
12 years ago
|
Env* AutoRollLoggerTest::env = Env::Default();
|
||
|
|
||
12 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 Log(logger, log_message) directly.
|
||
11 years ago
|
namespace {
|
||
12 years ago
|
void LogMessage(Logger* logger, const char* message) {
|
||
|
Log(logger, "%s", message);
|
||
|
}
|
||
|
|
||
11 years ago
|
void LogMessage(const InfoLogLevel log_level, Logger* logger,
|
||
|
const char* message) {
|
||
|
Log(log_level, logger, "%s", message);
|
||
|
}
|
||
11 years ago
|
} // namespace
|
||
11 years ago
|
|
||
11 years ago
|
namespace {
|
||
12 years ago
|
void GetFileCreateTime(const std::string& fname, uint64_t* file_ctime) {
|
||
|
struct stat s;
|
||
|
if (stat(fname.c_str(), &s) != 0) {
|
||
|
*file_ctime = (uint64_t)0;
|
||
|
}
|
||
|
*file_ctime = static_cast<uint64_t>(s.st_ctime);
|
||
|
}
|
||
11 years ago
|
} // namespace
|
||
12 years ago
|
|
||
|
void AutoRollLoggerTest::RollLogFileBySizeTest(AutoRollLogger* logger,
|
||
|
size_t log_max_size,
|
||
|
const 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
|
}
|
||
|
|
||
|
uint64_t AutoRollLoggerTest::RollLogFileByTimeTest(
|
||
|
AutoRollLogger* logger, size_t time, const string& log_message) {
|
||
|
uint64_t expected_create_time;
|
||
|
uint64_t actual_create_time;
|
||
|
uint64_t total_log_size;
|
||
10 years ago
|
EXPECT_OK(env->GetFileSize(kLogFile, &total_log_size));
|
||
12 years ago
|
GetFileCreateTime(kLogFile, &expected_create_time);
|
||
|
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) {
|
||
12 years ago
|
LogMessage(logger, log_message.c_str());
|
||
10 years ago
|
EXPECT_OK(logger->GetStatus());
|
||
12 years ago
|
// Make sure we always write to the same log file (by
|
||
|
// checking the create time);
|
||
|
GetFileCreateTime(kLogFile, &actual_create_time);
|
||
|
|
||
|
// Also make sure the log size is increasing.
|
||
10 years ago
|
EXPECT_EQ(expected_create_time, actual_create_time);
|
||
|
EXPECT_GT(logger->GetLogFileSize(), total_log_size);
|
||
12 years ago
|
total_log_size = logger->GetLogFileSize();
|
||
|
}
|
||
|
|
||
|
// -- Make the log file expire
|
||
10 years ago
|
#ifdef OS_WIN
|
||
|
Sleep(static_cast<unsigned int>(time) * 1000);
|
||
|
#else
|
||
10 years ago
|
sleep(static_cast<unsigned int>(time));
|
||
10 years ago
|
#endif
|
||
12 years ago
|
LogMessage(logger, log_message.c_str());
|
||
12 years ago
|
|
||
|
// At this time, the new log file should be created.
|
||
|
GetFileCreateTime(kLogFile, &actual_create_time);
|
||
10 years ago
|
EXPECT_GT(actual_create_time, expected_create_time);
|
||
|
EXPECT_LT(logger->GetLogFileSize(), total_log_size);
|
||
12 years ago
|
expected_create_time = actual_create_time;
|
||
|
|
||
|
return expected_create_time;
|
||
|
}
|
||
|
|
||
10 years ago
|
TEST_F(AutoRollLoggerTest, RollLogFileBySize) {
|
||
12 years ago
|
InitTestDb();
|
||
12 years ago
|
size_t log_max_size = 1024 * 5;
|
||
12 years ago
|
|
||
12 years ago
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_max_size, 0);
|
||
12 years ago
|
|
||
12 years ago
|
RollLogFileBySizeTest(&logger, log_max_size,
|
||
12 years ago
|
kSampleMessage + ":RollLogFileBySize");
|
||
|
}
|
||
|
|
||
10 years ago
|
TEST_F(AutoRollLoggerTest, RollLogFileByTime) {
|
||
10 years ago
|
size_t time = 2;
|
||
12 years ago
|
size_t log_size = 1024 * 5;
|
||
|
|
||
|
InitTestDb();
|
||
|
// -- Test the existence of file during the server restart.
|
||
10 years ago
|
ASSERT_EQ(Status::NotFound(), env->FileExists(kLogFile));
|
||
10 years ago
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_size, time);
|
||
10 years ago
|
ASSERT_OK(env->FileExists(kLogFile));
|
||
12 years ago
|
|
||
12 years ago
|
RollLogFileByTimeTest(&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;
|
||
|
|
||
|
AutoRollLogger* logger = new AutoRollLogger(
|
||
|
Env::Default(), kTestDir, "", log_size, 0);
|
||
|
|
||
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.
|
||
|
logger = new AutoRollLogger(
|
||
|
Env::Default(), kTestDir, "", log_size, 0);
|
||
|
ASSERT_EQ(logger->GetLogFileSize(), kZero);
|
||
|
delete logger;
|
||
|
}
|
||
|
|
||
10 years ago
|
TEST_F(AutoRollLoggerTest, CompositeRollByTimeAndSizeLogger) {
|
||
10 years ago
|
size_t time = 2, log_max_size = 1024 * 5;
|
||
12 years ago
|
|
||
|
InitTestDb();
|
||
|
|
||
12 years ago
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_max_size, time);
|
||
12 years ago
|
|
||
|
// Test the ability to roll by size
|
||
|
RollLogFileBySizeTest(
|
||
12 years ago
|
&logger, log_max_size,
|
||
12 years ago
|
kSampleMessage + ":CompositeRollByTimeAndSizeLogger");
|
||
|
|
||
|
// Test the ability to roll by Time
|
||
12 years ago
|
RollLogFileByTimeTest( &logger, time,
|
||
12 years ago
|
kSampleMessage + ":CompositeRollByTimeAndSizeLogger");
|
||
|
}
|
||
|
|
||
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;
|
||
12 years ago
|
shared_ptr<Logger> logger;
|
||
|
|
||
|
// 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
|
||
|
InitTestDb();
|
||
|
options.max_log_file_size = 0;
|
||
10 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());
|
||
|
RollLogFileByTimeTest(
|
||
|
auto_roll_logger, options.log_file_time_to_roll,
|
||
|
kSampleMessage + ":CreateLoggerFromOptions - time");
|
||
|
|
||
|
// roll by both Time and size
|
||
|
InitTestDb();
|
||
|
options.max_log_file_size = 1024 * 5;
|
||
10 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());
|
||
|
RollLogFileBySizeTest(
|
||
|
auto_roll_logger, options.max_log_file_size,
|
||
|
kSampleMessage + ":CreateLoggerFromOptions - both");
|
||
|
RollLogFileByTimeTest(
|
||
|
auto_roll_logger, options.log_file_time_to_roll,
|
||
|
kSampleMessage + ":CreateLoggerFromOptions - both");
|
||
|
}
|
||
10 years ago
|
#endif
|
||
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.
|
||
|
{
|
||
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_size, 0);
|
||
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.
|
||
10 years ago
|
Log(InfoLogLevel::HEADER_LEVEL, &logger, "%s", kSampleMessage.c_str());
|
||
11 years ago
|
Debug(&logger, "%s", kSampleMessage.c_str());
|
||
|
Info(&logger, "%s", kSampleMessage.c_str());
|
||
|
Warn(&logger, "%s", kSampleMessage.c_str());
|
||
|
Error(&logger, "%s", kSampleMessage.c_str());
|
||
|
Fatal(&logger, "%s", kSampleMessage.c_str());
|
||
10 years ago
|
log_lines += InfoLogLevel::HEADER_LEVEL - log_level + 1;
|
||
11 years ago
|
}
|
||
|
}
|
||
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
|
||
10 years ago
|
static std::vector<string> GetOldFileNames(const string& path) {
|
||
|
std::vector<string> ret;
|
||
|
|
||
10 years ago
|
const string dirname = path.substr(/*start=*/ 0, path.find_last_of("/"));
|
||
|
const string fname = path.substr(path.find_last_of("/") + 1);
|
||
|
|
||
10 years ago
|
std::vector<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
|
||
|
static size_t GetLinesCount(const string& fname, const string& pattern) {
|
||
|
stringstream ssbuf;
|
||
|
string line;
|
||
|
size_t count = 0;
|
||
|
|
||
|
ifstream inFile(fname.c_str());
|
||
|
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
|
|
||
10 years ago
|
AutoRollLogger logger(Env::Default(), kTestDir, /*db_log_dir=*/ "",
|
||
|
LOG_MAX_SIZE, /*log_file_time_to_roll=*/ 0);
|
||
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++) {
|
||
|
Header(&logger, "%s %d", HEADER_STR.c_str(), i);
|
||
|
}
|
||
|
} else if (test_num == 1) {
|
||
|
// HEADER_LEVEL should make this behave like calling Header()
|
||
|
for (size_t i = 0; i < MAX_HEADERS; i++) {
|
||
|
Log(InfoLogLevel::HEADER_LEVEL, &logger, "%s %d",
|
||
|
HEADER_STR.c_str(), i);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
const 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;
|
||
|
string deleteCmd = "rm -rf " + kTestDir;
|
||
|
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));
|
||
10 years ago
|
ASSERT_OK(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
|
}
|