diff --git a/serving/core/server.cc b/serving/core/server.cc index 797793a3b44f68e8126bb15e10f9db65109a6199..238ab7352a5a8c782663b06a33e13ff8e19813c9 100644 --- a/serving/core/server.cc +++ b/serving/core/server.cc @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -33,7 +34,6 @@ #include "core/serving_tensor.h" #include "core/http_process.h" - using ms_serving::MSService; using ms_serving::PredictReply; using ms_serving::PredictRequest; @@ -89,6 +89,55 @@ class MSServiceImpl final : public MSService::Service { std::mutex mutex_; }; +static std::pair NewHttpServer() { + auto option_args = Options::Instance().GetArgs(); + int32_t http_port = option_args->rest_api_port; + // init http server + event_init(); + evthread_use_pthreads(); + struct event_base *eb = event_base_new(); + if (eb == nullptr) { + MSI_LOG(ERROR) << "Serving Error: RESTful server start failed, new http event failed"; + std::cout << "Serving Error: RESTful server start failed, new http event failed" << std::endl; + return std::make_pair(nullptr, nullptr); + } + struct evhttp *http_server = evhttp_new(eb); + if (http_server == nullptr) { + MSI_LOG(ERROR) << "Serving Error: RESTful server start failed, create http server faild"; + std::cout << "Serving Error: RESTful server start failed, create http server faild" << std::endl; + event_base_free(eb); + return std::make_pair(nullptr, nullptr); + } + + struct sockaddr_in sin = {}; + sin.sin_family = AF_INET; + sin.sin_port = htons(http_port); + auto listener = + evconnlistener_new_bind(eb, nullptr, nullptr, LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_EXEC | LEV_OPT_CLOSE_ON_FREE, -1, + reinterpret_cast(&sin), sizeof(sin)); + + if (listener == nullptr) { + MSI_LOG_ERROR << "Serving Error: RESTful server start failed, create http listener faild, port " << http_port; + std::cout << "Serving Error: RESTful server start failed, create http listener faild, port " << http_port + << std::endl; + event_base_free(eb); + evhttp_free(http_server); + return std::make_pair(nullptr, nullptr); + } + auto bound = evhttp_bind_listener(http_server, listener); + if (bound == nullptr) { + MSI_LOG_ERROR << "Serving Error: RESTful server start failed, bind http listener to server faild, port " + << http_port; + std::cout << "Serving Error: RESTful server start failed, bind http listener to server faild, port " << http_port + << std::endl; + evconnlistener_free(listener); + event_base_free(eb); + evhttp_free(http_server); + return std::make_pair(nullptr, nullptr); + } + return std::make_pair(http_server, eb); +} + Status Server::BuildAndStart() { // handle exit signal signal(SIGINT, HandleSignal); @@ -103,35 +152,41 @@ Status Server::BuildAndStart() { auto device_id = option_args->device_id; res = Session::Instance().CreatDeviceSession(device_type, device_id); if (res != SUCCESS) { - MSI_LOG(ERROR) << "creat session failed"; + MSI_LOG(ERROR) << "Serving Error: create inference session failed, device type " << device_type << " device id " + << device_id; + std::cout << "Serving Error: create inference session failed, device type " << device_type << " device id " + << device_id << std::endl; ClearEnv(); return res; } VersionController version_controller(option_args->poll_model_wait_seconds, model_path, model_name); res = version_controller.Run(); if (res != SUCCESS) { - MSI_LOG(ERROR) << "load model failed"; + MSI_LOG(ERROR) << "Serving Error: load model failed, model directory " << option_args->model_path << " model name " + << option_args->model_name; + std::cout << "Serving Error: load model failed, model directory " << option_args->model_path << " model name " + << option_args->model_name << std::endl; ClearEnv(); return res; } - - // init http server - struct evhttp *http_server = NULL; - struct event_base *eb = NULL; + auto http_server_new_ret = NewHttpServer(); + struct evhttp *http_server = http_server_new_ret.first; + struct event_base *eb = http_server_new_ret.second; + if (http_server == nullptr || eb == nullptr) { + MSI_LOG(ERROR) << "Serving Error: RESTful server start failed"; + std::cout << "Serving Error: RESTful server start failed" << std::endl; + ClearEnv(); + return FAILED; + } + auto exit_http = [eb, http_server]() { + evhttp_free(http_server); + event_base_free(eb); + }; int32_t http_port = option_args->rest_api_port; std::string http_addr = "0.0.0.0"; - event_init(); - evthread_use_pthreads(); - eb = event_base_new(); - http_server = evhttp_new(eb); - evhttp_bind_socket_with_handle(http_server, http_addr.c_str(), http_port); - // http_server = evhttp_start(http_addr.c_str(), http_port); - if (http_server == NULL) { - MSI_LOG(ERROR) << "http server start failed."; - return res; - } + evhttp_set_timeout(http_server, 5); - evhttp_set_gencb(http_server, http_handler_msg, NULL); + evhttp_set_gencb(http_server, http_handler_msg, nullptr); // grpc server MSServiceImpl ms_service; @@ -146,16 +201,27 @@ Status Server::BuildAndStart() { serverBuilder.RegisterService(&ms_service); std::unique_ptr server(serverBuilder.BuildAndStart()); if (server == nullptr) { - MSI_LOG(ERROR) << "The serving server create failed"; + MSI_LOG(ERROR) << "Serving Error: create server failed, gRPC address " << server_address << ", RESTful address " + << http_addr << ":" << http_port << ", model directory " << option_args->model_path << " model name " + << option_args->model_name << ", device type " << option_args->device_type << ", device id " + << option_args->device_id; + std::cout << "Serving Error: create server failed, gRPC address " << server_address << ", RESTful address " + << http_addr << ":" << http_port << ", model directory " << option_args->model_path << " model name " + << option_args->model_name << ", device type " << option_args->device_type << ", device id " + << option_args->device_id << std::endl; ClearEnv(); + exit_http(); return FAILED; } auto grpc_server_run = [&server, &server_address]() { MSI_LOG(INFO) << "MS Serving grpc listening on " << server_address; + std::cout << "Serving: MS Serving gRPC start success, listening on " << server_address << std::endl; server->Wait(); }; auto http_server_run = [&eb, &http_addr, &http_port]() { MSI_LOG(INFO) << "MS Serving restful listening on " << http_addr << ":" << http_port; + std::cout << "Serving: MS Serving RESTful start success, listening on " << http_addr << ":" << http_port + << std::endl; event_base_dispatch(eb); }; std::thread grpc_thread(grpc_server_run); @@ -164,7 +230,8 @@ Status Server::BuildAndStart() { exit_future.wait(); ClearEnv(); server->Shutdown(); - event_base_loopexit(eb, NULL); + event_base_loopexit(eb, nullptr); + exit_http(); grpc_thread.join(); restful_thread.join(); return SUCCESS; diff --git a/serving/core/util/option_parser.cc b/serving/core/util/option_parser.cc index 4a71176a321eb7ae920973553a6b6f7935c92113..b460ea7857b1dc5850ade2f475056ea2ab00aff8 100644 --- a/serving/core/util/option_parser.cc +++ b/serving/core/util/option_parser.cc @@ -171,27 +171,27 @@ void Options::CreateOptions() { bool Options::CheckOptions() { if (args_->model_name == "" || args_->model_path == "") { - std::cout << "model_path and model_name should not be null" << std::endl; + std::cout << "Serving Error: model_path and model_name should not be null" << std::endl; return false; } if (args_->device_type != "Ascend") { - std::cout << "device_type only support Ascend right now" << std::endl; + std::cout << "Serving Error: device_type only support Ascend right now" << std::endl; return false; } if (args_->device_id > 7) { - std::cout << "the device_id should be in [0~7]" << std::endl; + std::cout << "Serving Error: the device_id should be in [0~7]" << std::endl; return false; } if (args_->grpc_port < 1 || args_->grpc_port > 65535) { - std::cout << "the port should be in [1~65535]" << std::endl; + std::cout << "Serving Error: the port should be in [1~65535]" << std::endl; return false; } if (args_->rest_api_port < 1 || args_->rest_api_port > 65535) { - std::cout << "the rest_api_port should be in [1~65535]" << std::endl; + std::cout << "Serving Error: the rest_api_port should be in [1~65535]" << std::endl; return false; } if (args_->rest_api_port == args_->grpc_port) { - std::cout << "the rest_api_port and grpc port should not be same" << std::endl; + std::cout << "Serving Error: the rest_api_port and grpc port should not be same" << std::endl; return false; } return true;