2020-06-21 14:37:27 +08:00
|
|
|
/**
|
|
|
|
* Copyright 2020 Huawei Technologies Co., Ltd
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
#include "core/server.h"
|
2020-08-18 19:31:57 +08:00
|
|
|
#include <evhttp.h>
|
|
|
|
#include <event.h>
|
|
|
|
#include <event2/thread.h>
|
2020-08-25 17:38:28 +08:00
|
|
|
#include <event2/listener.h>
|
2020-06-21 14:37:27 +08:00
|
|
|
#include <grpcpp/grpcpp.h>
|
|
|
|
#include <grpcpp/health_check_service_interface.h>
|
|
|
|
#include <grpcpp/ext/proto_server_reflection_plugin.h>
|
2020-08-18 19:31:57 +08:00
|
|
|
#include <future>
|
|
|
|
#include <memory>
|
2020-06-21 14:37:27 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <utility>
|
2020-07-21 11:19:00 +08:00
|
|
|
#include "include/infer_log.h"
|
2020-06-21 14:37:27 +08:00
|
|
|
#include "serving/ms_service.grpc.pb.h"
|
|
|
|
#include "core/util/option_parser.h"
|
|
|
|
#include "core/version_control/version_controller.h"
|
2020-08-18 19:31:57 +08:00
|
|
|
#include "core/session.h"
|
2020-07-21 11:19:00 +08:00
|
|
|
#include "core/serving_tensor.h"
|
2020-08-18 19:31:57 +08:00
|
|
|
#include "core/http_process.h"
|
|
|
|
|
2020-06-21 14:37:27 +08:00
|
|
|
using ms_serving::MSService;
|
|
|
|
using ms_serving::PredictReply;
|
|
|
|
using ms_serving::PredictRequest;
|
|
|
|
|
|
|
|
namespace mindspore {
|
|
|
|
namespace serving {
|
2020-07-21 11:19:00 +08:00
|
|
|
|
2020-06-21 14:37:27 +08:00
|
|
|
namespace {
|
2020-06-27 10:01:06 +08:00
|
|
|
static const uint32_t uint32max = 0x7FFFFFFF;
|
|
|
|
std::promise<void> exit_requested;
|
|
|
|
|
2020-07-31 16:23:52 +08:00
|
|
|
void ClearEnv() { Session::Instance().Clear(); }
|
2020-06-27 10:01:06 +08:00
|
|
|
void HandleSignal(int sig) { exit_requested.set_value(); }
|
2020-06-21 14:37:27 +08:00
|
|
|
|
2020-08-03 14:49:30 +08:00
|
|
|
grpc::Status CreatGRPCStatus(const Status &status) {
|
|
|
|
switch (status.StatusCode()) {
|
2020-07-31 16:23:52 +08:00
|
|
|
case SUCCESS:
|
|
|
|
return grpc::Status::OK;
|
|
|
|
case FAILED:
|
|
|
|
return grpc::Status::CANCELLED;
|
2020-08-03 14:49:30 +08:00
|
|
|
case INVALID_INPUTS: {
|
|
|
|
auto status_msg = status.StatusMessage();
|
|
|
|
if (status_msg.empty()) {
|
|
|
|
status_msg = "The Predict Inputs do not match the Model Request!";
|
|
|
|
}
|
|
|
|
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, status_msg);
|
|
|
|
}
|
2020-07-31 16:23:52 +08:00
|
|
|
default:
|
|
|
|
return grpc::Status::CANCELLED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 14:37:27 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Service Implement
|
|
|
|
class MSServiceImpl final : public MSService::Service {
|
|
|
|
grpc::Status Predict(grpc::ServerContext *context, const PredictRequest *request, PredictReply *reply) override {
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_TIME_STAMP_START(Predict)
|
|
|
|
auto res = Session::Instance().Predict(*request, *reply);
|
|
|
|
MSI_TIME_STAMP_END(Predict)
|
2020-07-31 16:23:52 +08:00
|
|
|
if (res != inference::SUCCESS) {
|
|
|
|
return CreatGRPCStatus(res);
|
2020-06-21 14:37:27 +08:00
|
|
|
}
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_LOG(INFO) << "Finish call service Eval";
|
2020-06-21 14:37:27 +08:00
|
|
|
return grpc::Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
grpc::Status Test(grpc::ServerContext *context, const PredictRequest *request, PredictReply *reply) override {
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_LOG(INFO) << "TestService call";
|
2020-06-21 14:37:27 +08:00
|
|
|
return grpc::Status::OK;
|
|
|
|
}
|
|
|
|
std::mutex mutex_;
|
|
|
|
};
|
|
|
|
|
2020-08-25 17:38:28 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-06-21 14:37:27 +08:00
|
|
|
Status Server::BuildAndStart() {
|
|
|
|
// handle exit signal
|
|
|
|
signal(SIGINT, HandleSignal);
|
2020-07-01 14:52:34 +08:00
|
|
|
signal(SIGTERM, HandleSignal);
|
2020-06-21 14:37:27 +08:00
|
|
|
Status res;
|
2020-08-18 19:31:57 +08:00
|
|
|
|
2020-06-21 14:37:27 +08:00
|
|
|
auto option_args = Options::Instance().GetArgs();
|
|
|
|
std::string server_address = "0.0.0.0:" + std::to_string(option_args->grpc_port);
|
|
|
|
std::string model_path = option_args->model_path;
|
|
|
|
std::string model_name = option_args->model_name;
|
|
|
|
std::string device_type = option_args->device_type;
|
|
|
|
auto device_id = option_args->device_id;
|
|
|
|
res = Session::Instance().CreatDeviceSession(device_type, device_id);
|
|
|
|
if (res != SUCCESS) {
|
2020-08-25 17:38:28 +08:00
|
|
|
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;
|
2020-06-21 14:37:27 +08:00
|
|
|
ClearEnv();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
VersionController version_controller(option_args->poll_model_wait_seconds, model_path, model_name);
|
|
|
|
res = version_controller.Run();
|
|
|
|
if (res != SUCCESS) {
|
2020-08-25 17:38:28 +08:00
|
|
|
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;
|
2020-06-21 14:37:27 +08:00
|
|
|
ClearEnv();
|
|
|
|
return res;
|
|
|
|
}
|
2020-08-25 17:38:28 +08:00
|
|
|
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);
|
|
|
|
};
|
2020-08-18 19:31:57 +08:00
|
|
|
int32_t http_port = option_args->rest_api_port;
|
|
|
|
std::string http_addr = "0.0.0.0";
|
2020-08-25 17:38:28 +08:00
|
|
|
|
2020-08-26 16:35:32 +08:00
|
|
|
evhttp_set_timeout(http_server, 60);
|
2020-08-25 17:38:28 +08:00
|
|
|
evhttp_set_gencb(http_server, http_handler_msg, nullptr);
|
2020-08-18 19:31:57 +08:00
|
|
|
|
|
|
|
// grpc server
|
2020-07-03 17:01:24 +08:00
|
|
|
MSServiceImpl ms_service;
|
2020-06-21 14:37:27 +08:00
|
|
|
grpc::EnableDefaultHealthCheckService(true);
|
|
|
|
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
|
|
|
|
// Set the port is not reuseable
|
|
|
|
auto option = grpc::MakeChannelArgumentOption(GRPC_ARG_ALLOW_REUSEPORT, 0);
|
2020-07-01 14:52:34 +08:00
|
|
|
grpc::ServerBuilder serverBuilder;
|
|
|
|
serverBuilder.SetOption(std::move(option));
|
|
|
|
serverBuilder.SetMaxMessageSize(uint32max);
|
|
|
|
serverBuilder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
|
2020-07-03 17:01:24 +08:00
|
|
|
serverBuilder.RegisterService(&ms_service);
|
2020-07-01 14:52:34 +08:00
|
|
|
std::unique_ptr<grpc::Server> server(serverBuilder.BuildAndStart());
|
2020-06-27 14:57:26 +08:00
|
|
|
if (server == nullptr) {
|
2020-08-25 17:38:28 +08:00
|
|
|
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;
|
2020-06-27 14:57:26 +08:00
|
|
|
ClearEnv();
|
2020-08-25 17:38:28 +08:00
|
|
|
exit_http();
|
2020-06-27 14:57:26 +08:00
|
|
|
return FAILED;
|
|
|
|
}
|
2020-08-18 19:31:57 +08:00
|
|
|
auto grpc_server_run = [&server, &server_address]() {
|
|
|
|
MSI_LOG(INFO) << "MS Serving grpc listening on " << server_address;
|
2020-08-25 17:38:28 +08:00
|
|
|
std::cout << "Serving: MS Serving gRPC start success, listening on " << server_address << std::endl;
|
2020-08-18 19:31:57 +08:00
|
|
|
server->Wait();
|
|
|
|
};
|
|
|
|
auto http_server_run = [&eb, &http_addr, &http_port]() {
|
|
|
|
MSI_LOG(INFO) << "MS Serving restful listening on " << http_addr << ":" << http_port;
|
2020-08-25 17:38:28 +08:00
|
|
|
std::cout << "Serving: MS Serving RESTful start success, listening on " << http_addr << ":" << http_port
|
|
|
|
<< std::endl;
|
2020-08-18 19:31:57 +08:00
|
|
|
event_base_dispatch(eb);
|
|
|
|
};
|
|
|
|
std::thread grpc_thread(grpc_server_run);
|
|
|
|
std::thread restful_thread(http_server_run);
|
2020-06-27 10:01:06 +08:00
|
|
|
auto exit_future = exit_requested.get_future();
|
|
|
|
exit_future.wait();
|
|
|
|
ClearEnv();
|
|
|
|
server->Shutdown();
|
2020-08-25 17:38:28 +08:00
|
|
|
event_base_loopexit(eb, nullptr);
|
|
|
|
exit_http();
|
2020-08-18 19:31:57 +08:00
|
|
|
grpc_thread.join();
|
|
|
|
restful_thread.join();
|
2020-06-21 14:37:27 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
} // namespace serving
|
|
|
|
} // namespace mindspore
|