YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/integration-tests/master_sysnamespace-itest.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 <yb/master/master_defaults.h>
15
16
#include "yb/integration-tests/mini_cluster.h"
17
18
#include "yb/common/value.pb.h"
19
20
#include "yb/master/master_client.proxy.h"
21
#include "yb/master/master_ddl.proxy.h"
22
#include "yb/master/mini_master.h"
23
24
#include "yb/rpc/messenger.h"
25
#include "yb/rpc/proxy.h"
26
#include "yb/rpc/rpc_controller.h"
27
28
#include "yb/util/result.h"
29
#include "yb/util/test_util.h"
30
31
namespace yb {
32
namespace master {
33
34
class MasterSysNamespaceTest : public YBTest {
35
 public:
36
1
  MasterSysNamespaceTest() {}
37
38
0
  ~MasterSysNamespaceTest() {}
39
40
 protected:
41
1
  void SetUp() override {
42
1
    YBTest::SetUp();
43
1
    MiniClusterOptions opts;
44
1
    opts.num_masters = 3;
45
1
    cluster_.reset(new MiniCluster(opts));
46
1
    ASSERT_OK(cluster_->Start());
47
0
    rpc::MessengerBuilder bld("Client");
48
0
    client_messenger_ = ASSERT_RESULT(bld.Build());
49
0
    rpc::ProxyCache proxy_cache(client_messenger_.get());
50
0
    auto host_port = ASSERT_RESULT(cluster_->GetLeaderMiniMaster())->bound_rpc_addr();
51
0
    proxy_ddl_ = std::make_unique<MasterDdlProxy>(&proxy_cache, host_port);
52
0
    proxy_client_ = std::make_unique<MasterClientProxy>(&proxy_cache, host_port);
53
0
  }
54
55
0
  void TearDown() override {
56
0
    client_messenger_->Shutdown();
57
0
    if (cluster_) {
58
0
      cluster_->Shutdown();
59
0
      cluster_.reset();
60
0
    }
61
62
0
    YBTest::TearDown();
63
0
  }
64
65
0
  void VerifyTabletLocations(const TabletLocationsPB& locs_pb) {
66
0
    ASSERT_FALSE(locs_pb.stale());
67
0
    ASSERT_EQ(3, locs_pb.replicas_size());
68
0
    for (const TabletLocationsPB::ReplicaPB& replica : locs_pb.replicas()) {
69
0
      if (replica.role() == PeerRole::LEADER) {
70
0
        auto* leader_mini_master = ASSERT_RESULT(cluster_->GetLeaderMiniMaster());
71
0
        ASSERT_EQ(
72
0
            leader_mini_master->bound_rpc_addr().host(),
73
0
            replica.ts_info().private_rpc_addresses(0).host());
74
0
        ASSERT_EQ(
75
0
            leader_mini_master->bound_rpc_addr().port(),
76
0
            replica.ts_info().private_rpc_addresses(0).port());
77
0
        ASSERT_EQ(leader_mini_master->permanent_uuid(), replica.ts_info().permanent_uuid());
78
0
      } else {
79
        // Search for appropriate master.
80
0
        size_t i;
81
0
        for (i = 0; i < cluster_->num_masters(); i++) {
82
0
          if (cluster_->mini_master(i)->permanent_uuid() == replica.ts_info().permanent_uuid()) {
83
0
            ASSERT_EQ(cluster_->mini_master(i)->bound_rpc_addr().host(),
84
0
                      replica.ts_info().private_rpc_addresses(0).host());
85
0
            ASSERT_EQ(cluster_->mini_master(i)->bound_rpc_addr().port(),
86
0
                      replica.ts_info().private_rpc_addresses(0).port());
87
0
            ASSERT_EQ(PeerRole::FOLLOWER, replica.role());
88
0
            break;
89
0
          }
90
0
        }
91
0
        ASSERT_FALSE(i == cluster_->num_masters());
92
0
      }
93
0
    }
94
0
  }
95
96
  void ValidateColumn(ColumnSchemaPB col_schema, string name, bool is_key, DataType type) {
97
    ASSERT_EQ(name, col_schema.name());
98
    ASSERT_EQ(is_key, col_schema.is_key());
99
    ASSERT_EQ(type, col_schema.type().main());
100
  }
101
102
  std::unique_ptr<MiniCluster> cluster_;
103
  std::unique_ptr<MasterClientProxy> proxy_client_;
104
  std::unique_ptr<MasterDdlProxy> proxy_ddl_;
105
  std::unique_ptr<rpc::Messenger> client_messenger_;
106
};
107
108
0
TEST_F(MasterSysNamespaceTest, TestSysNamespace) {
109
  // Test GetTableLocations.
110
0
  GetTableLocationsRequestPB req;
111
0
  GetTableLocationsResponsePB resp;
112
0
  TableIdentifierPB* table_identifier = req.mutable_table();
113
0
  table_identifier->set_table_name(master::kSystemPeersTableName);
114
0
  NamespaceIdentifierPB* namespace_identifier = table_identifier->mutable_namespace_();
115
0
  namespace_identifier->set_name(master::kSystemNamespaceName);
116
0
  namespace_identifier->set_id(master::kSystemNamespaceId);
117
0
  std::unique_ptr<rpc::RpcController> controller(new rpc::RpcController());
118
0
  ASSERT_OK(proxy_client_->GetTableLocations(req, &resp, controller.get()));
119
120
0
  ASSERT_FALSE(resp.has_error());
121
0
  ASSERT_EQ(TableType::YQL_TABLE_TYPE, resp.table_type());
122
0
  ASSERT_EQ(1, resp.tablet_locations_size());
123
0
  VerifyTabletLocations(resp.tablet_locations(0));
124
125
  // Test GetTabletLocations.
126
0
  GetTabletLocationsRequestPB tablet_req;
127
0
  GetTabletLocationsResponsePB tablet_resp;
128
0
  tablet_req.add_tablet_ids(resp.tablet_locations(0).tablet_id());
129
0
  controller->Reset();
130
0
  ASSERT_OK(proxy_client_->GetTabletLocations(tablet_req, &tablet_resp, controller.get()));
131
0
  ASSERT_FALSE(tablet_resp.has_error());
132
0
  ASSERT_EQ(1, tablet_resp.tablet_locations_size());
133
0
  VerifyTabletLocations(tablet_resp.tablet_locations(0));
134
135
  // Test GetTableSchema.
136
0
  GetTableSchemaRequestPB schema_req;
137
0
  GetTableSchemaResponsePB schema_resp;
138
0
  controller->Reset();
139
0
  *schema_req.mutable_table() = *table_identifier;
140
0
  ASSERT_OK(proxy_ddl_->GetTableSchema(schema_req, &schema_resp, controller.get()));
141
0
  ASSERT_FALSE(schema_resp.has_error());
142
0
  ASSERT_TRUE(schema_resp.create_table_done());
143
144
  // Validate schema.
145
0
  SchemaPB schema_pb = schema_resp.schema();
146
0
  ASSERT_EQ(9, schema_pb.columns_size());
147
0
  ValidateColumn(schema_pb.columns(0), "peer", /* is_key */ true, DataType::INET);
148
0
  ValidateColumn(schema_pb.columns(1), "data_center", /* is_key */ false, DataType::STRING);
149
0
  ValidateColumn(schema_pb.columns(2), "host_id", /* is_key */ false, DataType::UUID);
150
0
  ValidateColumn(schema_pb.columns(3), "preferred_ip", /* is_key */ false, DataType::INET);
151
0
  ValidateColumn(schema_pb.columns(4), "rack", /* is_key */ false, DataType::STRING);
152
0
  ValidateColumn(schema_pb.columns(5), "release_version", /* is_key */ false, DataType::STRING);
153
0
  ValidateColumn(schema_pb.columns(6), "rpc_address", /* is_key */ false, DataType::INET);
154
0
  ValidateColumn(schema_pb.columns(7), "schema_version", /* is_key */ false, DataType::UUID);
155
0
  ValidateColumn(schema_pb.columns(8), "tokens", /* is_key */ false, DataType::SET);
156
0
}
157
158
} // namespace master
159
} // namespace yb