diff --git a/cpp/CHANGELOG.md b/cpp/CHANGELOG.md index d4d609d4aaec6dc7f51ab384d94972fdab79fb25..300d40f0afe5bb40f4bd3f0fac3b9302f046ff6b 100644 --- a/cpp/CHANGELOG.md +++ b/cpp/CHANGELOG.md @@ -5,6 +5,7 @@ Please mark all change in change log and use the ticket from JIRA. # Milvus 0.5.0 (TODO) ## Bug +- MS-572 - Milvus crash when get SIGINT ## Improvement - MS-552 - Add and change the easylogging library diff --git a/cpp/src/main.cpp b/cpp/src/main.cpp index 3065e7d6845586161eb96133a8ee42b9fd76e8da..5819463d60c96a2269dfb13ea555ca6732e070f9 100644 --- a/cpp/src/main.cpp +++ b/cpp/src/main.cpp @@ -15,19 +15,20 @@ // specific language governing permissions and limitations // under the License. -#include "server/Server.h" -#include "version.h" - #include #include #include #include #include -#include "utils/easylogging++.h" -#include "metrics/Metrics.h" +#include +#include "utils/easylogging++.h" #include "utils/SignalUtil.h" #include "utils/CommonUtil.h" +#include "metrics/Metrics.h" +#include "server/Server.h" +#include "version.h" + INITIALIZE_EASYLOGGINGPP @@ -40,12 +41,6 @@ main(int argc, char *argv[]) { std::cout << std::endl << "Welcome to use Milvus by Zilliz!" << std::endl; std::cout << "Milvus " << BUILD_TYPE << " version: v" << MILVUS_VERSION << " built at " << BUILD_TIME << std::endl; - signal(SIGINT, server::SignalUtil::HandleSignal); - signal(SIGSEGV, server::SignalUtil::HandleSignal); - signal(SIGUSR1, server::SignalUtil::HandleSignal); - signal(SIGUSR2, server::SignalUtil::HandleSignal); - - std::string app_name = basename(argv[0]); static struct option long_options[] = {{"conf_file", required_argument, 0, 'c'}, {"log_conf_file", required_argument, 0, 'l'}, {"help", no_argument, 0, 'h'}, @@ -55,14 +50,12 @@ main(int argc, char *argv[]) { int option_index = 0; int64_t start_daemonized = 0; -// int pid_fd; std::string config_filename, log_config_file; std::string pid_filename; + std::string app_name = argv[0]; - app_name = argv[0]; - - if(argc < 2) { + if (argc < 2) { print_help(app_name); std::cout << "Milvus server exit..." << std::endl; return EXIT_FAILURE; @@ -94,29 +87,38 @@ main(int argc, char *argv[]) { break; } - case 'd': - start_daemonized = 1; + case 'd':start_daemonized = 1; break; - case 'h': - print_help(app_name); + case 'h':print_help(app_name); return EXIT_SUCCESS; - case '?': - print_help(app_name); + case '?':print_help(app_name); return EXIT_FAILURE; - default: - print_help(app_name); + default:print_help(app_name); break; } } - server::Server& server = server::Server::Instance(); + server::Server &server = server::Server::Instance(); server.Init(start_daemonized, pid_filename, config_filename, log_config_file); - return server.Start(); + server.Start(); + + /* Handle Signal */ + signal(SIGHUP, server::SignalUtil::HandleSignal); + signal(SIGINT, server::SignalUtil::HandleSignal); + signal(SIGUSR1, server::SignalUtil::HandleSignal); + signal(SIGSEGV, server::SignalUtil::HandleSignal); + signal(SIGUSR2, server::SignalUtil::HandleSignal); + signal(SIGTERM, server::SignalUtil::HandleSignal); + + /* wait signal */ + pause(); + + return 0; } void print_help(const std::string &app_name) { - std::cout << std::endl<< "Usage: " << app_name << " [OPTIONS]" << std::endl << std::endl; + std::cout << std::endl << "Usage: " << app_name << " [OPTIONS]" << std::endl << std::endl; std::cout << " Options:" << std::endl; std::cout << " -h --help Print this help" << std::endl; std::cout << " -c --conf_file filename Read configuration from the file" << std::endl; diff --git a/cpp/src/server/Server.cpp b/cpp/src/server/Server.cpp index e4cb7b8d4addac30edf8b038818fa121acfce33b..38b41a5331461386b7acd678f85720077489dcdd 100644 --- a/cpp/src/server/Server.cpp +++ b/cpp/src/server/Server.cpp @@ -17,7 +17,7 @@ #include #include "Server.h" -#include "server/grpc_impl/GrpcMilvusServer.h" +#include "server/grpc_impl/GrpcServer.h" #include "utils/Log.h" #include "utils/LogUtil.h" #include "utils/SignalUtil.h" @@ -154,74 +154,61 @@ Server::Daemonize() { } } -int +void Server::Start() { - if (daemonized_) { Daemonize(); } - do { - try { - // Read config file - if (LoadConfig() != SERVER_SUCCESS) { - return 1; - } + try { + /* Read config file */ + if (LoadConfig() != SERVER_SUCCESS) { + std::cerr << "Milvus server fail to load config file" << std::endl; + return; + } - //log path is defined by LoadConfig, so InitLog must be called after LoadConfig - ServerConfig &config = ServerConfig::GetInstance(); - ConfigNode server_config = config.GetConfig(CONFIG_SERVER); + /* log path is defined in Config file, so InitLog must be called after LoadConfig */ + ServerConfig &config = ServerConfig::GetInstance(); + ConfigNode server_config = config.GetConfig(CONFIG_SERVER); - std::string time_zone = server_config.GetValue(CONFIG_TIME_ZONE, "UTC+8"); - if (time_zone.length() == 3) { + std::string time_zone = server_config.GetValue(CONFIG_TIME_ZONE, "UTC+8"); + if (time_zone.length() == 3) { + time_zone = "CUT"; + } else { + int time_bias = std::stoi(time_zone.substr(3, std::string::npos)); + if (time_bias == 0) time_zone = "CUT"; + else if (time_bias > 0) { + time_zone = "CUT" + std::to_string(-time_bias); } else { - int time_bias = std::stoi(time_zone.substr(3, std::string::npos)); - if (time_bias == 0) - time_zone = "CUT"; - else if (time_bias > 0) { - time_zone = "CUT" + std::to_string(-time_bias); - } else { - time_zone = "CUT+" + std::to_string(-time_bias); - } - } - - if (setenv("TZ", time_zone.c_str(), 1) != 0) { - return -1; + time_zone = "CUT+" + std::to_string(-time_bias); } - tzset(); - - InitLog(log_config_file_); + } - // Handle Signal - signal(SIGINT, SignalUtil::HandleSignal); - signal(SIGHUP, SignalUtil::HandleSignal); - signal(SIGTERM, SignalUtil::HandleSignal); - server::Metrics::GetInstance().Init(); - server::SystemInfo::GetInstance().Init(); + if (setenv("TZ", time_zone.c_str(), 1) != 0) { + std::cerr << "Fail to setenv" << std::endl; + return; + } + tzset(); - std::cout << "Milvus server start successfully." << std::endl; - StartService(); + InitLog(log_config_file_); - while (running_) { - sleep(10); - } + server::Metrics::GetInstance().Init(); + server::SystemInfo::GetInstance().Init(); - } catch (std::exception &ex) { - std::cerr << "Milvus server encounter exception: " << std::string(ex.what()) - << "Is another server instance running?"; - break; - } - } while (false); + std::cout << "Milvus server start successfully." << std::endl; + StartService(); - return 0; + } catch (std::exception &ex) { + std::cerr << "Milvus server encounter exception: " << ex.what(); + } } void Server::Stop() { std::cerr << "Milvus server is going to shutdown ..." << std::endl; - // Unlock and close lockfile + /* Unlock and close lockfile */ if (pid_fd != -1) { int ret = lockf(pid_fd, F_ULOCK, 0); if (ret != 0) { @@ -235,7 +222,7 @@ Server::Stop() { } } - // Try to delete lockfile + /* delete lockfile */ if (!pid_filename_.empty()) { int ret = unlink(pid_filename_.c_str()); if (ret != 0) { @@ -245,10 +232,6 @@ Server::Stop() { } StopService(); - { - std::lock_guard lock(mutex_); - running_ = false; - } std::cerr << "Milvus server is closed!" << std::endl; } @@ -271,12 +254,12 @@ Server::StartService() { engine::KnowhereResource::Initialize(); engine::StartSchedulerService(); DBWrapper::GetInstance().StartService(); - grpc::GrpcMilvusServer::GetInstance().Start(); + grpc::GrpcServer::GetInstance().Start(); } void Server::StopService() { - grpc::GrpcMilvusServer::GetInstance().Stop(); + grpc::GrpcServer::GetInstance().Stop(); DBWrapper::GetInstance().StopService(); engine::StopSchedulerService(); engine::KnowhereResource::Finalize(); diff --git a/cpp/src/server/Server.h b/cpp/src/server/Server.h index a4757cd0c649498a83e2f05aacfd17d15aa95192..2c912c3e91e4bfa22df1d6f3172267504e9b0edb 100644 --- a/cpp/src/server/Server.h +++ b/cpp/src/server/Server.h @@ -37,13 +37,11 @@ class Server { const std::string &config_filename, const std::string &log_config_file); - int Start(); - + void Start(); void Stop(); private: Server(); - ~Server(); void Daemonize(); @@ -51,7 +49,6 @@ class Server { ErrorCode LoadConfig(); void StartService(); - void StopService(); private: @@ -60,9 +57,6 @@ class Server { std::string pid_filename_; std::string config_filename_; std::string log_config_file_; - - std::mutex mutex_; - bool running_ = true; }; // Server } // server diff --git a/cpp/src/server/grpc_impl/GrpcRequestTask.cpp b/cpp/src/server/grpc_impl/GrpcRequestTask.cpp index e49ea4823655609286f76abd776d0c2ae96996ff..ad0abd713d1817058694a6388fd3f1581bf03ab7 100644 --- a/cpp/src/server/grpc_impl/GrpcRequestTask.cpp +++ b/cpp/src/server/grpc_impl/GrpcRequestTask.cpp @@ -23,7 +23,7 @@ #include "utils/ValidationUtil.h" #include "../DBWrapper.h" #include "version.h" -#include "GrpcMilvusServer.h" +#include "GrpcServer.h" #include "db/Utils.h" #include "scheduler/SchedInst.h" //#include diff --git a/cpp/src/server/grpc_impl/GrpcMilvusServer.cpp b/cpp/src/server/grpc_impl/GrpcServer.cpp similarity index 90% rename from cpp/src/server/grpc_impl/GrpcMilvusServer.cpp rename to cpp/src/server/grpc_impl/GrpcServer.cpp index 1af355748836b8c326d9a4e3a21c62651a998652..4ff7a8200fabca4bcd6b8b490879dbc731b63418 100644 --- a/cpp/src/server/grpc_impl/GrpcMilvusServer.cpp +++ b/cpp/src/server/grpc_impl/GrpcServer.cpp @@ -16,7 +16,7 @@ // under the License. #include "milvus.grpc.pb.h" -#include "GrpcMilvusServer.h" +#include "GrpcServer.h" #include "server/ServerConfig.h" #include "server/DBWrapper.h" #include "utils/Log.h" @@ -34,7 +34,6 @@ #include #include #include -#include namespace zilliz { @@ -52,18 +51,19 @@ class NoReusePortOption : public ::grpc::ServerBuilderOption { args->SetInt(GRPC_ARG_ALLOW_REUSEPORT, 0); } - void UpdatePlugins(std::vector> * - plugins) override {} + void UpdatePlugins(std::vector> *plugins) override { + + } }; void -GrpcMilvusServer::Start() { - thread_ptr_ = std::make_shared(&GrpcMilvusServer::StartService, this); +GrpcServer::Start() { + thread_ptr_ = std::make_shared(&GrpcServer::StartService, this); } void -GrpcMilvusServer::Stop() { +GrpcServer::Stop() { StopService(); if (thread_ptr_) { thread_ptr_->join(); @@ -72,7 +72,7 @@ GrpcMilvusServer::Stop() { } Status -GrpcMilvusServer::StartService() { +GrpcServer::StartService() { ServerConfig &config = ServerConfig::GetInstance(); ConfigNode server_config = config.GetConfig(CONFIG_SERVER); ConfigNode engine_config = config.GetConfig(CONFIG_ENGINE); @@ -102,7 +102,7 @@ GrpcMilvusServer::StartService() { } Status -GrpcMilvusServer::StopService() { +GrpcServer::StopService() { if (server_ptr_ != nullptr) { server_ptr_->Shutdown(); } diff --git a/cpp/src/server/grpc_impl/GrpcMilvusServer.h b/cpp/src/server/grpc_impl/GrpcServer.h similarity index 87% rename from cpp/src/server/grpc_impl/GrpcMilvusServer.h rename to cpp/src/server/grpc_impl/GrpcServer.h index 40df193a2ffd2edab8c33b370618623cb111f80d..a861692facab5f84f40549f165d32e6e908ec421 100644 --- a/cpp/src/server/grpc_impl/GrpcMilvusServer.h +++ b/cpp/src/server/grpc_impl/GrpcServer.h @@ -24,15 +24,16 @@ #include #include + namespace zilliz { namespace milvus { namespace server { namespace grpc { -class GrpcMilvusServer { +class GrpcServer { public: - static GrpcMilvusServer& GetInstance() { - static GrpcMilvusServer grpc_server; + static GrpcServer &GetInstance() { + static GrpcServer grpc_server; return grpc_server; } @@ -40,8 +41,8 @@ class GrpcMilvusServer { void Stop(); private: - GrpcMilvusServer() = default; - ~GrpcMilvusServer() = default; + GrpcServer() = default; + ~GrpcServer() = default; Status StartService(); Status StopService(); diff --git a/cpp/src/utils/SignalUtil.cpp b/cpp/src/utils/SignalUtil.cpp index 4da8d31eb0b0080e690ee14aa008f830bb8c0b7e..8677016cf365ddb44f9245341633400994e63fab 100644 --- a/cpp/src/utils/SignalUtil.cpp +++ b/cpp/src/utils/SignalUtil.cpp @@ -22,27 +22,28 @@ #include #include + namespace zilliz { namespace milvus { namespace server { -void SignalUtil::HandleSignal(int signum){ +void SignalUtil::HandleSignal(int signum) { - switch(signum){ + switch (signum) { case SIGINT: - case SIGUSR2:{ + case SIGUSR2: { SERVER_LOG_INFO << "Server received signal: " << signum; - server::Server& server_ptr = server::Server::Instance(); + server::Server &server_ptr = server::Server::Instance(); server_ptr.Stop(); exit(0); } - default:{ + default: { SERVER_LOG_INFO << "Server received critical signal: " << signum; SignalUtil::PrintStacktrace(); - server::Server& server_ptr = server::Server::Instance(); + server::Server &server_ptr = server::Server::Instance(); server_ptr.Stop(); exit(1); @@ -54,9 +55,9 @@ void SignalUtil::PrintStacktrace() { SERVER_LOG_INFO << "Call stack:"; const int size = 32; - void* array[size]; + void *array[size]; int stack_num = backtrace(array, size); - char ** stacktrace = backtrace_symbols(array, stack_num); + char **stacktrace = backtrace_symbols(array, stack_num); for (int i = 0; i < stack_num; ++i) { std::string info = stacktrace[i]; SERVER_LOG_INFO << info; @@ -64,7 +65,6 @@ void SignalUtil::PrintStacktrace() { free(stacktrace); } - } } }