/Users/deen/code/yugabyte-db/src/yb/master/yql_virtual_table.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_virtual_table.h" |
15 | | |
16 | | #include "yb/common/ql_scanspec.h" |
17 | | #include "yb/common/schema.h" |
18 | | |
19 | | #include "yb/master/master.h" |
20 | | #include "yb/master/scoped_leader_shared_lock.h" |
21 | | #include "yb/master/ts_manager.h" |
22 | | #include "yb/master/yql_vtable_iterator.h" |
23 | | |
24 | | #include "yb/util/metrics.h" |
25 | | #include "yb/util/shared_lock.h" |
26 | | #include "yb/util/status_format.h" |
27 | | |
28 | | namespace yb { |
29 | | namespace master { |
30 | | |
31 | | namespace { |
32 | | static const char* const kBaseMetricDescription = "Time spent querying YCQL system table: "; |
33 | | static const char* const kMetricPrefixName = "ycql_queries_"; |
34 | | } |
35 | | |
36 | | YQLVirtualTable::YQLVirtualTable(const TableName& table_name, |
37 | | const NamespaceName &namespace_name, |
38 | | const Master* const master, |
39 | | const Schema& schema) |
40 | | : master_(master), |
41 | | table_name_(table_name), |
42 | 48.1k | schema_(std::make_unique<Schema>(schema)) { |
43 | 48.1k | std::string metricDescription = kBaseMetricDescription + table_name_; |
44 | 48.1k | std::string metricName = kMetricPrefixName + namespace_name + "_" + table_name; |
45 | 48.1k | EscapeMetricNameForPrometheus(&metricName); |
46 | | |
47 | 48.1k | std::unique_ptr<HistogramPrototype> prototype = std::make_unique<OwningHistogramPrototype>( |
48 | 48.1k | "server", metricName, metricDescription, |
49 | 48.1k | MetricUnit::kMicroseconds, metricDescription, |
50 | 48.1k | MetricLevel::kInfo, 0, 10000000, 2); |
51 | 48.1k | histogram_ = master->metric_entity()->FindOrCreateHistogram(std::move(prototype)); |
52 | 48.1k | } |
53 | | |
54 | 1.55k | YQLVirtualTable::~YQLVirtualTable() = default; |
55 | | |
56 | | CHECKED_STATUS YQLVirtualTable::GetIterator( |
57 | | const QLReadRequestPB& request, |
58 | | const Schema& projection, |
59 | | const Schema& schema, |
60 | | const TransactionOperationContext& txn_op_context, |
61 | | CoarseTimePoint deadline, |
62 | | const ReadHybridTime& read_time, |
63 | | const QLScanSpec& spec, |
64 | 260k | std::unique_ptr<docdb::YQLRowwiseIteratorIf>* iter) const { |
65 | | // Acquire shared lock on catalog manager to verify it is still the leader and metadata will |
66 | | // not change. |
67 | 260k | SCOPED_LEADER_SHARED_LOCK(l, master_->catalog_manager_impl()); |
68 | 260k | RETURN_NOT_OK(l.first_failed_status()); |
69 | | |
70 | 260k | MonoTime start_time = MonoTime::Now(); |
71 | 260k | iter->reset(new YQLVTableIterator( |
72 | 260k | VERIFY_RESULT(RetrieveData(request)), request.hashed_column_values())); |
73 | 0 | histogram_->Increment(MonoTime::Now().GetDeltaSince(start_time).ToMicroseconds()); |
74 | 260k | return Status::OK(); |
75 | 260k | } |
76 | | |
77 | | CHECKED_STATUS YQLVirtualTable::BuildYQLScanSpec( |
78 | | const QLReadRequestPB& request, |
79 | | const ReadHybridTime& read_time, |
80 | | const Schema& schema, |
81 | | const bool include_static_columns, |
82 | | const Schema& static_projection, |
83 | | std::unique_ptr<QLScanSpec>* spec, |
84 | 261k | std::unique_ptr<QLScanSpec>* static_row_spec) const { |
85 | | // There should be no static columns in system tables so we are not handling it. |
86 | 261k | if (include_static_columns) { |
87 | 0 | return STATUS(IllegalState, "system table contains no static columns"); |
88 | 0 | } |
89 | 261k | spec->reset(new QLScanSpec( |
90 | 261k | request.has_where_expr() ? &request.where_expr().condition()7.14k : nullptr254k , |
91 | 261k | request.has_if_expr() ? &request.if_expr().condition()0 : nullptr, |
92 | 261k | request.is_forward_scan())); |
93 | 261k | return Status::OK(); |
94 | 261k | } |
95 | | |
96 | | void YQLVirtualTable::GetSortedLiveDescriptors(std::vector<std::shared_ptr<TSDescriptor>>* descs) |
97 | 118k | const { |
98 | 118k | master_->ts_manager()->GetAllLiveDescriptors(descs); |
99 | 118k | std::sort( |
100 | 118k | descs->begin(), descs->end(), |
101 | 338k | [](const std::shared_ptr<TSDescriptor>& a, const std::shared_ptr<TSDescriptor>& b) -> bool { |
102 | 338k | return a->permanent_uuid() < b->permanent_uuid(); |
103 | 338k | }); |
104 | 118k | } |
105 | | |
106 | 396k | CatalogManagerIf& YQLVirtualTable::catalog_manager() const { |
107 | 396k | return *master_->catalog_manager(); |
108 | 396k | } |
109 | | |
110 | | Result<std::pair<int, DataType>> YQLVirtualTable::ColumnIndexAndType( |
111 | 18.4M | const std::string& col_name) const { |
112 | 18.4M | auto column_index = schema_->find_column(col_name); |
113 | 18.4M | if (column_index == Schema::kColumnNotFound) { |
114 | 0 | return STATUS_SUBSTITUTE(NotFound, "Couldn't find column $0 in schema", col_name); |
115 | 0 | } |
116 | 18.4M | const DataType data_type = schema_->column(column_index).type_info()->type; |
117 | 18.4M | return std::make_pair(column_index, data_type); |
118 | 18.4M | } |
119 | | |
120 | | const std::string kSystemTablesReleaseVersion = "3.9-SNAPSHOT"; |
121 | | const std::string kSystemTablesReleaseVersionColumn = "release_version"; |
122 | | |
123 | | } // namespace master |
124 | | } // namespace yb |