YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/db/auto_roll_logger.h
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under the BSD-style license found in the
3
//  LICENSE file in the root directory of this source tree. An additional grant
4
//  of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
// Logger implementation that can be shared by all environments
21
// where enough posix functionality is available.
22
23
#ifndef YB_ROCKSDB_DB_AUTO_ROLL_LOGGER_H
24
#define YB_ROCKSDB_DB_AUTO_ROLL_LOGGER_H
25
26
#pragma once
27
28
#include <assert.h>
29
30
#include <cstdint>
31
#include <functional>
32
#include <list>
33
#include <mutex>
34
#include <stack>
35
#include <string>
36
#include <type_traits>
37
38
#include "yb/rocksdb/db/filename.h"
39
#include "yb/rocksdb/port/port.h"
40
#include "yb/rocksdb/port/util_logger.h"
41
#include "yb/rocksdb/util/mutexlock.h"
42
#include "yb/rocksdb/util/sync_point.h"
43
44
#include "yb/util/cache_metrics.h"
45
46
namespace rocksdb {
47
48
// Rolls the log file by size and/or time
49
class AutoRollLogger : public Logger {
50
 public:
51
  AutoRollLogger(Env* env, const std::string& dbname,
52
                 const std::string& db_log_dir, size_t log_max_size,
53
                 size_t log_file_time_to_roll,
54
                 const InfoLogLevel log_level = InfoLogLevel::INFO_LEVEL);
55
56
  using Logger::Logv;
57
  void Logv(const char* format, va_list ap) override;
58
59
  // Write a header entry to the log. All header information will be written
60
  // again every time the log rolls over.
61
  virtual void LogHeaderWithContext(const char* file, const int line,
62
      const char *format, va_list ap) override;
63
64
  // check if the logger has encountered any problem.
65
487
  Status GetStatus() {
66
487
    return status_;
67
487
  }
68
69
443
  size_t GetLogFileSize() const override {
70
443
    std::shared_ptr<Logger> logger;
71
443
    {
72
443
      MutexLock l(&mutex_);
73
      // pin down the current logger_ instance before releasing the mutex.
74
443
      logger = logger_;
75
443
    }
76
443
    return logger->GetLogFileSize();
77
443
  }
78
79
7
  void Flush() override {
80
7
    std::shared_ptr<Logger> logger;
81
7
    {
82
7
      MutexLock l(&mutex_);
83
      // pin down the current logger_ instance before releasing the mutex.
84
7
      logger = logger_;
85
7
    }
86
7
    TEST_SYNC_POINT("AutoRollLogger::Flush:PinnedLogger");
87
7
    if (logger) {
88
7
      logger->Flush();
89
7
    }
90
7
  }
91
92
13
  virtual ~AutoRollLogger() {
93
13
  }
94
95
  void SetCallNowMicrosEveryNRecords(uint64_t call_NowMicros_every_N_records) {
96
    call_NowMicros_every_N_records_ = call_NowMicros_every_N_records;
97
  }
98
99
  // Expose the log file path for testing purpose
100
  std::string TEST_log_fname() const {
101
    return log_fname_;
102
  }
103
104
 private:
105
  bool LogExpired();
106
  Status ResetLogger();
107
  void RollLogFile();
108
  // Log message to logger without rolling
109
  void LogInternal(const char* format, ...);
110
  // Serialize the va_list to a string
111
  std::string ValistToString(const char* format, va_list args) const;
112
  // Write the logs marked as headers to the new log file
113
  void WriteHeaderInfo();
114
115
  std::string log_fname_; // Current active info log's file name.
116
  std::string dbname_;
117
  std::string db_log_dir_;
118
  std::string db_absolute_path_;
119
  Env* env_;
120
  std::shared_ptr<Logger> logger_;
121
  // current status of the logger
122
  Status status_;
123
  const size_t kMaxLogFileSize;
124
  const size_t kLogFileTimeToRoll;
125
  // header information
126
  std::list<std::string> headers_;
127
  // to avoid frequent env->NowMicros() calls, we cached the current time
128
  uint64_t cached_now;
129
  uint64_t ctime_;
130
  uint64_t cached_now_access_count;
131
  uint64_t call_NowMicros_every_N_records_;
132
  mutable port::Mutex mutex_;
133
};
134
135
// Facade to craete logger automatically
136
Status CreateLoggerFromOptions(const std::string& dbname,
137
                               const DBOptions& options,
138
                               std::shared_ptr<Logger>* logger);
139
140
}  // namespace rocksdb
141
142
#endif // YB_ROCKSDB_DB_AUTO_ROLL_LOGGER_H