forked from vesoft-inc/nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetaDaemon.cpp
287 lines (260 loc) · 10.5 KB
/
MetaDaemon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* Copyright (c) 2018 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/
#include "base/Base.h"
#include "common/base/SignalHandler.h"
#include <thrift/lib/cpp2/server/ThriftServer.h>
#include "meta/MetaServiceHandler.h"
#include "meta/MetaHttpIngestHandler.h"
#include "meta/MetaHttpStatusHandler.h"
#include "meta/MetaHttpDownloadHandler.h"
#include "webservice/WebService.h"
#include "network/NetworkUtils.h"
#include "process/ProcessUtils.h"
#include "hdfs/HdfsHelper.h"
#include "hdfs/HdfsCommandHelper.h"
#include "thread/GenericThreadPool.h"
#include "kvstore/PartManager.h"
#include "meta/ClusterIdMan.h"
#include "kvstore/NebulaStore.h"
#include "meta/ActiveHostsMan.h"
#include "meta/KVBasedGflagsManager.h"
using nebula::operator<<;
using nebula::ProcessUtils;
using nebula::Status;
DEFINE_int32(port, 45500, "Meta daemon listening port");
DEFINE_bool(reuse_port, true, "Whether to turn on the SO_REUSEPORT option");
DEFINE_string(data_path, "", "Root data path");
DEFINE_string(meta_server_addrs,
"",
"It is a list of IPs split by comma, used in cluster deployment"
"the ips number is equal to the replica number."
"If empty, it means it's a single node");
DEFINE_string(local_ip, "", "Local ip specified for NetworkUtils::getLocalIP");
DEFINE_int32(num_io_threads, 16, "Number of IO threads");
DEFINE_int32(meta_http_thread_num, 3, "Number of meta daemon's http thread");
DEFINE_int32(num_worker_threads, 32, "Number of workers");
DEFINE_string(pid_file, "pids/nebula-metad.pid", "File to hold the process id");
DEFINE_bool(daemonize, true, "Whether run as a daemon process");
DECLARE_bool(check_leader);
static std::unique_ptr<apache::thrift::ThriftServer> gServer;
static void signalHandler(int sig);
static Status setupSignalHandler();
namespace nebula {
namespace meta {
const std::string kClusterIdKey = "__meta_cluster_id_key__"; // NOLINT
} // namespace meta
} // namespace nebula
nebula::ClusterID gClusterId = 0;
std::unique_ptr<nebula::kvstore::KVStore> initKV(std::vector<nebula::HostAddr> peers,
nebula::HostAddr localhost) {
auto partMan
= std::make_unique<nebula::kvstore::MemPartManager>();
// The meta server has only one space (0), one part (0)
partMan->addPart(nebula::meta::kDefaultSpaceId,
nebula::meta::kDefaultPartId,
std::move(peers));
// folly IOThreadPoolExecutor
auto ioPool = std::make_shared<folly::IOThreadPoolExecutor>(FLAGS_num_io_threads);
std::shared_ptr<apache::thrift::concurrency::ThreadManager> threadManager(
apache::thrift::concurrency::PriorityThreadManager::newPriorityThreadManager(
FLAGS_num_worker_threads, true /*stats*/));
threadManager->setNamePrefix("executor");
threadManager->start();
// On metad, we are allowed to read on follower
FLAGS_check_leader = false;
nebula::kvstore::KVOptions options;
options.dataPaths_ = {FLAGS_data_path};
options.partMan_ = std::move(partMan);
auto kvstore = std::make_unique<nebula::kvstore::NebulaStore>(
std::move(options),
ioPool,
localhost,
threadManager);
if (!(kvstore->init())) {
LOG(ERROR) << "Nebula store init failed";
return nullptr;
}
LOG(INFO) << "Waiting for the leader elected...";
nebula::HostAddr leader;
while (true) {
auto ret = kvstore->partLeader(nebula::meta::kDefaultSpaceId,
nebula::meta::kDefaultPartId);
if (!nebula::ok(ret)) {
LOG(ERROR) << "Nebula store init failed";
return nullptr;
}
leader = nebula::value(ret);
if (leader != nebula::HostAddr(0, 0)) {
break;
}
LOG(INFO) << "Leader has not been elected, sleep 1s";
sleep(1);
}
gClusterId = nebula::meta::ClusterIdMan::getClusterIdFromKV(kvstore.get(),
nebula::meta::kClusterIdKey);
if (gClusterId == 0) {
if (leader == localhost) {
LOG(INFO) << "I am leader, create cluster Id";
gClusterId = nebula::meta::ClusterIdMan::create(FLAGS_meta_server_addrs);
if (!nebula::meta::ClusterIdMan::persistInKV(kvstore.get(),
nebula::meta::kClusterIdKey,
gClusterId)) {
LOG(ERROR) << "Persist cluster failed!";
return nullptr;
}
} else {
LOG(INFO) << "I am follower, wait for the leader's clusterId";
while (gClusterId == 0) {
LOG(INFO) << "Waiting for the leader's clusterId";
sleep(1);
gClusterId = nebula::meta::ClusterIdMan::getClusterIdFromKV(
kvstore.get(),
nebula::meta::kClusterIdKey);
}
}
}
LOG(INFO) << "Nebula store init succeeded, clusterId " << gClusterId;
return kvstore;
}
bool initWebService(nebula::kvstore::KVStore* kvstore,
nebula::hdfs::HdfsCommandHelper* helper,
nebula::thread::GenericThreadPool* pool) {
LOG(INFO) << "Starting Meta HTTP Service";
nebula::WebService::registerHandler("/status", [] {
return new nebula::meta::MetaHttpStatusHandler();
});
nebula::WebService::registerHandler("/download-dispatch", [kvstore, helper, pool] {
auto handler = new nebula::meta::MetaHttpDownloadHandler();
handler->init(kvstore, helper, pool);
return handler;
});
nebula::WebService::registerHandler("/ingest-dispatch", [kvstore, pool] {
auto handler = new nebula::meta::MetaHttpIngestHandler();
handler->init(kvstore, pool);
return handler;
});
auto status = nebula::WebService::start();
if (!status.ok()) {
LOG(ERROR) << "Failed to start web service: " << status;
return false;
}
return true;
}
bool initComponents(nebula::kvstore::KVStore* kvstore) {
auto gflagsManager = std::make_unique<nebula::meta::KVBasedGflagsManager>(kvstore);
gflagsManager->init();
return true;
}
int main(int argc, char *argv[]) {
google::SetVersionString(nebula::versionString());
folly::init(&argc, &argv, true);
if (FLAGS_data_path.empty()) {
LOG(ERROR) << "Meta Data Path should not empty";
return EXIT_FAILURE;
}
if (FLAGS_daemonize) {
google::SetStderrLogging(google::FATAL);
} else {
google::SetStderrLogging(google::INFO);
}
// Detect if the server has already been started
auto pidPath = FLAGS_pid_file;
auto status = ProcessUtils::isPidAvailable(pidPath);
if (!status.ok()) {
LOG(ERROR) << status;
return EXIT_FAILURE;
}
if (FLAGS_daemonize) {
status = ProcessUtils::daemonize(pidPath);
if (!status.ok()) {
LOG(ERROR) << status;
return EXIT_FAILURE;
}
} else {
status = ProcessUtils::makePidFile(pidPath);
if (!status.ok()) {
LOG(ERROR) << status;
return EXIT_FAILURE;
}
}
auto result = nebula::network::NetworkUtils::getLocalIP(FLAGS_local_ip);
if (!result.ok()) {
LOG(ERROR) << "Get local ip failed! status:" << result.status();
return EXIT_FAILURE;
}
auto hostAddrRet = nebula::network::NetworkUtils::toHostAddr(result.value(), FLAGS_port);
if (!hostAddrRet.ok()) {
LOG(ERROR) << "Bad local host addr, status:" << hostAddrRet.status();
return EXIT_FAILURE;
}
auto& localhost = hostAddrRet.value();
auto peersRet = nebula::network::NetworkUtils::toHosts(FLAGS_meta_server_addrs);
if (!peersRet.ok()) {
LOG(ERROR) << "Can't get peers address, status:" << peersRet.status();
return EXIT_FAILURE;
}
auto kvstore = initKV(peersRet.value(), localhost);
if (kvstore == nullptr) {
LOG(ERROR) << "Init kv failed!";
return EXIT_FAILURE;
}
if (!initComponents(kvstore.get())) {
LOG(ERROR) << "Init components failed";
return EXIT_FAILURE;
}
LOG(INFO) << "Start http service";
auto helper = std::make_unique<nebula::hdfs::HdfsCommandHelper>();
auto pool = std::make_unique<nebula::thread::GenericThreadPool>();
pool->start(FLAGS_meta_http_thread_num, "http thread pool");
if (!initWebService(kvstore.get(), helper.get(), pool.get())) {
LOG(ERROR) << "Init web service failed";
return EXIT_FAILURE;
}
// Setup the signal handlers
status = setupSignalHandler();
if (!status.ok()) {
LOG(ERROR) << status;
nebula::WebService::stop();
return EXIT_FAILURE;
}
auto handler = std::make_shared<nebula::meta::MetaServiceHandler>(kvstore.get(), gClusterId);
LOG(INFO) << "The meta deamon start on " << localhost;
try {
gServer = std::make_unique<apache::thrift::ThriftServer>();
gServer->setPort(FLAGS_port);
gServer->setReusePort(FLAGS_reuse_port);
gServer->setIdleTimeout(std::chrono::seconds(0)); // No idle timeout on client connection
gServer->setInterface(std::move(handler));
gServer->serve(); // Will wait until the server shuts down
} catch (const std::exception &e) {
nebula::WebService::stop();
LOG(ERROR) << "Exception thrown: " << e.what();
return EXIT_FAILURE;
}
nebula::WebService::stop();
LOG(INFO) << "The meta Daemon stopped";
return EXIT_SUCCESS;
}
Status setupSignalHandler() {
return nebula::SignalHandler::install(
{SIGINT, SIGTERM},
[](nebula::SignalHandler::GeneralSignalInfo *info) {
signalHandler(info->sig());
});
}
void signalHandler(int sig) {
switch (sig) {
case SIGINT:
case SIGTERM:
FLOG_INFO("Signal %d(%s) received, stopping this server", sig, ::strsignal(sig));
if (gServer) {
gServer->stop();
}
break;
default:
FLOG_ERROR("Signal %d(%s) received but ignored", sig, ::strsignal(sig));
}
}