Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
bdef0f62
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
bdef0f62
编写于
8月 26, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
8月 26, 2020
浏览文件
操作
浏览文件
下载
差异文件
!5151 serving RESTful, disable http port reuse, update error msg output to user
Merge pull request !5151 from 徐永飞/master
上级
b2cff284
2c0ded78
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
93 addition
and
26 deletion
+93
-26
serving/core/server.cc
serving/core/server.cc
+87
-20
serving/core/util/option_parser.cc
serving/core/util/option_parser.cc
+6
-6
未找到文件。
serving/core/server.cc
浏览文件 @
bdef0f62
...
...
@@ -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
;
...
...
serving/core/util/option_parser.cc
浏览文件 @
bdef0f62
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录