Browse Source

!179 fix async resetful

From: @zhangyinxia
Reviewed-by: @xu-yfei,@linqingke
Signed-off-by: @xu-yfei
tags/v1.2.0
mindspore-ci-bot Gitee 5 years ago
parent
commit
fa4cda9397
7 changed files with 32 additions and 51 deletions
  1. +0
    -9
      mindspore_serving/ccsrc/master/dispacther.cc
  2. +0
    -1
      mindspore_serving/ccsrc/master/dispacther.h
  3. +4
    -3
      mindspore_serving/ccsrc/master/restful/http_handle.cc
  4. +1
    -1
      mindspore_serving/ccsrc/master/restful/http_handle.h
  5. +22
    -22
      mindspore_serving/ccsrc/master/restful/http_process.cc
  6. +4
    -1
      mindspore_serving/ccsrc/master/restful/http_process.h
  7. +1
    -14
      mindspore_serving/ccsrc/master/restful/restful_server.cc

+ 0
- 9
mindspore_serving/ccsrc/master/dispacther.cc View File

@@ -55,15 +55,6 @@ DispatcherWorkerContext Dispatcher::GetWorkSession(const RequestSpec &request_sp
return context;
}

void Dispatcher::Dispatch(const proto::PredictRequest &request, proto::PredictReply *reply) {
MSI_EXCEPTION_IF_NULL(reply);
auto promise = std::make_shared<std::promise<void>>();
auto future = promise->get_future();
PredictOnFinish on_finish = [promise]() { promise->set_value(); };
DispatchAsync(request, reply, on_finish);
future.get(); // wait callback finish
}

void Dispatcher::DispatchAsync(const proto::PredictRequest &request, proto::PredictReply *reply,
PredictOnFinish on_finish) {
MSI_EXCEPTION_IF_NULL(reply);


+ 0
- 1
mindspore_serving/ccsrc/master/dispacther.h View File

@@ -40,7 +40,6 @@ class MS_API Dispatcher {
public:
Dispatcher();
~Dispatcher();
void Dispatch(const proto::PredictRequest &request, proto::PredictReply *reply);
void DispatchAsync(const proto::PredictRequest &request, proto::PredictReply *reply, PredictOnFinish on_finish);

Status RegisterServable(const proto::RegisterRequest &request, proto::RegisterReply *reply);


+ 4
- 3
mindspore_serving/ccsrc/master/restful/http_handle.cc View File

@@ -133,11 +133,12 @@ size_t GetTailEqualSize(const std::string &str) {
return count;
}

Status HandleRestfulRequest(const std::shared_ptr<RestfulRequest> &restful_request, json *const out_json) {
Status HandleRestfulRequest(const std::shared_ptr<RestfulRequest> &restful_request) {
Status status(SUCCESS);
std::shared_ptr<Dispatcher> dispatcher_ = Server::Instance().GetDispatcher();
RestfulService restful_service(dispatcher_);
status = restful_service.RunRestful(restful_request, out_json);
auto restful_service = std::make_shared<RestfulService>(dispatcher_);
status = restful_service->RunRestful(restful_request, restful_service);

return status;
}
} // namespace serving


+ 1
- 1
mindspore_serving/ccsrc/master/restful/http_handle.h View File

@@ -25,7 +25,7 @@
using nlohmann::json;
namespace mindspore {
namespace serving {
Status HandleRestfulRequest(const std::shared_ptr<RestfulRequest> &restful_request, json *const out_json);
Status HandleRestfulRequest(const std::shared_ptr<RestfulRequest> &restful_request);

size_t Base64Encode(const uint8_t *input, size_t length, uint8_t *output);
size_t Base64Decode(const uint8_t *target, size_t target_length, uint8_t *origin);


+ 22
- 22
mindspore_serving/ccsrc/master/restful/http_process.cc View File

@@ -676,35 +676,35 @@ Status RestfulService::GetScalarData(const json &js, size_t index, bool is_bytes

return status;
}

// 2.main
Status RestfulService::RunRestful(const std::shared_ptr<RestfulRequest> &restful_request, json *const out_json) {
PredictRequest request;
PredictReply reply;

MSI_TIME_STAMP_START(ParseRequest)
auto status = ParseRequest(restful_request, &request);
MSI_TIME_STAMP_END(ParseRequest)
Status RestfulService::RunRestful(const std::shared_ptr<RestfulRequest> &restful_request,
const std::shared_ptr<RestfulService> &restful_service) {
MSI_TIME_STAMP_START(RunRestful)
auto status = ParseRequest(restful_request, &request_);
if (status != SUCCESS) {
std::string error_msg = status.StatusMessage();
std::string msg = "Parser request failed, " + error_msg;
nlohmann::json js;
js["error_msg"] = msg;
restful_request->RestfulReplay(js.dump());
status = msg;
return status;
}

MSI_TIME_STAMP_START(Predict)
dispatcher_->Dispatch(request, &reply);
MSI_TIME_STAMP_END(Predict)

MSI_TIME_STAMP_START(CreateReplyJson)
status = ParseReply(reply, out_json);
MSI_TIME_STAMP_END(CreateReplyJson)
if (status != SUCCESS) {
std::string error_msg = status.StatusMessage();
std::string msg = "Parse reply failed, " + error_msg;
status = msg;
return status;
}
auto callback = [restful_service, restful_request, time_start_RunRestful]() {
nlohmann::json predict_json;
auto status = restful_service->ParseReply(restful_service->reply_, &predict_json);
if (status != SUCCESS) {
std::string error_msg = status.StatusMessage();
std::string msg = "Parse reply failed, " + error_msg;
nlohmann::json js;
js["error_msg"] = msg;
restful_request->RestfulReplay(js.dump());
} else {
restful_request->RestfulReplay(predict_json.dump());
}
MSI_TIME_STAMP_END(RunRestful)
};
dispatcher_->DispatchAsync(request_, &reply_, callback);
return SUCCESS;
}



+ 4
- 1
mindspore_serving/ccsrc/master/restful/http_process.h View File

@@ -45,7 +45,8 @@ class RestfulService {
public:
explicit RestfulService(const std::shared_ptr<Dispatcher> &dispatcher) : dispatcher_(dispatcher) {}
~RestfulService() = default;
Status RunRestful(const std::shared_ptr<RestfulRequest> &restful_request, json *const out_json);
Status RunRestful(const std::shared_ptr<RestfulRequest> &restful_request,
const std::shared_ptr<RestfulService> &restful_service);

private:
Status CheckObjTypeMatchShape(DataType data_type, const std::vector<int64_t> &shape);
@@ -104,6 +105,8 @@ class RestfulService {
int64_t instances_nums_{0};
std::shared_ptr<Dispatcher> dispatcher_;
std::vector<std::string> request_type_list_ = {kInstancesRequest};
proto::PredictRequest request_;
proto::PredictReply reply_;
};

} // namespace serving


+ 1
- 14
mindspore_serving/ccsrc/master/restful/restful_server.cc View File

@@ -22,20 +22,7 @@
namespace mindspore::serving {

Status RestfulServer::Run(const std::shared_ptr<RestfulRequest> &restful_request) {
Status status(SUCCESS);
nlohmann::json predict_json;
std::string predict_str;
status = HandleRestfulRequest(restful_request, &predict_json);
if (status != SUCCESS) {
predict_str = status.StatusMessage();
nlohmann::json js;
js["error_msg"] = predict_str;
predict_str = js.dump();
} else {
predict_str = predict_json.dump();
}
restful_request->RestfulReplay(predict_str);
return status;
return HandleRestfulRequest(restful_request);
}

void RestfulServer::Committer(const std::shared_ptr<RestfulRequest> &restful_request) {


Loading…
Cancel
Save