2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// 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).
|
2013-10-16 23:59:46 +02:00
|
|
|
//
|
2017-02-28 20:05:08 +01:00
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2019-06-01 02:19:43 +02:00
|
|
|
#include "logging/auto_roll_logger.h"
|
2017-03-16 03:22:52 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <algorithm>
|
2013-02-05 04:42:40 +01:00
|
|
|
#include <cmath>
|
2014-02-27 23:00:10 +01:00
|
|
|
#include <fstream>
|
2017-03-16 03:22:52 +01:00
|
|
|
#include <iostream>
|
2014-02-27 23:00:10 +01:00
|
|
|
#include <iterator>
|
2017-03-16 03:22:52 +01:00
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
2019-06-01 02:19:43 +02:00
|
|
|
#include "logging/logging.h"
|
2016-02-19 03:03:53 +01:00
|
|
|
#include "port/port.h"
|
2017-03-16 03:22:52 +01:00
|
|
|
#include "rocksdb/db.h"
|
2019-05-30 20:21:38 +02:00
|
|
|
#include "test_util/sync_point.h"
|
|
|
|
#include "test_util/testharness.h"
|
2019-07-09 23:48:07 +02:00
|
|
|
#include "test_util/testutil.h"
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-04-03 20:24:39 +02:00
|
|
|
namespace {
|
|
|
|
class NoSleepEnv : public EnvWrapper {
|
|
|
|
public:
|
|
|
|
NoSleepEnv(Env* base) : EnvWrapper(base) {}
|
2019-02-14 22:52:47 +01:00
|
|
|
void SleepForMicroseconds(int micros) override {
|
2017-04-03 20:24:39 +02:00
|
|
|
fake_time_ += static_cast<uint64_t>(micros);
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
uint64_t NowNanos() override { return fake_time_ * 1000; }
|
2017-04-03 20:24:39 +02:00
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
uint64_t NowMicros() override { return fake_time_; }
|
2017-04-03 20:24:39 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
uint64_t fake_time_ = 6666666666;
|
|
|
|
};
|
|
|
|
} // namespace
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2019-05-31 19:45:20 +02: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 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
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
class AutoRollLoggerTest : public testing::Test {
|
2013-02-05 04:42:40 +01:00
|
|
|
public:
|
|
|
|
static void InitTestDb() {
|
2015-07-22 23:36:43 +02:00
|
|
|
#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(),
|
2015-07-27 23:25:57 +02:00
|
|
|
[](char ch) { return ch == '/'; }, '\\');
|
2015-07-22 23:36:43 +02:00
|
|
|
std::string deleteCmd = "if exist " + testDir + " rd /s /q " + testDir;
|
|
|
|
#else
|
|
|
|
std::string deleteCmd = "rm -rf " + kTestDir;
|
|
|
|
#endif
|
2013-02-13 23:21:24 +01:00
|
|
|
ASSERT_TRUE(system(deleteCmd.c_str()) == 0);
|
2020-09-17 00:45:30 +02:00
|
|
|
ASSERT_OK(Env::Default()->CreateDir(kTestDir));
|
2013-02-05 04:42:40 +01:00
|
|
|
}
|
|
|
|
|
2016-05-20 16:42:18 +02:00
|
|
|
void RollLogFileBySizeTest(AutoRollLogger* logger, size_t log_max_size,
|
|
|
|
const std::string& log_message);
|
2017-04-03 20:24:39 +02:00
|
|
|
void RollLogFileByTimeTest(Env*, AutoRollLogger* logger, size_t time,
|
|
|
|
const std::string& log_message);
|
2019-05-31 19:45:20 +02:00
|
|
|
// 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, "");
|
|
|
|
}
|
|
|
|
}
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2016-05-20 16:42:18 +02:00
|
|
|
static const std::string kSampleMessage;
|
|
|
|
static const std::string kTestDir;
|
|
|
|
static const std::string kLogFile;
|
2017-04-03 20:24:39 +02:00
|
|
|
static Env* default_env;
|
2013-02-05 04:42:40 +01:00
|
|
|
};
|
|
|
|
|
2016-05-20 16:42:18 +02:00
|
|
|
const std::string AutoRollLoggerTest::kSampleMessage(
|
2013-02-05 04:42:40 +01:00
|
|
|
"this is the message to be written to the log file!!");
|
2018-07-14 02:18:39 +02:00
|
|
|
const std::string AutoRollLoggerTest::kTestDir(
|
|
|
|
test::PerThreadDBPath("db_log_test"));
|
|
|
|
const std::string AutoRollLoggerTest::kLogFile(
|
|
|
|
test::PerThreadDBPath("db_log_test") + "/LOG");
|
2017-04-03 20:24:39 +02:00
|
|
|
Env* AutoRollLoggerTest::default_env = Env::Default();
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
void AutoRollLoggerTest::RollLogFileBySizeTest(AutoRollLogger* logger,
|
|
|
|
size_t log_max_size,
|
2016-05-20 16:42:18 +02:00
|
|
|
const std::string& log_message) {
|
2014-04-11 00:27:42 +02:00
|
|
|
logger->SetInfoLogLevel(InfoLogLevel::INFO_LEVEL);
|
2020-02-11 07:24:29 +01:00
|
|
|
ASSERT_EQ(InfoLogLevel::INFO_LEVEL, logger->GetInfoLogLevel());
|
|
|
|
ASSERT_EQ(InfoLogLevel::INFO_LEVEL,
|
|
|
|
logger->TEST_inner_logger()->GetInfoLogLevel());
|
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
|
|
|
}
|
|
|
|
|
2017-04-03 20:24:39 +02:00
|
|
|
void AutoRollLoggerTest::RollLogFileByTimeTest(Env* env, AutoRollLogger* logger,
|
|
|
|
size_t time,
|
|
|
|
const std::string& log_message) {
|
|
|
|
uint64_t expected_ctime;
|
|
|
|
uint64_t actual_ctime;
|
|
|
|
|
2013-02-05 04:42:40 +01:00
|
|
|
uint64_t total_log_size;
|
rocksdb: Replace ASSERT* with EXPECT* in functions that does not return void value
Summary:
gtest does not use exceptions to fail a unit test by design, and `ASSERT*`s are implemented using `return`. As a consequence we cannot use `ASSERT*` in a function that does not return `void` value ([[ https://code.google.com/p/googletest/wiki/AdvancedGuide#Assertion_Placement | 1]]), and have to fix our existing code. This diff does this in a generic way, with no manual changes.
In order to detect all existing `ASSERT*` that are used in functions that doesn't return void value, I change the code to generate compile errors for such cases.
In `util/testharness.h` I defined `EXPECT*` assertions, the same way as `ASSERT*`, and redefined `ASSERT*` to return `void`. Then executed:
```lang=bash
% USE_CLANG=1 make all -j55 -k 2> build.log
% perl -naF: -e 'print "-- -number=".$F[1]." ".$F[0]."\n" if /: error:/' \
build.log | xargs -L 1 perl -spi -e 's/ASSERT/EXPECT/g if $. == $number'
% make format
```
After that I reverted back change to `ASSERT*` in `util/testharness.h`. But preserved introduced `EXPECT*`, which is the same as `ASSERT*`. This will be deleted once switched to gtest.
This diff is independent and contains manual changes only in `util/testharness.h`.
Test Plan:
Make sure all tests are passing.
```lang=bash
% USE_CLANG=1 make check
```
Reviewers: igor, lgalanis, sdong, yufei.zhu, rven, meyering
Reviewed By: meyering
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D33333
2015-03-17 04:52:32 +01:00
|
|
|
EXPECT_OK(env->GetFileSize(kLogFile, &total_log_size));
|
2017-04-03 20:24:39 +02:00
|
|
|
expected_ctime = logger->TEST_ctime();
|
2013-02-05 04:42:40 +01:00
|
|
|
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) {
|
2017-04-03 20:24:39 +02:00
|
|
|
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();
|
2013-02-05 04:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Make the log file expire
|
2017-04-03 20:24:39 +02:00
|
|
|
env->SleepForMicroseconds(static_cast<int>(time * 1000000));
|
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.
|
2017-04-03 20:24:39 +02:00
|
|
|
actual_ctime = logger->TEST_ctime();
|
|
|
|
EXPECT_LT(expected_ctime, actual_ctime);
|
rocksdb: Replace ASSERT* with EXPECT* in functions that does not return void value
Summary:
gtest does not use exceptions to fail a unit test by design, and `ASSERT*`s are implemented using `return`. As a consequence we cannot use `ASSERT*` in a function that does not return `void` value ([[ https://code.google.com/p/googletest/wiki/AdvancedGuide#Assertion_Placement | 1]]), and have to fix our existing code. This diff does this in a generic way, with no manual changes.
In order to detect all existing `ASSERT*` that are used in functions that doesn't return void value, I change the code to generate compile errors for such cases.
In `util/testharness.h` I defined `EXPECT*` assertions, the same way as `ASSERT*`, and redefined `ASSERT*` to return `void`. Then executed:
```lang=bash
% USE_CLANG=1 make all -j55 -k 2> build.log
% perl -naF: -e 'print "-- -number=".$F[1]." ".$F[0]."\n" if /: error:/' \
build.log | xargs -L 1 perl -spi -e 's/ASSERT/EXPECT/g if $. == $number'
% make format
```
After that I reverted back change to `ASSERT*` in `util/testharness.h`. But preserved introduced `EXPECT*`, which is the same as `ASSERT*`. This will be deleted once switched to gtest.
This diff is independent and contains manual changes only in `util/testharness.h`.
Test Plan:
Make sure all tests are passing.
```lang=bash
% USE_CLANG=1 make check
```
Reviewers: igor, lgalanis, sdong, yufei.zhu, rven, meyering
Reviewed By: meyering
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D33333
2015-03-17 04:52:32 +01:00
|
|
|
EXPECT_LT(logger->GetLogFileSize(), total_log_size);
|
2013-02-05 04:42:40 +01:00
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(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;
|
2019-05-31 19:45:20 +02:00
|
|
|
size_t keep_log_file_num = 10;
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2019-05-31 19:45:20 +02:00
|
|
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_max_size, 0,
|
|
|
|
keep_log_file_num);
|
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");
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(AutoRollLoggerTest, RollLogFileByTime) {
|
2017-04-03 20:24:39 +02:00
|
|
|
NoSleepEnv nse(Env::Default());
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2017-04-03 20:24:39 +02:00
|
|
|
size_t time = 2;
|
|
|
|
size_t log_size = 1024 * 5;
|
2019-05-31 19:45:20 +02:00
|
|
|
size_t keep_log_file_num = 10;
|
2017-04-03 20:24:39 +02:00
|
|
|
|
|
|
|
InitTestDb();
|
|
|
|
// -- Test the existence of file during the server restart.
|
|
|
|
ASSERT_EQ(Status::NotFound(), default_env->FileExists(kLogFile));
|
2019-05-31 19:45:20 +02:00
|
|
|
AutoRollLogger logger(&nse, kTestDir, "", log_size, time, keep_log_file_num);
|
2017-04-03 20:24:39 +02:00
|
|
|
ASSERT_OK(default_env->FileExists(kLogFile));
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2017-04-03 20:24:39 +02:00
|
|
|
RollLogFileByTimeTest(&nse, &logger, time,
|
|
|
|
kSampleMessage + ":RollLogFileByTime");
|
2013-02-05 04:42:40 +01:00
|
|
|
}
|
|
|
|
|
2020-02-11 07:24:29 +01:00
|
|
|
TEST_F(AutoRollLoggerTest, SetInfoLogLevel) {
|
|
|
|
InitTestDb();
|
|
|
|
Options options;
|
|
|
|
options.info_log_level = InfoLogLevel::FATAL_LEVEL;
|
|
|
|
options.max_log_file_size = 1024;
|
|
|
|
std::shared_ptr<Logger> logger;
|
|
|
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, options, &logger));
|
|
|
|
auto* auto_roll_logger = dynamic_cast<AutoRollLogger*>(logger.get());
|
|
|
|
ASSERT_NE(nullptr, auto_roll_logger);
|
|
|
|
ASSERT_EQ(InfoLogLevel::FATAL_LEVEL, auto_roll_logger->GetInfoLogLevel());
|
|
|
|
ASSERT_EQ(InfoLogLevel::FATAL_LEVEL,
|
|
|
|
auto_roll_logger->TEST_inner_logger()->GetInfoLogLevel());
|
|
|
|
auto_roll_logger->SetInfoLogLevel(InfoLogLevel::DEBUG_LEVEL);
|
|
|
|
ASSERT_EQ(InfoLogLevel::DEBUG_LEVEL, auto_roll_logger->GetInfoLogLevel());
|
|
|
|
ASSERT_EQ(InfoLogLevel::DEBUG_LEVEL, logger->GetInfoLogLevel());
|
|
|
|
ASSERT_EQ(InfoLogLevel::DEBUG_LEVEL,
|
|
|
|
auto_roll_logger->TEST_inner_logger()->GetInfoLogLevel());
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(AutoRollLoggerTest, OpenLogFilesMultipleTimesWithOptionLog_max_size) {
|
2013-02-05 04:42:40 +01:00
|
|
|
// 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;
|
2019-05-31 19:45:20 +02:00
|
|
|
size_t keep_log_file_num = 10;
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2019-05-31 19:45:20 +02:00
|
|
|
AutoRollLogger* logger = new AutoRollLogger(Env::Default(), kTestDir, "",
|
|
|
|
log_size, 0, keep_log_file_num);
|
2013-02-05 04:42:40 +01:00
|
|
|
|
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.
|
2019-05-31 19:45:20 +02:00
|
|
|
logger = new AutoRollLogger(Env::Default(), kTestDir, "", log_size, 0, 10);
|
2013-02-05 04:42:40 +01:00
|
|
|
ASSERT_EQ(logger->GetLogFileSize(), kZero);
|
|
|
|
delete logger;
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(AutoRollLoggerTest, CompositeRollByTimeAndSizeLogger) {
|
2014-08-18 19:23:18 +02:00
|
|
|
size_t time = 2, log_max_size = 1024 * 5;
|
2019-05-31 19:45:20 +02:00
|
|
|
size_t keep_log_file_num = 10;
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
InitTestDb();
|
|
|
|
|
2017-04-03 20:24:39 +02:00
|
|
|
NoSleepEnv nse(Env::Default());
|
2019-05-31 19:45:20 +02:00
|
|
|
AutoRollLogger logger(&nse, kTestDir, "", log_max_size, time,
|
|
|
|
keep_log_file_num);
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
// Test the ability to roll by size
|
2017-04-03 20:24:39 +02:00
|
|
|
RollLogFileBySizeTest(&logger, log_max_size,
|
|
|
|
kSampleMessage + ":CompositeRollByTimeAndSizeLogger");
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
// Test the ability to roll by Time
|
2017-04-03 20:24:39 +02:00
|
|
|
RollLogFileByTimeTest(&nse, &logger, time,
|
|
|
|
kSampleMessage + ":CompositeRollByTimeAndSizeLogger");
|
2013-02-05 04:42:40 +01:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:13:49 +02:00
|
|
|
#ifndef OS_WIN
|
2015-07-13 21:11:05 +02:00
|
|
|
// TODO: does not build for Windows because of PosixLogger use below. Need to
|
|
|
|
// port
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(AutoRollLoggerTest, CreateLoggerFromOptions) {
|
2014-02-05 01:31:18 +01:00
|
|
|
DBOptions options;
|
2017-04-03 20:24:39 +02:00
|
|
|
NoSleepEnv nse(Env::Default());
|
2018-11-09 20:17:34 +01:00
|
|
|
std::shared_ptr<Logger> logger;
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
// Normal logger
|
2015-10-30 02:07:37 +01:00
|
|
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, 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;
|
2015-10-30 02:07:37 +01:00
|
|
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, options, &logger));
|
2013-02-05 04:42:40 +01:00
|
|
|
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
|
2017-04-03 20:24:39 +02:00
|
|
|
options.env = &nse;
|
2013-02-05 04:42:40 +01:00
|
|
|
InitTestDb();
|
|
|
|
options.max_log_file_size = 0;
|
2014-08-18 19:23:18 +02:00
|
|
|
options.log_file_time_to_roll = 2;
|
2015-10-30 02:07:37 +01:00
|
|
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, options, &logger));
|
2013-02-05 04:42:40 +01:00
|
|
|
auto_roll_logger =
|
|
|
|
dynamic_cast<AutoRollLogger*>(logger.get());
|
2017-04-03 20:24:39 +02:00
|
|
|
RollLogFileByTimeTest(&nse, auto_roll_logger, options.log_file_time_to_roll,
|
|
|
|
kSampleMessage + ":CreateLoggerFromOptions - time");
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
// 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;
|
2015-10-30 02:07:37 +01:00
|
|
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, options, &logger));
|
2013-02-05 04:42:40 +01:00
|
|
|
auto_roll_logger =
|
|
|
|
dynamic_cast<AutoRollLogger*>(logger.get());
|
2017-04-03 20:24:39 +02:00
|
|
|
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");
|
2019-05-31 19:45:20 +02:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
2013-02-05 04:42:40 +01:00
|
|
|
}
|
2016-02-17 21:06:45 +01:00
|
|
|
|
|
|
|
TEST_F(AutoRollLoggerTest, LogFlushWhileRolling) {
|
|
|
|
DBOptions options;
|
2018-11-09 20:17:34 +01:00
|
|
|
std::shared_ptr<Logger> logger;
|
2016-02-17 21:06:45 +01:00
|
|
|
|
|
|
|
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);
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::port::Thread flush_thread;
|
2016-02-23 06:32:19 +01:00
|
|
|
|
2016-07-07 20:35:40 +02:00
|
|
|
// 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).
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependencyAndMarkers(
|
2016-07-07 20:35:40 +02:00
|
|
|
{{"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"}});
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2016-02-19 03:03:53 +01:00
|
|
|
|
2019-07-09 23:48:07 +02:00
|
|
|
flush_thread = port::Thread([&]() { auto_roll_logger->Flush(); });
|
2016-02-23 06:32:19 +01:00
|
|
|
TEST_SYNC_POINT(
|
|
|
|
"AutoRollLoggerTest::LogFlushWhileRolling:PreRollAndPostThreadInit");
|
2016-02-17 21:06:45 +01:00
|
|
|
RollLogFileBySizeTest(auto_roll_logger, options.max_log_file_size,
|
|
|
|
kSampleMessage + ":LogFlushWhileRolling");
|
|
|
|
flush_thread.join();
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2016-02-17 21:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // OS_WIN
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(AutoRollLoggerTest, InfoLogLevel) {
|
2014-02-26 23:41:28 +01:00
|
|
|
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.
|
|
|
|
{
|
2019-05-31 19:45:20 +02:00
|
|
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_size, 0, 10);
|
2015-07-03 02:14:39 +02:00
|
|
|
for (int log_level = InfoLogLevel::HEADER_LEVEL;
|
2014-04-11 00:27:42 +02:00
|
|
|
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;
|
2015-07-03 02:14:39 +02:00
|
|
|
log_type <= InfoLogLevel::HEADER_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());
|
|
|
|
}
|
2015-07-03 02:14:39 +02:00
|
|
|
log_lines += InfoLogLevel::HEADER_LEVEL - log_level + 1;
|
2014-02-27 23:00:10 +01:00
|
|
|
}
|
2015-07-03 02:14:39 +02:00
|
|
|
for (int log_level = InfoLogLevel::HEADER_LEVEL;
|
2014-04-11 00:27:42 +02:00
|
|
|
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.
|
2017-03-16 03:22:52 +01:00
|
|
|
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());
|
2015-07-03 02:14:39 +02:00
|
|
|
log_lines += InfoLogLevel::HEADER_LEVEL - log_level + 1;
|
2014-02-26 23:41:28 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-16 19:57:56 +01:00
|
|
|
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;
|
2019-05-31 19:45:20 +02:00
|
|
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_size, 0, 10);
|
2018-01-16 19:57:56 +01:00
|
|
|
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());
|
|
|
|
|
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
|
2016-05-20 16:42:18 +02:00
|
|
|
static std::vector<std::string> GetOldFileNames(const std::string& path) {
|
|
|
|
std::vector<std::string> ret;
|
2015-07-22 23:36:43 +02:00
|
|
|
|
2016-05-20 16:42:18 +02:00
|
|
|
const std::string dirname = path.substr(/*start=*/0, path.find_last_of("/"));
|
|
|
|
const std::string fname = path.substr(path.find_last_of("/") + 1);
|
2015-02-23 20:59:39 +01:00
|
|
|
|
2016-05-20 16:42:18 +02:00
|
|
|
std::vector<std::string> children;
|
2020-09-17 00:45:30 +02:00
|
|
|
EXPECT_OK(Env::Default()->GetChildren(dirname, &children));
|
2015-02-23 20:59:39 +01:00
|
|
|
|
|
|
|
// We know that the old log files are named [path]<something>
|
|
|
|
// Return all entities that match the pattern
|
2015-07-22 23:36:43 +02:00
|
|
|
for (auto& child : children) {
|
2015-02-23 20:59:39 +01:00
|
|
|
if (fname != child && child.find(fname) == 0) {
|
|
|
|
ret.push_back(dirname + "/" + child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(AutoRollLoggerTest, LogHeaderTest) {
|
2015-02-02 18:47:24 +01:00
|
|
|
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";
|
|
|
|
|
2015-07-03 02:14:39 +02:00
|
|
|
// 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++) {
|
2015-02-02 18:47:24 +01:00
|
|
|
|
2015-07-03 02:14:39 +02:00
|
|
|
InitTestDb();
|
2015-02-02 18:47:24 +01:00
|
|
|
|
2019-05-31 19:45:20 +02:00
|
|
|
AutoRollLogger logger(Env::Default(), kTestDir, /*db_log_dir=*/"",
|
|
|
|
LOG_MAX_SIZE, /*log_file_time_to_roll=*/0,
|
|
|
|
/*keep_log_file_num=*/10);
|
2015-02-23 20:59:39 +01:00
|
|
|
|
2015-07-03 02:14:39 +02:00
|
|
|
if (test_num == 0) {
|
|
|
|
// Log some headers explicitly using Header()
|
|
|
|
for (size_t i = 0; i < MAX_HEADERS; i++) {
|
2019-04-04 21:05:42 +02:00
|
|
|
Header(&logger, "%s %" ROCKSDB_PRIszt, HEADER_STR.c_str(), i);
|
2015-07-03 02:14:39 +02:00
|
|
|
}
|
|
|
|
} else if (test_num == 1) {
|
|
|
|
// HEADER_LEVEL should make this behave like calling Header()
|
|
|
|
for (size_t i = 0; i < MAX_HEADERS; i++) {
|
2019-04-04 21:05:42 +02:00
|
|
|
ROCKS_LOG_HEADER(&logger, "%s %" ROCKSDB_PRIszt, HEADER_STR.c_str(), i);
|
2015-07-03 02:14:39 +02:00
|
|
|
}
|
2015-02-23 20:59:39 +01:00
|
|
|
}
|
|
|
|
|
2016-05-20 16:42:18 +02:00
|
|
|
const std::string newfname = logger.TEST_log_fname();
|
2015-07-03 02:14:39 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2015-02-23 20:59:39 +01:00
|
|
|
|
2015-07-03 02:14:39 +02:00
|
|
|
Info(&logger, "Rollover");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the log for the latest file
|
|
|
|
LogFlush(&logger);
|
2015-02-02 18:47:24 +01:00
|
|
|
|
2015-07-22 23:36:43 +02:00
|
|
|
const auto oldfiles = GetOldFileNames(newfname);
|
2015-02-02 18:47:24 +01:00
|
|
|
|
2015-07-03 02:14:39 +02:00
|
|
|
ASSERT_EQ(oldfiles.size(), (size_t) 2);
|
2015-02-02 18:47:24 +01:00
|
|
|
|
2015-07-22 23:36:43 +02:00
|
|
|
for (auto& oldfname : oldfiles) {
|
2015-07-03 02:14:39 +02:00
|
|
|
// verify that the files rolled over
|
|
|
|
ASSERT_NE(oldfname, newfname);
|
|
|
|
// verify that the old log contains all the header logs
|
2019-07-09 23:48:07 +02:00
|
|
|
ASSERT_EQ(test::GetLinesCount(oldfname, HEADER_STR), MAX_HEADERS);
|
2015-07-03 02:14:39 +02:00
|
|
|
}
|
2015-02-02 18:47:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(AutoRollLoggerTest, LogFileExistence) {
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::DB* db;
|
|
|
|
ROCKSDB_NAMESPACE::Options options;
|
2016-04-15 02:33:53 +02:00
|
|
|
#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
|
2016-05-20 16:42:18 +02:00
|
|
|
std::string deleteCmd = "rm -rf " + kTestDir;
|
2016-04-15 02:33:53 +02:00
|
|
|
#endif
|
2014-06-16 19:27:42 +02:00
|
|
|
ASSERT_EQ(system(deleteCmd.c_str()), 0);
|
|
|
|
options.max_log_file_size = 100 * 1024 * 1024;
|
|
|
|
options.create_if_missing = true;
|
2020-02-20 21:07:53 +01:00
|
|
|
ASSERT_OK(ROCKSDB_NAMESPACE::DB::Open(options, kTestDir, &db));
|
2017-04-03 20:24:39 +02:00
|
|
|
ASSERT_OK(default_env->FileExists(kLogFile));
|
2014-06-16 19:27:42 +02:00
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
|
2019-07-24 21:04:58 +02:00
|
|
|
TEST_F(AutoRollLoggerTest, FileCreateFailure) {
|
|
|
|
Options options;
|
|
|
|
options.max_log_file_size = 100 * 1024 * 1024;
|
|
|
|
options.db_log_dir = "/a/dir/does/not/exist/at/all";
|
|
|
|
|
|
|
|
std::shared_ptr<Logger> logger;
|
|
|
|
ASSERT_NOK(CreateLoggerFromOptions("", options, &logger));
|
|
|
|
ASSERT_TRUE(!logger);
|
|
|
|
}
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2015-03-17 22:08:00 +01:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2013-02-05 04:42:40 +01:00
|
|
|
}
|
2017-02-28 20:05:08 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-04-16 02:19:57 +02:00
|
|
|
int main(int /*argc*/, char** /*argv*/) {
|
2017-02-28 20:05:08 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"SKIPPED as AutoRollLogger is not supported in ROCKSDB_LITE\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !ROCKSDB_LITE
|