YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/yql/cql/cqlserver/cql_service.h
Line
Count
Source
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
// This file contains the CQLServiceImpl class that implements the CQL server to handle requests
15
// from Cassandra clients using the CQL native protocol.
16
17
#ifndef YB_YQL_CQL_CQLSERVER_CQL_SERVICE_H_
18
#define YB_YQL_CQL_CQLSERVER_CQL_SERVICE_H_
19
20
#include <vector>
21
22
#include <boost/compute/detail/lru_cache.hpp>
23
24
#include "yb/client/client_fwd.h"
25
26
#include "yb/util/object_pool.h"
27
28
#include "yb/yql/cql/cqlserver/cqlserver_fwd.h"
29
#include "yb/yql/cql/cqlserver/cql_server_options.h"
30
#include "yb/yql/cql/cqlserver/cql_service.service.h"
31
#include "yb/yql/cql/cqlserver/cql_statement.h"
32
#include "yb/yql/cql/cqlserver/system_query_cache.h"
33
#include "yb/yql/cql/ql/parser/parser_fwd.h"
34
#include "yb/yql/cql/ql/util/cql_message.h"
35
36
namespace yb {
37
38
namespace cqlserver {
39
40
extern const char* const kRoleColumnNameSaltedHash;
41
extern const char* const kRoleColumnNameCanLogin;
42
43
class CQLMetrics;
44
class CQLProcessor;
45
class CQLServer;
46
47
class CQLServiceImpl : public CQLServerServiceIf,
48
                       public GarbageCollector,
49
                       public std::enable_shared_from_this<CQLServiceImpl> {
50
 public:
51
  // Constructor.
52
  CQLServiceImpl(CQLServer* server, const CQLServerOptions& opts);
53
  ~CQLServiceImpl();
54
55
  void CompleteInit();
56
57
  void Shutdown() override;
58
59
  void FillEndpoints(const rpc::RpcServicePtr& service, rpc::RpcEndpointMap* map) override;
60
61
  // Processing all incoming request from RPC and sending response back.
62
  void Handle(yb::rpc::InboundCallPtr call) override;
63
64
  // Either gets an available processor or creates a new one.
65
  Result<CQLProcessor*> GetProcessor();
66
67
  // Return CQL processor at pos as available.
68
  void ReturnProcessor(const CQLProcessorListPos& pos);
69
70
  // Allocate a prepared statement. If the statement already exists, return it instead.
71
  std::shared_ptr<CQLStatement> AllocatePreparedStatement(
72
      const ql::CQLMessage::QueryId& id, const std::string& keyspace, const std::string& query);
73
74
  // Look up a prepared statement by its id. Nullptr will be returned if the statement is not found.
75
  std::shared_ptr<const CQLStatement> GetPreparedStatement(const ql::CQLMessage::QueryId& id);
76
77
5.89k
  std::shared_ptr<ql::Statement> GetAuthPreparedStatement() const { return auth_prepared_stmt_; }
78
79
  // Delete the prepared statement from the cache.
80
  void DeletePreparedStatement(const std::shared_ptr<const CQLStatement>& stmt);
81
82
  // Check that the password and hash match.  Leverages shared LRU cache.
83
  bool CheckPassword(const std::string plain, const std::string expected_bcrypt_hash);
84
85
  // Return the memory tracker for prepared statements.
86
5.69k
  const MemTrackerPtr& prepared_stmts_mem_tracker() const {
87
5.69k
    return prepared_stmts_mem_tracker_;
88
5.69k
  }
89
90
18.0k
  const MemTrackerPtr& processors_mem_tracker() const {
91
18.0k
    return processors_mem_tracker_;
92
18.0k
  }
93
94
  // Return the YBClient to communicate with either master or tserver.
95
  client::YBClient* client() const;
96
97
  // Return the YBClientCache.
98
  const std::shared_ptr<client::YBMetaDataCache>& metadata_cache() const;
99
100
  // Return the CQL metrics.
101
36.3k
  const std::shared_ptr<CQLMetrics>& cql_metrics() const { return cql_metrics_; }
102
103
18.1k
  ThreadSafeObjectPool<ql::Parser>& parser_pool() { return parser_pool_; }
104
105
  // Return the messenger.
106
18.5M
  rpc::Messenger* messenger() { return messenger_; }
107
108
  client::TransactionPool* TransactionPool();
109
110
  server::Clock* clock();
111
112
318k
  std::shared_ptr<SystemQueryCache> system_cache() { return system_cache_; }
113
114
 private:
115
  constexpr static int kRpcTimeoutSec = 5;
116
117
  // Insert a prepared statement at the front of the LRU list. "prepared_stmts_mutex_" needs to be
118
  // locked before this call.
119
  void InsertLruPreparedStatementUnlocked(const std::shared_ptr<CQLStatement>& stmt);
120
121
  // Move a prepared statement to the front of the LRU list. "prepared_stmts_mutex_" needs to be
122
  // locked before this call.
123
  void MoveLruPreparedStatementUnlocked(const std::shared_ptr<CQLStatement>& stmt);
124
125
  // Delete a prepared statement from the cache and the LRU list. "prepared_stmts_mutex_" needs to
126
  // be locked before this call.
127
  void DeletePreparedStatementUnlocked(const std::shared_ptr<const CQLStatement> stmt);
128
129
  // Delete the least recently used prepared statement from the cache to free up memory.
130
  void CollectGarbage(size_t required) override;
131
132
  // CQLServer of this service.
133
  CQLServer* const server_;
134
135
  // A cache to reduce opening tables or (user-defined) types again and again.
136
  mutable std::shared_ptr<client::YBMetaDataCache> metadata_cache_;
137
  mutable std::atomic<bool> is_metadata_initialized_ = { false };
138
  mutable std::mutex metadata_init_mutex_;
139
140
  // List of CQL processors (in-use and available). In-use ones are at the beginning and available
141
  // ones at the end.
142
  CQLProcessorList processors_;
143
144
  // Next available CQL processor.
145
  CQLProcessorListPos next_available_processor_;
146
147
  // Mutex that protects access to processors_.
148
  std::mutex processors_mutex_;
149
150
  // Prepared statements cache.
151
  CQLStatementMap prepared_stmts_map_;
152
153
  // Prepared statements LRU list (least recently used one at the end).
154
  CQLStatementList prepared_stmts_list_;
155
156
  // Mutex that protects the prepared statements and the LRU list.
157
  std::mutex prepared_stmts_mutex_;
158
159
  std::shared_ptr<ql::Statement> auth_prepared_stmt_;
160
161
  // Tracker to measure and limit memory usage of prepared statements.
162
  MemTrackerPtr prepared_stmts_mem_tracker_;
163
164
  MemTrackerPtr processors_mem_tracker_;
165
166
  // Password and hash cache. Stores each password-hash pair as a compound key;
167
  // see implementation for rationale.
168
  boost::compute::detail::lru_cache<std::string, bool> password_cache_
169
    GUARDED_BY(password_cache_mutex_);
170
  std::mutex password_cache_mutex_;
171
172
  std::shared_ptr<SystemQueryCache> system_cache_;
173
174
  // Metrics to be collected and reported.
175
  yb::rpc::RpcMethodMetrics metrics_;
176
177
  std::shared_ptr<CQLMetrics> cql_metrics_;
178
179
  ThreadSafeObjectPool<ql::Parser> parser_pool_;
180
181
  // Used to requeue the cql_inbound call to handle the response callback(s).
182
  rpc::Messenger* messenger_ = nullptr;
183
184
  int64_t num_allocated_processors_ = 0;
185
};
186
187
}  // namespace cqlserver
188
}  // namespace yb
189
190
#endif  // YB_YQL_CQL_CQLSERVER_CQL_SERVICE_H_