YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/integration-tests/cluster_verifier.cc
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
#include "yb/integration-tests/cluster_verifier.h"
34
35
#include <atomic>
36
#include <memory>
37
#include <string>
38
#include <thread>
39
#include <vector>
40
41
#include <boost/range/iterator_range.hpp>
42
#include <gtest/gtest.h>
43
44
#include "yb/client/client.h"
45
#include "yb/client/table_handle.h"
46
47
#include "yb/gutil/strings/substitute.h"
48
49
#include "yb/integration-tests/mini_cluster_base.h"
50
51
#include "yb/tools/ysck_remote.h"
52
53
#include "yb/util/monotime.h"
54
#include "yb/util/result.h"
55
#include "yb/util/test_macros.h"
56
57
using std::string;
58
using std::vector;
59
60
namespace yb {
61
62
using strings::Substitute;
63
using tools::Ysck;
64
using tools::YsckCluster;
65
using tools::YsckMaster;
66
using tools::RemoteYsckMaster;
67
using client::YBTableName;
68
69
ClusterVerifier::ClusterVerifier(MiniClusterBase* cluster)
70
  : cluster_(cluster),
71
35
    checksum_options_(ChecksumOptions()) {
72
35
}
73
74
22
ClusterVerifier::~ClusterVerifier() {
75
22
}
76
77
0
void ClusterVerifier::SetVerificationTimeout(const MonoDelta& timeout) {
78
0
  checksum_options_.timeout = timeout;
79
0
}
80
81
0
void ClusterVerifier::SetScanConcurrency(int concurrency) {
82
0
  checksum_options_.scan_concurrency = concurrency;
83
0
}
84
85
35
void ClusterVerifier::CheckCluster() {
86
35
  MonoTime deadline = MonoTime::Now();
87
35
  deadline.AddDelta(checksum_options_.timeout);
88
89
35
  Status s;
90
35
  double sleep_time = 0.1;
91
1.24k
  while (MonoTime::Now().ComesBefore(deadline)) {
92
1.24k
    s = DoYsck();
93
1.24k
    if (s.ok()) {
94
29
      break;
95
29
    }
96
97
1.21k
    LOG(INFO) << "Check not successful yet, sleeping and retrying: " + s.ToString();
98
1.21k
    sleep_time *= 1.5;
99
1.21k
    if (sleep_time > 1) { sleep_time = 1; }
100
1.21k
    SleepFor(MonoDelta::FromSeconds(sleep_time));
101
1.21k
  }
102
35
  ASSERT_OK(s);
103
35
}
104
105
1.24k
Status ClusterVerifier::DoYsck() {
106
1.24k
  auto addr = VERIFY_RESULT(cluster_->GetLeaderMasterBoundRpcAddr());
107
108
1.24k
  std::shared_ptr<YsckMaster> master;
109
1.24k
  RETURN_NOT_OK(RemoteYsckMaster::Build(addr, &master));
110
1.24k
  std::shared_ptr<YsckCluster> cluster(new YsckCluster(master));
111
1.24k
  std::shared_ptr<Ysck> ysck(new Ysck(cluster));
112
113
  // This is required for everything below.
114
1.24k
  RETURN_NOT_OK(ysck->CheckMasterRunning());
115
1.24k
  RETURN_NOT_OK(ysck->FetchTableAndTabletInfo());
116
962
  RETURN_NOT_OK(ysck->CheckTabletServersRunning());
117
642
  RETURN_NOT_OK(ysck->CheckTablesConsistency());
118
119
634
  vector<string> tables;
120
634
  vector<string> tablets;
121
634
  RETURN_NOT_OK(ysck->ChecksumData(tables, tablets, checksum_options_));
122
35
  return Status::OK();
123
634
}
124
125
void ClusterVerifier::CheckRowCount(const YBTableName& table_name,
126
                                    ComparisonMode mode,
127
                                    size_t expected_row_count,
128
8
                                    YBConsistencyLevel consistency) {
129
8
  ASSERT_OK(DoCheckRowCount(table_name, mode, expected_row_count, consistency));
130
8
}
131
132
Status ClusterVerifier::DoCheckRowCount(const YBTableName& table_name,
133
                                        ComparisonMode mode,
134
                                        size_t expected_row_count,
135
9
                                        YBConsistencyLevel consistency) {
136
9
  auto client = VERIFY_RESULT_PREPEND(
137
9
      cluster_->CreateClient(), "Unable to connect to cluster");
138
139
9
  client::TableHandle table;
140
9
  RETURN_NOT_OK_PREPEND(table.Open(table_name, client.get()), "Unable to open table");
141
9
  client::TableIteratorOptions options;
142
9
  options.consistency = consistency;
143
9
  size_t count = boost::size(client::TableRange(table, options));
144
145
9
  if (mode == AT_LEAST && count < expected_row_count) {
146
0
    return STATUS(Corruption, Substitute("row count $0 is not at least expected value $1",
147
0
                                         count, expected_row_count));
148
9
  } else if (mode == EXACTLY && count != expected_row_count) {
149
0
    return STATUS(Corruption, Substitute("row count $0 is not exactly expected value $1",
150
0
                                         count, expected_row_count));
151
0
  }
152
9
  return Status::OK();
153
9
}
154
155
void ClusterVerifier::CheckRowCountWithRetries(const YBTableName& table_name,
156
                                               ComparisonMode mode,
157
                                               size_t expected_row_count,
158
1
                                               const MonoDelta& timeout) {
159
1
  MonoTime deadline = MonoTime::Now();
160
1
  deadline.AddDelta(timeout);
161
1
  Status s;
162
1
  while (true) {
163
1
    s = DoCheckRowCount(table_name, mode, expected_row_count, YBConsistencyLevel::STRONG);
164
1
    if (s.ok() || deadline.ComesBefore(MonoTime::Now())) break;
165
0
    LOG(WARNING) << "CheckRowCount() has not succeeded yet: " << s.ToString()
166
0
                 << "... will retry";
167
0
    SleepFor(MonoDelta::FromMilliseconds(100));
168
0
  }
169
170
1
  ASSERT_OK(s);
171
1
}
172
173
}  // namespace yb