/Users/deen/code/yugabyte-db/src/yb/master/yql_size_estimates_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_size_estimates_vtable.h" |
15 | | |
16 | | #include "yb/common/partition.h" |
17 | | #include "yb/common/schema.h" |
18 | | |
19 | | #include "yb/master/catalog_entity_info.h" |
20 | | #include "yb/master/catalog_manager_if.h" |
21 | | #include "yb/master/master_client.pb.h" |
22 | | |
23 | | #include "yb/util/status_log.h" |
24 | | #include "yb/util/yb_partition.h" |
25 | | |
26 | | namespace yb { |
27 | | namespace master { |
28 | | |
29 | | YQLSizeEstimatesVTable::YQLSizeEstimatesVTable(const TableName& table_name, |
30 | | const NamespaceName& namespace_name, |
31 | | Master * const master) |
32 | 3.00k | : YQLVirtualTable(table_name, namespace_name, master, CreateSchema()) { |
33 | 3.00k | } |
34 | | |
35 | | Result<std::shared_ptr<QLRowBlock>> YQLSizeEstimatesVTable::RetrieveData( |
36 | 22 | const QLReadRequestPB& request) const { |
37 | 22 | auto vtable = std::make_shared<QLRowBlock>(schema()); |
38 | 22 | auto* catalog_manager = &this->catalog_manager(); |
39 | | |
40 | 22 | auto tables = catalog_manager->GetTables(GetTablesMode::kVisibleToClient); |
41 | 402 | for (const auto& table : tables) { |
42 | 402 | Schema schema; |
43 | 402 | RETURN_NOT_OK(table->GetSchema(&schema)); |
44 | | |
45 | | // Get namespace for table. |
46 | 402 | auto ns_info = VERIFY_RESULT(catalog_manager->FindNamespaceById(table->namespace_id())); |
47 | | |
48 | | // Hide non-YQL tables. |
49 | 402 | if (table->GetTableType() != TableType::YQL_TABLE_TYPE) { |
50 | 0 | continue; |
51 | 0 | } |
52 | | |
53 | | // Get tablets for table. |
54 | 402 | auto tablets = table->GetTablets(); |
55 | 626 | for (const scoped_refptr<TabletInfo>& tablet : tablets) { |
56 | 626 | TabletLocationsPB tablet_locations_pb; |
57 | 626 | Status s = catalog_manager->GetTabletLocations(tablet->id(), &tablet_locations_pb); |
58 | | // Skip not-found tablets: they might not be running yet or have been deleted. |
59 | 626 | if (!s.ok()) { |
60 | 0 | continue; |
61 | 0 | } |
62 | | |
63 | 626 | QLRow &row = vtable->Extend(); |
64 | 626 | RETURN_NOT_OK(SetColumnValue(kKeyspaceName, ns_info->name(), &row)); |
65 | 626 | RETURN_NOT_OK(SetColumnValue(kTableName, table->name(), &row)); |
66 | | |
67 | 626 | const PartitionPB &partition = tablet_locations_pb.partition(); |
68 | 626 | uint16_t yb_start_hash = !partition.partition_key_start().empty() ? |
69 | 402 | PartitionSchema::DecodeMultiColumnHashValue(partition.partition_key_start())224 : 0; |
70 | 626 | string cql_start_hash = std::to_string(YBPartition::YBToCqlHashCode(yb_start_hash)); |
71 | 626 | RETURN_NOT_OK(SetColumnValue(kRangeStart, cql_start_hash, &row)); |
72 | | |
73 | 626 | uint16_t yb_end_hash = !partition.partition_key_end().empty() ? |
74 | 402 | PartitionSchema::DecodeMultiColumnHashValue(partition.partition_key_end())224 : 0; |
75 | 626 | string cql_end_hash = std::to_string(YBPartition::YBToCqlHashCode(yb_end_hash)); |
76 | 626 | RETURN_NOT_OK(SetColumnValue(kRangeEnd, cql_end_hash, &row)); |
77 | | |
78 | | // TODO: These values should eventually be reasonable estimates. |
79 | | // For now using 0 as defaults which should mean that clients will use their own defaults |
80 | | // (i.e. minimums) for number of splits -- typically one split per YugaByte tablet. |
81 | | |
82 | | // The estimated average size in bytes of all data for each partition (i.e. hash) key. |
83 | 626 | RETURN_NOT_OK(SetColumnValue(kMeanPartitionSize, 0, &row)); |
84 | | // The estimated number of partition (i.e. hash) keys in this tablet. |
85 | 626 | RETURN_NOT_OK(SetColumnValue(kPartitionsCount, 0, &row)); |
86 | 626 | } |
87 | 402 | } |
88 | | |
89 | 22 | return vtable; |
90 | 22 | } |
91 | | |
92 | 3.00k | Schema YQLSizeEstimatesVTable::CreateSchema() const { |
93 | 3.00k | SchemaBuilder builder; |
94 | 3.00k | CHECK_OK(builder.AddHashKeyColumn(kKeyspaceName, DataType::STRING)); |
95 | 3.00k | CHECK_OK(builder.AddKeyColumn(kTableName, DataType::STRING)); |
96 | 3.00k | CHECK_OK(builder.AddKeyColumn(kRangeStart, DataType::STRING)); |
97 | 3.00k | CHECK_OK(builder.AddKeyColumn(kRangeEnd, DataType::STRING)); |
98 | 3.00k | CHECK_OK(builder.AddColumn(kMeanPartitionSize, DataType::INT64)); |
99 | 3.00k | CHECK_OK(builder.AddColumn(kPartitionsCount, DataType::INT64)); |
100 | 3.00k | return builder.Build(); |
101 | 3.00k | } |
102 | | |
103 | | } // namespace master |
104 | | } // namespace yb |