You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.cc 9.5 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "core/server.h"
  17. #include <evhttp.h>
  18. #include <event.h>
  19. #include <event2/thread.h>
  20. #include <event2/listener.h>
  21. #include <grpcpp/grpcpp.h>
  22. #include <grpcpp/health_check_service_interface.h>
  23. #include <grpcpp/ext/proto_server_reflection_plugin.h>
  24. #include <future>
  25. #include <memory>
  26. #include <string>
  27. #include <vector>
  28. #include <utility>
  29. #include "include/infer_log.h"
  30. #include "serving/ms_service.grpc.pb.h"
  31. #include "core/util/option_parser.h"
  32. #include "core/version_control/version_controller.h"
  33. #include "core/session.h"
  34. #include "core/serving_tensor.h"
  35. #include "core/http_process.h"
  36. using ms_serving::MSService;
  37. using ms_serving::PredictReply;
  38. using ms_serving::PredictRequest;
  39. namespace mindspore {
  40. namespace serving {
  41. namespace {
  42. static const uint32_t uint32max = 0x7FFFFFFF;
  43. std::promise<void> exit_requested;
  44. void ClearEnv() { Session::Instance().Clear(); }
  45. void HandleSignal(int sig) { exit_requested.set_value(); }
  46. grpc::Status CreatGRPCStatus(const Status &status) {
  47. switch (status.StatusCode()) {
  48. case SUCCESS:
  49. return grpc::Status::OK;
  50. case FAILED:
  51. return grpc::Status::CANCELLED;
  52. case INVALID_INPUTS: {
  53. auto status_msg = status.StatusMessage();
  54. if (status_msg.empty()) {
  55. status_msg = "The Predict Inputs do not match the Model Request!";
  56. }
  57. return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, status_msg);
  58. }
  59. default:
  60. return grpc::Status::CANCELLED;
  61. }
  62. }
  63. } // namespace
  64. // Service Implement
  65. class MSServiceImpl final : public MSService::Service {
  66. grpc::Status Predict(grpc::ServerContext *context, const PredictRequest *request, PredictReply *reply) override {
  67. std::lock_guard<std::mutex> lock(mutex_);
  68. MSI_TIME_STAMP_START(Predict)
  69. auto res = Session::Instance().Predict(*request, *reply);
  70. MSI_TIME_STAMP_END(Predict)
  71. if (res != inference::SUCCESS) {
  72. return CreatGRPCStatus(res);
  73. }
  74. MSI_LOG(INFO) << "Finish call service Eval";
  75. return grpc::Status::OK;
  76. }
  77. grpc::Status Test(grpc::ServerContext *context, const PredictRequest *request, PredictReply *reply) override {
  78. MSI_LOG(INFO) << "TestService call";
  79. return grpc::Status::OK;
  80. }
  81. std::mutex mutex_;
  82. };
  83. static std::pair<struct evhttp *, struct event_base *> NewHttpServer() {
  84. auto option_args = Options::Instance().GetArgs();
  85. int32_t http_port = option_args->rest_api_port;
  86. // init http server
  87. event_init();
  88. evthread_use_pthreads();
  89. struct event_base *eb = event_base_new();
  90. if (eb == nullptr) {
  91. MSI_LOG(ERROR) << "Serving Error: RESTful server start failed, new http event failed";
  92. std::cout << "Serving Error: RESTful server start failed, new http event failed" << std::endl;
  93. return std::make_pair(nullptr, nullptr);
  94. }
  95. struct evhttp *http_server = evhttp_new(eb);
  96. if (http_server == nullptr) {
  97. MSI_LOG(ERROR) << "Serving Error: RESTful server start failed, create http server faild";
  98. std::cout << "Serving Error: RESTful server start failed, create http server faild" << std::endl;
  99. event_base_free(eb);
  100. return std::make_pair(nullptr, nullptr);
  101. }
  102. struct sockaddr_in sin = {};
  103. sin.sin_family = AF_INET;
  104. sin.sin_port = htons(http_port);
  105. auto listener =
  106. evconnlistener_new_bind(eb, nullptr, nullptr, LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_EXEC | LEV_OPT_CLOSE_ON_FREE, -1,
  107. reinterpret_cast<struct sockaddr *>(&sin), sizeof(sin));
  108. if (listener == nullptr) {
  109. MSI_LOG_ERROR << "Serving Error: RESTful server start failed, create http listener faild, port " << http_port;
  110. std::cout << "Serving Error: RESTful server start failed, create http listener faild, port " << http_port
  111. << std::endl;
  112. event_base_free(eb);
  113. evhttp_free(http_server);
  114. return std::make_pair(nullptr, nullptr);
  115. }
  116. auto bound = evhttp_bind_listener(http_server, listener);
  117. if (bound == nullptr) {
  118. MSI_LOG_ERROR << "Serving Error: RESTful server start failed, bind http listener to server faild, port "
  119. << http_port;
  120. std::cout << "Serving Error: RESTful server start failed, bind http listener to server faild, port " << http_port
  121. << std::endl;
  122. evconnlistener_free(listener);
  123. event_base_free(eb);
  124. evhttp_free(http_server);
  125. return std::make_pair(nullptr, nullptr);
  126. }
  127. return std::make_pair(http_server, eb);
  128. }
  129. Status Server::BuildAndStart() {
  130. // handle exit signal
  131. signal(SIGINT, HandleSignal);
  132. signal(SIGTERM, HandleSignal);
  133. Status res;
  134. auto option_args = Options::Instance().GetArgs();
  135. std::string server_address = "0.0.0.0:" + std::to_string(option_args->grpc_port);
  136. std::string model_path = option_args->model_path;
  137. std::string model_name = option_args->model_name;
  138. std::string device_type = option_args->device_type;
  139. auto device_id = option_args->device_id;
  140. res = Session::Instance().CreatDeviceSession(device_type, device_id);
  141. if (res != SUCCESS) {
  142. MSI_LOG(ERROR) << "Serving Error: create inference session failed, device type " << device_type << " device id "
  143. << device_id;
  144. std::cout << "Serving Error: create inference session failed, device type " << device_type << " device id "
  145. << device_id << std::endl;
  146. ClearEnv();
  147. return res;
  148. }
  149. VersionController version_controller(option_args->poll_model_wait_seconds, model_path, model_name);
  150. res = version_controller.Run();
  151. if (res != SUCCESS) {
  152. MSI_LOG(ERROR) << "Serving Error: load model failed, model directory " << option_args->model_path << " model name "
  153. << option_args->model_name;
  154. std::cout << "Serving Error: load model failed, model directory " << option_args->model_path << " model name "
  155. << option_args->model_name << std::endl;
  156. ClearEnv();
  157. return res;
  158. }
  159. auto http_server_new_ret = NewHttpServer();
  160. struct evhttp *http_server = http_server_new_ret.first;
  161. struct event_base *eb = http_server_new_ret.second;
  162. if (http_server == nullptr || eb == nullptr) {
  163. MSI_LOG(ERROR) << "Serving Error: RESTful server start failed";
  164. std::cout << "Serving Error: RESTful server start failed" << std::endl;
  165. ClearEnv();
  166. return FAILED;
  167. }
  168. auto exit_http = [eb, http_server]() {
  169. evhttp_free(http_server);
  170. event_base_free(eb);
  171. };
  172. int32_t http_port = option_args->rest_api_port;
  173. std::string http_addr = "0.0.0.0";
  174. evhttp_set_timeout(http_server, 60);
  175. evhttp_set_gencb(http_server, http_handler_msg, nullptr);
  176. // grpc server
  177. MSServiceImpl ms_service;
  178. grpc::EnableDefaultHealthCheckService(true);
  179. grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  180. // Set the port is not reuseable
  181. auto option = grpc::MakeChannelArgumentOption(GRPC_ARG_ALLOW_REUSEPORT, 0);
  182. grpc::ServerBuilder serverBuilder;
  183. serverBuilder.SetOption(std::move(option));
  184. serverBuilder.SetMaxMessageSize(uint32max);
  185. serverBuilder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  186. serverBuilder.RegisterService(&ms_service);
  187. std::unique_ptr<grpc::Server> server(serverBuilder.BuildAndStart());
  188. if (server == nullptr) {
  189. MSI_LOG(ERROR) << "Serving Error: create server failed, gRPC address " << server_address << ", RESTful address "
  190. << http_addr << ":" << http_port << ", model directory " << option_args->model_path << " model name "
  191. << option_args->model_name << ", device type " << option_args->device_type << ", device id "
  192. << option_args->device_id;
  193. std::cout << "Serving Error: create server failed, gRPC address " << server_address << ", RESTful address "
  194. << http_addr << ":" << http_port << ", model directory " << option_args->model_path << " model name "
  195. << option_args->model_name << ", device type " << option_args->device_type << ", device id "
  196. << option_args->device_id << std::endl;
  197. ClearEnv();
  198. exit_http();
  199. return FAILED;
  200. }
  201. auto grpc_server_run = [&server, &server_address]() {
  202. MSI_LOG(INFO) << "MS Serving grpc listening on " << server_address;
  203. std::cout << "Serving: MS Serving gRPC start success, listening on " << server_address << std::endl;
  204. server->Wait();
  205. };
  206. auto http_server_run = [&eb, &http_addr, &http_port]() {
  207. MSI_LOG(INFO) << "MS Serving restful listening on " << http_addr << ":" << http_port;
  208. std::cout << "Serving: MS Serving RESTful start success, listening on " << http_addr << ":" << http_port
  209. << std::endl;
  210. event_base_dispatch(eb);
  211. };
  212. std::thread grpc_thread(grpc_server_run);
  213. std::thread restful_thread(http_server_run);
  214. auto exit_future = exit_requested.get_future();
  215. exit_future.wait();
  216. ClearEnv();
  217. server->Shutdown();
  218. event_base_loopexit(eb, nullptr);
  219. exit_http();
  220. grpc_thread.join();
  221. restful_thread.join();
  222. return SUCCESS;
  223. }
  224. } // namespace serving
  225. } // namespace mindspore