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.

http_process.cc 20 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
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 <map>
  17. #include <vector>
  18. #include <string>
  19. #include <functional>
  20. #include <utility>
  21. #include <nlohmann/json.hpp>
  22. #include "serving/ms_service.pb.h"
  23. #include "util/status.h"
  24. #include "core/session.h"
  25. #include "core/http_process.h"
  26. #include "core/serving_tensor.h"
  27. using ms_serving::MSService;
  28. using ms_serving::PredictReply;
  29. using ms_serving::PredictRequest;
  30. using nlohmann::json;
  31. namespace mindspore {
  32. namespace serving {
  33. const int BUF_MAX = 0x7FFFFFFF;
  34. static constexpr char HTTP_DATA[] = "data";
  35. static constexpr char HTTP_TENSOR[] = "tensor";
  36. enum HTTP_TYPE { TYPE_DATA = 0, TYPE_TENSOR };
  37. enum HTTP_DATA_TYPE { HTTP_DATA_NONE, HTTP_DATA_INT, HTTP_DATA_FLOAT };
  38. static const std::map<inference::DataType, HTTP_DATA_TYPE> infer_type2_http_type{
  39. {inference::DataType::kMSI_Int32, HTTP_DATA_INT}, {inference::DataType::kMSI_Float32, HTTP_DATA_FLOAT}};
  40. Status GetPostMessage(struct evhttp_request *const req, std::string *const buf) {
  41. Status status(SUCCESS);
  42. size_t post_size = evbuffer_get_length(req->input_buffer);
  43. if (post_size == 0) {
  44. ERROR_INFER_STATUS(status, INVALID_INPUTS, "http message invalid");
  45. return status;
  46. } else if (post_size > BUF_MAX) {
  47. ERROR_INFER_STATUS(status, INVALID_INPUTS, "http message is bigger than 0x7FFFFFFF.");
  48. return status;
  49. } else {
  50. buf->resize(post_size);
  51. auto src_data = evbuffer_pullup(req->input_buffer, -1);
  52. if (src_data == nullptr) {
  53. ERROR_INFER_STATUS(status, FAILED, "get http message failed.");
  54. return status;
  55. }
  56. if (memcpy_s(buf->data(), post_size, src_data, post_size) != EOK) {
  57. ERROR_INFER_STATUS(status, FAILED, "copy http message failed.");
  58. return status;
  59. }
  60. return status;
  61. }
  62. }
  63. Status CheckRequestValid(const struct evhttp_request *const http_request) {
  64. Status status(SUCCESS);
  65. switch (evhttp_request_get_command(http_request)) {
  66. case EVHTTP_REQ_POST:
  67. return status;
  68. default:
  69. ERROR_INFER_STATUS(status, INVALID_INPUTS, "http message only support POST right now");
  70. return status;
  71. }
  72. }
  73. void ErrorMessage(struct evhttp_request *const req, Status status) {
  74. json error_json = {{"error_message", status.StatusMessage()}};
  75. std::string out_error_str = error_json.dump();
  76. struct evbuffer *retbuff = evbuffer_new();
  77. evbuffer_add(retbuff, out_error_str.data(), out_error_str.size());
  78. evhttp_send_reply(req, HTTP_OK, "Client", retbuff);
  79. evbuffer_free(retbuff);
  80. }
  81. Status CheckMessageValid(const json &message_info, HTTP_TYPE *const type) {
  82. Status status(SUCCESS);
  83. int count = 0;
  84. if (message_info.find(HTTP_DATA) != message_info.end()) {
  85. *type = TYPE_DATA;
  86. count++;
  87. }
  88. if (message_info.find(HTTP_TENSOR) != message_info.end()) {
  89. *type = TYPE_TENSOR;
  90. count++;
  91. }
  92. if (count != 1) {
  93. ERROR_INFER_STATUS(status, INVALID_INPUTS, "http message must have only one type of (data, tensor)");
  94. return status;
  95. }
  96. return status;
  97. }
  98. std::vector<int64_t> GetJsonArrayShape(const json &json_array) {
  99. std::vector<int64_t> json_shape;
  100. const json *tmp_json = &json_array;
  101. while (tmp_json->is_array()) {
  102. if (tmp_json->empty()) {
  103. break;
  104. }
  105. json_shape.push_back(tmp_json->size());
  106. tmp_json = &tmp_json->at(0);
  107. }
  108. return json_shape;
  109. }
  110. Status GetScalarDataFromJson(const json &json_data_array, ServingTensor *const request_tensor, HTTP_DATA_TYPE type) {
  111. Status status(SUCCESS);
  112. auto type_name = [](const json &json_data) -> std::string {
  113. if (json_data.is_number_integer()) {
  114. return "integer";
  115. } else if (json_data.is_number_float()) {
  116. return "float";
  117. }
  118. return json_data.type_name();
  119. };
  120. const json *json_data = &json_data_array;
  121. if (json_data_array.is_array()) {
  122. if (json_data_array.size() != 1 || json_data_array[0].is_array()) {
  123. status = INFER_STATUS(INVALID_INPUTS) << "get data failed, expected scalar data is scalar or shape(1) array, "
  124. "now array shape is "
  125. << GetJsonArrayShape(json_data_array);
  126. MSI_LOG_ERROR << status.StatusMessage();
  127. return status;
  128. }
  129. json_data = &json_data_array.at(0);
  130. }
  131. if (type == HTTP_DATA_INT) {
  132. auto data = reinterpret_cast<int32_t *>(request_tensor->mutable_data());
  133. if (!json_data->is_number_integer()) {
  134. status = INFER_STATUS(INVALID_INPUTS) << "get data failed, expected integer, given " << type_name(*json_data);
  135. MSI_LOG_ERROR << status.StatusMessage();
  136. return status;
  137. }
  138. data[0] = json_data->get<int32_t>();
  139. } else if (type == HTTP_DATA_FLOAT) {
  140. auto data = reinterpret_cast<float *>(request_tensor->mutable_data());
  141. if (!json_data->is_number_float()) {
  142. status = INFER_STATUS(INVALID_INPUTS) << "get data failed, expected float, given " << type_name(*json_data);
  143. MSI_LOG_ERROR << status.StatusMessage();
  144. return status;
  145. }
  146. data[0] = json_data->get<float>();
  147. }
  148. return SUCCESS;
  149. }
  150. Status GetDataFromJson(const json &json_data_array, ServingTensor *const request_tensor, size_t data_index,
  151. HTTP_DATA_TYPE type) {
  152. Status status(SUCCESS);
  153. auto type_name = [](const json &json_data) -> std::string {
  154. if (json_data.is_number_integer()) {
  155. return "integer";
  156. } else if (json_data.is_number_float()) {
  157. return "float";
  158. }
  159. return json_data.type_name();
  160. };
  161. size_t array_size = json_data_array.size();
  162. if (type == HTTP_DATA_INT) {
  163. auto data = reinterpret_cast<int32_t *>(request_tensor->mutable_data()) + data_index;
  164. for (size_t k = 0; k < array_size; k++) {
  165. auto &json_data = json_data_array[k];
  166. if (!json_data.is_number_integer()) {
  167. status = INFER_STATUS(INVALID_INPUTS) << "get data failed, expected integer, given " << type_name(json_data);
  168. MSI_LOG_ERROR << status.StatusMessage();
  169. return status;
  170. }
  171. data[k] = json_data.get<int32_t>();
  172. }
  173. } else if (type == HTTP_DATA_FLOAT) {
  174. auto data = reinterpret_cast<float *>(request_tensor->mutable_data()) + data_index;
  175. for (size_t k = 0; k < array_size; k++) {
  176. auto &json_data = json_data_array[k];
  177. if (!json_data.is_number_float()) {
  178. status = INFER_STATUS(INVALID_INPUTS) << "get data failed, expected float, given " << type_name(json_data);
  179. MSI_LOG_ERROR << status.StatusMessage();
  180. return status;
  181. }
  182. data[k] = json_data.get<float>();
  183. }
  184. }
  185. return SUCCESS;
  186. }
  187. Status RecusiveGetTensor(const json &json_data, size_t depth, ServingTensor *const request_tensor, size_t data_index,
  188. HTTP_DATA_TYPE type) {
  189. Status status(SUCCESS);
  190. std::vector<int64_t> required_shape = request_tensor->shape();
  191. if (depth >= required_shape.size()) {
  192. status = INFER_STATUS(INVALID_INPUTS)
  193. << "input tensor shape dims is more than required dims " << required_shape.size();
  194. MSI_LOG_ERROR << status.StatusMessage();
  195. return status;
  196. }
  197. if (!json_data.is_array()) {
  198. ERROR_INFER_STATUS(status, INVALID_INPUTS, "the tensor is constructed illegally");
  199. return status;
  200. }
  201. if (json_data.size() != static_cast<size_t>(required_shape[depth])) {
  202. status = INFER_STATUS(INVALID_INPUTS)
  203. << "tensor format request is constructed illegally, input tensor shape dim " << depth
  204. << " not match, required " << required_shape[depth] << ", given " << json_data.size();
  205. MSI_LOG_ERROR << status.StatusMessage();
  206. return status;
  207. }
  208. if (depth + 1 < required_shape.size()) {
  209. size_t sub_element_cnt =
  210. std::accumulate(required_shape.begin() + depth + 1, required_shape.end(), 1LL, std::multiplies<size_t>());
  211. for (size_t k = 0; k < json_data.size(); k++) {
  212. status = RecusiveGetTensor(json_data[k], depth + 1, request_tensor, data_index + sub_element_cnt * k, type);
  213. if (status != SUCCESS) {
  214. return status;
  215. }
  216. }
  217. } else {
  218. status = GetDataFromJson(json_data, request_tensor, data_index, type);
  219. if (status != SUCCESS) {
  220. return status;
  221. }
  222. }
  223. return status;
  224. }
  225. Status TransDataToPredictRequest(const json &message_info, PredictRequest *const request) {
  226. Status status = SUCCESS;
  227. auto tensors = message_info.find(HTTP_DATA);
  228. if (tensors == message_info.end()) {
  229. ERROR_INFER_STATUS(status, INVALID_INPUTS, "http message do not have data type");
  230. return status;
  231. }
  232. if (!tensors->is_array()) {
  233. ERROR_INFER_STATUS(status, INVALID_INPUTS, "the input tensor list is not array");
  234. return status;
  235. }
  236. auto const &json_shape = GetJsonArrayShape(*tensors);
  237. if (json_shape.size() != 2) { // 2 is data format list deep
  238. status = INFER_STATUS(INVALID_INPUTS)
  239. << "the data format request is constructed illegally, expected list nesting depth 2, given "
  240. << json_shape.size();
  241. MSI_LOG_ERROR << status.StatusMessage();
  242. return status;
  243. }
  244. if (tensors->size() != static_cast<size_t>(request->data_size())) {
  245. status = INFER_STATUS(INVALID_INPUTS)
  246. << "model input count not match, model required " << request->data_size() << ", given " << tensors->size();
  247. MSI_LOG_ERROR << status.StatusMessage();
  248. return status;
  249. }
  250. for (size_t i = 0; i < tensors->size(); i++) {
  251. const auto &tensor = tensors->at(i);
  252. ServingTensor request_tensor(*(request->mutable_data(i)));
  253. auto iter = infer_type2_http_type.find(request_tensor.data_type());
  254. if (iter == infer_type2_http_type.end()) {
  255. ERROR_INFER_STATUS(status, FAILED, "the model input type is not supported right now");
  256. return status;
  257. }
  258. HTTP_DATA_TYPE type = iter->second;
  259. if (!tensor.is_array()) {
  260. ERROR_INFER_STATUS(status, INVALID_INPUTS, "the tensor is constructed illegally");
  261. return status;
  262. }
  263. if (tensor.empty()) {
  264. ERROR_INFER_STATUS(status, INVALID_INPUTS, "the input tensor is null");
  265. return status;
  266. }
  267. if (tensor.size() != static_cast<size_t>(request_tensor.ElementNum())) {
  268. status = INFER_STATUS(INVALID_INPUTS) << "input " << i << " element count not match, model required "
  269. << request_tensor.ElementNum() << ", given " << tensor.size();
  270. MSI_LOG_ERROR << status.StatusMessage();
  271. return status;
  272. }
  273. status = GetDataFromJson(tensor, &request_tensor, 0, type);
  274. if (status != SUCCESS) {
  275. return status;
  276. }
  277. }
  278. return SUCCESS;
  279. }
  280. Status TransTensorToPredictRequest(const json &message_info, PredictRequest *const request) {
  281. Status status(SUCCESS);
  282. auto tensors = message_info.find(HTTP_TENSOR);
  283. if (tensors == message_info.end()) {
  284. ERROR_INFER_STATUS(status, INVALID_INPUTS, "http message do not have tensor type");
  285. return status;
  286. }
  287. if (!tensors->is_array()) {
  288. ERROR_INFER_STATUS(status, INVALID_INPUTS, "the input tensor list is not array");
  289. return status;
  290. }
  291. if (tensors->size() != static_cast<size_t>(request->data_size())) {
  292. status =
  293. INFER_STATUS(INVALID_INPUTS)
  294. << "model input count not match or json tensor request is constructed illegally, model input count required "
  295. << request->data_size() << ", given " << tensors->size();
  296. MSI_LOG_ERROR << status.StatusMessage();
  297. return status;
  298. }
  299. for (size_t i = 0; i < tensors->size(); i++) {
  300. const auto &tensor = tensors->at(i);
  301. ServingTensor request_tensor(*(request->mutable_data(i)));
  302. auto iter = infer_type2_http_type.find(request_tensor.data_type());
  303. if (iter == infer_type2_http_type.end()) {
  304. ERROR_INFER_STATUS(status, FAILED, "the model input type is not supported right now");
  305. return status;
  306. }
  307. HTTP_DATA_TYPE type = iter->second;
  308. // check data shape
  309. auto const &json_shape = GetJsonArrayShape(tensor);
  310. auto is_scalar_shape = [](const std::vector<int64_t> &shape) {
  311. return shape.empty() || (shape.size() == 1 && shape[0] == 1);
  312. };
  313. if (is_scalar_shape(request_tensor.shape())) {
  314. return GetScalarDataFromJson(tensor, &request_tensor, type);
  315. } else {
  316. if (json_shape != request_tensor.shape()) { // data shape not match
  317. status = INFER_STATUS(INVALID_INPUTS) << "input " << i << " shape is invalid, expected "
  318. << request_tensor.shape() << ", given " << json_shape;
  319. MSI_LOG_ERROR << status.StatusMessage();
  320. return status;
  321. }
  322. size_t depth = 0;
  323. size_t data_index = 0;
  324. status = RecusiveGetTensor(tensor, depth, &request_tensor, data_index, type);
  325. if (status != SUCCESS) {
  326. MSI_LOG_ERROR << "Transfer tensor to predict request failed";
  327. return status;
  328. }
  329. }
  330. }
  331. return status;
  332. }
  333. Status TransHTTPMsgToPredictRequest(struct evhttp_request *const http_request, PredictRequest *const request,
  334. HTTP_TYPE *const type) {
  335. Status status = CheckRequestValid(http_request);
  336. if (status != SUCCESS) {
  337. return status;
  338. }
  339. std::string post_message;
  340. status = GetPostMessage(http_request, &post_message);
  341. if (status != SUCCESS) {
  342. return status;
  343. }
  344. // get model required shape
  345. std::vector<inference::InferTensor> tensor_list;
  346. status = Session::Instance().GetModelInputsInfo(tensor_list);
  347. if (status != SUCCESS) {
  348. ERROR_INFER_STATUS(status, FAILED, "get model inputs info failed");
  349. return status;
  350. }
  351. for (auto &item : tensor_list) {
  352. auto input = request->add_data();
  353. ServingTensor tensor(*input);
  354. tensor.set_shape(item.shape());
  355. tensor.set_data_type(item.data_type());
  356. int64_t element_num = tensor.ElementNum();
  357. int64_t data_type_size = tensor.GetTypeSize(tensor.data_type());
  358. if (element_num <= 0 || INT64_MAX / element_num < data_type_size) {
  359. ERROR_INFER_STATUS(status, FAILED, "model shape invalid");
  360. return status;
  361. }
  362. tensor.resize_data(element_num * data_type_size);
  363. }
  364. MSI_TIME_STAMP_START(ParseJson)
  365. json message_info;
  366. try {
  367. message_info = nlohmann::json::parse(post_message);
  368. } catch (nlohmann::json::exception &e) {
  369. std::string json_exception = e.what();
  370. std::string error_message = "Illegal JSON format." + json_exception;
  371. ERROR_INFER_STATUS(status, INVALID_INPUTS, error_message);
  372. return status;
  373. }
  374. MSI_TIME_STAMP_END(ParseJson)
  375. status = CheckMessageValid(message_info, type);
  376. if (status != SUCCESS) {
  377. return status;
  378. }
  379. switch (*type) {
  380. case TYPE_DATA:
  381. status = TransDataToPredictRequest(message_info, request);
  382. break;
  383. case TYPE_TENSOR:
  384. status = TransTensorToPredictRequest(message_info, request);
  385. break;
  386. default:
  387. ERROR_INFER_STATUS(status, INVALID_INPUTS, "http message must have only one type of (data, tensor)");
  388. return status;
  389. }
  390. return status;
  391. }
  392. Status GetJsonFromTensor(const ms_serving::Tensor &tensor, int len, int *const pos, json *const out_json) {
  393. Status status(SUCCESS);
  394. switch (tensor.tensor_type()) {
  395. case ms_serving::MS_INT32: {
  396. auto data = reinterpret_cast<const int *>(tensor.data().data()) + *pos;
  397. std::vector<int32_t> result_tensor(len);
  398. memcpy_s(result_tensor.data(), result_tensor.size() * sizeof(int32_t), data, len * sizeof(int32_t));
  399. *out_json = std::move(result_tensor);
  400. *pos += len;
  401. break;
  402. }
  403. case ms_serving::MS_FLOAT32: {
  404. auto data = reinterpret_cast<const float *>(tensor.data().data()) + *pos;
  405. std::vector<float> result_tensor(len);
  406. (void)memcpy_s(result_tensor.data(), result_tensor.size() * sizeof(float), data, len * sizeof(float));
  407. *out_json = std::move(result_tensor);
  408. *pos += len;
  409. break;
  410. }
  411. default:
  412. MSI_LOG(ERROR) << "the result type is not supported in restful api, type is " << tensor.tensor_type();
  413. ERROR_INFER_STATUS(status, FAILED, "reply have unsupported type");
  414. }
  415. return status;
  416. }
  417. Status TransPredictReplyToData(const PredictReply &reply, json *const out_json) {
  418. Status status(SUCCESS);
  419. for (int i = 0; i < reply.result_size(); i++) {
  420. (*out_json)["data"].push_back(json());
  421. json &tensor_json = (*out_json)["data"].back();
  422. int num = 1;
  423. for (auto j = 0; j < reply.result(i).tensor_shape().dims_size(); j++) {
  424. num *= reply.result(i).tensor_shape().dims(j);
  425. }
  426. int pos = 0;
  427. status = GetJsonFromTensor(reply.result(i), num, &pos, &tensor_json);
  428. if (status != SUCCESS) {
  429. return status;
  430. }
  431. }
  432. return status;
  433. }
  434. Status RecusiveGetJson(const ms_serving::Tensor &tensor, int depth, int *const pos, json *const out_json) {
  435. Status status(SUCCESS);
  436. if (depth >= 10) {
  437. ERROR_INFER_STATUS(status, FAILED, "result tensor shape dims is larger than 10");
  438. return status;
  439. }
  440. if (depth == tensor.tensor_shape().dims_size() - 1) {
  441. status = GetJsonFromTensor(tensor, tensor.tensor_shape().dims(depth), pos, out_json);
  442. if (status != SUCCESS) {
  443. return status;
  444. }
  445. } else {
  446. for (int i = 0; i < tensor.tensor_shape().dims(depth); i++) {
  447. out_json->push_back(json());
  448. json &tensor_json = out_json->back();
  449. status = RecusiveGetJson(tensor, depth + 1, pos, &tensor_json);
  450. if (status != SUCCESS) {
  451. return status;
  452. }
  453. }
  454. }
  455. return status;
  456. }
  457. Status TransPredictReplyToTensor(const PredictReply &reply, json *const out_json) {
  458. Status status(SUCCESS);
  459. for (int i = 0; i < reply.result_size(); i++) {
  460. (*out_json)["tensor"].push_back(json());
  461. json &tensor_json = (*out_json)["tensor"].back();
  462. int pos = 0;
  463. status = RecusiveGetJson(reply.result(i), 0, &pos, &tensor_json);
  464. if (status != SUCCESS) {
  465. return status;
  466. }
  467. }
  468. return status;
  469. }
  470. Status TransPredictReplyToHTTPMsg(const PredictReply &reply, const HTTP_TYPE &type, struct evbuffer *const buf) {
  471. Status status(SUCCESS);
  472. json out_json;
  473. switch (type) {
  474. case TYPE_DATA:
  475. status = TransPredictReplyToData(reply, &out_json);
  476. break;
  477. case TYPE_TENSOR:
  478. status = TransPredictReplyToTensor(reply, &out_json);
  479. break;
  480. default:
  481. ERROR_INFER_STATUS(status, FAILED, "http message must have only one type of (data, tensor)");
  482. return status;
  483. }
  484. const std::string &out_str = out_json.dump();
  485. evbuffer_add(buf, out_str.data(), out_str.size());
  486. return status;
  487. }
  488. Status HttpHandleMsgDetail(struct evhttp_request *const req, void *const arg, struct evbuffer *const retbuff) {
  489. PredictRequest request;
  490. PredictReply reply;
  491. HTTP_TYPE type;
  492. MSI_TIME_STAMP_START(ParseRequest)
  493. auto status = TransHTTPMsgToPredictRequest(req, &request, &type);
  494. MSI_TIME_STAMP_END(ParseRequest)
  495. if (status != SUCCESS) {
  496. MSI_LOG(ERROR) << "restful trans to request failed";
  497. return status;
  498. }
  499. MSI_TIME_STAMP_START(Predict)
  500. status = Session::Instance().Predict(request, reply);
  501. MSI_TIME_STAMP_END(Predict)
  502. if (status != SUCCESS) {
  503. MSI_LOG(ERROR) << "restful predict failed";
  504. return status;
  505. }
  506. MSI_TIME_STAMP_START(CreateReplyJson)
  507. status = TransPredictReplyToHTTPMsg(reply, type, retbuff);
  508. MSI_TIME_STAMP_END(CreateReplyJson)
  509. if (status != SUCCESS) {
  510. MSI_LOG(ERROR) << "restful trans to reply failed";
  511. return status;
  512. }
  513. return SUCCESS;
  514. }
  515. void http_handler_msg(struct evhttp_request *const req, void *const arg) {
  516. MSI_TIME_STAMP_START(TotalRestfulPredict)
  517. struct evbuffer *retbuff = evbuffer_new();
  518. if (retbuff == nullptr) {
  519. MSI_LOG_ERROR << "Create event buffer failed";
  520. return;
  521. }
  522. auto status = HttpHandleMsgDetail(req, arg, retbuff);
  523. if (status != SUCCESS) {
  524. ErrorMessage(req, status);
  525. evbuffer_free(retbuff);
  526. return;
  527. }
  528. MSI_TIME_STAMP_START(ReplyJson)
  529. evhttp_send_reply(req, HTTP_OK, "Client", retbuff);
  530. MSI_TIME_STAMP_END(ReplyJson)
  531. evbuffer_free(retbuff);
  532. MSI_TIME_STAMP_END(TotalRestfulPredict)
  533. }
  534. } // namespace serving
  535. } // namespace mindspore