2013-10-16 23:59:46 +02:00
|
|
|
// 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.
|
|
|
|
//
|
2013-02-05 04:42:40 +01:00
|
|
|
#include <string>
|
|
|
|
#include <cmath>
|
2014-02-27 23:00:10 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <algorithm>
|
2013-02-05 04:42:40 +01:00
|
|
|
#include "util/testharness.h"
|
|
|
|
#include "util/auto_roll_logger.h"
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/db.h"
|
2013-02-05 04:42:40 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
class AutoRollLoggerTest {
|
|
|
|
public:
|
|
|
|
static void InitTestDb() {
|
|
|
|
string deleteCmd = "rm -rf " + kTestDir;
|
2013-02-13 23:21:24 +01:00
|
|
|
ASSERT_TRUE(system(deleteCmd.c_str()) == 0);
|
2013-02-05 04:42:40 +01:00
|
|
|
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!!");
|
2014-02-26 23:41:28 +01:00
|
|
|
const string AutoRollLoggerTest::kTestDir(test::TmpDir() + "/db_log_test");
|
|
|
|
const string AutoRollLoggerTest::kLogFile(test::TmpDir() + "/db_log_test/LOG");
|
2013-02-05 04:42:40 +01:00
|
|
|
Env* AutoRollLoggerTest::env = Env::Default();
|
|
|
|
|
2013-02-13 23:21:24 +01:00
|
|
|
// 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.
|
2014-04-10 06:17:14 +02:00
|
|
|
namespace {
|
2013-02-13 23:21:24 +01:00
|
|
|
void LogMessage(Logger* logger, const char* message) {
|
|
|
|
Log(logger, "%s", message);
|
|
|
|
}
|
|
|
|
|
2014-02-26 23:41:28 +01:00
|
|
|
void LogMessage(const InfoLogLevel log_level, Logger* logger,
|
|
|
|
const char* message) {
|
|
|
|
Log(log_level, logger, "%s", message);
|
|
|
|
}
|
2014-04-10 06:17:14 +02:00
|
|
|
} // namespace
|
2014-02-26 23:41:28 +01:00
|
|
|
|
2014-04-10 06:17:14 +02:00
|
|
|
namespace {
|
2013-02-05 04:42:40 +01:00
|
|
|
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);
|
|
|
|
}
|
2014-04-10 06:17:14 +02:00
|
|
|
} // namespace
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
void AutoRollLoggerTest::RollLogFileBySizeTest(AutoRollLogger* logger,
|
|
|
|
size_t log_max_size,
|
|
|
|
const string& log_message) {
|
2014-04-11 00:27:42 +02:00
|
|
|
logger->SetInfoLogLevel(InfoLogLevel::INFO_LEVEL);
|
2013-02-05 04:42:40 +01:00
|
|
|
// measure the size of each message, which is supposed
|
|
|
|
// to be equal or greater than log_message.size()
|
2013-02-13 23:21:24 +01:00
|
|
|
LogMessage(logger, log_message.c_str());
|
2013-02-05 04:42:40 +01:00
|
|
|
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) {
|
2013-02-13 23:21:24 +01:00
|
|
|
LogMessage(logger, log_message.c_str());
|
2013-02-05 04:42:40 +01:00
|
|
|
current_log_size += message_size;
|
|
|
|
ASSERT_EQ(current_log_size, logger->GetLogFileSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now the log file will be rolled
|
2013-02-13 23:21:24 +01:00
|
|
|
LogMessage(logger, log_message.c_str());
|
2013-03-25 18:56:48 +01:00
|
|
|
// 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());
|
2013-02-05 04:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
ASSERT_OK(env->GetFileSize(kLogFile, &total_log_size));
|
|
|
|
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) {
|
2013-02-13 23:21:24 +01:00
|
|
|
LogMessage(logger, log_message.c_str());
|
2013-02-05 04:42:40 +01:00
|
|
|
ASSERT_OK(logger->GetStatus());
|
|
|
|
// 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.
|
|
|
|
ASSERT_EQ(expected_create_time, actual_create_time);
|
|
|
|
ASSERT_GT(logger->GetLogFileSize(), total_log_size);
|
|
|
|
total_log_size = logger->GetLogFileSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Make the log file expire
|
2014-11-11 22:47:22 +01:00
|
|
|
sleep(static_cast<unsigned int>(time));
|
2013-02-13 23:21:24 +01:00
|
|
|
LogMessage(logger, log_message.c_str());
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
// At this time, the new log file should be created.
|
|
|
|
GetFileCreateTime(kLogFile, &actual_create_time);
|
|
|
|
ASSERT_GT(actual_create_time, expected_create_time);
|
|
|
|
ASSERT_LT(logger->GetLogFileSize(), total_log_size);
|
|
|
|
expected_create_time = actual_create_time;
|
|
|
|
|
|
|
|
return expected_create_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AutoRollLoggerTest, RollLogFileBySize) {
|
2013-02-19 09:09:16 +01:00
|
|
|
InitTestDb();
|
2013-02-13 23:21:24 +01:00
|
|
|
size_t log_max_size = 1024 * 5;
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2013-03-08 21:29:19 +01:00
|
|
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_max_size, 0);
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2013-03-08 21:29:19 +01:00
|
|
|
RollLogFileBySizeTest(&logger, log_max_size,
|
2013-02-05 04:42:40 +01:00
|
|
|
kSampleMessage + ":RollLogFileBySize");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AutoRollLoggerTest, RollLogFileByTime) {
|
2014-08-18 19:23:18 +02:00
|
|
|
size_t time = 2;
|
2013-02-05 04:42:40 +01:00
|
|
|
size_t log_size = 1024 * 5;
|
|
|
|
|
|
|
|
InitTestDb();
|
|
|
|
// -- Test the existence of file during the server restart.
|
|
|
|
ASSERT_TRUE(!env->FileExists(kLogFile));
|
2014-08-18 19:23:18 +02:00
|
|
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_size, time);
|
2013-02-05 04:42:40 +01:00
|
|
|
ASSERT_TRUE(env->FileExists(kLogFile));
|
|
|
|
|
2013-03-08 21:29:19 +01:00
|
|
|
RollLogFileByTimeTest(&logger, time, kSampleMessage + ":RollLogFileByTime");
|
2013-02-05 04:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AutoRollLoggerTest,
|
|
|
|
OpenLogFilesMultipleTimesWithOptionLog_max_size) {
|
|
|
|
// If only 'log_max_size' options is specified, then every time
|
2013-10-05 07:32:05 +02:00
|
|
|
// when rocksdb is restarted, a new empty log file will be created.
|
2013-02-05 04:42:40 +01:00
|
|
|
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);
|
|
|
|
|
2013-02-13 23:21:24 +01:00
|
|
|
LogMessage(logger, kSampleMessage.c_str());
|
2013-02-05 04:42:40 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AutoRollLoggerTest, CompositeRollByTimeAndSizeLogger) {
|
2014-08-18 19:23:18 +02:00
|
|
|
size_t time = 2, log_max_size = 1024 * 5;
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
InitTestDb();
|
|
|
|
|
2013-03-08 21:29:19 +01:00
|
|
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_max_size, time);
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
// Test the ability to roll by size
|
|
|
|
RollLogFileBySizeTest(
|
2013-03-08 21:29:19 +01:00
|
|
|
&logger, log_max_size,
|
2013-02-05 04:42:40 +01:00
|
|
|
kSampleMessage + ":CompositeRollByTimeAndSizeLogger");
|
|
|
|
|
|
|
|
// Test the ability to roll by Time
|
2013-03-08 21:29:19 +01:00
|
|
|
RollLogFileByTimeTest( &logger, time,
|
2013-02-05 04:42:40 +01:00
|
|
|
kSampleMessage + ":CompositeRollByTimeAndSizeLogger");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AutoRollLoggerTest, CreateLoggerFromOptions) {
|
2014-02-05 01:31:18 +01:00
|
|
|
DBOptions options;
|
2013-02-05 04:42:40 +01:00
|
|
|
shared_ptr<Logger> logger;
|
|
|
|
|
|
|
|
// Normal logger
|
|
|
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, "", env, options, &logger));
|
2013-02-13 23:21:24 +01:00
|
|
|
ASSERT_TRUE(dynamic_cast<PosixLogger*>(logger.get()));
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
// Only roll by size
|
|
|
|
InitTestDb();
|
|
|
|
options.max_log_file_size = 1024;
|
|
|
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, "", env, options, &logger));
|
|
|
|
AutoRollLogger* auto_roll_logger =
|
|
|
|
dynamic_cast<AutoRollLogger*>(logger.get());
|
2013-02-13 23:21:24 +01:00
|
|
|
ASSERT_TRUE(auto_roll_logger);
|
2013-02-05 04:42:40 +01:00
|
|
|
RollLogFileBySizeTest(
|
|
|
|
auto_roll_logger, options.max_log_file_size,
|
|
|
|
kSampleMessage + ":CreateLoggerFromOptions - size");
|
|
|
|
|
|
|
|
// Only roll by Time
|
|
|
|
InitTestDb();
|
|
|
|
options.max_log_file_size = 0;
|
2014-08-18 19:23:18 +02:00
|
|
|
options.log_file_time_to_roll = 2;
|
2013-02-05 04:42:40 +01:00
|
|
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, "", env, options, &logger));
|
|
|
|
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;
|
2014-08-18 19:23:18 +02:00
|
|
|
options.log_file_time_to_roll = 2;
|
2013-02-05 04:42:40 +01:00
|
|
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, "", env, options, &logger));
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2014-02-26 23:41:28 +01:00
|
|
|
TEST(AutoRollLoggerTest, InfoLogLevel) {
|
|
|
|
InitTestDb();
|
|
|
|
|
|
|
|
size_t log_size = 8192;
|
2014-02-28 07:15:30 +01:00
|
|
|
size_t log_lines = 0;
|
2014-02-27 23:00:10 +01:00
|
|
|
// 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);
|
2014-04-11 00:27:42 +02:00
|
|
|
for (int log_level = InfoLogLevel::FATAL_LEVEL;
|
|
|
|
log_level >= InfoLogLevel::DEBUG_LEVEL; log_level--) {
|
2014-02-27 23:00:10 +01:00
|
|
|
logger.SetInfoLogLevel((InfoLogLevel)log_level);
|
2014-04-11 00:27:42 +02:00
|
|
|
for (int log_type = InfoLogLevel::DEBUG_LEVEL;
|
|
|
|
log_type <= InfoLogLevel::FATAL_LEVEL; log_type++) {
|
2014-02-27 23:00:10 +01:00
|
|
|
// log messages with log level smaller than log_level will not be
|
|
|
|
// logged.
|
|
|
|
LogMessage((InfoLogLevel)log_type, &logger, kSampleMessage.c_str());
|
|
|
|
}
|
2014-04-11 00:27:42 +02:00
|
|
|
log_lines += InfoLogLevel::FATAL_LEVEL - log_level + 1;
|
2014-02-27 23:00:10 +01:00
|
|
|
}
|
2014-04-11 00:27:42 +02:00
|
|
|
for (int log_level = InfoLogLevel::FATAL_LEVEL;
|
|
|
|
log_level >= InfoLogLevel::DEBUG_LEVEL; log_level--) {
|
2014-02-27 23:00:10 +01:00
|
|
|
logger.SetInfoLogLevel((InfoLogLevel)log_level);
|
|
|
|
|
|
|
|
// again, messages with level smaller than log_level will not be logged.
|
|
|
|
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());
|
2014-04-11 00:27:42 +02:00
|
|
|
log_lines += InfoLogLevel::FATAL_LEVEL - log_level + 1;
|
2014-02-26 23:41:28 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-27 23:00:10 +01:00
|
|
|
std::ifstream inFile(AutoRollLoggerTest::kLogFile.c_str());
|
2014-02-28 07:15:30 +01:00
|
|
|
size_t lines = std::count(std::istreambuf_iterator<char>(inFile),
|
2014-02-27 23:00:10 +01:00
|
|
|
std::istreambuf_iterator<char>(), '\n');
|
|
|
|
ASSERT_EQ(log_lines, lines);
|
|
|
|
inFile.close();
|
2014-02-26 23:41:28 +01:00
|
|
|
}
|
|
|
|
|
2015-02-02 18:47:24 +01:00
|
|
|
// Test the logger Header function for roll over logs
|
|
|
|
// We expect the new logs creates as roll over to carry the headers specified
|
|
|
|
TEST(AutoRollLoggerTest, LogHeaderTest) {
|
|
|
|
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";
|
|
|
|
|
|
|
|
InitTestDb();
|
|
|
|
|
|
|
|
AutoRollLogger logger(Env::Default(), kTestDir, /*db_log_dir=*/ "",
|
|
|
|
LOG_MAX_SIZE, /*log_file_time_to_roll=*/ 0);
|
|
|
|
|
|
|
|
// log some headers
|
|
|
|
for (size_t i = 0; i < MAX_HEADERS; i++) {
|
|
|
|
Header(&logger, "%s %d", HEADER_STR.c_str(), i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// log enough data to cause a roll over
|
|
|
|
size_t i = 0;
|
|
|
|
while (logger.GetLogFileSize() < LOG_MAX_SIZE) {
|
|
|
|
Info(&logger, (kSampleMessage + ":LogHeaderTest line %d").c_str(), i);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify that the new log contains all the header logs
|
|
|
|
std::stringstream ssbuf;
|
|
|
|
std::string line;
|
|
|
|
size_t count = 0;
|
|
|
|
|
|
|
|
std::ifstream inFile(AutoRollLoggerTest::kLogFile.c_str());
|
|
|
|
ssbuf << inFile.rdbuf();
|
|
|
|
|
|
|
|
while (getline(ssbuf, line)) {
|
|
|
|
if (line.find(HEADER_STR) != std::string::npos) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_EQ(count, MAX_HEADERS);
|
|
|
|
}
|
|
|
|
|
2014-06-16 19:27:42 +02:00
|
|
|
TEST(AutoRollLoggerTest, LogFileExistence) {
|
|
|
|
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));
|
|
|
|
ASSERT_TRUE(env->FileExists(kLogFile));
|
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2013-10-04 06:49:15 +02:00
|
|
|
return rocksdb::test::RunAllTests();
|
2013-02-05 04:42:40 +01:00
|
|
|
}
|