Skip to content

Commit

Permalink
simplify graph signal handler
Browse files Browse the repository at this point in the history
  • Loading branch information
heroicNeZha committed Dec 23, 2021
1 parent 75f1112 commit 45c0e97
Showing 1 changed file with 55 additions and 18 deletions.
73 changes: 55 additions & 18 deletions src/daemons/GraphDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <string.h>
#include <thrift/lib/cpp2/server/ThriftServer.h>

#include <mutex>
#include <thread>

#include "common/base/Base.h"
#include "common/base/SignalHandler.h"
#include "common/fs/FileUtils.h"
Expand Down Expand Up @@ -43,6 +46,11 @@ extern Status setupBreakpad();
DECLARE_string(flagfile);
DECLARE_bool(containerized);

enum ServiceStatus { STATUS_UNINITIALIZED = 0, STATUS_RUNNING = 1, STATUS_STOPPED = 2 };
std::atomic<ServiceStatus> serverStatus_{STATUS_UNINITIALIZED};
std::mutex muStop_;
std::condition_variable cvStop_;

int main(int argc, char *argv[]) {
google::SetVersionString(nebula::versionString());
if (argc == 1) {
Expand Down Expand Up @@ -166,33 +174,56 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}

gServer->setPort(localhost.port);
gServer->setInterface(std::move(interface));
gServer->setReusePort(FLAGS_reuse_port);
gServer->setIdleTimeout(std::chrono::seconds(FLAGS_client_idle_timeout_secs));
gServer->setNumAcceptThreads(FLAGS_num_accept_threads);
gServer->setListenBacklog(FLAGS_listen_backlog);
if (FLAGS_enable_ssl || FLAGS_enable_graph_ssl) {
gServer->setSSLConfig(nebula::sslContextConfig());
}
setupThreadManager();

// Setup the signal handlers
status = setupSignalHandler();
if (!status.ok()) {
LOG(ERROR) << status;
return EXIT_FAILURE;
}

FLOG_INFO("Starting nebula-graphd on %s:%d\n", localhost.host.c_str(), localhost.port);
try {
gServer->serve(); // Blocking wait until shut down via gServer->stop()
} catch (const std::exception &e) {
FLOG_ERROR("Exception thrown while starting the RPC server: %s", e.what());
std::thread graph_thread([&] {
gServer->setPort(localhost.port);
gServer->setInterface(std::move(interface));
gServer->setReusePort(FLAGS_reuse_port);
gServer->setIdleTimeout(std::chrono::seconds(FLAGS_client_idle_timeout_secs));
gServer->setNumAcceptThreads(FLAGS_num_accept_threads);
gServer->setListenBacklog(FLAGS_listen_backlog);
if (FLAGS_enable_ssl || FLAGS_enable_graph_ssl) {
gServer->setSSLConfig(nebula::sslContextConfig());
}
setupThreadManager();
serverStatus_.store(STATUS_RUNNING);
FLOG_INFO("Starting nebula-graphd on %s:%d\n", localhost.host.c_str(), localhost.port);
try {
gServer->serve(); // Blocking wait until shut down via gServer->stop()
} catch (const std::exception &e) {
FLOG_ERROR("Exception thrown while starting the RPC server: %s", e.what());
return;
}
serverStatus_.store(STATUS_STOPPED);
FLOG_INFO("nebula-graphd on %s:%d has been stopped", localhost.host.c_str(), localhost.port);
});

while (serverStatus_ == STATUS_UNINITIALIZED) {
std::this_thread::sleep_for(std::chrono::microseconds(100));
}

if (serverStatus_ != STATUS_RUNNING) {
LOG(ERROR) << "Graphd server start failed";
gServer->stop();
return EXIT_FAILURE;
}

FLOG_INFO("nebula-graphd on %s:%d has been stopped", localhost.host.c_str(), localhost.port);
{
std::unique_lock<std::mutex> lkStop(muStop_);
while (serverStatus_ == STATUS_RUNNING) {
cvStop_.wait(lkStop);
}
}

gServer->stop();

graph_thread.join();

return EXIT_SUCCESS;
}
Expand All @@ -208,7 +239,13 @@ void signalHandler(int sig) {
case SIGINT:
case SIGTERM:
FLOG_INFO("Signal %d(%s) received, stopping this server", sig, ::strsignal(sig));
gServer->stop();
{
std::unique_lock<std::mutex> lkStop(muStop_);
if (serverStatus_ == STATUS_RUNNING) {
serverStatus_ = STATUS_STOPPED;
cvStop_.notify_one();
}
}
break;
default:
FLOG_ERROR("Signal %d(%s) received but ignored", sig, ::strsignal(sig));
Expand Down

0 comments on commit 45c0e97

Please sign in to comment.