YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/integration-tests/yb_table_test_base.h
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
#ifndef YB_INTEGRATION_TESTS_YB_TABLE_TEST_BASE_H_
15
#define YB_INTEGRATION_TESTS_YB_TABLE_TEST_BASE_H_
16
17
#include <atomic>
18
#include <cmath>
19
#include <cstdlib>
20
#include <future>
21
22
#include <gflags/gflags.h>
23
#include <glog/logging.h>
24
25
#include "yb/client/schema.h"
26
#include "yb/client/table_handle.h"
27
28
#include "yb/gutil/ref_counted.h"
29
#include "yb/gutil/strings/substitute.h"
30
31
#include "yb/integration-tests/external_mini_cluster.h"
32
#include "yb/integration-tests/mini_cluster.h"
33
34
#include "yb/master/mini_master.h"
35
36
#include "yb/tools/tools_fwd.h"
37
38
#include "yb/util/random.h"
39
#include "yb/util/random_util.h"
40
#include "yb/util/subprocess.h"
41
#include "yb/util/test_macros.h"
42
#include "yb/util/test_util.h"
43
44
namespace yb {
45
namespace integration_tests {
46
47
// This is a common base class that SQLTableTest and RedisTableTest inherit from.
48
// In future some of the functionality may be migrated to sub-base classes when it becomes bigger.
49
// i.e. scan related functions may be moved down because it is only supported for SQL tables.
50
class YBTableTestBase : public YBTest {
51
 protected:
52
  YBTableTestBase();
53
  ~YBTableTestBase();
54
55
  void SetUp() override;
56
  void TearDown() override;
57
58
  virtual void BeforeCreateTable();
59
  virtual void BeforeStartCluster();
60
61
  virtual bool use_external_mini_cluster();
62
  virtual bool use_yb_admin_client();
63
  virtual int session_timeout_ms();
64
  virtual size_t num_masters();
65
  virtual size_t num_tablet_servers();
66
  virtual int num_drives();
67
  virtual int num_tablets();
68
  virtual int client_rpc_timeout_ms();
69
  virtual client::YBTableName table_name();
70
  virtual bool need_redis_table();
71
  virtual bool enable_ysql();
72
73
  void CreateRedisTable(const client::YBTableName& table_name);
74
  virtual void CreateTable();
75
  void OpenTable();
76
  virtual void DeleteTable();
77
  virtual void PutKeyValue(yb::client::YBSession* session, string key, string value);
78
  virtual void PutKeyValue(string key, string value);
79
  void RestartCluster();
80
  std::vector<std::pair<std::string, std::string>> GetScanResults(const client::TableRange& range);
81
  void FetchTSMetricsPage();
82
  void WaitForLoadBalanceCompletion(
83
      yb::MonoDelta timeout = MonoDelta::FromMilliseconds(kDefaultLoadBalanceTimeoutMs));
84
85
  // These utility functions only work with external_mini_cluster_.
86
  template <class T>
87
75
  T GetMasterLeaderProxy() {
88
75
    DCHECK(use_external_mini_cluster());
89
75
    return external_mini_cluster_->GetLeaderMasterProxy<T>();
90
75
  }
91
92
  // Calls GetLoadOnTserver to get loads for the provided tservers.
93
  Result<std::vector<uint32_t>> GetTserverLoads(const std::vector<int>& ts_idxs);
94
  Result<uint32_t> GetLoadOnTserver(ExternalTabletServer* server);
95
96
  client::TableHandle table_;
97
  std::unique_ptr<client::YBClient> client_;
98
  std::unique_ptr<tools::enterprise::ClusterAdminClient> yb_admin_client_;
99
  bool table_exists_ = false;
100
101
  yb::MiniCluster* mini_cluster() {
102
    assert(!use_external_mini_cluster());
103
    return mini_cluster_.get();
104
  }
105
106
  yb::ExternalMiniCluster* external_mini_cluster() {
107
    assert(use_external_mini_cluster());
108
    return external_mini_cluster_.get();
109
  }
110
111
14
  virtual void CustomizeExternalMiniCluster(ExternalMiniClusterOptions* opts) {}
112
113
0
  vector<string> master_rpc_addresses_as_strings() {
114
0
    vector<string> host_ports;
115
0
    size_t num_masters = use_external_mini_cluster() ? external_mini_cluster()->num_masters()
116
0
                                                     : mini_cluster()->num_masters();
117
0
    for (size_t i = 0; i < num_masters; i++) {
118
0
      auto sock_addr = use_external_mini_cluster()
119
0
                           ? external_mini_cluster()->master(i)->bound_rpc_addr()
120
0
                           : mini_cluster()->mini_master(i)->bound_rpc_addr();
121
0
      host_ports.push_back(ToString(sock_addr));
122
0
    }
123
0
    return host_ports;
124
0
  }
125
126
  // This sets up common options for creating all tables the test needs to create.
127
  virtual std::unique_ptr<client::YBTableCreator> NewTableCreator();
128
129
  static constexpr int kDefaultNumMasters = 1;
130
  static constexpr int kDefaultNumTabletServers = 3;
131
  static constexpr int kDefaultNumDrives = 1;
132
  static constexpr int kDefaultSessionTimeoutMs = 60000;
133
  static constexpr int kDefaultClientRpcTimeoutMs = 30000;
134
  static constexpr int kDefaultLoadBalanceTimeoutMs = 60000;
135
  static constexpr bool kDefaultUsingExternalMiniCluster = false;
136
  static constexpr bool kDefaultEnableYSQL = true;
137
  static const client::YBTableName kDefaultTableName;
138
139
  // Set custom Env and rocksdb::Env to be used by MiniTabletServer, otherwise MiniTabletServer
140
  // will use own Env and rocksdb::Env.
141
  std::unique_ptr<Env> ts_env_;
142
  std::unique_ptr<rocksdb::Env> ts_rocksdb_env_;
143
144
  vector<uint16_t> master_rpc_ports();
145
  // Calls CreateYBClient and assigns it to local class field.
146
  void CreateClient();
147
  // Creates a ClientYB client without assigning it to the class field.
148
  std::unique_ptr<yb::client::YBClient> CreateYBClient();
149
  void CreateAdminClient();
150
151
  std::shared_ptr<yb::client::YBSession> NewSession();
152
153
  yb::client::YBSchema schema_;
154
  std::shared_ptr<yb::client::YBSession> session_;
155
156
  // Exactly one of the following two pointers will be set.
157
  std::unique_ptr<yb::MiniCluster> mini_cluster_;
158
  std::unique_ptr<yb::ExternalMiniCluster> external_mini_cluster_;
159
160
  // All the default tables that are pre-created. Used to skip the initial create table step, when
161
  // the given table has been already pre-created.
162
  vector<string> default_tables_created_;
163
164
  // For tests that use multiple tables, store the tables here.
165
  // For tests with a single table, this is equivalent to table_name().
166
  vector<client::YBTableName> table_names_;
167
};
168
169
}  // namespace integration_tests
170
}  // namespace yb
171
#endif  // YB_INTEGRATION_TESTS_YB_TABLE_TEST_BASE_H_