YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
32.1k
      schema_(std::make_unique<Schema>(schema)) {
43
32.1k
    std::string metricDescription = kBaseMetricDescription + table_name_;
44
32.1k
    std::string metricName = kMetricPrefixName + namespace_name + "_" + table_name;
45
32.1k
    EscapeMetricNameForPrometheus(&metricName);
46
47
32.1k
    std::unique_ptr<HistogramPrototype> prototype = std::make_unique<OwningHistogramPrototype>(
48
32.1k
                "server", metricName, metricDescription,
49
32.1k
                MetricUnit::kMicroseconds, metricDescription,
50
32.1k
                MetricLevel::kInfo, 0, 10000000, 2);
51
32.1k
    histogram_ = master->metric_entity()->FindOrCreateHistogram(std::move(prototype));
52
32.1k
}
53
54
1.18k
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
258k
    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
258k
  SCOPED_LEADER_SHARED_LOCK(l, master_->catalog_manager_impl());
68
258k
  RETURN_NOT_OK(l.first_failed_status());
69
70
258k
  MonoTime start_time = MonoTime::Now();
71
258k
  iter->reset(new YQLVTableIterator(
72
258k
      VERIFY_RESULT(RetrieveData(request)), request.hashed_column_values()));
73
258k
  histogram_->Increment(MonoTime::Now().GetDeltaSince(start_time).ToMicroseconds());
74
258k
  return Status::OK();
75
258k
}
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
259k
    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
259k
  if (include_static_columns) {
87
0
    return STATUS(IllegalState, "system table contains no static columns");
88
0
  }
89
259k
  spec->reset(new QLScanSpec(
90
252k
      request.has_where_expr() ? &request.where_expr().condition() : nullptr,
91
259k
      request.has_if_expr() ? &request.if_expr().condition() : nullptr,
92
259k
      request.is_forward_scan()));
93
259k
  return Status::OK();
94
259k
}
95
96
void YQLVirtualTable::GetSortedLiveDescriptors(std::vector<std::shared_ptr<TSDescriptor>>* descs)
97
116k
    const {
98
116k
  master_->ts_manager()->GetAllLiveDescriptors(descs);
99
116k
  std::sort(
100
116k
      descs->begin(), descs->end(),
101
318k
      [](const std::shared_ptr<TSDescriptor>& a, const std::shared_ptr<TSDescriptor>& b) -> bool {
102
318k
        return a->permanent_uuid() < b->permanent_uuid();
103
318k
      });
104
116k
}
105
106
395k
CatalogManagerIf& YQLVirtualTable::catalog_manager() const {
107
395k
  return *master_->catalog_manager();
108
395k
}
109
110
Result<std::pair<int, DataType>> YQLVirtualTable::ColumnIndexAndType(
111
18.2M
    const std::string& col_name) const {
112
18.2M
  auto column_index = schema_->find_column(col_name);
113
18.2M
  if (column_index == Schema::kColumnNotFound) {
114
0
    return STATUS_SUBSTITUTE(NotFound, "Couldn't find column $0 in schema", col_name);
115
0
  }
116
18.2M
  const DataType data_type = schema_->column(column_index).type_info()->type();
117
18.2M
  return std::make_pair(column_index, data_type);
118
18.2M
}
119
120
const std::string kSystemTablesReleaseVersion = "3.9-SNAPSHOT";
121
const std::string kSystemTablesReleaseVersionColumn = "release_version";
122
123
}  // namespace master
124
}  // namespace yb