2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2013-10-16 21:59:46 +00:00
|
|
|
// 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 03:42:40 +00:00
|
|
|
#include <string>
|
2016-02-17 20:06:45 +00:00
|
|
|
#include <thread>
|
2015-02-23 19:59:39 +00:00
|
|
|
#include <vector>
|
2013-02-05 03:42:40 +00:00
|
|
|
#include <cmath>
|
2014-02-27 22:00:10 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <algorithm>
|
2016-01-26 01:49:58 +00:00
|
|
|
#include "db/auto_roll_logger.h"
|
2016-02-19 02:03:53 +00:00
|
|
|
#include "port/port.h"
|
2016-02-17 20:06:45 +00:00
|
|
|
#include "util/sync_point.h"
|
2013-02-05 03:42:40 +00:00
|
|
|
#include "util/testharness.h"
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/db.h"
|
2013-02-05 03:42:40 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2013-02-05 03:42:40 +00:00
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
class AutoRollLoggerTest : public testing::Test {
|
2013-02-05 03:42:40 +00:00
|
|
|
public:
|
|
|
|
static void InitTestDb() {
|
2015-07-22 21:36:43 +00: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 21:25:57 +00:00
|
|
|
[](char ch) { return ch == '/'; }, '\\');
|
2015-07-22 21:36:43 +00:00
|
|
|
std::string deleteCmd = "if exist " + testDir + " rd /s /q " + testDir;
|
|
|
|
#else
|
|
|
|
std::string deleteCmd = "rm -rf " + kTestDir;
|
|
|
|
#endif
|
2013-02-13 22:21:24 +00:00
|
|
|
ASSERT_TRUE(system(deleteCmd.c_str()) == 0);
|
2013-02-05 03:42:40 +00:00
|
|
|
Env::Default()->CreateDir(kTestDir);
|
|
|
|
}
|
|
|
|
|
2016-05-20 14:42:18 +00:00
|
|
|
void RollLogFileBySizeTest(AutoRollLogger* logger, size_t log_max_size,
|
|
|
|
const std::string& log_message);
|
|
|
|
uint64_t RollLogFileByTimeTest(AutoRollLogger* logger, size_t time,
|
|
|
|
const std::string& log_message);
|
2013-02-05 03:42:40 +00:00
|
|
|
|
2016-05-20 14:42:18 +00:00
|
|
|
static const std::string kSampleMessage;
|
|
|
|
static const std::string kTestDir;
|
|
|
|
static const std::string kLogFile;
|
2013-02-05 03:42:40 +00:00
|
|
|
static Env* env;
|
|
|
|
};
|
|
|
|
|
2016-05-20 14:42:18 +00:00
|
|
|
const std::string AutoRollLoggerTest::kSampleMessage(
|
2013-02-05 03:42:40 +00:00
|
|
|
"this is the message to be written to the log file!!");
|
2016-05-20 14:42:18 +00:00
|
|
|
const std::string AutoRollLoggerTest::kTestDir(test::TmpDir() + "/db_log_test");
|
|
|
|
const std::string AutoRollLoggerTest::kLogFile(test::TmpDir() +
|
|
|
|
"/db_log_test/LOG");
|
2013-02-05 03:42:40 +00:00
|
|
|
Env* AutoRollLoggerTest::env = Env::Default();
|
|
|
|
|
2013-02-13 22:21:24 +00: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 04:17:14 +00:00
|
|
|
namespace {
|
2013-02-13 22:21:24 +00:00
|
|
|
void LogMessage(Logger* logger, const char* message) {
|
|
|
|
Log(logger, "%s", message);
|
|
|
|
}
|
|
|
|
|
2014-02-26 22:41:28 +00:00
|
|
|
void LogMessage(const InfoLogLevel log_level, Logger* logger,
|
|
|
|
const char* message) {
|
|
|
|
Log(log_level, logger, "%s", message);
|
|
|
|
}
|
2014-04-10 04:17:14 +00:00
|
|
|
} // namespace
|
2014-02-26 22:41:28 +00:00
|
|
|
|
2014-04-10 04:17:14 +00:00
|
|
|
namespace {
|
2013-02-05 03:42:40 +00: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 04:17:14 +00:00
|
|
|
} // namespace
|
2013-02-05 03:42:40 +00:00
|
|
|
|
|
|
|
void AutoRollLoggerTest::RollLogFileBySizeTest(AutoRollLogger* logger,
|
|
|
|
size_t log_max_size,
|
2016-05-20 14:42:18 +00:00
|
|
|
const std::string& log_message) {
|
2014-04-10 22:27:42 +00:00
|
|
|
logger->SetInfoLogLevel(InfoLogLevel::INFO_LEVEL);
|
2013-02-05 03:42:40 +00:00
|
|
|
// measure the size of each message, which is supposed
|
|
|
|
// to be equal or greater than log_message.size()
|
2013-02-13 22:21:24 +00:00
|
|
|
LogMessage(logger, log_message.c_str());
|
2013-02-05 03:42:40 +00: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 22:21:24 +00:00
|
|
|
LogMessage(logger, log_message.c_str());
|
2013-02-05 03:42:40 +00:00
|
|
|
current_log_size += message_size;
|
|
|
|
ASSERT_EQ(current_log_size, logger->GetLogFileSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now the log file will be rolled
|
2013-02-13 22:21:24 +00:00
|
|
|
LogMessage(logger, log_message.c_str());
|
2013-03-25 17:56:48 +00: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 03:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t AutoRollLoggerTest::RollLogFileByTimeTest(
|
2016-05-20 14:42:18 +00:00
|
|
|
AutoRollLogger* logger, size_t time, const std::string& log_message) {
|
2013-02-05 03:42:40 +00:00
|
|
|
uint64_t expected_create_time;
|
|
|
|
uint64_t actual_create_time;
|
|
|
|
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 03:52:32 +00:00
|
|
|
EXPECT_OK(env->GetFileSize(kLogFile, &total_log_size));
|
2013-02-05 03:42:40 +00:00
|
|
|
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 22:21:24 +00:00
|
|
|
LogMessage(logger, log_message.c_str());
|
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 03:52:32 +00:00
|
|
|
EXPECT_OK(logger->GetStatus());
|
2013-02-05 03:42:40 +00:00
|
|
|
// 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.
|
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 03:52:32 +00:00
|
|
|
EXPECT_EQ(expected_create_time, actual_create_time);
|
|
|
|
EXPECT_GT(logger->GetLogFileSize(), total_log_size);
|
2013-02-05 03:42:40 +00:00
|
|
|
total_log_size = logger->GetLogFileSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Make the log file expire
|
2015-07-01 23:13:49 +00:00
|
|
|
#ifdef OS_WIN
|
|
|
|
Sleep(static_cast<unsigned int>(time) * 1000);
|
|
|
|
#else
|
2014-11-11 21:47:22 +00:00
|
|
|
sleep(static_cast<unsigned int>(time));
|
2015-07-01 23:13:49 +00:00
|
|
|
#endif
|
2013-02-13 22:21:24 +00:00
|
|
|
LogMessage(logger, log_message.c_str());
|
2013-02-05 03:42:40 +00:00
|
|
|
|
|
|
|
// At this time, the new log file should be created.
|
|
|
|
GetFileCreateTime(kLogFile, &actual_create_time);
|
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 03:52:32 +00:00
|
|
|
EXPECT_GT(actual_create_time, expected_create_time);
|
|
|
|
EXPECT_LT(logger->GetLogFileSize(), total_log_size);
|
2013-02-05 03:42:40 +00:00
|
|
|
expected_create_time = actual_create_time;
|
|
|
|
|
|
|
|
return expected_create_time;
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(AutoRollLoggerTest, RollLogFileBySize) {
|
2013-02-19 08:09:16 +00:00
|
|
|
InitTestDb();
|
2013-02-13 22:21:24 +00:00
|
|
|
size_t log_max_size = 1024 * 5;
|
2013-02-05 03:42:40 +00:00
|
|
|
|
2013-03-08 20:29:19 +00:00
|
|
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_max_size, 0);
|
2013-02-05 03:42:40 +00:00
|
|
|
|
2013-03-08 20:29:19 +00:00
|
|
|
RollLogFileBySizeTest(&logger, log_max_size,
|
2013-02-05 03:42:40 +00:00
|
|
|
kSampleMessage + ":RollLogFileBySize");
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(AutoRollLoggerTest, RollLogFileByTime) {
|
2014-08-18 17:23:18 +00:00
|
|
|
size_t time = 2;
|
2013-02-05 03:42:40 +00:00
|
|
|
size_t log_size = 1024 * 5;
|
|
|
|
|
|
|
|
InitTestDb();
|
|
|
|
// -- Test the existence of file during the server restart.
|
2015-07-21 00:20:40 +00:00
|
|
|
ASSERT_EQ(Status::NotFound(), env->FileExists(kLogFile));
|
2014-08-18 17:23:18 +00:00
|
|
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_size, time);
|
2015-07-21 00:20:40 +00:00
|
|
|
ASSERT_OK(env->FileExists(kLogFile));
|
2013-02-05 03:42:40 +00:00
|
|
|
|
2013-03-08 20:29:19 +00:00
|
|
|
RollLogFileByTimeTest(&logger, time, kSampleMessage + ":RollLogFileByTime");
|
2013-02-05 03:42:40 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(AutoRollLoggerTest, OpenLogFilesMultipleTimesWithOptionLog_max_size) {
|
2013-02-05 03:42:40 +00:00
|
|
|
// If only 'log_max_size' options is specified, then every time
|
2013-10-05 05:32:05 +00:00
|
|
|
// when rocksdb is restarted, a new empty log file will be created.
|
2013-02-05 03:42:40 +00: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 22:21:24 +00:00
|
|
|
LogMessage(logger, kSampleMessage.c_str());
|
2013-02-05 03:42:40 +00: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;
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(AutoRollLoggerTest, CompositeRollByTimeAndSizeLogger) {
|
2014-08-18 17:23:18 +00:00
|
|
|
size_t time = 2, log_max_size = 1024 * 5;
|
2013-02-05 03:42:40 +00:00
|
|
|
|
|
|
|
InitTestDb();
|
|
|
|
|
2013-03-08 20:29:19 +00:00
|
|
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_max_size, time);
|
2013-02-05 03:42:40 +00:00
|
|
|
|
|
|
|
// Test the ability to roll by size
|
|
|
|
RollLogFileBySizeTest(
|
2013-03-08 20:29:19 +00:00
|
|
|
&logger, log_max_size,
|
2013-02-05 03:42:40 +00:00
|
|
|
kSampleMessage + ":CompositeRollByTimeAndSizeLogger");
|
|
|
|
|
|
|
|
// Test the ability to roll by Time
|
2013-03-08 20:29:19 +00:00
|
|
|
RollLogFileByTimeTest( &logger, time,
|
2013-02-05 03:42:40 +00:00
|
|
|
kSampleMessage + ":CompositeRollByTimeAndSizeLogger");
|
|
|
|
}
|
|
|
|
|
2015-07-01 23:13:49 +00:00
|
|
|
#ifndef OS_WIN
|
2015-07-13 19:11:05 +00:00
|
|
|
// TODO: does not build for Windows because of PosixLogger use below. Need to
|
|
|
|
// port
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(AutoRollLoggerTest, CreateLoggerFromOptions) {
|
2014-02-05 00:31:18 +00:00
|
|
|
DBOptions options;
|
2013-02-05 03:42:40 +00:00
|
|
|
shared_ptr<Logger> logger;
|
|
|
|
|
|
|
|
// Normal logger
|
2015-10-30 01:07:37 +00:00
|
|
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, options, &logger));
|
2013-02-13 22:21:24 +00:00
|
|
|
ASSERT_TRUE(dynamic_cast<PosixLogger*>(logger.get()));
|
2013-02-05 03:42:40 +00:00
|
|
|
|
|
|
|
// Only roll by size
|
|
|
|
InitTestDb();
|
|
|
|
options.max_log_file_size = 1024;
|
2015-10-30 01:07:37 +00:00
|
|
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, options, &logger));
|
2013-02-05 03:42:40 +00:00
|
|
|
AutoRollLogger* auto_roll_logger =
|
|
|
|
dynamic_cast<AutoRollLogger*>(logger.get());
|
2013-02-13 22:21:24 +00:00
|
|
|
ASSERT_TRUE(auto_roll_logger);
|
2013-02-05 03:42:40 +00: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 17:23:18 +00:00
|
|
|
options.log_file_time_to_roll = 2;
|
2015-10-30 01:07:37 +00:00
|
|
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, options, &logger));
|
2013-02-05 03:42:40 +00:00
|
|
|
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 17:23:18 +00:00
|
|
|
options.log_file_time_to_roll = 2;
|
2015-10-30 01:07:37 +00:00
|
|
|
ASSERT_OK(CreateLoggerFromOptions(kTestDir, options, &logger));
|
2013-02-05 03:42:40 +00:00
|
|
|
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");
|
|
|
|
}
|
2016-02-17 20:06:45 +00:00
|
|
|
|
|
|
|
TEST_F(AutoRollLoggerTest, LogFlushWhileRolling) {
|
|
|
|
DBOptions options;
|
|
|
|
shared_ptr<Logger> logger;
|
|
|
|
|
|
|
|
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);
|
|
|
|
std::thread flush_thread;
|
2016-02-23 05:32:19 +00:00
|
|
|
|
2016-07-07 18:35:40 +00: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).
|
|
|
|
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"}});
|
2016-02-17 20:06:45 +00:00
|
|
|
rocksdb::SyncPoint::GetInstance()->EnableProcessing();
|
2016-02-19 02:03:53 +00:00
|
|
|
|
2016-02-23 05:32:19 +00:00
|
|
|
flush_thread = std::thread([&]() { auto_roll_logger->Flush(); });
|
|
|
|
TEST_SYNC_POINT(
|
|
|
|
"AutoRollLoggerTest::LogFlushWhileRolling:PreRollAndPostThreadInit");
|
2016-02-17 20:06:45 +00:00
|
|
|
RollLogFileBySizeTest(auto_roll_logger, options.max_log_file_size,
|
|
|
|
kSampleMessage + ":LogFlushWhileRolling");
|
|
|
|
flush_thread.join();
|
|
|
|
rocksdb::SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // OS_WIN
|
2013-02-05 03:42:40 +00:00
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(AutoRollLoggerTest, InfoLogLevel) {
|
2014-02-26 22:41:28 +00:00
|
|
|
InitTestDb();
|
|
|
|
|
|
|
|
size_t log_size = 8192;
|
2014-02-28 06:15:30 +00:00
|
|
|
size_t log_lines = 0;
|
2014-02-27 22:00:10 +00: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);
|
2015-07-03 00:14:39 +00:00
|
|
|
for (int log_level = InfoLogLevel::HEADER_LEVEL;
|
2014-04-10 22:27:42 +00:00
|
|
|
log_level >= InfoLogLevel::DEBUG_LEVEL; log_level--) {
|
2014-02-27 22:00:10 +00:00
|
|
|
logger.SetInfoLogLevel((InfoLogLevel)log_level);
|
2014-04-10 22:27:42 +00:00
|
|
|
for (int log_type = InfoLogLevel::DEBUG_LEVEL;
|
2015-07-03 00:14:39 +00:00
|
|
|
log_type <= InfoLogLevel::HEADER_LEVEL; log_type++) {
|
2014-02-27 22:00:10 +00: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 00:14:39 +00:00
|
|
|
log_lines += InfoLogLevel::HEADER_LEVEL - log_level + 1;
|
2014-02-27 22:00:10 +00:00
|
|
|
}
|
2015-07-03 00:14:39 +00:00
|
|
|
for (int log_level = InfoLogLevel::HEADER_LEVEL;
|
2014-04-10 22:27:42 +00:00
|
|
|
log_level >= InfoLogLevel::DEBUG_LEVEL; log_level--) {
|
2014-02-27 22:00:10 +00:00
|
|
|
logger.SetInfoLogLevel((InfoLogLevel)log_level);
|
|
|
|
|
|
|
|
// again, messages with level smaller than log_level will not be logged.
|
2015-07-03 00:14:39 +00:00
|
|
|
Log(InfoLogLevel::HEADER_LEVEL, &logger, "%s", kSampleMessage.c_str());
|
2014-02-27 22:00:10 +00:00
|
|
|
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());
|
2015-07-03 00:14:39 +00:00
|
|
|
log_lines += InfoLogLevel::HEADER_LEVEL - log_level + 1;
|
2014-02-26 22:41:28 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-27 22:00:10 +00:00
|
|
|
std::ifstream inFile(AutoRollLoggerTest::kLogFile.c_str());
|
2014-02-28 06:15:30 +00:00
|
|
|
size_t lines = std::count(std::istreambuf_iterator<char>(inFile),
|
2014-02-27 22:00:10 +00:00
|
|
|
std::istreambuf_iterator<char>(), '\n');
|
|
|
|
ASSERT_EQ(log_lines, lines);
|
|
|
|
inFile.close();
|
2014-02-26 22:41:28 +00:00
|
|
|
}
|
|
|
|
|
2015-02-02 17:47:24 +00: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 14:42:18 +00:00
|
|
|
static std::vector<std::string> GetOldFileNames(const std::string& path) {
|
|
|
|
std::vector<std::string> ret;
|
2015-07-22 21:36:43 +00:00
|
|
|
|
2016-05-20 14:42:18 +00: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 19:59:39 +00:00
|
|
|
|
2016-05-20 14:42:18 +00:00
|
|
|
std::vector<std::string> children;
|
2015-02-23 19:59:39 +00:00
|
|
|
Env::Default()->GetChildren(dirname, &children);
|
|
|
|
|
|
|
|
// We know that the old log files are named [path]<something>
|
|
|
|
// Return all entities that match the pattern
|
2015-07-22 21:36:43 +00:00
|
|
|
for (auto& child : children) {
|
2015-02-23 19:59:39 +00:00
|
|
|
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
|
2016-05-20 14:42:18 +00:00
|
|
|
static size_t GetLinesCount(const std::string& fname,
|
|
|
|
const std::string& pattern) {
|
|
|
|
std::stringstream ssbuf;
|
|
|
|
std::string line;
|
2015-02-23 19:59:39 +00:00
|
|
|
size_t count = 0;
|
|
|
|
|
2016-05-20 14:42:18 +00:00
|
|
|
std::ifstream inFile(fname.c_str());
|
2015-02-23 19:59:39 +00:00
|
|
|
ssbuf << inFile.rdbuf();
|
|
|
|
|
|
|
|
while (getline(ssbuf, line)) {
|
|
|
|
if (line.find(pattern) != std::string::npos) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(AutoRollLoggerTest, LogHeaderTest) {
|
2015-02-02 17:47:24 +00: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 00:14:39 +00: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 17:47:24 +00:00
|
|
|
|
2015-07-03 00:14:39 +00:00
|
|
|
InitTestDb();
|
2015-02-02 17:47:24 +00:00
|
|
|
|
2015-07-03 00:14:39 +00:00
|
|
|
AutoRollLogger logger(Env::Default(), kTestDir, /*db_log_dir=*/ "",
|
|
|
|
LOG_MAX_SIZE, /*log_file_time_to_roll=*/ 0);
|
2015-02-23 19:59:39 +00:00
|
|
|
|
2015-07-03 00:14:39 +00:00
|
|
|
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);
|
|
|
|
}
|
2015-02-23 19:59:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 14:42:18 +00:00
|
|
|
const std::string newfname = logger.TEST_log_fname();
|
2015-07-03 00:14:39 +00: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 19:59:39 +00:00
|
|
|
|
2015-07-03 00:14:39 +00:00
|
|
|
Info(&logger, "Rollover");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the log for the latest file
|
|
|
|
LogFlush(&logger);
|
2015-02-02 17:47:24 +00:00
|
|
|
|
2015-07-22 21:36:43 +00:00
|
|
|
const auto oldfiles = GetOldFileNames(newfname);
|
2015-02-02 17:47:24 +00:00
|
|
|
|
2015-07-03 00:14:39 +00:00
|
|
|
ASSERT_EQ(oldfiles.size(), (size_t) 2);
|
2015-02-02 17:47:24 +00:00
|
|
|
|
2015-07-22 21:36:43 +00:00
|
|
|
for (auto& oldfname : oldfiles) {
|
2015-07-03 00:14:39 +00:00
|
|
|
// 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);
|
|
|
|
}
|
2015-02-02 17:47:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(AutoRollLoggerTest, LogFileExistence) {
|
2014-06-16 17:27:42 +00:00
|
|
|
rocksdb::DB* db;
|
|
|
|
rocksdb::Options options;
|
2016-04-15 00:33:53 +00: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 14:42:18 +00:00
|
|
|
std::string deleteCmd = "rm -rf " + kTestDir;
|
2016-04-15 00:33:53 +00:00
|
|
|
#endif
|
2014-06-16 17:27:42 +00:00
|
|
|
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));
|
2015-07-21 00:20:40 +00:00
|
|
|
ASSERT_OK(env->FileExists(kLogFile));
|
2014-06-16 17:27:42 +00:00
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|
2013-02-05 03:42:40 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2015-03-17 21:08:00 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2013-02-05 03:42:40 +00:00
|
|
|
}
|