YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/tserver/tablet_server.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
#ifndef YB_TSERVER_TABLET_SERVER_H_
33
#define YB_TSERVER_TABLET_SERVER_H_
34
35
#include <future>
36
#include <memory>
37
#include <string>
38
#include <vector>
39
40
#include "yb/consensus/metadata.pb.h"
41
#include "yb/cdc/cdc_consumer.fwd.h"
42
#include "yb/client/client_fwd.h"
43
44
#include "yb/encryption/encryption_fwd.h"
45
46
#include "yb/gutil/atomicops.h"
47
#include "yb/gutil/macros.h"
48
#include "yb/master/master_fwd.h"
49
#include "yb/server/webserver_options.h"
50
#include "yb/tserver/db_server_base.h"
51
#include "yb/tserver/tserver_shared_mem.h"
52
#include "yb/tserver/tablet_server_interface.h"
53
#include "yb/tserver/tablet_server_options.h"
54
55
#include "yb/util/locks.h"
56
#include "yb/util/net/net_util.h"
57
#include "yb/util/net/sockaddr.h"
58
#include "yb/util/status_fwd.h"
59
60
namespace rocksdb {
61
class Env;
62
}
63
64
namespace yb {
65
66
class Env;
67
class MaintenanceManager;
68
69
namespace tserver {
70
71
class TabletServer : public DbServerBase, public TabletServerIf {
72
 public:
73
  // TODO: move this out of this header, since clients want to use this
74
  // constant as well.
75
  static const uint16_t kDefaultPort = 9100;
76
  static const uint16_t kDefaultWebPort = 9000;
77
78
  // Default tserver and consensus RPC queue length per service.
79
  static constexpr uint32_t kDefaultSvcQueueLength = 5000;
80
81
  explicit TabletServer(const TabletServerOptions& opts);
82
  ~TabletServer();
83
84
  // Initializes the tablet server, including the bootstrapping of all
85
  // existing tablets.
86
  // Some initialization tasks are asynchronous, such as the bootstrapping
87
  // of tablets. Caller can block, waiting for the initialization to fully
88
  // complete by calling WaitInited().
89
  CHECKED_STATUS Init();
90
91
  CHECKED_STATUS GetRegistration(ServerRegistrationPB* reg,
92
    server::RpcOnly rpc_only = server::RpcOnly::kFalse) const override;
93
94
  // Waits for the tablet server to complete the initialization.
95
  CHECKED_STATUS WaitInited();
96
97
  CHECKED_STATUS Start();
98
  virtual void Shutdown();
99
100
  std::string ToString() const override;
101
102
2.73M
  TSTabletManager* tablet_manager() override { return tablet_manager_.get(); }
103
  TabletPeerLookupIf* tablet_peer_lookup() override;
104
105
338k
  Heartbeater* heartbeater() { return heartbeater_.get(); }
106
107
0
  MetricsSnapshotter* metrics_snapshotter() { return metrics_snapshotter_.get(); }
108
109
7
  void set_fail_heartbeats_for_tests(bool fail_heartbeats_for_tests) {
110
7
    base::subtle::NoBarrier_Store(&fail_heartbeats_for_tests_, fail_heartbeats_for_tests);
111
7
  }
112
113
816k
  bool fail_heartbeats_for_tests() const {
114
816k
    return base::subtle::NoBarrier_Load(&fail_heartbeats_for_tests_);
115
816k
  }
116
117
83.3k
  MaintenanceManager* maintenance_manager() {
118
83.3k
    return maintenance_manager_.get();
119
83.3k
  }
120
121
403k
  int64_t GetCurrentMasterIndex() { return master_config_index_; }
122
123
248
  void SetCurrentMasterIndex(int64_t index) { master_config_index_ = index; }
124
125
  // Update in-memory list of master addresses that this tablet server pings to.
126
  // If the update is from master leader, we use that list directly. If not, we
127
  // merge the existing in-memory master list with the provided config list.
128
  CHECKED_STATUS UpdateMasterAddresses(const consensus::RaftConfigPB& new_config,
129
                                       bool is_master_leader);
130
131
22.9M
  server::Clock* Clock() override { return clock(); }
132
133
0
  void SetClockForTests(server::ClockPtr clock) { clock_ = std::move(clock); }
134
135
17.4k
  const scoped_refptr<MetricEntity>& MetricEnt() const override { return metric_entity(); }
136
137
0
  tserver::TServerSharedData& SharedObject() override {
138
0
    return shared_object();
139
0
  }
140
141
  CHECKED_STATUS PopulateLiveTServers(const master::TSHeartbeatResponsePB& heartbeat_resp);
142
143
  CHECKED_STATUS GetLiveTServers(
144
      std::vector<master::TSInformationPB> *live_tservers) const override;
145
146
  CHECKED_STATUS GetTabletStatus(const GetTabletStatusRequestPB* req,
147
                                 GetTabletStatusResponsePB* resp) const override;
148
149
  bool LeaderAndReady(const TabletId& tablet_id, bool allow_stale = false) const override;
150
151
72.2k
  const std::string& permanent_uuid() const { return fs_manager_->uuid(); }
152
153
  // Returns the proxy to call this tablet server locally.
154
10.9k
  const std::shared_ptr<TabletServerServiceProxy>& proxy() const { return proxy_; }
155
156
19.2k
  const TabletServerOptions& options() const { return opts_; }
157
158
  void set_cluster_uuid(const std::string& cluster_uuid);
159
160
  std::string cluster_uuid() const;
161
162
  TabletServiceImpl* tablet_server_service();
163
164
  scoped_refptr<Histogram> GetMetricsHistogram(TabletServerServiceRpcMethodIndexes metric);
165
166
  const std::shared_ptr<MemTracker>& mem_tracker() const override;
167
168
  void SetPublisher(rpc::Publisher service) override;
169
170
0
  rpc::Publisher* GetPublisher() override {
171
0
    return publish_service_ptr_.get();
172
0
  }
173
174
  void SetYSQLCatalogVersion(uint64_t new_version, uint64_t new_breaking_version);
175
176
  void get_ysql_catalog_version(uint64_t* current_version,
177
496k
                                uint64_t* last_breaking_version) const override {
178
496k
    std::lock_guard<simple_spinlock> l(lock_);
179
496k
    if (current_version) {
180
0
      *current_version = ysql_catalog_version_;
181
0
    }
182
497k
    if (last_breaking_version) {
183
497k
      *last_breaking_version = ysql_last_breaking_catalog_version_;
184
497k
    }
185
496k
  }
186
187
  void UpdateTransactionTablesVersion(uint64_t new_version);
188
189
  virtual Env* GetEnv();
190
191
  virtual rocksdb::Env* GetRocksDBEnv();
192
193
  void SetUniverseKeys(const encryption::UniverseKeysPB& universe_keys);
194
195
  virtual CHECKED_STATUS SetUniverseKeyRegistry(
196
      const encryption::UniverseKeyRegistryPB& universe_key_registry);
197
198
  void GetUniverseKeyRegistrySync();
199
200
  uint64_t GetSharedMemoryPostgresAuthKey();
201
202
  // Currently only used by cdc.
203
0
  virtual int32_t cluster_config_version() const {
204
0
    return std::numeric_limits<int32_t>::max();
205
0
  }
206
207
  client::TransactionPool* TransactionPool() override;
208
209
  const std::shared_future<client::YBClient*>& client_future() const override;
210
211
20.3k
  const std::string& LogPrefix() const {
212
20.3k
    return log_prefix_;
213
20.3k
  }
214
215
17.8k
  const HostPort& pgsql_proxy_bind_address() const { return pgsql_proxy_bind_address_; }
216
217
  client::LocalTabletFilter CreateLocalTabletFilter() override;
218
219
 protected:
220
  virtual CHECKED_STATUS RegisterServices();
221
222
  friend class TabletServerTestBase;
223
224
  CHECKED_STATUS DisplayRpcIcons(std::stringstream* output) override;
225
226
  CHECKED_STATUS ValidateMasterAddressResolution() const;
227
228
  std::atomic<bool> initted_{false};
229
230
  // If true, all heartbeats will be seen as failed.
231
  Atomic32 fail_heartbeats_for_tests_;
232
233
  // The options passed at construction time, and will be updated if master config changes.
234
  TabletServerOptions opts_;
235
236
  // Manager for tablets which are available on this server.
237
  std::unique_ptr<TSTabletManager> tablet_manager_;
238
239
  // Used to forward redis pub/sub messages to the redis pub/sub handler
240
  yb::AtomicUniquePtr<rpc::Publisher> publish_service_ptr_;
241
242
  std::thread fetch_universe_key_thread_;
243
244
  // Thread responsible for heartbeating to the master.
245
  std::unique_ptr<Heartbeater> heartbeater_;
246
247
  std::unique_ptr<client::UniverseKeyClient> universe_key_client_;
248
249
  // Thread responsible for collecting metrics snapshots for native storage.
250
  std::unique_ptr<MetricsSnapshotter> metrics_snapshotter_;
251
252
  // Webserver path handlers
253
  std::unique_ptr<TabletServerPathHandlers> path_handlers_;
254
255
  // The maintenance manager for this tablet server
256
  std::shared_ptr<MaintenanceManager> maintenance_manager_;
257
258
  // Index at which master sent us the last config
259
  int64_t master_config_index_;
260
261
  // List of tservers that are alive from the master's perspective.
262
  std::vector<master::TSInformationPB> live_tservers_;
263
264
  // Lock to protect live_tservers_, cluster_uuid_.
265
  mutable simple_spinlock lock_;
266
267
  // Proxy to call this tablet server locally.
268
  std::shared_ptr<TabletServerServiceProxy> proxy_;
269
270
  // Cluster uuid. This is sent by the master leader during the first heartbeat.
271
  std::string cluster_uuid_;
272
273
  // Latest known version from the YSQL catalog (as reported by last heartbeat response).
274
  uint64_t ysql_catalog_version_ = 0;
275
  uint64_t ysql_last_breaking_catalog_version_ = 0;
276
277
  // An instance to tablet server service. This pointer is no longer valid after RpcAndWebServerBase
278
  // is shut down.
279
  TabletServiceImpl* tablet_server_service_;
280
281
 private:
282
  // Auto initialize some of the service flags that are defaulted to -1.
283
  void AutoInitServiceFlags();
284
285
  std::string log_prefix_;
286
287
  // Bind address of postgres proxy under this tserver.
288
  HostPort pgsql_proxy_bind_address_;
289
290
  DISALLOW_COPY_AND_ASSIGN(TabletServer);
291
};
292
293
} // namespace tserver
294
} // namespace yb
295
#endif // YB_TSERVER_TABLET_SERVER_H_