auto_roll_logger_test.cc 10.3 KB
Newer Older
1 2 3 4 5
//  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.
//
K
Kai Liu 已提交
6 7
#include <string>
#include <cmath>
8 9 10 11
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
K
Kai Liu 已提交
12 13
#include "util/testharness.h"
#include "util/auto_roll_logger.h"
14
#include "rocksdb/db.h"
K
Kai Liu 已提交
15 16 17 18 19
#include <sys/stat.h>
#include <errno.h>

using namespace std;

20
namespace rocksdb {
K
Kai Liu 已提交
21 22 23 24 25

class AutoRollLoggerTest {
 public:
  static void InitTestDb() {
    string deleteCmd = "rm -rf " + kTestDir;
26
    ASSERT_TRUE(system(deleteCmd.c_str()) == 0);
K
Kai Liu 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    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!!");
45 46
const string AutoRollLoggerTest::kTestDir(test::TmpDir() + "/db_log_test");
const string AutoRollLoggerTest::kLogFile(test::TmpDir() + "/db_log_test/LOG");
K
Kai Liu 已提交
47 48
Env* AutoRollLoggerTest::env = Env::Default();

49 50 51 52
// 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.
I
Igor Canadi 已提交
53
namespace {
54 55 56 57
void LogMessage(Logger* logger, const char* message) {
  Log(logger, "%s", message);
}

58 59 60 61
void LogMessage(const InfoLogLevel log_level, Logger* logger,
                const char* message) {
  Log(log_level, logger, "%s", message);
}
I
Igor Canadi 已提交
62
}  // namespace
63

I
Igor Canadi 已提交
64
namespace {
K
Kai Liu 已提交
65 66 67 68 69 70 71
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);
}
I
Igor Canadi 已提交
72
}  // namespace
K
Kai Liu 已提交
73 74 75 76

void AutoRollLoggerTest::RollLogFileBySizeTest(AutoRollLogger* logger,
                                               size_t log_max_size,
                                               const string& log_message) {
77
  logger->SetInfoLogLevel(InfoLogLevel::INFO_LEVEL);
K
Kai Liu 已提交
78 79
  // measure the size of each message, which is supposed
  // to be equal or greater than log_message.size()
80
  LogMessage(logger, log_message.c_str());
K
Kai Liu 已提交
81 82 83 84 85
  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) {
86
    LogMessage(logger, log_message.c_str());
K
Kai Liu 已提交
87 88 89 90 91
    current_log_size += message_size;
    ASSERT_EQ(current_log_size, logger->GetLogFileSize());
  }

  // Now the log file will be rolled
92
  LogMessage(logger, log_message.c_str());
93 94 95 96 97
  // 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());
K
Kai Liu 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111
}

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) {
112
     LogMessage(logger, log_message.c_str());
K
Kai Liu 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125
     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
  sleep(time);
126
  LogMessage(logger, log_message.c_str());
K
Kai Liu 已提交
127 128 129 130 131 132 133 134 135 136 137

  // 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) {
138
    InitTestDb();
139
    size_t log_max_size = 1024 * 5;
K
Kai Liu 已提交
140

141
    AutoRollLogger logger(Env::Default(), kTestDir, "", log_max_size, 0);
K
Kai Liu 已提交
142

143
    RollLogFileBySizeTest(&logger, log_max_size,
K
Kai Liu 已提交
144 145 146 147 148 149 150 151 152 153
                          kSampleMessage + ":RollLogFileBySize");
}

TEST(AutoRollLoggerTest, RollLogFileByTime) {
    size_t time = 1;
    size_t log_size = 1024 * 5;

    InitTestDb();
    // -- Test the existence of file during the server restart.
    ASSERT_TRUE(!env->FileExists(kLogFile));
154
    AutoRollLogger logger(Env::Default(), kTestDir, "", log_size, 1);
K
Kai Liu 已提交
155 156
    ASSERT_TRUE(env->FileExists(kLogFile));

157
    RollLogFileByTimeTest(&logger, time, kSampleMessage + ":RollLogFileByTime");
K
Kai Liu 已提交
158 159 160 161 162
}

TEST(AutoRollLoggerTest,
     OpenLogFilesMultipleTimesWithOptionLog_max_size) {
  // If only 'log_max_size' options is specified, then every time
163
  // when rocksdb is restarted, a new empty log file will be created.
K
Kai Liu 已提交
164 165 166 167 168 169 170 171 172 173 174
  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);

175
  LogMessage(logger, kSampleMessage.c_str());
K
Kai Liu 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  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) {
  size_t time = 1, log_max_size = 1024 * 5;

  InitTestDb();

191
  AutoRollLogger logger(Env::Default(), kTestDir, "", log_max_size, time);
K
Kai Liu 已提交
192 193 194

  // Test the ability to roll by size
  RollLogFileBySizeTest(
195
      &logger, log_max_size,
K
Kai Liu 已提交
196 197 198
      kSampleMessage + ":CompositeRollByTimeAndSizeLogger");

  // Test the ability to roll by Time
199
  RollLogFileByTimeTest( &logger, time,
K
Kai Liu 已提交
200 201 202 203
      kSampleMessage + ":CompositeRollByTimeAndSizeLogger");
}

TEST(AutoRollLoggerTest, CreateLoggerFromOptions) {
204
  DBOptions options;
K
Kai Liu 已提交
205 206 207 208
  shared_ptr<Logger> logger;

  // Normal logger
  ASSERT_OK(CreateLoggerFromOptions(kTestDir, "", env, options, &logger));
209
  ASSERT_TRUE(dynamic_cast<PosixLogger*>(logger.get()));
K
Kai Liu 已提交
210 211 212 213 214 215 216

  // 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());
217
  ASSERT_TRUE(auto_roll_logger);
K
Kai Liu 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  RollLogFileBySizeTest(
      auto_roll_logger, options.max_log_file_size,
      kSampleMessage + ":CreateLoggerFromOptions - size");

  // Only roll by Time
  InitTestDb();
  options.max_log_file_size = 0;
  options.log_file_time_to_roll = 1;
  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;
  options.log_file_time_to_roll = 1;
  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");
}

248 249 250 251
TEST(AutoRollLoggerTest, InfoLogLevel) {
  InitTestDb();

  size_t log_size = 8192;
252
  size_t log_lines = 0;
253 254 255 256
  // 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);
257 258
    for (int log_level = InfoLogLevel::FATAL_LEVEL;
         log_level >= InfoLogLevel::DEBUG_LEVEL; log_level--) {
259
      logger.SetInfoLogLevel((InfoLogLevel)log_level);
260 261
      for (int log_type = InfoLogLevel::DEBUG_LEVEL;
           log_type <= InfoLogLevel::FATAL_LEVEL; log_type++) {
262 263 264 265
        // log messages with log level smaller than log_level will not be
        // logged.
        LogMessage((InfoLogLevel)log_type, &logger, kSampleMessage.c_str());
      }
266
      log_lines += InfoLogLevel::FATAL_LEVEL - log_level + 1;
267
    }
268 269
    for (int log_level = InfoLogLevel::FATAL_LEVEL;
         log_level >= InfoLogLevel::DEBUG_LEVEL; log_level--) {
270 271 272 273 274 275 276 277
      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());
278
      log_lines += InfoLogLevel::FATAL_LEVEL - log_level + 1;
279 280
    }
  }
281
  std::ifstream inFile(AutoRollLoggerTest::kLogFile.c_str());
282
  size_t lines = std::count(std::istreambuf_iterator<char>(inFile),
283 284 285
                         std::istreambuf_iterator<char>(), '\n');
  ASSERT_EQ(log_lines, lines);
  inFile.close();
286 287
}

288 289 290 291 292 293 294 295 296 297 298 299
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;
}

300
}  // namespace rocksdb
K
Kai Liu 已提交
301 302

int main(int argc, char** argv) {
303
  return rocksdb::test::RunAllTests();
K
Kai Liu 已提交
304
}