YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/master/yql_local_vtable.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/yql_local_vtable.h"
15
16
#include "yb/common/ql_protocol.pb.h"
17
#include "yb/common/ql_type.h"
18
#include "yb/common/schema.h"
19
20
#include "yb/master/master.h"
21
#include "yb/master/master_heartbeat.pb.h"
22
#include "yb/master/ts_descriptor.h"
23
24
#include "yb/rpc/messenger.h"
25
26
#include "yb/util/net/dns_resolver.h"
27
#include "yb/util/net/inetaddress.h"
28
#include "yb/util/status_log.h"
29
30
namespace yb {
31
namespace master {
32
33
namespace {
34
35
const std::string kSystemLocalKeyColumn = "key";
36
const std::string kSystemLocalBootstrappedColumn = "bootstrapped";
37
const std::string kSystemLocalBroadcastAddressColumn = "broadcast_address";
38
const std::string kSystemLocalClusterNameColumn = "cluster_name";
39
const std::string kSystemLocalCQLVersionColumn = "cql_version";
40
const std::string kSystemLocalDataCenterColumn = "data_center";
41
const std::string kSystemLocalGossipGenerationColumn = "gossip_generation";
42
const std::string kSystemLocalHostIdColumn = "host_id";
43
const std::string kSystemLocalListenAddressColumn = "listen_address";
44
const std::string kSystemLocalNativeProtocolVersionColumn =
45
    "native_protocol_version";
46
const std::string kSystemLocalPartitionerColumn = "partitioner";
47
const std::string kSystemLocalRackColumn = "rack";
48
const std::string kSystemLocalRpcAddressColumn = "rpc_address";
49
const std::string kSystemLocalSchemaVersionColumn = "schema_version";
50
const std::string kSystemLocalThriftVersionColumn = "thrift_version";
51
const std::string kSystemLocalTokensColumn = "tokens";
52
const std::string kSystemLocalTruncatedAtColumn = "truncated_at";
53
54
} // namespace
55
56
LocalVTable::LocalVTable(const TableName& table_name,
57
                         const NamespaceName& namespace_name,
58
                         Master* const master)
59
2.00k
    : YQLVirtualTable(table_name, namespace_name, master, CreateSchema()) {
60
2.00k
}
61
62
Result<std::shared_ptr<QLRowBlock>> LocalVTable::RetrieveData(
63
70.3k
    const QLReadRequestPB& request) const {
64
70.3k
  vector<std::shared_ptr<TSDescriptor> > descs;
65
70.3k
  GetSortedLiveDescriptors(&descs);
66
70.3k
  auto vtable = std::make_shared<QLRowBlock>(schema());
67
68
70.3k
  struct Entry {
69
70.3k
    size_t index;
70
70.3k
    TSInformationPB ts_info;
71
70.3k
    util::PublicPrivateIPFutures ips;
72
70.3k
  };
73
74
70.3k
  std::vector<Entry> entries;
75
70.3k
  entries.reserve(descs.size());
76
77
70.3k
  InetAddress remote_ip;
78
79
70.3k
  size_t index = 0;
80
212k
  for (const std::shared_ptr<TSDescriptor>& desc : descs) {
81
212k
    ++index;
82
83
    // This is thread safe since all operations are reads.
84
212k
    TSInformationPB ts_info = *desc->GetTSInformationPB();
85
86
    // The system.local table contains only a single entry for the host that we are connected
87
    // to and hence we need to look for the 'remote_endpoint' here.
88
212k
    if (!request.proxy_uuid().empty()) {
89
212k
      if (desc->permanent_uuid() != request.proxy_uuid()) {
90
142k
        continue;
91
142k
      }
92
18.4E
    } else {
93
18.4E
      if (index == 1) {
94
0
        remote_ip = InetAddress(VERIFY_RESULT(master_->messenger()->resolver().Resolve(
95
0
            request.remote_endpoint().host())));
96
0
      }
97
18.4E
      if (!util::RemoteEndpointMatchesTServer(ts_info, remote_ip)) {
98
0
        continue;
99
0
      }
100
69.8k
    }
101
102
69.8k
    entries.push_back({index - 1, std::move(ts_info)});
103
69.8k
    entries.back().ips = util::GetPublicPrivateIPFutures(
104
69.8k
        entries.back().ts_info, &master_->messenger()->resolver());
105
69.8k
  }
106
107
70.3k
  for (const auto& entry : entries) {
108
70.3k
    QLRow& row = vtable->Extend();
109
70.3k
    InetAddress private_ip(VERIFY_RESULT(Copy(entry.ips.private_ip_future.get())));
110
70.3k
    InetAddress public_ip(VERIFY_RESULT(Copy(entry.ips.public_ip_future.get())));
111
70.3k
    const CloudInfoPB& cloud_info = entry.ts_info.registration().common().cloud_info();
112
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalKeyColumn, "local", &row));
113
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalBootstrappedColumn, "COMPLETED", &row));
114
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalBroadcastAddressColumn, public_ip, &row));
115
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalClusterNameColumn, "local cluster", &row));
116
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalCQLVersionColumn, "3.4.2", &row));
117
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalDataCenterColumn, cloud_info.placement_region(),
118
70.3k
                                 &row));
119
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalGossipGenerationColumn, 0, &row));
120
70.3k
    Uuid host_id;
121
70.3k
    RETURN_NOT_OK(host_id.FromHexString(entry.ts_info.tserver_instance().permanent_uuid()));
122
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalHostIdColumn, host_id, &row));
123
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalListenAddressColumn, private_ip, &row));
124
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalNativeProtocolVersionColumn, "4", &row));
125
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalPartitionerColumn,
126
70.3k
                                 "org.apache.cassandra.dht.Murmur3Partitioner", &row));
127
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalRackColumn, cloud_info.placement_zone(), &row));
128
70.3k
    RETURN_NOT_OK(SetColumnValue(yb::master::kSystemTablesReleaseVersionColumn,
129
70.3k
                                yb::master::kSystemTablesReleaseVersion, &row));
130
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalRpcAddressColumn, public_ip, &row));
131
132
70.3k
    Uuid schema_version = VERIFY_RESULT(Uuid::FromString(master::kDefaultSchemaVersion));
133
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalSchemaVersionColumn, schema_version, &row));
134
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalThriftVersionColumn, "20.1.0", &row));
135
    // setting tokens
136
70.3k
    RETURN_NOT_OK(SetColumnValue(kSystemLocalTokensColumn,
137
70.3k
                                 util::GetTokensValue(entry.index, descs.size()), &row));
138
70.3k
    break;
139
70.3k
  }
140
141
70.3k
  return vtable;
142
70.3k
}
143
144
2.00k
Schema LocalVTable::CreateSchema() const {
145
2.00k
  SchemaBuilder builder;
146
2.00k
  CHECK_OK(builder.AddHashKeyColumn(kSystemLocalKeyColumn, QLType::Create(DataType::STRING)));
147
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalBootstrappedColumn, QLType::Create(DataType::STRING)));
148
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalBroadcastAddressColumn, QLType::Create(DataType::INET)));
149
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalClusterNameColumn, QLType::Create(DataType::STRING)));
150
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalCQLVersionColumn, QLType::Create(DataType::STRING)));
151
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalDataCenterColumn, QLType::Create(DataType::STRING)));
152
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalGossipGenerationColumn, QLType::Create(DataType::INT32)));
153
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalHostIdColumn, QLType::Create(DataType::UUID)));
154
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalListenAddressColumn, QLType::Create(DataType::INET)));
155
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalNativeProtocolVersionColumn,
156
2.00k
                             QLType::Create(DataType::STRING)));
157
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalPartitionerColumn, QLType::Create(DataType::STRING)));
158
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalRackColumn, QLType::Create(DataType::STRING)));
159
2.00k
  CHECK_OK(builder.AddColumn(yb::master::kSystemTablesReleaseVersionColumn,
160
2.00k
                            QLType::Create(DataType::STRING)));
161
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalRpcAddressColumn, QLType::Create(DataType::INET)));
162
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalSchemaVersionColumn, QLType::Create(DataType::UUID)));
163
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalThriftVersionColumn, QLType::Create(DataType::STRING)));
164
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalTokensColumn, QLType::CreateTypeSet(DataType::STRING)));
165
2.00k
  CHECK_OK(builder.AddColumn(kSystemLocalTruncatedAtColumn,
166
2.00k
                             QLType::CreateTypeMap(DataType::UUID, DataType::BINARY)));
167
2.00k
  return builder.Build();
168
2.00k
}
169
170
}  // namespace master
171
}  // namespace yb