YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/integration-tests/log_version-test.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#include <fstream>
15
#include <regex>
16
#include <vector>
17
18
#include <boost/algorithm/string/predicate.hpp>
19
#include <glog/logging.h>
20
21
#include "yb/client/client.h"
22
23
#include "yb/integration-tests/external_mini_cluster-itest-base.h"
24
#include "yb/integration-tests/external_mini_cluster.h"
25
26
#include "yb/master/master_ddl.proxy.h"
27
28
#include "yb/rpc/rpc_controller.h"
29
30
#include "yb/util/path_util.h"
31
#include "yb/util/size_literals.h"
32
#include "yb/util/string_trim.h"
33
#include "yb/util/subprocess.h"
34
#include "yb/util/test_util.h"
35
36
using std::string;
37
38
namespace yb {
39
namespace test {
40
namespace {
41
42
const string kDurationPrefix("Running duration (h:mm:ss): ");
43
44
class LogHeader {
45
 public:
46
2
  explicit LogHeader(const string& file_path) {
47
2
    std::ifstream log_stream(file_path);
48
2
    string line;
49
22
    for(int i = 0; i < 10 && getline(log_stream, line); ++i) {
50
20
      lines_.push_back(std::move(line));
51
20
    }
52
2
  }
53
54
4
  const string& GetByPrefix(const string& prefix) const {
55
4
    static const std::string empty;
56
14
    for(const auto& l : lines_) {
57
14
      if(boost::algorithm::starts_with(l, prefix)) {
58
4
        return l;
59
4
      }
60
14
    }
61
0
    return empty;
62
4
  }
63
64
 private:
65
  std::vector<string> lines_;
66
};
67
68
} // namespace
69
70
class LogRollingTest : public ExternalMiniClusterITestBase {
71
 public:
72
1
  void SetUpCluster(ExternalMiniClusterOptions* opts) override {
73
1
    ExternalMiniClusterITestBase::SetUpCluster(opts);
74
1
    opts->log_to_file = true;
75
1
  }
76
};
77
78
1
TEST_F(LogRollingTest, Rolling) {
79
  // Set minimal possible size limit for file rolling
80
1
  StartCluster({}, {"--max_log_size=1"}, 1);
81
1
  auto master = cluster_->master();
82
1
  const auto exe = master->exe();
83
1
  string version;
84
1
  ASSERT_OK(Subprocess::Call({exe, "--version"}, &version));
85
1
  version = util::TrimStr(version);
86
1
  ASSERT_TRUE(std::regex_match(
87
1
      version, std::regex(R"(version \S+ build \S+ revision \S+ build_type \S+ built at .+)")));
88
1
  const auto log_path = JoinPathSegments(master->GetDataDirs()[0], "logs", BaseName(exe) + ".INFO");
89
1
  const auto fingerprint = "Application fingerprint: " + version;
90
1
  const LogHeader header(log_path);
91
1
  ASSERT_NE(header.GetByPrefix(fingerprint), "");
92
1
  const auto& initial_duration = header.GetByPrefix(kDurationPrefix);
93
1
  ASSERT_NE(initial_duration, "");
94
  // In case of log rolling log_path link will be pointed to newly created file
95
1
  const auto initial_target = ASSERT_RESULT(env_->ReadLink(log_path));
96
1
  auto prev_size = ASSERT_RESULT(env_->GetFileSize(log_path));
97
1
  auto master_proxy = cluster_->GetMasterProxy<master::MasterDdlProxy>();
98
443
  while(initial_target == ASSERT_RESULT(env_->ReadLink(log_path))) {
99
    // Call rpc functions to generate logs in master
100
9.28k
    for(int i = 0; i < 20; ++i) {
101
8.84k
      master::TruncateTableRequestPB req;
102
8.84k
      master::TruncateTableResponsePB resp;
103
8.84k
      rpc::RpcController rpc;
104
8.84k
      ASSERT_OK(master_proxy.TruncateTable(req, &resp, &rpc));
105
8.84k
    }
106
442
    const auto current_size = ASSERT_RESULT(env_->GetFileSize(log_path));
107
    // Make sure log size is changed and it is not much than 2Mb
108
    // Something goes wrong in other case
109
442
    ASSERT_NE(current_size, prev_size);
110
442
    ASSERT_LT(current_size, 2_MB);
111
442
    prev_size = current_size;
112
442
  }
113
1
  const LogHeader fresh_header(log_path);
114
1
  ASSERT_NE(fresh_header.GetByPrefix(fingerprint), "");
115
1
  const auto& duration = fresh_header.GetByPrefix(kDurationPrefix);
116
1
  ASSERT_NE(duration, "");
117
1
  ASSERT_NE(duration, initial_duration);
118
1
}
119
120
} // namespace test
121
} // namespace yb