提交 bdef0f62 编写于 作者: M mindspore-ci-bot 提交者: Gitee

!5151 serving RESTful, disable http port reuse, update error msg output to user

Merge pull request !5151 from 徐永飞/master
......@@ -17,6 +17,7 @@
#include <evhttp.h>
#include <event.h>
#include <event2/thread.h>
#include <event2/listener.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
......@@ -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<struct evhttp *, struct event_base *> 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<struct sockaddr *>(&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<grpc::Server> 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;
......
......@@ -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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册