YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/server/server_base.h
Line
Count
Source
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_SERVER_SERVER_BASE_H
33
#define YB_SERVER_SERVER_BASE_H
34
35
#include <memory>
36
#include <string>
37
38
#include "yb/gutil/macros.h"
39
#include "yb/gutil/ref_counted.h"
40
41
#include "yb/server/server_base_options.h"
42
#include "yb/server/webserver.h"
43
44
#include "yb/util/metrics_fwd.h"
45
#include "yb/util/status_fwd.h"
46
#include "yb/util/countdown_latch.h"
47
48
namespace yb {
49
50
class Env;
51
class FsManager;
52
class MemTracker;
53
class MetricEntity;
54
class MetricRegistry;
55
class NodeInstancePB;
56
class ScopedGLogMetrics;
57
class Thread;
58
class Webserver;
59
60
namespace server {
61
62
class Clock;
63
class RpcServer;
64
class ServerBaseOptions;
65
class ServerStatusPB;
66
67
// Base class that is common to implementing a Redis server, as well as
68
// a YB tablet server and master.
69
class RpcServerBase {
70
 public:
71
59.6k
  const RpcServer *rpc_server() const { return rpc_server_.get(); }
72
488k
  rpc::Messenger* messenger() const { return messenger_.get(); }
73
145k
  rpc::ProxyCache& proxy_cache() { return *proxy_cache_; }
74
75
  // Return the first RPC address that this server has bound to.
76
  // FATALs if the server is not started.
77
  Endpoint first_rpc_address() const;
78
79
  // Return the RPC addresses that this server has bound to.
80
  const std::vector<Endpoint>& rpc_addresses() const;
81
82
  // Return the instance identifier of this server.
83
  // This may not be called until after the server is Initted.
84
  const NodeInstancePB& instance_pb() const;
85
86
260k
  const std::shared_ptr<MemTracker>& mem_tracker() const { return mem_tracker_; }
87
88
322k
  const scoped_refptr<MetricEntity>& metric_entity() const { return metric_entity_; }
89
90
17.3k
  MetricRegistry* metric_registry() { return metric_registry_.get(); }
91
92
  // Returns this server's clock.
93
25.9M
  Clock* clock() { return clock_.get(); }
94
95
  virtual std::string ToString() const;
96
97
  // Return a PB describing the status of the server (version info, bound ports, etc)
98
  virtual void GetStatusPB(ServerStatusPB* status) const;
99
100
  CloudInfoPB MakeCloudInfoPB() const;
101
102
10.8k
  const ServerBaseOptions& options() const {
103
10.8k
    return options_;
104
10.8k
  }
105
106
  // Return the hostname of this server
107
  const std::string get_hostname() const;
108
109
 protected:
110
  RpcServerBase(std::string name,
111
                const ServerBaseOptions& options,
112
                const std::string& metrics_namespace,
113
                std::shared_ptr<MemTracker> mem_tracker,
114
                const scoped_refptr<Clock>& clock = nullptr);
115
  virtual ~RpcServerBase();
116
117
  CHECKED_STATUS Init();
118
  CHECKED_STATUS RegisterService(
119
      size_t queue_limit, rpc::ServiceIfPtr rpc_impl,
120
      rpc::ServicePriority priority = rpc::ServicePriority::kNormal);
121
  CHECKED_STATUS Start();
122
  CHECKED_STATUS StartRpcServer();
123
  void Shutdown();
124
  void SetConnectionContextFactory(rpc::ConnectionContextFactoryPtr connection_context_factory);
125
  virtual CHECKED_STATUS SetupMessengerBuilder(rpc::MessengerBuilder* builder);
126
127
  const std::string name_;
128
  std::shared_ptr<MemTracker> mem_tracker_;
129
  std::unique_ptr<MetricRegistry> metric_registry_;
130
  scoped_refptr<MetricEntity> metric_entity_;
131
  std::unique_ptr<RpcServer> rpc_server_;
132
  std::unique_ptr<rpc::Messenger> messenger_;
133
  std::unique_ptr<rpc::ProxyCache> proxy_cache_;
134
135
  scoped_refptr<Clock> clock_;
136
  bool external_clock_ = false;
137
138
  // The instance identifier of this server.
139
  std::unique_ptr<NodeInstancePB> instance_pb_;
140
141
  ServerBaseOptions options_;
142
143
  virtual CHECKED_STATUS DumpServerInfo(const std::string& path,
144
                        const std::string& format) const;
145
146
  bool initialized_;
147
 private:
148
  CHECKED_STATUS StartMetricsLogging();
149
  void MetricsLoggingThread();
150
151
  scoped_refptr<Thread> metrics_logging_thread_;
152
  CountDownLatch stop_metrics_logging_latch_;
153
154
  std::unique_ptr<ScopedGLogMetrics> glog_metrics_;
155
156
  DISALLOW_COPY_AND_ASSIGN(RpcServerBase);
157
};
158
159
YB_STRONGLY_TYPED_BOOL(RpcOnly);
160
161
// Base class for tablet server and master.
162
// Handles starting and stopping the RPC server and web server,
163
// and provides a common interface for server-type-agnostic functions.
164
class RpcAndWebServerBase : public RpcServerBase {
165
 public:
166
11.3k
  const Webserver *web_server() const { return web_server_.get(); }
167
168
6.50M
  FsManager* fs_manager() { return fs_manager_.get(); }
169
170
  // Return the first HTTP address that this server has bound to.
171
  // FATALs if the server is not started.
172
  Endpoint first_http_address() const;
173
174
  // Return a PB describing the status of the server (version info, bound ports, etc)
175
  void GetStatusPB(ServerStatusPB* status) const override;
176
177
  // Centralized method to get the Registration information for either the Master or Tserver.
178
  virtual CHECKED_STATUS GetRegistration(
179
      ServerRegistrationPB* reg, RpcOnly rpc_only = RpcOnly::kFalse) const;
180
181
 protected:
182
  RpcAndWebServerBase(
183
      std::string name, const ServerBaseOptions& options,
184
      const std::string& metrics_namespace,
185
      std::shared_ptr<MemTracker> mem_tracker,
186
      const scoped_refptr<Clock>& clock = nullptr);
187
  virtual ~RpcAndWebServerBase();
188
189
  virtual Status HandleDebugPage(const Webserver::WebRequest& req, Webserver::WebResponse* resp);
190
191
  virtual void DisplayGeneralInfoIcons(std::stringstream* output);
192
193
  virtual CHECKED_STATUS DisplayRpcIcons(std::stringstream* output);
194
195
  static void DisplayIconTile(std::stringstream* output, const std::string icon,
196
                              const std::string caption, const std::string url);
197
198
  CHECKED_STATUS Init();
199
  CHECKED_STATUS Start();
200
  void Shutdown();
201
202
  std::unique_ptr<FsManager> fs_manager_;
203
  std::unique_ptr<Webserver> web_server_;
204
205
 private:
206
  void GenerateInstanceID();
207
  std::string GetEasterEggMessage() const;
208
  std::string FooterHtml() const;
209
210
  scoped_refptr<AtomicMillisLag> server_uptime_ms_metric_;
211
212
  DISALLOW_COPY_AND_ASSIGN(RpcAndWebServerBase);
213
};
214
215
std::shared_ptr<MemTracker> CreateMemTrackerForServer();
216
217
YB_STRONGLY_TYPED_BOOL(Private);
218
219
// Returns public/private address for test server with specified index.
220
std::string TEST_RpcAddress(size_t index, Private priv);
221
// Returns bind endpoint for test server with specified index and specified port.
222
std::string TEST_RpcBindEndpoint(size_t index, uint16_t port);
223
224
// Sets up connectivity in test for specified messenger of server with index.
225
void TEST_SetupConnectivity(rpc::Messenger* messenger, size_t index);
226
// Isolates specific messenger, i.e. breaks any of this messengers connections with all other
227
// servers.
228
void TEST_Isolate(rpc::Messenger* messenger);
229
230
} // namespace server
231
} // namespace yb
232
233
#endif /* YB_SERVER_SERVER_BASE_H */